FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
48 typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
49  VP56mv *vect);
50 typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
51  int offset1, int offset2, int stride,
52  VP56mv mv, int mask, int select, int luma);
53 typedef void (*VP56ParseCoeff)(VP56Context *s);
54 typedef void (*VP56DefaultModelsInit)(VP56Context *s);
55 typedef void (*VP56ParseVectorModels)(VP56Context *s);
56 typedef int (*VP56ParseCoeffModels)(VP56Context *s);
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 {
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 */
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 */
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 */
170  VP56Context *alpha_context;
171 
174 
175  /* huffman decoding */
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);
186 int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
187  int flip, int has_alpha);
188 int ff_vp56_free(AVCodecContext *avctx);
189 int ff_vp56_free_context(VP56Context *s);
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 */