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