format_wav.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Work with WAV in the proprietary Microsoft format.
  21. * Microsoft WAV format (8000hz Signed Linear)
  22. * \arg File name extension: wav (lower case)
  23. * \ingroup formats
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/endian.h"
  33. /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
  34. /* Portions of the conversion code are by guido@sienanet.it */
  35. #define WAV_BUF_SIZE 320
  36. #define WAV_HEADER_SIZE 44
  37. struct wav_desc { /* format-specific parameters */
  38. int hz;
  39. int bytes;
  40. int lasttimeout;
  41. int maxlen;
  42. struct timeval last;
  43. };
  44. #define BLOCKSIZE 160
  45. #if __BYTE_ORDER == __LITTLE_ENDIAN
  46. #define htoll(b) (b)
  47. #define htols(b) (b)
  48. #define ltohl(b) (b)
  49. #define ltohs(b) (b)
  50. #else
  51. #if __BYTE_ORDER == __BIG_ENDIAN
  52. #define htoll(b) \
  53. (((((b) ) & 0xFF) << 24) | \
  54. ((( (b) >> 8) & 0xFF) << 16) | \
  55. ((( (b) >> 16) & 0xFF) << 8) | \
  56. ((( (b) >> 24) & 0xFF) ))
  57. #define htols(b) \
  58. (((((b) ) & 0xFF) << 8) | \
  59. ((( (b) >> 8) & 0xFF) ))
  60. #define ltohl(b) htoll(b)
  61. #define ltohs(b) htols(b)
  62. #else
  63. #error "Endianess not defined"
  64. #endif
  65. #endif
  66. static int check_header_fmt(FILE *f, int hsize, int hz)
  67. {
  68. short format, chans, bysam, bisam;
  69. int bysec;
  70. int freq;
  71. if (hsize < 16) {
  72. ast_log(LOG_WARNING, "Unexpected header size %d\n", hsize);
  73. return -1;
  74. }
  75. if (fread(&format, 1, 2, f) != 2) {
  76. ast_log(LOG_WARNING, "Read failed (format)\n");
  77. return -1;
  78. }
  79. if (ltohs(format) != 1) {
  80. ast_log(LOG_WARNING, "Not a supported wav file format (%d). Only PCM encoded, 16 bit, mono, 8kHz files are supported with a lowercase '.wav' extension.\n", ltohs(format));
  81. return -1;
  82. }
  83. if (fread(&chans, 1, 2, f) != 2) {
  84. ast_log(LOG_WARNING, "Read failed (format)\n");
  85. return -1;
  86. }
  87. if (ltohs(chans) != 1) {
  88. ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
  89. return -1;
  90. }
  91. if (fread(&freq, 1, 4, f) != 4) {
  92. ast_log(LOG_WARNING, "Read failed (freq)\n");
  93. return -1;
  94. }
  95. if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
  96. ((ltohl(freq) == 8000) && (hz != 8000)) ||
  97. ((ltohl(freq) == 16000) && (hz != 16000))) {
  98. ast_log(LOG_WARNING, "Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
  99. return -1;
  100. }
  101. /* Ignore the byte frequency */
  102. if (fread(&bysec, 1, 4, f) != 4) {
  103. ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
  104. return -1;
  105. }
  106. /* Check bytes per sample */
  107. if (fread(&bysam, 1, 2, f) != 2) {
  108. ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
  109. return -1;
  110. }
  111. if (ltohs(bysam) != 2) {
  112. ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
  113. return -1;
  114. }
  115. if (fread(&bisam, 1, 2, f) != 2) {
  116. ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
  117. return -1;
  118. }
  119. /* Skip any additional header */
  120. if (fseek(f,hsize-16,SEEK_CUR) == -1 ) {
  121. ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", hsize-16 );
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. static int check_header(FILE *f, int hz)
  127. {
  128. int type, size, formtype;
  129. int data;
  130. if (fread(&type, 1, 4, f) != 4) {
  131. ast_log(LOG_WARNING, "Read failed (type)\n");
  132. return -1;
  133. }
  134. if (fread(&size, 1, 4, f) != 4) {
  135. ast_log(LOG_WARNING, "Read failed (size)\n");
  136. return -1;
  137. }
  138. #if __BYTE_ORDER == __BIG_ENDIAN
  139. size = ltohl(size);
  140. #endif
  141. if (fread(&formtype, 1, 4, f) != 4) {
  142. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  143. return -1;
  144. }
  145. if (memcmp(&type, "RIFF", 4)) {
  146. ast_log(LOG_WARNING, "Does not begin with RIFF\n");
  147. return -1;
  148. }
  149. if (memcmp(&formtype, "WAVE", 4)) {
  150. ast_log(LOG_WARNING, "Does not contain WAVE\n");
  151. return -1;
  152. }
  153. /* Skip any facts and get the first data block */
  154. for(;;)
  155. {
  156. char buf[4];
  157. /* Begin data chunk */
  158. if (fread(&buf, 1, 4, f) != 4) {
  159. ast_log(LOG_WARNING, "Read failed (block header format)\n");
  160. return -1;
  161. }
  162. /* Data has the actual length of data in it */
  163. if (fread(&data, 1, 4, f) != 4) {
  164. ast_log(LOG_WARNING, "Read failed (block '%.4s' header length)\n", buf);
  165. return -1;
  166. }
  167. #if __BYTE_ORDER == __BIG_ENDIAN
  168. data = ltohl(data);
  169. #endif
  170. if (memcmp(&buf, "fmt ", 4) == 0) {
  171. if (check_header_fmt(f, data, hz))
  172. return -1;
  173. continue;
  174. }
  175. if(memcmp(buf, "data", 4) == 0 )
  176. break;
  177. ast_log(LOG_DEBUG, "Skipping unknown block '%.4s'\n", buf);
  178. if (fseek(f,data,SEEK_CUR) == -1 ) {
  179. ast_log(LOG_WARNING, "Failed to skip '%.4s' block: %d\n", buf, data);
  180. return -1;
  181. }
  182. }
  183. #if 0
  184. curpos = lseek(fd, 0, SEEK_CUR);
  185. truelength = lseek(fd, 0, SEEK_END);
  186. lseek(fd, curpos, SEEK_SET);
  187. truelength -= curpos;
  188. #endif
  189. return data;
  190. }
  191. static int update_header(FILE *f)
  192. {
  193. off_t cur,end;
  194. int datalen,filelen,bytes;
  195. cur = ftello(f);
  196. fseek(f, 0, SEEK_END);
  197. end = ftello(f);
  198. /* data starts 44 bytes in */
  199. bytes = end - 44;
  200. datalen = htoll(bytes);
  201. /* chunk size is bytes of data plus 36 bytes of header */
  202. filelen = htoll(36 + bytes);
  203. if (cur < 0) {
  204. ast_log(LOG_WARNING, "Unable to find our position\n");
  205. return -1;
  206. }
  207. if (fseek(f, 4, SEEK_SET)) {
  208. ast_log(LOG_WARNING, "Unable to set our position\n");
  209. return -1;
  210. }
  211. if (fwrite(&filelen, 1, 4, f) != 4) {
  212. ast_log(LOG_WARNING, "Unable to set write file size\n");
  213. return -1;
  214. }
  215. if (fseek(f, 40, SEEK_SET)) {
  216. ast_log(LOG_WARNING, "Unable to set our position\n");
  217. return -1;
  218. }
  219. if (fwrite(&datalen, 1, 4, f) != 4) {
  220. ast_log(LOG_WARNING, "Unable to set write datalen\n");
  221. return -1;
  222. }
  223. if (fseeko(f, cur, SEEK_SET)) {
  224. ast_log(LOG_WARNING, "Unable to return to position\n");
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. static int write_header(FILE *f, int writehz)
  230. {
  231. unsigned int hz;
  232. unsigned int bhz;
  233. unsigned int hs = htoll(16);
  234. unsigned short fmt = htols(1);
  235. unsigned short chans = htols(1);
  236. unsigned short bysam = htols(2);
  237. unsigned short bisam = htols(16);
  238. unsigned int size = htoll(0);
  239. if (writehz == 16000) {
  240. hz = htoll(16000);
  241. bhz = htoll(32000);
  242. } else {
  243. hz = htoll(8000);
  244. bhz = htoll(16000);
  245. }
  246. /* Write a wav header, ignoring sizes which will be filled in later */
  247. fseek(f,0,SEEK_SET);
  248. if (fwrite("RIFF", 1, 4, f) != 4) {
  249. ast_log(LOG_WARNING, "Unable to write header\n");
  250. return -1;
  251. }
  252. if (fwrite(&size, 1, 4, f) != 4) {
  253. ast_log(LOG_WARNING, "Unable to write header\n");
  254. return -1;
  255. }
  256. if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
  257. ast_log(LOG_WARNING, "Unable to write header\n");
  258. return -1;
  259. }
  260. if (fwrite(&hs, 1, 4, f) != 4) {
  261. ast_log(LOG_WARNING, "Unable to write header\n");
  262. return -1;
  263. }
  264. if (fwrite(&fmt, 1, 2, f) != 2) {
  265. ast_log(LOG_WARNING, "Unable to write header\n");
  266. return -1;
  267. }
  268. if (fwrite(&chans, 1, 2, f) != 2) {
  269. ast_log(LOG_WARNING, "Unable to write header\n");
  270. return -1;
  271. }
  272. if (fwrite(&hz, 1, 4, f) != 4) {
  273. ast_log(LOG_WARNING, "Unable to write header\n");
  274. return -1;
  275. }
  276. if (fwrite(&bhz, 1, 4, f) != 4) {
  277. ast_log(LOG_WARNING, "Unable to write header\n");
  278. return -1;
  279. }
  280. if (fwrite(&bysam, 1, 2, f) != 2) {
  281. ast_log(LOG_WARNING, "Unable to write header\n");
  282. return -1;
  283. }
  284. if (fwrite(&bisam, 1, 2, f) != 2) {
  285. ast_log(LOG_WARNING, "Unable to write header\n");
  286. return -1;
  287. }
  288. if (fwrite("data", 1, 4, f) != 4) {
  289. ast_log(LOG_WARNING, "Unable to write header\n");
  290. return -1;
  291. }
  292. if (fwrite(&size, 1, 4, f) != 4) {
  293. ast_log(LOG_WARNING, "Unable to write header\n");
  294. return -1;
  295. }
  296. return 0;
  297. }
  298. static int wav_open(struct ast_filestream *s)
  299. {
  300. /* We don't have any header to read or anything really, but
  301. if we did, it would go here. We also might want to check
  302. and be sure it's a valid file. */
  303. struct wav_desc *tmp = (struct wav_desc *)s->_private;
  304. if ((tmp->maxlen = check_header(s->f, (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0)
  305. return -1;
  306. return 0;
  307. }
  308. static int wav_rewrite(struct ast_filestream *s, const char *comment)
  309. {
  310. /* We don't have any header to read or anything really, but
  311. if we did, it would go here. We also might want to check
  312. and be sure it's a valid file. */
  313. struct wav_desc *tmp = (struct wav_desc *)s->_private;
  314. tmp->hz = (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000);
  315. if (write_header(s->f,tmp->hz))
  316. return -1;
  317. return 0;
  318. }
  319. static void wav_close(struct ast_filestream *s)
  320. {
  321. char zero = 0;
  322. struct wav_desc *fs = (struct wav_desc *)s->_private;
  323. if (s->mode == O_RDONLY) {
  324. return;
  325. }
  326. if (s->filename) {
  327. update_header(s->f);
  328. }
  329. /* Pad to even length */
  330. if (fs->bytes & 0x1) {
  331. if (!fwrite(&zero, 1, 1, s->f)) {
  332. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  333. }
  334. }
  335. }
  336. static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
  337. {
  338. int res;
  339. int samples; /* actual samples read */
  340. #if __BYTE_ORDER == __BIG_ENDIAN
  341. int x;
  342. short *tmp;
  343. #endif
  344. int bytes;
  345. off_t here;
  346. /* Send a frame from the file to the appropriate channel */
  347. struct wav_desc *fs = (struct wav_desc *)s->_private;
  348. bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
  349. here = ftello(s->f);
  350. if (fs->maxlen - here < bytes) /* truncate if necessary */
  351. bytes = fs->maxlen - here;
  352. if (bytes < 0)
  353. bytes = 0;
  354. /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
  355. s->fr.frametype = AST_FRAME_VOICE;
  356. ast_format_set(&s->fr.subclass.format, (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), 0);
  357. s->fr.mallocd = 0;
  358. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
  359. if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
  360. if (res)
  361. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  362. return NULL;
  363. }
  364. s->fr.datalen = res;
  365. s->fr.samples = samples = res / 2;
  366. #if __BYTE_ORDER == __BIG_ENDIAN
  367. tmp = (short *)(s->fr.data.ptr);
  368. /* file format is little endian so we need to swap */
  369. for( x = 0; x < samples; x++)
  370. tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
  371. #endif
  372. *whennext = samples;
  373. return &s->fr;
  374. }
  375. static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
  376. {
  377. #if __BYTE_ORDER == __BIG_ENDIAN
  378. int x;
  379. short tmp[16000], *tmpi;
  380. #endif
  381. struct wav_desc *s = (struct wav_desc *)fs->_private;
  382. int res;
  383. if (f->frametype != AST_FRAME_VOICE) {
  384. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  385. return -1;
  386. }
  387. if ((f->subclass.format.id != AST_FORMAT_SLINEAR) && (f->subclass.format.id != AST_FORMAT_SLINEAR16)) {
  388. ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(&f->subclass.format));
  389. return -1;
  390. }
  391. if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) {
  392. ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n");
  393. return -1;
  394. }
  395. if (!f->datalen)
  396. return -1;
  397. #if __BYTE_ORDER == __BIG_ENDIAN
  398. /* swap and write */
  399. if (f->datalen > sizeof(tmp)) {
  400. ast_log(LOG_WARNING, "Data length is too long\n");
  401. return -1;
  402. }
  403. tmpi = f->data.ptr;
  404. for (x=0; x < f->datalen/2; x++)
  405. tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
  406. if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
  407. #else
  408. /* just write */
  409. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen ) {
  410. #endif
  411. ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
  412. return -1;
  413. }
  414. s->bytes += f->datalen;
  415. return 0;
  416. }
  417. static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  418. {
  419. off_t min = WAV_HEADER_SIZE, max, cur, offset = 0, samples;
  420. samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
  421. if ((cur = ftello(fs->f)) < 0) {
  422. ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
  423. return -1;
  424. }
  425. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  426. ast_log(AST_LOG_WARNING, "Unable to seek to end of wav filestream %p: %s\n", fs, strerror(errno));
  427. return -1;
  428. }
  429. if ((max = ftello(fs->f)) < 0) {
  430. ast_log(AST_LOG_WARNING, "Unable to determine max position in wav filestream %p: %s\n", fs, strerror(errno));
  431. return -1;
  432. }
  433. if (whence == SEEK_SET) {
  434. offset = samples + min;
  435. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  436. offset = samples + cur;
  437. } else if (whence == SEEK_END) {
  438. offset = max - samples;
  439. }
  440. if (whence != SEEK_FORCECUR) {
  441. offset = (offset > max)?max:offset;
  442. }
  443. /* always protect the header space. */
  444. offset = (offset < min)?min:offset;
  445. return fseeko(fs->f, offset, SEEK_SET);
  446. }
  447. static int wav_trunc(struct ast_filestream *fs)
  448. {
  449. int fd;
  450. off_t cur;
  451. if ((fd = fileno(fs->f)) < 0) {
  452. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for wav filestream %p: %s\n", fs, strerror(errno));
  453. return -1;
  454. }
  455. if ((cur = ftello(fs->f)) < 0) {
  456. ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
  457. return -1;
  458. }
  459. /* Truncate file to current length */
  460. if (ftruncate(fd, cur)) {
  461. return -1;
  462. }
  463. return update_header(fs->f);
  464. }
  465. static off_t wav_tell(struct ast_filestream *fs)
  466. {
  467. off_t offset;
  468. offset = ftello(fs->f);
  469. /* subtract header size to get samples, then divide by 2 for 16 bit samples */
  470. return (offset - 44)/2;
  471. }
  472. static struct ast_format_def wav16_f = {
  473. .name = "wav16",
  474. .exts = "wav16",
  475. .open = wav_open,
  476. .rewrite = wav_rewrite,
  477. .write = wav_write,
  478. .seek = wav_seek,
  479. .trunc = wav_trunc,
  480. .tell = wav_tell,
  481. .read = wav_read,
  482. .close = wav_close,
  483. .buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
  484. .desc_size = sizeof(struct wav_desc),
  485. };
  486. static struct ast_format_def wav_f = {
  487. .name = "wav",
  488. .exts = "wav",
  489. .open = wav_open,
  490. .rewrite = wav_rewrite,
  491. .write = wav_write,
  492. .seek = wav_seek,
  493. .trunc = wav_trunc,
  494. .tell = wav_tell,
  495. .read = wav_read,
  496. .close = wav_close,
  497. .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
  498. .desc_size = sizeof(struct wav_desc),
  499. };
  500. static int load_module(void)
  501. {
  502. ast_format_set(&wav_f.format, AST_FORMAT_SLINEAR, 0);
  503. ast_format_set(&wav16_f.format, AST_FORMAT_SLINEAR16, 0);
  504. if (ast_format_def_register(&wav_f)
  505. || ast_format_def_register(&wav16_f))
  506. return AST_MODULE_LOAD_FAILURE;
  507. return AST_MODULE_LOAD_SUCCESS;
  508. }
  509. static int unload_module(void)
  510. {
  511. return ast_format_def_unregister(wav_f.name)
  512. || ast_format_def_unregister(wav16_f.name);
  513. }
  514. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
  515. .load = load_module,
  516. .unload = unload_module,
  517. .load_pri = AST_MODPRI_APP_DEPEND
  518. );