Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 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" |
wangfei | 1198746 | 2015-03-12 15:02:57 +0800 | [diff] [blame] | 25 | #include "cl_hdr_handler.h" |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 26 | #include "drm_bo_buffer.h" |
| 27 | |
| 28 | using namespace XCam; |
| 29 | |
| 30 | enum TestHandlerType { |
| 31 | TestHandlerUnknown = 0, |
| 32 | TestHandlerDemo, |
| 33 | TestHandlerBlackLevel, |
| 34 | TestHandlerDefect, |
| 35 | TestHandlerDemosic, |
| 36 | TestHandlerColorConversion, |
| 37 | TestHandlerHDR, |
| 38 | }; |
| 39 | |
| 40 | struct TestFileHandle { |
| 41 | FILE *fp; |
| 42 | TestFileHandle () |
| 43 | : fp (NULL) |
| 44 | {} |
| 45 | ~TestFileHandle () |
| 46 | { |
| 47 | if (fp) |
| 48 | fclose (fp); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | static XCamReturn |
| 53 | read_buf (SmartPtr<DrmBoBuffer> &buf, TestFileHandle &file) |
| 54 | { |
| 55 | const VideoBufferInfo info = buf->get_video_info (); |
| 56 | uint8_t *memory = NULL; |
| 57 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 58 | |
| 59 | memory = buf->map (); |
| 60 | if (fread (memory, 1, info.size, file.fp) != info.size) { |
| 61 | if (feof (file.fp)) |
| 62 | ret = XCAM_RETURN_BYPASS; |
| 63 | else { |
| 64 | XCAM_LOG_ERROR ("read file failed, size doesn't match"); |
| 65 | ret = XCAM_RETURN_ERROR_FILE; |
| 66 | } |
| 67 | } |
| 68 | buf->unmap (); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static XCamReturn |
| 73 | write_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 (fwrite (memory, 1, info.size, file.fp) != info.size) { |
| 81 | XCAM_LOG_ERROR ("read file failed, size doesn't match"); |
| 82 | ret = XCAM_RETURN_ERROR_FILE; |
| 83 | } |
| 84 | buf->unmap (); |
| 85 | return ret; |
| 86 | } |
| 87 | |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 88 | static XCamReturn |
| 89 | kernel_loop(SmartPtr<CLImageHandler> &image_handler, SmartPtr<DrmBoBuffer> &input_buf, SmartPtr<DrmBoBuffer> &output_buf, uint32_t kernel_loop_count) |
| 90 | { |
| 91 | int i; |
| 92 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 93 | for (i = 0; i < kernel_loop_count; i++) { |
| 94 | PROFILING_START(cl_kernel); |
| 95 | ret = image_handler->execute (input_buf, output_buf); |
| 96 | PROFILING_END(cl_kernel, kernel_loop_count) |
| 97 | } |
| 98 | return ret; |
| 99 | } |
| 100 | |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 101 | static void |
| 102 | print_help (const char *bin_name) |
| 103 | { |
| 104 | printf ("Usage: %s [-f format] -i input -o output\n" |
| 105 | "\t -t type specify image handler type\n" |
| 106 | "\t select from [demo, blacklevel, defect, demosic, csc, hdr]\n" |
| 107 | "\t -f format specify a format\n" |
| 108 | "\t select from [NV12, BA10]\n" |
| 109 | "\t -i input specify input file path\n" |
| 110 | "\t -o output specify output file path\n" |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 111 | "\t -p count specify cl kernel loop count\n" |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 112 | "\t -h help\n" |
| 113 | , bin_name); |
| 114 | } |
| 115 | |
| 116 | int main (int argc, char *argv[]) |
| 117 | { |
| 118 | uint32_t format = 0; |
| 119 | uint32_t buf_count = 0; |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 120 | uint32_t kernel_loop_count = 0; |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 121 | const char *input_file = NULL, *output_file = NULL; |
| 122 | TestFileHandle input_fp, output_fp; |
| 123 | const char *bin_name = argv[0]; |
| 124 | TestHandlerType handler_type = TestHandlerUnknown; |
| 125 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 126 | SmartPtr<CLImageHandler> image_handler; |
| 127 | VideoBufferInfo input_buf_info; |
| 128 | SmartPtr<DrmBoBuffer> input_buf, output_buf; |
| 129 | SmartPtr<CLContext> context; |
| 130 | SmartPtr<DrmDisplay> display; |
| 131 | SmartPtr<DrmBoBufferPool> buf_pool; |
| 132 | int opt = 0; |
| 133 | |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 134 | while ((opt = getopt(argc, argv, "f:i:o:t:p:h")) != -1) { |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 135 | switch (opt) { |
| 136 | case 'i': |
| 137 | input_file = optarg; |
| 138 | break; |
| 139 | case 'o': |
| 140 | output_file = optarg; |
| 141 | break; |
| 142 | |
| 143 | case 'f': { |
| 144 | if (!strcasecmp (optarg, "nv12")) |
| 145 | format = V4L2_PIX_FMT_NV12; |
| 146 | else if (!strcasecmp (optarg, "ba10")) |
| 147 | format = V4L2_PIX_FMT_SGRBG10; |
| 148 | else |
| 149 | print_help (bin_name); |
| 150 | break; |
| 151 | } |
| 152 | case 't': { |
| 153 | if (!strcasecmp (optarg, "demo")) |
| 154 | handler_type = TestHandlerDemo; |
| 155 | else if (!strcasecmp (optarg, "blacklevel")) |
| 156 | handler_type = TestHandlerBlackLevel; |
| 157 | else if (!strcasecmp (optarg, "defect")) |
| 158 | handler_type = TestHandlerDefect; |
| 159 | else if (!strcasecmp (optarg, "demosic")) |
| 160 | handler_type = TestHandlerDemosic; |
| 161 | else if (!strcasecmp (optarg, "csc")) |
| 162 | handler_type = TestHandlerColorConversion; |
| 163 | else if (!strcasecmp (optarg, "hdr")) |
| 164 | handler_type = TestHandlerHDR; |
| 165 | else |
| 166 | print_help (bin_name); |
| 167 | break; |
| 168 | } |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 169 | case 'p': |
| 170 | kernel_loop_count = atoi (optarg); |
| 171 | break; |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 172 | case 'h': |
| 173 | print_help (bin_name); |
| 174 | return 0; |
| 175 | |
| 176 | default: |
| 177 | print_help (bin_name); |
| 178 | return -1; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (!format || !input_file || !output_file || handler_type == TestHandlerUnknown) { |
| 183 | print_help (bin_name); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | input_fp.fp = fopen (input_file, "rb"); |
| 188 | output_fp.fp = fopen (output_file, "wb"); |
| 189 | if (!input_fp.fp || !output_fp.fp) { |
| 190 | XCAM_LOG_ERROR ("open input/output file failed"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | context = CLDevice::instance ()->get_context (); |
| 195 | |
| 196 | switch (handler_type) { |
| 197 | case TestHandlerDemo: |
| 198 | image_handler = create_cl_demo_image_handler (context); |
| 199 | break; |
| 200 | case TestHandlerBlackLevel: |
| 201 | break; |
| 202 | case TestHandlerDefect: |
| 203 | break; |
| 204 | case TestHandlerDemosic: |
| 205 | break; |
| 206 | case TestHandlerColorConversion: |
| 207 | break; |
| 208 | case TestHandlerHDR: |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 209 | image_handler = create_cl_hdr_image_handler (context); |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 210 | break; |
| 211 | |
| 212 | default: |
| 213 | XCAM_LOG_ERROR ("unsupported image handler type:%d", handler_type); |
| 214 | return -1; |
| 215 | } |
| 216 | if (!image_handler.ptr ()) { |
| 217 | XCAM_LOG_ERROR ("create image_handler failed"); |
| 218 | return -1; |
| 219 | } |
| 220 | input_buf_info.format = format; |
| 221 | input_buf_info.color_bits = 8; |
| 222 | input_buf_info.width = 1920; |
| 223 | input_buf_info.height = 1080; |
| 224 | switch (format) { |
| 225 | case V4L2_PIX_FMT_NV12: |
| 226 | input_buf_info.color_bits = 8; |
| 227 | input_buf_info.components = 2; |
| 228 | input_buf_info.strides[0] = input_buf_info.width; |
| 229 | input_buf_info.strides[1] = input_buf_info.strides[0]; |
| 230 | input_buf_info.offsets[0] = 0; |
| 231 | input_buf_info.offsets[1] = input_buf_info.strides[0] * input_buf_info.height; |
| 232 | input_buf_info.size = input_buf_info.strides[0] * input_buf_info.height * 3 / 2; |
| 233 | break; |
| 234 | case V4L2_PIX_FMT_SGRBG10: |
| 235 | input_buf_info.color_bits = 10; |
| 236 | input_buf_info.components = 1; |
| 237 | input_buf_info.strides[0] = input_buf_info.width * 2; |
| 238 | input_buf_info.offsets[0] = 0; |
| 239 | input_buf_info.size = input_buf_info.strides[0] * input_buf_info.height; |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | display = DrmDisplay::instance (); |
| 244 | buf_pool = new DrmBoBufferPool (display); |
| 245 | buf_pool->set_buffer_info (input_buf_info); |
| 246 | if (!buf_pool->init (6)) { |
| 247 | XCAM_LOG_ERROR ("init buffer pool failed"); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | while (!feof (input_fp.fp)) { |
| 252 | input_buf = buf_pool->get_buffer (buf_pool); |
| 253 | XCAM_ASSERT (input_buf.ptr ()); |
| 254 | ret = read_buf (input_buf, input_fp); |
| 255 | if (ret == XCAM_RETURN_BYPASS) |
| 256 | break; |
| 257 | CHECK (ret, "read buffer from %s failed", input_file); |
wangfei | cf53669 | 2015-03-19 16:44:50 +0800 | [diff] [blame^] | 258 | |
| 259 | if (kernel_loop_count != 0) |
| 260 | { |
| 261 | kernel_loop (image_handler, input_buf, output_buf, kernel_loop_count); |
| 262 | CHECK (ret, "execute kernels failed"); |
| 263 | return 0; |
| 264 | } |
| 265 | |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 266 | ret = image_handler->execute (input_buf, output_buf); |
| 267 | CHECK (ret, "execute kernels failed"); |
| 268 | XCAM_ASSERT (output_buf.ptr ()); |
| 269 | |
| 270 | ret = write_buf (output_buf, output_fp); |
| 271 | CHECK (ret, "read buffer from %s failed", output_file); |
| 272 | |
| 273 | ++buf_count; |
| 274 | } |
| 275 | XCAM_LOG_INFO ("processed %d buffers successfully", buf_count); |
| 276 | return 0; |
| 277 | } |