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 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 parser->codec_ids[3] == codec_id ||
43 parser->codec_ids[4] == codec_id)
48 s = av_mallocz(sizeof(AVCodecParserContext));
52 s->priv_data = av_mallocz(parser->priv_data_size);
57 if (parser->parser_init) {
58 ret = parser->parser_init(s);
60 av_free(s->priv_data);
69 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
70 can be returned if necessary */
71 int av_parser_parse(AVCodecParserContext *s,
72 AVCodecContext *avctx,
73 uint8_t **poutbuf, int *poutbuf_size,
74 const uint8_t *buf, int buf_size,
75 int64_t pts, int64_t dts)
78 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
81 /* padding is always necessary even if EOF, so we add it here */
82 memset(dummy_buf, 0, sizeof(dummy_buf));
85 /* add a new packet descriptor */
86 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
87 s->cur_frame_start_index = k;
88 s->cur_frame_offset[k] = s->cur_offset;
89 s->cur_frame_pts[k] = pts;
90 s->cur_frame_dts[k] = dts;
92 /* fill first PTS/DTS */
93 if (s->fetch_timestamp){
98 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
102 /* WARNING: the returned index can be negative */
103 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
104 //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);
105 /* update the file pointer */
107 /* fill the data for the current frame */
108 s->frame_offset = s->last_frame_offset;
109 s->pts = s->last_pts;
110 s->dts = s->last_dts;
112 /* offset of the next frame */
113 s->last_frame_offset = s->cur_offset + index;
114 /* find the packet in which the new frame starts. It
115 is tricky because of MPEG video start codes
116 which can begin in one packet and finish in
117 another packet. In the worst case, an MPEG
118 video start code could be in 4 different
120 k = s->cur_frame_start_index;
121 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
122 if (s->last_frame_offset >= s->cur_frame_offset[k])
124 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
127 s->last_pts = s->cur_frame_pts[k];
128 s->last_dts = s->cur_frame_dts[k];
130 /* some parsers tell us the packet size even before seeing the first byte of the next packet,
131 so the next pts/dts is in the next chunk */
132 if(index == buf_size){
133 s->fetch_timestamp=1;
138 s->cur_offset += index;
142 void av_parser_close(AVCodecParserContext *s)
144 if (s->parser->parser_close)
145 s->parser->parser_close(s);
146 av_free(s->priv_data);
150 /*****************************************************/
152 //#define END_NOT_FOUND (-100)
154 #define PICTURE_START_CODE 0x00000100
155 #define SEQ_START_CODE 0x000001b3
156 #define EXT_START_CODE 0x000001b5
157 #define SLICE_MIN_START_CODE 0x00000101
158 #define SLICE_MAX_START_CODE 0x000001af
160 typedef struct ParseContext1{
162 /* XXX/FIXME PC1 vs. PC */
165 int progressive_sequence;
168 /* XXX: suppress that, needed by MPEG4 */
174 * combines the (truncated) bitstream to a complete frame
175 * @returns -1 if no complete frame could be created
177 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
181 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
182 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
186 /* copy overreaded bytes from last frame into buffer */
187 for(; pc->overread>0; pc->overread--){
188 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
191 /* flush remaining if EOF */
192 if(!*buf_size && next == END_NOT_FOUND){
196 pc->last_index= pc->index;
198 /* copy into buffer end return */
199 if(next == END_NOT_FOUND){
200 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
202 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
203 pc->index += *buf_size;
208 pc->overread_index= pc->index + next;
210 /* append to buffer */
212 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
214 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
219 /* store overread bytes */
220 for(;next < 0; next++){
221 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
227 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
228 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
235 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
237 const uint8_t *buf_ptr;
238 unsigned int state=0xFFFFFFFF, v;
242 while (buf_ptr < buf_end) {
244 if (state == 0x000001) {
245 state = ((state << 8) | v) & 0xffffff;
249 state = ((state << 8) | v) & 0xffffff;
257 /* XXX: merge with libavcodec ? */
258 #define MPEG1_FRAME_RATE_BASE 1001
260 static const int frame_rate_tab[16] = {
272 // libmpeg3's "Unofficial economy rates": (10-13)
277 // random, just to avoid segfault !never encode these
282 static void mpegvideo_extract_headers(AVCodecParserContext *s,
283 AVCodecContext *avctx,
284 const uint8_t *buf, int buf_size)
286 ParseContext1 *pc = s->priv_data;
287 const uint8_t *buf_end;
289 int frame_rate_index, ext_type, bytes_left;
290 int frame_rate_ext_n, frame_rate_ext_d;
291 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
292 int horiz_size_ext, vert_size_ext;
295 buf_end = buf + buf_size;
296 while (buf < buf_end) {
297 start_code = find_start_code(&buf, buf_end);
298 bytes_left = buf_end - buf;
300 case PICTURE_START_CODE:
301 if (bytes_left >= 2) {
302 s->pict_type = (buf[1] >> 3) & 7;
306 if (bytes_left >= 4) {
307 pc->width = (buf[0] << 4) | (buf[1] >> 4);
308 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
309 avcodec_set_dimensions(avctx, pc->width, pc->height);
310 frame_rate_index = buf[3] & 0xf;
311 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
312 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
313 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
318 if (bytes_left >= 1) {
319 ext_type = (buf[0] >> 4);
321 case 0x1: /* sequence extension */
322 if (bytes_left >= 6) {
323 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
324 vert_size_ext = (buf[2] >> 5) & 3;
325 frame_rate_ext_n = (buf[5] >> 5) & 3;
326 frame_rate_ext_d = (buf[5] & 0x1f);
327 pc->progressive_sequence = buf[1] & (1 << 3);
328 avctx->has_b_frames= buf[5] >> 7;
330 pc->width |=(horiz_size_ext << 12);
331 pc->height |=( vert_size_ext << 12);
332 avcodec_set_dimensions(avctx, pc->width, pc->height);
333 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
334 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
335 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
336 avctx->sub_id = 2; /* forces MPEG2 */
339 case 0x8: /* picture coding extension */
340 if (bytes_left >= 5) {
341 picture_structure = buf[2]&3;
342 top_field_first = buf[3] & (1 << 7);
343 repeat_first_field = buf[3] & (1 << 1);
344 progressive_frame = buf[4] & (1 << 7);
346 /* check if we must repeat the frame */
347 if (repeat_first_field) {
348 if (pc->progressive_sequence) {
353 } else if (progressive_frame) {
358 /* the packet only represents half a frame
359 XXX,FIXME maybe find a different solution */
360 if(picture_structure != 3)
370 /* we stop parsing when we encounter a slice. It ensures
371 that this function takes a negligible amount of time */
372 if (start_code >= SLICE_MIN_START_CODE &&
373 start_code <= SLICE_MAX_START_CODE)
381 static int mpegvideo_parse(AVCodecParserContext *s,
382 AVCodecContext *avctx,
383 uint8_t **poutbuf, int *poutbuf_size,
384 const uint8_t *buf, int buf_size)
386 ParseContext1 *pc1 = s->priv_data;
387 ParseContext *pc= &pc1->pc;
390 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
392 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
397 /* we have a full frame : we just parse the first few MPEG headers
398 to have the full timing information. The time take by this
399 function should be negligible for uncorrupted streams */
400 mpegvideo_extract_headers(s, avctx, buf, buf_size);
402 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
403 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
406 *poutbuf = (uint8_t *)buf;
407 *poutbuf_size = buf_size;
411 void ff_parse_close(AVCodecParserContext *s)
413 ParseContext *pc = s->priv_data;
418 static void parse1_close(AVCodecParserContext *s)
420 ParseContext1 *pc1 = s->priv_data;
422 av_free(pc1->pc.buffer);
426 /*************************/
429 /* XXX: make it use less memory */
430 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
431 AVCodecContext *avctx,
432 const uint8_t *buf, int buf_size)
434 ParseContext1 *pc = s1->priv_data;
435 MpegEncContext *s = pc->enc;
436 GetBitContext gb1, *gb = &gb1;
440 s->current_picture_ptr = &s->current_picture;
442 if (avctx->extradata_size && pc->first_picture){
443 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
444 ret = ff_mpeg4_decode_picture_header(s, gb);
447 init_get_bits(gb, buf, 8 * buf_size);
448 ret = ff_mpeg4_decode_picture_header(s, gb);
450 avcodec_set_dimensions(avctx, s->width, s->height);
452 pc->first_picture = 0;
456 static int mpeg4video_parse_init(AVCodecParserContext *s)
458 ParseContext1 *pc = s->priv_data;
460 pc->enc = av_mallocz(sizeof(MpegEncContext));
463 pc->first_picture = 1;
467 static int mpeg4video_parse(AVCodecParserContext *s,
468 AVCodecContext *avctx,
469 uint8_t **poutbuf, int *poutbuf_size,
470 const uint8_t *buf, int buf_size)
472 ParseContext *pc = s->priv_data;
475 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
477 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
482 av_mpeg4_decode_header(s, avctx, buf, buf_size);
484 *poutbuf = (uint8_t *)buf;
485 *poutbuf_size = buf_size;
489 /*************************/
491 typedef struct MpegAudioParseContext {
492 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
495 int free_format_frame_size;
496 int free_format_next_header;
497 } MpegAudioParseContext;
499 #define MPA_HEADER_SIZE 4
501 /* header + layer + bitrate + freq + lsf/mpeg25 */
502 #define SAME_HEADER_MASK \
503 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
505 static int mpegaudio_parse_init(AVCodecParserContext *s1)
507 MpegAudioParseContext *s = s1->priv_data;
508 s->inbuf_ptr = s->inbuf;
512 static int mpegaudio_parse(AVCodecParserContext *s1,
513 AVCodecContext *avctx,
514 uint8_t **poutbuf, int *poutbuf_size,
515 const uint8_t *buf, int buf_size)
517 MpegAudioParseContext *s = s1->priv_data;
520 const uint8_t *buf_ptr;
525 while (buf_size > 0) {
526 len = s->inbuf_ptr - s->inbuf;
527 if (s->frame_size == 0) {
528 /* special case for next header for first frame in free
529 format case (XXX: find a simpler method) */
530 if (s->free_format_next_header != 0) {
531 s->inbuf[0] = s->free_format_next_header >> 24;
532 s->inbuf[1] = s->free_format_next_header >> 16;
533 s->inbuf[2] = s->free_format_next_header >> 8;
534 s->inbuf[3] = s->free_format_next_header;
535 s->inbuf_ptr = s->inbuf + 4;
536 s->free_format_next_header = 0;
539 /* no header seen : find one. We need at least MPA_HEADER_SIZE
541 len = MPA_HEADER_SIZE - len;
545 memcpy(s->inbuf_ptr, buf_ptr, len);
550 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
552 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
553 (s->inbuf[2] << 8) | s->inbuf[3];
555 ret = mpa_decode_header(avctx, header);
557 /* no sync found : move by one byte (inefficient, but simple!) */
558 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
560 dprintf("skip %x\n", header);
561 /* reset free format frame size to give a chance
562 to get a new bitrate */
563 s->free_format_frame_size = 0;
567 /* free format: prepare to compute frame size */
568 if (decode_header(s, header) == 1) {
576 if (s->frame_size == -1) {
577 /* free format : find next sync to compute frame size */
578 len = MPA_MAX_CODED_FRAME_SIZE - len;
582 /* frame too long: resync */
584 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
591 memcpy(s->inbuf_ptr, buf_ptr, len);
592 /* check for header */
593 p = s->inbuf_ptr - 3;
594 pend = s->inbuf_ptr + len - 4;
596 header = (p[0] << 24) | (p[1] << 16) |
598 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
599 (s->inbuf[2] << 8) | s->inbuf[3];
600 /* check with high probability that we have a
602 if ((header & SAME_HEADER_MASK) ==
603 (header1 & SAME_HEADER_MASK)) {
604 /* header found: update pointers */
605 len = (p + 4) - s->inbuf_ptr;
609 /* compute frame size */
610 s->free_format_next_header = header;
611 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
612 padding = (header1 >> 9) & 1;
614 s->free_format_frame_size -= padding * 4;
616 s->free_format_frame_size -= padding;
617 dprintf("free frame size=%d padding=%d\n",
618 s->free_format_frame_size, padding);
619 decode_header(s, header1);
624 /* not found: simply increase pointers */
631 if (len < s->frame_size) {
632 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
633 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
634 len = s->frame_size - len;
637 memcpy(s->inbuf_ptr, buf_ptr, len);
643 if (s->frame_size > 0 &&
644 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
646 *poutbuf_size = s->inbuf_ptr - s->inbuf;
647 s->inbuf_ptr = s->inbuf;
652 return buf_ptr - buf;
656 extern int a52_syncinfo (const uint8_t * buf, int * flags,
657 int * sample_rate, int * bit_rate);
659 typedef struct AC3ParseContext {
660 uint8_t inbuf[4096]; /* input buffer */
666 #define AC3_HEADER_SIZE 7
669 static int ac3_parse_init(AVCodecParserContext *s1)
671 AC3ParseContext *s = s1->priv_data;
672 s->inbuf_ptr = s->inbuf;
676 static int ac3_parse(AVCodecParserContext *s1,
677 AVCodecContext *avctx,
678 uint8_t **poutbuf, int *poutbuf_size,
679 const uint8_t *buf, int buf_size)
681 AC3ParseContext *s = s1->priv_data;
682 const uint8_t *buf_ptr;
683 int len, sample_rate, bit_rate;
684 static const int ac3_channels[8] = {
685 2, 1, 2, 3, 3, 4, 4, 5
692 while (buf_size > 0) {
693 len = s->inbuf_ptr - s->inbuf;
694 if (s->frame_size == 0) {
695 /* no header seen : find one. We need at least 7 bytes to parse it */
696 len = AC3_HEADER_SIZE - len;
699 memcpy(s->inbuf_ptr, buf_ptr, len);
703 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
704 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
706 /* no sync found : move by one byte (inefficient, but simple!) */
707 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
711 /* update codec info */
712 avctx->sample_rate = sample_rate;
713 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
714 if(avctx->channels!=1 && avctx->channels!=2){
715 avctx->channels = ac3_channels[s->flags & 7];
716 if (s->flags & A52_LFE)
719 avctx->bit_rate = bit_rate;
720 avctx->frame_size = 6 * 256;
723 } else if (len < s->frame_size) {
724 len = s->frame_size - len;
728 memcpy(s->inbuf_ptr, buf_ptr, len);
734 *poutbuf_size = s->frame_size;
735 s->inbuf_ptr = s->inbuf;
740 return buf_ptr - buf;
744 AVCodecParser mpegvideo_parser = {
745 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
746 sizeof(ParseContext1),
752 AVCodecParser mpeg4video_parser = {
754 sizeof(ParseContext1),
755 mpeg4video_parse_init,
760 AVCodecParser mpegaudio_parser = {
761 { CODEC_ID_MP2, CODEC_ID_MP3 },
762 sizeof(MpegAudioParseContext),
763 mpegaudio_parse_init,
769 AVCodecParser ac3_parser = {
771 sizeof(AC3ParseContext),