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