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