format_wav.c 14 KB

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