to resolve the Prevent issues
[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                 if(!g_strcmp0(gst_structure_get_string(structure, "format"), "ST12"))
536                 {
537                         debug_msg ("captured format is ST12\n");
538
539                         MMVideoBuffer *proved = NULL;
540                         player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
541
542                         /* get video frame info from proved buffer */
543                         memory = gst_buffer_get_all_memory(buffer);
544                         gst_memory_map(memory, &mapinfo, GST_MAP_READ);
545                         proved = (MMVideoBuffer *)mapinfo.data;
546
547                         if ( !proved || !proved->data[0] || !proved->data[1] )
548                                 return MM_ERROR_PLAYER_INTERNAL;
549
550                         yplane_size = proved->size[0];
551                         uvplane_size = proved->size[1];
552
553                         debug_msg ("yplane_size=%d, uvplane_size=%d\n", yplane_size, uvplane_size);
554                         memset(&player->captured, 0x00, sizeof(MMVideoBuffer));
555                         memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
556
557                         player->captured.data[0] = g_try_malloc(yplane_size);
558                         if ( !player->captured.data[0] ) {
559                                 gst_memory_unmap(memory, &mapinfo);
560                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
561                         }
562
563                         player->captured.data[1] = g_try_malloc(uvplane_size);
564                         if ( !player->captured.data[1] ) {
565                                 gst_memory_unmap(memory, &mapinfo);
566                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
567                         }
568
569                         memcpy(player->captured.data[0], proved->data[0], yplane_size);
570                         memcpy(player->captured.data[1], proved->data[1], uvplane_size);
571
572                         gst_memory_unmap(memory, &mapinfo);
573
574                         goto DONE;
575                 }
576                 else
577                 {
578                         GstVideoInfo format_info;
579                         gst_video_info_from_caps(&format_info, caps);
580
581                         switch(GST_VIDEO_INFO_FORMAT(&format_info))
582                         {
583                                 case GST_VIDEO_FORMAT_I420:
584                                         src_fmt = MM_UTIL_IMG_FMT_I420;
585                                         break;
586                                 case GST_VIDEO_FORMAT_BGRA:
587                                         src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
588                                         break;
589                                 case GST_VIDEO_FORMAT_BGRx:
590                                         src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
591                                         break;
592                                 default:
593                                         goto UNKNOWN;
594                                         break;
595                         }
596                 }
597
598                 #if 0
599                 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
600                 {
601                         MMPlayerMPlaneImage *proved = NULL;
602                         player->video_cs = MM_PLAYER_COLORSPACE_NV12;
603
604                         /* get video frame info from proved buffer */
605                         proved = (MMPlayerMPlaneImage *)GST_BUFFER_MALLOCDATA(buffer);
606
607                         if (!proved || !proved->a[0] || !proved->a[1])
608                                 return MM_ERROR_PLAYER_INTERNAL;
609
610                         memset(&player->captured, 0x00, sizeof(MMPlayerMPlaneImage));
611                         memcpy(&player->captured, proved, sizeof(MMPlayerMPlaneImage));
612
613                         player->captured.y_size = proved->s[0] * proved->h[0]; // must get data including padding
614                         player->captured.uv_size = proved->s[0] * proved->h[1];
615
616                         debug_msg ("y plane_size : %d, uv plane_size : %d", player->captured.y_size, player->captured.uv_size);
617
618                         player->captured.a[0] = g_try_malloc(player->captured.y_size);
619
620                         if ( !player->captured.a[0] ) {
621                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
622                         }
623
624                         player->captured.a[1] = g_try_malloc(player->captured.uv_size);
625
626                         if ( !player->captured.a[1] ) {
627                                 return MM_ERROR_SOUND_NO_FREE_SPACE;
628                         }
629
630                         memcpy(player->captured.a[0], proved->a[0], player->captured.y_size);
631                         memcpy(player->captured.a[1], proved->a[1], player->captured.uv_size);
632
633                         goto DONE;
634                 }
635                 break;
636                 #endif
637         }
638         else
639         {
640                 goto UNKNOWN;
641         }
642
643         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
644         __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
645         gst_buffer_unmap(buffer, &mapinfo);
646
647 DONE:
648         /* do convert colorspace */
649         g_cond_signal( &player->capture_thread_cond );
650
651         MMPLAYER_FLEAVE();
652
653         return MM_ERROR_NONE;
654
655 UNKNOWN:
656         debug_error("unknown format to capture\n");
657         return MM_ERROR_PLAYER_INTERNAL;
658 }
659
660 static GstPadProbeReturn
661 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
662 {
663         mm_player_t* player = (mm_player_t*) u_data;
664         GstBuffer *buffer = NULL;
665         int ret = MM_ERROR_NONE;
666
667         return_val_if_fail (info->data, GST_PAD_PROBE_REMOVE);
668         MMPLAYER_FENTER();
669
670         buffer = gst_pad_probe_info_get_buffer(info);
671         ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
672
673         if ( ret != MM_ERROR_NONE)
674         {
675                 debug_error("failed to get video frame");
676                 return GST_PAD_PROBE_REMOVE;
677         }
678
679         /* remove probe to be called at one time */
680         if (player->video_capture_cb_probe_id)
681         {
682                 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
683                 player->video_capture_cb_probe_id = 0;
684         }
685
686         MMPLAYER_FLEAVE();
687
688         return GST_PAD_PROBE_OK;
689 }
690
691 static int
692 __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)
693 {
694         unsigned char *dst_data = NULL;
695         unsigned int dst_size;
696         int ret = MM_ERROR_NONE;
697
698         return_val_if_fail(player, MM_ERROR_PLAYER_INTERNAL);
699         ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
700
701         if (ret != MM_ERROR_NONE)
702         {
703                 debug_error("failed to get image size for capture, %d\n", ret);
704                 return MM_ERROR_PLAYER_INTERNAL;
705         }
706
707         secure_debug_log("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
708
709         dst_data = (unsigned char*)g_malloc0(dst_size);
710
711         if (!dst_data)
712         {
713                 debug_error("no free space to capture\n");
714                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
715         }
716
717         ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
718
719         if (ret != MM_ERROR_NONE)
720         {
721                 debug_error("failed to convert for capture, %d\n", ret);
722                 return MM_ERROR_PLAYER_INTERNAL;
723         }
724
725         player->capture.size = dst_size;
726         player->capture.data = dst_data;
727
728         return MM_ERROR_NONE;
729 }
730
731 /*
732  * Get tiled address of position(x,y)
733  *
734  * @param x_size
735  *   width of tiled[in]
736  *
737  * @param y_size
738  *   height of tiled[in]
739  *
740  * @param x_pos
741  *   x position of tield[in]
742  *
743  * @param src_size
744  *   y position of tield[in]
745  *
746  * @return
747  *   address of tiled data
748  */
749 static int
750 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
751 {
752     int pixel_x_m1, pixel_y_m1;
753     int roundup_x;
754     int linear_addr0, linear_addr1, bank_addr ;
755     int x_addr;
756     int trans_addr;
757
758     pixel_x_m1 = x_size -1;
759     pixel_y_m1 = y_size -1;
760
761     roundup_x = ((pixel_x_m1 >> 7) + 1);
762
763     x_addr = x_pos >> 2;
764
765     if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
766         (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
767         linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
768         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
769
770         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
771             bank_addr = ((x_addr >> 4) & 0x1);
772         else
773             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
774     } else {
775         linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
776         linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
777
778         if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
779             bank_addr = ((x_addr >> 4) & 0x1);
780         else
781             bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
782     }
783
784     linear_addr0 = linear_addr0 << 2;
785     trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
786
787     return trans_addr;
788 }
789
790 /*
791  * Converts tiled data to linear
792  * Crops left, top, right, buttom
793  * 1. Y of NV12T to Y of YUV420P
794  * 2. Y of NV12T to Y of YUV420S
795  * 3. UV of NV12T to UV of YUV420S
796  *
797  * @param yuv420_dest
798  *   Y or UV plane address of YUV420[out]
799  *
800  * @param nv12t_src
801  *   Y or UV plane address of NV12T[in]
802  *
803  * @param yuv420_width
804  *   Width of YUV420[in]
805  *
806  * @param yuv420_height
807  *   Y: Height of YUV420, UV: Height/2 of YUV420[in]
808  *
809  * @param left
810  *   Crop size of left
811  *
812  * @param top
813  *   Crop size of top
814  *
815  * @param right
816  *   Crop size of right
817  *
818  * @param buttom
819  *   Crop size of buttom
820  */
821 static void
822 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
823                                 int left, int top, int right, int buttom)
824 {
825     int i, j;
826     int tiled_offset = 0, tiled_offset1 = 0;
827     int linear_offset = 0;
828     int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
829
830     temp3 = yuv420_width-right;
831     temp1 = temp3-left;
832     /* real width is greater than or equal 256 */
833     if (temp1 >= 256) {
834         for (i=top; i<yuv420_height-buttom; i=i+1) {
835             j = left;
836             temp3 = (j>>8)<<8;
837             temp3 = temp3>>6;
838             temp4 = i>>5;
839             if (temp4 & 0x1) {
840                 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
841                 tiled_offset = temp4-1;
842                 temp1 = ((yuv420_width+127)>>7)<<7;
843                 tiled_offset = tiled_offset*(temp1>>6);
844                 tiled_offset = tiled_offset+temp3;
845                 tiled_offset = tiled_offset+2;
846                 temp1 = (temp3>>2)<<2;
847                 tiled_offset = tiled_offset+temp1;
848                 tiled_offset = tiled_offset<<11;
849                 tiled_offset1 = tiled_offset+2048*2;
850                 temp4 = 8;
851             } else {
852                 temp2 = ((yuv420_height+31)>>5)<<5;
853                 if ((i+32)<temp2) {
854                     /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
855                     temp1 = temp3+2;
856                     temp1 = (temp1>>2)<<2;
857                     tiled_offset = temp3+temp1;
858                     temp1 = ((yuv420_width+127)>>7)<<7;
859                     tiled_offset = tiled_offset+temp4*(temp1>>6);
860                     tiled_offset = tiled_offset<<11;
861                     tiled_offset1 = tiled_offset+2048*6;
862                     temp4 = 8;
863                 } else {
864                     /* even2 fomula: x+x_block_num*y */
865                     temp1 = ((yuv420_width+127)>>7)<<7;
866                     tiled_offset = temp4*(temp1>>6);
867                     tiled_offset = tiled_offset+temp3;
868                     tiled_offset = tiled_offset<<11;
869                     tiled_offset1 = tiled_offset+2048*2;
870                     temp4 = 4;
871                 }
872             }
873
874             temp1 = i&0x1F;
875             tiled_offset = tiled_offset+64*(temp1);
876             tiled_offset1 = tiled_offset1+64*(temp1);
877             temp2 = yuv420_width-left-right;
878             linear_offset = temp2*(i-top);
879             temp3 = ((j+256)>>8)<<8;
880             temp3 = temp3-j;
881             temp1 = left&0x3F;
882             if (temp3 > 192) {
883                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
884                 temp2 = ((left+63)>>6)<<6;
885                 temp3 = ((yuv420_width-right)>>6)<<6;
886                 if (temp2 == temp3) {
887                     temp2 = yuv420_width-right-(64-temp1);
888                 }
889                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
890                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
891                 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
892                 linear_offset = linear_offset+256-temp1;
893             } else if (temp3 > 128) {
894                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
895                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
896                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
897                 linear_offset = linear_offset+192-temp1;
898             } else if (temp3 > 64) {
899                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
900                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
901                 linear_offset = linear_offset+128-temp1;
902             } else if (temp3 > 0) {
903                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
904                 linear_offset = linear_offset+64-temp1;
905             }
906
907             tiled_offset = tiled_offset+temp4*2048;
908             j = (left>>8)<<8;
909             j = j + 256;
910             temp2 = yuv420_width-right-256;
911             for (; j<=temp2; j=j+256) {
912                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
913                 tiled_offset1 = tiled_offset1+temp4*2048;
914                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
915                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
916                 tiled_offset = tiled_offset+temp4*2048;
917                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
918                 linear_offset = linear_offset+256;
919             }
920
921             tiled_offset1 = tiled_offset1+temp4*2048;
922             temp2 = yuv420_width-right-j;
923             if (temp2 > 192) {
924                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
925                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
926                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
927                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
928             } else if (temp2 > 128) {
929                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
930                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
931                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
932             } else if (temp2 > 64) {
933                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
934                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
935             } else {
936                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
937             }
938         }
939     } else if (temp1 >= 64) {
940         for (i=top; i<(yuv420_height-buttom); i=i+1) {
941             j = left;
942             tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
943             temp2 = ((j+64)>>6)<<6;
944             temp2 = temp2-j;
945             linear_offset = temp1*(i-top);
946             temp4 = j&0x3;
947             tiled_offset = tiled_offset+temp4;
948             memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
949             linear_offset = linear_offset+temp2;
950             j = j+temp2;
951             if ((j+64) <= temp3) {
952                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
953                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
954                 linear_offset = linear_offset+64;
955                 j = j+64;
956             }
957             if ((j+64) <= temp3) {
958                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
959                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
960                 linear_offset = linear_offset+64;
961                 j = j+64;
962             }
963             if (j < temp3) {
964                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
965                 temp2 = temp3-j;
966                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
967             }
968         }
969     } else {
970         for (i=top; i<(yuv420_height-buttom); i=i+1) {
971             linear_offset = temp1*(i-top);
972             for (j=left; j<(yuv420_width-right); j=j+2) {
973                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
974                 temp4 = j&0x3;
975                 tiled_offset = tiled_offset+temp4;
976                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
977                 linear_offset = linear_offset+2;
978             }
979         }
980     }
981 }