mpegaudio_parser.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. MPADecodeHeader s1, *s = &s1;
  44. if (ff_mpa_check_header(head) != 0)
  45. return -1;
  46. if (ff_mpegaudio_decode_header(s, head) != 0) {
  47. return -1;
  48. }
  49. switch(s->layer) {
  50. case 1:
  51. avctx->codec_id = CODEC_ID_MP1;
  52. *frame_size = 384;
  53. break;
  54. case 2:
  55. avctx->codec_id = CODEC_ID_MP2;
  56. *frame_size = 1152;
  57. break;
  58. default:
  59. case 3:
  60. avctx->codec_id = CODEC_ID_MP3;
  61. if (s->lsf)
  62. *frame_size = 576;
  63. else
  64. *frame_size = 1152;
  65. break;
  66. }
  67. *sample_rate = s->sample_rate;
  68. *channels = s->nb_channels;
  69. *bit_rate = s->bit_rate;
  70. avctx->sub_id = s->layer;
  71. return s->frame_size;
  72. }
  73. static av_cold int mpegaudio_parse_init(AVCodecParserContext *s1)
  74. {
  75. MpegAudioParseContext *s = s1->priv_data;
  76. s->inbuf_ptr = s->inbuf;
  77. return 0;
  78. }
  79. static int mpegaudio_parse(AVCodecParserContext *s1,
  80. AVCodecContext *avctx,
  81. const uint8_t **poutbuf, int *poutbuf_size,
  82. const uint8_t *buf, int buf_size)
  83. {
  84. MpegAudioParseContext *s = s1->priv_data;
  85. int len, ret, sr, channels, bit_rate, frame_size;
  86. uint32_t header;
  87. const uint8_t *buf_ptr;
  88. *poutbuf = NULL;
  89. *poutbuf_size = 0;
  90. buf_ptr = buf;
  91. while (buf_size > 0) {
  92. len = s->inbuf_ptr - s->inbuf;
  93. if (s->frame_size == 0) {
  94. /* special case for next header for first frame in free
  95. format case (XXX: find a simpler method) */
  96. if (s->free_format_next_header != 0) {
  97. AV_WB32(s->inbuf, s->free_format_next_header);
  98. s->inbuf_ptr = s->inbuf + 4;
  99. s->free_format_next_header = 0;
  100. goto got_header;
  101. }
  102. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  103. bytes to parse it */
  104. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  105. if (len > 0) {
  106. memcpy(s->inbuf_ptr, buf_ptr, len);
  107. buf_ptr += len;
  108. buf_size -= len;
  109. s->inbuf_ptr += len;
  110. }
  111. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  112. got_header:
  113. header = AV_RB32(s->inbuf);
  114. ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate);
  115. if (ret < 0) {
  116. s->header_count= -2;
  117. /* no sync found : move by one byte (inefficient, but simple!) */
  118. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  119. s->inbuf_ptr--;
  120. dprintf(avctx, "skip %x\n", header);
  121. /* reset free format frame size to give a chance
  122. to get a new bitrate */
  123. s->free_format_frame_size = 0;
  124. } else {
  125. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  126. s->header_count= -3;
  127. s->header= header;
  128. s->header_count++;
  129. s->frame_size = ret;
  130. #if 0
  131. /* free format: prepare to compute frame size */
  132. if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
  133. s->frame_size = -1;
  134. }
  135. #endif
  136. if(s->header_count > 1){
  137. avctx->sample_rate= sr;
  138. avctx->channels = channels;
  139. avctx->frame_size = frame_size;
  140. avctx->bit_rate = bit_rate;
  141. }
  142. }
  143. }
  144. } else
  145. #if 0
  146. if (s->frame_size == -1) {
  147. /* free format : find next sync to compute frame size */
  148. len = MPA_MAX_CODED_FRAME_SIZE - len;
  149. if (len > buf_size)
  150. len = buf_size;
  151. if (len == 0) {
  152. /* frame too long: resync */
  153. s->frame_size = 0;
  154. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  155. s->inbuf_ptr--;
  156. } else {
  157. uint8_t *p, *pend;
  158. uint32_t header1;
  159. int padding;
  160. memcpy(s->inbuf_ptr, buf_ptr, len);
  161. /* check for header */
  162. p = s->inbuf_ptr - 3;
  163. pend = s->inbuf_ptr + len - 4;
  164. while (p <= pend) {
  165. header = AV_RB32(p);
  166. header1 = AV_RB32(s->inbuf);
  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. ff_mpegaudio_decode_header((MPADecodeHeader *)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_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
  234. sizeof(MpegAudioParseContext),
  235. mpegaudio_parse_init,
  236. mpegaudio_parse,
  237. NULL,
  238. };