FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
28 #include "libavutil/attributes.h"
29 #include "parser.h"
30 #include "vc1.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 
34 /** The maximum number of bytes of a sequence, entry point or
35  * frame header whose values we pay any attention to */
36 #define UNESCAPED_THRESHOLD 37
37 
38 /** The maximum number of bytes of a sequence, entry point or
39  * frame header which must be valid memory (because they are
40  * used to update the bitstream cache in skip_bits() calls)
41  */
42 #define UNESCAPED_LIMIT 144
43 
44 typedef enum {
50 
51 typedef struct VC1ParseContext {
55  size_t bytes_to_skip;
57  size_t unesc_index;
60 
62  const uint8_t *buf, int buf_size)
63 {
64  /* Parse the header we just finished unescaping */
65  VC1ParseContext *vpc = s->priv_data;
66  GetBitContext gb;
67  int ret;
68  vpc->v.s.avctx = avctx;
69  vpc->v.parse_only = 1;
70  init_get_bits(&gb, buf, buf_size * 8);
71  switch (vpc->prev_start_code) {
72  case VC1_CODE_SEQHDR & 0xFF:
73  ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
74  break;
75  case VC1_CODE_ENTRYPOINT & 0xFF:
76  ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
77  break;
78  case VC1_CODE_FRAME & 0xFF:
79  if(vpc->v.profile < PROFILE_ADVANCED)
80  ret = ff_vc1_parse_frame_header (&vpc->v, &gb);
81  else
82  ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
83 
84  if (ret < 0)
85  break;
86 
87  /* keep AV_PICTURE_TYPE_BI internal to VC1 */
88  if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
90  else
91  s->pict_type = vpc->v.s.pict_type;
92 
93  if (avctx->ticks_per_frame > 1){
94  // process pulldown flags
95  s->repeat_pict = 1;
96  // Pulldown flags are only valid when 'broadcast' has been set.
97  // So ticks_per_frame will be 2
98  if (vpc->v.rff){
99  // repeat field
100  s->repeat_pict = 2;
101  }else if (vpc->v.rptfrm){
102  // repeat frames
103  s->repeat_pict = vpc->v.rptfrm * 2 + 1;
104  }
105  }else{
106  s->repeat_pict = 0;
107  }
108 
109  if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
110  s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
111  else
113 
114  break;
115  }
116  if (avctx->framerate.num)
117  avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
118  s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
119  : AV_PIX_FMT_NONE;
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  break;
184  }
185  if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
186  while (i < buf_size) {
187  if (search_state == NO_MATCH) {
188  i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
189  if (i < buf_size) {
190  search_state = ONE_ZERO;
191  }
192  i++;
193  } else {
194  b = buf[i++];
195  if (search_state == ONE_ZERO)
196  search_state = b ? NO_MATCH : TWO_ZEROS;
197  else if (search_state == TWO_ZEROS) {
198  if (b >= 1)
199  search_state = b == 1 ? ONE : NO_MATCH;
200  }
201  else { // search_state == ONE
202  search_state = NO_MATCH;
203  start_code_found = 1;
204  break;
205  }
206  }
207  }
208  }
209  if (start_code_found) {
210  vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
211 
212  vpc->prev_start_code = b;
213  unesc_index = 0;
214 
215  if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
216  if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
217  pic_found = 1;
218  }
219  else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
220  next = i - 4;
221  pic_found = b == (VC1_CODE_FRAME & 0xFF);
222  break;
223  }
224  }
225  }
226  }
227 
228  vpc->pc.frame_start_found = pic_found;
229  vpc->unesc_index = unesc_index;
230  vpc->search_state = search_state;
231 
233  next = buf_size;
234  } else {
235  if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
236  vpc->bytes_to_skip = 0;
237  *poutbuf = NULL;
238  *poutbuf_size = 0;
239  return buf_size;
240  }
241  }
242 
243  /* If we return with a valid pointer to a combined frame buffer
244  * then on the next call then we'll have been unhelpfully rewound
245  * by up to 4 bytes (depending upon whether the start code
246  * overlapped the input buffer, and if so by how much). We don't
247  * want this: it will either cause spurious second detections of
248  * the start code we've already seen, or cause extra bytes to be
249  * inserted at the start of the unescaped buffer. */
250  vpc->bytes_to_skip = 4;
251  if (next < 0 && next != END_NOT_FOUND)
252  vpc->bytes_to_skip += next;
253 
254  *poutbuf = buf;
255  *poutbuf_size = buf_size;
256  return next;
257 }
258 
259 static int vc1_split(AVCodecContext *avctx,
260  const uint8_t *buf, int buf_size)
261 {
262  uint32_t state = -1;
263  int charged = 0;
264  const uint8_t *ptr = buf, *end = buf + buf_size;
265 
266  while (ptr < end) {
267  ptr = avpriv_find_start_code(ptr, end, &state);
268  if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
269  charged = 1;
270  } else if (charged && IS_MARKER(state))
271  return ptr - 4 - buf;
272  }
273 
274  return 0;
275 }
276 
278 {
279  VC1ParseContext *vpc = s->priv_data;
280  vpc->v.s.slice_context_count = 1;
281  vpc->v.first_pic_header_flag = 1;
282  vpc->prev_start_code = 0;
283  vpc->bytes_to_skip = 0;
284  vpc->unesc_index = 0;
285  vpc->search_state = NO_MATCH;
286  return ff_vc1_init_common(&vpc->v);
287 }
288 
290  .codec_ids = { AV_CODEC_ID_VC1 },
291  .priv_data_size = sizeof(VC1ParseContext),
292  .parser_init = vc1_parse_init,
293  .parser_parse = vc1_parse,
294  .parser_close = ff_parse_close,
295  .split = vc1_split,
296 };
size_t unesc_index
Definition: vc1_parser.c:57
#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:42
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3460
BI type.
Definition: avutil.h:280
const char * s
Definition: avisynth_c.h:768
The VC1 Context.
Definition: vc1.h:173
AVCodecParser ff_vc1_parser
Definition: vc1_parser.c:289
static struct @260 state
int broadcast
TFF/RFF present.
Definition: vc1.h:200
enum AVFieldOrder field_order
Definition: avcodec.h:5291
int num
Numerator.
Definition: rational.h:59
const char * b
Definition: vf_curves.c:113
int codec_ids[5]
Definition: avcodec.h:5335
int(* startcode_find_candidate)(const uint8_t *buf, int size)
Search buf from the start for up to size bytes.
Definition: vc1dsp.h:82
int frame_start_found
Definition: parser.h:34
Macro definitions for various function/variable attributes.
uint8_t rff
Definition: vc1.h:311
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1898
uint8_t
#define av_cold
Definition: attributes.h:82
int first_pic_header_flag
Definition: vc1.h:368
av_cold int ff_vc1_init_common(VC1Context *v)
Init VC-1 specific tables and VC1Context members.
Definition: vc1.c:1575
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int interlace
Progressive/interlaced (RPTFTM syntax element)
Definition: vc1.h:201
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags...
Definition: vc1.h:218
bitstream reader API header.
int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Decode Simple/Main Profiles sequence header.
Definition: vc1.c:276
#define FFALIGN(x, a)
Definition: macros.h:48
VC1ParseSearchState
Definition: vc1_parser.c:44
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int psf
Progressive Segmented Frame.
Definition: vc1.h:211
VC1Context v
Definition: vc1_parser.c:53
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:153
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:251
#define IS_MARKER(state)
Definition: dca_parser.c:51
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:61
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:844
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:329
ParseContext pc
Definition: vc1_parser.c:52
#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:36
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:627
size_t bytes_to_skip
Definition: vc1_parser.c:55
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1907
static int vc1_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:259
VC1ParseSearchState search_state
Definition: vc1_parser.c:58
main external API structure.
Definition: avcodec.h:1761
void * buf
Definition: avisynth_c.h:690
uint8_t prev_start_code
Definition: vc1_parser.c:54
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
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
#define END_NOT_FOUND
Definition: parser.h:40
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:209
uint8_t tff
Definition: vc1.h:311
MpegEncContext s
Definition: vc1.h:174
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
Definition: vc1_parser.c:48
struct AVCodecContext * avctx
Definition: mpegvideo.h:95
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal api header.
static av_cold int vc1_parse_init(AVCodecParserContext *s)
Definition: vc1_parser.c:277
Bi-dir predicted.
Definition: avutil.h:276
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5201
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition: vc1.c:509
uint8_t rptfrm
Definition: vc1.h:311
int parse_only
Context is used within parser.
Definition: vc1.h:395
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:5185
uint8_t unesc_buffer[UNESCAPED_LIMIT]
Definition: vc1_parser.c:56
VC1DSPContext vc1dsp
Definition: vc1.h:177