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