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)
46 s = av_mallocz(sizeof(AVCodecParserContext));
50 s->priv_data = av_mallocz(parser->priv_data_size);
55 if (parser->parser_init) {
56 ret = parser->parser_init(s);
58 av_free(s->priv_data);
67 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
68 can be returned if necessary */
69 int av_parser_parse(AVCodecParserContext *s,
70 AVCodecContext *avctx,
71 uint8_t **poutbuf, int *poutbuf_size,
72 const uint8_t *buf, int buf_size,
73 int64_t pts, int64_t dts)
76 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
79 /* padding is always necessary even if EOF, so we add it here */
80 memset(dummy_buf, 0, sizeof(dummy_buf));
83 /* add a new packet descriptor */
84 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
85 s->cur_frame_start_index = k;
86 s->cur_frame_offset[k] = s->cur_offset;
87 s->cur_frame_pts[k] = pts;
88 s->cur_frame_dts[k] = dts;
90 /* fill first PTS/DTS */
91 if (s->fetch_timestamp){
98 /* WARNING: the returned index can be negative */
99 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
100 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d %d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
101 /* update the file pointer */
103 /* fill the data for the current frame */
104 s->frame_offset = s->last_frame_offset;
105 s->pts = s->last_pts;
106 s->dts = s->last_dts;
108 /* offset of the next frame */
109 s->last_frame_offset = s->cur_offset + index;
110 /* find the packet in which the new frame starts. It
111 is tricky because of MPEG video start codes
112 which can begin in one packet and finish in
113 another packet. In the worst case, an MPEG
114 video start code could be in 4 different
116 k = s->cur_frame_start_index;
117 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
118 if (s->last_frame_offset >= s->cur_frame_offset[k])
120 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
123 s->last_pts = s->cur_frame_pts[k];
124 s->last_dts = s->cur_frame_dts[k];
126 /* some parsers tell us the packet size even before seeing the first byte of the next packet,
127 so the next pts/dts is in the next chunk */
128 if(index == buf_size){
129 s->fetch_timestamp=1;
134 s->cur_offset += index;
138 void av_parser_close(AVCodecParserContext *s)
140 if (s->parser->parser_close)
141 s->parser->parser_close(s);
142 av_free(s->priv_data);
146 /*****************************************************/
148 //#define END_NOT_FOUND (-100)
150 #define PICTURE_START_CODE 0x00000100
151 #define SEQ_START_CODE 0x000001b3
152 #define EXT_START_CODE 0x000001b5
153 #define SLICE_MIN_START_CODE 0x00000101
154 #define SLICE_MAX_START_CODE 0x000001af
156 typedef struct ParseContext1{
158 /* XXX/FIXME PC1 vs. PC */
161 int progressive_sequence;
164 /* XXX: suppress that, needed by MPEG4 */
170 * combines the (truncated) bitstream to a complete frame
171 * @returns -1 if no complete frame could be created
173 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
177 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
178 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
182 /* copy overreaded bytes from last frame into buffer */
183 for(; pc->overread>0; pc->overread--){
184 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
187 pc->last_index= pc->index;
189 /* copy into buffer end return */
190 if(next == END_NOT_FOUND){
191 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
193 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
194 pc->index += *buf_size;
199 pc->overread_index= pc->index + next;
201 /* append to buffer */
203 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
205 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
210 /* store overread bytes */
211 for(;next < 0; next++){
212 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
218 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
219 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
226 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
228 const uint8_t *buf_ptr;
229 unsigned int state=0xFFFFFFFF, v;
233 while (buf_ptr < buf_end) {
235 if (state == 0x000001) {
236 state = ((state << 8) | v) & 0xffffff;
240 state = ((state << 8) | v) & 0xffffff;
248 /* XXX: merge with libavcodec ? */
249 #define MPEG1_FRAME_RATE_BASE 1001
251 static const int frame_rate_tab[16] = {
263 // libmpeg3's "Unofficial economy rates": (10-13)
268 // random, just to avoid segfault !never encode these
273 static void mpegvideo_extract_headers(AVCodecParserContext *s,
274 AVCodecContext *avctx,
275 const uint8_t *buf, int buf_size)
277 ParseContext1 *pc = s->priv_data;
278 const uint8_t *buf_end;
280 int frame_rate_index, ext_type, bytes_left;
281 int frame_rate_ext_n, frame_rate_ext_d;
282 int top_field_first, repeat_first_field, progressive_frame;
283 int horiz_size_ext, vert_size_ext;
286 buf_end = buf + buf_size;
287 while (buf < buf_end) {
288 start_code = find_start_code(&buf, buf_end);
289 bytes_left = buf_end - buf;
291 case PICTURE_START_CODE:
292 if (bytes_left >= 2) {
293 s->pict_type = (buf[1] >> 3) & 7;
297 if (bytes_left >= 4) {
298 pc->width = avctx->width = (buf[0] << 4) | (buf[1] >> 4);
299 pc->height = avctx->height = ((buf[1] & 0x0f) << 8) | buf[2];
300 frame_rate_index = buf[3] & 0xf;
301 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
302 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
303 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
308 if (bytes_left >= 1) {
309 ext_type = (buf[0] >> 4);
311 case 0x1: /* sequence extension */
312 if (bytes_left >= 6) {
313 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
314 vert_size_ext = (buf[2] >> 5) & 3;
315 frame_rate_ext_n = (buf[5] >> 5) & 3;
316 frame_rate_ext_d = (buf[5] & 0x1f);
317 pc->progressive_sequence = buf[1] & (1 << 3);
319 avctx->width = pc->width | (horiz_size_ext << 12);
320 avctx->height = pc->height | (vert_size_ext << 12);
321 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
322 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
323 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
324 avctx->sub_id = 2; /* forces MPEG2 */
327 case 0x8: /* picture coding extension */
328 if (bytes_left >= 5) {
329 top_field_first = buf[3] & (1 << 7);
330 repeat_first_field = buf[3] & (1 << 1);
331 progressive_frame = buf[4] & (1 << 7);
333 /* check if we must repeat the frame */
334 if (repeat_first_field) {
335 if (pc->progressive_sequence) {
340 } else if (progressive_frame) {
352 /* we stop parsing when we encounter a slice. It ensures
353 that this function takes a negligible amount of time */
354 if (start_code >= SLICE_MIN_START_CODE &&
355 start_code <= SLICE_MAX_START_CODE)
363 static int mpegvideo_parse(AVCodecParserContext *s,
364 AVCodecContext *avctx,
365 uint8_t **poutbuf, int *poutbuf_size,
366 const uint8_t *buf, int buf_size)
368 ParseContext1 *pc1 = s->priv_data;
369 ParseContext *pc= &pc1->pc;
372 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
374 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
379 /* we have a full frame : we just parse the first few MPEG headers
380 to have the full timing information. The time take by this
381 function should be negligible for uncorrupted streams */
382 mpegvideo_extract_headers(s, avctx, buf, buf_size);
384 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
385 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
388 *poutbuf = (uint8_t *)buf;
389 *poutbuf_size = buf_size;
393 void ff_parse_close(AVCodecParserContext *s)
395 ParseContext *pc = s->priv_data;
400 static void parse1_close(AVCodecParserContext *s)
402 ParseContext1 *pc1 = s->priv_data;
404 av_free(pc1->pc.buffer);
408 /*************************/
411 /* XXX: make it use less memory */
412 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
413 AVCodecContext *avctx,
414 const uint8_t *buf, int buf_size)
416 ParseContext1 *pc = s1->priv_data;
417 MpegEncContext *s = pc->enc;
418 GetBitContext gb1, *gb = &gb1;
422 s->current_picture_ptr = &s->current_picture;
424 if (avctx->extradata_size && pc->first_picture){
425 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
426 ret = ff_mpeg4_decode_picture_header(s, gb);
429 init_get_bits(gb, buf, 8 * buf_size);
430 ret = ff_mpeg4_decode_picture_header(s, gb);
432 avctx->width = s->width;
433 avctx->height = s->height;
435 pc->first_picture = 0;
439 static int mpeg4video_parse_init(AVCodecParserContext *s)
441 ParseContext1 *pc = s->priv_data;
443 pc->enc = av_mallocz(sizeof(MpegEncContext));
446 pc->first_picture = 1;
450 static int mpeg4video_parse(AVCodecParserContext *s,
451 AVCodecContext *avctx,
452 uint8_t **poutbuf, int *poutbuf_size,
453 const uint8_t *buf, int buf_size)
455 ParseContext *pc = s->priv_data;
458 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
460 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
465 av_mpeg4_decode_header(s, avctx, buf, buf_size);
467 *poutbuf = (uint8_t *)buf;
468 *poutbuf_size = buf_size;
472 /*************************/
474 typedef struct MpegAudioParseContext {
475 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
478 int free_format_frame_size;
479 int free_format_next_header;
480 } MpegAudioParseContext;
482 #define MPA_HEADER_SIZE 4
484 /* header + layer + bitrate + freq + lsf/mpeg25 */
485 #define SAME_HEADER_MASK \
486 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
488 static int mpegaudio_parse_init(AVCodecParserContext *s1)
490 MpegAudioParseContext *s = s1->priv_data;
491 s->inbuf_ptr = s->inbuf;
495 static int mpegaudio_parse(AVCodecParserContext *s1,
496 AVCodecContext *avctx,
497 uint8_t **poutbuf, int *poutbuf_size,
498 const uint8_t *buf, int buf_size)
500 MpegAudioParseContext *s = s1->priv_data;
503 const uint8_t *buf_ptr;
508 while (buf_size > 0) {
509 len = s->inbuf_ptr - s->inbuf;
510 if (s->frame_size == 0) {
511 /* special case for next header for first frame in free
512 format case (XXX: find a simpler method) */
513 if (s->free_format_next_header != 0) {
514 s->inbuf[0] = s->free_format_next_header >> 24;
515 s->inbuf[1] = s->free_format_next_header >> 16;
516 s->inbuf[2] = s->free_format_next_header >> 8;
517 s->inbuf[3] = s->free_format_next_header;
518 s->inbuf_ptr = s->inbuf + 4;
519 s->free_format_next_header = 0;
522 /* no header seen : find one. We need at least MPA_HEADER_SIZE
524 len = MPA_HEADER_SIZE - len;
528 memcpy(s->inbuf_ptr, buf_ptr, len);
533 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
535 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
536 (s->inbuf[2] << 8) | s->inbuf[3];
538 ret = mpa_decode_header(avctx, header);
540 /* no sync found : move by one byte (inefficient, but simple!) */
541 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
543 dprintf("skip %x\n", header);
544 /* reset free format frame size to give a chance
545 to get a new bitrate */
546 s->free_format_frame_size = 0;
550 /* free format: prepare to compute frame size */
551 if (decode_header(s, header) == 1) {
559 if (s->frame_size == -1) {
560 /* free format : find next sync to compute frame size */
561 len = MPA_MAX_CODED_FRAME_SIZE - len;
565 /* frame too long: resync */
567 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
574 memcpy(s->inbuf_ptr, buf_ptr, len);
575 /* check for header */
576 p = s->inbuf_ptr - 3;
577 pend = s->inbuf_ptr + len - 4;
579 header = (p[0] << 24) | (p[1] << 16) |
581 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
582 (s->inbuf[2] << 8) | s->inbuf[3];
583 /* check with high probability that we have a
585 if ((header & SAME_HEADER_MASK) ==
586 (header1 & SAME_HEADER_MASK)) {
587 /* header found: update pointers */
588 len = (p + 4) - s->inbuf_ptr;
592 /* compute frame size */
593 s->free_format_next_header = header;
594 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
595 padding = (header1 >> 9) & 1;
597 s->free_format_frame_size -= padding * 4;
599 s->free_format_frame_size -= padding;
600 dprintf("free frame size=%d padding=%d\n",
601 s->free_format_frame_size, padding);
602 decode_header(s, header1);
607 /* not found: simply increase pointers */
614 if (len < s->frame_size) {
615 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
616 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
617 len = s->frame_size - len;
620 memcpy(s->inbuf_ptr, buf_ptr, len);
626 if (s->frame_size > 0 &&
627 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
629 *poutbuf_size = s->inbuf_ptr - s->inbuf;
630 s->inbuf_ptr = s->inbuf;
635 return buf_ptr - buf;
639 extern int a52_syncinfo (const uint8_t * buf, int * flags,
640 int * sample_rate, int * bit_rate);
642 typedef struct AC3ParseContext {
643 uint8_t inbuf[4096]; /* input buffer */
649 #define AC3_HEADER_SIZE 7
652 static int ac3_parse_init(AVCodecParserContext *s1)
654 AC3ParseContext *s = s1->priv_data;
655 s->inbuf_ptr = s->inbuf;
659 static int ac3_parse(AVCodecParserContext *s1,
660 AVCodecContext *avctx,
661 uint8_t **poutbuf, int *poutbuf_size,
662 const uint8_t *buf, int buf_size)
664 AC3ParseContext *s = s1->priv_data;
665 const uint8_t *buf_ptr;
666 int len, sample_rate, bit_rate;
667 static const int ac3_channels[8] = {
668 2, 1, 2, 3, 3, 4, 4, 5
675 while (buf_size > 0) {
676 len = s->inbuf_ptr - s->inbuf;
677 if (s->frame_size == 0) {
678 /* no header seen : find one. We need at least 7 bytes to parse it */
679 len = AC3_HEADER_SIZE - len;
682 memcpy(s->inbuf_ptr, buf_ptr, len);
686 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
687 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
689 /* no sync found : move by one byte (inefficient, but simple!) */
690 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
694 /* update codec info */
695 avctx->sample_rate = sample_rate;
696 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
697 if(avctx->channels!=1 && avctx->channels!=2){
698 avctx->channels = ac3_channels[s->flags & 7];
699 if (s->flags & A52_LFE)
702 avctx->bit_rate = bit_rate;
703 avctx->frame_size = 6 * 256;
706 } else if (len < s->frame_size) {
707 len = s->frame_size - len;
711 memcpy(s->inbuf_ptr, buf_ptr, len);
717 *poutbuf_size = s->frame_size;
718 s->inbuf_ptr = s->inbuf;
723 return buf_ptr - buf;
727 AVCodecParser mpegvideo_parser = {
728 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
729 sizeof(ParseContext1),
735 AVCodecParser mpeg4video_parser = {
737 sizeof(ParseContext1),
738 mpeg4video_parse_init,
743 AVCodecParser mpegaudio_parser = {
744 { CODEC_ID_MP2, CODEC_ID_MP3 },
745 sizeof(MpegAudioParseContext),
746 mpegaudio_parse_init,
752 AVCodecParser ac3_parser = {
754 sizeof(AC3ParseContext),