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