]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudio_parser.c
Replace all CODEC_ID_* with AV_CODEC_ID_*
[ffmpeg] / libavcodec / mpegaudio_parser.c
1 /*
2  * MPEG Audio parser
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2003 Michael Niedermayer
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "parser.h"
24 #include "mpegaudiodecheader.h"
25
26
27 typedef struct MpegAudioParseContext {
28     ParseContext pc;
29     int frame_size;
30     uint32_t header;
31     int header_count;
32 } MpegAudioParseContext;
33
34 #define MPA_HEADER_SIZE 4
35
36 /* header + layer + bitrate + freq + lsf/mpeg25 */
37 #define SAME_HEADER_MASK \
38    (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
39
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)
44 {
45     MpegAudioParseContext *s = s1->priv_data;
46     ParseContext *pc = &s->pc;
47     uint32_t state= pc->state;
48     int i;
49     int next= END_NOT_FOUND;
50
51     for(i=0; i<buf_size; ){
52         if(s->frame_size){
53             int inc= FFMIN(buf_size - i, s->frame_size);
54             i += inc;
55             s->frame_size -= inc;
56
57             if(!s->frame_size){
58                 next= i;
59                 break;
60             }
61         }else{
62             while(i<buf_size){
63                 int ret, sr, channels, bit_rate, frame_size;
64
65                 state= (state<<8) + buf[i++];
66
67                 ret = avpriv_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
68                 if (ret < 4) {
69                     if (i > 4)
70                         s->header_count = -2;
71                 } else {
72                     if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
73                         s->header_count= -3;
74                     s->header= state;
75                     s->header_count++;
76                     s->frame_size = ret-4;
77
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;
83                     }
84                     break;
85                 }
86             }
87         }
88     }
89
90     pc->state= state;
91     if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
92         *poutbuf = NULL;
93         *poutbuf_size = 0;
94         return buf_size;
95     }
96
97     *poutbuf = buf;
98     *poutbuf_size = buf_size;
99     return next;
100 }
101
102
103 AVCodecParser ff_mpegaudio_parser = {
104     .codec_ids      = { AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, AV_CODEC_ID_MP3 },
105     .priv_data_size = sizeof(MpegAudioParseContext),
106     .parser_parse   = mpegaudio_parse,
107     .parser_close   = ff_parse_close,
108 };