Mercurial > libavcodec.hg
annotate mpegaudio_parser.c @ 8464:48fc08113bdf libavcodec
s/FFmpeg-devel/ffmpeg-devel/
| author | benoit |
|---|---|
| date | Fri, 26 Dec 2008 08:03:26 +0000 |
| parents | 2b0d01be134f |
| children | d7d0cde5f308 |
| 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" | |
| 5050 | 25 #include "mpegaudiodecheader.h" |
| 2386 | 26 |
| 27 | |
| 1613 | 28 typedef struct MpegAudioParseContext { |
| 2979 | 29 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ |
| 1613 | 30 uint8_t *inbuf_ptr; |
| 31 int frame_size; | |
| 32 int free_format_frame_size; | |
| 33 int free_format_next_header; | |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
34 uint32_t header; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
35 int header_count; |
| 1613 | 36 } MpegAudioParseContext; |
| 37 | |
| 38 #define MPA_HEADER_SIZE 4 | |
| 39 | |
| 40 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
|
2522
e25782262d7d
kill warnings patch by (M?ns Rullg?rd <mru inprovide com>)
michael
parents:
2486
diff
changeset
|
41 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
| 1613 | 42 #define SAME_HEADER_MASK \ |
| 2480 | 43 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
| 1613 | 44 |
| 5050 | 45 /* useful helper to get mpeg audio stream infos. Return -1 if error in |
| 46 header, otherwise the coded frame size in bytes */ | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
47 int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate) |
| 5050 | 48 { |
| 49 MPADecodeContext s1, *s = &s1; | |
| 50 s1.avctx = avctx; | |
| 51 | |
| 52 if (ff_mpa_check_header(head) != 0) | |
| 53 return -1; | |
| 54 | |
| 5051 | 55 if (ff_mpegaudio_decode_header(s, head) != 0) { |
| 5050 | 56 return -1; |
| 57 } | |
| 58 | |
| 59 switch(s->layer) { | |
| 60 case 1: | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
61 *frame_size = 384; |
| 5050 | 62 break; |
| 63 case 2: | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
64 *frame_size = 1152; |
| 5050 | 65 break; |
| 66 default: | |
| 67 case 3: | |
| 68 if (s->lsf) | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
69 *frame_size = 576; |
| 5050 | 70 else |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
71 *frame_size = 1152; |
| 5050 | 72 break; |
| 73 } | |
| 74 | |
| 75 *sample_rate = s->sample_rate; | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
76 *channels = s->nb_channels; |
|
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
77 *bit_rate = s->bit_rate; |
| 5050 | 78 avctx->sub_id = s->layer; |
| 79 return s->frame_size; | |
| 80 } | |
| 81 | |
| 1613 | 82 static int mpegaudio_parse_init(AVCodecParserContext *s1) |
| 83 { | |
| 84 MpegAudioParseContext *s = s1->priv_data; | |
| 85 s->inbuf_ptr = s->inbuf; | |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 static int mpegaudio_parse(AVCodecParserContext *s1, | |
| 90 AVCodecContext *avctx, | |
|
4931
0d1cc37d9430
make some parser parameters const to avoid casting const to non-const
aurel
parents:
4914
diff
changeset
|
91 const uint8_t **poutbuf, int *poutbuf_size, |
| 1613 | 92 const uint8_t *buf, int buf_size) |
| 93 { | |
| 94 MpegAudioParseContext *s = s1->priv_data; | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
95 int len, ret, sr, channels, bit_rate, frame_size; |
| 1613 | 96 uint32_t header; |
| 97 const uint8_t *buf_ptr; | |
| 98 | |
| 99 *poutbuf = NULL; | |
| 100 *poutbuf_size = 0; | |
| 101 buf_ptr = buf; | |
| 102 while (buf_size > 0) { | |
| 2979 | 103 len = s->inbuf_ptr - s->inbuf; |
| 104 if (s->frame_size == 0) { | |
| 1613 | 105 /* special case for next header for first frame in free |
| 106 format case (XXX: find a simpler method) */ | |
| 107 if (s->free_format_next_header != 0) { | |
| 5089 | 108 AV_WB32(s->inbuf, s->free_format_next_header); |
| 1613 | 109 s->inbuf_ptr = s->inbuf + 4; |
| 110 s->free_format_next_header = 0; | |
| 111 goto got_header; | |
| 112 } | |
| 2979 | 113 /* no header seen : find one. We need at least MPA_HEADER_SIZE |
| 1613 | 114 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
|
115 len = FFMIN(MPA_HEADER_SIZE - len, buf_size); |
| 2979 | 116 if (len > 0) { |
| 117 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 118 buf_ptr += len; | |
| 119 buf_size -= len; | |
| 120 s->inbuf_ptr += len; | |
| 121 } | |
| 122 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
| 1613 | 123 got_header: |
| 5089 | 124 header = AV_RB32(s->inbuf); |
| 1613 | 125 |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
126 ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate); |
| 1613 | 127 if (ret < 0) { |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
128 s->header_count= -2; |
| 2979 | 129 /* no sync found : move by one byte (inefficient, but simple!) */ |
| 130 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
| 131 s->inbuf_ptr--; | |
| 4652 | 132 dprintf(avctx, "skip %x\n", header); |
| 1613 | 133 /* reset free format frame size to give a chance |
| 134 to get a new bitrate */ | |
| 135 s->free_format_frame_size = 0; | |
| 2979 | 136 } else { |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
137 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
|
138 s->header_count= -3; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
139 s->header= header; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
140 s->header_count++; |
| 1613 | 141 s->frame_size = ret; |
| 2967 | 142 |
| 1613 | 143 #if 0 |
| 144 /* free format: prepare to compute frame size */ | |
| 5051 | 145 if (ff_mpegaudio_decode_header(s, header) == 1) { |
| 2979 | 146 s->frame_size = -1; |
| 1613 | 147 } |
| 148 #endif | |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
149 if(s->header_count > 1){ |
| 5260 | 150 avctx->sample_rate= sr; |
|
8420
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
151 avctx->channels = channels; |
|
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
152 avctx->frame_size = frame_size; |
|
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
153 avctx->bit_rate = bit_rate; |
|
2b0d01be134f
Change mpeg audio parser so it only sets frame_size, channels and bit_rate
michael
parents:
5260
diff
changeset
|
154 } |
| 5259 | 155 } |
| 2979 | 156 } |
| 2967 | 157 } else |
| 1613 | 158 #if 0 |
| 159 if (s->frame_size == -1) { | |
| 160 /* free format : find next sync to compute frame size */ | |
| 2979 | 161 len = MPA_MAX_CODED_FRAME_SIZE - len; |
| 162 if (len > buf_size) | |
| 163 len = buf_size; | |
| 1613 | 164 if (len == 0) { |
| 2979 | 165 /* frame too long: resync */ |
| 1613 | 166 s->frame_size = 0; |
| 2979 | 167 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
| 168 s->inbuf_ptr--; | |
| 1613 | 169 } else { |
| 170 uint8_t *p, *pend; | |
| 171 uint32_t header1; | |
| 172 int padding; | |
| 173 | |
| 174 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 175 /* check for header */ | |
| 176 p = s->inbuf_ptr - 3; | |
| 177 pend = s->inbuf_ptr + len - 4; | |
| 178 while (p <= pend) { | |
| 5089 | 179 header = AV_RB32(p); |
| 180 header1 = AV_RB32(s->inbuf); | |
| 1613 | 181 /* check with high probability that we have a |
| 182 valid header */ | |
| 183 if ((header & SAME_HEADER_MASK) == | |
| 184 (header1 & SAME_HEADER_MASK)) { | |
| 185 /* header found: update pointers */ | |
| 186 len = (p + 4) - s->inbuf_ptr; | |
| 187 buf_ptr += len; | |
| 188 buf_size -= len; | |
| 189 s->inbuf_ptr = p; | |
| 190 /* compute frame size */ | |
| 191 s->free_format_next_header = header; | |
| 192 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
| 193 padding = (header1 >> 9) & 1; | |
| 194 if (s->layer == 1) | |
| 195 s->free_format_frame_size -= padding * 4; | |
| 196 else | |
| 197 s->free_format_frame_size -= padding; | |
| 4652 | 198 dprintf(avctx, "free frame size=%d padding=%d\n", |
| 1613 | 199 s->free_format_frame_size, padding); |
| 5051 | 200 ff_mpegaudio_decode_header(s, header1); |
| 1613 | 201 goto next_data; |
| 202 } | |
| 203 p++; | |
| 204 } | |
| 205 /* not found: simply increase pointers */ | |
| 206 buf_ptr += len; | |
| 207 s->inbuf_ptr += len; | |
| 208 buf_size -= len; | |
| 209 } | |
| 2979 | 210 } else |
| 1613 | 211 #endif |
| 212 if (len < s->frame_size) { | |
| 213 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
| 214 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
|
215 len = FFMIN(s->frame_size - len, buf_size); |
| 2979 | 216 memcpy(s->inbuf_ptr, buf_ptr, len); |
| 217 buf_ptr += len; | |
| 218 s->inbuf_ptr += len; | |
| 219 buf_size -= len; | |
| 220 } | |
|
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
|
221 |
|
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
222 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
|
223 && 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
|
224 if(s->header_count > 0){ |
|
4931
0d1cc37d9430
make some parser parameters const to avoid casting const to non-const
aurel
parents:
4914
diff
changeset
|
225 *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
|
226 *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
|
227 } |
|
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 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
|
232 } |
|
949bc256f1e3
dont copy frame if the whole mp1/2/3 frame is available in one piece in the input
michael
parents:
3456
diff
changeset
|
233 |
| 1613 | 234 // next_data: |
| 2967 | 235 if (s->frame_size > 0 && |
| 1613 | 236 (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
|
237 if(s->header_count > 0){ |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
238 *poutbuf = s->inbuf; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
239 *poutbuf_size = s->inbuf_ptr - s->inbuf; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
240 } |
| 2979 | 241 s->inbuf_ptr = s->inbuf; |
| 242 s->frame_size = 0; | |
| 243 break; | |
| 244 } | |
| 1613 | 245 } |
| 246 return buf_ptr - buf; | |
| 247 } | |
|
4397
acb9faabab8d
Allows the AC3 parser to read the frame size and codec parameters from E-AC3 streams,
gpoirier
parents:
4310
diff
changeset
|
248 |
|
acb9faabab8d
Allows the AC3 parser to read the frame size and codec parameters from E-AC3 streams,
gpoirier
parents:
4310
diff
changeset
|
249 |
| 1613 | 250 AVCodecParser mpegaudio_parser = { |
| 251 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
| 252 sizeof(MpegAudioParseContext), | |
| 253 mpegaudio_parse_init, | |
| 254 mpegaudio_parse, | |
| 255 NULL, | |
| 256 }; |
