context_tree.c 7.92 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

12 13
#include "av1/encoder/context_tree.h"
#include "av1/encoder/encoder.h"
Jingning Han's avatar
Jingning Han committed
14

Jingning Han's avatar
Jingning Han committed
15
static const BLOCK_SIZE square[MAX_SB_SIZE_LOG2 - 1] = {
16
  BLOCK_4X4,     BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64,
17 18 19
#if CONFIG_EXT_PARTITION
  BLOCK_128X128,
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
20 21
};

22
static void alloc_mode_context(AV1_COMMON *cm, int num_pix,
23 24 25
#if CONFIG_EXT_PARTITION_TYPES
                               PARTITION_TYPE partition,
#endif
Jingning Han's avatar
Jingning Han committed
26
                               PICK_MODE_CONTEXT *ctx) {
27
  int i;
28
  const int num_blk = num_pix / 16;
Jingning Han's avatar
Jingning Han committed
29
  ctx->num_4x4_blk = num_blk;
30

31 32 33
#if CONFIG_EXT_PARTITION_TYPES
  ctx->partition = partition;
#endif
Jingning Han's avatar
Jingning Han committed
34 35

  for (i = 0; i < MAX_MB_PLANE; ++i) {
36
    CHECK_MEM_ERROR(cm, ctx->blk_skip[i], aom_calloc(num_blk, sizeof(uint8_t)));
37 38 39 40 41 42 43 44
    CHECK_MEM_ERROR(cm, ctx->coeff[i],
                    aom_memalign(32, num_pix * sizeof(*ctx->coeff[i])));
    CHECK_MEM_ERROR(cm, ctx->qcoeff[i],
                    aom_memalign(32, num_pix * sizeof(*ctx->qcoeff[i])));
    CHECK_MEM_ERROR(cm, ctx->dqcoeff[i],
                    aom_memalign(32, num_pix * sizeof(*ctx->dqcoeff[i])));
    CHECK_MEM_ERROR(cm, ctx->eobs[i],
                    aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
45 46 47 48 49
#if CONFIG_LV_MAP
    CHECK_MEM_ERROR(
        cm, ctx->txb_entropy_ctx[i],
        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
#endif
Jingning Han's avatar
Jingning Han committed
50
  }
51

52 53 54 55 56 57
  if (num_pix <= MAX_PALETTE_SQUARE) {
    for (i = 0; i < 2; ++i) {
      CHECK_MEM_ERROR(
          cm, ctx->color_index_map[i],
          aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
    }
58
  }
Jingning Han's avatar
Jingning Han committed
59 60 61
}

static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
62
  int i;
Jingning Han's avatar
Jingning Han committed
63
  for (i = 0; i < MAX_MB_PLANE; ++i) {
64
    aom_free(ctx->blk_skip[i]);
65
    ctx->blk_skip[i] = 0;
66 67 68 69 70 71 72 73
    aom_free(ctx->coeff[i]);
    ctx->coeff[i] = 0;
    aom_free(ctx->qcoeff[i]);
    ctx->qcoeff[i] = 0;
    aom_free(ctx->dqcoeff[i]);
    ctx->dqcoeff[i] = 0;
    aom_free(ctx->eobs[i]);
    ctx->eobs[i] = 0;
74 75 76 77
#if CONFIG_LV_MAP
    aom_free(ctx->txb_entropy_ctx[i]);
    ctx->txb_entropy_ctx[i] = 0;
#endif
Jingning Han's avatar
Jingning Han committed
78
  }
hui su's avatar
hui su committed
79 80

  for (i = 0; i < 2; ++i) {
81
    aom_free(ctx->color_index_map[i]);
hui su's avatar
hui su committed
82 83
    ctx->color_index_map[i] = 0;
  }
Jingning Han's avatar
Jingning Han committed
84 85
}

86
static void alloc_tree_contexts(AV1_COMMON *cm, PC_TREE *tree, int num_pix) {
87
#if CONFIG_EXT_PARTITION_TYPES
88 89 90 91 92
  alloc_mode_context(cm, num_pix, PARTITION_NONE, &tree->none);
  alloc_mode_context(cm, num_pix / 2, PARTITION_HORZ, &tree->horizontal[0]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_VERT, &tree->vertical[0]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_VERT, &tree->horizontal[1]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_VERT, &tree->vertical[1]);
93

94 95 96 97 98 99 100 101 102 103 104 105
  alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_A, &tree->horizontala[0]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_A, &tree->horizontala[1]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_HORZ_A, &tree->horizontala[2]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_HORZ_B, &tree->horizontalb[0]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_B, &tree->horizontalb[1]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_B, &tree->horizontalb[2]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_VERT_A, &tree->verticala[0]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_VERT_A, &tree->verticala[1]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_VERT_A, &tree->verticala[2]);
  alloc_mode_context(cm, num_pix / 2, PARTITION_VERT_B, &tree->verticalb[0]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_VERT_B, &tree->verticalb[1]);
  alloc_mode_context(cm, num_pix / 4, PARTITION_VERT_B, &tree->verticalb[2]);
106
  for (int i = 0; i < 4; ++i) {
107
    alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_4,
108
                       &tree->horizontal4[i]);
109
    alloc_mode_context(cm, num_pix / 4, PARTITION_HORZ_4, &tree->vertical4[i]);
110
  }
111
#else
112 113 114
  alloc_mode_context(cm, num_pix, &tree->none);
  alloc_mode_context(cm, num_pix / 2, &tree->horizontal[0]);
  alloc_mode_context(cm, num_pix / 2, &tree->vertical[0]);
Jingning Han's avatar
Jingning Han committed
115

116 117 118
  if (num_pix > 16) {
    alloc_mode_context(cm, num_pix / 2, &tree->horizontal[1]);
    alloc_mode_context(cm, num_pix / 2, &tree->vertical[1]);
Jingning Han's avatar
Jingning Han committed
119 120 121 122
  } else {
    memset(&tree->horizontal[1], 0, sizeof(tree->horizontal[1]));
    memset(&tree->vertical[1], 0, sizeof(tree->vertical[1]));
  }
123
#endif  // CONFIG_EXT_PARTITION_TYPES
Jingning Han's avatar
Jingning Han committed
124 125 126
}

static void free_tree_contexts(PC_TREE *tree) {
127 128 129 130 131 132 133 134
#if CONFIG_EXT_PARTITION_TYPES
  int i;
  for (i = 0; i < 3; i++) {
    free_mode_context(&tree->horizontala[i]);
    free_mode_context(&tree->horizontalb[i]);
    free_mode_context(&tree->verticala[i]);
    free_mode_context(&tree->verticalb[i]);
  }
135 136 137 138
  for (i = 0; i < 4; ++i) {
    free_mode_context(&tree->horizontal4[i]);
    free_mode_context(&tree->vertical4[i]);
  }
139
#endif  // CONFIG_EXT_PARTITION_TYPES
Jingning Han's avatar
Jingning Han committed
140 141 142 143 144 145 146 147 148 149 150
  free_mode_context(&tree->none);
  free_mode_context(&tree->horizontal[0]);
  free_mode_context(&tree->horizontal[1]);
  free_mode_context(&tree->vertical[0]);
  free_mode_context(&tree->vertical[1]);
}

// This function sets up a tree of contexts such that at each square
// partition level. There are contexts for none, horizontal, vertical, and
// split.  Along with a block_size value and a selected block_size which
// represents the state of our search.
151
void av1_setup_pc_tree(AV1_COMMON *cm, ThreadData *td) {
Jingning Han's avatar
Jingning Han committed
152
  int i, j;
153 154 155
#if CONFIG_EXT_PARTITION
  const int tree_nodes_inc = 1024;
#else
Jingning Han's avatar
Jingning Han committed
156
  const int tree_nodes_inc = 256;
157
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
158
  const int leaf_factor = 4;
159
#if CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
160 161
  const int leaf_nodes = 256 * leaf_factor;
  const int tree_nodes = tree_nodes_inc + 256 + 64 + 16 + 4 + 1;
162
#else
Jingning Han's avatar
Jingning Han committed
163 164
  const int leaf_nodes = 64 * leaf_factor;
  const int tree_nodes = tree_nodes_inc + 64 + 16 + 4 + 1;
165
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
166 167 168 169 170
  int pc_tree_index = 0;
  PC_TREE *this_pc;
  int square_index = 1;
  int nodes;

171
  aom_free(td->pc_tree);
172
  CHECK_MEM_ERROR(cm, td->pc_tree,
173
                  aom_calloc(tree_nodes, sizeof(*td->pc_tree)));
Jingning Han's avatar
Jingning Han committed
174 175 176 177 178 179
  this_pc = &td->pc_tree[0];

  // Sets up all the leaf nodes in the tree.
  for (pc_tree_index = 0; pc_tree_index < leaf_nodes; ++pc_tree_index) {
    PC_TREE *const tree = &td->pc_tree[pc_tree_index];
    tree->block_size = square[0];
180
    alloc_tree_contexts(cm, tree, 16);
Jingning Han's avatar
Jingning Han committed
181 182 183 184
  }

  // Each node has 4 leaf nodes, fill each block_size level of the tree
  // from leafs to the root.
185
  for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
Jingning Han's avatar
Jingning Han committed
186 187
    for (i = 0; i < nodes; ++i) {
      PC_TREE *const tree = &td->pc_tree[pc_tree_index];
188
      alloc_tree_contexts(cm, tree, 16 << (2 * square_index));
Jingning Han's avatar
Jingning Han committed
189
      tree->block_size = square[square_index];
190
      for (j = 0; j < 4; j++) tree->split[j] = this_pc++;
Jingning Han's avatar
Jingning Han committed
191 192 193 194
      ++pc_tree_index;
    }
    ++square_index;
  }
Geza Lore's avatar
Geza Lore committed
195 196 197 198 199 200 201

  // Set up the root node for the largest superblock size
  i = MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2;
  td->pc_root[i] = &td->pc_tree[tree_nodes - 1];
  td->pc_root[i]->none.best_mode_index = 2;
  // Set up the root nodes for the rest of the possible superblock sizes
  while (--i >= 0) {
202
    td->pc_root[i] = td->pc_root[i + 1]->split[0];
Geza Lore's avatar
Geza Lore committed
203 204
    td->pc_root[i]->none.best_mode_index = 2;
  }
Jingning Han's avatar
Jingning Han committed
205 206
}

207
void av1_free_pc_tree(ThreadData *td) {
208 209 210
#if CONFIG_EXT_PARTITION
  const int tree_nodes_inc = 1024;
#else
211
  const int tree_nodes_inc = 256;
212
#endif  // CONFIG_EXT_PARTITION
213

214
#if CONFIG_EXT_PARTITION
215
  const int tree_nodes = tree_nodes_inc + 256 + 64 + 16 + 4 + 1;
216
#else
217
  const int tree_nodes = tree_nodes_inc + 64 + 16 + 4 + 1;
218
#endif  // CONFIG_EXT_PARTITION
Jingning Han's avatar
Jingning Han committed
219
  int i;
220
  for (i = 0; i < tree_nodes; ++i) free_tree_contexts(&td->pc_tree[i]);
221
  aom_free(td->pc_tree);
Jingning Han's avatar
Jingning Han committed
222 223
  td->pc_tree = NULL;
}