Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "libavutil/attributes.h"
29 #include "parser.h"
30 #include "vc1.h"
31 #include "get_bits.h"
32 
35 #define UNESCAPED_THRESHOLD 37
36 
41 #define UNESCAPED_LIMIT 144
42 
43 typedef enum {
49 
50 typedef struct {
54  size_t bytes_to_skip;
55  uint8_t unesc_buffer[UNESCAPED_LIMIT];
56  size_t unesc_index;
59 
61  const uint8_t *buf, int buf_size)
62 {
63  /* Parse the header we just finished unescaping */
64  VC1ParseContext *vpc = s->priv_data;
65  GetBitContext gb;
66  vpc->v.s.avctx = avctx;
67  vpc->v.parse_only = 1;
68  init_get_bits(&gb, buf, buf_size * 8);
69  switch (vpc->prev_start_code) {
70  case VC1_CODE_SEQHDR & 0xFF:
71  ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
72  break;
73  case VC1_CODE_ENTRYPOINT & 0xFF:
74  ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
75  break;
76  case VC1_CODE_FRAME & 0xFF:
77  if(vpc->v.profile < PROFILE_ADVANCED)
78  ff_vc1_parse_frame_header (&vpc->v, &gb);
79  else
80  ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
81 
82  /* keep AV_PICTURE_TYPE_BI internal to VC1 */
83  if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
85  else
86  s->pict_type = vpc->v.s.pict_type;
87 
88  if (avctx->ticks_per_frame > 1){
89  // process pulldown flags
90  s->repeat_pict = 1;
91  // Pulldown flags are only valid when 'broadcast' has been set.
92  // So ticks_per_frame will be 2
93  if (vpc->v.rff){
94  // repeat field
95  s->repeat_pict = 2;
96  }else if (vpc->v.rptfrm){
97  // repeat frames
98  s->repeat_pict = vpc->v.rptfrm * 2 + 1;
99  }
100  }else{
101  s->repeat_pict = 0;
102  }
103 
104  if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
105  s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
106  else
108 
109  break;
110  }
111 }
112 
114  AVCodecContext *avctx,
115  const uint8_t **poutbuf, int *poutbuf_size,
116  const uint8_t *buf, int buf_size)
117 {
118  /* Here we do the searching for frame boundaries and headers at
119  * the same time. Only a minimal amount at the start of each
120  * header is unescaped. */
121  VC1ParseContext *vpc = s->priv_data;
122  int pic_found = vpc->pc.frame_start_found;
123  uint8_t *unesc_buffer = vpc->unesc_buffer;
124  size_t unesc_index = vpc->unesc_index;
125  VC1ParseSearchState search_state = vpc->search_state;
126  int next = END_NOT_FOUND;
127  int i = vpc->bytes_to_skip;
128 
129  if (pic_found && buf_size == 0) {
130  /* EOF considered as end of frame */
131  memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
132  vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
133  next = 0;
134  }
135  while (i < buf_size) {
136  int start_code_found = 0;
137  uint8_t b;
138  while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
139  b = buf[i++];
140  unesc_buffer[unesc_index++] = b;
141  if (search_state <= ONE_ZERO)
142  search_state = b ? NO_MATCH : search_state + 1;
143  else if (search_state == TWO_ZEROS) {
144  if (b == 1)
145  search_state = ONE;
146  else if (b > 1) {
147  if (b == 3)
148  unesc_index--; // swallow emulation prevention byte
149  search_state = NO_MATCH;
150  }
151  }
152  else { // search_state == ONE
153  // Header unescaping terminates early due to detection of next start code
154  search_state = NO_MATCH;
155  start_code_found = 1;
156  break;
157  }
158  }
159  if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
160  unesc_index >= UNESCAPED_THRESHOLD &&
161  vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
162  {
163  // No need to keep scanning the rest of the buffer for
164  // start codes if we know it contains a complete frame and
165  // we've already unescaped all we need of the frame header
166  vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
167  break;
168  }
169  if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
170  while (i < buf_size) {
171  if (search_state == NO_MATCH) {
172  i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
173  if (i < buf_size) {
174  search_state = ONE_ZERO;
175  }
176  i++;
177  } else {
178  b = buf[i++];
179  if (search_state == ONE_ZERO)
180  search_state = b ? NO_MATCH : TWO_ZEROS;
181  else if (search_state == TWO_ZEROS) {
182  if (b >= 1)
183  search_state = b == 1 ? ONE : NO_MATCH;
184  }
185  else { // search_state == ONE
186  search_state = NO_MATCH;
187  start_code_found = 1;
188  break;
189  }
190  }
191  }
192  }
193  if (start_code_found) {
194  vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
195 
196  vpc->prev_start_code = b;
197  unesc_index = 0;
198 
199  if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
200  if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
201  pic_found = 1;
202  }
203  else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
204  next = i - 4;
205  pic_found = b == (VC1_CODE_FRAME & 0xFF);
206  break;
207  }
208  }
209  }
210  }
211 
212  vpc->pc.frame_start_found = pic_found;
213  vpc->unesc_index = unesc_index;
214  vpc->search_state = search_state;
215 
217  next = buf_size;
218  } else {
219  if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
220  vpc->bytes_to_skip = 0;
221  *poutbuf = NULL;
222  *poutbuf_size = 0;
223  return buf_size;
224  }
225  }
226 
227  /* If we return with a valid pointer to a combined frame buffer
228  * then on the next call then we'll have been unhelpfully rewound
229  * by up to 4 bytes (depending upon whether the start code
230  * overlapped the input buffer, and if so by how much). We don't
231  * want this: it will either cause spurious second detections of
232  * the start code we've already seen, or cause extra bytes to be
233  * inserted at the start of the unescaped buffer. */
234  vpc->bytes_to_skip = 4;
235  if (next < 0)
236  vpc->bytes_to_skip += next;
237 
238  *poutbuf = buf;
239  *poutbuf_size = buf_size;
240  return next;
241 }
242 
243 static int vc1_split(AVCodecContext *avctx,
244  const uint8_t *buf, int buf_size)
245 {
246  int i;
247  uint32_t state= -1;
248  int charged=0;
249 
250  for(i=0; i<buf_size; i++){
251  state= (state<<8) | buf[i];
252  if(IS_MARKER(state)){
253  if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
254  charged=1;
255  }else if(charged){
256  return i-3;
257  }
258  }
259  }
260  return 0;
261 }
262 
264 {
265  VC1ParseContext *vpc = s->priv_data;
266  vpc->v.s.slice_context_count = 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  return ff_vc1_init_common(&vpc->v);
272 }
273 
275  .codec_ids = { AV_CODEC_ID_VC1 },
276  .priv_data_size = sizeof(VC1ParseContext),
277  .parser_init = vc1_parse_init,
278  .parser_parse = vc1_parse,
279  .parser_close = ff_parse_close,
280  .split = vc1_split,
281 };
size_t unesc_index
Definition: vc1_parser.c:56
#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:41
BI type.
Definition: avutil.h:259
The VC1 Context.
Definition: vc1.h:182
AVCodecParser ff_vc1_parser
Definition: vc1_parser.c:274
int broadcast
TFF/RFF present.
Definition: vc1.h:209
enum AVFieldOrder field_order
Definition: avcodec.h:3794
int codec_ids[5]
Definition: avcodec.h:3816
int(* startcode_find_candidate)(const uint8_t *buf, int size)
Search buf from the start for up to size bytes.
Definition: vc1dsp.h:81
int frame_start_found
Definition: parser.h:34
Macro definitions for various function/variable attributes.
uint8_t rff
Definition: vc1.h:319
uint8_t
#define av_cold
Definition: attributes.h:66
av_cold int ff_vc1_init_common(VC1Context *v)
Init VC-1 specific tables and VC1Context members.
Definition: vc1.c:1568
int interlace
Progressive/interlaced (RPTFTM syntax element)
Definition: vc1.h:210
#define b
Definition: input.c:52
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags...
Definition: vc1.h:227
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:294
VC1ParseSearchState
Definition: vc1_parser.c:43
int psf
Progressive Segmented Frame.
Definition: vc1.h:220
VC1Context v
Definition: vc1_parser.c:52
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:282
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:217
#define IS_MARKER(state, i, buf, buf_size)
Definition: dca_parser.c:37
static char * split(char *message, char delim)
Definition: af_channelmap.c:86
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:60
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:839
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:287
ParseContext pc
Definition: vc1_parser.c:51
#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:35
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:624
size_t bytes_to_skip
Definition: vc1_parser.c:54
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1178
static int vc1_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:243
VC1ParseSearchState search_state
Definition: vc1_parser.c:57
NULL
Definition: eval.c:55
main external API structure.
Definition: avcodec.h:1044
uint8_t prev_start_code
Definition: vc1_parser.c:53
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
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:113
#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:339
static uint32_t state
Definition: trasher.c:27
uint8_t tff
Definition: vc1.h:319
MpegEncContext s
Definition: vc1.h:183
Definition: vc1_parser.c:47
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
static av_cold int vc1_parse_init(AVCodecParserContext *s)
Definition: vc1_parser.c:263
Bi-dir predicted.
Definition: avutil.h:255
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3694
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition: vc1.c:526
uint8_t rptfrm
Definition: vc1.h:319
int parse_only
Context is used within parser.
Definition: vc1.h:403
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:3678
uint8_t unesc_buffer[UNESCAPED_LIMIT]
Definition: vc1_parser.c:55
VC1DSPContext vc1dsp
Definition: vc1.h:186