format_wav.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. size = ltohl(size);
  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. data = ltohl(data);
  169. if (memcmp(&buf, "fmt ", 4) == 0) {
  170. if (check_header_fmt(f, data, hz))
  171. return -1;
  172. continue;
  173. }
  174. if(memcmp(buf, "data", 4) == 0 )
  175. break;
  176. ast_log(LOG_DEBUG, "Skipping unknown block '%.4s'\n", buf);
  177. if (fseek(f,data,SEEK_CUR) == -1 ) {
  178. ast_log(LOG_WARNING, "Failed to skip '%.4s' block: %d\n", buf, data);
  179. return -1;
  180. }
  181. }
  182. #if 0
  183. curpos = lseek(fd, 0, SEEK_CUR);
  184. truelength = lseek(fd, 0, SEEK_END);
  185. lseek(fd, curpos, SEEK_SET);
  186. truelength -= curpos;
  187. #endif
  188. return data;
  189. }
  190. static int update_header(FILE *f)
  191. {
  192. off_t cur,end;
  193. int datalen,filelen,bytes;
  194. cur = ftello(f);
  195. fseek(f, 0, SEEK_END);
  196. end = ftello(f);
  197. /* data starts 44 bytes in */
  198. bytes = end - 44;
  199. datalen = htoll(bytes);
  200. /* chunk size is bytes of data plus 36 bytes of header */
  201. filelen = htoll(36 + bytes);
  202. if (cur < 0) {
  203. ast_log(LOG_WARNING, "Unable to find our position\n");
  204. return -1;
  205. }
  206. if (fseek(f, 4, SEEK_SET)) {
  207. ast_log(LOG_WARNING, "Unable to set our position\n");
  208. return -1;
  209. }
  210. if (fwrite(&filelen, 1, 4, f) != 4) {
  211. ast_log(LOG_WARNING, "Unable to set write file size\n");
  212. return -1;
  213. }
  214. if (fseek(f, 40, SEEK_SET)) {
  215. ast_log(LOG_WARNING, "Unable to set our position\n");
  216. return -1;
  217. }
  218. if (fwrite(&datalen, 1, 4, f) != 4) {
  219. ast_log(LOG_WARNING, "Unable to set write datalen\n");
  220. return -1;
  221. }
  222. if (fseeko(f, cur, SEEK_SET)) {
  223. ast_log(LOG_WARNING, "Unable to return to position\n");
  224. return -1;
  225. }
  226. return 0;
  227. }
  228. static int write_header(FILE *f, int writehz)
  229. {
  230. unsigned int hz;
  231. unsigned int bhz;
  232. unsigned int hs = htoll(16);
  233. unsigned short fmt = htols(1);
  234. unsigned short chans = htols(1);
  235. unsigned short bysam = htols(2);
  236. unsigned short bisam = htols(16);
  237. unsigned int size = htoll(0);
  238. if (writehz == 16000) {
  239. hz = htoll(16000);
  240. bhz = htoll(32000);
  241. } else {
  242. hz = htoll(8000);
  243. bhz = htoll(16000);
  244. }
  245. /* Write a wav header, ignoring sizes which will be filled in later */
  246. fseek(f,0,SEEK_SET);
  247. if (fwrite("RIFF", 1, 4, f) != 4) {
  248. ast_log(LOG_WARNING, "Unable to write header\n");
  249. return -1;
  250. }
  251. if (fwrite(&size, 1, 4, f) != 4) {
  252. ast_log(LOG_WARNING, "Unable to write header\n");
  253. return -1;
  254. }
  255. if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
  256. ast_log(LOG_WARNING, "Unable to write header\n");
  257. return -1;
  258. }
  259. if (fwrite(&hs, 1, 4, f) != 4) {
  260. ast_log(LOG_WARNING, "Unable to write header\n");
  261. return -1;
  262. }
  263. if (fwrite(&fmt, 1, 2, f) != 2) {
  264. ast_log(LOG_WARNING, "Unable to write header\n");
  265. return -1;
  266. }
  267. if (fwrite(&chans, 1, 2, f) != 2) {
  268. ast_log(LOG_WARNING, "Unable to write header\n");
  269. return -1;
  270. }
  271. if (fwrite(&hz, 1, 4, f) != 4) {
  272. ast_log(LOG_WARNING, "Unable to write header\n");
  273. return -1;
  274. }
  275. if (fwrite(&bhz, 1, 4, f) != 4) {
  276. ast_log(LOG_WARNING, "Unable to write header\n");
  277. return -1;
  278. }
  279. if (fwrite(&bysam, 1, 2, f) != 2) {
  280. ast_log(LOG_WARNING, "Unable to write header\n");
  281. return -1;
  282. }
  283. if (fwrite(&bisam, 1, 2, f) != 2) {
  284. ast_log(LOG_WARNING, "Unable to write header\n");
  285. return -1;
  286. }
  287. if (fwrite("data", 1, 4, f) != 4) {
  288. ast_log(LOG_WARNING, "Unable to write header\n");
  289. return -1;
  290. }
  291. if (fwrite(&size, 1, 4, f) != 4) {
  292. ast_log(LOG_WARNING, "Unable to write header\n");
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. static int wav_open(struct ast_filestream *s)
  298. {
  299. /* We don't have any header to read or anything really, but
  300. if we did, it would go here. We also might want to check
  301. and be sure it's a valid file. */
  302. struct wav_desc *tmp = (struct wav_desc *)s->_private;
  303. if ((tmp->maxlen = check_header(s->f, ast_format_get_sample_rate(s->fmt->format))) < 0)
  304. return -1;
  305. return 0;
  306. }
  307. static int wav_rewrite(struct ast_filestream *s, const char *comment)
  308. {
  309. /* We don't have any header to read or anything really, but
  310. if we did, it would go here. We also might want to check
  311. and be sure it's a valid file. */
  312. struct wav_desc *tmp = (struct wav_desc *)s->_private;
  313. tmp->hz = ast_format_get_sample_rate(s->fmt->format);
  314. if (write_header(s->f,tmp->hz))
  315. return -1;
  316. return 0;
  317. }
  318. static void wav_close(struct ast_filestream *s)
  319. {
  320. char zero = 0;
  321. struct wav_desc *fs = (struct wav_desc *)s->_private;
  322. if (s->mode == O_RDONLY) {
  323. return;
  324. }
  325. if (s->filename) {
  326. update_header(s->f);
  327. }
  328. /* Pad to even length */
  329. if (fs->bytes & 0x1) {
  330. if (!fwrite(&zero, 1, 1, s->f)) {
  331. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  332. }
  333. }
  334. }
  335. static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
  336. {
  337. int res;
  338. int samples; /* actual samples read */
  339. #if __BYTE_ORDER == __BIG_ENDIAN
  340. int x;
  341. short *tmp;
  342. #endif
  343. int bytes;
  344. off_t here;
  345. /* Send a frame from the file to the appropriate channel */
  346. struct wav_desc *fs = (struct wav_desc *)s->_private;
  347. bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
  348. here = ftello(s->f);
  349. if (fs->maxlen - here < bytes) /* truncate if necessary */
  350. bytes = fs->maxlen - here;
  351. if (bytes < 0)
  352. bytes = 0;
  353. /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
  354. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
  355. if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
  356. if (res)
  357. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  358. return NULL;
  359. }
  360. s->fr.datalen = res;
  361. s->fr.samples = samples = res / 2;
  362. #if __BYTE_ORDER == __BIG_ENDIAN
  363. tmp = (short *)(s->fr.data.ptr);
  364. /* file format is little endian so we need to swap */
  365. for( x = 0; x < samples; x++)
  366. tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
  367. #endif
  368. *whennext = samples;
  369. return &s->fr;
  370. }
  371. static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
  372. {
  373. #if __BYTE_ORDER == __BIG_ENDIAN
  374. int x;
  375. short tmp[16000], *tmpi;
  376. #endif
  377. struct wav_desc *s = (struct wav_desc *)fs->_private;
  378. int res;
  379. if (!f->datalen)
  380. return -1;
  381. #if __BYTE_ORDER == __BIG_ENDIAN
  382. /* swap and write */
  383. if (f->datalen > sizeof(tmp)) {
  384. ast_log(LOG_WARNING, "Data length is too long\n");
  385. return -1;
  386. }
  387. tmpi = f->data.ptr;
  388. for (x=0; x < f->datalen/2; x++)
  389. tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
  390. if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
  391. #else
  392. /* just write */
  393. if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen ) {
  394. #endif
  395. ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
  396. return -1;
  397. }
  398. s->bytes += f->datalen;
  399. return 0;
  400. }
  401. static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  402. {
  403. off_t min = WAV_HEADER_SIZE, max, cur, offset = 0, samples;
  404. samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
  405. if ((cur = ftello(fs->f)) < 0) {
  406. ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
  407. return -1;
  408. }
  409. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  410. ast_log(AST_LOG_WARNING, "Unable to seek to end of wav filestream %p: %s\n", fs, strerror(errno));
  411. return -1;
  412. }
  413. if ((max = ftello(fs->f)) < 0) {
  414. ast_log(AST_LOG_WARNING, "Unable to determine max position in wav filestream %p: %s\n", fs, strerror(errno));
  415. return -1;
  416. }
  417. if (whence == SEEK_SET)
  418. offset = samples + min;
  419. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  420. offset = samples + cur;
  421. else if (whence == SEEK_END)
  422. offset = max - samples;
  423. if (whence != SEEK_FORCECUR) {
  424. offset = (offset > max)?max:offset;
  425. }
  426. /* always protect the header space. */
  427. offset = (offset < min)?min:offset;
  428. return fseeko(fs->f, offset, SEEK_SET);
  429. }
  430. static int wav_trunc(struct ast_filestream *fs)
  431. {
  432. int fd;
  433. off_t cur;
  434. if ((fd = fileno(fs->f)) < 0) {
  435. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for wav filestream %p: %s\n", fs, strerror(errno));
  436. return -1;
  437. }
  438. if ((cur = ftello(fs->f)) < 0) {
  439. ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
  440. return -1;
  441. }
  442. /* Truncate file to current length */
  443. if (ftruncate(fd, cur)) {
  444. return -1;
  445. }
  446. return update_header(fs->f);
  447. }
  448. static off_t wav_tell(struct ast_filestream *fs)
  449. {
  450. off_t offset;
  451. offset = ftello(fs->f);
  452. /* subtract header size to get samples, then divide by 2 for 16 bit samples */
  453. return (offset - 44)/2;
  454. }
  455. static struct ast_format_def wav16_f = {
  456. .name = "wav16",
  457. .exts = "wav16",
  458. .open = wav_open,
  459. .rewrite = wav_rewrite,
  460. .write = wav_write,
  461. .seek = wav_seek,
  462. .trunc = wav_trunc,
  463. .tell = wav_tell,
  464. .read = wav_read,
  465. .close = wav_close,
  466. .buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
  467. .desc_size = sizeof(struct wav_desc),
  468. };
  469. static struct ast_format_def wav_f = {
  470. .name = "wav",
  471. .exts = "wav",
  472. .open = wav_open,
  473. .rewrite = wav_rewrite,
  474. .write = wav_write,
  475. .seek = wav_seek,
  476. .trunc = wav_trunc,
  477. .tell = wav_tell,
  478. .read = wav_read,
  479. .close = wav_close,
  480. .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
  481. .desc_size = sizeof(struct wav_desc),
  482. };
  483. static int load_module(void)
  484. {
  485. wav_f.format = ast_format_slin;
  486. wav16_f.format = ast_format_slin16;
  487. if (ast_format_def_register(&wav_f)
  488. || ast_format_def_register(&wav16_f))
  489. return AST_MODULE_LOAD_FAILURE;
  490. return AST_MODULE_LOAD_SUCCESS;
  491. }
  492. static int unload_module(void)
  493. {
  494. return ast_format_def_unregister(wav_f.name)
  495. || ast_format_def_unregister(wav16_f.name);
  496. }
  497. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
  498. .support_level = AST_MODULE_SUPPORT_CORE,
  499. .load = load_module,
  500. .unload = unload_module,
  501. .load_pri = AST_MODPRI_APP_DEPEND
  502. );