3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2003 Michael Niedermayer
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "mpegaudiodecheader.h"
27 typedef struct MpegAudioParseContext {
32 } MpegAudioParseContext;
34 #define MPA_HEADER_SIZE 4
36 /* header + layer + bitrate + freq + lsf/mpeg25 */
37 #define SAME_HEADER_MASK \
38 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
40 static int mpegaudio_parse(AVCodecParserContext *s1,
41 AVCodecContext *avctx,
42 const uint8_t **poutbuf, int *poutbuf_size,
43 const uint8_t *buf, int buf_size)
45 MpegAudioParseContext *s = s1->priv_data;
46 ParseContext *pc = &s->pc;
47 uint32_t state= pc->state;
49 int next= END_NOT_FOUND;
51 for(i=0; i<buf_size; ){
53 int inc= FFMIN(buf_size - i, s->frame_size);
63 int ret, sr, channels, bit_rate, frame_size;
65 state= (state<<8) + buf[i++];
67 ret = avpriv_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
72 if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
76 s->frame_size = ret-4;
78 if (s->header_count > 0) {
79 avctx->sample_rate= sr;
80 avctx->channels = channels;
81 s1->duration = frame_size;
82 avctx->bit_rate = bit_rate;
91 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
98 *poutbuf_size = buf_size;
103 AVCodecParser ff_mpegaudio_parser = {
104 .codec_ids = { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
105 .priv_data_size = sizeof(MpegAudioParseContext),
106 .parser_parse = mpegaudio_parse,
107 .parser_close = ff_parse_close,