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.

221 lines
8.0KB

  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. typedef struct MpegAudioParseContext {
  25. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  26. uint8_t *inbuf_ptr;
  27. int frame_size;
  28. int free_format_frame_size;
  29. int free_format_next_header;
  30. uint32_t header;
  31. int header_count;
  32. } MpegAudioParseContext;
  33. #define MPA_HEADER_SIZE 4
  34. /* header + layer + bitrate + freq + lsf/mpeg25 */
  35. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  36. #define SAME_HEADER_MASK \
  37. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  38. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  39. {
  40. MpegAudioParseContext *s = s1->priv_data;
  41. s->inbuf_ptr = s->inbuf;
  42. return 0;
  43. }
  44. static int mpegaudio_parse(AVCodecParserContext *s1,
  45. AVCodecContext *avctx,
  46. const uint8_t **poutbuf, int *poutbuf_size,
  47. const uint8_t *buf, int buf_size)
  48. {
  49. MpegAudioParseContext *s = s1->priv_data;
  50. int len, ret, sr;
  51. uint32_t header;
  52. const uint8_t *buf_ptr;
  53. *poutbuf = NULL;
  54. *poutbuf_size = 0;
  55. buf_ptr = buf;
  56. while (buf_size > 0) {
  57. len = s->inbuf_ptr - s->inbuf;
  58. if (s->frame_size == 0) {
  59. /* special case for next header for first frame in free
  60. format case (XXX: find a simpler method) */
  61. if (s->free_format_next_header != 0) {
  62. s->inbuf[0] = s->free_format_next_header >> 24;
  63. s->inbuf[1] = s->free_format_next_header >> 16;
  64. s->inbuf[2] = s->free_format_next_header >> 8;
  65. s->inbuf[3] = s->free_format_next_header;
  66. s->inbuf_ptr = s->inbuf + 4;
  67. s->free_format_next_header = 0;
  68. goto got_header;
  69. }
  70. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  71. bytes to parse it */
  72. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  73. if (len > 0) {
  74. memcpy(s->inbuf_ptr, buf_ptr, len);
  75. buf_ptr += len;
  76. buf_size -= len;
  77. s->inbuf_ptr += len;
  78. }
  79. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  80. got_header:
  81. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  82. (s->inbuf[2] << 8) | s->inbuf[3];
  83. ret = mpa_decode_header(avctx, header, &sr);
  84. if (ret < 0) {
  85. s->header_count= -2;
  86. /* no sync found : move by one byte (inefficient, but simple!) */
  87. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  88. s->inbuf_ptr--;
  89. dprintf(avctx, "skip %x\n", header);
  90. /* reset free format frame size to give a chance
  91. to get a new bitrate */
  92. s->free_format_frame_size = 0;
  93. } else {
  94. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  95. s->header_count= -3;
  96. s->header= header;
  97. s->header_count++;
  98. s->frame_size = ret;
  99. #if 0
  100. /* free format: prepare to compute frame size */
  101. if (decode_header(s, header) == 1) {
  102. s->frame_size = -1;
  103. }
  104. #endif
  105. }
  106. if(s->header_count > 1)
  107. avctx->sample_rate= sr;
  108. }
  109. } else
  110. #if 0
  111. if (s->frame_size == -1) {
  112. /* free format : find next sync to compute frame size */
  113. len = MPA_MAX_CODED_FRAME_SIZE - len;
  114. if (len > buf_size)
  115. len = buf_size;
  116. if (len == 0) {
  117. /* frame too long: resync */
  118. s->frame_size = 0;
  119. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  120. s->inbuf_ptr--;
  121. } else {
  122. uint8_t *p, *pend;
  123. uint32_t header1;
  124. int padding;
  125. memcpy(s->inbuf_ptr, buf_ptr, len);
  126. /* check for header */
  127. p = s->inbuf_ptr - 3;
  128. pend = s->inbuf_ptr + len - 4;
  129. while (p <= pend) {
  130. header = (p[0] << 24) | (p[1] << 16) |
  131. (p[2] << 8) | p[3];
  132. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  133. (s->inbuf[2] << 8) | s->inbuf[3];
  134. /* check with high probability that we have a
  135. valid header */
  136. if ((header & SAME_HEADER_MASK) ==
  137. (header1 & SAME_HEADER_MASK)) {
  138. /* header found: update pointers */
  139. len = (p + 4) - s->inbuf_ptr;
  140. buf_ptr += len;
  141. buf_size -= len;
  142. s->inbuf_ptr = p;
  143. /* compute frame size */
  144. s->free_format_next_header = header;
  145. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  146. padding = (header1 >> 9) & 1;
  147. if (s->layer == 1)
  148. s->free_format_frame_size -= padding * 4;
  149. else
  150. s->free_format_frame_size -= padding;
  151. dprintf(avctx, "free frame size=%d padding=%d\n",
  152. s->free_format_frame_size, padding);
  153. decode_header(s, header1);
  154. goto next_data;
  155. }
  156. p++;
  157. }
  158. /* not found: simply increase pointers */
  159. buf_ptr += len;
  160. s->inbuf_ptr += len;
  161. buf_size -= len;
  162. }
  163. } else
  164. #endif
  165. if (len < s->frame_size) {
  166. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  167. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  168. len = FFMIN(s->frame_size - len, buf_size);
  169. memcpy(s->inbuf_ptr, buf_ptr, len);
  170. buf_ptr += len;
  171. s->inbuf_ptr += len;
  172. buf_size -= len;
  173. }
  174. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  175. && buf_size + buf_ptr - buf >= s->frame_size){
  176. if(s->header_count > 0){
  177. *poutbuf = buf;
  178. *poutbuf_size = s->frame_size;
  179. }
  180. buf_ptr = buf + s->frame_size;
  181. s->inbuf_ptr = s->inbuf;
  182. s->frame_size = 0;
  183. break;
  184. }
  185. // next_data:
  186. if (s->frame_size > 0 &&
  187. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  188. if(s->header_count > 0){
  189. *poutbuf = s->inbuf;
  190. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  191. }
  192. s->inbuf_ptr = s->inbuf;
  193. s->frame_size = 0;
  194. break;
  195. }
  196. }
  197. return buf_ptr - buf;
  198. }
  199. AVCodecParser mpegaudio_parser = {
  200. { CODEC_ID_MP2, CODEC_ID_MP3 },
  201. sizeof(MpegAudioParseContext),
  202. mpegaudio_parse_init,
  203. mpegaudio_parse,
  204. NULL,
  205. };