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