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);
329 pc->width |=(horiz_size_ext << 12);
330 pc->height |=( vert_size_ext << 12);
331 avcodec_set_dimensions(avctx, pc->width, pc->height);
332 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
333 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
334 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
335 avctx->sub_id = 2; /* forces MPEG2 */
338 case 0x8: /* picture coding extension */
339 if (bytes_left >= 5) {
340 picture_structure = buf[2]&3;
341 top_field_first = buf[3] & (1 << 7);
342 repeat_first_field = buf[3] & (1 << 1);
343 progressive_frame = buf[4] & (1 << 7);
345 /* check if we must repeat the frame */
346 if (repeat_first_field) {
347 if (pc->progressive_sequence) {
352 } else if (progressive_frame) {
357 /* the packet only represents half a frame
358 XXX,FIXME maybe find a different solution */
359 if(picture_structure != 3)
369 /* we stop parsing when we encounter a slice. It ensures
370 that this function takes a negligible amount of time */
371 if (start_code >= SLICE_MIN_START_CODE &&
372 start_code <= SLICE_MAX_START_CODE)
380 static int mpegvideo_parse(AVCodecParserContext *s,
381 AVCodecContext *avctx,
382 uint8_t **poutbuf, int *poutbuf_size,
383 const uint8_t *buf, int buf_size)
385 ParseContext1 *pc1 = s->priv_data;
386 ParseContext *pc= &pc1->pc;
389 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
391 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
396 /* we have a full frame : we just parse the first few MPEG headers
397 to have the full timing information. The time take by this
398 function should be negligible for uncorrupted streams */
399 mpegvideo_extract_headers(s, avctx, buf, buf_size);
401 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
402 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
405 *poutbuf = (uint8_t *)buf;
406 *poutbuf_size = buf_size;
410 void ff_parse_close(AVCodecParserContext *s)
412 ParseContext *pc = s->priv_data;
417 static void parse1_close(AVCodecParserContext *s)
419 ParseContext1 *pc1 = s->priv_data;
421 av_free(pc1->pc.buffer);
425 /*************************/
428 /* XXX: make it use less memory */
429 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
430 AVCodecContext *avctx,
431 const uint8_t *buf, int buf_size)
433 ParseContext1 *pc = s1->priv_data;
434 MpegEncContext *s = pc->enc;
435 GetBitContext gb1, *gb = &gb1;
439 s->current_picture_ptr = &s->current_picture;
441 if (avctx->extradata_size && pc->first_picture){
442 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
443 ret = ff_mpeg4_decode_picture_header(s, gb);
446 init_get_bits(gb, buf, 8 * buf_size);
447 ret = ff_mpeg4_decode_picture_header(s, gb);
449 avcodec_set_dimensions(avctx, s->width, s->height);
451 pc->first_picture = 0;
455 static int mpeg4video_parse_init(AVCodecParserContext *s)
457 ParseContext1 *pc = s->priv_data;
459 pc->enc = av_mallocz(sizeof(MpegEncContext));
462 pc->first_picture = 1;
466 static int mpeg4video_parse(AVCodecParserContext *s,
467 AVCodecContext *avctx,
468 uint8_t **poutbuf, int *poutbuf_size,
469 const uint8_t *buf, int buf_size)
471 ParseContext *pc = s->priv_data;
474 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
476 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
481 av_mpeg4_decode_header(s, avctx, buf, buf_size);
483 *poutbuf = (uint8_t *)buf;
484 *poutbuf_size = buf_size;
488 /*************************/
490 typedef struct MpegAudioParseContext {
491 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
494 int free_format_frame_size;
495 int free_format_next_header;
496 } MpegAudioParseContext;
498 #define MPA_HEADER_SIZE 4
500 /* header + layer + bitrate + freq + lsf/mpeg25 */
501 #define SAME_HEADER_MASK \
502 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
504 static int mpegaudio_parse_init(AVCodecParserContext *s1)
506 MpegAudioParseContext *s = s1->priv_data;
507 s->inbuf_ptr = s->inbuf;
511 static int mpegaudio_parse(AVCodecParserContext *s1,
512 AVCodecContext *avctx,
513 uint8_t **poutbuf, int *poutbuf_size,
514 const uint8_t *buf, int buf_size)
516 MpegAudioParseContext *s = s1->priv_data;
519 const uint8_t *buf_ptr;
524 while (buf_size > 0) {
525 len = s->inbuf_ptr - s->inbuf;
526 if (s->frame_size == 0) {
527 /* special case for next header for first frame in free
528 format case (XXX: find a simpler method) */
529 if (s->free_format_next_header != 0) {
530 s->inbuf[0] = s->free_format_next_header >> 24;
531 s->inbuf[1] = s->free_format_next_header >> 16;
532 s->inbuf[2] = s->free_format_next_header >> 8;
533 s->inbuf[3] = s->free_format_next_header;
534 s->inbuf_ptr = s->inbuf + 4;
535 s->free_format_next_header = 0;
538 /* no header seen : find one. We need at least MPA_HEADER_SIZE
540 len = MPA_HEADER_SIZE - len;
544 memcpy(s->inbuf_ptr, buf_ptr, len);
549 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
551 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
552 (s->inbuf[2] << 8) | s->inbuf[3];
554 ret = mpa_decode_header(avctx, header);
556 /* no sync found : move by one byte (inefficient, but simple!) */
557 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
559 dprintf("skip %x\n", header);
560 /* reset free format frame size to give a chance
561 to get a new bitrate */
562 s->free_format_frame_size = 0;
566 /* free format: prepare to compute frame size */
567 if (decode_header(s, header) == 1) {
575 if (s->frame_size == -1) {
576 /* free format : find next sync to compute frame size */
577 len = MPA_MAX_CODED_FRAME_SIZE - len;
581 /* frame too long: resync */
583 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
590 memcpy(s->inbuf_ptr, buf_ptr, len);
591 /* check for header */
592 p = s->inbuf_ptr - 3;
593 pend = s->inbuf_ptr + len - 4;
595 header = (p[0] << 24) | (p[1] << 16) |
597 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
598 (s->inbuf[2] << 8) | s->inbuf[3];
599 /* check with high probability that we have a
601 if ((header & SAME_HEADER_MASK) ==
602 (header1 & SAME_HEADER_MASK)) {
603 /* header found: update pointers */
604 len = (p + 4) - s->inbuf_ptr;
608 /* compute frame size */
609 s->free_format_next_header = header;
610 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
611 padding = (header1 >> 9) & 1;
613 s->free_format_frame_size -= padding * 4;
615 s->free_format_frame_size -= padding;
616 dprintf("free frame size=%d padding=%d\n",
617 s->free_format_frame_size, padding);
618 decode_header(s, header1);
623 /* not found: simply increase pointers */
630 if (len < s->frame_size) {
631 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
632 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
633 len = s->frame_size - len;
636 memcpy(s->inbuf_ptr, buf_ptr, len);
642 if (s->frame_size > 0 &&
643 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
645 *poutbuf_size = s->inbuf_ptr - s->inbuf;
646 s->inbuf_ptr = s->inbuf;
651 return buf_ptr - buf;
655 extern int a52_syncinfo (const uint8_t * buf, int * flags,
656 int * sample_rate, int * bit_rate);
658 typedef struct AC3ParseContext {
659 uint8_t inbuf[4096]; /* input buffer */
665 #define AC3_HEADER_SIZE 7
668 static int ac3_parse_init(AVCodecParserContext *s1)
670 AC3ParseContext *s = s1->priv_data;
671 s->inbuf_ptr = s->inbuf;
675 static int ac3_parse(AVCodecParserContext *s1,
676 AVCodecContext *avctx,
677 uint8_t **poutbuf, int *poutbuf_size,
678 const uint8_t *buf, int buf_size)
680 AC3ParseContext *s = s1->priv_data;
681 const uint8_t *buf_ptr;
682 int len, sample_rate, bit_rate;
683 static const int ac3_channels[8] = {
684 2, 1, 2, 3, 3, 4, 4, 5
691 while (buf_size > 0) {
692 len = s->inbuf_ptr - s->inbuf;
693 if (s->frame_size == 0) {
694 /* no header seen : find one. We need at least 7 bytes to parse it */
695 len = AC3_HEADER_SIZE - len;
698 memcpy(s->inbuf_ptr, buf_ptr, len);
702 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
703 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
705 /* no sync found : move by one byte (inefficient, but simple!) */
706 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
710 /* update codec info */
711 avctx->sample_rate = sample_rate;
712 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
713 if(avctx->channels!=1 && avctx->channels!=2){
714 avctx->channels = ac3_channels[s->flags & 7];
715 if (s->flags & A52_LFE)
718 avctx->bit_rate = bit_rate;
719 avctx->frame_size = 6 * 256;
722 } else if (len < s->frame_size) {
723 len = s->frame_size - len;
727 memcpy(s->inbuf_ptr, buf_ptr, len);
733 *poutbuf_size = s->frame_size;
734 s->inbuf_ptr = s->inbuf;
739 return buf_ptr - buf;
743 AVCodecParser mpegvideo_parser = {
744 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
745 sizeof(ParseContext1),
751 AVCodecParser mpeg4video_parser = {
753 sizeof(ParseContext1),
754 mpeg4video_parse_init,
759 AVCodecParser mpegaudio_parser = {
760 { CODEC_ID_MP2, CODEC_ID_MP3 },
761 sizeof(MpegAudioParseContext),
762 mpegaudio_parse_init,
768 AVCodecParser ac3_parser = {
770 sizeof(AC3ParseContext),