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