vp9_tokenize.c 28 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
14
15
 */


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
16
17
#include "vp9_onyx_int.h"
#include "vp9_tokenize.h"
John Koleszar's avatar
John Koleszar committed
18
19
#include "vpx_mem/vpx_mem.h"

20
21
22
#include "vp9/common/vp9_pred_common.h"
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_entropy.h"
23

John Koleszar's avatar
John Koleszar committed
24
/* Global event counters used for accumulating statistics across several
25
   compressions, then generating vp9_context.c = initial stats. */
John Koleszar's avatar
John Koleszar committed
26
27

#ifdef ENTROPY_STATS
28
INT64 context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
29
30
INT64 hybrid_context_counters[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];

31
INT64 context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
32
33
INT64 hybrid_context_counters_8x8[BLOCK_TYPES_8X8] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];

Daniel Kang's avatar
Daniel Kang committed
34
INT64 context_counters_16x16[BLOCK_TYPES_16X16] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];
35
36
INT64 hybrid_context_counters_16x16[BLOCK_TYPES_16X16] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS];

Daniel Kang's avatar
Daniel Kang committed
37
38
extern unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
39
40
extern unsigned int hybrid_tree_update_hist[BLOCK_TYPES][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
Daniel Kang's avatar
Daniel Kang committed
41
42
extern unsigned int tree_update_hist_8x8[BLOCK_TYPES_8X8][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
43
44
extern unsigned int hybrid_tree_update_hist_8x8[BLOCK_TYPES_8X8][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
Daniel Kang's avatar
Daniel Kang committed
45
46
extern unsigned int tree_update_hist_16x16[BLOCK_TYPES_16X16][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
47
48
extern unsigned int hybrid_tree_update_hist_16x16[BLOCK_TYPES_16X16][COEF_BANDS]
                    [PREV_COEF_CONTEXTS][ENTROPY_NODES] [2];
49
50
#endif  /* ENTROPY_STATS */

John Koleszar's avatar
John Koleszar committed
51
static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2];
52
const TOKENVALUE *vp9_dct_value_tokens_ptr;
John Koleszar's avatar
John Koleszar committed
53
static int dct_value_cost[DCT_MAX_VALUE * 2];
54
const int *vp9_dct_value_cost_ptr;
55

John Koleszar's avatar
John Koleszar committed
56
static void fill_value_tokens() {
John Koleszar's avatar
John Koleszar committed
57

John Koleszar's avatar
John Koleszar committed
58
  TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE;
59
  vp9_extra_bit_struct *const e = vp9_extra_bits;
John Koleszar's avatar
John Koleszar committed
60

John Koleszar's avatar
John Koleszar committed
61
62
  int i = -DCT_MAX_VALUE;
  int sign = 1;
John Koleszar's avatar
John Koleszar committed
63

John Koleszar's avatar
John Koleszar committed
64
65
66
  do {
    if (!i)
      sign = 0;
John Koleszar's avatar
John Koleszar committed
67

John Koleszar's avatar
John Koleszar committed
68
69
70
    {
      const int a = sign ? -i : i;
      int eb = sign;
John Koleszar's avatar
John Koleszar committed
71

John Koleszar's avatar
John Koleszar committed
72
73
      if (a > 4) {
        int j = 4;
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75
        while (++j < 11  &&  e[j].base_val <= a) {}
John Koleszar's avatar
John Koleszar committed
76

John Koleszar's avatar
John Koleszar committed
77
78
79
80
        t[i].Token = --j;
        eb |= (a - e[j].base_val) << 1;
      } else
        t[i].Token = a;
John Koleszar's avatar
John Koleszar committed
81

John Koleszar's avatar
John Koleszar committed
82
83
      t[i].Extra = eb;
    }
John Koleszar's avatar
John Koleszar committed
84

John Koleszar's avatar
John Koleszar committed
85
86
87
    // initialize the cost for extra bits for all possible coefficient value.
    {
      int cost = 0;
88
      vp9_extra_bit_struct *p = vp9_extra_bits + t[i].Token;
John Koleszar's avatar
John Koleszar committed
89

John Koleszar's avatar
John Koleszar committed
90
91
92
      if (p->base_val) {
        const int extra = t[i].Extra;
        const int Length = p->Len;
John Koleszar's avatar
John Koleszar committed
93

John Koleszar's avatar
John Koleszar committed
94
        if (Length)
95
          cost += treed_cost(p->tree, p->prob, extra >> 1, Length);
John Koleszar's avatar
John Koleszar committed
96

97
        cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */
John Koleszar's avatar
John Koleszar committed
98
99
        dct_value_cost[i + DCT_MAX_VALUE] = cost;
      }
John Koleszar's avatar
John Koleszar committed
100
101
102

    }

John Koleszar's avatar
John Koleszar committed
103
104
  } while (++i < DCT_MAX_VALUE);

105
106
  vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE;
  vp9_dct_value_cost_ptr   = dct_value_cost + DCT_MAX_VALUE;
John Koleszar's avatar
John Koleszar committed
107
108
}

109
static void tokenize_b(VP9_COMP *cpi,
110
111
112
113
114
115
116
117
                       MACROBLOCKD *xd,
                       const BLOCKD * const b,
                       TOKENEXTRA **tp,
                       PLANE_TYPE type,
                       ENTROPY_CONTEXT *a,
                       ENTROPY_CONTEXT *l,
                       TX_SIZE tx_size,
                       int dry_run) {
Daniel Kang's avatar
Daniel Kang committed
118
  int pt; /* near block/prev token context index */
119
  int c = (type == PLANE_TYPE_Y_NO_DC) ? 1 : 0;
Daniel Kang's avatar
Daniel Kang committed
120
121
122
  const int eob = b->eob;     /* one beyond last nonzero coeff */
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  const short *qcoeff_ptr = b->qcoeff;
123
  int seg_eob;
Daniel Kang's avatar
Daniel Kang committed
124
  int segment_id = xd->mode_info_context->mbmi.segment_id;
125
126
  const int *bands, *scan;
  unsigned int (*counts)[COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
127
  vp9_prob (*probs)[COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
128
129
  const TX_TYPE tx_type = (type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, b) : DCT_DCT;
Daniel Kang's avatar
Daniel Kang committed
130

131
  VP9_COMBINEENTROPYCONTEXTS(pt, *a, *l);
132
133
134
135
  switch (tx_size) {
    default:
    case TX_4X4:
      seg_eob = 16;
136
137
      bands = vp9_coef_bands;
      scan = vp9_default_zig_zag1d;
138
139
140
141
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts;
        probs = cpi->common.fc.hybrid_coef_probs;
        if (tx_type == ADST_DCT) {
142
          scan = vp9_row_scan;
143
        } else if (tx_type == DCT_ADST) {
144
          scan = vp9_col_scan;
145
146
147
148
149
150
151
152
153
        }
      } else {
        counts = cpi->coef_counts;
        probs = cpi->common.fc.coef_probs;
      }
      break;
    case TX_8X8:
      if (type == PLANE_TYPE_Y2) {
        seg_eob = 4;
154
155
        bands = vp9_coef_bands;
        scan = vp9_default_zig_zag1d;
156
157
      } else {
        seg_eob = 64;
158
159
        bands = vp9_coef_bands_8x8;
        scan = vp9_default_zig_zag1d_8x8;
160
161
162
163
164
165
166
167
168
169
170
      }
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_8x8;
        probs = cpi->common.fc.hybrid_coef_probs_8x8;
      } else {
        counts = cpi->coef_counts_8x8;
        probs = cpi->common.fc.coef_probs_8x8;
      }
      break;
    case TX_16X16:
      seg_eob = 256;
171
172
      bands = vp9_coef_bands_16x16;
      scan = vp9_default_zig_zag1d_16x16;
173
174
175
176
177
178
179
180
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_16x16;
        probs = cpi->common.fc.hybrid_coef_probs_16x16;
      } else {
        counts = cpi->coef_counts_16x16;
        probs = cpi->common.fc.coef_probs_16x16;
      }
      break;
John Koleszar's avatar
John Koleszar committed
181
182
  }

183
184
  if (vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB))
    seg_eob = vp9_get_segdata(xd, segment_id, SEG_LVL_EOB);
John Koleszar's avatar
John Koleszar committed
185

186
  do {
187
    const int band = bands[c];
188
189
190
    int token;

    if (c < eob) {
191
      const int rc = scan[c];
192
      const int v = qcoeff_ptr[rc];
John Koleszar's avatar
John Koleszar committed
193

194
195
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

196
197
      t->Extra = vp9_dct_value_tokens_ptr[v].Extra;
      token    = vp9_dct_value_tokens_ptr[v].Token;
198
199
200
    } else {
      token = DCT_EOB_TOKEN;
    }
John Koleszar's avatar
John Koleszar committed
201
202

    t->Token = token;
203
204
205
    t->context_tree = probs[type][band][pt];
    t->skip_eob_node = (pt == 0) && ((band > 0 && type != PLANE_TYPE_Y_NO_DC) ||
                                     (band > 1 && type == PLANE_TYPE_Y_NO_DC));
206
    assert(vp9_coef_encodings[t->Token].Len - t->skip_eob_node > 0);
207
    if (!dry_run) {
208
      ++counts[type][band][pt][token];
209
    }
210
    pt = vp9_prev_token_class[token];
211
    ++t;
212
213
214
  } while (c < eob && ++c < seg_eob);

  *tp = t;
215
  *a = *l = (c != !type); /* 0 <-> all coeff data is zero */
John Koleszar's avatar
John Koleszar committed
216
217
}

218
int vp9_mby_is_skippable_4x4(MACROBLOCKD *xd, int has_y2_block) {
John Koleszar's avatar
John Koleszar committed
219
220
  int skip = 1;
  int i = 0;
221

John Koleszar's avatar
John Koleszar committed
222
223
  if (has_y2_block) {
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
224
225
      skip &= (xd->block[i].eob < 2);
    skip &= (!xd->block[24].eob);
John Koleszar's avatar
John Koleszar committed
226
227
  } else {
    for (i = 0; i < 16; i++)
Paul Wilkins's avatar
Paul Wilkins committed
228
      skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
229
230
  }
  return skip;
231
}
232

233
int vp9_mbuv_is_skippable_4x4(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
234
235
  int skip = 1;
  int i;
236

John Koleszar's avatar
John Koleszar committed
237
  for (i = 16; i < 24; i++)
Paul Wilkins's avatar
Paul Wilkins committed
238
    skip &= (!xd->block[i].eob);
John Koleszar's avatar
John Koleszar committed
239
  return skip;
240
241
}

242
243
244
static int mb_is_skippable_4x4(MACROBLOCKD *xd, int has_y2_block) {
  return (vp9_mby_is_skippable_4x4(xd, has_y2_block) &
          vp9_mbuv_is_skippable_4x4(xd));
245
}
Yaowu Xu's avatar
Yaowu Xu committed
246

247
int vp9_mby_is_skippable_8x8(MACROBLOCKD *xd, int has_y2_block) {
John Koleszar's avatar
John Koleszar committed
248
249
  int skip = 1;
  int i = 0;
250

Deb Mukherjee's avatar
Deb Mukherjee committed
251
252
253
254
255
256
257
258
  if (has_y2_block) {
    for (i = 0; i < 16; i += 4)
      skip &= (xd->block[i].eob < 2);
    skip &= (!xd->block[24].eob);
  } else {
    for (i = 0; i < 16; i += 4)
      skip &= (!xd->block[i].eob);
  }
John Koleszar's avatar
John Koleszar committed
259
  return skip;
260
}
261

262
int vp9_mbuv_is_skippable_8x8(MACROBLOCKD *xd) {
Paul Wilkins's avatar
Paul Wilkins committed
263
  return (!xd->block[16].eob) & (!xd->block[20].eob);
264
}
265

266
267
268
static int mb_is_skippable_8x8(MACROBLOCKD *xd, int has_y2_block) {
  return (vp9_mby_is_skippable_8x8(xd, has_y2_block) &
          vp9_mbuv_is_skippable_8x8(xd));
Deb Mukherjee's avatar
Deb Mukherjee committed
269
270
}

271
272
273
static int mb_is_skippable_8x8_4x4uv(MACROBLOCKD *xd, int has_y2_block) {
  return (vp9_mby_is_skippable_8x8(xd, has_y2_block) &
          vp9_mbuv_is_skippable_4x4(xd));
274
}
Yaowu Xu's avatar
Yaowu Xu committed
275

276
int vp9_mby_is_skippable_16x16(MACROBLOCKD *xd) {
Daniel Kang's avatar
Daniel Kang committed
277
  int skip = 1;
Paul Wilkins's avatar
Paul Wilkins committed
278
  skip &= !xd->block[0].eob;
Daniel Kang's avatar
Daniel Kang committed
279
280
281
  return skip;
}

282
283
static int mb_is_skippable_16x16(MACROBLOCKD *xd) {
  return (vp9_mby_is_skippable_16x16(xd) & vp9_mbuv_is_skippable_8x8(xd));
Daniel Kang's avatar
Daniel Kang committed
284
}
285

286
void vp9_tokenize_mb(VP9_COMP *cpi,
Paul Wilkins's avatar
Paul Wilkins committed
287
                     MACROBLOCKD *xd,
288
289
                     TOKENEXTRA **t,
                     int dry_run) {
290
  PLANE_TYPE plane_type;
John Koleszar's avatar
John Koleszar committed
291
292
  int has_y2_block;
  int b;
293
  int tx_size = xd->mode_info_context->mbmi.txfm_size;
Paul Wilkins's avatar
Paul Wilkins committed
294
  int mb_skip_context = vp9_get_pred_context(&cpi->common, xd, PRED_MBSKIP);
295
  TOKENEXTRA *t_backup = *t;
296
297
  ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *) xd->above_context;
  ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *) xd->left_context;
Paul Wilkins's avatar
Paul Wilkins committed
298

John Koleszar's avatar
John Koleszar committed
299
300
301
302
  // If the MB is going to be skipped because of a segment level flag
  // exclude this from the skip count stats used to calculate the
  // transmitted skip probability;
  int skip_inc;
Paul Wilkins's avatar
Paul Wilkins committed
303
  int segment_id = xd->mode_info_context->mbmi.segment_id;
304

305
306
  if (!vp9_segfeature_active(xd, segment_id, SEG_LVL_EOB) ||
      (vp9_get_segdata(xd, segment_id, SEG_LVL_EOB) != 0)) {
John Koleszar's avatar
John Koleszar committed
307
308
309
    skip_inc = 1;
  } else
    skip_inc = 0;
310

311
312
  has_y2_block = (tx_size != TX_16X16
                  && xd->mode_info_context->mbmi.mode != B_PRED
Paul Wilkins's avatar
Paul Wilkins committed
313
314
                  && xd->mode_info_context->mbmi.mode != I8X8_PRED
                  && xd->mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
315

316
  switch (tx_size) {
Daniel Kang's avatar
Daniel Kang committed
317
    case TX_16X16:
Daniel Kang's avatar
Daniel Kang committed
318
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_16x16(xd);
Daniel Kang's avatar
Daniel Kang committed
319
320
      break;
    case TX_8X8:
321
322
      if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
          xd->mode_info_context->mbmi.mode == SPLITMV)
Deb Mukherjee's avatar
Deb Mukherjee committed
323
324
        xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_8x8_4x4uv(xd, 0);
      else
325
        xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_8x8(xd, has_y2_block);
Daniel Kang's avatar
Daniel Kang committed
326
      break;
Deb Mukherjee's avatar
Deb Mukherjee committed
327

Daniel Kang's avatar
Daniel Kang committed
328
    default:
329
      xd->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable_4x4(xd, has_y2_block);
Daniel Kang's avatar
Daniel Kang committed
330
331
      break;
  }
332

Paul Wilkins's avatar
Paul Wilkins committed
333
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
334
335
    if (!dry_run)
      cpi->skip_true_count[mb_skip_context] += skip_inc;
John Koleszar's avatar
John Koleszar committed
336
    if (!cpi->common.mb_no_coeff_skip) {
337
      vp9_stuff_mb(cpi, xd, t, dry_run);
John Koleszar's avatar
John Koleszar committed
338
    } else {
339
      vp9_fix_contexts(xd);
John Koleszar's avatar
John Koleszar committed
340
    }
341
342
    if (dry_run)
      *t = t_backup;
John Koleszar's avatar
John Koleszar committed
343
344
345
    return;
  }

346
347
  if (!dry_run)
    cpi->skip_false_count[mb_skip_context] += skip_inc;
348

John Koleszar's avatar
John Koleszar committed
349
  if (has_y2_block) {
350
    if (tx_size == TX_8X8) {
351
      tokenize_b(cpi, xd, xd->block + 24, t, PLANE_TYPE_Y2,
352
                 A + vp9_block2above_8x8[24], L + vp9_block2left_8x8[24],
353
354
355
                 TX_8X8, dry_run);
    } else {
      tokenize_b(cpi, xd, xd->block + 24, t, PLANE_TYPE_Y2,
356
                 A + vp9_block2above[24], L + vp9_block2left[24],
357
358
                 TX_4X4, dry_run);
    }
John Koleszar's avatar
John Koleszar committed
359

360
361
362
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else
    plane_type = PLANE_TYPE_Y_WITH_DC;
John Koleszar's avatar
John Koleszar committed
363

364
  if (tx_size == TX_16X16) {
365
366
    tokenize_b(cpi, xd, xd->block, t, PLANE_TYPE_Y_WITH_DC,
               A, L, TX_16X16, dry_run);
367
368
    A[1] = A[2] = A[3] = A[0];
    L[1] = L[2] = L[3] = L[0];
369

Daniel Kang's avatar
Daniel Kang committed
370
    for (b = 16; b < 24; b += 4) {
371
      tokenize_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV,
372
                 A + vp9_block2above_8x8[b], L + vp9_block2left_8x8[b],
373
                 TX_8X8, dry_run);
374
375
      A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
      L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
Daniel Kang's avatar
Daniel Kang committed
376
377
378
    }
    vpx_memset(&A[8], 0, sizeof(A[8]));
    vpx_memset(&L[8], 0, sizeof(L[8]));
379
  } else if (tx_size == TX_8X8) {
John Koleszar's avatar
John Koleszar committed
380
    for (b = 0; b < 16; b += 4) {
381
      tokenize_b(cpi, xd, xd->block + b, t, plane_type,
382
                 A + vp9_block2above_8x8[b], L + vp9_block2left_8x8[b],
383
                 TX_8X8, dry_run);
384
385
      A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
      L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
John Koleszar's avatar
John Koleszar committed
386
    }
387
388
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
389
390
      for (b = 16; b < 24; b++) {
        tokenize_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV,
391
                   A + vp9_block2above[b], L + vp9_block2left[b],
392
393
                   TX_4X4, dry_run);
      }
394
    } else {
Deb Mukherjee's avatar
Deb Mukherjee committed
395
      for (b = 16; b < 24; b += 4) {
396
        tokenize_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV,
397
                   A + vp9_block2above_8x8[b], L + vp9_block2left_8x8[b],
398
                   TX_8X8, dry_run);
399
400
        A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
        L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
Deb Mukherjee's avatar
Deb Mukherjee committed
401
      }
402
    }
Jingning Han's avatar
Jingning Han committed
403
  } else {
404
405
    for (b = 0; b < 16; b++) {
      tokenize_b(cpi, xd, xd->block + b, t, plane_type,
406
                 A + vp9_block2above[b], L + vp9_block2left[b],
407
408
409
410
411
                 TX_4X4, dry_run);
    }

    for (b = 16; b < 24; b++) {
      tokenize_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV,
412
                 A + vp9_block2above[b], L + vp9_block2left[b],
413
414
                 TX_4X4, dry_run);
    }
Jingning Han's avatar
Jingning Han committed
415
  }
416
417
  if (dry_run)
    *t = t_backup;
John Koleszar's avatar
John Koleszar committed
418
}
419

John Koleszar's avatar
John Koleszar committed
420
421

#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
422
423
424
425
426
void init_context_counters(void) {
  FILE *f = fopen("context.bin", "rb");
  if (!f) {
    vpx_memset(context_counters, 0, sizeof(context_counters));
    vpx_memset(context_counters_8x8, 0, sizeof(context_counters_8x8));
Daniel Kang's avatar
Daniel Kang committed
427
    vpx_memset(context_counters_16x16, 0, sizeof(context_counters_16x16));
John Koleszar's avatar
John Koleszar committed
428
429
430
  } else {
    fread(context_counters, sizeof(context_counters), 1, f);
    fread(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
431
    fread(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
John Koleszar's avatar
John Koleszar committed
432
433
434
435
436
437
438
    fclose(f);
  }

  f = fopen("treeupdate.bin", "rb");
  if (!f) {
    vpx_memset(tree_update_hist, 0, sizeof(tree_update_hist));
    vpx_memset(tree_update_hist_8x8, 0, sizeof(tree_update_hist_8x8));
Daniel Kang's avatar
Daniel Kang committed
439
    vpx_memset(tree_update_hist_16x16, 0, sizeof(tree_update_hist_16x16));
John Koleszar's avatar
John Koleszar committed
440
441
442
  } else {
    fread(tree_update_hist, sizeof(tree_update_hist), 1, f);
    fread(tree_update_hist_8x8, sizeof(tree_update_hist_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
443
    fread(tree_update_hist_16x16, sizeof(tree_update_hist_16x16), 1, f);
John Koleszar's avatar
John Koleszar committed
444
445
    fclose(f);
  }
John Koleszar's avatar
John Koleszar committed
446
447
}

John Koleszar's avatar
John Koleszar committed
448
449
void print_context_counters() {
  int type, band, pt, t;
450
  FILE *f = fopen("vp9_context.c", "w");
John Koleszar's avatar
John Koleszar committed
451

452
  fprintf(f, "#include \"vp9_entropy.h\"\n");
John Koleszar's avatar
John Koleszar committed
453
454
  fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n");
  fprintf(f, "static const unsigned int\n"
455
          "vp9_default_coef_counts[BLOCK_TYPES]\n"
John Koleszar's avatar
John Koleszar committed
456
457
458
          "                      [COEF_BANDS]\n"
          "                      [PREV_COEF_CONTEXTS]\n"
          "                      [MAX_ENTROPY_TOKENS]={\n");
John Koleszar's avatar
John Koleszar committed
459
460

# define Comma( X) (X? ",":"")
John Koleszar's avatar
John Koleszar committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        fprintf(f, "%s\n      {", Comma(pt));

        t = 0;
        do {
          const INT64 x = context_counters [type] [band] [pt] [t];
          const int y = (int) x;
          assert(x == (INT64) y);  /* no overflow handling yet */
          fprintf(f, "%s %d", Comma(t), y);
        } while (++t < MAX_ENTROPY_TOKENS);
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES);
  fprintf(f, "\n};\n");
485

486
  fprintf(f, "static const unsigned int\nvp9_default_coef_counts_8x8"
John Koleszar's avatar
John Koleszar committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
          "[BLOCK_TYPES_8X8] [COEF_BANDS]"
          "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        fprintf(f, "%s\n      {", Comma(pt));
        t = 0;
        do {
          const INT64 x = context_counters_8x8 [type] [band] [pt] [t];
          const int y = (int) x;
502

John Koleszar's avatar
John Koleszar committed
503
504
          assert(x == (INT64) y);  /* no overflow handling yet */
          fprintf(f, "%s %d", Comma(t), y);
John Koleszar's avatar
John Koleszar committed
505

John Koleszar's avatar
John Koleszar committed
506
        } while (++t < MAX_ENTROPY_TOKENS);
507

John Koleszar's avatar
John Koleszar committed
508
509
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
510

John Koleszar's avatar
John Koleszar committed
511
      fprintf(f, "\n    }");
512

John Koleszar's avatar
John Koleszar committed
513
    } while (++band < COEF_BANDS);
514

John Koleszar's avatar
John Koleszar committed
515
516
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES_8X8);
Daniel Kang's avatar
Daniel Kang committed
517
  fprintf(f, "\n};\n");
518

519
  fprintf(f, "static const unsigned int\nvp9_default_coef_counts_16x16"
Daniel Kang's avatar
Daniel Kang committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
          "[BLOCK_TYPES_16X16] [COEF_BANDS]"
          "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {");
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        fprintf(f, "%s\n      {", Comma(pt));
        t = 0;
        do {
          const INT64 x = context_counters_16x16 [type] [band] [pt] [t];
          const int y = (int) x;

          assert(x == (INT64) y);  /* no overflow handling yet */
          fprintf(f, "%s %d", Comma(t), y);

        } while (++t < MAX_ENTROPY_TOKENS);

        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);

      fprintf(f, "\n    }");

    } while (++band < COEF_BANDS);

    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES_16X16);
John Koleszar's avatar
John Koleszar committed
550
  fprintf(f, "\n};\n");
551

552
  fprintf(f, "static const vp9_prob\n"
553
          "vp9_default_coef_probs[BLOCK_TYPES] [COEF_BANDS] \n"
John Koleszar's avatar
John Koleszar committed
554
555
556
557
558
559
560
561
562
563
564
          "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {");
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        unsigned int branch_ct [ENTROPY_NODES] [2];
        unsigned int coef_counts[MAX_ENTROPY_TOKENS];
565
        vp9_prob coef_probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
566
567
        for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
          coef_counts[t] = context_counters [type] [band] [pt] [t];
568
        vp9_tree_probs_from_distribution(
569
          MAX_ENTROPY_TOKENS, vp9_coef_encodings, vp9_coef_tree,
John Koleszar's avatar
John Koleszar committed
570
571
          coef_probs, branch_ct, coef_counts, 256, 1);
        fprintf(f, "%s\n      {", Comma(pt));
572

John Koleszar's avatar
John Koleszar committed
573
574
575
        t = 0;
        do {
          fprintf(f, "%s %d", Comma(t), coef_probs[t]);
576

John Koleszar's avatar
John Koleszar committed
577
        } while (++t < ENTROPY_NODES);
578

John Koleszar's avatar
John Koleszar committed
579
580
581
582
583
584
585
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES);
  fprintf(f, "\n};\n");
586

587
  fprintf(f, "static const vp9_prob\n"
588
          "vp9_default_coef_probs_8x8[BLOCK_TYPES_8X8] [COEF_BANDS]\n"
John Koleszar's avatar
John Koleszar committed
589
590
591
592
593
594
595
596
597
598
599
          "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {");
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        unsigned int branch_ct [ENTROPY_NODES] [2];
        unsigned int coef_counts[MAX_ENTROPY_TOKENS];
600
        vp9_prob coef_probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
601
602
        for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
          coef_counts[t] = context_counters_8x8[type] [band] [pt] [t];
603
        vp9_tree_probs_from_distribution(
604
          MAX_ENTROPY_TOKENS, vp9_coef_encodings, vp9_coef_tree,
John Koleszar's avatar
John Koleszar committed
605
606
          coef_probs, branch_ct, coef_counts, 256, 1);
        fprintf(f, "%s\n      {", Comma(pt));
607

Daniel Kang's avatar
Daniel Kang committed
608
        t = 0;
John Koleszar's avatar
John Koleszar committed
609
610
611
612
613
614
615
616
617
618
619
        do {
          fprintf(f, "%s %d", Comma(t), coef_probs[t]);
        } while (++t < ENTROPY_NODES);
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES_8X8);
  fprintf(f, "\n};\n");

620
  fprintf(f, "static const vp9_prob\n"
621
          "vp9_default_coef_probs_16x16[BLOCK_TYPES_16X16] [COEF_BANDS]\n"
Daniel Kang's avatar
Daniel Kang committed
622
623
624
625
626
627
628
629
630
631
632
          "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {");
  type = 0;
  do {
    fprintf(f, "%s\n  { /* block Type %d */", Comma(type), type);
    band = 0;
    do {
      fprintf(f, "%s\n    { /* Coeff Band %d */", Comma(band), band);
      pt = 0;
      do {
        unsigned int branch_ct [ENTROPY_NODES] [2];
        unsigned int coef_counts[MAX_ENTROPY_TOKENS];
633
        vp9_prob coef_probs[ENTROPY_NODES];
Daniel Kang's avatar
Daniel Kang committed
634
635
        for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
          coef_counts[t] = context_counters_16x16[type] [band] [pt] [t];
636
        vp9_tree_probs_from_distribution(
637
          MAX_ENTROPY_TOKENS, vp9_coef_encodings, vp9_coef_tree,
Daniel Kang's avatar
Daniel Kang committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
          coef_probs, branch_ct, coef_counts, 256, 1);
        fprintf(f, "%s\n      {", Comma(pt));

        t = 0;
        do {
          fprintf(f, "%s %d", Comma(t), coef_probs[t]);
        } while (++t < ENTROPY_NODES);
        fprintf(f, "}");
      } while (++pt < PREV_COEF_CONTEXTS);
      fprintf(f, "\n    }");
    } while (++band < COEF_BANDS);
    fprintf(f, "\n  }");
  } while (++type < BLOCK_TYPES_16X16);
  fprintf(f, "\n};\n");

John Koleszar's avatar
John Koleszar committed
653
654
655
656
657
  fclose(f);

  f = fopen("context.bin", "wb");
  fwrite(context_counters, sizeof(context_counters), 1, f);
  fwrite(context_counters_8x8, sizeof(context_counters_8x8), 1, f);
Daniel Kang's avatar
Daniel Kang committed
658
  fwrite(context_counters_16x16, sizeof(context_counters_16x16), 1, f);
John Koleszar's avatar
John Koleszar committed
659
  fclose(f);
John Koleszar's avatar
John Koleszar committed
660
661
662
}
#endif

663
void vp9_tokenize_initialize() {
John Koleszar's avatar
John Koleszar committed
664
  fill_value_tokens();
John Koleszar's avatar
John Koleszar committed
665
666
}

667
static __inline void stuff_b(VP9_COMP *cpi,
668
669
670
671
672
673
674
675
676
677
                             MACROBLOCKD *xd,
                             const BLOCKD * const b,
                             TOKENEXTRA **tp,
                             PLANE_TYPE type,
                             ENTROPY_CONTEXT *a,
                             ENTROPY_CONTEXT *l,
                             TX_SIZE tx_size,
                             int dry_run) {
  const int *bands;
  unsigned int (*counts)[COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS];
678
  vp9_prob (*probs)[COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES];
679
680
681
682
  int pt, band;
  TOKENEXTRA *t = *tp;
  const TX_TYPE tx_type = (type == PLANE_TYPE_Y_WITH_DC) ?
                          get_tx_type(xd, b) : DCT_DCT;
683
  VP9_COMBINEENTROPYCONTEXTS(pt, *a, *l);
John Koleszar's avatar
John Koleszar committed
684

685
686
687
  switch (tx_size) {
    default:
    case TX_4X4:
688
      bands = vp9_coef_bands;
689
690
691
692
693
694
695
696
697
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts;
        probs = cpi->common.fc.hybrid_coef_probs;
      } else {
        counts = cpi->coef_counts;
        probs = cpi->common.fc.coef_probs;
      }
      break;
    case TX_8X8:
698
      bands = vp9_coef_bands_8x8;
699
700
701
702
703
704
705
706
707
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_8x8;
        probs = cpi->common.fc.hybrid_coef_probs_8x8;
      } else {
        counts = cpi->coef_counts_8x8;
        probs = cpi->common.fc.coef_probs_8x8;
      }
      break;
    case TX_16X16:
708
      bands = vp9_coef_bands_16x16;
709
710
711
712
713
714
715
716
717
718
      if (tx_type != DCT_DCT) {
        counts = cpi->hybrid_coef_counts_16x16;
        probs = cpi->common.fc.hybrid_coef_probs_16x16;
      } else {
        counts = cpi->coef_counts_16x16;
        probs = cpi->common.fc.coef_probs_16x16;
      }
      break;
  }
  band = bands[(type == PLANE_TYPE_Y_NO_DC) ? 1 : 0];
John Koleszar's avatar
John Koleszar committed
719
  t->Token = DCT_EOB_TOKEN;
720
  t->context_tree = probs[type][band][pt];
John Koleszar's avatar
John Koleszar committed
721
722
723
  t->skip_eob_node = 0;
  ++t;
  *tp = t;
724
  *a = *l = 0;
725
  if (!dry_run) {
726
    ++counts[type][band][pt][DCT_EOB_TOKEN];
727
  }
728
729
}

730
731
static void stuff_mb_8x8(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
Paul Wilkins's avatar
Paul Wilkins committed
732
733
  ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
  ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
734
  PLANE_TYPE plane_type;
John Koleszar's avatar
John Koleszar committed
735
  int b;
736
737
738
  const int has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED &&
                            xd->mode_info_context->mbmi.mode != I8X8_PRED &&
                            xd->mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
739

740
  if (has_y2_block) {
741
    stuff_b(cpi, xd, xd->block + 24, t, PLANE_TYPE_Y2,
742
            A + vp9_block2above_8x8[24], L + vp9_block2left_8x8[24],
743
            TX_8X8, dry_run);
744
745
746
747
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else {
    plane_type = PLANE_TYPE_Y_WITH_DC;
  }
John Koleszar's avatar
John Koleszar committed
748
749

  for (b = 0; b < 16; b += 4) {
750
751
752
753
    stuff_b(cpi, xd, xd->block + b, t, plane_type, A + vp9_block2above_8x8[b],
            L + vp9_block2left_8x8[b], TX_8X8, dry_run);
    A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
    L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
John Koleszar's avatar
John Koleszar committed
754
755
756
  }

  for (b = 16; b < 24; b += 4) {
757
    stuff_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV,
758
            A + vp9_block2above_8x8[b], L + vp9_block2left_8x8[b],
759
            TX_8X8, dry_run);
760
761
    A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
    L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
John Koleszar's avatar
John Koleszar committed
762
  }
Daniel Kang's avatar
Daniel Kang committed
763
764
}

765
766
static void stuff_mb_16x16(VP9_COMP *cpi, MACROBLOCKD *xd,
                           TOKENEXTRA **t, int dry_run) {
Paul Wilkins's avatar
Paul Wilkins committed
767
768
  ENTROPY_CONTEXT * A = (ENTROPY_CONTEXT *)xd->above_context;
  ENTROPY_CONTEXT * L = (ENTROPY_CONTEXT *)xd->left_context;
769
  int b;
Daniel Kang's avatar
Daniel Kang committed
770

771
  stuff_b(cpi, xd, xd->block, t, PLANE_TYPE_Y_WITH_DC, A, L, TX_16X16, dry_run);
772
773
  A[1] = A[2] = A[3] = A[0];
  L[1] = L[2] = L[3] = L[0];
Daniel Kang's avatar
Daniel Kang committed
774
  for (b = 16; b < 24; b += 4) {
775
776
777
778
    stuff_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV, A + vp9_block2above[b],
            L + vp9_block2above_8x8[b], TX_8X8, dry_run);
    A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
    L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
Daniel Kang's avatar
Daniel Kang committed
779
780
781
  }
  vpx_memset(&A[8], 0, sizeof(A[8]));
  vpx_memset(&L[8], 0, sizeof(L[8]));
John Koleszar's avatar
John Koleszar committed
782
783
}

784
785
static void stuff_mb_4x4(VP9_COMP *cpi, MACROBLOCKD *xd,
                         TOKENEXTRA **t, int dry_run) {
Paul Wilkins's avatar
Paul Wilkins committed
786
787
  ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
  ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
John Koleszar's avatar
John Koleszar committed
788
  int b;
789
  PLANE_TYPE plane_type;
790
791
792
  const int has_y2_block = (xd->mode_info_context->mbmi.mode != B_PRED &&
                            xd->mode_info_context->mbmi.mode != I8X8_PRED &&
                            xd->mode_info_context->mbmi.mode != SPLITMV);
John Koleszar's avatar
John Koleszar committed
793

794
  if (has_y2_block) {
795
796
    stuff_b(cpi, xd, xd->block + 24, t, PLANE_TYPE_Y2, A + vp9_block2above[24],
            L + vp9_block2left[24], TX_4X4, dry_run);
797
798
799
800
    plane_type = PLANE_TYPE_Y_NO_DC;
  } else {
    plane_type = PLANE_TYPE_Y_WITH_DC;
  }
John Koleszar's avatar
John Koleszar committed
801

John Koleszar's avatar
John Koleszar committed
802
  for (b = 0; b < 16; b++)
803
804
    stuff_b(cpi, xd, xd->block + b, t, plane_type, A + vp9_block2above[b],
            L + vp9_block2left[b], TX_4X4, dry_run);
John Koleszar's avatar
John Koleszar committed
805

John Koleszar's avatar
John Koleszar committed
806
  for (b = 16; b < 24; b++)
807
808
    stuff_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV, A + vp9_block2above[b],
            L + vp9_block2left[b], TX_4X4, dry_run);
John Koleszar's avatar
John Koleszar committed
809
}
Deb Mukherjee's avatar
Deb Mukherjee committed
810

811
812
static void stuff_mb_8x8_4x4uv(VP9_COMP *cpi, MACROBLOCKD *xd,
                               TOKENEXTRA **t, int dry_run) {
Deb Mukherjee's avatar
Deb Mukherjee committed
813
814
815
816
817
  ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)xd->above_context;
  ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)xd->left_context;
  int b;

  for (b = 0; b < 16; b += 4) {
818
    stuff_b(cpi, xd, xd->block + b, t, PLANE_TYPE_Y_WITH_DC,
819
            A + vp9_block2above_8x8[b], L + vp9_block2left_8x8[b],
820
            TX_8X8, dry_run);
821
822
    A[vp9_block2above_8x8[b] + 1] = A[vp9_block2above_8x8[b]];
    L[vp9_block2left_8x8[b] + 1]  = L[vp9_block2left_8x8[b]];
Deb Mukherjee's avatar
Deb Mukherjee committed
823
824
825
  }

  for (b = 16; b < 24; b++)
826
827
    stuff_b(cpi, xd, xd->block + b, t, PLANE_TYPE_UV, A + vp9_block2above[b],
            L + vp9_block2left[b], TX_4X4, dry_run);
Deb Mukherjee's avatar
Deb Mukherjee committed
828
829
}

830
void vp9_stuff_mb(VP9_COMP *cpi, MACROBLOCKD *xd, TOKENEXTRA **t, int dry_run) {
831
  TX_SIZE tx_size = xd->mode_info_context->mbmi.txfm_size;
832
  TOKENEXTRA * const t_backup = *t;
833
834

  if (tx_size == TX_16X16) {
835
    stuff_mb_16x16(cpi, xd, t, dry_run);
836
  } else if (tx_size == TX_8X8) {
837
838
    if (xd->mode_info_context->mbmi.mode == I8X8_PRED ||
        xd->mode_info_context->mbmi.mode == SPLITMV) {
839
      stuff_mb_8x8_4x4uv(cpi, xd, t, dry_run);
840
    } else {
841
      stuff_mb_8x8(cpi, xd, t, dry_run);
842
843
    }
  } else {
844
    stuff_mb_4x4(cpi, xd, t, dry_run);
845
  }
846
847
848
849

  if (dry_run) {
    *t = t_backup;
  }
850
851
}

852
void vp9_fix_contexts(MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
853
  /* Clear entropy contexts for Y2 blocks */
Paul Wilkins's avatar
Paul Wilkins committed
854
855
856
857
  if ((xd->mode_info_context->mbmi.mode != B_PRED
      && xd->mode_info_context->mbmi.mode != I8X8_PRED
      && xd->mode_info_context->mbmi.mode != SPLITMV)
      || xd->mode_info_context->mbmi.txfm_size == TX_16X16
Daniel Kang's avatar
Daniel Kang committed
858
      ) {
Paul Wilkins's avatar
Paul Wilkins committed
859
860
    vpx_memset(xd->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
    vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
John Koleszar's avatar
John Koleszar committed
861
  } else {
Paul Wilkins's avatar
Paul Wilkins committed
862
863
    vpx_memset(xd->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
    vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
John Koleszar's avatar
John Koleszar committed
864
  }
John Koleszar's avatar
John Koleszar committed
865
}