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"
27 typedef struct MpegAudioParseContext
{
28 uint8_t inbuf
[MPA_MAX_CODED_FRAME_SIZE
]; /* input buffer */
31 int free_format_frame_size
;
32 int free_format_next_header
;
35 } MpegAudioParseContext
;
37 #define MPA_HEADER_SIZE 4
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))
44 static int mpegaudio_parse_init(AVCodecParserContext
*s1
)
46 MpegAudioParseContext
*s
= s1
->priv_data
;
47 s
->inbuf_ptr
= s
->inbuf
;
51 static int mpegaudio_parse(AVCodecParserContext
*s1
,
52 AVCodecContext
*avctx
,
53 const uint8_t **poutbuf
, int *poutbuf_size
,
54 const uint8_t *buf
, int buf_size
)
56 MpegAudioParseContext
*s
= s1
->priv_data
;
59 const uint8_t *buf_ptr
;
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;
78 /* no header seen : find one. We need at least MPA_HEADER_SIZE
80 len
= FFMIN(MPA_HEADER_SIZE
- len
, buf_size
);
82 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
87 if ((s
->inbuf_ptr
- s
->inbuf
) >= MPA_HEADER_SIZE
) {
89 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
90 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
92 ret
= mpa_decode_header(avctx
, header
, &sr
);
95 /* no sync found : move by one byte (inefficient, but simple!) */
96 memmove(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
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;
103 if((header
&SAME_HEADER_MASK
) != (s
->header
&SAME_HEADER_MASK
) && s
->header
)
110 /* free format: prepare to compute frame size */
111 if (decode_header(s
, header
) == 1) {
116 if(s
->header_count
> 1)
117 avctx
->sample_rate
= sr
;
121 if (s
->frame_size
== -1) {
122 /* free format : find next sync to compute frame size */
123 len
= MPA_MAX_CODED_FRAME_SIZE
- len
;
127 /* frame too long: resync */
129 memmove(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
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;
141 header
= (p
[0] << 24) | (p
[1] << 16) |
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
147 if ((header
& SAME_HEADER_MASK
) ==
148 (header1
& SAME_HEADER_MASK
)) {
149 /* header found: update pointers */
150 len
= (p
+ 4) - s
->inbuf_ptr
;
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;
159 s
->free_format_frame_size
-= padding
* 4;
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
);
169 /* not found: simply increase pointers */
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
);
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){
190 *poutbuf_size
= s
->frame_size
;
192 buf_ptr
= buf
+ s
->frame_size
;
193 s
->inbuf_ptr
= s
->inbuf
;
199 if (s
->frame_size
> 0 &&
200 (s
->inbuf_ptr
- s
->inbuf
) >= s
->frame_size
) {
201 if(s
->header_count
> 0){
203 *poutbuf_size
= s
->inbuf_ptr
- s
->inbuf
;
205 s
->inbuf_ptr
= s
->inbuf
;
210 return buf_ptr
- buf
;
214 AVCodecParser mpegaudio_parser
= {
215 { CODEC_ID_MP2
, CODEC_ID_MP3
},
216 sizeof(MpegAudioParseContext
),
217 mpegaudio_parse_init
,