1 /*
2 * Copyright (c) 2019 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file in the root of the source tree. An additional
6 * intellectual property rights grant can be found in the file PATENTS.
7 * All contributing project authors may be found in the AUTHORS file in
8 * the root of the source tree.
9 */
10
11 #include "vp9/vp9_iface_common.h"
yuvconfig2image(vpx_image_t * img,const YV12_BUFFER_CONFIG * yv12,void * user_priv)12 void yuvconfig2image(vpx_image_t *img, const YV12_BUFFER_CONFIG *yv12,
13 void *user_priv) {
14 /** vpx_img_wrap() doesn't allow specifying independent strides for
15 * the Y, U, and V planes, nor other alignment adjustments that
16 * might be representable by a YV12_BUFFER_CONFIG, so we just
17 * initialize all the fields.*/
18 int bps;
19 if (!yv12->subsampling_y) {
20 if (!yv12->subsampling_x) {
21 img->fmt = VPX_IMG_FMT_I444;
22 bps = 24;
23 } else {
24 img->fmt = VPX_IMG_FMT_I422;
25 bps = 16;
26 }
27 } else {
28 if (!yv12->subsampling_x) {
29 img->fmt = VPX_IMG_FMT_I440;
30 bps = 16;
31 } else {
32 img->fmt = VPX_IMG_FMT_I420;
33 bps = 12;
34 }
35 }
36 img->cs = yv12->color_space;
37 img->range = yv12->color_range;
38 img->bit_depth = 8;
39 img->w = yv12->y_stride;
40 img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * VP9_ENC_BORDER_IN_PIXELS, 3);
41 img->d_w = yv12->y_crop_width;
42 img->d_h = yv12->y_crop_height;
43 img->r_w = yv12->render_width;
44 img->r_h = yv12->render_height;
45 img->x_chroma_shift = yv12->subsampling_x;
46 img->y_chroma_shift = yv12->subsampling_y;
47 img->planes[VPX_PLANE_Y] = yv12->y_buffer;
48 img->planes[VPX_PLANE_U] = yv12->u_buffer;
49 img->planes[VPX_PLANE_V] = yv12->v_buffer;
50 img->planes[VPX_PLANE_ALPHA] = NULL;
51 img->stride[VPX_PLANE_Y] = yv12->y_stride;
52 img->stride[VPX_PLANE_U] = yv12->uv_stride;
53 img->stride[VPX_PLANE_V] = yv12->uv_stride;
54 img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
55 #if CONFIG_VP9_HIGHBITDEPTH
56 if (yv12->flags & YV12_FLAG_HIGHBITDEPTH) {
57 // vpx_image_t uses byte strides and a pointer to the first byte
58 // of the image.
59 img->fmt = (vpx_img_fmt_t)(img->fmt | VPX_IMG_FMT_HIGHBITDEPTH);
60 img->bit_depth = yv12->bit_depth;
61 img->planes[VPX_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer);
62 img->planes[VPX_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer);
63 img->planes[VPX_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer);
64 img->planes[VPX_PLANE_ALPHA] = NULL;
65 img->stride[VPX_PLANE_Y] = 2 * yv12->y_stride;
66 img->stride[VPX_PLANE_U] = 2 * yv12->uv_stride;
67 img->stride[VPX_PLANE_V] = 2 * yv12->uv_stride;
68 img->stride[VPX_PLANE_ALPHA] = 2 * yv12->y_stride;
69 }
70 #endif // CONFIG_VP9_HIGHBITDEPTH
71 img->bps = bps;
72 img->user_priv = user_priv;
73 img->img_data = yv12->buffer_alloc;
74 img->img_data_owner = 0;
75 img->self_allocd = 0;
76 }
77
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)78 vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
79 YV12_BUFFER_CONFIG *yv12) {
80 yv12->y_buffer = img->planes[VPX_PLANE_Y];
81 yv12->u_buffer = img->planes[VPX_PLANE_U];
82 yv12->v_buffer = img->planes[VPX_PLANE_V];
83
84 yv12->y_crop_width = img->d_w;
85 yv12->y_crop_height = img->d_h;
86 yv12->render_width = img->r_w;
87 yv12->render_height = img->r_h;
88 yv12->y_width = img->d_w;
89 yv12->y_height = img->d_h;
90
91 yv12->uv_width =
92 img->x_chroma_shift == 1 ? (1 + yv12->y_width) / 2 : yv12->y_width;
93 yv12->uv_height =
94 img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height;
95 yv12->uv_crop_width = yv12->uv_width;
96 yv12->uv_crop_height = yv12->uv_height;
97
98 yv12->y_stride = img->stride[VPX_PLANE_Y];
99 yv12->uv_stride = img->stride[VPX_PLANE_U];
100 yv12->color_space = img->cs;
101 yv12->color_range = img->range;
102
103 #if CONFIG_VP9_HIGHBITDEPTH
104 if (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
105 // In vpx_image_t
106 // planes point to uint8 address of start of data
107 // stride counts uint8s to reach next row
108 // In YV12_BUFFER_CONFIG
109 // y_buffer, u_buffer, v_buffer point to uint16 address of data
110 // stride and border counts in uint16s
111 // This means that all the address calculations in the main body of code
112 // should work correctly.
113 // However, before we do any pixel operations we need to cast the address
114 // to a uint16 ponter and double its value.
115 yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer);
116 yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer);
117 yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer);
118 yv12->y_stride >>= 1;
119 yv12->uv_stride >>= 1;
120 yv12->flags = YV12_FLAG_HIGHBITDEPTH;
121 } else {
122 yv12->flags = 0;
123 }
124 yv12->border = (yv12->y_stride - img->w) / 2;
125 #else
126 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
127 #endif // CONFIG_VP9_HIGHBITDEPTH
128 yv12->subsampling_x = img->x_chroma_shift;
129 yv12->subsampling_y = img->y_chroma_shift;
130 return VPX_CODEC_OK;
131 }
132