Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 10 | */ |
| 11 | #ifndef AV1_AV1_IFACE_COMMON_H_ |
| 12 | #define AV1_AV1_IFACE_COMMON_H_ |
| 13 | |
| 14 | #include "aom_ports/mem.h" |
| 15 | |
| 16 | static void yuvconfig2image(aom_image_t *img, const YV12_BUFFER_CONFIG *yv12, |
| 17 | void *user_priv) { |
| 18 | /** aom_img_wrap() doesn't allow specifying independent strides for |
| 19 | * the Y, U, and V planes, nor other alignment adjustments that |
| 20 | * might be representable by a YV12_BUFFER_CONFIG, so we just |
| 21 | * initialize all the fields.*/ |
| 22 | int bps; |
| 23 | if (!yv12->subsampling_y) { |
| 24 | if (!yv12->subsampling_x) { |
| 25 | img->fmt = AOM_IMG_FMT_I444; |
| 26 | bps = 24; |
| 27 | } else { |
| 28 | img->fmt = AOM_IMG_FMT_I422; |
| 29 | bps = 16; |
| 30 | } |
| 31 | } else { |
| 32 | if (!yv12->subsampling_x) { |
| 33 | img->fmt = AOM_IMG_FMT_I440; |
| 34 | bps = 16; |
| 35 | } else { |
| 36 | img->fmt = AOM_IMG_FMT_I420; |
| 37 | bps = 12; |
| 38 | } |
| 39 | } |
| 40 | img->cs = yv12->color_space; |
anorkin | 76fb126 | 2017-03-22 15:12:12 -0700 | [diff] [blame] | 41 | #if CONFIG_COLORSPACE_HEADERS |
| 42 | img->tf = yv12->transfer_function; |
| 43 | img->csp = yv12->chroma_sample_position; |
| 44 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 45 | img->range = yv12->color_range; |
| 46 | img->bit_depth = 8; |
| 47 | img->w = yv12->y_stride; |
Yaowu Xu | 671f2bd | 2016-09-30 15:07:57 -0700 | [diff] [blame] | 48 | img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * AOM_BORDER_IN_PIXELS, 3); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 49 | img->d_w = yv12->y_crop_width; |
| 50 | img->d_h = yv12->y_crop_height; |
| 51 | img->r_w = yv12->render_width; |
| 52 | img->r_h = yv12->render_height; |
| 53 | img->x_chroma_shift = yv12->subsampling_x; |
| 54 | img->y_chroma_shift = yv12->subsampling_y; |
| 55 | img->planes[AOM_PLANE_Y] = yv12->y_buffer; |
| 56 | img->planes[AOM_PLANE_U] = yv12->u_buffer; |
| 57 | img->planes[AOM_PLANE_V] = yv12->v_buffer; |
| 58 | img->planes[AOM_PLANE_ALPHA] = NULL; |
| 59 | img->stride[AOM_PLANE_Y] = yv12->y_stride; |
| 60 | img->stride[AOM_PLANE_U] = yv12->uv_stride; |
| 61 | img->stride[AOM_PLANE_V] = yv12->uv_stride; |
| 62 | img->stride[AOM_PLANE_ALPHA] = yv12->y_stride; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 63 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 64 | if (yv12->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 65 | // aom_image_t uses byte strides and a pointer to the first byte |
| 66 | // of the image. |
| 67 | img->fmt = (aom_img_fmt_t)(img->fmt | AOM_IMG_FMT_HIGHBITDEPTH); |
| 68 | img->bit_depth = yv12->bit_depth; |
| 69 | img->planes[AOM_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer); |
| 70 | img->planes[AOM_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer); |
| 71 | img->planes[AOM_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer); |
| 72 | img->planes[AOM_PLANE_ALPHA] = NULL; |
| 73 | img->stride[AOM_PLANE_Y] = 2 * yv12->y_stride; |
| 74 | img->stride[AOM_PLANE_U] = 2 * yv12->uv_stride; |
| 75 | img->stride[AOM_PLANE_V] = 2 * yv12->uv_stride; |
| 76 | img->stride[AOM_PLANE_ALPHA] = 2 * yv12->y_stride; |
| 77 | } |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 78 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 79 | img->bps = bps; |
| 80 | img->user_priv = user_priv; |
| 81 | img->img_data = yv12->buffer_alloc; |
| 82 | img->img_data_owner = 0; |
| 83 | img->self_allocd = 0; |
| 84 | } |
| 85 | |
| 86 | static aom_codec_err_t image2yuvconfig(const aom_image_t *img, |
| 87 | YV12_BUFFER_CONFIG *yv12) { |
| 88 | yv12->y_buffer = img->planes[AOM_PLANE_Y]; |
| 89 | yv12->u_buffer = img->planes[AOM_PLANE_U]; |
| 90 | yv12->v_buffer = img->planes[AOM_PLANE_V]; |
| 91 | |
| 92 | yv12->y_crop_width = img->d_w; |
| 93 | yv12->y_crop_height = img->d_h; |
| 94 | yv12->render_width = img->r_w; |
| 95 | yv12->render_height = img->r_h; |
| 96 | yv12->y_width = img->d_w; |
| 97 | yv12->y_height = img->d_h; |
| 98 | |
| 99 | yv12->uv_width = |
| 100 | img->x_chroma_shift == 1 ? (1 + yv12->y_width) / 2 : yv12->y_width; |
| 101 | yv12->uv_height = |
| 102 | img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height; |
| 103 | yv12->uv_crop_width = yv12->uv_width; |
| 104 | yv12->uv_crop_height = yv12->uv_height; |
| 105 | |
| 106 | yv12->y_stride = img->stride[AOM_PLANE_Y]; |
| 107 | yv12->uv_stride = img->stride[AOM_PLANE_U]; |
| 108 | yv12->color_space = img->cs; |
anorkin | 76fb126 | 2017-03-22 15:12:12 -0700 | [diff] [blame] | 109 | #if CONFIG_COLORSPACE_HEADERS |
| 110 | yv12->transfer_function = img->tf; |
| 111 | yv12->chroma_sample_position = img->csp; |
| 112 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 113 | yv12->color_range = img->range; |
| 114 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 115 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 116 | if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) { |
| 117 | // In aom_image_t |
| 118 | // planes point to uint8 address of start of data |
| 119 | // stride counts uint8s to reach next row |
| 120 | // In YV12_BUFFER_CONFIG |
| 121 | // y_buffer, u_buffer, v_buffer point to uint16 address of data |
| 122 | // stride and border counts in uint16s |
| 123 | // This means that all the address calculations in the main body of code |
| 124 | // should work correctly. |
| 125 | // However, before we do any pixel operations we need to cast the address |
| 126 | // to a uint16 ponter and double its value. |
| 127 | yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer); |
| 128 | yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer); |
| 129 | yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer); |
| 130 | yv12->y_stride >>= 1; |
| 131 | yv12->uv_stride >>= 1; |
| 132 | yv12->flags = YV12_FLAG_HIGHBITDEPTH; |
| 133 | } else { |
| 134 | yv12->flags = 0; |
| 135 | } |
| 136 | yv12->border = (yv12->y_stride - img->w) / 2; |
| 137 | #else |
| 138 | yv12->border = (img->stride[AOM_PLANE_Y] - img->w) / 2; |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 139 | #endif // CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | yv12->subsampling_x = img->x_chroma_shift; |
| 141 | yv12->subsampling_y = img->y_chroma_shift; |
| 142 | return AOM_CODEC_OK; |
| 143 | } |
| 144 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 145 | #endif // AV1_AV1_IFACE_COMMON_H_ |