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.

260 lines
8.9KB

  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, int *channels, int *frame_size, int *bit_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->codec_id = CODEC_ID_MP1;
  53. *frame_size = 384;
  54. break;
  55. case 2:
  56. avctx->codec_id = CODEC_ID_MP2;
  57. *frame_size = 1152;
  58. break;
  59. default:
  60. case 3:
  61. avctx->codec_id = CODEC_ID_MP3;
  62. if (s->lsf)
  63. *frame_size = 576;
  64. else
  65. *frame_size = 1152;
  66. break;
  67. }
  68. *sample_rate = s->sample_rate;
  69. *channels = s->nb_channels;
  70. *bit_rate = s->bit_rate;
  71. avctx->sub_id = s->layer;
  72. return s->frame_size;
  73. }
  74. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  75. {
  76. MpegAudioParseContext *s = s1->priv_data;
  77. s->inbuf_ptr = s->inbuf;
  78. return 0;
  79. }
  80. static int mpegaudio_parse(AVCodecParserContext *s1,
  81. AVCodecContext *avctx,
  82. const uint8_t **poutbuf, int *poutbuf_size,
  83. const uint8_t *buf, int buf_size)
  84. {
  85. MpegAudioParseContext *s = s1->priv_data;
  86. int len, ret, sr, channels, bit_rate, frame_size;
  87. uint32_t header;
  88. const uint8_t *buf_ptr;
  89. *poutbuf = NULL;
  90. *poutbuf_size = 0;
  91. buf_ptr = buf;
  92. while (buf_size > 0) {
  93. len = s->inbuf_ptr - s->inbuf;
  94. if (s->frame_size == 0) {
  95. /* special case for next header for first frame in free
  96. format case (XXX: find a simpler method) */
  97. if (s->free_format_next_header != 0) {
  98. AV_WB32(s->inbuf, 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 = AV_RB32(s->inbuf);
  115. ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate);
  116. if (ret < 0) {
  117. s->header_count= -2;
  118. /* no sync found : move by one byte (inefficient, but simple!) */
  119. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  120. s->inbuf_ptr--;
  121. dprintf(avctx, "skip %x\n", header);
  122. /* reset free format frame size to give a chance
  123. to get a new bitrate */
  124. s->free_format_frame_size = 0;
  125. } else {
  126. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  127. s->header_count= -3;
  128. s->header= header;
  129. s->header_count++;
  130. s->frame_size = ret;
  131. #if 0
  132. /* free format: prepare to compute frame size */
  133. if (ff_mpegaudio_decode_header(s, header) == 1) {
  134. s->frame_size = -1;
  135. }
  136. #endif
  137. if(s->header_count > 1){
  138. avctx->sample_rate= sr;
  139. avctx->channels = channels;
  140. avctx->frame_size = frame_size;
  141. avctx->bit_rate = bit_rate;
  142. }
  143. }
  144. }
  145. } else
  146. #if 0
  147. if (s->frame_size == -1) {
  148. /* free format : find next sync to compute frame size */
  149. len = MPA_MAX_CODED_FRAME_SIZE - len;
  150. if (len > buf_size)
  151. len = buf_size;
  152. if (len == 0) {
  153. /* frame too long: resync */
  154. s->frame_size = 0;
  155. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  156. s->inbuf_ptr--;
  157. } else {
  158. uint8_t *p, *pend;
  159. uint32_t header1;
  160. int padding;
  161. memcpy(s->inbuf_ptr, buf_ptr, len);
  162. /* check for header */
  163. p = s->inbuf_ptr - 3;
  164. pend = s->inbuf_ptr + len - 4;
  165. while (p <= pend) {
  166. header = AV_RB32(p);
  167. header1 = AV_RB32(s->inbuf);
  168. /* check with high probability that we have a
  169. valid header */
  170. if ((header & SAME_HEADER_MASK) ==
  171. (header1 & SAME_HEADER_MASK)) {
  172. /* header found: update pointers */
  173. len = (p + 4) - s->inbuf_ptr;
  174. buf_ptr += len;
  175. buf_size -= len;
  176. s->inbuf_ptr = p;
  177. /* compute frame size */
  178. s->free_format_next_header = header;
  179. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  180. padding = (header1 >> 9) & 1;
  181. if (s->layer == 1)
  182. s->free_format_frame_size -= padding * 4;
  183. else
  184. s->free_format_frame_size -= padding;
  185. dprintf(avctx, "free frame size=%d padding=%d\n",
  186. s->free_format_frame_size, padding);
  187. ff_mpegaudio_decode_header(s, header1);
  188. goto next_data;
  189. }
  190. p++;
  191. }
  192. /* not found: simply increase pointers */
  193. buf_ptr += len;
  194. s->inbuf_ptr += len;
  195. buf_size -= len;
  196. }
  197. } else
  198. #endif
  199. if (len < s->frame_size) {
  200. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  201. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  202. len = FFMIN(s->frame_size - len, buf_size);
  203. memcpy(s->inbuf_ptr, buf_ptr, len);
  204. buf_ptr += len;
  205. s->inbuf_ptr += len;
  206. buf_size -= len;
  207. }
  208. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  209. && buf_size + buf_ptr - buf >= s->frame_size){
  210. if(s->header_count > 0){
  211. *poutbuf = buf;
  212. *poutbuf_size = s->frame_size;
  213. }
  214. buf_ptr = buf + s->frame_size;
  215. s->inbuf_ptr = s->inbuf;
  216. s->frame_size = 0;
  217. break;
  218. }
  219. // next_data:
  220. if (s->frame_size > 0 &&
  221. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  222. if(s->header_count > 0){
  223. *poutbuf = s->inbuf;
  224. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  225. }
  226. s->inbuf_ptr = s->inbuf;
  227. s->frame_size = 0;
  228. break;
  229. }
  230. }
  231. return buf_ptr - buf;
  232. }
  233. AVCodecParser mpegaudio_parser = {
  234. { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
  235. sizeof(MpegAudioParseContext),
  236. mpegaudio_parse_init,
  237. mpegaudio_parse,
  238. NULL,
  239. };