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);
62 /* create capture cond */
63 g_cond_init(&player->capture_thread_cond);
66 player->capture_thread_exit = FALSE;
68 /* create video capture thread */
69 player->capture_thread =
70 g_thread_try_new ("capture_thread",__mmplayer_capture_thread, (gpointer)player, NULL);
72 if ( ! player->capture_thread )
81 g_mutex_clear(&player->capture_thread_mutex );
83 g_cond_clear (&player->capture_thread_cond );
85 return MM_ERROR_PLAYER_INTERNAL;
89 _mmplayer_release_video_capture(mm_player_t* player)
91 return_val_if_fail ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
92 /* release capture thread */
93 g_mutex_lock(&player->capture_thread_mutex);
94 player->capture_thread_exit = TRUE;
95 g_cond_signal( &player->capture_thread_cond );
96 g_mutex_unlock(&player->capture_thread_mutex);
98 debug_log("waitting for capture thread exit");
99 g_thread_join ( player->capture_thread );
100 g_mutex_clear(&player->capture_thread_mutex );
101 g_cond_clear(&player->capture_thread_cond );
102 debug_log("capture thread released");
104 return MM_ERROR_NONE;
108 _mmplayer_do_video_capture(MMHandleType hplayer)
110 mm_player_t* player = (mm_player_t*) hplayer;
111 int ret = MM_ERROR_NONE;
116 return_val_if_fail(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
118 /* capturing or not */
119 if (player->video_capture_cb_probe_id || player->capture.data
120 || player->captured.data[0] || player->captured.data[1]
123 debug_warning("capturing... we can't do any more");
124 return MM_ERROR_PLAYER_INVALID_STATE;
127 /* check if video pipeline is linked or not */
128 if (!player->pipeline->videobin)
130 debug_warning("not ready to capture");
131 return MM_ERROR_PLAYER_INVALID_STATE;
134 /* check if drm file */
135 if (player->is_drm_file)
137 debug_warning("not supported in drm file");
138 return MM_ERROR_PLAYER_DRM_OUTPUT_PROTECTION;
141 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink" );
143 if (player->state != MM_PLAYER_STATE_PLAYING)
145 if (player->state == MM_PLAYER_STATE_PAUSED) // get last buffer from video sink
147 GstSample *sample = NULL;
149 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
151 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
155 GstBuffer *buf = NULL;
156 buf = gst_sample_get_buffer(sample);
160 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buf);
164 debug_warning("failed to get video frame");
166 gst_sample_unref(sample);
172 debug_warning("invalid state(%d) to capture", player->state);
173 return MM_ERROR_PLAYER_INVALID_STATE;
178 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
179 __mmplayer_video_capture_probe, player, NULL);
181 gst_object_unref(GST_OBJECT(pad));
190 __mmplayer_handle_orientation (mm_player_t* player, int orientation, int format)
192 unsigned char *src_buffer = NULL;
193 int ret = MM_ERROR_NONE;
194 unsigned char *dst_frame = NULL;
195 unsigned int dst_width = 0;
196 unsigned int dst_height = 0;
197 unsigned int dst_size = 0;
198 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
200 player->capture.orientation = orientation;
202 if (orientation == 90 || orientation == 270)
204 dst_width = player->captured.height[0];
205 dst_height = player->captured.width[0];
206 debug_log ("interchange width & height");
208 else if (orientation == 180)
210 dst_width = player->captured.width[0];
211 dst_height = player->captured.height[0];
213 else if (orientation == 0)
215 debug_error ("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;
222 debug_error ("wrong orientation value...");
225 /* height & width will be interchanged for 90 and 270 orientation */
226 ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
227 if (ret != MM_ERROR_NONE)
229 debug_error("failed to get destination frame size");
233 debug_log ("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
235 dst_frame = (unsigned char*) malloc (dst_size);
238 debug_error("failed to allocate memory");
239 return MM_ERROR_PLAYER_NO_FREE_SPACE;
242 src_buffer = (unsigned char*)player->capture.data;
244 /* convert orientation degree into enum here */
248 rot_enum = MM_UTIL_ROTATE_0;
251 rot_enum = MM_UTIL_ROTATE_90;
254 rot_enum = MM_UTIL_ROTATE_180;
257 rot_enum = MM_UTIL_ROTATE_270;
260 debug_error("wrong rotate value");
264 debug_log ("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
266 ret = mm_util_rotate_image (src_buffer,
267 player->captured.width[0], player->captured.height[0], format,
268 dst_frame, &dst_width, &dst_height, rot_enum);
269 if (ret != MM_ERROR_NONE)
271 debug_error("failed to do rotate image");
275 debug_log ("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
279 player->capture.data = dst_frame;
280 player->capture.size = dst_size;
281 player->capture.orientation = orientation;
282 player->capture.width = dst_width;
283 player->capture.height= dst_height;
285 player->captured.width[0] = player->captured.stride_width[0] = dst_width;
286 player->captured.height[0] = player->captured.stride_height[0] = dst_height;
293 __mmplayer_capture_thread(gpointer data)
295 mm_player_t* player = (mm_player_t*) data;
296 MMMessageParamType msg = {0, };
297 unsigned char * linear_y_plane = NULL;
298 unsigned char * linear_uv_plane = NULL;
302 return_val_if_fail(player, NULL);
304 while (!player->capture_thread_exit)
306 debug_log("capture thread started. waiting for signal");
308 g_mutex_lock(&player->capture_thread_mutex);
309 g_cond_wait(&player->capture_thread_cond, &player->capture_thread_mutex );
311 if ( player->capture_thread_exit )
313 debug_log("exiting capture thread");
316 debug_log("capture thread is recieved signal");
318 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
319 * Because deadlock can be happened if other player api is used in message callback.
321 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
323 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
325 int linear_y_plane_size;
326 int linear_uv_plane_size;
327 int width = player->captured.width[0];
328 int height = player->captured.height[0];
329 unsigned char * src_buffer = NULL;
331 linear_y_plane_size = (width * height);
332 linear_uv_plane_size = (width * height / 2);
334 linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
335 if (linear_y_plane == NULL)
337 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
341 linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
342 if (linear_uv_plane == NULL)
344 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
347 /* NV12 tiled to linear */
348 __csc_tiled_to_linear_crop(linear_y_plane,
349 player->captured.data[0], width, height, 0,0,0,0);
350 __csc_tiled_to_linear_crop(linear_uv_plane,
351 player->captured.data[1], width, height / 2, 0,0,0,0);
353 MMPLAYER_FREEIF(player->captured.data[0]);
354 MMPLAYER_FREEIF(player->captured.data[1]);
356 src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
358 if (src_buffer == NULL)
360 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
363 memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
364 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
365 memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
367 /* NV12 linear to RGB888 */
368 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
369 width, height, MM_UTIL_IMG_FMT_RGB888);
371 if (ret != MM_ERROR_NONE)
373 debug_error("failed to convert nv12 linear");
377 MMPLAYER_FREEIF(src_buffer);
378 MMPLAYER_FREEIF(linear_y_plane);
379 MMPLAYER_FREEIF(linear_uv_plane);
380 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
381 #define MM_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
383 char *src_buffer = NULL;
384 /* using original width otherwises, app can't know aligned to resize */
385 int width_align = player->captured.width[0];
386 int src_buffer_size = width_align * player->captured.height[0] * 3/2;
391 if (!src_buffer_size) {
392 debug_error("invalid data size");
396 src_buffer = (char*) g_try_malloc(src_buffer_size);
399 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
403 memset(src_buffer, 0x00, src_buffer_size);
405 temp = player->captured.data[0];
406 dst_buf = src_buffer;
409 for (i = 0; i < player->captured.height[0]; i++) {
410 memcpy(dst_buf, temp, width_align);
411 dst_buf += width_align;
412 temp += player->captured.stride_width[0];
415 temp = player->captured.data[1];
418 for (j = 0; j < player->captured.height[1]; j++) {
419 memcpy(dst_buf, temp, width_align);
420 dst_buf += width_align;
421 temp += player->captured.stride_width[0];
424 /* free captured buf */
425 MMPLAYER_FREEIF(player->captured.data[0]);
426 MMPLAYER_FREEIF(player->captured.data[1]);
429 ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
430 width_align, player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
431 if (ret != MM_ERROR_NONE)
433 debug_error("failed to convert nv12 linear");
439 ret = _mmplayer_get_video_rotate_angle ((MMHandleType)player, &orientation);
440 if (ret != MM_ERROR_NONE)
442 debug_error("failed to get rotation angle");
446 debug_log ("orientation value = %d", orientation);
448 ret = __mmplayer_handle_orientation (player, orientation, MM_UTIL_IMG_FMT_RGB888);
449 if (ret != MM_ERROR_NONE)
451 debug_error("failed to convert nv12 linear");
455 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
456 msg.data = &player->capture;
457 msg.size = player->capture.size;
458 // msg.captured_frame.width = player->capture.width;
459 // msg.captured_frame.height = player->capture.height;
460 // msg.captured_frame.orientation = player->capture.orientation;
462 if (player->cmd >= MMPLAYER_COMMAND_START)
464 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_CAPTURED, &msg );
465 debug_log("returned from capture message callback");
468 g_mutex_unlock(&player->capture_thread_mutex);
470 //MMPLAYER_FREEIF(player->capture.data);
473 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED)
476 MMPLAYER_FREEIF(linear_y_plane);
477 MMPLAYER_FREEIF(linear_uv_plane);
478 MMPLAYER_FREEIF(player->captured.data[0]);
479 MMPLAYER_FREEIF(player->captured.data[1]);
482 msg.union_type = MM_MSG_UNION_CODE;
484 g_mutex_unlock(&player->capture_thread_mutex);
485 MMPLAYER_POST_MSG( player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg );
489 g_mutex_unlock(&player->capture_thread_mutex);
494 * The output is fixed as RGB888
497 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
499 gint yplane_size = 0;
500 gint uvplane_size = 0;
503 GstCaps *caps = NULL;
504 GstStructure *structure = NULL;
505 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
506 GstMemory *memory = NULL;
507 mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
508 mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
512 return_val_if_fail ( player, MM_ERROR_PLAYER_NOT_INITIALIZED );
513 return_val_if_fail ( buffer, MM_ERROR_INVALID_ARGUMENT );
516 caps = gst_pad_get_current_caps(pad);
518 return_val_if_fail ( caps, MM_ERROR_INVALID_ARGUMENT );
519 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
521 structure = gst_caps_get_structure (caps, 0);
523 return_val_if_fail (structure != NULL, MM_ERROR_PLAYER_INTERNAL);
525 /* init capture image buffer */
526 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
528 gst_structure_get_int (structure, "width", &src_width);
529 gst_structure_get_int (structure, "height", &src_height);
531 /* check rgb or yuv */
532 if (gst_structure_has_name(structure, "video/x-raw"))
535 if(!g_strcmp0(gst_structure_get_string(structure, "format"), "ST12"))
537 debug_msg ("captured format is ST12\n");
539 MMVideoBuffer *proved = NULL;
540 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
542 /* get video frame info from proved buffer */
543 memory = gst_buffer_get_all_memory(buffer);
544 gst_memory_map(memory, &mapinfo, GST_MAP_READ);
545 proved = (MMVideoBuffer *)mapinfo.data;
547 if ( !proved || !proved->data[0] || !proved->data[1] )
548 return MM_ERROR_PLAYER_INTERNAL;
550 yplane_size = proved->size[0];
551 uvplane_size = proved->size[1];
553 debug_msg ("yplane_size=%d, uvplane_size=%d\n", yplane_size, uvplane_size);
554 memset(&player->captured, 0x00, sizeof(MMVideoBuffer));
555 memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
557 player->captured.data[0] = g_try_malloc(yplane_size);
558 if ( !player->captured.data[0] ) {
559 gst_memory_unmap(memory, &mapinfo);
560 return MM_ERROR_SOUND_NO_FREE_SPACE;
563 player->captured.data[1] = g_try_malloc(uvplane_size);
564 if ( !player->captured.data[1] ) {
565 gst_memory_unmap(memory, &mapinfo);
566 return MM_ERROR_SOUND_NO_FREE_SPACE;
569 memcpy(player->captured.data[0], proved->data[0], yplane_size);
570 memcpy(player->captured.data[1], proved->data[1], uvplane_size);
572 gst_memory_unmap(memory, &mapinfo);
578 GstVideoInfo format_info;
579 gst_video_info_from_caps(&format_info, caps);
581 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;
599 case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
601 MMPlayerMPlaneImage *proved = NULL;
602 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
604 /* get video frame info from proved buffer */
605 proved = (MMPlayerMPlaneImage *)GST_BUFFER_MALLOCDATA(buffer);
607 if (!proved || !proved->a[0] || !proved->a[1])
608 return MM_ERROR_PLAYER_INTERNAL;
610 memset(&player->captured, 0x00, sizeof(MMPlayerMPlaneImage));
611 memcpy(&player->captured, proved, sizeof(MMPlayerMPlaneImage));
613 player->captured.y_size = proved->s[0] * proved->h[0]; // must get data including padding
614 player->captured.uv_size = proved->s[0] * proved->h[1];
616 debug_msg ("y plane_size : %d, uv plane_size : %d", player->captured.y_size, player->captured.uv_size);
618 player->captured.a[0] = g_try_malloc(player->captured.y_size);
620 if ( !player->captured.a[0] ) {
621 return MM_ERROR_SOUND_NO_FREE_SPACE;
624 player->captured.a[1] = g_try_malloc(player->captured.uv_size);
626 if ( !player->captured.a[1] ) {
627 return MM_ERROR_SOUND_NO_FREE_SPACE;
630 memcpy(player->captured.a[0], proved->a[0], player->captured.y_size);
631 memcpy(player->captured.a[1], proved->a[1], player->captured.uv_size);
643 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
644 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
645 gst_buffer_unmap(buffer, &mapinfo);
648 /* do convert colorspace */
649 g_cond_signal( &player->capture_thread_cond );
653 return MM_ERROR_NONE;
656 debug_error("unknown format to capture\n");
657 return MM_ERROR_PLAYER_INTERNAL;
660 static GstPadProbeReturn
661 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
663 mm_player_t* player = (mm_player_t*) u_data;
664 GstBuffer *buffer = NULL;
665 int ret = MM_ERROR_NONE;
667 return_val_if_fail (info->data, GST_PAD_PROBE_REMOVE);
670 buffer = gst_pad_probe_info_get_buffer(info);
671 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
673 if ( ret != MM_ERROR_NONE)
675 debug_error("failed to get video frame");
676 return GST_PAD_PROBE_REMOVE;
679 /* remove probe to be called at one time */
680 if (player->video_capture_cb_probe_id)
682 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
683 player->video_capture_cb_probe_id = 0;
688 return GST_PAD_PROBE_OK;
692 __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)
694 unsigned char *dst_data = NULL;
695 unsigned int dst_size;
696 int ret = MM_ERROR_NONE;
698 return_val_if_fail(player, MM_ERROR_PLAYER_INTERNAL);
699 ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
701 if (ret != MM_ERROR_NONE)
703 debug_error("failed to get image size for capture, %d\n", ret);
704 return MM_ERROR_PLAYER_INTERNAL;
707 secure_debug_log("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
709 dst_data = (unsigned char*)g_malloc0(dst_size);
713 debug_error("no free space to capture\n");
714 return MM_ERROR_PLAYER_NO_FREE_SPACE;
717 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
719 if (ret != MM_ERROR_NONE)
721 debug_error("failed to convert for capture, %d\n", ret);
722 return MM_ERROR_PLAYER_INTERNAL;
725 player->capture.size = dst_size;
726 player->capture.data = dst_data;
728 return MM_ERROR_NONE;
732 * Get tiled address of position(x,y)
738 * height of tiled[in]
741 * x position of tield[in]
744 * y position of tield[in]
747 * address of tiled data
750 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
752 int pixel_x_m1, pixel_y_m1;
754 int linear_addr0, linear_addr1, bank_addr ;
758 pixel_x_m1 = x_size -1;
759 pixel_y_m1 = y_size -1;
761 roundup_x = ((pixel_x_m1 >> 7) + 1);
765 if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
766 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
767 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
768 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
770 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
771 bank_addr = ((x_addr >> 4) & 0x1);
773 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
775 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
776 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
778 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
779 bank_addr = ((x_addr >> 4) & 0x1);
781 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
784 linear_addr0 = linear_addr0 << 2;
785 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
791 * Converts tiled data to linear
792 * Crops left, top, right, buttom
793 * 1. Y of NV12T to Y of YUV420P
794 * 2. Y of NV12T to Y of YUV420S
795 * 3. UV of NV12T to UV of YUV420S
798 * Y or UV plane address of YUV420[out]
801 * Y or UV plane address of NV12T[in]
803 * @param yuv420_width
804 * Width of YUV420[in]
806 * @param yuv420_height
807 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
819 * Crop size of buttom
822 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
823 int left, int top, int right, int buttom)
826 int tiled_offset = 0, tiled_offset1 = 0;
827 int linear_offset = 0;
828 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
830 temp3 = yuv420_width-right;
832 /* real width is greater than or equal 256 */
834 for (i=top; i<yuv420_height-buttom; i=i+1) {
840 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
841 tiled_offset = temp4-1;
842 temp1 = ((yuv420_width+127)>>7)<<7;
843 tiled_offset = tiled_offset*(temp1>>6);
844 tiled_offset = tiled_offset+temp3;
845 tiled_offset = tiled_offset+2;
846 temp1 = (temp3>>2)<<2;
847 tiled_offset = tiled_offset+temp1;
848 tiled_offset = tiled_offset<<11;
849 tiled_offset1 = tiled_offset+2048*2;
852 temp2 = ((yuv420_height+31)>>5)<<5;
854 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
856 temp1 = (temp1>>2)<<2;
857 tiled_offset = temp3+temp1;
858 temp1 = ((yuv420_width+127)>>7)<<7;
859 tiled_offset = tiled_offset+temp4*(temp1>>6);
860 tiled_offset = tiled_offset<<11;
861 tiled_offset1 = tiled_offset+2048*6;
864 /* even2 fomula: x+x_block_num*y */
865 temp1 = ((yuv420_width+127)>>7)<<7;
866 tiled_offset = temp4*(temp1>>6);
867 tiled_offset = tiled_offset+temp3;
868 tiled_offset = tiled_offset<<11;
869 tiled_offset1 = tiled_offset+2048*2;
875 tiled_offset = tiled_offset+64*(temp1);
876 tiled_offset1 = tiled_offset1+64*(temp1);
877 temp2 = yuv420_width-left-right;
878 linear_offset = temp2*(i-top);
879 temp3 = ((j+256)>>8)<<8;
883 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
884 temp2 = ((left+63)>>6)<<6;
885 temp3 = ((yuv420_width-right)>>6)<<6;
886 if (temp2 == temp3) {
887 temp2 = yuv420_width-right-(64-temp1);
889 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
890 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
891 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
892 linear_offset = linear_offset+256-temp1;
893 } else if (temp3 > 128) {
894 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
895 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
896 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
897 linear_offset = linear_offset+192-temp1;
898 } else if (temp3 > 64) {
899 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
900 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
901 linear_offset = linear_offset+128-temp1;
902 } else if (temp3 > 0) {
903 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
904 linear_offset = linear_offset+64-temp1;
907 tiled_offset = tiled_offset+temp4*2048;
910 temp2 = yuv420_width-right-256;
911 for (; j<=temp2; j=j+256) {
912 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
913 tiled_offset1 = tiled_offset1+temp4*2048;
914 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
915 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
916 tiled_offset = tiled_offset+temp4*2048;
917 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
918 linear_offset = linear_offset+256;
921 tiled_offset1 = tiled_offset1+temp4*2048;
922 temp2 = yuv420_width-right-j;
924 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
925 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
926 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
927 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
928 } else if (temp2 > 128) {
929 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
930 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
931 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
932 } else if (temp2 > 64) {
933 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
934 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
936 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
939 } else if (temp1 >= 64) {
940 for (i=top; i<(yuv420_height-buttom); i=i+1) {
942 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
943 temp2 = ((j+64)>>6)<<6;
945 linear_offset = temp1*(i-top);
947 tiled_offset = tiled_offset+temp4;
948 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
949 linear_offset = linear_offset+temp2;
951 if ((j+64) <= temp3) {
952 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
953 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
954 linear_offset = linear_offset+64;
957 if ((j+64) <= temp3) {
958 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
959 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
960 linear_offset = linear_offset+64;
964 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
966 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
970 for (i=top; i<(yuv420_height-buttom); i=i+1) {
971 linear_offset = temp1*(i-top);
972 for (j=left; j<(yuv420_width-right); j=j+2) {
973 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
975 tiled_offset = tiled_offset+temp4;
976 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
977 linear_offset = linear_offset+2;