File: vpx_image_test.cc

package info (click to toggle)
libvpx 1.12.0-1%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,216 kB
  • sloc: ansic: 235,377; cpp: 110,379; asm: 22,260; sh: 4,891; python: 4,390; perl: 1,840; makefile: 439
file content (100 lines) | stat: -rw-r--r-- 3,169 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *  Copyright (c) 2024 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <climits>

#include "vpx/vpx_image.h"
#include "third_party/googletest/src/include/gtest/gtest.h"

TEST(VpxImageTest, VpxImgWrapInvalidAlign) {
  const int kWidth = 128;
  const int kHeight = 128;
  unsigned char buf[kWidth * kHeight * 3];

  vpx_image_t img;
  // Set img_data and img_data_owner to junk values. vpx_img_wrap() should
  // not read these values on failure.
  unsigned char empty[] = "";
  img.img_data = empty;
  img.img_data_owner = 1;

  vpx_img_fmt_t format = VPX_IMG_FMT_I444;
  // 'align' must be a power of 2 but is not. This causes the vpx_img_wrap()
  // call to fail. The test verifies we do not read the junk values in 'img'.
  unsigned int align = 31;
  EXPECT_EQ(vpx_img_wrap(&img, format, kWidth, kHeight, align, buf), nullptr);
}

TEST(VpxImageTest, VpxImgAllocNv12) {
  const int kWidth = 128;
  const int kHeight = 128;

  vpx_image_t img;
  vpx_img_fmt_t format = VPX_IMG_FMT_NV12;
  unsigned int align = 32;
  EXPECT_EQ(vpx_img_alloc(&img, format, kWidth, kHeight, align), &img);
  EXPECT_EQ(img.stride[VPX_PLANE_U], img.stride[VPX_PLANE_Y]);
  EXPECT_EQ(img.stride[VPX_PLANE_V], img.stride[VPX_PLANE_U]);
  EXPECT_EQ(img.planes[VPX_PLANE_V], img.planes[VPX_PLANE_U] + 1);
  vpx_img_free(&img);
}

TEST(VpxImageTest, VpxImgAllocHugeWidth) {
  // The stride (0x80000000 * 2) would overflow unsigned int.
  vpx_image_t *image =
      vpx_img_alloc(nullptr, VPX_IMG_FMT_I42016, 0x80000000, 1, 1);
  ASSERT_EQ(image, nullptr);

  // The stride (0x80000000) would overflow int.
  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, 0x80000000, 1, 1);
  ASSERT_EQ(image, nullptr);

  // The aligned width (UINT_MAX + 1) would overflow unsigned int.
  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, UINT_MAX, 1, 1);
  ASSERT_EQ(image, nullptr);

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, 0x7ffffffe, 1, 1);
  if (image) {
    vpx_img_free(image);
  }

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, 285245883, 64, 1);
  if (image) {
    vpx_img_free(image);
  }

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_NV12, 285245883, 64, 1);
  if (image) {
    vpx_img_free(image);
  }

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_YV12, 285245883, 64, 1);
  if (image) {
    vpx_img_free(image);
  }

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I42016, 65536, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[VPX_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    vpx_img_free(image);
  }

  image = vpx_img_alloc(nullptr, VPX_IMG_FMT_I42016, 285245883, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[VPX_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    vpx_img_free(image);
  }
}