r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / parser.c
blob280bb45f578b518b27c056c88be44ebfadcbfffa
1 /*
2 * Audio and Video frame extraction
3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2003 Michael Niedermayer.
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.
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.
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
20 #include "avcodec.h"
21 #include "mpegvideo.h"
22 #include "mpegaudio.h"
24 AVCodecParser *av_first_parser = NULL;
26 void av_register_codec_parser(AVCodecParser *parser)
28 parser->next = av_first_parser;
29 av_first_parser = parser;
32 AVCodecParserContext *av_parser_init(int codec_id)
34 AVCodecParserContext *s;
35 AVCodecParser *parser;
36 int ret;
38 if(codec_id == CODEC_ID_NONE)
39 return NULL;
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 ||
44 parser->codec_ids[2] == codec_id ||
45 parser->codec_ids[3] == codec_id ||
46 parser->codec_ids[4] == codec_id)
47 goto found;
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;
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;
68 s->fetch_timestamp=1;
69 return s;
72 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
73 can be returned if necessary */
74 int av_parser_parse(AVCodecParserContext *s,
75 AVCodecContext *avctx,
76 uint8_t **poutbuf, int *poutbuf_size,
77 const uint8_t *buf, int buf_size,
78 int64_t pts, int64_t dts)
80 int index, i, k;
81 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
83 if (buf_size == 0) {
84 /* padding is always necessary even if EOF, so we add it here */
85 memset(dummy_buf, 0, sizeof(dummy_buf));
86 buf = dummy_buf;
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;
95 /* fill first PTS/DTS */
96 if (s->fetch_timestamp){
97 s->fetch_timestamp=0;
98 s->last_pts = pts;
99 s->last_dts = dts;
100 s->cur_frame_pts[k] =
101 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
105 /* WARNING: the returned index can be negative */
106 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
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);
108 /* update the file pointer */
109 if (*poutbuf_size) {
110 /* fill the data for the current frame */
111 s->frame_offset = s->last_frame_offset;
112 s->pts = s->last_pts;
113 s->dts = s->last_dts;
115 /* offset of the next frame */
116 s->last_frame_offset = s->cur_offset + index;
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);
130 s->last_pts = s->cur_frame_pts[k];
131 s->last_dts = s->cur_frame_dts[k];
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;
139 if (index < 0)
140 index = 0;
141 s->cur_offset += index;
142 return index;
147 * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
149 int av_parser_change(AVCodecParserContext *s,
150 AVCodecContext *avctx,
151 uint8_t **poutbuf, int *poutbuf_size,
152 const uint8_t *buf, int buf_size, int keyframe){
154 if(s && s->parser->split){
155 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
156 int i= s->parser->split(avctx, buf, buf_size);
157 buf += i;
158 buf_size -= i;
162 *poutbuf= buf;
163 *poutbuf_size= buf_size;
164 if(avctx->extradata){
165 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
166 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
167 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
168 int size= buf_size + avctx->extradata_size;
169 *poutbuf_size= size;
170 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
172 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
173 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
174 return 1;
178 return 0;
181 void av_parser_close(AVCodecParserContext *s)
183 if (s->parser->parser_close)
184 s->parser->parser_close(s);
185 av_free(s->priv_data);
186 av_free(s);
189 /*****************************************************/
191 //#define END_NOT_FOUND (-100)
193 #define PICTURE_START_CODE 0x00000100
194 #define SEQ_START_CODE 0x000001b3
195 #define EXT_START_CODE 0x000001b5
196 #define SLICE_MIN_START_CODE 0x00000101
197 #define SLICE_MAX_START_CODE 0x000001af
199 typedef struct ParseContext1{
200 ParseContext pc;
201 /* XXX/FIXME PC1 vs. PC */
202 /* MPEG2 specific */
203 int frame_rate;
204 int progressive_sequence;
205 int width, height;
207 /* XXX: suppress that, needed by MPEG4 */
208 MpegEncContext *enc;
209 int first_picture;
210 } ParseContext1;
213 * combines the (truncated) bitstream to a complete frame
214 * @returns -1 if no complete frame could be created
216 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
218 #if 0
219 if(pc->overread){
220 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
221 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
223 #endif
225 /* copy overreaded bytes from last frame into buffer */
226 for(; pc->overread>0; pc->overread--){
227 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
230 /* flush remaining if EOF */
231 if(!*buf_size && next == END_NOT_FOUND){
232 next= 0;
235 pc->last_index= pc->index;
237 /* copy into buffer end return */
238 if(next == END_NOT_FOUND){
239 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
241 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
242 pc->index += *buf_size;
243 return -1;
246 *buf_size=
247 pc->overread_index= pc->index + next;
249 /* append to buffer */
250 if(pc->index){
251 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
253 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
254 pc->index = 0;
255 *buf= pc->buffer;
258 /* store overread bytes */
259 for(;next < 0; next++){
260 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
261 pc->overread++;
264 #if 0
265 if(pc->overread){
266 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
267 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
269 #endif
271 return 0;
274 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
276 const uint8_t *buf_ptr;
277 unsigned int state=0xFFFFFFFF, v;
278 int val;
280 buf_ptr = *pbuf_ptr;
281 while (buf_ptr < buf_end) {
282 v = *buf_ptr++;
283 if (state == 0x000001) {
284 state = ((state << 8) | v) & 0xffffff;
285 val = state;
286 goto found;
288 state = ((state << 8) | v) & 0xffffff;
290 val = -1;
291 found:
292 *pbuf_ptr = buf_ptr;
293 return val;
296 /* XXX: merge with libavcodec ? */
297 #define MPEG1_FRAME_RATE_BASE 1001
299 static const int frame_rate_tab[16] = {
301 24000,
302 24024,
303 25025,
304 30000,
305 30030,
306 50050,
307 60000,
308 60060,
309 // Xing's 15fps: (9)
310 15015,
311 // libmpeg3's "Unofficial economy rates": (10-13)
312 5005,
313 10010,
314 12012,
315 15015,
316 // random, just to avoid segfault !never encode these
317 25025,
318 25025,
321 //FIXME move into mpeg12.c
322 static void mpegvideo_extract_headers(AVCodecParserContext *s,
323 AVCodecContext *avctx,
324 const uint8_t *buf, int buf_size)
326 ParseContext1 *pc = s->priv_data;
327 const uint8_t *buf_end;
328 int32_t start_code;
329 int frame_rate_index, ext_type, bytes_left;
330 int frame_rate_ext_n, frame_rate_ext_d;
331 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
332 int horiz_size_ext, vert_size_ext, bit_rate_ext;
333 //FIXME replace the crap with get_bits()
334 s->repeat_pict = 0;
335 buf_end = buf + buf_size;
336 while (buf < buf_end) {
337 start_code = find_start_code(&buf, buf_end);
338 bytes_left = buf_end - buf;
339 switch(start_code) {
340 case PICTURE_START_CODE:
341 if (bytes_left >= 2) {
342 s->pict_type = (buf[1] >> 3) & 7;
344 break;
345 case SEQ_START_CODE:
346 if (bytes_left >= 7) {
347 pc->width = (buf[0] << 4) | (buf[1] >> 4);
348 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
349 avcodec_set_dimensions(avctx, pc->width, pc->height);
350 frame_rate_index = buf[3] & 0xf;
351 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
352 avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
353 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
354 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
355 avctx->sub_id = 1;
357 break;
358 case EXT_START_CODE:
359 if (bytes_left >= 1) {
360 ext_type = (buf[0] >> 4);
361 switch(ext_type) {
362 case 0x1: /* sequence extension */
363 if (bytes_left >= 6) {
364 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
365 vert_size_ext = (buf[2] >> 5) & 3;
366 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
367 frame_rate_ext_n = (buf[5] >> 5) & 3;
368 frame_rate_ext_d = (buf[5] & 0x1f);
369 pc->progressive_sequence = buf[1] & (1 << 3);
370 avctx->has_b_frames= !(buf[5] >> 7);
372 pc->width |=(horiz_size_ext << 12);
373 pc->height |=( vert_size_ext << 12);
374 avctx->bit_rate += (bit_rate_ext << 18) * 400;
375 avcodec_set_dimensions(avctx, pc->width, pc->height);
376 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
377 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
378 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
379 avctx->sub_id = 2; /* forces MPEG2 */
381 break;
382 case 0x8: /* picture coding extension */
383 if (bytes_left >= 5) {
384 picture_structure = buf[2]&3;
385 top_field_first = buf[3] & (1 << 7);
386 repeat_first_field = buf[3] & (1 << 1);
387 progressive_frame = buf[4] & (1 << 7);
389 /* check if we must repeat the frame */
390 if (repeat_first_field) {
391 if (pc->progressive_sequence) {
392 if (top_field_first)
393 s->repeat_pict = 4;
394 else
395 s->repeat_pict = 2;
396 } else if (progressive_frame) {
397 s->repeat_pict = 1;
401 /* the packet only represents half a frame
402 XXX,FIXME maybe find a different solution */
403 if(picture_structure != 3)
404 s->repeat_pict = -1;
406 break;
409 break;
410 case -1:
411 goto the_end;
412 default:
413 /* we stop parsing when we encounter a slice. It ensures
414 that this function takes a negligible amount of time */
415 if (start_code >= SLICE_MIN_START_CODE &&
416 start_code <= SLICE_MAX_START_CODE)
417 goto the_end;
418 break;
421 the_end: ;
424 static int mpegvideo_parse(AVCodecParserContext *s,
425 AVCodecContext *avctx,
426 uint8_t **poutbuf, int *poutbuf_size,
427 const uint8_t *buf, int buf_size)
429 ParseContext1 *pc1 = s->priv_data;
430 ParseContext *pc= &pc1->pc;
431 int next;
433 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
435 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
436 *poutbuf = NULL;
437 *poutbuf_size = 0;
438 return buf_size;
440 /* we have a full frame : we just parse the first few MPEG headers
441 to have the full timing information. The time take by this
442 function should be negligible for uncorrupted streams */
443 mpegvideo_extract_headers(s, avctx, buf, buf_size);
444 #if 0
445 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
446 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
447 #endif
449 *poutbuf = (uint8_t *)buf;
450 *poutbuf_size = buf_size;
451 return next;
454 static int mpegvideo_split(AVCodecContext *avctx,
455 const uint8_t *buf, int buf_size)
457 int i;
458 uint32_t state= -1;
460 for(i=0; i<buf_size; i++){
461 state= (state<<8) | buf[i];
462 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
463 return i-3;
465 return 0;
468 void ff_parse_close(AVCodecParserContext *s)
470 ParseContext *pc = s->priv_data;
472 av_free(pc->buffer);
475 static void parse1_close(AVCodecParserContext *s)
477 ParseContext1 *pc1 = s->priv_data;
479 av_free(pc1->pc.buffer);
480 av_free(pc1->enc);
483 /*************************/
485 /* used by parser */
486 /* XXX: make it use less memory */
487 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
488 AVCodecContext *avctx,
489 const uint8_t *buf, int buf_size)
491 ParseContext1 *pc = s1->priv_data;
492 MpegEncContext *s = pc->enc;
493 GetBitContext gb1, *gb = &gb1;
494 int ret;
496 s->avctx = avctx;
497 s->current_picture_ptr = &s->current_picture;
499 if (avctx->extradata_size && pc->first_picture){
500 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
501 ret = ff_mpeg4_decode_picture_header(s, gb);
504 init_get_bits(gb, buf, 8 * buf_size);
505 ret = ff_mpeg4_decode_picture_header(s, gb);
506 if (s->width) {
507 avcodec_set_dimensions(avctx, s->width, s->height);
509 pc->first_picture = 0;
510 return ret;
513 static int mpeg4video_parse_init(AVCodecParserContext *s)
515 ParseContext1 *pc = s->priv_data;
517 pc->enc = av_mallocz(sizeof(MpegEncContext));
518 if (!pc->enc)
519 return -1;
520 pc->first_picture = 1;
521 return 0;
524 static int mpeg4video_parse(AVCodecParserContext *s,
525 AVCodecContext *avctx,
526 uint8_t **poutbuf, int *poutbuf_size,
527 const uint8_t *buf, int buf_size)
529 ParseContext *pc = s->priv_data;
530 int next;
532 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
534 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
535 *poutbuf = NULL;
536 *poutbuf_size = 0;
537 return buf_size;
539 av_mpeg4_decode_header(s, avctx, buf, buf_size);
541 *poutbuf = (uint8_t *)buf;
542 *poutbuf_size = buf_size;
543 return next;
546 static int mpeg4video_split(AVCodecContext *avctx,
547 const uint8_t *buf, int buf_size)
549 int i;
550 uint32_t state= -1;
552 for(i=0; i<buf_size; i++){
553 state= (state<<8) | buf[i];
554 if(state == 0x1B3 || state == 0x1B6)
555 return i-3;
557 return 0;
560 /*************************/
562 typedef struct MpegAudioParseContext {
563 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
564 uint8_t *inbuf_ptr;
565 int frame_size;
566 int free_format_frame_size;
567 int free_format_next_header;
568 uint32_t header;
569 int header_count;
570 } MpegAudioParseContext;
572 #define MPA_HEADER_SIZE 4
574 /* header + layer + bitrate + freq + lsf/mpeg25 */
575 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
576 #define SAME_HEADER_MASK \
577 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
579 static int mpegaudio_parse_init(AVCodecParserContext *s1)
581 MpegAudioParseContext *s = s1->priv_data;
582 s->inbuf_ptr = s->inbuf;
583 return 0;
586 static int mpegaudio_parse(AVCodecParserContext *s1,
587 AVCodecContext *avctx,
588 uint8_t **poutbuf, int *poutbuf_size,
589 const uint8_t *buf, int buf_size)
591 MpegAudioParseContext *s = s1->priv_data;
592 int len, ret, sr;
593 uint32_t header;
594 const uint8_t *buf_ptr;
596 *poutbuf = NULL;
597 *poutbuf_size = 0;
598 buf_ptr = buf;
599 while (buf_size > 0) {
600 len = s->inbuf_ptr - s->inbuf;
601 if (s->frame_size == 0) {
602 /* special case for next header for first frame in free
603 format case (XXX: find a simpler method) */
604 if (s->free_format_next_header != 0) {
605 s->inbuf[0] = s->free_format_next_header >> 24;
606 s->inbuf[1] = s->free_format_next_header >> 16;
607 s->inbuf[2] = s->free_format_next_header >> 8;
608 s->inbuf[3] = s->free_format_next_header;
609 s->inbuf_ptr = s->inbuf + 4;
610 s->free_format_next_header = 0;
611 goto got_header;
613 /* no header seen : find one. We need at least MPA_HEADER_SIZE
614 bytes to parse it */
615 len = MPA_HEADER_SIZE - len;
616 if (len > buf_size)
617 len = buf_size;
618 if (len > 0) {
619 memcpy(s->inbuf_ptr, buf_ptr, len);
620 buf_ptr += len;
621 buf_size -= len;
622 s->inbuf_ptr += len;
624 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
625 got_header:
626 sr= avctx->sample_rate;
627 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
628 (s->inbuf[2] << 8) | s->inbuf[3];
630 ret = mpa_decode_header(avctx, header);
631 if (ret < 0) {
632 s->header_count= -2;
633 /* no sync found : move by one byte (inefficient, but simple!) */
634 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
635 s->inbuf_ptr--;
636 dprintf("skip %x\n", header);
637 /* reset free format frame size to give a chance
638 to get a new bitrate */
639 s->free_format_frame_size = 0;
640 } else {
641 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
642 s->header_count= -3;
643 s->header= header;
644 s->header_count++;
645 s->frame_size = ret;
647 #if 0
648 /* free format: prepare to compute frame size */
649 if (decode_header(s, header) == 1) {
650 s->frame_size = -1;
652 #endif
654 if(s->header_count <= 0)
655 avctx->sample_rate= sr; //FIXME ugly
657 } else
658 #if 0
659 if (s->frame_size == -1) {
660 /* free format : find next sync to compute frame size */
661 len = MPA_MAX_CODED_FRAME_SIZE - len;
662 if (len > buf_size)
663 len = buf_size;
664 if (len == 0) {
665 /* frame too long: resync */
666 s->frame_size = 0;
667 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
668 s->inbuf_ptr--;
669 } else {
670 uint8_t *p, *pend;
671 uint32_t header1;
672 int padding;
674 memcpy(s->inbuf_ptr, buf_ptr, len);
675 /* check for header */
676 p = s->inbuf_ptr - 3;
677 pend = s->inbuf_ptr + len - 4;
678 while (p <= pend) {
679 header = (p[0] << 24) | (p[1] << 16) |
680 (p[2] << 8) | p[3];
681 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
682 (s->inbuf[2] << 8) | s->inbuf[3];
683 /* check with high probability that we have a
684 valid header */
685 if ((header & SAME_HEADER_MASK) ==
686 (header1 & SAME_HEADER_MASK)) {
687 /* header found: update pointers */
688 len = (p + 4) - s->inbuf_ptr;
689 buf_ptr += len;
690 buf_size -= len;
691 s->inbuf_ptr = p;
692 /* compute frame size */
693 s->free_format_next_header = header;
694 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
695 padding = (header1 >> 9) & 1;
696 if (s->layer == 1)
697 s->free_format_frame_size -= padding * 4;
698 else
699 s->free_format_frame_size -= padding;
700 dprintf("free frame size=%d padding=%d\n",
701 s->free_format_frame_size, padding);
702 decode_header(s, header1);
703 goto next_data;
705 p++;
707 /* not found: simply increase pointers */
708 buf_ptr += len;
709 s->inbuf_ptr += len;
710 buf_size -= len;
712 } else
713 #endif
714 if (len < s->frame_size) {
715 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
716 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
717 len = s->frame_size - len;
718 if (len > buf_size)
719 len = buf_size;
720 memcpy(s->inbuf_ptr, buf_ptr, len);
721 buf_ptr += len;
722 s->inbuf_ptr += len;
723 buf_size -= len;
725 // next_data:
726 if (s->frame_size > 0 &&
727 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
728 if(s->header_count > 0){
729 *poutbuf = s->inbuf;
730 *poutbuf_size = s->inbuf_ptr - s->inbuf;
732 s->inbuf_ptr = s->inbuf;
733 s->frame_size = 0;
734 break;
737 return buf_ptr - buf;
740 #ifdef CONFIG_AC3
741 extern int a52_syncinfo (const uint8_t * buf, int * flags,
742 int * sample_rate, int * bit_rate);
744 typedef struct AC3ParseContext {
745 uint8_t inbuf[4096]; /* input buffer */
746 uint8_t *inbuf_ptr;
747 int frame_size;
748 int flags;
749 } AC3ParseContext;
751 #define AC3_HEADER_SIZE 7
752 #define A52_LFE 16
754 static int ac3_parse_init(AVCodecParserContext *s1)
756 AC3ParseContext *s = s1->priv_data;
757 s->inbuf_ptr = s->inbuf;
758 return 0;
761 static int ac3_parse(AVCodecParserContext *s1,
762 AVCodecContext *avctx,
763 uint8_t **poutbuf, int *poutbuf_size,
764 const uint8_t *buf, int buf_size)
766 AC3ParseContext *s = s1->priv_data;
767 const uint8_t *buf_ptr;
768 int len, sample_rate, bit_rate;
769 static const int ac3_channels[8] = {
770 2, 1, 2, 3, 3, 4, 4, 5
773 *poutbuf = NULL;
774 *poutbuf_size = 0;
776 buf_ptr = buf;
777 while (buf_size > 0) {
778 len = s->inbuf_ptr - s->inbuf;
779 if (s->frame_size == 0) {
780 /* no header seen : find one. We need at least 7 bytes to parse it */
781 len = AC3_HEADER_SIZE - len;
782 if (len > buf_size)
783 len = buf_size;
784 memcpy(s->inbuf_ptr, buf_ptr, len);
785 buf_ptr += len;
786 s->inbuf_ptr += len;
787 buf_size -= len;
788 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
789 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
790 if (len == 0) {
791 /* no sync found : move by one byte (inefficient, but simple!) */
792 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
793 s->inbuf_ptr--;
794 } else {
795 s->frame_size = len;
796 /* update codec info */
797 avctx->sample_rate = sample_rate;
798 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
799 if(avctx->channels!=1 && avctx->channels!=2){
800 avctx->channels = ac3_channels[s->flags & 7];
801 if (s->flags & A52_LFE)
802 avctx->channels++;
804 avctx->bit_rate = bit_rate;
805 avctx->frame_size = 6 * 256;
808 } else if (len < s->frame_size) {
809 len = s->frame_size - len;
810 if (len > buf_size)
811 len = buf_size;
813 memcpy(s->inbuf_ptr, buf_ptr, len);
814 buf_ptr += len;
815 s->inbuf_ptr += len;
816 buf_size -= len;
817 } else {
818 *poutbuf = s->inbuf;
819 *poutbuf_size = s->frame_size;
820 s->inbuf_ptr = s->inbuf;
821 s->frame_size = 0;
822 break;
825 return buf_ptr - buf;
827 #endif
829 AVCodecParser mpegvideo_parser = {
830 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
831 sizeof(ParseContext1),
832 NULL,
833 mpegvideo_parse,
834 parse1_close,
835 mpegvideo_split,
838 AVCodecParser mpeg4video_parser = {
839 { CODEC_ID_MPEG4 },
840 sizeof(ParseContext1),
841 mpeg4video_parse_init,
842 mpeg4video_parse,
843 parse1_close,
844 mpeg4video_split,
847 AVCodecParser mpegaudio_parser = {
848 { CODEC_ID_MP2, CODEC_ID_MP3 },
849 sizeof(MpegAudioParseContext),
850 mpegaudio_parse_init,
851 mpegaudio_parse,
852 NULL,
855 #ifdef CONFIG_AC3
856 AVCodecParser ac3_parser = {
857 { CODEC_ID_AC3 },
858 sizeof(AC3ParseContext),
859 ac3_parse_init,
860 ac3_parse,
861 NULL,
863 #endif