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
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;
38 if(codec_id == CODEC_ID_NONE)
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)
51 s = av_mallocz(sizeof(AVCodecParserContext));
55 s->priv_data = av_mallocz(parser->priv_data_size);
60 if (parser->parser_init) {
61 ret = parser->parser_init(s);
63 av_free(s->priv_data);
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)
81 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
84 /* padding is always necessary even if EOF, so we add it here */
85 memset(dummy_buf, 0, sizeof(dummy_buf));
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){
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 */
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
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])
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;
141 s->cur_offset += index;
145 void av_parser_close(AVCodecParserContext *s)
147 if (s->parser->parser_close)
148 s->parser->parser_close(s);
149 av_free(s->priv_data);
153 /*****************************************************/
155 //#define END_NOT_FOUND (-100)
157 #define PICTURE_START_CODE 0x00000100
158 #define SEQ_START_CODE 0x000001b3
159 #define EXT_START_CODE 0x000001b5
160 #define SLICE_MIN_START_CODE 0x00000101
161 #define SLICE_MAX_START_CODE 0x000001af
163 typedef struct ParseContext1{
165 /* XXX/FIXME PC1 vs. PC */
168 int progressive_sequence;
171 /* XXX: suppress that, needed by MPEG4 */
177 * combines the (truncated) bitstream to a complete frame
178 * @returns -1 if no complete frame could be created
180 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
184 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
185 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
189 /* copy overreaded bytes from last frame into buffer */
190 for(; pc->overread>0; pc->overread--){
191 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
194 /* flush remaining if EOF */
195 if(!*buf_size && next == END_NOT_FOUND){
199 pc->last_index= pc->index;
201 /* copy into buffer end return */
202 if(next == END_NOT_FOUND){
203 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
205 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
206 pc->index += *buf_size;
211 pc->overread_index= pc->index + next;
213 /* append to buffer */
215 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
217 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
222 /* store overread bytes */
223 for(;next < 0; next++){
224 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
230 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
231 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
238 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
240 const uint8_t *buf_ptr;
241 unsigned int state=0xFFFFFFFF, v;
245 while (buf_ptr < buf_end) {
247 if (state == 0x000001) {
248 state = ((state << 8) | v) & 0xffffff;
252 state = ((state << 8) | v) & 0xffffff;
260 /* XXX: merge with libavcodec ? */
261 #define MPEG1_FRAME_RATE_BASE 1001
263 static const int frame_rate_tab[16] = {
275 // libmpeg3's "Unofficial economy rates": (10-13)
280 // random, just to avoid segfault !never encode these
285 static void mpegvideo_extract_headers(AVCodecParserContext *s,
286 AVCodecContext *avctx,
287 const uint8_t *buf, int buf_size)
289 ParseContext1 *pc = s->priv_data;
290 const uint8_t *buf_end;
292 int frame_rate_index, ext_type, bytes_left;
293 int frame_rate_ext_n, frame_rate_ext_d;
294 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
295 int horiz_size_ext, vert_size_ext;
298 buf_end = buf + buf_size;
299 while (buf < buf_end) {
300 start_code = find_start_code(&buf, buf_end);
301 bytes_left = buf_end - buf;
303 case PICTURE_START_CODE:
304 if (bytes_left >= 2) {
305 s->pict_type = (buf[1] >> 3) & 7;
309 if (bytes_left >= 4) {
310 pc->width = (buf[0] << 4) | (buf[1] >> 4);
311 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
312 avcodec_set_dimensions(avctx, pc->width, pc->height);
313 frame_rate_index = buf[3] & 0xf;
314 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
315 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
316 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
321 if (bytes_left >= 1) {
322 ext_type = (buf[0] >> 4);
324 case 0x1: /* sequence extension */
325 if (bytes_left >= 6) {
326 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
327 vert_size_ext = (buf[2] >> 5) & 3;
328 frame_rate_ext_n = (buf[5] >> 5) & 3;
329 frame_rate_ext_d = (buf[5] & 0x1f);
330 pc->progressive_sequence = buf[1] & (1 << 3);
331 avctx->has_b_frames= buf[5] >> 7;
333 pc->width |=(horiz_size_ext << 12);
334 pc->height |=( vert_size_ext << 12);
335 avcodec_set_dimensions(avctx, pc->width, pc->height);
336 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
337 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
338 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
339 avctx->sub_id = 2; /* forces MPEG2 */
342 case 0x8: /* picture coding extension */
343 if (bytes_left >= 5) {
344 picture_structure = buf[2]&3;
345 top_field_first = buf[3] & (1 << 7);
346 repeat_first_field = buf[3] & (1 << 1);
347 progressive_frame = buf[4] & (1 << 7);
349 /* check if we must repeat the frame */
350 if (repeat_first_field) {
351 if (pc->progressive_sequence) {
356 } else if (progressive_frame) {
361 /* the packet only represents half a frame
362 XXX,FIXME maybe find a different solution */
363 if(picture_structure != 3)
373 /* we stop parsing when we encounter a slice. It ensures
374 that this function takes a negligible amount of time */
375 if (start_code >= SLICE_MIN_START_CODE &&
376 start_code <= SLICE_MAX_START_CODE)
384 static int mpegvideo_parse(AVCodecParserContext *s,
385 AVCodecContext *avctx,
386 uint8_t **poutbuf, int *poutbuf_size,
387 const uint8_t *buf, int buf_size)
389 ParseContext1 *pc1 = s->priv_data;
390 ParseContext *pc= &pc1->pc;
393 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
395 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
400 /* we have a full frame : we just parse the first few MPEG headers
401 to have the full timing information. The time take by this
402 function should be negligible for uncorrupted streams */
403 mpegvideo_extract_headers(s, avctx, buf, buf_size);
405 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
406 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
409 *poutbuf = (uint8_t *)buf;
410 *poutbuf_size = buf_size;
414 void ff_parse_close(AVCodecParserContext *s)
416 ParseContext *pc = s->priv_data;
421 static void parse1_close(AVCodecParserContext *s)
423 ParseContext1 *pc1 = s->priv_data;
425 av_free(pc1->pc.buffer);
429 /*************************/
432 /* XXX: make it use less memory */
433 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
434 AVCodecContext *avctx,
435 const uint8_t *buf, int buf_size)
437 ParseContext1 *pc = s1->priv_data;
438 MpegEncContext *s = pc->enc;
439 GetBitContext gb1, *gb = &gb1;
443 s->current_picture_ptr = &s->current_picture;
445 if (avctx->extradata_size && pc->first_picture){
446 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
447 ret = ff_mpeg4_decode_picture_header(s, gb);
450 init_get_bits(gb, buf, 8 * buf_size);
451 ret = ff_mpeg4_decode_picture_header(s, gb);
453 avcodec_set_dimensions(avctx, s->width, s->height);
455 pc->first_picture = 0;
459 static int mpeg4video_parse_init(AVCodecParserContext *s)
461 ParseContext1 *pc = s->priv_data;
463 pc->enc = av_mallocz(sizeof(MpegEncContext));
466 pc->first_picture = 1;
470 static int mpeg4video_parse(AVCodecParserContext *s,
471 AVCodecContext *avctx,
472 uint8_t **poutbuf, int *poutbuf_size,
473 const uint8_t *buf, int buf_size)
475 ParseContext *pc = s->priv_data;
478 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
480 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
485 av_mpeg4_decode_header(s, avctx, buf, buf_size);
487 *poutbuf = (uint8_t *)buf;
488 *poutbuf_size = buf_size;
492 /*************************/
494 typedef struct MpegAudioParseContext {
495 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
498 int free_format_frame_size;
499 int free_format_next_header;
502 } MpegAudioParseContext;
504 #define MPA_HEADER_SIZE 4
506 /* header + layer + bitrate + freq + lsf/mpeg25 */
507 #define SAME_HEADER_MASK \
508 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
510 static int mpegaudio_parse_init(AVCodecParserContext *s1)
512 MpegAudioParseContext *s = s1->priv_data;
513 s->inbuf_ptr = s->inbuf;
517 static int mpegaudio_parse(AVCodecParserContext *s1,
518 AVCodecContext *avctx,
519 uint8_t **poutbuf, int *poutbuf_size,
520 const uint8_t *buf, int buf_size)
522 MpegAudioParseContext *s = s1->priv_data;
525 const uint8_t *buf_ptr;
530 while (buf_size > 0) {
531 len = s->inbuf_ptr - s->inbuf;
532 if (s->frame_size == 0) {
533 /* special case for next header for first frame in free
534 format case (XXX: find a simpler method) */
535 if (s->free_format_next_header != 0) {
536 s->inbuf[0] = s->free_format_next_header >> 24;
537 s->inbuf[1] = s->free_format_next_header >> 16;
538 s->inbuf[2] = s->free_format_next_header >> 8;
539 s->inbuf[3] = s->free_format_next_header;
540 s->inbuf_ptr = s->inbuf + 4;
541 s->free_format_next_header = 0;
544 /* no header seen : find one. We need at least MPA_HEADER_SIZE
546 len = MPA_HEADER_SIZE - len;
550 memcpy(s->inbuf_ptr, buf_ptr, len);
555 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
557 sr= avctx->sample_rate;
558 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
559 (s->inbuf[2] << 8) | s->inbuf[3];
561 ret = mpa_decode_header(avctx, header);
564 /* no sync found : move by one byte (inefficient, but simple!) */
565 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
567 dprintf("skip %x\n", header);
568 /* reset free format frame size to give a chance
569 to get a new bitrate */
570 s->free_format_frame_size = 0;
572 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
579 /* free format: prepare to compute frame size */
580 if (decode_header(s, header) == 1) {
585 if(s->header_count <= 0)
586 avctx->sample_rate= sr; //FIXME ugly
590 if (s->frame_size == -1) {
591 /* free format : find next sync to compute frame size */
592 len = MPA_MAX_CODED_FRAME_SIZE - len;
596 /* frame too long: resync */
598 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
605 memcpy(s->inbuf_ptr, buf_ptr, len);
606 /* check for header */
607 p = s->inbuf_ptr - 3;
608 pend = s->inbuf_ptr + len - 4;
610 header = (p[0] << 24) | (p[1] << 16) |
612 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
613 (s->inbuf[2] << 8) | s->inbuf[3];
614 /* check with high probability that we have a
616 if ((header & SAME_HEADER_MASK) ==
617 (header1 & SAME_HEADER_MASK)) {
618 /* header found: update pointers */
619 len = (p + 4) - s->inbuf_ptr;
623 /* compute frame size */
624 s->free_format_next_header = header;
625 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
626 padding = (header1 >> 9) & 1;
628 s->free_format_frame_size -= padding * 4;
630 s->free_format_frame_size -= padding;
631 dprintf("free frame size=%d padding=%d\n",
632 s->free_format_frame_size, padding);
633 decode_header(s, header1);
638 /* not found: simply increase pointers */
645 if (len < s->frame_size) {
646 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
647 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
648 len = s->frame_size - len;
651 memcpy(s->inbuf_ptr, buf_ptr, len);
657 if (s->frame_size > 0 &&
658 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
659 if(s->header_count > 0){
661 *poutbuf_size = s->inbuf_ptr - s->inbuf;
663 s->inbuf_ptr = s->inbuf;
668 return buf_ptr - buf;
672 extern int a52_syncinfo (const uint8_t * buf, int * flags,
673 int * sample_rate, int * bit_rate);
675 typedef struct AC3ParseContext {
676 uint8_t inbuf[4096]; /* input buffer */
682 #define AC3_HEADER_SIZE 7
685 static int ac3_parse_init(AVCodecParserContext *s1)
687 AC3ParseContext *s = s1->priv_data;
688 s->inbuf_ptr = s->inbuf;
692 static int ac3_parse(AVCodecParserContext *s1,
693 AVCodecContext *avctx,
694 uint8_t **poutbuf, int *poutbuf_size,
695 const uint8_t *buf, int buf_size)
697 AC3ParseContext *s = s1->priv_data;
698 const uint8_t *buf_ptr;
699 int len, sample_rate, bit_rate;
700 static const int ac3_channels[8] = {
701 2, 1, 2, 3, 3, 4, 4, 5
708 while (buf_size > 0) {
709 len = s->inbuf_ptr - s->inbuf;
710 if (s->frame_size == 0) {
711 /* no header seen : find one. We need at least 7 bytes to parse it */
712 len = AC3_HEADER_SIZE - len;
715 memcpy(s->inbuf_ptr, buf_ptr, len);
719 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
720 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
722 /* no sync found : move by one byte (inefficient, but simple!) */
723 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
727 /* update codec info */
728 avctx->sample_rate = sample_rate;
729 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
730 if(avctx->channels!=1 && avctx->channels!=2){
731 avctx->channels = ac3_channels[s->flags & 7];
732 if (s->flags & A52_LFE)
735 avctx->bit_rate = bit_rate;
736 avctx->frame_size = 6 * 256;
739 } else if (len < s->frame_size) {
740 len = s->frame_size - len;
744 memcpy(s->inbuf_ptr, buf_ptr, len);
750 *poutbuf_size = s->frame_size;
751 s->inbuf_ptr = s->inbuf;
756 return buf_ptr - buf;
760 AVCodecParser mpegvideo_parser = {
761 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
762 sizeof(ParseContext1),
768 AVCodecParser mpeg4video_parser = {
770 sizeof(ParseContext1),
771 mpeg4video_parse_init,
776 AVCodecParser mpegaudio_parser = {
777 { CODEC_ID_MP2, CODEC_ID_MP3 },
778 sizeof(MpegAudioParseContext),
779 mpegaudio_parse_init,
785 AVCodecParser ac3_parser = {
787 sizeof(AC3ParseContext),