Mercurial > libavcodec.hg
annotate mpegaudio_parser.c @ 4977:9d7c1fbd3359 libavcodec
move mjpeg parser in its own file
author | aurel |
---|---|
date | Thu, 10 May 2007 22:37:36 +0000 |
parents | 0d1cc37d9430 |
children | a5f6fbc9fa66 |
rev | line source |
---|---|
1613 | 1 /* |
4913 | 2 * MPEG Audio parser |
1613 | 3 * Copyright (c) 2003 Fabrice Bellard. |
4 * Copyright (c) 2003 Michael Niedermayer. | |
5 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
6 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
7 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
1613 | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
1613 | 12 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
1613 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1613 | 21 */ |
2967 | 22 |
4913 | 23 #include "parser.h" |
24 #include "mpegaudio.h" | |
2386 | 25 |
26 | |
1613 | 27 typedef struct MpegAudioParseContext { |
2979 | 28 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ |
1613 | 29 uint8_t *inbuf_ptr; |
30 int frame_size; | |
31 int free_format_frame_size; | |
32 int free_format_next_header; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
33 uint32_t header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
34 int header_count; |
1613 | 35 } MpegAudioParseContext; |
36 | |
37 #define MPA_HEADER_SIZE 4 | |
38 | |
39 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
2522
e25782262d7d
kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents:
2486
diff
changeset
|
40 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
1613 | 41 #define SAME_HEADER_MASK \ |
2480 | 42 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
1613 | 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, | |
4931
0d1cc37d9430
make some parser parameters const to avoid casting const to non-const
aurel
parents:
4914
diff
changeset
|
53 const uint8_t **poutbuf, int *poutbuf_size, |
1613 | 54 const uint8_t *buf, int buf_size) |
55 { | |
56 MpegAudioParseContext *s = s1->priv_data; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
57 int len, ret, sr; |
1613 | 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) { | |
2979 | 65 len = s->inbuf_ptr - s->inbuf; |
66 if (s->frame_size == 0) { | |
1613 | 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 } | |
2979 | 78 /* no header seen : find one. We need at least MPA_HEADER_SIZE |
1613 | 79 bytes to parse it */ |
3639
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
80 len = FFMIN(MPA_HEADER_SIZE - len, buf_size); |
2979 | 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) { | |
1613 | 88 got_header: |
2979 | 89 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
90 (s->inbuf[2] << 8) | s->inbuf[3]; | |
1613 | 91 |
4104
04ff8026d9c0
dont set the sampling rate just because 1 mp3 packet header says so (fixes playback speed on some old mencoder generated avis which where then dumped to mp3)
michael
parents:
3989
diff
changeset
|
92 ret = mpa_decode_header(avctx, header, &sr); |
1613 | 93 if (ret < 0) { |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
94 s->header_count= -2; |
2979 | 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--; | |
4652 | 98 dprintf(avctx, "skip %x\n", header); |
1613 | 99 /* reset free format frame size to give a chance |
100 to get a new bitrate */ | |
101 s->free_format_frame_size = 0; | |
2979 | 102 } else { |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
103 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
104 s->header_count= -3; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
105 s->header= header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
106 s->header_count++; |
1613 | 107 s->frame_size = ret; |
2967 | 108 |
1613 | 109 #if 0 |
110 /* free format: prepare to compute frame size */ | |
2979 | 111 if (decode_header(s, header) == 1) { |
112 s->frame_size = -1; | |
1613 | 113 } |
114 #endif | |
2979 | 115 } |
4104
04ff8026d9c0
dont set the sampling rate just because 1 mp3 packet header says so (fixes playback speed on some old mencoder generated avis which where then dumped to mp3)
michael
parents:
3989
diff
changeset
|
116 if(s->header_count > 1) |
04ff8026d9c0
dont set the sampling rate just because 1 mp3 packet header says so (fixes playback speed on some old mencoder generated avis which where then dumped to mp3)
michael
parents:
3989
diff
changeset
|
117 avctx->sample_rate= sr; |
2979 | 118 } |
2967 | 119 } else |
1613 | 120 #if 0 |
121 if (s->frame_size == -1) { | |
122 /* free format : find next sync to compute frame size */ | |
2979 | 123 len = MPA_MAX_CODED_FRAME_SIZE - len; |
124 if (len > buf_size) | |
125 len = buf_size; | |
1613 | 126 if (len == 0) { |
2979 | 127 /* frame too long: resync */ |
1613 | 128 s->frame_size = 0; |
2979 | 129 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
130 s->inbuf_ptr--; | |
1613 | 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; | |
4652 | 162 dprintf(avctx, "free frame size=%d padding=%d\n", |
1613 | 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 } | |
2979 | 174 } else |
1613 | 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; | |
3639
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
179 len = FFMIN(s->frame_size - len, buf_size); |
2979 | 180 memcpy(s->inbuf_ptr, buf_ptr, len); |
181 buf_ptr += len; | |
182 s->inbuf_ptr += len; | |
183 buf_size -= len; | |
184 } | |
3639
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
185 |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
186 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
187 && buf_size + buf_ptr - buf >= s->frame_size){ |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
188 if(s->header_count > 0){ |
4931
0d1cc37d9430
make some parser parameters const to avoid casting const to non-const
aurel
parents:
4914
diff
changeset
|
189 *poutbuf = buf; |
3639
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
190 *poutbuf_size = s->frame_size; |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
191 } |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
192 buf_ptr = buf + s->frame_size; |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
193 s->inbuf_ptr = s->inbuf; |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
194 s->frame_size = 0; |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
195 break; |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
196 } |
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
197 |
1613 | 198 // next_data: |
2967 | 199 if (s->frame_size > 0 && |
1613 | 200 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
201 if(s->header_count > 0){ |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
202 *poutbuf = s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
203 *poutbuf_size = s->inbuf_ptr - s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
204 } |
2979 | 205 s->inbuf_ptr = s->inbuf; |
206 s->frame_size = 0; | |
207 break; | |
208 } | |
1613 | 209 } |
210 return buf_ptr - buf; | |
211 } | |
4397
acb9faabab8d
Allows the AC3 parser to read the frame size and codec parameters from E-AC3 streams,
gpoirier
parents:
4310
diff
changeset
|
212 |
acb9faabab8d
Allows the AC3 parser to read the frame size and codec parameters from E-AC3 streams,
gpoirier
parents:
4310
diff
changeset
|
213 |
1613 | 214 AVCodecParser mpegaudio_parser = { |
215 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
216 sizeof(MpegAudioParseContext), | |
217 mpegaudio_parse_init, | |
218 mpegaudio_parse, | |
219 NULL, | |
220 }; |