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