FFmpeg
Loading...
Searching...
No Matches
vc1_parser.c
Go to the documentation of this file.
1/*
2 * VC-1 and WMV3 parser
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * VC-1 and WMV3 parser
26 */
27
29#include "libavutil/avassert.h"
30#include "parser.h"
31#include "parser_internal.h"
32#include "vc1.h"
33#include "get_bits.h"
34#include "vc1dsp.h"
35
36/** The maximum number of bytes of a sequence, entry point or
37 * frame header whose values we pay any attention to */
38#define UNESCAPED_THRESHOLD 37
39
40/** The maximum number of bytes of a sequence, entry point or
41 * frame header which must be valid memory (because they are
42 * used to update the bitstream cache in skip_bits() calls)
43 */
44#define UNESCAPED_LIMIT 144
45
52
63
65 const uint8_t *buf, int buf_size)
66{
67 /* Parse the header we just finished unescaping */
68 VC1ParseContext *vpc = s->priv_data;
70 int ret;
71 vpc->v.s.avctx = avctx;
72 ret = init_get_bits8(&gb, buf, buf_size);
73 av_assert1(ret >= 0); // buf_size is bounded by UNESCAPED_THRESHOLD
74
75 switch (vpc->prev_start_code) {
76 case VC1_CODE_SEQHDR & 0xFF:
77 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
78 break;
79 case VC1_CODE_ENTRYPOINT & 0xFF:
80 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
81 break;
82 case VC1_CODE_FRAME & 0xFF:
83 if(vpc->v.profile < PROFILE_ADVANCED)
84 ret = ff_vc1_parse_frame_header (&vpc->v, &gb);
85 else
87
88 if (ret < 0)
89 break;
90
91 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
92 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
93 s->pict_type = AV_PICTURE_TYPE_B;
94 else
95 s->pict_type = vpc->v.s.pict_type;
96
97 if (vpc->v.broadcast){
98 // process pulldown flags
99 s->repeat_pict = 1;
100 // Pulldown flags are only valid when 'broadcast' has been set.
101 if (vpc->v.rff){
102 // repeat field
103 s->repeat_pict = 2;
104 }else if (vpc->v.rptfrm){
105 // repeat frames
106 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
107 }
108 }else{
109 s->repeat_pict = 0;
110 }
111
112 if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
113 s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
114 else
115 s->field_order = AV_FIELD_PROGRESSIVE;
116
117 break;
118 }
119 s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
121 if (avctx->width && avctx->height) {
122 s->width = avctx->width;
123 s->height = avctx->height;
124 s->coded_width = FFALIGN(avctx->coded_width, 16);
125 s->coded_height = FFALIGN(avctx->coded_height, 16);
126 }
127}
128
129/**
130 * Seed the parse context from extradata, the way the decoder does at init.
131 *
132 * libavformat closes and reopens the parser on every reposition
133 * (ff_read_frame_flush()), so each seek starts from a zeroed VC1Context: profile
134 * reads as simple, and max_coded_width/max_coded_height as zero, until an
135 * in-stream sequence header happens to pass. An entry point reaching a context in
136 * that state is read at the wrong bit offset, because whether hrd_full[] precedes
137 * coded_size_flag is a property of the sequence header, and the size it then
138 * falls back to is the zero pair.
139 *
140 * Unlike the decoder, a parser that learns nothing here still runs: it keeps
141 * reading headers out of the stream as it did before.
142 */
144{
145 VC1ParseContext *vpc = s->priv_data;
146 int seq_initialized, ep_initialized;
147
148 if (!avctx->extradata || avctx->extradata_size < 16)
149 return;
150
151 vpc->v.s.avctx = avctx;
152 ff_vc1_decode_extradata(avctx, &vpc->v, &seq_initialized, &ep_initialized);
153}
154
156 AVCodecContext *avctx,
157 const uint8_t **poutbuf, int *poutbuf_size,
158 const uint8_t *buf, int buf_size)
159{
160 /* Here we do the searching for frame boundaries and headers at
161 * the same time. Only a minimal amount at the start of each
162 * header is unescaped. */
163 VC1ParseContext *vpc = s->priv_data;
164 int pic_found = vpc->pc.frame_start_found;
165 uint8_t *unesc_buffer = vpc->unesc_buffer;
166 size_t unesc_index = vpc->unesc_index;
167 VC1ParseSearchState search_state = vpc->search_state;
168 int start_code_found = 0;
169 int next = END_NOT_FOUND;
170 int i = vpc->bytes_to_skip;
171
172 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
173 /* Each call is a whole frame: scan it from its own start code */
174 unesc_index = 0;
175 search_state = NO_MATCH;
176 vpc->prev_start_code = 0;
177 i = 0;
178 }
179
180 if (!vpc->extradata_parsed) {
181 vpc->extradata_parsed = 1;
182 vc1_parse_extradata(s, avctx);
183 }
184
185 if (pic_found && buf_size == 0) {
186 /* EOF considered as end of frame */
187 memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
188 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
189 next = 0;
190 }
191 while (i < buf_size) {
192 uint8_t b;
193 start_code_found = 0;
194 while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
195 b = buf[i++];
196 unesc_buffer[unesc_index++] = b;
197 if (search_state <= ONE_ZERO)
198 search_state = b ? NO_MATCH : search_state + 1;
199 else if (search_state == TWO_ZEROS) {
200 if (b == 1)
201 search_state = ONE;
202 else if (b > 1) {
203 if (b == 3)
204 unesc_index--; // swallow emulation prevention byte
205 search_state = NO_MATCH;
206 }
207 }
208 else { // search_state == ONE
209 // Header unescaping terminates early due to detection of next start code
210 search_state = NO_MATCH;
211 start_code_found = 1;
212 break;
213 }
214 }
215 if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
216 (unesc_index >= UNESCAPED_THRESHOLD || i == buf_size) &&
217 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
218 {
219 // No need to keep scanning the rest of the buffer for
220 // start codes if we know it contains a complete frame and
221 // we've already unescaped all we need of the frame header,
222 // or the frame ended first, as a skipped frame does
223 memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
224 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
225 unesc_index = 0;
226 break;
227 }
228 if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
229 while (i < buf_size) {
230 if (search_state == NO_MATCH) {
231 i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
232 if (i < buf_size) {
233 search_state = ONE_ZERO;
234 }
235 i++;
236 } else {
237 b = buf[i++];
238 if (search_state == ONE_ZERO)
239 search_state = b ? NO_MATCH : TWO_ZEROS;
240 else if (search_state == TWO_ZEROS) {
241 if (b >= 1)
242 search_state = b == 1 ? ONE : NO_MATCH;
243 }
244 else { // search_state == ONE
245 search_state = NO_MATCH;
246 start_code_found = 1;
247 break;
248 }
249 }
250 }
251 }
252 if (start_code_found) {
253 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
254
255 vpc->prev_start_code = b;
256 unesc_index = 0;
257
258 if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
259 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
260 pic_found = 1;
261 }
262 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)
263 && b != (VC1_CODE_ENDOFSEQ & 0xFF)) {
264 next = i - 4;
265 pic_found = b == (VC1_CODE_FRAME & 0xFF);
266 break;
267 }
268 }
269 }
270 }
271
272 vpc->pc.frame_start_found = pic_found;
273 vpc->unesc_index = unesc_index;
274 vpc->search_state = search_state;
275
276 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
277 next = buf_size;
278 } else {
279 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
280 vpc->bytes_to_skip = 0;
281 *poutbuf = NULL;
282 *poutbuf_size = 0;
283 return buf_size;
284 }
285 }
286
287 /* If we return with a valid pointer to a combined frame buffer
288 * then on the next call then we'll have been unhelpfully rewound
289 * by up to 4 bytes (depending upon whether the start code
290 * overlapped the input buffer, and if so by how much). We don't
291 * want this: it will either cause spurious second detections of
292 * the start code we've already seen, or cause extra bytes to be
293 * inserted at the start of the unescaped buffer. */
294 vpc->bytes_to_skip = 4;
295 if (next < 0 && next != END_NOT_FOUND)
296 vpc->bytes_to_skip += next;
297
298 *poutbuf = buf;
299 *poutbuf_size = buf_size;
300 return next;
301}
302
304{
305 VC1ParseContext *vpc = s->priv_data;
306 /* Report no picture type until a frame header has actually been read.
307 * av_parser_init() leaves pict_type at AV_PICTURE_TYPE_I, and libavformat
308 * marks a packet a key frame from that default; a track whose frames carry
309 * no start codes yields no header to this parser at all. */
310 s->pict_type = AV_PICTURE_TYPE_NONE;
311 vpc->v.s.slice_context_count = 1;
312 vpc->v.first_pic_header_flag = 1;
313 vpc->v.parse_only = 1;
314 vpc->prev_start_code = 0;
315 vpc->extradata_parsed = 0;
316 vpc->bytes_to_skip = 0;
317 vpc->unesc_index = 0;
318 vpc->search_state = NO_MATCH;
319 ff_vc1dsp_init(&vpc->v.vc1dsp); /* startcode_find_candidate */
320 return 0;
321}
322
325 .priv_data_size = sizeof(VC1ParseContext),
327 .parse = vc1_parse,
329};
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2672
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:230
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:231
@ AV_FIELD_PROGRESSIVE
Definition defs.h:229
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
bitstream reader API header.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
@ AV_PICTURE_TYPE_BI
BI type.
Definition avutil.h:284
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
#define b
Definition input.c:43
#define ONE
Definition jrevdct.c:137
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
av_cold void ff_vc1dsp_init(VC1DSPContext *dsp)
Definition vc1dsp.c:922
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
const FFCodecParser ff_vc1_parser
Definition vc1_parser.c:323
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:605
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:620
struct AVCodecContext * avctx
Definition mpegvideo.h:82
int slice_context_count
number of used thread_contexts
Definition mpegvideo.h:114
enum AVPictureType pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition mpegvideo.h:154
int frame_start_found
Definition parser.h:34
The VC1 Context.
Definition vc1.h:176
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags.
Definition vc1.h:220
int first_pic_header_flag
Definition vc1.h:371
uint8_t tff
Definition vc1.h:321
VC1DSPContext vc1dsp
Definition vc1.h:181
int parse_only
Context is used within parser.
Definition vc1.h:403
int psf
Progressive Segmented Frame.
Definition vc1.h:213
int broadcast
TFF/RFF present.
Definition vc1.h:202
int chromaformat
2 bits, 2=4:2:0, only defined
Definition vc1.h:200
MpegEncContext s
Definition vc1.h:177
uint8_t rff
Definition vc1.h:321
int interlace
Progressive/interlaced (RPTFTM syntax element)
Definition vc1.h:203
uint8_t rptfrm
Definition vc1.h:321
int(* startcode_find_candidate)(const uint8_t *buf, int size)
Search buf from the start for up to size bytes.
Definition vc1dsp.h:81
size_t bytes_to_skip
Definition vc1_parser.c:58
uint8_t prev_start_code
Definition vc1_parser.c:56
uint8_t unesc_buffer[UNESCAPED_LIMIT]
Definition vc1_parser.c:59
uint8_t extradata_parsed
Definition vc1_parser.c:57
VC1Context v
Definition vc1_parser.c:55
VC1ParseSearchState search_state
Definition vc1_parser.c:61
size_t unesc_index
Definition vc1_parser.c:60
ParseContext pc
Definition vc1_parser.c:54
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition vc1.c:887
int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Decode Simple/Main Profiles sequence header.
Definition vc1.c:276
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition vc1.c:667
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition vc1.c:495
int ff_vc1_decode_extradata(AVCodecContext *avctx, VC1Context *v, int *seq_initialized, int *ep_initialized)
Read the sequence and entry point headers carried in VC-1 extradata.
Definition vc1.c:555
@ PROFILE_ADVANCED
Definition vc1_common.h:52
@ VC1_CODE_SEQHDR
Definition vc1_common.h:40
@ VC1_CODE_ENDOFSEQ
Definition vc1_common.h:35
@ VC1_CODE_FIELD
Definition vc1_common.h:37
@ VC1_CODE_FRAME
Definition vc1_common.h:38
@ VC1_CODE_SLICE
Definition vc1_common.h:36
@ VC1_CODE_ENTRYPOINT
Definition vc1_common.h:39
#define UNESCAPED_THRESHOLD
The maximum number of bytes of a sequence, entry point or frame header whose values we pay any attent...
Definition vc1_parser.c:38
static int vc1_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition vc1_parser.c:155
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition vc1_parser.c:64
static av_cold int vc1_parse_init(AVCodecParserContext *s)
Definition vc1_parser.c:303
VC1ParseSearchState
Definition vc1_parser.c:46
@ TWO_ZEROS
Definition vc1_parser.c:49
@ NO_MATCH
Definition vc1_parser.c:47
@ ONE_ZERO
Definition vc1_parser.c:48
static void vc1_parse_extradata(AVCodecParserContext *s, AVCodecContext *avctx)
Seed the parse context from extradata, the way the decoder does at init.
Definition vc1_parser.c:143
#define UNESCAPED_LIMIT
The maximum number of bytes of a sequence, entry point or frame header which must be valid memory (be...
Definition vc1_parser.c:44
VC-1 and WMV3 decoder.