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
86 ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
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 (!vpc->extradata_parsed) {
173 vpc->extradata_parsed = 1;
174 vc1_parse_extradata(s, avctx);
175 }
176
177 if (pic_found && buf_size == 0) {
178 /* EOF considered as end of frame */
179 memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
180 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
181 next = 0;
182 }
183 while (i < buf_size) {
184 uint8_t b;
185 start_code_found = 0;
186 while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
187 b = buf[i++];
188 unesc_buffer[unesc_index++] = b;
189 if (search_state <= ONE_ZERO)
190 search_state = b ? NO_MATCH : search_state + 1;
191 else if (search_state == TWO_ZEROS) {
192 if (b == 1)
193 search_state = ONE;
194 else if (b > 1) {
195 if (b == 3)
196 unesc_index--; // swallow emulation prevention byte
197 search_state = NO_MATCH;
198 }
199 }
200 else { // search_state == ONE
201 // Header unescaping terminates early due to detection of next start code
202 search_state = NO_MATCH;
203 start_code_found = 1;
204 break;
205 }
206 }
207 if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
208 unesc_index >= UNESCAPED_THRESHOLD &&
209 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
210 {
211 // No need to keep scanning the rest of the buffer for
212 // start codes if we know it contains a complete frame and
213 // we've already unescaped all we need of the frame header
214 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
215 unesc_index = 0;
216 break;
217 }
218 if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
219 while (i < buf_size) {
220 if (search_state == NO_MATCH) {
221 i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
222 if (i < buf_size) {
223 search_state = ONE_ZERO;
224 }
225 i++;
226 } else {
227 b = buf[i++];
228 if (search_state == ONE_ZERO)
229 search_state = b ? NO_MATCH : TWO_ZEROS;
230 else if (search_state == TWO_ZEROS) {
231 if (b >= 1)
232 search_state = b == 1 ? ONE : NO_MATCH;
233 }
234 else { // search_state == ONE
235 search_state = NO_MATCH;
236 start_code_found = 1;
237 break;
238 }
239 }
240 }
241 }
242 if (start_code_found) {
243 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
244
245 vpc->prev_start_code = b;
246 unesc_index = 0;
247
248 if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
249 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
250 pic_found = 1;
251 }
252 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)
253 && b != (VC1_CODE_ENDOFSEQ & 0xFF)) {
254 next = i - 4;
255 pic_found = b == (VC1_CODE_FRAME & 0xFF);
256 break;
257 }
258 }
259 }
260 }
261
262 vpc->pc.frame_start_found = pic_found;
263 vpc->unesc_index = unesc_index;
264 vpc->search_state = search_state;
265
266 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
267 next = buf_size;
268 } else {
269 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
270 vpc->bytes_to_skip = 0;
271 *poutbuf = NULL;
272 *poutbuf_size = 0;
273 return buf_size;
274 }
275 }
276
277 /* If we return with a valid pointer to a combined frame buffer
278 * then on the next call then we'll have been unhelpfully rewound
279 * by up to 4 bytes (depending upon whether the start code
280 * overlapped the input buffer, and if so by how much). We don't
281 * want this: it will either cause spurious second detections of
282 * the start code we've already seen, or cause extra bytes to be
283 * inserted at the start of the unescaped buffer. */
284 vpc->bytes_to_skip = 4;
285 if (next < 0 && next != END_NOT_FOUND)
286 vpc->bytes_to_skip += next;
287
288 *poutbuf = buf;
289 *poutbuf_size = buf_size;
290 return next;
291}
292
294{
295 VC1ParseContext *vpc = s->priv_data;
296 /* Report no picture type until a frame header has actually been read.
297 * av_parser_init() leaves pict_type at AV_PICTURE_TYPE_I, and libavformat
298 * marks a packet a key frame from that default; a track whose frames carry
299 * no start codes yields no header to this parser at all. */
300 s->pict_type = AV_PICTURE_TYPE_NONE;
301 vpc->v.s.slice_context_count = 1;
302 vpc->v.first_pic_header_flag = 1;
303 vpc->v.parse_only = 1;
304 vpc->prev_start_code = 0;
305 vpc->extradata_parsed = 0;
306 vpc->bytes_to_skip = 0;
307 vpc->unesc_index = 0;
308 vpc->search_state = NO_MATCH;
309 ff_vc1dsp_init(&vpc->v.vc1dsp); /* startcode_find_candidate */
310 return 0;
311}
312
315 .priv_data_size = sizeof(VC1ParseContext),
317 .parse = vc1_parse,
319};
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
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
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:2666
#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:228
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:229
@ AV_FIELD_PROGRESSIVE
Definition defs.h:227
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
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 ff_vc1dsp_init(VC1DSPContext *dsp)
Definition vc1dsp.c:968
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:313
@ 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:604
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:619
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:84
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:293
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.