yading@10: /* yading@10: * MPEG1 / MPEG2 video parser yading@10: * Copyright (c) 2000,2001 Fabrice Bellard yading@10: * Copyright (c) 2002-2004 Michael Niedermayer yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "parser.h" yading@10: #include "mpeg12.h" yading@10: #include "internal.h" yading@10: yading@10: struct MpvParseContext { yading@10: ParseContext pc; yading@10: AVRational frame_rate; yading@10: int progressive_sequence; yading@10: int width, height; yading@10: }; yading@10: yading@10: yading@10: static void mpegvideo_extract_headers(AVCodecParserContext *s, yading@10: AVCodecContext *avctx, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: struct MpvParseContext *pc = s->priv_data; yading@10: const uint8_t *buf_end = buf + buf_size; yading@10: uint32_t start_code; yading@10: int frame_rate_index, ext_type, bytes_left; yading@10: int frame_rate_ext_n, frame_rate_ext_d; yading@10: int top_field_first, repeat_first_field, progressive_frame; yading@10: int horiz_size_ext, vert_size_ext, bit_rate_ext; yading@10: int did_set_size=0; yading@10: int bit_rate = 0; yading@10: int vbv_delay = 0; yading@10: //FIXME replace the crap with get_bits() yading@10: s->repeat_pict = 0; yading@10: yading@10: while (buf < buf_end) { yading@10: start_code= -1; yading@10: buf= avpriv_find_start_code(buf, buf_end, &start_code); yading@10: bytes_left = buf_end - buf; yading@10: switch(start_code) { yading@10: case PICTURE_START_CODE: yading@10: if (bytes_left >= 2) { yading@10: s->pict_type = (buf[1] >> 3) & 7; yading@10: if (bytes_left >= 4) yading@10: vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3); yading@10: } yading@10: break; yading@10: case SEQ_START_CODE: yading@10: if (bytes_left >= 7) { yading@10: pc->width = (buf[0] << 4) | (buf[1] >> 4); yading@10: pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; yading@10: if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){ yading@10: avcodec_set_dimensions(avctx, pc->width, pc->height); yading@10: did_set_size=1; yading@10: } yading@10: frame_rate_index = buf[3] & 0xf; yading@10: pc->frame_rate.den = avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_index].num; yading@10: pc->frame_rate.num = avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_index].den; yading@10: bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6); yading@10: avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO; yading@10: } yading@10: break; yading@10: case EXT_START_CODE: yading@10: if (bytes_left >= 1) { yading@10: ext_type = (buf[0] >> 4); yading@10: switch(ext_type) { yading@10: case 0x1: /* sequence extension */ yading@10: if (bytes_left >= 6) { yading@10: horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); yading@10: vert_size_ext = (buf[2] >> 5) & 3; yading@10: bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); yading@10: frame_rate_ext_n = (buf[5] >> 5) & 3; yading@10: frame_rate_ext_d = (buf[5] & 0x1f); yading@10: pc->progressive_sequence = buf[1] & (1 << 3); yading@10: avctx->has_b_frames= !(buf[5] >> 7); yading@10: yading@10: pc->width |=(horiz_size_ext << 12); yading@10: pc->height |=( vert_size_ext << 12); yading@10: bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18); yading@10: if(did_set_size) yading@10: avcodec_set_dimensions(avctx, pc->width, pc->height); yading@10: avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1) * 2; yading@10: avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1); yading@10: avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO; yading@10: } yading@10: break; yading@10: case 0x8: /* picture coding extension */ yading@10: if (bytes_left >= 5) { yading@10: top_field_first = buf[3] & (1 << 7); yading@10: repeat_first_field = buf[3] & (1 << 1); yading@10: progressive_frame = buf[4] & (1 << 7); yading@10: yading@10: /* check if we must repeat the frame */ yading@10: s->repeat_pict = 1; yading@10: if (repeat_first_field) { yading@10: if (pc->progressive_sequence) { yading@10: if (top_field_first) yading@10: s->repeat_pict = 5; yading@10: else yading@10: s->repeat_pict = 3; yading@10: } else if (progressive_frame) { yading@10: s->repeat_pict = 2; yading@10: } yading@10: } yading@10: } yading@10: break; yading@10: } yading@10: } yading@10: break; yading@10: case -1: yading@10: goto the_end; yading@10: default: yading@10: /* we stop parsing when we encounter a slice. It ensures yading@10: that this function takes a negligible amount of time */ yading@10: if (start_code >= SLICE_MIN_START_CODE && yading@10: start_code <= SLICE_MAX_START_CODE) yading@10: goto the_end; yading@10: break; yading@10: } yading@10: } yading@10: the_end: ; yading@10: if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) { yading@10: avctx->rc_max_rate = 400*bit_rate; yading@10: } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate && yading@10: (bit_rate != 0x3FFFF || vbv_delay != 0xFFFF)) { yading@10: avctx->bit_rate = 400*bit_rate; yading@10: } yading@10: } yading@10: yading@10: static int mpegvideo_parse(AVCodecParserContext *s, yading@10: AVCodecContext *avctx, yading@10: const uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: struct MpvParseContext *pc1 = s->priv_data; yading@10: ParseContext *pc= &pc1->pc; yading@10: int next; yading@10: yading@10: if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ yading@10: next= buf_size; yading@10: }else{ yading@10: next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s); yading@10: yading@10: if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return buf_size; yading@10: } yading@10: yading@10: } yading@10: /* we have a full frame : we just parse the first few MPEG headers yading@10: to have the full timing information. The time take by this yading@10: function should be negligible for uncorrupted streams */ yading@10: mpegvideo_extract_headers(s, avctx, buf, buf_size); yading@10: av_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", yading@10: s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict); yading@10: yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return next; yading@10: } yading@10: yading@10: static int mpegvideo_split(AVCodecContext *avctx, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: int i; yading@10: uint32_t state= -1; yading@10: int found=0; yading@10: yading@10: for(i=0; i= 0x100) yading@10: return i-3; yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: static int mpegvideo_parse_init(AVCodecParserContext *s) yading@10: { yading@10: s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial yading@10: return 0; yading@10: } yading@10: yading@10: AVCodecParser ff_mpegvideo_parser = { yading@10: .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO }, yading@10: .priv_data_size = sizeof(struct MpvParseContext), yading@10: .parser_init = mpegvideo_parse_init, yading@10: .parser_parse = mpegvideo_parse, yading@10: .parser_close = ff_parse_close, yading@10: .split = mpegvideo_split, yading@10: };