3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * VC-1 and WMV3 parser
28 #include "libavutil/attributes.h"
38 static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
39 const uint8_t *buf, int buf_size)
41 VC1ParseContext *vpc = s->priv_data;
43 const uint8_t *start, *end, *next;
44 uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
46 vpc->v.s.avctx = avctx;
47 vpc->v.parse_only = 1;
48 vpc->v.first_pic_header_flag = 1;
52 for(start = buf, end = buf + buf_size; next < end; start = next){
56 next = find_next_marker(start + 4, end);
57 size = next - start - 4;
58 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
59 init_get_bits(&gb, buf2, buf2_size * 8);
60 if(size <= 0) continue;
61 switch(AV_RB32(start)){
63 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
65 case VC1_CODE_ENTRYPOINT:
66 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
69 if(vpc->v.profile < PROFILE_ADVANCED)
70 ret = ff_vc1_parse_frame_header (&vpc->v, &gb);
72 ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
77 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
78 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
79 s->pict_type = AV_PICTURE_TYPE_B;
81 s->pict_type = vpc->v.s.pict_type;
83 if (avctx->ticks_per_frame > 1){
84 // process pulldown flags
86 // Pulldown flags are only valid when 'broadcast' has been set.
87 // So ticks_per_frame will be 2
91 }else if (vpc->v.rptfrm){
93 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
97 if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
98 s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
100 s->field_order = AV_FIELD_PROGRESSIVE;
110 * Find the end of the current frame in the bitstream.
111 * @return the position of the first byte of the next frame, or -1
113 static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
118 pic_found= pc->frame_start_found;
123 for(i=0; i<buf_size; i++){
124 state= (state<<8) | buf[i];
125 if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
134 /* EOF considered as end of frame */
137 for(; i<buf_size; i++){
138 state= (state<<8) | buf[i];
139 if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
140 pc->frame_start_found=0;
146 pc->frame_start_found= pic_found;
148 return END_NOT_FOUND;
151 static int vc1_parse(AVCodecParserContext *s,
152 AVCodecContext *avctx,
153 const uint8_t **poutbuf, int *poutbuf_size,
154 const uint8_t *buf, int buf_size)
156 VC1ParseContext *vpc = s->priv_data;
159 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
162 next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
164 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
171 vc1_extract_headers(s, avctx, buf, buf_size);
174 *poutbuf_size = buf_size;
178 static int vc1_split(AVCodecContext *avctx,
179 const uint8_t *buf, int buf_size)
185 for(i=0; i<buf_size; i++){
186 state= (state<<8) | buf[i];
187 if(IS_MARKER(state)){
188 if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
198 static av_cold int vc1_parse_init(AVCodecParserContext *s)
200 VC1ParseContext *vpc = s->priv_data;
201 vpc->v.s.slice_context_count = 1;
202 return ff_vc1_init_common(&vpc->v);
205 AVCodecParser ff_vc1_parser = {
206 .codec_ids = { AV_CODEC_ID_VC1 },
207 .priv_data_size = sizeof(VC1ParseContext),
208 .parser_init = vc1_parse_init,
209 .parser_parse = vc1_parse,
210 .parser_close = ff_parse_close,