Coverage Report

Created: 2026-07-16 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/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
817k
                                   struct aom_internal_error_info *error) {
38
817k
  const int num_planes = seq_params->monochrome ? 1 : MAX_MB_PLANE;
39
817k
  const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
40
817k
  const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
41
817k
                                                   seq_params->subsampling_y);
42
2.23M
  for (int i = 0; i < num_planes; i++) {
43
1.41M
    const int max_num_pix =
44
1.41M
        (i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
45
1.41M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->coeff_buf[i],
46
1.41M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
47
1.41M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->qcoeff_buf[i],
48
1.41M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
49
1.41M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->dqcoeff_buf[i],
50
1.41M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
51
1.41M
  }
52
817k
}
53
54
817k
void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
55
3.26M
  for (int i = 0; i < 3; i++) {
56
2.45M
    aom_free(shared_bufs->coeff_buf[i]);
57
2.45M
    aom_free(shared_bufs->qcoeff_buf[i]);
58
2.45M
    aom_free(shared_bufs->dqcoeff_buf[i]);
59
2.45M
    shared_bufs->coeff_buf[i] = NULL;
60
2.45M
    shared_bufs->qcoeff_buf[i] = NULL;
61
2.45M
    shared_bufs->dqcoeff_buf[i] = NULL;
62
2.45M
  }
63
817k
}
64
65
PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
66
                                 BLOCK_SIZE bsize,
67
20.6M
                                 PC_TREE_SHARED_BUFFERS *shared_bufs) {
68
20.6M
  PICK_MODE_CONTEXT *volatile ctx = NULL;
69
20.6M
  const AV1_COMMON *const cm = &cpi->common;
70
20.6M
  struct aom_internal_error_info error;
71
72
20.6M
  if (setjmp(error.jmp)) {
73
0
    av1_free_pmc(ctx, av1_num_planes(cm));
74
0
    return NULL;
75
0
  }
76
20.6M
  error.setjmp = 1;
77
78
20.6M
  AOM_CHECK_MEM_ERROR(&error, ctx, aom_calloc(1, sizeof(*ctx)));
79
20.6M
  ctx->rd_mode_is_ready = 0;
80
81
20.6M
  const int num_planes = av1_num_planes(cm);
82
20.6M
  const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
83
20.6M
  const int num_blk = num_pix / 16;
84
85
20.6M
  AOM_CHECK_MEM_ERROR(&error, ctx->tx_type_map,
86
20.6M
                      aom_calloc(num_blk, sizeof(*ctx->tx_type_map)));
87
20.6M
  ctx->num_4x4_blk = num_blk;
88
89
58.6M
  for (int i = 0; i < num_planes; ++i) {
90
37.9M
    ctx->coeff[i] = shared_bufs->coeff_buf[i];
91
37.9M
    ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
92
37.9M
    ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
93
37.9M
    AOM_CHECK_MEM_ERROR(&error, ctx->eobs[i],
94
37.9M
                        aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
95
37.9M
    AOM_CHECK_MEM_ERROR(
96
37.9M
        &error, ctx->txb_entropy_ctx[i],
97
37.9M
        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
98
37.9M
  }
99
100
20.7M
  if (num_pix <= MAX_PALETTE_SQUARE) {
101
62.1M
    for (int i = 0; i < 2; ++i) {
102
41.3M
      if (cm->features.allow_screen_content_tools) {
103
3.78M
        AOM_CHECK_MEM_ERROR(
104
3.78M
            &error, ctx->color_index_map[i],
105
3.78M
            aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
106
37.6M
      } else {
107
37.6M
        ctx->color_index_map[i] = NULL;
108
37.6M
      }
109
41.3M
    }
110
20.7M
  }
111
112
20.6M
  av1_invalid_rd_stats(&ctx->rd_stats);
113
114
20.6M
  return ctx;
115
20.6M
}
116
117
1.78M
void av1_reset_pmc(PICK_MODE_CONTEXT *ctx) {
118
1.78M
  av1_zero_array(ctx->tx_type_map, ctx->num_4x4_blk);
119
1.78M
  av1_invalid_rd_stats(&ctx->rd_stats);
120
1.78M
}
121
122
889M
void av1_free_pmc(PICK_MODE_CONTEXT *ctx, int num_planes) {
123
889M
  if (ctx == NULL) return;
124
125
20.5M
  aom_free(ctx->tx_type_map);
126
58.4M
  for (int i = 0; i < num_planes; ++i) {
127
37.9M
    ctx->coeff[i] = NULL;
128
37.9M
    ctx->qcoeff[i] = NULL;
129
37.9M
    ctx->dqcoeff[i] = NULL;
130
37.9M
    aom_free(ctx->eobs[i]);
131
37.9M
    ctx->eobs[i] = NULL;
132
37.9M
    aom_free(ctx->txb_entropy_ctx[i]);
133
37.9M
    ctx->txb_entropy_ctx[i] = NULL;
134
37.9M
  }
135
136
61.9M
  for (int i = 0; i < 2; ++i) {
137
41.3M
    if (ctx->color_index_map[i]) {
138
3.78M
      aom_free(ctx->color_index_map[i]);
139
3.78M
      ctx->color_index_map[i] = NULL;
140
3.78M
    }
141
41.3M
  }
142
143
20.5M
  aom_free(ctx);
144
20.5M
}
145
146
31.3M
PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize) {
147
31.3M
  PC_TREE *pc_tree = aom_calloc(1, sizeof(*pc_tree));
148
31.3M
  if (pc_tree == NULL) return NULL;
149
150
31.3M
  pc_tree->partitioning = PARTITION_NONE;
151
31.3M
  pc_tree->block_size = bsize;
152
153
31.3M
  return pc_tree;
154
31.3M
}
155
156
#define FREE_PMC_NODE(CTX)         \
157
889M
  do {                             \
158
889M
    av1_free_pmc(CTX, num_planes); \
159
889M
    CTX = NULL;                    \
160
889M
  } 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
47.4M
                                PARTITION_SEARCH_TYPE partition_search_type) {
165
47.4M
  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
46.4M
  if (partition_search_type == VAR_BASED_PARTITION && !keep_best &&
170
12.8M
      !keep_none) {
171
12.8M
    FREE_PMC_NODE(pc_tree->none);
172
173
38.5M
    for (int i = 0; i < 2; ++i) {
174
25.7M
      FREE_PMC_NODE(pc_tree->horizontal[i]);
175
25.7M
      FREE_PMC_NODE(pc_tree->vertical[i]);
176
25.7M
    }
177
178
#if !defined(NDEBUG) && !CONFIG_REALTIME_ONLY
179
    for (int i = 0; i < 3; ++i) {
180
      assert(pc_tree->horizontala[i] == NULL);
181
      assert(pc_tree->horizontalb[i] == NULL);
182
      assert(pc_tree->verticala[i] == NULL);
183
      assert(pc_tree->verticalb[i] == NULL);
184
    }
185
    for (int i = 0; i < 4; ++i) {
186
      assert(pc_tree->horizontal4[i] == NULL);
187
      assert(pc_tree->vertical4[i] == NULL);
188
    }
189
#endif
190
191
64.1M
    for (int i = 0; i < 4; ++i) {
192
51.3M
      if (pc_tree->split[i] != NULL) {
193
12.7M
        av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0,
194
12.7M
                                   partition_search_type);
195
12.7M
        pc_tree->split[i] = NULL;
196
12.7M
      }
197
51.3M
    }
198
12.8M
    aom_free(pc_tree);
199
12.8M
    return;
200
12.8M
  }
201
202
33.6M
  const PARTITION_TYPE partition = pc_tree->partitioning;
203
204
33.6M
  if (!keep_none && (!keep_best || (partition != PARTITION_NONE)))
205
18.4M
    FREE_PMC_NODE(pc_tree->none);
206
207
100M
  for (int i = 0; i < 2; ++i) {
208
67.2M
    if (!keep_best || (partition != PARTITION_HORZ))
209
66.9M
      FREE_PMC_NODE(pc_tree->horizontal[i]);
210
67.2M
    if (!keep_best || (partition != PARTITION_VERT))
211
67.0M
      FREE_PMC_NODE(pc_tree->vertical[i]);
212
67.2M
  }
213
33.6M
#if !CONFIG_REALTIME_ONLY
214
134M
  for (int i = 0; i < 3; ++i) {
215
100M
    if (!keep_best || (partition != PARTITION_HORZ_A))
216
100M
      FREE_PMC_NODE(pc_tree->horizontala[i]);
217
100M
    if (!keep_best || (partition != PARTITION_HORZ_B))
218
100M
      FREE_PMC_NODE(pc_tree->horizontalb[i]);
219
100M
    if (!keep_best || (partition != PARTITION_VERT_A))
220
100M
      FREE_PMC_NODE(pc_tree->verticala[i]);
221
100M
    if (!keep_best || (partition != PARTITION_VERT_B))
222
100M
      FREE_PMC_NODE(pc_tree->verticalb[i]);
223
100M
  }
224
168M
  for (int i = 0; i < 4; ++i) {
225
134M
    if (!keep_best || (partition != PARTITION_HORZ_4))
226
134M
      FREE_PMC_NODE(pc_tree->horizontal4[i]);
227
134M
    if (!keep_best || (partition != PARTITION_VERT_4))
228
134M
      FREE_PMC_NODE(pc_tree->vertical4[i]);
229
134M
  }
230
33.6M
#endif
231
33.6M
  if (!keep_best || (partition != PARTITION_SPLIT)) {
232
157M
    for (int i = 0; i < 4; ++i) {
233
126M
      if (pc_tree->split[i] != NULL) {
234
18.1M
        av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0,
235
18.1M
                                   partition_search_type);
236
18.1M
        pc_tree->split[i] = NULL;
237
18.1M
      }
238
126M
    }
239
31.5M
  }
240
241
33.6M
  if (!keep_best && !keep_none) aom_free(pc_tree);
242
33.6M
}
243
244
337k
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
337k
  if (cpi->oxcf.kf_cfg.key_freq_max == 0) return 0;
249
250
61.8k
  AV1_COMMON *const cm = &cpi->common;
251
61.8k
  const int stat_generation_stage = is_stat_generation_stage(cpi);
252
61.8k
  const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
253
61.8k
  const int tree_nodes =
254
61.8k
      av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
255
61.8k
  int sms_tree_index = 0;
256
61.8k
  SIMPLE_MOTION_DATA_TREE *this_sms;
257
61.8k
  int square_index = 1;
258
61.8k
  int nodes;
259
260
61.8k
  aom_free(td->sms_tree);
261
61.8k
  td->sms_tree =
262
61.8k
      (SIMPLE_MOTION_DATA_TREE *)aom_calloc(tree_nodes, sizeof(*td->sms_tree));
263
61.8k
  if (!td->sms_tree) return -1;
264
61.8k
  this_sms = &td->sms_tree[0];
265
266
61.8k
  if (!stat_generation_stage) {
267
47.8k
    const int leaf_factor = is_sb_size_128 ? 4 : 1;
268
47.8k
    const int leaf_nodes = 256 * leaf_factor;
269
270
    // Sets up all the leaf nodes in the tree.
271
22.5M
    for (sms_tree_index = 0; sms_tree_index < leaf_nodes; ++sms_tree_index) {
272
22.4M
      SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
273
22.4M
      tree->block_size = square[0];
274
22.4M
    }
275
276
    // Each node has 4 leaf nodes, fill each block_size level of the tree
277
    // from leafs to the root.
278
252k
    for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
279
7.68M
      for (int i = 0; i < nodes; ++i) {
280
7.48M
        SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
281
7.48M
        tree->block_size = square[square_index];
282
37.4M
        for (int j = 0; j < 4; j++) tree->split[j] = this_sms++;
283
7.48M
        ++sms_tree_index;
284
7.48M
      }
285
204k
      ++square_index;
286
204k
    }
287
47.8k
  } 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
13.9k
    SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
292
13.9k
    square_index = 2;
293
13.9k
    tree->block_size = square[square_index];
294
13.9k
  }
295
296
  // Set up the root node for the largest superblock size
297
61.8k
  td->sms_root = &td->sms_tree[tree_nodes - 1];
298
61.8k
  return 0;
299
61.8k
}
300
301
817k
void av1_free_sms_tree(ThreadData *td) {
302
817k
  aom_free(td->sms_tree);
303
  td->sms_tree = NULL;
304
817k
}