[Release version 0.10.64] Fix Tizen coding rule violation
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_util.c
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 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 =======================================================================================*/
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <sys/vfs.h> /* struct statfs */
29 #include <sys/time.h> /* gettimeofday */
30 #include <sys/stat.h>
31 #include <gst/video/video-info.h>
32 #include <gio/gio.h>
33
34 #include "mm_camcorder_internal.h"
35 #include "mm_camcorder_util.h"
36 #include "mm_camcorder_sound.h"
37 #include <mm_util_imgp.h>
38 #include <mm_util_jpeg.h>
39
40 #include <storage.h>
41
42 /*-----------------------------------------------------------------------
43 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
44 -----------------------------------------------------------------------*/
45
46 /*-----------------------------------------------------------------------
47 |    LOCAL VARIABLE DEFINITIONS for internal                            |
48 -----------------------------------------------------------------------*/
49 #define TIME_STRING_MAX_LEN                     64
50 #define __MMCAMCORDER_CAPTURE_WAIT_TIMEOUT      5
51
52 #define FPUTC_CHECK(x_char, x_file) \
53 { \
54         if (fputc(x_char, x_file) == EOF) { \
55                 _mmcam_dbg_err("[Critical] fputc() returns fail.\n"); \
56                 return FALSE; \
57         } \
58 }
59 #define FPUTS_CHECK(x_str, x_file) \
60 { \
61         if (fputs(x_str, x_file) == EOF) { \
62                 _mmcam_dbg_err("[Critical] fputs() returns fail.\n"); \
63                 SAFE_G_FREE(str); \
64                 return FALSE; \
65         } \
66 }
67
68 /*---------------------------------------------------------------------------
69 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
70 ---------------------------------------------------------------------------*/
71 /* STATIC INTERNAL FUNCTION */
72
73 //static gint            skip_mdat(FILE *f);
74 static guint16           get_language_code(const char *str);
75 static gchar*            str_to_utf8(const gchar *str);
76 static inline gboolean   write_tag(FILE *f, const gchar *tag);
77 static inline gboolean   write_to_32(FILE *f, guint val);
78 static inline gboolean   write_to_16(FILE *f, guint val);
79 static inline gboolean   write_to_24(FILE *f, guint val);
80 #ifdef _USE_YUV_TO_RGB888_
81 static gboolean _mmcamcorder_convert_YUV_to_RGB888(unsigned char *src, int src_fmt, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
82 #endif /* _USE_YUV_TO_RGB888_ */
83 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
84 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
85 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
86
87
88 /*===========================================================================================
89 |                                                                                                                                                                                       |
90 |  FUNCTION DEFINITIONS                                                                                                                                         |
91 |                                                                                                                                                                                       |
92 ========================================================================================== */
93 /*---------------------------------------------------------------------------
94 |    GLOBAL FUNCTION DEFINITIONS:                                                                                       |
95 ---------------------------------------------------------------------------*/
96
97 static int __gdbus_method_call_sync(GDBusConnection *conn, const char *bus_name,
98         const char *object, const char *iface, const char *method,
99         GVariant *args, GVariant **result, bool is_sync)
100 {
101         int ret = MM_ERROR_NONE;
102         GVariant *dbus_reply = NULL;
103
104         if (!conn || !object || !iface || !method) {
105                 _mmcam_dbg_err("Invalid Argument %p %p %p %p",
106                         conn, object, iface, method);
107                 return MM_ERROR_INVALID_ARGUMENT;
108         }
109
110         _mmcam_dbg_log("Dbus call - obj [%s], iface [%s], method [%s]", object, iface, method);
111
112         if (is_sync) {
113                 dbus_reply = g_dbus_connection_call_sync(conn,
114                         bus_name, object, iface, method, args, NULL,
115                         G_DBUS_CALL_FLAGS_NONE, G_DBUS_REPLY_TIMEOUT, NULL, NULL);
116                 if (dbus_reply) {
117                         _mmcam_dbg_log("Method Call '%s.%s' Success", iface, method);
118                         *result = dbus_reply;
119                 } else {
120                         _mmcam_dbg_err("dbus method call sync reply failed");
121                         ret = MM_ERROR_CAMCORDER_INTERNAL;
122                 }
123         } else {
124                 g_dbus_connection_call(conn, bus_name, object, iface, method, args, NULL,
125                         G_DBUS_CALL_FLAGS_NONE, G_DBUS_REPLY_TIMEOUT, NULL, NULL, NULL);
126         }
127
128         return ret;
129 }
130
131 static int __gdbus_subscribe_signal(GDBusConnection *conn,
132         const char *object_name, const char *iface_name, const char *signal_name,
133         GDBusSignalCallback signal_cb, guint *subscribe_id, void *userdata)
134 {
135         guint subs_id = 0;
136
137         if (!conn || !object_name || !iface_name || !signal_name || !signal_cb || !subscribe_id) {
138                 _mmcam_dbg_err("Invalid Argument %p %p %p %p %p %p",
139                         conn, object_name, iface_name, signal_name, signal_cb, subscribe_id);
140                 return MM_ERROR_INVALID_ARGUMENT;
141         }
142
143         _mmcam_dbg_log("subscirbe signal Obj %s, iface_name %s, sig_name %s",
144                 object_name, iface_name, signal_name);
145
146         subs_id = g_dbus_connection_signal_subscribe(conn,
147                 NULL, iface_name, signal_name, object_name, NULL,
148                 G_DBUS_SIGNAL_FLAGS_NONE, signal_cb, userdata, NULL);
149         if (!subs_id) {
150                 _mmcam_dbg_err("g_dbus_connection_signal_subscribe() failed");
151                 return MM_ERROR_CAMCORDER_INTERNAL;
152         } else {
153                 *subscribe_id = subs_id;
154                 _mmcam_dbg_log("subs_id %u", subs_id);
155         }
156
157         return MM_ERROR_NONE;
158 }
159
160
161 static void __gdbus_stream_eos_cb(GDBusConnection *connection,
162         const gchar *sender_name, const gchar *object_path, const gchar *interface_name,
163         const gchar *signal_name, GVariant *param, gpointer user_data)
164 {
165         int played_idx = 0;
166         _MMCamcorderGDbusCbInfo *gdbus_info = NULL;
167
168         _mmcam_dbg_log("entered");
169
170         if (!param || !user_data) {
171                 _mmcam_dbg_err("invalid parameter %p %p", param, user_data);
172                 return;
173         }
174
175         gdbus_info = (_MMCamcorderGDbusCbInfo *)user_data;
176
177         g_variant_get(param, "(i)", &played_idx);
178
179         g_mutex_lock(&gdbus_info->sync_mutex);
180
181         _mmcam_dbg_log("gdbus_info->param %d, played_idx : %d",
182                 gdbus_info->param, played_idx);
183
184         if (gdbus_info->param == played_idx) {
185                 g_dbus_connection_signal_unsubscribe(connection, gdbus_info->subscribe_id);
186
187                 gdbus_info->is_playing = FALSE;
188                 gdbus_info->subscribe_id = 0;
189                 gdbus_info->param = 0;
190
191                 g_cond_signal(&gdbus_info->sync_cond);
192         }
193
194         g_mutex_unlock(&gdbus_info->sync_mutex);
195
196         return;
197 }
198
199 static int __gdbus_wait_for_cb_return(_MMCamcorderGDbusCbInfo *gdbus_info, int time_out)
200 {
201         int ret = MM_ERROR_NONE;
202         gint64 end_time = 0;
203
204         if (!gdbus_info) {
205                 _mmcam_dbg_err("invalid info");
206                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
207         }
208
209         g_mutex_lock(&gdbus_info->sync_mutex);
210
211         _mmcam_dbg_log("entered");
212
213         if (gdbus_info->is_playing == FALSE) {
214                 _mmcam_dbg_log("callback is already returned");
215                 g_mutex_unlock(&gdbus_info->sync_mutex);
216                 return MM_ERROR_NONE;
217         }
218
219         end_time = g_get_monotonic_time() + (time_out * G_TIME_SPAN_MILLISECOND);
220
221         if (g_cond_wait_until(&gdbus_info->sync_cond, &gdbus_info->sync_mutex, end_time)) {
222                 _mmcam_dbg_log("wait signal received");
223         } else {
224                 _mmcam_dbg_err("wait time is expired");
225                 ret = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
226         }
227
228         g_mutex_unlock(&gdbus_info->sync_mutex);
229
230         return ret;
231 }
232
233
234 gint32 _mmcamcorder_double_to_fix(gdouble d_number)
235 {
236         return (gint32) (d_number * 65536.0);
237 }
238
239 // find top level tag only, do not use this function for finding sub level tags
240 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
241 {
242         size_t read_item = 0;
243         guchar buf[8];
244
245         if (do_rewind)
246                 rewind(f);
247
248         while ((read_item = fread(&buf, sizeof(guchar), 8, f)) > 0) {
249                 uint64_t buf_size = 0;
250                 uint32_t buf_fourcc = 0;
251
252                 if (read_item < 8) {
253                         _mmcam_dbg_err("fread failed : %d", read_item);
254                         break;
255                 }
256
257                 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
258
259                 if (tag_fourcc == buf_fourcc) {
260                         _mmcam_dbg_log("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
261                         return TRUE;
262                 } else {
263                         _mmcam_dbg_log("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
264
265                         buf_size = _mmcamcorder_get_container_size(buf);
266
267                         /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
268                         if (buf_fourcc == MMCAM_FOURCC('m', 'd', 'a', 't') &&
269                             buf_size == 1) {
270                                 read_item = fread(&buf, sizeof(guchar), 8, f);
271                                 if (read_item < 8) {
272                                         _mmcam_dbg_err("fread failed");
273                                         return FALSE;
274                                 }
275
276                                 buf_size = _mmcamcorder_get_container_size64(buf);
277                                 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
278                         } else {
279                                 buf_size = buf_size - 8; /* include tag */
280                         }
281
282                         _mmcam_dbg_log("seek %llu", buf_size);
283                         if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
284                                 _mmcam_dbg_err("fseeko() fail");
285                                 return FALSE;
286                         }
287                 }
288         }
289
290         _mmcam_dbg_log("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
291
292         return FALSE;
293 }
294
295 gboolean _mmcamcorder_find_fourcc(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
296 {
297         size_t read_item = 0;
298         guchar buf[8];
299
300         if (do_rewind)
301                 rewind(f);
302
303         while ((read_item = fread(&buf, sizeof(guchar), 8, f))  > 0) {
304                 uint64_t buf_size = 0;
305                 uint32_t buf_fourcc = 0;
306
307                 if (read_item < 8) {
308                         _mmcam_dbg_err("fread failed : %d", read_item);
309                         break;
310                 }
311
312                 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
313
314                 if (tag_fourcc == buf_fourcc) {
315                         _mmcam_dbg_log("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
316                         return TRUE;
317                 } else if (buf_fourcc == MMCAM_FOURCC('m', 'o', 'o', 'v') &&
318                                    tag_fourcc != buf_fourcc) {
319                         if (_mmcamcorder_find_fourcc(f, tag_fourcc, FALSE))
320                                 return TRUE;
321                         else
322                                 continue;
323                 } else {
324                         _mmcam_dbg_log("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
325
326                         buf_size = _mmcamcorder_get_container_size(buf);
327
328                         /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
329                         if (buf_fourcc == MMCAM_FOURCC('m', 'd', 'a', 't') &&
330                             buf_size == 1) {
331                                 read_item = fread(&buf, sizeof(guchar), 8, f);
332                                 if (read_item < 8) {
333                                         _mmcam_dbg_err("fread failed");
334                                         return FALSE;
335                                 }
336
337                                 buf_size = _mmcamcorder_get_container_size64(buf);
338                                 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
339                         } else {
340                                 buf_size = buf_size - 8; /* include tag */
341                         }
342
343                         _mmcam_dbg_log("seek %llu", buf_size);
344                         if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
345                                 _mmcam_dbg_err("fseeko() fail");
346                                 return FALSE;
347                         }
348                 }
349         }
350
351         _mmcam_dbg_log("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
352
353         return FALSE;
354 }
355
356 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos)
357 {
358         _mmcam_dbg_log("size : %"G_GINT64_FORMAT"", curr_pos-prev_pos);
359         if (fseeko(f, prev_pos, SEEK_SET) != 0) {
360                 _mmcam_dbg_err("fseeko() fail");
361                 return FALSE;
362         }
363
364         if (!write_to_32(f, curr_pos -prev_pos))
365                 return FALSE;
366
367         if (fseeko(f, curr_pos, SEEK_SET) != 0) {
368                 _mmcam_dbg_err("fseeko() fail");
369                 return FALSE;
370         }
371
372         return TRUE;
373 }
374
375 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info)
376 {
377         gint64 current_pos, pos;
378         gchar *str = NULL;
379
380         _mmcam_dbg_log("");
381
382         if ((pos = ftello(f)) < 0) {
383                 _mmcam_dbg_err("ftello() returns negative value");
384                 return FALSE;
385         }
386
387         if (!write_to_32(f, 0)) //size
388                 return FALSE;
389
390         if (!write_tag(f, "loci")) // type
391                 return FALSE;
392
393         FPUTC_CHECK(0, f);              // version
394
395         if (!write_to_24(f, 0)) // flags
396                 return FALSE;
397
398         if (!write_to_16(f, get_language_code("eng"))) // language
399                 return FALSE;
400
401         str = str_to_utf8("location_name");
402
403         FPUTS_CHECK(str, f); // name
404         SAFE_G_FREE(str);
405
406         FPUTC_CHECK('\0', f);
407         FPUTC_CHECK(0, f);              //role
408
409         if (!write_to_32(f, info.longitude))    // Longitude
410                 return FALSE;
411
412         if (!write_to_32(f, info.latitude)) // Latitude
413                 return FALSE;
414
415         if (!write_to_32(f, info.altitude))     // Altitude
416                 return FALSE;
417
418         str = str_to_utf8("Astronomical_body");
419         FPUTS_CHECK(str, f);//Astronomical_body
420         SAFE_G_FREE(str);
421
422         FPUTC_CHECK('\0', f);
423
424         str = str_to_utf8("Additional_notes");
425         FPUTS_CHECK(str, f); // Additional_notes
426         SAFE_G_FREE(str);
427
428         FPUTC_CHECK('\0', f);
429
430         if ((current_pos = ftello(f)) < 0) {
431                 _mmcam_dbg_err("ftello() returns negative value");
432                 return FALSE;
433         }
434
435         if (!_mmcamcorder_update_size(f, pos, current_pos))
436                 return FALSE;
437
438         return TRUE;
439 }
440
441 void _mmcamcorder_write_Latitude(FILE *f, int value)
442 {
443         char s_latitude[9];
444         int l_decimal = 0;
445         int l_below_decimal = 0;
446
447         l_decimal = value / 10000;
448         if (value < 0) {
449                 if (l_decimal == 0)
450                         snprintf(s_latitude, 5, "-%.2d.", l_decimal);
451                 else
452                         snprintf(s_latitude, 5, "%.2d.", l_decimal);
453         } else {
454                 snprintf(s_latitude, 5, "+%.2d.", l_decimal);
455         }
456
457         l_below_decimal = value - (l_decimal * 10000);
458         if (l_below_decimal < 0)
459                 l_below_decimal = -l_below_decimal;
460
461         snprintf(&s_latitude[4], 5, "%.4d", l_below_decimal);
462
463         write_tag(f, s_latitude);
464 }
465
466 void _mmcamcorder_write_Longitude(FILE *f, int value)
467 {
468         char s_longitude[10];
469         int l_decimal = 0;
470         int l_below_decimal = 0;
471
472         l_decimal = value / 10000;
473         if (value < 0) {
474                 if (l_decimal == 0)
475                         snprintf(s_longitude, 6, "-%.3d.", l_decimal);
476                 else
477                         snprintf(s_longitude, 6, "%.3d.", l_decimal);
478         } else {
479                 snprintf(s_longitude, 6, "+%.3d.", l_decimal);
480         }
481
482         l_below_decimal = value - (l_decimal * 10000);
483         if (l_below_decimal < 0)
484                 l_below_decimal = -l_below_decimal;
485
486         snprintf(&s_longitude[5], 5, "%.4d", l_below_decimal);
487
488         write_tag(f, s_longitude);
489 }
490
491 #define D_GEOGRAPH "\xA9xyz"
492 // 0x0012 -> latitude(8) + longitude(9) + seperator(1) = 18
493 // 0x15c7 -> encode in english
494 #define D_INFO_GEOGRAPH 0x001215c7
495
496 gboolean _mmcamcorder_write_geodata(FILE *f, _MMCamcorderLocationInfo info)
497 {
498         gint64 current_pos, pos;
499
500         _mmcam_dbg_log("");
501
502         if ((pos = ftello(f)) < 0) {
503                 _mmcam_dbg_err("ftello() returns negative value");
504                 return FALSE;
505         }
506
507         if (!write_to_32(f, 0))                 //size
508                 return FALSE;
509         // tag -> .xyz
510         if (!write_tag(f, D_GEOGRAPH))  // type
511                 return FALSE;
512
513         if (!write_to_32(f, D_INFO_GEOGRAPH))
514                 return FALSE;
515
516         _mmcamcorder_write_Latitude(f, info.latitude);
517         _mmcamcorder_write_Longitude(f, info.longitude);
518
519         FPUTC_CHECK(0x2F, f);
520
521         if ((current_pos = ftello(f)) < 0) {
522                 _mmcam_dbg_err("ftello() returns negative value");
523                 return FALSE;
524         }
525
526         if (!_mmcamcorder_update_size(f, pos, current_pos))
527                 return FALSE;
528
529         return TRUE;
530 }
531
532
533 gboolean _mmcamcorder_write_udta(FILE *f, int gps_enable, _MMCamcorderLocationInfo info, _MMCamcorderLocationInfo geotag)
534 {
535         gint64 current_pos, pos;
536
537         _mmcam_dbg_log("gps enable : %d", gps_enable);
538         if (gps_enable == FALSE) {
539                 _mmcam_dbg_log("no need to write udta");
540                 return TRUE;
541         }
542
543         if ((pos = ftello(f)) < 0) {
544                 _mmcam_dbg_err("ftello() returns negative value");
545                 return FALSE;
546         }
547
548         /* size */
549         if (!write_to_32(f, 0)) {
550                 _mmcam_dbg_err("failed to write size");
551                 return FALSE;
552         }
553
554         /* type */
555         if (!write_tag(f, "udta")) {
556                 _mmcam_dbg_err("failed to write type udta");
557                 return FALSE;
558         }
559
560         if (gps_enable) {
561                 if (!_mmcamcorder_write_loci(f, info)) {
562                         _mmcam_dbg_err("failed to write loci");
563                         return FALSE;
564                 }
565
566                 if (!_mmcamcorder_write_geodata(f, geotag)) {
567                         _mmcam_dbg_err("failed to write geodata");
568                         return FALSE;
569                 }
570         }
571
572         if ((current_pos = ftello(f)) < 0) {
573                 _mmcam_dbg_err("ftello() returns negative value");
574                 return FALSE;
575         }
576
577         if (!_mmcamcorder_update_size(f, pos, current_pos)) {
578                 _mmcam_dbg_err("failed to update size");
579                 return FALSE;
580         }
581
582         _mmcam_dbg_log("done");
583
584         return TRUE;
585 }
586
587
588 guint64 _mmcamcorder_get_container_size(const guchar *size)
589 {
590         guint64 result = 0;
591         guint64 temp = 0;
592
593         temp = size[0];
594         result = temp << 24;
595         temp = size[1];
596         result = result | (temp << 16);
597         temp = size[2];
598         result = result | (temp << 8);
599         result = result | size[3];
600
601         _mmcam_dbg_log("result : %llu", result);
602
603         return result;
604 }
605
606
607 guint64 _mmcamcorder_get_container_size64(const guchar *size)
608 {
609         guint64 result = 0;
610         guint64 temp = 0;
611
612         temp = size[0];
613         result = temp << 56;
614         temp = size[1];
615         result = result | (temp << 48);
616         temp = size[2];
617         result = result | (temp << 40);
618         temp = size[3];
619         result = result | (temp << 32);
620         temp = size[4];
621         result = result | (temp << 24);
622         temp = size[5];
623         result = result | (temp << 16);
624         temp = size[6];
625         result = result | (temp << 8);
626         result = result | size[7];
627
628         _mmcam_dbg_log("result : %llu", result);
629
630         return result;
631 }
632
633
634 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation)
635 {
636         /* for 0 degree */
637         guint32 a = 0x00010000;
638         guint32 b = 0;
639         guint32 c = 0;
640         guint32 d = 0x00010000;
641
642         switch (orientation) {
643         case MM_CAMCORDER_TAG_VIDEO_ORT_90:/* 90 degree */
644                 a = 0;
645                 b = 0x00010000;
646                 c = 0xffff0000;
647                 d = 0;
648                 break;
649         case MM_CAMCORDER_TAG_VIDEO_ORT_180:/* 180 degree */
650                 a = 0xffff0000;
651                 d = 0xffff0000;
652                 break;
653         case MM_CAMCORDER_TAG_VIDEO_ORT_270:/* 270 degree */
654                 a = 0;
655                 b = 0xffff0000;
656                 c = 0x00010000;
657                 d = 0;
658                 break;
659         case MM_CAMCORDER_TAG_VIDEO_ORT_NONE:/* 0 degree */
660         default:
661                 break;
662         }
663
664         write_to_32(f, a);
665         write_to_32(f, b);
666         write_to_32(f, 0);
667         write_to_32(f, c);
668         write_to_32(f, d);
669         write_to_32(f, 0);
670         write_to_32(f, 0);
671         write_to_32(f, 0);
672         write_to_32(f, 0x40000000);
673
674         _mmcam_dbg_log("orientation : %d, write data 0x%x 0x%x 0x%x 0x%x",
675                                    orientation, a, b, c, d);
676
677         return TRUE;
678 }
679
680
681 int _mmcamcorder_get_freespace(const gchar *path, const gchar *root_directory, guint64 *free_space)
682 {
683         int ret = 0;
684         struct statvfs vfs;
685
686         int is_internal = TRUE;
687         struct stat stat_path;
688         struct stat stat_root;
689
690         if (path == NULL || free_space == NULL) {
691                 _mmcam_dbg_err("invalid parameter %p, %p", path, free_space);
692                 return -1;
693         }
694
695         if (root_directory && strlen(root_directory) > 0) {
696                 if (stat(path, &stat_path) != 0) {
697                         *free_space = 0;
698                         _mmcam_dbg_err("failed to stat for [%s][errno %d]", path, errno);
699                         return -1;
700                 }
701
702                 if (stat(root_directory, &stat_root) != 0) {
703                         *free_space = 0;
704                         _mmcam_dbg_err("failed to stat for [%s][errno %d]", root_directory, errno);
705                         return -1;
706                 }
707
708                 if (stat_path.st_dev != stat_root.st_dev)
709                         is_internal = FALSE;
710         } else {
711                 _mmcam_dbg_warn("root_directory is NULL, assume that it's internal storage.");
712         }
713
714         if (is_internal)
715                 ret = storage_get_internal_memory_size(&vfs);
716         else
717                 ret = storage_get_external_memory_size(&vfs);
718
719         if (ret < 0) {
720                 *free_space = 0;
721                 _mmcam_dbg_err("failed to get memory size [%s]", path);
722                 return -1;
723         } else {
724                 *free_space = vfs.f_bsize * vfs.f_bavail;
725                 /*
726                 _mmcam_dbg_log("vfs.f_bsize [%lu], vfs.f_bavail [%lu]", vfs.f_bsize, vfs.f_bavail);
727                 _mmcam_dbg_log("memory size %llu [%s]", *free_space, path);
728                 */
729                 return 1;
730         }
731 }
732
733
734 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type)
735 {
736         struct statfs fs;
737
738         g_assert(path);
739
740         if (!g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
741                 _mmcam_dbg_log("File(%s) doesn't exist.", path);
742                 return -2;
743         }
744
745         if (-1 == statfs(path, &fs)) {
746                 _mmcam_dbg_log("statfs failed.(%s)", path);
747                 return -1;
748         }
749
750         *file_system_type = (int)fs.f_type;
751
752         return 0;
753 }
754
755
756 int _mmcamcorder_get_device_flash_brightness(GDBusConnection *conn, int *brightness)
757 {
758         int get_value = 0;
759         int ret = MM_ERROR_NONE;
760         GVariant *params = NULL;
761         GVariant *result = NULL;
762
763         ret = __gdbus_method_call_sync(conn, "org.tizen.system.deviced",
764                 "/Org/Tizen/System/DeviceD/Led", "org.tizen.system.deviced.Led",
765                 "GetBrightnessForCamera", params, &result, TRUE);
766         if (ret != MM_ERROR_NONE) {
767                 _mmcam_dbg_err("Dbus Call on Client Error");
768                 return ret;
769         }
770
771         if (result) {
772                 g_variant_get(result, "(i)", &get_value);
773                 *brightness = get_value;
774                 _mmcam_dbg_log("flash brightness : %d", *brightness);
775         } else {
776                 _mmcam_dbg_err("replied result is null");
777                 ret = MM_ERROR_CAMCORDER_INTERNAL;
778         }
779
780         return ret;
781 }
782
783
784 int _mmcamcorder_send_sound_play_message(GDBusConnection *conn, _MMCamcorderGDbusCbInfo *gdbus_info,
785         const char *sample_name, const char *stream_role, const char *volume_gain, int sync_play)
786 {
787         int get_value = 0;
788         int ret = MM_ERROR_NONE;
789         GVariant *params = NULL, *result = NULL;
790         guint subs_id = 0;
791
792         if (!conn || !gdbus_info) {
793                 _mmcam_dbg_err("Invalid parameter %p %p", conn, gdbus_info);
794                 return MM_ERROR_CAMCORDER_INTERNAL;
795         }
796
797         params = g_variant_new("(sss)", sample_name, stream_role, volume_gain);
798         result = g_variant_new("(i)", get_value);
799
800         ret = __gdbus_method_call_sync(conn, "org.pulseaudio.Server",
801                 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer",
802                 "SamplePlay", params, &result, TRUE);
803         if (ret != MM_ERROR_NONE) {
804                 _mmcam_dbg_err("Dbus Call on Client Error");
805                 return ret;
806         }
807
808         if (result) {
809                 g_variant_get(result, "(i)", &get_value);
810                 _mmcam_dbg_log("played index : %d", get_value);
811         } else {
812                 _mmcam_dbg_err("replied result is null");
813                 return MM_ERROR_CAMCORDER_INTERNAL;
814         }
815
816         g_mutex_lock(&gdbus_info->sync_mutex);
817
818         if (gdbus_info->subscribe_id > 0) {
819                 _mmcam_dbg_warn("subscribe_id[%u] is remained. remove it.", gdbus_info->subscribe_id);
820
821                 g_dbus_connection_signal_unsubscribe(conn, gdbus_info->subscribe_id);
822
823                 gdbus_info->subscribe_id = 0;
824         }
825
826         gdbus_info->is_playing = TRUE;
827         gdbus_info->param = get_value;
828
829         ret = __gdbus_subscribe_signal(conn,
830                 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer", "EOS",
831                 __gdbus_stream_eos_cb, &subs_id, gdbus_info);
832
833         if (ret == MM_ERROR_NONE)
834                 gdbus_info->subscribe_id = subs_id;
835
836         g_mutex_unlock(&gdbus_info->sync_mutex);
837
838         if (sync_play && ret == MM_ERROR_NONE)
839                 ret = __gdbus_wait_for_cb_return(gdbus_info, G_DBUS_CB_TIMEOUT_MSEC);
840
841         return ret;
842 }
843
844
845 int _mmcamcorder_get_file_size(const char *filename, guint64 *size)
846 {
847         struct stat buf;
848
849         if (stat(filename, &buf) != 0)
850                 return -1;
851         *size = (guint64)buf.st_size;
852         return 1;
853 }
854
855
856 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
857 {
858         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
859         GList *list = NULL;
860         MMCamcorderHandlerItem *item = NULL;
861
862         mmf_return_if_fail(hcamcorder);
863
864         if (!hcamcorder->buffer_probes) {
865                 _mmcam_dbg_warn("list for buffer probe is NULL");
866                 return;
867         }
868
869         _mmcam_dbg_log("start - category : 0x%x", category);
870
871         list = hcamcorder->buffer_probes;
872         while (list) {
873                 item = list->data;
874                 if (!item) {
875                         _mmcam_dbg_err("Remove buffer probe faild, the item is NULL");
876                         list = g_list_next(list);
877                         continue;
878                 }
879
880                 if (item->category & category) {
881                         if (item->object && GST_IS_PAD(item->object)) {
882                                 _mmcam_dbg_log("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
883                                         GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
884                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
885                         } else {
886                                 _mmcam_dbg_warn("Remove buffer probe faild, the pad is null or not pad, just remove item from list and free it");
887                         }
888
889                         list = g_list_next(list);
890                         hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
891                         SAFE_G_FREE(item);
892                 } else {
893                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
894                         list = g_list_next(list);
895                 }
896         }
897
898         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
899                 g_list_free(hcamcorder->buffer_probes);
900                 hcamcorder->buffer_probes = NULL;
901         }
902
903         _mmcam_dbg_log("done");
904
905         return;
906 }
907
908
909 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object)
910 {
911         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
912         GList *list = NULL;
913         MMCamcorderHandlerItem *item = NULL;
914
915         mmf_return_if_fail(hcamcorder);
916
917         if (!hcamcorder->buffer_probes) {
918                 _mmcam_dbg_warn("list for buffer probe is NULL");
919                 return;
920         }
921
922         _mmcam_dbg_log("start - object : %p", object);
923
924         list = hcamcorder->buffer_probes;
925         while (list) {
926                 item = list->data;
927                 if (!item) {
928                         _mmcam_dbg_err("Remove buffer probe faild, the item is NULL");
929                         list = g_list_next(list);
930                         continue;
931                 }
932
933                 if (item->object && item->object == object) {
934                         if (GST_IS_PAD(item->object)) {
935                                 _mmcam_dbg_log("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
936                                         GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
937                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
938                         } else {
939                                 _mmcam_dbg_warn("Remove buffer probe faild, the pad is null or not pad, just remove item from list and free it");
940                         }
941
942                         list = g_list_next(list);
943                         hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
944                         SAFE_G_FREE(item);
945
946                         break;
947                 } else {
948                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
949                         list = g_list_next(list);
950                 }
951         }
952
953         _mmcam_dbg_log("done");
954
955         return;
956 }
957
958
959 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
960 {
961         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
962         GList *list = NULL;
963         MMCamcorderHandlerItem *item = NULL;
964
965         mmf_return_if_fail(hcamcorder);
966
967         if (!hcamcorder->event_probes) {
968                 _mmcam_dbg_warn("list for event probe is NULL");
969                 return;
970         }
971
972         _mmcam_dbg_log("start - category : 0x%x", category);
973
974         list = hcamcorder->event_probes;
975         while (list) {
976                 item = list->data;
977                 if (!item) {
978                         _mmcam_dbg_err("Remove event probe faild, the item is NULL");
979                         list = g_list_next(list);
980                         continue;
981                 }
982
983                 if (item->category & category) {
984                         if (item->object && GST_IS_PAD(item->object)) {
985                                 _mmcam_dbg_log("Remove event probe on [%s:%s] - [ID : %lu], [Category : %x]",
986                                         GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
987                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
988                         } else {
989                                 _mmcam_dbg_warn("Remove event probe faild, the pad is null or not pad, just remove item from list and free it");
990                         }
991
992                         list = g_list_next(list);
993                         hcamcorder->event_probes = g_list_remove(hcamcorder->event_probes, item);
994                         SAFE_G_FREE(item);
995                 } else {
996                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
997                         list = g_list_next(list);
998                 }
999         }
1000
1001         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1002                 g_list_free(hcamcorder->event_probes);
1003                 hcamcorder->event_probes = NULL;
1004         }
1005
1006         _mmcam_dbg_log("done");
1007
1008         return;
1009 }
1010
1011
1012 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category)
1013 {
1014         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1015         GList *list = NULL;
1016         MMCamcorderHandlerItem *item = NULL;
1017
1018         mmf_return_if_fail(hcamcorder);
1019
1020         if (!hcamcorder->signals) {
1021                 _mmcam_dbg_warn("list for signal is NULL");
1022                 return;
1023         }
1024
1025         _mmcam_dbg_log("start - category : 0x%x", category);
1026
1027         list = hcamcorder->signals;
1028         while (list) {
1029                 item = list->data;
1030                 if (!item) {
1031                         _mmcam_dbg_err("Fail to Disconnecting signal, the item is NULL");
1032                         list = g_list_next(list);
1033                         continue;
1034                 }
1035
1036                 if (item->category & category) {
1037                         if (item->object && GST_IS_ELEMENT(item->object)) {
1038                                 if (g_signal_handler_is_connected(item->object, item->handler_id)) {
1039                                         _mmcam_dbg_log("Disconnect signal from [%s] : [ID : %lu], [Category : %x]",
1040                                                 GST_OBJECT_NAME(item->object), item->handler_id, item->category);
1041                                         g_signal_handler_disconnect(item->object, item->handler_id);
1042                                 } else {
1043                                         _mmcam_dbg_warn("Signal was not connected, cannot disconnect it :  [%s]  [ID : %lu], [Category : %x]",
1044                                                 GST_OBJECT_NAME(item->object), item->handler_id, item->category);
1045                                 }
1046                         } else {
1047                                 _mmcam_dbg_err("Fail to Disconnecting signal, the element is null or not element, just remove item from list and free it");
1048                         }
1049
1050                         list = g_list_next(list);
1051                         hcamcorder->signals = g_list_remove(hcamcorder->signals, item);
1052                         SAFE_G_FREE(item);
1053                 } else {
1054                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1055                         list = g_list_next(list);
1056                 }
1057         }
1058
1059         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1060                 g_list_free(hcamcorder->signals);
1061                 hcamcorder->signals = NULL;
1062         }
1063
1064         _mmcam_dbg_log("done");
1065
1066         return;
1067 }
1068
1069
1070 void _mmcamcorder_remove_all_handlers(MMHandleType handle,  _MMCamcorderHandlerCategory category)
1071 {
1072         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1073
1074         _mmcam_dbg_log("ENTER");
1075
1076         if (hcamcorder->signals)
1077                 _mmcamcorder_disconnect_signal((MMHandleType)hcamcorder, category);
1078         if (hcamcorder->event_probes)
1079                 _mmcamcorder_remove_event_probe((MMHandleType)hcamcorder, category);
1080         if (hcamcorder->buffer_probes)
1081                 _mmcamcorder_remove_buffer_probe((MMHandleType)hcamcorder, category);
1082
1083         _mmcam_dbg_log("LEAVE");
1084 }
1085
1086
1087 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was)
1088 {
1089         int i = 0;
1090         _MMCamcorderSubContext *sc = (_MMCamcorderSubContext *)data;
1091
1092         mmf_return_if_fail(sc);
1093         mmf_return_if_fail(sc->element);
1094
1095         for (i = 0 ; i < _MMCAMCORDER_PIPELINE_ELEMENT_NUM ; i++) {
1096                 if (sc->element[i].gst && (G_OBJECT(sc->element[i].gst) == where_the_object_was)) {
1097                         _mmcam_dbg_warn("The element[%d][%p] is finalized", sc->element[i].id, sc->element[i].gst);
1098                         sc->element[i].gst = NULL;
1099                         sc->element[i].id = _MMCAMCORDER_NONE;
1100                         return;
1101                 }
1102         }
1103
1104         mmf_return_if_fail(sc->encode_element);
1105
1106         for (i = 0 ; i < _MMCAMCORDER_ENCODE_PIPELINE_ELEMENT_NUM ; i++) {
1107                 if (sc->encode_element[i].gst && (G_OBJECT(sc->encode_element[i].gst) == where_the_object_was)) {
1108                         _mmcam_dbg_warn("The encode element[%d][%p] is finalized", sc->encode_element[i].id, sc->encode_element[i].gst);
1109                         sc->encode_element[i].gst = NULL;
1110                         sc->encode_element[i].id = _MMCAMCORDER_ENCODE_NONE;
1111                         return;
1112                 }
1113         }
1114
1115         _mmcam_dbg_warn("there is no matching element %p", where_the_object_was);
1116
1117         return;
1118 }
1119
1120
1121 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1122 gboolean _mmcamcorder_msg_callback(void *data)
1123 {
1124         _MMCamcorderMsgItem *item = (_MMCamcorderMsgItem*)data;
1125         mmf_camcorder_t *hcamcorder = NULL;
1126         mmf_return_val_if_fail(item, FALSE);
1127
1128         g_mutex_lock(&item->lock);
1129
1130         hcamcorder = MMF_CAMCORDER(item->handle);
1131         if (hcamcorder == NULL) {
1132                 _mmcam_dbg_warn("msg id:0x%x, item:%p, handle is NULL", item->id, item);
1133                 goto MSG_CALLBACK_DONE;
1134         }
1135
1136         /*_mmcam_dbg_log("msg id:%x, msg_cb:%p, msg_data:%p, item:%p", item->id, hcamcorder->msg_cb, hcamcorder->msg_data, item);*/
1137
1138         _MMCAMCORDER_LOCK((MMHandleType)hcamcorder);
1139
1140         /* remove item from msg data */
1141         if (hcamcorder->msg_data) {
1142                 hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1143         } else {
1144                 _mmcam_dbg_warn("msg_data is NULL but item[%p] will be removed", item);
1145         }
1146
1147         _MMCAMCORDER_UNLOCK((MMHandleType)hcamcorder);
1148
1149         _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1150
1151         if ((hcamcorder) && (hcamcorder->msg_cb))
1152                 hcamcorder->msg_cb(item->id, (MMMessageParamType*)(&(item->param)), hcamcorder->msg_cb_param);
1153
1154         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1155
1156         _MMCAMCORDER_SIGNAL(hcamcorder);
1157
1158 MSG_CALLBACK_DONE:
1159         /* release allocated memory */
1160         if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1161                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1162                 if (cam_fd_info) {
1163                         SAFE_G_FREE(cam_fd_info->face_info);
1164                         SAFE_G_FREE(cam_fd_info);
1165
1166                         item->param.data = NULL;
1167                         item->param.size = 0;
1168                 }
1169         } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1170                 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1171                 if (report) {
1172                         if (report->recording_filename)
1173                                 SAFE_G_FREE(report->recording_filename);
1174
1175                         SAFE_G_FREE(report);
1176                         item->param.data = NULL;
1177                 }
1178         }
1179
1180         g_mutex_unlock(&item->lock);
1181         g_mutex_clear(&item->lock);
1182
1183         SAFE_G_FREE(item);
1184
1185         /* For not being called again */
1186         return FALSE;
1187 }
1188 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1189
1190
1191 gboolean _mmcamcorder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data)
1192 {
1193         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1194 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1195         _MMCamcorderMsgItem *item = NULL;
1196 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1197
1198         mmf_return_val_if_fail(hcamcorder, FALSE);
1199         mmf_return_val_if_fail(data, FALSE);
1200
1201         switch (data->id) {
1202         case MM_MESSAGE_CAMCORDER_STATE_CHANGED:
1203         case MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_ASM:
1204         case MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_RM:
1205                 data->param.union_type = MM_MSG_UNION_STATE;
1206                 break;
1207         case MM_MESSAGE_CAMCORDER_RECORDING_STATUS:
1208                 data->param.union_type = MM_MSG_UNION_RECORDING_STATUS;
1209                 break;
1210         case MM_MESSAGE_CAMCORDER_FIRMWARE_UPDATE:
1211                 data->param.union_type = MM_MSG_UNION_FIRMWARE;
1212                 break;
1213         case MM_MESSAGE_CAMCORDER_CURRENT_VOLUME:
1214                 data->param.union_type = MM_MSG_UNION_REC_VOLUME_DB;
1215                 break;
1216         case MM_MESSAGE_CAMCORDER_TIME_LIMIT:
1217         case MM_MESSAGE_CAMCORDER_MAX_SIZE:
1218         case MM_MESSAGE_CAMCORDER_NO_FREE_SPACE:
1219         case MM_MESSAGE_CAMCORDER_ERROR:
1220         case MM_MESSAGE_CAMCORDER_FOCUS_CHANGED:
1221         case MM_MESSAGE_CAMCORDER_CAPTURED:
1222         case MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED:
1223         case MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED:
1224         case MM_MESSAGE_READY_TO_RESUME:
1225         default:
1226                 data->param.union_type = MM_MSG_UNION_CODE;
1227                 break;
1228         }
1229
1230 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1231         item = g_malloc(sizeof(_MMCamcorderMsgItem));
1232         if (item) {
1233                 memcpy(item, data, sizeof(_MMCamcorderMsgItem));
1234                 item->handle = handle;
1235                 g_mutex_init(&item->lock);
1236
1237                 _MMCAMCORDER_LOCK(handle);
1238                 hcamcorder->msg_data = g_list_append(hcamcorder->msg_data, item);
1239                 /*_mmcam_dbg_log("item[%p]", item);*/
1240
1241                 /* Use DEFAULT priority */
1242                 g_idle_add_full(G_PRIORITY_DEFAULT, _mmcamcorder_msg_callback, item, NULL);
1243
1244                 _MMCAMCORDER_UNLOCK(handle);
1245         } else {
1246                 _mmcam_dbg_err("item[id:0x%x] malloc failed : %d", data->id, sizeof(_MMCamcorderMsgItem));
1247         }
1248 #else /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1249         _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1250
1251         if (hcamcorder->msg_cb)
1252                 hcamcorder->msg_cb(data->id, (MMMessageParamType*)(&(data->param)), hcamcorder->msg_cb_param);
1253         else
1254                 _mmcam_dbg_log("message callback is NULL. message id %d", data->id);
1255
1256         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1257
1258         /* release allocated memory */
1259         if (data->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1260                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)data->param.data;
1261                 if (cam_fd_info) {
1262                         SAFE_G_FREE(cam_fd_info->face_info);
1263                         SAFE_G_FREE(cam_fd_info);
1264                         data->param.size = 0;
1265                 }
1266         } else if (data->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || data->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1267                 MMCamRecordingReport *report = (MMCamRecordingReport *)data->param.data;
1268                 if (report) {
1269                         SAFE_G_FREE(report->recording_filename);
1270                         data->param.data = NULL;
1271                 }
1272                 SAFE_G_FREE(report);
1273         }
1274 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1275
1276         return TRUE;
1277 }
1278
1279
1280 void _mmcamcorder_remove_message_all(MMHandleType handle)
1281 {
1282         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1283         gboolean ret = TRUE;
1284 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1285         _MMCamcorderMsgItem *item = NULL;
1286         GList *list = NULL;
1287         gint64 end_time = 0;
1288 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1289
1290         mmf_return_if_fail(hcamcorder);
1291
1292         _MMCAMCORDER_LOCK(handle);
1293
1294 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1295         if (!hcamcorder->msg_data) {
1296                 _mmcam_dbg_log("No message data is remained.");
1297         } else {
1298                 list = hcamcorder->msg_data;
1299
1300                 while (list) {
1301                         item = list->data;
1302                         list = g_list_next(list);
1303
1304                         if (!item) {
1305                                 _mmcam_dbg_err("Fail to remove message. The item is NULL");
1306                         } else {
1307                                 if (g_mutex_trylock(&(item->lock))) {
1308                                         ret = g_idle_remove_by_data(item);
1309
1310                                         _mmcam_dbg_log("remove msg item[%p], ret[%d]", item, ret);
1311
1312                                         if (ret == FALSE) {
1313                                                 item->handle = 0;
1314                                                 _mmcam_dbg_warn("failed to remove msg cb for item %p, it will be called later with NULL handle", item);
1315                                         }
1316
1317                                         /* release allocated memory */
1318                                         if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1319                                                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1320                                                 if (cam_fd_info) {
1321                                                         SAFE_G_FREE(cam_fd_info->face_info);
1322                                                         item->param.size = 0;
1323                                                 }
1324                                                 SAFE_G_FREE(cam_fd_info);
1325                                         } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED || item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1326                                                 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1327                                                 if (report)
1328                                                         SAFE_G_FREE(report->recording_filename);
1329
1330                                                 SAFE_G_FREE(report);
1331                                         }
1332
1333                                         hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1334
1335                                         g_mutex_unlock(&(item->lock));
1336
1337                                         if (ret == TRUE) {
1338                                                 g_mutex_clear(&item->lock);
1339
1340                                                 SAFE_G_FREE(item);
1341
1342                                                 _mmcam_dbg_log("remove msg done");
1343                                         }
1344                                 } else {
1345                                         _mmcam_dbg_warn("item lock failed. it's being called...");
1346
1347                                         end_time = g_get_monotonic_time() + (100 * G_TIME_SPAN_MILLISECOND);
1348
1349                                         if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time))
1350                                                 _mmcam_dbg_warn("signal received");
1351                                         else
1352                                                 _mmcam_dbg_warn("timeout");
1353                                 }
1354                         }
1355                 }
1356
1357                 g_list_free(hcamcorder->msg_data);
1358                 hcamcorder->msg_data = NULL;
1359         }
1360 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1361
1362         /* remove idle function for playing capture sound */
1363         do {
1364                 ret = g_idle_remove_by_data(hcamcorder);
1365                 _mmcam_dbg_log("remove idle function for playing capture sound. ret[%d]", ret);
1366         } while (ret);
1367
1368         _MMCAMCORDER_UNLOCK(handle);
1369
1370         return;
1371 }
1372
1373
1374 int _mmcamcorder_get_pixel_format(GstCaps *caps)
1375 {
1376         const GstStructure *structure;
1377         const char *media_type;
1378         GstVideoInfo media_info;
1379         MMPixelFormatType type = 0;
1380         unsigned int fourcc = 0;
1381
1382         mmf_return_val_if_fail(caps != NULL, MM_PIXEL_FORMAT_INVALID);
1383
1384         structure = gst_caps_get_structure(caps, 0);
1385         media_type = gst_structure_get_name(structure);
1386         if (media_type == NULL) {
1387                 _mmcam_dbg_err("failed to get media_type");
1388                 return MM_PIXEL_FORMAT_INVALID;
1389         }
1390
1391         gst_video_info_init(&media_info);
1392
1393         if (!strcmp(media_type, "image/jpeg")) {
1394                 _mmcam_dbg_log("It is jpeg.");
1395                 type = MM_PIXEL_FORMAT_ENCODED;
1396         } else if (!strcmp(media_type, "video/x-raw") &&
1397                    gst_video_info_from_caps(&media_info, caps) &&
1398                    GST_VIDEO_INFO_IS_YUV(&media_info)) {
1399                 _mmcam_dbg_log("It is yuv.");
1400                 fourcc = gst_video_format_to_fourcc(GST_VIDEO_INFO_FORMAT(&media_info));
1401                 type = _mmcamcorder_get_pixtype(fourcc);
1402         } else if (!strcmp(media_type, "video/x-raw") &&
1403                    gst_video_info_from_caps(&media_info, caps) &&
1404                    GST_VIDEO_INFO_IS_RGB(&media_info)) {
1405                 _mmcam_dbg_log("It is rgb.");
1406                 type = MM_PIXEL_FORMAT_RGB888;
1407         } else if (!strcmp(media_type, "video/x-h264")) {
1408                 _mmcam_dbg_log("It is H264");
1409                 type = MM_PIXEL_FORMAT_ENCODED_H264;
1410         } else {
1411                 _mmcam_dbg_err("Not supported format [%s]", media_type);
1412                 type = MM_PIXEL_FORMAT_INVALID;
1413         }
1414
1415         /*_mmcam_dbg_log( "Type [%d]", type );*/
1416
1417         return type;
1418 }
1419
1420 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format)
1421 {
1422         unsigned int fourcc = 0;
1423
1424         /*_mmcam_dbg_log("pixtype(%d)", pixtype);*/
1425
1426         switch (pixtype) {
1427         case MM_PIXEL_FORMAT_NV12:
1428                 if (use_zero_copy_format)
1429                         fourcc = GST_MAKE_FOURCC('S', 'N', '1', '2');
1430                 else
1431                         fourcc = GST_MAKE_FOURCC('N', 'V', '1', '2');
1432
1433                 break;
1434         case MM_PIXEL_FORMAT_NV21:
1435                 if (use_zero_copy_format)
1436                         fourcc = GST_MAKE_FOURCC('S', 'N', '2', '1');
1437                 else
1438                         fourcc = GST_MAKE_FOURCC('N', 'V', '2', '1');
1439
1440                 break;
1441         case MM_PIXEL_FORMAT_YUYV:
1442                 if (use_zero_copy_format)
1443                         fourcc = GST_MAKE_FOURCC('S', 'U', 'Y', 'V');
1444                 else
1445                         fourcc = GST_MAKE_FOURCC('Y', 'U', 'Y', '2');
1446
1447                 break;
1448         case MM_PIXEL_FORMAT_UYVY:
1449                 if (use_zero_copy_format)
1450                         fourcc = GST_MAKE_FOURCC('S', 'Y', 'V', 'Y');
1451                 else
1452                         fourcc = GST_MAKE_FOURCC('U', 'Y', 'V', 'Y');
1453
1454                 break;
1455         case MM_PIXEL_FORMAT_I420:
1456                 if (use_zero_copy_format)
1457                         fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1458                 else
1459                         fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1460
1461                 break;
1462         case MM_PIXEL_FORMAT_YV12:
1463                 fourcc = GST_MAKE_FOURCC('Y', 'V', '1', '2');
1464                 break;
1465         case MM_PIXEL_FORMAT_422P:
1466                 fourcc = GST_MAKE_FOURCC('4', '2', '2', 'P');
1467                 break;
1468         case MM_PIXEL_FORMAT_RGB565:
1469                 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', 'P');
1470                 break;
1471         case MM_PIXEL_FORMAT_RGB888:
1472                 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', ' ');
1473                 break;
1474         case MM_PIXEL_FORMAT_ENCODED:
1475                 if (codectype == MM_IMAGE_CODEC_JPEG) {
1476                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1477                 } else if (codectype == MM_IMAGE_CODEC_JPEG_SRW) {
1478                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: JPEG+SamsungRAW format */
1479                 } else if (codectype == MM_IMAGE_CODEC_SRW) {
1480                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: SamsungRAW format */
1481                 } else if (codectype == MM_IMAGE_CODEC_PNG) {
1482                         fourcc = GST_MAKE_FOURCC('P', 'N', 'G', ' ');
1483                 } else {
1484                         /* Please let us know what other fourcces are. ex) BMP, GIF?*/
1485                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1486                 }
1487                 break;
1488         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1489                 fourcc = GST_MAKE_FOURCC('I', 'T', 'L', 'V');
1490                 break;
1491         case MM_PIXEL_FORMAT_ENCODED_H264:
1492                 fourcc = GST_MAKE_FOURCC('H', '2', '6', '4');
1493                 break;
1494         default:
1495                 _mmcam_dbg_log("Not proper pixel type[%d]. Set default - I420", pixtype);
1496                 if (use_zero_copy_format)
1497                         fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1498                 else
1499                         fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1500
1501                 break;
1502         }
1503
1504         return fourcc;
1505 }
1506
1507
1508 int _mmcamcorder_get_pixtype(unsigned int fourcc)
1509 {
1510         int pixtype = MM_PIXEL_FORMAT_INVALID;
1511         /*
1512         char *pfourcc = (char*)&fourcc;
1513
1514         _mmcam_dbg_log("fourcc(%c%c%c%c)",
1515                                    pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
1516         */
1517
1518         switch (fourcc) {
1519         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
1520         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
1521                 pixtype = MM_PIXEL_FORMAT_NV12;
1522                 break;
1523         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
1524         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
1525                 pixtype = MM_PIXEL_FORMAT_NV21;
1526                 break;
1527         case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
1528         case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
1529         case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
1530                 pixtype = MM_PIXEL_FORMAT_YUYV;
1531                 break;
1532         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
1533         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
1534                 pixtype = MM_PIXEL_FORMAT_UYVY;
1535                 break;
1536         case GST_MAKE_FOURCC('S', '4', '2', '0'):
1537         case GST_MAKE_FOURCC('I', '4', '2', '0'):
1538                 pixtype = MM_PIXEL_FORMAT_I420;
1539                 break;
1540         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
1541                 pixtype = MM_PIXEL_FORMAT_YV12;
1542                 break;
1543         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
1544                 pixtype = MM_PIXEL_FORMAT_422P;
1545                 break;
1546         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
1547                 pixtype = MM_PIXEL_FORMAT_RGB565;
1548                 break;
1549         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
1550                 pixtype = MM_PIXEL_FORMAT_RGB888;
1551                 break;
1552         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
1553         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
1554                 pixtype = MM_PIXEL_FORMAT_ARGB;
1555                 break;
1556         case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
1557         case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
1558         case GST_MAKE_FOURCC('S', 'R', '3', '2'):
1559                 pixtype = MM_PIXEL_FORMAT_RGBA;
1560                 break;
1561         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
1562         case GST_MAKE_FOURCC('P', 'N', 'G', ' '):
1563                 pixtype = MM_PIXEL_FORMAT_ENCODED;
1564                 break;
1565         /*FIXME*/
1566         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
1567                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
1568                 break;
1569         case GST_MAKE_FOURCC('H', '2', '6', '4'):
1570                 pixtype = MM_PIXEL_FORMAT_ENCODED_H264;
1571                 break;
1572         default:
1573                 _mmcam_dbg_log("Not supported fourcc type(%c%c%c%c)", fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
1574                 pixtype = MM_PIXEL_FORMAT_INVALID;
1575                 break;
1576         }
1577
1578         return pixtype;
1579 }
1580
1581
1582 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list)
1583 {
1584         GList *local_list = element_list;
1585         _MMCamcorderGstElement *element = NULL;
1586
1587         mmf_return_val_if_fail(bin && local_list, FALSE);
1588
1589         while (local_list) {
1590                 element = (_MMCamcorderGstElement*)local_list->data;
1591                 if (element && element->gst) {
1592                         if (!gst_bin_add(bin, GST_ELEMENT(element->gst))) {
1593                                 _mmcam_dbg_err("Add element [%s] to bin [%s] FAILED",
1594                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1595                                         GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1596                                 return FALSE;
1597                         } else {
1598                                 _mmcam_dbg_log("Add element [%s] to bin [%s] OK",
1599                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1600                                         GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1601                         }
1602                 }
1603                 local_list = local_list->next;
1604         }
1605
1606         return TRUE;
1607 }
1608
1609 gboolean _mmcamcorder_link_elements(GList *element_list)
1610 {
1611         GList                  *local_list  = element_list;
1612         _MMCamcorderGstElement *element     = NULL;
1613         _MMCamcorderGstElement *pre_element = NULL;
1614
1615         mmf_return_val_if_fail(local_list, FALSE);
1616
1617         pre_element = (_MMCamcorderGstElement*)local_list->data;
1618         local_list = local_list->next;
1619
1620         while (local_list) {
1621                 element = (_MMCamcorderGstElement*)local_list->data;
1622                 if (pre_element && pre_element->gst && element && element->gst) {
1623                         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(pre_element->gst), GST_ELEMENT(element->gst))) {
1624                                 _mmcam_dbg_log("Link [%s] to [%s] OK",
1625                                         GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1626                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1627                         } else {
1628                                 _mmcam_dbg_err("Link [%s] to [%s] FAILED",
1629                                         GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1630                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1631                                 return FALSE;
1632                         }
1633                 }
1634
1635                 pre_element = element;
1636                 local_list = local_list->next;
1637         }
1638
1639         return TRUE;
1640 }
1641
1642 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
1643         unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, unsigned int *dst_length)
1644 {
1645         int ret = TRUE;
1646         int mm_ret = MM_ERROR_NONE;
1647         int input_format = MM_UTIL_IMG_FMT_YUV420;
1648         unsigned char *dst_tmp_data = NULL;
1649
1650         if (!src_data || !dst_data || !dst_width || !dst_height || !dst_length) {
1651                 _mmcam_dbg_err("something is NULL %p,%p,%p,%p,%p", src_data, dst_data, dst_width, dst_height, dst_length);
1652                 return FALSE;
1653         }
1654
1655         /* set input format for mm-util */
1656         switch (src_format) {
1657         case MM_PIXEL_FORMAT_I420:
1658                 input_format = MM_UTIL_IMG_FMT_I420;
1659                 break;
1660         case MM_PIXEL_FORMAT_YV12:
1661                 input_format = MM_UTIL_IMG_FMT_YUV420;
1662                 break;
1663         case MM_PIXEL_FORMAT_NV12:
1664                 input_format = MM_UTIL_IMG_FMT_NV12;
1665                 break;
1666         case MM_PIXEL_FORMAT_YUYV:
1667                 input_format = MM_UTIL_IMG_FMT_YUYV;
1668                 break;
1669         case MM_PIXEL_FORMAT_UYVY:
1670                 input_format = MM_UTIL_IMG_FMT_UYVY;
1671                 break;
1672         case MM_PIXEL_FORMAT_RGB888:
1673                 input_format = MM_UTIL_IMG_FMT_RGB888;
1674                 break;
1675         default:
1676                 _mmcam_dbg_err("NOT supported format", src_format);
1677                 return FALSE;
1678         }
1679
1680         _mmcam_dbg_log("src size %dx%d -> dst size %dx%d", src_width, src_height, *dst_width, *dst_height);
1681
1682         /* get length of resized image */
1683         mm_ret = mm_util_get_image_size(input_format, *dst_width, *dst_height, dst_length);
1684         if (mm_ret != MM_ERROR_NONE) {
1685                 GST_ERROR("mm_util_get_image_size failed 0x%x", ret);
1686                 return FALSE;
1687         }
1688
1689         _mmcam_dbg_log("dst_length : %d", *dst_length);
1690
1691         dst_tmp_data = (unsigned char *)malloc(*dst_length);
1692         if (dst_tmp_data == NULL) {
1693                 _mmcam_dbg_err("failed to alloc dst_thumb_size(size %d)", *dst_length);
1694                 return FALSE;
1695         }
1696
1697         mm_ret = mm_util_resize_image(src_data, src_width, src_height, input_format, dst_tmp_data, dst_width, dst_height);
1698
1699         if (mm_ret != MM_ERROR_NONE) {
1700                 GST_ERROR("mm_util_resize_image failed 0x%x", ret);
1701                 free(dst_tmp_data);
1702                 return FALSE;
1703         }
1704
1705         *dst_data = dst_tmp_data;
1706
1707         _mmcam_dbg_log("resize done %p, %dx%d", *dst_data, *dst_width, *dst_height);
1708
1709         return TRUE;
1710 }
1711
1712
1713 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
1714         int src_format, unsigned int src_length, unsigned int jpeg_quality,
1715         void **result_data, unsigned int *result_length)
1716 {
1717         int ret = 0;
1718         gboolean ret_conv = TRUE;
1719         int jpeg_format = 0;
1720         unsigned char *converted_src = NULL;
1721         unsigned int converted_src_size = 0;
1722
1723         mmf_return_val_if_fail(src_data && result_data && result_length, FALSE);
1724
1725         switch (src_format) {
1726         case MM_PIXEL_FORMAT_NV12:
1727                 //jpeg_format = MM_UTIL_JPEG_FMT_NV12;
1728                 ret_conv = _mmcamcorder_convert_NV12_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1729                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1730                 break;
1731         case MM_PIXEL_FORMAT_NV16:
1732                 jpeg_format = MM_UTIL_JPEG_FMT_NV16;
1733                 converted_src = src_data;
1734                 break;
1735         case MM_PIXEL_FORMAT_NV21:
1736                 jpeg_format = MM_UTIL_JPEG_FMT_NV21;
1737                 converted_src = src_data;
1738                 break;
1739         case MM_PIXEL_FORMAT_YUYV:
1740                 //jpeg_format = MM_UTIL_JPEG_FMT_YUYV;
1741                 ret_conv = _mmcamcorder_convert_YUYV_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1742                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1743                 break;
1744         case MM_PIXEL_FORMAT_UYVY:
1745                 //jpeg_format = MM_UTIL_JPEG_FMT_UYVY;
1746                 ret_conv = _mmcamcorder_convert_UYVY_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1747                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1748                 break;
1749         case MM_PIXEL_FORMAT_I420:
1750                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1751                 converted_src = src_data;
1752                 break;
1753         case MM_PIXEL_FORMAT_RGB888:
1754                 jpeg_format = MM_UTIL_JPEG_FMT_RGB888;
1755                 converted_src = src_data;
1756                 break;
1757         case MM_PIXEL_FORMAT_RGBA:
1758                 jpeg_format = MM_UTIL_JPEG_FMT_RGBA8888;
1759                 converted_src = src_data;
1760                 break;
1761         case MM_PIXEL_FORMAT_ARGB:
1762                 jpeg_format = MM_UTIL_JPEG_FMT_ARGB8888;
1763                 converted_src = src_data;
1764                 break;
1765         case MM_PIXEL_FORMAT_422P:
1766                 jpeg_format = MM_UTIL_JPEG_FMT_YUV422;  // not supported
1767                 return FALSE;
1768         case MM_PIXEL_FORMAT_NV12T:
1769         case MM_PIXEL_FORMAT_YV12:
1770         case MM_PIXEL_FORMAT_RGB565:
1771         case MM_PIXEL_FORMAT_ENCODED:
1772         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1773         default:
1774                 return FALSE;
1775         }
1776
1777         if (ret_conv == FALSE) {
1778                 if (converted_src &&
1779                     converted_src != src_data) {
1780                         free(converted_src);
1781                         converted_src = NULL;
1782                 }
1783                 _mmcam_dbg_err("color convert error source format[%d], jpeg format[%d]", src_format, jpeg_format);
1784                 return FALSE;
1785         }
1786
1787         ret = mm_util_jpeg_encode_to_memory(result_data, (int *)result_length,
1788                 converted_src, src_width, src_height, jpeg_format, jpeg_quality);
1789
1790         if (converted_src && (converted_src != src_data)) {
1791                 free(converted_src);
1792                 converted_src = NULL;
1793         }
1794
1795         if (ret != MM_ERROR_NONE) {
1796                 _mmcam_dbg_err("No encoder supports %d format, error code %x", src_format, ret);
1797                 return FALSE;
1798         }
1799
1800         return TRUE;
1801 }
1802
1803
1804 /* make UYVY smaller as multiple size. ex: 640x480 -> 320x240 or 160x120 ... */
1805 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
1806         unsigned char **dst, unsigned int dst_width, unsigned int dst_height)
1807 {
1808         unsigned int i = 0;
1809         int j = 0;
1810         int k = 0;
1811         int src_index = 0;
1812         int ratio_width = 0;
1813         int ratio_height = 0;
1814         int line_base = 0;
1815         int line_width = 0;
1816         int jump_width = 0;
1817         unsigned char *result = NULL;
1818
1819         if (src == NULL || dst == NULL) {
1820                 _mmcam_dbg_err("src[%p] or dst[%p] is NULL", src, dst);
1821                 return FALSE;
1822         }
1823
1824         result = (unsigned char *)malloc((dst_width * dst_height)<<1);
1825         if (!result) {
1826                 _mmcam_dbg_err("failed to alloc dst data");
1827                 return FALSE;
1828         }
1829
1830         ratio_width = src_width / dst_width;
1831         ratio_height = src_height / dst_height;
1832         line_width = src_width << 1;
1833         jump_width = ratio_width << 1;
1834
1835         _mmcam_dbg_warn("[src %dx%d] [dst %dx%d] [line width %d] [ratio width %d, height %d]",
1836                 src_width, src_height, dst_width, dst_height, line_width, ratio_width, ratio_height);
1837
1838         for (i = 0 ; i < src_height ; i += ratio_height) {
1839                 line_base = i * line_width;
1840                 for (j = 0 ; j < line_width ; j += jump_width) {
1841                         src_index = line_base + j;
1842                         result[k++] = src[src_index];
1843                         result[k++] = src[src_index+1];
1844
1845                         j += jump_width;
1846                         src_index = line_base + j;
1847                         if (src_index % 4 == 0)
1848                                 result[k++] = src[src_index+2];
1849                         else
1850                                 result[k++] = src[src_index];
1851
1852                         result[k++] = src[src_index+1];
1853                 }
1854         }
1855
1856         *dst = result;
1857
1858         _mmcam_dbg_warn("converting done - result %p", result);
1859
1860         return TRUE;
1861 }
1862
1863
1864 static guint16 get_language_code(const char *str)
1865 {
1866         return (guint16)(((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1867 }
1868
1869 static gchar * str_to_utf8(const gchar *str)
1870 {
1871         return g_convert(str, -1, "UTF-8", "ASCII", NULL, NULL, NULL);
1872 }
1873
1874 static inline gboolean write_tag(FILE *f, const gchar *tag)
1875 {
1876         while (*tag)
1877                 FPUTC_CHECK(*tag++, f);
1878
1879         return TRUE;
1880 }
1881
1882 static inline gboolean write_to_32(FILE *f, guint val)
1883 {
1884         FPUTC_CHECK(val >> 24, f);
1885         FPUTC_CHECK(val >> 16, f);
1886         FPUTC_CHECK(val >> 8, f);
1887         FPUTC_CHECK(val, f);
1888         return TRUE;
1889 }
1890
1891 static inline gboolean write_to_16(FILE *f, guint val)
1892 {
1893         FPUTC_CHECK(val >> 8, f);
1894         FPUTC_CHECK(val, f);
1895         return TRUE;
1896 }
1897
1898 static inline gboolean write_to_24(FILE *f, guint val)
1899 {
1900         write_to_16(f, val >> 8);
1901         FPUTC_CHECK(val, f);
1902         return TRUE;
1903 }
1904
1905 void *_mmcamcorder_util_task_thread_func(void *data)
1906 {
1907         int ret = MM_ERROR_NONE;
1908         mmf_camcorder_t *hcamcorder = (mmf_camcorder_t *)data;
1909
1910         if (!hcamcorder) {
1911                 _mmcam_dbg_err("handle is NULL");
1912                 return NULL;
1913         }
1914
1915         _mmcam_dbg_warn("start thread");
1916
1917         g_mutex_lock(&hcamcorder->task_thread_lock);
1918
1919         while (hcamcorder->task_thread_state != _MMCAMCORDER_TASK_THREAD_STATE_EXIT) {
1920                 switch (hcamcorder->task_thread_state) {
1921                 case _MMCAMCORDER_TASK_THREAD_STATE_NONE:
1922                         _mmcam_dbg_warn("wait for task signal");
1923                         g_cond_wait(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock);
1924                         _mmcam_dbg_warn("task signal received : state %d", hcamcorder->task_thread_state);
1925                         break;
1926                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_PLAY_START:
1927                         _mmcamcorder_sound_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE02, FALSE);
1928                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1929                         break;
1930                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_SOLO_PLAY_START:
1931                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE01, FALSE);
1932                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1933                         break;
1934                 case _MMCAMCORDER_TASK_THREAD_STATE_ENCODE_PIPE_CREATE:
1935                         ret = _mmcamcorder_video_prepare_record((MMHandleType)hcamcorder);
1936
1937                         /* Play record start sound */
1938                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_REC_START, FALSE);
1939
1940                         _mmcam_dbg_log("_mmcamcorder_video_prepare_record return 0x%x", ret);
1941                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1942                         break;
1943                 case _MMCAMCORDER_TASK_THREAD_STATE_CHECK_CAPTURE_IN_RECORDING:
1944                         {
1945                                 gint64 end_time = 0;
1946
1947                                 _mmcam_dbg_warn("wait for capture data in recording. wait signal...");
1948
1949                                 end_time = g_get_monotonic_time() + (5 * G_TIME_SPAN_SECOND);
1950
1951                                 if (g_cond_wait_until(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock, end_time)) {
1952                                         _mmcam_dbg_warn("signal received");
1953                                 } else {
1954                                         _MMCamcorderMsgItem message;
1955
1956                                         memset(&message, 0x0, sizeof(_MMCamcorderMsgItem));
1957
1958                                         _mmcam_dbg_err("capture data wait time out, send error message");
1959
1960                                         message.id = MM_MESSAGE_CAMCORDER_ERROR;
1961                                         message.param.code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
1962
1963                                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &message);
1964
1965                                         hcamcorder->capture_in_recording = FALSE;
1966                                 }
1967
1968                                 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1969                         }
1970                         break;
1971                 default:
1972                         _mmcam_dbg_warn("invalid task thread state %d", hcamcorder->task_thread_state);
1973                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
1974                         break;
1975                 }
1976         }
1977
1978         g_mutex_unlock(&hcamcorder->task_thread_lock);
1979
1980         _mmcam_dbg_warn("exit thread");
1981
1982         return NULL;
1983 }
1984
1985 #ifdef _USE_YUV_TO_RGB888_
1986 static gboolean
1987 _mmcamcorder_convert_YUV_to_RGB888(unsigned char *src, int src_fmt, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
1988 {
1989         int ret = 0;
1990         int src_cs = MM_UTIL_IMG_FMT_UYVY;
1991         int dst_cs = MM_UTIL_IMG_FMT_RGB888;
1992         unsigned int dst_size = 0;
1993
1994         if (src_fmt == COLOR_FORMAT_YUYV) {
1995                 _mmcam_dbg_log("Convert YUYV to RGB888\n");
1996                 src_cs = MM_UTIL_IMG_FMT_YUYV;
1997         } else if (src_fmt == COLOR_FORMAT_UYVY) {
1998                 _mmcam_dbg_log("Convert UYVY to RGB888\n");
1999                 src_cs = MM_UTIL_IMG_FMT_UYVY;
2000         } else if (src_fmt == COLOR_FORMAT_NV12) {
2001                 _mmcam_dbg_log("Convert NV12 to RGB888\n");
2002                 src_cs = MM_UTIL_IMG_FMT_NV12;
2003         } else {
2004                 _mmcam_dbg_err("NOT supported format [%d]\n", src_fmt);
2005                 return FALSE;
2006         }
2007
2008         ret = mm_util_get_image_size(dst_cs, width, height, &dst_size);
2009         if (ret != 0) {
2010                 _mmcam_dbg_err("mm_util_get_image_size failed [%x]\n", ret);
2011                 return FALSE;
2012         }
2013
2014         *dst = malloc(dst_size);
2015         if (*dst == NULL) {
2016                 _mmcam_dbg_err("malloc failed\n");
2017                 return FALSE;
2018         }
2019
2020         *dst_len = dst_size;
2021         ret = mm_util_convert_colorspace(src, width, height, src_cs, *dst, dst_cs);
2022         if (ret == 0) {
2023                 _mmcam_dbg_log("Convert [dst_size:%d] OK.\n", dst_size);
2024                 return TRUE;
2025         } else {
2026                 free(*dst);
2027                 *dst = NULL;
2028
2029                 _mmcam_dbg_err("Convert [size:%d] FAILED.\n", dst_size);
2030                 return FALSE;
2031         }
2032 }
2033 #endif /* _USE_YUV_TO_RGB888_ */
2034
2035
2036 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2037 {
2038         unsigned int i = 0;
2039         int j = 0;
2040         int src_offset = 0;
2041         int dst_y_offset = 0;
2042         int dst_u_offset = 0;
2043         int dst_v_offset = 0;
2044         int loop_length = 0;
2045         unsigned int dst_size = 0;
2046         unsigned char *dst_data = NULL;
2047
2048         if (!src || !dst || !dst_len) {
2049                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2050                 return FALSE;
2051         }
2052
2053         dst_size = (width * height * 3) >> 1;
2054
2055         _mmcam_dbg_log("YUVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2056
2057         dst_data = (unsigned char *)malloc(dst_size);
2058         if (!dst_data) {
2059                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2060                 return FALSE;
2061         }
2062
2063         loop_length = width << 1;
2064         dst_u_offset = width * height;
2065         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2066
2067         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2068
2069         for (i = 0 ; i < height ; i++) {
2070                 for (j = 0 ; j < loop_length ; j += 2) {
2071                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2072
2073                         if (i % 2 == 0) {
2074                                 if (j % 4 == 0)
2075                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2076                                 else
2077                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2078                         } else {
2079                                 src_offset++;
2080                         }
2081                 }
2082         }
2083
2084         *dst = dst_data;
2085         *dst_len = dst_size;
2086
2087         _mmcam_dbg_log("DONE: YUVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2088
2089         return TRUE;
2090 }
2091
2092
2093 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2094 {
2095         unsigned int i = 0;
2096         int j = 0;
2097         int src_offset = 0;
2098         int dst_y_offset = 0;
2099         int dst_u_offset = 0;
2100         int dst_v_offset = 0;
2101         int loop_length = 0;
2102         unsigned int dst_size = 0;
2103         unsigned char *dst_data = NULL;
2104
2105         if (!src || !dst || !dst_len) {
2106                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2107                 return FALSE;
2108         }
2109
2110         dst_size = (width * height * 3) >> 1;
2111
2112         _mmcam_dbg_log("UYVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2113
2114         dst_data = (unsigned char *)malloc(dst_size);
2115         if (!dst_data) {
2116                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2117                 return FALSE;
2118         }
2119
2120         loop_length = width << 1;
2121         dst_u_offset = width * height;
2122         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2123
2124         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2125
2126         for (i = 0 ; i < height ; i++) {
2127                 for (j = 0 ; j < loop_length ; j += 2) {
2128                         if (i % 2 == 0) {
2129                                 if (j % 4 == 0)
2130                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2131                                 else
2132                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2133                         } else {
2134                                 src_offset++;
2135                         }
2136
2137                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2138                 }
2139         }
2140
2141         *dst = dst_data;
2142         *dst_len = dst_size;
2143
2144         _mmcam_dbg_log("DONE: UYVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2145
2146         return TRUE;
2147 }
2148
2149
2150 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2151 {
2152         int i = 0;
2153         int src_offset = 0;
2154         int dst_y_offset = 0;
2155         int dst_u_offset = 0;
2156         int dst_v_offset = 0;
2157         int loop_length = 0;
2158         unsigned int dst_size = 0;
2159         unsigned char *dst_data = NULL;
2160
2161         if (!src || !dst || !dst_len) {
2162                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2163                 return FALSE;
2164         }
2165
2166         dst_size = (width * height * 3) >> 1;
2167
2168         _mmcam_dbg_log("NV12 -> I420 : %dx%d, dst size %d", width, height, dst_size);
2169
2170         dst_data = (unsigned char *)malloc(dst_size);
2171         if (!dst_data) {
2172                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2173                 return FALSE;
2174         }
2175
2176         loop_length = width << 1;
2177         dst_u_offset = width * height;
2178         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2179
2180         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2181
2182         /* memcpy Y */
2183         memcpy(dst_data, src, dst_u_offset);
2184
2185         loop_length = dst_u_offset >> 1;
2186         src_offset = dst_u_offset;
2187
2188         /* set U and V */
2189         for (i = 0 ; i < loop_length ; i++) {
2190                 if (i % 2 == 0)
2191                         dst_data[dst_u_offset++] = src[src_offset++];
2192                 else
2193                         dst_data[dst_v_offset++] = src[src_offset++];
2194         }
2195
2196         *dst = dst_data;
2197         *dst_len = dst_size;
2198
2199         _mmcam_dbg_log("DONE: NV12 -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2200
2201         return TRUE;
2202 }