00001 /* 00002 * MPEG Audio parser 00003 * Copyright (c) 2003 Fabrice Bellard 00004 * Copyright (c) 2003 Michael Niedermayer 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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]; /* input buffer */ 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 /* header + layer + bitrate + freq + lsf/mpeg25 */ 00041 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ 00042 #define SAME_HEADER_MASK \ 00043 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) 00044 00045 /* useful helper to get mpeg audio stream infos. Return -1 if error in 00046 header, otherwise the coded frame size in bytes */ 00047 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate) 00048 { 00049 MPADecodeHeader s1, *s = &s1; 00050 00051 if (ff_mpa_check_header(head) != 0) 00052 return -1; 00053 00054 if (ff_mpegaudio_decode_header(s, head) != 0) { 00055 return -1; 00056 } 00057 00058 switch(s->layer) { 00059 case 1: 00060 avctx->codec_id = CODEC_ID_MP1; 00061 *frame_size = 384; 00062 break; 00063 case 2: 00064 avctx->codec_id = CODEC_ID_MP2; 00065 *frame_size = 1152; 00066 break; 00067 default: 00068 case 3: 00069 avctx->codec_id = CODEC_ID_MP3; 00070 if (s->lsf) 00071 *frame_size = 576; 00072 else 00073 *frame_size = 1152; 00074 break; 00075 } 00076 00077 *sample_rate = s->sample_rate; 00078 *channels = s->nb_channels; 00079 *bit_rate = s->bit_rate; 00080 avctx->sub_id = s->layer; 00081 return s->frame_size; 00082 } 00083 00084 static av_cold int mpegaudio_parse_init(AVCodecParserContext *s1) 00085 { 00086 MpegAudioParseContext *s = s1->priv_data; 00087 s->inbuf_ptr = s->inbuf; 00088 return 0; 00089 } 00090 00091 static int mpegaudio_parse(AVCodecParserContext *s1, 00092 AVCodecContext *avctx, 00093 const uint8_t **poutbuf, int *poutbuf_size, 00094 const uint8_t *buf, int buf_size) 00095 { 00096 MpegAudioParseContext *s = s1->priv_data; 00097 int len, ret, sr, channels, bit_rate, frame_size; 00098 uint32_t header; 00099 const uint8_t *buf_ptr; 00100 00101 *poutbuf = NULL; 00102 *poutbuf_size = 0; 00103 buf_ptr = buf; 00104 while (buf_size > 0) { 00105 len = s->inbuf_ptr - s->inbuf; 00106 if (s->frame_size == 0) { 00107 /* special case for next header for first frame in free 00108 format case (XXX: find a simpler method) */ 00109 if (s->free_format_next_header != 0) { 00110 AV_WB32(s->inbuf, s->free_format_next_header); 00111 s->inbuf_ptr = s->inbuf + 4; 00112 s->free_format_next_header = 0; 00113 goto got_header; 00114 } 00115 /* no header seen : find one. We need at least MPA_HEADER_SIZE 00116 bytes to parse it */ 00117 len = FFMIN(MPA_HEADER_SIZE - len, buf_size); 00118 if (len > 0) { 00119 memcpy(s->inbuf_ptr, buf_ptr, len); 00120 buf_ptr += len; 00121 buf_size -= len; 00122 s->inbuf_ptr += len; 00123 } 00124 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { 00125 got_header: 00126 header = AV_RB32(s->inbuf); 00127 00128 ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate); 00129 if (ret < 0) { 00130 s->header_count= -2; 00131 /* no sync found : move by one byte (inefficient, but simple!) */ 00132 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); 00133 s->inbuf_ptr--; 00134 dprintf(avctx, "skip %x\n", header); 00135 /* reset free format frame size to give a chance 00136 to get a new bitrate */ 00137 s->free_format_frame_size = 0; 00138 } else { 00139 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) 00140 s->header_count= -3; 00141 s->header= header; 00142 s->header_count++; 00143 s->frame_size = ret; 00144 00145 #if 0 00146 /* free format: prepare to compute frame size */ 00147 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) { 00148 s->frame_size = -1; 00149 } 00150 #endif 00151 if(s->header_count > 1){ 00152 avctx->sample_rate= sr; 00153 avctx->channels = channels; 00154 avctx->frame_size = frame_size; 00155 avctx->bit_rate = bit_rate; 00156 } 00157 } 00158 } 00159 } else 00160 #if 0 00161 if (s->frame_size == -1) { 00162 /* free format : find next sync to compute frame size */ 00163 len = MPA_MAX_CODED_FRAME_SIZE - len; 00164 if (len > buf_size) 00165 len = buf_size; 00166 if (len == 0) { 00167 /* frame too long: resync */ 00168 s->frame_size = 0; 00169 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); 00170 s->inbuf_ptr--; 00171 } else { 00172 uint8_t *p, *pend; 00173 uint32_t header1; 00174 int padding; 00175 00176 memcpy(s->inbuf_ptr, buf_ptr, len); 00177 /* check for header */ 00178 p = s->inbuf_ptr - 3; 00179 pend = s->inbuf_ptr + len - 4; 00180 while (p <= pend) { 00181 header = AV_RB32(p); 00182 header1 = AV_RB32(s->inbuf); 00183 /* check with high probability that we have a 00184 valid header */ 00185 if ((header & SAME_HEADER_MASK) == 00186 (header1 & SAME_HEADER_MASK)) { 00187 /* header found: update pointers */ 00188 len = (p + 4) - s->inbuf_ptr; 00189 buf_ptr += len; 00190 buf_size -= len; 00191 s->inbuf_ptr = p; 00192 /* compute frame size */ 00193 s->free_format_next_header = header; 00194 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; 00195 padding = (header1 >> 9) & 1; 00196 if (s->layer == 1) 00197 s->free_format_frame_size -= padding * 4; 00198 else 00199 s->free_format_frame_size -= padding; 00200 dprintf(avctx, "free frame size=%d padding=%d\n", 00201 s->free_format_frame_size, padding); 00202 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header1); 00203 goto next_data; 00204 } 00205 p++; 00206 } 00207 /* not found: simply increase pointers */ 00208 buf_ptr += len; 00209 s->inbuf_ptr += len; 00210 buf_size -= len; 00211 } 00212 } else 00213 #endif 00214 if (len < s->frame_size) { 00215 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) 00216 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; 00217 len = FFMIN(s->frame_size - len, buf_size); 00218 memcpy(s->inbuf_ptr, buf_ptr, len); 00219 buf_ptr += len; 00220 s->inbuf_ptr += len; 00221 buf_size -= len; 00222 } 00223 00224 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf 00225 && buf_size + buf_ptr - buf >= s->frame_size){ 00226 if(s->header_count > 0){ 00227 *poutbuf = buf; 00228 *poutbuf_size = s->frame_size; 00229 } 00230 buf_ptr = buf + s->frame_size; 00231 s->inbuf_ptr = s->inbuf; 00232 s->frame_size = 0; 00233 break; 00234 } 00235 00236 // next_data: 00237 if (s->frame_size > 0 && 00238 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { 00239 if(s->header_count > 0){ 00240 *poutbuf = s->inbuf; 00241 *poutbuf_size = s->inbuf_ptr - s->inbuf; 00242 } 00243 s->inbuf_ptr = s->inbuf; 00244 s->frame_size = 0; 00245 break; 00246 } 00247 } 00248 return buf_ptr - buf; 00249 } 00250 00251 00252 AVCodecParser mpegaudio_parser = { 00253 { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 }, 00254 sizeof(MpegAudioParseContext), 00255 mpegaudio_parse_init, 00256 mpegaudio_parse, 00257 NULL, 00258 };