vp56.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * VP5 and VP6 compatible video decoder (common features)
24  */
25 
26 #ifndef AVCODEC_VP56_H
27 #define AVCODEC_VP56_H
28 
29 #include "vp56data.h"
30 #include "dsputil.h"
31 #include "get_bits.h"
32 #include "hpeldsp.h"
33 #include "bytestream.h"
34 #include "h264chroma.h"
35 #include "videodsp.h"
36 #include "vp3dsp.h"
37 #include "vp56dsp.h"
38 
39 typedef struct vp56_context VP56Context;
40 
41 typedef struct VP56mv {
42  DECLARE_ALIGNED(4, int16_t, x);
43  int16_t y;
44 } VP56mv;
45 
46 #define VP56_SIZE_CHANGE 1
47 
49  VP56mv *vect);
51  int offset1, int offset2, int stride,
52  VP56mv mv, int mask, int select, int luma);
57 typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
58  int buf_size);
59 
60 typedef struct VP56RangeCoder {
61  int high;
62  int bits; /* stored negated (i.e. negative "bits" is a positive number of
63  bits left) in order to eliminate a negate in cache refilling */
64  const uint8_t *buffer;
65  const uint8_t *end;
66  unsigned int code_word;
68 
69 typedef struct VP56RefDc {
72  int16_t dc_coeff;
73 } VP56RefDc;
74 
75 typedef struct VP56Macroblock {
79 
80 typedef struct VP56Model {
81  uint8_t coeff_reorder[64]; /* used in vp6 only */
82  uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
83  uint8_t vector_sig[2]; /* delta sign */
84  uint8_t vector_dct[2]; /* delta coding types */
85  uint8_t vector_pdi[2][2]; /* predefined delta init */
86  uint8_t vector_pdv[2][7]; /* predefined delta values */
87  uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
88  uint8_t coeff_dccv[2][11]; /* DC coeff value */
89  uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
90  uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
91  uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
92  uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
93  uint8_t mb_type[3][10][10]; /* model for decoding MB type */
94  uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
95 } VP56Model;
96 
97 struct vp56_context {
104  uint8_t idct_scantable[64];
112 
113  /* frame info */
115  int plane_width[4];
116  int plane_height[4];
117  int mb_width; /* number of horizontal MB */
118  int mb_height; /* number of vertical MB */
119  int block_offset[6];
120 
122  uint16_t dequant_dc;
123  uint16_t dequant_ac;
124 
125  /* DC predictors management */
127  VP56RefDc left_block[4];
128  int above_block_idx[6];
129  int16_t prev_dc[3][3]; /* [plan][ref_frame] */
130 
131  /* blocks / macroblock */
134  DECLARE_ALIGNED(16, int16_t, block_coeff)[6][64];
135 
136  /* motion vectors */
137  VP56mv mv[6]; /* vectors for each block in MB */
138  VP56mv vector_candidate[2];
140 
141  /* filtering hints */
142  int filter_header; /* used in vp6 only */
148 
149  uint8_t coeff_ctx[4][64]; /* used in vp5 only */
150  uint8_t coeff_ctx_last[4]; /* used in vp5 only */
151 
153 
154  /* upside-down flipping hints */
155  int flip; /* are we flipping ? */
156  int frbi; /* first row block index in MB */
157  int srbi; /* second row block index in MB */
158  int stride[4]; /* stride for each plan */
159 
168 
169  /* for "slice" parallelism between YUV and A */
171 
174 
175  /* huffman decoding */
178  VLC dccv_vlc[2];
179  VLC runv_vlc[2];
180  VLC ract_vlc[2][3][6];
181  unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
182 };
183 
184 
185 int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
187  int flip, int has_alpha);
188 int ff_vp56_free(AVCodecContext *avctx);
190 void ff_vp56_init_dequant(VP56Context *s, int quantizer);
191 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
192  AVPacket *avpkt);
193 
194 
195 /**
196  * vp56 specific range coder implementation
197  */
198 
199 extern const uint8_t ff_vp56_norm_shift[256];
200 void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
201 
203 {
204  int shift = ff_vp56_norm_shift[c->high];
205  int bits = c->bits;
206  unsigned int code_word = c->code_word;
207 
208  c->high <<= shift;
209  code_word <<= shift;
210  bits += shift;
211  if(bits >= 0 && c->buffer < c->end) {
212  code_word |= bytestream_get_be16(&c->buffer) << bits;
213  bits -= 16;
214  }
215  c->bits = bits;
216  return code_word;
217 }
218 
219 #if ARCH_ARM
220 #include "arm/vp56_arith.h"
221 #elif ARCH_X86
222 #include "x86/vp56_arith.h"
223 #endif
224 
225 #ifndef vp56_rac_get_prob
226 #define vp56_rac_get_prob vp56_rac_get_prob
228 {
229  unsigned int code_word = vp56_rac_renorm(c);
230  unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
231  unsigned int low_shift = low << 16;
232  int bit = code_word >= low_shift;
233 
234  c->high = bit ? c->high - low : low;
235  c->code_word = bit ? code_word - low_shift : code_word;
236 
237  return bit;
238 }
239 #endif
240 
241 #ifndef vp56_rac_get_prob_branchy
242 // branchy variant, to be used where there's a branch based on the bit decoded
244 {
245  unsigned long code_word = vp56_rac_renorm(c);
246  unsigned low = 1 + (((c->high - 1) * prob) >> 8);
247  unsigned low_shift = low << 16;
248 
249  if (code_word >= low_shift) {
250  c->high -= low;
251  c->code_word = code_word - low_shift;
252  return 1;
253  }
254 
255  c->high = low;
256  c->code_word = code_word;
257  return 0;
258 }
259 #endif
260 
262 {
263  unsigned int code_word = vp56_rac_renorm(c);
264  /* equiprobable */
265  int low = (c->high + 1) >> 1;
266  unsigned int low_shift = low << 16;
267  int bit = code_word >= low_shift;
268  if (bit) {
269  c->high -= low;
270  code_word -= low_shift;
271  } else {
272  c->high = low;
273  }
274 
275  c->code_word = code_word;
276  return bit;
277 }
278 
279 // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
281 {
282  return vp56_rac_get_prob(c, 128);
283 }
284 
286 {
287  int value = 0;
288 
289  while (bits--) {
290  value = (value << 1) | vp56_rac_get(c);
291  }
292 
293  return value;
294 }
295 
297 {
298  int value = 0;
299 
300  while (bits--) {
301  value = (value << 1) | vp8_rac_get(c);
302  }
303 
304  return value;
305 }
306 
307 // fixme: add 1 bit to all the calls to this?
309 {
310  int v;
311 
312  if (!vp8_rac_get(c))
313  return 0;
314 
315  v = vp8_rac_get_uint(c, bits);
316 
317  if (vp8_rac_get(c))
318  v = -v;
319 
320  return v;
321 }
322 
323 // P(7)
325 {
326  int v = vp56_rac_gets(c, 7) << 1;
327  return v + !v;
328 }
329 
331 {
332  int v = vp8_rac_get_uint(c, 7) << 1;
333  return v + !v;
334 }
335 
336 static av_always_inline
338  const VP56Tree *tree,
339  const uint8_t *probs)
340 {
341  while (tree->val > 0) {
342  if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
343  tree += tree->val;
344  else
345  tree++;
346  }
347  return -tree->val;
348 }
349 
350 // how probabilities are associated with decisions is different I think
351 // well, the new scheme fits in the old but this way has one fewer branches per decision
352 static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
353  const uint8_t *probs)
354 {
355  int i = 0;
356 
357  do {
358  i = tree[i][vp56_rac_get_prob(c, probs[i])];
359  } while (i > 0);
360 
361  return -i;
362 }
363 
364 // DCTextra
366 {
367  int v = 0;
368 
369  do {
370  v = (v<<1) + vp56_rac_get_prob(c, *prob++);
371  } while (*prob);
372 
373  return v;
374 }
375 
376 #endif /* AVCODEC_VP56_H */
float v
const char * s
Definition: avisynth_c.h:668
static int shift(int a, int b)
Definition: sonic.c:86
unsigned int code_word
Definition: vp56.h:66
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
int(* VP56ParseCoeffModels)(VP56Context *s)
Definition: vp56.h:56
static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp56.h:352
VP56ParseCoeffModels parse_coeff_models
Definition: vp56.h:166
DECLARE_ALIGNED(4, int16_t, x)
uint16_t dequant_dc
Definition: vp56.h:122
int filter_header
Definition: vp56.h:142
int stride
Definition: mace.c:144
VP56RangeCoder c
Definition: vp56.h:108
int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
Definition: vp56.c:678
int16_t y
Definition: vp56.h:43
void(* VP56ParseVectorModels)(VP56Context *s)
Definition: vp56.h:55
uint8_t bits
Definition: crc.c:216
VP56Context * alpha_context
Definition: vp56.h:170
uint8_t
HpelDSPContext hdsp
Definition: vp56.h:100
void(* VP56ParseCoeff)(VP56Context *s)
Definition: vp56.h:53
uint8_t * edge_emu_buffer_alloc
Definition: vp56.h:106
VP56Frame
Definition: vp56data.h:31
struct VP56Model VP56Model
void ff_vp56_init_dequant(VP56Context *s, int quantizer)
Definition: vp56.c:34
VP56ParseCoeff parse_coeff
Definition: vp56.h:163
bitstream reader API header.
static av_always_inline int vp56_rac_get_tree(VP56RangeCoder *c, const VP56Tree *tree, const uint8_t *probs)
Definition: vp56.h:337
int filter_mode
Definition: vp56.h:145
VP56ParseVectorAdjustment parse_vector_adjustment
Definition: vp56.h:161
Discrete Time axis x
void(* VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src, int offset1, int offset2, int stride, VP56mv mv, int mask, int select, int luma)
Definition: vp56.h:50
static const uint16_t mask[17]
Definition: lzw.c:37
AVCodecContext * avctx
Definition: vp56.h:98
static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
Definition: vp56.h:261
Spectrum Plot time data
VP3DSPContext vp3dsp
Definition: vp56.h:102
VP56mb mb_type
Definition: vp56.h:132
Definition: vp56.h:69
uint8_t type
Definition: vp56.h:76
VP56RefDc * above_blocks
Definition: vp56.h:126
VP56Macroblock * macroblocks
Definition: vp56.h:133
VP56RangeCoder cc
Definition: vp56.h:109
VP56ParseVectorModels parse_vector_models
Definition: vp56.h:165
struct VP56Macroblock VP56Macroblock
Definition: get_bits.h:63
const uint8_t * end
Definition: vp56.h:65
int deblock_filtering
Definition: vp56.h:143
const uint8_t * vp56_coord_div
Definition: vp56.h:160
VP5 and VP6 compatible video decoder (common data)
static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
Definition: vp56.h:324
H264ChromaContext h264chroma
Definition: vp56.h:99
Half-pel DSP context.
Definition: hpeldsp.h:45
int8_t prob_idx
Definition: vp56data.h:54
int vector_candidate_pos
Definition: vp56.h:139
static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
Definition: vp56.h:308
struct VP56RefDc VP56RefDc
struct VP56mv VP56mv
static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
Definition: vp56.h:296
if it could not because there are no more frames
int sample_variance_threshold
Definition: vp56.h:147
VP56DefaultModelsInit default_models_init
Definition: vp56.h:164
GetBitContext gb
Definition: vp56.h:177
int mb_height
Definition: vp56.h:118
#define vp56_rac_get_prob
Definition: vp56.h:226
int golden_frame
Definition: vp56.h:114
VP56Frame ref_frame
Definition: vp56.h:71
VP56Model * modelp
Definition: vp56.h:172
static const int8_t mv[256][2]
int has_alpha
Definition: vp56.h:152
static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
Definition: vp56.h:243
Half-pel DSP functions.
int sub_version
Definition: vp56.h:111
int frbi
Definition: vp56.h:156
int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s, int flip, int has_alpha)
Definition: vp56.c:684
AVS_Value src
Definition: avisynth_c.h:523
typedef void(RENAME(mix_any_func_type))
main external API structure.
VP56RangeCoder * ccp
Definition: vp56.h:110
static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
Definition: vp56.h:202
int ff_vp56_free(AVCodecContext *avctx)
Definition: vp56.c:737
uint8_t not_null_dc
Definition: vp56.h:70
void * buf
Definition: avisynth_c.h:594
int bits
Definition: vp56.h:62
double value
Definition: eval.c:82
synthesis window for stochastic i
int filter_selection
Definition: vp56.h:144
int8_t val
Definition: vp56data.h:53
int max_vector_length
Definition: vp56.h:146
VideoDSPContext vdsp
Definition: vp56.h:101
VP56DSPContext vp56dsp
Definition: vp56.h:103
int mb_width
Definition: vp56.h:117
static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
Definition: vp56.h:365
int ff_vp56_free_context(VP56Context *s)
Definition: vp56.c:743
VP56ParseHeader parse_header
Definition: vp56.h:167
const uint8_t ff_vp56_norm_shift[256]
vp56 specific range coder implementation
Definition: vp56rac.c:25
int flip
Definition: vp56.h:155
void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: vp56rac.c:40
int use_huffman
Definition: vp56.h:176
Definition: vp56.h:41
int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp56.c:496
VP56mv mv
Definition: vp56.h:77
int(* VP56ParseHeader)(VP56Context *s, const uint8_t *buf, int buf_size)
Definition: vp56.h:57
uint16_t dequant_ac
Definition: vp56.h:123
static double c[64]
uint8_t * edge_emu_buffer
Definition: vp56.h:107
static void flip(AVCodecContext *avctx, AVPicture *picture)
void(* VP56ParseVectorAdjustment)(VP56Context *s, VP56mv *vect)
Definition: vp56.h:48
void(* VP56DefaultModelsInit)(VP56Context *s)
Definition: vp56.h:54
static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
Definition: vp56.h:280
VP56Filter filter
Definition: vp56.h:162
VP56Model model
Definition: vp56.h:173
Core video DSP helper functions.
DSP utils.
VP56mb
Definition: vp56data.h:39
int16_t dc_coeff
Definition: vp56.h:72
static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
Definition: vp56.h:285
struct VP56RangeCoder VP56RangeCoder
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int quantizer
Definition: vp56.h:121
static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
Definition: vp56.h:330
const uint8_t * buffer
Definition: vp56.h:64
int srbi
Definition: vp56.h:157
#define av_always_inline
Definition: attributes.h:41
Definition: vp56.h:80
int high
Definition: vp56.h:61
This structure stores compressed data.
#define av_unused
Definition: attributes.h:114