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 
     12 #include "vpx_scale/yv12config.h"
     13 #include "vpx_mem/vpx_mem.h"
     14 
     15 /****************************************************************************
     16 *  Exports
     17 ****************************************************************************/
     18 
     19 /****************************************************************************
     20  *
     21  ****************************************************************************/
     22 int
     23 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) {
     24   if (ybf) {
     25     vpx_free(ybf->buffer_alloc);
     26 
     27     /* buffer_alloc isn't accessed by most functions.  Rather y_buffer,
     28       u_buffer and v_buffer point to buffer_alloc and are used.  Clear out
     29       all of this so that a freed pointer isn't inadvertently used */
     30     vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG));
     31   } else {
     32     return -1;
     33   }
     34 
     35   return 0;
     36 }
     37 
     38 /****************************************************************************
     39  *
     40  ****************************************************************************/
     41 int
     42 vp8_yv12_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height, int border) {
     43   /*NOTE:*/
     44 
     45   if (ybf) {
     46     int y_stride = ((width + 2 * border) + 31) & ~31;
     47     int yplane_size = (height + 2 * border) * y_stride;
     48     int uv_width = width >> 1;
     49     int uv_height = height >> 1;
     50     /** There is currently a bunch of code which assumes
     51       *  uv_stride == y_stride/2, so enforce this here. */
     52     int uv_stride = y_stride >> 1;
     53     int uvplane_size = (uv_height + border) * uv_stride;
     54 
     55     vp8_yv12_de_alloc_frame_buffer(ybf);
     56 
     57     /** Only support allocating buffers that have a height and width that
     58       *  are multiples of 16, and a border that's a multiple of 32.
     59       * The border restriction is required to get 16-byte alignment of the
     60       *  start of the chroma rows without intoducing an arbitrary gap
     61       *  between planes, which would break the semantics of things like
     62       *  vpx_img_set_rect(). */
     63     if ((width & 0xf) | (height & 0xf) | (border & 0x1f))
     64       return -3;
     65 
     66     ybf->y_width  = width;
     67     ybf->y_height = height;
     68     ybf->y_stride = y_stride;
     69 
     70     ybf->uv_width = uv_width;
     71     ybf->uv_height = uv_height;
     72     ybf->uv_stride = uv_stride;
     73 
     74     ybf->border = border;
     75     ybf->frame_size = yplane_size + 2 * uvplane_size;
     76 
     77     ybf->buffer_alloc = (unsigned char *) vpx_memalign(32, ybf->frame_size);
     78 
     79     if (ybf->buffer_alloc == NULL)
     80       return -1;
     81 
     82     ybf->y_buffer = ybf->buffer_alloc + (border * y_stride) + border;
     83     ybf->u_buffer = ybf->buffer_alloc + yplane_size + (border / 2  * uv_stride) + border / 2;
     84     ybf->v_buffer = ybf->buffer_alloc + yplane_size + uvplane_size + (border / 2  * uv_stride) + border / 2;
     85 
     86     ybf->corrupted = 0; /* assume not currupted by errors */
     87   } else {
     88     return -2;
     89   }
     90 
     91   return 0;
     92 }
     93