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_imgp.h>
34 #include <gst/video/video-info.h>
36 /*---------------------------------------------------------------------------
37 | LOCAL VARIABLE DEFINITIONS for internal |
38 ---------------------------------------------------------------------------*/
40 /*---------------------------------------------------------------------------
41 | LOCAL FUNCTION PROTOTYPES: |
42 ---------------------------------------------------------------------------*/
43 static GstPadProbeReturn __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
44 static int __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer);
45 static gpointer __mmplayer_capture_thread(gpointer data);
46 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);
47 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
48 static int __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_img_format src_fmt, unsigned int src_w, unsigned int src_h, mm_util_img_format dst_fmt);
50 /*===========================================================================================
52 | FUNCTION DEFINITIONS |
54 ========================================================================================== */
56 _mmplayer_initialize_video_capture(mm_player_t* player)
58 int ret = MM_ERROR_NONE;
59 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
60 /* create capture mutex */
61 g_mutex_init(&player->capture_thread_mutex);
63 /* create capture cond */
64 g_cond_init(&player->capture_thread_cond);
67 player->capture_thread_exit = FALSE;
69 /* create video capture thread */
70 player->capture_thread =
71 g_thread_try_new("capture_thread", __mmplayer_capture_thread, (gpointer)player, NULL);
73 if (!player->capture_thread) {
74 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
82 g_mutex_clear(&player->capture_thread_mutex);
84 g_cond_clear(&player->capture_thread_cond);
90 _mmplayer_release_video_capture(mm_player_t* player)
92 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
93 /* release capture thread */
94 g_mutex_lock(&player->capture_thread_mutex);
95 player->capture_thread_exit = TRUE;
96 g_cond_signal(&player->capture_thread_cond);
97 g_mutex_unlock(&player->capture_thread_mutex);
99 LOGD("waitting for capture thread exit");
100 g_thread_join(player->capture_thread);
101 g_mutex_clear(&player->capture_thread_mutex);
102 g_cond_clear(&player->capture_thread_cond);
103 LOGD("capture thread released");
105 return MM_ERROR_NONE;
109 _mmplayer_do_video_capture(MMHandleType hplayer)
111 mm_player_t* player = (mm_player_t*) hplayer;
112 int ret = MM_ERROR_NONE;
117 MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
119 /* capturing or not */
120 if (player->video_capture_cb_probe_id || player->capture.data
121 || player->captured.data[0] || player->captured.data[1]) {
122 LOGW("capturing... we can't do any more");
123 return MM_ERROR_PLAYER_INVALID_STATE;
125 gint surface_type = 0;
126 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
128 /* check if video pipeline is linked or not */
129 if (!player->pipeline->videobin) {
130 if (surface_type == MM_DISPLAY_SURFACE_REMOTE && player->video_fakesink) {
131 LOGD("it is evas surface type.");
133 LOGW("not ready to capture");
134 return MM_ERROR_PLAYER_INVALID_STATE;
138 if (player->pipeline->videobin)
139 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
141 pad = gst_element_get_static_pad(player->video_fakesink, "sink");
143 if (player->state != MM_PLAYER_STATE_PLAYING) {
144 if (player->state == MM_PLAYER_STATE_PAUSED) {
145 // get last buffer from video sink
146 GstSample *sample = NULL;
148 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
150 if (player->pipeline->videobin)
151 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
153 g_object_get(player->video_fakesink, "last-sample", &sample, NULL);
156 GstBuffer *buf = NULL;
157 buf = gst_sample_get_buffer(sample);
160 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buf);
162 LOGW("failed to get video frame");
163 gst_sample_unref(sample);
167 LOGW("invalid state(%d) to capture", player->state);
168 return MM_ERROR_PLAYER_INVALID_STATE;
173 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
174 __mmplayer_video_capture_probe, player, NULL);
176 gst_object_unref(GST_OBJECT(pad));
185 __mmplayer_handle_orientation(mm_player_t* player, int orientation, int format)
187 unsigned char *src_buffer = NULL;
188 int ret = MM_ERROR_NONE;
189 unsigned char *dst_frame = NULL;
190 unsigned int dst_width = 0;
191 unsigned int dst_height = 0;
192 unsigned int dst_size = 0;
193 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
195 player->capture.orientation = orientation;
197 if (orientation == 90 || orientation == 270) {
198 dst_width = player->captured.height[0];
199 dst_height = player->captured.width[0];
200 LOGD("interchange width & height");
201 } else if (orientation == 180) {
202 dst_width = player->captured.width[0];
203 dst_height = player->captured.height[0];
204 } else if (orientation == 0) {
205 LOGE("no need handle orientation : %d", orientation);
206 player->capture.width = player->captured.width[0];
207 player->capture.height = player->captured.height[0];
208 return MM_ERROR_NONE;
210 LOGE("wrong orientation value...");
212 /* height & width will be interchanged for 90 and 270 orientation */
213 ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
214 if (ret != MM_ERROR_NONE) {
215 LOGE("failed to get destination frame size");
219 LOGD("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
221 dst_frame = (unsigned char*) malloc(dst_size);
223 LOGE("failed to allocate memory");
224 return MM_ERROR_PLAYER_NO_FREE_SPACE;
227 src_buffer = (unsigned char*)player->capture.data;
229 /* convert orientation degree into enum here */
230 switch (orientation) {
232 rot_enum = MM_UTIL_ROTATE_0;
235 rot_enum = MM_UTIL_ROTATE_90;
238 rot_enum = MM_UTIL_ROTATE_180;
241 rot_enum = MM_UTIL_ROTATE_270;
244 LOGE("wrong rotate value");
248 LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
250 ret = mm_util_rotate_image(src_buffer,
251 player->captured.width[0], player->captured.height[0], format,
252 dst_frame, &dst_width, &dst_height, rot_enum);
253 if (ret != MM_ERROR_NONE) {
254 LOGE("failed to do rotate image");
259 LOGD("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
263 player->capture.data = dst_frame;
264 player->capture.size = dst_size;
265 player->capture.orientation = orientation;
266 player->capture.width = dst_width;
267 player->capture.height = dst_height;
269 player->captured.width[0] = player->captured.stride_width[0] = dst_width;
270 player->captured.height[0] = player->captured.stride_height[0] = dst_height;
277 __mmplayer_capture_thread(gpointer data)
279 mm_player_t* player = (mm_player_t*) data;
280 MMMessageParamType msg = {0, };
281 unsigned char * linear_y_plane = NULL;
282 unsigned char * linear_uv_plane = NULL;
286 MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
288 g_mutex_lock(&player->capture_thread_mutex);
289 while (!player->capture_thread_exit) {
290 LOGD("capture thread started. waiting for signal");
291 g_cond_wait(&player->capture_thread_cond, &player->capture_thread_mutex);
293 if (player->capture_thread_exit) {
294 LOGD("exiting capture thread");
297 LOGD("capture thread is received signal");
299 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
300 * Because deadlock can be happened if other player api is used in message callback.
302 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
303 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
305 int linear_y_plane_size;
306 int linear_uv_plane_size;
307 int width = player->captured.width[0];
308 int height = player->captured.height[0];
309 unsigned char * src_buffer = NULL;
311 linear_y_plane_size = (width * height);
312 linear_uv_plane_size = (width * height / 2);
314 linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
315 if (linear_y_plane == NULL) {
316 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
320 linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
321 if (linear_uv_plane == NULL) {
322 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
325 /* NV12 tiled to linear */
326 __csc_tiled_to_linear_crop(linear_y_plane,
327 player->captured.data[0], width, height, 0, 0, 0, 0);
328 __csc_tiled_to_linear_crop(linear_uv_plane,
329 player->captured.data[1], width, height / 2, 0, 0, 0, 0);
331 MMPLAYER_FREEIF(player->captured.data[0]);
332 MMPLAYER_FREEIF(player->captured.data[1]);
334 src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
336 if (src_buffer == NULL) {
337 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
340 memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
341 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
342 memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
344 /* NV12 linear to RGB888 */
345 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
346 width, height, MM_UTIL_IMG_FMT_RGB888);
348 if (ret != MM_ERROR_NONE) {
349 LOGE("failed to convert nv12 linear");
353 MMPLAYER_FREEIF(src_buffer);
354 MMPLAYER_FREEIF(linear_y_plane);
355 MMPLAYER_FREEIF(linear_uv_plane);
356 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
357 #define MM_ALIGN(x, a) (((x) +(a) - 1) & ~((a) - 1))
359 char *src_buffer = NULL;
360 /* using original width otherwises, app can't know aligned to resize */
361 int width_align = player->captured.width[0];
362 int y_size = width_align * player->captured.height[0];
363 int uv_size = width_align * player->captured.height[1];
364 int src_buffer_size = y_size + uv_size;
369 if (!src_buffer_size) {
370 LOGE("invalid data size");
374 src_buffer = (char*) g_try_malloc(src_buffer_size);
377 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
381 memset(src_buffer, 0x00, src_buffer_size);
383 temp = player->captured.data[0];
384 dst_buf = src_buffer;
387 for (i = 0; i < player->captured.height[0]; i++) {
388 memcpy(dst_buf, temp, width_align);
389 dst_buf += width_align;
390 temp += player->captured.stride_width[0];
393 temp = player->captured.data[1];
396 for (j = 0; j < player->captured.height[1]; j++) {
397 memcpy(dst_buf, temp, width_align);
398 dst_buf += width_align;
399 temp += player->captured.stride_width[1];
402 /* free captured buf */
403 MMPLAYER_FREEIF(player->captured.data[0]);
404 MMPLAYER_FREEIF(player->captured.data[1]);
407 ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
408 width_align, player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
409 if (ret != MM_ERROR_NONE) {
410 LOGE("failed to convert nv12 linear");
415 ret = _mmplayer_get_video_rotate_angle((MMHandleType)player, &orientation);
416 if (ret != MM_ERROR_NONE) {
417 LOGE("failed to get rotation angle");
421 LOGD("orientation value = %d", orientation);
423 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_IMG_FMT_RGB888);
424 if (ret != MM_ERROR_NONE) {
425 LOGE("failed to convert nv12 linear");
429 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
430 msg.data = &player->capture;
431 msg.size = player->capture.size;
432 // msg.captured_frame.width = player->capture.width;
433 // msg.captured_frame.height = player->capture.height;
434 // msg.captured_frame.orientation = player->capture.orientation;
436 if (player->cmd >= MMPLAYER_COMMAND_START) {
437 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
438 LOGD("returned from capture message callback");
441 //MMPLAYER_FREEIF(player->capture.data);
444 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
446 MMPLAYER_FREEIF(linear_y_plane);
447 MMPLAYER_FREEIF(linear_uv_plane);
448 MMPLAYER_FREEIF(player->captured.data[0]);
449 MMPLAYER_FREEIF(player->captured.data[1]);
450 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
451 MMPLAYER_FREEIF(player->captured.data[0]);
452 MMPLAYER_FREEIF(player->captured.data[1]);
455 msg.union_type = MM_MSG_UNION_CODE;
457 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
461 g_mutex_unlock(&player->capture_thread_mutex);
466 * The output is fixed as RGB888
469 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
471 gint yplane_size = 0;
472 gint uvplane_size = 0;
475 GstCaps *caps = NULL;
476 GstStructure *structure = NULL;
477 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
478 GstMemory *memory = NULL;
479 mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
480 mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
484 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
485 MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
488 caps = gst_pad_get_current_caps(pad);
490 MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
491 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
493 structure = gst_caps_get_structure(caps, 0);
495 MMPLAYER_RETURN_VAL_IF_FAIL(structure != NULL, MM_ERROR_PLAYER_INTERNAL);
497 /* init capture image buffer */
498 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
500 gst_structure_get_int(structure, "width", &src_width);
501 gst_structure_get_int(structure, "height", &src_height);
503 /* check rgb or yuv */
504 if (gst_structure_has_name(structure, "video/x-raw")) {
506 const gchar *gst_format = gst_structure_get_string(structure, "format");
507 if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12")) {
510 tbm_bo_handle uv_hnd;
511 LOGI("captured format is %s\n", gst_format);
513 MMVideoBuffer *proved = NULL;
514 if (!g_strcmp0(gst_format, "ST12"))
515 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
517 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
519 /* get video frame info from proved buffer */
520 n = gst_buffer_n_memory(buffer);
521 memory = gst_buffer_peek_memory(buffer, n-1);
522 gst_memory_map(memory, &mapinfo, GST_MAP_READ);
523 proved = (MMVideoBuffer *)mapinfo.data;
525 if (!proved || !proved->data[0] || !proved->data[1]) {
526 LOGE("fail to gst_memory_map");
527 return MM_ERROR_PLAYER_INTERNAL;
530 memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
532 yplane_size = proved->size[0];
533 uvplane_size = proved->size[1];
534 LOGI("yplane_size=%d, uvplane_size=%d", yplane_size, uvplane_size);
536 player->captured.data[0] = g_try_malloc(yplane_size);
537 if (!player->captured.data[0]) {
538 gst_memory_unmap(memory, &mapinfo);
539 return MM_ERROR_SOUND_NO_FREE_SPACE;
544 player->captured.data[1] = g_try_malloc(uvplane_size);
546 if (uvplane_size < proved->stride_width[1] * proved->stride_height[1]) {
547 player->captured.data[1] =
548 g_try_malloc(proved->stride_width[1] * proved->stride_height[1]);
550 player->captured.data[1] = g_try_malloc(uvplane_size);
552 if (!player->captured.data[1]) {
553 gst_memory_unmap(memory, &mapinfo);
554 return MM_ERROR_SOUND_NO_FREE_SPACE;
557 LOGD("Buffer type %d", proved->type);
558 if (proved->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
559 tbm_bo_ref(proved->handle.bo[0]);
560 tbm_bo_ref(proved->handle.bo[1]);
561 y_hnd = tbm_bo_get_handle(proved->handle.bo[0], TBM_DEVICE_CPU);
562 LOGD("source y : %p, size %d", y_hnd.ptr, yplane_size);
564 memcpy(player->captured.data[0], y_hnd.ptr, yplane_size);
566 uv_hnd = tbm_bo_get_handle(proved->handle.bo[1], TBM_DEVICE_CPU);
567 LOGD("source uv : %p, size %d", uv_hnd.ptr, uvplane_size);
569 memcpy(player->captured.data[1], uv_hnd.ptr, uvplane_size);
571 tbm_bo_unref(proved->handle.bo[0]);
572 tbm_bo_unref(proved->handle.bo[1]);
575 gst_memory_unmap(memory, &mapinfo);
579 GstVideoInfo format_info;
580 gst_video_info_from_caps(&format_info, caps);
582 switch (GST_VIDEO_INFO_FORMAT(&format_info)) {
583 case GST_VIDEO_FORMAT_I420:
584 src_fmt = MM_UTIL_IMG_FMT_I420;
586 case GST_VIDEO_FORMAT_BGRA:
587 src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
589 case GST_VIDEO_FORMAT_BGRx:
590 src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
600 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
601 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
602 gst_buffer_unmap(buffer, &mapinfo);
605 /* do convert colorspace */
606 g_cond_signal(&player->capture_thread_cond);
610 return MM_ERROR_NONE;
613 LOGE("unknown format to capture\n");
614 return MM_ERROR_PLAYER_INTERNAL;
617 static GstPadProbeReturn
618 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
620 mm_player_t* player = (mm_player_t*) u_data;
621 GstBuffer *buffer = NULL;
622 int ret = MM_ERROR_NONE;
624 MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
627 buffer = gst_pad_probe_info_get_buffer(info);
628 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
630 if (ret != MM_ERROR_NONE) {
631 LOGE("failed to get video frame");
632 return GST_PAD_PROBE_REMOVE;
635 /* remove probe to be called at one time */
636 if (player->video_capture_cb_probe_id) {
637 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
638 player->video_capture_cb_probe_id = 0;
643 return GST_PAD_PROBE_OK;
647 __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_img_format src_fmt, unsigned int src_w, unsigned int src_h, mm_util_img_format dst_fmt)
649 unsigned char *dst_data = NULL;
650 unsigned int dst_size;
651 int ret = MM_ERROR_NONE;
653 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
654 ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
656 if (ret != MM_ERROR_NONE) {
657 LOGE("failed to get image size for capture, %d\n", ret);
658 return MM_ERROR_PLAYER_INTERNAL;
661 SECURE_LOGD("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
663 dst_data = (unsigned char*)g_malloc0(dst_size);
666 LOGE("no free space to capture\n");
668 return MM_ERROR_PLAYER_NO_FREE_SPACE;
671 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
673 if (ret != MM_ERROR_NONE) {
674 LOGE("failed to convert for capture, %d\n", ret);
676 return MM_ERROR_PLAYER_INTERNAL;
679 player->capture.size = dst_size;
680 player->capture.data = dst_data;
682 return MM_ERROR_NONE;
686 * Get tiled address of position(x,y)
692 * height of tiled[in]
695 * x position of tield[in]
698 * y position of tield[in]
701 * address of tiled data
704 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
706 int pixel_x_m1, pixel_y_m1;
708 int linear_addr0, linear_addr1, bank_addr ;
712 pixel_x_m1 = x_size -1;
713 pixel_y_m1 = y_size -1;
715 roundup_x = ((pixel_x_m1 >> 7) + 1);
719 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
720 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
721 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
722 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 6) & 0x3f));
724 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
725 bank_addr = ((x_addr >> 4) & 0x1);
727 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
729 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
730 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 5) & 0x7f));
732 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
733 bank_addr = ((x_addr >> 4) & 0x1);
735 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
738 linear_addr0 = linear_addr0 << 2;
739 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
745 * Converts tiled data to linear
746 * Crops left, top, right, buttom
747 * 1. Y of NV12T to Y of YUV420P
748 * 2. Y of NV12T to Y of YUV420S
749 * 3. UV of NV12T to UV of YUV420S
752 * Y or UV plane address of YUV420[out]
755 * Y or UV plane address of NV12T[in]
757 * @param yuv420_width
758 * Width of YUV420[in]
760 * @param yuv420_height
761 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
773 * Crop size of buttom
776 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
777 int left, int top, int right, int buttom)
780 int tiled_offset = 0, tiled_offset1 = 0;
781 int linear_offset = 0;
782 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
784 temp3 = yuv420_width-right;
786 /* real width is greater than or equal 256 */
788 for (i = top; i < yuv420_height-buttom; i = i+1) {
794 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
795 tiled_offset = temp4-1;
796 temp1 = ((yuv420_width+127)>>7)<<7;
797 tiled_offset = tiled_offset*(temp1>>6);
798 tiled_offset = tiled_offset+temp3;
799 tiled_offset = tiled_offset+2;
800 temp1 = (temp3>>2)<<2;
801 tiled_offset = tiled_offset+temp1;
802 tiled_offset = tiled_offset<<11;
803 tiled_offset1 = tiled_offset+2048*2;
806 temp2 = ((yuv420_height+31)>>5)<<5;
807 if ((i+32) < temp2) {
808 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
810 temp1 = (temp1>>2)<<2;
811 tiled_offset = temp3+temp1;
812 temp1 = ((yuv420_width+127)>>7)<<7;
813 tiled_offset = tiled_offset+temp4*(temp1>>6);
814 tiled_offset = tiled_offset<<11;
815 tiled_offset1 = tiled_offset+2048*6;
818 /* even2 fomula: x+x_block_num*y */
819 temp1 = ((yuv420_width+127)>>7)<<7;
820 tiled_offset = temp4*(temp1>>6);
821 tiled_offset = tiled_offset+temp3;
822 tiled_offset = tiled_offset<<11;
823 tiled_offset1 = tiled_offset+2048*2;
829 tiled_offset = tiled_offset+64*(temp1);
830 tiled_offset1 = tiled_offset1+64*(temp1);
831 temp2 = yuv420_width-left-right;
832 linear_offset = temp2*(i-top);
833 temp3 = ((j+256)>>8)<<8;
837 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
838 temp2 = ((left+63)>>6)<<6;
839 temp3 = ((yuv420_width-right)>>6)<<6;
841 temp2 = yuv420_width-right-(64-temp1);
842 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
843 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
844 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
845 linear_offset = linear_offset+256-temp1;
846 } else if (temp3 > 128) {
847 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
848 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
849 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
850 linear_offset = linear_offset+192-temp1;
851 } else if (temp3 > 64) {
852 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
853 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
854 linear_offset = linear_offset+128-temp1;
855 } else if (temp3 > 0) {
856 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
857 linear_offset = linear_offset+64-temp1;
860 tiled_offset = tiled_offset+temp4*2048;
863 temp2 = yuv420_width-right-256;
864 for (; j <= temp2; j = j+256) {
865 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
866 tiled_offset1 = tiled_offset1+temp4*2048;
867 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
868 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
869 tiled_offset = tiled_offset+temp4*2048;
870 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
871 linear_offset = linear_offset+256;
874 tiled_offset1 = tiled_offset1+temp4*2048;
875 temp2 = yuv420_width-right-j;
877 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
878 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
879 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
880 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
881 } else if (temp2 > 128) {
882 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
883 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
884 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
885 } else if (temp2 > 64) {
886 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
887 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
889 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
891 } else if (temp1 >= 64) {
892 for (i = top; i < (yuv420_height-buttom); i = i+1) {
894 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
895 temp2 = ((j+64)>>6)<<6;
897 linear_offset = temp1*(i-top);
899 tiled_offset = tiled_offset+temp4;
900 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
901 linear_offset = linear_offset+temp2;
903 if ((j+64) <= temp3) {
904 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
905 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
906 linear_offset = linear_offset+64;
909 if ((j+64) <= temp3) {
910 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
911 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
912 linear_offset = linear_offset+64;
916 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
918 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
922 for (i = top; i < (yuv420_height-buttom); i = i+1) {
923 linear_offset = temp1*(i-top);
924 for (j = left; j < (yuv420_width-right); j = j+2) {
925 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
927 tiled_offset = tiled_offset+temp4;
928 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
929 linear_offset = linear_offset+2;