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 return s->frame_size;
00080 }
00081
00082 static int mpegaudio_parse_init(AVCodecParserContext *s1)
00083 {
00084 MpegAudioParseContext *s = s1->priv_data;
00085 s->inbuf_ptr = s->inbuf;
00086 return 0;
00087 }
00088
00089 static int mpegaudio_parse(AVCodecParserContext *s1,
00090 AVCodecContext *avctx,
00091 const uint8_t **poutbuf, int *poutbuf_size,
00092 const uint8_t *buf, int buf_size)
00093 {
00094 MpegAudioParseContext *s = s1->priv_data;
00095 int len, ret, sr;
00096 uint32_t header;
00097 const uint8_t *buf_ptr;
00098
00099 *poutbuf = NULL;
00100 *poutbuf_size = 0;
00101 buf_ptr = buf;
00102 while (buf_size > 0) {
00103 len = s->inbuf_ptr - s->inbuf;
00104 if (s->frame_size == 0) {
00105
00106
00107 if (s->free_format_next_header != 0) {
00108 AV_WB32(s->inbuf, s->free_format_next_header);
00109 s->inbuf_ptr = s->inbuf + 4;
00110 s->free_format_next_header = 0;
00111 goto got_header;
00112 }
00113
00114
00115 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
00116 if (len > 0) {
00117 memcpy(s->inbuf_ptr, buf_ptr, len);
00118 buf_ptr += len;
00119 buf_size -= len;
00120 s->inbuf_ptr += len;
00121 }
00122 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
00123 got_header:
00124 header = AV_RB32(s->inbuf);
00125
00126 ret = ff_mpa_decode_header(avctx, header, &sr);
00127 if (ret < 0) {
00128 s->header_count= -2;
00129
00130 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00131 s->inbuf_ptr--;
00132 dprintf(avctx, "skip %x\n", header);
00133
00134
00135 s->free_format_frame_size = 0;
00136 } else {
00137 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
00138 s->header_count= -3;
00139 s->header= header;
00140 s->header_count++;
00141 s->frame_size = ret;
00142
00143 #if 0
00144
00145 if (ff_mpegaudio_decode_header(s, header) == 1) {
00146 s->frame_size = -1;
00147 }
00148 #endif
00149 if(s->header_count > 1)
00150 avctx->sample_rate= sr;
00151 }
00152 }
00153 } else
00154 #if 0
00155 if (s->frame_size == -1) {
00156
00157 len = MPA_MAX_CODED_FRAME_SIZE - len;
00158 if (len > buf_size)
00159 len = buf_size;
00160 if (len == 0) {
00161
00162 s->frame_size = 0;
00163 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
00164 s->inbuf_ptr--;
00165 } else {
00166 uint8_t *p, *pend;
00167 uint32_t header1;
00168 int padding;
00169
00170 memcpy(s->inbuf_ptr, buf_ptr, len);
00171
00172 p = s->inbuf_ptr - 3;
00173 pend = s->inbuf_ptr + len - 4;
00174 while (p <= pend) {
00175 header = AV_RB32(p);
00176 header1 = AV_RB32(s->inbuf);
00177
00178
00179 if ((header & SAME_HEADER_MASK) ==
00180 (header1 & SAME_HEADER_MASK)) {
00181
00182 len = (p + 4) - s->inbuf_ptr;
00183 buf_ptr += len;
00184 buf_size -= len;
00185 s->inbuf_ptr = p;
00186
00187 s->free_format_next_header = header;
00188 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
00189 padding = (header1 >> 9) & 1;
00190 if (s->layer == 1)
00191 s->free_format_frame_size -= padding * 4;
00192 else
00193 s->free_format_frame_size -= padding;
00194 dprintf(avctx, "free frame size=%d padding=%d\n",
00195 s->free_format_frame_size, padding);
00196 ff_mpegaudio_decode_header(s, header1);
00197 goto next_data;
00198 }
00199 p++;
00200 }
00201
00202 buf_ptr += len;
00203 s->inbuf_ptr += len;
00204 buf_size -= len;
00205 }
00206 } else
00207 #endif
00208 if (len < s->frame_size) {
00209 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
00210 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
00211 len = FFMIN(s->frame_size - len, buf_size);
00212 memcpy(s->inbuf_ptr, buf_ptr, len);
00213 buf_ptr += len;
00214 s->inbuf_ptr += len;
00215 buf_size -= len;
00216 }
00217
00218 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
00219 && buf_size + buf_ptr - buf >= s->frame_size){
00220 if(s->header_count > 0){
00221 *poutbuf = buf;
00222 *poutbuf_size = s->frame_size;
00223 }
00224 buf_ptr = buf + s->frame_size;
00225 s->inbuf_ptr = s->inbuf;
00226 s->frame_size = 0;
00227 break;
00228 }
00229
00230
00231 if (s->frame_size > 0 &&
00232 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
00233 if(s->header_count > 0){
00234 *poutbuf = s->inbuf;
00235 *poutbuf_size = s->inbuf_ptr - s->inbuf;
00236 }
00237 s->inbuf_ptr = s->inbuf;
00238 s->frame_size = 0;
00239 break;
00240 }
00241 }
00242 return buf_ptr - buf;
00243 }
00244
00245
00246 AVCodecParser mpegaudio_parser = {
00247 { CODEC_ID_MP2, CODEC_ID_MP3 },
00248 sizeof(MpegAudioParseContext),
00249 mpegaudio_parse_init,
00250 mpegaudio_parse,
00251 NULL,
00252 };