blob: a28721dd5851b54770d99ac1ea01adda3ab84bbb [file] [log] [blame]
Wind Yuan20f67cb2015-03-05 16:15:21 +08001/*
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"
wangfei11987462015-03-12 15:02:57 +080025#include "cl_hdr_handler.h"
Wind Yuan20f67cb2015-03-05 16:15:21 +080026#include "drm_bo_buffer.h"
27
28using namespace XCam;
29
30enum TestHandlerType {
31 TestHandlerUnknown = 0,
32 TestHandlerDemo,
33 TestHandlerBlackLevel,
34 TestHandlerDefect,
35 TestHandlerDemosic,
36 TestHandlerColorConversion,
37 TestHandlerHDR,
38};
39
40struct TestFileHandle {
41 FILE *fp;
42 TestFileHandle ()
43 : fp (NULL)
44 {}
45 ~TestFileHandle ()
46 {
47 if (fp)
48 fclose (fp);
49 }
50};
51
52static XCamReturn
53read_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
72static XCamReturn
73write_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
wangfeicf536692015-03-19 16:44:50 +080088static XCamReturn
89kernel_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 Yuan20f67cb2015-03-05 16:15:21 +0800101static void
102print_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"
wangfeicf536692015-03-19 16:44:50 +0800111 "\t -p count specify cl kernel loop count\n"
Wind Yuan20f67cb2015-03-05 16:15:21 +0800112 "\t -h help\n"
113 , bin_name);
114}
115
116int main (int argc, char *argv[])
117{
118 uint32_t format = 0;
119 uint32_t buf_count = 0;
wangfeicf536692015-03-19 16:44:50 +0800120 uint32_t kernel_loop_count = 0;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800121 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
wangfeicf536692015-03-19 16:44:50 +0800134 while ((opt = getopt(argc, argv, "f:i:o:t:p:h")) != -1) {
Wind Yuan20f67cb2015-03-05 16:15:21 +0800135 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 }
wangfeicf536692015-03-19 16:44:50 +0800169 case 'p':
170 kernel_loop_count = atoi (optarg);
171 break;
Wind Yuan20f67cb2015-03-05 16:15:21 +0800172 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:
wangfeicf536692015-03-19 16:44:50 +0800209 image_handler = create_cl_hdr_image_handler (context);
Wind Yuan20f67cb2015-03-05 16:15:21 +0800210 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);
wangfeicf536692015-03-19 16:44:50 +0800258
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 Yuan20f67cb2015-03-05 16:15:21 +0800266 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}