test-cl-image.cpp revision 1c90c3e61f731e076d8cdd3d3e6825f93457a60d
1/* 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" 25#include "cl_hdr_handler.h" 26#include "cl_blc_handler.h" 27#include "drm_bo_buffer.h" 28#include "cl_demosaic_handler.h" 29#include "cl_csc_handler.h" 30#include "cl_wb_handler.h" 31#include "cl_denoise_handler.h" 32#include "cl_gamma_handler.h" 33#include "cl_snr_handler.h" 34#include "cl_macc_handler.h" 35#include "cl_ee_handler.h" 36#include "cl_dpc_handler.h" 37#include "cl_bnr_handler.h" 38#include "cl_bayer_pipe_handler.h" 39 40using namespace XCam; 41 42enum TestHandlerType { 43 TestHandlerUnknown = 0, 44 TestHandlerDemo, 45 TestHandlerBlackLevel, 46 TestHandlerDefect, 47 TestHandlerDemosaic, 48 TestHandlerColorConversion, 49 TestHandlerHDR, 50 TestHandlerWhiteBalance, 51 TestHandlerDenoise, 52 TestHandlerGamma, 53 TestHandlerSimpleNoiseReduction, 54 TestHandlerBayerNoiseReduction, 55 TestHandlerMacc, 56 TestHandlerEe, 57 TestHandlerBayerPipe, 58}; 59 60struct TestFileHandle { 61 FILE *fp; 62 TestFileHandle () 63 : fp (NULL) 64 {} 65 ~TestFileHandle () 66 { 67 if (fp) 68 fclose (fp); 69 } 70}; 71 72static XCamReturn 73read_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file) 74{ 75 const VideoBufferInfo info = buf->get_video_info (); 76 uint8_t *memory = NULL; 77 XCamReturn ret = XCAM_RETURN_NO_ERROR; 78 79 memory = buf->map (); 80 if (fread (memory, 1, info.size, file.fp) != info.size) { 81 if (feof (file.fp)) 82 ret = XCAM_RETURN_BYPASS; 83 else { 84 XCAM_LOG_ERROR ("read file failed, size doesn't match"); 85 ret = XCAM_RETURN_ERROR_FILE; 86 } 87 } 88 buf->unmap (); 89 return ret; 90} 91 92static XCamReturn 93write_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file) 94{ 95 const VideoBufferInfo info = buf->get_video_info (); 96 uint8_t *memory = NULL; 97 XCamReturn ret = XCAM_RETURN_NO_ERROR; 98 99 memory = buf->map (); 100 if (fwrite (memory, 1, info.size, file.fp) != info.size) { 101 XCAM_LOG_ERROR ("read file failed, size doesn't match"); 102 ret = XCAM_RETURN_ERROR_FILE; 103 } 104 buf->unmap (); 105 return ret; 106} 107 108static XCamReturn 109kernel_loop(SmartPtr<CLImageHandler> &image_handler, SmartPtr<DrmBoBuffer> &input_buf, SmartPtr<DrmBoBuffer> &output_buf, uint32_t kernel_loop_count) 110{ 111 XCamReturn ret = XCAM_RETURN_NO_ERROR; 112 for (uint32_t i = 0; i < kernel_loop_count; i++) { 113 PROFILING_START(cl_kernel); 114 ret = image_handler->execute (input_buf, output_buf); 115 PROFILING_END(cl_kernel, kernel_loop_count) 116 } 117 return ret; 118} 119 120static void 121print_help (const char *bin_name) 122{ 123 printf ("Usage: %s [-f format] -i input -o output\n" 124 "\t -t type specify image handler type\n" 125 "\t select from [demo, blacklevel, defect, demosaic, csc, hdr, wb, denoise, gamma, snr, bnr, macc, ee, bayerpipe]\n" 126 "\t -f input_format specify a input format\n" 127 "\t -g output_format specify a output format\n" 128 "\t select from [NV12, BA10, RGBA]\n" 129 "\t -i input specify input file path\n" 130 "\t -o output specify output file path\n" 131 "\t -p count specify cl kernel loop count\n" 132 "\t -c csc_type specify csc type, default:rgba2nv12\n" 133 "\t select from [rgbatonv12, rgbatolab, rgba64torgba, yuyvtorgba, nv12torgba]\n" 134 "\t -d hdr_type specify hdr type, default:rgb\n" 135 "\t select from [rgb, lab]\n" 136 "\t -h help\n" 137 , bin_name); 138} 139 140int main (int argc, char *argv[]) 141{ 142 uint32_t input_format = 0; 143 uint32_t output_format = V4L2_PIX_FMT_RGBA32; 144 uint32_t buf_count = 0; 145 uint32_t kernel_loop_count = 0; 146 const char *input_file = NULL, *output_file = NULL; 147 TestFileHandle input_fp, output_fp; 148 const char *bin_name = argv[0]; 149 TestHandlerType handler_type = TestHandlerUnknown; 150 XCamReturn ret = XCAM_RETURN_NO_ERROR; 151 SmartPtr<CLImageHandler> image_handler; 152 VideoBufferInfo input_buf_info; 153 SmartPtr<CLContext> context; 154 SmartPtr<DrmDisplay> display; 155 SmartPtr<BufferPool> buf_pool; 156 int opt = 0; 157 CLCscType csc_type = CL_CSC_TYPE_RGBATONV12; 158 CLHdrType hdr_type = CL_HDR_TYPE_RGB; 159 160 while ((opt = getopt(argc, argv, "f:i:o:t:p:c:d:g:h")) != -1) { 161 switch (opt) { 162 case 'i': 163 input_file = optarg; 164 break; 165 case 'o': 166 output_file = optarg; 167 break; 168 169 case 'f': { 170 if (!strcasecmp (optarg, "nv12")) 171 input_format = V4L2_PIX_FMT_NV12; 172 else if (!strcasecmp (optarg, "ba10")) 173 input_format = V4L2_PIX_FMT_SGRBG10; 174 else if (! strcasecmp (optarg, "rgba")) 175 input_format = V4L2_PIX_FMT_RGBA32; 176 else if (! strcasecmp (optarg, "rgba64")) 177 input_format = XCAM_PIX_FMT_RGBA64; 178 else if (!strcasecmp (optarg, "rg12")) 179 input_format = V4L2_PIX_FMT_SRGGB12; 180 else 181 print_help (bin_name); 182 break; 183 } 184 case 'g': { 185 if (!strcasecmp (optarg, "nv12")) 186 output_format = V4L2_PIX_FMT_NV12; 187 else if (!strcasecmp (optarg, "ba10")) 188 output_format = V4L2_PIX_FMT_SGRBG10; 189 else if (! strcasecmp (optarg, "rgba")) 190 output_format = V4L2_PIX_FMT_RGBA32; 191 else if (! strcasecmp (optarg, "rgba64")) 192 output_format = XCAM_PIX_FMT_RGBA64; 193 194 else 195 print_help (bin_name); 196 break; 197 } 198 case 't': { 199 if (!strcasecmp (optarg, "demo")) 200 handler_type = TestHandlerDemo; 201 else if (!strcasecmp (optarg, "blacklevel")) 202 handler_type = TestHandlerBlackLevel; 203 else if (!strcasecmp (optarg, "defect")) 204 handler_type = TestHandlerDefect; 205 else if (!strcasecmp (optarg, "demosaic")) 206 handler_type = TestHandlerDemosaic; 207 else if (!strcasecmp (optarg, "csc")) 208 handler_type = TestHandlerColorConversion; 209 else if (!strcasecmp (optarg, "hdr")) 210 handler_type = TestHandlerHDR; 211 else if (!strcasecmp (optarg, "wb")) 212 handler_type = TestHandlerWhiteBalance; 213 else if (!strcasecmp (optarg, "denoise")) 214 handler_type = TestHandlerDenoise; 215 else if (!strcasecmp (optarg, "gamma")) 216 handler_type = TestHandlerGamma; 217 else if (!strcasecmp (optarg, "snr")) 218 handler_type = TestHandlerSimpleNoiseReduction; 219 else if (!strcasecmp (optarg, "bnr")) 220 handler_type = TestHandlerBayerNoiseReduction; 221 else if (!strcasecmp (optarg, "macc")) 222 handler_type = TestHandlerMacc; 223 else if (!strcasecmp (optarg, "ee")) 224 handler_type = TestHandlerEe; 225 else if (!strcasecmp (optarg, "bayerpipe")) 226 handler_type = TestHandlerBayerPipe; 227 else 228 print_help (bin_name); 229 break; 230 } 231 case 'p': 232 kernel_loop_count = atoi (optarg); 233 break; 234 case 'c': 235 if (!strcasecmp (optarg, "rgbatonv12")) 236 csc_type = CL_CSC_TYPE_RGBATONV12; 237 else if (!strcasecmp (optarg, "rgbatolab")) 238 csc_type = CL_CSC_TYPE_RGBATOLAB; 239 else if (!strcasecmp (optarg, "rgba64torgba")) 240 csc_type = CL_CSC_TYPE_RGBA64TORGBA; 241 else if (!strcasecmp (optarg, "yuyvtorgba")) 242 csc_type = CL_CSC_TYPE_YUYVTORGBA; 243 else if (!strcasecmp (optarg, "nv12torgba")) 244 csc_type = CL_CSC_TYPE_NV12TORGBA; 245 else 246 print_help (bin_name); 247 break; 248 case 'd': 249 if (!strcasecmp (optarg, "rgb")) 250 hdr_type = CL_HDR_TYPE_RGB; 251 else if (!strcasecmp (optarg, "lab")) 252 hdr_type = CL_HDR_TYPE_LAB; 253 else 254 print_help (bin_name); 255 break; 256 257 case 'h': 258 print_help (bin_name); 259 return 0; 260 261 default: 262 print_help (bin_name); 263 return -1; 264 } 265 } 266 267 if (!input_format || !input_file || !output_file || handler_type == TestHandlerUnknown) { 268 print_help (bin_name); 269 return -1; 270 } 271 272 input_fp.fp = fopen (input_file, "rb"); 273 output_fp.fp = fopen (output_file, "wb"); 274 if (!input_fp.fp || !output_fp.fp) { 275 XCAM_LOG_ERROR ("open input/output file failed"); 276 return -1; 277 } 278 279 context = CLDevice::instance ()->get_context (); 280 281 switch (handler_type) { 282 case TestHandlerDemo: 283 image_handler = create_cl_demo_image_handler (context); 284 break; 285 case TestHandlerBlackLevel: { 286 XCam3aResultBlackLevel blc; 287 blc.r_level = 0.05; 288 blc.gr_level = 0.05; 289 blc.gb_level = 0.05; 290 blc.b_level = 0.05; 291 image_handler = create_cl_blc_image_handler (context); 292 SmartPtr<CLBlcImageHandler> blc_handler; 293 blc_handler = image_handler.dynamic_cast_ptr<CLBlcImageHandler> (); 294 XCAM_ASSERT (blc_handler.ptr ()); 295 blc_handler->set_blc_config (blc); 296 break; 297 } 298 case TestHandlerDefect: { 299 XCam3aResultDefectPixel dpc; 300 dpc.r_threshold = 0.125; 301 dpc.gr_threshold = 0.125; 302 dpc.gb_threshold = 0.125; 303 dpc.b_threshold = 0.125; 304 image_handler = create_cl_dpc_image_handler (context); 305 SmartPtr<CLDpcImageHandler> dpc_handler; 306 dpc_handler = image_handler.dynamic_cast_ptr<CLDpcImageHandler> (); 307 XCAM_ASSERT (dpc_handler.ptr ()); 308 dpc_handler->set_dpc_config (dpc); 309 break; 310 } 311 break; 312 case TestHandlerDemosaic: { 313 SmartPtr<CLBayer2RGBImageHandler> ba2rgb_handler; 314 image_handler = create_cl_demosaic_image_handler (context); 315 ba2rgb_handler = image_handler.dynamic_cast_ptr<CLBayer2RGBImageHandler> (); 316 XCAM_ASSERT (ba2rgb_handler.ptr ()); 317 ba2rgb_handler->set_output_format (output_format); 318 break; 319 } 320 case TestHandlerColorConversion: { 321 SmartPtr<CLCscImageHandler> csc_handler; 322 XCam3aResultColorMatrix color_matrix; 323 double matrix_table[XCAM_COLOR_MATRIX_SIZE] = {0.299, 0.587, 0.114, -0.14713, -0.28886, 0.436, 0.615, -0.51499, -0.10001}; 324 memcpy (color_matrix.matrix, matrix_table, sizeof(double)*XCAM_COLOR_MATRIX_SIZE); 325 image_handler = create_cl_csc_image_handler (context, csc_type); 326 csc_handler = image_handler.dynamic_cast_ptr<CLCscImageHandler> (); 327 XCAM_ASSERT (csc_handler.ptr ()); 328 csc_handler->set_rgbtoyuv_matrix(color_matrix); 329 break; 330 } 331 case TestHandlerHDR: 332 image_handler = create_cl_hdr_image_handler (context, hdr_type); 333 break; 334 case TestHandlerDenoise: 335 image_handler = create_cl_denoise_image_handler (context); 336 break; 337 case TestHandlerSimpleNoiseReduction: 338 image_handler = create_cl_snr_image_handler (context); 339 break; 340 case TestHandlerWhiteBalance: { 341 XCam3aResultWhiteBalance wb; 342 wb.r_gain = 1.0; 343 wb.gr_gain = 1.0; 344 wb.gb_gain = 1.0; 345 wb.b_gain = 1.0; 346 SmartPtr<CLWbImageHandler> wb_handler; 347 image_handler = create_cl_wb_image_handler (context); 348 wb_handler = image_handler.dynamic_cast_ptr<CLWbImageHandler> (); 349 XCAM_ASSERT (wb_handler.ptr ()); 350 wb_handler->set_wb_config (wb); 351 break; 352 } 353 case TestHandlerGamma: { 354 XCam3aResultGammaTable gamma_table; 355 for(int i = 0; i < XCAM_GAMMA_TABLE_SIZE; ++i) 356 gamma_table.table[i] = (double)(pow(i / 255.0, 1 / 2.2) * 255.0); 357 SmartPtr<CLGammaImageHandler> gamma_handler; 358 image_handler = create_cl_gamma_image_handler (context); 359 gamma_handler = image_handler.dynamic_cast_ptr<CLGammaImageHandler> (); 360 XCAM_ASSERT (gamma_handler.ptr ()); 361 gamma_handler->set_gamma_table (gamma_table); 362 break; 363 } 364 case TestHandlerBayerNoiseReduction: { 365 XCam3aResultBayerNoiseReduction bnr; 366 bnr.bnr_gain = 0.2; 367 bnr.direction = 0.01; 368 image_handler = create_cl_bnr_image_handler (context); 369 SmartPtr<CLBnrImageHandler> bnr_handler; 370 bnr_handler = image_handler.dynamic_cast_ptr<CLBnrImageHandler> (); 371 XCAM_ASSERT (bnr_handler.ptr ()); 372 bnr_handler->set_bnr_config (bnr); 373 break; 374 } 375 case TestHandlerMacc: 376 image_handler = create_cl_macc_image_handler (context); 377 break; 378 case TestHandlerEe: { 379 XCam3aResultEdgeEnhancement ee; 380 XCam3aResultNoiseReduction nr; 381 ee.gain = 2.0; 382 ee.threshold = 150.0; 383 nr.gain = 0.1; 384 SmartPtr<CLEeImageHandler> ee_handler; 385 image_handler = create_cl_ee_image_handler (context); 386 ee_handler = image_handler.dynamic_cast_ptr<CLEeImageHandler> (); 387 XCAM_ASSERT (ee_handler.ptr ()); 388 ee_handler->set_ee_config_ee (ee); 389 ee_handler->set_ee_config_nr (nr); 390 break; 391 } 392 case TestHandlerBayerPipe: { 393 image_handler = create_cl_bayer_pipe_image_handler (context); 394 SmartPtr<CLBayerPipeImageHandler> bayer_pipe = image_handler.dynamic_cast_ptr<CLBayerPipeImageHandler> (); 395 XCAM_ASSERT (bayer_pipe.ptr ()); 396 bayer_pipe->set_output_format (output_format); 397 break; 398 } 399 default: 400 XCAM_LOG_ERROR ("unsupported image handler type:%d", handler_type); 401 return -1; 402 } 403 if (!image_handler.ptr ()) { 404 XCAM_LOG_ERROR ("create image_handler failed"); 405 return -1; 406 } 407 408 input_buf_info.init (input_format, 1920, 1080); 409 display = DrmDisplay::instance (); 410 buf_pool = new DrmBoBufferPool (display); 411 buf_pool->set_video_info (input_buf_info); 412 if (!buf_pool->reserve (6)) { 413 XCAM_LOG_ERROR ("init buffer pool failed"); 414 return -1; 415 } 416 417 while (!feof (input_fp.fp)) { 418 SmartPtr<DrmBoBuffer> input_buf, output_buf; 419 SmartPtr<BufferProxy> tmp_buf = buf_pool->get_buffer (buf_pool); 420 input_buf = tmp_buf.dynamic_cast_ptr<DrmBoBuffer> (); 421 422 XCAM_ASSERT (input_buf.ptr ()); 423 ret = read_buf (input_buf, input_fp); 424 if (ret == XCAM_RETURN_BYPASS) 425 break; 426 CHECK (ret, "read buffer from %s failed", input_file); 427 428 if (kernel_loop_count != 0) 429 { 430 kernel_loop (image_handler, input_buf, output_buf, kernel_loop_count); 431 CHECK (ret, "execute kernels failed"); 432 return 0; 433 } 434 435 ret = image_handler->execute (input_buf, output_buf); 436 CHECK (ret, "execute kernels failed"); 437 XCAM_ASSERT (output_buf.ptr ()); 438 439 ret = write_buf (output_buf, output_fp); 440 CHECK (ret, "read buffer from %s failed", output_file); 441 442 ++buf_count; 443 } 444 XCAM_LOG_INFO ("processed %d buffers successfully", buf_count); 445 return 0; 446} 447