1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define private public
17 #define protected public
18 #include <gtest/gtest.h>
19
20 #include "buffer_packer_stream.h"
21 #include "image_format_convert.h"
22 #include "image_format_convert_ext_utils.h"
23 #include "image_log.h"
24 #include "image_source.h"
25 #include "image_type.h"
26 #include "image_utils.h"
27 #include "media_errors.h"
28
29 using namespace testing::ext;
30 namespace OHOS {
31 namespace Media {
32 static constexpr int32_t LENGTH = 8;
33
34 struct ImageSize {
35 int32_t width = 0;
36 int32_t height = 0;
37 float dstWidth = 0;
38 float dstHeight = 0;
39 const uint32_t color = 0;
40 uint32_t dst = 0;
41 };
42
43 class ImageFormatConvertFailTest : public testing::Test {
44 public:
ImageFormatConvertFailTest()45 ImageFormatConvertFailTest() {}
~ImageFormatConvertFailTest()46 ~ImageFormatConvertFailTest() {}
47 static ConvertFunction TestGetConvertFuncByFormat(PixelFormat srcFormat, PixelFormat destFormat);
48 };
49
TestGetConvertFuncByFormat(PixelFormat srcFormat,PixelFormat destFormat)50 ConvertFunction ImageFormatConvertFailTest::TestGetConvertFuncByFormat(PixelFormat srcFormat, PixelFormat destFormat)
51 {
52 return ImageFormatConvert::GetConvertFuncByFormat(srcFormat, destFormat);
53 }
54
55 /**
56 * @tc.name: GetConvertFuncByFormat_Test_DMA_ALLOC
57 * @tc.desc: test RGB_565 to YUV-Nv21 with DMA_ALLOC
58 * @tc.type: FUNC
59 */
60 HWTEST_F(ImageFormatConvertFailTest, GetConvertFuncByFormat_Test_DMA_ALLOC, TestSize.Level1)
61 {
62 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_DMA_ALLOC start";
63 uint8_t src[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
64 uint8_t dst[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
65 PixelFormat srcFormat = PixelFormat::RGB_565;
66 PixelFormat destFormat = PixelFormat::NV21;
67 ConvertFunction cvtFunc = ImageFormatConvertFailTest::TestGetConvertFuncByFormat(srcFormat, destFormat);
68
69 const_uint8_buffer_type srcBuffer = src;
70 RGBDataInfo rgbInfo = { 1, 1 };
71 rgbInfo.stride = 1;
72 ColorSpace colorspace = ColorSpace::UNKNOWN;
73 DestConvertInfo destInfo = { 1, 1 };
74 destInfo.allocType = AllocatorType::DMA_ALLOC;
75 destInfo.format = PixelFormat::NV21;
76 destInfo.buffer = dst;
77 destInfo.bufferSize = 1;
78 destInfo.yStride = 1;
79 destInfo.uvStride = 2;
80 destInfo.yOffset = 1;
81 destInfo.uvOffset = 1;
82
83 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), true);
84 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_DMA_ALLOC end";
85 }
86
87 /**
88 * @tc.name: YUVGetConvertFuncByFormat_DMA_ALLOC
89 * @tc.desc: test YUV-Nv21 to RGBA_8888 with DMA_ALLOC
90 * @tc.type: FUNC
91 */
92 HWTEST_F(ImageFormatConvertFailTest, YUVGetConvertFuncByFormat_DMA_ALLOC, TestSize.Level1)
93 {
94 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_DMA_ALLOC start";
95 uint8_t src[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
96 uint8_t dst[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
97 PixelFormat srcFormat = PixelFormat::NV21;
98 PixelFormat destFormat = PixelFormat::RGBA_8888;
99 YUVDataInfo yDInfo;
100
101 yDInfo.imageSize = {1, 1};
102 yDInfo.yWidth = 1;
103 yDInfo.yHeight = 1;
104 yDInfo.uvWidth = 2;
105 yDInfo.uvHeight = 1;
106 yDInfo.yStride = 1;
107 yDInfo.uStride = 2;
108 yDInfo.vStride = 2;
109 yDInfo.uvStride = 2;
110 yDInfo.yOffset = 0;
111 yDInfo.uOffset = 1;
112 yDInfo.vOffset = 1;
113 yDInfo.uvOffset = 1;
114
115 ColorSpace colorspace = ColorSpace::UNKNOWN;
116 DestConvertInfo destInfo = { 1, 1 };
117 destInfo.allocType = AllocatorType::DMA_ALLOC;
118 destInfo.format = PixelFormat::RGBA_8888;
119 destInfo.buffer = dst;
120 destInfo.bufferSize = LENGTH;
121 destInfo.yStride = 1;
122 destInfo.uvStride = 2;
123 destInfo.yOffset = 0;
124 destInfo.uvOffset = 1;
125
126 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
127 EXPECT_EQ(yuvCvtFunc(src, yDInfo, destInfo, colorspace), true);
128 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_DMA_ALLOC end";
129 }
130
131 /**
132 * @tc.name: GetConvertFuncByFormat_Test_Null
133 * @tc.desc: test BGRA_8888 to YUV-Nv21 null ptr
134 * @tc.type: FUNC
135 */
136 HWTEST_F(ImageFormatConvertFailTest, GetConvertFuncByFormat_Test_Null, TestSize.Level1)
137 {
138 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_Null start";
139 PixelFormat srcFormat = PixelFormat::BGRA_8888;
140 PixelFormat destFormat = PixelFormat::NV21;
141 const_uint8_buffer_type srcBuffer = nullptr;
142 RGBDataInfo rgbInfo = { 1, 1 };
143 ColorSpace colorspace = ColorSpace::UNKNOWN;
144 DestConvertInfo destInfo = { 1, 1 };
145
146 ConvertFunction cvtFunc = ImageFormatConvertFailTest::TestGetConvertFuncByFormat(srcFormat, destFormat);
147 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), false);
148 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_Null end";
149 }
150
151 /**
152 * @tc.name: YUVGetConvertFuncByFormat_Test_Null_8888
153 * @tc.desc: test YUV-Nv21 to RGBA_8888 null ptr
154 * @tc.type: FUNC
155 */
156 HWTEST_F(ImageFormatConvertFailTest, YUVGetConvertFuncByFormat_Test_Null_8888, TestSize.Level1)
157 {
158 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_Test_Null_8888 start";
159 PixelFormat srcFormat = PixelFormat::NV21;
160 PixelFormat destFormat = PixelFormat::RGBA_8888;
161 const_uint8_buffer_type srcBuffer = nullptr;
162 YUVDataInfo yDInfo;
163 ColorSpace colorspace = ColorSpace::UNKNOWN;
164 DestConvertInfo destInfo = { 1, 1 };
165
166 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
167 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
168 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_Test_Null_8888 end";
169 }
170
171 /**
172 * @tc.name: YUVGetConvertFuncByFormat_Test_Null_888
173 * @tc.desc: test YUV-Nv21 to RGB_888 null ptr
174 * @tc.type: FUNC
175 */
176 HWTEST_F(ImageFormatConvertFailTest, YUVGetConvertFuncByFormat_Test_Null_888, TestSize.Level1)
177 {
178 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_Test_Null_888 start";
179 PixelFormat srcFormat = PixelFormat::NV21;
180 PixelFormat destFormat = PixelFormat::RGB_888;
181 const_uint8_buffer_type srcBuffer = nullptr;
182 YUVDataInfo yDInfo;
183 ColorSpace colorspace = ColorSpace::UNKNOWN;
184 DestConvertInfo destInfo = { 1, 1 };
185
186 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
187 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
188 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: YUVGetConvertFuncByFormat_Test_Null_888 end";
189 }
190
191 /**
192 * @tc.name: GetConvertFuncByFormat_Test_InvalidSize
193 * @tc.desc: test RGB_565 to YUV-Nv21 with invalid width
194 * @tc.type: FUNC
195 */
196 HWTEST_F(ImageFormatConvertFailTest, GetConvertFuncByFormat_Test_InvalidSize, TestSize.Level1)
197 {
198 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_DMA_ALLOC start";
199 uint8_t src[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
200 uint8_t dst[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
201 PixelFormat srcFormat = PixelFormat::RGB_565;
202 PixelFormat destFormat = PixelFormat::NV21;
203 ConvertFunction cvtFunc = ImageFormatConvertFailTest::TestGetConvertFuncByFormat(srcFormat, destFormat);
204
205 const_uint8_buffer_type srcBuffer = src;
206 RGBDataInfo rgbInfo = { PIXEL_MAP_MAX_RAM_SIZE + 1, 1 };
207 rgbInfo.stride = PIXEL_MAP_MAX_RAM_SIZE + 1;
208 ColorSpace colorspace = ColorSpace::UNKNOWN;
209 DestConvertInfo destInfo = { PIXEL_MAP_MAX_RAM_SIZE + 1, 1 };
210 destInfo.allocType = AllocatorType::DMA_ALLOC;
211 destInfo.format = PixelFormat::NV21;
212 destInfo.buffer = dst;
213 destInfo.bufferSize = 1;
214 destInfo.yStride = PIXEL_MAP_MAX_RAM_SIZE + 1;
215 destInfo.uvStride = 2;
216 destInfo.yOffset = 1;
217 destInfo.uvOffset = 1;
218
219 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), false);
220 GTEST_LOG_(INFO) << "ImageFormatConvertFailTest: GetConvertFuncByFormat_Test_DMA_ALLOC end";
221 }
222
223 /**
224 * @tc.name: YUVP010ConvertToRGB
225 * @tc.desc: Test YUVP010ConvertToRGB with invalid info.
226 * @tc.type: FUNC
227 */
228 HWTEST_F(ImageFormatConvertFailTest, YUVP010ConvertToRGB, TestSize.Level1)
229 {
230 PixelFormat srcFormat = PixelFormat::YCBCR_P010;
231 PixelFormat destFormat = PixelFormat::RGB_888;
232 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
233 YUVDataInfo yDInfo;
234 ColorSpace colorspace = ColorSpace::UNKNOWN;
235 DestConvertInfo destInfo = { 0, 0 };
236
237 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
238 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
239 }
240
241 /**
242 * @tc.name: RGBConvertToYUVP010
243 * @tc.desc: Test RGBConvertToYUVP010 with invalid info.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(ImageFormatConvertFailTest, RGBConvertToYUVP010, TestSize.Level1)
247 {
248 PixelFormat srcFormat = PixelFormat::RGB_888;
249 PixelFormat destFormat = PixelFormat::YCBCR_P010;
250 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
251 RGBDataInfo rgbInfo;
252 ColorSpace colorspace = ColorSpace::UNKNOWN;
253 DestConvertInfo destInfo = { 0, 0 };
254
255 ConvertFunction cvtFunc = ImageFormatConvert::GetConvertFuncByFormat(srcFormat, destFormat);
256 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), false);
257 }
258
259 /**
260 * @tc.name: YUVConvertToYUVP010
261 * @tc.desc: Test YUVConvertToYUVP010 with invalid info.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(ImageFormatConvertFailTest, YUVConvertToYUVP010, TestSize.Level1)
265 {
266 PixelFormat srcFormat = PixelFormat::NV12;
267 PixelFormat destFormat = PixelFormat::YCBCR_P010;
268 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
269 YUVDataInfo yDInfo;
270 ColorSpace colorspace = ColorSpace::UNKNOWN;
271 DestConvertInfo destInfo = { 0, 0 };
272
273 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
274 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
275 }
276
277 /**
278 * @tc.name: YUVP010ConvertToYUV
279 * @tc.desc: Test YUVP010ConvertToYUV with invalid info.
280 * @tc.type: FUNC
281 */
282 HWTEST_F(ImageFormatConvertFailTest, YUVP010ConvertToYUV, TestSize.Level1)
283 {
284 PixelFormat srcFormat = PixelFormat::YCBCR_P010;
285 PixelFormat destFormat = PixelFormat::NV12;
286 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
287 YUVDataInfo yDInfo;
288 ColorSpace colorspace = ColorSpace::UNKNOWN;
289 DestConvertInfo destInfo = { 0, 0 };
290
291 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
292 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
293 }
294
295 /**
296 * @tc.name: YUVConvertToRGB1010102
297 * @tc.desc: Test YUVConvertToRGB1010102 with invalid info.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(ImageFormatConvertFailTest, YUVConvertToRGB1010102, TestSize.Level1)
301 {
302 PixelFormat srcFormat = PixelFormat::NV12;
303 PixelFormat destFormat = PixelFormat::RGBA_1010102;
304 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
305 YUVDataInfo yDInfo;
306 ColorSpace colorspace = ColorSpace::UNKNOWN;
307 DestConvertInfo destInfo = { 0, 0 };
308
309 YUVConvertFunction yuvCvtFunc = ImageFormatConvert::YUVGetConvertFuncByFormat(srcFormat, destFormat);
310 EXPECT_EQ(yuvCvtFunc(srcBuffer, yDInfo, destInfo, colorspace), false);
311 }
312
313 /**
314 * @tc.name: RGB1010102ConvertToYUV
315 * @tc.desc: Test RGB1010102ConvertToYUV with invalid info.
316 * @tc.type: FUNC
317 */
318 HWTEST_F(ImageFormatConvertFailTest, RGB1010102ConvertToYUV, TestSize.Level1)
319 {
320 PixelFormat srcFormat = PixelFormat::RGBA_1010102;
321 PixelFormat destFormat = PixelFormat::NV12;
322 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
323 RGBDataInfo rgbInfo;
324 ColorSpace colorspace = ColorSpace::UNKNOWN;
325 DestConvertInfo destInfo = { 0, 0 };
326
327 ConvertFunction cvtFunc = ImageFormatConvert::GetConvertFuncByFormat(srcFormat, destFormat);
328 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), false);
329 }
330
331 /**
332 * @tc.name: RGB1010102ConvertToYUVP010
333 * @tc.desc: Test RGB1010102ConvertToYUVP010 with invalid info.
334 * @tc.type: FUNC
335 */
336 HWTEST_F(ImageFormatConvertFailTest, RGB1010102ConvertToYUVP010, TestSize.Level1)
337 {
338 PixelFormat srcFormat = PixelFormat::RGBA_1010102;
339 PixelFormat destFormat = PixelFormat::YCBCR_P010;
340 uint8_t srcBuffer[LENGTH] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
341 RGBDataInfo rgbInfo;
342 ColorSpace colorspace = ColorSpace::UNKNOWN;
343 DestConvertInfo destInfo = { 0, 0 };
344
345 ConvertFunction cvtFunc = ImageFormatConvert::GetConvertFuncByFormat(srcFormat, destFormat);
346 EXPECT_EQ(cvtFunc(srcBuffer, rgbInfo, destInfo, colorspace), false);
347 }
348 } // namespace Media
349 } // namespace OHOS