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