3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2003 Michael Niedermayer.
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "mpegaudio.h"
25 #include "mpegaudiodecheader.h"
28 typedef struct MpegAudioParseContext {
29 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
32 int free_format_frame_size;
33 int free_format_next_header;
36 } MpegAudioParseContext;
38 #define MPA_HEADER_SIZE 4
40 /* header + layer + bitrate + freq + lsf/mpeg25 */
41 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
42 #define SAME_HEADER_MASK \
43 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
45 /* useful helper to get mpeg audio stream infos. Return -1 if error in
46 header, otherwise the coded frame size in bytes */
47 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
49 MPADecodeContext s1, *s = &s1;
52 if (ff_mpa_check_header(head) != 0)
55 if (ff_mpegaudio_decode_header(s, head) != 0) {
61 avctx->codec_id = CODEC_ID_MP1;
65 avctx->codec_id = CODEC_ID_MP2;
70 avctx->codec_id = CODEC_ID_MP3;
78 *sample_rate = s->sample_rate;
79 *channels = s->nb_channels;
80 *bit_rate = s->bit_rate;
81 avctx->sub_id = s->layer;
85 static int mpegaudio_parse_init(AVCodecParserContext *s1)
87 MpegAudioParseContext *s = s1->priv_data;
88 s->inbuf_ptr = s->inbuf;
92 static int mpegaudio_parse(AVCodecParserContext *s1,
93 AVCodecContext *avctx,
94 const uint8_t **poutbuf, int *poutbuf_size,
95 const uint8_t *buf, int buf_size)
97 MpegAudioParseContext *s = s1->priv_data;
98 int len, ret, sr, channels, bit_rate, frame_size;
100 const uint8_t *buf_ptr;
105 while (buf_size > 0) {
106 len = s->inbuf_ptr - s->inbuf;
107 if (s->frame_size == 0) {
108 /* special case for next header for first frame in free
109 format case (XXX: find a simpler method) */
110 if (s->free_format_next_header != 0) {
111 AV_WB32(s->inbuf, s->free_format_next_header);
112 s->inbuf_ptr = s->inbuf + 4;
113 s->free_format_next_header = 0;
116 /* no header seen : find one. We need at least MPA_HEADER_SIZE
118 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
120 memcpy(s->inbuf_ptr, buf_ptr, len);
125 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
127 header = AV_RB32(s->inbuf);
129 ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate);
132 /* no sync found : move by one byte (inefficient, but simple!) */
133 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
135 dprintf(avctx, "skip %x\n", header);
136 /* reset free format frame size to give a chance
137 to get a new bitrate */
138 s->free_format_frame_size = 0;
140 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
147 /* free format: prepare to compute frame size */
148 if (ff_mpegaudio_decode_header(s, header) == 1) {
152 if(s->header_count > 1){
153 avctx->sample_rate= sr;
154 avctx->channels = channels;
155 avctx->frame_size = frame_size;
156 avctx->bit_rate = bit_rate;
162 if (s->frame_size == -1) {
163 /* free format : find next sync to compute frame size */
164 len = MPA_MAX_CODED_FRAME_SIZE - len;
168 /* frame too long: resync */
170 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
177 memcpy(s->inbuf_ptr, buf_ptr, len);
178 /* check for header */
179 p = s->inbuf_ptr - 3;
180 pend = s->inbuf_ptr + len - 4;
183 header1 = AV_RB32(s->inbuf);
184 /* check with high probability that we have a
186 if ((header & SAME_HEADER_MASK) ==
187 (header1 & SAME_HEADER_MASK)) {
188 /* header found: update pointers */
189 len = (p + 4) - s->inbuf_ptr;
193 /* compute frame size */
194 s->free_format_next_header = header;
195 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
196 padding = (header1 >> 9) & 1;
198 s->free_format_frame_size -= padding * 4;
200 s->free_format_frame_size -= padding;
201 dprintf(avctx, "free frame size=%d padding=%d\n",
202 s->free_format_frame_size, padding);
203 ff_mpegaudio_decode_header(s, header1);
208 /* not found: simply increase pointers */
215 if (len < s->frame_size) {
216 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
217 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
218 len = FFMIN(s->frame_size - len, buf_size);
219 memcpy(s->inbuf_ptr, buf_ptr, len);
225 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
226 && buf_size + buf_ptr - buf >= s->frame_size){
227 if(s->header_count > 0){
229 *poutbuf_size = s->frame_size;
231 buf_ptr = buf + s->frame_size;
232 s->inbuf_ptr = s->inbuf;
238 if (s->frame_size > 0 &&
239 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
240 if(s->header_count > 0){
242 *poutbuf_size = s->inbuf_ptr - s->inbuf;
244 s->inbuf_ptr = s->inbuf;
249 return buf_ptr - buf;
253 AVCodecParser mpegaudio_parser = {
254 { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
255 sizeof(MpegAudioParseContext),
256 mpegaudio_parse_init,