John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | #ifndef VP9_VP9_IFACE_COMMON_H_ |
| 11 | #define VP9_VP9_IFACE_COMMON_H_ |
| 12 | |
Johann | 1d7ccd5 | 2015-05-11 19:09:22 -0700 | [diff] [blame] | 13 | #include "vpx_ports/mem.h" |
| 14 | |
Dmitry Kovalev | 50e54c1 | 2013-04-01 18:23:04 -0700 | [diff] [blame] | 15 | static void yuvconfig2image(vpx_image_t *img, const YV12_BUFFER_CONFIG *yv12, |
| 16 | void *user_priv) { |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 17 | /** vpx_img_wrap() doesn't allow specifying independent strides for |
| 18 | * the Y, U, and V planes, nor other alignment adjustments that |
| 19 | * might be representable by a YV12_BUFFER_CONFIG, so we just |
| 20 | * initialize all the fields.*/ |
Dmitry Kovalev | 49d8bdc | 2014-04-29 16:12:44 -0700 | [diff] [blame] | 21 | int bps; |
Alex Converse | a0befb9 | 2014-10-01 11:23:57 -0700 | [diff] [blame] | 22 | if (!yv12->subsampling_y) { |
| 23 | if (!yv12->subsampling_x) { |
John Koleszar | da58436 | 2013-05-06 15:52:06 -0700 | [diff] [blame] | 24 | img->fmt = VPX_IMG_FMT_I444; |
| 25 | bps = 24; |
| 26 | } else { |
| 27 | img->fmt = VPX_IMG_FMT_I422; |
| 28 | bps = 16; |
| 29 | } |
| 30 | } else { |
Alex Converse | a0befb9 | 2014-10-01 11:23:57 -0700 | [diff] [blame] | 31 | if (!yv12->subsampling_x) { |
Deb Mukherjee | a30774c | 2014-10-01 12:17:37 -0700 | [diff] [blame] | 32 | img->fmt = VPX_IMG_FMT_I440; |
| 33 | bps = 16; |
| 34 | } else { |
| 35 | img->fmt = VPX_IMG_FMT_I420; |
| 36 | bps = 12; |
| 37 | } |
John Koleszar | da58436 | 2013-05-06 15:52:06 -0700 | [diff] [blame] | 38 | } |
Yaowu Xu | 6b223fc | 2015-01-09 13:04:48 -0800 | [diff] [blame] | 39 | img->cs = yv12->color_space; |
Ronald S. Bultje | eeb5ef0 | 2015-09-15 21:56:51 -0400 | [diff] [blame] | 40 | img->range = yv12->color_range; |
Deb Mukherjee | 5820c5d | 2014-06-12 16:53:13 -0700 | [diff] [blame] | 41 | img->bit_depth = 8; |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 42 | img->w = yv12->y_stride; |
hkuang | 437004c | 2014-01-10 13:10:39 -0800 | [diff] [blame] | 43 | img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * VP9_ENC_BORDER_IN_PIXELS, 3); |
John Koleszar | da58436 | 2013-05-06 15:52:06 -0700 | [diff] [blame] | 44 | img->d_w = yv12->y_crop_width; |
| 45 | img->d_h = yv12->y_crop_height; |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 46 | img->r_w = yv12->render_width; |
| 47 | img->r_h = yv12->render_height; |
Alex Converse | a0befb9 | 2014-10-01 11:23:57 -0700 | [diff] [blame] | 48 | img->x_chroma_shift = yv12->subsampling_x; |
| 49 | img->y_chroma_shift = yv12->subsampling_y; |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 50 | img->planes[VPX_PLANE_Y] = yv12->y_buffer; |
| 51 | img->planes[VPX_PLANE_U] = yv12->u_buffer; |
| 52 | img->planes[VPX_PLANE_V] = yv12->v_buffer; |
Alex Converse | 5926e7c | 2014-07-17 11:29:59 -0700 | [diff] [blame] | 53 | img->planes[VPX_PLANE_ALPHA] = NULL; |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 54 | img->stride[VPX_PLANE_Y] = yv12->y_stride; |
| 55 | img->stride[VPX_PLANE_U] = yv12->uv_stride; |
| 56 | img->stride[VPX_PLANE_V] = yv12->uv_stride; |
Alex Converse | 5926e7c | 2014-07-17 11:29:59 -0700 | [diff] [blame] | 57 | img->stride[VPX_PLANE_ALPHA] = yv12->y_stride; |
Deb Mukherjee | 9ed23de | 2014-09-25 15:46:50 -0700 | [diff] [blame] | 58 | #if CONFIG_VP9_HIGHBITDEPTH |
| 59 | if (yv12->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 60 | // vpx_image_t uses byte strides and a pointer to the first byte |
| 61 | // of the image. |
James Zern | 58cb788 | 2015-09-10 00:19:18 -0700 | [diff] [blame] | 62 | img->fmt = (vpx_img_fmt_t)(img->fmt | VPX_IMG_FMT_HIGHBITDEPTH); |
Deb Mukherjee | 9ed23de | 2014-09-25 15:46:50 -0700 | [diff] [blame] | 63 | img->bit_depth = yv12->bit_depth; |
| 64 | img->planes[VPX_PLANE_Y] = (uint8_t*)CONVERT_TO_SHORTPTR(yv12->y_buffer); |
| 65 | img->planes[VPX_PLANE_U] = (uint8_t*)CONVERT_TO_SHORTPTR(yv12->u_buffer); |
| 66 | img->planes[VPX_PLANE_V] = (uint8_t*)CONVERT_TO_SHORTPTR(yv12->v_buffer); |
| 67 | img->planes[VPX_PLANE_ALPHA] = NULL; |
| 68 | img->stride[VPX_PLANE_Y] = 2 * yv12->y_stride; |
| 69 | img->stride[VPX_PLANE_U] = 2 * yv12->uv_stride; |
| 70 | img->stride[VPX_PLANE_V] = 2 * yv12->uv_stride; |
| 71 | img->stride[VPX_PLANE_ALPHA] = 2 * yv12->y_stride; |
| 72 | } |
| 73 | #endif // CONFIG_VP9_HIGHBITDEPTH |
John Koleszar | da58436 | 2013-05-06 15:52:06 -0700 | [diff] [blame] | 74 | img->bps = bps; |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 75 | img->user_priv = user_priv; |
| 76 | img->img_data = yv12->buffer_alloc; |
| 77 | img->img_data_owner = 0; |
| 78 | img->self_allocd = 0; |
| 79 | } |
| 80 | |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 81 | static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img, |
| 82 | YV12_BUFFER_CONFIG *yv12) { |
| 83 | yv12->y_buffer = img->planes[VPX_PLANE_Y]; |
| 84 | yv12->u_buffer = img->planes[VPX_PLANE_U]; |
| 85 | yv12->v_buffer = img->planes[VPX_PLANE_V]; |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 86 | |
| 87 | yv12->y_crop_width = img->d_w; |
| 88 | yv12->y_crop_height = img->d_h; |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 89 | yv12->render_width = img->r_w; |
| 90 | yv12->render_height = img->r_h; |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 91 | yv12->y_width = img->d_w; |
| 92 | yv12->y_height = img->d_h; |
| 93 | |
| 94 | yv12->uv_width = img->x_chroma_shift == 1 ? (1 + yv12->y_width) / 2 |
| 95 | : yv12->y_width; |
| 96 | yv12->uv_height = img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 |
| 97 | : yv12->y_height; |
Yaowu Xu | d602500 | 2014-10-08 09:06:36 -0700 | [diff] [blame] | 98 | yv12->uv_crop_width = yv12->uv_width; |
| 99 | yv12->uv_crop_height = yv12->uv_height; |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 100 | |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 101 | yv12->y_stride = img->stride[VPX_PLANE_Y]; |
| 102 | yv12->uv_stride = img->stride[VPX_PLANE_U]; |
Yaowu Xu | 6b223fc | 2015-01-09 13:04:48 -0800 | [diff] [blame] | 103 | yv12->color_space = img->cs; |
Ronald S. Bultje | eeb5ef0 | 2015-09-15 21:56:51 -0400 | [diff] [blame] | 104 | yv12->color_range = img->range; |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 105 | |
Deb Mukherjee | 9ed23de | 2014-09-25 15:46:50 -0700 | [diff] [blame] | 106 | #if CONFIG_VP9_HIGHBITDEPTH |
| 107 | if (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { |
| 108 | // In vpx_image_t |
| 109 | // planes point to uint8 address of start of data |
| 110 | // stride counts uint8s to reach next row |
| 111 | // In YV12_BUFFER_CONFIG |
| 112 | // y_buffer, u_buffer, v_buffer point to uint16 address of data |
| 113 | // stride and border counts in uint16s |
| 114 | // This means that all the address calculations in the main body of code |
| 115 | // should work correctly. |
| 116 | // However, before we do any pixel operations we need to cast the address |
| 117 | // to a uint16 ponter and double its value. |
| 118 | yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer); |
| 119 | yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer); |
| 120 | yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer); |
| 121 | yv12->y_stride >>= 1; |
| 122 | yv12->uv_stride >>= 1; |
| 123 | yv12->flags = YV12_FLAG_HIGHBITDEPTH; |
| 124 | } else { |
| 125 | yv12->flags = 0; |
| 126 | } |
| 127 | yv12->border = (yv12->y_stride - img->w) / 2; |
| 128 | #else |
| 129 | yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2; |
| 130 | #endif // CONFIG_VP9_HIGHBITDEPTH |
Alex Converse | a0befb9 | 2014-10-01 11:23:57 -0700 | [diff] [blame] | 131 | yv12->subsampling_x = img->x_chroma_shift; |
| 132 | yv12->subsampling_y = img->y_chroma_shift; |
Dmitry Kovalev | b7a4f8a | 2013-05-15 16:29:20 -0700 | [diff] [blame] | 133 | return VPX_CODEC_OK; |
| 134 | } |
| 135 | |
Dmitry Kovalev | 50e54c1 | 2013-04-01 18:23:04 -0700 | [diff] [blame] | 136 | #endif // VP9_VP9_IFACE_COMMON_H_ |