1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <climits>
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_image.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
16*77c1e3ccSAndroid Build Coastguard Worker
TEST(AomImageTest,AomImgWrapInvalidAlign)17*77c1e3ccSAndroid Build Coastguard Worker TEST(AomImageTest, AomImgWrapInvalidAlign) {
18*77c1e3ccSAndroid Build Coastguard Worker const int kWidth = 128;
19*77c1e3ccSAndroid Build Coastguard Worker const int kHeight = 128;
20*77c1e3ccSAndroid Build Coastguard Worker unsigned char buf[kWidth * kHeight * 3];
21*77c1e3ccSAndroid Build Coastguard Worker
22*77c1e3ccSAndroid Build Coastguard Worker aom_image_t img;
23*77c1e3ccSAndroid Build Coastguard Worker // Set img_data and img_data_owner to junk values. aom_img_wrap() should
24*77c1e3ccSAndroid Build Coastguard Worker // not read these values on failure.
25*77c1e3ccSAndroid Build Coastguard Worker img.img_data = (unsigned char *)"";
26*77c1e3ccSAndroid Build Coastguard Worker img.img_data_owner = 1;
27*77c1e3ccSAndroid Build Coastguard Worker
28*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t format = AOM_IMG_FMT_I444;
29*77c1e3ccSAndroid Build Coastguard Worker // 'align' must be a power of 2 but is not. This causes the aom_img_wrap()
30*77c1e3ccSAndroid Build Coastguard Worker // call to fail. The test verifies we do not read the junk values in 'img'.
31*77c1e3ccSAndroid Build Coastguard Worker unsigned int align = 31;
32*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(aom_img_wrap(&img, format, kWidth, kHeight, align, buf), nullptr);
33*77c1e3ccSAndroid Build Coastguard Worker }
34*77c1e3ccSAndroid Build Coastguard Worker
TEST(AomImageTest,AomImgSetRectOverflow)35*77c1e3ccSAndroid Build Coastguard Worker TEST(AomImageTest, AomImgSetRectOverflow) {
36*77c1e3ccSAndroid Build Coastguard Worker const int kWidth = 128;
37*77c1e3ccSAndroid Build Coastguard Worker const int kHeight = 128;
38*77c1e3ccSAndroid Build Coastguard Worker unsigned char buf[kWidth * kHeight * 3];
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker aom_image_t img;
41*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t format = AOM_IMG_FMT_I444;
42*77c1e3ccSAndroid Build Coastguard Worker unsigned int align = 32;
43*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(aom_img_wrap(&img, format, kWidth, kHeight, align, buf), &img);
44*77c1e3ccSAndroid Build Coastguard Worker
45*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(aom_img_set_rect(&img, 0, 0, kWidth, kHeight, 0), 0);
46*77c1e3ccSAndroid Build Coastguard Worker // This would result in overflow because -1 is cast to UINT_MAX.
47*77c1e3ccSAndroid Build Coastguard Worker EXPECT_NE(aom_img_set_rect(&img, static_cast<unsigned int>(-1),
48*77c1e3ccSAndroid Build Coastguard Worker static_cast<unsigned int>(-1), kWidth, kHeight, 0),
49*77c1e3ccSAndroid Build Coastguard Worker 0);
50*77c1e3ccSAndroid Build Coastguard Worker }
51*77c1e3ccSAndroid Build Coastguard Worker
TEST(AomImageTest,AomImgAllocNone)52*77c1e3ccSAndroid Build Coastguard Worker TEST(AomImageTest, AomImgAllocNone) {
53*77c1e3ccSAndroid Build Coastguard Worker const int kWidth = 128;
54*77c1e3ccSAndroid Build Coastguard Worker const int kHeight = 128;
55*77c1e3ccSAndroid Build Coastguard Worker
56*77c1e3ccSAndroid Build Coastguard Worker aom_image_t img;
57*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t format = AOM_IMG_FMT_NONE;
58*77c1e3ccSAndroid Build Coastguard Worker unsigned int align = 32;
59*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(aom_img_alloc(&img, format, kWidth, kHeight, align), nullptr);
60*77c1e3ccSAndroid Build Coastguard Worker }
61*77c1e3ccSAndroid Build Coastguard Worker
TEST(AomImageTest,AomImgAllocNv12)62*77c1e3ccSAndroid Build Coastguard Worker TEST(AomImageTest, AomImgAllocNv12) {
63*77c1e3ccSAndroid Build Coastguard Worker const int kWidth = 128;
64*77c1e3ccSAndroid Build Coastguard Worker const int kHeight = 128;
65*77c1e3ccSAndroid Build Coastguard Worker
66*77c1e3ccSAndroid Build Coastguard Worker aom_image_t img;
67*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t format = AOM_IMG_FMT_NV12;
68*77c1e3ccSAndroid Build Coastguard Worker unsigned int align = 32;
69*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(aom_img_alloc(&img, format, kWidth, kHeight, align), &img);
70*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(img.stride[AOM_PLANE_U], img.stride[AOM_PLANE_Y]);
71*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(img.stride[AOM_PLANE_V], 0);
72*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(img.planes[AOM_PLANE_V], nullptr);
73*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&img);
74*77c1e3ccSAndroid Build Coastguard Worker }
75*77c1e3ccSAndroid Build Coastguard Worker
TEST(AomImageTest,AomImgAllocHugeWidth)76*77c1e3ccSAndroid Build Coastguard Worker TEST(AomImageTest, AomImgAllocHugeWidth) {
77*77c1e3ccSAndroid Build Coastguard Worker // The stride (0x80000000 * 2) would overflow unsigned int.
78*77c1e3ccSAndroid Build Coastguard Worker aom_image_t *image =
79*77c1e3ccSAndroid Build Coastguard Worker aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 0x80000000, 1, 1);
80*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(image, nullptr);
81*77c1e3ccSAndroid Build Coastguard Worker
82*77c1e3ccSAndroid Build Coastguard Worker // The stride (0x80000000) would overflow int.
83*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 0x80000000, 1, 1);
84*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(image, nullptr);
85*77c1e3ccSAndroid Build Coastguard Worker
86*77c1e3ccSAndroid Build Coastguard Worker // The aligned width (UINT_MAX + 1) would overflow unsigned int.
87*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, UINT_MAX, 1, 1);
88*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(image, nullptr);
89*77c1e3ccSAndroid Build Coastguard Worker
90*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc_with_border(nullptr, AOM_IMG_FMT_I422, 1, INT_MAX, 1,
91*77c1e3ccSAndroid Build Coastguard Worker 0x40000000, 0);
92*77c1e3ccSAndroid Build Coastguard Worker if (image) {
93*77c1e3ccSAndroid Build Coastguard Worker uint16_t *y_plane =
94*77c1e3ccSAndroid Build Coastguard Worker reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
95*77c1e3ccSAndroid Build Coastguard Worker y_plane[0] = 0;
96*77c1e3ccSAndroid Build Coastguard Worker y_plane[image->d_w - 1] = 0;
97*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
98*77c1e3ccSAndroid Build Coastguard Worker }
99*77c1e3ccSAndroid Build Coastguard Worker
100*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 0x7ffffffe, 1, 1);
101*77c1e3ccSAndroid Build Coastguard Worker if (image) {
102*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
103*77c1e3ccSAndroid Build Coastguard Worker }
104*77c1e3ccSAndroid Build Coastguard Worker
105*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, 285245883, 64, 1);
106*77c1e3ccSAndroid Build Coastguard Worker if (image) {
107*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
108*77c1e3ccSAndroid Build Coastguard Worker }
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_NV12, 285245883, 64, 1);
111*77c1e3ccSAndroid Build Coastguard Worker if (image) {
112*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
113*77c1e3ccSAndroid Build Coastguard Worker }
114*77c1e3ccSAndroid Build Coastguard Worker
115*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_YV12, 285245883, 64, 1);
116*77c1e3ccSAndroid Build Coastguard Worker if (image) {
117*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker
120*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 65536, 2, 1);
121*77c1e3ccSAndroid Build Coastguard Worker if (image) {
122*77c1e3ccSAndroid Build Coastguard Worker uint16_t *y_plane =
123*77c1e3ccSAndroid Build Coastguard Worker reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
124*77c1e3ccSAndroid Build Coastguard Worker y_plane[0] = 0;
125*77c1e3ccSAndroid Build Coastguard Worker y_plane[image->d_w - 1] = 0;
126*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
127*77c1e3ccSAndroid Build Coastguard Worker }
128*77c1e3ccSAndroid Build Coastguard Worker
129*77c1e3ccSAndroid Build Coastguard Worker image = aom_img_alloc(nullptr, AOM_IMG_FMT_I42016, 285245883, 2, 1);
130*77c1e3ccSAndroid Build Coastguard Worker if (image) {
131*77c1e3ccSAndroid Build Coastguard Worker uint16_t *y_plane =
132*77c1e3ccSAndroid Build Coastguard Worker reinterpret_cast<uint16_t *>(image->planes[AOM_PLANE_Y]);
133*77c1e3ccSAndroid Build Coastguard Worker y_plane[0] = 0;
134*77c1e3ccSAndroid Build Coastguard Worker y_plane[image->d_w - 1] = 0;
135*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(image);
136*77c1e3ccSAndroid Build Coastguard Worker }
137*77c1e3ccSAndroid Build Coastguard Worker }
138