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
62
64 const uint8_t *buf, int buf_size)
65{
66 /* Parse the header we just finished unescaping */
67 VC1ParseContext *vpc = s->priv_data;
69 int ret;
70 vpc->v.s.avctx = avctx;
71 ret = init_get_bits8(&gb, buf, buf_size);
72 av_assert1(ret >= 0); // buf_size is bounded by UNESCAPED_THRESHOLD
73
74 switch (vpc->prev_start_code) {
75 case VC1_CODE_SEQHDR & 0xFF:
76 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
77 break;
78 case VC1_CODE_ENTRYPOINT & 0xFF:
79 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
80 break;
81 case VC1_CODE_FRAME & 0xFF:
82 if(vpc->v.profile < PROFILE_ADVANCED)
83 ret = ff_vc1_parse_frame_header (&vpc->v, &gb);
84 else
85 ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
86
87 if (ret < 0)
88 break;
89
90 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
91 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
92 s->pict_type = AV_PICTURE_TYPE_B;
93 else
94 s->pict_type = vpc->v.s.pict_type;
95
96 if (vpc->v.broadcast){
97 // process pulldown flags
98 s->repeat_pict = 1;
99 // Pulldown flags are only valid when 'broadcast' has been set.
100 if (vpc->v.rff){
101 // repeat field
102 s->repeat_pict = 2;
103 }else if (vpc->v.rptfrm){
104 // repeat frames
105 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
106 }
107 }else{
108 s->repeat_pict = 0;
109 }
110
111 if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
112 s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
113 else
114 s->field_order = AV_FIELD_PROGRESSIVE;
115
116 break;
117 }
118 s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
120 if (avctx->width && avctx->height) {
121 s->width = avctx->width;
122 s->height = avctx->height;
123 s->coded_width = FFALIGN(avctx->coded_width, 16);
124 s->coded_height = FFALIGN(avctx->coded_height, 16);
125 }
126}
127
129 AVCodecContext *avctx,
130 const uint8_t **poutbuf, int *poutbuf_size,
131 const uint8_t *buf, int buf_size)
132{
133 /* Here we do the searching for frame boundaries and headers at
134 * the same time. Only a minimal amount at the start of each
135 * header is unescaped. */
136 VC1ParseContext *vpc = s->priv_data;
137 int pic_found = vpc->pc.frame_start_found;
138 uint8_t *unesc_buffer = vpc->unesc_buffer;
139 size_t unesc_index = vpc->unesc_index;
140 VC1ParseSearchState search_state = vpc->search_state;
141 int start_code_found = 0;
142 int next = END_NOT_FOUND;
143 int i = vpc->bytes_to_skip;
144
145 if (pic_found && buf_size == 0) {
146 /* EOF considered as end of frame */
147 memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
148 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
149 next = 0;
150 }
151 while (i < buf_size) {
152 uint8_t b;
153 start_code_found = 0;
154 while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
155 b = buf[i++];
156 unesc_buffer[unesc_index++] = b;
157 if (search_state <= ONE_ZERO)
158 search_state = b ? NO_MATCH : search_state + 1;
159 else if (search_state == TWO_ZEROS) {
160 if (b == 1)
161 search_state = ONE;
162 else if (b > 1) {
163 if (b == 3)
164 unesc_index--; // swallow emulation prevention byte
165 search_state = NO_MATCH;
166 }
167 }
168 else { // search_state == ONE
169 // Header unescaping terminates early due to detection of next start code
170 search_state = NO_MATCH;
171 start_code_found = 1;
172 break;
173 }
174 }
175 if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
176 unesc_index >= UNESCAPED_THRESHOLD &&
177 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
178 {
179 // No need to keep scanning the rest of the buffer for
180 // start codes if we know it contains a complete frame and
181 // we've already unescaped all we need of the frame header
182 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
183 unesc_index = 0;
184 break;
185 }
186 if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
187 while (i < buf_size) {
188 if (search_state == NO_MATCH) {
189 i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
190 if (i < buf_size) {
191 search_state = ONE_ZERO;
192 }
193 i++;
194 } else {
195 b = buf[i++];
196 if (search_state == ONE_ZERO)
197 search_state = b ? NO_MATCH : TWO_ZEROS;
198 else if (search_state == TWO_ZEROS) {
199 if (b >= 1)
200 search_state = b == 1 ? ONE : NO_MATCH;
201 }
202 else { // search_state == ONE
203 search_state = NO_MATCH;
204 start_code_found = 1;
205 break;
206 }
207 }
208 }
209 }
210 if (start_code_found) {
211 vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
212
213 vpc->prev_start_code = b;
214 unesc_index = 0;
215
216 if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
217 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
218 pic_found = 1;
219 }
220 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)
221 && b != (VC1_CODE_ENDOFSEQ & 0xFF)) {
222 next = i - 4;
223 pic_found = b == (VC1_CODE_FRAME & 0xFF);
224 break;
225 }
226 }
227 }
228 }
229
230 vpc->pc.frame_start_found = pic_found;
231 vpc->unesc_index = unesc_index;
232 vpc->search_state = search_state;
233
234 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
235 next = buf_size;
236 } else {
237 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
238 vpc->bytes_to_skip = 0;
239 *poutbuf = NULL;
240 *poutbuf_size = 0;
241 return buf_size;
242 }
243 }
244
245 /* If we return with a valid pointer to a combined frame buffer
246 * then on the next call then we'll have been unhelpfully rewound
247 * by up to 4 bytes (depending upon whether the start code
248 * overlapped the input buffer, and if so by how much). We don't
249 * want this: it will either cause spurious second detections of
250 * the start code we've already seen, or cause extra bytes to be
251 * inserted at the start of the unescaped buffer. */
252 vpc->bytes_to_skip = 4;
253 if (next < 0 && next != END_NOT_FOUND)
254 vpc->bytes_to_skip += next;
255
256 *poutbuf = buf;
257 *poutbuf_size = buf_size;
258 return next;
259}
260
262{
263 VC1ParseContext *vpc = s->priv_data;
264 vpc->v.s.slice_context_count = 1;
265 vpc->v.first_pic_header_flag = 1;
266 vpc->v.parse_only = 1;
267 vpc->prev_start_code = 0;
268 vpc->bytes_to_skip = 0;
269 vpc->unesc_index = 0;
270 vpc->search_state = NO_MATCH;
271 ff_vc1dsp_init(&vpc->v.vc1dsp); /* startcode_find_candidate */
272 return 0;
273}
274
277 .priv_data_size = sizeof(VC1ParseContext),
279 .parse = vc1_parse,
281};
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:2651
#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:214
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:215
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
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_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:275
@ 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
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:57
uint8_t prev_start_code
Definition vc1_parser.c:56
uint8_t unesc_buffer[UNESCAPED_LIMIT]
Definition vc1_parser.c:58
VC1Context v
Definition vc1_parser.c:55
VC1ParseSearchState search_state
Definition vc1_parser.c:60
size_t unesc_index
Definition vc1_parser.c:59
ParseContext pc
Definition vc1_parser.c:54
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition vc1.c:837
int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Decode Simple/Main Profiles sequence header.
Definition vc1.c:275
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition vc1.c:617
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition vc1.c:494
@ 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:128
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition vc1_parser.c:63
static av_cold int vc1_parse_init(AVCodecParserContext *s)
Definition vc1_parser.c:261
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
#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.