4 * Copyright(c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 /*===========================================================================================
27 ========================================================================================== */
29 #include "mm_player_utils.h"
30 #include "mm_player_capture.h"
31 #include "mm_player_priv.h"
33 #include <mm_util_image.h>
34 #include <mm_util_imgp.h>
35 #include <gst/video/video-info.h>
37 //#define CAPTURE_OUTPUT_DUMP 1
38 /*---------------------------------------------------------------------------
39 | LOCAL VARIABLE DEFINITIONS for internal |
40 ---------------------------------------------------------------------------*/
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);
59 /*===========================================================================================
61 | FUNCTION DEFINITIONS |
63 ========================================================================================== */
65 _mmplayer_initialize_video_capture(mmplayer_t *player)
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);
72 /* create capture cond */
73 g_cond_init(&player->capture_thread_cond);
75 player->capture_thread_exit = FALSE;
77 /* create video capture thread */
78 player->capture_thread =
79 g_thread_try_new("capture_thread", __mmplayer_capture_thread, (gpointer)player, NULL);
81 if (!player->capture_thread) {
82 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
90 g_mutex_clear(&player->capture_thread_mutex);
91 g_cond_clear(&player->capture_thread_cond);
96 _mmplayer_release_video_capture(mmplayer_t *player)
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);
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");
111 return MM_ERROR_NONE;
115 _mmplayer_do_video_capture(MMHandleType hplayer)
117 mmplayer_t *player = (mmplayer_t *)hplayer;
118 int ret = MM_ERROR_NONE;
123 MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
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;
131 gint surface_type = 0;
132 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
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;
140 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
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;
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);
151 GstBuffer *buf = NULL;
152 buf = gst_sample_get_buffer(sample);
154 if (__mmplayer_get_video_frame_from_buffer(player, pad, buf) != MM_ERROR_NONE)
155 ret = MM_ERROR_PLAYER_INTERNAL;
157 LOGW("failed to get video frame");
159 gst_sample_unref(sample);
163 LOGW("invalid state(%d) to capture", player->state);
164 gst_object_unref(GST_OBJECT(pad));
166 return MM_ERROR_PLAYER_INVALID_STATE;
171 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
172 __mmplayer_video_capture_probe, player, NULL);
174 gst_object_unref(GST_OBJECT(pad));
183 __mmplayer_handle_orientation(mmplayer_t *player, int orientation, int format)
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;
190 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
192 player->capture.orientation = orientation;
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;
201 src_buffer = (unsigned char *)player->capture.data;
203 /* convert orientation degree into enum here */
204 switch (orientation) {
206 rot_enum = MM_UTIL_ROTATE_90;
209 rot_enum = MM_UTIL_ROTATE_180;
212 rot_enum = MM_UTIL_ROTATE_270;
215 LOGE("wrong rotate value");
219 LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
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");
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");
234 mm_image_debug_image(dst_image, NULL);
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");
245 player->capture.size = (int)dst_size;
246 player->capture.orientation = orientation;
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;
255 __mmplayer_capture_thread(gpointer data)
257 mmplayer_t *player = (mmplayer_t *)data;
258 MMMessageParamType msg = {0, };
260 int display_angle = 0;
264 MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
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);
271 if (player->capture_thread_exit) {
272 LOGD("exiting capture thread");
275 LOGD("capture thread is received signal");
277 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
278 * Because deadlock can be happened if other player api is used in message callback.
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");
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");
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");
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");
310 LOGE("invalid format");
311 msg.code = MM_ERROR_PLAYER_INTERNAL;
315 ret = _mmplayer_get_video_angle((MMHandleType)player, &display_angle, &orientation);
316 if (ret != MM_ERROR_NONE) {
317 LOGE("failed to get rotation angle");
321 LOGD("orientation value = %d user_angle = %d", orientation, display_angle);
323 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_COLOR_RGB24);
324 if (ret != MM_ERROR_NONE) {
325 LOGE("failed to convert nv12 linear");
329 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
330 msg.data = &player->capture;
331 msg.size = player->capture.size;
333 if (player->cmd >= MMPLAYER_COMMAND_START) {
334 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
335 LOGD("returned from capture message callback");
340 for (i = 0; i < player->captured.plane_num; i++)
341 MMPLAYER_FREEIF(player->captured.data[i]);
343 msg.union_type = MM_MSG_UNION_CODE;
345 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
349 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
354 * The output is fixed as RGB888
357 __mmplayer_get_video_frame_from_buffer(mmplayer_t *player, GstPad *pad, GstBuffer *buffer)
360 guint plane_size = 0;
361 GstCaps *caps = NULL;
362 GstVideoFrame vframe;
364 guint8 *pixels = NULL;
365 int ret = MM_ERROR_NONE;
369 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
370 MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
373 caps = gst_pad_get_current_caps(pad);
375 MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
376 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
378 /* init capture image buffer */
379 memset(&player->capture, 0x00, sizeof(mmplayer_video_capture_t));
381 gst_video_info_from_caps(&vinfo, caps);
383 gst_caps_unref(caps);
385 LOGI("captured format is %s", gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(&vinfo)));
387 player->captured.width[0] = GST_VIDEO_INFO_WIDTH(&vinfo);
388 player->captured.height[0] = GST_VIDEO_INFO_HEIGHT(&vinfo);
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;
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;
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;
408 case GST_VIDEO_FORMAT_BGRx:
409 player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
412 player->video_cs = MM_PLAYER_COLORSPACE_MAX;
413 ret = MM_ERROR_PLAYER_INTERNAL;
417 if (ret != MM_ERROR_NONE) {
418 LOGE("unknown format to capture");
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];
428 LOGE("invalid plane size");
429 gst_video_frame_unmap(&vframe);
430 return MM_ERROR_PLAYER_INTERNAL;
432 pixels = GST_VIDEO_FRAME_PLANE_DATA(&vframe, i);
433 player->captured.data[i] = g_malloc(plane_size);
434 memcpy(player->captured.data[i], pixels, plane_size);
436 player->captured.plane_num = GST_VIDEO_FRAME_N_PLANES(&vframe);
437 gst_video_frame_unmap(&vframe);
439 /* do convert colorspace */
440 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
445 static GstPadProbeReturn
446 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
448 mmplayer_t *player = (mmplayer_t *)u_data;
449 GstBuffer *buffer = NULL;
450 int ret = MM_ERROR_NONE;
452 MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
455 buffer = gst_pad_probe_info_get_buffer(info);
456 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
457 if (ret != MM_ERROR_NONE) {
458 LOGE("failed to get video frame");
459 return GST_PAD_PROBE_REMOVE;
462 /* remove probe to be called at one time */
463 if (player->video_capture_cb_probe_id) {
464 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
465 player->video_capture_cb_probe_id = 0;
470 return GST_PAD_PROBE_OK;
474 __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)
476 int ret = MM_ERROR_NONE;
477 mm_util_image_h src_image = NULL;
478 mm_util_image_h dst_image = NULL;
481 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
482 SECURE_LOGD("src size info. width: %d, height: %d", src_w, src_h);
484 ret = mm_image_create_image(src_w, src_h, src_fmt, src_data, src_size, &src_image);
485 if (ret != MM_ERROR_NONE) {
486 LOGE("failed to create image for capture, %d", ret);
487 return MM_ERROR_PLAYER_INTERNAL;
490 ret = mm_util_convert_colorspace(src_image, dst_fmt, &dst_image);
491 mm_image_destroy_image(src_image);
492 if (ret != MM_ERROR_NONE) {
493 LOGE("failed to convert for capture, %d", ret);
494 return MM_ERROR_PLAYER_INTERNAL;
496 mm_image_debug_image(dst_image, NULL);
498 ret = mm_image_get_image(dst_image, NULL, NULL, NULL, &player->capture.data, &size);
499 mm_image_destroy_image(dst_image);
500 if (ret != MM_ERROR_NONE) {
501 LOGE("failed to get image for capture, %d", ret);
502 return MM_ERROR_PLAYER_INTERNAL;
505 player->capture.size = (int)size;
507 return MM_ERROR_NONE;
511 * Get tiled address of position(x,y)
517 * height of tiled[in]
520 * x position of tield[in]
523 * y position of tield[in]
526 * address of tiled data
529 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
531 int pixel_x_m1, pixel_y_m1;
533 int linear_addr0, linear_addr1, bank_addr ;
537 pixel_x_m1 = x_size -1;
538 pixel_y_m1 = y_size -1;
540 roundup_x = ((pixel_x_m1 >> 7) + 1);
544 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
545 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
546 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
547 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
549 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
550 bank_addr = ((x_addr >> 4) & 0x1);
552 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
554 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
555 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
557 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
558 bank_addr = ((x_addr >> 4) & 0x1);
560 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
563 linear_addr0 = linear_addr0 << 2;
564 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
570 * Converts tiled data to linear
571 * Crops left, top, right, buttom
572 * 1. Y of NV12T to Y of YUV420P
573 * 2. Y of NV12T to Y of YUV420S
574 * 3. UV of NV12T to UV of YUV420S
577 * Y or UV plane address of YUV420[out]
580 * Y or UV plane address of NV12T[in]
582 * @param yuv420_width
583 * Width of YUV420[in]
585 * @param yuv420_height
586 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
598 * Crop size of buttom
601 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
602 int left, int top, int right, int buttom)
605 int tiled_offset = 0, tiled_offset1 = 0;
606 int linear_offset = 0;
607 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
609 temp3 = yuv420_width-right;
611 /* real width is greater than or equal 256 */
613 for (i = top; i < yuv420_height-buttom; i = i+1) {
615 temp3 = (j >> 8) << 8;
619 /* odd fomula: 2 + x + (x >> 2) << 2 + x_block_num * (y - 1) */
620 tiled_offset = temp4 - 1;
621 temp1 = ((yuv420_width + 127) >> 7) << 7;
622 tiled_offset = tiled_offset * (temp1 >> 6);
623 tiled_offset = tiled_offset + temp3;
624 tiled_offset = tiled_offset + 2;
625 temp1 = (temp3 >> 2) << 2;
626 tiled_offset = tiled_offset + temp1;
627 tiled_offset = tiled_offset << 11;
628 tiled_offset1 = tiled_offset + 2048 * 2;
631 temp2 = ((yuv420_height+31) >> 5) << 5;
632 if ((i + 32) < temp2) {
633 /* even1 fomula: x + ((x + 2) >> 2) << 2 + x_block_num * y */
635 temp1 = (temp1 >> 2) << 2;
636 tiled_offset = temp3 + temp1;
637 temp1 = ((yuv420_width + 127) >> 7) << 7;
638 tiled_offset = tiled_offset + temp4 * (temp1 >> 6);
639 tiled_offset = tiled_offset << 11;
640 tiled_offset1 = tiled_offset + 2048 * 6;
643 /* even2 fomula: x + x_block_num * y */
644 temp1 = ((yuv420_width + 127) >> 7) << 7;
645 tiled_offset = temp4 * (temp1 >> 6);
646 tiled_offset = tiled_offset + temp3;
647 tiled_offset = tiled_offset << 11;
648 tiled_offset1 = tiled_offset + 2048 * 2;
654 tiled_offset = tiled_offset + 64 * temp1;
655 tiled_offset1 = tiled_offset1 + 64 * temp1;
656 temp2 = yuv420_width - left - right;
657 linear_offset = temp2 * (i - top);
658 temp3 = ((j + 256) >> 8) << 8;
662 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset + temp1, 64 - temp1);
663 memcpy(yuv420_dest + linear_offset + 64 - temp1, nv12t_src + tiled_offset + 2048, 64);
664 memcpy(yuv420_dest + linear_offset + 128 - temp1, nv12t_src + tiled_offset1, 64);
665 memcpy(yuv420_dest + linear_offset + 192 - temp1, nv12t_src + tiled_offset1 + 2048, 64);
666 linear_offset = linear_offset + 256 - temp1;
667 } else if (temp3 > 128) {
668 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset + 2048 + temp1, 64 - temp1);
669 memcpy(yuv420_dest + linear_offset + 64 - temp1, nv12t_src + tiled_offset1, 64);
670 memcpy(yuv420_dest + linear_offset + 128 - temp1, nv12t_src + tiled_offset1 + 2048, 64);
671 linear_offset = linear_offset + 192 - temp1;
672 } else if (temp3 > 64) {
673 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset1 + temp1, 64 - temp1);
674 memcpy(yuv420_dest + linear_offset + 64 - temp1, nv12t_src + tiled_offset1 + 2048, 64);
675 linear_offset = linear_offset + 128 - temp1;
676 } else if (temp3 > 0) {
677 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset1 + 2048 + temp1, 64 - temp1);
678 linear_offset = linear_offset + 64 - temp1;
681 tiled_offset = tiled_offset + temp4 * 2048;
682 j = (left >> 8) << 8;
684 temp2 = yuv420_width - right - 256;
685 for (; j <= temp2; j = j + 256) {
686 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 64);
687 tiled_offset1 = tiled_offset1 + temp4 * 2048;
688 memcpy(yuv420_dest + linear_offset + 64, nv12t_src + tiled_offset + 2048, 64);
689 memcpy(yuv420_dest + linear_offset + 128, nv12t_src + tiled_offset1, 64);
690 tiled_offset = tiled_offset + temp4 * 2048;
691 memcpy(yuv420_dest + linear_offset + 192, nv12t_src + tiled_offset1 + 2048, 64);
692 linear_offset = linear_offset + 256;
695 tiled_offset1 = tiled_offset1 + temp4 * 2048;
696 temp2 = yuv420_width - right - j;
698 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 64);
699 memcpy(yuv420_dest + linear_offset + 64, nv12t_src + tiled_offset + 2048, 64);
700 memcpy(yuv420_dest + linear_offset + 128, nv12t_src + tiled_offset1, 64);
701 memcpy(yuv420_dest + linear_offset + 192, nv12t_src + tiled_offset1 + 2048, temp2 - 192);
702 } else if (temp2 > 128) {
703 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 64);
704 memcpy(yuv420_dest + linear_offset + 64, nv12t_src + tiled_offset + 2048, 64);
705 memcpy(yuv420_dest + linear_offset + 128, nv12t_src + tiled_offset1, temp2 - 128);
706 } else if (temp2 > 64) {
707 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 64);
708 memcpy(yuv420_dest + linear_offset + 64, nv12t_src + tiled_offset + 2048, temp2 - 64);
710 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, temp2);
713 } else if (temp1 >= 64) {
714 for (i = top; i < (yuv420_height - buttom); i++) {
716 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
717 temp2 = ((j + 64) >> 6) << 6;
719 linear_offset = temp1 * (i - top);
721 tiled_offset = tiled_offset + temp4;
722 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, temp2);
723 linear_offset = linear_offset + temp2;
725 if ((j + 64) <= temp3) {
726 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
727 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 64);
728 linear_offset = linear_offset + 64;
731 if ((j + 64) <= temp3) {
732 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
733 memcpy(yuv420_dest+linear_offset, nv12t_src + tiled_offset, 64);
734 linear_offset = linear_offset + 64;
738 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
740 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, temp2);
744 for (i = top; i < (yuv420_height - buttom); i++) {
745 linear_offset = temp1 * (i - top);
746 for (j = left; j < (yuv420_width - right); j += 2) {
747 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
749 tiled_offset = tiled_offset + temp4;
750 memcpy(yuv420_dest + linear_offset, nv12t_src + tiled_offset, 2);
751 linear_offset = linear_offset + 2;
757 #ifdef CAPTURE_OUTPUT_DUMP /* for capture output dump */
759 capture_output_dump(mmplayer_t *player)
761 unsigned char *temp = NULL;
762 char file[100] = { 0, };
767 LOGE("capture output dump start. size = %d", player->capture.size);
768 sprintf(file, "/tmp/dec_output_dump_%dx%d.yuv", player->captured.width[0], player->captured.height[0]);
769 fp = fopen(file, "ab");
771 for (i = 0; i < player->captured.plane_num; i++) {
772 temp = player->captured.data[i];
774 for (j = 0; j < player->captured.height[i]; j++) {
775 ret = fwrite(temp, player->captured.width[i], 1, fp);
776 temp += player->captured.stride_width[i];
779 LOGE("capture yuv dumped!! ret = %d", ret);
782 temp = (unsigned char *)player->capture.data;
783 sprintf(file, "/tmp/dec_output_dump_%dx%d.rgb", player->captured.width[0], player->captured.height[0]);
784 fp = fopen(file, "ab");
785 ret = fwrite(temp, player->capture.size, 1, fp);
787 LOGE("capture rgb dumped!! ret = %d", ret);
792 __mm_player_convert_NV12_tiled(mmplayer_t *player)
794 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
796 int ret = MM_ERROR_NONE;
797 unsigned char *src_buffer = NULL;
798 unsigned char *linear_y_plane = NULL;
799 unsigned char *linear_uv_plane = NULL;
800 guint linear_y_plane_size;
801 guint linear_uv_plane_size;
802 guint width = player->captured.width[0];
803 guint height = player->captured.height[0];
805 linear_y_plane_size = width * height;
806 linear_uv_plane_size = linear_y_plane_size / 2;
808 if (!linear_y_plane_size || !linear_uv_plane_size) {
809 LOGE("invalid plane size");
810 return MM_ERROR_PLAYER_INTERNAL;
812 linear_y_plane = (unsigned char *)g_malloc(linear_y_plane_size);
813 linear_uv_plane = (unsigned char *)g_malloc(linear_uv_plane_size);
815 /* NV12 tiled to linear */
816 __csc_tiled_to_linear_crop(linear_y_plane,
817 player->captured.data[0], width, height, 0, 0, 0, 0);
818 __csc_tiled_to_linear_crop(linear_uv_plane,
819 player->captured.data[1], width, height / 2, 0, 0, 0, 0);
821 src_buffer = (unsigned char *)g_malloc(linear_y_plane_size + linear_uv_plane_size);
823 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
824 memcpy(src_buffer + linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
826 /* NV12 linear to RGB888 */
827 ret = __mm_player_convert_colorspace(player, src_buffer, (size_t)(linear_y_plane_size + linear_uv_plane_size),
828 MM_UTIL_COLOR_NV12, width, height, MM_UTIL_COLOR_RGB24);
830 MMPLAYER_FREEIF(src_buffer);
831 MMPLAYER_FREEIF(linear_y_plane);
832 MMPLAYER_FREEIF(linear_uv_plane);
834 for (i = 0; i < player->captured.plane_num; i++)
835 MMPLAYER_FREEIF(player->captured.data[i]);
841 __mm_player_convert_NV12(mmplayer_t *player)
843 unsigned char *src_buffer = NULL;
844 unsigned char *p_buf = NULL;
845 unsigned char *temp = NULL;
846 int planes[MAX_BUFFER_PLANE] = {0, };
847 int ret = MM_ERROR_NONE;
850 /* using original width otherwises, app can't know aligned to resize */
851 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
852 planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
853 guint src_buffer_size = planes[0] + planes[1];
855 if (!src_buffer_size) {
856 LOGE("invalid data size");
857 return MM_ERROR_PLAYER_INTERNAL;
859 src_buffer = (unsigned char *)g_malloc(src_buffer_size);
863 temp = player->captured.data[0];
866 for (i = 0; i < player->captured.height[0]; i++) {
867 memcpy(p_buf, temp, player->captured.width[0]);
868 p_buf += player->captured.width[0];
869 temp += player->captured.stride_width[0];
872 temp = player->captured.data[1];
875 for (j = 0; j < player->captured.height[1]; j++) {
876 memcpy(p_buf, temp, player->captured.width[1]);
877 p_buf += player->captured.width[1];
878 temp += player->captured.stride_width[1];
882 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, (size_t)src_buffer_size,
883 MM_UTIL_COLOR_NV12, player->captured.width[0],
884 player->captured.height[0], MM_UTIL_COLOR_RGB24);
886 #ifdef CAPTURE_OUTPUT_DUMP
887 capture_output_dump(player);
889 MMPLAYER_FREEIF(src_buffer);
891 for (i = 0; i < player->captured.plane_num; i++)
892 MMPLAYER_FREEIF(player->captured.data[i]);
898 __mm_player_convert_I420(mmplayer_t *player)
900 unsigned char *src_buffer = NULL;
901 unsigned char *p_buf = NULL;
902 unsigned char *temp = NULL;
903 int planes[MAX_BUFFER_PLANE] = {0, };
904 int ret = MM_ERROR_NONE;
907 /* using original width otherwises, app can't know aligned to resize */
908 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
909 planes[1] = planes[2] = (player->captured.stride_width[0] >> 1)
910 * (player->captured.stride_height[0] >> 1);
912 if (!planes[0] || !planes[1] || !planes[2]) {
913 LOGE("invalid plane size");
914 return MM_ERROR_PLAYER_INTERNAL;
916 src_buffer = (unsigned char *)g_malloc(planes[0] + planes[1] + planes[2]);
921 temp = player->captured.data[0];
923 for (i = 0; i < player->captured.height[0]; i++) {
924 memcpy(p_buf, temp, player->captured.width[0]);
925 temp += player->captured.stride_width[0];
926 p_buf += player->captured.width[0];
930 temp = player->captured.data[1];
932 for (i = 0; i < player->captured.height[1]; i++) {
933 memcpy(p_buf, temp, player->captured.width[1]);
934 temp += player->captured.stride_width[1];
935 p_buf += player->captured.width[1];
939 temp = player->captured.data[2];
941 for (i = 0; i < player->captured.height[2]; i++) {
942 memcpy(p_buf, temp, player->captured.width[2]);
943 temp += player->captured.stride_width[2];
944 p_buf += player->captured.width[2];
948 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, (size_t)(planes[0] + planes[1] + planes[2]),
949 MM_UTIL_COLOR_I420, player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
951 #ifdef CAPTURE_OUTPUT_DUMP
952 capture_output_dump(player);
954 MMPLAYER_FREEIF(src_buffer);
956 for (i = 0; i < player->captured.plane_num; i++)
957 MMPLAYER_FREEIF(player->captured.data[i]);
963 __mm_player_convert_BGRx(mmplayer_t *player)
967 int ret = MM_ERROR_NONE;
968 unsigned char *src_buffer = NULL;
970 size = (player->captured.stride_width[0] * player->captured.stride_height[0]);
973 LOGE("invalid size");
974 return MM_ERROR_PLAYER_INTERNAL;
976 src_buffer = (unsigned char *)g_malloc(size);
977 memcpy(src_buffer, player->captured.data[0], size);
980 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, (size_t)size, MM_UTIL_COLOR_BGRX,
981 player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
983 #ifdef CAPTURE_OUTPUT_DUMP
984 capture_output_dump(player);
986 MMPLAYER_FREEIF(src_buffer);
988 for (i = 0; i < player->captured.plane_num; i++)
989 MMPLAYER_FREEIF(player->captured.data[i]);