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