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