From 06ff74e51dd83299f62dfc176804c57bd2f3a704 Mon Sep 17 00:00:00 2001 From: Vivia Nikolaidou Date: Fri, 30 Dec 2016 19:08:16 +0200 Subject: [PATCH] videotimecode: Add GstValue functions Add compare, serialization and deserialization functions https://bugzilla.gnome.org/show_bug.cgi?id=772764 --- gst-libs/gst/video/gstvideotimecode.c | 108 +++++++++++++++++++++++++- gst-libs/gst/video/gstvideotimecode.h | 1 + 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/gst-libs/gst/video/gstvideotimecode.c b/gst-libs/gst/video/gstvideotimecode.c index 74bb53859d..c1aabf78ca 100644 --- a/gst-libs/gst/video/gstvideotimecode.c +++ b/gst-libs/gst/video/gstvideotimecode.c @@ -18,10 +18,37 @@ */ #include "gstvideotimecode.h" +#include "stdio.h" -G_DEFINE_BOXED_TYPE (GstVideoTimeCode, gst_video_time_code, +static void +gst_video_time_code_gvalue_to_string (const GValue * tc_val, GValue * str_val); +static void +gst_video_time_code_gvalue_from_string (const GValue * str_val, + GValue * tc_val); +static gboolean gst_video_time_code_deserialize (GValue * dest, + const gchar * tc_str); +static gchar *gst_video_time_code_serialize (const GValue * val); + +static void +_init (GType type) +{ + static GstValueTable table = + { 0, (GstValueCompareFunc) gst_video_time_code_compare, + (GstValueSerializeFunc) gst_video_time_code_serialize, + (GstValueDeserializeFunc) gst_video_time_code_deserialize + }; + + table.type = type; + gst_value_register (&table); + g_value_register_transform_func (type, G_TYPE_STRING, + (GValueTransform) gst_video_time_code_gvalue_to_string); + g_value_register_transform_func (G_TYPE_STRING, type, + (GValueTransform) gst_video_time_code_gvalue_from_string); +} + +G_DEFINE_BOXED_TYPE_WITH_CODE (GstVideoTimeCode, gst_video_time_code, (GBoxedCopyFunc) gst_video_time_code_copy, - (GBoxedFreeFunc) gst_video_time_code_free); + (GBoxedFreeFunc) gst_video_time_code_free, _init (g_define_type_id)); /** * gst_video_time_code_is_valid: @@ -515,6 +542,83 @@ gst_video_time_code_new_empty (void) return tc; } +static void +gst_video_time_code_gvalue_from_string (const GValue * str_val, GValue * tc_val) +{ + const gchar *tc_str = g_value_get_string (str_val); + GstVideoTimeCode *tc; + + tc = gst_video_time_code_new_from_string (tc_str); + g_value_take_boxed (tc_val, tc); +} + +static void +gst_video_time_code_gvalue_to_string (const GValue * tc_val, GValue * str_val) +{ + const GstVideoTimeCode *tc = g_value_get_boxed (tc_val); + gchar *tc_str; + + tc_str = gst_video_time_code_to_string (tc); + g_value_take_string (str_val, tc_str); +} + +static gchar * +gst_video_time_code_serialize (const GValue * val) +{ + GstVideoTimeCode *tc = g_value_get_boxed (val); + return gst_video_time_code_to_string (tc); +} + +static gboolean +gst_video_time_code_deserialize (GValue * dest, const gchar * tc_str) +{ + GstVideoTimeCode *tc = gst_video_time_code_new_from_string (tc_str); + + if (tc == NULL || !gst_video_time_code_is_valid (tc)) + return FALSE; + + g_value_take_boxed (dest, tc); + return TRUE; +} + +/** + * gst_video_time_code_new_from_string: + * @tc_str: The string that represents the #GstVideoTimeCode + * + * Returns: a new #GstVideoTimeCode from the given string + * + * Since: 1.12 + */ +GstVideoTimeCode * +gst_video_time_code_new_from_string (const gchar * tc_str) +{ + GstVideoTimeCode *tc; + guint hours, minutes, seconds, frames; + + if (sscanf (tc_str, "%02u:%02u:%02u:%02u", &hours, &minutes, &seconds, + &frames) + == 4 + || sscanf (tc_str, "%02u:%02u:%02u;%02u", &hours, &minutes, &seconds, + &frames) + == 4 + || sscanf (tc_str, "%02u:%02u:%02u.%02u", &hours, &minutes, &seconds, + &frames) + == 4 + || sscanf (tc_str, "%02u:%02u:%02u,%02u", &hours, &minutes, &seconds, + &frames) + == 4) { + tc = gst_video_time_code_new (0, 1, NULL, GST_VIDEO_TIME_CODE_FLAGS_NONE, + hours, minutes, seconds, frames, 0); + + return tc; + } else { + GST_ERROR ("Warning: Could not parse timecode %s. " + "Please input a timecode in the form 00:00:00:00", tc_str); + return NULL; + } +} + + /** * gst_video_time_code_init: * @tc: a #GstVideoTimeCode diff --git a/gst-libs/gst/video/gstvideotimecode.h b/gst-libs/gst/video/gstvideotimecode.h index 89c28c0199..1443a7bc4e 100644 --- a/gst-libs/gst/video/gstvideotimecode.h +++ b/gst-libs/gst/video/gstvideotimecode.h @@ -113,6 +113,7 @@ GstVideoTimeCode * gst_video_time_code_new (guint fp guint frames, guint field_count); GstVideoTimeCode * gst_video_time_code_new_empty (void); +GstVideoTimeCode * gst_video_time_code_new_from_string (const gchar * tc_str); void gst_video_time_code_free (GstVideoTimeCode * tc); GstVideoTimeCode * gst_video_time_code_copy (const GstVideoTimeCode * tc);