format_wav_gsm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 Save GSM in the proprietary Microsoft format.
  21. *
  22. * Microsoft WAV format (Proprietary GSM)
  23. * \arg File name extension: WAV,wav49 (Upper case WAV, lower case is another format)
  24. * This format can be played on Windows systems, used for
  25. * e-mail attachments mainly.
  26. * \ingroup formats
  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 "msgsm.h"
  34. /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
  35. /* Portions of the conversion code are by guido@sienanet.it */
  36. #define GSM_FRAME_SIZE 33
  37. #define MSGSM_FRAME_SIZE 65
  38. #define MSGSM_DATA_OFFSET 60 /* offset of data bytes */
  39. #define GSM_SAMPLES 160 /* samples in a GSM block */
  40. #define MSGSM_SAMPLES (2*GSM_SAMPLES) /* samples in an MSGSM block */
  41. /* begin binary data: */
  42. char msgsm_silence[] = /* 65 */
  43. {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
  44. ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
  45. ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
  46. ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
  47. ,0x92,0x24,0x49,0x92,0x00};
  48. /* end binary data. size = 65 bytes */
  49. struct wavg_desc {
  50. /* Believe it or not, we must decode/recode to account for the
  51. weird MS format */
  52. int secondhalf; /* Are we on the second half */
  53. };
  54. #if __BYTE_ORDER == __LITTLE_ENDIAN
  55. #define htoll(b) (b)
  56. #define htols(b) (b)
  57. #define ltohl(b) (b)
  58. #define ltohs(b) (b)
  59. #else
  60. #if __BYTE_ORDER == __BIG_ENDIAN
  61. #define htoll(b) \
  62. (((((b) ) & 0xFF) << 24) | \
  63. ((((b) >> 8) & 0xFF) << 16) | \
  64. ((((b) >> 16) & 0xFF) << 8) | \
  65. ((((b) >> 24) & 0xFF) ))
  66. #define htols(b) \
  67. (((((b) ) & 0xFF) << 8) | \
  68. ((((b) >> 8) & 0xFF) ))
  69. #define ltohl(b) htoll(b)
  70. #define ltohs(b) htols(b)
  71. #else
  72. #error "Endianess not defined"
  73. #endif
  74. #endif
  75. static int check_header(FILE *f)
  76. {
  77. int type, size, formtype;
  78. int fmt, hsize, fact;
  79. short format, chans;
  80. int freq;
  81. int data;
  82. if (fread(&type, 1, 4, f) != 4) {
  83. ast_log(LOG_WARNING, "Read failed (type)\n");
  84. return -1;
  85. }
  86. if (fread(&size, 1, 4, f) != 4) {
  87. ast_log(LOG_WARNING, "Read failed (size)\n");
  88. return -1;
  89. }
  90. size = ltohl(size);
  91. if (fread(&formtype, 1, 4, f) != 4) {
  92. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  93. return -1;
  94. }
  95. if (memcmp(&type, "RIFF", 4)) {
  96. ast_log(LOG_WARNING, "Does not begin with RIFF\n");
  97. return -1;
  98. }
  99. if (memcmp(&formtype, "WAVE", 4)) {
  100. ast_log(LOG_WARNING, "Does not contain WAVE\n");
  101. return -1;
  102. }
  103. if (fread(&fmt, 1, 4, f) != 4) {
  104. ast_log(LOG_WARNING, "Read failed (fmt)\n");
  105. return -1;
  106. }
  107. if (memcmp(&fmt, "fmt ", 4)) {
  108. ast_log(LOG_WARNING, "Does not say fmt\n");
  109. return -1;
  110. }
  111. if (fread(&hsize, 1, 4, f) != 4) {
  112. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  113. return -1;
  114. }
  115. if (ltohl(hsize) != 20) {
  116. ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
  117. return -1;
  118. }
  119. if (fread(&format, 1, 2, f) != 2) {
  120. ast_log(LOG_WARNING, "Read failed (format)\n");
  121. return -1;
  122. }
  123. if (ltohs(format) != 49) {
  124. ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
  125. return -1;
  126. }
  127. if (fread(&chans, 1, 2, f) != 2) {
  128. ast_log(LOG_WARNING, "Read failed (format)\n");
  129. return -1;
  130. }
  131. if (ltohs(chans) != 1) {
  132. ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
  133. return -1;
  134. }
  135. if (fread(&freq, 1, 4, f) != 4) {
  136. ast_log(LOG_WARNING, "Read failed (freq)\n");
  137. return -1;
  138. }
  139. if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
  140. ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
  141. return -1;
  142. }
  143. /* Ignore the byte frequency */
  144. if (fread(&freq, 1, 4, f) != 4) {
  145. ast_log(LOG_WARNING, "Read failed (X_1)\n");
  146. return -1;
  147. }
  148. /* Ignore the two weird fields */
  149. if (fread(&freq, 1, 4, f) != 4) {
  150. ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
  151. return -1;
  152. }
  153. /* Ignore the byte frequency */
  154. if (fread(&freq, 1, 4, f) != 4) {
  155. ast_log(LOG_WARNING, "Read failed (Y_1)\n");
  156. return -1;
  157. }
  158. /* Check for the word fact */
  159. if (fread(&fact, 1, 4, f) != 4) {
  160. ast_log(LOG_WARNING, "Read failed (fact)\n");
  161. return -1;
  162. }
  163. if (memcmp(&fact, "fact", 4)) {
  164. ast_log(LOG_WARNING, "Does not say fact\n");
  165. return -1;
  166. }
  167. /* Ignore the "fact value" */
  168. if (fread(&fact, 1, 4, f) != 4) {
  169. ast_log(LOG_WARNING, "Read failed (fact header)\n");
  170. return -1;
  171. }
  172. if (fread(&fact, 1, 4, f) != 4) {
  173. ast_log(LOG_WARNING, "Read failed (fact value)\n");
  174. return -1;
  175. }
  176. /* Check for the word data */
  177. if (fread(&data, 1, 4, f) != 4) {
  178. ast_log(LOG_WARNING, "Read failed (data)\n");
  179. return -1;
  180. }
  181. if (memcmp(&data, "data", 4)) {
  182. ast_log(LOG_WARNING, "Does not say data\n");
  183. return -1;
  184. }
  185. /* Ignore the data length */
  186. if (fread(&data, 1, 4, f) != 4) {
  187. ast_log(LOG_WARNING, "Read failed (data)\n");
  188. return -1;
  189. }
  190. return 0;
  191. }
  192. static int update_header(FILE *f)
  193. {
  194. off_t cur,end,bytes;
  195. int datalen, filelen, samples;
  196. cur = ftello(f);
  197. fseek(f, 0, SEEK_END);
  198. end = ftello(f);
  199. /* in a gsm WAV, data starts 60 bytes in */
  200. bytes = end - MSGSM_DATA_OFFSET;
  201. samples = htoll(bytes / MSGSM_FRAME_SIZE * MSGSM_SAMPLES);
  202. datalen = htoll(bytes);
  203. filelen = htoll(MSGSM_DATA_OFFSET - 8 + 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 write file size\n");
  214. return -1;
  215. }
  216. if (fseek(f, 48, SEEK_SET)) {
  217. ast_log(LOG_WARNING, "Unable to set our position\n");
  218. return -1;
  219. }
  220. if (fwrite(&samples, 1, 4, f) != 4) {
  221. ast_log(LOG_WARNING, "Unable to write samples\n");
  222. return -1;
  223. }
  224. if (fseek(f, 56, SEEK_SET)) {
  225. ast_log(LOG_WARNING, "Unable to set our position\n");
  226. return -1;
  227. }
  228. if (fwrite(&datalen, 1, 4, f) != 4) {
  229. ast_log(LOG_WARNING, "Unable to write datalen\n");
  230. return -1;
  231. }
  232. if (fseeko(f, cur, SEEK_SET)) {
  233. ast_log(LOG_WARNING, "Unable to return to position\n");
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. static int write_header(FILE *f)
  239. {
  240. /* Samples per second (always 8000 for this format). */
  241. unsigned int sample_rate = htoll(8000);
  242. /* Bytes per second (always 1625 for this format). */
  243. unsigned int byte_sample_rate = htoll(1625);
  244. /* This is the size of the "fmt " subchunk */
  245. unsigned int fmtsize = htoll(20);
  246. /* WAV #49 */
  247. unsigned short fmt = htols(49);
  248. /* Mono = 1 channel */
  249. unsigned short chans = htols(1);
  250. /* Each block of data is exactly 65 bytes in size. */
  251. unsigned int block_align = htoll(MSGSM_FRAME_SIZE);
  252. /* Not actually 2, but rounded up to the nearest bit */
  253. unsigned short bits_per_sample = htols(2);
  254. /* Needed for compressed formats */
  255. unsigned short extra_format = htols(MSGSM_SAMPLES);
  256. /* This is the size of the "fact" subchunk */
  257. unsigned int factsize = htoll(4);
  258. /* Number of samples in the data chunk */
  259. unsigned int num_samples = htoll(0);
  260. /* Number of bytes in the data chunk */
  261. unsigned int size = htoll(0);
  262. /* Write a GSM header, ignoring sizes which will be filled in later */
  263. /* 0: Chunk ID */
  264. if (fwrite("RIFF", 1, 4, f) != 4) {
  265. ast_log(LOG_WARNING, "Unable to write header\n");
  266. return -1;
  267. }
  268. /* 4: Chunk Size */
  269. if (fwrite(&size, 1, 4, f) != 4) {
  270. ast_log(LOG_WARNING, "Unable to write header\n");
  271. return -1;
  272. }
  273. /* 8: Chunk Format */
  274. if (fwrite("WAVE", 1, 4, f) != 4) {
  275. ast_log(LOG_WARNING, "Unable to write header\n");
  276. return -1;
  277. }
  278. /* 12: Subchunk 1: ID */
  279. if (fwrite("fmt ", 1, 4, f) != 4) {
  280. ast_log(LOG_WARNING, "Unable to write header\n");
  281. return -1;
  282. }
  283. /* 16: Subchunk 1: Size (minus 8) */
  284. if (fwrite(&fmtsize, 1, 4, f) != 4) {
  285. ast_log(LOG_WARNING, "Unable to write header\n");
  286. return -1;
  287. }
  288. /* 20: Subchunk 1: Audio format (49) */
  289. if (fwrite(&fmt, 1, 2, f) != 2) {
  290. ast_log(LOG_WARNING, "Unable to write header\n");
  291. return -1;
  292. }
  293. /* 22: Subchunk 1: Number of channels */
  294. if (fwrite(&chans, 1, 2, f) != 2) {
  295. ast_log(LOG_WARNING, "Unable to write header\n");
  296. return -1;
  297. }
  298. /* 24: Subchunk 1: Sample rate */
  299. if (fwrite(&sample_rate, 1, 4, f) != 4) {
  300. ast_log(LOG_WARNING, "Unable to write header\n");
  301. return -1;
  302. }
  303. /* 28: Subchunk 1: Byte rate */
  304. if (fwrite(&byte_sample_rate, 1, 4, f) != 4) {
  305. ast_log(LOG_WARNING, "Unable to write header\n");
  306. return -1;
  307. }
  308. /* 32: Subchunk 1: Block align */
  309. if (fwrite(&block_align, 1, 4, f) != 4) {
  310. ast_log(LOG_WARNING, "Unable to write header\n");
  311. return -1;
  312. }
  313. /* 36: Subchunk 1: Bits per sample */
  314. if (fwrite(&bits_per_sample, 1, 2, f) != 2) {
  315. ast_log(LOG_WARNING, "Unable to write header\n");
  316. return -1;
  317. }
  318. /* 38: Subchunk 1: Extra format bytes */
  319. if (fwrite(&extra_format, 1, 2, f) != 2) {
  320. ast_log(LOG_WARNING, "Unable to write header\n");
  321. return -1;
  322. }
  323. /* 40: Subchunk 2: ID */
  324. if (fwrite("fact", 1, 4, f) != 4) {
  325. ast_log(LOG_WARNING, "Unable to write header\n");
  326. return -1;
  327. }
  328. /* 44: Subchunk 2: Size (minus 8) */
  329. if (fwrite(&factsize, 1, 4, f) != 4) {
  330. ast_log(LOG_WARNING, "Unable to write header\n");
  331. return -1;
  332. }
  333. /* 48: Subchunk 2: Number of samples */
  334. if (fwrite(&num_samples, 1, 4, f) != 4) {
  335. ast_log(LOG_WARNING, "Unable to write header\n");
  336. return -1;
  337. }
  338. /* 52: Subchunk 3: ID */
  339. if (fwrite("data", 1, 4, f) != 4) {
  340. ast_log(LOG_WARNING, "Unable to write header\n");
  341. return -1;
  342. }
  343. /* 56: Subchunk 3: Size */
  344. if (fwrite(&size, 1, 4, f) != 4) {
  345. ast_log(LOG_WARNING, "Unable to write header\n");
  346. return -1;
  347. }
  348. return 0;
  349. }
  350. static int wav_open(struct ast_filestream *s)
  351. {
  352. /* We don't have any header to read or anything really, but
  353. if we did, it would go here. We also might want to check
  354. and be sure it's a valid file. */
  355. struct wavg_desc *fs = (struct wavg_desc *)s->_private;
  356. if (check_header(s->f))
  357. return -1;
  358. fs->secondhalf = 0; /* not strictly necessary */
  359. return 0;
  360. }
  361. static int wav_rewrite(struct ast_filestream *s, const char *comment)
  362. {
  363. /* We don't have any header to read or anything really, but
  364. if we did, it would go here. We also might want to check
  365. and be sure it's a valid file. */
  366. if (write_header(s->f))
  367. return -1;
  368. return 0;
  369. }
  370. static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
  371. {
  372. /* Send a frame from the file to the appropriate channel */
  373. struct wavg_desc *fs = (struct wavg_desc *)s->_private;
  374. s->fr.frametype = AST_FRAME_VOICE;
  375. s->fr.subclass = AST_FORMAT_GSM;
  376. s->fr.offset = AST_FRIENDLY_OFFSET;
  377. s->fr.samples = GSM_SAMPLES;
  378. s->fr.mallocd = 0;
  379. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
  380. if (fs->secondhalf) {
  381. /* Just return a frame based on the second GSM frame */
  382. s->fr.data.ptr = (char *)s->fr.data.ptr + GSM_FRAME_SIZE;
  383. s->fr.offset += GSM_FRAME_SIZE;
  384. } else {
  385. /* read and convert */
  386. unsigned char msdata[MSGSM_FRAME_SIZE];
  387. int res;
  388. if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
  389. if (res && (res != 1))
  390. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  391. return NULL;
  392. }
  393. /* Convert from MS format to two real GSM frames */
  394. conv65(msdata, s->fr.data.ptr);
  395. }
  396. fs->secondhalf = !fs->secondhalf;
  397. *whennext = GSM_SAMPLES;
  398. return &s->fr;
  399. }
  400. static int wav_write(struct ast_filestream *s, struct ast_frame *f)
  401. {
  402. int len;
  403. int size;
  404. struct wavg_desc *fs = (struct wavg_desc *)s->_private;
  405. if (f->frametype != AST_FRAME_VOICE) {
  406. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  407. return -1;
  408. }
  409. if (f->subclass != AST_FORMAT_GSM) {
  410. ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
  411. return -1;
  412. }
  413. /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE
  414. * we assume it is already in the correct format.
  415. */
  416. if (!(f->datalen % MSGSM_FRAME_SIZE)) {
  417. size = MSGSM_FRAME_SIZE;
  418. fs->secondhalf = 0;
  419. } else {
  420. size = GSM_FRAME_SIZE;
  421. }
  422. for (len = 0; len < f->datalen ; len += size) {
  423. int res;
  424. unsigned char *src, msdata[MSGSM_FRAME_SIZE];
  425. if (fs->secondhalf) { /* second half of raw gsm to be converted */
  426. memcpy(s->buf + GSM_FRAME_SIZE, f->data.ptr + len, GSM_FRAME_SIZE);
  427. conv66((unsigned char *) s->buf, msdata);
  428. src = msdata;
  429. fs->secondhalf = 0;
  430. } else if (size == GSM_FRAME_SIZE) { /* first half of raw gsm */
  431. memcpy(s->buf, f->data.ptr + len, GSM_FRAME_SIZE);
  432. src = NULL; /* nothing to write */
  433. fs->secondhalf = 1;
  434. } else { /* raw msgsm data */
  435. src = f->data.ptr + len;
  436. }
  437. if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
  438. ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
  439. return -1;
  440. }
  441. update_header(s->f); /* XXX inefficient! */
  442. }
  443. return 0;
  444. }
  445. static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  446. {
  447. off_t offset=0, distance, max;
  448. struct wavg_desc *s = (struct wavg_desc *)fs->_private;
  449. off_t min = MSGSM_DATA_OFFSET;
  450. off_t cur = ftello(fs->f);
  451. fseek(fs->f, 0, SEEK_END);
  452. max = ftello(fs->f); /* XXX ideally, should round correctly */
  453. /* Compute the distance in bytes, rounded to the block size */
  454. distance = (sample_offset/MSGSM_SAMPLES) * MSGSM_FRAME_SIZE;
  455. if (whence == SEEK_SET)
  456. offset = distance + min;
  457. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  458. offset = distance + cur;
  459. else if (whence == SEEK_END)
  460. offset = max - distance;
  461. /* always protect against seeking past end of header */
  462. if (offset < min)
  463. offset = min;
  464. if (whence != SEEK_FORCECUR) {
  465. if (offset > max)
  466. offset = max;
  467. } else if (offset > max) {
  468. int i;
  469. fseek(fs->f, 0, SEEK_END);
  470. for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
  471. if (!fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f)) {
  472. ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
  473. }
  474. }
  475. }
  476. s->secondhalf = 0;
  477. return fseeko(fs->f, offset, SEEK_SET);
  478. }
  479. static int wav_trunc(struct ast_filestream *fs)
  480. {
  481. if (ftruncate(fileno(fs->f), ftello(fs->f)))
  482. return -1;
  483. return update_header(fs->f);
  484. }
  485. static off_t wav_tell(struct ast_filestream *fs)
  486. {
  487. off_t offset;
  488. offset = ftello(fs->f);
  489. /* since this will most likely be used later in play or record, lets stick
  490. * to that level of resolution, just even frames boundaries */
  491. return (offset - MSGSM_DATA_OFFSET)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES;
  492. }
  493. static const struct ast_format wav49_f = {
  494. .name = "wav49",
  495. .exts = "WAV|wav49",
  496. .format = AST_FORMAT_GSM,
  497. .open = wav_open,
  498. .rewrite = wav_rewrite,
  499. .write = wav_write,
  500. .seek = wav_seek,
  501. .trunc = wav_trunc,
  502. .tell = wav_tell,
  503. .read = wav_read,
  504. .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,
  505. .desc_size = sizeof(struct wavg_desc),
  506. };
  507. static int load_module(void)
  508. {
  509. if (ast_format_register(&wav49_f))
  510. return AST_MODULE_LOAD_FAILURE;
  511. return AST_MODULE_LOAD_SUCCESS;
  512. }
  513. static int unload_module(void)
  514. {
  515. return ast_format_unregister(wav49_f.name);
  516. }
  517. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (Proprietary GSM)",
  518. .load = load_module,
  519. .unload = unload_module,
  520. .load_pri = 10,
  521. );