format_wav.c 14 KB

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