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_img_format src_fmt, unsigned int src_w, unsigned int src_h, mm_util_img_format dst_fmt);
50 #ifdef CAPTURE_OUTPUT_DUMP
51 static void capture_output_dump(mm_player_t* player);
54 /*===========================================================================================
56 | FUNCTION DEFINITIONS |
58 ========================================================================================== */
60 _mmplayer_initialize_video_capture(mm_player_t* player)
62 int ret = MM_ERROR_NONE;
63 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
64 /* create capture mutex */
65 g_mutex_init(&player->capture_thread_mutex);
67 /* create capture cond */
68 g_cond_init(&player->capture_thread_cond);
71 player->capture_thread_exit = FALSE;
73 /* create video capture thread */
74 player->capture_thread =
75 g_thread_try_new("capture_thread", __mmplayer_capture_thread, (gpointer)player, NULL);
77 if (!player->capture_thread) {
78 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
86 g_mutex_clear(&player->capture_thread_mutex);
88 g_cond_clear(&player->capture_thread_cond);
94 _mmplayer_release_video_capture(mm_player_t* player)
96 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
97 /* release capture thread */
98 MMPLAYER_CAPTURE_THREAD_LOCK(player);
99 player->capture_thread_exit = TRUE;
100 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
101 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
103 LOGD("waitting for capture thread exit");
104 g_thread_join(player->capture_thread);
105 g_mutex_clear(&player->capture_thread_mutex);
106 g_cond_clear(&player->capture_thread_cond);
107 LOGD("capture thread released");
109 return MM_ERROR_NONE;
113 _mmplayer_do_video_capture(MMHandleType hplayer)
115 mm_player_t* player = (mm_player_t*) hplayer;
116 int ret = MM_ERROR_NONE;
121 MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
123 /* capturing or not */
124 if (player->video_capture_cb_probe_id || player->capture.data
125 || player->captured.data[0] || player->captured.data[1]) {
126 LOGW("capturing... we can't do any more");
127 return MM_ERROR_PLAYER_INVALID_STATE;
129 gint surface_type = 0;
130 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
132 /* check if video pipeline is linked or not */
133 if (!player->pipeline->videobin) {
134 if (surface_type == MM_DISPLAY_SURFACE_REMOTE && player->video_fakesink) {
135 LOGD("it is evas surface type.");
137 LOGW("not ready to capture");
138 return MM_ERROR_PLAYER_INVALID_STATE;
142 if (player->pipeline->videobin)
143 pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
145 pad = gst_element_get_static_pad(player->video_fakesink, "sink");
147 if (player->state != MM_PLAYER_STATE_PLAYING) {
148 if (player->state == MM_PLAYER_STATE_PAUSED) {
149 // get last buffer from video sink
150 GstSample *sample = NULL;
152 gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, NULL, NULL, 5 * GST_SECOND); //5 seconds
154 if (player->pipeline->videobin)
155 g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "last-sample", &sample, NULL);
157 g_object_get(player->video_fakesink, "last-sample", &sample, NULL);
160 GstBuffer *buf = NULL;
161 buf = gst_sample_get_buffer(sample);
163 if (__mmplayer_get_video_frame_from_buffer(player, pad, buf) != MM_ERROR_NONE)
164 ret = MM_ERROR_PLAYER_INTERNAL;
166 LOGW("failed to get video frame");
168 gst_sample_unref(sample);
172 LOGW("invalid state(%d) to capture", player->state);
173 gst_object_unref(GST_OBJECT(pad));
175 return MM_ERROR_PLAYER_INVALID_STATE;
180 player->video_capture_cb_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
181 __mmplayer_video_capture_probe, player, NULL);
183 gst_object_unref(GST_OBJECT(pad));
192 __mmplayer_handle_orientation(mm_player_t* player, int orientation, int format)
194 unsigned char *src_buffer = NULL;
195 int ret = MM_ERROR_NONE;
196 unsigned char *dst_frame = NULL;
197 unsigned int dst_width = 0;
198 unsigned int dst_height = 0;
199 unsigned int dst_size = 0;
200 mm_util_img_rotate_type rot_enum = MM_UTIL_ROTATE_NUM;
202 player->capture.orientation = orientation;
204 if (orientation == 90 || orientation == 270) {
205 dst_width = player->captured.height[0];
206 dst_height = player->captured.width[0];
207 LOGD("interchange width & height");
208 } else if (orientation == 180) {
209 dst_width = player->captured.width[0];
210 dst_height = player->captured.height[0];
211 } else if (orientation == 0) {
212 LOGE("no need handle orientation : %d", orientation);
213 player->capture.width = player->captured.width[0];
214 player->capture.height = player->captured.height[0];
215 return MM_ERROR_NONE;
217 LOGE("wrong orientation value...");
219 /* height & width will be interchanged for 90 and 270 orientation */
220 ret = mm_util_get_image_size(format, dst_width, dst_height, &dst_size);
221 if (ret != MM_ERROR_NONE) {
222 LOGE("failed to get destination frame size");
226 LOGD("before rotation : dst_width = %d and dst_height = %d", dst_width, dst_height);
228 dst_frame = (unsigned char*) malloc(dst_size);
230 LOGE("failed to allocate memory");
231 return MM_ERROR_PLAYER_NO_FREE_SPACE;
234 src_buffer = (unsigned char*)player->capture.data;
236 /* convert orientation degree into enum here */
237 switch (orientation) {
239 rot_enum = MM_UTIL_ROTATE_90;
242 rot_enum = MM_UTIL_ROTATE_180;
245 rot_enum = MM_UTIL_ROTATE_270;
248 LOGE("wrong rotate value");
252 LOGD("source buffer for rotation = %p and rotation = %d", src_buffer, rot_enum);
254 ret = mm_util_rotate_image(src_buffer,
255 player->captured.width[0], player->captured.height[0], format,
256 dst_frame, &dst_width, &dst_height, rot_enum);
257 if (ret != MM_ERROR_NONE) {
258 LOGE("failed to do rotate image");
263 LOGD("after rotation same stride: dst_width = %d and dst_height = %d", dst_width, dst_height);
267 player->capture.data = dst_frame;
268 player->capture.size = dst_size;
269 player->capture.orientation = orientation;
270 player->capture.width = dst_width;
271 player->capture.height = dst_height;
273 player->captured.width[0] = player->captured.stride_width[0] = dst_width;
274 player->captured.height[0] = player->captured.stride_height[0] = dst_height;
281 __mmplayer_capture_thread(gpointer data)
283 mm_player_t* player = (mm_player_t*) data;
284 MMMessageParamType msg = {0, };
285 unsigned char * src_buffer = NULL;
286 unsigned char * linear_y_plane = NULL;
287 unsigned char * linear_uv_plane = NULL;
290 int planes[MAX_BUFFER_PLANE] = {0, };
291 unsigned char * p_buf = NULL;
292 unsigned char * temp = NULL;
295 MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
297 MMPLAYER_CAPTURE_THREAD_LOCK(player);
298 while (!player->capture_thread_exit) {
299 LOGD("capture thread started. waiting for signal");
300 MMPLAYER_CAPTURE_THREAD_WAIT(player);
302 if (player->capture_thread_exit) {
303 LOGD("exiting capture thread");
306 LOGD("capture thread is received signal");
308 /* NOTE: Don't use MMPLAYER_CMD_LOCK() here.
309 * Because deadlock can be happened if other player api is used in message callback.
311 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
312 /* Colorspace conversion : NV12T-> NV12-> RGB888 */
314 int linear_y_plane_size;
315 int linear_uv_plane_size;
316 int width = player->captured.width[0];
317 int height = player->captured.height[0];
319 linear_y_plane_size = (width * height);
320 linear_uv_plane_size = (width * height / 2);
322 linear_y_plane = (unsigned char*) g_try_malloc(linear_y_plane_size);
323 if (linear_y_plane == NULL) {
324 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
328 linear_uv_plane = (unsigned char*) g_try_malloc(linear_uv_plane_size);
329 if (linear_uv_plane == NULL) {
330 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
333 /* NV12 tiled to linear */
334 __csc_tiled_to_linear_crop(linear_y_plane,
335 player->captured.data[0], width, height, 0, 0, 0, 0);
336 __csc_tiled_to_linear_crop(linear_uv_plane,
337 player->captured.data[1], width, height / 2, 0, 0, 0, 0);
339 for (i = 0; i < player->captured.handle_num; i++) {
340 MMPLAYER_FREEIF(player->captured.data[i]);
343 src_buffer = (unsigned char*) g_try_malloc(linear_y_plane_size+linear_uv_plane_size);
345 if (src_buffer == NULL) {
346 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
349 memset(src_buffer, 0x00, linear_y_plane_size+linear_uv_plane_size);
350 memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
351 memcpy(src_buffer+linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
353 /* NV12 linear to RGB888 */
354 ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_IMG_FMT_NV12,
355 width, height, MM_UTIL_IMG_FMT_RGB888);
357 if (ret != MM_ERROR_NONE) {
358 LOGE("failed to convert nv12 linear");
362 MMPLAYER_FREEIF(src_buffer);
363 MMPLAYER_FREEIF(linear_y_plane);
364 MMPLAYER_FREEIF(linear_uv_plane);
365 } else if (MM_PLAYER_COLORSPACE_NV12 == player->video_cs) {
366 #define MM_ALIGN(x, a) (((x) +(a) - 1) & ~((a) - 1))
368 /* using original width otherwises, app can't know aligned to resize */
369 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
370 planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
371 int src_buffer_size = planes[0] + planes[1];
372 src_buffer = (unsigned char*) g_try_malloc(src_buffer_size);
375 if (!src_buffer_size) {
376 LOGE("invalid data size");
381 msg.code = MM_ERROR_PLAYER_NO_FREE_SPACE;
385 memset(src_buffer, 0x00, src_buffer_size);
387 temp = player->captured.data[0];
390 for (i = 0; i < player->captured.height[0]; i++) {
391 memcpy(p_buf, temp, player->captured.width[0]);
392 p_buf += player->captured.width[0];
393 temp += player->captured.stride_width[0];
396 temp = player->captured.data[1];
399 for (j = 0; j < player->captured.height[1]; j++) {
400 memcpy(p_buf, temp, player->captured.width[1]);
401 p_buf += player->captured.width[1];
402 temp += player->captured.stride_width[1];
406 ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_NV12,
407 player->captured.width[0], player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
408 if (ret != MM_ERROR_NONE) {
409 LOGE("failed to convert nv12 linear");
412 #ifdef CAPTURE_OUTPUT_DUMP
413 capture_output_dump(player);
416 MMPLAYER_FREEIF(src_buffer);
417 /* free captured buf */
418 for (i = 0; i < player->captured.handle_num; i++) {
419 MMPLAYER_FREEIF(player->captured.data[i]);
421 } else if (MM_PLAYER_COLORSPACE_I420 == player->video_cs) {
422 planes[0] = player->captured.stride_width[0] * player->captured.stride_height[0];
423 planes[1] = planes[2] = (player->captured.stride_width[0]>>1) * (player->captured.stride_height[0]>>1);
425 src_buffer = (unsigned char*) g_try_malloc(player->captured.stride_width[0] * player->captured.stride_height[0]*3/2);
428 memset(p_buf, 0x00, planes[0]);
429 temp = player->captured.data[0];
431 for (i = 0; i < player->captured.height[0]; i++) {
432 memcpy(p_buf, temp, player->captured.width[0]);
433 temp += player->captured.stride_width[0];
434 p_buf += player->captured.width[0];
438 memset(p_buf, 0x00, planes[1]);
439 temp = player->captured.data[1];
441 for (i = 0; i < player->captured.height[1]; i++) {
442 memcpy(p_buf, temp, player->captured.width[1]);
443 temp += player->captured.stride_width[1];
444 p_buf += player->captured.width[1];
448 memset(p_buf, 0x00, planes[2]);
449 temp = player->captured.data[2];
451 for (i = 0; i < player->captured.height[2]; i++) {
452 memcpy(p_buf, temp, player->captured.width[2]);
453 temp += player->captured.stride_width[2];
454 p_buf += player->captured.width[2];
458 ret = __mm_player_convert_colorspace(player, (unsigned char*)src_buffer, MM_UTIL_IMG_FMT_I420,
459 player->captured.width[0], player->captured.height[0], MM_UTIL_IMG_FMT_RGB888);
460 if (ret != MM_ERROR_NONE) {
461 LOGE("failed to convert I420 linear");
464 #ifdef CAPTURE_OUTPUT_DUMP
465 capture_output_dump(player);
468 MMPLAYER_FREEIF(src_buffer);
469 /* free captured buf */
470 for (i = 0; i < player->captured.handle_num; i++) {
471 MMPLAYER_FREEIF(player->captured.data[i]);
475 ret = _mmplayer_get_video_rotate_angle((MMHandleType)player, &orientation);
476 if (ret != MM_ERROR_NONE) {
477 LOGE("failed to get rotation angle");
481 LOGD("orientation value = %d", orientation);
483 ret = __mmplayer_handle_orientation(player, orientation, MM_UTIL_IMG_FMT_RGB888);
484 if (ret != MM_ERROR_NONE) {
485 LOGE("failed to convert nv12 linear");
489 player->capture.fmt = MM_PLAYER_COLORSPACE_RGB888;
490 msg.data = &player->capture;
491 msg.size = player->capture.size;
492 // msg.captured_frame.width = player->capture.width;
493 // msg.captured_frame.height = player->capture.height;
494 // msg.captured_frame.orientation = player->capture.orientation;
496 if (player->cmd >= MMPLAYER_COMMAND_START) {
497 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_CAPTURED, &msg);
498 LOGD("returned from capture message callback");
501 //MMPLAYER_FREEIF(player->capture.data);
504 MMPLAYER_FREEIF(src_buffer);
506 for (i = 0; i < player->captured.handle_num; i++) {
507 MMPLAYER_FREEIF(player->captured.data[i]);
510 if (player->video_cs == MM_PLAYER_COLORSPACE_NV12_TILED) {
512 MMPLAYER_FREEIF(linear_y_plane);
513 MMPLAYER_FREEIF(linear_uv_plane);
516 msg.union_type = MM_MSG_UNION_CODE;
518 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_NOT_CAPTURED, &msg);
522 MMPLAYER_CAPTURE_THREAD_UNLOCK(player);
527 * The output is fixed as RGB888
530 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
532 int ret = MM_ERROR_NONE;
533 gint planes[MAX_BUFFER_PLANE] = {0, };
537 GstCaps *caps = NULL;
538 GstStructure *structure = NULL;
539 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
540 GstMemory *memory = NULL;
541 mm_util_img_format src_fmt = MM_UTIL_IMG_FMT_YUV420;
542 mm_util_img_format dst_fmt = MM_UTIL_IMG_FMT_RGB888; // fixed
546 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
547 MMPLAYER_RETURN_VAL_IF_FAIL(buffer, MM_ERROR_INVALID_ARGUMENT);
550 caps = gst_pad_get_current_caps(pad);
552 MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
553 MMPLAYER_LOG_GST_CAPS_TYPE(caps);
555 structure = gst_caps_get_structure(caps, 0);
557 LOGE("cannot get structure from caps.\n");
558 ret = MM_ERROR_PLAYER_INTERNAL;
562 /* init capture image buffer */
563 memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
565 gst_structure_get_int(structure, "width", &src_width);
566 gst_structure_get_int(structure, "height", &src_height);
568 /* check rgb or yuv */
569 if (gst_structure_has_name(structure, "video/x-raw")) {
571 const gchar *gst_format = gst_structure_get_string(structure, "format");
572 if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12") || !g_strcmp0(gst_format, "S420")) {
574 LOGI("captured format is %s\n", gst_format);
576 MMVideoBuffer *proved = NULL;
577 if (!g_strcmp0(gst_format, "ST12"))
578 player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
579 else if (!g_strcmp0(gst_format, "S420"))
580 player->video_cs = MM_PLAYER_COLORSPACE_I420;
581 else if (!g_strcmp0(gst_format, "SN12"))
582 player->video_cs = MM_PLAYER_COLORSPACE_NV12;
584 player->video_cs = MM_PLAYER_COLORSPACE_MAX;
586 /* get video frame info from proved buffer */
587 n = gst_buffer_n_memory(buffer);
588 memory = gst_buffer_peek_memory(buffer, n-1);
589 gst_memory_map(memory, &mapinfo, GST_MAP_READ);
590 proved = (MMVideoBuffer *)mapinfo.data;
592 if (!proved || !proved->data[0] || !proved->data[1]) {
593 LOGE("fail to gst_memory_map");
594 ret = MM_ERROR_PLAYER_INTERNAL;
598 memcpy(&player->captured, proved, sizeof(MMVideoBuffer));
600 planes[0] = proved->size[0];
601 planes[1] = proved->size[1];
602 planes[2] = proved->size[2];
605 player->captured.data[0] = g_try_malloc(planes[0]);
606 if (!player->captured.data[0]) {
607 gst_memory_unmap(memory, &mapinfo);
608 LOGE("no free space\n");
609 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
614 if (proved->size[1]) {
615 player->captured.data[1] = g_try_malloc(planes[1]);
617 if (!player->captured.data[1]) {
618 LOGE("no free space\n");
619 gst_memory_unmap(memory, &mapinfo);
620 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
626 if (proved->size[2]) {
627 player->captured.data[2] = g_try_malloc(planes[2]);
629 if (!player->captured.data[2]) {
630 LOGE("no free space\n");
631 gst_memory_unmap(memory, &mapinfo);
632 ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
637 LOGD("Buffer type %d", proved->type);
638 if (proved->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
640 for (i = 0; i < player->captured.handle_num; i++) {
641 tbm_bo_ref(proved->handle.bo[i]);
643 LOGD("plane[0] : %p, size %d", proved->data[0], planes[0]);
646 memcpy(player->captured.data[0], proved->data[0], planes[0]);
648 LOGD("plane[1] : %p, size %d", proved->data[1], planes[1]);
651 memcpy(player->captured.data[1], proved->data[1], planes[1]);
653 if (player->video_cs == MM_PLAYER_COLORSPACE_I420) {
654 LOGD("plane[2] : %p, size %d", proved->data[2], planes[2]);
657 memcpy(player->captured.data[2], proved->data[2], planes[2]);
660 for (i = 0; i < player->captured.handle_num; i++) {
661 tbm_bo_unref(proved->handle.bo[i]);
664 LOGE("Not support video buffer type %d", proved->type);
665 gst_memory_unmap(memory, &mapinfo);
666 ret = MM_ERROR_PLAYER_INTERNAL;
670 gst_memory_unmap(memory, &mapinfo);
673 GstVideoInfo format_info;
674 gst_video_info_from_caps(&format_info, caps);
676 switch (GST_VIDEO_INFO_FORMAT(&format_info)) {
677 case GST_VIDEO_FORMAT_I420:
678 src_fmt = MM_UTIL_IMG_FMT_I420;
680 case GST_VIDEO_FORMAT_BGRA:
681 src_fmt = MM_UTIL_IMG_FMT_BGRA8888;
683 case GST_VIDEO_FORMAT_BGRx:
684 src_fmt = MM_UTIL_IMG_FMT_BGRX8888;
687 LOGE("unknown format to capture\n");
688 ret = MM_ERROR_PLAYER_INTERNAL;
694 LOGE("unknown format to capture\n");
695 ret = MM_ERROR_PLAYER_INTERNAL;
699 gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
700 __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
701 gst_buffer_unmap(buffer, &mapinfo);
704 /* do convert colorspace */
705 MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
708 gst_caps_unref(caps);
717 gst_caps_unref(caps);
721 for (i = 0; i < player->captured.handle_num; i++) {
722 MMPLAYER_FREEIF(player->captured.data[i]);
729 static GstPadProbeReturn
730 __mmplayer_video_capture_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
732 mm_player_t* player = (mm_player_t*) u_data;
733 GstBuffer *buffer = NULL;
734 int ret = MM_ERROR_NONE;
736 MMPLAYER_RETURN_VAL_IF_FAIL(info->data, GST_PAD_PROBE_REMOVE);
739 buffer = gst_pad_probe_info_get_buffer(info);
740 ret = __mmplayer_get_video_frame_from_buffer(player, pad, buffer);
742 if (ret != MM_ERROR_NONE) {
743 LOGE("failed to get video frame");
744 return GST_PAD_PROBE_REMOVE;
747 /* remove probe to be called at one time */
748 if (player->video_capture_cb_probe_id) {
749 gst_pad_remove_probe(pad, player->video_capture_cb_probe_id);
750 player->video_capture_cb_probe_id = 0;
755 return GST_PAD_PROBE_OK;
759 __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)
761 unsigned char *dst_data = NULL;
762 unsigned int dst_size;
763 int ret = MM_ERROR_NONE;
765 MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_INTERNAL);
766 ret = mm_util_get_image_size(dst_fmt, src_w, src_h, &dst_size);
768 if (ret != MM_ERROR_NONE) {
769 LOGE("failed to get image size for capture, %d\n", ret);
770 return MM_ERROR_PLAYER_INTERNAL;
773 SECURE_LOGD("width: %d, height: %d to capture, dest size: %d\n", src_w, src_h, dst_size);
775 dst_data = (unsigned char*)g_malloc0(dst_size);
778 LOGE("no free space to capture\n");
780 return MM_ERROR_PLAYER_NO_FREE_SPACE;
783 ret = mm_util_convert_colorspace(src_data, src_w, src_h, src_fmt, dst_data, dst_fmt);
785 if (ret != MM_ERROR_NONE) {
786 LOGE("failed to convert for capture, %d\n", ret);
788 return MM_ERROR_PLAYER_INTERNAL;
791 player->capture.size = dst_size;
792 player->capture.data = dst_data;
794 return MM_ERROR_NONE;
798 * Get tiled address of position(x,y)
804 * height of tiled[in]
807 * x position of tield[in]
810 * y position of tield[in]
813 * address of tiled data
816 __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
818 int pixel_x_m1, pixel_y_m1;
820 int linear_addr0, linear_addr1, bank_addr ;
824 pixel_x_m1 = x_size -1;
825 pixel_y_m1 = y_size -1;
827 roundup_x = ((pixel_x_m1 >> 7) + 1);
831 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
832 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
833 linear_addr0 = (((y_pos & 0x1f) <<4) | (x_addr & 0xf));
834 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 6) & 0x3f));
836 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
837 bank_addr = ((x_addr >> 4) & 0x1);
839 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
841 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
842 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x +((x_addr >> 5) & 0x7f));
844 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
845 bank_addr = ((x_addr >> 4) & 0x1);
847 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
850 linear_addr0 = linear_addr0 << 2;
851 trans_addr = (linear_addr1 <<13) | (bank_addr << 11) | linear_addr0;
857 * Converts tiled data to linear
858 * Crops left, top, right, buttom
859 * 1. Y of NV12T to Y of YUV420P
860 * 2. Y of NV12T to Y of YUV420S
861 * 3. UV of NV12T to UV of YUV420S
864 * Y or UV plane address of YUV420[out]
867 * Y or UV plane address of NV12T[in]
869 * @param yuv420_width
870 * Width of YUV420[in]
872 * @param yuv420_height
873 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
885 * Crop size of buttom
888 __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
889 int left, int top, int right, int buttom)
892 int tiled_offset = 0, tiled_offset1 = 0;
893 int linear_offset = 0;
894 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
896 temp3 = yuv420_width-right;
898 /* real width is greater than or equal 256 */
900 for (i = top; i < yuv420_height-buttom; i = i+1) {
906 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
907 tiled_offset = temp4-1;
908 temp1 = ((yuv420_width+127)>>7)<<7;
909 tiled_offset = tiled_offset*(temp1>>6);
910 tiled_offset = tiled_offset+temp3;
911 tiled_offset = tiled_offset+2;
912 temp1 = (temp3>>2)<<2;
913 tiled_offset = tiled_offset+temp1;
914 tiled_offset = tiled_offset<<11;
915 tiled_offset1 = tiled_offset+2048*2;
918 temp2 = ((yuv420_height+31)>>5)<<5;
919 if ((i+32) < temp2) {
920 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
922 temp1 = (temp1>>2)<<2;
923 tiled_offset = temp3+temp1;
924 temp1 = ((yuv420_width+127)>>7)<<7;
925 tiled_offset = tiled_offset+temp4*(temp1>>6);
926 tiled_offset = tiled_offset<<11;
927 tiled_offset1 = tiled_offset+2048*6;
930 /* even2 fomula: x+x_block_num*y */
931 temp1 = ((yuv420_width+127)>>7)<<7;
932 tiled_offset = temp4*(temp1>>6);
933 tiled_offset = tiled_offset+temp3;
934 tiled_offset = tiled_offset<<11;
935 tiled_offset1 = tiled_offset+2048*2;
941 tiled_offset = tiled_offset+64*(temp1);
942 tiled_offset1 = tiled_offset1+64*(temp1);
943 temp2 = yuv420_width-left-right;
944 linear_offset = temp2*(i-top);
945 temp3 = ((j+256)>>8)<<8;
949 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
950 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
951 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
952 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
953 linear_offset = linear_offset+256-temp1;
954 } else if (temp3 > 128) {
955 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
956 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
957 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
958 linear_offset = linear_offset+192-temp1;
959 } else if (temp3 > 64) {
960 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
961 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
962 linear_offset = linear_offset+128-temp1;
963 } else if (temp3 > 0) {
964 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
965 linear_offset = linear_offset+64-temp1;
968 tiled_offset = tiled_offset+temp4*2048;
971 temp2 = yuv420_width-right-256;
972 for (; j <= temp2; j = j+256) {
973 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
974 tiled_offset1 = tiled_offset1+temp4*2048;
975 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
976 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
977 tiled_offset = tiled_offset+temp4*2048;
978 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
979 linear_offset = linear_offset+256;
982 tiled_offset1 = tiled_offset1+temp4*2048;
983 temp2 = yuv420_width-right-j;
985 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
986 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
987 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
988 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
989 } else if (temp2 > 128) {
990 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
991 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
992 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
993 } else if (temp2 > 64) {
994 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
995 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
997 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
999 } else if (temp1 >= 64) {
1000 for (i = top; i < (yuv420_height-buttom); i = i+1) {
1002 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1003 temp2 = ((j+64)>>6)<<6;
1005 linear_offset = temp1*(i-top);
1007 tiled_offset = tiled_offset+temp4;
1008 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
1009 linear_offset = linear_offset+temp2;
1011 if ((j+64) <= temp3) {
1012 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1013 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1014 linear_offset = linear_offset+64;
1017 if ((j+64) <= temp3) {
1018 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1019 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
1020 linear_offset = linear_offset+64;
1024 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1026 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
1030 for (i = top; i < (yuv420_height-buttom); i = i+1) {
1031 linear_offset = temp1*(i-top);
1032 for (j = left; j < (yuv420_width-right); j = j+2) {
1033 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
1035 tiled_offset = tiled_offset+temp4;
1036 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
1037 linear_offset = linear_offset+2;
1043 #ifdef CAPTURE_OUTPUT_DUMP /* for capture output dump */
1044 static void capture_output_dump(mm_player_t* player)
1046 unsigned char *temp = NULL;
1047 char file[100] = { 0, };
1052 LOGE("capture output dump start. size = %d", player->capture.size);
1053 sprintf(file, "/tmp/dec_output_dump_%dx%d.yuv", player->captured.width[0], player->captured.height[0]);
1054 fp = fopen(file, "ab");
1056 for (i = 0; i < player->captured.plane_num; i++) {
1057 temp = player->captured.data[i];
1059 for (j = 0; j < player->captured.height[i]; j++) {
1060 ret = fwrite(temp, player->captured.width[i], 1, fp);
1061 temp += player->captured.width[i];
1064 LOGE("capture yuv dumped!! ret = %d", ret);
1067 temp = (unsigned char *)player->capture.data;
1068 sprintf(file, "/tmp/dec_output_dump_%dx%d.rgb", player->captured.width[0], player->captured.height[0]);
1069 fp = fopen(file, "ab");
1070 ret = fwrite(temp, player->capture.size, 1, fp);
1072 LOGE("capture rgb dumped!! ret = %d", ret);