2 * Audio and Video frame extraction
3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2003 Michael Niedermayer.
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "mpegvideo.h"
24 #include "mpegaudio.h"
26 AVCodecParser *av_first_parser = NULL;
28 void av_register_codec_parser(AVCodecParser *parser)
30 parser->next = av_first_parser;
31 av_first_parser = parser;
34 AVCodecParserContext *av_parser_init(int codec_id)
36 AVCodecParserContext *s;
37 AVCodecParser *parser;
40 if(codec_id == CODEC_ID_NONE)
43 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
44 if (parser->codec_ids[0] == codec_id ||
45 parser->codec_ids[1] == codec_id ||
46 parser->codec_ids[2] == codec_id ||
47 parser->codec_ids[3] == codec_id ||
48 parser->codec_ids[4] == codec_id)
53 s = av_mallocz(sizeof(AVCodecParserContext));
57 s->priv_data = av_mallocz(parser->priv_data_size);
62 if (parser->parser_init) {
63 ret = parser->parser_init(s);
65 av_free(s->priv_data);
74 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
75 can be returned if necessary */
76 int av_parser_parse(AVCodecParserContext *s,
77 AVCodecContext *avctx,
78 uint8_t **poutbuf, int *poutbuf_size,
79 const uint8_t *buf, int buf_size,
80 int64_t pts, int64_t dts)
83 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
86 /* padding is always necessary even if EOF, so we add it here */
87 memset(dummy_buf, 0, sizeof(dummy_buf));
90 /* add a new packet descriptor */
91 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
92 s->cur_frame_start_index = k;
93 s->cur_frame_offset[k] = s->cur_offset;
94 s->cur_frame_pts[k] = pts;
95 s->cur_frame_dts[k] = dts;
97 /* fill first PTS/DTS */
98 if (s->fetch_timestamp){
102 s->cur_frame_pts[k] =
103 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
107 /* WARNING: the returned index can be negative */
108 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
109 //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);
110 /* update the file pointer */
112 /* fill the data for the current frame */
113 s->frame_offset = s->last_frame_offset;
114 s->pts = s->last_pts;
115 s->dts = s->last_dts;
117 /* offset of the next frame */
118 s->last_frame_offset = s->cur_offset + index;
119 /* find the packet in which the new frame starts. It
120 is tricky because of MPEG video start codes
121 which can begin in one packet and finish in
122 another packet. In the worst case, an MPEG
123 video start code could be in 4 different
125 k = s->cur_frame_start_index;
126 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
127 if (s->last_frame_offset >= s->cur_frame_offset[k])
129 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
132 s->last_pts = s->cur_frame_pts[k];
133 s->last_dts = s->cur_frame_dts[k];
135 /* some parsers tell us the packet size even before seeing the first byte of the next packet,
136 so the next pts/dts is in the next chunk */
137 if(index == buf_size){
138 s->fetch_timestamp=1;
143 s->cur_offset += index;
149 * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
150 * @deprecated use AVBitstreamFilter
152 int av_parser_change(AVCodecParserContext *s,
153 AVCodecContext *avctx,
154 uint8_t **poutbuf, int *poutbuf_size,
155 const uint8_t *buf, int buf_size, int keyframe){
157 if(s && s->parser->split){
158 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
159 int i= s->parser->split(avctx, buf, buf_size);
165 /* cast to avoid warning about discarding qualifiers */
166 *poutbuf= (uint8_t *) buf;
167 *poutbuf_size= buf_size;
168 if(avctx->extradata){
169 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
170 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
171 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
172 int size= buf_size + avctx->extradata_size;
174 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
176 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
177 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
185 void av_parser_close(AVCodecParserContext *s)
187 if (s->parser->parser_close)
188 s->parser->parser_close(s);
189 av_free(s->priv_data);
193 /*****************************************************/
195 //#define END_NOT_FOUND (-100)
197 #define PICTURE_START_CODE 0x00000100
198 #define SEQ_START_CODE 0x000001b3
199 #define EXT_START_CODE 0x000001b5
200 #define SLICE_MIN_START_CODE 0x00000101
201 #define SLICE_MAX_START_CODE 0x000001af
203 typedef struct ParseContext1{
205 /* XXX/FIXME PC1 vs. PC */
208 int progressive_sequence;
211 /* XXX: suppress that, needed by MPEG4 */
217 * combines the (truncated) bitstream to a complete frame
218 * @returns -1 if no complete frame could be created
220 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
224 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
225 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
229 /* copy overreaded bytes from last frame into buffer */
230 for(; pc->overread>0; pc->overread--){
231 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
234 /* flush remaining if EOF */
235 if(!*buf_size && next == END_NOT_FOUND){
239 pc->last_index= pc->index;
241 /* copy into buffer end return */
242 if(next == END_NOT_FOUND){
243 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
245 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
246 pc->index += *buf_size;
251 pc->overread_index= pc->index + next;
253 /* append to buffer */
255 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
257 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
262 /* store overread bytes */
263 for(;next < 0; next++){
264 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
270 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
271 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
278 /* XXX: merge with libavcodec ? */
279 #define MPEG1_FRAME_RATE_BASE 1001
281 static const int frame_rate_tab[16] = {
293 // libmpeg3's "Unofficial economy rates": (10-13)
298 // random, just to avoid segfault !never encode these
303 #ifdef CONFIG_MPEGVIDEO_PARSER
304 //FIXME move into mpeg12.c
305 static void mpegvideo_extract_headers(AVCodecParserContext *s,
306 AVCodecContext *avctx,
307 const uint8_t *buf, int buf_size)
309 ParseContext1 *pc = s->priv_data;
310 const uint8_t *buf_end;
312 int frame_rate_index, ext_type, bytes_left;
313 int frame_rate_ext_n, frame_rate_ext_d;
314 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
315 int horiz_size_ext, vert_size_ext, bit_rate_ext;
316 //FIXME replace the crap with get_bits()
318 buf_end = buf + buf_size;
319 while (buf < buf_end) {
321 buf= ff_find_start_code(buf, buf_end, &start_code);
322 bytes_left = buf_end - buf;
324 case PICTURE_START_CODE:
325 if (bytes_left >= 2) {
326 s->pict_type = (buf[1] >> 3) & 7;
330 if (bytes_left >= 7) {
331 pc->width = (buf[0] << 4) | (buf[1] >> 4);
332 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
333 avcodec_set_dimensions(avctx, pc->width, pc->height);
334 frame_rate_index = buf[3] & 0xf;
335 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
336 avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
337 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
338 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
343 if (bytes_left >= 1) {
344 ext_type = (buf[0] >> 4);
346 case 0x1: /* sequence extension */
347 if (bytes_left >= 6) {
348 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
349 vert_size_ext = (buf[2] >> 5) & 3;
350 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
351 frame_rate_ext_n = (buf[5] >> 5) & 3;
352 frame_rate_ext_d = (buf[5] & 0x1f);
353 pc->progressive_sequence = buf[1] & (1 << 3);
354 avctx->has_b_frames= !(buf[5] >> 7);
356 pc->width |=(horiz_size_ext << 12);
357 pc->height |=( vert_size_ext << 12);
358 avctx->bit_rate += (bit_rate_ext << 18) * 400;
359 avcodec_set_dimensions(avctx, pc->width, pc->height);
360 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
361 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
362 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
363 avctx->sub_id = 2; /* forces MPEG2 */
366 case 0x8: /* picture coding extension */
367 if (bytes_left >= 5) {
368 picture_structure = buf[2]&3;
369 top_field_first = buf[3] & (1 << 7);
370 repeat_first_field = buf[3] & (1 << 1);
371 progressive_frame = buf[4] & (1 << 7);
373 /* check if we must repeat the frame */
374 if (repeat_first_field) {
375 if (pc->progressive_sequence) {
380 } else if (progressive_frame) {
385 /* the packet only represents half a frame
386 XXX,FIXME maybe find a different solution */
387 if(picture_structure != 3)
397 /* we stop parsing when we encounter a slice. It ensures
398 that this function takes a negligible amount of time */
399 if (start_code >= SLICE_MIN_START_CODE &&
400 start_code <= SLICE_MAX_START_CODE)
408 static int mpegvideo_parse(AVCodecParserContext *s,
409 AVCodecContext *avctx,
410 uint8_t **poutbuf, int *poutbuf_size,
411 const uint8_t *buf, int buf_size)
413 ParseContext1 *pc1 = s->priv_data;
414 ParseContext *pc= &pc1->pc;
417 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
420 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
422 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
429 /* we have a full frame : we just parse the first few MPEG headers
430 to have the full timing information. The time take by this
431 function should be negligible for uncorrupted streams */
432 mpegvideo_extract_headers(s, avctx, buf, buf_size);
434 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
435 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
438 *poutbuf = (uint8_t *)buf;
439 *poutbuf_size = buf_size;
443 static int mpegvideo_split(AVCodecContext *avctx,
444 const uint8_t *buf, int buf_size)
449 for(i=0; i<buf_size; i++){
450 state= (state<<8) | buf[i];
451 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
456 #endif /* CONFIG_MPEGVIDEO_PARSER */
458 void ff_parse_close(AVCodecParserContext *s)
460 ParseContext *pc = s->priv_data;
465 static void parse1_close(AVCodecParserContext *s)
467 ParseContext1 *pc1 = s->priv_data;
469 av_free(pc1->pc.buffer);
473 /*************************/
475 #ifdef CONFIG_MPEG4VIDEO_PARSER
477 /* XXX: make it use less memory */
478 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
479 AVCodecContext *avctx,
480 const uint8_t *buf, int buf_size)
482 ParseContext1 *pc = s1->priv_data;
483 MpegEncContext *s = pc->enc;
484 GetBitContext gb1, *gb = &gb1;
488 s->current_picture_ptr = &s->current_picture;
490 if (avctx->extradata_size && pc->first_picture){
491 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
492 ret = ff_mpeg4_decode_picture_header(s, gb);
495 init_get_bits(gb, buf, 8 * buf_size);
496 ret = ff_mpeg4_decode_picture_header(s, gb);
498 avcodec_set_dimensions(avctx, s->width, s->height);
500 s1->pict_type= s->pict_type;
501 pc->first_picture = 0;
505 static int mpeg4video_parse_init(AVCodecParserContext *s)
507 ParseContext1 *pc = s->priv_data;
509 pc->enc = av_mallocz(sizeof(MpegEncContext));
512 pc->first_picture = 1;
516 static int mpeg4video_parse(AVCodecParserContext *s,
517 AVCodecContext *avctx,
518 uint8_t **poutbuf, int *poutbuf_size,
519 const uint8_t *buf, int buf_size)
521 ParseContext *pc = s->priv_data;
524 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
527 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
529 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
535 av_mpeg4_decode_header(s, avctx, buf, buf_size);
537 *poutbuf = (uint8_t *)buf;
538 *poutbuf_size = buf_size;
543 #ifdef CONFIG_CAVSVIDEO_PARSER
544 static int cavsvideo_parse(AVCodecParserContext *s,
545 AVCodecContext *avctx,
546 uint8_t **poutbuf, int *poutbuf_size,
547 const uint8_t *buf, int buf_size)
549 ParseContext *pc = s->priv_data;
552 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
555 next= ff_cavs_find_frame_end(pc, buf, buf_size);
557 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
563 *poutbuf = (uint8_t *)buf;
564 *poutbuf_size = buf_size;
567 #endif /* CONFIG_CAVSVIDEO_PARSER */
569 static int mpeg4video_split(AVCodecContext *avctx,
570 const uint8_t *buf, int buf_size)
575 for(i=0; i<buf_size; i++){
576 state= (state<<8) | buf[i];
577 if(state == 0x1B3 || state == 0x1B6)
583 /*************************/
585 #ifdef CONFIG_MPEGAUDIO_PARSER
586 typedef struct MpegAudioParseContext {
587 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
590 int free_format_frame_size;
591 int free_format_next_header;
594 } MpegAudioParseContext;
596 #define MPA_HEADER_SIZE 4
598 /* header + layer + bitrate + freq + lsf/mpeg25 */
599 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
600 #define SAME_HEADER_MASK \
601 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
603 static int mpegaudio_parse_init(AVCodecParserContext *s1)
605 MpegAudioParseContext *s = s1->priv_data;
606 s->inbuf_ptr = s->inbuf;
610 static int mpegaudio_parse(AVCodecParserContext *s1,
611 AVCodecContext *avctx,
612 uint8_t **poutbuf, int *poutbuf_size,
613 const uint8_t *buf, int buf_size)
615 MpegAudioParseContext *s = s1->priv_data;
618 const uint8_t *buf_ptr;
623 while (buf_size > 0) {
624 len = s->inbuf_ptr - s->inbuf;
625 if (s->frame_size == 0) {
626 /* special case for next header for first frame in free
627 format case (XXX: find a simpler method) */
628 if (s->free_format_next_header != 0) {
629 s->inbuf[0] = s->free_format_next_header >> 24;
630 s->inbuf[1] = s->free_format_next_header >> 16;
631 s->inbuf[2] = s->free_format_next_header >> 8;
632 s->inbuf[3] = s->free_format_next_header;
633 s->inbuf_ptr = s->inbuf + 4;
634 s->free_format_next_header = 0;
637 /* no header seen : find one. We need at least MPA_HEADER_SIZE
639 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
641 memcpy(s->inbuf_ptr, buf_ptr, len);
646 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
648 sr= avctx->sample_rate;
649 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
650 (s->inbuf[2] << 8) | s->inbuf[3];
652 ret = mpa_decode_header(avctx, header);
655 /* no sync found : move by one byte (inefficient, but simple!) */
656 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
658 dprintf("skip %x\n", header);
659 /* reset free format frame size to give a chance
660 to get a new bitrate */
661 s->free_format_frame_size = 0;
663 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
670 /* free format: prepare to compute frame size */
671 if (decode_header(s, header) == 1) {
676 if(s->header_count <= 0)
677 avctx->sample_rate= sr; //FIXME ugly
681 if (s->frame_size == -1) {
682 /* free format : find next sync to compute frame size */
683 len = MPA_MAX_CODED_FRAME_SIZE - len;
687 /* frame too long: resync */
689 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
696 memcpy(s->inbuf_ptr, buf_ptr, len);
697 /* check for header */
698 p = s->inbuf_ptr - 3;
699 pend = s->inbuf_ptr + len - 4;
701 header = (p[0] << 24) | (p[1] << 16) |
703 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
704 (s->inbuf[2] << 8) | s->inbuf[3];
705 /* check with high probability that we have a
707 if ((header & SAME_HEADER_MASK) ==
708 (header1 & SAME_HEADER_MASK)) {
709 /* header found: update pointers */
710 len = (p + 4) - s->inbuf_ptr;
714 /* compute frame size */
715 s->free_format_next_header = header;
716 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
717 padding = (header1 >> 9) & 1;
719 s->free_format_frame_size -= padding * 4;
721 s->free_format_frame_size -= padding;
722 dprintf("free frame size=%d padding=%d\n",
723 s->free_format_frame_size, padding);
724 decode_header(s, header1);
729 /* not found: simply increase pointers */
736 if (len < s->frame_size) {
737 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
738 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
739 len = FFMIN(s->frame_size - len, buf_size);
740 memcpy(s->inbuf_ptr, buf_ptr, len);
746 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
747 && buf_size + buf_ptr - buf >= s->frame_size){
748 if(s->header_count > 0){
750 *poutbuf_size = s->frame_size;
752 buf_ptr = buf + s->frame_size;
753 s->inbuf_ptr = s->inbuf;
759 if (s->frame_size > 0 &&
760 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
761 if(s->header_count > 0){
763 *poutbuf_size = s->inbuf_ptr - s->inbuf;
765 s->inbuf_ptr = s->inbuf;
770 return buf_ptr - buf;
772 #endif /* CONFIG_MPEGAUDIO_PARSER */
774 #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
775 /* also used for ADTS AAC */
776 typedef struct AC3ParseContext {
780 int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
781 int *bit_rate, int *samples);
782 uint8_t inbuf[8192]; /* input buffer */
785 #define AC3_HEADER_SIZE 7
786 #define AAC_HEADER_SIZE 7
788 #ifdef CONFIG_AC3_PARSER
789 static const int ac3_sample_rates[4] = {
790 48000, 44100, 32000, 0
793 static const int ac3_frame_sizes[64][3] = {
826 { 1024, 1114, 1536 },
827 { 1024, 1115, 1536 },
828 { 1152, 1253, 1728 },
829 { 1152, 1254, 1728 },
830 { 1280, 1393, 1920 },
831 { 1280, 1394, 1920 },
834 static const int ac3_bitrates[64] = {
835 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
836 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
837 384, 448, 448, 512, 512, 576, 576, 640, 640,
840 static const int ac3_channels[8] = {
841 2, 1, 2, 3, 3, 4, 4, 5
843 #endif /* CONFIG_AC3_PARSER */
845 #ifdef CONFIG_AAC_PARSER
846 static const int aac_sample_rates[16] = {
847 96000, 88200, 64000, 48000, 44100, 32000,
848 24000, 22050, 16000, 12000, 11025, 8000, 7350
851 static const int aac_channels[8] = {
852 0, 1, 2, 3, 4, 5, 6, 8
856 #ifdef CONFIG_AC3_PARSER
857 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
858 int *bit_rate, int *samples)
860 unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
863 init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
865 if(get_bits(&bits, 16) != 0x0b77)
868 skip_bits(&bits, 16); /* crc */
869 fscod = get_bits(&bits, 2);
870 frmsizecod = get_bits(&bits, 6);
872 if(!ac3_sample_rates[fscod])
875 bsid = get_bits(&bits, 5);
878 skip_bits(&bits, 3); /* bsmod */
879 acmod = get_bits(&bits, 3);
880 if(acmod & 1 && acmod != 1)
881 skip_bits(&bits, 2); /* cmixlev */
883 skip_bits(&bits, 2); /* surmixlev */
885 skip_bits(&bits, 2); /* dsurmod */
886 lfeon = get_bits1(&bits);
888 *sample_rate = ac3_sample_rates[fscod];
889 *bit_rate = ac3_bitrates[frmsizecod] * 1000;
890 *channels = ac3_channels[acmod] + lfeon;
893 return ac3_frame_sizes[frmsizecod][fscod] * 2;
895 #endif /* CONFIG_AC3_PARSER */
897 #ifdef CONFIG_AAC_PARSER
898 static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
899 int *bit_rate, int *samples)
902 int size, rdb, ch, sr;
904 init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
906 if(get_bits(&bits, 12) != 0xfff)
909 skip_bits1(&bits); /* id */
910 skip_bits(&bits, 2); /* layer */
911 skip_bits1(&bits); /* protection_absent */
912 skip_bits(&bits, 2); /* profile_objecttype */
913 sr = get_bits(&bits, 4); /* sample_frequency_index */
914 if(!aac_sample_rates[sr])
916 skip_bits1(&bits); /* private_bit */
917 ch = get_bits(&bits, 3); /* channel_configuration */
918 if(!aac_channels[ch])
920 skip_bits1(&bits); /* original/copy */
921 skip_bits1(&bits); /* home */
923 /* adts_variable_header */
924 skip_bits1(&bits); /* copyright_identification_bit */
925 skip_bits1(&bits); /* copyright_identification_start */
926 size = get_bits(&bits, 13); /* aac_frame_length */
927 skip_bits(&bits, 11); /* adts_buffer_fullness */
928 rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
930 *channels = aac_channels[ch];
931 *sample_rate = aac_sample_rates[sr];
932 *samples = (rdb + 1) * 1024;
933 *bit_rate = size * 8 * *sample_rate / *samples;
937 #endif /* CONFIG_AAC_PARSER */
939 #ifdef CONFIG_AC3_PARSER
940 static int ac3_parse_init(AVCodecParserContext *s1)
942 AC3ParseContext *s = s1->priv_data;
943 s->inbuf_ptr = s->inbuf;
944 s->header_size = AC3_HEADER_SIZE;
950 #ifdef CONFIG_AAC_PARSER
951 static int aac_parse_init(AVCodecParserContext *s1)
953 AC3ParseContext *s = s1->priv_data;
954 s->inbuf_ptr = s->inbuf;
955 s->header_size = AAC_HEADER_SIZE;
961 /* also used for ADTS AAC */
962 static int ac3_parse(AVCodecParserContext *s1,
963 AVCodecContext *avctx,
964 uint8_t **poutbuf, int *poutbuf_size,
965 const uint8_t *buf, int buf_size)
967 AC3ParseContext *s = s1->priv_data;
968 const uint8_t *buf_ptr;
969 int len, sample_rate, bit_rate, channels, samples;
975 while (buf_size > 0) {
976 len = s->inbuf_ptr - s->inbuf;
977 if (s->frame_size == 0) {
978 /* no header seen : find one. We need at least s->header_size
980 len = FFMIN(s->header_size - len, buf_size);
982 memcpy(s->inbuf_ptr, buf_ptr, len);
986 if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
987 len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
990 /* no sync found : move by one byte (inefficient, but simple!) */
991 memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
995 /* update codec info */
996 avctx->sample_rate = sample_rate;
997 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
998 if(avctx->codec_id == CODEC_ID_AC3){
999 if(avctx->channels!=1 && avctx->channels!=2){
1000 avctx->channels = channels;
1003 avctx->channels = channels;
1005 avctx->bit_rate = bit_rate;
1006 avctx->frame_size = samples;
1010 len = FFMIN(s->frame_size - len, buf_size);
1012 memcpy(s->inbuf_ptr, buf_ptr, len);
1014 s->inbuf_ptr += len;
1017 if(s->inbuf_ptr - s->inbuf == s->frame_size){
1018 *poutbuf = s->inbuf;
1019 *poutbuf_size = s->frame_size;
1020 s->inbuf_ptr = s->inbuf;
1026 return buf_ptr - buf;
1028 #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
1030 #ifdef CONFIG_MPEGVIDEO_PARSER
1031 AVCodecParser mpegvideo_parser = {
1032 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
1033 sizeof(ParseContext1),
1040 #ifdef CONFIG_MPEG4VIDEO_PARSER
1041 AVCodecParser mpeg4video_parser = {
1043 sizeof(ParseContext1),
1044 mpeg4video_parse_init,
1050 #ifdef CONFIG_CAVSVIDEO_PARSER
1051 AVCodecParser cavsvideo_parser = {
1053 sizeof(ParseContext1),
1060 #ifdef CONFIG_MPEGAUDIO_PARSER
1061 AVCodecParser mpegaudio_parser = {
1062 { CODEC_ID_MP2, CODEC_ID_MP3 },
1063 sizeof(MpegAudioParseContext),
1064 mpegaudio_parse_init,
1069 #ifdef CONFIG_AC3_PARSER
1070 AVCodecParser ac3_parser = {
1072 sizeof(AC3ParseContext),
1078 #ifdef CONFIG_AAC_PARSER
1079 AVCodecParser aac_parser = {
1081 sizeof(AC3ParseContext),