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 LOGW("not ready to capture");
138 return MM_ERROR_PLAYER_INVALID_STATE;
141 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
143 if (player->state != MM_PLAYER_STATE_PLAYING) {
144 if (player->state == MM_PLAYER_STATE_PAUSED) {
145 // get last buffer from video sink
146 GstSample *sample = NULL;
148 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
149 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
152 GstBuffer *buf = NULL;
153 buf = gst_sample_get_buffer(sample);
155 if (__mmplayer_get_video_frame_from_buffer(player, pad, buf) != MM_ERROR_NONE)
156 ret = MM_ERROR_PLAYER_INTERNAL;
158 LOGW("failed to get video frame");
160 gst_sample_unref(sample);
164 LOGW("invalid state(%d) to capture", player->state);
165 gst_object_unref(GST_OBJECT(pad));
167 return MM_ERROR_PLAYER_INVALID_STATE;
172 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
173 __mmplayer_video_capture_probe, player, NULL);
175 gst_object_unref(GST_OBJECT(pad));
184 __mmplayer_handle_orientation(mm_player_t* player, int orientation, int format)
186 unsigned char *src_buffer = NULL;
187 int ret = MM_ERROR_NONE;
188 unsigned char *dst_frame = NULL;
189 unsigned int dst_width = 0;
190 unsigned int dst_height = 0;
192 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
194 player->capture.orientation = orientation;
196 if (orientation == 90 || orientation == 270) {
197 dst_width = player->captured.height[0];
198 dst_height = player->captured.width[0];
199 LOGD("interchange width & height");
200 } else if (orientation == 180) {
201 dst_width = player->captured.width[0];
202 dst_height = player->captured.height[0];
203 } else if (orientation == 0) {
204 LOGE("no need handle orientation : %d", orientation);
205 player->capture.width = player->captured.width[0];
206 player->capture.height = player->captured.height[0];
207 return MM_ERROR_NONE;
209 LOGE("wrong orientation value...");
211 /* height & width will be interchanged for 90 and 270 orientation */
212 LOGD("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
213 src_buffer = (unsigned char*)player->capture.data;
215 /* convert orientation degree into enum here */
216 switch (orientation) {
218 rot_enum = MM_UTIL_ROTATE_90;
221 rot_enum = MM_UTIL_ROTATE_180;
224 rot_enum = MM_UTIL_ROTATE_270;
227 LOGE("wrong rotate value");
231 LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
233 ret = mm_util_rotate_image(src_buffer,
234 player->captured.width[0], player->captured.height[0], format, rot_enum,
235 &dst_frame, &dst_width, &dst_height, &dst_size);
236 if (ret != MM_ERROR_NONE || !dst_frame) {
237 LOGE("failed to do rotate image");
242 LOGD("after rotation same stride: dst_width = %d and dst_height = %d, dst_size = %zu", dst_width, dst_height, dst_size);
246 player->capture.data = dst_frame;
247 player->capture.size = (int)dst_size;
248 player->capture.orientation = orientation;
249 player->capture.width = dst_width;
250 player->capture.height = dst_height;
252 player->captured.width[0] = player->captured.stride_width[0] = dst_width;
253 player->captured.height[0] = player->captured.stride_height[0] = dst_height;
260 __mmplayer_capture_thread(gpointer data)
262 mm_player_t *player = (mm_player_t *) data;
263 MMMessageParamType msg = {0, };
265 int display_angle = 0;
269 MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
271 MMPLAYER_CAPTURE_THREAD_LOCK(player);
272 while (!player->capture_thread_exit) {
273 LOGD("capture thread started. waiting for signal");
274 MMPLAYER_CAPTURE_THREAD_WAIT(player);
276 if (player->capture_thread_exit) {
277 LOGD("exiting capture thread");
280 LOGD("capture thread is received signal");
282 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
283 * Because deadlock can be happened if other player api is used in message callback.
285 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
286 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
287 ret = __mm_player_convert_NV12_tiled(player);
288 if (ret != MM_ERROR_NONE) {
292 } else if (player->video_cs == MM_PLAYER_COLORSPACE_NV12) {
293 ret = __mm_player_convert_NV12(player);
294 if (ret != MM_ERROR_NONE) {
298 } else if (player->video_cs == MM_PLAYER_COLORSPACE_I420) {
299 ret = __mm_player_convert_I420(player);
300 if (ret != MM_ERROR_NONE) {
306 ret = __mmplayer_get_video_angle((MMHandleType)player, &display_angle, &orientation);
307 if (ret != MM_ERROR_NONE) {
308 LOGE("failed to get rotation angle");
312 LOGD("orientation value = %d user_angle = %d", orientation, display_angle);
314 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_COLOR_RGB24);
315 if (ret != MM_ERROR_NONE) {
316 LOGE("failed to convert nv12 linear");
320 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
321 msg.data = &player->capture;
322 msg.size = player->capture.size;
324 if (player->cmd >= MMPLAYER_COMMAND_START) {
325 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
326 LOGD("returned from capture message callback");
331 for (i = 0; i < player->captured.handle_num; i++)
332 MMPLAYER_FREEIF(player->captured.data[i]);
334 msg.union_type = MM_MSG_UNION_CODE;
336 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
340 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
345 * The output is fixed as RGB888
348 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
350 int ret = MM_ERROR_NONE;
355 GstCaps *caps = NULL;
356 GstStructure *structure = NULL;
357 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
358 mm_util_color_format_e src_fmt = MM_UTIL_COLOR_YUV420;
359 mm_util_color_format_e dst_fmt = MM_UTIL_COLOR_RGB24; // fixed
363 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
364 MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
367 caps = gst_pad_get_current_caps(pad);
369 MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
370 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
372 structure = gst_caps_get_structure(caps, 0);
374 LOGE("cannot get structure from caps.\n");
375 ret = MM_ERROR_PLAYER_INTERNAL;
379 /* init capture image buffer */
380 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
382 gst_structure_get_int(structure, "width", &src_width);
383 gst_structure_get_int(structure, "height", &src_height);
385 GstVideoInfo video_info;
386 gst_video_info_from_caps(&video_info, caps);
388 /* check rgb or yuv */
389 if (gst_structure_has_name(structure, "video/x-raw")) {
391 const gchar *gst_format = gst_structure_get_string(structure, "format");
393 if (!g_strcmp0(gst_format, "ST12"))
394 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
395 else if (!g_strcmp0(gst_format, "S420"))
396 player->video_cs = MM_PLAYER_COLORSPACE_I420;
397 else if (!g_strcmp0(gst_format, "SN12"))
398 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
399 else if (!g_strcmp0(gst_format, "BGRx"))
400 player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
402 player->video_cs = MM_PLAYER_COLORSPACE_MAX;
404 LOGI("captured format is %s\n", gst_format);
406 if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12")
407 || !g_strcmp0(gst_format, "S420")) {
408 GstVideoFrame vframe;
409 GstVideoFormat format;
410 const GstVideoFormatInfo *finfo;
412 /* get video frame info from proved buffer */
413 format = gst_video_format_from_string(gst_format);
414 finfo = gst_video_format_get_info(format);
416 if (gst_video_frame_map(&vframe, &video_info, buffer, GST_MAP_READ)) {
417 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES(finfo); i++) {
418 player->captured.width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
419 player->captured.height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
420 player->captured.stride_width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
421 player->captured.stride_height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
422 size = player->captured.stride_width[i] * player->captured.stride_height[i];
423 guint8 *pixels = GST_VIDEO_FRAME_PLANE_DATA(&vframe, i);
425 player->captured.data[i] = g_try_malloc(size);
426 if (!player->captured.data[i]) {
427 gst_video_frame_unmap(&vframe);
428 LOGE("no free space\n");
429 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
432 memcpy(player->captured.data[i], pixels, size);
434 player->captured.handle_num = GST_VIDEO_FRAME_N_PLANES(&vframe);
435 gst_video_frame_unmap(&vframe);
439 player->captured.width[0] = src_width;
440 player->captured.height[0] = src_height;
441 player->captured.stride_width[0] = MM_ALIGN(src_width, 4);
442 player->captured.stride_height[0] = src_height;
443 switch (GST_VIDEO_INFO_FORMAT(&video_info)) {
444 case GST_VIDEO_FORMAT_I420:
445 src_fmt = MM_UTIL_COLOR_I420;
446 player->captured.width[1] = player->captured.width[2] = src_width>>1;
447 player->captured.height[1] = player->captured.width[2] = src_height>>1;
448 player->captured.stride_width[1] = player->captured.stride_width[2] = MM_ALIGN(player->captured.width[1], 4);
449 player->captured.stride_height[1] = player->captured.stride_height[2] = src_height>>1;
451 case GST_VIDEO_FORMAT_BGRA:
452 src_fmt = MM_UTIL_COLOR_BGRA;
454 case GST_VIDEO_FORMAT_BGRx:
455 src_fmt = MM_UTIL_COLOR_BGRX;
458 LOGE("unknown format to capture\n");
459 ret = MM_ERROR_PLAYER_INTERNAL;
463 player->captured.handle_num = 1;
466 LOGE("unknown format to capture\n");
467 ret = MM_ERROR_PLAYER_INTERNAL;
471 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
472 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
473 gst_buffer_unmap(buffer, &mapinfo);
476 /* do convert colorspace */
477 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
480 gst_caps_unref(caps);
489 gst_caps_unref(caps);
493 for (i = 0; i < player->captured.handle_num; i++) {
494 MMPLAYER_FREEIF(player->captured.data[i]);
501 static GstPadProbeReturn
502 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
504 mm_player_t* player = (mm_player_t*) u_data;
505 GstBuffer *buffer = NULL;
506 int ret = MM_ERROR_NONE;
508 MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
511 buffer = gst_pad_probe_info_get_buffer(info);
512 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
514 if (ret != MM_ERROR_NONE) {
515 LOGE("failed to get video frame");
516 return GST_PAD_PROBE_REMOVE;
519 /* remove probe to be called at one time */
520 if (player->video_capture_cb_probe_id) {
521 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
522 player->video_capture_cb_probe_id = 0;
527 return GST_PAD_PROBE_OK;
531 __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)
533 unsigned char *dst_data = NULL;
534 unsigned int dst_width = 0;
535 unsigned int dst_height = 0;
537 int ret = MM_ERROR_NONE;
539 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
540 SECURE_LOGD("src size info. width: %d, height: %d \n", src_w, src_h);
542 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_fmt, &dst_data, &dst_width, &dst_height, &dst_size);
543 if (ret != MM_ERROR_NONE || !dst_data) {
544 LOGE("failed to convert for capture, %d\n", ret);
546 return MM_ERROR_PLAYER_INTERNAL;
548 SECURE_LOGD("dst size info. width: %d, height: %d, size: %zu\n", dst_width, dst_height, dst_size);
550 player->capture.size = (int)dst_size;
551 player->capture.data = dst_data;
553 return MM_ERROR_NONE;
557 * Get tiled address of position(x,y)
563 * height of tiled[in]
566 * x position of tield[in]
569 * y position of tield[in]
572 * address of tiled data
575 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
577 int pixel_x_m1, pixel_y_m1;
579 int linear_addr0, linear_addr1, bank_addr ;
583 pixel_x_m1 = x_size -1;
584 pixel_y_m1 = y_size -1;
586 roundup_x = ((pixel_x_m1 >> 7) + 1);
590 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
591 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
592 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
593 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 6) & 0x3f));
595 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
596 bank_addr = ((x_addr >> 4) & 0x1);
598 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
600 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
601 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 5) & 0x7f));
603 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
604 bank_addr = ((x_addr >> 4) & 0x1);
606 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
609 linear_addr0 = linear_addr0 << 2;
610 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
616 * Converts tiled data to linear
617 * Crops left, top, right, buttom
618 * 1. Y of NV12T to Y of YUV420P
619 * 2. Y of NV12T to Y of YUV420S
620 * 3. UV of NV12T to UV of YUV420S
623 * Y or UV plane address of YUV420[out]
626 * Y or UV plane address of NV12T[in]
628 * @param yuv420_width
629 * Width of YUV420[in]
631 * @param yuv420_height
632 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
644 * Crop size of buttom
647 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
648 int left, int top, int right, int buttom)
651 int tiled_offset = 0, tiled_offset1 = 0;
652 int linear_offset = 0;
653 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
655 temp3 = yuv420_width-right;
657 /* real width is greater than or equal 256 */
659 for (i = top; i < yuv420_height-buttom; i = i+1) {
665 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
666 tiled_offset = temp4-1;
667 temp1 = ((yuv420_width+127)>>7)<<7;
668 tiled_offset = tiled_offset*(temp1>>6);
669 tiled_offset = tiled_offset+temp3;
670 tiled_offset = tiled_offset+2;
671 temp1 = (temp3>>2)<<2;
672 tiled_offset = tiled_offset+temp1;
673 tiled_offset = tiled_offset<<11;
674 tiled_offset1 = tiled_offset+2048*2;
677 temp2 = ((yuv420_height+31)>>5)<<5;
678 if ((i+32) < temp2) {
679 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
681 temp1 = (temp1>>2)<<2;
682 tiled_offset = temp3+temp1;
683 temp1 = ((yuv420_width+127)>>7)<<7;
684 tiled_offset = tiled_offset+temp4*(temp1>>6);
685 tiled_offset = tiled_offset<<11;
686 tiled_offset1 = tiled_offset+2048*6;
689 /* even2 fomula: x+x_block_num*y */
690 temp1 = ((yuv420_width+127)>>7)<<7;
691 tiled_offset = temp4*(temp1>>6);
692 tiled_offset = tiled_offset+temp3;
693 tiled_offset = tiled_offset<<11;
694 tiled_offset1 = tiled_offset+2048*2;
700 tiled_offset = tiled_offset+64*(temp1);
701 tiled_offset1 = tiled_offset1+64*(temp1);
702 temp2 = yuv420_width-left-right;
703 linear_offset = temp2*(i-top);
704 temp3 = ((j+256)>>8)<<8;
708 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
709 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
710 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
711 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
712 linear_offset = linear_offset+256-temp1;
713 } else if (temp3 > 128) {
714 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
715 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
716 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
717 linear_offset = linear_offset+192-temp1;
718 } else if (temp3 > 64) {
719 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
720 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
721 linear_offset = linear_offset+128-temp1;
722 } else if (temp3 > 0) {
723 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
724 linear_offset = linear_offset+64-temp1;
727 tiled_offset = tiled_offset+temp4*2048;
730 temp2 = yuv420_width-right-256;
731 for (; j <= temp2; j = j+256) {
732 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
733 tiled_offset1 = tiled_offset1+temp4*2048;
734 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
735 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
736 tiled_offset = tiled_offset+temp4*2048;
737 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
738 linear_offset = linear_offset+256;
741 tiled_offset1 = tiled_offset1+temp4*2048;
742 temp2 = yuv420_width-right-j;
744 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
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 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
748 } else if (temp2 > 128) {
749 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
750 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
751 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
752 } else if (temp2 > 64) {
753 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
754 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
756 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
758 } else if (temp1 >= 64) {
759 for (i = top; i < (yuv420_height-buttom); i = i+1) {
761 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
762 temp2 = ((j+64)>>6)<<6;
764 linear_offset = temp1*(i-top);
766 tiled_offset = tiled_offset+temp4;
767 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
768 linear_offset = linear_offset+temp2;
770 if ((j+64) <= temp3) {
771 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
772 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
773 linear_offset = linear_offset+64;
776 if ((j+64) <= temp3) {
777 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
778 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
779 linear_offset = linear_offset+64;
783 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
785 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
789 for (i = top; i < (yuv420_height-buttom); i = i+1) {
790 linear_offset = temp1*(i-top);
791 for (j = left; j < (yuv420_width-right); j = j+2) {
792 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
794 tiled_offset = tiled_offset+temp4;
795 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
796 linear_offset = linear_offset+2;
802 #ifdef CAPTURE_OUTPUT_DUMP /* for capture output dump */
803 static void capture_output_dump(mm_player_t* player)
805 unsigned char *temp = NULL;
806 char file[100] = { 0, };
811 LOGE("capture output dump start. size = %d", player->capture.size);
812 sprintf(file, "/tmp/dec_output_dump_%dx%d.yuv", player->captured.width[0], player->captured.height[0]);
813 fp = fopen(file, "ab");
815 for (i = 0; i < player->captured.plane_num; i++) {
816 temp = player->captured.data[i];
818 for (j = 0; j < player->captured.height[i]; j++) {
819 ret = fwrite(temp, player->captured.width[i], 1, fp);
820 temp += player->captured.width[i];
823 LOGE("capture yuv dumped!! ret = %d", ret);
826 temp = (unsigned char *)player->capture.data;
827 sprintf(file, "/tmp/dec_output_dump_%dx%d.rgb", player->captured.width[0], player->captured.height[0]);
828 fp = fopen(file, "ab");
829 ret = fwrite(temp, player->capture.size, 1, fp);
831 LOGE("capture rgb dumped!! ret = %d", ret);
836 __mm_player_convert_NV12_tiled(mm_player_t *player)
838 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
839 int ret = MM_ERROR_NONE;
840 unsigned char *src_buffer = NULL;
841 unsigned char *linear_y_plane = NULL;
842 unsigned char *linear_uv_plane = NULL;
843 int linear_y_plane_size;
844 int linear_uv_plane_size;
845 int width = player->captured.width[0];
846 int height = player->captured.height[0];
849 linear_y_plane_size = (width * height);
850 linear_uv_plane_size = linear_y_plane_size / 2;
852 linear_y_plane = (unsigned char *)g_try_malloc(linear_y_plane_size);
854 return MM_ERROR_PLAYER_NO_FREE_SPACE;
856 linear_uv_plane = (unsigned char *)g_try_malloc(linear_uv_plane_size);
857 if (!linear_uv_plane) {
858 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
861 /* NV12 tiled to linear */
862 __csc_tiled_to_linear_crop(linear_y_plane,
863 player->captured.data[0], width, height, 0, 0, 0, 0);
864 __csc_tiled_to_linear_crop(linear_uv_plane,
865 player->captured.data[1], width, height / 2, 0, 0, 0, 0);
867 src_buffer = (unsigned char *)g_try_malloc(linear_y_plane_size + linear_uv_plane_size);
870 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
873 memset(src_buffer, 0x00, linear_y_plane_size + linear_uv_plane_size);
874 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
875 memcpy(src_buffer + linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
877 /* NV12 linear to RGB888 */
878 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_COLOR_NV12,
879 width, height, MM_UTIL_COLOR_RGB24);
880 if (ret != MM_ERROR_NONE) {
881 LOGE("failed to convert nv12 linear");
887 MMPLAYER_FREEIF(src_buffer);
888 MMPLAYER_FREEIF(linear_y_plane);
889 MMPLAYER_FREEIF(linear_uv_plane);
891 for (i = 0; i < player->captured.handle_num; i++)
892 MMPLAYER_FREEIF(player->captured.data[i]);
898 __mm_player_convert_NV12(mm_player_t *player)
900 unsigned char *src_buffer = NULL;
901 unsigned char *p_buf = NULL;
902 unsigned char *temp = NULL;
903 int planes[MAX_BUFFER_PLANE] = {0, };
904 int ret = MM_ERROR_NONE;
907 /* using original width otherwises, app can't know aligned to resize */
908 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
909 planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
910 int src_buffer_size = planes[0] + planes[1];
912 if (!src_buffer_size) {
913 LOGE("invalid data size");
914 return MM_ERROR_PLAYER_INTERNAL;
916 src_buffer = (unsigned char *)g_try_malloc(src_buffer_size);
918 return MM_ERROR_PLAYER_NO_FREE_SPACE;
920 memset(src_buffer, 0x00, src_buffer_size);
923 temp = player->captured.data[0];
926 for (i = 0; i < player->captured.height[0]; i++) {
927 memcpy(p_buf, temp, player->captured.width[0]);
928 p_buf += player->captured.width[0];
929 temp += player->captured.stride_width[0];
932 temp = player->captured.data[1];
935 for (j = 0; j < player->captured.height[1]; j++) {
936 memcpy(p_buf, temp, player->captured.width[1]);
937 p_buf += player->captured.width[1];
938 temp += player->captured.stride_width[1];
942 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer,
943 MM_UTIL_COLOR_NV12, player->captured.width[0],
944 player->captured.height[0], MM_UTIL_COLOR_RGB24);
945 if (ret != MM_ERROR_NONE) {
946 LOGE("failed to convert nv12 linear");
949 #ifdef CAPTURE_OUTPUT_DUMP
950 capture_output_dump(player);
955 MMPLAYER_FREEIF(src_buffer);
956 /* free captured buf */
957 for (i = 0; i < player->captured.handle_num; i++)
958 MMPLAYER_FREEIF(player->captured.data[i]);
964 __mm_player_convert_I420(mm_player_t *player)
966 unsigned char *src_buffer = NULL;
967 unsigned char *p_buf = NULL;
968 unsigned char *temp = NULL;
969 int planes[MAX_BUFFER_PLANE] = {0, };
970 int ret = MM_ERROR_NONE;
973 /* using original width otherwises, app can't know aligned to resize */
974 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
975 planes[1] = planes[2] = (player->captured.stride_width[0] >> 1)
976 * (player->captured.stride_height[0] >> 1);
978 src_buffer = (unsigned char *)g_try_malloc(player->captured.stride_width[0]
979 * player->captured.stride_height[0] * 3 / 2);
982 return MM_ERROR_PLAYER_NO_FREE_SPACE;
985 memset(src_buffer, 0x00, planes[0]);
988 temp = player->captured.data[0];
990 for (i = 0; i < player->captured.height[0]; i++) {
991 memcpy(p_buf, temp, player->captured.width[0]);
992 temp += player->captured.stride_width[0];
993 p_buf += player->captured.width[0];
997 memset(p_buf, 0x00, planes[1]);
998 temp = player->captured.data[1];
1000 for (i = 0; i < player->captured.height[1]; i++) {
1001 memcpy(p_buf, temp, player->captured.width[1]);
1002 temp += player->captured.stride_width[1];
1003 p_buf += player->captured.width[1];
1007 memset(p_buf, 0x00, planes[2]);
1008 temp = player->captured.data[2];
1010 for (i = 0; i < player->captured.height[2]; i++) {
1011 memcpy(p_buf, temp, player->captured.width[2]);
1012 temp += player->captured.stride_width[2];
1013 p_buf += player->captured.width[2];
1016 /* I420 -> RGB888 */
1017 ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, MM_UTIL_COLOR_I420,
1018 player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
1019 if (ret != MM_ERROR_NONE) {
1020 LOGE("failed to convert I420 linear");
1023 #ifdef CAPTURE_OUTPUT_DUMP
1024 capture_output_dump(player);
1029 MMPLAYER_FREEIF(src_buffer);
1030 /* free captured buf */
1031 for (i = 0; i < player->captured.handle_num; i++)
1032 MMPLAYER_FREEIF(player->captured.data[i]);