1 /*
2 * Copyright (c) 2020, 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 // Tests for https://crbug.com/aomedia/2777.
13 //
14 // Encode images with a small width (<= two AV1 superblocks) or a small height
15 // (<= one AV1 superblock) with multiple threads. aom_codec_encode() should
16 // not crash.
17
18 #include <memory>
19
20 #include "aom/aomcx.h"
21 #include "aom/aom_encoder.h"
22 #include "config/aom_config.h"
23 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
24
25 namespace {
26
27 // Dummy buffer of zero samples.
28 constexpr unsigned char kBuffer[256 * 512 + 2 * 128 * 256] = { 0 };
29 #if CONFIG_REALTIME_ONLY
30 const int kUsage = 1;
31 #else
32 const int kUsage = 0;
33 #endif
34
TEST(EncodeSmallWidthHeight,SmallWidthMultiThreaded)35 TEST(EncodeSmallWidthHeight, SmallWidthMultiThreaded) {
36 // The image has only one tile and the tile is two AV1 superblocks wide.
37 // For speed >= 1, superblock size is 64x64 (see av1_select_sb_size()).
38 constexpr int kWidth = 128;
39 constexpr int kHeight = 512;
40
41 aom_image_t img;
42 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, kWidth, kHeight, 1,
43 const_cast<unsigned char *>(kBuffer)));
44
45 aom_codec_iface_t *iface = aom_codec_av1_cx();
46 aom_codec_enc_cfg_t cfg;
47 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
48 cfg.g_threads = 2;
49 cfg.g_w = kWidth;
50 cfg.g_h = kHeight;
51 aom_codec_ctx_t enc;
52 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
53 EXPECT_EQ(AOM_CODEC_OK, aom_codec_control(&enc, AOME_SET_CPUUSED, 5));
54 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
55 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
56 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
57 }
58
59 #if !CONFIG_REALTIME_ONLY
TEST(EncodeSmallWidthHeight,SmallWidthMultiThreadedSpeed0)60 TEST(EncodeSmallWidthHeight, SmallWidthMultiThreadedSpeed0) {
61 // The image has only one tile and the tile is two AV1 superblocks wide.
62 // For speed 0, superblock size is 128x128 (see av1_select_sb_size()).
63 constexpr int kWidth = 256;
64 constexpr int kHeight = 512;
65
66 aom_image_t img;
67 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, kWidth, kHeight, 1,
68 const_cast<unsigned char *>(kBuffer)));
69
70 aom_codec_iface_t *iface = aom_codec_av1_cx();
71 aom_codec_enc_cfg_t cfg;
72 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, 0));
73 cfg.g_threads = 2;
74 cfg.g_w = kWidth;
75 cfg.g_h = kHeight;
76 aom_codec_ctx_t enc;
77 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
78 EXPECT_EQ(AOM_CODEC_OK, aom_codec_control(&enc, AOME_SET_CPUUSED, 0));
79 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
80 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
81 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
82 }
83 #endif
84
TEST(EncodeSmallWidthHeight,SmallHeightMultiThreaded)85 TEST(EncodeSmallWidthHeight, SmallHeightMultiThreaded) {
86 // The image has only one tile and the tile is one AV1 superblock tall.
87 // For speed >= 1, superblock size is 64x64 (see av1_select_sb_size()).
88 constexpr int kWidth = 512;
89 constexpr int kHeight = 64;
90
91 aom_image_t img;
92 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, kWidth, kHeight, 1,
93 const_cast<unsigned char *>(kBuffer)));
94
95 aom_codec_iface_t *iface = aom_codec_av1_cx();
96 aom_codec_enc_cfg_t cfg;
97 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
98 cfg.g_threads = 2;
99 cfg.g_w = kWidth;
100 cfg.g_h = kHeight;
101 aom_codec_ctx_t enc;
102 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
103 EXPECT_EQ(AOM_CODEC_OK, aom_codec_control(&enc, AOME_SET_CPUUSED, 5));
104 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
105 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
106 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
107 }
108
109 #if !CONFIG_REALTIME_ONLY
TEST(EncodeSmallWidthHeight,SmallHeightMultiThreadedSpeed0)110 TEST(EncodeSmallWidthHeight, SmallHeightMultiThreadedSpeed0) {
111 // The image has only one tile and the tile is one AV1 superblock tall.
112 // For speed 0, superblock size is 128x128 (see av1_select_sb_size()).
113 constexpr int kWidth = 512;
114 constexpr int kHeight = 128;
115
116 aom_image_t img;
117 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, kWidth, kHeight, 1,
118 const_cast<unsigned char *>(kBuffer)));
119
120 aom_codec_iface_t *iface = aom_codec_av1_cx();
121 aom_codec_enc_cfg_t cfg;
122 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, 0));
123 cfg.g_threads = 2;
124 cfg.g_w = kWidth;
125 cfg.g_h = kHeight;
126 aom_codec_ctx_t enc;
127 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
128 EXPECT_EQ(AOM_CODEC_OK, aom_codec_control(&enc, AOME_SET_CPUUSED, 0));
129 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
130 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
131 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
132 }
133 #endif
134
135 // A reproducer test for aomedia:3113. The test should complete without any
136 // memory errors.
137 TEST(EncodeSmallWidthHeight, 1x1) {
138 constexpr int kWidth = 1;
139 constexpr int kHeight = 1;
140
141 // This test cannot use aom_img_alloc() or aom_img_wrap() because they call
142 // align_image_dimension() to align img.w and img.h to the next even number
143 // (2). In this test it is important to set img.w and img.h to 1. Therefore we
144 // set up img manually.
145 aom_image_t img;
146 memset(&img, 0, sizeof(img));
147 img.fmt = AOM_IMG_FMT_I420;
148 img.bit_depth = 8;
149 img.w = kWidth;
150 img.h = kHeight;
151 img.d_w = kWidth;
152 img.d_h = kHeight;
153 img.x_chroma_shift = 1;
154 img.y_chroma_shift = 1;
155 img.bps = 12;
156 int y_stride = kWidth;
157 int uv_stride = (kWidth + 1) >> 1;
158 int y_height = kHeight;
159 int uv_height = (kHeight + 1) >> 1;
160 img.stride[AOM_PLANE_Y] = y_stride;
161 img.stride[AOM_PLANE_U] = img.stride[AOM_PLANE_V] = uv_stride;
162 std::unique_ptr<unsigned char[]> y_plane(
163 new unsigned char[y_height * y_stride]());
164 ASSERT_NE(y_plane, nullptr);
165 std::unique_ptr<unsigned char[]> u_plane(
166 new unsigned char[uv_height * uv_stride]());
167 ASSERT_NE(u_plane, nullptr);
168 std::unique_ptr<unsigned char[]> v_plane(
169 new unsigned char[uv_height * uv_stride]());
170 ASSERT_NE(v_plane, nullptr);
171 img.planes[AOM_PLANE_Y] = y_plane.get();
172 img.planes[AOM_PLANE_U] = u_plane.get();
173 img.planes[AOM_PLANE_V] = v_plane.get();
174
175 aom_codec_iface_t *iface = aom_codec_av1_cx();
176 aom_codec_enc_cfg_t cfg;
177 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_config_default(iface, &cfg, kUsage));
178 cfg.g_w = kWidth;
179 cfg.g_h = kHeight;
180 aom_codec_ctx_t enc;
181 EXPECT_EQ(AOM_CODEC_OK, aom_codec_enc_init(&enc, iface, &cfg, 0));
182 EXPECT_EQ(AOM_CODEC_OK, aom_codec_control(&enc, AOME_SET_CPUUSED, 5));
183 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, &img, 0, 1, 0));
184 EXPECT_EQ(AOM_CODEC_OK, aom_codec_encode(&enc, nullptr, 0, 0, 0));
185 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc));
186 }
187
188 } // namespace
189