Coverage Report

Created: 2026-04-01 07:42

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