Merge "Support using wl_surface for legacy_player_test" into tizen
[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         while (!player->capture_thread_exit)
319         {
320                 LOGD("capture thread started. waiting for signal");
321
322                 g_mutex_lock(&player->capture_thread_mutex);
323                 g_cond_wait(&player->capture_thread_cond, &player->capture_thread_mutex );
324
325                 if ( player->capture_thread_exit )
326                 {
327                         LOGD("exiting capture thread");
328                         goto EXIT;
329                 }
330                 LOGD("capture thread is recieved signal");
331
332                 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
333                  * Because deadlock can be happened if other player api is used in message callback.
334                  */
335                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
336                 {
337                         /* Colorspace conversion : NV12T-> NV12-> RGB888 */
338                         int ret = 0;
339                         int linear_y_plane_size;
340                         int linear_uv_plane_size;
341                         int width = player->captured.width[0];
342                         int height = player->captured.height[0];
343                         unsigned char * src_buffer = NULL;
344
345                         linear_y_plane_size = (width * height);
346                         linear_uv_plane_size = (width * height / 2);
347
348                         linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
349                         if (linear_y_plane == NULL)
350                         {
351                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
352                                 goto ERROR;
353                         }
354
355                         linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
356                         if (linear_uv_plane == NULL)
357                         {
358                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
359                                 goto ERROR;
360                         }
361                         /* NV12 tiled to linear */
362                         __csc_tiled_to_linear_crop(linear_y_plane,
363                                         player->captured.data[0], width, height, 0,0,0,0);
364                         __csc_tiled_to_linear_crop(linear_uv_plane,
365                                         player->captured.data[1], width, height / 2, 0,0,0,0);
366
367                         MMPLAYER_FREEIF(player->captured.data[0]);
368                         MMPLAYER_FREEIF(player->captured.data[1]);
369
370                         src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
371
372                         if (src_buffer == NULL)
373                         {
374                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
375                                 goto ERROR;
376                         }
377                         memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
378                         memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
379                         memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
380
381                         /* NV12 linear to RGB888 */
382                         ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
383                                 width, height, MM_UTIL_IMG_FMT_RGB888);
384
385                         if (ret != MM_ERROR_NONE)
386                         {
387                                 LOGE("failed to convert nv12 linear");
388                                 goto ERROR;
389                         }
390                         /* clean */
391                         MMPLAYER_FREEIF(src_buffer);
392                         MMPLAYER_FREEIF(linear_y_plane);
393                         MMPLAYER_FREEIF(linear_uv_plane);
394                 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
395                         #define MM_ALIGN(x, a)       (((x) + (a) - 1) & ~((a) - 1))
396                         int ret = 0;
397                         char *src_buffer = NULL;
398                         /* using original width otherwises, app can't know aligned to resize */
399                         int width_align = player->captured.width[0];
400                         int y_size = width_align * player->captured.height[0];
401                         int uv_size = width_align * player->captured.height[1];
402                         int src_buffer_size = y_size + uv_size;
403                         int i, j;
404                         char*temp = NULL;
405                         char*dst_buf = NULL;
406
407                         if (!src_buffer_size) {
408                                 LOGE("invalid data size");
409                                 goto ERROR;
410                         }
411
412                         src_buffer = (char*) g_try_malloc(src_buffer_size);
413
414                         if (!src_buffer) {
415                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
416                                 goto ERROR;
417                         }
418
419                         memset(src_buffer, 0x00, src_buffer_size);
420
421                         temp = player->captured.data[0];
422                         dst_buf = src_buffer;
423
424                         /* set Y plane */
425                         for (i = 0; i < player->captured.height[0]; i++) {
426                                 memcpy(dst_buf, temp, width_align);
427                                 dst_buf += width_align;
428                                 temp += player->captured.stride_width[0];
429                         }
430
431                         temp = player->captured.data[1];
432
433                         /* set UV plane*/
434                         for (j = 0; j < player->captured.height[1]; j++) {
435                                 memcpy(dst_buf, temp, width_align);
436                                 dst_buf += width_align;
437                                 temp += player->captured.stride_width[1];
438                         }
439
440                         /* free captured buf */
441                         MMPLAYER_FREEIF(player->captured.data[0]);
442                         MMPLAYER_FREEIF(player->captured.data[1]);
443
444                         /* NV12 -> RGB888 */
445                         ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
446                                 width_align, player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
447                         if (ret != MM_ERROR_NONE)
448                         {
449                                 LOGE("failed to convert nv12 linear");
450                                 goto ERROR;
451                         }
452                 }
453
454                 ret = _mmplayer_get_video_rotate_angle ((MMHandleType)player, &orientation);
455                 if (ret != MM_ERROR_NONE)
456                 {
457                         LOGE("failed to get rotation angle");
458                         goto ERROR;
459                 }
460
461                 LOGD ("orientation value = %d", orientation);
462
463                 ret = __mmplayer_handle_orientation (player, orientation, MM_UTIL_IMG_FMT_RGB888);
464                 if (ret != MM_ERROR_NONE)
465                 {
466                         LOGE("failed to convert nv12 linear");
467                         goto ERROR;
468                 }
469
470                 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
471                 msg.data = &player->capture;
472                 msg.size = player->capture.size;
473 //              msg.captured_frame.width = player->capture.width;
474 //              msg.captured_frame.height = player->capture.height;
475 //              msg.captured_frame.orientation = player->capture.orientation;
476
477                 if (player->cmd >= MMPLAYER_COMMAND_START)
478                 {
479                         MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_CAPTURED, &msg );
480                         LOGD("returned from capture message callback");
481                 }
482
483                 g_mutex_unlock(&player->capture_thread_mutex);
484
485                 //MMPLAYER_FREEIF(player->capture.data);
486                 continue;
487 ERROR:
488                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
489                 {
490                         /* clean */
491                         MMPLAYER_FREEIF(linear_y_plane);
492                         MMPLAYER_FREEIF(linear_uv_plane);
493                         MMPLAYER_FREEIF(player->captured.data[0]);
494                         MMPLAYER_FREEIF(player->captured.data[1]);
495                 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
496                         MMPLAYER_FREEIF(player->captured.data[0]);
497                         MMPLAYER_FREEIF(player->captured.data[1]);
498                 }
499
500                 msg.union_type = MM_MSG_UNION_CODE;
501
502                 g_mutex_unlock(&player->capture_thread_mutex);
503                 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg );
504         }
505         return NULL;
506 EXIT:
507         g_mutex_unlock(&player->capture_thread_mutex);
508         return NULL;
509 }
510
511 /**
512   * The output is fixed as RGB888
513   */
514 static int
515 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
516 {
517         gint yplane_size = 0;
518         gint uvplane_size = 0;
519         gint src_width = 0;
520         gint src_height = 0;
521         GstCaps *caps = NULL;
522         GstStructure *structure = NULL;
523         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
524         GstMemory *memory = NULL;
525         mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
526         mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
527
528         MMPLAYER_FENTER();
529
530         MMPLAYER_RETURN_VAL_IF_FAIL ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
531         MMPLAYER_RETURN_VAL_IF_FAIL ( buffer, MM_ERROR_INVALID_ARGUMENT );
532
533         /* get fourcc */
534         caps = gst_pad_get_current_caps(pad);
535
536         MMPLAYER_RETURN_VAL_IF_FAIL ( caps, MM_ERROR_INVALID_ARGUMENT );
537         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
538
539         structure = gst_caps_get_structure (caps, 0);
540
541         MMPLAYER_RETURN_VAL_IF_FAIL (structure != NULL, MM_ERROR_PLAYER_INTERNAL);
542
543         /* init capture image buffer */
544         memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
545
546         gst_structure_get_int (structure, "width", &src_width);
547         gst_structure_get_int (structure, "height", &src_height);
548
549         /* check rgb or yuv */
550         if (gst_structure_has_name(structure, "video/x-raw"))
551         {
552                 /* NV12T */
553                 const gchar *gst_format = gst_structure_get_string(structure, "format");
554                 if(!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12"))
555                 {
556                         guint n;
557                         tbm_bo_handle y_hnd;
558                         tbm_bo_handle uv_hnd;
559                         LOGI ("captured format is %s\n", gst_format);
560
561                         MMVideoBuffer *proved = NULL;
562                         if(!g_strcmp0(gst_format, "ST12"))
563                                 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
564                         else
565                                 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
566
567                         /* get video frame info from proved buffer */
568                         n = gst_buffer_n_memory(buffer);
569                         memory = gst_buffer_peek_memory(buffer, n-1);
570                         gst_memory_map(memory, &mapinfo, GST_MAP_READ);
571                         proved = (MMVideoBuffer *)mapinfo.data;
572
573                         if ( !proved || !proved->data[0] || !proved->data[1] ) {
574                                 LOGE("fail to gst_memory_map");
575                                 return MM_ERROR_PLAYER_INTERNAL;
576                         }
577
578                         memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
579
580                         yplane_size = proved->size[0];
581                         uvplane_size = proved->size[1];
582                         LOGI ("yplane_size=%d, uvplane_size=%d", yplane_size, uvplane_size);
583
584                         player->captured.data[0] = g_try_malloc(yplane_size);
585                         if ( !player->captured.data[0] ) {
586                                 gst_memory_unmap(memory, &mapinfo);
587                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
588                         }
589
590 /* FIXME */
591 #if 0
592                         player->captured.data[1] = g_try_malloc(uvplane_size);
593 #else
594                         if(uvplane_size < proved->stride_width[1] * proved->stride_height[1]) {
595                                 player->captured.data[1] =
596                                         g_try_malloc(proved->stride_width[1] * proved->stride_height[1]);
597                         }
598                         else
599                                 player->captured.data[1] = g_try_malloc(uvplane_size);
600 #endif
601                         if ( !player->captured.data[1] ) {
602                                 gst_memory_unmap(memory, &mapinfo);
603                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
604                         }
605
606                         LOGD("Buffer type %d", proved->type);
607                         if(proved->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
608                                 tbm_bo_ref(proved->handle.bo[0]);
609                                 tbm_bo_ref(proved->handle.bo[1]);
610                                 y_hnd = tbm_bo_get_handle(proved->handle.bo[0], TBM_DEVICE_CPU);
611                                 LOGD("source y : %p, size %d", y_hnd.ptr, yplane_size);
612                                 if(y_hnd.ptr) {
613                                         memcpy(player->captured.data[0], y_hnd.ptr, yplane_size);
614                                 }
615
616                                 uv_hnd = tbm_bo_get_handle(proved->handle.bo[1], TBM_DEVICE_CPU);
617                                 LOGD("source uv : %p, size %d",uv_hnd.ptr, uvplane_size);
618                                 if(uv_hnd.ptr) {
619                                         memcpy(player->captured.data[1], uv_hnd.ptr, uvplane_size);
620                                 }
621                                 tbm_bo_unref(proved->handle.bo[0]);
622                                 tbm_bo_unref(proved->handle.bo[1]);
623                         }
624
625                         gst_memory_unmap(memory, &mapinfo);
626
627                         goto DONE;
628                 }
629                 else
630                 {
631                         GstVideoInfo format_info;
632                         gst_video_info_from_caps(&format_info, caps);
633
634                         switch(GST_VIDEO_INFO_FORMAT(&format_info))
635                         {
636                                 case GST_VIDEO_FORMAT_I420:
637                                         src_fmt = MM_UTIL_IMG_FMT_I420;
638                                         break;
639                                 case GST_VIDEO_FORMAT_BGRA:
640                                         src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
641                                         break;
642                                 case GST_VIDEO_FORMAT_BGRx:
643                                         src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
644                                         break;
645                                 default:
646                                         goto UNKNOWN;
647                                         break;
648                         }
649                 }
650         }
651         else
652         {
653                 goto UNKNOWN;
654         }
655
656         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
657         __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
658         gst_buffer_unmap(buffer, &mapinfo);
659
660 DONE:
661         /* do convert colorspace */
662         g_cond_signal( &player->capture_thread_cond );
663
664         MMPLAYER_FLEAVE();
665
666         return MM_ERROR_NONE;
667
668 UNKNOWN:
669         LOGE("unknown format to capture\n");
670         return MM_ERROR_PLAYER_INTERNAL;
671 }
672
673 static GstPadProbeReturn
674 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
675 {
676         mm_player_t* player = (mm_player_t*) u_data;
677         GstBuffer *buffer = NULL;
678         int ret = MM_ERROR_NONE;
679
680         MMPLAYER_RETURN_VAL_IF_FAIL (info->data, GST_PAD_PROBE_REMOVE);
681         MMPLAYER_FENTER();
682
683         buffer = gst_pad_probe_info_get_buffer(info);
684         ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
685
686         if ( ret != MM_ERROR_NONE)
687         {
688                 LOGE("failed to get video frame");
689                 return GST_PAD_PROBE_REMOVE;
690         }
691
692         /* remove probe to be called at one time */
693         if (player->video_capture_cb_probe_id)
694         {
695                 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
696                 player->video_capture_cb_probe_id = 0;
697         }
698
699         MMPLAYER_FLEAVE();
700
701         return GST_PAD_PROBE_OK;
702 }
703
704 static int
705 __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)
706 {
707         unsigned char *dst_data = NULL;
708         unsigned int dst_size;
709         int ret = MM_ERROR_NONE;
710
711         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
712         ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
713
714         if (ret != MM_ERROR_NONE)
715         {
716                 LOGE("failed to get image size for capture, %d\n", ret);
717                 return MM_ERROR_PLAYER_INTERNAL;
718         }
719
720         SECURE_LOGD("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
721
722         dst_data = (unsigned char*)g_malloc0(dst_size);
723
724         if (!dst_data)
725         {
726                 LOGE("no free space to capture\n");
727                 g_free(dst_data);
728                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
729         }
730
731         ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
732
733         if (ret != MM_ERROR_NONE)
734         {
735                 LOGE("failed to convert for capture, %d\n", ret);
736                 g_free(dst_data);
737                 return MM_ERROR_PLAYER_INTERNAL;
738         }
739
740         player->capture.size = dst_size;
741         player->capture.data = dst_data;
742
743         return MM_ERROR_NONE;
744 }
745
746 /*
747  * Get tiled address of position(x,y)
748  *
749  * @param x_size
750  *   width of tiled[in]
751  *
752  * @param y_size
753  *   height of tiled[in]
754  *
755  * @param x_pos
756  *   x position of tield[in]
757  *
758  * @param src_size
759  *   y position of tield[in]
760  *
761  * @return
762  *   address of tiled data
763  */
764 static int
765 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
766 {
767     int pixel_x_m1, pixel_y_m1;
768     int roundup_x;
769     int linear_addr0, linear_addr1, bank_addr ;
770     int x_addr;
771     int trans_addr;
772
773     pixel_x_m1 = x_size -1;
774     pixel_y_m1 = y_size -1;
775
776     roundup_x = ((pixel_x_m1 >> 7) + 1);
777
778     x_addr = x_pos >> 2;
779
780     if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
781         (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
782         linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
783         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
784
785         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
786             bank_addr = ((x_addr >> 4) & 0x1);
787         else
788             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
789     } else {
790         linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
791         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
792
793         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
794             bank_addr = ((x_addr >> 4) & 0x1);
795         else
796             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
797     }
798
799     linear_addr0 = linear_addr0 << 2;
800     trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
801
802     return trans_addr;
803 }
804
805 /*
806  * Converts tiled data to linear
807  * Crops left, top, right, buttom
808  * 1. Y of NV12T to Y of YUV420P
809  * 2. Y of NV12T to Y of YUV420S
810  * 3. UV of NV12T to UV of YUV420S
811  *
812  * @param yuv420_dest
813  *   Y or UV plane address of YUV420[out]
814  *
815  * @param nv12t_src
816  *   Y or UV plane address of NV12T[in]
817  *
818  * @param yuv420_width
819  *   Width of YUV420[in]
820  *
821  * @param yuv420_height
822  *   Y: Height of YUV420, UV: Height/2 of YUV420[in]
823  *
824  * @param left
825  *   Crop size of left
826  *
827  * @param top
828  *   Crop size of top
829  *
830  * @param right
831  *   Crop size of right
832  *
833  * @param buttom
834  *   Crop size of buttom
835  */
836 static void
837 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
838                                 int left, int top, int right, int buttom)
839 {
840     int i, j;
841     int tiled_offset = 0, tiled_offset1 = 0;
842     int linear_offset = 0;
843     int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
844
845     temp3 = yuv420_width-right;
846     temp1 = temp3-left;
847     /* real width is greater than or equal 256 */
848     if (temp1 >= 256) {
849         for (i=top; i<yuv420_height-buttom; i=i+1) {
850             j = left;
851             temp3 = (j>>8)<<8;
852             temp3 = temp3>>6;
853             temp4 = i>>5;
854             if (temp4 & 0x1) {
855                 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
856                 tiled_offset = temp4-1;
857                 temp1 = ((yuv420_width+127)>>7)<<7;
858                 tiled_offset = tiled_offset*(temp1>>6);
859                 tiled_offset = tiled_offset+temp3;
860                 tiled_offset = tiled_offset+2;
861                 temp1 = (temp3>>2)<<2;
862                 tiled_offset = tiled_offset+temp1;
863                 tiled_offset = tiled_offset<<11;
864                 tiled_offset1 = tiled_offset+2048*2;
865                 temp4 = 8;
866             } else {
867                 temp2 = ((yuv420_height+31)>>5)<<5;
868                 if ((i+32)<temp2) {
869                     /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
870                     temp1 = temp3+2;
871                     temp1 = (temp1>>2)<<2;
872                     tiled_offset = temp3+temp1;
873                     temp1 = ((yuv420_width+127)>>7)<<7;
874                     tiled_offset = tiled_offset+temp4*(temp1>>6);
875                     tiled_offset = tiled_offset<<11;
876                     tiled_offset1 = tiled_offset+2048*6;
877                     temp4 = 8;
878                 } else {
879                     /* even2 fomula: x+x_block_num*y */
880                     temp1 = ((yuv420_width+127)>>7)<<7;
881                     tiled_offset = temp4*(temp1>>6);
882                     tiled_offset = tiled_offset+temp3;
883                     tiled_offset = tiled_offset<<11;
884                     tiled_offset1 = tiled_offset+2048*2;
885                     temp4 = 4;
886                 }
887             }
888
889             temp1 = i&0x1F;
890             tiled_offset = tiled_offset+64*(temp1);
891             tiled_offset1 = tiled_offset1+64*(temp1);
892             temp2 = yuv420_width-left-right;
893             linear_offset = temp2*(i-top);
894             temp3 = ((j+256)>>8)<<8;
895             temp3 = temp3-j;
896             temp1 = left&0x3F;
897             if (temp3 > 192) {
898                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
899                 temp2 = ((left+63)>>6)<<6;
900                 temp3 = ((yuv420_width-right)>>6)<<6;
901                 if (temp2 == temp3) {
902                     temp2 = yuv420_width-right-(64-temp1);
903                 }
904                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
905                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
906                 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
907                 linear_offset = linear_offset+256-temp1;
908             } else if (temp3 > 128) {
909                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
910                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
911                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
912                 linear_offset = linear_offset+192-temp1;
913             } else if (temp3 > 64) {
914                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
915                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
916                 linear_offset = linear_offset+128-temp1;
917             } else if (temp3 > 0) {
918                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
919                 linear_offset = linear_offset+64-temp1;
920             }
921
922             tiled_offset = tiled_offset+temp4*2048;
923             j = (left>>8)<<8;
924             j = j + 256;
925             temp2 = yuv420_width-right-256;
926             for (; j<=temp2; j=j+256) {
927                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
928                 tiled_offset1 = tiled_offset1+temp4*2048;
929                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
930                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
931                 tiled_offset = tiled_offset+temp4*2048;
932                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
933                 linear_offset = linear_offset+256;
934             }
935
936             tiled_offset1 = tiled_offset1+temp4*2048;
937             temp2 = yuv420_width-right-j;
938             if (temp2 > 192) {
939                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
940                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
941                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
942                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
943             } else if (temp2 > 128) {
944                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
945                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
946                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
947             } else if (temp2 > 64) {
948                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
949                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
950             } else {
951                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
952             }
953         }
954     } else if (temp1 >= 64) {
955         for (i=top; i<(yuv420_height-buttom); i=i+1) {
956             j = left;
957             tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
958             temp2 = ((j+64)>>6)<<6;
959             temp2 = temp2-j;
960             linear_offset = temp1*(i-top);
961             temp4 = j&0x3;
962             tiled_offset = tiled_offset+temp4;
963             memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
964             linear_offset = linear_offset+temp2;
965             j = j+temp2;
966             if ((j+64) <= temp3) {
967                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
968                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
969                 linear_offset = linear_offset+64;
970                 j = j+64;
971             }
972             if ((j+64) <= temp3) {
973                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
974                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
975                 linear_offset = linear_offset+64;
976                 j = j+64;
977             }
978             if (j < temp3) {
979                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
980                 temp2 = temp3-j;
981                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
982             }
983         }
984     } else {
985         for (i=top; i<(yuv420_height-buttom); i=i+1) {
986             linear_offset = temp1*(i-top);
987             for (j=left; j<(yuv420_width-right); j=j+2) {
988                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
989                 temp4 = j&0x3;
990                 tiled_offset = tiled_offset+temp4;
991                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
992                 linear_offset = linear_offset+2;
993             }
994         }
995     }
996 }