FFmpeg
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 "libavutil/avassert.h"
30 #include "parser.h"
31 #include "vc1.h"
32 #include "get_bits.h"
33 #include "vc1dsp.h"
34 
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
38 
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)
42  */
43 #define UNESCAPED_LIMIT 144
44 
45 typedef enum {
51 
52 typedef struct VC1ParseContext {
55  uint8_t prev_start_code;
56  size_t bytes_to_skip;
58  size_t unesc_index;
61 
63  const uint8_t *buf, int buf_size)
64 {
65  /* Parse the header we just finished unescaping */
66  VC1ParseContext *vpc = s->priv_data;
67  GetBitContext gb;
68  int ret;
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
72 
73  switch (vpc->prev_start_code) {
74  case VC1_CODE_SEQHDR & 0xFF:
75  ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
76  break;
77  case VC1_CODE_ENTRYPOINT & 0xFF:
78  ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
79  break;
80  case VC1_CODE_FRAME & 0xFF:
81  if(vpc->v.profile < PROFILE_ADVANCED)
82  ret = ff_vc1_parse_frame_header (&vpc->v, &gb);
83  else
84  ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
85 
86  if (ret < 0)
87  break;
88 
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;
92  else
93  s->pict_type = vpc->v.s.pict_type;
94 
95  if (vpc->v.broadcast){
96  // process pulldown flags
97  s->repeat_pict = 1;
98  // Pulldown flags are only valid when 'broadcast' has been set.
99  if (vpc->v.rff){
100  // repeat field
101  s->repeat_pict = 2;
102  }else if (vpc->v.rptfrm){
103  // repeat frames
104  s->repeat_pict = vpc->v.rptfrm * 2 + 1;
105  }
106  }else{
107  s->repeat_pict = 0;
108  }
109 
110  if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
111  s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
112  else
113  s->field_order = AV_FIELD_PROGRESSIVE;
114 
115  break;
116  }
117  s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
118  : AV_PIX_FMT_NONE;
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);
124  }
125 }
126 
128  AVCodecContext *avctx,
129  const uint8_t **poutbuf, int *poutbuf_size,
130  const uint8_t *buf, int buf_size)
131 {
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;
143 
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);
148  next = 0;
149  }
150  while (i < buf_size) {
151  uint8_t b;
152  start_code_found = 0;
153  while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
154  b = buf[i++];
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) {
159  if (b == 1)
160  search_state = ONE;
161  else if (b > 1) {
162  if (b == 3)
163  unesc_index--; // swallow emulation prevention byte
164  search_state = NO_MATCH;
165  }
166  }
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;
171  break;
172  }
173  }
174  if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
175  unesc_index >= UNESCAPED_THRESHOLD &&
176  vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
177  {
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);
182  break;
183  }
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);
188  if (i < buf_size) {
189  search_state = ONE_ZERO;
190  }
191  i++;
192  } else {
193  b = buf[i++];
194  if (search_state == ONE_ZERO)
195  search_state = b ? NO_MATCH : TWO_ZEROS;
196  else if (search_state == TWO_ZEROS) {
197  if (b >= 1)
198  search_state = b == 1 ? ONE : NO_MATCH;
199  }
200  else { // search_state == ONE
201  search_state = NO_MATCH;
202  start_code_found = 1;
203  break;
204  }
205  }
206  }
207  }
208  if (start_code_found) {
209  vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
210 
211  vpc->prev_start_code = b;
212  unesc_index = 0;
213 
214  if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
215  if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
216  pic_found = 1;
217  }
218  else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
219  next = i - 4;
220  pic_found = b == (VC1_CODE_FRAME & 0xFF);
221  break;
222  }
223  }
224  }
225  }
226 
227  vpc->pc.frame_start_found = pic_found;
228  vpc->unesc_index = unesc_index;
229  vpc->search_state = search_state;
230 
231  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
232  next = buf_size;
233  } else {
234  if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
235  vpc->bytes_to_skip = 0;
236  *poutbuf = NULL;
237  *poutbuf_size = 0;
238  return buf_size;
239  }
240  }
241 
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;
252 
253  *poutbuf = buf;
254  *poutbuf_size = buf_size;
255  return next;
256 }
257 
259 {
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 */
269  return 0;
270 }
271 
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,
278 };
VC1Context
The VC1 Context.
Definition: vc1.h:173
VC1ParseContext::search_state
VC1ParseSearchState search_state
Definition: vc1_parser.c:59
VC1Context::interlace
int interlace
Progressive/interlaced (RPTFTM syntax element)
Definition: vc1.h:199
vc1dsp.h
vc1.h
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:202
VC1ParseContext::bytes_to_skip
size_t bytes_to_skip
Definition: vc1_parser.c:56
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:290
b
#define b
Definition: input.c:41
VC1ParseContext::prev_start_code
uint8_t prev_start_code
Definition: vc1_parser.c:55
VC1ParseSearchState
VC1ParseSearchState
Definition: vc1_parser.c:45
MpegEncContext::avctx
struct AVCodecContext * avctx
Definition: mpegvideo.h:91
NO_MATCH
@ NO_MATCH
Definition: vc1_parser.c:46
VC1Context::rptfrm
uint8_t rptfrm
Definition: vc1.h:311
ff_vc1_parse_frame_header
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:624
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:203
ParseContext
Definition: parser.h:28
MpegEncContext::pict_type
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:206
ff_vc1_parser
const AVCodecParser ff_vc1_parser
Definition: vc1_parser.c:272
GetBitContext
Definition: get_bits.h:108
ONE
@ ONE
Definition: vc1_parser.c:49
VC1Context::first_pic_header_flag
int first_pic_header_flag
Definition: vc1.h:366
ff_vc1_decode_sequence_header
int ff_vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Decode Simple/Main Profiles sequence header.
Definition: vc1.c:274
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:639
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
VC1_CODE_SLICE
@ VC1_CODE_SLICE
Definition: vc1_common.h:36
s
#define s(width, name)
Definition: cbs_vp9.c:198
VC1ParseContext
Definition: vc1_parser.c:52
PROFILE_ADVANCED
@ PROFILE_ADVANCED
Definition: vc1_common.h:52
vc1_parse_init
static av_cold int vc1_parse_init(AVCodecParserContext *s)
Definition: vc1_parser.c:258
get_bits.h
VC1ParseContext::unesc_buffer
uint8_t unesc_buffer[UNESCAPED_LIMIT]
Definition: vc1_parser.c:57
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
NULL
#define NULL
Definition: coverity.c:32
VC1ParseContext::unesc_index
size_t unesc_index
Definition: vc1_parser.c:58
VC1_CODE_SEQHDR
@ VC1_CODE_SEQHDR
Definition: vc1_common.h:40
UNESCAPED_LIMIT
#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:43
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
vc1_parse
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:127
VC1_CODE_FIELD
@ VC1_CODE_FIELD
Definition: vc1_common.h:37
VC1_CODE_ENTRYPOINT
@ VC1_CODE_ENTRYPOINT
Definition: vc1_common.h:39
VC1Context::vc1dsp
VC1DSPContext vc1dsp
Definition: vc1.h:177
MpegEncContext::slice_context_count
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:153
VC1_CODE_FRAME
@ VC1_CODE_FRAME
Definition: vc1_common.h:38
ff_vc1dsp_init
av_cold void ff_vc1dsp_init(VC1DSPContext *dsp)
Definition: vc1dsp.c:974
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2908
VC1Context::parse_only
int parse_only
Context is used within parser.
Definition: vc1.h:398
VC1Context::chromaformat
int chromaformat
2 bits, 2=4:2:0, only defined
Definition: vc1.h:196
ONE_ZERO
@ ONE_ZERO
Definition: vc1_parser.c:47
attributes.h
ff_combine_frame
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:203
VC1DSPContext::startcode_find_candidate
int(* startcode_find_candidate)(const uint8_t *buf, int size)
Search buf from the start for up to size bytes.
Definition: vc1dsp.h:82
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2782
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
TWO_ZEROS
@ TWO_ZEROS
Definition: vc1_parser.c:48
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:204
ff_vc1_decode_entry_point
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition: vc1.c:501
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
ff_vc1_parse_frame_header_adv
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:846
VC1Context::s
MpegEncContext s
Definition: vc1.h:174
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
parser.h
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecParserContext
Definition: avcodec.h:2748
ret
ret
Definition: filter_design.txt:187
UNESCAPED_THRESHOLD
#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:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
VC1ParseContext::v
VC1Context v
Definition: vc1_parser.c:54
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
VC1Context::tff
uint8_t tff
Definition: vc1.h:311
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
VC1Context::profile
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags.
Definition: vc1.h:216
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:639
VC1Context::psf
int psf
Progressive Segmented Frame.
Definition: vc1.h:209
VC1Context::broadcast
int broadcast
TFF/RFF present.
Definition: vc1.h:198
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParser
Definition: avcodec.h:2907
AV_PICTURE_TYPE_BI
@ AV_PICTURE_TYPE_BI
BI type.
Definition: avutil.h:285
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
vc1_extract_header
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:62
VC1ParseContext::pc
ParseContext pc
Definition: vc1_parser.c:53
VC1Context::rff
uint8_t rff
Definition: vc1.h:311