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.

259 lines
8.8KB

  1. /*
  2. * MPEG Audio parser
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. * Copyright (c) 2003 Michael Niedermayer.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "parser.h"
  23. #include "mpegaudio.h"
  24. #include "mpegaudiodecheader.h"
  25. typedef struct MpegAudioParseContext {
  26. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  27. uint8_t *inbuf_ptr;
  28. int frame_size;
  29. int free_format_frame_size;
  30. int free_format_next_header;
  31. uint32_t header;
  32. int header_count;
  33. } MpegAudioParseContext;
  34. #define MPA_HEADER_SIZE 4
  35. /* header + layer + bitrate + freq + lsf/mpeg25 */
  36. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  37. #define SAME_HEADER_MASK \
  38. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  39. /* useful helper to get mpeg audio stream infos. Return -1 if error in
  40. header, otherwise the coded frame size in bytes */
  41. int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
  42. {
  43. MPADecodeContext s1, *s = &s1;
  44. s1.avctx = avctx;
  45. if (ff_mpa_check_header(head) != 0)
  46. return -1;
  47. if (decode_header(s, head) != 0) {
  48. return -1;
  49. }
  50. switch(s->layer) {
  51. case 1:
  52. avctx->frame_size = 384;
  53. break;
  54. case 2:
  55. avctx->frame_size = 1152;
  56. break;
  57. default:
  58. case 3:
  59. if (s->lsf)
  60. avctx->frame_size = 576;
  61. else
  62. avctx->frame_size = 1152;
  63. break;
  64. }
  65. *sample_rate = s->sample_rate;
  66. avctx->channels = s->nb_channels;
  67. avctx->bit_rate = s->bit_rate;
  68. avctx->sub_id = s->layer;
  69. return s->frame_size;
  70. }
  71. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  72. {
  73. MpegAudioParseContext *s = s1->priv_data;
  74. s->inbuf_ptr = s->inbuf;
  75. return 0;
  76. }
  77. static int mpegaudio_parse(AVCodecParserContext *s1,
  78. AVCodecContext *avctx,
  79. const uint8_t **poutbuf, int *poutbuf_size,
  80. const uint8_t *buf, int buf_size)
  81. {
  82. MpegAudioParseContext *s = s1->priv_data;
  83. int len, ret, sr;
  84. uint32_t header;
  85. const uint8_t *buf_ptr;
  86. *poutbuf = NULL;
  87. *poutbuf_size = 0;
  88. buf_ptr = buf;
  89. while (buf_size > 0) {
  90. len = s->inbuf_ptr - s->inbuf;
  91. if (s->frame_size == 0) {
  92. /* special case for next header for first frame in free
  93. format case (XXX: find a simpler method) */
  94. if (s->free_format_next_header != 0) {
  95. s->inbuf[0] = s->free_format_next_header >> 24;
  96. s->inbuf[1] = s->free_format_next_header >> 16;
  97. s->inbuf[2] = s->free_format_next_header >> 8;
  98. s->inbuf[3] = s->free_format_next_header;
  99. s->inbuf_ptr = s->inbuf + 4;
  100. s->free_format_next_header = 0;
  101. goto got_header;
  102. }
  103. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  104. bytes to parse it */
  105. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  106. if (len > 0) {
  107. memcpy(s->inbuf_ptr, buf_ptr, len);
  108. buf_ptr += len;
  109. buf_size -= len;
  110. s->inbuf_ptr += len;
  111. }
  112. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  113. got_header:
  114. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  115. (s->inbuf[2] << 8) | s->inbuf[3];
  116. ret = mpa_decode_header(avctx, header, &sr);
  117. if (ret < 0) {
  118. s->header_count= -2;
  119. /* no sync found : move by one byte (inefficient, but simple!) */
  120. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  121. s->inbuf_ptr--;
  122. dprintf(avctx, "skip %x\n", header);
  123. /* reset free format frame size to give a chance
  124. to get a new bitrate */
  125. s->free_format_frame_size = 0;
  126. } else {
  127. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  128. s->header_count= -3;
  129. s->header= header;
  130. s->header_count++;
  131. s->frame_size = ret;
  132. #if 0
  133. /* free format: prepare to compute frame size */
  134. if (decode_header(s, header) == 1) {
  135. s->frame_size = -1;
  136. }
  137. #endif
  138. }
  139. if(s->header_count > 1)
  140. avctx->sample_rate= sr;
  141. }
  142. } else
  143. #if 0
  144. if (s->frame_size == -1) {
  145. /* free format : find next sync to compute frame size */
  146. len = MPA_MAX_CODED_FRAME_SIZE - len;
  147. if (len > buf_size)
  148. len = buf_size;
  149. if (len == 0) {
  150. /* frame too long: resync */
  151. s->frame_size = 0;
  152. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  153. s->inbuf_ptr--;
  154. } else {
  155. uint8_t *p, *pend;
  156. uint32_t header1;
  157. int padding;
  158. memcpy(s->inbuf_ptr, buf_ptr, len);
  159. /* check for header */
  160. p = s->inbuf_ptr - 3;
  161. pend = s->inbuf_ptr + len - 4;
  162. while (p <= pend) {
  163. header = (p[0] << 24) | (p[1] << 16) |
  164. (p[2] << 8) | p[3];
  165. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  166. (s->inbuf[2] << 8) | s->inbuf[3];
  167. /* check with high probability that we have a
  168. valid header */
  169. if ((header & SAME_HEADER_MASK) ==
  170. (header1 & SAME_HEADER_MASK)) {
  171. /* header found: update pointers */
  172. len = (p + 4) - s->inbuf_ptr;
  173. buf_ptr += len;
  174. buf_size -= len;
  175. s->inbuf_ptr = p;
  176. /* compute frame size */
  177. s->free_format_next_header = header;
  178. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  179. padding = (header1 >> 9) & 1;
  180. if (s->layer == 1)
  181. s->free_format_frame_size -= padding * 4;
  182. else
  183. s->free_format_frame_size -= padding;
  184. dprintf(avctx, "free frame size=%d padding=%d\n",
  185. s->free_format_frame_size, padding);
  186. decode_header(s, header1);
  187. goto next_data;
  188. }
  189. p++;
  190. }
  191. /* not found: simply increase pointers */
  192. buf_ptr += len;
  193. s->inbuf_ptr += len;
  194. buf_size -= len;
  195. }
  196. } else
  197. #endif
  198. if (len < s->frame_size) {
  199. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  200. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  201. len = FFMIN(s->frame_size - len, buf_size);
  202. memcpy(s->inbuf_ptr, buf_ptr, len);
  203. buf_ptr += len;
  204. s->inbuf_ptr += len;
  205. buf_size -= len;
  206. }
  207. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  208. && buf_size + buf_ptr - buf >= s->frame_size){
  209. if(s->header_count > 0){
  210. *poutbuf = buf;
  211. *poutbuf_size = s->frame_size;
  212. }
  213. buf_ptr = buf + s->frame_size;
  214. s->inbuf_ptr = s->inbuf;
  215. s->frame_size = 0;
  216. break;
  217. }
  218. // next_data:
  219. if (s->frame_size > 0 &&
  220. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  221. if(s->header_count > 0){
  222. *poutbuf = s->inbuf;
  223. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  224. }
  225. s->inbuf_ptr = s->inbuf;
  226. s->frame_size = 0;
  227. break;
  228. }
  229. }
  230. return buf_ptr - buf;
  231. }
  232. AVCodecParser mpegaudio_parser = {
  233. { CODEC_ID_MP2, CODEC_ID_MP3 },
  234. sizeof(MpegAudioParseContext),
  235. mpegaudio_parse_init,
  236. mpegaudio_parse,
  237. NULL,
  238. };