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 //#define CAPTURE_OUTPUT_DUMP 1
37 /*---------------------------------------------------------------------------
38 | LOCAL VARIABLE DEFINITIONS for internal |
39 ---------------------------------------------------------------------------*/
41 /*---------------------------------------------------------------------------
42 | LOCAL FUNCTION PROTOTYPES: |
43 ---------------------------------------------------------------------------*/
44 static GstPadProbeReturn __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
45 static int __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer);
46 static gpointer __mmplayer_capture_thread(gpointer data);
47 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);
48 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
49 static int __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_color_format_e src_fmt, unsigned int src_w, unsigned int src_h, mm_util_color_format_e dst_fmt);
50 static int __mm_player_convert_NV12_tiled(mm_player_t *player);
51 static int __mm_player_convert_NV12(mm_player_t *player);
52 static int __mm_player_convert_I420(mm_player_t *player);
53 #ifdef CAPTURE_OUTPUT_DUMP
54 static void capture_output_dump(mm_player_t* player);
57 /*===========================================================================================
59 | FUNCTION DEFINITIONS |
61 ========================================================================================== */
63 _mmplayer_initialize_video_capture(mm_player_t* player)
65 int ret = MM_ERROR_NONE;
66 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
67 /* create capture mutex */
68 g_mutex_init(&player->capture_thread_mutex);
70 /* create capture cond */
71 g_cond_init(&player->capture_thread_cond);
74 player->capture_thread_exit = FALSE;
76 /* create video capture thread */
77 player->capture_thread =
78 g_thread_try_new("capture_thread", __mmplayer_capture_thread, (gpointer)player, NULL);
80 if (!player->capture_thread) {
81 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
89 g_mutex_clear(&player->capture_thread_mutex);
91 g_cond_clear(&player->capture_thread_cond);
97 _mmplayer_release_video_capture(mm_player_t* player)
99 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
100 /* release capture thread */
101 MMPLAYER_CAPTURE_THREAD_LOCK(player);
102 player->capture_thread_exit = TRUE;
103 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
104 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
106 LOGD("waitting for capture thread exit");
107 g_thread_join(player->capture_thread);
108 g_mutex_clear(&player->capture_thread_mutex);
109 g_cond_clear(&player->capture_thread_cond);
110 LOGD("capture thread released");
112 return MM_ERROR_NONE;
116 _mmplayer_do_video_capture(MMHandleType hplayer)
118 mm_player_t* player = (mm_player_t*) hplayer;
119 int ret = MM_ERROR_NONE;
124 MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
126 /* capturing or not */
127 if (player->video_capture_cb_probe_id || player->capture.data
128 || player->captured.data[0] || player->captured.data[1]) {
129 LOGW("capturing... we can't do any more");
130 return MM_ERROR_PLAYER_INVALID_STATE;
132 gint surface_type = 0;
133 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
135 /* check if video pipeline is linked or not */
136 if (!player->pipeline->videobin) {
137 if (surface_type == MM_DISPLAY_SURFACE_REMOTE && player->video_fakesink) {
138 LOGD("it is evas surface type.");
140 LOGW("not ready to capture");
141 return MM_ERROR_PLAYER_INVALID_STATE;
145 if (player->pipeline->videobin)
146 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
148 pad = gst_element_get_static_pad(player->video_fakesink, "sink");
150 if (player->state != MM_PLAYER_STATE_PLAYING) {
151 if (player->state == MM_PLAYER_STATE_PAUSED) {
152 // get last buffer from video sink
153 GstSample *sample = NULL;
155 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
157 if (player->pipeline->videobin)
158 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
160 g_object_get(player->video_fakesink, "last-sample", &sample, NULL);
163 GstBuffer *buf = NULL;
164 buf = gst_sample_get_buffer(sample);
166 if (__mmplayer_get_video_frame_from_buffer(player, pad, buf) != MM_ERROR_NONE)
167 ret = MM_ERROR_PLAYER_INTERNAL;
169 LOGW("failed to get video frame");
171 gst_sample_unref(sample);
175 LOGW("invalid state(%d) to capture", player->state);
176 gst_object_unref(GST_OBJECT(pad));
178 return MM_ERROR_PLAYER_INVALID_STATE;
183 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
184 __mmplayer_video_capture_probe, player, NULL);
186 gst_object_unref(GST_OBJECT(pad));
195 __mmplayer_handle_orientation(mm_player_t* player, int orientation, int format)
197 unsigned char *src_buffer = NULL;
198 int ret = MM_ERROR_NONE;
199 unsigned char *dst_frame = NULL;
200 unsigned int dst_width = 0;
201 unsigned int dst_height = 0;
203 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
205 player->capture.orientation = orientation;
207 if (orientation == 90 || orientation == 270) {
208 dst_width = player->captured.height[0];
209 dst_height = player->captured.width[0];
210 LOGD("interchange width & height");
211 } else if (orientation == 180) {
212 dst_width = player->captured.width[0];
213 dst_height = player->captured.height[0];
214 } else if (orientation == 0) {
215 LOGE("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;
220 LOGE("wrong orientation value...");
222 /* height & width will be interchanged for 90 and 270 orientation */
223 LOGD("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
224 src_buffer = (unsigned char*)player->capture.data;
226 /* convert orientation degree into enum here */
227 switch (orientation) {
229 rot_enum = MM_UTIL_ROTATE_90;
232 rot_enum = MM_UTIL_ROTATE_180;
235 rot_enum = MM_UTIL_ROTATE_270;
238 LOGE("wrong rotate value");
242 LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
244 ret = mm_util_rotate_image(src_buffer,
245 player->captured.width[0], player->captured.height[0], format, rot_enum,
246 &dst_frame, &dst_width, &dst_height, &dst_size);
247 if (ret != MM_ERROR_NONE || !dst_frame) {
248 LOGE("failed to do rotate image");
253 LOGD("after rotation same stride: dst_width = %d and dst_height = %d, dst_size = %zu", dst_width, dst_height, dst_size);
257 player->capture.data = dst_frame;
258 player->capture.size = (int)dst_size;
259 player->capture.orientation = orientation;
260 player->capture.width = dst_width;
261 player->capture.height = dst_height;
263 player->captured.width[0] = player->captured.stride_width[0] = dst_width;
264 player->captured.height[0] = player->captured.stride_height[0] = dst_height;
271 __mmplayer_capture_thread(gpointer data)
273 mm_player_t *player = (mm_player_t *) data;
274 MMMessageParamType msg = {0, };
276 int display_angle = 0;
280 MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
282 MMPLAYER_CAPTURE_THREAD_LOCK(player);
283 while (!player->capture_thread_exit) {
284 LOGD("capture thread started. waiting for signal");
285 MMPLAYER_CAPTURE_THREAD_WAIT(player);
287 if (player->capture_thread_exit) {
288 LOGD("exiting capture thread");
291 LOGD("capture thread is received signal");
293 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
294 * Because deadlock can be happened if other player api is used in message callback.
296 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
297 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
298 ret = __mm_player_convert_NV12_tiled(player);
299 if (ret != MM_ERROR_NONE) {
303 } else if (player->video_cs == MM_PLAYER_COLORSPACE_NV12) {
304 ret = __mm_player_convert_NV12(player);
305 if (ret != MM_ERROR_NONE) {
309 } else if (player->video_cs == MM_PLAYER_COLORSPACE_I420) {
310 ret = __mm_player_convert_I420(player);
311 if (ret != MM_ERROR_NONE) {
317 ret = __mmplayer_get_video_angle((MMHandleType)player, &display_angle, &orientation);
318 if (ret != MM_ERROR_NONE) {
319 LOGE("failed to get rotation angle");
323 LOGD("orientation value = %d user_angle = %d", orientation, display_angle);
325 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_COLOR_RGB24);
326 if (ret != MM_ERROR_NONE) {
327 LOGE("failed to convert nv12 linear");
331 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
332 msg.data = &player->capture;
333 msg.size = player->capture.size;
335 if (player->cmd >= MMPLAYER_COMMAND_START) {
336 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
337 LOGD("returned from capture message callback");
342 for (i = 0; i < player->captured.handle_num; i++)
343 MMPLAYER_FREEIF(player->captured.data[i]);
345 msg.union_type = MM_MSG_UNION_CODE;
347 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
351 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
356 * The output is fixed as RGB888
359 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
361 int ret = MM_ERROR_NONE;
366 GstCaps *caps = NULL;
367 GstStructure *structure = NULL;
368 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
369 mm_util_color_format_e src_fmt = MM_UTIL_COLOR_YUV420;
370 mm_util_color_format_e dst_fmt = MM_UTIL_COLOR_RGB24; // fixed
374 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
375 MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
378 caps = gst_pad_get_current_caps(pad);
380 MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
381 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
383 structure = gst_caps_get_structure(caps, 0);
385 LOGE("cannot get structure from caps.\n");
386 ret = MM_ERROR_PLAYER_INTERNAL;
390 /* init capture image buffer */
391 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
393 gst_structure_get_int(structure, "width", &src_width);
394 gst_structure_get_int(structure, "height", &src_height);
396 GstVideoInfo video_info;
397 gst_video_info_from_caps(&video_info, caps);
399 /* check rgb or yuv */
400 if (gst_structure_has_name(structure, "video/x-raw")) {
402 const gchar *gst_format = gst_structure_get_string(structure, "format");
404 if (!g_strcmp0(gst_format, "ST12"))
405 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
406 else if (!g_strcmp0(gst_format, "S420"))
407 player->video_cs = MM_PLAYER_COLORSPACE_I420;
408 else if (!g_strcmp0(gst_format, "SN12"))
409 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
410 else if (!g_strcmp0(gst_format, "BGRx"))
411 player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
413 player->video_cs = MM_PLAYER_COLORSPACE_MAX;
415 LOGI("captured format is %s\n", gst_format);
417 if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12")
418 || !g_strcmp0(gst_format, "S420")) {
419 GstVideoFrame vframe;
420 GstVideoFormat format;
421 const GstVideoFormatInfo *finfo;
423 /* get video frame info from proved buffer */
424 format = gst_video_format_from_string(gst_format);
425 finfo = gst_video_format_get_info(format);
427 if (gst_video_frame_map(&vframe, &video_info, buffer, GST_MAP_READ)) {
428 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES(finfo); i++) {
429 player->captured.width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
430 player->captured.height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
431 player->captured.stride_width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
432 player->captured.stride_height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
433 size = player->captured.stride_width[i] * player->captured.stride_height[i];
434 guint8 *pixels = GST_VIDEO_FRAME_PLANE_DATA(&vframe, i);
436 player->captured.data[i] = g_try_malloc(size);
437 if (!player->captured.data[i]) {
438 gst_video_frame_unmap(&vframe);
439 LOGE("no free space\n");
440 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
443 memcpy(player->captured.data[i], pixels, size);
445 player->captured.handle_num = GST_VIDEO_FRAME_N_PLANES(&vframe);
446 gst_video_frame_unmap(&vframe);
450 player->captured.width[0] = src_width;
451 player->captured.height[0] = src_height;
452 player->captured.stride_width[0] = MM_ALIGN(src_width, 4);
453 player->captured.stride_height[0] = src_height;
454 switch (GST_VIDEO_INFO_FORMAT(&video_info)) {
455 case GST_VIDEO_FORMAT_I420:
456 src_fmt = MM_UTIL_COLOR_I420;
457 player->captured.width[1] = player->captured.width[2] = src_width>>1;
458 player->captured.height[1] = player->captured.width[2] = src_height>>1;
459 player->captured.stride_width[1] = player->captured.stride_width[2] = MM_ALIGN(player->captured.width[1], 4);
460 player->captured.stride_height[1] = player->captured.stride_height[2] = src_height>>1;
462 case GST_VIDEO_FORMAT_BGRA:
463 src_fmt = MM_UTIL_COLOR_BGRA;
465 case GST_VIDEO_FORMAT_BGRx:
466 src_fmt = MM_UTIL_COLOR_BGRX;
469 LOGE("unknown format to capture\n");
470 ret = MM_ERROR_PLAYER_INTERNAL;
474 player->captured.handle_num = 1;
477 LOGE("unknown format to capture\n");
478 ret = MM_ERROR_PLAYER_INTERNAL;
482 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
483 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
484 gst_buffer_unmap(buffer, &mapinfo);
487 /* do convert colorspace */
488 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
491 gst_caps_unref(caps);
500 gst_caps_unref(caps);
504 for (i = 0; i < player->captured.handle_num; i++) {
505 MMPLAYER_FREEIF(player->captured.data[i]);
512 static GstPadProbeReturn
513 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
515 mm_player_t* player = (mm_player_t*) u_data;
516 GstBuffer *buffer = NULL;
517 int ret = MM_ERROR_NONE;
519 MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
522 buffer = gst_pad_probe_info_get_buffer(info);
523 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
525 if (ret != MM_ERROR_NONE) {
526 LOGE("failed to get video frame");
527 return GST_PAD_PROBE_REMOVE;
530 /* remove probe to be called at one time */
531 if (player->video_capture_cb_probe_id) {
532 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
533 player->video_capture_cb_probe_id = 0;
538 return GST_PAD_PROBE_OK;
542 __mm_player_convert_colorspace(mm_player_t* player, unsigned char* src_data, mm_util_color_format_e src_fmt, unsigned int src_w, unsigned int src_h, mm_util_color_format_e dst_fmt)
544 unsigned char *dst_data = NULL;
545 unsigned int dst_width = 0;
546 unsigned int dst_height = 0;
548 int ret = MM_ERROR_NONE;
550 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
551 SECURE_LOGD("src size info. width: %d, height: %d \n", src_w, src_h);
553 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_fmt, &dst_data, &dst_width, &dst_height, &dst_size);
554 if (ret != MM_ERROR_NONE || !dst_data) {
555 LOGE("failed to convert for capture, %d\n", ret);
557 return MM_ERROR_PLAYER_INTERNAL;
559 SECURE_LOGD("dst size info. width: %d, height: %d, size: %zu\n", dst_width, dst_height, dst_size);
561 player->capture.size = (int)dst_size;
562 player->capture.data = dst_data;
564 return MM_ERROR_NONE;
568 * Get tiled address of position(x,y)
574 * height of tiled[in]
577 * x position of tield[in]
580 * y position of tield[in]
583 * address of tiled data
586 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
588 int pixel_x_m1, pixel_y_m1;
590 int linear_addr0, linear_addr1, bank_addr ;
594 pixel_x_m1 = x_size -1;
595 pixel_y_m1 = y_size -1;
597 roundup_x = ((pixel_x_m1 >> 7) + 1);
601 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
602 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
603 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
604 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 6) & 0x3f));
606 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
607 bank_addr = ((x_addr >> 4) & 0x1);
609 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
611 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
612 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 5) & 0x7f));
614 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
615 bank_addr = ((x_addr >> 4) & 0x1);
617 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
620 linear_addr0 = linear_addr0 << 2;
621 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
627 * Converts tiled data to linear
628 * Crops left, top, right, buttom
629 * 1. Y of NV12T to Y of YUV420P
630 * 2. Y of NV12T to Y of YUV420S
631 * 3. UV of NV12T to UV of YUV420S
634 * Y or UV plane address of YUV420[out]
637 * Y or UV plane address of NV12T[in]
639 * @param yuv420_width
640 * Width of YUV420[in]
642 * @param yuv420_height
643 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
655 * Crop size of buttom
658 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
659 int left, int top, int right, int buttom)
662 int tiled_offset = 0, tiled_offset1 = 0;
663 int linear_offset = 0;
664 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
666 temp3 = yuv420_width-right;
668 /* real width is greater than or equal 256 */
670 for (i = top; i < yuv420_height-buttom; i = i+1) {
676 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
677 tiled_offset = temp4-1;
678 temp1 = ((yuv420_width+127)>>7)<<7;
679 tiled_offset = tiled_offset*(temp1>>6);
680 tiled_offset = tiled_offset+temp3;
681 tiled_offset = tiled_offset+2;
682 temp1 = (temp3>>2)<<2;
683 tiled_offset = tiled_offset+temp1;
684 tiled_offset = tiled_offset<<11;
685 tiled_offset1 = tiled_offset+2048*2;
688 temp2 = ((yuv420_height+31)>>5)<<5;
689 if ((i+32) < temp2) {
690 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
692 temp1 = (temp1>>2)<<2;
693 tiled_offset = temp3+temp1;
694 temp1 = ((yuv420_width+127)>>7)<<7;
695 tiled_offset = tiled_offset+temp4*(temp1>>6);
696 tiled_offset = tiled_offset<<11;
697 tiled_offset1 = tiled_offset+2048*6;
700 /* even2 fomula: x+x_block_num*y */
701 temp1 = ((yuv420_width+127)>>7)<<7;
702 tiled_offset = temp4*(temp1>>6);
703 tiled_offset = tiled_offset+temp3;
704 tiled_offset = tiled_offset<<11;
705 tiled_offset1 = tiled_offset+2048*2;
711 tiled_offset = tiled_offset+64*(temp1);
712 tiled_offset1 = tiled_offset1+64*(temp1);
713 temp2 = yuv420_width-left-right;
714 linear_offset = temp2*(i-top);
715 temp3 = ((j+256)>>8)<<8;
719 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
720 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
721 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
722 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
723 linear_offset = linear_offset+256-temp1;
724 } else if (temp3 > 128) {
725 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
726 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
727 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
728 linear_offset = linear_offset+192-temp1;
729 } else if (temp3 > 64) {
730 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
731 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
732 linear_offset = linear_offset+128-temp1;
733 } else if (temp3 > 0) {
734 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
735 linear_offset = linear_offset+64-temp1;
738 tiled_offset = tiled_offset+temp4*2048;
741 temp2 = yuv420_width-right-256;
742 for (; j <= temp2; j = j+256) {
743 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
744 tiled_offset1 = tiled_offset1+temp4*2048;
745 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
746 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
747 tiled_offset = tiled_offset+temp4*2048;
748 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
749 linear_offset = linear_offset+256;
752 tiled_offset1 = tiled_offset1+temp4*2048;
753 temp2 = yuv420_width-right-j;
755 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
756 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
757 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
758 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
759 } else if (temp2 > 128) {
760 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
761 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
762 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
763 } else if (temp2 > 64) {
764 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
765 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
767 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
769 } else if (temp1 >= 64) {
770 for (i = top; i < (yuv420_height-buttom); i = i+1) {
772 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
773 temp2 = ((j+64)>>6)<<6;
775 linear_offset = temp1*(i-top);
777 tiled_offset = tiled_offset+temp4;
778 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
779 linear_offset = linear_offset+temp2;
781 if ((j+64) <= temp3) {
782 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
783 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
784 linear_offset = linear_offset+64;
787 if ((j+64) <= temp3) {
788 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
789 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
790 linear_offset = linear_offset+64;
794 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
796 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
800 for (i = top; i < (yuv420_height-buttom); i = i+1) {
801 linear_offset = temp1*(i-top);
802 for (j = left; j < (yuv420_width-right); j = j+2) {
803 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
805 tiled_offset = tiled_offset+temp4;
806 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
807 linear_offset = linear_offset+2;
813 #ifdef CAPTURE_OUTPUT_DUMP /* for capture output dump */
814 static void capture_output_dump(mm_player_t* player)
816 unsigned char *temp = NULL;
817 char file[100] = { 0, };
822 LOGE("capture output dump start. size = %d", player->capture.size);
823 sprintf(file, "/tmp/dec_output_dump_%dx%d.yuv", player->captured.width[0], player->captured.height[0]);
824 fp = fopen(file, "ab");
826 for (i = 0; i < player->captured.plane_num; i++) {
827 temp = player->captured.data[i];
829 for (j = 0; j < player->captured.height[i]; j++) {
830 ret = fwrite(temp, player->captured.width[i], 1, fp);
831 temp += player->captured.width[i];
834 LOGE("capture yuv dumped!! ret = %d", ret);
837 temp = (unsigned char *)player->capture.data;
838 sprintf(file, "/tmp/dec_output_dump_%dx%d.rgb", player->captured.width[0], player->captured.height[0]);
839 fp = fopen(file, "ab");
840 ret = fwrite(temp, player->capture.size, 1, fp);
842 LOGE("capture rgb dumped!! ret = %d", ret);
847 __mm_player_convert_NV12_tiled(mm_player_t *player)
849 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
850 int ret = MM_ERROR_NONE;
851 unsigned char *src_buffer = NULL;
852 unsigned char *linear_y_plane = NULL;
853 unsigned char *linear_uv_plane = NULL;
854 int linear_y_plane_size;
855 int linear_uv_plane_size;
856 int width = player->captured.width[0];
857 int height = player->captured.height[0];
860 linear_y_plane_size = (width * height);
861 linear_uv_plane_size = linear_y_plane_size / 2;
863 linear_y_plane = (unsigned char *)g_try_malloc(linear_y_plane_size);
865 return MM_ERROR_PLAYER_NO_FREE_SPACE;
867 linear_uv_plane = (unsigned char *)g_try_malloc(linear_uv_plane_size);
868 if (!linear_uv_plane) {
869 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
872 /* NV12 tiled to linear */
873 __csc_tiled_to_linear_crop(linear_y_plane,
874 player->captured.data[0], width, height, 0, 0, 0, 0);
875 __csc_tiled_to_linear_crop(linear_uv_plane,
876 player->captured.data[1], width, height / 2, 0, 0, 0, 0);
878 src_buffer = (unsigned char *)g_try_malloc(linear_y_plane_size + linear_uv_plane_size);
881 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
884 memset(src_buffer, 0x00, linear_y_plane_size + linear_uv_plane_size);
885 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
886 memcpy(src_buffer + linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
888 /* NV12 linear to RGB888 */
889 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_COLOR_NV12,
890 width, height, MM_UTIL_COLOR_RGB24);
891 if (ret != MM_ERROR_NONE) {
892 LOGE("failed to convert nv12 linear");
898 MMPLAYER_FREEIF(src_buffer);
899 MMPLAYER_FREEIF(linear_y_plane);
900 MMPLAYER_FREEIF(linear_uv_plane);
902 for (i = 0; i < player->captured.handle_num; i++)
903 MMPLAYER_FREEIF(player->captured.data[i]);
909 __mm_player_convert_NV12(mm_player_t *player)
911 unsigned char *src_buffer = NULL;
912 unsigned char *p_buf = NULL;
913 unsigned char *temp = NULL;
914 int planes[MAX_BUFFER_PLANE] = {0, };
915 int ret = MM_ERROR_NONE;
918 /* using original width otherwises, app can't know aligned to resize */
919 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
920 planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
921 int src_buffer_size = planes[0] + planes[1];
923 if (!src_buffer_size) {
924 LOGE("invalid data size");
925 return MM_ERROR_PLAYER_INTERNAL;
927 src_buffer = (unsigned char *)g_try_malloc(src_buffer_size);
929 return MM_ERROR_PLAYER_NO_FREE_SPACE;
931 memset(src_buffer, 0x00, src_buffer_size);
934 temp = player->captured.data[0];
937 for (i = 0; i < player->captured.height[0]; i++) {
938 memcpy(p_buf, temp, player->captured.width[0]);
939 p_buf += player->captured.width[0];
940 temp += player->captured.stride_width[0];
943 temp = player->captured.data[1];
946 for (j = 0; j < player->captured.height[1]; j++) {
947 memcpy(p_buf, temp, player->captured.width[1]);
948 p_buf += player->captured.width[1];
949 temp += player->captured.stride_width[1];
953 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer,
954 MM_UTIL_COLOR_NV12, player->captured.width[0],
955 player->captured.height[0], MM_UTIL_COLOR_RGB24);
956 if (ret != MM_ERROR_NONE) {
957 LOGE("failed to convert nv12 linear");
960 #ifdef CAPTURE_OUTPUT_DUMP
961 capture_output_dump(player);
966 MMPLAYER_FREEIF(src_buffer);
967 /* free captured buf */
968 for (i = 0; i < player->captured.handle_num; i++)
969 MMPLAYER_FREEIF(player->captured.data[i]);
975 __mm_player_convert_I420(mm_player_t *player)
977 unsigned char *src_buffer = NULL;
978 unsigned char *p_buf = NULL;
979 unsigned char *temp = NULL;
980 int planes[MAX_BUFFER_PLANE] = {0, };
981 int ret = MM_ERROR_NONE;
984 /* using original width otherwises, app can't know aligned to resize */
985 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
986 planes[1] = planes[2] = (player->captured.stride_width[0] >> 1)
987 * (player->captured.stride_height[0] >> 1);
989 src_buffer = (unsigned char *)g_try_malloc(player->captured.stride_width[0]
990 * player->captured.stride_height[0] * 3 / 2);
993 return MM_ERROR_PLAYER_NO_FREE_SPACE;
996 memset(src_buffer, 0x00, planes[0]);
999 temp = player->captured.data[0];
1001 for (i = 0; i < player->captured.height[0]; i++) {
1002 memcpy(p_buf, temp, player->captured.width[0]);
1003 temp += player->captured.stride_width[0];
1004 p_buf += player->captured.width[0];
1008 memset(p_buf, 0x00, planes[1]);
1009 temp = player->captured.data[1];
1011 for (i = 0; i < player->captured.height[1]; i++) {
1012 memcpy(p_buf, temp, player->captured.width[1]);
1013 temp += player->captured.stride_width[1];
1014 p_buf += player->captured.width[1];
1018 memset(p_buf, 0x00, planes[2]);
1019 temp = player->captured.data[2];
1021 for (i = 0; i < player->captured.height[2]; i++) {
1022 memcpy(p_buf, temp, player->captured.width[2]);
1023 temp += player->captured.stride_width[2];
1024 p_buf += player->captured.width[2];
1027 /* I420 -> RGB888 */
1028 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, MM_UTIL_COLOR_I420,
1029 player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
1030 if (ret != MM_ERROR_NONE) {
1031 LOGE("failed to convert I420 linear");
1034 #ifdef CAPTURE_OUTPUT_DUMP
1035 capture_output_dump(player);
1040 MMPLAYER_FREEIF(src_buffer);
1041 /* free captured buf */
1042 for (i = 0; i < player->captured.handle_num; i++)
1043 MMPLAYER_FREEIF(player->captured.data[i]);