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 "parser.h"
29 #include "vc1.h"
30 #include "get_bits.h"
31 
32 typedef struct {
36 
38  const uint8_t *buf, int buf_size)
39 {
40  VC1ParseContext *vpc = s->priv_data;
41  GetBitContext gb;
42  const uint8_t *start, *end, *next;
44 
45  vpc->v.s.avctx = avctx;
46  vpc->v.parse_only = 1;
47  next = buf;
48  s->repeat_pict = 0;
49 
50  for(start = buf, end = buf + buf_size; next < end; start = next){
51  int buf2_size, size;
52 
53  next = find_next_marker(start + 4, end);
54  size = next - start - 4;
55  buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
56  init_get_bits(&gb, buf2, buf2_size * 8);
57  if(size <= 0) continue;
58  switch(AV_RB32(start)){
59  case VC1_CODE_SEQHDR:
60  ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
61  break;
63  ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
64  break;
65  case VC1_CODE_FRAME:
66  if(vpc->v.profile < PROFILE_ADVANCED)
67  ff_vc1_parse_frame_header (&vpc->v, &gb);
68  else
69  ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
70 
71  /* keep AV_PICTURE_TYPE_BI internal to VC1 */
72  if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
74  else
75  s->pict_type = vpc->v.s.pict_type;
76 
77  if (avctx->ticks_per_frame > 1){
78  // process pulldown flags
79  s->repeat_pict = 1;
80  // Pulldown flags are only valid when 'broadcast' has been set.
81  // So ticks_per_frame will be 2
82  if (vpc->v.rff){
83  // repeat field
84  s->repeat_pict = 2;
85  }else if (vpc->v.rptfrm){
86  // repeat frames
87  s->repeat_pict = vpc->v.rptfrm * 2 + 1;
88  }
89  }
90 
91  break;
92  }
93  }
94 
95  av_free(buf2);
96 }
97 
98 /**
99  * Find the end of the current frame in the bitstream.
100  * @return the position of the first byte of the next frame, or -1
101  */
103  int buf_size) {
104  int pic_found, i;
105  uint32_t state;
106 
107  pic_found= pc->frame_start_found;
108  state= pc->state;
109 
110  i=0;
111  if(!pic_found){
112  for(i=0; i<buf_size; i++){
113  state= (state<<8) | buf[i];
114  if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
115  i++;
116  pic_found=1;
117  break;
118  }
119  }
120  }
121 
122  if(pic_found){
123  /* EOF considered as end of frame */
124  if (buf_size == 0)
125  return 0;
126  for(; i<buf_size; i++){
127  state= (state<<8) | buf[i];
128  if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
129  pc->frame_start_found=0;
130  pc->state=-1;
131  return i-3;
132  }
133  }
134  }
135  pc->frame_start_found= pic_found;
136  pc->state= state;
137  return END_NOT_FOUND;
138 }
139 
141  AVCodecContext *avctx,
142  const uint8_t **poutbuf, int *poutbuf_size,
143  const uint8_t *buf, int buf_size)
144 {
145  VC1ParseContext *vpc = s->priv_data;
146  int next;
147 
149  next= buf_size;
150  }else{
151  next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
152 
153  if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
154  *poutbuf = NULL;
155  *poutbuf_size = 0;
156  return buf_size;
157  }
158  }
159 
160  vc1_extract_headers(s, avctx, buf, buf_size);
161 
162  *poutbuf = buf;
163  *poutbuf_size = buf_size;
164  return next;
165 }
166 
167 static int vc1_split(AVCodecContext *avctx,
168  const uint8_t *buf, int buf_size)
169 {
170  int i;
171  uint32_t state= -1;
172  int charged=0;
173 
174  for(i=0; i<buf_size; i++){
175  state= (state<<8) | buf[i];
176  if(IS_MARKER(state)){
177  if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
178  charged=1;
179  }else if(charged){
180  return i-3;
181  }
182  }
183  }
184  return 0;
185 }
186 
188 {
189  VC1ParseContext *vpc = s->priv_data;
190  vpc->v.s.slice_context_count = 1;
191  return ff_vc1_init_common(&vpc->v);
192 }
193 
195  .codec_ids = { AV_CODEC_ID_VC1 },
196  .priv_data_size = sizeof(VC1ParseContext),
197  .parser_init = vc1_parse_init,
198  .parser_parse = vc1_parse,
199  .parser_close = ff_parse_close,
200  .split = vc1_split,
201 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
BI type.
Definition: avutil.h:222
const char * s
Definition: avisynth_c.h:668
The VC1 Context.
Definition: vc1.h:182
AVCodecParser ff_vc1_parser
Definition: vc1_parser.c:194
static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: vc1_parser.c:102
int frame_start_found
Definition: parser.h:34
uint8_t rff
Definition: vc1.h:314
uint8_t
#define AV_RB32
end end
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags...
Definition: vc1.h:224
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:293
static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:37
VC1Context v
Definition: vc1_parser.c:34
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:319
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
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:214
#define PARSER_FLAG_COMPLETE_FRAMES
#define IS_MARKER(state, i, buf, buf_size)
Definition: dca_parser.c:37
int size
int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:830
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:279
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
ParseContext pc
Definition: vc1_parser.c:33
int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext *gb)
Definition: vc1.c:579
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Note except for filters that can have queued request_frame does not push and as a the filter_frame method will be called and do the work Legacy the filter_frame method was split
static int vc1_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: vc1_parser.c:167
NULL
Definition: eval.c:55
int ff_vc1_init_common(VC1Context *v)
Init VC-1 specific tables and VC1Context members.
Definition: vc1.c:1544
main external API structure.
void * buf
Definition: avisynth_c.h:594
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
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:140
#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:377
static uint32_t state
Definition: trasher.c:27
MpegEncContext s
Definition: vc1.h:183
struct AVCodecContext * avctx
Definition: mpegvideo.h:243
Bi-dir predicted.
Definition: avutil.h:218
int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
Definition: vc1.c:524
uint8_t rptfrm
Definition: vc1.h:314
static int vc1_parse_init(AVCodecParserContext *s)
Definition: vc1_parser.c:187
int parse_only
Context is used within parser.
Definition: vc1.h:400
static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst)
Definition: vc1.h:422
int repeat_pict
This field is used for proper frame duration computation in lavf.
void INT64 start
Definition: avisynth_c.h:594
static av_always_inline const uint8_t * find_next_marker(const uint8_t *src, const uint8_t *end)
Find VC-1 marker in buffer.
Definition: vc1.h:408