test-cl-image.cpp revision 42d62b1bccdc1a46b06b1b80b24c6af7036fb65b
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 36using namespace XCam; 37 38enum TestHandlerType { 39 TestHandlerUnknown = 0, 40 TestHandlerDemo, 41 TestHandlerBlackLevel, 42 TestHandlerDefect, 43 TestHandlerDemosaic, 44 TestHandlerColorConversion, 45 TestHandlerHDR, 46 TestHandlerWhiteBalance, 47 TestHandlerDenoise, 48 TestHandlerGamma, 49 TestHandlerSimpleNoiseReduction, 50 TestHandlerMacc, 51}; 52 53struct TestFileHandle { 54 FILE *fp; 55 TestFileHandle () 56 : fp (NULL) 57 {} 58 ~TestFileHandle () 59 { 60 if (fp) 61 fclose (fp); 62 } 63}; 64 65static XCamReturn 66read_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file) 67{ 68 const VideoBufferInfo info = buf->get_video_info (); 69 uint8_t *memory = NULL; 70 XCamReturn ret = XCAM_RETURN_NO_ERROR; 71 72 memory = buf->map (); 73 if (fread (memory, 1, info.size, file.fp) != info.size) { 74 if (feof (file.fp)) 75 ret = XCAM_RETURN_BYPASS; 76 else { 77 XCAM_LOG_ERROR ("read file failed, size doesn't match"); 78 ret = XCAM_RETURN_ERROR_FILE; 79 } 80 } 81 buf->unmap (); 82 return ret; 83} 84 85static XCamReturn 86write_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file) 87{ 88 const VideoBufferInfo info = buf->get_video_info (); 89 uint8_t *memory = NULL; 90 XCamReturn ret = XCAM_RETURN_NO_ERROR; 91 92 memory = buf->map (); 93 if (fwrite (memory, 1, info.size, file.fp) != info.size) { 94 XCAM_LOG_ERROR ("read file failed, size doesn't match"); 95 ret = XCAM_RETURN_ERROR_FILE; 96 } 97 buf->unmap (); 98 return ret; 99} 100 101static XCamReturn 102kernel_loop(SmartPtr<CLImageHandler> &image_handler, SmartPtr<DrmBoBuffer> &input_buf, SmartPtr<DrmBoBuffer> &output_buf, uint32_t kernel_loop_count) 103{ 104 XCamReturn ret = XCAM_RETURN_NO_ERROR; 105 for (uint32_t i = 0; i < kernel_loop_count; i++) { 106 PROFILING_START(cl_kernel); 107 ret = image_handler->execute (input_buf, output_buf); 108 PROFILING_END(cl_kernel, kernel_loop_count) 109 } 110 return ret; 111} 112 113static void 114print_help (const char *bin_name) 115{ 116 printf ("Usage: %s [-f format] -i input -o output\n" 117 "\t -t type specify image handler type\n" 118 "\t select from [demo, blacklevel, defect, demosaic, csc, hdr, wb, denoise, gamma, snr, macc]\n" 119 "\t -f input_format specify a input format\n" 120 "\t -g output_format specify a output format\n" 121 "\t select from [NV12, BA10, RGBA]\n" 122 "\t -i input specify input file path\n" 123 "\t -o output specify output file path\n" 124 "\t -p count specify cl kernel loop count\n" 125 "\t -c csc_type specify csc type, default:rgba2nv12\n" 126 "\t select from [rgbatonv12, rgbatolab, rgba64torgba]\n" 127 "\t -d hdr_type specify hdr type, default:rgb\n" 128 "\t select from [rgb, lab]\n" 129 "\t -h help\n" 130 , bin_name); 131} 132 133int main (int argc, char *argv[]) 134{ 135 uint32_t input_format = 0; 136 uint32_t output_format = V4L2_PIX_FMT_RGBA32; 137 uint32_t buf_count = 0; 138 uint32_t kernel_loop_count = 0; 139 const char *input_file = NULL, *output_file = NULL; 140 TestFileHandle input_fp, output_fp; 141 const char *bin_name = argv[0]; 142 TestHandlerType handler_type = TestHandlerUnknown; 143 XCamReturn ret = XCAM_RETURN_NO_ERROR; 144 SmartPtr<CLImageHandler> image_handler; 145 VideoBufferInfo input_buf_info; 146 SmartPtr<CLContext> context; 147 SmartPtr<DrmDisplay> display; 148 SmartPtr<BufferPool> buf_pool; 149 int opt = 0; 150 CLCscType csc_type = CL_CSC_TYPE_RGBATONV12; 151 CLHdrType hdr_type = CL_HDR_TYPE_RGB; 152 153 while ((opt = getopt(argc, argv, "f:i:o:t:p:c:d:g:h")) != -1) { 154 switch (opt) { 155 case 'i': 156 input_file = optarg; 157 break; 158 case 'o': 159 output_file = optarg; 160 break; 161 162 case 'f': { 163 if (!strcasecmp (optarg, "nv12")) 164 input_format = V4L2_PIX_FMT_NV12; 165 else if (!strcasecmp (optarg, "ba10")) 166 input_format = V4L2_PIX_FMT_SGRBG10; 167 else if (! strcasecmp (optarg, "rgba")) 168 input_format = V4L2_PIX_FMT_RGBA32; 169 else if (! strcasecmp (optarg, "rgba64")) 170 input_format = XCAM_PIX_FMT_RGBA64; 171 172 else 173 print_help (bin_name); 174 break; 175 } 176 case 'g': { 177 if (!strcasecmp (optarg, "nv12")) 178 output_format = V4L2_PIX_FMT_NV12; 179 else if (!strcasecmp (optarg, "ba10")) 180 output_format = V4L2_PIX_FMT_SGRBG10; 181 else if (! strcasecmp (optarg, "rgba")) 182 output_format = V4L2_PIX_FMT_RGBA32; 183 else if (! strcasecmp (optarg, "rgba64")) 184 output_format = XCAM_PIX_FMT_RGBA64; 185 186 else 187 print_help (bin_name); 188 break; 189 } 190 case 't': { 191 if (!strcasecmp (optarg, "demo")) 192 handler_type = TestHandlerDemo; 193 else if (!strcasecmp (optarg, "blacklevel")) 194 handler_type = TestHandlerBlackLevel; 195 else if (!strcasecmp (optarg, "defect")) 196 handler_type = TestHandlerDefect; 197 else if (!strcasecmp (optarg, "demosaic")) 198 handler_type = TestHandlerDemosaic; 199 else if (!strcasecmp (optarg, "csc")) 200 handler_type = TestHandlerColorConversion; 201 else if (!strcasecmp (optarg, "hdr")) 202 handler_type = TestHandlerHDR; 203 else if (!strcasecmp (optarg, "wb")) 204 handler_type = TestHandlerWhiteBalance; 205 else if (!strcasecmp (optarg, "denoise")) 206 handler_type = TestHandlerDenoise; 207 else if (!strcasecmp (optarg, "gamma")) 208 handler_type = TestHandlerGamma; 209 else if (!strcasecmp (optarg, "snr")) 210 handler_type = TestHandlerSimpleNoiseReduction; 211 else if (!strcasecmp (optarg, "macc")) 212 handler_type = TestHandlerMacc; 213 else 214 print_help (bin_name); 215 break; 216 } 217 case 'p': 218 kernel_loop_count = atoi (optarg); 219 break; 220 case 'c': 221 if (!strcasecmp (optarg, "rgbatonv12")) 222 csc_type = CL_CSC_TYPE_RGBATONV12; 223 else if (!strcasecmp (optarg, "rgbatolab")) 224 csc_type = CL_CSC_TYPE_RGBATOLAB; 225 else if (!strcasecmp (optarg, "rgba64torgba")) 226 csc_type = CL_CSC_TYPE_RGBA64TORGBA; 227 else 228 print_help (bin_name); 229 break; 230 case 'd': 231 if (!strcasecmp (optarg, "rgb")) 232 hdr_type = CL_HDR_TYPE_RGB; 233 else if (!strcasecmp (optarg, "lab")) 234 hdr_type = CL_HDR_TYPE_LAB; 235 else 236 print_help (bin_name); 237 break; 238 239 case 'h': 240 print_help (bin_name); 241 return 0; 242 243 default: 244 print_help (bin_name); 245 return -1; 246 } 247 } 248 249 if (!input_format || !input_file || !output_file || handler_type == TestHandlerUnknown) { 250 print_help (bin_name); 251 return -1; 252 } 253 254 input_fp.fp = fopen (input_file, "rb"); 255 output_fp.fp = fopen (output_file, "wb"); 256 if (!input_fp.fp || !output_fp.fp) { 257 XCAM_LOG_ERROR ("open input/output file failed"); 258 return -1; 259 } 260 261 context = CLDevice::instance ()->get_context (); 262 263 switch (handler_type) { 264 case TestHandlerDemo: 265 image_handler = create_cl_demo_image_handler (context); 266 break; 267 case TestHandlerBlackLevel: { 268 XCam3aResultBlackLevel blc; 269 blc.r_level = 0.05; 270 blc.gr_level = 0.05; 271 blc.gb_level = 0.05; 272 blc.b_level = 0.05; 273 image_handler = create_cl_blc_image_handler (context); 274 SmartPtr<CLBlcImageHandler> blc_handler; 275 blc_handler = image_handler.dynamic_cast_ptr<CLBlcImageHandler> (); 276 XCAM_ASSERT (blc_handler.ptr ()); 277 blc_handler->set_blc_config (blc); 278 break; 279 } 280 case TestHandlerDefect: 281 break; 282 case TestHandlerDemosaic: { 283 SmartPtr<CLBayer2RGBImageHandler> ba2rgb_handler; 284 image_handler = create_cl_demosaic_image_handler (context); 285 ba2rgb_handler = image_handler.dynamic_cast_ptr<CLBayer2RGBImageHandler> (); 286 XCAM_ASSERT (ba2rgb_handler.ptr ()); 287 ba2rgb_handler->set_output_format (output_format); 288 break; 289 } 290 case TestHandlerColorConversion: { 291 image_handler = create_cl_csc_image_handler (context, csc_type); 292 break; 293 } 294 case TestHandlerHDR: 295 image_handler = create_cl_hdr_image_handler (context, hdr_type); 296 break; 297 case TestHandlerDenoise: 298 image_handler = create_cl_denoise_image_handler (context); 299 break; 300 case TestHandlerSimpleNoiseReduction: 301 image_handler = create_cl_snr_image_handler (context); 302 break; 303 case TestHandlerWhiteBalance: { 304 XCam3aResultWhiteBalance wb; 305 wb.r_gain = 1.0; 306 wb.gr_gain = 1.0; 307 wb.gb_gain = 1.0; 308 wb.b_gain = 1.0; 309 SmartPtr<CLWbImageHandler> wb_handler; 310 image_handler = create_cl_wb_image_handler (context); 311 wb_handler = image_handler.dynamic_cast_ptr<CLWbImageHandler> (); 312 XCAM_ASSERT (wb_handler.ptr ()); 313 wb_handler->set_wb_config (wb); 314 break; 315 } 316 case TestHandlerGamma: { 317 XCam3aResultGammaTable gamma_table; 318 for(int i = 0; i < XCAM_GAMMA_TABLE_SIZE; ++i) 319 gamma_table.table[i] = (double)(pow(i / 255.0, 1 / 2.2) * 255.0); 320 SmartPtr<CLGammaImageHandler> gamma_handler; 321 image_handler = create_cl_gamma_image_handler (context); 322 gamma_handler = image_handler.dynamic_cast_ptr<CLGammaImageHandler> (); 323 XCAM_ASSERT (gamma_handler.ptr ()); 324 gamma_handler->set_gamma_table (gamma_table); 325 break; 326 } 327 case TestHandlerMacc: 328 image_handler = create_cl_macc_image_handler (context); 329 break; 330 331 default: 332 XCAM_LOG_ERROR ("unsupported image handler type:%d", handler_type); 333 return -1; 334 } 335 if (!image_handler.ptr ()) { 336 XCAM_LOG_ERROR ("create image_handler failed"); 337 return -1; 338 } 339 340 input_buf_info.init (input_format, 1920, 1080); 341 display = DrmDisplay::instance (); 342 buf_pool = new DrmBoBufferPool (display); 343 buf_pool->set_video_info (input_buf_info); 344 if (!buf_pool->reserve (6)) { 345 XCAM_LOG_ERROR ("init buffer pool failed"); 346 return -1; 347 } 348 349 while (!feof (input_fp.fp)) { 350 SmartPtr<DrmBoBuffer> input_buf, output_buf; 351 SmartPtr<BufferProxy> tmp_buf = buf_pool->get_buffer (buf_pool); 352 input_buf = tmp_buf.dynamic_cast_ptr<DrmBoBuffer> (); 353 354 XCAM_ASSERT (input_buf.ptr ()); 355 ret = read_buf (input_buf, input_fp); 356 if (ret == XCAM_RETURN_BYPASS) 357 break; 358 CHECK (ret, "read buffer from %s failed", input_file); 359 360 if (kernel_loop_count != 0) 361 { 362 kernel_loop (image_handler, input_buf, output_buf, kernel_loop_count); 363 CHECK (ret, "execute kernels failed"); 364 return 0; 365 } 366 367 ret = image_handler->execute (input_buf, output_buf); 368 CHECK (ret, "execute kernels failed"); 369 XCAM_ASSERT (output_buf.ptr ()); 370 371 ret = write_buf (output_buf, output_fp); 372 CHECK (ret, "read buffer from %s failed", output_file); 373 374 ++buf_count; 375 } 376 XCAM_LOG_INFO ("processed %d buffers successfully", buf_count); 377 return 0; 378} 379