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 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, aom_calloc(num_blk, sizeof(uint8_t)));
34   for (i = 0; i < num_planes; ++i) {
35     ctx->coeff[i] = shared_bufs->coeff_buf[i];
36     ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
37     ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
38     CHECK_MEM_ERROR(cm, ctx->eobs[i],
39                     aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
40     CHECK_MEM_ERROR(
41         cm, ctx->txb_entropy_ctx[i],
42         aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
43   }
44 
45   if (num_pix <= MAX_PALETTE_SQUARE) {
46     for (i = 0; i < 2; ++i) {
47       CHECK_MEM_ERROR(
48           cm, ctx->color_index_map[i],
49           aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
50     }
51   }
52 }
53 
free_mode_context(PICK_MODE_CONTEXT * ctx,const int num_planes)54 static void free_mode_context(PICK_MODE_CONTEXT *ctx, const int num_planes) {
55   int i;
56   aom_free(ctx->blk_skip);
57   ctx->blk_skip = 0;
58   for (i = 0; i < num_planes; ++i) {
59     ctx->coeff[i] = 0;
60     ctx->qcoeff[i] = 0;
61     ctx->dqcoeff[i] = 0;
62     aom_free(ctx->eobs[i]);
63     ctx->eobs[i] = 0;
64     aom_free(ctx->txb_entropy_ctx[i]);
65     ctx->txb_entropy_ctx[i] = 0;
66   }
67 
68   for (i = 0; i < 2; ++i) {
69     aom_free(ctx->color_index_map[i]);
70     ctx->color_index_map[i] = 0;
71   }
72 }
73 
alloc_tree_contexts(AV1_COMMON * cm,PC_TREE * tree,int num_pix,int is_leaf,PC_TREE_SHARED_BUFFERS * shared_bufs)74 static void alloc_tree_contexts(AV1_COMMON *cm, PC_TREE *tree, int num_pix,
75                                 int is_leaf,
76                                 PC_TREE_SHARED_BUFFERS *shared_bufs) {
77   alloc_mode_context(cm, num_pix, &tree->none, shared_bufs);
78 
79   if (is_leaf) return;
80 
81   alloc_mode_context(cm, num_pix / 2, &tree->horizontal[0], shared_bufs);
82   alloc_mode_context(cm, num_pix / 2, &tree->vertical[0], shared_bufs);
83 
84   alloc_mode_context(cm, num_pix / 2, &tree->horizontal[1], shared_bufs);
85   alloc_mode_context(cm, num_pix / 2, &tree->vertical[1], shared_bufs);
86 
87   alloc_mode_context(cm, num_pix / 4, &tree->horizontala[0], shared_bufs);
88   alloc_mode_context(cm, num_pix / 4, &tree->horizontala[1], shared_bufs);
89   alloc_mode_context(cm, num_pix / 2, &tree->horizontala[2], shared_bufs);
90 
91   alloc_mode_context(cm, num_pix / 2, &tree->horizontalb[0], shared_bufs);
92   alloc_mode_context(cm, num_pix / 4, &tree->horizontalb[1], shared_bufs);
93   alloc_mode_context(cm, num_pix / 4, &tree->horizontalb[2], shared_bufs);
94 
95   alloc_mode_context(cm, num_pix / 4, &tree->verticala[0], shared_bufs);
96   alloc_mode_context(cm, num_pix / 4, &tree->verticala[1], shared_bufs);
97   alloc_mode_context(cm, num_pix / 2, &tree->verticala[2], shared_bufs);
98 
99   alloc_mode_context(cm, num_pix / 2, &tree->verticalb[0], shared_bufs);
100   alloc_mode_context(cm, num_pix / 4, &tree->verticalb[1], shared_bufs);
101   alloc_mode_context(cm, num_pix / 4, &tree->verticalb[2], shared_bufs);
102 
103   for (int i = 0; i < 4; ++i) {
104     alloc_mode_context(cm, num_pix / 4, &tree->horizontal4[i], shared_bufs);
105     alloc_mode_context(cm, num_pix / 4, &tree->vertical4[i], shared_bufs);
106   }
107 }
108 
free_tree_contexts(PC_TREE * tree,const int num_planes)109 static void free_tree_contexts(PC_TREE *tree, const int num_planes) {
110   int i;
111   for (i = 0; i < 3; i++) {
112     free_mode_context(&tree->horizontala[i], num_planes);
113     free_mode_context(&tree->horizontalb[i], num_planes);
114     free_mode_context(&tree->verticala[i], num_planes);
115     free_mode_context(&tree->verticalb[i], num_planes);
116   }
117   for (i = 0; i < 4; ++i) {
118     free_mode_context(&tree->horizontal4[i], num_planes);
119     free_mode_context(&tree->vertical4[i], num_planes);
120   }
121   free_mode_context(&tree->none, num_planes);
122   free_mode_context(&tree->horizontal[0], num_planes);
123   free_mode_context(&tree->horizontal[1], num_planes);
124   free_mode_context(&tree->vertical[0], num_planes);
125   free_mode_context(&tree->vertical[1], num_planes);
126 }
127 
128 // This function sets up a tree of contexts such that at each square
129 // partition level. There are contexts for none, horizontal, vertical, and
130 // split.  Along with a block_size value and a selected block_size which
131 // represents the state of our search.
av1_setup_pc_tree(AV1_COMMON * cm,ThreadData * td)132 void av1_setup_pc_tree(AV1_COMMON *cm, ThreadData *td) {
133   int i, j;
134   const int tree_nodes_inc = 1024;
135   const int leaf_factor = 4;
136   const int leaf_nodes = 256 * leaf_factor;
137   const int tree_nodes = tree_nodes_inc + 256 + 64 + 16 + 4 + 1;
138   int pc_tree_index = 0;
139   PC_TREE *this_pc;
140   PC_TREE_SHARED_BUFFERS shared_bufs;
141   int square_index = 1;
142   int nodes;
143 
144   aom_free(td->pc_tree);
145   CHECK_MEM_ERROR(cm, td->pc_tree,
146                   aom_calloc(tree_nodes, sizeof(*td->pc_tree)));
147   this_pc = &td->pc_tree[0];
148 
149   for (i = 0; i < 3; i++) {
150     const int max_num_pix = MAX_SB_SIZE * MAX_SB_SIZE;
151     CHECK_MEM_ERROR(cm, td->tree_coeff_buf[i],
152                     aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
153     CHECK_MEM_ERROR(cm, td->tree_qcoeff_buf[i],
154                     aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
155     CHECK_MEM_ERROR(cm, td->tree_dqcoeff_buf[i],
156                     aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
157     shared_bufs.coeff_buf[i] = td->tree_coeff_buf[i];
158     shared_bufs.qcoeff_buf[i] = td->tree_qcoeff_buf[i];
159     shared_bufs.dqcoeff_buf[i] = td->tree_dqcoeff_buf[i];
160   }
161 
162   // Sets up all the leaf nodes in the tree.
163   for (pc_tree_index = 0; pc_tree_index < leaf_nodes; ++pc_tree_index) {
164     PC_TREE *const tree = &td->pc_tree[pc_tree_index];
165     tree->block_size = square[0];
166     alloc_tree_contexts(cm, tree, 16, 1, &shared_bufs);
167   }
168 
169   // Each node has 4 leaf nodes, fill each block_size level of the tree
170   // from leafs to the root.
171   for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
172     for (i = 0; i < nodes; ++i) {
173       PC_TREE *const tree = &td->pc_tree[pc_tree_index];
174       alloc_tree_contexts(cm, tree, 16 << (2 * square_index), 0, &shared_bufs);
175       tree->block_size = square[square_index];
176       for (j = 0; j < 4; j++) tree->split[j] = this_pc++;
177       ++pc_tree_index;
178     }
179     ++square_index;
180   }
181 
182   // Set up the root node for the largest superblock size
183   i = MAX_MIB_SIZE_LOG2 - MIN_MIB_SIZE_LOG2;
184   td->pc_root[i] = &td->pc_tree[tree_nodes - 1];
185   td->pc_root[i]->none.best_mode_index = 2;
186   // Set up the root nodes for the rest of the possible superblock sizes
187   while (--i >= 0) {
188     td->pc_root[i] = td->pc_root[i + 1]->split[0];
189     td->pc_root[i]->none.best_mode_index = 2;
190   }
191 }
192 
av1_free_pc_tree(ThreadData * td,const int num_planes)193 void av1_free_pc_tree(ThreadData *td, const int num_planes) {
194   if (td->pc_tree != NULL) {
195     const int tree_nodes_inc = 1024;
196     const int tree_nodes = tree_nodes_inc + 256 + 64 + 16 + 4 + 1;
197     for (int i = 0; i < tree_nodes; ++i) {
198       free_tree_contexts(&td->pc_tree[i], num_planes);
199     }
200     for (int i = 0; i < 3; ++i) {
201       aom_free(td->tree_coeff_buf[i]);
202       aom_free(td->tree_qcoeff_buf[i]);
203       aom_free(td->tree_dqcoeff_buf[i]);
204       td->tree_coeff_buf[i] = NULL;
205       td->tree_qcoeff_buf[i] = NULL;
206       td->tree_dqcoeff_buf[i] = NULL;
207     }
208     aom_free(td->pc_tree);
209     td->pc_tree = NULL;
210   }
211 }
212 
av1_copy_tree_context(PICK_MODE_CONTEXT * dst_ctx,PICK_MODE_CONTEXT * src_ctx)213 void av1_copy_tree_context(PICK_MODE_CONTEXT *dst_ctx,
214                            PICK_MODE_CONTEXT *src_ctx) {
215   dst_ctx->mic = src_ctx->mic;
216   dst_ctx->mbmi_ext = src_ctx->mbmi_ext;
217 
218   dst_ctx->num_4x4_blk = src_ctx->num_4x4_blk;
219   dst_ctx->skip = src_ctx->skip;
220   dst_ctx->skippable = src_ctx->skippable;
221   dst_ctx->best_mode_index = src_ctx->best_mode_index;
222 
223   memcpy(dst_ctx->blk_skip, src_ctx->blk_skip,
224          sizeof(uint8_t) * src_ctx->num_4x4_blk);
225 
226   dst_ctx->hybrid_pred_diff = src_ctx->hybrid_pred_diff;
227   dst_ctx->comp_pred_diff = src_ctx->comp_pred_diff;
228   dst_ctx->single_pred_diff = src_ctx->single_pred_diff;
229 
230   dst_ctx->rate = src_ctx->rate;
231   dst_ctx->dist = src_ctx->dist;
232   dst_ctx->rdcost = src_ctx->rdcost;
233   dst_ctx->rd_mode_is_ready = src_ctx->rd_mode_is_ready;
234 
235   memcpy(dst_ctx->pred_mv, src_ctx->pred_mv, sizeof(MV) * REF_FRAMES);
236   dst_ctx->pred_interp_filter = src_ctx->pred_interp_filter;
237 
238   dst_ctx->partition = src_ctx->partition;
239 }
240