comparison parser.c @ 1613:0279c6c61f11 libavcodec

new audio/video parser API
author bellard
date Mon, 10 Nov 2003 15:29:20 +0000
parents
children 6c82ef97d3e6
comparison
equal deleted inserted replaced
1612:0d2b59cf9f45 1613:0279c6c61f11
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;
37
38 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
39 if (parser->codec_ids[0] == codec_id ||
40 parser->codec_ids[1] == codec_id ||
41 parser->codec_ids[2] == codec_id)
42 goto found;
43 }
44 return NULL;
45 found:
46 s = av_mallocz(sizeof(AVCodecParserContext));
47 if (!s)
48 return NULL;
49 s->parser = parser;
50 s->priv_data = av_mallocz(parser->priv_data_size);
51 if (!s->priv_data) {
52 av_free(s);
53 return NULL;
54 }
55 if (parser->parser_init) {
56 ret = parser->parser_init(s);
57 if (ret != 0) {
58 av_free(s->priv_data);
59 av_free(s);
60 return NULL;
61 }
62 }
63 return s;
64 }
65
66 int av_parser_parse(AVCodecParserContext *s,
67 AVCodecContext *avctx,
68 uint8_t **poutbuf, int *poutbuf_size,
69 const uint8_t *buf, int buf_size)
70 {
71 int index;
72 /* WARNING: the returned index can be negative */
73 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
74 /* update the file pointer */
75 if (*poutbuf_size) {
76 s->frame_offset = s->last_frame_offset;
77 s->last_frame_offset = s->cur_offset + index;
78 }
79 if (index < 0)
80 index = 0;
81 s->cur_offset += index;
82 return index;
83 }
84
85 void av_parser_close(AVCodecParserContext *s)
86 {
87 if (s->parser->parser_close)
88 s->parser->parser_close(s);
89 av_free(s->priv_data);
90 av_free(s);
91 }
92
93 /*****************************************************/
94
95 //#define END_NOT_FOUND (-100)
96
97 #define PICTURE_START_CODE 0x00000100
98 #define SEQ_START_CODE 0x000001b3
99 #define EXT_START_CODE 0x000001b5
100 #define SLICE_MIN_START_CODE 0x00000101
101 #define SLICE_MAX_START_CODE 0x000001af
102
103 typedef struct ParseContext1{
104 uint8_t *buffer;
105 int index;
106 int last_index;
107 int buffer_size;
108 uint32_t state; ///< contains the last few bytes in MSB order
109 int frame_start_found;
110 int overread; ///< the number of bytes which where irreversibly read from the next frame
111 int overread_index; ///< the index into ParseContext1.buffer of the overreaded bytes
112
113 /* MPEG2 specific */
114 int frame_rate;
115 int progressive_sequence;
116 int width, height;
117 /* XXX: suppress that, needed by MPEG4 */
118 MpegEncContext *enc;
119 } ParseContext1;
120
121 /**
122 * combines the (truncated) bitstream to a complete frame
123 * @returns -1 if no complete frame could be created
124 */
125 static int ff_combine_frame1(ParseContext1 *pc, int next, uint8_t **buf, int *buf_size)
126 {
127 #if 0
128 if(pc->overread){
129 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
130 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
131 }
132 #endif
133
134 /* copy overreaded bytes from last frame into buffer */
135 for(; pc->overread>0; pc->overread--){
136 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
137 }
138
139 pc->last_index= pc->index;
140
141 /* copy into buffer end return */
142 if(next == END_NOT_FOUND){
143 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
144
145 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
146 pc->index += *buf_size;
147 return -1;
148 }
149
150 *buf_size=
151 pc->overread_index= pc->index + next;
152
153 /* append to buffer */
154 if(pc->index){
155 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
156
157 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
158 pc->index = 0;
159 *buf= pc->buffer;
160 }
161
162 /* store overread bytes */
163 for(;next < 0; next++){
164 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
165 pc->overread++;
166 }
167
168 #if 0
169 if(pc->overread){
170 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
171 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
172 }
173 #endif
174
175 return 0;
176 }
177
178 /**
179 * finds the end of the current frame in the bitstream.
180 * @return the position of the first byte of the next frame, or -1
181 */
182 static int mpeg1_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
183 {
184 int i;
185 uint32_t state;
186
187 state= pc->state;
188
189 i=0;
190 if(!pc->frame_start_found){
191 for(i=0; i<buf_size; i++){
192 state= (state<<8) | buf[i];
193 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
194 i++;
195 pc->frame_start_found=1;
196 break;
197 }
198 }
199 }
200
201 if(pc->frame_start_found){
202 for(; i<buf_size; i++){
203 state= (state<<8) | buf[i];
204 if((state&0xFFFFFF00) == 0x100){
205 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
206 pc->frame_start_found=0;
207 pc->state=-1;
208 return i-3;
209 }
210 }
211 }
212 }
213 pc->state= state;
214 return END_NOT_FOUND;
215 }
216
217 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
218 {
219 const uint8_t *buf_ptr;
220 unsigned int state=0xFFFFFFFF, v;
221 int val;
222
223 buf_ptr = *pbuf_ptr;
224 while (buf_ptr < buf_end) {
225 v = *buf_ptr++;
226 if (state == 0x000001) {
227 state = ((state << 8) | v) & 0xffffff;
228 val = state;
229 goto found;
230 }
231 state = ((state << 8) | v) & 0xffffff;
232 }
233 val = -1;
234 found:
235 *pbuf_ptr = buf_ptr;
236 return val;
237 }
238
239 /* XXX: merge with libavcodec ? */
240 #define MPEG1_FRAME_RATE_BASE 1001
241
242 static const int frame_rate_tab[16] = {
243 0,
244 24000,
245 24024,
246 25025,
247 30000,
248 30030,
249 50050,
250 60000,
251 60060,
252 // Xing's 15fps: (9)
253 15015,
254 // libmpeg3's "Unofficial economy rates": (10-13)
255 5005,
256 10010,
257 12012,
258 15015,
259 // random, just to avoid segfault !never encode these
260 25025,
261 25025,
262 };
263
264 static void mpegvideo_extract_headers(AVCodecParserContext *s,
265 AVCodecContext *avctx,
266 const uint8_t *buf, int buf_size)
267 {
268 ParseContext1 *pc = s->priv_data;
269 const uint8_t *buf_end;
270 int32_t start_code;
271 int frame_rate_index, ext_type, bytes_left;
272 int frame_rate_ext_n, frame_rate_ext_d;
273 int top_field_first, repeat_first_field, progressive_frame;
274 int horiz_size_ext, vert_size_ext;
275
276 s->repeat_pict = 0;
277 buf_end = buf + buf_size;
278 while (buf < buf_end) {
279 start_code = find_start_code(&buf, buf_end);
280 bytes_left = buf_end - buf;
281 switch(start_code) {
282 case PICTURE_START_CODE:
283 if (bytes_left >= 2) {
284 s->pict_type = (buf[1] >> 3) & 7;
285 }
286 break;
287 case SEQ_START_CODE:
288 if (bytes_left >= 4) {
289 pc->width = avctx->width = (buf[0] << 4) | (buf[1] >> 4);
290 pc->height = avctx->height = ((buf[1] & 0x0f) << 8) | buf[2];
291 frame_rate_index = buf[3] & 0xf;
292 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
293 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
294 }
295 break;
296 case EXT_START_CODE:
297 if (bytes_left >= 1) {
298 ext_type = (buf[0] >> 4);
299 switch(ext_type) {
300 case 0x1: /* sequence extension */
301 if (bytes_left >= 6) {
302 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
303 vert_size_ext = (buf[2] >> 5) & 3;
304 frame_rate_ext_n = (buf[5] >> 5) & 3;
305 frame_rate_ext_d = (buf[5] & 0x1f);
306 pc->progressive_sequence = buf[1] & (1 << 3);
307
308 avctx->width = pc->width | (horiz_size_ext << 12);
309 avctx->height = pc->height | (vert_size_ext << 12);
310 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
311 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
312 avctx->sub_id = 2; /* forces MPEG2 */
313 }
314 break;
315 case 0x8: /* picture coding extension */
316 if (bytes_left >= 5) {
317 top_field_first = buf[3] & (1 << 7);
318 repeat_first_field = buf[3] & (1 << 1);
319 progressive_frame = buf[4] & (1 << 7);
320
321 /* check if we must repeat the frame */
322 if (repeat_first_field) {
323 if (pc->progressive_sequence) {
324 if (top_field_first)
325 s->repeat_pict = 4;
326 else
327 s->repeat_pict = 2;
328 } else if (progressive_frame) {
329 s->repeat_pict = 1;
330 }
331 }
332 }
333 break;
334 }
335 }
336 break;
337 case -1:
338 goto the_end;
339 default:
340 /* we stop parsing when we encounter a slice. It ensures
341 that this function takes a negligible amount of time */
342 if (start_code >= SLICE_MIN_START_CODE &&
343 start_code <= SLICE_MAX_START_CODE)
344 goto the_end;
345 break;
346 }
347 }
348 the_end: ;
349 }
350
351 static int mpegvideo_parse(AVCodecParserContext *s,
352 AVCodecContext *avctx,
353 uint8_t **poutbuf, int *poutbuf_size,
354 const uint8_t *buf, int buf_size)
355 {
356 ParseContext1 *pc = s->priv_data;
357 int next;
358
359 next= mpeg1_find_frame_end(pc, buf, buf_size);
360
361 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
362 *poutbuf = NULL;
363 *poutbuf_size = 0;
364 return buf_size;
365 }
366 /* we have a full frame : we just parse the first few MPEG headers
367 to have the full timing information. The time take by this
368 function should be negligible for uncorrupted streams */
369 mpegvideo_extract_headers(s, avctx, buf, buf_size);
370 #if 0
371 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
372 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
373 #endif
374
375 *poutbuf = (uint8_t *)buf;
376 *poutbuf_size = buf_size;
377 return next;
378 }
379
380 static void mpegvideo_parse_close(AVCodecParserContext *s)
381 {
382 ParseContext1 *pc = s->priv_data;
383
384 av_free(pc->buffer);
385 av_free(pc->enc);
386 }
387
388 /*************************/
389
390 /**
391 * finds the end of the current frame in the bitstream.
392 * @return the position of the first byte of the next frame, or -1
393 */
394 static int mpeg4_find_frame_end(ParseContext1 *pc,
395 const uint8_t *buf, int buf_size)
396 {
397 int vop_found, i;
398 uint32_t state;
399
400 vop_found= pc->frame_start_found;
401 state= pc->state;
402
403 i=0;
404 if(!vop_found){
405 for(i=0; i<buf_size; i++){
406 state= (state<<8) | buf[i];
407 if(state == 0x1B6){
408 i++;
409 vop_found=1;
410 break;
411 }
412 }
413 }
414
415 if(vop_found){
416 for(; i<buf_size; i++){
417 state= (state<<8) | buf[i];
418 if((state&0xFFFFFF00) == 0x100){
419 pc->frame_start_found=0;
420 pc->state=-1;
421 return i-3;
422 }
423 }
424 }
425 pc->frame_start_found= vop_found;
426 pc->state= state;
427 return END_NOT_FOUND;
428 }
429
430 /* used by parser */
431 /* XXX: make it use less memory */
432 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
433 AVCodecContext *avctx,
434 const uint8_t *buf, int buf_size)
435 {
436 ParseContext1 *pc = s1->priv_data;
437 MpegEncContext *s = pc->enc;
438 GetBitContext gb1, *gb = &gb1;
439 int ret;
440
441 s->avctx = avctx;
442 init_get_bits(gb, buf, 8 * buf_size);
443 ret = ff_mpeg4_decode_picture_header(s, gb);
444 if (s->width) {
445 avctx->width = s->width;
446 avctx->height = s->height;
447 }
448 return ret;
449 }
450
451 int mpeg4video_parse_init(AVCodecParserContext *s)
452 {
453 ParseContext1 *pc = s->priv_data;
454 pc->enc = av_mallocz(sizeof(MpegEncContext));
455 if (!pc->enc)
456 return -1;
457 return 0;
458 }
459
460 static int mpeg4video_parse(AVCodecParserContext *s,
461 AVCodecContext *avctx,
462 uint8_t **poutbuf, int *poutbuf_size,
463 const uint8_t *buf, int buf_size)
464 {
465 ParseContext1 *pc = s->priv_data;
466 int next;
467
468 next= mpeg4_find_frame_end(pc, buf, buf_size);
469
470 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
471 *poutbuf = NULL;
472 *poutbuf_size = 0;
473 return buf_size;
474 }
475 av_mpeg4_decode_header(s, avctx, buf, buf_size);
476
477 *poutbuf = (uint8_t *)buf;
478 *poutbuf_size = buf_size;
479 return next;
480 }
481
482 /*************************/
483
484 static int h263_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
485 {
486 int vop_found, i;
487 uint32_t state;
488
489 vop_found= pc->frame_start_found;
490 state= pc->state;
491
492 i=0;
493 if(!vop_found){
494 for(i=0; i<buf_size; i++){
495 state= (state<<8) | buf[i];
496 if(state>>(32-22) == 0x20){
497 i++;
498 vop_found=1;
499 break;
500 }
501 }
502 }
503
504 if(vop_found){
505 for(; i<buf_size; i++){
506 state= (state<<8) | buf[i];
507 if(state>>(32-22) == 0x20){
508 pc->frame_start_found=0;
509 pc->state=-1;
510 return i-3;
511 }
512 }
513 }
514 pc->frame_start_found= vop_found;
515 pc->state= state;
516
517 return END_NOT_FOUND;
518 }
519
520 static int h263_parse(AVCodecParserContext *s,
521 AVCodecContext *avctx,
522 uint8_t **poutbuf, int *poutbuf_size,
523 const uint8_t *buf, int buf_size)
524 {
525 ParseContext1 *pc = s->priv_data;
526 int next;
527
528 next= h263_find_frame_end(pc, buf, buf_size);
529
530 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
531 *poutbuf = NULL;
532 *poutbuf_size = 0;
533 return buf_size;
534 }
535
536 *poutbuf = (uint8_t *)buf;
537 *poutbuf_size = buf_size;
538 return next;
539 }
540
541 /*************************/
542
543 /**
544 * finds the end of the current frame in the bitstream.
545 * @return the position of the first byte of the next frame, or -1
546 */
547 static int h264_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
548 {
549 int i;
550 uint32_t state;
551 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
552 // mb_addr= pc->mb_addr - 1;
553 state= pc->state;
554 //FIXME this will fail with slices
555 for(i=0; i<buf_size; i++){
556 state= (state<<8) | buf[i];
557 if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
558 if(pc->frame_start_found){
559 pc->state=-1;
560 pc->frame_start_found= 0;
561 return i-3;
562 }
563 pc->frame_start_found= 1;
564 }
565 }
566
567 pc->state= state;
568 return END_NOT_FOUND;
569 }
570
571 static int h264_parse(AVCodecParserContext *s,
572 AVCodecContext *avctx,
573 uint8_t **poutbuf, int *poutbuf_size,
574 const uint8_t *buf, int buf_size)
575 {
576 ParseContext1 *pc = s->priv_data;
577 int next;
578
579 next= h264_find_frame_end(pc, buf, buf_size);
580
581 if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
582 *poutbuf = NULL;
583 *poutbuf_size = 0;
584 return buf_size;
585 }
586
587 *poutbuf = (uint8_t *)buf;
588 *poutbuf_size = buf_size;
589 return next;
590 }
591
592 /*************************/
593
594 typedef struct MpegAudioParseContext {
595 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
596 uint8_t *inbuf_ptr;
597 int frame_size;
598 int free_format_frame_size;
599 int free_format_next_header;
600 } MpegAudioParseContext;
601
602 #define MPA_HEADER_SIZE 4
603
604 /* header + layer + bitrate + freq + lsf/mpeg25 */
605 #define SAME_HEADER_MASK \
606 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
607
608 static int mpegaudio_parse_init(AVCodecParserContext *s1)
609 {
610 MpegAudioParseContext *s = s1->priv_data;
611 s->inbuf_ptr = s->inbuf;
612 return 0;
613 }
614
615 static int mpegaudio_parse(AVCodecParserContext *s1,
616 AVCodecContext *avctx,
617 uint8_t **poutbuf, int *poutbuf_size,
618 const uint8_t *buf, int buf_size)
619 {
620 MpegAudioParseContext *s = s1->priv_data;
621 int len, ret;
622 uint32_t header;
623 const uint8_t *buf_ptr;
624
625 *poutbuf = NULL;
626 *poutbuf_size = 0;
627 buf_ptr = buf;
628 while (buf_size > 0) {
629 len = s->inbuf_ptr - s->inbuf;
630 if (s->frame_size == 0) {
631 /* special case for next header for first frame in free
632 format case (XXX: find a simpler method) */
633 if (s->free_format_next_header != 0) {
634 s->inbuf[0] = s->free_format_next_header >> 24;
635 s->inbuf[1] = s->free_format_next_header >> 16;
636 s->inbuf[2] = s->free_format_next_header >> 8;
637 s->inbuf[3] = s->free_format_next_header;
638 s->inbuf_ptr = s->inbuf + 4;
639 s->free_format_next_header = 0;
640 goto got_header;
641 }
642 /* no header seen : find one. We need at least MPA_HEADER_SIZE
643 bytes to parse it */
644 len = MPA_HEADER_SIZE - len;
645 if (len > buf_size)
646 len = buf_size;
647 if (len > 0) {
648 memcpy(s->inbuf_ptr, buf_ptr, len);
649 buf_ptr += len;
650 buf_size -= len;
651 s->inbuf_ptr += len;
652 }
653 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
654 got_header:
655 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
656 (s->inbuf[2] << 8) | s->inbuf[3];
657
658 ret = mpa_decode_header(avctx, header);
659 if (ret < 0) {
660 /* no sync found : move by one byte (inefficient, but simple!) */
661 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
662 s->inbuf_ptr--;
663 dprintf("skip %x\n", header);
664 /* reset free format frame size to give a chance
665 to get a new bitrate */
666 s->free_format_frame_size = 0;
667 } else {
668 s->frame_size = ret;
669 #if 0
670 /* free format: prepare to compute frame size */
671 if (decode_header(s, header) == 1) {
672 s->frame_size = -1;
673 }
674 #endif
675 }
676 }
677 } else
678 #if 0
679 if (s->frame_size == -1) {
680 /* free format : find next sync to compute frame size */
681 len = MPA_MAX_CODED_FRAME_SIZE - len;
682 if (len > buf_size)
683 len = buf_size;
684 if (len == 0) {
685 /* frame too long: resync */
686 s->frame_size = 0;
687 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
688 s->inbuf_ptr--;
689 } else {
690 uint8_t *p, *pend;
691 uint32_t header1;
692 int padding;
693
694 memcpy(s->inbuf_ptr, buf_ptr, len);
695 /* check for header */
696 p = s->inbuf_ptr - 3;
697 pend = s->inbuf_ptr + len - 4;
698 while (p <= pend) {
699 header = (p[0] << 24) | (p[1] << 16) |
700 (p[2] << 8) | p[3];
701 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
702 (s->inbuf[2] << 8) | s->inbuf[3];
703 /* check with high probability that we have a
704 valid header */
705 if ((header & SAME_HEADER_MASK) ==
706 (header1 & SAME_HEADER_MASK)) {
707 /* header found: update pointers */
708 len = (p + 4) - s->inbuf_ptr;
709 buf_ptr += len;
710 buf_size -= len;
711 s->inbuf_ptr = p;
712 /* compute frame size */
713 s->free_format_next_header = header;
714 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
715 padding = (header1 >> 9) & 1;
716 if (s->layer == 1)
717 s->free_format_frame_size -= padding * 4;
718 else
719 s->free_format_frame_size -= padding;
720 dprintf("free frame size=%d padding=%d\n",
721 s->free_format_frame_size, padding);
722 decode_header(s, header1);
723 goto next_data;
724 }
725 p++;
726 }
727 /* not found: simply increase pointers */
728 buf_ptr += len;
729 s->inbuf_ptr += len;
730 buf_size -= len;
731 }
732 } else
733 #endif
734 if (len < s->frame_size) {
735 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
736 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
737 len = s->frame_size - len;
738 if (len > buf_size)
739 len = buf_size;
740 memcpy(s->inbuf_ptr, buf_ptr, len);
741 buf_ptr += len;
742 s->inbuf_ptr += len;
743 buf_size -= len;
744 }
745 // next_data:
746 if (s->frame_size > 0 &&
747 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
748 *poutbuf = s->inbuf;
749 *poutbuf_size = s->inbuf_ptr - s->inbuf;
750 s->inbuf_ptr = s->inbuf;
751 s->frame_size = 0;
752 break;
753 }
754 }
755 return buf_ptr - buf;
756 }
757
758 #ifdef CONFIG_AC3
759 extern int a52_syncinfo (const uint8_t * buf, int * flags,
760 int * sample_rate, int * bit_rate);
761
762 typedef struct AC3ParseContext {
763 uint8_t inbuf[4096]; /* input buffer */
764 uint8_t *inbuf_ptr;
765 int frame_size;
766 int flags;
767 } AC3ParseContext;
768
769 #define AC3_HEADER_SIZE 7
770 #define A52_LFE 16
771
772 static int ac3_parse_init(AVCodecParserContext *s1)
773 {
774 AC3ParseContext *s = s1->priv_data;
775 s->inbuf_ptr = s->inbuf;
776 return 0;
777 }
778
779 static int ac3_parse(AVCodecParserContext *s1,
780 AVCodecContext *avctx,
781 uint8_t **poutbuf, int *poutbuf_size,
782 const uint8_t *buf, int buf_size)
783 {
784 AC3ParseContext *s = s1->priv_data;
785 const uint8_t *buf_ptr;
786 int len, sample_rate, bit_rate;
787 static const int ac3_channels[8] = {
788 2, 1, 2, 3, 3, 4, 4, 5
789 };
790
791 *poutbuf = NULL;
792 *poutbuf_size = 0;
793
794 buf_ptr = buf;
795 while (buf_size > 0) {
796 len = s->inbuf_ptr - s->inbuf;
797 if (s->frame_size == 0) {
798 /* no header seen : find one. We need at least 7 bytes to parse it */
799 len = AC3_HEADER_SIZE - len;
800 if (len > buf_size)
801 len = buf_size;
802 memcpy(s->inbuf_ptr, buf_ptr, len);
803 buf_ptr += len;
804 s->inbuf_ptr += len;
805 buf_size -= len;
806 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
807 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
808 if (len == 0) {
809 /* no sync found : move by one byte (inefficient, but simple!) */
810 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
811 s->inbuf_ptr--;
812 } else {
813 s->frame_size = len;
814 /* update codec info */
815 avctx->sample_rate = sample_rate;
816 avctx->channels = ac3_channels[s->flags & 7];
817 if (s->flags & A52_LFE)
818 avctx->channels++;
819 avctx->bit_rate = bit_rate;
820 avctx->frame_size = 6 * 256;
821 }
822 }
823 } else if (len < s->frame_size) {
824 len = s->frame_size - len;
825 if (len > buf_size)
826 len = buf_size;
827
828 memcpy(s->inbuf_ptr, buf_ptr, len);
829 buf_ptr += len;
830 s->inbuf_ptr += len;
831 buf_size -= len;
832 } else {
833 *poutbuf = s->inbuf;
834 *poutbuf_size = s->frame_size;
835 s->inbuf_ptr = s->inbuf;
836 s->frame_size = 0;
837 break;
838 }
839 }
840 return buf_ptr - buf;
841 }
842 #endif
843
844 AVCodecParser mpegvideo_parser = {
845 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
846 sizeof(ParseContext1),
847 NULL,
848 mpegvideo_parse,
849 mpegvideo_parse_close,
850 };
851
852 AVCodecParser mpeg4video_parser = {
853 { CODEC_ID_MPEG4 },
854 sizeof(ParseContext1),
855 mpeg4video_parse_init,
856 mpeg4video_parse,
857 mpegvideo_parse_close,
858 };
859
860 AVCodecParser h263_parser = {
861 { CODEC_ID_H263 },
862 sizeof(ParseContext1),
863 NULL,
864 h263_parse,
865 mpegvideo_parse_close,
866 };
867
868 AVCodecParser h264_parser = {
869 { CODEC_ID_H264 },
870 sizeof(ParseContext1),
871 NULL,
872 h264_parse,
873 mpegvideo_parse_close,
874 };
875
876 AVCodecParser mpegaudio_parser = {
877 { CODEC_ID_MP2, CODEC_ID_MP3 },
878 sizeof(MpegAudioParseContext),
879 mpegaudio_parse_init,
880 mpegaudio_parse,
881 NULL,
882 };
883
884 #ifdef CONFIG_AC3
885 AVCodecParser ac3_parser = {
886 { CODEC_ID_AC3 },
887 sizeof(AC3ParseContext),
888 ac3_parse_init,
889 ac3_parse,
890 NULL,
891 };
892 #endif