Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/common/vp9_alloccommon.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "./vpx_config.h"
12
#include "vpx_mem/vpx_mem.h"
13
14
#include "vp9/common/vp9_alloccommon.h"
15
#include "vp9/common/vp9_blockd.h"
16
#include "vp9/common/vp9_entropymode.h"
17
#include "vp9/common/vp9_entropymv.h"
18
#include "vp9/common/vp9_onyxc_int.h"
19
20
void vp9_set_mi_size(int *mi_rows, int *mi_cols, int *mi_stride, int width,
21
120k
                     int height) {
22
120k
  const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
23
120k
  const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
24
120k
  *mi_cols = aligned_width >> MI_SIZE_LOG2;
25
120k
  *mi_rows = aligned_height >> MI_SIZE_LOG2;
26
120k
  *mi_stride = calc_mi_size(*mi_cols);
27
120k
}
28
29
void vp9_set_mb_size(int *mb_rows, int *mb_cols, int *mb_num, int mi_rows,
30
116k
                     int mi_cols) {
31
116k
  *mb_cols = (mi_cols + 1) >> 1;
32
116k
  *mb_rows = (mi_rows + 1) >> 1;
33
116k
  *mb_num = (*mb_rows) * (*mb_cols);
34
116k
}
35
36
113k
void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
37
113k
  vp9_set_mi_size(&cm->mi_rows, &cm->mi_cols, &cm->mi_stride, width, height);
38
113k
  vp9_set_mb_size(&cm->mb_rows, &cm->mb_cols, &cm->MBs, cm->mi_rows,
39
113k
                  cm->mi_cols);
40
113k
}
41
42
21.0k
static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
43
21.0k
  int i;
44
45
63.1k
  for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
46
42.1k
    cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
47
42.1k
    if (cm->seg_map_array[i] == NULL) return 1;
48
42.1k
  }
49
21.0k
  cm->seg_map_alloc_size = seg_map_size;
50
51
  // Init the index.
52
21.0k
  cm->seg_map_idx = 0;
53
21.0k
  cm->prev_seg_map_idx = 1;
54
55
21.0k
  cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
56
21.0k
  cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
57
58
21.0k
  return 0;
59
21.0k
}
60
61
43.3k
static void free_seg_map(VP9_COMMON *cm) {
62
43.3k
  int i;
63
64
130k
  for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
65
86.6k
    vpx_free(cm->seg_map_array[i]);
66
86.6k
    cm->seg_map_array[i] = NULL;
67
86.6k
  }
68
43.3k
  cm->seg_map_alloc_size = 0;
69
70
43.3k
  cm->current_frame_seg_map = NULL;
71
43.3k
  cm->last_frame_seg_map = NULL;
72
43.3k
}
73
74
22.2k
void vp9_free_ref_frame_buffers(BufferPool *pool) {
75
22.2k
  int i;
76
77
22.2k
  if (!pool) return;
78
79
289k
  for (i = 0; i < FRAME_BUFFERS; ++i) {
80
267k
    if (!pool->frame_bufs[i].released &&
81
256k
        pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
82
21.1k
      pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
83
21.1k
      pool->frame_bufs[i].ref_count = 0;
84
21.1k
      pool->frame_bufs[i].released = 1;
85
21.1k
    }
86
267k
    vpx_free(pool->frame_bufs[i].mvs);
87
267k
    pool->frame_bufs[i].mvs = NULL;
88
267k
    vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
89
267k
  }
90
22.2k
}
91
92
0
void vp9_free_postproc_buffers(VP9_COMMON *cm) {
93
#if CONFIG_VP9_POSTPROC
94
  vpx_free_frame_buffer(&cm->post_proc_buffer);
95
  vpx_free_frame_buffer(&cm->post_proc_buffer_int);
96
  vpx_free(cm->postproc_state.limits);
97
  cm->postproc_state.limits = NULL;
98
  vpx_free(cm->postproc_state.generated_noise);
99
  cm->postproc_state.generated_noise = NULL;
100
  cm->postproc_state.generated_noise_size = 0;
101
#else
102
0
  (void)cm;
103
0
#endif
104
0
}
105
106
22.2k
void vp9_free_context_buffers(VP9_COMMON *cm) {
107
22.2k
  if (cm->free_mi) cm->free_mi(cm);
108
22.2k
  free_seg_map(cm);
109
22.2k
  vpx_free(cm->above_context);
110
22.2k
  cm->above_context = NULL;
111
22.2k
  vpx_free(cm->above_seg_context);
112
22.2k
  cm->above_seg_context = NULL;
113
22.2k
  cm->above_context_alloc_cols = 0;
114
22.2k
  vpx_free(cm->lf.lfm);
115
22.2k
  cm->lf.lfm = NULL;
116
22.2k
}
117
118
56.9k
int vp9_alloc_loop_filter(VP9_COMMON *cm) {
119
56.9k
  vpx_free(cm->lf.lfm);
120
  // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region.  The
121
  // stride and rows are rounded up / truncated to a multiple of 8.
122
56.9k
  cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
123
56.9k
  cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
124
56.9k
      ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
125
56.9k
      sizeof(*cm->lf.lfm));
126
56.9k
  if (!cm->lf.lfm) return 1;
127
56.9k
  return 0;
128
56.9k
}
129
130
56.9k
int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
131
56.9k
  int new_mi_size;
132
133
56.9k
  vp9_set_mb_mi(cm, width, height);
134
56.9k
  new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
135
56.9k
  if (cm->mi_alloc_size < new_mi_size) {
136
21.0k
    cm->free_mi(cm);
137
21.0k
    if (cm->alloc_mi(cm, new_mi_size)) goto fail;
138
21.0k
  }
139
56.9k
  if (cm->above_context_alloc_cols < cm->mi_cols) {
140
19.9k
    vpx_free(cm->above_context);
141
19.9k
    cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
142
19.9k
        2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
143
19.9k
        sizeof(*cm->above_context));
144
19.9k
    if (!cm->above_context) goto fail;
145
146
19.9k
    vpx_free(cm->above_seg_context);
147
19.9k
    cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
148
19.9k
        mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
149
19.9k
    if (!cm->above_seg_context) goto fail;
150
19.9k
    cm->above_context_alloc_cols = cm->mi_cols;
151
19.9k
  }
152
153
56.9k
  if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
154
    // Create the segmentation map structure and set to 0.
155
21.0k
    free_seg_map(cm);
156
21.0k
    if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
157
21.0k
  }
158
159
56.9k
  if (vp9_alloc_loop_filter(cm)) goto fail;
160
161
56.9k
  return 0;
162
163
0
fail:
164
  // clear the mi_* values to force a realloc on resync
165
0
  vp9_set_mb_mi(cm, 0, 0);
166
0
  vp9_free_context_buffers(cm);
167
0
  return 1;
168
56.9k
}
169
170
18.4k
void vp9_remove_common(VP9_COMMON *cm) {
171
#if CONFIG_VP9_POSTPROC
172
  vp9_free_postproc_buffers(cm);
173
#endif
174
18.4k
  vp9_free_context_buffers(cm);
175
176
18.4k
  vpx_free(cm->fc);
177
18.4k
  cm->fc = NULL;
178
18.4k
  vpx_free(cm->frame_contexts);
179
18.4k
  cm->frame_contexts = NULL;
180
18.4k
}
181
182
109k
void vp9_init_context_buffers(VP9_COMMON *cm) {
183
109k
  cm->setup_mi(cm);
184
109k
  if (cm->last_frame_seg_map)
185
109k
    memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
186
109k
}
187
188
51.0k
void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
189
  // Swap indices.
190
51.0k
  const int tmp = cm->seg_map_idx;
191
51.0k
  cm->seg_map_idx = cm->prev_seg_map_idx;
192
51.0k
  cm->prev_seg_map_idx = tmp;
193
194
51.0k
  cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
195
51.0k
  cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
196
51.0k
}