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.

253 lines
8.5KB

  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 ff_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 (ff_mpegaudio_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. AV_WB32(s->inbuf, s->free_format_next_header);
  96. s->inbuf_ptr = s->inbuf + 4;
  97. s->free_format_next_header = 0;
  98. goto got_header;
  99. }
  100. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  101. bytes to parse it */
  102. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  103. if (len > 0) {
  104. memcpy(s->inbuf_ptr, buf_ptr, len);
  105. buf_ptr += len;
  106. buf_size -= len;
  107. s->inbuf_ptr += len;
  108. }
  109. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  110. got_header:
  111. header = AV_RB32(s->inbuf);
  112. ret = ff_mpa_decode_header(avctx, header, &sr);
  113. if (ret < 0) {
  114. s->header_count= -2;
  115. /* no sync found : move by one byte (inefficient, but simple!) */
  116. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  117. s->inbuf_ptr--;
  118. dprintf(avctx, "skip %x\n", header);
  119. /* reset free format frame size to give a chance
  120. to get a new bitrate */
  121. s->free_format_frame_size = 0;
  122. } else {
  123. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  124. s->header_count= -3;
  125. s->header= header;
  126. s->header_count++;
  127. s->frame_size = ret;
  128. #if 0
  129. /* free format: prepare to compute frame size */
  130. if (ff_mpegaudio_decode_header(s, header) == 1) {
  131. s->frame_size = -1;
  132. }
  133. #endif
  134. if(s->header_count > 1)
  135. avctx->sample_rate= sr;
  136. }
  137. }
  138. } else
  139. #if 0
  140. if (s->frame_size == -1) {
  141. /* free format : find next sync to compute frame size */
  142. len = MPA_MAX_CODED_FRAME_SIZE - len;
  143. if (len > buf_size)
  144. len = buf_size;
  145. if (len == 0) {
  146. /* frame too long: resync */
  147. s->frame_size = 0;
  148. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  149. s->inbuf_ptr--;
  150. } else {
  151. uint8_t *p, *pend;
  152. uint32_t header1;
  153. int padding;
  154. memcpy(s->inbuf_ptr, buf_ptr, len);
  155. /* check for header */
  156. p = s->inbuf_ptr - 3;
  157. pend = s->inbuf_ptr + len - 4;
  158. while (p <= pend) {
  159. header = AV_RB32(p);
  160. header1 = AV_RB32(s->inbuf);
  161. /* check with high probability that we have a
  162. valid header */
  163. if ((header & SAME_HEADER_MASK) ==
  164. (header1 & SAME_HEADER_MASK)) {
  165. /* header found: update pointers */
  166. len = (p + 4) - s->inbuf_ptr;
  167. buf_ptr += len;
  168. buf_size -= len;
  169. s->inbuf_ptr = p;
  170. /* compute frame size */
  171. s->free_format_next_header = header;
  172. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  173. padding = (header1 >> 9) & 1;
  174. if (s->layer == 1)
  175. s->free_format_frame_size -= padding * 4;
  176. else
  177. s->free_format_frame_size -= padding;
  178. dprintf(avctx, "free frame size=%d padding=%d\n",
  179. s->free_format_frame_size, padding);
  180. ff_mpegaudio_decode_header(s, header1);
  181. goto next_data;
  182. }
  183. p++;
  184. }
  185. /* not found: simply increase pointers */
  186. buf_ptr += len;
  187. s->inbuf_ptr += len;
  188. buf_size -= len;
  189. }
  190. } else
  191. #endif
  192. if (len < s->frame_size) {
  193. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  194. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  195. len = FFMIN(s->frame_size - len, buf_size);
  196. memcpy(s->inbuf_ptr, buf_ptr, len);
  197. buf_ptr += len;
  198. s->inbuf_ptr += len;
  199. buf_size -= len;
  200. }
  201. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  202. && buf_size + buf_ptr - buf >= s->frame_size){
  203. if(s->header_count > 0){
  204. *poutbuf = buf;
  205. *poutbuf_size = s->frame_size;
  206. }
  207. buf_ptr = buf + s->frame_size;
  208. s->inbuf_ptr = s->inbuf;
  209. s->frame_size = 0;
  210. break;
  211. }
  212. // next_data:
  213. if (s->frame_size > 0 &&
  214. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  215. if(s->header_count > 0){
  216. *poutbuf = s->inbuf;
  217. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  218. }
  219. s->inbuf_ptr = s->inbuf;
  220. s->frame_size = 0;
  221. break;
  222. }
  223. }
  224. return buf_ptr - buf;
  225. }
  226. AVCodecParser mpegaudio_parser = {
  227. { CODEC_ID_MP2, CODEC_ID_MP3 },
  228. sizeof(MpegAudioParseContext),
  229. mpegaudio_parse_init,
  230. mpegaudio_parse,
  231. NULL,
  232. };