vp9_pred_common.c 17.6 KB
Newer Older
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1

2
/*
Paul Wilkins's avatar
Paul Wilkins committed
3
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4
5
6
7
8
9
10
11
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

12
13
#include <limits.h>

Ronald S. Bultje's avatar
Ronald S. Bultje committed
14
#include "vp9/common/vp9_common.h"
15
16
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_seg_common.h"
17
#include "vp9/common/vp9_treecoder.h"
18

19
20
21
22
23
24
25
26
static INLINE const MB_MODE_INFO *get_above_mbmi(const MODE_INFO *const above) {
  return (above != NULL) ? &above->mbmi : NULL;
}

static INLINE const MB_MODE_INFO *get_left_mbmi(const MODE_INFO *const left) {
  return (left != NULL) ? &left->mbmi : NULL;
}

27
// Returns a context number for the given MB prediction signal
28
unsigned char vp9_get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
29
30
31
32
  const MODE_INFO *const above_mi = get_above_mi(xd);
  const MODE_INFO *const left_mi = get_left_mi(xd);
  const int above_in_image = above_mi != NULL;
  const int left_in_image = left_mi != NULL;
33
34
35
36
37
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  // left
38
  const int left_mv_pred = left_in_image ? is_inter_block(&left_mi->mbmi)
39
                                         : 0;
40
  const int left_interp = left_in_image && left_mv_pred
41
                              ? left_mi->mbmi.interp_filter
42
                              : SWITCHABLE_FILTERS;
43
44

  // above
45
  const int above_mv_pred = above_in_image ? is_inter_block(&above_mi->mbmi)
46
                                           : 0;
47
  const int above_interp = above_in_image && above_mv_pred
48
                               ? above_mi->mbmi.interp_filter
49
                               : SWITCHABLE_FILTERS;
50
51

  if (left_interp == above_interp)
52
    return left_interp;
53
54
  else if (left_interp == SWITCHABLE_FILTERS &&
           above_interp != SWITCHABLE_FILTERS)
55
    return above_interp;
56
57
  else if (left_interp != SWITCHABLE_FILTERS &&
           above_interp == SWITCHABLE_FILTERS)
58
    return left_interp;
59
  else
60
    return SWITCHABLE_FILTERS;
61
}
62
// Returns a context number for the given MB prediction signal
63
64
65
66
67
int vp9_get_intra_inter_context(const MACROBLOCKD *xd) {
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(get_above_mi(xd));
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(get_left_mi(xd));
  const int above_in_image = above_mbmi != NULL;
  const int left_in_image = left_mbmi != NULL;
68
  const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
69
  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
  // The mode info data structure has a one element border above and to the
  // left of the entries corresponding to real macroblocks.
  // The prediction flags in these dummy entries are initialized to 0.
  // 0 - inter/inter, inter/--, --/inter, --/--
  // 1 - intra/inter, inter/intra
  // 2 - intra/--, --/intra
  // 3 - intra/intra
  if (above_in_image && left_in_image)  // both edges available
    return left_intra && above_intra ? 3
                                     : left_intra || above_intra;
  else if (above_in_image || left_in_image)  // one edge available
    return 2 * (above_in_image ? above_intra : left_intra);
  else
    return 0;
85
}
86
87
88
89
90
91
92
93

int vp9_get_reference_mode_context(const VP9_COMMON *cm,
                                   const MACROBLOCKD *xd) {
  int ctx;
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(get_above_mi(xd));
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(get_left_mi(xd));
  const int above_in_image = above_mbmi != NULL;
  const int left_in_image = left_mbmi != NULL;
94
95
96
97
98
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  if (above_in_image && left_in_image) {  // both edges available
99
    if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
100
      // neither edge uses comp pred (0/1)
101
102
      ctx = (above_mbmi->ref_frame[0] == cm->comp_fixed_ref) ^
            (left_mbmi->ref_frame[0] == cm->comp_fixed_ref);
103
    else if (!has_second_ref(above_mbmi))
104
      // one of two edges uses comp pred (2/3)
105
106
      ctx = 2 + (above_mbmi->ref_frame[0] == cm->comp_fixed_ref ||
                 !is_inter_block(above_mbmi));
107
    else if (!has_second_ref(left_mbmi))
108
      // one of two edges uses comp pred (2/3)
109
110
      ctx = 2 + (left_mbmi->ref_frame[0] == cm->comp_fixed_ref ||
                 !is_inter_block(left_mbmi));
111
    else  // both edges use comp pred (4)
112
      ctx = 4;
113
  } else if (above_in_image || left_in_image) {  // one edge available
114
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
115

116
    if (!has_second_ref(edge_mbmi))
117
      // edge does not use comp pred (0/1)
118
      ctx = edge_mbmi->ref_frame[0] == cm->comp_fixed_ref;
119
120
    else
      // edge uses comp pred (3)
121
      ctx = 3;
122
  } else {  // no edges available (1)
123
    ctx = 1;
124
  }
125
126
  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
  return ctx;
127
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
128

129
130
131
132
// Returns a context number for the given MB prediction signal
unsigned char vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
                                              const MACROBLOCKD *xd) {
  int pred_context;
133
134
135
136
137
138
  const MODE_INFO *const above_mi = get_above_mi(xd);
  const MODE_INFO *const left_mi = get_left_mi(xd);
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
  const int above_in_image = above_mi != NULL;
  const int left_in_image = left_mi != NULL;
139
  const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
140
  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
141
142
143
144
145
146
147
148
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
  const int var_ref_idx = !fix_ref_idx;

  if (above_in_image && left_in_image) {  // both edges available
149
    if (above_intra && left_intra) {  // intra/intra (2)
150
      pred_context = 2;
151
152
    } else if (above_intra || left_intra) {  // intra/inter
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
153

154
      if (!has_second_ref(edge_mbmi))  // single pred (1/3)
155
        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]);
156
      else  // comp pred (1/3)
157
158
        pred_context = 1 + 2 * (edge_mbmi->ref_frame[var_ref_idx]
                                    != cm->comp_var_ref[1]);
159
    } else {  // inter/inter
160
161
      const int l_sg = !has_second_ref(left_mbmi);
      const int a_sg = !has_second_ref(above_mbmi);
162
163
164
165
      MV_REFERENCE_FRAME vrfa = a_sg ? above_mbmi->ref_frame[0]
                                     : above_mbmi->ref_frame[var_ref_idx];
      MV_REFERENCE_FRAME vrfl = l_sg ? left_mbmi->ref_frame[0]
                                     : left_mbmi->ref_frame[var_ref_idx];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
166

167
168
169
      if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) {
        pred_context = 0;
      } else if (l_sg && a_sg) {  // single/single
170
171
        if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) ||
            (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0]))
Ronald S. Bultje's avatar
Ronald S. Bultje committed
172
          pred_context = 4;
173
        else if (vrfa == vrfl)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
174
          pred_context = 3;
175
        else
176
177
178
179
          pred_context = 1;
      } else if (l_sg || a_sg) {  // single/comp
        MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl;
        MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl;
180
        if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1])
181
          pred_context = 1;
182
        else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1])
Ronald S. Bultje's avatar
Ronald S. Bultje committed
183
          pred_context = 2;
184
        else
185
186
187
188
          pred_context = 4;
      } else if (vrfa == vrfl) {  // comp/comp
        pred_context = 4;
      } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
189
190
191
        pred_context = 2;
      }
    }
192
  } else if (above_in_image || left_in_image) {  // one edge available
193
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
194

195
    if (!is_inter_block(edge_mbmi)) {
196
      pred_context = 2;
197
198
199
    } else {
      if (has_second_ref(edge_mbmi))
        pred_context = 4 * (edge_mbmi->ref_frame[var_ref_idx]
200
                              != cm->comp_var_ref[1]);
201
202
203
      else
        pred_context = 3 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]);
    }
204
205
206
207
  } else {  // no edges available (2)
    pred_context = 2;
  }
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
208

209
210
  return pred_context;
}
211
unsigned char vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
212
  int pred_context;
213
214
215
216
217
218
  const MODE_INFO *const above_mi = get_above_mi(xd);
  const MODE_INFO *const left_mi = get_left_mi(xd);
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
  const int above_in_image = above_mi != NULL;
  const int left_in_image = left_mi != NULL;
219
  const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
220
  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
221
222
223
224
225
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  if (above_in_image && left_in_image) {  // both edges available
226
    if (above_intra && left_intra) {  // intra/intra
227
      pred_context = 2;
228
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
229
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
230
      if (!has_second_ref(edge_mbmi))
231
232
233
234
        pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
      else
        pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME ||
                            edge_mbmi->ref_frame[1] == LAST_FRAME);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    } else {  // inter/inter
      if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) {
        pred_context = 2 * (above_mbmi->ref_frame[0] == LAST_FRAME) +
                       2 * (left_mbmi->ref_frame[0] == LAST_FRAME);
      } else if (has_second_ref(above_mbmi) && has_second_ref(left_mbmi)) {
        pred_context = 1 + (above_mbmi->ref_frame[0] == LAST_FRAME ||
                            above_mbmi->ref_frame[1] == LAST_FRAME ||
                            left_mbmi->ref_frame[0] == LAST_FRAME ||
                            left_mbmi->ref_frame[1] == LAST_FRAME);
      } else {
        const MV_REFERENCE_FRAME rfs = !has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0];
        const MV_REFERENCE_FRAME crf1 = has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0];
        const MV_REFERENCE_FRAME crf2 = has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[1] : left_mbmi->ref_frame[1];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
251

252
253
254
255
256
        if (rfs == LAST_FRAME)
          pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
        else
          pred_context = crf1 == LAST_FRAME || crf2 == LAST_FRAME;
      }
257
258
    }
  } else if (above_in_image || left_in_image) {  // one edge available
259
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
260
    if (!is_inter_block(edge_mbmi)) {  // intra
261
      pred_context = 2;
262
263
264
265
266
267
268
269
    } else {  // inter
      if (!has_second_ref(edge_mbmi))
        pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
      else
        pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME ||
                            edge_mbmi->ref_frame[1] == LAST_FRAME);
    }
  } else {  // no edges available
270
271
    pred_context = 2;
  }
272

273
274
275
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  return pred_context;
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
276

277
unsigned char vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
278
  int pred_context;
279
280
281
282
283
284
  const MODE_INFO *const above_mi = get_above_mi(xd);
  const MODE_INFO *const left_mi = get_left_mi(xd);
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi);
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi);
  const int above_in_image = above_mi != NULL;
  const int left_in_image = left_mi != NULL;
285
  const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1;
286
  const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1;
287

288
289
290
291
292
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  if (above_in_image && left_in_image) {  // both edges available
293
    if (above_intra && left_intra) {  // intra/intra
294
      pred_context = 2;
295
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
296
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
297
      if (!has_second_ref(edge_mbmi)) {
298
        if (edge_mbmi->ref_frame[0] == LAST_FRAME)
299
          pred_context = 3;
300
301
        else
          pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME);
302
      } else {
303
304
        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME ||
                                edge_mbmi->ref_frame[1] == GOLDEN_FRAME);
305
      }
306
307
308
309
310
311
312
313
314
    } else {  // inter/inter
      if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) {
        if (above_mbmi->ref_frame[0] == LAST_FRAME &&
            left_mbmi->ref_frame[0] == LAST_FRAME) {
          pred_context = 3;
        } else if (above_mbmi->ref_frame[0] == LAST_FRAME ||
                   left_mbmi->ref_frame[0] == LAST_FRAME) {
          const MB_MODE_INFO *edge_mbmi =
              above_mbmi->ref_frame[0] == LAST_FRAME ? left_mbmi : above_mbmi;
315

316
317
318
319
320
321
322
323
324
325
326
327
328
329
          pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME);
        } else {
          pred_context = 2 * (above_mbmi->ref_frame[0] == GOLDEN_FRAME) +
                         2 * (left_mbmi->ref_frame[0] == GOLDEN_FRAME);
        }
      } else if (has_second_ref(above_mbmi) && has_second_ref(left_mbmi)) {
        if (above_mbmi->ref_frame[0] == left_mbmi->ref_frame[0] &&
            above_mbmi->ref_frame[1] == left_mbmi->ref_frame[1])
          pred_context = 3 * (above_mbmi->ref_frame[0] == GOLDEN_FRAME ||
                              above_mbmi->ref_frame[1] == GOLDEN_FRAME ||
                              left_mbmi->ref_frame[0] == GOLDEN_FRAME ||
                              left_mbmi->ref_frame[1] == GOLDEN_FRAME);
        else
          pred_context = 2;
330
      } else {
331
332
333
334
335
336
        const MV_REFERENCE_FRAME rfs = !has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0];
        const MV_REFERENCE_FRAME crf1 = has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0];
        const MV_REFERENCE_FRAME crf2 = has_second_ref(above_mbmi) ?
                  above_mbmi->ref_frame[1] : left_mbmi->ref_frame[1];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
337

338
339
340
341
342
343
344
        if (rfs == GOLDEN_FRAME)
          pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
        else if (rfs == ALTREF_FRAME)
          pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME;
        else
          pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
345
    }
346
  } else if (above_in_image || left_in_image) {  // one edge available
347
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
348

349
350
    if (!is_inter_block(edge_mbmi) ||
        (edge_mbmi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mbmi)))
351
      pred_context = 2;
352
    else if (!has_second_ref(edge_mbmi))
353
354
355
356
      pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME);
    else
      pred_context = 3 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME ||
                          edge_mbmi->ref_frame[1] == GOLDEN_FRAME);
357
358
  } else {  // no edges available (2)
    pred_context = 2;
John Koleszar's avatar
John Koleszar committed
359
  }
360
  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
John Koleszar's avatar
John Koleszar committed
361
  return pred_context;
362
}
363
// Returns a context number for the given MB prediction signal
364
365
366
// The mode info data structure has a one element border above and to the
// left of the entries corresponding to real blocks.
// The prediction flags in these dummy entries are initialized to 0.
367
int vp9_get_tx_size_context(const MACROBLOCKD *xd) {
368
  const int max_tx_size = max_txsize_lookup[xd->mi_8x8[0]->mbmi.sb_type];
369
370
371
372
373
374
375
376
377
378
  const MB_MODE_INFO *const above_mbmi = get_above_mbmi(get_above_mi(xd));
  const MB_MODE_INFO *const left_mbmi = get_left_mbmi(get_left_mi(xd));
  const int has_above = above_mbmi != NULL;
  const int has_left = left_mbmi != NULL;
  int above_ctx = (has_above && !above_mbmi->skip_coeff) ? above_mbmi->tx_size
                                                         : max_tx_size;
  int left_ctx = (has_left && !left_mbmi->skip_coeff) ? left_mbmi->tx_size
                                                      : max_tx_size;
  if (!has_left)
    left_ctx = above_ctx;
379

380
381
  if (!has_above)
    above_ctx = left_ctx;
382

383
  return (above_ctx + left_ctx) > max_tx_size;
384
385
}

386
int vp9_get_segment_id(VP9_COMMON *cm, const uint8_t *segment_ids,
387
                       BLOCK_SIZE bsize, int mi_row, int mi_col) {
388
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
389
390
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
391
  const int xmis = MIN(cm->mi_cols - mi_col, bw);
392
393
  const int ymis = MIN(cm->mi_rows - mi_row, bh);
  int x, y, segment_id = INT_MAX;
394

395
396
397
398
399
  for (y = 0; y < ymis; y++)
    for (x = 0; x < xmis; x++)
      segment_id = MIN(segment_id,
                       segment_ids[mi_offset + y * cm->mi_cols + x]);

Paul Wilkins's avatar
Paul Wilkins committed
400
  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
401
  return segment_id;
402
}