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