format_wav.c 14 KB

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