/src/libvpx/vp9/vp9_iface_common.c
Line | Count | Source (jump to first uncovered line) |
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" |
12 | | void yuvconfig2image(vpx_image_t *img, const YV12_BUFFER_CONFIG *yv12, |
13 | 0 | 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 | 0 | int bps; |
19 | 0 | if (!yv12->subsampling_y) { |
20 | 0 | if (!yv12->subsampling_x) { |
21 | 0 | img->fmt = VPX_IMG_FMT_I444; |
22 | 0 | bps = 24; |
23 | 0 | } else { |
24 | 0 | img->fmt = VPX_IMG_FMT_I422; |
25 | 0 | bps = 16; |
26 | 0 | } |
27 | 0 | } else { |
28 | 0 | if (!yv12->subsampling_x) { |
29 | 0 | img->fmt = VPX_IMG_FMT_I440; |
30 | 0 | bps = 16; |
31 | 0 | } else { |
32 | 0 | img->fmt = VPX_IMG_FMT_I420; |
33 | 0 | bps = 12; |
34 | 0 | } |
35 | 0 | } |
36 | 0 | img->cs = yv12->color_space; |
37 | 0 | img->range = yv12->color_range; |
38 | 0 | img->bit_depth = 8; |
39 | 0 | img->w = yv12->y_stride; |
40 | 0 | img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * VP9_ENC_BORDER_IN_PIXELS, 3); |
41 | 0 | img->d_w = yv12->y_crop_width; |
42 | 0 | img->d_h = yv12->y_crop_height; |
43 | 0 | img->r_w = yv12->render_width; |
44 | 0 | img->r_h = yv12->render_height; |
45 | 0 | img->x_chroma_shift = yv12->subsampling_x; |
46 | 0 | img->y_chroma_shift = yv12->subsampling_y; |
47 | 0 | img->planes[VPX_PLANE_Y] = yv12->y_buffer; |
48 | 0 | img->planes[VPX_PLANE_U] = yv12->u_buffer; |
49 | 0 | img->planes[VPX_PLANE_V] = yv12->v_buffer; |
50 | 0 | img->planes[VPX_PLANE_ALPHA] = NULL; |
51 | 0 | img->stride[VPX_PLANE_Y] = yv12->y_stride; |
52 | 0 | img->stride[VPX_PLANE_U] = yv12->uv_stride; |
53 | 0 | img->stride[VPX_PLANE_V] = yv12->uv_stride; |
54 | 0 | img->stride[VPX_PLANE_ALPHA] = yv12->y_stride; |
55 | 0 | #if CONFIG_VP9_HIGHBITDEPTH |
56 | 0 | 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 | 0 | img->fmt = (vpx_img_fmt_t)(img->fmt | VPX_IMG_FMT_HIGHBITDEPTH); |
60 | 0 | img->bit_depth = yv12->bit_depth; |
61 | 0 | img->planes[VPX_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer); |
62 | 0 | img->planes[VPX_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer); |
63 | 0 | img->planes[VPX_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer); |
64 | 0 | img->planes[VPX_PLANE_ALPHA] = NULL; |
65 | 0 | img->stride[VPX_PLANE_Y] = 2 * yv12->y_stride; |
66 | 0 | img->stride[VPX_PLANE_U] = 2 * yv12->uv_stride; |
67 | 0 | img->stride[VPX_PLANE_V] = 2 * yv12->uv_stride; |
68 | 0 | img->stride[VPX_PLANE_ALPHA] = 2 * yv12->y_stride; |
69 | 0 | } |
70 | 0 | #endif // CONFIG_VP9_HIGHBITDEPTH |
71 | 0 | img->bps = bps; |
72 | 0 | img->user_priv = user_priv; |
73 | 0 | img->img_data = yv12->buffer_alloc; |
74 | 0 | img->img_data_owner = 0; |
75 | 0 | img->self_allocd = 0; |
76 | 0 | } |
77 | | |
78 | | vpx_codec_err_t image2yuvconfig(const vpx_image_t *img, |
79 | 42.7k | YV12_BUFFER_CONFIG *yv12) { |
80 | 42.7k | yv12->y_buffer = img->planes[VPX_PLANE_Y]; |
81 | 42.7k | yv12->u_buffer = img->planes[VPX_PLANE_U]; |
82 | 42.7k | yv12->v_buffer = img->planes[VPX_PLANE_V]; |
83 | | |
84 | 42.7k | yv12->y_crop_width = img->d_w; |
85 | 42.7k | yv12->y_crop_height = img->d_h; |
86 | 42.7k | yv12->render_width = img->r_w; |
87 | 42.7k | yv12->render_height = img->r_h; |
88 | 42.7k | yv12->y_width = img->d_w; |
89 | 42.7k | yv12->y_height = img->d_h; |
90 | | |
91 | 42.7k | yv12->uv_width = img->x_chroma_shift == 1 || img->fmt == VPX_IMG_FMT_NV12 |
92 | 42.7k | ? (1 + yv12->y_width) / 2 |
93 | 42.7k | : yv12->y_width; |
94 | 42.7k | yv12->uv_height = |
95 | 42.7k | img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height; |
96 | 42.7k | yv12->uv_crop_width = yv12->uv_width; |
97 | 42.7k | yv12->uv_crop_height = yv12->uv_height; |
98 | | |
99 | 42.7k | yv12->y_stride = img->stride[VPX_PLANE_Y]; |
100 | 42.7k | yv12->uv_stride = img->stride[VPX_PLANE_U]; |
101 | 42.7k | yv12->color_space = img->cs; |
102 | 42.7k | yv12->color_range = img->range; |
103 | | |
104 | 42.7k | #if CONFIG_VP9_HIGHBITDEPTH |
105 | 42.7k | if (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) { |
106 | | // In vpx_image_t |
107 | | // planes point to uint8 address of start of data |
108 | | // stride counts uint8s to reach next row |
109 | | // In YV12_BUFFER_CONFIG |
110 | | // y_buffer, u_buffer, v_buffer point to uint16 address of data |
111 | | // stride and border counts in uint16s |
112 | | // This means that all the address calculations in the main body of code |
113 | | // should work correctly. |
114 | | // However, before we do any pixel operations we need to cast the address |
115 | | // to a uint16 ponter and double its value. |
116 | 0 | yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer); |
117 | 0 | yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer); |
118 | 0 | yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer); |
119 | 0 | yv12->y_stride >>= 1; |
120 | 0 | yv12->uv_stride >>= 1; |
121 | 0 | yv12->flags = YV12_FLAG_HIGHBITDEPTH; |
122 | 42.7k | } else { |
123 | 42.7k | yv12->flags = 0; |
124 | 42.7k | } |
125 | 42.7k | yv12->border = (yv12->y_stride - img->w) / 2; |
126 | | #else |
127 | | yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2; |
128 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
129 | 42.7k | yv12->subsampling_x = img->x_chroma_shift; |
130 | 42.7k | yv12->subsampling_y = img->y_chroma_shift; |
131 | | // When reading the data, UV are in one plane for NV12 format, thus |
132 | | // x_chroma_shift is 0. After converting, UV are in separate planes, and |
133 | | // subsampling_x should be set to 1. |
134 | 42.7k | if (img->fmt == VPX_IMG_FMT_NV12) yv12->subsampling_x = 1; |
135 | 42.7k | return VPX_CODEC_OK; |
136 | 42.7k | } |