00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "parser.h"
00024 #include "mpegaudio.h"
00025 #include "mpegaudiodecheader.h"
00026
00027
00028 typedef struct MpegAudioParseContext {
00029 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];
00030 uint8_t *inbuf_ptr;
00031 int frame_size;
00032 int free_format_frame_size;
00033 int free_format_next_header;
00034 uint32_t header;
00035 int header_count;
00036 } MpegAudioParseContext;
00037
00038 #define MPA_HEADER_SIZE 4
00039
00040
00041 #undef SAME_HEADER_MASK
00042 #define SAME_HEADER_MASK \
00043 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
00044
00045
00046
00047 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
00048 {
00049 MPADecodeContext s1, *s = &s1;
00050 s1.avctx = avctx;
00051
00052 if (ff_mpa_check_header(head) != 0)
00053 return -1;
00054
00055 if (ff_mpegaudio_decode_header(s, head) != 0) {
00056 return -1;
00057 }
00058
00059 switch(s->layer) {
00060 case 1:
00061 avctx->frame_size = 384;
00062 break;
00063 case 2:
00064 avctx->frame_size = 1152;
00065 break;
00066 default:
00067 case 3:
00068 if (s->lsf)
00069 avctx->frame_size = 576;
00070 else
00071 avctx->frame_size = 1152;
00072 break;
00073 }
00074
00075 *sample_rate = s->sample_rate;
00076 avctx->channels = s->nb_channels;
00077 avctx->bit_rate = s->bit_rate;
00078 avctx->sub_id = s->layer;
00079 avctx->avcodec_dual_language = (s->mode == MPA_DUAL) ? 1 : 0;
00080 return s->frame_size;
00081 }
00082
00083 static int mpegaudio_parse_init(AVCodecParserContext *s1)
00084 {
00085 MpegAudioParseContext *s = s1->priv_data;
00086 s->inbuf_ptr = s->inbuf;
00087 return 0;
00088 }
00089
00090 static int mpegaudio_parse(AVCodecParserContext *s1,
00091 AVCodecContext *avctx,
00092 const uint8_t **poutbuf, int *poutbuf_size,
00093 const uint8_t *buf, int buf_size)
00094 {
00095 MpegAudioParseContext *s = s1->priv_data;
00096 int len, ret, sr;
00097 uint32_t header;
00098 const uint8_t *buf_ptr;
00099
00100 *poutbuf = NULL;
00101 *poutbuf_size = 0;
00102 buf_ptr = buf;
00103 while (buf_size > 0) {
00104 len = s->inbuf_ptr - s->inbuf;
00105 if (s->frame_size == 0) {
00106
00107
00108 if (s->free_format_next_header != 0) {
00109 AV_WB32(s->inbuf, s->free_format_next_header);
00110 s->inbuf_ptr = s->inbuf + 4;
00111 s->free_format_next_header = 0;
00112 goto got_header;
00113 }
00114
00115
00116 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
00117 if (len > 0) {
00118 memcpy(s->inbuf_ptr, buf_ptr, len);
00119 buf_ptr += len;
00120 buf_size -= len;
00121 s->inbuf_ptr += len;
00122 }
00123 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
00124 got_header:
00125 header = AV_RB32(s->inbuf);
00126
00127 ret = ff_mpa_decode_header(avctx, header, &sr);
00128 if (ret < 0) {
00129 s->header_count= -2;
00130
00131 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00132 s->inbuf_ptr--;
00133 dprintf(avctx, "skip %x\n", header);
00134
00135
00136 s->free_format_frame_size = 0;
00137 } else {
00138 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
00139 s->header_count= -3;
00140 s->header= header;
00141 s->header_count++;
00142 s->frame_size = ret;
00143
00144 #if 0
00145
00146 if (ff_mpegaudio_decode_header(s, header) == 1) {
00147 s->frame_size = -1;
00148 }
00149 #endif
00150 if(s->header_count > 1)
00151 avctx->sample_rate= sr;
00152 }
00153 }
00154 } else
00155 #if 0
00156 if (s->frame_size == -1) {
00157
00158 len = MPA_MAX_CODED_FRAME_SIZE - len;
00159 if (len > buf_size)
00160 len = buf_size;
00161 if (len == 0) {
00162
00163 s->frame_size = 0;
00164 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00165 s->inbuf_ptr--;
00166 } else {
00167 uint8_t *p, *pend;
00168 uint32_t header1;
00169 int padding;
00170
00171 memcpy(s->inbuf_ptr, buf_ptr, len);
00172
00173 p = s->inbuf_ptr - 3;
00174 pend = s->inbuf_ptr + len - 4;
00175 while (p <= pend) {
00176 header = AV_RB32(p);
00177 header1 = AV_RB32(s->inbuf);
00178
00179
00180 if ((header & SAME_HEADER_MASK) ==
00181 (header1 & SAME_HEADER_MASK)) {
00182
00183 len = (p + 4) - s->inbuf_ptr;
00184 buf_ptr += len;
00185 buf_size -= len;
00186 s->inbuf_ptr = p;
00187
00188 s->free_format_next_header = header;
00189 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
00190 padding = (header1 >> 9) & 1;
00191 if (s->layer == 1)
00192 s->free_format_frame_size -= padding * 4;
00193 else
00194 s->free_format_frame_size -= padding;
00195 dprintf(avctx, "free frame size=%d padding=%d\n",
00196 s->free_format_frame_size, padding);
00197 ff_mpegaudio_decode_header(s, header1);
00198 goto next_data;
00199 }
00200 p++;
00201 }
00202
00203 buf_ptr += len;
00204 s->inbuf_ptr += len;
00205 buf_size -= len;
00206 }
00207 } else
00208 #endif
00209 if (len < s->frame_size) {
00210 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
00211 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
00212 len = FFMIN(s->frame_size - len, buf_size);
00213 memcpy(s->inbuf_ptr, buf_ptr, len);
00214 buf_ptr += len;
00215 s->inbuf_ptr += len;
00216 buf_size -= len;
00217 }
00218
00219 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
00220 && buf_size + buf_ptr - buf >= s->frame_size){
00221 if(s->header_count > 0){
00222 *poutbuf = buf;
00223 *poutbuf_size = s->frame_size;
00224 }
00225 buf_ptr = buf + s->frame_size;
00226 s->inbuf_ptr = s->inbuf;
00227 s->frame_size = 0;
00228 break;
00229 }
00230
00231
00232 if (s->frame_size > 0 &&
00233 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
00234 if(s->header_count > 0){
00235 *poutbuf = s->inbuf;
00236 *poutbuf_size = s->inbuf_ptr - s->inbuf;
00237 }
00238 s->inbuf_ptr = s->inbuf;
00239 s->frame_size = 0;
00240 break;
00241 }
00242 }
00243 return buf_ptr - buf;
00244 }
00245
00246
00247 AVCodecParser mpegaudio_parser = {
00248 { CODEC_ID_MP2, CODEC_ID_MP3 },
00249 sizeof(MpegAudioParseContext),
00250 mpegaudio_parse_init,
00251 mpegaudio_parse,
00252 NULL,
00253 };