Coverage Report

Created: 2026-02-26 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
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
2.47k
                                   struct aom_internal_error_info *error) {
39
2.47k
  const int num_planes = seq_params->monochrome ? 1 : MAX_MB_PLANE;
40
2.47k
  const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
41
2.47k
  const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
42
2.47k
                                                   seq_params->subsampling_y);
43
9.89k
  for (int i = 0; i < num_planes; i++) {
44
7.42k
    const int max_num_pix =
45
7.42k
        (i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
46
7.42k
    AOM_CHECK_MEM_ERROR(error, shared_bufs->coeff_buf[i],
47
7.42k
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
48
7.42k
    AOM_CHECK_MEM_ERROR(error, shared_bufs->qcoeff_buf[i],
49
7.42k
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
50
7.42k
    AOM_CHECK_MEM_ERROR(error, shared_bufs->dqcoeff_buf[i],
51
7.42k
                        aom_memalign(32, max_num_pix * sizeof(tran_low_t)));
52
7.42k
  }
53
2.47k
}
54
55
2.47k
void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
56
9.89k
  for (int i = 0; i < 3; i++) {
57
7.42k
    aom_free(shared_bufs->coeff_buf[i]);
58
7.42k
    aom_free(shared_bufs->qcoeff_buf[i]);
59
7.42k
    aom_free(shared_bufs->dqcoeff_buf[i]);
60
7.42k
    shared_bufs->coeff_buf[i] = NULL;
61
7.42k
    shared_bufs->qcoeff_buf[i] = NULL;
62
7.42k
    shared_bufs->dqcoeff_buf[i] = NULL;
63
7.42k
  }
64
2.47k
}
65
66
PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
67
                                 BLOCK_SIZE bsize,
68
80.1k
                                 PC_TREE_SHARED_BUFFERS *shared_bufs) {
69
80.1k
  PICK_MODE_CONTEXT *ctx = NULL;
70
80.1k
  const AV1_COMMON *const cm = &cpi->common;
71
80.1k
  struct aom_internal_error_info error;
72
73
80.1k
  AOM_CHECK_MEM_ERROR(&error, ctx, aom_calloc(1, sizeof(*ctx)));
74
80.1k
  ctx->rd_mode_is_ready = 0;
75
76
80.1k
  const int num_planes = av1_num_planes(cm);
77
80.1k
  const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
78
80.1k
  const int num_blk = num_pix / 16;
79
80
80.1k
  AOM_CHECK_MEM_ERROR(&error, ctx->blk_skip,
81
80.1k
                      aom_calloc(num_blk, sizeof(*ctx->blk_skip)));
82
80.1k
  AOM_CHECK_MEM_ERROR(&error, ctx->tx_type_map,
83
80.1k
                      aom_calloc(num_blk, sizeof(*ctx->tx_type_map)));
84
80.1k
  ctx->num_4x4_blk = num_blk;
85
86
320k
  for (int i = 0; i < num_planes; ++i) {
87
240k
    ctx->coeff[i] = shared_bufs->coeff_buf[i];
88
240k
    ctx->qcoeff[i] = shared_bufs->qcoeff_buf[i];
89
240k
    ctx->dqcoeff[i] = shared_bufs->dqcoeff_buf[i];
90
240k
    AOM_CHECK_MEM_ERROR(&error, ctx->eobs[i],
91
240k
                        aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
92
240k
    AOM_CHECK_MEM_ERROR(
93
240k
        &error, ctx->txb_entropy_ctx[i],
94
240k
        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
95
240k
  }
96
97
80.1k
  if (num_pix <= MAX_PALETTE_SQUARE) {
98
240k
    for (int i = 0; i < 2; ++i) {
99
160k
      if (!cpi->sf.rt_sf.use_nonrd_pick_mode || frame_is_intra_only(cm)) {
100
160k
        AOM_CHECK_MEM_ERROR(
101
160k
            &error, ctx->color_index_map[i],
102
160k
            aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
103
160k
      } else {
104
0
        ctx->color_index_map[i] = NULL;
105
0
      }
106
160k
    }
107
80.1k
  }
108
109
80.1k
  av1_invalid_rd_stats(&ctx->rd_stats);
110
111
80.1k
  return ctx;
112
80.1k
}
113
114
5.95M
void av1_free_pmc(PICK_MODE_CONTEXT *ctx, int num_planes) {
115
5.95M
  if (ctx == NULL) return;
116
117
80.0k
  aom_free(ctx->blk_skip);
118
80.0k
  ctx->blk_skip = NULL;
119
80.0k
  aom_free(ctx->tx_type_map);
120
320k
  for (int i = 0; i < num_planes; ++i) {
121
240k
    ctx->coeff[i] = NULL;
122
240k
    ctx->qcoeff[i] = NULL;
123
240k
    ctx->dqcoeff[i] = NULL;
124
240k
    aom_free(ctx->eobs[i]);
125
240k
    ctx->eobs[i] = NULL;
126
240k
    aom_free(ctx->txb_entropy_ctx[i]);
127
240k
    ctx->txb_entropy_ctx[i] = NULL;
128
240k
  }
129
130
240k
  for (int i = 0; i < 2; ++i) {
131
160k
    if (ctx->color_index_map[i]) {
132
160k
      aom_free(ctx->color_index_map[i]);
133
160k
      ctx->color_index_map[i] = NULL;
134
160k
    }
135
160k
  }
136
137
80.0k
  aom_free(ctx);
138
80.0k
}
139
140
177k
PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize) {
141
177k
  PC_TREE *pc_tree = NULL;
142
177k
  struct aom_internal_error_info error;
143
144
177k
  AOM_CHECK_MEM_ERROR(&error, pc_tree, aom_calloc(1, sizeof(*pc_tree)));
145
146
177k
  pc_tree->partitioning = PARTITION_NONE;
147
177k
  pc_tree->block_size = bsize;
148
177k
  pc_tree->index = 0;
149
150
177k
  pc_tree->none = NULL;
151
531k
  for (int i = 0; i < 2; ++i) {
152
354k
    pc_tree->horizontal[i] = NULL;
153
354k
    pc_tree->vertical[i] = NULL;
154
354k
  }
155
708k
  for (int i = 0; i < 3; ++i) {
156
531k
    pc_tree->horizontala[i] = NULL;
157
531k
    pc_tree->horizontalb[i] = NULL;
158
531k
    pc_tree->verticala[i] = NULL;
159
531k
    pc_tree->verticalb[i] = NULL;
160
531k
  }
161
885k
  for (int i = 0; i < 4; ++i) {
162
708k
    pc_tree->horizontal4[i] = NULL;
163
708k
    pc_tree->vertical4[i] = NULL;
164
708k
    pc_tree->split[i] = NULL;
165
708k
  }
166
167
177k
  return pc_tree;
168
177k
}
169
170
#define FREE_PMC_NODE(CTX)         \
171
5.95M
  do {                             \
172
5.95M
    av1_free_pmc(CTX, num_planes); \
173
5.95M
    CTX = NULL;                    \
174
5.95M
  } while (0)
175
176
void av1_free_pc_tree_recursive(PC_TREE *pc_tree, int num_planes, int keep_best,
177
241k
                                int keep_none) {
178
241k
  if (pc_tree == NULL) return;
179
180
241k
  const PARTITION_TYPE partition = pc_tree->partitioning;
181
182
241k
  if (!keep_none && (!keep_best || (partition != PARTITION_NONE)))
183
177k
    FREE_PMC_NODE(pc_tree->none);
184
185
723k
  for (int i = 0; i < 2; ++i) {
186
482k
    if (!keep_best || (partition != PARTITION_HORZ))
187
474k
      FREE_PMC_NODE(pc_tree->horizontal[i]);
188
482k
    if (!keep_best || (partition != PARTITION_VERT))
189
474k
      FREE_PMC_NODE(pc_tree->vertical[i]);
190
482k
  }
191
965k
  for (int i = 0; i < 3; ++i) {
192
723k
    if (!keep_best || (partition != PARTITION_HORZ_A))
193
723k
      FREE_PMC_NODE(pc_tree->horizontala[i]);
194
723k
    if (!keep_best || (partition != PARTITION_HORZ_B))
195
723k
      FREE_PMC_NODE(pc_tree->horizontalb[i]);
196
723k
    if (!keep_best || (partition != PARTITION_VERT_A))
197
723k
      FREE_PMC_NODE(pc_tree->verticala[i]);
198
723k
    if (!keep_best || (partition != PARTITION_VERT_B))
199
723k
      FREE_PMC_NODE(pc_tree->verticalb[i]);
200
723k
  }
201
1.20M
  for (int i = 0; i < 4; ++i) {
202
965k
    if (!keep_best || (partition != PARTITION_HORZ_4))
203
965k
      FREE_PMC_NODE(pc_tree->horizontal4[i]);
204
965k
    if (!keep_best || (partition != PARTITION_VERT_4))
205
965k
      FREE_PMC_NODE(pc_tree->vertical4[i]);
206
965k
  }
207
208
241k
  if (!keep_best || (partition != PARTITION_SPLIT)) {
209
1.19M
    for (int i = 0; i < 4; ++i) {
210
952k
      if (pc_tree->split[i] != NULL) {
211
168k
        av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0);
212
168k
        pc_tree->split[i] = NULL;
213
168k
      }
214
952k
    }
215
238k
  }
216
217
241k
  if (!keep_best && !keep_none) aom_free(pc_tree);
218
241k
}
219
220
2.47k
void av1_setup_sms_tree(AV1_COMP *const cpi, ThreadData *td) {
221
2.47k
  AV1_COMMON *const cm = &cpi->common;
222
2.47k
  const int stat_generation_stage = is_stat_generation_stage(cpi);
223
2.47k
  const int is_sb_size_128 = cm->seq_params->sb_size == BLOCK_128X128;
224
2.47k
  const int tree_nodes =
225
2.47k
      av1_get_pc_tree_nodes(is_sb_size_128, stat_generation_stage);
226
2.47k
  int sms_tree_index = 0;
227
2.47k
  SIMPLE_MOTION_DATA_TREE *this_sms;
228
2.47k
  int square_index = 1;
229
2.47k
  int nodes;
230
231
2.47k
  aom_free(td->sms_tree);
232
2.47k
  CHECK_MEM_ERROR(cm, td->sms_tree,
233
2.47k
                  aom_calloc(tree_nodes, sizeof(*td->sms_tree)));
234
2.47k
  this_sms = &td->sms_tree[0];
235
236
2.47k
  if (!stat_generation_stage) {
237
2.47k
    const int leaf_factor = is_sb_size_128 ? 4 : 1;
238
2.47k
    const int leaf_nodes = 256 * leaf_factor;
239
240
    // Sets up all the leaf nodes in the tree.
241
1.36M
    for (sms_tree_index = 0; sms_tree_index < leaf_nodes; ++sms_tree_index) {
242
1.36M
      SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
243
1.36M
      tree->block_size = square[0];
244
1.36M
    }
245
246
    // Each node has 4 leaf nodes, fill each block_size level of the tree
247
    // from leafs to the root.
248
13.3k
    for (nodes = leaf_nodes >> 2; nodes > 0; nodes >>= 2) {
249
465k
      for (int i = 0; i < nodes; ++i) {
250
454k
        SIMPLE_MOTION_DATA_TREE *const tree = &td->sms_tree[sms_tree_index];
251
454k
        tree->block_size = square[square_index];
252
2.27M
        for (int j = 0; j < 4; j++) tree->split[j] = this_sms++;
253
454k
        ++sms_tree_index;
254
454k
      }
255
10.8k
      ++square_index;
256
10.8k
    }
257
2.47k
  } 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
2.47k
  td->sms_root = &td->sms_tree[tree_nodes - 1];
268
2.47k
}
269
270
2.47k
void av1_free_sms_tree(ThreadData *td) {
271
2.47k
  if (td->sms_tree != NULL) {
272
2.47k
    aom_free(td->sms_tree);
273
    td->sms_tree = NULL;
274
2.47k
  }
275
2.47k
}