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"
28 AVCodecParser *av_first_parser = NULL;
30 void av_register_codec_parser(AVCodecParser *parser)
32 parser->next = av_first_parser;
33 av_first_parser = parser;
36 AVCodecParserContext *av_parser_init(int codec_id)
38 AVCodecParserContext *s;
39 AVCodecParser *parser;
42 if(codec_id == CODEC_ID_NONE)
45 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
46 if (parser->codec_ids[0] == codec_id ||
47 parser->codec_ids[1] == codec_id ||
48 parser->codec_ids[2] == codec_id ||
49 parser->codec_ids[3] == codec_id ||
50 parser->codec_ids[4] == codec_id)
55 s = av_mallocz(sizeof(AVCodecParserContext));
59 s->priv_data = av_mallocz(parser->priv_data_size);
64 if (parser->parser_init) {
65 ret = parser->parser_init(s);
67 av_free(s->priv_data);
73 s->pict_type = FF_I_TYPE;
80 * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output)
81 * @param pts input presentation timestamp
82 * @param dts input decoding timestamp
83 * @param poutbuf will contain a pointer to the first byte of the output frame
84 * @param poutbuf_size will contain the length of the output frame
85 * @return the number of bytes of the input bitstream used
90 * len = av_parser_parse(myparser, AVCodecContext, &data, &size,
97 * decode_frame(data, size);
101 int av_parser_parse(AVCodecParserContext *s,
102 AVCodecContext *avctx,
103 uint8_t **poutbuf, int *poutbuf_size,
104 const uint8_t *buf, int buf_size,
105 int64_t pts, int64_t dts)
108 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
111 /* padding is always necessary even if EOF, so we add it here */
112 memset(dummy_buf, 0, sizeof(dummy_buf));
115 /* add a new packet descriptor */
116 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
117 s->cur_frame_start_index = k;
118 s->cur_frame_offset[k] = s->cur_offset;
119 s->cur_frame_pts[k] = pts;
120 s->cur_frame_dts[k] = dts;
122 /* fill first PTS/DTS */
123 if (s->fetch_timestamp){
124 s->fetch_timestamp=0;
127 s->cur_frame_pts[k] =
128 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
132 /* WARNING: the returned index can be negative */
133 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
134 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%"PRId64", %"PRId64", out:%"PRId64", %"PRId64", in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
135 /* update the file pointer */
137 /* fill the data for the current frame */
138 s->frame_offset = s->last_frame_offset;
139 s->pts = s->last_pts;
140 s->dts = s->last_dts;
142 /* offset of the next frame */
143 s->last_frame_offset = s->cur_offset + index;
144 /* find the packet in which the new frame starts. It
145 is tricky because of MPEG video start codes
146 which can begin in one packet and finish in
147 another packet. In the worst case, an MPEG
148 video start code could be in 4 different
150 k = s->cur_frame_start_index;
151 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
152 if (s->last_frame_offset >= s->cur_frame_offset[k])
154 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
157 s->last_pts = s->cur_frame_pts[k];
158 s->last_dts = s->cur_frame_dts[k];
160 /* some parsers tell us the packet size even before seeing the first byte of the next packet,
161 so the next pts/dts is in the next chunk */
162 if(index == buf_size){
163 s->fetch_timestamp=1;
168 s->cur_offset += index;
174 * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
175 * @deprecated use AVBitstreamFilter
177 int av_parser_change(AVCodecParserContext *s,
178 AVCodecContext *avctx,
179 uint8_t **poutbuf, int *poutbuf_size,
180 const uint8_t *buf, int buf_size, int keyframe){
182 if(s && s->parser->split){
183 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
184 int i= s->parser->split(avctx, buf, buf_size);
190 /* cast to avoid warning about discarding qualifiers */
191 *poutbuf= (uint8_t *) buf;
192 *poutbuf_size= buf_size;
193 if(avctx->extradata){
194 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
195 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
196 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
197 int size= buf_size + avctx->extradata_size;
199 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
201 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
202 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
210 void av_parser_close(AVCodecParserContext *s)
212 if (s->parser->parser_close)
213 s->parser->parser_close(s);
214 av_free(s->priv_data);
218 /*****************************************************/
221 * combines the (truncated) bitstream to a complete frame
222 * @returns -1 if no complete frame could be created
224 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
228 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
229 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
233 /* Copy overread bytes from last frame into buffer. */
234 for(; pc->overread>0; pc->overread--){
235 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
238 /* flush remaining if EOF */
239 if(!*buf_size && next == END_NOT_FOUND){
243 pc->last_index= pc->index;
245 /* copy into buffer end return */
246 if(next == END_NOT_FOUND){
247 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
249 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
250 pc->index += *buf_size;
255 pc->overread_index= pc->index + next;
257 /* append to buffer */
259 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
261 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
266 /* store overread bytes */
267 for(;next < 0; next++){
268 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
274 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
275 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
282 void ff_parse_close(AVCodecParserContext *s)
284 ParseContext *pc = s->priv_data;
289 void ff_parse1_close(AVCodecParserContext *s)
291 ParseContext1 *pc1 = s->priv_data;
293 av_free(pc1->pc.buffer);
297 /*************************/
299 #ifdef CONFIG_MPEG4VIDEO_PARSER
301 /* XXX: make it use less memory */
302 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
303 AVCodecContext *avctx,
304 const uint8_t *buf, int buf_size)
306 ParseContext1 *pc = s1->priv_data;
307 MpegEncContext *s = pc->enc;
308 GetBitContext gb1, *gb = &gb1;
312 s->current_picture_ptr = &s->current_picture;
314 if (avctx->extradata_size && pc->first_picture){
315 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
316 ret = ff_mpeg4_decode_picture_header(s, gb);
319 init_get_bits(gb, buf, 8 * buf_size);
320 ret = ff_mpeg4_decode_picture_header(s, gb);
322 avcodec_set_dimensions(avctx, s->width, s->height);
324 s1->pict_type= s->pict_type;
325 pc->first_picture = 0;
329 static int mpeg4video_parse_init(AVCodecParserContext *s)
331 ParseContext1 *pc = s->priv_data;
333 pc->enc = av_mallocz(sizeof(MpegEncContext));
336 pc->first_picture = 1;
340 static int mpeg4video_parse(AVCodecParserContext *s,
341 AVCodecContext *avctx,
342 uint8_t **poutbuf, int *poutbuf_size,
343 const uint8_t *buf, int buf_size)
345 ParseContext *pc = s->priv_data;
348 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
351 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
353 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
359 av_mpeg4_decode_header(s, avctx, buf, buf_size);
361 *poutbuf = (uint8_t *)buf;
362 *poutbuf_size = buf_size;
367 int ff_mpeg4video_split(AVCodecContext *avctx,
368 const uint8_t *buf, int buf_size)
373 for(i=0; i<buf_size; i++){
374 state= (state<<8) | buf[i];
375 if(state == 0x1B3 || state == 0x1B6)
381 /*************************/
383 #ifdef CONFIG_MPEGAUDIO_PARSER
384 typedef struct MpegAudioParseContext {
385 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
388 int free_format_frame_size;
389 int free_format_next_header;
392 } MpegAudioParseContext;
394 #define MPA_HEADER_SIZE 4
396 /* header + layer + bitrate + freq + lsf/mpeg25 */
397 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
398 #define SAME_HEADER_MASK \
399 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
401 static int mpegaudio_parse_init(AVCodecParserContext *s1)
403 MpegAudioParseContext *s = s1->priv_data;
404 s->inbuf_ptr = s->inbuf;
408 static int mpegaudio_parse(AVCodecParserContext *s1,
409 AVCodecContext *avctx,
410 uint8_t **poutbuf, int *poutbuf_size,
411 const uint8_t *buf, int buf_size)
413 MpegAudioParseContext *s = s1->priv_data;
416 const uint8_t *buf_ptr;
421 while (buf_size > 0) {
422 len = s->inbuf_ptr - s->inbuf;
423 if (s->frame_size == 0) {
424 /* special case for next header for first frame in free
425 format case (XXX: find a simpler method) */
426 if (s->free_format_next_header != 0) {
427 s->inbuf[0] = s->free_format_next_header >> 24;
428 s->inbuf[1] = s->free_format_next_header >> 16;
429 s->inbuf[2] = s->free_format_next_header >> 8;
430 s->inbuf[3] = s->free_format_next_header;
431 s->inbuf_ptr = s->inbuf + 4;
432 s->free_format_next_header = 0;
435 /* no header seen : find one. We need at least MPA_HEADER_SIZE
437 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
439 memcpy(s->inbuf_ptr, buf_ptr, len);
444 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
446 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
447 (s->inbuf[2] << 8) | s->inbuf[3];
449 ret = mpa_decode_header(avctx, header, &sr);
452 /* no sync found : move by one byte (inefficient, but simple!) */
453 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
455 dprintf(avctx, "skip %x\n", header);
456 /* reset free format frame size to give a chance
457 to get a new bitrate */
458 s->free_format_frame_size = 0;
460 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
467 /* free format: prepare to compute frame size */
468 if (decode_header(s, header) == 1) {
473 if(s->header_count > 1)
474 avctx->sample_rate= sr;
478 if (s->frame_size == -1) {
479 /* free format : find next sync to compute frame size */
480 len = MPA_MAX_CODED_FRAME_SIZE - len;
484 /* frame too long: resync */
486 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
493 memcpy(s->inbuf_ptr, buf_ptr, len);
494 /* check for header */
495 p = s->inbuf_ptr - 3;
496 pend = s->inbuf_ptr + len - 4;
498 header = (p[0] << 24) | (p[1] << 16) |
500 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
501 (s->inbuf[2] << 8) | s->inbuf[3];
502 /* check with high probability that we have a
504 if ((header & SAME_HEADER_MASK) ==
505 (header1 & SAME_HEADER_MASK)) {
506 /* header found: update pointers */
507 len = (p + 4) - s->inbuf_ptr;
511 /* compute frame size */
512 s->free_format_next_header = header;
513 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
514 padding = (header1 >> 9) & 1;
516 s->free_format_frame_size -= padding * 4;
518 s->free_format_frame_size -= padding;
519 dprintf(avctx, "free frame size=%d padding=%d\n",
520 s->free_format_frame_size, padding);
521 decode_header(s, header1);
526 /* not found: simply increase pointers */
533 if (len < s->frame_size) {
534 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
535 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
536 len = FFMIN(s->frame_size - len, buf_size);
537 memcpy(s->inbuf_ptr, buf_ptr, len);
543 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
544 && buf_size + buf_ptr - buf >= s->frame_size){
545 if(s->header_count > 0){
547 *poutbuf_size = s->frame_size;
549 buf_ptr = buf + s->frame_size;
550 s->inbuf_ptr = s->inbuf;
556 if (s->frame_size > 0 &&
557 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
558 if(s->header_count > 0){
560 *poutbuf_size = s->inbuf_ptr - s->inbuf;
562 s->inbuf_ptr = s->inbuf;
567 return buf_ptr - buf;
569 #endif /* CONFIG_MPEGAUDIO_PARSER */
571 #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
572 /* also used for ADTS AAC */
573 typedef struct AC3ParseContext {
577 int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
578 int *bit_rate, int *samples);
579 uint8_t inbuf[8192]; /* input buffer */
582 #define AC3_HEADER_SIZE 7
583 #define AAC_HEADER_SIZE 7
585 #ifdef CONFIG_AC3_PARSER
587 static const uint8_t eac3_blocks[4] = {
591 #endif /* CONFIG_AC3_PARSER */
593 #ifdef CONFIG_AAC_PARSER
594 static const int aac_sample_rates[16] = {
595 96000, 88200, 64000, 48000, 44100, 32000,
596 24000, 22050, 16000, 12000, 11025, 8000, 7350
599 static const int aac_channels[8] = {
600 0, 1, 2, 3, 4, 5, 6, 8
604 #ifdef CONFIG_AC3_PARSER
605 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
606 int *bit_rate, int *samples)
609 unsigned int fscod, acmod, bsid, lfeon;
610 unsigned int strmtyp, substreamid, frmsiz, fscod2, numblkscod;
614 err = ff_ac3_parse_header(buf, &hdr);
616 if(err < 0 && err != -2)
620 if(bsid <= 10) { /* Normal AC-3 */
621 *sample_rate = hdr.sample_rate;
622 *bit_rate = hdr.bit_rate;
623 *channels = hdr.channels;
624 *samples = AC3_FRAME_SIZE;
625 return hdr.frame_size;
626 } else if (bsid > 10 && bsid <= 16) { /* Enhanced AC-3 */
627 init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8);
628 strmtyp = get_bits(&bits, 2);
629 substreamid = get_bits(&bits, 3);
631 if (strmtyp != 0 || substreamid != 0)
632 return 0; /* Currently don't support additional streams */
634 frmsiz = get_bits(&bits, 11) + 1;
635 fscod = get_bits(&bits, 2);
637 fscod2 = get_bits(&bits, 2);
643 *sample_rate = ff_ac3_freqs[fscod2] / 2;
645 numblkscod = get_bits(&bits, 2);
647 *sample_rate = ff_ac3_freqs[fscod];
650 acmod = get_bits(&bits, 3);
651 lfeon = get_bits1(&bits);
653 *samples = eac3_blocks[numblkscod] * 256;
654 *bit_rate = frmsiz * (*sample_rate) * 16 / (*samples);
655 *channels = ff_ac3_channels[acmod] + lfeon;
660 /* Unsupported bitstream version */
663 #endif /* CONFIG_AC3_PARSER */
665 #ifdef CONFIG_AAC_PARSER
666 static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
667 int *bit_rate, int *samples)
670 int size, rdb, ch, sr;
672 init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
674 if(get_bits(&bits, 12) != 0xfff)
677 skip_bits1(&bits); /* id */
678 skip_bits(&bits, 2); /* layer */
679 skip_bits1(&bits); /* protection_absent */
680 skip_bits(&bits, 2); /* profile_objecttype */
681 sr = get_bits(&bits, 4); /* sample_frequency_index */
682 if(!aac_sample_rates[sr])
684 skip_bits1(&bits); /* private_bit */
685 ch = get_bits(&bits, 3); /* channel_configuration */
686 if(!aac_channels[ch])
688 skip_bits1(&bits); /* original/copy */
689 skip_bits1(&bits); /* home */
691 /* adts_variable_header */
692 skip_bits1(&bits); /* copyright_identification_bit */
693 skip_bits1(&bits); /* copyright_identification_start */
694 size = get_bits(&bits, 13); /* aac_frame_length */
695 skip_bits(&bits, 11); /* adts_buffer_fullness */
696 rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
698 *channels = aac_channels[ch];
699 *sample_rate = aac_sample_rates[sr];
700 *samples = (rdb + 1) * 1024;
701 *bit_rate = size * 8 * *sample_rate / *samples;
705 #endif /* CONFIG_AAC_PARSER */
707 #ifdef CONFIG_AC3_PARSER
708 static int ac3_parse_init(AVCodecParserContext *s1)
710 AC3ParseContext *s = s1->priv_data;
711 s->inbuf_ptr = s->inbuf;
712 s->header_size = AC3_HEADER_SIZE;
718 #ifdef CONFIG_AAC_PARSER
719 static int aac_parse_init(AVCodecParserContext *s1)
721 AC3ParseContext *s = s1->priv_data;
722 s->inbuf_ptr = s->inbuf;
723 s->header_size = AAC_HEADER_SIZE;
729 /* also used for ADTS AAC */
730 static int ac3_parse(AVCodecParserContext *s1,
731 AVCodecContext *avctx,
732 uint8_t **poutbuf, int *poutbuf_size,
733 const uint8_t *buf, int buf_size)
735 AC3ParseContext *s = s1->priv_data;
736 const uint8_t *buf_ptr;
737 int len, sample_rate, bit_rate, channels, samples;
743 while (buf_size > 0) {
744 len = s->inbuf_ptr - s->inbuf;
745 if (s->frame_size == 0) {
746 /* no header seen : find one. We need at least s->header_size
748 len = FFMIN(s->header_size - len, buf_size);
750 memcpy(s->inbuf_ptr, buf_ptr, len);
754 if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
755 len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
758 /* no sync found : move by one byte (inefficient, but simple!) */
759 memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
763 /* update codec info */
764 avctx->sample_rate = sample_rate;
765 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
766 if(avctx->codec_id == CODEC_ID_AC3){
767 if(avctx->channels!=1 && avctx->channels!=2){
768 avctx->channels = channels;
771 avctx->channels = channels;
773 avctx->bit_rate = bit_rate;
774 avctx->frame_size = samples;
778 len = FFMIN(s->frame_size - len, buf_size);
780 memcpy(s->inbuf_ptr, buf_ptr, len);
785 if(s->inbuf_ptr - s->inbuf == s->frame_size){
787 *poutbuf_size = s->frame_size;
788 s->inbuf_ptr = s->inbuf;
794 return buf_ptr - buf;
796 #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
798 #ifdef CONFIG_MPEG4VIDEO_PARSER
799 AVCodecParser mpeg4video_parser = {
801 sizeof(ParseContext1),
802 mpeg4video_parse_init,
808 #ifdef CONFIG_MPEGAUDIO_PARSER
809 AVCodecParser mpegaudio_parser = {
810 { CODEC_ID_MP2, CODEC_ID_MP3 },
811 sizeof(MpegAudioParseContext),
812 mpegaudio_parse_init,
817 #ifdef CONFIG_AC3_PARSER
818 AVCodecParser ac3_parser = {
820 sizeof(AC3ParseContext),
826 #ifdef CONFIG_AAC_PARSER
827 AVCodecParser aac_parser = {
829 sizeof(AC3ParseContext),