1. Seperate preview pipeline and encoding pipeline
[platform/core/multimedia/libmm-camcorder.git] / src / include / mm_camcorder_util.h
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifndef __MM_CAMCORDER_UTIL_H__
23 #define __MM_CAMCORDER_UTIL_H__
24
25 /*=======================================================================================
26 | INCLUDE FILES                                                                         |
27 ========================================================================================*/
28 #include <camsrcjpegenc.h>
29 #include <linux/magic.h>
30
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /*=======================================================================================
37 | GLOBAL DEFINITIONS AND DECLARATIONS FOR CAMCORDER                                     |
38 ========================================================================================*/
39
40 /*=======================================================================================
41 | MACRO DEFINITIONS                                                                     |
42 ========================================================================================*/
43 #ifndef CLEAR
44 #define CLEAR(x)            memset (&(x), 0, sizeof (x))
45 #endif
46
47 #define MMCAMCORDER_ADD_BUFFER_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
48 do { \
49         MMCamcorderHandlerItem *item = NULL; \
50         item = (MMCamcorderHandlerItem *)g_malloc(sizeof(MMCamcorderHandlerItem)); \
51         if (!item) {\
52                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
53         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
54                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
55         } else { \
56                 item->object = G_OBJECT(x_pad); \
57                 item->category = x_category; \
58                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_BUFFER, x_callback, x_hcamcorder, NULL); \
59                 x_hcamcorder->buffer_probes = g_list_append(x_hcamcorder->buffer_probes, item); \
60                 _mmcam_dbg_log("Adding buffer probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
61         } \
62 } while (0);
63
64 #define MMCAMCORDER_ADD_EVENT_PROBE(x_pad, x_category, x_callback, x_hcamcorder) \
65 do { \
66         MMCamcorderHandlerItem *item = NULL; \
67         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
68         if (!item) { \
69                 _mmcam_dbg_err("Cannot connect buffer probe [malloc fail] \n"); \
70         } \
71         else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
72                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
73         } else { \
74                 item->object =G_OBJECT(x_pad); \
75                 item->category = x_category; \
76                 item->handler_id = gst_pad_add_probe(x_pad, GST_PAD_PROBE_TYPE_EVENT_BOTH, x_callback, x_hcamcorder, NULL); \
77                 x_hcamcorder->event_probes = g_list_append(x_hcamcorder->event_probes, item); \
78                 _mmcam_dbg_log("Adding event probe on [%s:%s] - [ID : %lu], [Category : %x] ", GST_DEBUG_PAD_NAME(item->object), item->handler_id, item->category); \
79         } \
80 } while (0);
81
82 #define MMCAMCORDER_SIGNAL_CONNECT( x_object, x_category, x_signal, x_callback, x_hcamcorder) \
83 do { \
84         MMCamcorderHandlerItem* item = NULL; \
85         item = (MMCamcorderHandlerItem *) g_malloc(sizeof(MMCamcorderHandlerItem)); \
86         if (!item) { \
87                 _mmcam_dbg_err("Cannot connect signal [%s]\n", x_signal ); \
88         } else if (x_category == 0 || !(x_category & _MMCAMCORDER_HANDLER_CATEGORY_ALL)) { \
89                 _mmcam_dbg_err("Invalid handler category : %x \n", x_category); \
90         } else { \
91                 item->object = G_OBJECT(x_object); \
92                 item->category = x_category; \
93                 item->handler_id = g_signal_connect(G_OBJECT(x_object), x_signal,\
94                                                     G_CALLBACK(x_callback), x_hcamcorder ); \
95                 x_hcamcorder->signals = g_list_append(x_hcamcorder->signals, item); \
96                 _mmcam_dbg_log("Connecting signal on [%s] - [ID : %lu], [Category : %x] ", GST_OBJECT_NAME(item->object), item->handler_id, item->category); \
97         } \
98 } while (0);
99
100 #define MMCAMCORDER_G_OBJECT_GET(obj, name, value) \
101 do { \
102         if (obj) { \
103                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
104                         g_object_get(G_OBJECT(obj), name, value, NULL); \
105                 } else { \
106                         _mmcam_dbg_warn ("The object doesn't have a property named(%s)", name); \
107                 } \
108         } else { \
109                 _mmcam_dbg_err("Null object"); \
110         } \
111 } while(0);
112
113 #define MMCAMCORDER_G_OBJECT_SET(obj, name, value) \
114 do { \
115         if (obj) { \
116                 if(g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(obj)), name)) { \
117                         g_object_set(G_OBJECT(obj), name, value, NULL); \
118                 } else { \
119                         _mmcam_dbg_warn ("The object doesn't have a property named(%s)", name); \
120                 } \
121         } else { \
122                 _mmcam_dbg_err("Null object"); \
123         } \
124 } while(0);
125
126 #define MMCAM_FOURCC(a,b,c,d)  (guint32)((a)|(b)<<8|(c)<<16|(d)<<24)
127 #define MMCAM_FOURCC_ARGS(fourcc) \
128         ((gchar)((fourcc)&0xff)), \
129         ((gchar)(((fourcc)>>8)&0xff)), \
130         ((gchar)(((fourcc)>>16)&0xff)), \
131         ((gchar)(((fourcc)>>24)&0xff))
132
133 #define MMCAM_SEND_MESSAGE(handle, msg_id, msg_code) \
134 {\
135         _MMCamcorderMsgItem msg;\
136         msg.id = msg_id;\
137         msg.param.code = msg_code;\
138         _mmcam_dbg_log("msg id : %x, code : %x", msg_id, msg_code);\
139         _mmcamcroder_send_message((MMHandleType)handle, &msg);\
140 }
141
142
143 /*=======================================================================================
144 | ENUM DEFINITIONS                                                                      |
145 ========================================================================================*/
146 /**
147  *Type define of util.
148  */
149 typedef enum {
150         _MMCAMCORDER_HANDLER_PREVIEW = (1 << 0),
151         _MMCAMCORDER_HANDLER_VIDEOREC = (1 << 1),
152         _MMCAMCORDER_HANDLER_STILLSHOT = (1 << 2),
153         _MMCAMCORDER_HANDLER_AUDIOREC = (1 << 3),
154 } _MMCamcorderHandlerCategory;
155
156 /*=======================================================================================
157 | STRUCTURE DEFINITIONS                                                                 |
158 ========================================================================================*/
159
160 /**
161  * Structure of location info
162  */
163 typedef struct {
164         gint32 longitude;
165         gint32 latitude;
166         gint32 altitude;
167 } _MMCamcorderLocationInfo;
168
169 /**
170  * Structure of handler item
171  */
172 typedef struct {
173         GObject *object;
174         _MMCamcorderHandlerCategory category;
175         gulong handler_id;
176 } MMCamcorderHandlerItem;
177
178 /**
179  * Structure of message item
180  */
181 typedef struct {
182         MMHandleType handle;            /**< handle */
183         int id;                         /**< message id */
184         MMMessageParamType param;       /**< message parameter */
185         pthread_mutex_t lock;           /**< mutex for item */
186 } _MMCamcorderMsgItem;
187
188 /**
189  * Structure of zero copy image buffer
190  */
191 #define SCMN_IMGB_MAX_PLANE         (4)
192
193 /* image buffer definition ***************************************************
194
195     +------------------------------------------+ ---
196     |                                          |  ^
197     |     a[], p[]                             |  |
198     |     +---------------------------+ ---    |  |
199     |     |                           |  ^     |  |
200     |     |<---------- w[] ---------->|  |     |  |
201     |     |                           |  |     |  |
202     |     |                           |        |
203     |     |                           |  h[]   |  e[]
204     |     |                           |        |
205     |     |                           |  |     |  |
206     |     |                           |  |     |  |
207     |     |                           |  v     |  |
208     |     +---------------------------+ ---    |  |
209     |                                          |  v
210     +------------------------------------------+ ---
211
212     |<----------------- s[] ------------------>|
213 */
214
215 typedef struct
216 {
217         /* width of each image plane */
218         int w[SCMN_IMGB_MAX_PLANE];
219         /* height of each image plane */
220         int h[SCMN_IMGB_MAX_PLANE];
221         /* stride of each image plane */
222         int s[SCMN_IMGB_MAX_PLANE];
223         /* elevation of each image plane */
224         int e[SCMN_IMGB_MAX_PLANE];
225         /* user space address of each image plane */
226         void *a[SCMN_IMGB_MAX_PLANE];
227         /* physical address of each image plane, if needs */
228         void *p[SCMN_IMGB_MAX_PLANE];
229         /* color space type of image */
230         int cs;
231         /* left postion, if needs */
232         int x;
233         /* top position, if needs */
234         int y;
235         /* to align memory */
236         int __dummy2;
237         /* arbitrary data */
238         int data[16];
239         /* dmabuf or ion fd */
240         int fd[SCMN_IMGB_MAX_PLANE];
241         /* flag for buffer share */
242         int buf_share_method;
243         /* Y plane size */
244         int y_size;
245         /* UV plane size */
246         int uv_size;
247         /* Tizen buffer object of each image plane */
248         void *bo[SCMN_IMGB_MAX_PLANE];
249 } SCMN_IMGB;
250
251 /*=======================================================================================
252 | CONSTANT DEFINITIONS                                                                  |
253 ========================================================================================*/
254 #define FAT32_FILE_SYSTEM_MAX_SIZE              (4294967295UL)     /* 4 GigaByte - 1 byte */
255 #define NANO_SEC_PER_MILI_SEC                   1000000
256 #define _MMCAMCORDER_HANDLER_CATEGORY_ALL \
257         (_MMCAMCORDER_HANDLER_PREVIEW | _MMCAMCORDER_HANDLER_VIDEOREC |_MMCAMCORDER_HANDLER_STILLSHOT | _MMCAMCORDER_HANDLER_AUDIOREC)
258
259 /*=======================================================================================
260 | GLOBAL FUNCTION PROTOTYPES                                                            |
261 ========================================================================================*/
262 /* GStreamer */
263 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
264 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object);
265 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category);
266 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category);
267 void _mmcamcorder_remove_all_handlers(MMHandleType handle, _MMCamcorderHandlerCategory category);
268 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was);
269 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list);
270 gboolean _mmcamcorder_link_elements(GList *element_list);
271
272 /* Message */
273 gboolean _mmcamcroder_msg_callback(void *data);
274 gboolean _mmcamcroder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data);
275 void _mmcamcroder_remove_message_all(MMHandleType handle);
276
277 /* Pixel format */
278 int _mmcamcorder_get_pixel_format(GstCaps *pad);
279 int _mmcamcorder_get_pixtype(unsigned int fourcc);
280 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format);
281
282 /* JPEG encode */
283 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
284                                   int src_format, unsigned int src_length, unsigned int jpeg_quality,
285                                   void **result_data, unsigned int *result_length, int enc_type);
286 /* resize */
287 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
288                                    unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, unsigned int *dst_length);
289 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
290                                            unsigned char **dst, unsigned int dst_width, unsigned int dst_height);
291
292 /* Recording */
293 /* find top level tag only, do not use this function for finding sub level tags.
294    tag_fourcc is Four-character-code (FOURCC) */
295 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind);
296 gint32 _mmcamcorder_double_to_fix(gdouble d_number);
297 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos);
298 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info);
299 gboolean _mmcamcorder_write_udta(FILE *f, _MMCamcorderLocationInfo info);
300 guint64 _mmcamcorder_get_container_size(const guchar *size);
301 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation);
302
303 /* File system */
304 int _mmcamcorder_get_freespace(const gchar *path, guint64 *free_space);
305 int _mmcamcorder_get_file_size(const char *filename, guint64 *size);
306 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type);
307 gboolean _mmcamcorder_check_file_path(const gchar *path);
308
309 /* Task */
310 void *_mmcamcorder_util_task_thread_func(void *data);
311
312 #ifdef __cplusplus
313 }
314 #endif
315
316 #endif /* __MM_CAMCORDER_UTIL_H__ */