Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * main.cpp - test |
| 3 | * |
| 4 | * Copyright (c) 2014 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 "device_manager.h" |
| 22 | #include "atomisp_device.h" |
| 23 | #include "isp_controller.h" |
| 24 | #include "isp_image_processor.h" |
| 25 | #include "x3a_analyzer_simple.h" |
| 26 | #if HAVE_IA_AIQ |
| 27 | #include "x3a_analyzer_aiq.h" |
| 28 | #endif |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 29 | #if HAVE_LIBCL |
| 30 | #include "cl_image_processor.h" |
| 31 | #endif |
| 32 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <signal.h> |
| 35 | #include <stdlib.h> |
John Ye | ae9da73 | 2015-01-22 13:36:36 +0800 | [diff] [blame] | 36 | #include "test_common.h" |
| 37 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 38 | |
| 39 | using namespace XCam; |
| 40 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 41 | class MainDeviceManager |
| 42 | : public DeviceManager |
| 43 | { |
| 44 | public: |
| 45 | MainDeviceManager () |
| 46 | : _file (NULL) |
| 47 | , _save_file (false) |
| 48 | , _interval (1) |
| 49 | , _frame_count (0) |
| 50 | {} |
| 51 | |
| 52 | ~MainDeviceManager () { |
| 53 | close_file (); |
| 54 | } |
| 55 | |
| 56 | void enable_save_file (bool enable) { |
| 57 | _save_file = enable; |
| 58 | } |
| 59 | void set_interval (uint32_t inteval) { |
| 60 | _interval = inteval; |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | virtual void handle_message (SmartPtr<XCamMessage> &msg); |
| 65 | virtual void handle_buffer (SmartPtr<VideoBuffer> &buf); |
| 66 | |
| 67 | private: |
| 68 | void open_file (); |
| 69 | void close_file (); |
| 70 | |
| 71 | FILE *_file; |
| 72 | bool _save_file; |
| 73 | uint32_t _interval; |
| 74 | uint32_t _frame_count; |
| 75 | }; |
| 76 | |
| 77 | void |
| 78 | MainDeviceManager::handle_message (SmartPtr<XCamMessage> &msg) |
| 79 | { |
| 80 | XCAM_UNUSED (msg); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | MainDeviceManager::handle_buffer (SmartPtr<VideoBuffer> &buf) |
| 85 | { |
| 86 | if (!_save_file) |
| 87 | return ; |
| 88 | |
| 89 | if ((_frame_count++ % _interval) != 0) |
| 90 | return; |
| 91 | |
| 92 | const VideoBufferInfo & frame_info = buf->get_video_info (); |
| 93 | uint8_t *frame = buf->map (); |
| 94 | // only for NV12 |
| 95 | uint32_t size = XCAM_ALIGN_UP(frame_info.width, 2) * XCAM_ALIGN_UP(frame_info.height, 2) * 3 / 2; |
| 96 | if (frame == NULL) |
| 97 | return; |
| 98 | open_file (); |
| 99 | if (fwrite (frame, size, 1, _file) <= 0) { |
| 100 | XCAM_LOG_WARNING ("write frame failed."); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | MainDeviceManager::open_file () |
| 106 | { |
| 107 | if (_file) |
| 108 | return; |
| 109 | _file = fopen (DEFAULT_SAVE_FILE_NAME, "wb"); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | MainDeviceManager::close_file () |
| 114 | { |
| 115 | if (_file) |
| 116 | fclose (_file); |
| 117 | _file = NULL; |
| 118 | } |
| 119 | |
| 120 | #define V4L2_CAPTURE_MODE_STILL 0x2000 |
| 121 | #define V4L2_CAPTURE_MODE_VIDEO 0x4000 |
| 122 | #define V4L2_CAPTURE_MODE_PREVIEW 0x8000 |
| 123 | |
| 124 | |
| 125 | static Mutex g_mutex; |
| 126 | static Cond g_cond; |
| 127 | static bool g_stop = false; |
| 128 | |
| 129 | void dev_stop_handler(int sig) |
| 130 | { |
| 131 | XCAM_UNUSED (sig); |
| 132 | |
| 133 | SmartLock locker (g_mutex); |
| 134 | g_stop = true; |
| 135 | g_cond.broadcast (); |
| 136 | |
| 137 | //exit(0); |
| 138 | } |
| 139 | |
| 140 | void print_help (const char *bin_name) |
| 141 | { |
| 142 | printf ("Usage: %s [-a analyzer]\n" |
| 143 | "\t -a analyzer specify a analyzer\n" |
| 144 | "\t select from [simple, aiq], default is [simple]\n" |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 145 | "\t -m mem_type specify video memory type\n" |
| 146 | "\t mem_type select from [dma, mmap], default is [mmap]\n" |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 147 | "\t -s save file to %s\n" |
| 148 | "\t -n interval save file on every [interval] frame\n" |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 149 | "\t -c process image with cl kernel\n" |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 150 | "\t -h help\n" |
| 151 | , bin_name |
| 152 | , DEFAULT_SAVE_FILE_NAME); |
| 153 | } |
| 154 | |
| 155 | int main (int argc, char *argv[]) |
| 156 | { |
| 157 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 158 | SmartPtr<MainDeviceManager> device_manager = new MainDeviceManager; |
| 159 | SmartPtr<V4l2Device> device; |
| 160 | SmartPtr<V4l2SubDevice> event_device; |
| 161 | SmartPtr<IspController> isp_controller; |
| 162 | SmartPtr<X3aAnalyzer> analyzer; |
| 163 | SmartPtr<ImageProcessor> processor; |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 164 | #if HAVE_LIBCL |
| 165 | SmartPtr<CLImageProcessor> cl_processor; |
| 166 | #endif |
| 167 | bool have_cl_processor = false; |
| 168 | enum v4l2_memory v4l2_mem_type = V4L2_MEMORY_MMAP; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 169 | const char *bin_name = argv[0]; |
| 170 | int opt; |
| 171 | |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 172 | while ((opt = getopt(argc, argv, "sca:n:m:h")) != -1) { |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 173 | switch (opt) { |
| 174 | case 'a': { |
| 175 | if (!strcmp (optarg, "simple")) |
| 176 | analyzer = new X3aAnalyzerSimple (); |
| 177 | #if HAVE_IA_AIQ |
| 178 | else if (!strcmp (optarg, "aiq")) |
| 179 | analyzer = new X3aAnalyzerAiq (isp_controller, DEFAULT_CPF_FILE); |
| 180 | #endif |
| 181 | else { |
| 182 | print_help (bin_name); |
| 183 | return -1; |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 188 | case 'm': { |
| 189 | if (!strcmp (optarg, "dma")) |
| 190 | v4l2_mem_type = V4L2_MEMORY_DMABUF; |
| 191 | else if (!strcmp (optarg, "mmap")) |
| 192 | v4l2_mem_type = V4L2_MEMORY_MMAP; |
| 193 | else |
| 194 | print_help (bin_name); |
| 195 | break; |
| 196 | } |
| 197 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 198 | case 's': |
| 199 | device_manager->enable_save_file (true); |
| 200 | break; |
| 201 | case 'n': |
| 202 | device_manager->set_interval (atoi(optarg)); |
| 203 | break; |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 204 | case 'c': |
| 205 | have_cl_processor = true; |
| 206 | break; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 207 | case 'h': |
| 208 | print_help (bin_name); |
| 209 | return 0; |
| 210 | |
| 211 | default: |
| 212 | print_help (bin_name); |
| 213 | return -1; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (!device.ptr ()) |
| 218 | device = new AtomispDevice (DEFAULT_CAPTURE_DEVICE); |
| 219 | if (!event_device.ptr ()) |
| 220 | event_device = new V4l2SubDevice (DEFAULT_EVENT_DEVICE); |
| 221 | if (!isp_controller.ptr ()) |
| 222 | isp_controller = new IspController (device); |
| 223 | if (!processor.ptr ()) |
| 224 | processor = new IspImageProcessor (isp_controller); |
| 225 | //if (!analyzer.ptr()) |
| 226 | // analyzer = new X3aAnalyzerSimple (); |
| 227 | |
| 228 | signal(SIGINT, dev_stop_handler); |
| 229 | |
| 230 | device->set_sensor_id (0); |
| 231 | device->set_capture_mode (V4L2_CAPTURE_MODE_VIDEO); |
| 232 | //device->set_mem_type (V4L2_MEMORY_DMABUF); |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 233 | device->set_mem_type (v4l2_mem_type); |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 234 | device->set_buffer_count (8); |
| 235 | device->set_framerate (25, 1); |
| 236 | ret = device->open (); |
| 237 | CHECK (ret, "device(%s) open failed", device->get_device_name()); |
| 238 | ret = device->set_format (1920, 1080, V4L2_PIX_FMT_NV12, V4L2_FIELD_NONE, 1920 * 2); |
| 239 | CHECK (ret, "device(%s) set format failed", device->get_device_name()); |
| 240 | |
| 241 | ret = event_device->open (); |
| 242 | CHECK (ret, "event device(%s) open failed", event_device->get_device_name()); |
| 243 | int event = V4L2_EVENT_ATOMISP_3A_STATS_READY; |
| 244 | ret = event_device->subscribe_event (event); |
| 245 | CHECK_CONTINUE ( |
| 246 | ret, |
| 247 | "device(%s) subscribe event(%d) failed", |
| 248 | event_device->get_device_name(), event); |
| 249 | event = V4L2_EVENT_FRAME_SYNC; |
| 250 | ret = event_device->subscribe_event (event); |
| 251 | CHECK_CONTINUE ( |
| 252 | ret, |
| 253 | "device(%s) subscribe event(%d) failed", |
| 254 | event_device->get_device_name(), event); |
| 255 | |
| 256 | device_manager->set_capture_device (device); |
| 257 | device_manager->set_event_device (event_device); |
| 258 | device_manager->set_isp_controller (isp_controller); |
| 259 | if (analyzer.ptr()) |
| 260 | device_manager->set_analyzer (analyzer); |
| 261 | device_manager->add_image_processor (processor); |
Wind Yuan | d442731 | 2015-02-11 16:09:00 +0800 | [diff] [blame^] | 262 | #if HAVE_LIBCL |
| 263 | if (have_cl_processor) { |
| 264 | cl_processor = new CLImageProcessor (); |
| 265 | device_manager->add_image_processor (cl_processor); |
| 266 | } |
| 267 | #endif |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 268 | ret = device_manager->start (); |
| 269 | CHECK (ret, "device manager start failed"); |
| 270 | |
| 271 | // wait for interruption |
| 272 | { |
| 273 | SmartLock locker (g_mutex); |
| 274 | while (!g_stop) |
| 275 | g_cond.wait (g_mutex); |
| 276 | } |
| 277 | |
| 278 | ret = device_manager->stop(); |
| 279 | CHECK_CONTINUE (ret, "device manager stop failed"); |
| 280 | device->close (); |
| 281 | event_device->close (); |
| 282 | |
| 283 | return 0; |
| 284 | } |