avoid a 'discards qualifiers from pointer' warning
[ffmpeg.git] / 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 FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "parser.h"
24 #include "mpegaudio.h"
25
26
27 typedef struct MpegAudioParseContext {
28     uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];    /* input buffer */
29     uint8_t *inbuf_ptr;
30     int frame_size;
31     int free_format_frame_size;
32     int free_format_next_header;
33     uint32_t header;
34     int header_count;
35 } MpegAudioParseContext;
36
37 #define MPA_HEADER_SIZE 4
38
39 /* header + layer + bitrate + freq + lsf/mpeg25 */
40 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
41 #define SAME_HEADER_MASK \
42    (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
43
44 static int mpegaudio_parse_init(AVCodecParserContext *s1)
45 {
46     MpegAudioParseContext *s = s1->priv_data;
47     s->inbuf_ptr = s->inbuf;
48     return 0;
49 }
50
51 static int mpegaudio_parse(AVCodecParserContext *s1,
52                            AVCodecContext *avctx,
53                            uint8_t **poutbuf, int *poutbuf_size,
54                            const uint8_t *buf, int buf_size)
55 {
56     MpegAudioParseContext *s = s1->priv_data;
57     int len, ret, sr;
58     uint32_t header;
59     const uint8_t *buf_ptr;
60
61     *poutbuf = NULL;
62     *poutbuf_size = 0;
63     buf_ptr = buf;
64     while (buf_size > 0) {
65         len = s->inbuf_ptr - s->inbuf;
66         if (s->frame_size == 0) {
67             /* special case for next header for first frame in free
68                format case (XXX: find a simpler method) */
69             if (s->free_format_next_header != 0) {
70                 s->inbuf[0] = s->free_format_next_header >> 24;
71                 s->inbuf[1] = s->free_format_next_header >> 16;
72                 s->inbuf[2] = s->free_format_next_header >> 8;
73                 s->inbuf[3] = s->free_format_next_header;
74                 s->inbuf_ptr = s->inbuf + 4;
75                 s->free_format_next_header = 0;
76                 goto got_header;
77             }
78             /* no header seen : find one. We need at least MPA_HEADER_SIZE
79                bytes to parse it */
80             len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
81             if (len > 0) {
82                 memcpy(s->inbuf_ptr, buf_ptr, len);
83                 buf_ptr += len;
84                 buf_size -= len;
85                 s->inbuf_ptr += len;
86             }
87             if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
88             got_header:
89                 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
90                     (s->inbuf[2] << 8) | s->inbuf[3];
91
92                 ret = mpa_decode_header(avctx, header, &sr);
93                 if (ret < 0) {
94                     s->header_count= -2;
95                     /* no sync found : move by one byte (inefficient, but simple!) */
96                     memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
97                     s->inbuf_ptr--;
98                     dprintf(avctx, "skip %x\n", header);
99                     /* reset free format frame size to give a chance
100                        to get a new bitrate */
101                     s->free_format_frame_size = 0;
102                 } else {
103                     if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
104                         s->header_count= -3;
105                     s->header= header;
106                     s->header_count++;
107                     s->frame_size = ret;
108
109 #if 0
110                     /* free format: prepare to compute frame size */
111                     if (decode_header(s, header) == 1) {
112                         s->frame_size = -1;
113                     }
114 #endif
115                 }
116                 if(s->header_count > 1)
117                     avctx->sample_rate= sr;
118             }
119         } else
120 #if 0
121         if (s->frame_size == -1) {
122             /* free format : find next sync to compute frame size */
123             len = MPA_MAX_CODED_FRAME_SIZE - len;
124             if (len > buf_size)
125                 len = buf_size;
126             if (len == 0) {
127                 /* frame too long: resync */
128                 s->frame_size = 0;
129                 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
130                 s->inbuf_ptr--;
131             } else {
132                 uint8_t *p, *pend;
133                 uint32_t header1;
134                 int padding;
135
136                 memcpy(s->inbuf_ptr, buf_ptr, len);
137                 /* check for header */
138                 p = s->inbuf_ptr - 3;
139                 pend = s->inbuf_ptr + len - 4;
140                 while (p <= pend) {
141                     header = (p[0] << 24) | (p[1] << 16) |
142                         (p[2] << 8) | p[3];
143                     header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
144                         (s->inbuf[2] << 8) | s->inbuf[3];
145                     /* check with high probability that we have a
146                        valid header */
147                     if ((header & SAME_HEADER_MASK) ==
148                         (header1 & SAME_HEADER_MASK)) {
149                         /* header found: update pointers */
150                         len = (p + 4) - s->inbuf_ptr;
151                         buf_ptr += len;
152                         buf_size -= len;
153                         s->inbuf_ptr = p;
154                         /* compute frame size */
155                         s->free_format_next_header = header;
156                         s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
157                         padding = (header1 >> 9) & 1;
158                         if (s->layer == 1)
159                             s->free_format_frame_size -= padding * 4;
160                         else
161                             s->free_format_frame_size -= padding;
162                         dprintf(avctx, "free frame size=%d padding=%d\n",
163                                 s->free_format_frame_size, padding);
164                         decode_header(s, header1);
165                         goto next_data;
166                     }
167                     p++;
168                 }
169                 /* not found: simply increase pointers */
170                 buf_ptr += len;
171                 s->inbuf_ptr += len;
172                 buf_size -= len;
173             }
174         } else
175 #endif
176         if (len < s->frame_size) {
177             if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
178                 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
179             len = FFMIN(s->frame_size - len, buf_size);
180             memcpy(s->inbuf_ptr, buf_ptr, len);
181             buf_ptr += len;
182             s->inbuf_ptr += len;
183             buf_size -= len;
184         }
185
186         if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
187            && buf_size + buf_ptr - buf >= s->frame_size){
188             if(s->header_count > 0){
189                 *poutbuf = (uint8_t *)buf;
190                 *poutbuf_size = s->frame_size;
191             }
192             buf_ptr = buf + s->frame_size;
193             s->inbuf_ptr = s->inbuf;
194             s->frame_size = 0;
195             break;
196         }
197
198         //    next_data:
199         if (s->frame_size > 0 &&
200             (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
201             if(s->header_count > 0){
202                 *poutbuf = s->inbuf;
203                 *poutbuf_size = s->inbuf_ptr - s->inbuf;
204             }
205             s->inbuf_ptr = s->inbuf;
206             s->frame_size = 0;
207             break;
208         }
209     }
210     return buf_ptr - buf;
211 }
212
213
214 AVCodecParser mpegaudio_parser = {
215     { CODEC_ID_MP2, CODEC_ID_MP3 },
216     sizeof(MpegAudioParseContext),
217     mpegaudio_parse_init,
218     mpegaudio_parse,
219     NULL,
220 };