Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/context_tree.c
Line
Count
Source (jump to first uncovered line)
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
#include "av1/encoder/rd.h"
15
16
void av1_copy_tree_context(PICK_MODE_CONTEXT *dst_ctx,
17
0
                           PICK_MODE_CONTEXT *src_ctx) {
18
0
  dst_ctx->mic = src_ctx->mic;
19
0
  dst_ctx->mbmi_ext_best = src_ctx->mbmi_ext_best;
20
21
0
  dst_ctx->num_4x4_blk = src_ctx->num_4x4_blk;
22
0
  dst_ctx->skippable = src_ctx->skippable;
23
#if CONFIG_INTERNAL_STATS
24
  dst_ctx->best_mode_index = src_ctx->best_mode_index;
25
#endif  // CONFIG_INTERNAL_STATS
26
27
0
  memcpy(dst_ctx->blk_skip, src_ctx->blk_skip,
28
0
         sizeof(uint8_t) * src_ctx->num_4x4_blk);
29
0
  av1_copy_array(dst_ctx->tx_type_map, src_ctx->tx_type_map,
30
0
                 src_ctx->num_4x4_blk);
31
32
0
  dst_ctx->rd_stats = src_ctx->rd_stats;
33
0
  dst_ctx->rd_mode_is_ready = src_ctx->rd_mode_is_ready;
34
0
}
35
36
void av1_setup_shared_coeff_buffer(const SequenceHeader *const seq_params,
37
                                   PC_TREE_SHARED_BUFFERS *shared_bufs,
38
0
                                   struct aom_internal_error_info *error) {
39
0
  const int num_planes = seq_params->monochrome ? 1 : MAX_MB_PLANE;
40
0
  const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
41
0
  const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
42
0
                                                   seq_params->subsampling_y);
43
0
  for (int i = 0; i < num_planes; i++) {
44
0
    const int max_num_pix =
45
0
        (i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
46
0
    AOM_CHECK_MEM_ERROR(error, shared_bufs->coeff_buf[i],
47
0
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
48
0
    AOM_CHECK_MEM_ERROR(error, shared_bufs->qcoeff_buf[i],
49
0
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
50
0
    AOM_CHECK_MEM_ERROR(error, shared_bufs->dqcoeff_buf[i],
51
0
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
52
0
  }
53
0
}
54
55
0
void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
56
0
  for (int i = 0; i < 3; i++) {
57
0
    aom_free(shared_bufs->coeff_buf[i]);
58
0
    aom_free(shared_bufs->qcoeff_buf[i]);
59
0
    aom_free(shared_bufs->dqcoeff_buf[i]);
60
0
    shared_bufs->coeff_buf[i] = NULL;
61
0
    shared_bufs->qcoeff_buf[i] = NULL;
62
0
    shared_bufs->dqcoeff_buf[i] = NULL;
63
0
  }
64
0
}
65
66
PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
67
                                 BLOCK_SIZE bsize,
68
0
                                 PC_TREE_SHARED_BUFFERS *shared_bufs) {
69
0
  PICK_MODE_CONTEXT *ctx = NULL;
70
0
  const AV1_COMMON *const cm = &cpi->common;
71
0
  struct aom_internal_error_info error;
72
73
0
  AOM_CHECK_MEM_ERROR(&error, ctx, aom_calloc(1, sizeof(*ctx)));
74
0
  ctx->rd_mode_is_ready = 0;
75
76
0
  const int num_planes = av1_num_planes(cm);
77
0
  const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
78
0
  const int num_blk = num_pix / 16;
79
80
0
  AOM_CHECK_MEM_ERROR(&error, ctx->blk_skip,
81
0
                      aom_calloc(num_blk, sizeof(*ctx->blk_skip)));
82
0
  AOM_CHECK_MEM_ERROR(&error, ctx->tx_type_map,
83
0
                      aom_calloc(num_blk, sizeof(*ctx->tx_type_map)));
84
0
  ctx->num_4x4_blk = num_blk;
85
86
0
  for (int i = 0; i < num_planes; ++i) {
87
0
    ctx->coeff[i] = shared_bufs->coeff_buf[i];
88
0
    ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
89
0
    ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
90
0
    AOM_CHECK_MEM_ERROR(&error, ctx->eobs[i],
91
0
                        aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
92
0
    AOM_CHECK_MEM_ERROR(
93
0
        &error, ctx->txb_entropy_ctx[i],
94
0
        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
95
0
  }
96
97
0
  if (num_pix <= MAX_PALETTE_SQUARE) {
98
0
    for (int i = 0; i < 2; ++i) {
99
0
      if (!cpi->sf.rt_sf.use_nonrd_pick_mode || frame_is_intra_only(cm)) {
100
0
        AOM_CHECK_MEM_ERROR(
101
0
            &error, ctx->color_index_map[i],
102
0
            aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
103
0
      } else {
104
0
        ctx->color_index_map[i] = NULL;
105
0
      }
106
0
    }
107
0
  }
108
109
0
  av1_invalid_rd_stats(&ctx->rd_stats);
110
111
0
  return ctx;
112
0
}
113
114
0
void av1_free_pmc(PICK_MODE_CONTEXT *ctx, int num_planes) {
115
0
  if (ctx == NULL) return;
116
117
0
  aom_free(ctx->blk_skip);
118
0
  ctx->blk_skip = NULL;
119
0
  aom_free(ctx->tx_type_map);
120
0
  for (int i = 0; i < num_planes; ++i) {
121
0
    ctx->coeff[i] = NULL;
122
0
    ctx->qcoeff[i] = NULL;
123
0
    ctx->dqcoeff[i] = NULL;
124
0
    aom_free(ctx->eobs[i]);
125
0
    ctx->eobs[i] = NULL;
126
0
    aom_free(ctx->txb_entropy_ctx[i]);
127
0
    ctx->txb_entropy_ctx[i] = NULL;
128
0
  }
129
130
0
  for (int i = 0; i < 2; ++i) {
131
0
    if (ctx->color_index_map[i]) {
132
0
      aom_free(ctx->color_index_map[i]);
133
0
      ctx->color_index_map[i] = NULL;
134
0
    }
135
0
  }
136
137
0
  aom_free(ctx);
138
0
}
139
140
0
PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize) {
141
0
  PC_TREE *pc_tree = NULL;
142
0
  struct aom_internal_error_info error;
143
144
0
  AOM_CHECK_MEM_ERROR(&error, pc_tree, aom_calloc(1, sizeof(*pc_tree)));
145
146
0
  pc_tree->partitioning = PARTITION_NONE;
147
0
  pc_tree->block_size = bsize;
148
0
  pc_tree->index = 0;
149
150
0
  pc_tree->none = NULL;
151
0
  for (int i = 0; i < 2; ++i) {
152
0
    pc_tree->horizontal[i] = NULL;
153
0
    pc_tree->vertical[i] = NULL;
154
0
  }
155
0
  for (int i = 0; i < 3; ++i) {
156
0
    pc_tree->horizontala[i] = NULL;
157
0
    pc_tree->horizontalb[i] = NULL;
158
0
    pc_tree->verticala[i] = NULL;
159
0
    pc_tree->verticalb[i] = NULL;
160
0
  }
161
0
  for (int i = 0; i < 4; ++i) {
162
0
    pc_tree->horizontal4[i] = NULL;
163
0
    pc_tree->vertical4[i] = NULL;
164
0
    pc_tree->split[i] = NULL;
165
0
  }
166
167
0
  return pc_tree;
168
0
}
169
170
#define FREE_PMC_NODE(CTX)         \
171
0
  do {                             \
172
0
    av1_free_pmc(CTX, num_planes); \
173
0
    CTX = NULL;                    \
174
0
  } while (0)
175
176
void av1_free_pc_tree_recursive(PC_TREE *pc_tree, int num_planes, int keep_best,
177
0
                                int keep_none) {
178
0
  if (pc_tree == NULL) return;
179
180
0
  const PARTITION_TYPE partition = pc_tree->partitioning;
181
182
0
  if (!keep_none && (!keep_best || (partition != PARTITION_NONE)))
183
0
    FREE_PMC_NODE(pc_tree->none);
184
185
0
  for (int i = 0; i < 2; ++i) {
186
0
    if (!keep_best || (partition != PARTITION_HORZ))
187
0
      FREE_PMC_NODE(pc_tree->horizontal[i]);
188
0
    if (!keep_best || (partition != PARTITION_VERT))
189
0
      FREE_PMC_NODE(pc_tree->vertical[i]);
190
0
  }
191
0
  for (int i = 0; i < 3; ++i) {
192
0
    if (!keep_best || (partition != PARTITION_HORZ_A))
193
0
      FREE_PMC_NODE(pc_tree->horizontala[i]);
194
0
    if (!keep_best || (partition != PARTITION_HORZ_B))
195
0
      FREE_PMC_NODE(pc_tree->horizontalb[i]);
196
0
    if (!keep_best || (partition != PARTITION_VERT_A))
197
0
      FREE_PMC_NODE(pc_tree->verticala[i]);
198
0
    if (!keep_best || (partition != PARTITION_VERT_B))
199
0
      FREE_PMC_NODE(pc_tree->verticalb[i]);
200
0
  }
201
0
  for (int i = 0; i < 4; ++i) {
202
0
    if (!keep_best || (partition != PARTITION_HORZ_4))
203
0
      FREE_PMC_NODE(pc_tree->horizontal4[i]);
204
0
    if (!keep_best || (partition != PARTITION_VERT_4))
205
0
      FREE_PMC_NODE(pc_tree->vertical4[i]);
206
0
  }
207
208
0
  if (!keep_best || (partition != PARTITION_SPLIT)) {
209
0
    for (int i = 0; i < 4; ++i) {
210
0
      if (pc_tree->split[i] != NULL) {
211
0
        av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0);
212
0
        pc_tree->split[i] = NULL;
213
0
      }
214
0
    }
215
0
  }
216
217
0
  if (!keep_best && !keep_none) aom_free(pc_tree);
218
0
}
219
220
0
void av1_setup_sms_tree(AV1_COMP *const cpi, ThreadData *td) {
221
0
  AV1_COMMON *const cm = &cpi->common;
222
0
  const int stat_generation_stage = is_stat_generation_stage(cpi);
223
0
  const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
224
0
  const int tree_nodes =
225
0
      av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
226
0
  int sms_tree_index = 0;
227
0
  SIMPLE_MOTION_DATA_TREE *this_sms;
228
0
  int square_index = 1;
229
0
  int nodes;
230
231
0
  aom_free(td->sms_tree);
232
0
  CHECK_MEM_ERROR(cm, td->sms_tree,
233
0
                  aom_calloc(tree_nodes, sizeof(*td->sms_tree)));
234
0
  this_sms = &td->sms_tree[0];
235
236
0
  if (!stat_generation_stage) {
237
0
    const int leaf_factor = is_sb_size_128 ? 4 : 1;
238
0
    const int leaf_nodes = 256 * leaf_factor;
239
240
    // Sets up all the leaf nodes in the tree.
241
0
    for (sms_tree_index = 0; sms_tree_index < leaf_nodes; ++sms_tree_index) {
242
0
      SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
243
0
      tree->block_size = square[0];
244
0
    }
245
246
    // Each node has 4 leaf nodes, fill each block_size level of the tree
247
    // from leafs to the root.
248
0
    for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
249
0
      for (int i = 0; i < nodes; ++i) {
250
0
        SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
251
0
        tree->block_size = square[square_index];
252
0
        for (int j = 0; j < 4; j++) tree->split[j] = this_sms++;
253
0
        ++sms_tree_index;
254
0
      }
255
0
      ++square_index;
256
0
    }
257
0
  } else {
258
    // Allocation for firstpass/LAP stage
259
    // TODO(Mufaddal): refactor square_index to use a common block_size macro
260
    // from firstpass.c
261
0
    SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
262
0
    square_index = 2;
263
0
    tree->block_size = square[square_index];
264
0
  }
265
266
  // Set up the root node for the largest superblock size
267
0
  td->sms_root = &td->sms_tree[tree_nodes - 1];
268
0
}
269
270
0
void av1_free_sms_tree(ThreadData *td) {
271
0
  if (td->sms_tree != NULL) {
272
0
    aom_free(td->sms_tree);
273
0
    td->sms_tree = NULL;
274
0
  }
275
0
}