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