You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
8.0KB

  1. /*
  2. * MPEG Audio decoder
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "avcodec.h"
  23. #include "mpglib/mpg123.h"
  24. /*
  25. * TODO:
  26. * - add free format
  27. * - do not rely anymore on mpglib (first step: implement dct64 and decoding filter)
  28. */
  29. #define HEADER_SIZE 4
  30. #define BACKSTEP_SIZE 512
  31. typedef struct MPADecodeContext {
  32. struct mpstr mpstr;
  33. UINT8 inbuf1[2][MAXFRAMESIZE + BACKSTEP_SIZE]; /* input buffer */
  34. int inbuf_index;
  35. UINT8 *inbuf_ptr, *inbuf;
  36. int frame_size;
  37. int error_protection;
  38. int layer;
  39. int sample_rate;
  40. int bit_rate;
  41. int old_frame_size;
  42. GetBitContext gb;
  43. } MPADecodeContext;
  44. /* XXX: suppress that mess */
  45. struct mpstr *gmp;
  46. GetBitContext *gmp_gb;
  47. static MPADecodeContext *gmp_s;
  48. /* XXX: merge constants with encoder */
  49. static const unsigned short mp_bitrate_tab[2][3][15] = {
  50. { {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
  51. {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 },
  52. {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 } },
  53. { {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
  54. {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
  55. {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
  56. }
  57. };
  58. static unsigned short mp_freq_tab[3] = { 44100, 48000, 32000 };
  59. static int decode_init(AVCodecContext * avctx)
  60. {
  61. MPADecodeContext *s = avctx->priv_data;
  62. struct mpstr *mp = &s->mpstr;
  63. static int init;
  64. mp->fr.single = -1;
  65. mp->synth_bo = 1;
  66. if(!init) {
  67. init = 1;
  68. make_decode_tables(32767);
  69. init_layer2();
  70. init_layer3(SBLIMIT);
  71. }
  72. s->inbuf_index = 0;
  73. s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
  74. s->inbuf_ptr = s->inbuf;
  75. return 0;
  76. }
  77. /* fast header check for resync */
  78. static int check_header(UINT32 header)
  79. {
  80. /* header */
  81. if ((header & 0xffe00000) != 0xffe00000)
  82. return -1;
  83. /* layer check */
  84. if (((header >> 17) & 3) == 0)
  85. return -1;
  86. /* bit rate : currently no free format supported */
  87. if (((header >> 12) & 0xf) == 0xf ||
  88. ((header >> 12) & 0xf) == 0x0)
  89. return -1;
  90. /* frequency */
  91. if (((header >> 10) & 3) == 3)
  92. return -1;
  93. return 0;
  94. }
  95. /* header decoding. MUST check the header before because no
  96. consistency check is done there */
  97. static void decode_header(MPADecodeContext *s, UINT32 header)
  98. {
  99. struct frame *fr = &s->mpstr.fr;
  100. int sample_rate, frame_size;
  101. if (header & (1<<20)) {
  102. fr->lsf = (header & (1<<19)) ? 0 : 1;
  103. fr->mpeg25 = 0;
  104. } else {
  105. fr->lsf = 1;
  106. fr->mpeg25 = 1;
  107. }
  108. s->layer = 4 - ((header >> 17) & 3);
  109. /* extract frequency */
  110. fr->sampling_frequency = ((header >> 10) & 3);
  111. sample_rate = mp_freq_tab[fr->sampling_frequency] >> (fr->lsf + fr->mpeg25);
  112. fr->sampling_frequency += 3 * (fr->lsf + fr->mpeg25);
  113. s->error_protection = ((header>>16) & 1) ^ 1;
  114. fr->bitrate_index = ((header>>12)&0xf);
  115. fr->padding = ((header>>9)&0x1);
  116. fr->extension = ((header>>8)&0x1);
  117. fr->mode = ((header>>6)&0x3);
  118. fr->mode_ext = ((header>>4)&0x3);
  119. fr->copyright = ((header>>3)&0x1);
  120. fr->original = ((header>>2)&0x1);
  121. fr->emphasis = header & 0x3;
  122. fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  123. frame_size = mp_bitrate_tab[fr->lsf][s->layer - 1][fr->bitrate_index];
  124. s->bit_rate = frame_size * 1000;
  125. switch(s->layer) {
  126. case 1:
  127. frame_size = (frame_size * 12000) / sample_rate;
  128. frame_size = ((frame_size + fr->padding) << 2);
  129. break;
  130. case 2:
  131. frame_size = (frame_size * 144000) / sample_rate;
  132. frame_size += fr->padding;
  133. break;
  134. case 3:
  135. frame_size = (frame_size * 144000) / (sample_rate << fr->lsf);
  136. frame_size += fr->padding;
  137. break;
  138. }
  139. s->frame_size = frame_size;
  140. s->sample_rate = sample_rate;
  141. #if 0
  142. printf("layer%d, %d Hz, %d kbits/s, %s\n",
  143. s->layer, s->sample_rate, s->bit_rate, fr->stereo ? "stereo" : "mono");
  144. #endif
  145. }
  146. static int mp_decode_frame(MPADecodeContext *s,
  147. short *samples)
  148. {
  149. int nb_bytes;
  150. init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, s->inbuf_ptr - s->inbuf - HEADER_SIZE);
  151. /* skip error protection field */
  152. if (s->error_protection)
  153. get_bits(&s->gb, 16);
  154. /* XXX: horrible: global! */
  155. gmp = &s->mpstr;
  156. gmp_s = s;
  157. gmp_gb = &s->gb;
  158. nb_bytes = 0;
  159. switch(s->layer) {
  160. case 1:
  161. do_layer1(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
  162. break;
  163. case 2:
  164. do_layer2(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
  165. break;
  166. case 3:
  167. do_layer3(&s->mpstr.fr,(unsigned char *)samples, &nb_bytes);
  168. s->inbuf_index ^= 1;
  169. s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
  170. s->old_frame_size = s->frame_size;
  171. break;
  172. default:
  173. break;
  174. }
  175. return nb_bytes;
  176. }
  177. /*
  178. * seek back in the stream for backstep bytes (at most 511 bytes, and
  179. * at most in last frame). Note that this is slightly incorrect (data
  180. * can span more than one block!)
  181. */
  182. int set_pointer(long backstep)
  183. {
  184. UINT8 *ptr;
  185. /* compute current position in stream */
  186. ptr = gmp_gb->buf_ptr - (gmp_gb->bit_cnt >> 3);
  187. /* copy old data before current one */
  188. ptr -= backstep;
  189. memcpy(ptr, gmp_s->inbuf1[gmp_s->inbuf_index ^ 1] +
  190. BACKSTEP_SIZE + gmp_s->old_frame_size - backstep, backstep);
  191. /* init get bits again */
  192. init_get_bits(gmp_gb, ptr, gmp_s->frame_size + backstep);
  193. return 0;
  194. }
  195. static int decode_frame(AVCodecContext * avctx,
  196. void *data, int *data_size,
  197. UINT8 * buf, int buf_size)
  198. {
  199. MPADecodeContext *s = avctx->priv_data;
  200. UINT32 header;
  201. UINT8 *buf_ptr;
  202. int len, out_size;
  203. short *out_samples = data;
  204. *data_size = 0;
  205. buf_ptr = buf;
  206. while (buf_size > 0) {
  207. len = s->inbuf_ptr - s->inbuf;
  208. if (s->frame_size == 0) {
  209. /* no header seen : find one. We need at least 7 bytes to parse it */
  210. len = HEADER_SIZE - len;
  211. if (len > buf_size)
  212. len = buf_size;
  213. memcpy(s->inbuf_ptr, buf_ptr, len);
  214. buf_ptr += len;
  215. s->inbuf_ptr += len;
  216. buf_size -= len;
  217. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  218. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  219. (s->inbuf[2] << 8) | s->inbuf[3];
  220. if (check_header(header) < 0) {
  221. /* no sync found : move by one byte (inefficient, but simple!) */
  222. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  223. s->inbuf_ptr--;
  224. } else {
  225. decode_header(s, header);
  226. /* update codec info */
  227. avctx->sample_rate = s->sample_rate;
  228. avctx->channels = s->mpstr.fr.stereo ? 2 : 1;
  229. avctx->bit_rate = s->bit_rate;
  230. }
  231. }
  232. } else if (len < s->frame_size) {
  233. len = s->frame_size - len;
  234. if (len > buf_size)
  235. len = buf_size;
  236. memcpy(s->inbuf_ptr, buf_ptr, len);
  237. buf_ptr += len;
  238. s->inbuf_ptr += len;
  239. buf_size -= len;
  240. } else {
  241. out_size = mp_decode_frame(s, out_samples);
  242. s->inbuf_ptr = s->inbuf;
  243. s->frame_size = 0;
  244. *data_size = out_size;
  245. break;
  246. }
  247. }
  248. return buf_ptr - buf;
  249. }
  250. AVCodec mp3_decoder =
  251. {
  252. "mpegaudio",
  253. CODEC_TYPE_AUDIO,
  254. CODEC_ID_MP2,
  255. sizeof(MPADecodeContext),
  256. decode_init,
  257. NULL,
  258. NULL,
  259. decode_frame,
  260. };