Coverage Report

Created: 2026-05-16 07:49

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
8.81k
                     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
8.81k
  int bps;
20
8.81k
  if (!yv12->subsampling_y) {
21
2.62k
    if (!yv12->subsampling_x) {
22
2.39k
      img->fmt = VPX_IMG_FMT_I444;
23
2.39k
      bps = 24;
24
2.39k
    } else {
25
223
      img->fmt = VPX_IMG_FMT_I422;
26
223
      bps = 16;
27
223
    }
28
6.18k
  } else {
29
6.18k
    if (!yv12->subsampling_x) {
30
5.68k
      img->fmt = VPX_IMG_FMT_I440;
31
5.68k
      bps = 16;
32
5.68k
    } else {
33
508
      img->fmt = VPX_IMG_FMT_I420;
34
508
      bps = 12;
35
508
    }
36
6.18k
  }
37
8.81k
  img->cs = yv12->color_space;
38
8.81k
  img->range = yv12->color_range;
39
8.81k
  img->bit_depth = 8;
40
8.81k
  img->w = yv12->y_stride;
41
8.81k
  img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * VP9_ENC_BORDER_IN_PIXELS, 3);
42
8.81k
  img->d_w = yv12->y_crop_width;
43
8.81k
  img->d_h = yv12->y_crop_height;
44
8.81k
  img->r_w = yv12->render_width;
45
8.81k
  img->r_h = yv12->render_height;
46
8.81k
  img->x_chroma_shift = yv12->subsampling_x;
47
8.81k
  img->y_chroma_shift = yv12->subsampling_y;
48
8.81k
  img->planes[VPX_PLANE_Y] = yv12->y_buffer;
49
8.81k
  img->planes[VPX_PLANE_U] = yv12->u_buffer;
50
8.81k
  img->planes[VPX_PLANE_V] = yv12->v_buffer;
51
8.81k
  img->planes[VPX_PLANE_ALPHA] = NULL;
52
8.81k
  img->stride[VPX_PLANE_Y] = yv12->y_stride;
53
8.81k
  img->stride[VPX_PLANE_U] = yv12->uv_stride;
54
8.81k
  img->stride[VPX_PLANE_V] = yv12->uv_stride;
55
8.81k
  img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
56
8.81k
#if CONFIG_VP9_HIGHBITDEPTH
57
8.81k
  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
8.35k
    img->fmt = (vpx_img_fmt_t)(img->fmt | VPX_IMG_FMT_HIGHBITDEPTH);
61
8.35k
    img->bit_depth = yv12->bit_depth;
62
8.35k
    img->planes[VPX_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer);
63
8.35k
    img->planes[VPX_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer);
64
8.35k
    img->planes[VPX_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer);
65
8.35k
    img->planes[VPX_PLANE_ALPHA] = NULL;
66
8.35k
    img->stride[VPX_PLANE_Y] = 2 * yv12->y_stride;
67
8.35k
    img->stride[VPX_PLANE_U] = 2 * yv12->uv_stride;
68
8.35k
    img->stride[VPX_PLANE_V] = 2 * yv12->uv_stride;
69
8.35k
    img->stride[VPX_PLANE_ALPHA] = 2 * yv12->y_stride;
70
8.35k
  }
71
8.81k
#endif  // CONFIG_VP9_HIGHBITDEPTH
72
8.81k
  img->bps = bps;
73
8.81k
  img->user_priv = user_priv;
74
8.81k
  img->img_data = yv12->buffer_alloc;
75
8.81k
  img->img_data_owner = 0;
76
8.81k
  img->self_allocd = 0;
77
8.81k
}
78
79
vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
80
73.6k
                                YV12_BUFFER_CONFIG *yv12) {
81
73.6k
  yv12->y_buffer = img->planes[VPX_PLANE_Y];
82
73.6k
  yv12->u_buffer = img->planes[VPX_PLANE_U];
83
73.6k
  yv12->v_buffer = img->planes[VPX_PLANE_V];
84
85
73.6k
  yv12->y_crop_width = img->d_w;
86
73.6k
  yv12->y_crop_height = img->d_h;
87
73.6k
  yv12->render_width = img->r_w;
88
73.6k
  yv12->render_height = img->r_h;
89
73.6k
  yv12->y_width = img->d_w;
90
73.6k
  yv12->y_height = img->d_h;
91
92
73.6k
  yv12->uv_width = img->x_chroma_shift == 1 || img->fmt == VPX_IMG_FMT_NV12
93
73.6k
                       ? (1 + yv12->y_width) / 2
94
73.6k
                       : yv12->y_width;
95
73.6k
  yv12->uv_height =
96
73.6k
      img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height;
97
73.6k
  yv12->uv_crop_width = yv12->uv_width;
98
73.6k
  yv12->uv_crop_height = yv12->uv_height;
99
100
73.6k
  yv12->y_stride = img->stride[VPX_PLANE_Y];
101
73.6k
  assert(img->stride[VPX_PLANE_U] == img->stride[VPX_PLANE_V]);
102
73.6k
  yv12->uv_stride = img->stride[VPX_PLANE_U];
103
73.6k
  yv12->color_space = img->cs;
104
73.6k
  yv12->color_range = img->range;
105
106
73.6k
#if CONFIG_VP9_HIGHBITDEPTH
107
73.6k
  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
73.6k
  } else {
125
73.6k
    yv12->flags = 0;
126
73.6k
  }
127
73.6k
  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
73.6k
  yv12->subsampling_x = img->x_chroma_shift;
132
73.6k
  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
73.6k
  if (img->fmt == VPX_IMG_FMT_NV12) yv12->subsampling_x = 1;
137
73.6k
  return VPX_CODEC_OK;
138
73.6k
}