yv12config.c 6.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11
 */

12 13
#include <assert.h>

Yaowu Xu's avatar
Yaowu Xu committed
14
#include "aom_mem/aom_mem.h"
15
#include "aom_ports/mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
16
#include "aom_scale/yv12config.h"
John Koleszar's avatar
John Koleszar committed
17 18

/****************************************************************************
Johann's avatar
Johann committed
19 20
 *  Exports
 ****************************************************************************/
John Koleszar's avatar
John Koleszar committed
21 22 23 24

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

Yaowu Xu's avatar
Yaowu Xu committed
28 29
#if CONFIG_AV1
// TODO(jkoleszar): Maybe replace this with struct aom_image
30

Yaowu Xu's avatar
Yaowu Xu committed
31
int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
32
  if (ybf) {
33
    if (ybf->buffer_alloc_sz > 0) {
Yaowu Xu's avatar
Yaowu Xu committed
34
      aom_free(ybf->buffer_alloc);
35
    }
Yaowu Xu's avatar
Yaowu Xu committed
36
    if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
37

38 39 40
    /* 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
41
    memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
42 43 44 45 46 47 48
  } else {
    return -1;
  }

  return 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
49
int aom_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
50
                             int ss_x, int ss_y, int use_highbitdepth,
clang-format's avatar
clang-format committed
51
                             int border, int byte_alignment,
Yaowu Xu's avatar
Yaowu Xu committed
52 53
                             aom_codec_frame_buffer_t *fb,
                             aom_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
54
  if (ybf) {
Yaowu Xu's avatar
Yaowu Xu committed
55
    const int aom_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
56 57
    const int aligned_width = (width + 7) & ~7;
    const int aligned_height = (height + 7) & ~7;
58
    const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
clang-format's avatar
clang-format committed
59 60
    const uint64_t yplane_size =
        (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
61 62 63 64 65
    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
66 67
    const uint64_t uvplane_size =
        (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
68

69 70
    const uint64_t frame_size =
        (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
71 72 73

    uint8_t *buf = NULL;

74 75
    if (cb != NULL) {
      const int align_addr_extra_size = 31;
76
      const uint64_t external_frame_size = frame_size + align_addr_extra_size;
77 78 79

      assert(fb != NULL);

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

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

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

87
      ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
88 89 90 91 92 93 94 95 96

#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
Yaowu Xu's avatar
Yaowu Xu committed
97
    } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
98
      // Allocation to hold larger frame, or first allocation.
Yaowu Xu's avatar
Yaowu Xu committed
99
      aom_free(ybf->buffer_alloc);
100 101
      ybf->buffer_alloc = NULL;

clang-format's avatar
clang-format committed
102
      if (frame_size != (size_t)frame_size) return -1;
103

Yaowu Xu's avatar
Yaowu Xu committed
104
      ybf->buffer_alloc = (uint8_t *)aom_memalign(32, (size_t)frame_size);
clang-format's avatar
clang-format committed
105
      if (!ybf->buffer_alloc) return -1;
106

107
      ybf->buffer_alloc_sz = (size_t)frame_size;
108

109
      // This memset is needed for fixing valgrind error from C loop filter
110
      // due to access uninitialized memory in frame border. It could be
111
      // removed if border is totally removed.
James Zern's avatar
James Zern committed
112
      memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
113 114 115 116
    }

    /* 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
117
     * the start of the chroma rows without introducing an arbitrary gap
118
     * between planes, which would break the semantics of things like
Yaowu Xu's avatar
Yaowu Xu committed
119
     * aom_img_set_rect(). */
clang-format's avatar
clang-format committed
120
    if (border & 0x1f) return -3;
121 122 123

    ybf->y_crop_width = width;
    ybf->y_crop_height = height;
clang-format's avatar
clang-format committed
124
    ybf->y_width = aligned_width;
125 126 127
    ybf->y_height = aligned_height;
    ybf->y_stride = y_stride;

128 129
    ybf->uv_crop_width = (width + ss_x) >> ss_x;
    ybf->uv_crop_height = (height + ss_y) >> ss_y;
130 131 132 133 134
    ybf->uv_width = uv_width;
    ybf->uv_height = uv_height;
    ybf->uv_stride = uv_stride;

    ybf->border = border;
135
    ybf->frame_size = (size_t)frame_size;
136 137
    ybf->subsampling_x = ss_x;
    ybf->subsampling_y = ss_y;
138

139
    buf = ybf->buffer_alloc;
140 141
    if (use_highbitdepth) {
      // Store uint16 addresses when using 16bit framebuffers
142
      buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
143 144 145 146
      ybf->flags = YV12_FLAG_HIGHBITDEPTH;
    } else {
      ybf->flags = 0;
    }
147

148
    ybf->y_buffer = (uint8_t *)yv12_align_addr(
Yaowu Xu's avatar
Yaowu Xu committed
149
        buf + (border * y_stride) + border, aom_byte_align);
150 151
    ybf->u_buffer = (uint8_t *)yv12_align_addr(
        buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
Yaowu Xu's avatar
Yaowu Xu committed
152
        aom_byte_align);
clang-format's avatar
clang-format committed
153 154 155
    ybf->v_buffer =
        (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
                                       (uv_border_h * uv_stride) + uv_border_w,
Yaowu Xu's avatar
Yaowu Xu committed
156
                                   aom_byte_align);
157

Yaowu Xu's avatar
Yaowu Xu committed
158 159 160 161 162 163
    if (use_highbitdepth) {
      if (ybf->y_buffer_8bit) aom_free(ybf->y_buffer_8bit);
      ybf->y_buffer_8bit = (uint8_t *)aom_memalign(32, (size_t)yplane_size);
      if (!ybf->y_buffer_8bit) return -1;
    } else {
      assert(!ybf->y_buffer_8bit);
164 165
    }

Adrian Grange's avatar
Adrian Grange committed
166
    ybf->corrupted = 0; /* assume not corrupted by errors */
167 168 169 170 171
    return 0;
  }
  return -2;
}

Yaowu Xu's avatar
Yaowu Xu committed
172
int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
173 174
                           int ss_x, int ss_y, int use_highbitdepth, int border,
                           int byte_alignment) {
175
  if (ybf) {
Yaowu Xu's avatar
Yaowu Xu committed
176 177
    aom_free_frame_buffer(ybf);
    return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
178 179
                                    use_highbitdepth, border, byte_alignment,
                                    NULL, NULL, NULL);
180 181 182 183
  }
  return -2;
}
#endif