Mercurial > libavcodec.hg
annotate parser.c @ 2554:84a8f374bf63 libavcodec
fixing playback of xvid pretending to be divx (sample_vop_nc.avi)
author | michael |
---|---|
date | Tue, 08 Mar 2005 14:39:58 +0000 |
parents | 1fefaaee0fdc |
children | e7f2b8fadfb0 |
rev | line source |
---|---|
1613 | 1 /* |
2 * Audio and Video frame extraction | |
3 * Copyright (c) 2003 Fabrice Bellard. | |
4 * Copyright (c) 2003 Michael Niedermayer. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 */ | |
20 #include "avcodec.h" | |
21 #include "mpegvideo.h" | |
22 #include "mpegaudio.h" | |
23 | |
24 AVCodecParser *av_first_parser = NULL; | |
25 | |
26 void av_register_codec_parser(AVCodecParser *parser) | |
27 { | |
28 parser->next = av_first_parser; | |
29 av_first_parser = parser; | |
30 } | |
31 | |
32 AVCodecParserContext *av_parser_init(int codec_id) | |
33 { | |
34 AVCodecParserContext *s; | |
35 AVCodecParser *parser; | |
36 int ret; | |
2486
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
37 |
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
38 if(codec_id == CODEC_ID_NONE) |
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
39 return NULL; |
1613 | 40 |
41 for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
42 if (parser->codec_ids[0] == codec_id || | |
43 parser->codec_ids[1] == codec_id || | |
2348 | 44 parser->codec_ids[2] == codec_id || |
45 parser->codec_ids[3] == codec_id || | |
46 parser->codec_ids[4] == codec_id) | |
1613 | 47 goto found; |
48 } | |
49 return NULL; | |
50 found: | |
51 s = av_mallocz(sizeof(AVCodecParserContext)); | |
52 if (!s) | |
53 return NULL; | |
54 s->parser = parser; | |
55 s->priv_data = av_mallocz(parser->priv_data_size); | |
56 if (!s->priv_data) { | |
57 av_free(s); | |
58 return NULL; | |
59 } | |
60 if (parser->parser_init) { | |
61 ret = parser->parser_init(s); | |
62 if (ret != 0) { | |
63 av_free(s->priv_data); | |
64 av_free(s); | |
65 return NULL; | |
66 } | |
67 } | |
2030 | 68 s->fetch_timestamp=1; |
1613 | 69 return s; |
70 } | |
71 | |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
72 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
73 can be returned if necessary */ |
1613 | 74 int av_parser_parse(AVCodecParserContext *s, |
75 AVCodecContext *avctx, | |
76 uint8_t **poutbuf, int *poutbuf_size, | |
1696 | 77 const uint8_t *buf, int buf_size, |
78 int64_t pts, int64_t dts) | |
1613 | 79 { |
1696 | 80 int index, i, k; |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
81 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
82 |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
83 if (buf_size == 0) { |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
84 /* padding is always necessary even if EOF, so we add it here */ |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
85 memset(dummy_buf, 0, sizeof(dummy_buf)); |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
86 buf = dummy_buf; |
1696 | 87 } else { |
88 /* add a new packet descriptor */ | |
89 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
90 s->cur_frame_start_index = k; | |
91 s->cur_frame_offset[k] = s->cur_offset; | |
92 s->cur_frame_pts[k] = pts; | |
93 s->cur_frame_dts[k] = dts; | |
94 | |
95 /* fill first PTS/DTS */ | |
2030 | 96 if (s->fetch_timestamp){ |
97 s->fetch_timestamp=0; | |
1696 | 98 s->last_pts = pts; |
99 s->last_dts = dts; | |
2107 | 100 s->cur_frame_pts[k] = |
101 s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
1696 | 102 } |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
103 } |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
104 |
1613 | 105 /* WARNING: the returned index can be negative */ |
106 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
2107 | 107 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id); |
1613 | 108 /* update the file pointer */ |
109 if (*poutbuf_size) { | |
1696 | 110 /* fill the data for the current frame */ |
1613 | 111 s->frame_offset = s->last_frame_offset; |
1696 | 112 s->pts = s->last_pts; |
113 s->dts = s->last_dts; | |
114 | |
115 /* offset of the next frame */ | |
1613 | 116 s->last_frame_offset = s->cur_offset + index; |
1696 | 117 /* find the packet in which the new frame starts. It |
118 is tricky because of MPEG video start codes | |
119 which can begin in one packet and finish in | |
120 another packet. In the worst case, an MPEG | |
121 video start code could be in 4 different | |
122 packets. */ | |
123 k = s->cur_frame_start_index; | |
124 for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
125 if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
126 break; | |
127 k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
128 } | |
2030 | 129 |
1696 | 130 s->last_pts = s->cur_frame_pts[k]; |
131 s->last_dts = s->cur_frame_dts[k]; | |
2030 | 132 |
133 /* some parsers tell us the packet size even before seeing the first byte of the next packet, | |
134 so the next pts/dts is in the next chunk */ | |
135 if(index == buf_size){ | |
136 s->fetch_timestamp=1; | |
137 } | |
1613 | 138 } |
139 if (index < 0) | |
140 index = 0; | |
141 s->cur_offset += index; | |
142 return index; | |
143 } | |
144 | |
145 void av_parser_close(AVCodecParserContext *s) | |
146 { | |
147 if (s->parser->parser_close) | |
148 s->parser->parser_close(s); | |
149 av_free(s->priv_data); | |
150 av_free(s); | |
151 } | |
152 | |
153 /*****************************************************/ | |
154 | |
155 //#define END_NOT_FOUND (-100) | |
156 | |
157 #define PICTURE_START_CODE 0x00000100 | |
158 #define SEQ_START_CODE 0x000001b3 | |
159 #define EXT_START_CODE 0x000001b5 | |
160 #define SLICE_MIN_START_CODE 0x00000101 | |
161 #define SLICE_MAX_START_CODE 0x000001af | |
162 | |
163 typedef struct ParseContext1{ | |
1988 | 164 ParseContext pc; |
165 /* XXX/FIXME PC1 vs. PC */ | |
1613 | 166 /* MPEG2 specific */ |
167 int frame_rate; | |
168 int progressive_sequence; | |
169 int width, height; | |
1614 | 170 |
1613 | 171 /* XXX: suppress that, needed by MPEG4 */ |
172 MpegEncContext *enc; | |
1614 | 173 int first_picture; |
1613 | 174 } ParseContext1; |
175 | |
176 /** | |
177 * combines the (truncated) bitstream to a complete frame | |
178 * @returns -1 if no complete frame could be created | |
179 */ | |
1988 | 180 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
1613 | 181 { |
182 #if 0 | |
183 if(pc->overread){ | |
184 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
185 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
186 } | |
187 #endif | |
188 | |
189 /* copy overreaded bytes from last frame into buffer */ | |
190 for(; pc->overread>0; pc->overread--){ | |
191 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
192 } | |
2386 | 193 |
194 /* flush remaining if EOF */ | |
195 if(!*buf_size && next == END_NOT_FOUND){ | |
196 next= 0; | |
197 } | |
198 | |
1613 | 199 pc->last_index= pc->index; |
200 | |
201 /* copy into buffer end return */ | |
202 if(next == END_NOT_FOUND){ | |
203 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
204 | |
205 memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
206 pc->index += *buf_size; | |
207 return -1; | |
208 } | |
209 | |
210 *buf_size= | |
211 pc->overread_index= pc->index + next; | |
212 | |
213 /* append to buffer */ | |
214 if(pc->index){ | |
215 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
216 | |
217 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
218 pc->index = 0; | |
219 *buf= pc->buffer; | |
220 } | |
221 | |
222 /* store overread bytes */ | |
223 for(;next < 0; next++){ | |
224 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
225 pc->overread++; | |
226 } | |
227 | |
228 #if 0 | |
229 if(pc->overread){ | |
230 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
231 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
232 } | |
233 #endif | |
234 | |
235 return 0; | |
236 } | |
237 | |
238 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) | |
239 { | |
240 const uint8_t *buf_ptr; | |
241 unsigned int state=0xFFFFFFFF, v; | |
242 int val; | |
243 | |
244 buf_ptr = *pbuf_ptr; | |
245 while (buf_ptr < buf_end) { | |
246 v = *buf_ptr++; | |
247 if (state == 0x000001) { | |
248 state = ((state << 8) | v) & 0xffffff; | |
249 val = state; | |
250 goto found; | |
251 } | |
252 state = ((state << 8) | v) & 0xffffff; | |
253 } | |
254 val = -1; | |
255 found: | |
256 *pbuf_ptr = buf_ptr; | |
257 return val; | |
258 } | |
259 | |
260 /* XXX: merge with libavcodec ? */ | |
261 #define MPEG1_FRAME_RATE_BASE 1001 | |
262 | |
263 static const int frame_rate_tab[16] = { | |
264 0, | |
265 24000, | |
266 24024, | |
267 25025, | |
268 30000, | |
269 30030, | |
270 50050, | |
271 60000, | |
272 60060, | |
273 // Xing's 15fps: (9) | |
274 15015, | |
275 // libmpeg3's "Unofficial economy rates": (10-13) | |
276 5005, | |
277 10010, | |
278 12012, | |
279 15015, | |
280 // random, just to avoid segfault !never encode these | |
281 25025, | |
282 25025, | |
283 }; | |
284 | |
285 static void mpegvideo_extract_headers(AVCodecParserContext *s, | |
286 AVCodecContext *avctx, | |
287 const uint8_t *buf, int buf_size) | |
288 { | |
289 ParseContext1 *pc = s->priv_data; | |
290 const uint8_t *buf_end; | |
291 int32_t start_code; | |
292 int frame_rate_index, ext_type, bytes_left; | |
293 int frame_rate_ext_n, frame_rate_ext_d; | |
2119 | 294 int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
2539 | 295 int horiz_size_ext, vert_size_ext, bit_rate_ext; |
1613 | 296 |
297 s->repeat_pict = 0; | |
298 buf_end = buf + buf_size; | |
299 while (buf < buf_end) { | |
300 start_code = find_start_code(&buf, buf_end); | |
301 bytes_left = buf_end - buf; | |
302 switch(start_code) { | |
303 case PICTURE_START_CODE: | |
304 if (bytes_left >= 2) { | |
305 s->pict_type = (buf[1] >> 3) & 7; | |
306 } | |
307 break; | |
308 case SEQ_START_CODE: | |
2539 | 309 if (bytes_left >= 7) { |
2269 | 310 pc->width = (buf[0] << 4) | (buf[1] >> 4); |
311 pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
2270 | 312 avcodec_set_dimensions(avctx, pc->width, pc->height); |
1613 | 313 frame_rate_index = buf[3] & 0xf; |
314 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index]; | |
315 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE; | |
2539 | 316 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400; |
1681 | 317 avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
318 avctx->sub_id = 1; | |
1613 | 319 } |
320 break; | |
321 case EXT_START_CODE: | |
322 if (bytes_left >= 1) { | |
323 ext_type = (buf[0] >> 4); | |
324 switch(ext_type) { | |
325 case 0x1: /* sequence extension */ | |
326 if (bytes_left >= 6) { | |
327 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
328 vert_size_ext = (buf[2] >> 5) & 3; | |
2539 | 329 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); |
1613 | 330 frame_rate_ext_n = (buf[5] >> 5) & 3; |
331 frame_rate_ext_d = (buf[5] & 0x1f); | |
332 pc->progressive_sequence = buf[1] & (1 << 3); | |
2389
429c1eedeee9
fix timestamp prediction for low_delay mpeg streams
michael
parents:
2386
diff
changeset
|
333 avctx->has_b_frames= buf[5] >> 7; |
1613 | 334 |
2269 | 335 pc->width |=(horiz_size_ext << 12); |
336 pc->height |=( vert_size_ext << 12); | |
2539 | 337 avctx->bit_rate += (bit_rate_ext << 18) * 400; |
2270 | 338 avcodec_set_dimensions(avctx, pc->width, pc->height); |
1613 | 339 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1); |
340 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
1681 | 341 avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
1613 | 342 avctx->sub_id = 2; /* forces MPEG2 */ |
343 } | |
344 break; | |
345 case 0x8: /* picture coding extension */ | |
346 if (bytes_left >= 5) { | |
2120 | 347 picture_structure = buf[2]&3; |
1613 | 348 top_field_first = buf[3] & (1 << 7); |
349 repeat_first_field = buf[3] & (1 << 1); | |
350 progressive_frame = buf[4] & (1 << 7); | |
351 | |
352 /* check if we must repeat the frame */ | |
353 if (repeat_first_field) { | |
354 if (pc->progressive_sequence) { | |
355 if (top_field_first) | |
356 s->repeat_pict = 4; | |
357 else | |
358 s->repeat_pict = 2; | |
359 } else if (progressive_frame) { | |
360 s->repeat_pict = 1; | |
361 } | |
362 } | |
2119 | 363 |
364 /* the packet only represents half a frame | |
365 XXX,FIXME maybe find a different solution */ | |
366 if(picture_structure != 3) | |
367 s->repeat_pict = -1; | |
1613 | 368 } |
369 break; | |
370 } | |
371 } | |
372 break; | |
373 case -1: | |
374 goto the_end; | |
375 default: | |
376 /* we stop parsing when we encounter a slice. It ensures | |
377 that this function takes a negligible amount of time */ | |
378 if (start_code >= SLICE_MIN_START_CODE && | |
379 start_code <= SLICE_MAX_START_CODE) | |
380 goto the_end; | |
381 break; | |
382 } | |
383 } | |
384 the_end: ; | |
385 } | |
386 | |
387 static int mpegvideo_parse(AVCodecParserContext *s, | |
388 AVCodecContext *avctx, | |
389 uint8_t **poutbuf, int *poutbuf_size, | |
390 const uint8_t *buf, int buf_size) | |
391 { | |
1988 | 392 ParseContext1 *pc1 = s->priv_data; |
393 ParseContext *pc= &pc1->pc; | |
1613 | 394 int next; |
395 | |
1988 | 396 next= ff_mpeg1_find_frame_end(pc, buf, buf_size); |
1613 | 397 |
1988 | 398 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 399 *poutbuf = NULL; |
400 *poutbuf_size = 0; | |
401 return buf_size; | |
402 } | |
403 /* we have a full frame : we just parse the first few MPEG headers | |
404 to have the full timing information. The time take by this | |
405 function should be negligible for uncorrupted streams */ | |
406 mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
407 #if 0 | |
408 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | |
409 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict); | |
410 #endif | |
411 | |
412 *poutbuf = (uint8_t *)buf; | |
413 *poutbuf_size = buf_size; | |
414 return next; | |
415 } | |
416 | |
1988 | 417 void ff_parse_close(AVCodecParserContext *s) |
1613 | 418 { |
1988 | 419 ParseContext *pc = s->priv_data; |
1613 | 420 |
421 av_free(pc->buffer); | |
1988 | 422 } |
423 | |
424 static void parse1_close(AVCodecParserContext *s) | |
425 { | |
426 ParseContext1 *pc1 = s->priv_data; | |
427 | |
428 av_free(pc1->pc.buffer); | |
429 av_free(pc1->enc); | |
1613 | 430 } |
431 | |
432 /*************************/ | |
433 | |
434 /* used by parser */ | |
435 /* XXX: make it use less memory */ | |
436 static int av_mpeg4_decode_header(AVCodecParserContext *s1, | |
437 AVCodecContext *avctx, | |
438 const uint8_t *buf, int buf_size) | |
439 { | |
440 ParseContext1 *pc = s1->priv_data; | |
441 MpegEncContext *s = pc->enc; | |
442 GetBitContext gb1, *gb = &gb1; | |
443 int ret; | |
444 | |
445 s->avctx = avctx; | |
1614 | 446 s->current_picture_ptr = &s->current_picture; |
447 | |
448 if (avctx->extradata_size && pc->first_picture){ | |
449 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
450 ret = ff_mpeg4_decode_picture_header(s, gb); | |
451 } | |
452 | |
1613 | 453 init_get_bits(gb, buf, 8 * buf_size); |
454 ret = ff_mpeg4_decode_picture_header(s, gb); | |
455 if (s->width) { | |
2270 | 456 avcodec_set_dimensions(avctx, s->width, s->height); |
1613 | 457 } |
1614 | 458 pc->first_picture = 0; |
1613 | 459 return ret; |
460 } | |
461 | |
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1988
diff
changeset
|
462 static int mpeg4video_parse_init(AVCodecParserContext *s) |
1613 | 463 { |
464 ParseContext1 *pc = s->priv_data; | |
1614 | 465 |
1613 | 466 pc->enc = av_mallocz(sizeof(MpegEncContext)); |
467 if (!pc->enc) | |
468 return -1; | |
1614 | 469 pc->first_picture = 1; |
1613 | 470 return 0; |
471 } | |
472 | |
473 static int mpeg4video_parse(AVCodecParserContext *s, | |
474 AVCodecContext *avctx, | |
475 uint8_t **poutbuf, int *poutbuf_size, | |
476 const uint8_t *buf, int buf_size) | |
477 { | |
1988 | 478 ParseContext *pc = s->priv_data; |
1613 | 479 int next; |
480 | |
1988 | 481 next= ff_mpeg4_find_frame_end(pc, buf, buf_size); |
1613 | 482 |
1988 | 483 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 484 *poutbuf = NULL; |
485 *poutbuf_size = 0; | |
486 return buf_size; | |
487 } | |
488 av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
489 | |
490 *poutbuf = (uint8_t *)buf; | |
491 *poutbuf_size = buf_size; | |
492 return next; | |
493 } | |
494 | |
495 /*************************/ | |
496 | |
497 typedef struct MpegAudioParseContext { | |
498 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ | |
499 uint8_t *inbuf_ptr; | |
500 int frame_size; | |
501 int free_format_frame_size; | |
502 int free_format_next_header; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
503 uint32_t header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
504 int header_count; |
1613 | 505 } MpegAudioParseContext; |
506 | |
507 #define MPA_HEADER_SIZE 4 | |
508 | |
509 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
2522
e25782262d7d
kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents:
2486
diff
changeset
|
510 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
1613 | 511 #define SAME_HEADER_MASK \ |
2480 | 512 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
1613 | 513 |
514 static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
515 { | |
516 MpegAudioParseContext *s = s1->priv_data; | |
517 s->inbuf_ptr = s->inbuf; | |
518 return 0; | |
519 } | |
520 | |
521 static int mpegaudio_parse(AVCodecParserContext *s1, | |
522 AVCodecContext *avctx, | |
523 uint8_t **poutbuf, int *poutbuf_size, | |
524 const uint8_t *buf, int buf_size) | |
525 { | |
526 MpegAudioParseContext *s = s1->priv_data; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
527 int len, ret, sr; |
1613 | 528 uint32_t header; |
529 const uint8_t *buf_ptr; | |
530 | |
531 *poutbuf = NULL; | |
532 *poutbuf_size = 0; | |
533 buf_ptr = buf; | |
534 while (buf_size > 0) { | |
535 len = s->inbuf_ptr - s->inbuf; | |
536 if (s->frame_size == 0) { | |
537 /* special case for next header for first frame in free | |
538 format case (XXX: find a simpler method) */ | |
539 if (s->free_format_next_header != 0) { | |
540 s->inbuf[0] = s->free_format_next_header >> 24; | |
541 s->inbuf[1] = s->free_format_next_header >> 16; | |
542 s->inbuf[2] = s->free_format_next_header >> 8; | |
543 s->inbuf[3] = s->free_format_next_header; | |
544 s->inbuf_ptr = s->inbuf + 4; | |
545 s->free_format_next_header = 0; | |
546 goto got_header; | |
547 } | |
548 /* no header seen : find one. We need at least MPA_HEADER_SIZE | |
549 bytes to parse it */ | |
550 len = MPA_HEADER_SIZE - len; | |
551 if (len > buf_size) | |
552 len = buf_size; | |
553 if (len > 0) { | |
554 memcpy(s->inbuf_ptr, buf_ptr, len); | |
555 buf_ptr += len; | |
556 buf_size -= len; | |
557 s->inbuf_ptr += len; | |
558 } | |
559 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
560 got_header: | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
561 sr= avctx->sample_rate; |
1613 | 562 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
563 (s->inbuf[2] << 8) | s->inbuf[3]; | |
564 | |
565 ret = mpa_decode_header(avctx, header); | |
566 if (ret < 0) { | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
567 s->header_count= -2; |
1613 | 568 /* no sync found : move by one byte (inefficient, but simple!) */ |
569 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
570 s->inbuf_ptr--; | |
571 dprintf("skip %x\n", header); | |
572 /* reset free format frame size to give a chance | |
573 to get a new bitrate */ | |
574 s->free_format_frame_size = 0; | |
575 } else { | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
576 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
|
577 s->header_count= -3; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
578 s->header= header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
579 s->header_count++; |
1613 | 580 s->frame_size = ret; |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
581 |
1613 | 582 #if 0 |
583 /* free format: prepare to compute frame size */ | |
584 if (decode_header(s, header) == 1) { | |
585 s->frame_size = -1; | |
586 } | |
587 #endif | |
588 } | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
589 if(s->header_count <= 0) |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
590 avctx->sample_rate= sr; //FIXME ugly |
1613 | 591 } |
592 } else | |
593 #if 0 | |
594 if (s->frame_size == -1) { | |
595 /* free format : find next sync to compute frame size */ | |
596 len = MPA_MAX_CODED_FRAME_SIZE - len; | |
597 if (len > buf_size) | |
598 len = buf_size; | |
599 if (len == 0) { | |
600 /* frame too long: resync */ | |
601 s->frame_size = 0; | |
602 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
603 s->inbuf_ptr--; | |
604 } else { | |
605 uint8_t *p, *pend; | |
606 uint32_t header1; | |
607 int padding; | |
608 | |
609 memcpy(s->inbuf_ptr, buf_ptr, len); | |
610 /* check for header */ | |
611 p = s->inbuf_ptr - 3; | |
612 pend = s->inbuf_ptr + len - 4; | |
613 while (p <= pend) { | |
614 header = (p[0] << 24) | (p[1] << 16) | | |
615 (p[2] << 8) | p[3]; | |
616 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
617 (s->inbuf[2] << 8) | s->inbuf[3]; | |
618 /* check with high probability that we have a | |
619 valid header */ | |
620 if ((header & SAME_HEADER_MASK) == | |
621 (header1 & SAME_HEADER_MASK)) { | |
622 /* header found: update pointers */ | |
623 len = (p + 4) - s->inbuf_ptr; | |
624 buf_ptr += len; | |
625 buf_size -= len; | |
626 s->inbuf_ptr = p; | |
627 /* compute frame size */ | |
628 s->free_format_next_header = header; | |
629 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
630 padding = (header1 >> 9) & 1; | |
631 if (s->layer == 1) | |
632 s->free_format_frame_size -= padding * 4; | |
633 else | |
634 s->free_format_frame_size -= padding; | |
635 dprintf("free frame size=%d padding=%d\n", | |
636 s->free_format_frame_size, padding); | |
637 decode_header(s, header1); | |
638 goto next_data; | |
639 } | |
640 p++; | |
641 } | |
642 /* not found: simply increase pointers */ | |
643 buf_ptr += len; | |
644 s->inbuf_ptr += len; | |
645 buf_size -= len; | |
646 } | |
647 } else | |
648 #endif | |
649 if (len < s->frame_size) { | |
650 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
651 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
652 len = s->frame_size - len; | |
653 if (len > buf_size) | |
654 len = buf_size; | |
655 memcpy(s->inbuf_ptr, buf_ptr, len); | |
656 buf_ptr += len; | |
657 s->inbuf_ptr += len; | |
658 buf_size -= len; | |
659 } | |
660 // next_data: | |
661 if (s->frame_size > 0 && | |
662 (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
|
663 if(s->header_count > 0){ |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
664 *poutbuf = s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
665 *poutbuf_size = s->inbuf_ptr - s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
666 } |
1613 | 667 s->inbuf_ptr = s->inbuf; |
668 s->frame_size = 0; | |
669 break; | |
670 } | |
671 } | |
672 return buf_ptr - buf; | |
673 } | |
674 | |
675 #ifdef CONFIG_AC3 | |
676 extern int a52_syncinfo (const uint8_t * buf, int * flags, | |
677 int * sample_rate, int * bit_rate); | |
678 | |
679 typedef struct AC3ParseContext { | |
680 uint8_t inbuf[4096]; /* input buffer */ | |
681 uint8_t *inbuf_ptr; | |
682 int frame_size; | |
683 int flags; | |
684 } AC3ParseContext; | |
685 | |
686 #define AC3_HEADER_SIZE 7 | |
687 #define A52_LFE 16 | |
688 | |
689 static int ac3_parse_init(AVCodecParserContext *s1) | |
690 { | |
691 AC3ParseContext *s = s1->priv_data; | |
692 s->inbuf_ptr = s->inbuf; | |
693 return 0; | |
694 } | |
695 | |
696 static int ac3_parse(AVCodecParserContext *s1, | |
697 AVCodecContext *avctx, | |
698 uint8_t **poutbuf, int *poutbuf_size, | |
699 const uint8_t *buf, int buf_size) | |
700 { | |
701 AC3ParseContext *s = s1->priv_data; | |
702 const uint8_t *buf_ptr; | |
703 int len, sample_rate, bit_rate; | |
704 static const int ac3_channels[8] = { | |
705 2, 1, 2, 3, 3, 4, 4, 5 | |
706 }; | |
707 | |
708 *poutbuf = NULL; | |
709 *poutbuf_size = 0; | |
710 | |
711 buf_ptr = buf; | |
712 while (buf_size > 0) { | |
713 len = s->inbuf_ptr - s->inbuf; | |
714 if (s->frame_size == 0) { | |
715 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
716 len = AC3_HEADER_SIZE - len; | |
717 if (len > buf_size) | |
718 len = buf_size; | |
719 memcpy(s->inbuf_ptr, buf_ptr, len); | |
720 buf_ptr += len; | |
721 s->inbuf_ptr += len; | |
722 buf_size -= len; | |
723 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
724 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
725 if (len == 0) { | |
726 /* no sync found : move by one byte (inefficient, but simple!) */ | |
727 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
728 s->inbuf_ptr--; | |
729 } else { | |
730 s->frame_size = len; | |
731 /* update codec info */ | |
732 avctx->sample_rate = sample_rate; | |
1987 | 733 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
734 if(avctx->channels!=1 && avctx->channels!=2){ | |
735 avctx->channels = ac3_channels[s->flags & 7]; | |
736 if (s->flags & A52_LFE) | |
737 avctx->channels++; | |
738 } | |
1613 | 739 avctx->bit_rate = bit_rate; |
740 avctx->frame_size = 6 * 256; | |
741 } | |
742 } | |
743 } else if (len < s->frame_size) { | |
744 len = s->frame_size - len; | |
745 if (len > buf_size) | |
746 len = buf_size; | |
747 | |
748 memcpy(s->inbuf_ptr, buf_ptr, len); | |
749 buf_ptr += len; | |
750 s->inbuf_ptr += len; | |
751 buf_size -= len; | |
752 } else { | |
753 *poutbuf = s->inbuf; | |
754 *poutbuf_size = s->frame_size; | |
755 s->inbuf_ptr = s->inbuf; | |
756 s->frame_size = 0; | |
757 break; | |
758 } | |
759 } | |
760 return buf_ptr - buf; | |
761 } | |
762 #endif | |
763 | |
764 AVCodecParser mpegvideo_parser = { | |
765 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
766 sizeof(ParseContext1), | |
767 NULL, | |
768 mpegvideo_parse, | |
1988 | 769 parse1_close, |
1613 | 770 }; |
771 | |
772 AVCodecParser mpeg4video_parser = { | |
773 { CODEC_ID_MPEG4 }, | |
774 sizeof(ParseContext1), | |
775 mpeg4video_parse_init, | |
776 mpeg4video_parse, | |
1988 | 777 parse1_close, |
1613 | 778 }; |
779 | |
780 AVCodecParser mpegaudio_parser = { | |
781 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
782 sizeof(MpegAudioParseContext), | |
783 mpegaudio_parse_init, | |
784 mpegaudio_parse, | |
785 NULL, | |
786 }; | |
787 | |
788 #ifdef CONFIG_AC3 | |
789 AVCodecParser ac3_parser = { | |
790 { CODEC_ID_AC3 }, | |
791 sizeof(AC3ParseContext), | |
792 ac3_parse_init, | |
793 ac3_parse, | |
794 NULL, | |
795 }; | |
796 #endif |