blob: 07ae254aea93145597109e5437428d2255ff3e6a [file] [log] [blame]
Jingning Han3ee6db62015-08-05 19:00:31 -07001/*
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
9 */
10
Jingning Han54d66ef2015-08-06 21:14:07 -070011#include "vp10/encoder/context_tree.h"
12#include "vp10/encoder/encoder.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070013
14static const BLOCK_SIZE square[] = {
clang-format99e28b82016-01-27 12:42:45 -080015 BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64,
Jingning Han3ee6db62015-08-05 19:00:31 -070016};
17
Yaowu Xufc7cbd12015-08-13 09:36:53 -070018static void alloc_mode_context(VP10_COMMON *cm, int num_4x4_blk,
Jingning Han3ee6db62015-08-05 19:00:31 -070019 PICK_MODE_CONTEXT *ctx) {
20 const int num_blk = (num_4x4_blk < 4 ? 4 : num_4x4_blk);
21 const int num_pix = num_blk << 4;
22 int i, k;
23 ctx->num_4x4_blk = num_blk;
24
clang-format99e28b82016-01-27 12:42:45 -080025 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, vpx_calloc(num_blk, sizeof(uint8_t)));
Jingning Han3ee6db62015-08-05 19:00:31 -070026 for (i = 0; i < MAX_MB_PLANE; ++i) {
27 for (k = 0; k < 3; ++k) {
28 CHECK_MEM_ERROR(cm, ctx->coeff[i][k],
Geza Lore9cfba092015-10-19 14:05:35 +010029 vpx_memalign(32, num_pix * sizeof(*ctx->coeff[i][k])));
Jingning Han3ee6db62015-08-05 19:00:31 -070030 CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k],
Geza Lore9cfba092015-10-19 14:05:35 +010031 vpx_memalign(32, num_pix * sizeof(*ctx->qcoeff[i][k])));
Jingning Han3ee6db62015-08-05 19:00:31 -070032 CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k],
Geza Lore9cfba092015-10-19 14:05:35 +010033 vpx_memalign(32, num_pix * sizeof(*ctx->dqcoeff[i][k])));
Jingning Han3ee6db62015-08-05 19:00:31 -070034 CHECK_MEM_ERROR(cm, ctx->eobs[i][k],
Geza Lore9cfba092015-10-19 14:05:35 +010035 vpx_memalign(32, num_blk * sizeof(*ctx->eobs[i][k])));
clang-format99e28b82016-01-27 12:42:45 -080036 ctx->coeff_pbuf[i][k] = ctx->coeff[i][k];
37 ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k];
Jingning Han3ee6db62015-08-05 19:00:31 -070038 ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k];
clang-format99e28b82016-01-27 12:42:45 -080039 ctx->eobs_pbuf[i][k] = ctx->eobs[i][k];
Jingning Han3ee6db62015-08-05 19:00:31 -070040 }
41 }
42}
43
44static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
45 int i, k;
46 vpx_free(ctx->zcoeff_blk);
47 ctx->zcoeff_blk = 0;
48 for (i = 0; i < MAX_MB_PLANE; ++i) {
49 for (k = 0; k < 3; ++k) {
50 vpx_free(ctx->coeff[i][k]);
51 ctx->coeff[i][k] = 0;
52 vpx_free(ctx->qcoeff[i][k]);
53 ctx->qcoeff[i][k] = 0;
54 vpx_free(ctx->dqcoeff[i][k]);
55 ctx->dqcoeff[i][k] = 0;
56 vpx_free(ctx->eobs[i][k]);
57 ctx->eobs[i][k] = 0;
58 }
59 }
hui su5d011cb2015-09-15 12:44:13 -070060
61 for (i = 0; i < 2; ++i) {
62 vpx_free(ctx->color_index_map[i]);
63 ctx->color_index_map[i] = 0;
64 }
Jingning Han3ee6db62015-08-05 19:00:31 -070065}
66
Yaowu Xufc7cbd12015-08-13 09:36:53 -070067static void alloc_tree_contexts(VP10_COMMON *cm, PC_TREE *tree,
Jingning Han3ee6db62015-08-05 19:00:31 -070068 int num_4x4_blk) {
69 alloc_mode_context(cm, num_4x4_blk, &tree->none);
clang-format99e28b82016-01-27 12:42:45 -080070 alloc_mode_context(cm, num_4x4_blk / 2, &tree->horizontal[0]);
71 alloc_mode_context(cm, num_4x4_blk / 2, &tree->vertical[0]);
Jingning Han3ee6db62015-08-05 19:00:31 -070072
73 if (num_4x4_blk > 4) {
clang-format99e28b82016-01-27 12:42:45 -080074 alloc_mode_context(cm, num_4x4_blk / 2, &tree->horizontal[1]);
75 alloc_mode_context(cm, num_4x4_blk / 2, &tree->vertical[1]);
Jingning Han3ee6db62015-08-05 19:00:31 -070076 } else {
77 memset(&tree->horizontal[1], 0, sizeof(tree->horizontal[1]));
78 memset(&tree->vertical[1], 0, sizeof(tree->vertical[1]));
79 }
80}
81
82static void free_tree_contexts(PC_TREE *tree) {
83 free_mode_context(&tree->none);
84 free_mode_context(&tree->horizontal[0]);
85 free_mode_context(&tree->horizontal[1]);
86 free_mode_context(&tree->vertical[0]);
87 free_mode_context(&tree->vertical[1]);
88}
89
90// This function sets up a tree of contexts such that at each square
91// partition level. There are contexts for none, horizontal, vertical, and
92// split. Along with a block_size value and a selected block_size which
93// represents the state of our search.
Yaowu Xufc7cbd12015-08-13 09:36:53 -070094void vp10_setup_pc_tree(VP10_COMMON *cm, ThreadData *td) {
Jingning Han3ee6db62015-08-05 19:00:31 -070095 int i, j;
96 const int leaf_nodes = 64;
97 const int tree_nodes = 64 + 16 + 4 + 1;
98 int pc_tree_index = 0;
99 PC_TREE *this_pc;
100 PICK_MODE_CONTEXT *this_leaf;
101 int square_index = 1;
102 int nodes;
103
104 vpx_free(td->leaf_tree);
clang-format99e28b82016-01-27 12:42:45 -0800105 CHECK_MEM_ERROR(cm, td->leaf_tree,
106 vpx_calloc(leaf_nodes, sizeof(*td->leaf_tree)));
Jingning Han3ee6db62015-08-05 19:00:31 -0700107 vpx_free(td->pc_tree);
clang-format99e28b82016-01-27 12:42:45 -0800108 CHECK_MEM_ERROR(cm, td->pc_tree,
109 vpx_calloc(tree_nodes, sizeof(*td->pc_tree)));
Jingning Han3ee6db62015-08-05 19:00:31 -0700110
111 this_pc = &td->pc_tree[0];
112 this_leaf = &td->leaf_tree[0];
113
114 // 4x4 blocks smaller than 8x8 but in the same 8x8 block share the same
115 // context so we only need to allocate 1 for each 8x8 block.
clang-format99e28b82016-01-27 12:42:45 -0800116 for (i = 0; i < leaf_nodes; ++i) alloc_mode_context(cm, 1, &td->leaf_tree[i]);
Jingning Han3ee6db62015-08-05 19:00:31 -0700117
118 // Sets up all the leaf nodes in the tree.
119 for (pc_tree_index = 0; pc_tree_index < leaf_nodes; ++pc_tree_index) {
120 PC_TREE *const tree = &td->pc_tree[pc_tree_index];
121 tree->block_size = square[0];
122 alloc_tree_contexts(cm, tree, 4);
123 tree->leaf_split[0] = this_leaf++;
clang-format99e28b82016-01-27 12:42:45 -0800124 for (j = 1; j < 4; j++) tree->leaf_split[j] = tree->leaf_split[0];
Jingning Han3ee6db62015-08-05 19:00:31 -0700125 }
126
127 // Each node has 4 leaf nodes, fill each block_size level of the tree
128 // from leafs to the root.
129 for (nodes = 16; nodes > 0; nodes >>= 2) {
130 for (i = 0; i < nodes; ++i) {
131 PC_TREE *const tree = &td->pc_tree[pc_tree_index];
132 alloc_tree_contexts(cm, tree, 4 << (2 * square_index));
133 tree->block_size = square[square_index];
clang-format99e28b82016-01-27 12:42:45 -0800134 for (j = 0; j < 4; j++) tree->split[j] = this_pc++;
Jingning Han3ee6db62015-08-05 19:00:31 -0700135 ++pc_tree_index;
136 }
137 ++square_index;
138 }
139 td->pc_root = &td->pc_tree[tree_nodes - 1];
140 td->pc_root[0].none.best_mode_index = 2;
141}
142
143void vp10_free_pc_tree(ThreadData *td) {
144 const int tree_nodes = 64 + 16 + 4 + 1;
145 int i;
146
147 // Set up all 4x4 mode contexts
clang-format99e28b82016-01-27 12:42:45 -0800148 for (i = 0; i < 64; ++i) free_mode_context(&td->leaf_tree[i]);
Jingning Han3ee6db62015-08-05 19:00:31 -0700149
150 // Sets up all the leaf nodes in the tree.
clang-format99e28b82016-01-27 12:42:45 -0800151 for (i = 0; i < tree_nodes; ++i) free_tree_contexts(&td->pc_tree[i]);
Jingning Han3ee6db62015-08-05 19:00:31 -0700152
153 vpx_free(td->pc_tree);
154 td->pc_tree = NULL;
155 vpx_free(td->leaf_tree);
156 td->leaf_tree = NULL;
157}