blob: f3a3ab643e441fc567b587069c4d3d212dea8588 [file] [log] [blame]
Wind Yuan20f67cb2015-03-05 16:15:21 +08001/*
2 * test_cl_image.cpp - test cl image
3 *
4 * Copyright (c) 2014-2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21#include "test_common.h"
22#include "cl_device.h"
23#include "cl_context.h"
24#include "cl_demo_handler.h"
wangfei11987462015-03-12 15:02:57 +080025#include "cl_hdr_handler.h"
ShincyTu5710f972015-03-23 23:20:44 +080026#include "cl_blc_handler.h"
Wind Yuan20f67cb2015-03-05 16:15:21 +080027#include "drm_bo_buffer.h"
Wind Yuan5c96d9f2015-03-18 15:32:37 +080028#include "cl_demosaic_handler.h"
wangfei6ea22212015-03-23 19:10:26 +080029#include "cl_csc_handler.h"
wangfei98428352015-04-02 10:41:27 +080030#include "cl_wb_handler.h"
Juan Zhaoab5e6fe2015-04-08 14:12:46 +080031#include "cl_denoise_handler.h"
wangfei7e0436e2015-04-15 19:22:36 +080032#include "cl_gamma_handler.h"
Wind Yuan20f67cb2015-03-05 16:15:21 +080033
34using namespace XCam;
35
36enum TestHandlerType {
37 TestHandlerUnknown = 0,
38 TestHandlerDemo,
39 TestHandlerBlackLevel,
40 TestHandlerDefect,
Wind Yuan5c96d9f2015-03-18 15:32:37 +080041 TestHandlerDemosaic,
Wind Yuan20f67cb2015-03-05 16:15:21 +080042 TestHandlerColorConversion,
43 TestHandlerHDR,
wangfei98428352015-04-02 10:41:27 +080044 TestHandlerWhiteBalance,
Juan Zhaoab5e6fe2015-04-08 14:12:46 +080045 TestHandlerDenoise,
wangfei7e0436e2015-04-15 19:22:36 +080046 TestHandlerGamma,
Wind Yuan20f67cb2015-03-05 16:15:21 +080047};
48
49struct TestFileHandle {
50 FILE *fp;
51 TestFileHandle ()
52 : fp (NULL)
53 {}
54 ~TestFileHandle ()
55 {
56 if (fp)
57 fclose (fp);
58 }
59};
60
61static XCamReturn
62read_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file)
63{
64 const VideoBufferInfo info = buf->get_video_info ();
65 uint8_t *memory = NULL;
66 XCamReturn ret = XCAM_RETURN_NO_ERROR;
67
68 memory = buf->map ();
69 if (fread (memory, 1, info.size, file.fp) != info.size) {
70 if (feof (file.fp))
71 ret = XCAM_RETURN_BYPASS;
72 else {
73 XCAM_LOG_ERROR ("read file failed, size doesn't match");
74 ret = XCAM_RETURN_ERROR_FILE;
75 }
76 }
77 buf->unmap ();
78 return ret;
79}
80
81static XCamReturn
82write_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file)
83{
84 const VideoBufferInfo info = buf->get_video_info ();
85 uint8_t *memory = NULL;
86 XCamReturn ret = XCAM_RETURN_NO_ERROR;
87
88 memory = buf->map ();
89 if (fwrite (memory, 1, info.size, file.fp) != info.size) {
90 XCAM_LOG_ERROR ("read file failed, size doesn't match");
91 ret = XCAM_RETURN_ERROR_FILE;
92 }
93 buf->unmap ();
94 return ret;
95}
96
wangfeicf536692015-03-19 16:44:50 +080097static XCamReturn
98kernel_loop(SmartPtr<CLImageHandler> &image_handler, SmartPtr<DrmBoBuffer> &input_buf, SmartPtr<DrmBoBuffer> &output_buf, uint32_t kernel_loop_count)
99{
wangfeicf536692015-03-19 16:44:50 +0800100 XCamReturn ret = XCAM_RETURN_NO_ERROR;
wangfei125221b2015-04-08 13:49:20 +0800101 for (uint32_t i = 0; i < kernel_loop_count; i++) {
wangfeicf536692015-03-19 16:44:50 +0800102 PROFILING_START(cl_kernel);
103 ret = image_handler->execute (input_buf, output_buf);
104 PROFILING_END(cl_kernel, kernel_loop_count)
105 }
106 return ret;
107}
108
Wind Yuan20f67cb2015-03-05 16:15:21 +0800109static void
110print_help (const char *bin_name)
111{
112 printf ("Usage: %s [-f format] -i input -o output\n"
113 "\t -t type specify image handler type\n"
wangfei7e0436e2015-04-15 19:22:36 +0800114 "\t select from [demo, blacklevel, defect, demosaic, csc, hdr, wb, denoise, gamma]\n"
wangfei227ba272015-04-16 20:58:45 +0800115 "\t -f input_format specify a input format\n"
116 "\t -g output_format specify a output format\n"
Wind Yuan0a1c73e2015-03-26 08:50:06 +0800117 "\t select from [NV12, BA10, RGBA]\n"
Wind Yuan20f67cb2015-03-05 16:15:21 +0800118 "\t -i input specify input file path\n"
119 "\t -o output specify output file path\n"
wangfeicf536692015-03-19 16:44:50 +0800120 "\t -p count specify cl kernel loop count\n"
wangfei125221b2015-04-08 13:49:20 +0800121 "\t -c csc_type specify csc type, default:rgba2nv12\n"
wangfei227ba272015-04-16 20:58:45 +0800122 "\t select from [rgbatonv12, rgbatolab, rgba64torgba]\n"
wangfei125221b2015-04-08 13:49:20 +0800123 "\t -d hdr_type specify hdr type, default:rgb\n"
124 "\t select from [rgb, lab]\n"
Wind Yuan20f67cb2015-03-05 16:15:21 +0800125 "\t -h help\n"
126 , bin_name);
127}
128
129int main (int argc, char *argv[])
130{
wangfei227ba272015-04-16 20:58:45 +0800131 uint32_t input_format = 0;
132 uint32_t output_format = V4L2_PIX_FMT_RGBA32;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800133 uint32_t buf_count = 0;
wangfeicf536692015-03-19 16:44:50 +0800134 uint32_t kernel_loop_count = 0;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800135 const char *input_file = NULL, *output_file = NULL;
136 TestFileHandle input_fp, output_fp;
137 const char *bin_name = argv[0];
138 TestHandlerType handler_type = TestHandlerUnknown;
139 XCamReturn ret = XCAM_RETURN_NO_ERROR;
140 SmartPtr<CLImageHandler> image_handler;
141 VideoBufferInfo input_buf_info;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800142 SmartPtr<CLContext> context;
143 SmartPtr<DrmDisplay> display;
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800144 SmartPtr<BufferPool> buf_pool;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800145 int opt = 0;
wangfei125221b2015-04-08 13:49:20 +0800146 CLCscType csc_type = CL_CSC_TYPE_RGBATONV12;
147 CLHdrType hdr_type = CL_HDR_TYPE_RGB;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800148
wangfei227ba272015-04-16 20:58:45 +0800149 while ((opt = getopt(argc, argv, "f:i:o:t:p:c:d:g:h")) != -1) {
Wind Yuan20f67cb2015-03-05 16:15:21 +0800150 switch (opt) {
151 case 'i':
152 input_file = optarg;
153 break;
154 case 'o':
155 output_file = optarg;
156 break;
157
158 case 'f': {
159 if (!strcasecmp (optarg, "nv12"))
wangfei227ba272015-04-16 20:58:45 +0800160 input_format = V4L2_PIX_FMT_NV12;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800161 else if (!strcasecmp (optarg, "ba10"))
wangfei227ba272015-04-16 20:58:45 +0800162 input_format = V4L2_PIX_FMT_SGRBG10;
wangfei6ea22212015-03-23 19:10:26 +0800163 else if (! strcasecmp (optarg, "rgba"))
wangfei227ba272015-04-16 20:58:45 +0800164 input_format = V4L2_PIX_FMT_RGBA32;
165 else if (! strcasecmp (optarg, "rgba64"))
166 input_format = XCAM_PIX_FMT_RGBA64;
167
168 else
169 print_help (bin_name);
170 break;
171 }
172 case 'g': {
173 if (!strcasecmp (optarg, "nv12"))
174 output_format = V4L2_PIX_FMT_NV12;
175 else if (!strcasecmp (optarg, "ba10"))
176 output_format = V4L2_PIX_FMT_SGRBG10;
177 else if (! strcasecmp (optarg, "rgba"))
178 output_format = V4L2_PIX_FMT_RGBA32;
179 else if (! strcasecmp (optarg, "rgba64"))
180 output_format = XCAM_PIX_FMT_RGBA64;
wangfei125221b2015-04-08 13:49:20 +0800181
Wind Yuan20f67cb2015-03-05 16:15:21 +0800182 else
183 print_help (bin_name);
184 break;
185 }
186 case 't': {
187 if (!strcasecmp (optarg, "demo"))
188 handler_type = TestHandlerDemo;
189 else if (!strcasecmp (optarg, "blacklevel"))
190 handler_type = TestHandlerBlackLevel;
191 else if (!strcasecmp (optarg, "defect"))
192 handler_type = TestHandlerDefect;
Wind Yuan5c96d9f2015-03-18 15:32:37 +0800193 else if (!strcasecmp (optarg, "demosaic"))
194 handler_type = TestHandlerDemosaic;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800195 else if (!strcasecmp (optarg, "csc"))
196 handler_type = TestHandlerColorConversion;
197 else if (!strcasecmp (optarg, "hdr"))
198 handler_type = TestHandlerHDR;
wangfei98428352015-04-02 10:41:27 +0800199 else if (!strcasecmp (optarg, "wb"))
200 handler_type = TestHandlerWhiteBalance;
Juan Zhaoab5e6fe2015-04-08 14:12:46 +0800201 else if (!strcasecmp (optarg, "denoise"))
202 handler_type = TestHandlerDenoise;
wangfei7e0436e2015-04-15 19:22:36 +0800203 else if (!strcasecmp (optarg, "gamma"))
204 handler_type = TestHandlerGamma;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800205 else
206 print_help (bin_name);
207 break;
208 }
wangfeicf536692015-03-19 16:44:50 +0800209 case 'p':
210 kernel_loop_count = atoi (optarg);
211 break;
wangfei1a7d9f32015-03-26 18:30:00 +0800212 case 'c':
wangfei227ba272015-04-16 20:58:45 +0800213 if (!strcasecmp (optarg, "rgbatonv12"))
wangfei1a7d9f32015-03-26 18:30:00 +0800214 csc_type = CL_CSC_TYPE_RGBATONV12;
wangfei227ba272015-04-16 20:58:45 +0800215 else if (!strcasecmp (optarg, "rgbatolab"))
wangfei1a7d9f32015-03-26 18:30:00 +0800216 csc_type = CL_CSC_TYPE_RGBATOLAB;
wangfei227ba272015-04-16 20:58:45 +0800217 else if (!strcasecmp (optarg, "rgba64torgba"))
218 csc_type = CL_CSC_TYPE_RGBA64TORGBA;
wangfei1a7d9f32015-03-26 18:30:00 +0800219 else
220 print_help (bin_name);
221 break;
wangfei125221b2015-04-08 13:49:20 +0800222 case 'd':
223 if (!strcasecmp (optarg, "rgb"))
224 hdr_type = CL_HDR_TYPE_RGB;
225 else if (!strcasecmp (optarg, "lab"))
226 hdr_type = CL_HDR_TYPE_LAB;
227 else
228 print_help (bin_name);
229 break;
230
Wind Yuan20f67cb2015-03-05 16:15:21 +0800231 case 'h':
232 print_help (bin_name);
233 return 0;
234
235 default:
236 print_help (bin_name);
237 return -1;
238 }
239 }
240
wangfei227ba272015-04-16 20:58:45 +0800241 if (!input_format || !input_file || !output_file || handler_type == TestHandlerUnknown) {
Wind Yuan20f67cb2015-03-05 16:15:21 +0800242 print_help (bin_name);
243 return -1;
244 }
245
246 input_fp.fp = fopen (input_file, "rb");
247 output_fp.fp = fopen (output_file, "wb");
248 if (!input_fp.fp || !output_fp.fp) {
249 XCAM_LOG_ERROR ("open input/output file failed");
250 return -1;
251 }
252
253 context = CLDevice::instance ()->get_context ();
254
255 switch (handler_type) {
256 case TestHandlerDemo:
257 image_handler = create_cl_demo_image_handler (context);
258 break;
259 case TestHandlerBlackLevel:
ShincyTu5710f972015-03-23 23:20:44 +0800260 image_handler = create_cl_blc_image_handler (context);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800261 break;
262 case TestHandlerDefect:
263 break;
Wind Yuan5c96d9f2015-03-18 15:32:37 +0800264 case TestHandlerDemosaic: {
265 SmartPtr<CLBayer2RGBImageHandler> ba2rgb_handler;
266 image_handler = create_cl_demosaic_image_handler (context);
267 ba2rgb_handler = image_handler.dynamic_cast_ptr<CLBayer2RGBImageHandler> ();
268 XCAM_ASSERT (ba2rgb_handler.ptr ());
wangfei227ba272015-04-16 20:58:45 +0800269 ba2rgb_handler->set_output_format (output_format);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800270 break;
Wind Yuan5c96d9f2015-03-18 15:32:37 +0800271 }
wangfei6ea22212015-03-23 19:10:26 +0800272 case TestHandlerColorConversion: {
wangfei1a7d9f32015-03-26 18:30:00 +0800273 image_handler = create_cl_csc_image_handler (context, csc_type);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800274 break;
wangfei6ea22212015-03-23 19:10:26 +0800275 }
Wind Yuan20f67cb2015-03-05 16:15:21 +0800276 case TestHandlerHDR:
wangfei125221b2015-04-08 13:49:20 +0800277 image_handler = create_cl_hdr_image_handler (context, hdr_type);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800278 break;
Juan Zhaoab5e6fe2015-04-08 14:12:46 +0800279 case TestHandlerDenoise:
280 image_handler = create_cl_denoise_image_handler (context);
281 break;
wangfei98428352015-04-02 10:41:27 +0800282 case TestHandlerWhiteBalance: {
283 XCam3aResultWhiteBalance wb;
284 wb.r_gain = 1.0;
285 wb.gr_gain = 1.0;
286 wb.gb_gain = 1.0;
287 wb.b_gain = 1.0;
288 SmartPtr<CLWbImageHandler> wb_handler;
289 image_handler = create_cl_wb_image_handler (context);
290 wb_handler = image_handler.dynamic_cast_ptr<CLWbImageHandler> ();
291 XCAM_ASSERT (wb_handler.ptr ());
292 wb_handler->set_wb_config (wb);
293 break;
294 }
wangfei7e0436e2015-04-15 19:22:36 +0800295 case TestHandlerGamma: {
296 XCam3aResultGammaTable gamma_table;
297 for(int i = 0; i < XCAM_GAMMA_TABLE_SIZE; ++i)
298 gamma_table.table[i] = (double)(pow(i / 255.0, 1 / 2.2) * 255.0);
299 SmartPtr<CLGammaImageHandler> gamma_handler;
300 image_handler = create_cl_gamma_image_handler (context);
301 gamma_handler = image_handler.dynamic_cast_ptr<CLGammaImageHandler> ();
302 XCAM_ASSERT (gamma_handler.ptr ());
303 gamma_handler->set_gamma_table (gamma_table);
304 break;
305 }
306
Wind Yuan20f67cb2015-03-05 16:15:21 +0800307 default:
308 XCAM_LOG_ERROR ("unsupported image handler type:%d", handler_type);
309 return -1;
310 }
311 if (!image_handler.ptr ()) {
312 XCAM_LOG_ERROR ("create image_handler failed");
313 return -1;
314 }
Wind Yuan20f67cb2015-03-05 16:15:21 +0800315
wangfei227ba272015-04-16 20:58:45 +0800316 input_buf_info.init (input_format, 1920, 1080);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800317 display = DrmDisplay::instance ();
318 buf_pool = new DrmBoBufferPool (display);
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800319 buf_pool->set_video_info (input_buf_info);
320 if (!buf_pool->reserve (6)) {
Wind Yuan20f67cb2015-03-05 16:15:21 +0800321 XCAM_LOG_ERROR ("init buffer pool failed");
322 return -1;
323 }
324
325 while (!feof (input_fp.fp)) {
Wind Yuan4bbe4ec2015-04-02 18:00:35 +0800326 SmartPtr<DrmBoBuffer> input_buf, output_buf;
327 SmartPtr<BufferProxy> tmp_buf = buf_pool->get_buffer (buf_pool);
328 input_buf = tmp_buf.dynamic_cast_ptr<DrmBoBuffer> ();
329
Wind Yuan20f67cb2015-03-05 16:15:21 +0800330 XCAM_ASSERT (input_buf.ptr ());
331 ret = read_buf (input_buf, input_fp);
332 if (ret == XCAM_RETURN_BYPASS)
333 break;
334 CHECK (ret, "read buffer from %s failed", input_file);
wangfeicf536692015-03-19 16:44:50 +0800335
336 if (kernel_loop_count != 0)
337 {
338 kernel_loop (image_handler, input_buf, output_buf, kernel_loop_count);
339 CHECK (ret, "execute kernels failed");
340 return 0;
341 }
342
Wind Yuan20f67cb2015-03-05 16:15:21 +0800343 ret = image_handler->execute (input_buf, output_buf);
344 CHECK (ret, "execute kernels failed");
345 XCAM_ASSERT (output_buf.ptr ());
346
347 ret = write_buf (output_buf, output_fp);
348 CHECK (ret, "read buffer from %s failed", output_file);
349
350 ++buf_count;
351 }
352 XCAM_LOG_INFO ("processed %d buffers successfully", buf_count);
353 return 0;
354}