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"
29 #include "libavutil/avassert.h"
35 /** The maximum number of bytes of a sequence, entry point or
36 * frame header whose values we pay any attention to */
37 #define UNESCAPED_THRESHOLD 37
39 /** The maximum number of bytes of a sequence, entry point or
40 * frame header which must be valid memory (because they are
41 * used to update the bitstream cache in skip_bits() calls)
43 #define UNESCAPED_LIMIT 144
50 } VC1ParseSearchState
;
52 typedef struct VC1ParseContext
{
55 uint8_t prev_start_code
;
57 uint8_t unesc_buffer
[UNESCAPED_LIMIT
];
59 VC1ParseSearchState search_state
;
62 static void vc1_extract_header(AVCodecParserContext
*s
, AVCodecContext
*avctx
,
63 const uint8_t *buf
, int buf_size
)
65 /* Parse the header we just finished unescaping */
66 VC1ParseContext
*vpc
= s
->priv_data
;
69 vpc
->v
.s
.avctx
= avctx
;
70 ret
= init_get_bits8(&gb
, buf
, buf_size
);
71 av_assert1(ret
>= 0); // buf_size is bounded by UNESCAPED_THRESHOLD
73 switch (vpc
->prev_start_code
) {
74 case VC1_CODE_SEQHDR
& 0xFF:
75 ff_vc1_decode_sequence_header(avctx
, &vpc
->v
, &gb
);
77 case VC1_CODE_ENTRYPOINT
& 0xFF:
78 ff_vc1_decode_entry_point(avctx
, &vpc
->v
, &gb
);
80 case VC1_CODE_FRAME
& 0xFF:
81 if(vpc
->v
.profile
< PROFILE_ADVANCED
)
82 ret
= ff_vc1_parse_frame_header (&vpc
->v
, &gb
);
84 ret
= ff_vc1_parse_frame_header_adv(&vpc
->v
, &gb
);
89 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
90 if (vpc
->v
.s
.pict_type
== AV_PICTURE_TYPE_BI
)
91 s
->pict_type
= AV_PICTURE_TYPE_B
;
93 s
->pict_type
= vpc
->v
.s
.pict_type
;
95 if (vpc
->v
.broadcast
){
96 // process pulldown flags
98 // Pulldown flags are only valid when 'broadcast' has been set.
102 }else if (vpc
->v
.rptfrm
){
104 s
->repeat_pict
= vpc
->v
.rptfrm
* 2 + 1;
110 if (vpc
->v
.broadcast
&& vpc
->v
.interlace
&& !vpc
->v
.psf
)
111 s
->field_order
= vpc
->v
.tff
? AV_FIELD_TT
: AV_FIELD_BB
;
113 s
->field_order
= AV_FIELD_PROGRESSIVE
;
117 s
->format
= vpc
->v
.chromaformat
== 1 ? AV_PIX_FMT_YUV420P
119 if (avctx
->width
&& avctx
->height
) {
120 s
->width
= avctx
->width
;
121 s
->height
= avctx
->height
;
122 s
->coded_width
= FFALIGN(avctx
->coded_width
, 16);
123 s
->coded_height
= FFALIGN(avctx
->coded_height
, 16);
127 static int vc1_parse(AVCodecParserContext
*s
,
128 AVCodecContext
*avctx
,
129 const uint8_t **poutbuf
, int *poutbuf_size
,
130 const uint8_t *buf
, int buf_size
)
132 /* Here we do the searching for frame boundaries and headers at
133 * the same time. Only a minimal amount at the start of each
134 * header is unescaped. */
135 VC1ParseContext
*vpc
= s
->priv_data
;
136 int pic_found
= vpc
->pc
.frame_start_found
;
137 uint8_t *unesc_buffer
= vpc
->unesc_buffer
;
138 size_t unesc_index
= vpc
->unesc_index
;
139 VC1ParseSearchState search_state
= vpc
->search_state
;
140 int start_code_found
= 0;
141 int next
= END_NOT_FOUND
;
142 int i
= vpc
->bytes_to_skip
;
144 if (pic_found
&& buf_size
== 0) {
145 /* EOF considered as end of frame */
146 memset(unesc_buffer
+ unesc_index
, 0, UNESCAPED_THRESHOLD
- unesc_index
);
147 vc1_extract_header(s
, avctx
, unesc_buffer
, unesc_index
);
150 while (i
< buf_size
) {
152 start_code_found
= 0;
153 while (i
< buf_size
&& unesc_index
< UNESCAPED_THRESHOLD
) {
155 unesc_buffer
[unesc_index
++] = b
;
156 if (search_state
<= ONE_ZERO
)
157 search_state
= b
? NO_MATCH
: search_state
+ 1;
158 else if (search_state
== TWO_ZEROS
) {
163 unesc_index
--; // swallow emulation prevention byte
164 search_state
= NO_MATCH
;
167 else { // search_state == ONE
168 // Header unescaping terminates early due to detection of next start code
169 search_state
= NO_MATCH
;
170 start_code_found
= 1;
174 if ((s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
) &&
175 unesc_index
>= UNESCAPED_THRESHOLD
&&
176 vpc
->prev_start_code
== (VC1_CODE_FRAME
& 0xFF))
178 // No need to keep scanning the rest of the buffer for
179 // start codes if we know it contains a complete frame and
180 // we've already unescaped all we need of the frame header
181 vc1_extract_header(s
, avctx
, unesc_buffer
, unesc_index
);
184 if (unesc_index
>= UNESCAPED_THRESHOLD
&& !start_code_found
) {
185 while (i
< buf_size
) {
186 if (search_state
== NO_MATCH
) {
187 i
+= vpc
->v
.vc1dsp
.startcode_find_candidate(buf
+ i
, buf_size
- i
);
189 search_state
= ONE_ZERO
;
194 if (search_state
== ONE_ZERO
)
195 search_state
= b
? NO_MATCH
: TWO_ZEROS
;
196 else if (search_state
== TWO_ZEROS
) {
198 search_state
= b
== 1 ? ONE
: NO_MATCH
;
200 else { // search_state == ONE
201 search_state
= NO_MATCH
;
202 start_code_found
= 1;
208 if (start_code_found
) {
209 vc1_extract_header(s
, avctx
, unesc_buffer
, unesc_index
);
211 vpc
->prev_start_code
= b
;
214 if (!(s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
)) {
215 if (!pic_found
&& (b
== (VC1_CODE_FRAME
& 0xFF) || b
== (VC1_CODE_FIELD
& 0xFF))) {
218 else if (pic_found
&& b
!= (VC1_CODE_FIELD
& 0xFF) && b
!= (VC1_CODE_SLICE
& 0xFF)) {
220 pic_found
= b
== (VC1_CODE_FRAME
& 0xFF);
227 vpc
->pc
.frame_start_found
= pic_found
;
228 vpc
->unesc_index
= unesc_index
;
229 vpc
->search_state
= search_state
;
231 if (s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
) {
234 if (ff_combine_frame(&vpc
->pc
, next
, &buf
, &buf_size
) < 0) {
235 vpc
->bytes_to_skip
= 0;
242 /* If we return with a valid pointer to a combined frame buffer
243 * then on the next call then we'll have been unhelpfully rewound
244 * by up to 4 bytes (depending upon whether the start code
245 * overlapped the input buffer, and if so by how much). We don't
246 * want this: it will either cause spurious second detections of
247 * the start code we've already seen, or cause extra bytes to be
248 * inserted at the start of the unescaped buffer. */
249 vpc
->bytes_to_skip
= 4;
250 if (next
< 0 && next
!= END_NOT_FOUND
)
251 vpc
->bytes_to_skip
+= next
;
254 *poutbuf_size
= buf_size
;
258 static av_cold
int vc1_parse_init(AVCodecParserContext
*s
)
260 VC1ParseContext
*vpc
= s
->priv_data
;
261 vpc
->v
.s
.slice_context_count
= 1;
262 vpc
->v
.first_pic_header_flag
= 1;
263 vpc
->v
.parse_only
= 1;
264 vpc
->prev_start_code
= 0;
265 vpc
->bytes_to_skip
= 0;
266 vpc
->unesc_index
= 0;
267 vpc
->search_state
= NO_MATCH
;
268 ff_vc1dsp_init(&vpc
->v
.vc1dsp
); /* startcode_find_candidate */
272 const AVCodecParser ff_vc1_parser
= {
273 .codec_ids
= { AV_CODEC_ID_VC1
},
274 .priv_data_size
= sizeof(VC1ParseContext
),
275 .parser_init
= vc1_parse_init
,
276 .parser_parse
= vc1_parse
,
277 .parser_close
= ff_parse_close
,