[0.6.151] removed complexity of __mmplayer_get_video_frame_from_buffer() 82/193382/16
authorSejun Park <sejun79.park@samsung.com>
Tue, 20 Nov 2018 05:15:32 +0000 (14:15 +0900)
committerSejun Park <sejun79.park@samsung.com>
Thu, 22 Nov 2018 06:02:44 +0000 (15:02 +0900)
Change-Id: Ib74fc3cee030afe1f78b29a19f6162c219351271

packaging/libmm-player.spec
src/mm_player_capture.c

index 8672c1d..337df9b 100644 (file)
@@ -1,6 +1,6 @@
 Name:       libmm-player
 Summary:    Multimedia Framework Player Library
-Version:    0.6.150
+Version:    0.6.151
 Release:    0
 Group:      Multimedia/Libraries
 License:    Apache-2.0
index 3f742a5..c0ff4c8 100644 (file)
@@ -50,6 +50,7 @@ static int __mm_player_convert_colorspace(mm_player_t* player, unsigned char* sr
 static int __mm_player_convert_NV12_tiled(mm_player_t *player);
 static int __mm_player_convert_NV12(mm_player_t *player);
 static int __mm_player_convert_I420(mm_player_t *player);
+static int __mm_player_convert_BGRx(mm_player_t *player);
 #ifdef CAPTURE_OUTPUT_DUMP
 static void capture_output_dump(mm_player_t* player);
 #endif
@@ -286,21 +287,35 @@ __mmplayer_capture_thread(gpointer data)
                        /* Colorspace conversion : NV12T-> NV12-> RGB888 */
                        ret = __mm_player_convert_NV12_tiled(player);
                        if (ret != MM_ERROR_NONE) {
+                               LOGE("failed to covert NV12T");
                                msg.code = ret;
                                goto ERROR;
                        }
                } else if (player->video_cs == MM_PLAYER_COLORSPACE_NV12) {
                        ret = __mm_player_convert_NV12(player);
                        if (ret != MM_ERROR_NONE) {
+                               LOGE("failed to covert NV12");
                                msg.code = ret;
                                goto ERROR;
                        }
                } else if (player->video_cs == MM_PLAYER_COLORSPACE_I420) {
                        ret = __mm_player_convert_I420(player);
                        if (ret != MM_ERROR_NONE) {
+                               LOGE("failed to covert I420");
                                msg.code = ret;
                                goto ERROR;
                        }
+               } else if (player->video_cs == MM_PLAYER_COLORSPACE_BGRx) {
+                       ret = __mm_player_convert_BGRx(player);
+                       if (ret != MM_ERROR_NONE) {
+                               LOGE("failed to covert BGRx");
+                               msg.code = ret;
+                               goto ERROR;
+                       }
+               } else {
+                       LOGE("invalid format");
+                       msg.code = MM_ERROR_PLAYER_INTERNAL;
+                       goto ERROR;
                }
 
                ret = __mmplayer_get_video_angle((MMHandleType)player, &display_angle, &orientation);
@@ -328,7 +343,7 @@ __mmplayer_capture_thread(gpointer data)
 
                continue;
 ERROR:
-               for (i = 0; i < player->captured.handle_num; i++)
+               for (i = 0; i < player->captured.plane_num; i++)
                        MMPLAYER_FREEIF(player->captured.data[i]);
 
                msg.union_type = MM_MSG_UNION_CODE;
@@ -347,16 +362,13 @@ EXIT:
 static int
 __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuffer *buffer)
 {
-       int ret = MM_ERROR_NONE;
-       gint size = 0;
        gint i = 0;
-       gint src_width = 0;
-       gint src_height = 0;
+       guint plane_size = 0;
        GstCaps *caps = NULL;
-       GstStructure *structure = NULL;
-       GstMapInfo mapinfo = GST_MAP_INFO_INIT;
-       mm_util_color_format_e src_fmt = MM_UTIL_COLOR_YUV420;
-       mm_util_color_format_e dst_fmt = MM_UTIL_COLOR_RGB24; // fixed
+       GstVideoFrame vframe;
+       GstVideoInfo vinfo;
+       guint8 *pixels = NULL;
+       int ret = MM_ERROR_NONE;
 
        MMPLAYER_FENTER();
 
@@ -369,132 +381,70 @@ __mmplayer_get_video_frame_from_buffer(mm_player_t* player, GstPad *pad, GstBuff
        MMPLAYER_RETURN_VAL_IF_FAIL(caps, MM_ERROR_INVALID_ARGUMENT);
        MMPLAYER_LOG_GST_CAPS_TYPE(caps);
 
-       structure = gst_caps_get_structure(caps, 0);
-       if (!structure) {
-               LOGE("cannot get structure from caps.\n");
-               ret = MM_ERROR_PLAYER_INTERNAL;
-               goto ERROR;
-       }
-
        /* init capture image buffer */
        memset(&player->capture, 0x00, sizeof(MMPlayerVideoCapture));
 
-       gst_structure_get_int(structure, "width", &src_width);
-       gst_structure_get_int(structure, "height", &src_height);
-
-       GstVideoInfo video_info;
-       gst_video_info_from_caps(&video_info, caps);
-
-       /* check rgb or yuv */
-       if (gst_structure_has_name(structure, "video/x-raw")) {
-               /* NV12T */
-               const gchar *gst_format = gst_structure_get_string(structure, "format");
-
-               if (!g_strcmp0(gst_format, "ST12"))
-                       player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
-               else if (!g_strcmp0(gst_format, "S420"))
-                       player->video_cs = MM_PLAYER_COLORSPACE_I420;
-               else if (!g_strcmp0(gst_format, "SN12"))
-                       player->video_cs = MM_PLAYER_COLORSPACE_NV12;
-               else if (!g_strcmp0(gst_format, "BGRx"))
-                       player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
-               else
-                       player->video_cs = MM_PLAYER_COLORSPACE_MAX;
-
-               LOGI("captured format is %s\n", gst_format);
-
-               if (!g_strcmp0(gst_format, "ST12") || !g_strcmp0(gst_format, "SN12")
-                       || !g_strcmp0(gst_format, "S420")) {
-                       GstVideoFrame vframe;
-                       GstVideoFormat format;
-                       const GstVideoFormatInfo *finfo;
-
-                       /* get video frame info from proved buffer */
-                       format = gst_video_format_from_string(gst_format);
-                       finfo = gst_video_format_get_info(format);
-
-                       if (gst_video_frame_map(&vframe, &video_info, buffer, GST_MAP_READ)) {
-                               for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES(finfo); i++) {
-                                       player->captured.width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
-                                       player->captured.height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
-                                       player->captured.stride_width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
-                                       player->captured.stride_height[i] = GST_VIDEO_FRAME_COMP_HEIGHT(&vframe, i);
-                                       size = player->captured.stride_width[i] * player->captured.stride_height[i];
-                                       guint8 *pixels = GST_VIDEO_FRAME_PLANE_DATA(&vframe, i);
-
-                                       player->captured.data[i] = g_try_malloc(size);
-                                       if (!player->captured.data[i]) {
-                                               gst_video_frame_unmap(&vframe);
-                                               LOGE("no free space\n");
-                                               ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
-                                               goto ERROR;
-                                       }
-                                       memcpy(player->captured.data[i], pixels, size);
-                               }
-                               player->captured.handle_num = GST_VIDEO_FRAME_N_PLANES(&vframe);
-                               gst_video_frame_unmap(&vframe);
-                       }
-                       goto DONE;
-               } else {
-                       player->captured.width[0] = src_width;
-                       player->captured.height[0] = src_height;
-                       player->captured.stride_width[0] = MM_ALIGN(src_width, 4);
-                       player->captured.stride_height[0] = src_height;
-                       switch (GST_VIDEO_INFO_FORMAT(&video_info)) {
-                       case GST_VIDEO_FORMAT_I420:
-                               src_fmt = MM_UTIL_COLOR_I420;
-                               player->captured.width[1] = player->captured.width[2] = src_width>>1;
-                               player->captured.height[1] = player->captured.width[2] = src_height>>1;
-                               player->captured.stride_width[1] = player->captured.stride_width[2] = MM_ALIGN(player->captured.width[1], 4);
-                               player->captured.stride_height[1] = player->captured.stride_height[2] = src_height>>1;
-                               break;
-                       case GST_VIDEO_FORMAT_BGRA:
-                               src_fmt = MM_UTIL_COLOR_BGRA;
-                               break;
-                       case GST_VIDEO_FORMAT_BGRx:
-                               src_fmt = MM_UTIL_COLOR_BGRX;
-                               break;
-                       default:
-                               LOGE("unknown format to capture\n");
-                               ret = MM_ERROR_PLAYER_INTERNAL;
-                               goto ERROR;
-                               break;
-                       }
-                       player->captured.handle_num = 1;
-               }
-       } else {
-               LOGE("unknown format to capture\n");
-               ret = MM_ERROR_PLAYER_INTERNAL;
-               goto ERROR;
-       }
-
-       gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
-       __mm_player_convert_colorspace(player, mapinfo.data, src_fmt, src_width, src_height, dst_fmt);
-       gst_buffer_unmap(buffer, &mapinfo);
+       gst_video_info_from_caps(&vinfo, caps);
 
-DONE:
-       /* do convert colorspace */
-       MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
+       gst_caps_unref(caps);
 
-       if (caps) {
-               gst_caps_unref(caps);
-               caps = NULL;
-       }
+       LOGI("captured format is %s\n", gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(&vinfo)));
 
-       return ret;
+       player->captured.width[0] = GST_VIDEO_INFO_WIDTH(&vinfo);
+       player->captured.height[0] = GST_VIDEO_INFO_HEIGHT(&vinfo);
 
-ERROR:
+       switch (GST_VIDEO_INFO_FORMAT(&vinfo)) {
+       case GST_VIDEO_FORMAT_NV12:
+       case GST_VIDEO_FORMAT_SN12:
+               player->video_cs = MM_PLAYER_COLORSPACE_NV12;
+               player->captured.width[1] = player->captured.width[0];
+               player->captured.height[1] = player->captured.height[0]>>1;
+               break;
+       case GST_VIDEO_FORMAT_I420:
+       case GST_VIDEO_FORMAT_S420:
+               player->video_cs = MM_PLAYER_COLORSPACE_I420;
+               player->captured.width[1] = player->captured.width[2] = player->captured.width[0]>>1;
+               player->captured.height[1] = player->captured.height[2] = player->captured.height[0]>>1;
+               break;
+       case GST_VIDEO_FORMAT_ST12:
+               player->video_cs = MM_PLAYER_COLORSPACE_NV12_TILED;
+               player->captured.width[1] = player->captured.width[0];
+               player->captured.height[1] = player->captured.height[0]>>1;
+               break;
+       case GST_VIDEO_FORMAT_BGRx:
+               player->video_cs = MM_PLAYER_COLORSPACE_BGRx;
+               break;
+       default:
+               player->video_cs = MM_PLAYER_COLORSPACE_MAX;
+               ret = MM_ERROR_PLAYER_INTERNAL;
+               break;
+       }
 
-       if (caps) {
-               gst_caps_unref(caps);
-               caps = NULL;
+       if (ret != MM_ERROR_NONE) {
+               LOGE("unknown format to capture\n");
+               return ret;
        }
 
-       for (i = 0; i < player->captured.handle_num; i++) {
-               MMPLAYER_FREEIF(player->captured.data[i]);
+       if (gst_video_frame_map(&vframe, &vinfo, buffer, GST_MAP_READ)) {
+               for (i = 0; i < GST_VIDEO_FRAME_N_PLANES(&vframe); i++) {
+                       player->captured.stride_width[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&vframe, i);
+                       player->captured.stride_height[i] = player->captured.height[i];
+                       plane_size = player->captured.stride_width[i] * player->captured.stride_height[i];
+
+                       if (!plane_size) {
+                               LOGE("invalid plane size");
+                               return MM_ERROR_PLAYER_INTERNAL;
+                       }
+                       pixels = GST_VIDEO_FRAME_PLANE_DATA(&vframe, i);
+                       player->captured.data[i] = g_malloc(plane_size);
+                       memcpy(player->captured.data[i], pixels, plane_size);
+               }
+               player->captured.plane_num = GST_VIDEO_FRAME_N_PLANES(&vframe);
+               gst_video_frame_unmap(&vframe);
        }
+       /* do convert colorspace */
+       MMPLAYER_CAPTURE_THREAD_SIGNAL(player);
 
-       MMPLAYER_FLEAVE();
        return ret;
 }
 
@@ -817,7 +767,7 @@ static void capture_output_dump(mm_player_t* player)
 
                for (j = 0; j < player->captured.height[i]; j++) {
                        ret = fwrite(temp, player->captured.width[i], 1, fp);
-                       temp += player->captured.width[i];
+                       temp += player->captured.stride_width[i];
                }
        }
        LOGE("capture yuv dumped!! ret = %d", ret);
@@ -836,59 +786,46 @@ static int
 __mm_player_convert_NV12_tiled(mm_player_t *player)
 {
        /* Colorspace conversion : NV12T-> NV12-> RGB888 */
+       int i;
        int ret = MM_ERROR_NONE;
        unsigned char *src_buffer = NULL;
        unsigned char *linear_y_plane = NULL;
        unsigned char *linear_uv_plane = NULL;
-       int linear_y_plane_size;
-       int linear_uv_plane_size;
-       int width = player->captured.width[0];
-       int height = player->captured.height[0];
-       int i;
+       guint linear_y_plane_size;
+       guint linear_uv_plane_size;
+       guint width = player->captured.width[0];
+       guint height = player->captured.height[0];
 
        linear_y_plane_size = (width * height);
        linear_uv_plane_size = linear_y_plane_size / 2;
 
-       linear_y_plane = (unsigned char *)g_try_malloc(linear_y_plane_size);
-       if (!linear_y_plane)
-               return MM_ERROR_PLAYER_NO_FREE_SPACE;
-
-       linear_uv_plane = (unsigned char *)g_try_malloc(linear_uv_plane_size);
-       if (!linear_uv_plane) {
-               ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
-               goto EXIT;
+       if (!linear_y_plane_size || !linear_uv_plane_size) {
+               LOGE("invalid plane size");
+               return MM_ERROR_PLAYER_INTERNAL;
        }
+       linear_y_plane = (unsigned char *)g_malloc(linear_y_plane_size);
+       linear_uv_plane = (unsigned char *)g_malloc(linear_uv_plane_size);
+
        /* NV12 tiled to linear */
        __csc_tiled_to_linear_crop(linear_y_plane,
                player->captured.data[0], width, height, 0, 0, 0, 0);
        __csc_tiled_to_linear_crop(linear_uv_plane,
                player->captured.data[1], width, height / 2, 0, 0, 0, 0);
 
-       src_buffer = (unsigned char *)g_try_malloc(linear_y_plane_size + linear_uv_plane_size);
+       src_buffer = (unsigned char *)g_malloc(linear_y_plane_size + linear_uv_plane_size);
 
-       if (!src_buffer) {
-               ret = MM_ERROR_PLAYER_NO_FREE_SPACE;
-               goto EXIT;
-       }
-       memset(src_buffer, 0x00, linear_y_plane_size + linear_uv_plane_size);
        memcpy(src_buffer, linear_y_plane, linear_y_plane_size);
        memcpy(src_buffer + linear_y_plane_size, linear_uv_plane, linear_uv_plane_size);
 
        /* NV12 linear to RGB888 */
        ret = __mm_player_convert_colorspace(player, src_buffer, MM_UTIL_COLOR_NV12,
                width, height, MM_UTIL_COLOR_RGB24);
-       if (ret != MM_ERROR_NONE) {
-               LOGE("failed to convert nv12 linear");
-               goto EXIT;
-       }
 
-EXIT:
-       /* clean */
        MMPLAYER_FREEIF(src_buffer);
        MMPLAYER_FREEIF(linear_y_plane);
        MMPLAYER_FREEIF(linear_uv_plane);
 
-       for (i = 0; i < player->captured.handle_num; i++)
+       for (i = 0; i < player->captured.plane_num; i++)
                MMPLAYER_FREEIF(player->captured.data[i]);
 
        return ret;
@@ -907,17 +844,14 @@ __mm_player_convert_NV12(mm_player_t *player)
        /* using original width otherwises, app can't know aligned to resize */
        planes[0] =  player->captured.stride_width[0] * player->captured.stride_height[0];
        planes[1] = player->captured.stride_width[1] * player->captured.stride_height[1];
-       int src_buffer_size = planes[0] + planes[1];
+       guint src_buffer_size = planes[0] + planes[1];
 
        if (!src_buffer_size) {
                LOGE("invalid data size");
                return MM_ERROR_PLAYER_INTERNAL;
        }
-       src_buffer = (unsigned char *)g_try_malloc(src_buffer_size);
-       if (!src_buffer)
-               return MM_ERROR_PLAYER_NO_FREE_SPACE;
+       src_buffer = (unsigned char *)g_malloc(src_buffer_size);
 
-       memset(src_buffer, 0x00, src_buffer_size);
        p_buf = src_buffer;
 
        temp = player->captured.data[0];
@@ -942,19 +876,13 @@ __mm_player_convert_NV12(mm_player_t *player)
        ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer,
                        MM_UTIL_COLOR_NV12, player->captured.width[0],
                        player->captured.height[0], MM_UTIL_COLOR_RGB24);
-       if (ret != MM_ERROR_NONE) {
-               LOGE("failed to convert nv12 linear");
-               goto EXIT;
-       }
+
 #ifdef CAPTURE_OUTPUT_DUMP
        capture_output_dump(player);
 #endif
-
-EXIT:
-       /* clean */
        MMPLAYER_FREEIF(src_buffer);
-       /* free captured buf */
-       for (i = 0; i < player->captured.handle_num; i++)
+
+       for (i = 0; i < player->captured.plane_num; i++)
                MMPLAYER_FREEIF(player->captured.data[i]);
 
        return ret;
@@ -975,14 +903,13 @@ __mm_player_convert_I420(mm_player_t *player)
        planes[1] = planes[2] = (player->captured.stride_width[0] >> 1)
                                                                * (player->captured.stride_height[0] >> 1);
 
-       src_buffer = (unsigned char *)g_try_malloc(player->captured.stride_width[0]
-                                                                       * player->captured.stride_height[0] * 3 / 2);
-
-       if (!src_buffer)
-               return MM_ERROR_PLAYER_NO_FREE_SPACE;
+       if (!planes[0] || !planes[1] || !planes[2]) {
+               LOGE("invalid plane size");
+               return MM_ERROR_PLAYER_INTERNAL;
+       }
+       src_buffer = (unsigned char *)g_malloc(planes[0] + planes[1] + planes[2]);
 
        /* set Y plane */
-       memset(src_buffer, 0x00, planes[0]);
        p_buf = src_buffer;
 
        temp = player->captured.data[0];
@@ -994,7 +921,6 @@ __mm_player_convert_I420(mm_player_t *player)
        }
 
        /* set U plane */
-       memset(p_buf, 0x00, planes[1]);
        temp = player->captured.data[1];
 
        for (i = 0; i < player->captured.height[1]; i++) {
@@ -1004,7 +930,6 @@ __mm_player_convert_I420(mm_player_t *player)
        }
 
        /* set V plane */
-       memset(p_buf, 0x00, planes[2]);
        temp = player->captured.data[2];
 
        for (i = 0; i < player->captured.height[2]; i++) {
@@ -1016,19 +941,45 @@ __mm_player_convert_I420(mm_player_t *player)
        /* I420 -> RGB888 */
        ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, MM_UTIL_COLOR_I420,
                        player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
-       if (ret != MM_ERROR_NONE) {
-               LOGE("failed to convert I420 linear");
-               goto EXIT;
-       }
+
 #ifdef CAPTURE_OUTPUT_DUMP
        capture_output_dump(player);
 #endif
+       MMPLAYER_FREEIF(src_buffer);
 
-EXIT:
-       /* clean */
+       for (i = 0; i < player->captured.plane_num; i++)
+               MMPLAYER_FREEIF(player->captured.data[i]);
+
+       return ret;
+}
+
+static int
+__mm_player_convert_BGRx(mm_player_t *player)
+{
+       int i;
+       guint size;
+       int ret = MM_ERROR_NONE;
+       unsigned char *src_buffer = NULL;
+
+       size = (player->captured.stride_width[0] * player->captured.stride_height[0]);
+
+       if (!size) {
+               LOGE("invalid size");
+               return MM_ERROR_PLAYER_INTERNAL;
+       }
+       src_buffer = (unsigned char *)g_malloc(size);
+       memcpy(src_buffer, player->captured.data[0], size);
+
+       /* BGRx -> RGB888 */
+       ret = __mm_player_convert_colorspace(player, (unsigned char *)src_buffer, MM_UTIL_COLOR_BGRX,
+                       player->captured.width[0], player->captured.height[0], MM_UTIL_COLOR_RGB24);
+
+#ifdef CAPTURE_OUTPUT_DUMP
+       capture_output_dump(player);
+#endif
        MMPLAYER_FREEIF(src_buffer);
-       /* free captured buf */
-       for (i = 0; i < player->captured.handle_num; i++)
+
+       for (i = 0; i < player->captured.plane_num; i++)
                MMPLAYER_FREEIF(player->captured.data[i]);
 
        return ret;