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