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