Coverage Report

Created: 2026-06-10 06:30

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
802k
                                   struct aom_internal_error_info *error) {
38
802k
  const int num_planes = seq_params->monochrome ? 1 : MAX_MB_PLANE;
39
802k
  const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
40
802k
  const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
41
802k
                                                   seq_params->subsampling_y);
42
2.19M
  for (int i = 0; i < num_planes; i++) {
43
1.39M
    const int max_num_pix =
44
1.39M
        (i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
45
1.39M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->coeff_buf[i],
46
1.39M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
47
1.39M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->qcoeff_buf[i],
48
1.39M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
49
1.39M
    AOM_CHECK_MEM_ERROR(error, shared_bufs->dqcoeff_buf[i],
50
1.39M
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
51
1.39M
  }
52
802k
}
53
54
802k
void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
55
3.20M
  for (int i = 0; i < 3; i++) {
56
2.40M
    aom_free(shared_bufs->coeff_buf[i]);
57
2.40M
    aom_free(shared_bufs->qcoeff_buf[i]);
58
2.40M
    aom_free(shared_bufs->dqcoeff_buf[i]);
59
2.40M
    shared_bufs->coeff_buf[i] = NULL;
60
2.40M
    shared_bufs->qcoeff_buf[i] = NULL;
61
2.40M
    shared_bufs->dqcoeff_buf[i] = NULL;
62
2.40M
  }
63
802k
}
64
65
PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
66
                                 BLOCK_SIZE bsize,
67
20.9M
                                 PC_TREE_SHARED_BUFFERS *shared_bufs) {
68
20.9M
  PICK_MODE_CONTEXT *volatile ctx = NULL;
69
20.9M
  const AV1_COMMON *const cm = &cpi->common;
70
20.9M
  struct aom_internal_error_info error;
71
72
20.9M
  if (setjmp(error.jmp)) {
73
0
    av1_free_pmc(ctx, av1_num_planes(cm));
74
0
    return NULL;
75
0
  }
76
20.9M
  error.setjmp = 1;
77
78
20.9M
  AOM_CHECK_MEM_ERROR(&error, ctx, aom_calloc(1, sizeof(*ctx)));
79
20.9M
  ctx->rd_mode_is_ready = 0;
80
81
20.9M
  const int num_planes = av1_num_planes(cm);
82
20.9M
  const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
83
20.9M
  const int num_blk = num_pix / 16;
84
85
20.9M
  AOM_CHECK_MEM_ERROR(&error, ctx->tx_type_map,
86
20.9M
                      aom_calloc(num_blk, sizeof(*ctx->tx_type_map)));
87
20.9M
  ctx->num_4x4_blk = num_blk;
88
89
59.4M
  for (int i = 0; i < num_planes; ++i) {
90
38.5M
    ctx->coeff[i] = shared_bufs->coeff_buf[i];
91
38.5M
    ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
92
38.5M
    ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
93
38.5M
    AOM_CHECK_MEM_ERROR(&error, ctx->eobs[i],
94
38.5M
                        aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
95
38.5M
    AOM_CHECK_MEM_ERROR(
96
38.5M
        &error, ctx->txb_entropy_ctx[i],
97
38.5M
        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
98
38.5M
  }
99
100
20.9M
  if (num_pix <= MAX_PALETTE_SQUARE) {
101
62.8M
    for (int i = 0; i < 2; ++i) {
102
41.8M
      if (cm->features.allow_screen_content_tools) {
103
3.60M
        AOM_CHECK_MEM_ERROR(
104
3.60M
            &error, ctx->color_index_map[i],
105
3.60M
            aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
106
38.2M
      } else {
107
38.2M
        ctx->color_index_map[i] = NULL;
108
38.2M
      }
109
41.8M
    }
110
20.9M
  }
111
112
20.9M
  av1_invalid_rd_stats(&ctx->rd_stats);
113
114
20.9M
  return ctx;
115
20.9M
}
116
117
1.85M
void av1_reset_pmc(PICK_MODE_CONTEXT *ctx) {
118
1.85M
  av1_zero_array(ctx->tx_type_map, ctx->num_4x4_blk);
119
1.85M
  av1_invalid_rd_stats(&ctx->rd_stats);
120
1.85M
}
121
122
898M
void av1_free_pmc(PICK_MODE_CONTEXT *ctx, int num_planes) {
123
898M
  if (ctx == NULL) return;
124
125
20.7M
  aom_free(ctx->tx_type_map);
126
59.3M
  for (int i = 0; i < num_planes; ++i) {
127
38.5M
    ctx->coeff[i] = NULL;
128
38.5M
    ctx->qcoeff[i] = NULL;
129
38.5M
    ctx->dqcoeff[i] = NULL;
130
38.5M
    aom_free(ctx->eobs[i]);
131
38.5M
    ctx->eobs[i] = NULL;
132
38.5M
    aom_free(ctx->txb_entropy_ctx[i]);
133
38.5M
    ctx->txb_entropy_ctx[i] = NULL;
134
38.5M
  }
135
136
62.6M
  for (int i = 0; i < 2; ++i) {
137
41.8M
    if (ctx->color_index_map[i]) {
138
3.60M
      aom_free(ctx->color_index_map[i]);
139
3.60M
      ctx->color_index_map[i] = NULL;
140
3.60M
    }
141
41.8M
  }
142
143
20.7M
  aom_free(ctx);
144
20.7M
}
145
146
31.4M
PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize) {
147
31.4M
  PC_TREE *pc_tree = aom_calloc(1, sizeof(*pc_tree));
148
31.4M
  if (pc_tree == NULL) return NULL;
149
150
31.4M
  pc_tree->partitioning = PARTITION_NONE;
151
31.4M
  pc_tree->block_size = bsize;
152
153
31.4M
  return pc_tree;
154
31.4M
}
155
156
#define FREE_PMC_NODE(CTX)         \
157
898M
  do {                             \
158
898M
    av1_free_pmc(CTX, num_planes); \
159
898M
    CTX = NULL;                    \
160
898M
  } 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.8M
                                PARTITION_SEARCH_TYPE partition_search_type) {
165
47.8M
  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.9M
  if (partition_search_type == VAR_BASED_PARTITION && !keep_best &&
170
12.9M
      !keep_none) {
171
12.9M
    FREE_PMC_NODE(pc_tree->none);
172
173
38.7M
    for (int i = 0; i < 2; ++i) {
174
25.8M
      FREE_PMC_NODE(pc_tree->horizontal[i]);
175
25.8M
      FREE_PMC_NODE(pc_tree->vertical[i]);
176
25.8M
    }
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.4M
    for (int i = 0; i < 4; ++i) {
192
51.5M
      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.5M
    }
198
12.9M
    aom_free(pc_tree);
199
12.9M
    return;
200
12.9M
  }
201
202
33.9M
  const PARTITION_TYPE partition = pc_tree->partitioning;
203
204
33.9M
  if (!keep_none && (!keep_best || (partition != PARTITION_NONE)))
205
18.5M
    FREE_PMC_NODE(pc_tree->none);
206
207
101M
  for (int i = 0; i < 2; ++i) {
208
67.9M
    if (!keep_best || (partition != PARTITION_HORZ))
209
67.7M
      FREE_PMC_NODE(pc_tree->horizontal[i]);
210
67.9M
    if (!keep_best || (partition != PARTITION_VERT))
211
67.7M
      FREE_PMC_NODE(pc_tree->vertical[i]);
212
67.9M
  }
213
33.9M
#if !CONFIG_REALTIME_ONLY
214
135M
  for (int i = 0; i < 3; ++i) {
215
101M
    if (!keep_best || (partition != PARTITION_HORZ_A))
216
101M
      FREE_PMC_NODE(pc_tree->horizontala[i]);
217
101M
    if (!keep_best || (partition != PARTITION_HORZ_B))
218
101M
      FREE_PMC_NODE(pc_tree->horizontalb[i]);
219
101M
    if (!keep_best || (partition != PARTITION_VERT_A))
220
101M
      FREE_PMC_NODE(pc_tree->verticala[i]);
221
101M
    if (!keep_best || (partition != PARTITION_VERT_B))
222
101M
      FREE_PMC_NODE(pc_tree->verticalb[i]);
223
101M
  }
224
169M
  for (int i = 0; i < 4; ++i) {
225
135M
    if (!keep_best || (partition != PARTITION_HORZ_4))
226
135M
      FREE_PMC_NODE(pc_tree->horizontal4[i]);
227
135M
    if (!keep_best || (partition != PARTITION_VERT_4))
228
135M
      FREE_PMC_NODE(pc_tree->vertical4[i]);
229
135M
  }
230
33.9M
#endif
231
33.9M
  if (!keep_best || (partition != PARTITION_SPLIT)) {
232
159M
    for (int i = 0; i < 4; ++i) {
233
127M
      if (pc_tree->split[i] != NULL) {
234
18.3M
        av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0,
235
18.3M
                                   partition_search_type);
236
18.3M
        pc_tree->split[i] = NULL;
237
18.3M
      }
238
127M
    }
239
31.9M
  }
240
241
33.9M
  if (!keep_best && !keep_none) aom_free(pc_tree);
242
33.9M
}
243
244
331k
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
331k
  if (cpi->oxcf.kf_cfg.key_freq_max == 0) return 0;
249
250
58.9k
  AV1_COMMON *const cm = &cpi->common;
251
58.9k
  const int stat_generation_stage = is_stat_generation_stage(cpi);
252
58.9k
  const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
253
58.9k
  const int tree_nodes =
254
58.9k
      av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
255
58.9k
  int sms_tree_index = 0;
256
58.9k
  SIMPLE_MOTION_DATA_TREE *this_sms;
257
58.9k
  int square_index = 1;
258
58.9k
  int nodes;
259
260
58.9k
  aom_free(td->sms_tree);
261
58.9k
  td->sms_tree =
262
58.9k
      (SIMPLE_MOTION_DATA_TREE *)aom_calloc(tree_nodes, sizeof(*td->sms_tree));
263
58.9k
  if (!td->sms_tree) return -1;
264
58.9k
  this_sms = &td->sms_tree[0];
265
266
58.9k
  if (!stat_generation_stage) {
267
46.3k
    const int leaf_factor = is_sb_size_128 ? 4 : 1;
268
46.3k
    const int leaf_nodes = 256 * leaf_factor;
269
270
    // Sets up all the leaf nodes in the tree.
271
21.5M
    for (sms_tree_index = 0; sms_tree_index < leaf_nodes; ++sms_tree_index) {
272
21.5M
      SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
273
21.5M
      tree->block_size = square[0];
274
21.5M
    }
275
276
    // Each node has 4 leaf nodes, fill each block_size level of the tree
277
    // from leafs to the root.
278
244k
    for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
279
7.36M
      for (int i = 0; i < nodes; ++i) {
280
7.16M
        SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
281
7.16M
        tree->block_size = square[square_index];
282
35.8M
        for (int j = 0; j < 4; j++) tree->split[j] = this_sms++;
283
7.16M
        ++sms_tree_index;
284
7.16M
      }
285
198k
      ++square_index;
286
198k
    }
287
46.3k
  } 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
12.5k
    SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
292
12.5k
    square_index = 2;
293
12.5k
    tree->block_size = square[square_index];
294
12.5k
  }
295
296
  // Set up the root node for the largest superblock size
297
58.9k
  td->sms_root = &td->sms_tree[tree_nodes - 1];
298
58.9k
  return 0;
299
58.9k
}
300
301
802k
void av1_free_sms_tree(ThreadData *td) {
302
802k
  aom_free(td->sms_tree);
303
  td->sms_tree = NULL;
304
802k
}