[0.6.92] apply new enum of libmm-utility
[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 //#define       CAPTURE_OUTPUT_DUMP     1
37 /*---------------------------------------------------------------------------
38 |    LOCAL VARIABLE DEFINITIONS for internal                                                            |
39 ---------------------------------------------------------------------------*/
40
41 /*---------------------------------------------------------------------------
42 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
43 ---------------------------------------------------------------------------*/
44 static GstPadProbeReturn __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
45 static int  __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer);
46 static gpointer __mmplayer_capture_thread(gpointer data);
47 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);
48 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
49 static int __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_color_format_e src_fmt, unsigned int src_w, unsigned int src_h, mm_util_color_format_e dst_fmt);
50 #ifdef CAPTURE_OUTPUT_DUMP
51 static void capture_output_dump(mm_player_t* player);
52 #endif
53
54 /*===========================================================================================
55 |                                                                                                                                                                                       |
56 |  FUNCTION DEFINITIONS                                                                                                                                         |
57 |                                                                                                                                                                                       |
58 ========================================================================================== */
59 int
60 _mmplayer_initialize_video_capture(mm_player_t* player)
61 {
62         int ret = MM_ERROR_NONE;
63         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
64         /* create capture mutex */
65         g_mutex_init(&player->capture_thread_mutex);
66
67         /* create capture cond */
68         g_cond_init(&player->capture_thread_cond);
69
70
71         player->capture_thread_exit = FALSE;
72
73         /* create video capture thread */
74         player->capture_thread =
75                         g_thread_try_new("capture_thread", __mmplayer_capture_thread, (gpointer)player, NULL);
76
77         if (!player->capture_thread) {
78                 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
79                 goto ERROR;
80         }
81
82         return ret;
83
84 ERROR:
85         /* capture thread */
86         g_mutex_clear(&player->capture_thread_mutex);
87
88         g_cond_clear(&player->capture_thread_cond);
89
90         return ret;
91 }
92
93 int
94 _mmplayer_release_video_capture(mm_player_t* player)
95 {
96         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
97         /* release capture thread */
98         MMPLAYER_CAPTURE_THREAD_LOCK(player);
99         player->capture_thread_exit = TRUE;
100         MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
101         MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
102
103         LOGD("waitting for capture thread exit");
104         g_thread_join(player->capture_thread);
105         g_mutex_clear(&player->capture_thread_mutex);
106         g_cond_clear(&player->capture_thread_cond);
107         LOGD("capture thread released");
108
109         return MM_ERROR_NONE;
110 }
111
112 int
113 _mmplayer_do_video_capture(MMHandleType hplayer)
114 {
115         mm_player_t* player = (mm_player_t*) hplayer;
116         int ret = MM_ERROR_NONE;
117         GstPad *pad = NULL;
118
119         MMPLAYER_FENTER();
120
121         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
122
123         /* capturing or not */
124         if (player->video_capture_cb_probe_id || player->capture.data
125                         || player->captured.data[0] || player->captured.data[1]) {
126                 LOGW("capturing... we can't do any more");
127                 return MM_ERROR_PLAYER_INVALID_STATE;
128         }
129         gint surface_type = 0;
130         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
131
132         /* check if video pipeline is linked or not */
133         if (!player->pipeline->videobin) {
134                 if (surface_type == MM_DISPLAY_SURFACE_REMOTE && player->video_fakesink) {
135                         LOGD("it is evas surface type.");
136                 } else {
137                         LOGW("not ready to capture");
138                         return MM_ERROR_PLAYER_INVALID_STATE;
139                 }
140         }
141
142         if (player->pipeline->videobin)
143                 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
144         else // evas surface
145                 pad = gst_element_get_static_pad(player->video_fakesink, "sink");
146
147         if (player->state != MM_PLAYER_STATE_PLAYING) {
148                 if (player->state == MM_PLAYER_STATE_PAUSED) {
149                         // get last buffer from video sink
150                         GstSample *sample = NULL;
151
152                         gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
153
154                         if (player->pipeline->videobin)
155                                 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
156                         else // evas surface
157                                 g_object_get(player->video_fakesink, "last-sample", &sample, NULL);
158
159                         if (sample) {
160                                 GstBuffer *buf = NULL;
161                                 buf = gst_sample_get_buffer(sample);
162                                 if (buf) {
163                                         if (__mmplayer_get_video_frame_from_buffer(player, pad, buf) != MM_ERROR_NONE)
164                                                 ret = MM_ERROR_PLAYER_INTERNAL;
165                                 } else {
166                                         LOGW("failed to get video frame");
167                                 }
168                                 gst_sample_unref(sample);
169                         }
170                         return ret;
171                 } else {
172                         LOGW("invalid state(%d) to capture", player->state);
173                         gst_object_unref(GST_OBJECT(pad));
174                         pad = NULL;
175                         return MM_ERROR_PLAYER_INVALID_STATE;
176                 }
177         }
178
179         /* register probe */
180         player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
181                 __mmplayer_video_capture_probe, player, NULL);
182
183         gst_object_unref(GST_OBJECT(pad));
184         pad = NULL;
185
186         MMPLAYER_FLEAVE();
187
188         return ret;
189 }
190
191 int
192 __mmplayer_handle_orientation(mm_player_t* player, int orientation, int format)
193 {
194         unsigned char *src_buffer = NULL;
195         int ret = MM_ERROR_NONE;
196         unsigned char *dst_frame = NULL;
197         unsigned int dst_width = 0;
198         unsigned int dst_height = 0;
199         unsigned int dst_size = 0;
200         mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
201
202         player->capture.orientation = orientation;
203
204         if (orientation == 90 || orientation == 270) {
205                 dst_width = player->captured.height[0];
206                 dst_height = player->captured.width[0];
207                 LOGD("interchange width & height");
208         } else if (orientation == 180) {
209                 dst_width = player->captured.width[0];
210                 dst_height = player->captured.height[0];
211         } else if (orientation == 0) {
212                 LOGE("no need handle orientation : %d", orientation);
213                 player->capture.width = player->captured.width[0];
214                 player->capture.height = player->captured.height[0];
215                 return MM_ERROR_NONE;
216         } else
217                 LOGE("wrong orientation value...");
218
219     /* height & width will be interchanged for 90 and 270 orientation */
220         ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
221         if (ret != MM_ERROR_NONE) {
222                 LOGE("failed to get destination frame size");
223                 return ret;
224         }
225
226         LOGD("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
227
228         dst_frame = (unsigned char*) malloc(dst_size);
229         if (!dst_frame) {
230                 LOGE("failed to allocate memory");
231                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
232         }
233
234         src_buffer = (unsigned char*)player->capture.data;
235
236         /* convert orientation degree into enum here */
237         switch (orientation) {
238         case 90:
239                 rot_enum = MM_UTIL_ROTATE_90;
240                 break;
241         case 180:
242                 rot_enum = MM_UTIL_ROTATE_180;
243                 break;
244         case 270:
245                 rot_enum = MM_UTIL_ROTATE_270;
246                 break;
247         default:
248                 LOGE("wrong rotate value");
249                 break;
250         }
251
252         LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
253
254         ret = mm_util_rotate_image(src_buffer,
255                         player->captured.width[0], player->captured.height[0], format,
256                         dst_frame, &dst_width, &dst_height, rot_enum);
257         if (ret != MM_ERROR_NONE) {
258                 LOGE("failed to do rotate image");
259                 free(dst_frame);
260                 return ret;
261         }
262
263         LOGD("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
264
265         g_free(src_buffer);
266
267         player->capture.data = dst_frame;
268         player->capture.size = dst_size;
269         player->capture.orientation = orientation;
270         player->capture.width = dst_width;
271         player->capture.height = dst_height;
272
273         player->captured.width[0] = player->captured.stride_width[0] = dst_width;
274         player->captured.height[0] = player->captured.stride_height[0] = dst_height;
275
276         return ret;
277 }
278
279
280 static gpointer
281 __mmplayer_capture_thread(gpointer data)
282 {
283         mm_player_t* player = (mm_player_t*) data;
284         MMMessageParamType msg = {0, };
285         unsigned char * src_buffer = NULL;
286         unsigned char * linear_y_plane = NULL;
287         unsigned char * linear_uv_plane = NULL;
288         int orientation = 0;
289         int user_angle = 0;
290         int ret = 0;
291         int planes[MAX_BUFFER_PLANE] = {0, };
292         unsigned char * p_buf = NULL;
293         unsigned char * temp = NULL;
294         int i, j;
295
296         MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
297
298         MMPLAYER_CAPTURE_THREAD_LOCK(player);
299         while (!player->capture_thread_exit) {
300                 LOGD("capture thread started. waiting for signal");
301                 MMPLAYER_CAPTURE_THREAD_WAIT(player);
302
303                 if (player->capture_thread_exit) {
304                         LOGD("exiting capture thread");
305                         goto EXIT;
306                 }
307                 LOGD("capture thread is received signal");
308
309                 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
310                  * Because deadlock can be happened if other player api is used in message callback.
311                  */
312                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
313                         /* Colorspace conversion : NV12T-> NV12-> RGB888 */
314                         int ret = 0;
315                         int linear_y_plane_size;
316                         int linear_uv_plane_size;
317                         int width = player->captured.width[0];
318                         int height = player->captured.height[0];
319
320                         linear_y_plane_size = (width * height);
321                         linear_uv_plane_size = (width * height / 2);
322
323                         linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
324                         if (linear_y_plane == NULL) {
325                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
326                                 goto ERROR;
327                         }
328
329                         linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
330                         if (linear_uv_plane == NULL) {
331                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
332                                 goto ERROR;
333                         }
334                         /* NV12 tiled to linear */
335                         __csc_tiled_to_linear_crop(linear_y_plane,
336                                         player->captured.data[0], width, height, 0, 0, 0, 0);
337                         __csc_tiled_to_linear_crop(linear_uv_plane,
338                                         player->captured.data[1], width, height / 2, 0, 0, 0, 0);
339
340                         for (i = 0; i < player->captured.handle_num; i++) {
341                                 MMPLAYER_FREEIF(player->captured.data[i]);
342                         }
343
344                         src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
345
346                         if (src_buffer == NULL) {
347                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
348                                 goto ERROR;
349                         }
350                         memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
351                         memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
352                         memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
353
354                         /* NV12 linear to RGB888 */
355                         ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_COLOR_NV12,
356                                 width, height, MM_UTIL_COLOR_RGB24);
357
358                         if (ret != MM_ERROR_NONE) {
359                                 LOGE("failed to convert nv12 linear");
360                                 goto ERROR;
361                         }
362                         /* clean */
363                         MMPLAYER_FREEIF(src_buffer);
364                         MMPLAYER_FREEIF(linear_y_plane);
365                         MMPLAYER_FREEIF(linear_uv_plane);
366                 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
367                         #define MM_ALIGN(x, a)      (((x) +(a) - 1) & ~((a) - 1))
368                         int ret = 0;
369                         /* using original width otherwises, app can't know aligned to resize */
370                         planes[0] =  player->captured.stride_width[0] * player->captured.stride_height[0];
371                         planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
372                         int src_buffer_size = planes[0] + planes[1];
373                         src_buffer = (unsigned char*) g_try_malloc(src_buffer_size);
374                         p_buf = src_buffer;
375
376                         if (!src_buffer_size) {
377                                 LOGE("invalid data size");
378                                 goto ERROR;
379                         }
380
381                         if (!src_buffer) {
382                                 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
383                                 goto ERROR;
384                         }
385
386                         memset(src_buffer, 0x00, src_buffer_size);
387
388                         temp = player->captured.data[0];
389
390                         /* set Y plane */
391                         for (i = 0; i < player->captured.height[0]; i++) {
392                                 memcpy(p_buf, temp, player->captured.width[0]);
393                                 p_buf += player->captured.width[0];
394                                 temp += player->captured.stride_width[0];
395                         }
396
397                         temp = player->captured.data[1];
398
399                         /* set UV plane*/
400                         for (j = 0; j < player->captured.height[1]; j++) {
401                                 memcpy(p_buf, temp, player->captured.width[1]);
402                                 p_buf += player->captured.width[1];
403                                 temp += player->captured.stride_width[1];
404                         }
405
406                         /* NV12 -> RGB888 */
407                         ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_COLOR_NV12,
408                                 player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
409                         if (ret != MM_ERROR_NONE) {
410                                 LOGE("failed to convert nv12 linear");
411                                 goto ERROR;
412                         }
413 #ifdef CAPTURE_OUTPUT_DUMP
414                         capture_output_dump(player);
415 #endif
416                         /* clean */
417                         MMPLAYER_FREEIF(src_buffer);
418                         /* free captured buf */
419                         for (i = 0; i < player->captured.handle_num; i++) {
420                                 MMPLAYER_FREEIF(player->captured.data[i]);
421                         }
422                 } else if (MM_PLAYER_COLORSPACE_I420 == player->video_cs) {
423                         planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
424                         planes[1] = planes[2] = (player->captured.stride_width[0]>>1) * (player->captured.stride_height[0]>>1);
425
426                         src_buffer = (unsigned char*) g_try_malloc(player->captured.stride_width[0] * player->captured.stride_height[0]*3/2);
427                         p_buf = src_buffer;
428                         /* set Y plane */
429                         memset(p_buf, 0x00, planes[0]);
430                         temp = player->captured.data[0];
431
432                         for (i = 0; i < player->captured.height[0]; i++) {
433                                 memcpy(p_buf, temp, player->captured.width[0]);
434                                 temp += player->captured.stride_width[0];
435                                 p_buf += player->captured.width[0];
436                         }
437
438                         /* set U plane */
439                         memset(p_buf, 0x00, planes[1]);
440                         temp = player->captured.data[1];
441
442                         for (i = 0; i < player->captured.height[1]; i++) {
443                                 memcpy(p_buf, temp, player->captured.width[1]);
444                                 temp += player->captured.stride_width[1];
445                                 p_buf += player->captured.width[1];
446                         }
447
448                         /* set V plane */
449                         memset(p_buf, 0x00, planes[2]);
450                         temp = player->captured.data[2];
451
452                         for (i = 0; i < player->captured.height[2]; i++) {
453                                 memcpy(p_buf, temp, player->captured.width[2]);
454                                 temp += player->captured.stride_width[2];
455                                 p_buf += player->captured.width[2];
456                         }
457
458                         /* I420 -> RGB888 */
459                         ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_COLOR_I420,
460                                 player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
461                         if (ret != MM_ERROR_NONE) {
462                                 LOGE("failed to convert I420 linear");
463                                 goto ERROR;
464                         }
465 #ifdef CAPTURE_OUTPUT_DUMP
466                         capture_output_dump(player);
467 #endif
468                         /* clean */
469                         MMPLAYER_FREEIF(src_buffer);
470                         /* free captured buf */
471                         for (i = 0; i < player->captured.handle_num; i++) {
472                                 MMPLAYER_FREEIF(player->captured.data[i]);
473                         }
474                 }
475
476                 ret = __mmplayer_get_video_angle((MMHandleType)player, &user_angle, &orientation);
477                 if (ret != MM_ERROR_NONE) {
478                         LOGE("failed to get rotation angle");
479                         goto ERROR;
480                 }
481
482                 LOGD("orientation value = %d user_angle = %d", orientation, user_angle);
483
484                 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_COLOR_RGB24);
485                 if (ret != MM_ERROR_NONE) {
486                         LOGE("failed to convert nv12 linear");
487                         goto ERROR;
488                 }
489
490                 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
491                 msg.data = &player->capture;
492                 msg.size = player->capture.size;
493 //              msg.captured_frame.width = player->capture.width;
494 //              msg.captured_frame.height = player->capture.height;
495 //              msg.captured_frame.orientation = player->capture.orientation;
496
497                 if (player->cmd >= MMPLAYER_COMMAND_START) {
498                         MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
499                         LOGD("returned from capture message callback");
500                 }
501
502                 //MMPLAYER_FREEIF(player->capture.data);
503                 continue;
504 ERROR:
505                 MMPLAYER_FREEIF(src_buffer);
506
507                 for (i = 0; i < player->captured.handle_num; i++) {
508                         MMPLAYER_FREEIF(player->captured.data[i]);
509                 }
510
511                 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
512                         /* clean */
513                         MMPLAYER_FREEIF(linear_y_plane);
514                         MMPLAYER_FREEIF(linear_uv_plane);
515                 }
516
517                 msg.union_type = MM_MSG_UNION_CODE;
518
519                 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
520         }
521
522 EXIT:
523         MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
524         return NULL;
525 }
526
527 /**
528   * The output is fixed as RGB888
529   */
530 static int
531 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
532 {
533         int ret = MM_ERROR_NONE;
534         gint planes[MAX_BUFFER_PLANE] = {0, };
535         gint i = 0;
536         gint src_width = 0;
537         gint src_height = 0;
538         GstCaps *caps = NULL;
539         GstStructure *structure = NULL;
540         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
541         GstMemory *memory = NULL;
542         mm_util_color_format_e src_fmt = MM_UTIL_COLOR_YUV420;
543         mm_util_color_format_e dst_fmt = MM_UTIL_COLOR_RGB24; // fixed
544
545         MMPLAYER_FENTER();
546
547         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
548         MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
549
550         /* get fourcc */
551         caps = gst_pad_get_current_caps(pad);
552
553         MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
554         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
555
556         structure = gst_caps_get_structure(caps, 0);
557         if (!structure) {
558                 LOGE("cannot get structure from caps.\n");
559                 ret = MM_ERROR_PLAYER_INTERNAL;
560                 goto ERROR;
561         }
562
563         /* init capture image buffer */
564         memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
565
566         gst_structure_get_int(structure, "width", &src_width);
567         gst_structure_get_int(structure, "height", &src_height);
568
569         /* check rgb or yuv */
570         if (gst_structure_has_name(structure, "video/x-raw")) {
571                 /* NV12T */
572                 const gchar *gst_format = gst_structure_get_string(structure, "format");
573
574                 if (!g_strcmp0(gst_format, "ST12"))
575                         player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
576                 else if (!g_strcmp0(gst_format, "S420"))
577                         player->video_cs = MM_PLAYER_COLORSPACE_I420;
578                 else if (!g_strcmp0(gst_format, "SN12"))
579                         player->video_cs = MM_PLAYER_COLORSPACE_NV12;
580                 else if (!g_strcmp0(gst_format, "BGRx"))
581                         player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
582                 else
583                         player->video_cs = MM_PLAYER_COLORSPACE_MAX;
584
585                 LOGI("captured format is %s\n", gst_format);
586
587                 if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12")
588                         || !g_strcmp0(gst_format, "S420")) {
589                         guint n;
590                         MMVideoBuffer *proved = NULL;
591
592                         /* get video frame info from proved buffer */
593                         n = gst_buffer_n_memory(buffer);
594                         memory = gst_buffer_peek_memory(buffer, n-1);
595                         gst_memory_map(memory, &mapinfo, GST_MAP_READ);
596                         proved = (MMVideoBuffer *)mapinfo.data;
597
598                         if (!proved || !proved->data[0] || !proved->data[1]) {
599                                 LOGE("fail to gst_memory_map");
600                                 ret = MM_ERROR_PLAYER_INTERNAL;
601                                 goto ERROR;
602                         }
603
604                         memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
605
606                         planes[0] = proved->size[0];
607                         planes[1] = proved->size[1];
608                         planes[2] = proved->size[2];
609
610                         /* Y */
611                         player->captured.data[0] = g_try_malloc(planes[0]);
612                         if (!player->captured.data[0]) {
613                                 gst_memory_unmap(memory, &mapinfo);
614                                 LOGE("no free space\n");
615                                 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
616                                 goto ERROR;
617                         }
618
619                         /* U */
620                         if (proved->size[1]) {
621                                 player->captured.data[1] = g_try_malloc(planes[1]);
622
623                                 if (!player->captured.data[1]) {
624                                         LOGE("no free space\n");
625                                         gst_memory_unmap(memory, &mapinfo);
626                                         ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
627                                         goto ERROR;
628                                 }
629                         }
630
631                         /* V */
632                         if (proved->size[2]) {
633                                 player->captured.data[2] = g_try_malloc(planes[2]);
634
635                                 if (!player->captured.data[2]) {
636                                         LOGE("no free space\n");
637                                         gst_memory_unmap(memory, &mapinfo);
638                                         ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
639                                         goto ERROR;
640                                 }
641                         }
642
643                         LOGD("Buffer type %d", proved->type);
644                         if (proved->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
645
646                                 for (i = 0; i < player->captured.handle_num; i++) {
647                                         tbm_bo_ref(proved->handle.bo[i]);
648                                 }
649                                 LOGD("plane[0] : %p, size %d", proved->data[0], planes[0]);
650
651                                 if (proved->data[0])
652                                         memcpy(player->captured.data[0], proved->data[0], planes[0]);
653
654                                 LOGD("plane[1] : %p, size %d", proved->data[1], planes[1]);
655
656                                 if (proved->data[1])
657                                         memcpy(player->captured.data[1], proved->data[1], planes[1]);
658
659                                 if (player->video_cs == MM_PLAYER_COLORSPACE_I420) {
660                                         LOGD("plane[2] : %p, size %d", proved->data[2], planes[2]);
661
662                                         if (proved->data[2])
663                                                 memcpy(player->captured.data[2], proved->data[2], planes[2]);
664                                 }
665
666                                 for (i = 0; i < player->captured.handle_num; i++) {
667                                         tbm_bo_unref(proved->handle.bo[i]);
668                                 }
669                         } else {
670                                 LOGE("Not support video buffer type %d", proved->type);
671                                 gst_memory_unmap(memory, &mapinfo);
672                                 ret = MM_ERROR_PLAYER_INTERNAL;
673                                 goto ERROR;
674                         }
675
676                         gst_memory_unmap(memory, &mapinfo);
677                         goto DONE;
678                 } else {
679                         GstVideoInfo format_info;
680                         gst_video_info_from_caps(&format_info, caps);
681
682                         player->captured.width[0] = src_width;
683                         player->captured.height[0] = src_height;
684
685                         switch (GST_VIDEO_INFO_FORMAT(&format_info)) {
686                         case GST_VIDEO_FORMAT_I420:
687                                 src_fmt = MM_UTIL_COLOR_I420;
688                                 player->captured.width[1] = player->captured.width[2] = src_width>>1;
689                                 player->captured.height[1] = player->captured.width[2] = src_height>>1;
690                                 break;
691                         case GST_VIDEO_FORMAT_BGRA:
692                                 src_fmt = MM_UTIL_COLOR_BGRA;
693                                 break;
694                         case GST_VIDEO_FORMAT_BGRx:
695                                 src_fmt = MM_UTIL_COLOR_BGRX;
696                                 break;
697                         default:
698                                 LOGE("unknown format to capture\n");
699                                 ret = MM_ERROR_PLAYER_INTERNAL;
700                                 goto ERROR;
701                                 break;
702                         }
703                 }
704         } else {
705                 LOGE("unknown format to capture\n");
706                 ret = MM_ERROR_PLAYER_INTERNAL;
707                 goto ERROR;
708         }
709
710         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
711         __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
712         gst_buffer_unmap(buffer, &mapinfo);
713
714 DONE:
715         /* do convert colorspace */
716         MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
717
718         if (caps) {
719                 gst_caps_unref(caps);
720                 caps = NULL;
721         }
722
723         return ret;
724
725 ERROR:
726
727         if (caps) {
728                 gst_caps_unref(caps);
729                 caps = NULL;
730         }
731
732         for (i = 0; i < player->captured.handle_num; i++) {
733                 MMPLAYER_FREEIF(player->captured.data[i]);
734         }
735
736         MMPLAYER_FLEAVE();
737         return ret;
738 }
739
740 static GstPadProbeReturn
741 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
742 {
743         mm_player_t* player = (mm_player_t*) u_data;
744         GstBuffer *buffer = NULL;
745         int ret = MM_ERROR_NONE;
746
747         MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
748         MMPLAYER_FENTER();
749
750         buffer = gst_pad_probe_info_get_buffer(info);
751         ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
752
753         if (ret != MM_ERROR_NONE) {
754                 LOGE("failed to get video frame");
755                 return GST_PAD_PROBE_REMOVE;
756         }
757
758         /* remove probe to be called at one time */
759         if (player->video_capture_cb_probe_id) {
760                 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
761                 player->video_capture_cb_probe_id = 0;
762         }
763
764         MMPLAYER_FLEAVE();
765
766         return GST_PAD_PROBE_OK;
767 }
768
769 static int
770 __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_color_format_e src_fmt, unsigned int src_w, unsigned int src_h, mm_util_color_format_e dst_fmt)
771 {
772         unsigned char *dst_data = NULL;
773         unsigned int dst_size;
774         int ret = MM_ERROR_NONE;
775
776         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
777         ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
778
779         if (ret != MM_ERROR_NONE) {
780                 LOGE("failed to get image size for capture, %d\n", ret);
781                 return MM_ERROR_PLAYER_INTERNAL;
782         }
783
784         SECURE_LOGD("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
785
786         dst_data = (unsigned char*)g_malloc0(dst_size);
787
788         if (!dst_data) {
789                 LOGE("no free space to capture\n");
790                 g_free(dst_data);
791                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
792         }
793
794         ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
795
796         if (ret != MM_ERROR_NONE) {
797                 LOGE("failed to convert for capture, %d\n", ret);
798                 g_free(dst_data);
799                 return MM_ERROR_PLAYER_INTERNAL;
800         }
801
802         player->capture.size = dst_size;
803         player->capture.data = dst_data;
804
805         return MM_ERROR_NONE;
806 }
807
808 /*
809  * Get tiled address of position(x,y)
810  *
811  * @param x_size
812  *   width of tiled[in]
813  *
814  * @param y_size
815  *   height of tiled[in]
816  *
817  * @param x_pos
818  *   x position of tield[in]
819  *
820  * @param src_size
821  *   y position of tield[in]
822  *
823  * @return
824  *   address of tiled data
825  */
826 static int
827 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
828 {
829         int pixel_x_m1, pixel_y_m1;
830         int roundup_x;
831         int linear_addr0, linear_addr1, bank_addr ;
832         int x_addr;
833         int trans_addr;
834
835         pixel_x_m1 = x_size -1;
836         pixel_y_m1 = y_size -1;
837
838         roundup_x = ((pixel_x_m1 >> 7) + 1);
839
840         x_addr = x_pos >> 2;
841
842         if ((y_size <= y_pos+32) && (y_pos < y_size) &&
843                 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
844                 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
845                 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 6) & 0x3f));
846
847                 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
848                         bank_addr = ((x_addr >> 4) & 0x1);
849                 else
850                         bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
851         } else {
852                 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
853                 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 5) & 0x7f));
854
855                 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
856                         bank_addr = ((x_addr >> 4) & 0x1);
857                 else
858                         bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
859         }
860
861         linear_addr0 = linear_addr0 << 2;
862         trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
863
864         return trans_addr;
865 }
866
867 /*
868  * Converts tiled data to linear
869  * Crops left, top, right, buttom
870  * 1. Y of NV12T to Y of YUV420P
871  * 2. Y of NV12T to Y of YUV420S
872  * 3. UV of NV12T to UV of YUV420S
873  *
874  * @param yuv420_dest
875  *   Y or UV plane address of YUV420[out]
876  *
877  * @param nv12t_src
878  *   Y or UV plane address of NV12T[in]
879  *
880  * @param yuv420_width
881  *   Width of YUV420[in]
882  *
883  * @param yuv420_height
884  *   Y: Height of YUV420, UV: Height/2 of YUV420[in]
885  *
886  * @param left
887  *   Crop size of left
888  *
889  * @param top
890  *   Crop size of top
891  *
892  * @param right
893  *   Crop size of right
894  *
895  * @param buttom
896  *   Crop size of buttom
897  */
898 static void
899 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
900                                                         int left, int top, int right, int buttom)
901 {
902         int i, j;
903         int tiled_offset = 0, tiled_offset1 = 0;
904         int linear_offset = 0;
905         int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
906
907         temp3 = yuv420_width-right;
908         temp1 = temp3-left;
909         /* real width is greater than or equal 256 */
910         if (temp1 >= 256) {
911                 for (i = top; i < yuv420_height-buttom; i = i+1) {
912                         j = left;
913                         temp3 = (j>>8)<<8;
914                         temp3 = temp3>>6;
915                         temp4 = i>>5;
916                         if (temp4 & 0x1) {
917                                 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
918                                 tiled_offset = temp4-1;
919                                 temp1 = ((yuv420_width+127)>>7)<<7;
920                                 tiled_offset = tiled_offset*(temp1>>6);
921                                 tiled_offset = tiled_offset+temp3;
922                                 tiled_offset = tiled_offset+2;
923                                 temp1 = (temp3>>2)<<2;
924                                 tiled_offset = tiled_offset+temp1;
925                                 tiled_offset = tiled_offset<<11;
926                                 tiled_offset1 = tiled_offset+2048*2;
927                                 temp4 = 8;
928                         } else {
929                                 temp2 = ((yuv420_height+31)>>5)<<5;
930                                 if ((i+32) < temp2) {
931                                         /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
932                                         temp1 = temp3+2;
933                                         temp1 = (temp1>>2)<<2;
934                                         tiled_offset = temp3+temp1;
935                                         temp1 = ((yuv420_width+127)>>7)<<7;
936                                         tiled_offset = tiled_offset+temp4*(temp1>>6);
937                                         tiled_offset = tiled_offset<<11;
938                                         tiled_offset1 = tiled_offset+2048*6;
939                                         temp4 = 8;
940                                 } else {
941                                         /* even2 fomula: x+x_block_num*y */
942                                         temp1 = ((yuv420_width+127)>>7)<<7;
943                                         tiled_offset = temp4*(temp1>>6);
944                                         tiled_offset = tiled_offset+temp3;
945                                         tiled_offset = tiled_offset<<11;
946                                         tiled_offset1 = tiled_offset+2048*2;
947                                         temp4 = 4;
948                                 }
949                         }
950
951                         temp1 = i&0x1F;
952                         tiled_offset = tiled_offset+64*(temp1);
953                         tiled_offset1 = tiled_offset1+64*(temp1);
954                         temp2 = yuv420_width-left-right;
955                         linear_offset = temp2*(i-top);
956                         temp3 = ((j+256)>>8)<<8;
957                         temp3 = temp3-j;
958                         temp1 = left&0x3F;
959                         if (temp3 > 192) {
960                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
961                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
962                                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
963                                 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
964                                 linear_offset = linear_offset+256-temp1;
965                         } else if (temp3 > 128) {
966                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
967                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
968                                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
969                                 linear_offset = linear_offset+192-temp1;
970                         } else if (temp3 > 64) {
971                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
972                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
973                                 linear_offset = linear_offset+128-temp1;
974                         } else if (temp3 > 0) {
975                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
976                                 linear_offset = linear_offset+64-temp1;
977                         }
978
979                         tiled_offset = tiled_offset+temp4*2048;
980                         j = (left>>8)<<8;
981                         j = j + 256;
982                         temp2 = yuv420_width-right-256;
983                         for (; j <= temp2; j = j+256) {
984                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
985                                 tiled_offset1 = tiled_offset1+temp4*2048;
986                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
987                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
988                                 tiled_offset = tiled_offset+temp4*2048;
989                                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
990                                 linear_offset = linear_offset+256;
991                         }
992
993                         tiled_offset1 = tiled_offset1+temp4*2048;
994                         temp2 = yuv420_width-right-j;
995                         if (temp2 > 192) {
996                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
997                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
998                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
999                                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
1000                         } else if (temp2 > 128) {
1001                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1002                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
1003                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
1004                         } else if (temp2 > 64) {
1005                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1006                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
1007                         } else
1008                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
1009                 }
1010         } else if (temp1 >= 64) {
1011                 for (i = top; i < (yuv420_height-buttom); i = i+1) {
1012                         j = left;
1013                         tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1014                         temp2 = ((j+64)>>6)<<6;
1015                         temp2 = temp2-j;
1016                         linear_offset = temp1*(i-top);
1017                         temp4 = j&0x3;
1018                         tiled_offset = tiled_offset+temp4;
1019                         memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
1020                         linear_offset = linear_offset+temp2;
1021                         j = j+temp2;
1022                         if ((j+64) <= temp3) {
1023                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1024                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1025                                 linear_offset = linear_offset+64;
1026                                 j = j+64;
1027                         }
1028                         if ((j+64) <= temp3) {
1029                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1030                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1031                                 linear_offset = linear_offset+64;
1032                                 j = j+64;
1033                         }
1034                         if (j < temp3) {
1035                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1036                                 temp2 = temp3-j;
1037                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
1038                         }
1039                 }
1040         } else {
1041                 for (i = top; i < (yuv420_height-buttom); i = i+1) {
1042                         linear_offset = temp1*(i-top);
1043                         for (j = left; j < (yuv420_width-right); j = j+2) {
1044                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1045                                 temp4 = j&0x3;
1046                                 tiled_offset = tiled_offset+temp4;
1047                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
1048                                 linear_offset = linear_offset+2;
1049                         }
1050                 }
1051         }
1052 }
1053
1054 #ifdef CAPTURE_OUTPUT_DUMP /* for capture output dump */
1055 static void capture_output_dump(mm_player_t* player)
1056 {
1057         unsigned char *temp = NULL;
1058         char file[100] = { 0, };
1059         FILE *fp = NULL;
1060         int ret = 0;
1061         int i = 0, j = 0;
1062
1063         LOGE("capture output dump start. size = %d", player->capture.size);
1064         sprintf(file, "/tmp/dec_output_dump_%dx%d.yuv", player->captured.width[0], player->captured.height[0]);
1065         fp = fopen(file, "ab");
1066
1067         for (i = 0; i < player->captured.plane_num; i++) {
1068                 temp = player->captured.data[i];
1069
1070                 for (j = 0; j < player->captured.height[i]; j++) {
1071                         ret = fwrite(temp, player->captured.width[i], 1, fp);
1072                         temp += player->captured.width[i];
1073                 }
1074         }
1075         LOGE("capture yuv dumped!! ret = %d", ret);
1076         fclose(fp);
1077
1078         temp = (unsigned char *)player->capture.data;
1079         sprintf(file, "/tmp/dec_output_dump_%dx%d.rgb", player->captured.width[0], player->captured.height[0]);
1080         fp = fopen(file, "ab");
1081         ret = fwrite(temp, player->capture.size, 1, fp);
1082         fclose(fp);
1083         LOGE("capture rgb dumped!! ret = %d", ret);
1084 }
1085 #endif