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 | |
| 88 | static void |
| 89 | print_help (const char *bin_name) |
| 90 | { |
| 91 | printf ("Usage: %s [-f format] -i input -o output\n" |
| 92 | "\t -t type specify image handler type\n" |
| 93 | "\t select from [demo, blacklevel, defect, demosic, csc, hdr]\n" |
| 94 | "\t -f format specify a format\n" |
| 95 | "\t select from [NV12, BA10]\n" |
| 96 | "\t -i input specify input file path\n" |
| 97 | "\t -o output specify output file path\n" |
| 98 | "\t -h help\n" |
| 99 | , bin_name); |
| 100 | } |
| 101 | |
| 102 | int main (int argc, char *argv[]) |
| 103 | { |
| 104 | uint32_t format = 0; |
| 105 | uint32_t buf_count = 0; |
| 106 | const char *input_file = NULL, *output_file = NULL; |
| 107 | TestFileHandle input_fp, output_fp; |
| 108 | const char *bin_name = argv[0]; |
| 109 | TestHandlerType handler_type = TestHandlerUnknown; |
| 110 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 111 | SmartPtr<CLImageHandler> image_handler; |
| 112 | VideoBufferInfo input_buf_info; |
| 113 | SmartPtr<DrmBoBuffer> input_buf, output_buf; |
| 114 | SmartPtr<CLContext> context; |
| 115 | SmartPtr<DrmDisplay> display; |
| 116 | SmartPtr<DrmBoBufferPool> buf_pool; |
| 117 | int opt = 0; |
| 118 | |
| 119 | while ((opt = getopt(argc, argv, "f:i:o:t:h")) != -1) { |
| 120 | switch (opt) { |
| 121 | case 'i': |
| 122 | input_file = optarg; |
| 123 | break; |
| 124 | case 'o': |
| 125 | output_file = optarg; |
| 126 | break; |
| 127 | |
| 128 | case 'f': { |
| 129 | if (!strcasecmp (optarg, "nv12")) |
| 130 | format = V4L2_PIX_FMT_NV12; |
| 131 | else if (!strcasecmp (optarg, "ba10")) |
| 132 | format = V4L2_PIX_FMT_SGRBG10; |
| 133 | else |
| 134 | print_help (bin_name); |
| 135 | break; |
| 136 | } |
| 137 | case 't': { |
| 138 | if (!strcasecmp (optarg, "demo")) |
| 139 | handler_type = TestHandlerDemo; |
| 140 | else if (!strcasecmp (optarg, "blacklevel")) |
| 141 | handler_type = TestHandlerBlackLevel; |
| 142 | else if (!strcasecmp (optarg, "defect")) |
| 143 | handler_type = TestHandlerDefect; |
| 144 | else if (!strcasecmp (optarg, "demosic")) |
| 145 | handler_type = TestHandlerDemosic; |
| 146 | else if (!strcasecmp (optarg, "csc")) |
| 147 | handler_type = TestHandlerColorConversion; |
| 148 | else if (!strcasecmp (optarg, "hdr")) |
| 149 | handler_type = TestHandlerHDR; |
| 150 | else |
| 151 | print_help (bin_name); |
| 152 | break; |
| 153 | } |
| 154 | case 'h': |
| 155 | print_help (bin_name); |
| 156 | return 0; |
| 157 | |
| 158 | default: |
| 159 | print_help (bin_name); |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (!format || !input_file || !output_file || handler_type == TestHandlerUnknown) { |
| 165 | print_help (bin_name); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | input_fp.fp = fopen (input_file, "rb"); |
| 170 | output_fp.fp = fopen (output_file, "wb"); |
| 171 | if (!input_fp.fp || !output_fp.fp) { |
| 172 | XCAM_LOG_ERROR ("open input/output file failed"); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | context = CLDevice::instance ()->get_context (); |
| 177 | |
| 178 | switch (handler_type) { |
| 179 | case TestHandlerDemo: |
| 180 | image_handler = create_cl_demo_image_handler (context); |
| 181 | break; |
| 182 | case TestHandlerBlackLevel: |
| 183 | break; |
| 184 | case TestHandlerDefect: |
| 185 | break; |
| 186 | case TestHandlerDemosic: |
| 187 | break; |
| 188 | case TestHandlerColorConversion: |
| 189 | break; |
| 190 | case TestHandlerHDR: |
wangfei | 1198746 | 2015-03-12 15:02:57 +0800 | [diff] [blame^] | 191 | image_handler = create_cl_hdr_image_handler (context); |
Wind Yuan | 20f67cb | 2015-03-05 16:15:21 +0800 | [diff] [blame] | 192 | break; |
| 193 | |
| 194 | default: |
| 195 | XCAM_LOG_ERROR ("unsupported image handler type:%d", handler_type); |
| 196 | return -1; |
| 197 | } |
| 198 | if (!image_handler.ptr ()) { |
| 199 | XCAM_LOG_ERROR ("create image_handler failed"); |
| 200 | return -1; |
| 201 | } |
| 202 | input_buf_info.format = format; |
| 203 | input_buf_info.color_bits = 8; |
| 204 | input_buf_info.width = 1920; |
| 205 | input_buf_info.height = 1080; |
| 206 | switch (format) { |
| 207 | case V4L2_PIX_FMT_NV12: |
| 208 | input_buf_info.color_bits = 8; |
| 209 | input_buf_info.components = 2; |
| 210 | input_buf_info.strides[0] = input_buf_info.width; |
| 211 | input_buf_info.strides[1] = input_buf_info.strides[0]; |
| 212 | input_buf_info.offsets[0] = 0; |
| 213 | input_buf_info.offsets[1] = input_buf_info.strides[0] * input_buf_info.height; |
| 214 | input_buf_info.size = input_buf_info.strides[0] * input_buf_info.height * 3 / 2; |
| 215 | break; |
| 216 | case V4L2_PIX_FMT_SGRBG10: |
| 217 | input_buf_info.color_bits = 10; |
| 218 | input_buf_info.components = 1; |
| 219 | input_buf_info.strides[0] = input_buf_info.width * 2; |
| 220 | input_buf_info.offsets[0] = 0; |
| 221 | input_buf_info.size = input_buf_info.strides[0] * input_buf_info.height; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | display = DrmDisplay::instance (); |
| 226 | buf_pool = new DrmBoBufferPool (display); |
| 227 | buf_pool->set_buffer_info (input_buf_info); |
| 228 | if (!buf_pool->init (6)) { |
| 229 | XCAM_LOG_ERROR ("init buffer pool failed"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | while (!feof (input_fp.fp)) { |
| 234 | input_buf = buf_pool->get_buffer (buf_pool); |
| 235 | XCAM_ASSERT (input_buf.ptr ()); |
| 236 | ret = read_buf (input_buf, input_fp); |
| 237 | if (ret == XCAM_RETURN_BYPASS) |
| 238 | break; |
| 239 | CHECK (ret, "read buffer from %s failed", input_file); |
| 240 | ret = image_handler->execute (input_buf, output_buf); |
| 241 | CHECK (ret, "execute kernels failed"); |
| 242 | XCAM_ASSERT (output_buf.ptr ()); |
| 243 | |
| 244 | ret = write_buf (output_buf, output_fp); |
| 245 | CHECK (ret, "read buffer from %s failed", output_file); |
| 246 | |
| 247 | ++buf_count; |
| 248 | } |
| 249 | XCAM_LOG_INFO ("processed %d buffers successfully", buf_count); |
| 250 | return 0; |
| 251 | } |