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