summaryrefslogtreecommitdiffstats
path: root/third_party/aom/test/aom_image_test.cc
blob: 0dfb9122159e69261f79674402f9107f3341052d (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include <climits>

#include "aom/aom_image.h"
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"

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

  aom_image_t img;
  // Set img_data and img_data_owner to junk values. aom_img_wrap() should
  // not read these values on failure.
  img.img_data = (unsigned char *)"";
  img.img_data_owner = 1;

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

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

  aom_image_t img;
  aom_img_fmt_t format = AOM_IMG_FMT_I444;
  unsigned int align = 32;
  EXPECT_EQ(aom_img_wrap(&img, format, kWidth, kHeight, align, buf), &img);

  EXPECT_EQ(aom_img_set_rect(&img, 0, 0, kWidth, kHeight, 0), 0);
  // This would result in overflow because -1 is cast to UINT_MAX.
  EXPECT_NE(aom_img_set_rect(&img, static_cast<unsigned int>(-1),
                             static_cast<unsigned int>(-1), kWidth, kHeight, 0),
            0);
}

TEST(AomImageTest, AomImgAllocNone) {
  const int kWidth = 128;
  const int kHeight = 128;

  aom_image_t img;
  aom_img_fmt_t format = AOM_IMG_FMT_NONE;
  unsigned int align = 32;
  ASSERT_EQ(aom_img_alloc(&img, format, kWidth, kHeight, align), nullptr);
}

TEST(AomImageTest, AomImgAllocNv12) {
  const int kWidth = 128;
  const int kHeight = 128;

  aom_image_t img;
  aom_img_fmt_t format = AOM_IMG_FMT_NV12;
  unsigned int align = 32;
  EXPECT_EQ(aom_img_alloc(&img, format, kWidth, kHeight, align), &img);
  EXPECT_EQ(img.stride[AOM_PLANE_U], img.stride[AOM_PLANE_Y]);
  EXPECT_EQ(img.stride[AOM_PLANE_V], 0);
  EXPECT_EQ(img.planes[AOM_PLANE_V], nullptr);
  aom_img_free(&img);
}

TEST(AomImageTest, AomImgAllocHugeWidth) {
  // The stride (0x80000000 * 2) would overflow unsigned int.
  aom_image_t *image =
      aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 0x80000000, 1, 1);
  ASSERT_EQ(image, nullptr);

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

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

  image = aom_img_alloc_with_border(nullptr, AOM_IMG_FMT_I422, 1, INT_MAX, 1,
                                    0x40000000, 0);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 0x7ffffffe, 1, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_NV12, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_YV12, 285245883, 64, 1);
  if (image) {
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 65536, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }

  image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 285245883, 2, 1);
  if (image) {
    uint16_t *y_plane =
        reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
    y_plane[0] = 0;
    y_plane[image->d_w - 1] = 0;
    aom_img_free(image);
  }
}