blob: 59db77688d148ea14f37a3906262917938ce2a37 [file] [log] [blame]
Yaowu Xuf883b422016-08-30 14:01:10 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuf883b422016-08-30 14:01:10 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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 Xuf883b422016-08-30 14:01:10 -070010 */
11#ifndef AV1_AV1_IFACE_COMMON_H_
12#define AV1_AV1_IFACE_COMMON_H_
13
14#include "aom_ports/mem.h"
15
16static void yuvconfig2image(aom_image_t *img, const YV12_BUFFER_CONFIG *yv12,
17 void *user_priv) {
Johann123e8a62017-12-28 14:40:49 -080018 /* 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 */
Yaowu Xuf883b422016-08-30 14:01:10 -070023 int bps;
24 if (!yv12->subsampling_y) {
25 if (!yv12->subsampling_x) {
26 img->fmt = AOM_IMG_FMT_I444;
27 bps = 24;
28 } else {
29 img->fmt = AOM_IMG_FMT_I422;
30 bps = 16;
31 }
32 } else {
Thomas Daede2e3cd5c2018-03-30 14:22:15 -070033 img->fmt = AOM_IMG_FMT_I420;
34 bps = 12;
Yaowu Xuf883b422016-08-30 14:01:10 -070035 }
Andrey Norkin9e694632017-12-21 18:50:57 -080036 img->cp = yv12->color_primaries;
37 img->tc = yv12->transfer_characteristics;
38 img->mc = yv12->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -080039 img->monochrome = yv12->monochrome;
anorkin76fb1262017-03-22 15:12:12 -070040 img->csp = yv12->chroma_sample_position;
Yaowu Xuf883b422016-08-30 14:01:10 -070041 img->range = yv12->color_range;
42 img->bit_depth = 8;
43 img->w = yv12->y_stride;
Yaowu Xu671f2bd2016-09-30 15:07:57 -070044 img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * AOM_BORDER_IN_PIXELS, 3);
Yaowu Xuf883b422016-08-30 14:01:10 -070045 img->d_w = yv12->y_crop_width;
46 img->d_h = yv12->y_crop_height;
47 img->r_w = yv12->render_width;
48 img->r_h = yv12->render_height;
49 img->x_chroma_shift = yv12->subsampling_x;
50 img->y_chroma_shift = yv12->subsampling_y;
51 img->planes[AOM_PLANE_Y] = yv12->y_buffer;
52 img->planes[AOM_PLANE_U] = yv12->u_buffer;
53 img->planes[AOM_PLANE_V] = yv12->v_buffer;
54 img->planes[AOM_PLANE_ALPHA] = NULL;
55 img->stride[AOM_PLANE_Y] = yv12->y_stride;
56 img->stride[AOM_PLANE_U] = yv12->uv_stride;
57 img->stride[AOM_PLANE_V] = yv12->uv_stride;
58 img->stride[AOM_PLANE_ALPHA] = yv12->y_stride;
Yaowu Xuf883b422016-08-30 14:01:10 -070059 if (yv12->flags & YV12_FLAG_HIGHBITDEPTH) {
60 // aom_image_t uses byte strides and a pointer to the first byte
61 // of the image.
62 img->fmt = (aom_img_fmt_t)(img->fmt | AOM_IMG_FMT_HIGHBITDEPTH);
63 img->bit_depth = yv12->bit_depth;
64 img->planes[AOM_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer);
65 img->planes[AOM_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer);
66 img->planes[AOM_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer);
67 img->planes[AOM_PLANE_ALPHA] = NULL;
68 img->stride[AOM_PLANE_Y] = 2 * yv12->y_stride;
69 img->stride[AOM_PLANE_U] = 2 * yv12->uv_stride;
70 img->stride[AOM_PLANE_V] = 2 * yv12->uv_stride;
71 img->stride[AOM_PLANE_ALPHA] = 2 * yv12->y_stride;
72 }
Yaowu Xuf883b422016-08-30 14:01:10 -070073 img->bps = bps;
74 img->user_priv = user_priv;
75 img->img_data = yv12->buffer_alloc;
76 img->img_data_owner = 0;
77 img->self_allocd = 0;
78}
79
80static aom_codec_err_t image2yuvconfig(const aom_image_t *img,
81 YV12_BUFFER_CONFIG *yv12) {
82 yv12->y_buffer = img->planes[AOM_PLANE_Y];
83 yv12->u_buffer = img->planes[AOM_PLANE_U];
84 yv12->v_buffer = img->planes[AOM_PLANE_V];
85
86 yv12->y_crop_width = img->d_w;
87 yv12->y_crop_height = img->d_h;
88 yv12->render_width = img->r_w;
89 yv12->render_height = img->r_h;
90 yv12->y_width = img->d_w;
91 yv12->y_height = img->d_h;
92
93 yv12->uv_width =
94 img->x_chroma_shift == 1 ? (1 + yv12->y_width) / 2 : yv12->y_width;
95 yv12->uv_height =
96 img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height;
97 yv12->uv_crop_width = yv12->uv_width;
98 yv12->uv_crop_height = yv12->uv_height;
99
100 yv12->y_stride = img->stride[AOM_PLANE_Y];
101 yv12->uv_stride = img->stride[AOM_PLANE_U];
Andrey Norkin9e694632017-12-21 18:50:57 -0800102 yv12->color_primaries = img->cp;
103 yv12->transfer_characteristics = img->tc;
104 yv12->matrix_coefficients = img->mc;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800105 yv12->monochrome = img->monochrome;
anorkin76fb1262017-03-22 15:12:12 -0700106 yv12->chroma_sample_position = img->csp;
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 yv12->color_range = img->range;
108
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
110 // In aom_image_t
111 // planes point to uint8 address of start of data
112 // stride counts uint8s to reach next row
113 // In YV12_BUFFER_CONFIG
114 // y_buffer, u_buffer, v_buffer point to uint16 address of data
115 // stride and border counts in uint16s
116 // This means that all the address calculations in the main body of code
117 // should work correctly.
118 // However, before we do any pixel operations we need to cast the address
119 // to a uint16 ponter and double its value.
120 yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer);
121 yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer);
122 yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer);
123 yv12->y_stride >>= 1;
124 yv12->uv_stride >>= 1;
125 yv12->flags = YV12_FLAG_HIGHBITDEPTH;
126 } else {
127 yv12->flags = 0;
128 }
129 yv12->border = (yv12->y_stride - img->w) / 2;
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 yv12->subsampling_x = img->x_chroma_shift;
131 yv12->subsampling_y = img->y_chroma_shift;
132 return AOM_CODEC_OK;
133}
134
Yaowu Xuf883b422016-08-30 14:01:10 -0700135#endif // AV1_AV1_IFACE_COMMON_H_