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