yv12config.c 10.4 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12
#include <assert.h>

Yaowu Xu's avatar
Yaowu Xu committed
13 14 15
#include "aom_scale/yv12config.h"
#include "aom_mem/vpx_mem.h"
#include "aom_ports/mem.h"
John Koleszar's avatar
John Koleszar committed
16 17 18 19 20 21 22 23

/****************************************************************************
*  Exports
****************************************************************************/

/****************************************************************************
 *
 ****************************************************************************/
24
#define yv12_align_addr(addr, align) \
clang-format's avatar
clang-format committed
25
  (void *)(((size_t)(addr) + ((align)-1)) & (size_t) - (align))
26

clang-format's avatar
clang-format committed
27
int vpx_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
John Koleszar's avatar
John Koleszar committed
28
  if (ybf) {
Yaowu Xu's avatar
Yaowu Xu committed
29
    // If libaom is using frame buffer callbacks then buffer_alloc_sz must
30 31 32 33
    // not be set.
    if (ybf->buffer_alloc_sz > 0) {
      vpx_free(ybf->buffer_alloc);
    }
John Koleszar's avatar
John Koleszar committed
34 35 36 37

    /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
      u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
      all of this so that a freed pointer isn't inadvertently used */
James Zern's avatar
James Zern committed
38
    memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
John Koleszar's avatar
John Koleszar committed
39 40 41 42 43
  } else {
    return -1;
  }

  return 0;
John Koleszar's avatar
John Koleszar committed
44 45
}

clang-format's avatar
clang-format committed
46 47
int vpx_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width,
                                  int height, int border) {
John Koleszar's avatar
John Koleszar committed
48
  if (ybf) {
John Koleszar's avatar
John Koleszar committed
49 50 51 52 53 54
    int aligned_width = (width + 15) & ~15;
    int aligned_height = (height + 15) & ~15;
    int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
    int yplane_size = (aligned_height + 2 * border) * y_stride;
    int uv_width = aligned_width >> 1;
    int uv_height = aligned_height >> 1;
John Koleszar's avatar
John Koleszar committed
55 56 57 58
    /** There is currently a bunch of code which assumes
      *  uv_stride == y_stride/2, so enforce this here. */
    int uv_stride = y_stride >> 1;
    int uvplane_size = (uv_height + border) * uv_stride;
59
    const int frame_size = yplane_size + 2 * uvplane_size;
John Koleszar's avatar
John Koleszar committed
60

61
    if (!ybf->buffer_alloc) {
62
      ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
63 64 65
      ybf->buffer_alloc_sz = frame_size;
    }

clang-format's avatar
clang-format committed
66
    if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size) return -1;
John Koleszar's avatar
John Koleszar committed
67

John Koleszar's avatar
John Koleszar committed
68 69
    /* Only support allocating buffers that have a border that's a multiple
     * of 32. The border restriction is required to get 16-byte alignment of
70
     * the start of the chroma rows without introducing an arbitrary gap
John Koleszar's avatar
John Koleszar committed
71 72
     * between planes, which would break the semantics of things like
     * vpx_img_set_rect(). */
clang-format's avatar
clang-format committed
73
    if (border & 0x1f) return -3;
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75 76
    ybf->y_crop_width = width;
    ybf->y_crop_height = height;
clang-format's avatar
clang-format committed
77
    ybf->y_width = aligned_width;
John Koleszar's avatar
John Koleszar committed
78
    ybf->y_height = aligned_height;
John Koleszar's avatar
John Koleszar committed
79 80
    ybf->y_stride = y_stride;

Johann's avatar
Johann committed
81 82
    ybf->uv_crop_width = (width + 1) / 2;
    ybf->uv_crop_height = (height + 1) / 2;
John Koleszar's avatar
John Koleszar committed
83 84 85 86
    ybf->uv_width = uv_width;
    ybf->uv_height = uv_height;
    ybf->uv_stride = uv_stride;

87 88 89 90
    ybf->alpha_width = 0;
    ybf->alpha_height = 0;
    ybf->alpha_stride = 0;

John Koleszar's avatar
John Koleszar committed
91
    ybf->border = border;
92
    ybf->frame_size = frame_size;
John Koleszar's avatar
John Koleszar committed
93 94

    ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
clang-format's avatar
clang-format committed
95 96 97 98
    ybf->u_buffer =
        ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
    ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
                    (border / 2 * uv_stride) + border / 2;
99
    ybf->alpha_buffer = NULL;
John Koleszar's avatar
John Koleszar committed
100 101

    ybf->corrupted = 0; /* assume not currupted by errors */
102
    return 0;
John Koleszar's avatar
John Koleszar committed
103
  }
104 105
  return -2;
}
John Koleszar's avatar
John Koleszar committed
106

clang-format's avatar
clang-format committed
107 108
int vpx_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
                                int border) {
109
  if (ybf) {
Yaowu Xu's avatar
Yaowu Xu committed
110 111
    vpx_yv12_de_alloc_frame_buffer(ybf);
    return vpx_yv12_realloc_frame_buffer(ybf, width, height, border);
112 113
  }
  return -2;
John Koleszar's avatar
John Koleszar committed
114
}
115

116
#if CONFIG_VP10
117 118
// TODO(jkoleszar): Maybe replace this with struct vpx_image

119
int vpx_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
120
  if (ybf) {
121 122 123
    if (ybf->buffer_alloc_sz > 0) {
      vpx_free(ybf->buffer_alloc);
    }
124 125 126 127

    /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
      u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
      all of this so that a freed pointer isn't inadvertently used */
James Zern's avatar
James Zern committed
128
    memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
129 130 131 132 133 134 135
  } else {
    return -1;
  }

  return 0;
}

clang-format's avatar
clang-format committed
136
int vpx_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
137
                             int ss_x, int ss_y,
138
#if CONFIG_VPX_HIGHBITDEPTH
139 140
                             int use_highbitdepth,
#endif
clang-format's avatar
clang-format committed
141
                             int border, int byte_alignment,
142
                             vpx_codec_frame_buffer_t *fb,
clang-format's avatar
clang-format committed
143
                             vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
144
  if (ybf) {
145
    const int vpx_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
146 147
    const int aligned_width = (width + 7) & ~7;
    const int aligned_height = (height + 7) & ~7;
148
    const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
clang-format's avatar
clang-format committed
149 150
    const uint64_t yplane_size =
        (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
151 152 153 154 155
    const int uv_width = aligned_width >> ss_x;
    const int uv_height = aligned_height >> ss_y;
    const int uv_stride = y_stride >> ss_x;
    const int uv_border_w = border >> ss_x;
    const int uv_border_h = border >> ss_y;
clang-format's avatar
clang-format committed
156 157
    const uint64_t uvplane_size =
        (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
158

159 160 161 162 163 164
#if CONFIG_ALPHA
    const int alpha_width = aligned_width;
    const int alpha_height = aligned_height;
    const int alpha_stride = y_stride;
    const int alpha_border_w = border;
    const int alpha_border_h = border;
clang-format's avatar
clang-format committed
165 166 167
    const uint64_t alpha_plane_size =
        (alpha_height + 2 * alpha_border_h) * (uint64_t)alpha_stride +
        byte_alignment;
168
#if CONFIG_VPX_HIGHBITDEPTH
clang-format's avatar
clang-format committed
169 170
    const uint64_t frame_size =
        (1 + use_highbitdepth) *
171 172
        (yplane_size + 2 * uvplane_size + alpha_plane_size);
#else
clang-format's avatar
clang-format committed
173 174
    const uint64_t frame_size =
        yplane_size + 2 * uvplane_size + alpha_plane_size;
175
#endif  // CONFIG_VPX_HIGHBITDEPTH
176
#else
177
#if CONFIG_VPX_HIGHBITDEPTH
178 179
    const uint64_t frame_size =
        (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
180
#else
181
    const uint64_t frame_size = yplane_size + 2 * uvplane_size;
182
#endif  // CONFIG_VPX_HIGHBITDEPTH
183
#endif  // CONFIG_ALPHA
184 185 186

    uint8_t *buf = NULL;

187 188
    if (cb != NULL) {
      const int align_addr_extra_size = 31;
189
      const uint64_t external_frame_size = frame_size + align_addr_extra_size;
190 191 192

      assert(fb != NULL);

clang-format's avatar
clang-format committed
193
      if (external_frame_size != (size_t)external_frame_size) return -1;
194

195
      // Allocation to hold larger frame, or first allocation.
clang-format's avatar
clang-format committed
196
      if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1;
197

clang-format's avatar
clang-format committed
198
      if (fb->data == NULL || fb->size < external_frame_size) return -1;
199

200
      ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
201 202 203 204 205 206 207 208 209

#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
      // This memset is needed for fixing the issue of using uninitialized
      // value in msan test. It will cause a perf loss, so only do this for
      // msan test.
      memset(ybf->buffer_alloc, 0, (int)frame_size);
#endif
#endif
210
    } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
211
      // Allocation to hold larger frame, or first allocation.
212
      vpx_free(ybf->buffer_alloc);
213 214
      ybf->buffer_alloc = NULL;

clang-format's avatar
clang-format committed
215
      if (frame_size != (size_t)frame_size) return -1;
216 217

      ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
clang-format's avatar
clang-format committed
218
      if (!ybf->buffer_alloc) return -1;
219

Yunqing Wang's avatar
Yunqing Wang committed
220
      ybf->buffer_alloc_sz = (int)frame_size;
221

222
      // This memset is needed for fixing valgrind error from C loop filter
223
      // due to access uninitialized memory in frame border. It could be
224
      // removed if border is totally removed.
James Zern's avatar
James Zern committed
225
      memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
226 227 228 229
    }

    /* Only support allocating buffers that have a border that's a multiple
     * of 32. The border restriction is required to get 16-byte alignment of
230
     * the start of the chroma rows without introducing an arbitrary gap
231 232
     * between planes, which would break the semantics of things like
     * vpx_img_set_rect(). */
clang-format's avatar
clang-format committed
233
    if (border & 0x1f) return -3;
234 235 236

    ybf->y_crop_width = width;
    ybf->y_crop_height = height;
clang-format's avatar
clang-format committed
237
    ybf->y_width = aligned_width;
238 239 240
    ybf->y_height = aligned_height;
    ybf->y_stride = y_stride;

241 242
    ybf->uv_crop_width = (width + ss_x) >> ss_x;
    ybf->uv_crop_height = (height + ss_y) >> ss_y;
243 244 245 246 247
    ybf->uv_width = uv_width;
    ybf->uv_height = uv_height;
    ybf->uv_stride = uv_stride;

    ybf->border = border;
Yunqing Wang's avatar
Yunqing Wang committed
248
    ybf->frame_size = (int)frame_size;
249 250
    ybf->subsampling_x = ss_x;
    ybf->subsampling_y = ss_y;
251

252
    buf = ybf->buffer_alloc;
253
#if CONFIG_VPX_HIGHBITDEPTH
254 255
    if (use_highbitdepth) {
      // Store uint16 addresses when using 16bit framebuffers
256
      buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
257 258 259 260
      ybf->flags = YV12_FLAG_HIGHBITDEPTH;
    } else {
      ybf->flags = 0;
    }
261
#endif  // CONFIG_VPX_HIGHBITDEPTH
262

263
    ybf->y_buffer = (uint8_t *)yv12_align_addr(
264
        buf + (border * y_stride) + border, vpx_byte_align);
265 266
    ybf->u_buffer = (uint8_t *)yv12_align_addr(
        buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
267
        vpx_byte_align);
clang-format's avatar
clang-format committed
268 269 270 271
    ybf->v_buffer =
        (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
                                       (uv_border_h * uv_stride) + uv_border_w,
                                   vpx_byte_align);
272

273 274 275 276
#if CONFIG_ALPHA
    ybf->alpha_width = alpha_width;
    ybf->alpha_height = alpha_height;
    ybf->alpha_stride = alpha_stride;
277
    ybf->alpha_buffer = (uint8_t *)yv12_align_addr(
clang-format's avatar
clang-format committed
278 279 280
        buf + yplane_size + 2 * uvplane_size + (alpha_border_h * alpha_stride) +
            alpha_border_w,
        vpx_byte_align);
281
#endif
Adrian Grange's avatar
Adrian Grange committed
282
    ybf->corrupted = 0; /* assume not corrupted by errors */
283 284 285 286 287
    return 0;
  }
  return -2;
}

clang-format's avatar
clang-format committed
288
int vpx_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
289
                           int ss_x, int ss_y,
290
#if CONFIG_VPX_HIGHBITDEPTH
291 292
                           int use_highbitdepth,
#endif
clang-format's avatar
clang-format committed
293
                           int border, int byte_alignment) {
294
  if (ybf) {
295 296
    vpx_free_frame_buffer(ybf);
    return vpx_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
297
#if CONFIG_VPX_HIGHBITDEPTH
298 299
                                    use_highbitdepth,
#endif
300
                                    border, byte_alignment, NULL, NULL, NULL);
301 302 303 304
  }
  return -2;
}
#endif