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 ========================================================================================== */
28 #include "mm_player_utils.h"
29 #include "mm_player_capture.h"
30 #include "mm_player_priv.h"
31 #include "mm_player_utils.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 return_val_if_fail ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
59 /* create capture mutex */
60 g_mutex_init(&player->capture_thread_mutex);
61 if ( !(&player->capture_thread_mutex) )
63 debug_error("Cannot create capture mutex");
67 /* create capture cond */
68 g_cond_init(&player->capture_thread_cond);
69 if (!(&player->capture_thread_cond) )
71 debug_error("Cannot create capture 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 )
90 if ( &player->capture_thread_mutex )
91 g_mutex_clear(&player->capture_thread_mutex );
93 if (&player->capture_thread_cond )
94 g_cond_clear (&player->capture_thread_cond );
96 return MM_ERROR_PLAYER_INTERNAL;
100 _mmplayer_release_video_capture(mm_player_t* player)
102 return_val_if_fail ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
103 /* release capture thread */
104 if ( &player->capture_thread_cond &&
105 &player->capture_thread_mutex &&
106 player->capture_thread )
108 g_mutex_lock(&player->capture_thread_mutex);
109 player->capture_thread_exit = TRUE;
110 g_cond_signal( &player->capture_thread_cond );
111 g_mutex_unlock(&player->capture_thread_mutex);
113 debug_log("waitting for capture thread exit");
114 g_thread_join ( player->capture_thread );
115 g_mutex_clear(&player->capture_thread_mutex );
116 g_cond_clear(&player->capture_thread_cond );
117 debug_log("capture thread released");
120 return MM_ERROR_NONE;
124 _mmplayer_do_video_capture(MMHandleType hplayer)
126 mm_player_t* player = (mm_player_t*) hplayer;
127 int ret = MM_ERROR_NONE;
132 return_val_if_fail(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
134 /* capturing or not */
135 if (player->video_capture_cb_probe_id || player->capture.data || player->captured.a[0] || player->captured.a[1])
137 debug_warning("capturing... we can't do any more");
138 return MM_ERROR_PLAYER_INVALID_STATE;
141 /* check if video pipeline is linked or not */
142 if (!player->pipeline->videobin)
144 debug_warning("not ready to capture");
145 return MM_ERROR_PLAYER_INVALID_STATE;
148 /* check if drm file */
149 if (player->is_drm_file)
151 debug_warning("not supported in drm file");
152 return MM_ERROR_PLAYER_DRM_OUTPUT_PROTECTION;
155 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink" );
157 if (player->state != MM_PLAYER_STATE_PLAYING)
159 if (player->state == MM_PLAYER_STATE_PAUSED) // get last buffer from video sink
161 GstSample *sample = NULL;
163 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
165 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
169 GstBuffer *buf = NULL;
170 buf = gst_sample_get_buffer(sample);
174 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buf);
178 debug_warning("failed to get video frame");
180 gst_sample_unref(sample);
186 debug_warning("invalid state(%d) to capture", player->state);
187 return MM_ERROR_PLAYER_INVALID_STATE;
192 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
193 __mmplayer_video_capture_probe, player, NULL);
195 gst_object_unref(GST_OBJECT(pad));
204 __mmplayer_handle_orientation (mm_player_t* player, int orientation, int format)
206 unsigned char *src_buffer = NULL;
207 int ret = MM_ERROR_NONE;
208 unsigned char *dst_frame = NULL;
209 unsigned int dst_width = 0;
210 unsigned int dst_height = 0;
211 unsigned int dst_size = 0;
212 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
214 player->capture.orientation = orientation;
216 if (orientation == 90 || orientation == 270)
218 dst_width = player->captured.h[0];
219 dst_height = player->captured.w[0];
220 debug_log ("interchange width & height");
222 else if (orientation == 180)
224 dst_width = player->captured.w[0];
225 dst_height = player->captured.h[0];
227 else if (orientation == 0)
229 debug_error ("no need handle orientation : %d", orientation);
230 player->capture.width = player->captured.w[0];
231 player->capture.height = player->captured.h[0];
232 return MM_ERROR_NONE;
236 debug_error ("wrong orientation value...");
239 /* height & width will be interchanged for 90 and 270 orientation */
240 ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
241 if (ret != MM_ERROR_NONE)
243 debug_error("failed to get destination frame size");
247 debug_log ("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
249 dst_frame = (unsigned char*) malloc (dst_size);
252 debug_error("failed to allocate memory");
253 return MM_ERROR_PLAYER_NO_FREE_SPACE;
256 src_buffer = (unsigned char*)player->capture.data;
258 /* convert orientation degree into enum here */
262 rot_enum = MM_UTIL_ROTATE_0;
265 rot_enum = MM_UTIL_ROTATE_90;
268 rot_enum = MM_UTIL_ROTATE_180;
271 rot_enum = MM_UTIL_ROTATE_270;
274 debug_error("wrong rotate value");
278 debug_log ("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
280 ret = mm_util_rotate_image (src_buffer, player->captured.w[0], player->captured.h[0], format,
281 dst_frame, &dst_width, &dst_height, rot_enum);
282 if (ret != MM_ERROR_NONE)
284 debug_error("failed to do rotate image");
288 debug_log ("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
292 player->capture.data = dst_frame;
293 player->capture.size = dst_size;
294 player->capture.orientation = orientation;
295 player->capture.width = dst_width;
296 player->capture.height= dst_height;
298 player->captured.w[0] = player->captured.s[0] = dst_width;
299 player->captured.h[0] = player->captured.e[0] = dst_height;
306 __mmplayer_capture_thread(gpointer data)
308 mm_player_t* player = (mm_player_t*) data;
309 MMMessageParamType msg = {0, };
310 unsigned char * linear_y_plane = NULL;
311 unsigned char * linear_uv_plane = NULL;
315 return_val_if_fail(player, NULL);
317 while (!player->capture_thread_exit)
319 debug_log("capture thread started. waiting for signal");
321 g_mutex_lock(&player->capture_thread_mutex);
322 g_cond_wait(&player->capture_thread_cond, &player->capture_thread_mutex );
324 if ( player->capture_thread_exit )
326 debug_log("exiting capture thread");
329 debug_log("capture thread is recieved signal");
331 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
332 * Because deadlock can be happened if other player api is used in message callback.
334 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
336 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
338 int linear_y_plane_size;
339 int linear_uv_plane_size;
340 unsigned char * src_buffer = NULL;
342 linear_y_plane_size = (player->captured.w[0] * player->captured.h[0]);
343 linear_uv_plane_size = (player->captured.w[0] * player->captured.h[0]/2);
345 linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
346 if (linear_y_plane == NULL)
348 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
352 linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
353 if (linear_uv_plane == NULL)
355 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
358 /* NV12 tiled to linear */
359 __csc_tiled_to_linear_crop(linear_y_plane, player->captured.a[0], player->captured.w[0], player->captured.h[0], 0,0,0,0);
360 __csc_tiled_to_linear_crop(linear_uv_plane, player->captured.a[1], player->captured.w[0], player->captured.h[0]/2, 0,0,0,0);
362 MMPLAYER_FREEIF(player->captured.a[0]);
363 MMPLAYER_FREEIF(player->captured.a[1]);
365 src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
367 if (src_buffer == NULL)
369 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
372 memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
373 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
374 memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
376 /* NV12 linear to RGB888 */
377 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
378 player->captured.w[0], player->captured.h[0], MM_UTIL_IMG_FMT_RGB888);
380 if (ret != MM_ERROR_NONE)
382 debug_error("failed to convert nv12 linear");
386 MMPLAYER_FREEIF(src_buffer);
387 MMPLAYER_FREEIF(linear_y_plane);
388 MMPLAYER_FREEIF(linear_uv_plane);
389 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
390 #define MM_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
392 char *src_buffer = NULL;
393 /* using original width otherwises, app can't know aligned to resize */
394 int width_align = player->captured.w[0];//MM_ALIGN(player->captured.w[0], 8);
395 int src_buffer_size = width_align * player->captured.h[0] * 3/2;
400 if (!src_buffer_size) {
401 debug_error("invalid data size");
405 src_buffer = (char*) g_try_malloc(src_buffer_size);
408 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
412 memset(src_buffer, 0x00, src_buffer_size);
414 temp = player->captured.a[0];
415 dst_buf = src_buffer;
418 for (i = 0; i < player->captured.h[0]; i++) {
419 memcpy(dst_buf, temp, width_align);
420 dst_buf += width_align;
421 temp += player->captured.s[0];
424 temp = player->captured.a[1];
427 for (j = 0; j < player->captured.h[1]; j++) {
428 memcpy(dst_buf, temp, width_align);
429 dst_buf += width_align;
430 temp += player->captured.s[0];
433 /* free captured buf */
434 MMPLAYER_FREEIF(player->captured.a[0]);
435 MMPLAYER_FREEIF(player->captured.a[1]);
438 ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
439 width_align, player->captured.h[0], MM_UTIL_IMG_FMT_RGB888);
440 if (ret != MM_ERROR_NONE)
442 debug_error("failed to convert nv12 linear");
448 ret = _mmplayer_get_video_rotate_angle ((MMHandleType)player, &orientation);
449 if (ret != MM_ERROR_NONE)
451 debug_error("failed to get rotation angle");
455 debug_log ("orientation value = %d", orientation);
457 ret = __mmplayer_handle_orientation (player, orientation, MM_UTIL_IMG_FMT_RGB888);
458 if (ret != MM_ERROR_NONE)
460 debug_error("failed to convert nv12 linear");
464 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
465 msg.data = &player->capture;
466 msg.size = player->capture.size;
467 // msg.captured_frame.width = player->capture.width;
468 // msg.captured_frame.height = player->capture.height;
469 // msg.captured_frame.orientation = player->capture.orientation;
471 if (player->cmd >= MMPLAYER_COMMAND_START)
473 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_CAPTURED, &msg );
474 debug_log("returned from capture message callback");
477 g_mutex_unlock(&player->capture_thread_mutex);
479 //MMPLAYER_FREEIF(player->capture.data);
482 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
485 MMPLAYER_FREEIF(linear_y_plane);
486 MMPLAYER_FREEIF(linear_uv_plane);
487 MMPLAYER_FREEIF(player->captured.a[0]);
488 MMPLAYER_FREEIF(player->captured.a[1]);
491 msg.union_type = MM_MSG_UNION_CODE;
493 g_mutex_unlock(&player->capture_thread_mutex);
494 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg );
498 g_mutex_unlock(&player->capture_thread_mutex);
503 * The output is fixed as RGB888
506 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
508 gint yplane_size = 0;
509 gint uvplane_size = 0;
512 GstCaps *caps = NULL;
513 GstStructure *structure = NULL;
514 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
515 GstMemory *memory = NULL;
516 mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
517 mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
521 return_val_if_fail ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
522 return_val_if_fail ( buffer, MM_ERROR_INVALID_ARGUMENT );
525 caps = gst_pad_get_current_caps(pad);
527 return_val_if_fail ( caps, MM_ERROR_INVALID_ARGUMENT );
528 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
530 structure = gst_caps_get_structure (caps, 0);
532 return_val_if_fail (structure != NULL, MM_ERROR_PLAYER_INTERNAL);
534 /* init capture image buffer */
535 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
537 gst_structure_get_int (structure, "width", &src_width);
538 gst_structure_get_int (structure, "height", &src_height);
540 /* check rgb or yuv */
541 if (gst_structure_has_name(structure, "video/x-raw"))
544 if(!g_strcmp0(gst_structure_get_string(structure, "format"), "ST12"))
546 debug_msg ("captured format is ST12\n");
548 MMPlayerMPlaneImage *proved = NULL;
549 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
551 /* get video frame info from proved buffer */
552 // memory = gst_buffer_get_memory(buffer, 1);
553 memory = gst_buffer_get_all_memory(buffer);
554 gst_memory_map(memory, &mapinfo, GST_MAP_READ);
555 proved = (MMPlayerMPlaneImage *)mapinfo.data;
557 if ( !proved || !proved->a[0] || !proved->a[1] )
558 return MM_ERROR_PLAYER_INTERNAL;
560 yplane_size = proved->y_size;
561 uvplane_size = proved->uv_size;
563 debug_msg ("yplane_size=%d, uvplane_size=%d\n", yplane_size, uvplane_size);
564 memset(&player->captured, 0x00, sizeof(MMPlayerMPlaneImage));
565 memcpy(&player->captured, proved, sizeof(MMPlayerMPlaneImage));
567 player->captured.a[0] = g_try_malloc(yplane_size);
568 if ( !player->captured.a[0] ) {
569 gst_memory_unmap(memory, &mapinfo);
570 return MM_ERROR_SOUND_NO_FREE_SPACE;
573 player->captured.a[1] = g_try_malloc(uvplane_size);
574 if ( !player->captured.a[1] ) {
575 gst_memory_unmap(memory, &mapinfo);
576 return MM_ERROR_SOUND_NO_FREE_SPACE;
579 memcpy(player->captured.a[0], proved->a[0], yplane_size);
580 memcpy(player->captured.a[1], proved->a[1], uvplane_size);
582 gst_memory_unmap(memory, &mapinfo);
588 GstVideoInfo format_info;
589 gst_video_info_from_caps(&format_info, caps);
591 switch(GST_VIDEO_INFO_FORMAT(&format_info))
593 case GST_VIDEO_FORMAT_I420:
594 src_fmt = MM_UTIL_IMG_FMT_I420;
596 case GST_VIDEO_FORMAT_BGRA:
597 src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
599 case GST_VIDEO_FORMAT_BGRx:
600 src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
609 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
611 MMPlayerMPlaneImage *proved = NULL;
612 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
614 /* get video frame info from proved buffer */
615 proved = (MMPlayerMPlaneImage *)GST_BUFFER_MALLOCDATA(buffer);
617 if (!proved || !proved->a[0] || !proved->a[1])
618 return MM_ERROR_PLAYER_INTERNAL;
620 memset(&player->captured, 0x00, sizeof(MMPlayerMPlaneImage));
621 memcpy(&player->captured, proved, sizeof(MMPlayerMPlaneImage));
623 player->captured.y_size = proved->s[0] * proved->h[0]; // must get data including padding
624 player->captured.uv_size = proved->s[0] * proved->h[1];
626 debug_msg ("y plane_size : %d, uv plane_size : %d", player->captured.y_size, player->captured.uv_size);
628 player->captured.a[0] = g_try_malloc(player->captured.y_size);
630 if ( !player->captured.a[0] ) {
631 return MM_ERROR_SOUND_NO_FREE_SPACE;
634 player->captured.a[1] = g_try_malloc(player->captured.uv_size);
636 if ( !player->captured.a[1] ) {
637 return MM_ERROR_SOUND_NO_FREE_SPACE;
640 memcpy(player->captured.a[0], proved->a[0], player->captured.y_size);
641 memcpy(player->captured.a[1], proved->a[1], player->captured.uv_size);
653 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
654 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
655 gst_buffer_unmap(buffer, &mapinfo);
658 /* do convert colorspace */
659 g_cond_signal( &player->capture_thread_cond );
663 return MM_ERROR_NONE;
666 debug_error("unknown format to capture\n");
667 return MM_ERROR_PLAYER_INTERNAL;
670 static GstPadProbeReturn
671 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
673 mm_player_t* player = (mm_player_t*) u_data;
674 GstBuffer *buffer = NULL;
675 int ret = MM_ERROR_NONE;
677 return_val_if_fail (info->data, GST_PAD_PROBE_REMOVE);
680 buffer = gst_pad_probe_info_get_buffer(info);
681 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
683 if ( ret != MM_ERROR_NONE)
685 debug_error("failed to get video frame");
686 return GST_PAD_PROBE_REMOVE;
689 /* remove probe to be called at one time */
690 if (player->video_capture_cb_probe_id)
692 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
693 player->video_capture_cb_probe_id = 0;
698 return GST_PAD_PROBE_OK;
702 __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)
704 unsigned char *dst_data = NULL;
705 unsigned int dst_size;
706 int ret = MM_ERROR_NONE;
708 return_val_if_fail(player, MM_ERROR_PLAYER_INTERNAL);
709 ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
711 if (ret != MM_ERROR_NONE)
713 debug_error("failed to get image size for capture, %d\n", ret);
714 return MM_ERROR_PLAYER_INTERNAL;
717 secure_debug_log("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
719 dst_data = (unsigned char*)g_malloc0(dst_size);
723 debug_error("no free space to capture\n");
724 return MM_ERROR_PLAYER_NO_FREE_SPACE;
727 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
729 if (ret != MM_ERROR_NONE)
731 debug_error("failed to convert for capture, %d\n", ret);
732 return MM_ERROR_PLAYER_INTERNAL;
735 player->capture.size = dst_size;
736 player->capture.data = dst_data;
738 return MM_ERROR_NONE;
742 * Get tiled address of position(x,y)
748 * height of tiled[in]
751 * x position of tield[in]
754 * y position of tield[in]
757 * address of tiled data
760 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
762 int pixel_x_m1, pixel_y_m1;
764 int linear_addr0, linear_addr1, bank_addr ;
768 pixel_x_m1 = x_size -1;
769 pixel_y_m1 = y_size -1;
771 roundup_x = ((pixel_x_m1 >> 7) + 1);
775 if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
776 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
777 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
778 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
780 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
781 bank_addr = ((x_addr >> 4) & 0x1);
783 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
785 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
786 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
788 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
789 bank_addr = ((x_addr >> 4) & 0x1);
791 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
794 linear_addr0 = linear_addr0 << 2;
795 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
801 * Converts tiled data to linear
802 * Crops left, top, right, buttom
803 * 1. Y of NV12T to Y of YUV420P
804 * 2. Y of NV12T to Y of YUV420S
805 * 3. UV of NV12T to UV of YUV420S
808 * Y or UV plane address of YUV420[out]
811 * Y or UV plane address of NV12T[in]
813 * @param yuv420_width
814 * Width of YUV420[in]
816 * @param yuv420_height
817 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
829 * Crop size of buttom
832 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
833 int left, int top, int right, int buttom)
836 int tiled_offset = 0, tiled_offset1 = 0;
837 int linear_offset = 0;
838 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
840 temp3 = yuv420_width-right;
842 /* real width is greater than or equal 256 */
844 for (i=top; i<yuv420_height-buttom; i=i+1) {
850 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
851 tiled_offset = temp4-1;
852 temp1 = ((yuv420_width+127)>>7)<<7;
853 tiled_offset = tiled_offset*(temp1>>6);
854 tiled_offset = tiled_offset+temp3;
855 tiled_offset = tiled_offset+2;
856 temp1 = (temp3>>2)<<2;
857 tiled_offset = tiled_offset+temp1;
858 tiled_offset = tiled_offset<<11;
859 tiled_offset1 = tiled_offset+2048*2;
862 temp2 = ((yuv420_height+31)>>5)<<5;
864 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
866 temp1 = (temp1>>2)<<2;
867 tiled_offset = temp3+temp1;
868 temp1 = ((yuv420_width+127)>>7)<<7;
869 tiled_offset = tiled_offset+temp4*(temp1>>6);
870 tiled_offset = tiled_offset<<11;
871 tiled_offset1 = tiled_offset+2048*6;
874 /* even2 fomula: x+x_block_num*y */
875 temp1 = ((yuv420_width+127)>>7)<<7;
876 tiled_offset = temp4*(temp1>>6);
877 tiled_offset = tiled_offset+temp3;
878 tiled_offset = tiled_offset<<11;
879 tiled_offset1 = tiled_offset+2048*2;
885 tiled_offset = tiled_offset+64*(temp1);
886 tiled_offset1 = tiled_offset1+64*(temp1);
887 temp2 = yuv420_width-left-right;
888 linear_offset = temp2*(i-top);
889 temp3 = ((j+256)>>8)<<8;
893 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
894 temp2 = ((left+63)>>6)<<6;
895 temp3 = ((yuv420_width-right)>>6)<<6;
896 if (temp2 == temp3) {
897 temp2 = yuv420_width-right-(64-temp1);
899 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
900 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
901 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
902 linear_offset = linear_offset+256-temp1;
903 } else if (temp3 > 128) {
904 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
905 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
906 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
907 linear_offset = linear_offset+192-temp1;
908 } else if (temp3 > 64) {
909 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
910 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
911 linear_offset = linear_offset+128-temp1;
912 } else if (temp3 > 0) {
913 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
914 linear_offset = linear_offset+64-temp1;
917 tiled_offset = tiled_offset+temp4*2048;
920 temp2 = yuv420_width-right-256;
921 for (; j<=temp2; j=j+256) {
922 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
923 tiled_offset1 = tiled_offset1+temp4*2048;
924 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
925 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
926 tiled_offset = tiled_offset+temp4*2048;
927 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
928 linear_offset = linear_offset+256;
931 tiled_offset1 = tiled_offset1+temp4*2048;
932 temp2 = yuv420_width-right-j;
934 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
935 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
936 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
937 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
938 } else if (temp2 > 128) {
939 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
940 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
941 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
942 } else if (temp2 > 64) {
943 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
944 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
946 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
949 } else if (temp1 >= 64) {
950 for (i=top; i<(yuv420_height-buttom); i=i+1) {
952 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
953 temp2 = ((j+64)>>6)<<6;
955 linear_offset = temp1*(i-top);
957 tiled_offset = tiled_offset+temp4;
958 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
959 linear_offset = linear_offset+temp2;
961 if ((j+64) <= temp3) {
962 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
963 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
964 linear_offset = linear_offset+64;
967 if ((j+64) <= temp3) {
968 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
969 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
970 linear_offset = linear_offset+64;
974 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
976 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
980 for (i=top; i<(yuv420_height-buttom); i=i+1) {
981 linear_offset = temp1*(i-top);
982 for (j=left; j<(yuv420_width-right); j=j+2) {
983 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
985 tiled_offset = tiled_offset+temp4;
986 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
987 linear_offset = linear_offset+2;