Home | History | Annotate | Download | only in generic
      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 <assert.h>
     12 #include <limits.h>
     13 
     14 #include "vpx_scale/yv12config.h"
     15 #include "vpx_mem/vpx_mem.h"
     16 #include "vpx_ports/mem.h"
     17 
     18 /****************************************************************************
     19 *  Exports
     20 ****************************************************************************/
     21 
     22 /****************************************************************************
     23  *
     24  ****************************************************************************/
     25 #define yv12_align_addr(addr, align) \
     26   (void *)(((size_t)(addr) + ((align)-1)) & (size_t) - (align))
     27 
     28 int vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
     29   if (ybf) {
     30     // If libvpx is using frame buffer callbacks then buffer_alloc_sz must
     31     // not be set.
     32     if (ybf->buffer_alloc_sz > 0) {
     33       vpx_free(ybf->buffer_alloc);
     34     }
     35 
     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     memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
     40   } else {
     41     return -1;
     42   }
     43 
     44   return 0;
     45 }
     46 
     47 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width,
     48                                   int height, int border) {
     49   if (ybf) {
     50     int aligned_width = (width + 15) & ~15;
     51     int aligned_height = (height + 15) & ~15;
     52     int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
     53     int yplane_size = (aligned_height + 2 * border) * y_stride;
     54     int uv_width = aligned_width >> 1;
     55     int uv_height = aligned_height >> 1;
     56     /** There is currently a bunch of code which assumes
     57       *  uv_stride == y_stride/2, so enforce this here. */
     58     int uv_stride = y_stride >> 1;
     59     int uvplane_size = (uv_height + border) * uv_stride;
     60     const int frame_size = yplane_size + 2 * uvplane_size;
     61 
     62     if (!ybf->buffer_alloc) {
     63       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size);
     64       ybf->buffer_alloc_sz = frame_size;
     65     }
     66 
     67     if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size) return -1;
     68 
     69     /* Only support allocating buffers that have a border that's a multiple
     70      * of 32. The border restriction is required to get 16-byte alignment of
     71      * the start of the chroma rows without introducing an arbitrary gap
     72      * between planes, which would break the semantics of things like
     73      * vpx_img_set_rect(). */
     74     if (border & 0x1f) return -3;
     75 
     76     ybf->y_crop_width = width;
     77     ybf->y_crop_height = height;
     78     ybf->y_width = aligned_width;
     79     ybf->y_height = aligned_height;
     80     ybf->y_stride = y_stride;
     81 
     82     ybf->uv_crop_width = (width + 1) / 2;
     83     ybf->uv_crop_height = (height + 1) / 2;
     84     ybf->uv_width = uv_width;
     85     ybf->uv_height = uv_height;
     86     ybf->uv_stride = uv_stride;
     87 
     88     ybf->alpha_width = 0;
     89     ybf->alpha_height = 0;
     90     ybf->alpha_stride = 0;
     91 
     92     ybf->border = border;
     93     ybf->frame_size = frame_size;
     94 
     95     ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
     96     ybf->u_buffer =
     97         ybf->buffer_alloc + yplane_size + (border / 2 * uv_stride) + border / 2;
     98     ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size +
     99                     (border / 2 * uv_stride) + border / 2;
    100     ybf->alpha_buffer = NULL;
    101 
    102     ybf->corrupted = 0; /* assume not currupted by errors */
    103     return 0;
    104   }
    105   return -2;
    106 }
    107 
    108 int vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
    109                                 int border) {
    110   if (ybf) {
    111     vp8_yv12_de_alloc_frame_buffer(ybf);
    112     return vp8_yv12_realloc_frame_buffer(ybf, width, height, border);
    113   }
    114   return -2;
    115 }
    116 
    117 #if CONFIG_VP9
    118 // TODO(jkoleszar): Maybe replace this with struct vpx_image
    119 
    120 int vpx_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
    121   if (ybf) {
    122     if (ybf->buffer_alloc_sz > 0) {
    123       vpx_free(ybf->buffer_alloc);
    124     }
    125 
    126     /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
    127       u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
    128       all of this so that a freed pointer isn't inadvertently used */
    129     memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
    130   } else {
    131     return -1;
    132   }
    133 
    134   return 0;
    135 }
    136 
    137 int vpx_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
    138                              int ss_x, int ss_y,
    139 #if CONFIG_VP9_HIGHBITDEPTH
    140                              int use_highbitdepth,
    141 #endif
    142                              int border, int byte_alignment,
    143                              vpx_codec_frame_buffer_t *fb,
    144                              vpx_get_frame_buffer_cb_fn_t cb, void *cb_priv) {
    145   if (ybf) {
    146     const int vp9_byte_align = (byte_alignment == 0) ? 1 : byte_alignment;
    147     const int aligned_width = (width + 7) & ~7;
    148     const int aligned_height = (height + 7) & ~7;
    149     const int y_stride = ((aligned_width + 2 * border) + 31) & ~31;
    150     const uint64_t yplane_size =
    151         (aligned_height + 2 * border) * (uint64_t)y_stride + byte_alignment;
    152     const int uv_width = aligned_width >> ss_x;
    153     const int uv_height = aligned_height >> ss_y;
    154     const int uv_stride = y_stride >> ss_x;
    155     const int uv_border_w = border >> ss_x;
    156     const int uv_border_h = border >> ss_y;
    157     const uint64_t uvplane_size =
    158         (uv_height + 2 * uv_border_h) * (uint64_t)uv_stride + byte_alignment;
    159 
    160 #if CONFIG_VP9_HIGHBITDEPTH
    161     const uint64_t frame_size =
    162         (1 + use_highbitdepth) * (yplane_size + 2 * uvplane_size);
    163 #else
    164     const uint64_t frame_size = yplane_size + 2 * uvplane_size;
    165 #endif  // CONFIG_VP9_HIGHBITDEPTH
    166 
    167     uint8_t *buf = NULL;
    168 
    169     // frame_size is stored in buffer_alloc_sz, which is an int. If it won't
    170     // fit, fail early.
    171     if (frame_size > INT_MAX) {
    172       return -1;
    173     }
    174 
    175     if (cb != NULL) {
    176       const int align_addr_extra_size = 31;
    177       const uint64_t external_frame_size = frame_size + align_addr_extra_size;
    178 
    179       assert(fb != NULL);
    180 
    181       if (external_frame_size != (size_t)external_frame_size) return -1;
    182 
    183       // Allocation to hold larger frame, or first allocation.
    184       if (cb(cb_priv, (size_t)external_frame_size, fb) < 0) return -1;
    185 
    186       if (fb->data == NULL || fb->size < external_frame_size) return -1;
    187 
    188       ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32);
    189 
    190 #if defined(__has_feature)
    191 #if __has_feature(memory_sanitizer)
    192       // This memset is needed for fixing the issue of using uninitialized
    193       // value in msan test. It will cause a perf loss, so only do this for
    194       // msan test.
    195       memset(ybf->buffer_alloc, 0, (int)frame_size);
    196 #endif
    197 #endif
    198     } else if (frame_size > (size_t)ybf->buffer_alloc_sz) {
    199       // Allocation to hold larger frame, or first allocation.
    200       vpx_free(ybf->buffer_alloc);
    201       ybf->buffer_alloc = NULL;
    202 
    203       ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, (size_t)frame_size);
    204       if (!ybf->buffer_alloc) return -1;
    205 
    206       ybf->buffer_alloc_sz = (int)frame_size;
    207 
    208       // This memset is needed for fixing valgrind error from C loop filter
    209       // due to access uninitialized memory in frame border. It could be
    210       // removed if border is totally removed.
    211       memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz);
    212     }
    213 
    214     /* Only support allocating buffers that have a border that's a multiple
    215      * of 32. The border restriction is required to get 16-byte alignment of
    216      * the start of the chroma rows without introducing an arbitrary gap
    217      * between planes, which would break the semantics of things like
    218      * vpx_img_set_rect(). */
    219     if (border & 0x1f) return -3;
    220 
    221     ybf->y_crop_width = width;
    222     ybf->y_crop_height = height;
    223     ybf->y_width = aligned_width;
    224     ybf->y_height = aligned_height;
    225     ybf->y_stride = y_stride;
    226 
    227     ybf->uv_crop_width = (width + ss_x) >> ss_x;
    228     ybf->uv_crop_height = (height + ss_y) >> ss_y;
    229     ybf->uv_width = uv_width;
    230     ybf->uv_height = uv_height;
    231     ybf->uv_stride = uv_stride;
    232 
    233     ybf->border = border;
    234     ybf->frame_size = (int)frame_size;
    235     ybf->subsampling_x = ss_x;
    236     ybf->subsampling_y = ss_y;
    237 
    238     buf = ybf->buffer_alloc;
    239 #if CONFIG_VP9_HIGHBITDEPTH
    240     if (use_highbitdepth) {
    241       // Store uint16 addresses when using 16bit framebuffers
    242       buf = CONVERT_TO_BYTEPTR(ybf->buffer_alloc);
    243       ybf->flags = YV12_FLAG_HIGHBITDEPTH;
    244     } else {
    245       ybf->flags = 0;
    246     }
    247 #endif  // CONFIG_VP9_HIGHBITDEPTH
    248 
    249     ybf->y_buffer = (uint8_t *)yv12_align_addr(
    250         buf + (border * y_stride) + border, vp9_byte_align);
    251     ybf->u_buffer = (uint8_t *)yv12_align_addr(
    252         buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
    253         vp9_byte_align);
    254     ybf->v_buffer =
    255         (uint8_t *)yv12_align_addr(buf + yplane_size + uvplane_size +
    256                                        (uv_border_h * uv_stride) + uv_border_w,
    257                                    vp9_byte_align);
    258 
    259     ybf->corrupted = 0; /* assume not corrupted by errors */
    260     return 0;
    261   }
    262   return -2;
    263 }
    264 
    265 int vpx_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
    266                            int ss_x, int ss_y,
    267 #if CONFIG_VP9_HIGHBITDEPTH
    268                            int use_highbitdepth,
    269 #endif
    270                            int border, int byte_alignment) {
    271   if (ybf) {
    272     vpx_free_frame_buffer(ybf);
    273     return vpx_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
    274 #if CONFIG_VP9_HIGHBITDEPTH
    275                                     use_highbitdepth,
    276 #endif
    277                                     border, byte_alignment, NULL, NULL, NULL);
    278   }
    279   return -2;
    280 }
    281 #endif
    282