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 Libav.
8 * Libav 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 * Libav 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 Libav; 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"
33 /** The maximum number of bytes of a sequence, entry point or
34 * frame header whose values we pay any attention to */
35 #define UNESCAPED_THRESHOLD 37
37 /** The maximum number of bytes of a sequence, entry point or
38 * frame header which must be valid memory (because they are
39 * used to update the bitstream cache in skip_bits() calls)
41 #define UNESCAPED_LIMIT 144
48 } VC1ParseSearchState;
53 uint8_t prev_start_code;
55 uint8_t unesc_buffer[UNESCAPED_LIMIT];
57 VC1ParseSearchState search_state;
60 static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
61 const uint8_t *buf, int buf_size)
63 /* Parse the header we just finished unescaping */
64 VC1ParseContext *vpc = s->priv_data;
66 vpc->v.s.avctx = avctx;
67 vpc->v.parse_only = 1;
68 init_get_bits(&gb, buf, buf_size * 8);
69 switch (vpc->prev_start_code) {
70 case VC1_CODE_SEQHDR & 0xFF:
71 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
73 case VC1_CODE_ENTRYPOINT & 0xFF:
74 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
76 case VC1_CODE_FRAME & 0xFF:
77 if(vpc->v.profile < PROFILE_ADVANCED)
78 ff_vc1_parse_frame_header (&vpc->v, &gb);
80 ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
82 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
83 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
84 s->pict_type = AV_PICTURE_TYPE_B;
86 s->pict_type = vpc->v.s.pict_type;
88 if (avctx->ticks_per_frame > 1){
89 // process pulldown flags
91 // Pulldown flags are only valid when 'broadcast' has been set.
92 // So ticks_per_frame will be 2
96 }else if (vpc->v.rptfrm){
98 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
104 if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
105 s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
107 s->field_order = AV_FIELD_PROGRESSIVE;
113 static int vc1_parse(AVCodecParserContext *s,
114 AVCodecContext *avctx,
115 const uint8_t **poutbuf, int *poutbuf_size,
116 const uint8_t *buf, int buf_size)
118 /* Here we do the searching for frame boundaries and headers at
119 * the same time. Only a minimal amount at the start of each
120 * header is unescaped. */
121 VC1ParseContext *vpc = s->priv_data;
122 int pic_found = vpc->pc.frame_start_found;
123 uint8_t *unesc_buffer = vpc->unesc_buffer;
124 size_t unesc_index = vpc->unesc_index;
125 VC1ParseSearchState search_state = vpc->search_state;
126 int start_code_found = 0;
127 int next = END_NOT_FOUND;
128 int i = vpc->bytes_to_skip;
130 if (pic_found && buf_size == 0) {
131 /* EOF considered as end of frame */
132 memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
133 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
136 while (i < buf_size) {
138 start_code_found = 0;
139 while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
141 unesc_buffer[unesc_index++] = b;
142 if (search_state <= ONE_ZERO)
143 search_state = b ? NO_MATCH : search_state + 1;
144 else if (search_state == TWO_ZEROS) {
149 unesc_index--; // swallow emulation prevention byte
150 search_state = NO_MATCH;
153 else { // search_state == ONE
154 // Header unescaping terminates early due to detection of next start code
155 search_state = NO_MATCH;
156 start_code_found = 1;
160 if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
161 unesc_index >= UNESCAPED_THRESHOLD &&
162 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
164 // No need to keep scanning the rest of the buffer for
165 // start codes if we know it contains a complete frame and
166 // we've already unescaped all we need of the frame header
167 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
170 if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
171 while (i < buf_size) {
172 if (search_state == NO_MATCH) {
173 i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
175 search_state = ONE_ZERO;
180 if (search_state == ONE_ZERO)
181 search_state = b ? NO_MATCH : TWO_ZEROS;
182 else if (search_state == TWO_ZEROS) {
184 search_state = b == 1 ? ONE : NO_MATCH;
186 else { // search_state == ONE
187 search_state = NO_MATCH;
188 start_code_found = 1;
194 if (start_code_found) {
195 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
197 vpc->prev_start_code = b;
200 if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
201 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
204 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
206 pic_found = b == (VC1_CODE_FRAME & 0xFF);
213 vpc->pc.frame_start_found = pic_found;
214 vpc->unesc_index = unesc_index;
215 vpc->search_state = search_state;
217 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
220 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
221 vpc->bytes_to_skip = 0;
228 /* If we return with a valid pointer to a combined frame buffer
229 * then on the next call then we'll have been unhelpfully rewound
230 * by up to 4 bytes (depending upon whether the start code
231 * overlapped the input buffer, and if so by how much). We don't
232 * want this: it will either cause spurious second detections of
233 * the start code we've already seen, or cause extra bytes to be
234 * inserted at the start of the unescaped buffer. */
235 vpc->bytes_to_skip = 4;
236 if (next < 0 && start_code_found)
237 vpc->bytes_to_skip += next;
240 *poutbuf_size = buf_size;
244 static int vc1_split(AVCodecContext *avctx,
245 const uint8_t *buf, int buf_size)
251 for(i=0; i<buf_size; i++){
252 state= (state<<8) | buf[i];
253 if(IS_MARKER(state)){
254 if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
264 static av_cold int vc1_parse_init(AVCodecParserContext *s)
266 VC1ParseContext *vpc = s->priv_data;
267 vpc->v.s.slice_context_count = 1;
268 vpc->prev_start_code = 0;
269 vpc->bytes_to_skip = 0;
270 vpc->unesc_index = 0;
271 vpc->search_state = NO_MATCH;
272 return ff_vc1_init_common(&vpc->v);
275 AVCodecParser ff_vc1_parser = {
276 .codec_ids = { AV_CODEC_ID_VC1 },
277 .priv_data_size = sizeof(VC1ParseContext),
278 .parser_init = vc1_parse_init,
279 .parser_parse = vc1_parse,
280 .parser_close = ff_parse_close,