modify capture muxtex lock handling
[platform/core/multimedia/libmm-player.git] / src / mm_player_capture.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>,
7  * Seungbae Shin <seungbae.shin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 /*===========================================================================================
24 |                                                                                                                                                                                       |
25 |  INCLUDE FILES                                                                                                                                                        |
26 |                                                                                                                                                                                       |
27 ========================================================================================== */
28 #include <dlog.h>
29 #include "mm_player_utils.h"
30 #include "mm_player_capture.h"
31 #include "mm_player_priv.h"
32
33 #include <mm_util_imgp.h>
34 #include <gst/video/video-info.h>
35
36 /*---------------------------------------------------------------------------
37 |    LOCAL VARIABLE DEFINITIONS for internal                                                            |
38 ---------------------------------------------------------------------------*/
39
40 /*---------------------------------------------------------------------------
41 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
42 ---------------------------------------------------------------------------*/
43 static GstPadProbeReturn __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
44 static int  __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer);
45 static gpointer __mmplayer_capture_thread(gpointer data);
46 static void __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height, int left, int top, int right, int buttom);
47 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
48 static int __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_img_format src_fmt, unsigned int src_w, unsigned int src_h, mm_util_img_format dst_fmt);
49
50 /*===========================================================================================
51 |                                                                                                                                                                                       |
52 |  FUNCTION DEFINITIONS                                                                                                                                         |
53 |                                                                                                                                                                                       |
54 ========================================================================================== */
55 int
56 _mmplayer_initialize_video_capture(mm_player_t* player)
57 {
58         MMPLAYER_RETURN_VAL_IF_FAIL ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
59         /* create capture mutex */
60         g_mutex_init(&player->capture_thread_mutex);
61
62         /* create capture cond */
63         g_cond_init(&player->capture_thread_cond);
64
65
66         player->capture_thread_exit = FALSE;
67
68         /* create video capture thread */
69         player->capture_thread =
70                         g_thread_try_new ("capture_thread",__mmplayer_capture_thread, (gpointer)player, NULL);
71
72         if ( ! player->capture_thread )
73         {
74                 goto ERROR;
75         }
76
77         return MM_ERROR_NONE;
78
79 ERROR:
80         /* capture thread */
81         g_mutex_clear(&player->capture_thread_mutex );
82
83         g_cond_clear (&player->capture_thread_cond );
84
85         return MM_ERROR_PLAYER_INTERNAL;
86 }
87
88 int
89 _mmplayer_release_video_capture(mm_player_t* player)
90 {
91         MMPLAYER_RETURN_VAL_IF_FAIL ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
92         /* release capture thread */
93         g_mutex_lock(&player->capture_thread_mutex);
94         player->capture_thread_exit = TRUE;
95         g_cond_signal( &player->capture_thread_cond );
96         g_mutex_unlock(&player->capture_thread_mutex);
97
98         LOGD("waitting for capture thread exit");
99         g_thread_join ( player->capture_thread );
100         g_mutex_clear(&player->capture_thread_mutex );
101         g_cond_clear(&player->capture_thread_cond );
102         LOGD("capture thread released");
103
104         return MM_ERROR_NONE;
105 }
106
107 int
108 _mmplayer_do_video_capture(MMHandleType hplayer)
109 {
110         mm_player_t* player = (mm_player_t*) hplayer;
111         int ret = MM_ERROR_NONE;
112         GstPad *pad = NULL;
113
114         MMPLAYER_FENTER();
115
116         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
117
118         /* capturing or not */
119         if (player->video_capture_cb_probe_id || player->capture.data
120                         || player->captured.data[0] || player->captured.data[1]
121                         )
122         {
123                 LOGW("capturing... we can't do any more");
124                 return MM_ERROR_PLAYER_INVALID_STATE;
125         }
126         gint surface_type = 0;
127         mm_attrs_get_int_by_name (player->attrs, "display_surface_type", &surface_type);
128
129         /* check if video pipeline is linked or not */
130         if (!player->pipeline->videobin)
131         {
132                 if (surface_type == MM_DISPLAY_SURFACE_REMOTE && player->video_fakesink)
133                         LOGD("it is evas surface type.");
134                 else
135                 {
136                         LOGW("not ready to capture");
137                         return MM_ERROR_PLAYER_INVALID_STATE;
138                 }
139         }
140
141         /* check if drm file */
142         if (player->is_drm_file)
143         {
144                 LOGW("not supported in drm file");
145                 return MM_ERROR_PLAYER_DRM_OUTPUT_PROTECTION;
146         }
147
148         if (player->pipeline->videobin)
149                 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink" );
150         else // evas surface
151                 pad = gst_element_get_static_pad (player->video_fakesink, "sink");
152
153         if (player->state != MM_PLAYER_STATE_PLAYING)
154         {
155                 if (player->state == MM_PLAYER_STATE_PAUSED) // get last buffer from video sink
156                 {
157                         GstSample *sample = NULL;
158
159                         gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
160
161                         if (player->pipeline->videobin)
162                                 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
163                         else // evas surface
164                                 g_object_get(player->video_fakesink, "last-sample", &sample, NULL);
165
166                         if (sample)
167                         {
168                                 GstBuffer *buf = NULL;
169                                 buf = gst_sample_get_buffer(sample);
170
171                                 if (buf)
172                                 {
173                                         ret = __mmplayer_get_video_frame_from_buffer(player, pad, buf);
174                                 }
175                                 else
176                                 {
177                                         LOGW("failed to get video frame");
178                                 }
179                                 gst_sample_unref(sample);
180                         }
181                         return ret;
182                 }
183                 else
184                 {
185                         LOGW("invalid state(%d) to capture", player->state);
186                         return MM_ERROR_PLAYER_INVALID_STATE;
187                 }
188         }
189
190         /* register probe */
191         player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
192                 __mmplayer_video_capture_probe, player, NULL);
193
194         gst_object_unref(GST_OBJECT(pad));
195         pad = NULL;
196
197         MMPLAYER_FLEAVE();
198
199         return ret;
200 }
201
202 int
203 __mmplayer_handle_orientation (mm_player_t* player, int orientation, int format)
204 {
205     unsigned char *src_buffer = NULL;
206     int ret = MM_ERROR_NONE;
207     unsigned char *dst_frame = NULL;
208     unsigned int dst_width = 0;
209     unsigned int dst_height = 0;
210     unsigned int dst_size = 0;
211     mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
212
213         player->capture.orientation = orientation;
214
215     if (orientation == 90 || orientation == 270)
216     {
217                 dst_width = player->captured.height[0];
218                 dst_height = player->captured.width[0];
219                 LOGD ("interchange width & height");
220     }
221     else if (orientation == 180)
222     {
223                 dst_width = player->captured.width[0];
224                 dst_height = player->captured.height[0];
225     }
226     else if (orientation == 0)
227     {
228                 LOGE ("no need handle orientation : %d", orientation);
229                 player->capture.width = player->captured.width[0];
230                 player->capture.height = player->captured.height[0];
231                 return MM_ERROR_NONE;
232     }
233     else
234     {
235                 LOGE ("wrong orientation value...");
236     }
237
238     /* height & width will be interchanged for 90 and 270 orientation */
239     ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
240     if (ret != MM_ERROR_NONE)
241     {
242                 LOGE("failed to get destination frame size");
243                 return ret;
244     }
245
246     LOGD ("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
247
248     dst_frame = (unsigned char*) malloc (dst_size);
249     if (!dst_frame)
250     {
251       LOGE("failed to allocate memory");
252       return MM_ERROR_PLAYER_NO_FREE_SPACE;
253     }
254
255     src_buffer = (unsigned char*)player->capture.data;
256
257         /* convert orientation degree into enum here */
258         switch(orientation)
259         {
260                 case 0:
261                         rot_enum = MM_UTIL_ROTATE_0;
262                 break;
263                 case 90:
264                         rot_enum = MM_UTIL_ROTATE_90;
265                 break;
266                 case 180:
267                         rot_enum = MM_UTIL_ROTATE_180;
268                 break;
269                 case 270:
270                         rot_enum = MM_UTIL_ROTATE_270;
271                 break;
272                 default:
273                         LOGE("wrong rotate value");
274                 break;
275         }
276
277     LOGD ("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
278
279     ret = mm_util_rotate_image (src_buffer,
280                         player->captured.width[0], player->captured.height[0], format,
281                         dst_frame, &dst_width, &dst_height, rot_enum);
282     if (ret != MM_ERROR_NONE)
283     {
284                 LOGE("failed to do rotate image");
285                 free(dst_frame);
286                 return ret;
287     }
288
289     LOGD ("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
290
291     g_free (src_buffer);
292
293     player->capture.data = dst_frame;
294     player->capture.size = dst_size;
295         player->capture.orientation = orientation;
296         player->capture.width = dst_width;
297         player->capture.height= dst_height;
298
299     player->captured.width[0] = player->captured.stride_width[0] = dst_width;
300     player->captured.height[0] = player->captured.stride_height[0] = dst_height;
301
302     return ret;
303 }
304
305
306 static gpointer
307 __mmplayer_capture_thread(gpointer data)
308 {
309         mm_player_t* player = (mm_player_t*) data;
310         MMMessageParamType msg = {0, };
311         unsigned char * linear_y_plane = NULL;
312         unsigned char * linear_uv_plane = NULL;
313         int orientation = 0;
314         int ret = 0;
315
316         MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
317
318         g_mutex_lock(&player->capture_thread_mutex);
319         while (!player->capture_thread_exit)
320         {
321                 LOGD("capture thread started. waiting for signal");
322                 g_cond_wait(&player->capture_thread_cond, &player->capture_thread_mutex );
323
324                 if ( player->capture_thread_exit )
325                 {
326                         LOGD("exiting capture thread");
327                         goto EXIT;
328                 }
329                 LOGD("capture thread is received signal");
330
331                 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
332                  * Because deadlock can be happened if other player api is used in message callback.
333                  */
334                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
335                 {
336                         /* Colorspace conversion : NV12T-> NV12-> RGB888 */
337                         int ret = 0;
338                         int linear_y_plane_size;
339                         int linear_uv_plane_size;
340                         int width = player->captured.width[0];
341                         int height = player->captured.height[0];
342                         unsigned char * src_buffer = NULL;
343
344                         linear_y_plane_size = (width * height);
345                         linear_uv_plane_size = (width * height / 2);
346
347                         linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
348                         if (linear_y_plane == NULL)
349                         {
350                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
351                                 goto ERROR;
352                         }
353
354                         linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
355                         if (linear_uv_plane == NULL)
356                         {
357                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
358                                 goto ERROR;
359                         }
360                         /* NV12 tiled to linear */
361                         __csc_tiled_to_linear_crop(linear_y_plane,
362                                         player->captured.data[0], width, height, 0,0,0,0);
363                         __csc_tiled_to_linear_crop(linear_uv_plane,
364                                         player->captured.data[1], width, height / 2, 0,0,0,0);
365
366                         MMPLAYER_FREEIF(player->captured.data[0]);
367                         MMPLAYER_FREEIF(player->captured.data[1]);
368
369                         src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
370
371                         if (src_buffer == NULL)
372                         {
373                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
374                                 goto ERROR;
375                         }
376                         memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
377                         memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
378                         memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
379
380                         /* NV12 linear to RGB888 */
381                         ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
382                                 width, height, MM_UTIL_IMG_FMT_RGB888);
383
384                         if (ret != MM_ERROR_NONE)
385                         {
386                                 LOGE("failed to convert nv12 linear");
387                                 goto ERROR;
388                         }
389                         /* clean */
390                         MMPLAYER_FREEIF(src_buffer);
391                         MMPLAYER_FREEIF(linear_y_plane);
392                         MMPLAYER_FREEIF(linear_uv_plane);
393                 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
394                         #define MM_ALIGN(x, a)       (((x) + (a) - 1) & ~((a) - 1))
395                         int ret = 0;
396                         char *src_buffer = NULL;
397                         /* using original width otherwises, app can't know aligned to resize */
398                         int width_align = player->captured.width[0];
399                         int y_size = width_align * player->captured.height[0];
400                         int uv_size = width_align * player->captured.height[1];
401                         int src_buffer_size = y_size + uv_size;
402                         int i, j;
403                         char*temp = NULL;
404                         char*dst_buf = NULL;
405
406                         if (!src_buffer_size) {
407                                 LOGE("invalid data size");
408                                 goto ERROR;
409                         }
410
411                         src_buffer = (char*) g_try_malloc(src_buffer_size);
412
413                         if (!src_buffer) {
414                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
415                                 goto ERROR;
416                         }
417
418                         memset(src_buffer, 0x00, src_buffer_size);
419
420                         temp = player->captured.data[0];
421                         dst_buf = src_buffer;
422
423                         /* set Y plane */
424                         for (i = 0; i < player->captured.height[0]; i++) {
425                                 memcpy(dst_buf, temp, width_align);
426                                 dst_buf += width_align;
427                                 temp += player->captured.stride_width[0];
428                         }
429
430                         temp = player->captured.data[1];
431
432                         /* set UV plane*/
433                         for (j = 0; j < player->captured.height[1]; j++) {
434                                 memcpy(dst_buf, temp, width_align);
435                                 dst_buf += width_align;
436                                 temp += player->captured.stride_width[1];
437                         }
438
439                         /* free captured buf */
440                         MMPLAYER_FREEIF(player->captured.data[0]);
441                         MMPLAYER_FREEIF(player->captured.data[1]);
442
443                         /* NV12 -> RGB888 */
444                         ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
445                                 width_align, player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
446                         if (ret != MM_ERROR_NONE)
447                         {
448                                 LOGE("failed to convert nv12 linear");
449                                 goto ERROR;
450                         }
451                 }
452
453                 ret = _mmplayer_get_video_rotate_angle ((MMHandleType)player, &orientation);
454                 if (ret != MM_ERROR_NONE)
455                 {
456                         LOGE("failed to get rotation angle");
457                         goto ERROR;
458                 }
459
460                 LOGD ("orientation value = %d", orientation);
461
462                 ret = __mmplayer_handle_orientation (player, orientation, MM_UTIL_IMG_FMT_RGB888);
463                 if (ret != MM_ERROR_NONE)
464                 {
465                         LOGE("failed to convert nv12 linear");
466                         goto ERROR;
467                 }
468
469                 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
470                 msg.data = &player->capture;
471                 msg.size = player->capture.size;
472 //              msg.captured_frame.width = player->capture.width;
473 //              msg.captured_frame.height = player->capture.height;
474 //              msg.captured_frame.orientation = player->capture.orientation;
475
476                 if (player->cmd >= MMPLAYER_COMMAND_START)
477                 {
478                         MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_CAPTURED, &msg );
479                         LOGD("returned from capture message callback");
480                 }
481
482                 //MMPLAYER_FREEIF(player->capture.data);
483                 continue;
484 ERROR:
485                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
486                 {
487                         /* clean */
488                         MMPLAYER_FREEIF(linear_y_plane);
489                         MMPLAYER_FREEIF(linear_uv_plane);
490                         MMPLAYER_FREEIF(player->captured.data[0]);
491                         MMPLAYER_FREEIF(player->captured.data[1]);
492                 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
493                         MMPLAYER_FREEIF(player->captured.data[0]);
494                         MMPLAYER_FREEIF(player->captured.data[1]);
495                 }
496
497                 msg.union_type = MM_MSG_UNION_CODE;
498
499                 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg );
500         }
501
502 EXIT:
503         g_mutex_unlock(&player->capture_thread_mutex);
504         return NULL;
505 }
506
507 /**
508   * The output is fixed as RGB888
509   */
510 static int
511 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
512 {
513         gint yplane_size = 0;
514         gint uvplane_size = 0;
515         gint src_width = 0;
516         gint src_height = 0;
517         GstCaps *caps = NULL;
518         GstStructure *structure = NULL;
519         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
520         GstMemory *memory = NULL;
521         mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
522         mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
523
524         MMPLAYER_FENTER();
525
526         MMPLAYER_RETURN_VAL_IF_FAIL ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
527         MMPLAYER_RETURN_VAL_IF_FAIL ( buffer, MM_ERROR_INVALID_ARGUMENT );
528
529         /* get fourcc */
530         caps = gst_pad_get_current_caps(pad);
531
532         MMPLAYER_RETURN_VAL_IF_FAIL ( caps, MM_ERROR_INVALID_ARGUMENT );
533         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
534
535         structure = gst_caps_get_structure (caps, 0);
536
537         MMPLAYER_RETURN_VAL_IF_FAIL (structure != NULL, MM_ERROR_PLAYER_INTERNAL);
538
539         /* init capture image buffer */
540         memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
541
542         gst_structure_get_int (structure, "width", &src_width);
543         gst_structure_get_int (structure, "height", &src_height);
544
545         /* check rgb or yuv */
546         if (gst_structure_has_name(structure, "video/x-raw"))
547         {
548                 /* NV12T */
549                 const gchar *gst_format = gst_structure_get_string(structure, "format");
550                 if(!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12"))
551                 {
552                         guint n;
553                         tbm_bo_handle y_hnd;
554                         tbm_bo_handle uv_hnd;
555                         LOGI ("captured format is %s\n", gst_format);
556
557                         MMVideoBuffer *proved = NULL;
558                         if(!g_strcmp0(gst_format, "ST12"))
559                                 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
560                         else
561                                 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
562
563                         /* get video frame info from proved buffer */
564                         n = gst_buffer_n_memory(buffer);
565                         memory = gst_buffer_peek_memory(buffer, n-1);
566                         gst_memory_map(memory, &mapinfo, GST_MAP_READ);
567                         proved = (MMVideoBuffer *)mapinfo.data;
568
569                         if ( !proved || !proved->data[0] || !proved->data[1] ) {
570                                 LOGE("fail to gst_memory_map");
571                                 return MM_ERROR_PLAYER_INTERNAL;
572                         }
573
574                         memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
575
576                         yplane_size = proved->size[0];
577                         uvplane_size = proved->size[1];
578                         LOGI ("yplane_size=%d, uvplane_size=%d", yplane_size, uvplane_size);
579
580                         player->captured.data[0] = g_try_malloc(yplane_size);
581                         if ( !player->captured.data[0] ) {
582                                 gst_memory_unmap(memory, &mapinfo);
583                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
584                         }
585
586 /* FIXME */
587 #if 0
588                         player->captured.data[1] = g_try_malloc(uvplane_size);
589 #else
590                         if(uvplane_size < proved->stride_width[1] * proved->stride_height[1]) {
591                                 player->captured.data[1] =
592                                         g_try_malloc(proved->stride_width[1] * proved->stride_height[1]);
593                         }
594                         else
595                                 player->captured.data[1] = g_try_malloc(uvplane_size);
596 #endif
597                         if ( !player->captured.data[1] ) {
598                                 gst_memory_unmap(memory, &mapinfo);
599                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
600                         }
601
602                         LOGD("Buffer type %d", proved->type);
603                         if(proved->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
604                                 tbm_bo_ref(proved->handle.bo[0]);
605                                 tbm_bo_ref(proved->handle.bo[1]);
606                                 y_hnd = tbm_bo_get_handle(proved->handle.bo[0], TBM_DEVICE_CPU);
607                                 LOGD("source y : %p, size %d", y_hnd.ptr, yplane_size);
608                                 if(y_hnd.ptr) {
609                                         memcpy(player->captured.data[0], y_hnd.ptr, yplane_size);
610                                 }
611
612                                 uv_hnd = tbm_bo_get_handle(proved->handle.bo[1], TBM_DEVICE_CPU);
613                                 LOGD("source uv : %p, size %d",uv_hnd.ptr, uvplane_size);
614                                 if(uv_hnd.ptr) {
615                                         memcpy(player->captured.data[1], uv_hnd.ptr, uvplane_size);
616                                 }
617                                 tbm_bo_unref(proved->handle.bo[0]);
618                                 tbm_bo_unref(proved->handle.bo[1]);
619                         }
620
621                         gst_memory_unmap(memory, &mapinfo);
622
623                         goto DONE;
624                 }
625                 else
626                 {
627                         GstVideoInfo format_info;
628                         gst_video_info_from_caps(&format_info, caps);
629
630                         switch(GST_VIDEO_INFO_FORMAT(&format_info))
631                         {
632                                 case GST_VIDEO_FORMAT_I420:
633                                         src_fmt = MM_UTIL_IMG_FMT_I420;
634                                         break;
635                                 case GST_VIDEO_FORMAT_BGRA:
636                                         src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
637                                         break;
638                                 case GST_VIDEO_FORMAT_BGRx:
639                                         src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
640                                         break;
641                                 default:
642                                         goto UNKNOWN;
643                                         break;
644                         }
645                 }
646         }
647         else
648         {
649                 goto UNKNOWN;
650         }
651
652         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
653         __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
654         gst_buffer_unmap(buffer, &mapinfo);
655
656 DONE:
657         /* do convert colorspace */
658         g_cond_signal( &player->capture_thread_cond );
659
660         MMPLAYER_FLEAVE();
661
662         return MM_ERROR_NONE;
663
664 UNKNOWN:
665         LOGE("unknown format to capture\n");
666         return MM_ERROR_PLAYER_INTERNAL;
667 }
668
669 static GstPadProbeReturn
670 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
671 {
672         mm_player_t* player = (mm_player_t*) u_data;
673         GstBuffer *buffer = NULL;
674         int ret = MM_ERROR_NONE;
675
676         MMPLAYER_RETURN_VAL_IF_FAIL (info->data, GST_PAD_PROBE_REMOVE);
677         MMPLAYER_FENTER();
678
679         buffer = gst_pad_probe_info_get_buffer(info);
680         ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
681
682         if ( ret != MM_ERROR_NONE)
683         {
684                 LOGE("failed to get video frame");
685                 return GST_PAD_PROBE_REMOVE;
686         }
687
688         /* remove probe to be called at one time */
689         if (player->video_capture_cb_probe_id)
690         {
691                 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
692                 player->video_capture_cb_probe_id = 0;
693         }
694
695         MMPLAYER_FLEAVE();
696
697         return GST_PAD_PROBE_OK;
698 }
699
700 static int
701 __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_img_format src_fmt, unsigned int src_w, unsigned int src_h, mm_util_img_format dst_fmt)
702 {
703         unsigned char *dst_data = NULL;
704         unsigned int dst_size;
705         int ret = MM_ERROR_NONE;
706
707         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
708         ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
709
710         if (ret != MM_ERROR_NONE)
711         {
712                 LOGE("failed to get image size for capture, %d\n", ret);
713                 return MM_ERROR_PLAYER_INTERNAL;
714         }
715
716         SECURE_LOGD("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
717
718         dst_data = (unsigned char*)g_malloc0(dst_size);
719
720         if (!dst_data)
721         {
722                 LOGE("no free space to capture\n");
723                 g_free(dst_data);
724                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
725         }
726
727         ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
728
729         if (ret != MM_ERROR_NONE)
730         {
731                 LOGE("failed to convert for capture, %d\n", ret);
732                 g_free(dst_data);
733                 return MM_ERROR_PLAYER_INTERNAL;
734         }
735
736         player->capture.size = dst_size;
737         player->capture.data = dst_data;
738
739         return MM_ERROR_NONE;
740 }
741
742 /*
743  * Get tiled address of position(x,y)
744  *
745  * @param x_size
746  *   width of tiled[in]
747  *
748  * @param y_size
749  *   height of tiled[in]
750  *
751  * @param x_pos
752  *   x position of tield[in]
753  *
754  * @param src_size
755  *   y position of tield[in]
756  *
757  * @return
758  *   address of tiled data
759  */
760 static int
761 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
762 {
763     int pixel_x_m1, pixel_y_m1;
764     int roundup_x;
765     int linear_addr0, linear_addr1, bank_addr ;
766     int x_addr;
767     int trans_addr;
768
769     pixel_x_m1 = x_size -1;
770     pixel_y_m1 = y_size -1;
771
772     roundup_x = ((pixel_x_m1 >> 7) + 1);
773
774     x_addr = x_pos >> 2;
775
776     if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
777         (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
778         linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
779         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
780
781         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
782             bank_addr = ((x_addr >> 4) & 0x1);
783         else
784             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
785     } else {
786         linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
787         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
788
789         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
790             bank_addr = ((x_addr >> 4) & 0x1);
791         else
792             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
793     }
794
795     linear_addr0 = linear_addr0 << 2;
796     trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
797
798     return trans_addr;
799 }
800
801 /*
802  * Converts tiled data to linear
803  * Crops left, top, right, buttom
804  * 1. Y of NV12T to Y of YUV420P
805  * 2. Y of NV12T to Y of YUV420S
806  * 3. UV of NV12T to UV of YUV420S
807  *
808  * @param yuv420_dest
809  *   Y or UV plane address of YUV420[out]
810  *
811  * @param nv12t_src
812  *   Y or UV plane address of NV12T[in]
813  *
814  * @param yuv420_width
815  *   Width of YUV420[in]
816  *
817  * @param yuv420_height
818  *   Y: Height of YUV420, UV: Height/2 of YUV420[in]
819  *
820  * @param left
821  *   Crop size of left
822  *
823  * @param top
824  *   Crop size of top
825  *
826  * @param right
827  *   Crop size of right
828  *
829  * @param buttom
830  *   Crop size of buttom
831  */
832 static void
833 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
834                                 int left, int top, int right, int buttom)
835 {
836     int i, j;
837     int tiled_offset = 0, tiled_offset1 = 0;
838     int linear_offset = 0;
839     int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
840
841     temp3 = yuv420_width-right;
842     temp1 = temp3-left;
843     /* real width is greater than or equal 256 */
844     if (temp1 >= 256) {
845         for (i=top; i<yuv420_height-buttom; i=i+1) {
846             j = left;
847             temp3 = (j>>8)<<8;
848             temp3 = temp3>>6;
849             temp4 = i>>5;
850             if (temp4 & 0x1) {
851                 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
852                 tiled_offset = temp4-1;
853                 temp1 = ((yuv420_width+127)>>7)<<7;
854                 tiled_offset = tiled_offset*(temp1>>6);
855                 tiled_offset = tiled_offset+temp3;
856                 tiled_offset = tiled_offset+2;
857                 temp1 = (temp3>>2)<<2;
858                 tiled_offset = tiled_offset+temp1;
859                 tiled_offset = tiled_offset<<11;
860                 tiled_offset1 = tiled_offset+2048*2;
861                 temp4 = 8;
862             } else {
863                 temp2 = ((yuv420_height+31)>>5)<<5;
864                 if ((i+32)<temp2) {
865                     /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
866                     temp1 = temp3+2;
867                     temp1 = (temp1>>2)<<2;
868                     tiled_offset = temp3+temp1;
869                     temp1 = ((yuv420_width+127)>>7)<<7;
870                     tiled_offset = tiled_offset+temp4*(temp1>>6);
871                     tiled_offset = tiled_offset<<11;
872                     tiled_offset1 = tiled_offset+2048*6;
873                     temp4 = 8;
874                 } else {
875                     /* even2 fomula: x+x_block_num*y */
876                     temp1 = ((yuv420_width+127)>>7)<<7;
877                     tiled_offset = temp4*(temp1>>6);
878                     tiled_offset = tiled_offset+temp3;
879                     tiled_offset = tiled_offset<<11;
880                     tiled_offset1 = tiled_offset+2048*2;
881                     temp4 = 4;
882                 }
883             }
884
885             temp1 = i&0x1F;
886             tiled_offset = tiled_offset+64*(temp1);
887             tiled_offset1 = tiled_offset1+64*(temp1);
888             temp2 = yuv420_width-left-right;
889             linear_offset = temp2*(i-top);
890             temp3 = ((j+256)>>8)<<8;
891             temp3 = temp3-j;
892             temp1 = left&0x3F;
893             if (temp3 > 192) {
894                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
895                 temp2 = ((left+63)>>6)<<6;
896                 temp3 = ((yuv420_width-right)>>6)<<6;
897                 if (temp2 == temp3) {
898                     temp2 = yuv420_width-right-(64-temp1);
899                 }
900                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
901                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
902                 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
903                 linear_offset = linear_offset+256-temp1;
904             } else if (temp3 > 128) {
905                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
906                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
907                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
908                 linear_offset = linear_offset+192-temp1;
909             } else if (temp3 > 64) {
910                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
911                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
912                 linear_offset = linear_offset+128-temp1;
913             } else if (temp3 > 0) {
914                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
915                 linear_offset = linear_offset+64-temp1;
916             }
917
918             tiled_offset = tiled_offset+temp4*2048;
919             j = (left>>8)<<8;
920             j = j + 256;
921             temp2 = yuv420_width-right-256;
922             for (; j<=temp2; j=j+256) {
923                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
924                 tiled_offset1 = tiled_offset1+temp4*2048;
925                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
926                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
927                 tiled_offset = tiled_offset+temp4*2048;
928                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
929                 linear_offset = linear_offset+256;
930             }
931
932             tiled_offset1 = tiled_offset1+temp4*2048;
933             temp2 = yuv420_width-right-j;
934             if (temp2 > 192) {
935                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
936                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
937                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
938                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
939             } else if (temp2 > 128) {
940                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
941                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
942                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
943             } else if (temp2 > 64) {
944                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
945                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
946             } else {
947                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
948             }
949         }
950     } else if (temp1 >= 64) {
951         for (i=top; i<(yuv420_height-buttom); i=i+1) {
952             j = left;
953             tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
954             temp2 = ((j+64)>>6)<<6;
955             temp2 = temp2-j;
956             linear_offset = temp1*(i-top);
957             temp4 = j&0x3;
958             tiled_offset = tiled_offset+temp4;
959             memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
960             linear_offset = linear_offset+temp2;
961             j = j+temp2;
962             if ((j+64) <= temp3) {
963                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
964                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
965                 linear_offset = linear_offset+64;
966                 j = j+64;
967             }
968             if ((j+64) <= temp3) {
969                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
970                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
971                 linear_offset = linear_offset+64;
972                 j = j+64;
973             }
974             if (j < temp3) {
975                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
976                 temp2 = temp3-j;
977                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
978             }
979         }
980     } else {
981         for (i=top; i<(yuv420_height-buttom); i=i+1) {
982             linear_offset = temp1*(i-top);
983             for (j=left; j<(yuv420_width-right); j=j+2) {
984                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
985                 temp4 = j&0x3;
986                 tiled_offset = tiled_offset+temp4;
987                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
988                 linear_offset = linear_offset+2;
989             }
990         }
991     }
992 }