1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "av1/encoder/context_tree.h"
13 #include "av1/encoder/encoder.h"
14
15 static const BLOCK_SIZE square[MAX_SB_SIZE_LOG2 - 1] = {
16 BLOCK_4X4, BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64, BLOCK_128X128,
17 };
18
19 typedef struct {
20 tran_low_t *coeff_buf[MAX_MB_PLANE];
21 tran_low_t *qcoeff_buf[MAX_MB_PLANE];
22 tran_low_t *dqcoeff_buf[MAX_MB_PLANE];
23 } PC_TREE_SHARED_BUFFERS;
24
alloc_mode_context(AV1_COMMON * cm,int num_pix,PICK_MODE_CONTEXT * ctx,PC_TREE_SHARED_BUFFERS * shared_bufs)25 static AOM_INLINE void alloc_mode_context(AV1_COMMON *cm, int num_pix,
26 PICK_MODE_CONTEXT *ctx,
27 PC_TREE_SHARED_BUFFERS *shared_bufs) {
28 const int num_planes = av1_num_planes(cm);
29 int i;
30 const int num_blk = num_pix / 16;
31 ctx->num_4x4_blk = num_blk;
32
33 CHECK_MEM_ERROR(cm, ctx->blk_skip,
34 aom_calloc(num_blk, sizeof(*ctx->blk_skip)));
35 CHECK_MEM_ERROR(cm, ctx->tx_type_map,
36 aom_calloc(num_blk, sizeof(*ctx->tx_type_map)));
37 for (i = 0; i < num_planes; ++i) {
38 ctx->coeff[i] = shared_bufs->coeff_buf[i];
39 ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
40 ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
41 CHECK_MEM_ERROR(cm, ctx->eobs[i],
42 aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
43 CHECK_MEM_ERROR(
44 cm, ctx->txb_entropy_ctx[i],
45 aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
46 }
47
48 if (num_pix <= MAX_PALETTE_SQUARE) {
49 for (i = 0; i < 2; ++i) {
50 CHECK_MEM_ERROR(
51 cm, ctx->color_index_map[i],
52 aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
53 }
54 }
55 }
56
free_mode_context(PICK_MODE_CONTEXT * ctx,const int num_planes)57 static AOM_INLINE void free_mode_context(PICK_MODE_CONTEXT *ctx,
58 const int num_planes) {
59 int i;
60 aom_free(ctx->blk_skip);
61 ctx->blk_skip = 0;
62 aom_free(ctx->tx_type_map);
63 ctx->tx_type_map = 0;
64 for (i = 0; i < num_planes; ++i) {
65 ctx->coeff[i] = 0;
66 ctx->qcoeff[i] = 0;
67 ctx->dqcoeff[i] = 0;
68 aom_free(ctx->eobs[i]);
69 ctx->eobs[i] = 0;
70 aom_free(ctx->txb_entropy_ctx[i]);
71 ctx->txb_entropy_ctx[i] = 0;
72 }
73
74 for (i = 0; i < 2; ++i) {
75 aom_free(ctx->color_index_map[i]);
76 ctx->color_index_map[i] = 0;
77 }
78 }
79
alloc_tree_contexts(AV1_COMMON * cm,PC_TREE * tree,int num_pix,int is_leaf,PC_TREE_SHARED_BUFFERS * shared_bufs)80 static AOM_INLINE void alloc_tree_contexts(
81 AV1_COMMON *cm, PC_TREE *tree, int num_pix, int is_leaf,
82 PC_TREE_SHARED_BUFFERS *shared_bufs) {
83 alloc_mode_context(cm, num_pix, &tree->none, shared_bufs);
84
85 if (is_leaf) return;
86
87 alloc_mode_context(cm, num_pix / 2, &tree->horizontal[0], shared_bufs);
88 alloc_mode_context(cm, num_pix / 2, &tree->vertical[0], shared_bufs);
89
90 alloc_mode_context(cm, num_pix / 2, &tree->horizontal[1], shared_bufs);
91 alloc_mode_context(cm, num_pix / 2, &tree->vertical[1], shared_bufs);
92
93 alloc_mode_context(cm, num_pix / 4, &tree->horizontala[0], shared_bufs);
94 alloc_mode_context(cm, num_pix / 4, &tree->horizontala[1], shared_bufs);
95 alloc_mode_context(cm, num_pix / 2, &tree->horizontala[2], shared_bufs);
96
97 alloc_mode_context(cm, num_pix / 2, &tree->horizontalb[0], shared_bufs);
98 alloc_mode_context(cm, num_pix / 4, &tree->horizontalb[1], shared_bufs);
99 alloc_mode_context(cm, num_pix / 4, &tree->horizontalb[2], shared_bufs);
100
101 alloc_mode_context(cm, num_pix / 4, &tree->verticala[0], shared_bufs);
102 alloc_mode_context(cm, num_pix / 4, &tree->verticala[1], shared_bufs);
103 alloc_mode_context(cm, num_pix / 2, &tree->verticala[2], shared_bufs);
104
105 alloc_mode_context(cm, num_pix / 2, &tree->verticalb[0], shared_bufs);
106 alloc_mode_context(cm, num_pix / 4, &tree->verticalb[1], shared_bufs);
107 alloc_mode_context(cm, num_pix / 4, &tree->verticalb[2], shared_bufs);
108
109 for (int i = 0; i < 4; ++i) {
110 alloc_mode_context(cm, num_pix / 4, &tree->horizontal4[i], shared_bufs);
111 alloc_mode_context(cm, num_pix / 4, &tree->vertical4[i], shared_bufs);
112 }
113 }
114
free_tree_contexts(PC_TREE * tree,const int num_planes)115 static AOM_INLINE void free_tree_contexts(PC_TREE *tree, const int num_planes) {
116 int i;
117 for (i = 0; i < 3; i++) {
118 free_mode_context(&tree->horizontala[i], num_planes);
119 free_mode_context(&tree->horizontalb[i], num_planes);
120 free_mode_context(&tree->verticala[i], num_planes);
121 free_mode_context(&tree->verticalb[i], num_planes);
122 }
123 for (i = 0; i < 4; ++i) {
124 free_mode_context(&tree->horizontal4[i], num_planes);
125 free_mode_context(&tree->vertical4[i], num_planes);
126 }
127 free_mode_context(&tree->none, num_planes);
128 free_mode_context(&tree->horizontal[0], num_planes);
129 free_mode_context(&tree->horizontal[1], num_planes);
130 free_mode_context(&tree->vertical[0], num_planes);
131 free_mode_context(&tree->vertical[1], num_planes);
132 }
133
134 // This function will compute the number of pc_tree nodes to be allocated
135 // or freed as per the super block size of BLOCK_128X128 or BLOCK_64X64
get_pc_tree_nodes(const int is_sb_size_128,int stat_generation_stage)136 static AOM_INLINE int get_pc_tree_nodes(const int is_sb_size_128,
137 int stat_generation_stage) {
138 const int tree_nodes_inc = is_sb_size_128 ? 1024 : 0;
139 const int tree_nodes =
140 stat_generation_stage ? 1 : (tree_nodes_inc + 256 + 64 + 16 + 4 + 1);
141 return tree_nodes;
142 }
143
144 // This function sets up a tree of contexts such that at each square
145 // partition level. There are contexts for none, horizontal, vertical, and
146 // split. Along with a block_size value and a selected block_size which
147 // represents the state of our search.
av1_setup_pc_tree(AV1_COMP * const cpi,ThreadData * td)148 void av1_setup_pc_tree(AV1_COMP *const cpi, ThreadData *td) {
149 AV1_COMMON *const cm = &cpi->common;
150 int i, j, stat_generation_stage = is_stat_generation_stage(cpi);
151 const int is_sb_size_128 = cm->seq_params.sb_size == BLOCK_128X128;
152 const int tree_nodes =
153 get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
154 int pc_tree_index = 0;
155 PC_TREE *this_pc;
156 PC_TREE_SHARED_BUFFERS shared_bufs;
157 int square_index = 1;
158 int nodes;
159
160 aom_free(td->pc_tree);
161 CHECK_MEM_ERROR(cm, td->pc_tree,
162 aom_calloc(tree_nodes, sizeof(*td->pc_tree)));
163 this_pc = &td->pc_tree[0];
164
165 for (i = 0; i < 3; i++) {
166 const int max_num_pix = MAX_SB_SIZE * MAX_SB_SIZE;
167 CHECK_MEM_ERROR(cm, td->tree_coeff_buf[i],
168 aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
169 CHECK_MEM_ERROR(cm, td->tree_qcoeff_buf[i],
170 aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
171 CHECK_MEM_ERROR(cm, td->tree_dqcoeff_buf[i],
172 aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
173 shared_bufs.coeff_buf[i] = td->tree_coeff_buf[i];
174 shared_bufs.qcoeff_buf[i] = td->tree_qcoeff_buf[i];
175 shared_bufs.dqcoeff_buf[i] = td->tree_dqcoeff_buf[i];
176 }
177
178 if (!stat_generation_stage) {
179 const int leaf_factor = is_sb_size_128 ? 4 : 1;
180 const int leaf_nodes = 256 * leaf_factor;
181
182 // Sets up all the leaf nodes in the tree.
183 for (pc_tree_index = 0; pc_tree_index < leaf_nodes; ++pc_tree_index) {
184 PC_TREE *const tree = &td->pc_tree[pc_tree_index];
185 tree->block_size = square[0];
186 alloc_tree_contexts(cm, tree, 16, 1, &shared_bufs);
187 }
188
189 // Each node has 4 leaf nodes, fill each block_size level of the tree
190 // from leafs to the root.
191 for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
192 for (i = 0; i < nodes; ++i) {
193 PC_TREE *const tree = &td->pc_tree[pc_tree_index];
194 alloc_tree_contexts(cm, tree, 16 << (2 * square_index), 0,
195 &shared_bufs);
196 tree->block_size = square[square_index];
197 for (j = 0; j < 4; j++) tree->split[j] = this_pc++;
198 ++pc_tree_index;
199 }
200 ++square_index;
201 }
202 } else {
203 // Allocation for firstpass/LAP stage
204 // TODO(Mufaddal): refactor square_index to use a common block_size macro
205 // from firstpass.c
206 PC_TREE *const tree = &td->pc_tree[pc_tree_index];
207 square_index = 2;
208 alloc_tree_contexts(cm, tree, 16 << (2 * square_index), 1, &shared_bufs);
209 tree->block_size = square[square_index];
210 }
211
212 // Set up the root node for the applicable superblock size
213 td->pc_root = &td->pc_tree[tree_nodes - 1];
214 #if CONFIG_INTERNAL_STATS
215 td->pc_root->none.best_mode_index = THR_INVALID;
216 #endif // CONFIG_INTERNAL_STATS
217 }
218
av1_free_pc_tree(const AV1_COMP * const cpi,ThreadData * td,const int num_planes,BLOCK_SIZE sb_size)219 void av1_free_pc_tree(const AV1_COMP *const cpi, ThreadData *td,
220 const int num_planes, BLOCK_SIZE sb_size) {
221 int stat_generation_stage = is_stat_generation_stage(cpi);
222 if (td->pc_tree != NULL) {
223 const int is_sb_size_128 = sb_size == BLOCK_128X128;
224 const int tree_nodes =
225 get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
226 for (int i = 0; i < tree_nodes; ++i) {
227 free_tree_contexts(&td->pc_tree[i], num_planes);
228 }
229 for (int i = 0; i < 3; ++i) {
230 aom_free(td->tree_coeff_buf[i]);
231 aom_free(td->tree_qcoeff_buf[i]);
232 aom_free(td->tree_dqcoeff_buf[i]);
233 td->tree_coeff_buf[i] = NULL;
234 td->tree_qcoeff_buf[i] = NULL;
235 td->tree_dqcoeff_buf[i] = NULL;
236 }
237 aom_free(td->pc_tree);
238 td->pc_tree = NULL;
239 }
240 }
241
av1_copy_tree_context(PICK_MODE_CONTEXT * dst_ctx,PICK_MODE_CONTEXT * src_ctx)242 void av1_copy_tree_context(PICK_MODE_CONTEXT *dst_ctx,
243 PICK_MODE_CONTEXT *src_ctx) {
244 dst_ctx->mic = src_ctx->mic;
245 dst_ctx->mbmi_ext_best = src_ctx->mbmi_ext_best;
246
247 dst_ctx->num_4x4_blk = src_ctx->num_4x4_blk;
248 dst_ctx->skippable = src_ctx->skippable;
249 #if CONFIG_INTERNAL_STATS
250 dst_ctx->best_mode_index = src_ctx->best_mode_index;
251 #endif // CONFIG_INTERNAL_STATS
252
253 memcpy(dst_ctx->blk_skip, src_ctx->blk_skip,
254 sizeof(uint8_t) * src_ctx->num_4x4_blk);
255 av1_copy_array(dst_ctx->tx_type_map, src_ctx->tx_type_map,
256 src_ctx->num_4x4_blk);
257
258 dst_ctx->hybrid_pred_diff = src_ctx->hybrid_pred_diff;
259 dst_ctx->comp_pred_diff = src_ctx->comp_pred_diff;
260 dst_ctx->single_pred_diff = src_ctx->single_pred_diff;
261
262 dst_ctx->rd_stats = src_ctx->rd_stats;
263 dst_ctx->rd_mode_is_ready = src_ctx->rd_mode_is_ready;
264
265 memcpy(dst_ctx->pred_mv, src_ctx->pred_mv, sizeof(MV) * REF_FRAMES);
266
267 dst_ctx->partition = src_ctx->partition;
268 }
269