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