Coverage Report

Created: 2026-04-01 07:49

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