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 const gchar *gst_format = gst_structure_get_string(structure, "format");
536 if(!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12"))
539 debug_msg ("captured format is %s\n", gst_format);
541 MMVideoBuffer *proved = NULL;
542 if(!g_strcmp0(gst_format, "ST12"))
543 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
545 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
547 /* get video frame info from proved buffer */
548 n = gst_buffer_n_memory(buffer);
549 memory = gst_buffer_peek_memory(buffer, n-1);
550 gst_memory_map(memory, &mapinfo, GST_MAP_READ);
551 proved = (MMVideoBuffer *)mapinfo.data;
553 if ( !proved || !proved->data[0] || !proved->data[1] ) {
554 debug_error("fail to gst_memory_map");
555 return MM_ERROR_PLAYER_INTERNAL;
559 yplane_size = proved->size[0];
560 uvplane_size = proved->size[1];
562 yplane_size = proved->stride_width[0] * proved->stride_height[0];
563 uvplane_size = proved->stride_width[1] * proved->stride_height[1];
566 debug_msg ("yplane_size=%d, uvplane_size=%d\n", yplane_size, uvplane_size);
567 memset(&player->captured, 0x00, sizeof(MMVideoBuffer));
568 memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
570 player->captured.data[0] = g_try_malloc(yplane_size);
571 if ( !player->captured.data[0] ) {
572 gst_memory_unmap(memory, &mapinfo);
573 return MM_ERROR_SOUND_NO_FREE_SPACE;
576 player->captured.data[1] = g_try_malloc(uvplane_size);
577 if ( !player->captured.data[1] ) {
578 gst_memory_unmap(memory, &mapinfo);
579 return MM_ERROR_SOUND_NO_FREE_SPACE;
582 memcpy(player->captured.data[0], proved->data[0], yplane_size);
583 memcpy(player->captured.data[1], proved->data[1], uvplane_size);
585 gst_memory_unmap(memory, &mapinfo);
591 GstVideoInfo format_info;
592 gst_video_info_from_caps(&format_info, caps);
594 switch(GST_VIDEO_INFO_FORMAT(&format_info))
596 case GST_VIDEO_FORMAT_I420:
597 src_fmt = MM_UTIL_IMG_FMT_I420;
599 case GST_VIDEO_FORMAT_BGRA:
600 src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
602 case GST_VIDEO_FORMAT_BGRx:
603 src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
616 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
617 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
618 gst_buffer_unmap(buffer, &mapinfo);
621 /* do convert colorspace */
622 g_cond_signal( &player->capture_thread_cond );
626 return MM_ERROR_NONE;
629 debug_error("unknown format to capture\n");
630 return MM_ERROR_PLAYER_INTERNAL;
633 static GstPadProbeReturn
634 __mmplayer_video_capture_probe (GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
636 mm_player_t* player = (mm_player_t*) u_data;
637 GstBuffer *buffer = NULL;
638 int ret = MM_ERROR_NONE;
640 return_val_if_fail (info->data, GST_PAD_PROBE_REMOVE);
643 buffer = gst_pad_probe_info_get_buffer(info);
644 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
646 if ( ret != MM_ERROR_NONE)
648 debug_error("failed to get video frame");
649 return GST_PAD_PROBE_REMOVE;
652 /* remove probe to be called at one time */
653 if (player->video_capture_cb_probe_id)
655 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
656 player->video_capture_cb_probe_id = 0;
661 return GST_PAD_PROBE_OK;
665 __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)
667 unsigned char *dst_data = NULL;
668 unsigned int dst_size;
669 int ret = MM_ERROR_NONE;
671 return_val_if_fail(player, MM_ERROR_PLAYER_INTERNAL);
672 ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
674 if (ret != MM_ERROR_NONE)
676 debug_error("failed to get image size for capture, %d\n", ret);
677 return MM_ERROR_PLAYER_INTERNAL;
680 secure_debug_log("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
682 dst_data = (unsigned char*)g_malloc0(dst_size);
686 debug_error("no free space to capture\n");
687 return MM_ERROR_PLAYER_NO_FREE_SPACE;
690 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
692 if (ret != MM_ERROR_NONE)
694 debug_error("failed to convert for capture, %d\n", ret);
695 return MM_ERROR_PLAYER_INTERNAL;
698 player->capture.size = dst_size;
699 player->capture.data = dst_data;
701 return MM_ERROR_NONE;
705 * Get tiled address of position(x,y)
711 * height of tiled[in]
714 * x position of tield[in]
717 * y position of tield[in]
720 * address of tiled data
723 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
725 int pixel_x_m1, pixel_y_m1;
727 int linear_addr0, linear_addr1, bank_addr ;
731 pixel_x_m1 = x_size -1;
732 pixel_y_m1 = y_size -1;
734 roundup_x = ((pixel_x_m1 >> 7) + 1);
738 if ((y_size <= y_pos+32) && ( y_pos < y_size) &&
739 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
740 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
741 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
743 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
744 bank_addr = ((x_addr >> 4) & 0x1);
746 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
748 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
749 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
751 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
752 bank_addr = ((x_addr >> 4) & 0x1);
754 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
757 linear_addr0 = linear_addr0 << 2;
758 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
764 * Converts tiled data to linear
765 * Crops left, top, right, buttom
766 * 1. Y of NV12T to Y of YUV420P
767 * 2. Y of NV12T to Y of YUV420S
768 * 3. UV of NV12T to UV of YUV420S
771 * Y or UV plane address of YUV420[out]
774 * Y or UV plane address of NV12T[in]
776 * @param yuv420_width
777 * Width of YUV420[in]
779 * @param yuv420_height
780 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
792 * Crop size of buttom
795 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
796 int left, int top, int right, int buttom)
799 int tiled_offset = 0, tiled_offset1 = 0;
800 int linear_offset = 0;
801 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
803 temp3 = yuv420_width-right;
805 /* real width is greater than or equal 256 */
807 for (i=top; i<yuv420_height-buttom; i=i+1) {
813 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
814 tiled_offset = temp4-1;
815 temp1 = ((yuv420_width+127)>>7)<<7;
816 tiled_offset = tiled_offset*(temp1>>6);
817 tiled_offset = tiled_offset+temp3;
818 tiled_offset = tiled_offset+2;
819 temp1 = (temp3>>2)<<2;
820 tiled_offset = tiled_offset+temp1;
821 tiled_offset = tiled_offset<<11;
822 tiled_offset1 = tiled_offset+2048*2;
825 temp2 = ((yuv420_height+31)>>5)<<5;
827 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
829 temp1 = (temp1>>2)<<2;
830 tiled_offset = temp3+temp1;
831 temp1 = ((yuv420_width+127)>>7)<<7;
832 tiled_offset = tiled_offset+temp4*(temp1>>6);
833 tiled_offset = tiled_offset<<11;
834 tiled_offset1 = tiled_offset+2048*6;
837 /* even2 fomula: x+x_block_num*y */
838 temp1 = ((yuv420_width+127)>>7)<<7;
839 tiled_offset = temp4*(temp1>>6);
840 tiled_offset = tiled_offset+temp3;
841 tiled_offset = tiled_offset<<11;
842 tiled_offset1 = tiled_offset+2048*2;
848 tiled_offset = tiled_offset+64*(temp1);
849 tiled_offset1 = tiled_offset1+64*(temp1);
850 temp2 = yuv420_width-left-right;
851 linear_offset = temp2*(i-top);
852 temp3 = ((j+256)>>8)<<8;
856 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
857 temp2 = ((left+63)>>6)<<6;
858 temp3 = ((yuv420_width-right)>>6)<<6;
859 if (temp2 == temp3) {
860 temp2 = yuv420_width-right-(64-temp1);
862 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
863 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
864 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
865 linear_offset = linear_offset+256-temp1;
866 } else if (temp3 > 128) {
867 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
868 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
869 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
870 linear_offset = linear_offset+192-temp1;
871 } else if (temp3 > 64) {
872 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
873 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
874 linear_offset = linear_offset+128-temp1;
875 } else if (temp3 > 0) {
876 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
877 linear_offset = linear_offset+64-temp1;
880 tiled_offset = tiled_offset+temp4*2048;
883 temp2 = yuv420_width-right-256;
884 for (; j<=temp2; j=j+256) {
885 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
886 tiled_offset1 = tiled_offset1+temp4*2048;
887 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
888 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
889 tiled_offset = tiled_offset+temp4*2048;
890 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
891 linear_offset = linear_offset+256;
894 tiled_offset1 = tiled_offset1+temp4*2048;
895 temp2 = yuv420_width-right-j;
897 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
898 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
899 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
900 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
901 } else if (temp2 > 128) {
902 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
903 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
904 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
905 } else if (temp2 > 64) {
906 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
907 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
909 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
912 } else if (temp1 >= 64) {
913 for (i=top; i<(yuv420_height-buttom); i=i+1) {
915 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
916 temp2 = ((j+64)>>6)<<6;
918 linear_offset = temp1*(i-top);
920 tiled_offset = tiled_offset+temp4;
921 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
922 linear_offset = linear_offset+temp2;
924 if ((j+64) <= temp3) {
925 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
926 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
927 linear_offset = linear_offset+64;
930 if ((j+64) <= temp3) {
931 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
932 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
933 linear_offset = linear_offset+64;
937 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
939 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
943 for (i=top; i<(yuv420_height-buttom); i=i+1) {
944 linear_offset = temp1*(i-top);
945 for (j=left; j<(yuv420_width-right); j=j+2) {
946 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
948 tiled_offset = tiled_offset+temp4;
949 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
950 linear_offset = linear_offset+2;