Coverage Report

Created: 2022-08-24 06:17

/src/aom/aom_scale/generic/yv12config.c
Line
Count
Source (jump to first uncovered line)
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 <assert.h>
13
14
#include "aom/internal/aom_image_internal.h"
15
#include "aom_mem/aom_mem.h"
16
#include "aom_ports/mem.h"
17
#include "aom_scale/yv12config.h"
18
#include "av1/common/enums.h"
19
20
/****************************************************************************
21
 *  Exports
22
 ****************************************************************************/
23
24
/****************************************************************************
25
 *
26
 ****************************************************************************/
27
28
// TODO(jkoleszar): Maybe replace this with struct aom_image
29
124k
int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
30
124k
  if (ybf) {
31
124k
    if (ybf->buffer_alloc_sz > 0) {
32
3.56k
      aom_free(ybf->buffer_alloc);
33
3.56k
    }
34
124k
    if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
35
124k
    aom_remove_metadata_from_frame_buffer(ybf);
36
    /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
37
      u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
38
      all of this so that a freed pointer isn't inadvertently used */
39
124k
    memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
40
124k
    return 0;
41
124k
  }
42
43
0
  return AOM_CODEC_MEM_ERROR;
44
124k
}
45
46
static int realloc_frame_buffer_aligned(
47
    YV12_BUFFER_CONFIG *ybf, int width, int height, int ss_x, int ss_y,
48
    int use_highbitdepth, int border, int byte_alignment,
49
    aom_codec_frame_buffer_t *fb, aom_get_frame_buffer_cb_fn_t cb,
50
    void *cb_priv, const int y_stride, const uint64_t yplane_size,
51
    const uint64_t uvplane_size, const int aligned_width,
52
    const int aligned_height, const int uv_width, const int uv_height,
53
    const int uv_stride, const int uv_border_w, const int uv_border_h,
54
6.80k
    int alloc_y_buffer_8bit) {
55
6.80k
  if (ybf) {
56
6.80k
    const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
57
6.80k
    const uint64_t frame_size =
58
6.80k
        (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
59
60
6.80k
    uint8_t *buf = NULL;
61
62
6.80k
#if defined AOM_MAX_ALLOCABLE_MEMORY
63
    // The size of ybf->buffer_alloc.
64
6.80k
    uint64_t alloc_size = frame_size;
65
    // The size of ybf->y_buffer_8bit.
66
6.80k
    if (use_highbitdepth) alloc_size += yplane_size;
67
    // The decoder may allocate REF_FRAMES frame buffers in the frame buffer
68
    // pool. Bound the total amount of allocated memory as if these REF_FRAMES
69
    // frame buffers were allocated in a single allocation.
70
6.80k
    if (alloc_size > AOM_MAX_ALLOCABLE_MEMORY / REF_FRAMES)
71
0
      return AOM_CODEC_MEM_ERROR;
72
6.80k
#endif
73
74
6.80k
    if (cb != NULL) {
75
1.98k
      const int align_addr_extra_size = 31;
76
1.98k
      const uint64_t external_frame_size = frame_size + align_addr_extra_size;
77
78
1.98k
      assert(fb != NULL);
79
80
1.98k
      if (external_frame_size != (size_t)external_frame_size)
81
0
        return AOM_CODEC_MEM_ERROR;
82
83
      // Allocation to hold larger frame, or first allocation.
84
1.98k
      if (cb(cb_priv, (size_t)external_frame_size, fb) < 0)
85
0
        return AOM_CODEC_MEM_ERROR;
86
87
1.98k
      if (fb->data == NULL || fb->size < external_frame_size)
88
0
        return AOM_CODEC_MEM_ERROR;
89
90
1.98k
      ybf->buffer_alloc = (uint8_t *)aom_align_addr(fb->data, 32);
91
92
1.98k
#if defined(__has_feature)
93
#if __has_feature(memory_sanitizer)
94
      // This memset is needed for fixing the issue of using uninitialized
95
      // value in msan test. It will cause a perf loss, so only do this for
96
      // msan test.
97
      memset(ybf->buffer_alloc, 0, (size_t)frame_size);
98
#endif
99
1.98k
#endif
100
4.82k
    } else if (frame_size > ybf->buffer_alloc_sz) {
101
      // Allocation to hold larger frame, or first allocation.
102
3.56k
      aom_free(ybf->buffer_alloc);
103
3.56k
      ybf->buffer_alloc = NULL;
104
3.56k
      ybf->buffer_alloc_sz = 0;
105
106
3.56k
      if (frame_size != (size_t)frame_size) return AOM_CODEC_MEM_ERROR;
107
108
3.56k
      ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
109
3.56k
      if (!ybf->buffer_alloc) return AOM_CODEC_MEM_ERROR;
110
111
3.56k
      ybf->buffer_alloc_sz = (size_t)frame_size;
112
113
      // This memset is needed for fixing valgrind error from C loop filter
114
      // due to access uninitialized memory in frame border. It could be
115
      // removed if border is totally removed.
116
3.56k
      memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
117
3.56k
    }
118
119
6.80k
    ybf->y_crop_width = width;
120
6.80k
    ybf->y_crop_height = height;
121
6.80k
    ybf->y_width = aligned_width;
122
6.80k
    ybf->y_height = aligned_height;
123
6.80k
    ybf->y_stride = y_stride;
124
125
6.80k
    ybf->uv_crop_width = (width + ss_x) >> ss_x;
126
6.80k
    ybf->uv_crop_height = (height + ss_y) >> ss_y;
127
6.80k
    ybf->uv_width = uv_width;
128
6.80k
    ybf->uv_height = uv_height;
129
6.80k
    ybf->uv_stride = uv_stride;
130
131
6.80k
    ybf->border = border;
132
6.80k
    ybf->frame_size = (size_t)frame_size;
133
6.80k
    ybf->subsampling_x = ss_x;
134
6.80k
    ybf->subsampling_y = ss_y;
135
136
6.80k
    buf = ybf->buffer_alloc;
137
6.80k
    if (use_highbitdepth) {
138
      // Store uint16 addresses when using 16bit framebuffers
139
1.09k
      buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
140
1.09k
      ybf->flags = YV12_FLAG_HIGHBITDEPTH;
141
5.71k
    } else {
142
5.71k
      ybf->flags = 0;
143
5.71k
    }
144
145
6.80k
    ybf->y_buffer = (uint8_t *)aom_align_addr(
146
6.80k
        buf + (border * y_stride) + border, aom_byte_align);
147
6.80k
    ybf->u_buffer = (uint8_t *)aom_align_addr(
148
6.80k
        buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
149
6.80k
        aom_byte_align);
150
6.80k
    ybf->v_buffer =
151
6.80k
        (uint8_t *)aom_align_addr(buf + yplane_size + uvplane_size +
152
6.80k
                                      (uv_border_h * uv_stride) + uv_border_w,
153
6.80k
                                  aom_byte_align);
154
155
6.80k
    ybf->use_external_reference_buffers = 0;
156
157
6.80k
    if (use_highbitdepth && alloc_y_buffer_8bit) {
158
0
      if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
159
0
      ybf->y_buffer_8bit = (uint8_t *)aom_memalign(32, (size_t)yplane_size);
160
0
      if (!ybf->y_buffer_8bit) return AOM_CODEC_MEM_ERROR;
161
6.80k
    } else {
162
6.80k
      if (ybf->y_buffer_8bit) {
163
0
        aom_free(ybf->y_buffer_8bit);
164
0
        ybf->y_buffer_8bit = NULL;
165
0
        ybf->buf_8bit_valid = 0;
166
0
      }
167
6.80k
    }
168
169
6.80k
    ybf->corrupted = 0; /* assume not corrupted by errors */
170
6.80k
    return 0;
171
6.80k
  }
172
0
  return AOM_CODEC_MEM_ERROR;
173
6.80k
}
174
175
static int calc_stride_and_planesize(const int ss_x, const int ss_y,
176
                                     const int aligned_width,
177
                                     const int aligned_height, const int border,
178
                                     const int byte_alignment, int *y_stride,
179
                                     int *uv_stride, uint64_t *yplane_size,
180
                                     uint64_t *uvplane_size,
181
6.80k
                                     const int uv_height) {
182
  /* Only support allocating buffers that have a border that's a multiple
183
   * of 32. The border restriction is required to get 16-byte alignment of
184
   * the start of the chroma rows without introducing an arbitrary gap
185
   * between planes, which would break the semantics of things like
186
   * aom_img_set_rect(). */
187
6.80k
  if (border & 0x1f) return AOM_CODEC_MEM_ERROR;
188
6.80k
  *y_stride = aom_calc_y_stride(aligned_width, border);
189
6.80k
  *yplane_size =
190
6.80k
      (aligned_height + 2 * border) * (uint64_t)(*y_stride) + byte_alignment;
191
192
6.80k
  *uv_stride = *y_stride >> ss_x;
193
6.80k
  *uvplane_size = (uv_height + 2 * (border >> ss_y)) * (uint64_t)(*uv_stride) +
194
6.80k
                  byte_alignment;
195
6.80k
  return 0;
196
6.80k
}
197
198
int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
199
                             int ss_x, int ss_y, int use_highbitdepth,
200
                             int border, int byte_alignment,
201
                             aom_codec_frame_buffer_t *fb,
202
                             aom_get_frame_buffer_cb_fn_t cb, void *cb_priv,
203
6.80k
                             int alloc_y_buffer_8bit) {
204
6.80k
#if CONFIG_SIZE_LIMIT
205
6.80k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
206
0
    return AOM_CODEC_MEM_ERROR;
207
6.80k
#endif
208
209
6.80k
  if (ybf) {
210
6.80k
    int y_stride = 0;
211
6.80k
    int uv_stride = 0;
212
6.80k
    uint64_t yplane_size = 0;
213
6.80k
    uint64_t uvplane_size = 0;
214
6.80k
    const int aligned_width = (width + 7) & ~7;
215
6.80k
    const int aligned_height = (height + 7) & ~7;
216
6.80k
    const int uv_width = aligned_width >> ss_x;
217
6.80k
    const int uv_height = aligned_height >> ss_y;
218
6.80k
    const int uv_border_w = border >> ss_x;
219
6.80k
    const int uv_border_h = border >> ss_y;
220
221
6.80k
    int error = calc_stride_and_planesize(
222
6.80k
        ss_x, ss_y, aligned_width, aligned_height, border, byte_alignment,
223
6.80k
        &y_stride, &uv_stride, &yplane_size, &uvplane_size, uv_height);
224
6.80k
    if (error) return error;
225
6.80k
    return realloc_frame_buffer_aligned(
226
6.80k
        ybf, width, height, ss_x, ss_y, use_highbitdepth, border,
227
6.80k
        byte_alignment, fb, cb, cb_priv, y_stride, yplane_size, uvplane_size,
228
6.80k
        aligned_width, aligned_height, uv_width, uv_height, uv_stride,
229
6.80k
        uv_border_w, uv_border_h, alloc_y_buffer_8bit);
230
6.80k
  }
231
0
  return AOM_CODEC_MEM_ERROR;
232
6.80k
}
233
234
int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
235
                           int ss_x, int ss_y, int use_highbitdepth, int border,
236
0
                           int byte_alignment) {
237
0
  if (ybf) {
238
0
    aom_free_frame_buffer(ybf);
239
0
    return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
240
0
                                    use_highbitdepth, border, byte_alignment,
241
0
                                    NULL, NULL, NULL, 0);
242
0
  }
243
0
  return AOM_CODEC_MEM_ERROR;
244
0
}
245
246
125k
void aom_remove_metadata_from_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
247
125k
  if (ybf && ybf->metadata) {
248
0
    aom_img_metadata_array_free(ybf->metadata);
249
0
    ybf->metadata = NULL;
250
0
  }
251
125k
}
252
253
int aom_copy_metadata_to_frame_buffer(YV12_BUFFER_CONFIG *ybf,
254
1.26k
                                      const aom_metadata_array_t *arr) {
255
1.26k
  if (!ybf || !arr || !arr->metadata_array) return -1;
256
0
  if (ybf->metadata == arr) return 0;
257
0
  aom_remove_metadata_from_frame_buffer(ybf);
258
0
  ybf->metadata = aom_img_metadata_array_alloc(arr->sz);
259
0
  if (!ybf->metadata) return -1;
260
0
  for (size_t i = 0; i < ybf->metadata->sz; i++) {
261
0
    ybf->metadata->metadata_array[i] = aom_img_metadata_alloc(
262
0
        arr->metadata_array[i]->type, arr->metadata_array[i]->payload,
263
0
        arr->metadata_array[i]->sz, arr->metadata_array[i]->insert_flag);
264
0
    if (ybf->metadata->metadata_array[i] == NULL) {
265
0
      aom_img_metadata_array_free(ybf->metadata);
266
0
      ybf->metadata = NULL;
267
0
      return -1;
268
0
    }
269
0
  }
270
0
  ybf->metadata->sz = arr->sz;
271
0
  return 0;
272
0
}