FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vp9mvs.c
Go to the documentation of this file.
1 /*
2  * VP9 compatible video decoder
3  *
4  * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5  * Copyright (C) 2013 Clément Bœsch <u pkh me>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "internal.h"
25 #include "vp56.h"
26 #include "vp9.h"
27 #include "vp9data.h"
28 #include "vp9dec.h"
29 
30 static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src,
31  VP9Context *s)
32 {
33  dst->x = av_clip(src->x, s->min_mv.x, s->max_mv.x);
34  dst->y = av_clip(src->y, s->min_mv.y, s->max_mv.y);
35 }
36 
37 static void find_ref_mvs(VP9Context *s,
38  VP56mv *pmv, int ref, int z, int idx, int sb)
39 {
40  static const int8_t mv_ref_blk_off[N_BS_SIZES][8][2] = {
41  [BS_64x64] = { { 3, -1 }, { -1, 3 }, { 4, -1 }, { -1, 4 },
42  { -1, -1 }, { 0, -1 }, { -1, 0 }, { 6, -1 } },
43  [BS_64x32] = { { 0, -1 }, { -1, 0 }, { 4, -1 }, { -1, 2 },
44  { -1, -1 }, { 0, -3 }, { -3, 0 }, { 2, -1 } },
45  [BS_32x64] = { { -1, 0 }, { 0, -1 }, { -1, 4 }, { 2, -1 },
46  { -1, -1 }, { -3, 0 }, { 0, -3 }, { -1, 2 } },
47  [BS_32x32] = { { 1, -1 }, { -1, 1 }, { 2, -1 }, { -1, 2 },
48  { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
49  [BS_32x16] = { { 0, -1 }, { -1, 0 }, { 2, -1 }, { -1, -1 },
50  { -1, 1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
51  [BS_16x32] = { { -1, 0 }, { 0, -1 }, { -1, 2 }, { -1, -1 },
52  { 1, -1 }, { -3, 0 }, { 0, -3 }, { -3, -3 } },
53  [BS_16x16] = { { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 },
54  { -1, -1 }, { 0, -3 }, { -3, 0 }, { -3, -3 } },
55  [BS_16x8] = { { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, -1 },
56  { 0, -2 }, { -2, 0 }, { -2, -1 }, { -1, -2 } },
57  [BS_8x16] = { { -1, 0 }, { 0, -1 }, { -1, 1 }, { -1, -1 },
58  { -2, 0 }, { 0, -2 }, { -1, -2 }, { -2, -1 } },
59  [BS_8x8] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
60  { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
61  [BS_8x4] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
62  { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
63  [BS_4x8] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
64  { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
65  [BS_4x4] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 },
66  { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } },
67  };
68  VP9Block *b = s->b;
69  int row = s->row, col = s->col, row7 = s->row7;
70  const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
71 #define INVALID_MV 0x80008000U
72  uint32_t mem = INVALID_MV, mem_sub8x8 = INVALID_MV;
73  int i;
74 
75 #define RETURN_DIRECT_MV(mv) \
76  do { \
77  uint32_t m = AV_RN32A(&mv); \
78  if (!idx) { \
79  AV_WN32A(pmv, m); \
80  return; \
81  } else if (mem == INVALID_MV) { \
82  mem = m; \
83  } else if (m != mem) { \
84  AV_WN32A(pmv, m); \
85  return; \
86  } \
87  } while (0)
88 
89  if (sb >= 0) {
90  if (sb == 2 || sb == 1) {
91  RETURN_DIRECT_MV(b->mv[0][z]);
92  } else if (sb == 3) {
93  RETURN_DIRECT_MV(b->mv[2][z]);
94  RETURN_DIRECT_MV(b->mv[1][z]);
95  RETURN_DIRECT_MV(b->mv[0][z]);
96  }
97 
98 #define RETURN_MV(mv) \
99  do { \
100  if (sb > 0) { \
101  VP56mv tmp; \
102  uint32_t m; \
103  av_assert2(idx == 1); \
104  av_assert2(mem != INVALID_MV); \
105  if (mem_sub8x8 == INVALID_MV) { \
106  clamp_mv(&tmp, &mv, s); \
107  m = AV_RN32A(&tmp); \
108  if (m != mem) { \
109  AV_WN32A(pmv, m); \
110  return; \
111  } \
112  mem_sub8x8 = AV_RN32A(&mv); \
113  } else if (mem_sub8x8 != AV_RN32A(&mv)) { \
114  clamp_mv(&tmp, &mv, s); \
115  m = AV_RN32A(&tmp); \
116  if (m != mem) { \
117  AV_WN32A(pmv, m); \
118  } else { \
119  /* BUG I'm pretty sure this isn't the intention */ \
120  AV_WN32A(pmv, 0); \
121  } \
122  return; \
123  } \
124  } else { \
125  uint32_t m = AV_RN32A(&mv); \
126  if (!idx) { \
127  clamp_mv(pmv, &mv, s); \
128  return; \
129  } else if (mem == INVALID_MV) { \
130  mem = m; \
131  } else if (m != mem) { \
132  clamp_mv(pmv, &mv, s); \
133  return; \
134  } \
135  } \
136  } while (0)
137 
138  if (row > 0) {
139  VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
140  if (mv->ref[0] == ref)
141  RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
142  else if (mv->ref[1] == ref)
143  RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]);
144  }
145  if (col > s->tile_col_start) {
146  VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
147  if (mv->ref[0] == ref)
148  RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
149  else if (mv->ref[1] == ref)
150  RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][1]);
151  }
152  i = 2;
153  } else {
154  i = 0;
155  }
156 
157  // previously coded MVs in this neighborhood, using same reference frame
158  for (; i < 8; i++) {
159  int c = p[i][0] + col, r = p[i][1] + row;
160 
161  if (c >= s->tile_col_start && c < s->cols &&
162  r >= 0 && r < s->rows) {
163  VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
164 
165  if (mv->ref[0] == ref)
166  RETURN_MV(mv->mv[0]);
167  else if (mv->ref[1] == ref)
168  RETURN_MV(mv->mv[1]);
169  }
170  }
171 
172  // MV at this position in previous frame, using same reference frame
173  if (s->s.h.use_last_frame_mvs) {
174  VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
175 
178  if (mv->ref[0] == ref)
179  RETURN_MV(mv->mv[0]);
180  else if (mv->ref[1] == ref)
181  RETURN_MV(mv->mv[1]);
182  }
183 
184 #define RETURN_SCALE_MV(mv, scale) \
185  do { \
186  if (scale) { \
187  VP56mv mv_temp = { -mv.x, -mv.y }; \
188  RETURN_MV(mv_temp); \
189  } else { \
190  RETURN_MV(mv); \
191  } \
192  } while (0)
193 
194  // previously coded MVs in this neighborhood, using different reference frame
195  for (i = 0; i < 8; i++) {
196  int c = p[i][0] + col, r = p[i][1] + row;
197 
198  if (c >= s->tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
199  VP9mvrefPair *mv = &s->s.frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
200 
201  if (mv->ref[0] != ref && mv->ref[0] >= 0)
202  RETURN_SCALE_MV(mv->mv[0],
203  s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
204  if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
205  // BUG - libvpx has this condition regardless of whether
206  // we used the first ref MV and pre-scaling
207  AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
208  RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
209  }
210  }
211  }
212 
213  // MV at this position in previous frame, using different reference frame
214  if (s->s.h.use_last_frame_mvs) {
215  VP9mvrefPair *mv = &s->s.frames[REF_FRAME_MVPAIR].mv[row * s->sb_cols * 8 + col];
216 
217  // no need to await_progress, because we already did that above
218  if (mv->ref[0] != ref && mv->ref[0] >= 0)
219  RETURN_SCALE_MV(mv->mv[0], s->s.h.signbias[mv->ref[0]] != s->s.h.signbias[ref]);
220  if (mv->ref[1] != ref && mv->ref[1] >= 0 &&
221  // BUG - libvpx has this condition regardless of whether
222  // we used the first ref MV and pre-scaling
223  AV_RN32A(&mv->mv[0]) != AV_RN32A(&mv->mv[1])) {
224  RETURN_SCALE_MV(mv->mv[1], s->s.h.signbias[mv->ref[1]] != s->s.h.signbias[ref]);
225  }
226  }
227 
228  AV_ZERO32(pmv);
229  clamp_mv(pmv, pmv, s);
230 #undef INVALID_MV
231 #undef RETURN_MV
232 #undef RETURN_SCALE_MV
233 }
234 
235 static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
236 {
237  int bit, sign = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].sign);
239  s->prob.p.mv_comp[idx].classes);
240 
241  s->counts.mv_comp[idx].sign[sign]++;
242  s->counts.mv_comp[idx].classes[c]++;
243  if (c) {
244  int m;
245 
246  for (n = 0, m = 0; m < c; m++) {
247  bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].bits[m]);
248  n |= bit << m;
249  s->counts.mv_comp[idx].bits[m][bit]++;
250  }
251  n <<= 3;
253  s->prob.p.mv_comp[idx].fp);
254  n |= bit << 1;
255  s->counts.mv_comp[idx].fp[bit]++;
256  if (hp) {
257  bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].hp);
258  s->counts.mv_comp[idx].hp[bit]++;
259  n |= bit;
260  } else {
261  n |= 1;
262  // bug in libvpx - we count for bw entropy purposes even if the
263  // bit wasn't coded
264  s->counts.mv_comp[idx].hp[1]++;
265  }
266  n += 8 << c;
267  } else {
268  n = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0);
269  s->counts.mv_comp[idx].class0[n]++;
271  s->prob.p.mv_comp[idx].class0_fp[n]);
272  s->counts.mv_comp[idx].class0_fp[n][bit]++;
273  n = (n << 3) | (bit << 1);
274  if (hp) {
275  bit = vp56_rac_get_prob(&s->c, s->prob.p.mv_comp[idx].class0_hp);
276  s->counts.mv_comp[idx].class0_hp[bit]++;
277  n |= bit;
278  } else {
279  n |= 1;
280  // bug in libvpx - we count for bw entropy purposes even if the
281  // bit wasn't coded
282  s->counts.mv_comp[idx].class0_hp[1]++;
283  }
284  }
285 
286  return sign ? -(n + 1) : (n + 1);
287 }
288 
289 void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
290 {
291  VP9Block *b = s->b;
292 
293  if (mode == ZEROMV) {
294  AV_ZERO64(mv);
295  } else {
296  int hp;
297 
298  // FIXME cache this value and reuse for other subblocks
299  find_ref_mvs(s, &mv[0], b->ref[0], 0, mode == NEARMV,
300  mode == NEWMV ? -1 : sb);
301  // FIXME maybe move this code into find_ref_mvs()
302  if ((mode == NEWMV || sb == -1) &&
303  !(hp = s->s.h.highprecisionmvs &&
304  abs(mv[0].x) < 64 && abs(mv[0].y) < 64)) {
305  if (mv[0].y & 1) {
306  if (mv[0].y < 0)
307  mv[0].y++;
308  else
309  mv[0].y--;
310  }
311  if (mv[0].x & 1) {
312  if (mv[0].x < 0)
313  mv[0].x++;
314  else
315  mv[0].x--;
316  }
317  }
318  if (mode == NEWMV) {
320  s->prob.p.mv_joint);
321 
322  s->counts.mv_joint[j]++;
323  if (j >= MV_JOINT_V)
324  mv[0].y += read_mv_component(s, 0, hp);
325  if (j & 1)
326  mv[0].x += read_mv_component(s, 1, hp);
327  }
328 
329  if (b->comp) {
330  // FIXME cache this value and reuse for other subblocks
331  find_ref_mvs(s, &mv[1], b->ref[1], 1, mode == NEARMV,
332  mode == NEWMV ? -1 : sb);
333  if ((mode == NEWMV || sb == -1) &&
334  !(hp = s->s.h.highprecisionmvs &&
335  abs(mv[1].x) < 64 && abs(mv[1].y) < 64)) {
336  if (mv[1].y & 1) {
337  if (mv[1].y < 0)
338  mv[1].y++;
339  else
340  mv[1].y--;
341  }
342  if (mv[1].x & 1) {
343  if (mv[1].x < 0)
344  mv[1].x++;
345  else
346  mv[1].x--;
347  }
348  }
349  if (mode == NEWMV) {
351  s->prob.p.mv_joint);
352 
353  s->counts.mv_joint[j]++;
354  if (j >= MV_JOINT_V)
355  mv[1].y += read_mv_component(s, 0, hp);
356  if (j & 1)
357  mv[1].x += read_mv_component(s, 1, hp);
358  }
359  }
360  }
361 }
ThreadFrame tf
Definition: vp9shared.h:60
const int8_t ff_vp9_mv_joint_tree[3][2]
Definition: vp9data.c:2224
const char * s
Definition: avisynth_c.h:768
VP5 and VP6 compatible video decoder (common features)
static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp56.h:380
int row
Definition: vp9dec.h:98
VP9BitstreamHeader h
Definition: vp9shared.h:160
ProbContext p
Definition: vp9dec.h:120
struct ProbContext::@145 mv_comp[2]
const char * b
Definition: vf_curves.c:113
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define src
Definition: vp8dsp.c:254
uint8_t ref[2]
Definition: vp9dec.h:78
#define RETURN_MV(mv)
int y
Definition: vp9dec.h:192
const int8_t ff_vp9_mv_class_tree[10][2]
Definition: vp9data.c:2230
#define AV_RN32A(p)
Definition: intreadwrite.h:531
int16_t y
Definition: vp56.h:68
int mem
Definition: avisynth_c.h:821
VP9Frame frames[3]
Definition: vp9shared.h:166
int col
Definition: vp9dec.h:98
int x
Definition: vp9dec.h:192
uint8_t hp
Definition: vp9dec.h:66
uint8_t sign
Definition: vp9dec.h:59
uint8_t fp[3]
Definition: vp9dec.h:64
uint8_t signbias[3]
Definition: vp9shared.h:112
unsigned mv_joint[4]
Definition: vp9dec.h:140
const char * r
Definition: vf_curves.c:111
struct VP9Context::@149::@151 mv_comp[2]
#define REF_FRAME_MVPAIR
Definition: vp9shared.h:164
static av_always_inline void clamp_mv(VP56mv *dst, const VP56mv *src, VP9Context *s)
Definition: vp9mvs.c:30
int uses_2pass
Definition: vp9shared.h:64
const int8_t ff_vp9_mv_fp_tree[3][2]
Definition: vp9data.c:2243
VP56mv left_mv_ctx[16][2]
Definition: vp9dec.h:159
int8_t ref[2]
Definition: vp9shared.h:56
uint8_t class0_hp
Definition: vp9dec.h:65
VP9SharedContext s
Definition: vp9dec.h:88
int n
Definition: avisynth_c.h:684
MVJoint
Definition: vp9dec.h:37
#define vp56_rac_get_prob
Definition: vp56.h:254
unsigned tile_col_start
Definition: vp9dec.h:118
#define RETURN_SCALE_MV(mv, scale)
unsigned sb_cols
Definition: vp9dec.h:111
static const int8_t mv[256][2]
Definition: 4xm.c:77
int row7
Definition: vp9dec.h:98
VP56mv(* above_mv_ctx)[2]
Definition: vp9dec.h:181
uint8_t class0_fp[2][3]
Definition: vp9dec.h:63
#define CUR_FRAME
Definition: vp9shared.h:163
static void find_ref_mvs(VP9Context *s, VP56mv *pmv, int ref, int z, int idx, int sb)
Definition: vp9mvs.c:37
enum BlockSize bs
Definition: vp9dec.h:81
VP56mv mv[4][2]
Definition: vp9dec.h:80
VP9Block * b
Definition: vp9dec.h:96
Definition: vp56.h:66
uint8_t comp
Definition: vp9dec.h:78
uint8_t bits[10]
Definition: vp9dec.h:62
static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp)
Definition: vp9mvs.c:235
#define AV_ZERO64(d)
Definition: intreadwrite.h:623
int16_t x
Definition: vp56.h:67
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static double c[64]
void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb)
Definition: vp9mvs.c:289
#define INVALID_MV
struct VP9Context::@148 prob
#define RETURN_DIRECT_MV(mv)
struct VP9Context::@149 counts
uint8_t mv_joint[3]
Definition: vp9dec.h:57
uint8_t class0
Definition: vp9dec.h:61
#define AV_ZERO32(d)
Definition: intreadwrite.h:619
VP9mvrefPair * mv
Definition: vp9shared.h:63
uint8_t use_last_frame_mvs
Definition: vp9shared.h:110
struct VP9Context::@150 min_mv
#define av_always_inline
Definition: attributes.h:39
VP56mv mv[2]
Definition: vp9shared.h:55
struct VP9Context::@150 max_mv
uint8_t classes[10]
Definition: vp9dec.h:60
uint8_t highprecisionmvs
Definition: vp9shared.h:104
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
VP56RangeCoder c
Definition: vp9dec.h:93