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