format_mp3.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Anthony Minessale <anthmct@yahoo.com>
  5. *
  6. * Derived from other asterisk sound formats by
  7. * Mark Spencer <markster@linux-support.net>
  8. *
  9. * Thanks to mpglib from http://www.mpg123.org/
  10. * and Chris Stenton [jacs@gnome.co.uk]
  11. * for coding the ability to play stereo and non-8khz files
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*!
  23. * \file
  24. * \brief MP3 Format Handler
  25. * \ingroup formats
  26. */
  27. /*** MODULEINFO
  28. <defaultenabled>no</defaultenabled>
  29. <support_level>extended</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "mp3/mpg123.h"
  34. #include "mp3/mpglib.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/mod_format.h"
  37. #include "asterisk/logger.h"
  38. #include "asterisk/format_cache.h"
  39. #define MP3_BUFLEN 320
  40. #define MP3_SCACHE 16384
  41. #define MP3_DCACHE 8192
  42. struct mp3_private {
  43. /*! state for the mp3 decoder */
  44. struct mpstr mp;
  45. /*! buffer to hold mp3 data after read from disk */
  46. char sbuf[MP3_SCACHE];
  47. /*! buffer for slinear audio after being decoded out of sbuf */
  48. char dbuf[MP3_DCACHE];
  49. /*! how much data has been written to the output buffer in the ast_filestream */
  50. int buflen;
  51. /*! how much data has been written to sbuf */
  52. int sbuflen;
  53. /*! how much data is left to be read out of dbuf, starting at dbufoffset */
  54. int dbuflen;
  55. /*! current offset for reading data out of dbuf */
  56. int dbufoffset;
  57. int offset;
  58. long seek;
  59. };
  60. static const char name[] = "mp3";
  61. #define BLOCKSIZE 160
  62. #define OUTSCALE 4096
  63. #define GAIN -4 /* 2^GAIN is the multiple to increase the volume by */
  64. #if __BYTE_ORDER == __LITTLE_ENDIAN
  65. #define htoll(b) (b)
  66. #define htols(b) (b)
  67. #define ltohl(b) (b)
  68. #define ltohs(b) (b)
  69. #else
  70. #if __BYTE_ORDER == __BIG_ENDIAN
  71. #define htoll(b) \
  72. (((((b) ) & 0xFF) << 24) | \
  73. ((((b) >> 8) & 0xFF) << 16) | \
  74. ((((b) >> 16) & 0xFF) << 8) | \
  75. ((((b) >> 24) & 0xFF) ))
  76. #define htols(b) \
  77. (((((b) ) & 0xFF) << 8) | \
  78. ((((b) >> 8) & 0xFF) ))
  79. #define ltohl(b) htoll(b)
  80. #define ltohs(b) htols(b)
  81. #else
  82. #error "Endianess not defined"
  83. #endif
  84. #endif
  85. static int mp3_open(struct ast_filestream *s)
  86. {
  87. struct mp3_private *p = s->_private;
  88. InitMP3(&p->mp, OUTSCALE);
  89. return 0;
  90. }
  91. static void mp3_close(struct ast_filestream *s)
  92. {
  93. struct mp3_private *p = s->_private;
  94. ExitMP3(&p->mp);
  95. return;
  96. }
  97. static int mp3_squeue(struct ast_filestream *s)
  98. {
  99. struct mp3_private *p = s->_private;
  100. int res=0;
  101. res = ftell(s->f);
  102. p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
  103. if(p->sbuflen < 0) {
  104. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
  105. return -1;
  106. }
  107. res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
  108. if(res != MP3_OK)
  109. return -1;
  110. p->sbuflen -= p->dbuflen;
  111. p->dbufoffset = 0;
  112. return 0;
  113. }
  114. static int mp3_dqueue(struct ast_filestream *s)
  115. {
  116. struct mp3_private *p = s->_private;
  117. int res=0;
  118. if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
  119. p->sbuflen -= p->dbuflen;
  120. p->dbufoffset = 0;
  121. }
  122. return res;
  123. }
  124. static int mp3_queue(struct ast_filestream *s)
  125. {
  126. struct mp3_private *p = s->_private;
  127. int res = 0, bytes = 0;
  128. if(p->seek) {
  129. ExitMP3(&p->mp);
  130. InitMP3(&p->mp, OUTSCALE);
  131. fseek(s->f, 0, SEEK_SET);
  132. p->sbuflen = p->dbuflen = p->offset = 0;
  133. while(p->offset < p->seek) {
  134. if(mp3_squeue(s))
  135. return -1;
  136. while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
  137. for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
  138. p->dbufoffset++;
  139. p->offset++;
  140. if(p->offset >= p->seek)
  141. break;
  142. }
  143. }
  144. if(res == MP3_ERR)
  145. return -1;
  146. }
  147. p->seek = 0;
  148. return 0;
  149. }
  150. if(p->dbuflen == 0) {
  151. if(p->sbuflen) {
  152. res = mp3_dqueue(s);
  153. if(res == MP3_ERR)
  154. return -1;
  155. }
  156. if(! p->sbuflen || res != MP3_OK) {
  157. if(mp3_squeue(s))
  158. return -1;
  159. }
  160. }
  161. return 0;
  162. }
  163. static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
  164. {
  165. struct mp3_private *p = s->_private;
  166. int delay =0;
  167. int save=0;
  168. /* Pre-populate the buffer that holds audio to be returned (dbuf) */
  169. if (mp3_queue(s)) {
  170. return NULL;
  171. }
  172. if (p->dbuflen) {
  173. /* Read out what's waiting in dbuf */
  174. for (p->buflen = 0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
  175. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen + p->dbufoffset];
  176. }
  177. p->dbufoffset += p->buflen;
  178. p->dbuflen -= p->buflen;
  179. }
  180. if (p->buflen < MP3_BUFLEN) {
  181. /* dbuf didn't have enough, so reset dbuf, fill it back up and continue */
  182. p->dbuflen = p->dbufoffset = 0;
  183. if (mp3_queue(s)) {
  184. return NULL;
  185. }
  186. /* Make sure dbuf has enough to complete this read attempt */
  187. if (p->dbuflen >= (MP3_BUFLEN - p->buflen)) {
  188. for (save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
  189. s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen - save) + p->dbufoffset];
  190. }
  191. p->dbufoffset += (MP3_BUFLEN - save);
  192. p->dbuflen -= (MP3_BUFLEN - save);
  193. }
  194. }
  195. p->offset += p->buflen;
  196. delay = p->buflen / 2;
  197. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
  198. s->fr.samples = delay;
  199. *whennext = delay;
  200. return &s->fr;
  201. }
  202. static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
  203. {
  204. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  205. return -1;
  206. }
  207. static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
  208. {
  209. struct mp3_private *p = s->_private;
  210. off_t min,max,cur;
  211. long offset=0,samples;
  212. samples = sample_offset * 2;
  213. min = 0;
  214. fseek(s->f, 0, SEEK_END);
  215. max = ftell(s->f) * 100;
  216. cur = p->offset;
  217. if (whence == SEEK_SET)
  218. offset = samples + min;
  219. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  220. offset = samples + cur;
  221. else if (whence == SEEK_END)
  222. offset = max - samples;
  223. if (whence != SEEK_FORCECUR) {
  224. offset = (offset > max)?max:offset;
  225. }
  226. p->seek = offset;
  227. return fseek(s->f, offset, SEEK_SET);
  228. }
  229. static int mp3_rewrite(struct ast_filestream *s, const char *comment)
  230. {
  231. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  232. return -1;
  233. }
  234. static int mp3_trunc(struct ast_filestream *s)
  235. {
  236. ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
  237. return -1;
  238. }
  239. static off_t mp3_tell(struct ast_filestream *s)
  240. {
  241. struct mp3_private *p = s->_private;
  242. return p->offset/2;
  243. }
  244. static char *mp3_getcomment(struct ast_filestream *s)
  245. {
  246. return NULL;
  247. }
  248. static struct ast_format_def mp3_f = {
  249. .name = "mp3",
  250. .exts = "mp3",
  251. .open = mp3_open,
  252. .write = mp3_write,
  253. .rewrite = mp3_rewrite,
  254. .seek = mp3_seek,
  255. .trunc = mp3_trunc,
  256. .tell = mp3_tell,
  257. .read = mp3_read,
  258. .close = mp3_close,
  259. .getcomment = mp3_getcomment,
  260. .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
  261. .desc_size = sizeof(struct mp3_private),
  262. };
  263. static int load_module(void)
  264. {
  265. mp3_f.format = ast_format_slin;
  266. InitMP3Constants();
  267. return ast_format_def_register(&mp3_f);
  268. }
  269. static int unload_module(void)
  270. {
  271. return ast_format_def_unregister(name);
  272. }
  273. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");