yading@10: /* yading@10: * VC-1 and WMV3 parser yading@10: * Copyright (c) 2006-2007 Konstantin Shishkov yading@10: * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, 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: /** yading@10: * @file yading@10: * VC-1 and WMV3 parser yading@10: */ yading@10: yading@10: #include "parser.h" yading@10: #include "vc1.h" yading@10: #include "get_bits.h" yading@10: yading@10: typedef struct { yading@10: ParseContext pc; yading@10: VC1Context v; yading@10: } VC1ParseContext; yading@10: yading@10: static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: VC1ParseContext *vpc = s->priv_data; yading@10: GetBitContext gb; yading@10: const uint8_t *start, *end, *next; yading@10: uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); yading@10: yading@10: vpc->v.s.avctx = avctx; yading@10: vpc->v.parse_only = 1; yading@10: next = buf; yading@10: s->repeat_pict = 0; yading@10: yading@10: for(start = buf, end = buf + buf_size; next < end; start = next){ yading@10: int buf2_size, size; yading@10: yading@10: next = find_next_marker(start + 4, end); yading@10: size = next - start - 4; yading@10: buf2_size = vc1_unescape_buffer(start + 4, size, buf2); yading@10: init_get_bits(&gb, buf2, buf2_size * 8); yading@10: if(size <= 0) continue; yading@10: switch(AV_RB32(start)){ yading@10: case VC1_CODE_SEQHDR: yading@10: ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb); yading@10: break; yading@10: case VC1_CODE_ENTRYPOINT: yading@10: ff_vc1_decode_entry_point(avctx, &vpc->v, &gb); yading@10: break; yading@10: case VC1_CODE_FRAME: yading@10: if(vpc->v.profile < PROFILE_ADVANCED) yading@10: ff_vc1_parse_frame_header (&vpc->v, &gb); yading@10: else yading@10: ff_vc1_parse_frame_header_adv(&vpc->v, &gb); yading@10: yading@10: /* keep AV_PICTURE_TYPE_BI internal to VC1 */ yading@10: if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI) yading@10: s->pict_type = AV_PICTURE_TYPE_B; yading@10: else yading@10: s->pict_type = vpc->v.s.pict_type; yading@10: yading@10: if (avctx->ticks_per_frame > 1){ yading@10: // process pulldown flags yading@10: s->repeat_pict = 1; yading@10: // Pulldown flags are only valid when 'broadcast' has been set. yading@10: // So ticks_per_frame will be 2 yading@10: if (vpc->v.rff){ yading@10: // repeat field yading@10: s->repeat_pict = 2; yading@10: }else if (vpc->v.rptfrm){ yading@10: // repeat frames yading@10: s->repeat_pict = vpc->v.rptfrm * 2 + 1; yading@10: } yading@10: } yading@10: yading@10: break; yading@10: } yading@10: } yading@10: yading@10: av_free(buf2); yading@10: } yading@10: yading@10: /** yading@10: * Find the end of the current frame in the bitstream. yading@10: * @return the position of the first byte of the next frame, or -1 yading@10: */ yading@10: static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf, yading@10: int buf_size) { yading@10: int pic_found, i; yading@10: uint32_t state; yading@10: yading@10: pic_found= pc->frame_start_found; yading@10: state= pc->state; yading@10: yading@10: i=0; yading@10: if(!pic_found){ yading@10: for(i=0; iframe_start_found=0; yading@10: pc->state=-1; yading@10: return i-3; yading@10: } yading@10: } yading@10: } yading@10: pc->frame_start_found= pic_found; yading@10: pc->state= state; yading@10: return END_NOT_FOUND; yading@10: } yading@10: yading@10: static int vc1_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: VC1ParseContext *vpc = s->priv_data; 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= vc1_find_frame_end(&vpc->pc, buf, buf_size); yading@10: yading@10: if (ff_combine_frame(&vpc->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: vc1_extract_headers(s, avctx, buf, buf_size); yading@10: yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return next; yading@10: } yading@10: yading@10: static int vc1_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 charged=0; yading@10: yading@10: for(i=0; ipriv_data; yading@10: vpc->v.s.slice_context_count = 1; yading@10: return ff_vc1_init_common(&vpc->v); yading@10: } yading@10: yading@10: AVCodecParser ff_vc1_parser = { yading@10: .codec_ids = { AV_CODEC_ID_VC1 }, yading@10: .priv_data_size = sizeof(VC1ParseContext), yading@10: .parser_init = vc1_parse_init, yading@10: .parser_parse = vc1_parse, yading@10: .parser_close = ff_parse_close, yading@10: .split = vc1_split, yading@10: };