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