Initial release
[adaptation/ap_samsung/gst-plugins-s5pc2xx.git] / camerasrc / src / include / camerasrc-common.h
1 /*
2  * camerasrc
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at your option)
11  * any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23
24 #ifndef __CAMERASRC_COMMON_H__
25 #define __CAMERASRC_COMMON_H__
26
27 #include <stdio.h>
28 #include <malloc.h>
29 #include <pthread.h>
30 #include <errno.h>      /*EXXX*/
31 #include <sys/ioctl.h>  /*ioctl*/
32 #include <string.h>     /*memcpy*/
33
34 #include <sys/types.h>  /*open*/
35 #include <sys/stat.h>
36 #include <fcntl.h>
37
38 #include <unistd.h>     /*mmap*/
39 #include <sys/mman.h>   /*alloc series, free..*/
40 #include <sys/time.h>   /*gettimeofday*/
41 #include <math.h>       /*log2*/
42 #include <gst/gst.h>
43
44 #undef __ASM_ARM_TYPES_H
45 #undef __ASSEMBLY_
46 #undef _I386_TYPES_H
47
48 #include <asm/types.h>
49 #include <linux/videodev2.h>    /* V4L2 APIs */
50 #include <linux/videodev2_exynos_camera.h>
51 #include <linux/videodev2_exynos_media.h>
52
53 #include "camerasrc.h"
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59
60 /**
61  * Memory utility definitions
62  */
63 #if !defined (PAGE_SHIFT)
64     #define PAGE_SHIFT sysconf(_SC_PAGESIZE)
65 #endif
66 #if !defined (PAGE_SIZE)
67     #define PAGE_SIZE (1UL << PAGE_SHIFT)
68 #endif
69 #if !defined (PAGE_MASK)
70     #define PAGE_MASK (~(PAGE_SIZE-1))
71 #endif
72
73 #define PAGE_ALIGN(addr)    (((addr)+PAGE_SIZE-1)&PAGE_MASK)
74 #define CLEAR(x)            memset (&(x), 0, sizeof (x))
75
76 #define CAMERASRC_MAX_WIDTH                     2560
77 #define CAMERASRC_MAX_HEIGHT                    1920
78 #define CAMERASRC_CID_NOT_SUPPORT               -1
79 #define CAMERASRC_USRPTR_MAX_BUFFER_NUM         12
80 #define CAMERASRC_MAX_FILENAME_LEN              255
81 #define CAMERASRC_DEV_NODE_PREFIX               "/dev/video"
82 #define CAMERASRC_DEV_FD_INIT                   -1
83 #define CAMERASRC_DEV_ON_ACCESS_STR             "ONACCESS"
84 #define CAMERASRC_DEV_RELEASED_STR              "RELEASED"
85 #define CAMERASRC_OPENED_CHK_FILENAME           "/tmp/.dev_chk"
86 #define CAMERASRC_THREAD_KILL                   -999
87 #define CAMERASRC_DEV_FD_EMERGENCY_CLOSED       -999
88 #define CAMERASRC_ERRMSG_MAX_LEN                128
89 #define CAMERASRC_FRAME_DUMP_PATH                   "/tmp/"
90 #define CAMERASRC_DBG_SCRIPT_PATH               "/mnt/mmc/cam_dbg_script"
91 #define CAMERASRC_PRIMARY_BASIC_INFO_PATH       "/tmp/.camprimarybasicinfo"
92 #define CAMERASRC_PRIMARY_MISC_INFO_PATH        "/tmp/.camprimarymiscinfo"
93 #define CAMERASRC_PRIMARY_EXTRA_INFO_PATH        "/tmp/.camprimaryextrainfo"
94 #define CAMERASRC_SECONDARY_BASIC_INFO_PATH     "/tmp/.camsecondarybasicinfo"
95 #define CAMERASRC_SECONDARY_MISC_INFO_PATH      "/tmp/.camsecondarymiscinfo"
96 #define CAMERASRC_SECONDARY_EXTRA_INFO_PATH      "/tmp/.camsecondaryextrainfo"
97 #define CAMERASRC_MAX_IMAGE_BUFFER_PLANES       3
98
99 #define USE_OPEN_CHK                            /*< Using open check with temporary file */
100 #define USE_SENSOR_MODE                         1
101 /*#define USE_SKIP_FRAME*/                        /*< Skip frame toggle */
102 /*#define USE_IOCTL_DEBUG*/                       /*< For debugging ioctl name, argument, address, etc */
103 /*#define USE_FRAME_COPY_BOUNDARY_CHECK*/         /*< Copy boundary checks occurs seg fault when overrun */
104 /*#define USE_SKIP_FRAME_AT_RAW_FRAME*/           /*< In pumping raw frame, initial 2-3 frames are darker. so skip it */
105 /*#define USE_CAMERASRC_FRAME_DUMP*/              /*< Debug system annoying me. Use printf!!!! */
106 /*#define USE_USERPTR_DEBUG*/
107 /*#define ENABLE_Q_ERROR*/
108
109 #ifndef GST_CAT_DEFAULT
110 GST_DEBUG_CATEGORY_EXTERN(camerasrc_debug);
111 #define GST_CAT_DEFAULT camerasrc_debug
112 #endif /* GST_CAT_DEFAULT */
113
114
115 #define camsrc_info(msg, args...)          GST_INFO(msg, ##args)
116 #define camsrc_warning(msg, args...)       GST_WARNING(msg, ##args)
117 #define camsrc_error(msg, args...)         GST_ERROR(msg, ##args)
118 #define camsrc_critical(msg, args...)      GST_ERROR(msg, ##args)
119 #define camsrc_assert(condition) { \
120         if (!(condition)) { \
121                 GST_ERROR("failed [%s]", #condition); \
122         } \
123 }
124
125 /*
126  * Values for internal
127  */
128 enum camerasrc_op_mode_t {
129     CAMERASRC_OP_PREVIEW = 0,
130     CAMERASRC_OP_CAPTURE,
131     CAMERASRC_OP_VIDEO,
132     CAMERASRC_OP_REGISTER_VALUE,
133     CAMERASRC_OP_NUM,
134 };
135
136 /*
137  * Values for internal
138  */
139 enum camerasrc_ctrl_property_t{
140     CAMERASRC_CTRL_SUPPORT = 0,
141     CAMERASRC_CTRL_MAX_VALUE,
142     CAMERASRC_CTRL_MIN_VALUE,
143     CAMERASRC_CTRL_CID_VALUE,
144     CAMERASRC_CTRL_CURRENT_VALUE,
145     CAMERASRC_CTRL_PROPERTY_NUM,
146 };
147
148 /*
149  * Values for internal
150  */
151 enum camerasrc_quality_t{
152     CAMERASRC_QUALITY_NORMAL = 0,
153     CAMERASRC_QUALITY_HIGH,
154     CAMERASRC_QUALITY_NUM,
155 };
156
157 enum camerasrc_dev_recog_t{
158     CAMERASRC_DEV_RECOG_ID = 0,
159     CAMERASRC_DEV_RECOG_INDEX,
160     CAMERASRC_DEV_RECOG_NUM,
161 };
162
163 /**
164  * Phase, camerasrc consist of two phase, running and non-running.
165  */
166 typedef enum {
167     CAMERASRC_PHASE_RUNNING = 0,
168     CAMERASRC_PHASE_NON_RUNNING,
169     CAMERASRC_PHASE_NUM,
170 } _camerasrc_phase_t;
171
172 typedef enum {
173     CAMERASRC_MISC_STILL_SIGNAL = 0,
174     CAMERASRC_MISC_SKIP_FRAME,
175     CAMERASRC_MISC_FUNC_NUM,
176 } _camerasrc_misc_func_t;
177
178 typedef enum{
179     _CAMERASRC_CMD_AF_CONTROL = 0,
180     _CAMERASRC_CMD_AF_AREA,
181     _CAMERASRC_CMD_STROBE_MODE,
182     _CAMERASRC_CMD_FACEDETECTION,
183     _CAMERASRC_CMD_SHUTTER_SPEED,
184     _CAMERASRC_CMD_SUPPORT_EMBED_EXIF,
185     _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING,
186     _CAMERASRC_CMD_CHECK_ESD,
187     _CAMERASRC_CMD_JPEG_COMPRESS_RATIO,
188     _CAMERASRC_CMD_JPEG_LENGTH,
189     _CAMERASRC_CMD_JPEG_THMBNL_LENGTH,
190     _CAMERASRC_CMD_JPEG_THMBNL_OFFSET,
191     _CAMERASRC_CMD_JPEG_SCRNL_LENGTH,
192     _CAMERASRC_CMD_JPEG_SCRNL_OFFSET,
193     _CAMERASRC_CMD_EXPOSURE_VALUE,
194     _CAMERASRC_CMD_ESD_CHECK,
195     _CAMERASRC_CMD_FRAME_DATA,
196     _CAMERASRC_CMD_EXIF_INFO,
197     _CAMERASRC_CMD_CTRL,
198     _CAMERASRC_CMD_ROTATION,
199     _CAMERASRC_CMD_SENSOR_MODE,
200     _CAMERASRC_CMD_VFLIP,
201     _CAMERASRC_CMD_HFLIP,
202     _CAMERASRC_CMD_NUM,
203 }_camsrc_cmd_t;
204
205 typedef struct{
206     int cid;
207     int value;
208 } _camerasrc_ctrl_t;
209
210 enum {
211     _CAMERASRC_FACEDETECTION_START = 0,
212     _CAMERASRC_FACEDETECTION_STOP,
213     _CAMERASRC_FACEDETECTION_NUM,
214 };
215
216 enum {
217     _CAMERASRC_AF_START = 0,
218     _CAMERASRC_AF_STOP,
219     _CAMERASRC_AF_RELEASE,
220     _CAMERASRC_AF_INIT,
221     _CAMERASRC_AF_DESTROY,
222 };
223
224 // U T I L I T Y   D E F I N I T I O N
225 /**
226  * Mapping device index - Device ID
227  */
228 #define _CAMERASRC_GET_DEV_INDEX(dev_id)        _camerasrc_dev_index[dev_id][CAMERASRC_DEV_RECOG_INDEX]
229 #define _CAMERASRC_GET_DEV_ID(dev_idx)          _camerasrc_dev_index[dev_idx][CAMERASRC_DEV_RECOG_ID]
230
231 /**
232  * For colorspace - pixel format combinability check
233  */
234 #define _CAMERASRC_MATCH_COL_TO_PIX(dev_id, colorspace, pixel_fmt, quality)     _camerasrc_match_col_to_pix[dev_id][colorspace][pixel_fmt][quality]
235
236 /**
237  * For control capability check
238  */
239 #define _CAMERASRC_CHK_SUPPORT_CONTROL(ctrl_id, dev_id)         _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_SUPPORT]
240 #define _CAMERASRC_MAX_VALUE(ctrl_id, dev_id)                   _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_MAX_VALUE]
241 #define _CAMERASRC_MIN_VALUE(ctrl_id, dev_id)                   _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_MIN_VALUE]
242 #define _CAMERASRC_GET_CID(ctrl_id, dev_id)                     _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CID_VALUE]
243 #define _CAMERASRC_GET_CURRENT_VALUE(ctrl_id, dev_id)           _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CURRENT_VALUE]
244 #define _CAMERASRC_SET_CURRENT_VALUE(ctrl_id, dev_id, value)    _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CURRENT_VALUE] = value
245
246 /**
247  * Need miscellaneous function on operation?
248  */
249 #define _CAMERASRC_NEED_MISC_FUNCTION(dev_id, operation, colorspace, misc_func)         _camerasrc_misc_func_list[dev_id][operation][colorspace][misc_func]
250 #define _CAMERASRC_SUPPORT_AF(dev_id) _camerasrc_af_support[dev_id]
251
252 /**
253  * Utility definitions
254  */
255 #define CAMERASRC_SET_STATE(handle, state) { \
256         handle->prev_state = handle->cur_state; \
257         handle->cur_state = state; \
258         camsrc_info("Set state [%d] -> [%d]", handle->prev_state, handle->cur_state); \
259 }
260 #define CAMERASRC_SET_PHASE(handle, phase)      handle->cur_phase = phase;
261 #define CAMERASRC_STATE(handle)                 (handle->cur_state)
262 #define CAMERASRC_PREV_STREAM_STATE(handle)     -1
263 #define CAMERASRC_PHASE(handle)                 (handle->cur_phase)
264 #define CAMERASRC_HANDLE(handle)                ((camerasrc_handle_t*) handle)
265 #define CAMERASRC_CURRENT_DEV_ID(handle)        (handle->dev_id)
266 #define CAMERASRC_IS_DEV_CLOSED(p) (p->dev_fd == -1 || p->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED)
267
268 #define YUV422_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width) << 1)
269 #define YUV420_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width * 3) >> 1)
270 #define RGB565_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width) << 1)
271
272 #define ISO_APPROXIMATE_VALUE(iso_in, iso_approximated) { \
273         if(iso_in > 8.909 && iso_in <= 11.22) iso_approximated = 10; \
274         else if(iso_in > 11.22 && iso_in <= 14.14) iso_approximated = 12; \
275         else if(iso_in > 14.14 && iso_in <= 17.82) iso_approximated = 16; \
276         else if(iso_in > 17.82 && iso_in <= 22.45) iso_approximated = 20; \
277         else if(iso_in > 22.45 && iso_in <= 28.28) iso_approximated = 25; \
278         else if(iso_in > 28.28 && iso_in <= 35.64) iso_approximated = 32; \
279         else if(iso_in > 35.64 && iso_in <= 44.90) iso_approximated = 40; \
280         else if(iso_in > 44.90 && iso_in <= 56.57) iso_approximated = 50; \
281         else if(iso_in > 56.57 && iso_in <= 71.27) iso_approximated = 64; \
282         else if(iso_in > 71.27 && iso_in <= 89.09) iso_approximated = 80; \
283         else if(iso_in > 89.09 && iso_in <= 112.2) iso_approximated = 100; \
284         else if(iso_in > 112.2 && iso_in <= 141.4) iso_approximated = 125; \
285         else if(iso_in > 141.4 && iso_in <= 178.2) iso_approximated = 160; \
286         else if(iso_in > 178.2 && iso_in <= 224.5) iso_approximated = 200; \
287         else if(iso_in > 224.5 && iso_in <= 282.8) iso_approximated = 250; \
288         else if(iso_in > 282.8 && iso_in <= 356.4) iso_approximated = 320; \
289         else if(iso_in > 356.4 && iso_in <= 449.0) iso_approximated = 400; \
290         else if(iso_in > 449.0 && iso_in <= 565.7) iso_approximated = 500; \
291         else if(iso_in > 565.7 && iso_in <= 712.7) iso_approximated = 640; \
292         else if(iso_in > 712.7 && iso_in <= 890.9) iso_approximated = 800; \
293         else if(iso_in > 890.9 && iso_in <= 1122) iso_approximated = 1000; \
294         else if(iso_in > 1122 && iso_in <= 1414) iso_approximated = 1250; \
295         else if(iso_in > 1414 && iso_in <= 1782) iso_approximated = 1600; \
296         else if(iso_in > 1782 && iso_in <= 2245) iso_approximated = 2000; \
297         else if(iso_in > 2245 && iso_in <= 2828) iso_approximated = 2500; \
298         else if(iso_in > 2828 && iso_in <= 3564) iso_approximated = 3200; \
299         else if(iso_in > 3564 && iso_in <= 4490) iso_approximated = 4000; \
300         else if(iso_in > 4490 && iso_in <= 5657) iso_approximated = 5000; \
301         else if(iso_in > 5657 && iso_in <= 7127) iso_approximated = 6400; \
302         else if(iso_in > 7127 && iso_in <= 8909) iso_approximated = 8000; \
303         else { \
304                 camsrc_warning("Invalid parameter(Maybe kernel failure).. give default value, 100");\
305                 iso_approximated = 100;\
306         }\
307 }
308
309 #define PHOTOMETRY_MODE_TO_METERING_MODE(photometry_mode, metering_mode) { \
310         if(photometry_mode == V4L2_PHOTOMETRY_MULTISEG) metering_mode = 1; \
311         else if (photometry_mode == V4L2_PHOTOMETRY_CWA) metering_mode = 2; \
312         else if (photometry_mode == V4L2_PHOTOMETRY_SPOT) metering_mode = 3; \
313         else if (photometry_mode == V4L2_PHOTOMETRY_AFSPOT) metering_mode = 3; \
314         else metering_mode = 1; \
315 }
316
317 #define CAMERASRC_EXIF_SHUTTERSPEED_VALUE_IN_APEX(NUM, DEN) (int)(-(log2((double)((double)NUM/(double)DEN))))
318 #define CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(NUM, DEN)     (int)(2 * (log2((double)((double)NUM/(double)DEN))))
319
320
321 typedef void *(*camerasrc_signal_func_t) (camsrc_handle_t handle);
322 typedef int (*camerasrc_skip_frame_func_t) (camsrc_handle_t handle, long int timeout, int skip_frame);
323
324 typedef struct _camerasrc_handle_t {
325         int is_async_open;
326
327         /* device information */
328         int dev_fd;
329         int cur_dev_id;
330         camerasrc_buffer_t alter_frame;
331         int check_esd;
332         int errnum;
333         int lens_rotation;      /* physical rotation of lens */
334
335         /* state information */
336         int prev_stream_state;
337         int prev_state;
338         int cur_state;
339         int cur_phase;
340
341         /* image format information */
342         int is_highquality;
343         int is_preset;
344         camerasrc_resol_name_t resolution;
345         camerasrc_format_t format;
346
347         /* buffer information */
348         camerasrc_io_method_t io_method;
349         camerasrc_usr_buf_t *present_buf;
350         int buffer_idx;
351         int num_buffers;
352         int queued_buffer_count;
353         int first_frame;
354         struct v4l2_buffer queued_buf_list[CAMERASRC_USRPTR_MAX_BUFFER_NUM];
355         camerasrc_buffer_t *buffer;
356         camerasrc_buffer_t scrnl_buf;  /* screennail buffer of captured JPEG image */
357
358         /* autofocusing information */
359         camerasrc_af_mode_t cur_af_mode;
360         camerasrc_af_scan_range_t cur_af_range;
361         camerasrc_callback_t af_cb;
362         pthread_t focusing_thread;
363         pthread_cond_t af_wait_cond;
364         camerasrc_auto_focus_status_t af_status;
365         camerasrc_auto_focus_cmd_t af_cmd;
366         int af_dev_val;
367         void *af_usr_data;
368         int hold_af_after_capturing;
369         int af_init_called;                     /* whether af init was called after device open. */
370         struct timeval set_af_area_time;        /* for AF start delay after set AF area */
371
372         /* Jpg Still information */
373         camerasrc_skip_frame_func_t skip_frame_func;
374
375         /* Shutter & exposure value */
376         int isAutoexposure;
377
378         /* fps */
379         camerasrc_frac_t timeperframe;
380
381         /* sensor mode */
382         camerasrc_sensor_mode_t sensor_mode;
383
384         /* flip */
385         int vflip;
386         int hflip;
387
388         /* thread safe mechanism */
389         pthread_mutex_t mutex;
390         pthread_mutex_t af_mutex;
391         pthread_cond_t cond;
392 } camerasrc_handle_t;
393
394 typedef struct {
395     int (*_ioctl) (camerasrc_handle_t *handle, int request, void *arg);
396     int (*_ioctl_once) (camerasrc_handle_t *handle, int request, void *arg);
397     void *(*_run_autofocusing) (camerasrc_handle_t *handle);
398     int (*_skip_frame) (camerasrc_handle_t *handle, long int timeout, int skip_frame);
399     int (*_copy_frame) (camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail);
400     int (*_set_cmd) (camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value);
401     int (*_get_cmd) (camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value);
402 } CAMERASRC_DEV_DEPENDENT_MISC_FUNC;
403
404 #ifdef __cplusplus
405 }
406 #endif
407
408 #endif /*__CAMERASRC_COMMON_H__*/