2016-05-14 09:20:38 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2016> Vivia Nikolaidou <vivia@toolsonair.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2018-04-28 13:50:11 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2016-05-14 09:20:38 +00:00
|
|
|
|
2017-02-21 10:59:12 +00:00
|
|
|
#include <stdio.h>
|
2016-05-14 09:20:38 +00:00
|
|
|
#include "gstvideotimecode.h"
|
2016-12-30 17:08:16 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2016-05-14 09:20:38 +00:00
|
|
|
|
2016-12-30 17:08:16 +00:00
|
|
|
G_DEFINE_BOXED_TYPE_WITH_CODE (GstVideoTimeCode, gst_video_time_code,
|
2016-05-14 09:20:38 +00:00
|
|
|
(GBoxedCopyFunc) gst_video_time_code_copy,
|
2016-12-30 17:08:16 +00:00
|
|
|
(GBoxedFreeFunc) gst_video_time_code_free, _init (g_define_type_id));
|
2016-05-14 09:20:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_is_valid:
|
|
|
|
* @tc: #GstVideoTimeCode to check
|
|
|
|
*
|
|
|
|
* Returns: whether @tc is a valid timecode (supported frame rate,
|
|
|
|
* hours/minutes/seconds/frames not overflowing)
|
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_video_time_code_is_valid (const GstVideoTimeCode * tc)
|
|
|
|
{
|
2017-02-21 10:59:12 +00:00
|
|
|
guint fr;
|
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
g_return_val_if_fail (tc != NULL, FALSE);
|
|
|
|
|
2018-12-14 19:04:36 +00:00
|
|
|
if (tc->config.fps_n == 0 || tc->config.fps_d == 0)
|
2018-12-14 11:56:28 +00:00
|
|
|
return FALSE;
|
2017-02-21 10:59:12 +00:00
|
|
|
|
|
|
|
if (tc->hours >= 24)
|
2016-05-14 09:20:38 +00:00
|
|
|
return FALSE;
|
|
|
|
if (tc->minutes >= 60)
|
|
|
|
return FALSE;
|
|
|
|
if (tc->seconds >= 60)
|
|
|
|
return FALSE;
|
2018-12-14 11:56:28 +00:00
|
|
|
|
|
|
|
/* We can't have more frames than rounded up frames per second */
|
|
|
|
fr = (tc->config.fps_n + (tc->config.fps_d >> 1)) / tc->config.fps_d;
|
2017-02-21 10:59:12 +00:00
|
|
|
if (tc->frames >= fr && (tc->config.fps_n != 0 || tc->config.fps_d != 1))
|
2016-05-14 09:20:38 +00:00
|
|
|
return FALSE;
|
2018-12-14 11:56:28 +00:00
|
|
|
|
|
|
|
/* We either need a specific X/1001 framerate or otherwise an integer
|
|
|
|
* framerate */
|
2016-05-14 09:20:38 +00:00
|
|
|
if (tc->config.fps_d == 1001) {
|
2017-12-13 11:30:54 +00:00
|
|
|
if (tc->config.fps_n != 30000 && tc->config.fps_n != 60000 &&
|
|
|
|
tc->config.fps_n != 24000)
|
2016-05-14 09:20:38 +00:00
|
|
|
return FALSE;
|
|
|
|
} else if (tc->config.fps_n % tc->config.fps_d != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-12-14 11:56:28 +00:00
|
|
|
|
2018-12-14 12:13:18 +00:00
|
|
|
/* We only support 30000/1001 and 60000/1001 as drop-frame framerates.
|
|
|
|
* 24000/1001 is *not* a drop-frame framerate! */
|
|
|
|
if (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) {
|
|
|
|
if (tc->config.fps_d != 1001 || (tc->config.fps_n != 30000
|
|
|
|
&& tc->config.fps_n != 60000))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:56:28 +00:00
|
|
|
/* Drop-frame framerates require skipping over the first two
|
|
|
|
* timecodes every minutes except for every tenth minute in case
|
|
|
|
* of 30000/1001 and the first four timecodes for 60000/1001 */
|
2017-02-21 10:59:12 +00:00
|
|
|
if ((tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) &&
|
|
|
|
tc->minutes % 10 && tc->seconds == 0 && tc->frames < fr / 15) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-14 09:20:38 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_to_string:
|
2018-12-14 19:24:27 +00:00
|
|
|
* @tc: A #GstVideoTimeCode to convert
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Returns: the SMPTE ST 2059-1:2015 string representation of @tc. That will
|
2018-12-14 18:58:40 +00:00
|
|
|
* take the form hh:mm:ss:ff. The last separator (between seconds and frames)
|
2016-05-14 09:20:38 +00:00
|
|
|
* may vary:
|
|
|
|
*
|
|
|
|
* ';' for drop-frame, non-interlaced content and for drop-frame interlaced
|
|
|
|
* field 2
|
|
|
|
* ',' for drop-frame interlaced field 1
|
|
|
|
* ':' for non-drop-frame, non-interlaced content and for non-drop-frame
|
|
|
|
* interlaced field 2
|
|
|
|
* '.' for non-drop-frame interlaced field 1
|
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
gst_video_time_code_to_string (const GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
gchar *ret;
|
|
|
|
gboolean top_dot_present;
|
|
|
|
gchar sep;
|
|
|
|
|
|
|
|
/* Top dot is present for non-interlaced content, and for field 2 in
|
|
|
|
* interlaced content */
|
|
|
|
top_dot_present =
|
2016-08-17 10:03:43 +00:00
|
|
|
!((tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_INTERLACED) != 0
|
2016-05-14 09:20:38 +00:00
|
|
|
&& tc->field_count == 1);
|
|
|
|
|
|
|
|
if (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
|
|
|
|
sep = top_dot_present ? ';' : ',';
|
|
|
|
else
|
|
|
|
sep = top_dot_present ? ':' : '.';
|
|
|
|
|
|
|
|
ret =
|
|
|
|
g_strdup_printf ("%02d:%02d:%02d%c%02d", tc->hours, tc->minutes,
|
|
|
|
tc->seconds, sep, tc->frames);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_to_date_time:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: A valid #GstVideoTimeCode to convert
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 19:04:36 +00:00
|
|
|
* The @tc.config->latest_daily_jam is required to be non-NULL.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: (nullable): the #GDateTime representation of @tc or %NULL if @tc
|
|
|
|
* has no daily jam.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
GDateTime *
|
|
|
|
gst_video_time_code_to_date_time (const GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
GDateTime *ret;
|
|
|
|
GDateTime *ret2;
|
|
|
|
gdouble add_us;
|
|
|
|
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc), NULL);
|
|
|
|
|
2018-12-14 18:59:11 +00:00
|
|
|
if (tc->config.latest_daily_jam == NULL) {
|
2016-05-14 09:20:38 +00:00
|
|
|
gchar *tc_str = gst_video_time_code_to_string (tc);
|
|
|
|
GST_WARNING
|
|
|
|
("Asked to convert time code %s to GDateTime, but its latest daily jam is NULL",
|
|
|
|
tc_str);
|
|
|
|
g_free (tc_str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:59:11 +00:00
|
|
|
ret = g_date_time_ref (tc->config.latest_daily_jam);
|
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
gst_util_fraction_to_double (tc->frames * tc->config.fps_d, tc->config.fps_n,
|
|
|
|
&add_us);
|
|
|
|
if ((tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_INTERLACED)
|
|
|
|
&& tc->field_count == 1) {
|
|
|
|
gdouble sub_us;
|
|
|
|
|
|
|
|
gst_util_fraction_to_double (tc->config.fps_d, 2 * tc->config.fps_n,
|
|
|
|
&sub_us);
|
|
|
|
add_us -= sub_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret2 = g_date_time_add_seconds (ret, add_us + tc->seconds);
|
|
|
|
g_date_time_unref (ret);
|
|
|
|
ret = g_date_time_add_minutes (ret2, tc->minutes);
|
|
|
|
g_date_time_unref (ret2);
|
|
|
|
ret2 = g_date_time_add_hours (ret, tc->hours);
|
|
|
|
g_date_time_unref (ret);
|
|
|
|
|
|
|
|
return ret2;
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:40:21 +00:00
|
|
|
/**
|
|
|
|
* gst_video_time_code_init_from_date_time:
|
2018-12-14 18:58:40 +00:00
|
|
|
* @tc: an uninitialized #GstVideoTimeCode
|
2017-02-15 17:40:21 +00:00
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
|
|
|
* @dt: #GDateTime to convert
|
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* The resulting config->latest_daily_jam is set to midnight, and timecode is
|
|
|
|
* set to the given time.
|
2017-02-15 17:40:21 +00:00
|
|
|
*
|
2018-12-14 15:56:45 +00:00
|
|
|
* Will assert on invalid parameters, use gst_video_time_code_init_from_date_time_full()
|
|
|
|
* for being able to handle invalid parameters.
|
|
|
|
*
|
2017-02-15 17:40:21 +00:00
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_init_from_date_time (GstVideoTimeCode * tc,
|
|
|
|
guint fps_n, guint fps_d,
|
|
|
|
GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count)
|
2018-12-14 15:56:45 +00:00
|
|
|
{
|
|
|
|
if (!gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt,
|
|
|
|
flags, field_count))
|
|
|
|
g_return_if_fail (gst_video_time_code_is_valid (tc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_init_from_date_time_full:
|
|
|
|
* @tc: a #GstVideoTimeCode
|
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
|
|
|
* @dt: #GDateTime to convert
|
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
|
|
|
* The resulting config->latest_daily_jam is set to
|
|
|
|
* midnight, and timecode is set to the given time.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @tc could be correctly initialized to a valid timecode
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_video_time_code_init_from_date_time_full (GstVideoTimeCode * tc,
|
|
|
|
guint fps_n, guint fps_d,
|
|
|
|
GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count)
|
2017-02-15 17:40:21 +00:00
|
|
|
{
|
|
|
|
GDateTime *jam;
|
|
|
|
guint64 frames;
|
2017-03-10 15:40:13 +00:00
|
|
|
gboolean add_a_frame = FALSE;
|
2017-02-15 17:40:21 +00:00
|
|
|
|
2018-12-14 15:56:45 +00:00
|
|
|
g_return_val_if_fail (tc != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (dt != NULL, FALSE);
|
2018-12-14 19:04:36 +00:00
|
|
|
g_return_val_if_fail (fps_n != 0 && fps_d != 0, FALSE);
|
2018-12-14 15:56:45 +00:00
|
|
|
|
|
|
|
gst_video_time_code_clear (tc);
|
|
|
|
|
2017-02-15 17:40:21 +00:00
|
|
|
jam = g_date_time_new_local (g_date_time_get_year (dt),
|
|
|
|
g_date_time_get_month (dt), g_date_time_get_day_of_month (dt), 0, 0, 0.0);
|
|
|
|
|
|
|
|
/* Note: This might be inaccurate for 1 frame
|
|
|
|
* in case we have a drop frame timecode */
|
|
|
|
frames =
|
|
|
|
gst_util_uint64_scale_round (g_date_time_get_microsecond (dt) *
|
|
|
|
G_GINT64_CONSTANT (1000), fps_n, fps_d * GST_SECOND);
|
2017-04-06 19:38:34 +00:00
|
|
|
if (G_UNLIKELY (((frames == fps_n) && (fps_d == 1)) ||
|
|
|
|
((frames == fps_n / 1000) && (fps_d == 1001)))) {
|
2017-03-10 15:40:13 +00:00
|
|
|
/* Avoid invalid timecodes */
|
|
|
|
frames--;
|
|
|
|
add_a_frame = TRUE;
|
|
|
|
}
|
2017-02-15 17:40:21 +00:00
|
|
|
|
|
|
|
gst_video_time_code_init (tc, fps_n, fps_d, jam, flags,
|
|
|
|
g_date_time_get_hour (dt), g_date_time_get_minute (dt),
|
|
|
|
g_date_time_get_second (dt), frames, field_count);
|
|
|
|
|
|
|
|
if (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) {
|
|
|
|
guint df = (tc->config.fps_n + (tc->config.fps_d >> 1)) /
|
|
|
|
(15 * tc->config.fps_d);
|
|
|
|
if (tc->minutes % 10 && tc->seconds == 0 && tc->frames < df) {
|
|
|
|
tc->frames = df;
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 15:40:13 +00:00
|
|
|
if (add_a_frame)
|
|
|
|
gst_video_time_code_increment_frame (tc);
|
2017-02-15 17:40:21 +00:00
|
|
|
|
|
|
|
g_date_time_unref (jam);
|
|
|
|
|
2018-12-14 15:56:45 +00:00
|
|
|
return gst_video_time_code_is_valid (tc);
|
2017-02-15 17:40:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
/**
|
|
|
|
* gst_video_time_code_nsec_since_daily_jam:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: a valid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 19:04:36 +00:00
|
|
|
* Returns: how many nsec have passed since the daily jam of @tc.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
guint64
|
|
|
|
gst_video_time_code_nsec_since_daily_jam (const GstVideoTimeCode * tc)
|
|
|
|
{
|
2016-11-16 17:13:14 +00:00
|
|
|
guint64 frames, nsec;
|
2016-05-14 09:20:38 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc), -1);
|
|
|
|
|
2016-11-16 17:13:14 +00:00
|
|
|
frames = gst_video_time_code_frames_since_daily_jam (tc);
|
|
|
|
nsec =
|
|
|
|
gst_util_uint64_scale (frames, GST_SECOND * tc->config.fps_d,
|
|
|
|
tc->config.fps_n);
|
2016-05-14 09:20:38 +00:00
|
|
|
|
|
|
|
return nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_frames_since_daily_jam:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: a valid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 19:04:36 +00:00
|
|
|
* Returns: how many frames have passed since the daily jam of @tc.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
guint64
|
|
|
|
gst_video_time_code_frames_since_daily_jam (const GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
guint ff_nom;
|
|
|
|
gdouble ff;
|
|
|
|
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc), -1);
|
|
|
|
|
|
|
|
gst_util_fraction_to_double (tc->config.fps_n, tc->config.fps_d, &ff);
|
|
|
|
if (tc->config.fps_d == 1001) {
|
|
|
|
ff_nom = tc->config.fps_n / 1000;
|
|
|
|
} else {
|
|
|
|
ff_nom = ff;
|
|
|
|
}
|
|
|
|
if (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) {
|
|
|
|
/* these need to be truncated to integer: side effect, code looks cleaner
|
|
|
|
* */
|
|
|
|
guint ff_minutes = 60 * ff;
|
|
|
|
guint ff_hours = 3600 * ff;
|
|
|
|
/* for 30000/1001 we drop the first 2 frames per minute, for 60000/1001 we
|
|
|
|
* drop the first 4 : so we use this number */
|
|
|
|
guint dropframe_multiplier;
|
|
|
|
|
|
|
|
if (tc->config.fps_n == 30000) {
|
|
|
|
dropframe_multiplier = 2;
|
|
|
|
} else if (tc->config.fps_n == 60000) {
|
|
|
|
dropframe_multiplier = 4;
|
|
|
|
} else {
|
2018-12-14 19:00:03 +00:00
|
|
|
/* already checked by gst_video_time_code_is_valid() */
|
|
|
|
g_assert_not_reached ();
|
2016-05-14 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tc->frames + (ff_nom * tc->seconds) +
|
|
|
|
(ff_minutes * tc->minutes) +
|
|
|
|
dropframe_multiplier * ((gint) (tc->minutes / 10)) +
|
|
|
|
(ff_hours * tc->hours);
|
|
|
|
} else {
|
|
|
|
return tc->frames + (ff_nom * (tc->seconds + (60 * (tc->minutes +
|
|
|
|
(60 * tc->hours)))));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_increment_frame:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: a valid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Adds one frame to @tc.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_increment_frame (GstVideoTimeCode * tc)
|
|
|
|
{
|
2016-11-11 18:35:00 +00:00
|
|
|
gst_video_time_code_add_frames (tc, 1);
|
2016-05-14 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_add_frames:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: a valid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
* @frames: How many frames to add or subtract
|
|
|
|
*
|
2017-02-21 10:59:12 +00:00
|
|
|
* Adds or subtracts @frames amount of frames to @tc. tc needs to
|
2018-12-14 18:58:40 +00:00
|
|
|
* contain valid data, as verified by gst_video_time_code_is_valid().
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_add_frames (GstVideoTimeCode * tc, gint64 frames)
|
|
|
|
{
|
|
|
|
guint64 framecount;
|
|
|
|
guint64 h_notmod24;
|
|
|
|
guint64 h_new, min_new, sec_new, frames_new;
|
|
|
|
gdouble ff;
|
|
|
|
guint ff_nom;
|
2016-08-19 12:29:13 +00:00
|
|
|
/* This allows for better readability than putting G_GUINT64_CONSTANT(60)
|
|
|
|
* into a long calculation line */
|
|
|
|
const guint64 sixty = 60;
|
2016-05-14 09:20:38 +00:00
|
|
|
/* formulas found in SMPTE ST 2059-1:2015 section 9.4.3
|
|
|
|
* and adapted for 60/1.001 as well as 30/1.001 */
|
|
|
|
|
|
|
|
g_return_if_fail (gst_video_time_code_is_valid (tc));
|
|
|
|
|
|
|
|
gst_util_fraction_to_double (tc->config.fps_n, tc->config.fps_d, &ff);
|
|
|
|
if (tc->config.fps_d == 1001) {
|
|
|
|
ff_nom = tc->config.fps_n / 1000;
|
|
|
|
} else {
|
|
|
|
ff_nom = ff;
|
|
|
|
}
|
2018-12-14 19:00:03 +00:00
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
if (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) {
|
|
|
|
/* these need to be truncated to integer: side effect, code looks cleaner
|
|
|
|
* */
|
|
|
|
guint ff_minutes = 60 * ff;
|
|
|
|
guint ff_hours = 3600 * ff;
|
|
|
|
/* a bunch of intermediate variables, to avoid monster code with possible
|
|
|
|
* integer overflows */
|
|
|
|
guint64 min_new_tmp1, min_new_tmp2, min_new_tmp3, min_new_denom;
|
|
|
|
/* for 30000/1001 we drop the first 2 frames per minute, for 60000/1001 we
|
|
|
|
* drop the first 4 : so we use this number */
|
|
|
|
guint dropframe_multiplier;
|
|
|
|
|
2018-12-14 19:00:03 +00:00
|
|
|
if (tc->config.fps_n == 30000) {
|
2016-05-14 09:20:38 +00:00
|
|
|
dropframe_multiplier = 2;
|
2018-12-14 19:00:03 +00:00
|
|
|
} else if (tc->config.fps_n == 60000) {
|
2016-05-14 09:20:38 +00:00
|
|
|
dropframe_multiplier = 4;
|
2018-12-14 19:00:03 +00:00
|
|
|
} else {
|
|
|
|
/* already checked by gst_video_time_code_is_valid() */
|
|
|
|
g_assert_not_reached ();
|
2016-05-14 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
framecount =
|
|
|
|
frames + tc->frames + (ff_nom * tc->seconds) +
|
|
|
|
(ff_minutes * tc->minutes) +
|
|
|
|
dropframe_multiplier * ((gint) (tc->minutes / 10)) +
|
|
|
|
(ff_hours * tc->hours);
|
|
|
|
h_notmod24 = gst_util_uint64_scale_int (framecount, 1, ff_hours);
|
|
|
|
|
2016-08-19 12:29:13 +00:00
|
|
|
min_new_denom = sixty * ff_nom;
|
2016-05-14 09:20:38 +00:00
|
|
|
min_new_tmp1 = (framecount - (h_notmod24 * ff_hours)) / min_new_denom;
|
|
|
|
min_new_tmp2 = framecount + dropframe_multiplier * min_new_tmp1;
|
2016-08-19 12:29:13 +00:00
|
|
|
min_new_tmp1 =
|
|
|
|
(framecount - (h_notmod24 * ff_hours)) / (sixty * 10 * ff_nom);
|
2016-05-14 09:20:38 +00:00
|
|
|
min_new_tmp3 =
|
|
|
|
dropframe_multiplier * min_new_tmp1 + (h_notmod24 * ff_hours);
|
|
|
|
min_new =
|
|
|
|
gst_util_uint64_scale_int (min_new_tmp2 - min_new_tmp3, 1,
|
|
|
|
min_new_denom);
|
|
|
|
|
|
|
|
sec_new =
|
|
|
|
(guint64) ((framecount - (ff_minutes * min_new) -
|
|
|
|
dropframe_multiplier * ((gint) (min_new / 10)) -
|
|
|
|
(ff_hours * h_notmod24)) / ff_nom);
|
|
|
|
|
|
|
|
frames_new =
|
|
|
|
framecount - (ff_nom * sec_new) - (ff_minutes * min_new) -
|
|
|
|
(dropframe_multiplier * ((gint) (min_new / 10))) -
|
|
|
|
(ff_hours * h_notmod24);
|
|
|
|
} else {
|
|
|
|
framecount =
|
2016-08-19 12:29:13 +00:00
|
|
|
frames + tc->frames + (ff_nom * (tc->seconds + (sixty * (tc->minutes +
|
|
|
|
(sixty * tc->hours)))));
|
|
|
|
h_notmod24 =
|
|
|
|
gst_util_uint64_scale_int (framecount, 1, ff_nom * sixty * sixty);
|
2016-05-14 09:20:38 +00:00
|
|
|
min_new =
|
2016-08-19 12:29:13 +00:00
|
|
|
gst_util_uint64_scale_int ((framecount -
|
|
|
|
(ff_nom * sixty * sixty * h_notmod24)), 1, (ff_nom * sixty));
|
2016-05-14 09:20:38 +00:00
|
|
|
sec_new =
|
2016-08-19 12:29:13 +00:00
|
|
|
gst_util_uint64_scale_int ((framecount - (ff_nom * sixty * (min_new +
|
|
|
|
(sixty * h_notmod24)))), 1, ff_nom);
|
2016-05-14 09:20:38 +00:00
|
|
|
frames_new =
|
2016-08-19 12:29:13 +00:00
|
|
|
framecount - (ff_nom * (sec_new + sixty * (min_new +
|
|
|
|
(sixty * h_notmod24))));
|
2016-05-14 09:20:38 +00:00
|
|
|
if (frames_new > ff_nom)
|
|
|
|
frames_new = 0;
|
|
|
|
}
|
2018-12-14 19:00:03 +00:00
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
h_new = h_notmod24 % 24;
|
|
|
|
|
2018-12-14 19:00:03 +00:00
|
|
|
/* The calculations above should always give correct results */
|
2016-05-14 09:20:38 +00:00
|
|
|
g_assert (min_new < 60);
|
|
|
|
g_assert (sec_new < 60);
|
|
|
|
g_assert (frames_new < ff_nom);
|
2018-12-14 19:00:03 +00:00
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
tc->hours = h_new;
|
|
|
|
tc->minutes = min_new;
|
|
|
|
tc->seconds = sec_new;
|
|
|
|
tc->frames = frames_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_compare:
|
2018-12-14 18:58:40 +00:00
|
|
|
* @tc1: a valid #GstVideoTimeCode
|
2018-12-14 19:04:36 +00:00
|
|
|
* @tc2: another valid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Compares @tc1 and @tc2. If both have latest daily jam information, it is
|
2016-05-14 09:20:38 +00:00
|
|
|
* taken into account. Otherwise, it is assumed that the daily jam of both
|
2018-12-14 19:04:36 +00:00
|
|
|
* @tc1 and @tc2 was at the same time. Both time codes must be valid.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Returns: 1 if @tc1 is after @tc2, -1 if @tc1 is before @tc2, 0 otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
gint
|
|
|
|
gst_video_time_code_compare (const GstVideoTimeCode * tc1,
|
|
|
|
const GstVideoTimeCode * tc2)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc1), -1);
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc2), -1);
|
|
|
|
|
|
|
|
if (tc1->config.latest_daily_jam == NULL
|
|
|
|
|| tc2->config.latest_daily_jam == NULL) {
|
|
|
|
guint64 nsec1, nsec2;
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
gchar *str1, *str2;
|
|
|
|
|
|
|
|
str1 = gst_video_time_code_to_string (tc1);
|
|
|
|
str2 = gst_video_time_code_to_string (tc2);
|
|
|
|
GST_INFO
|
|
|
|
("Comparing time codes %s and %s, but at least one of them has no "
|
|
|
|
"latest daily jam information. Assuming they started together",
|
|
|
|
str1, str2);
|
|
|
|
g_free (str1);
|
|
|
|
g_free (str2);
|
|
|
|
#endif
|
|
|
|
if (tc1->hours > tc2->hours) {
|
|
|
|
return 1;
|
|
|
|
} else if (tc1->hours < tc2->hours) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tc1->minutes > tc2->minutes) {
|
|
|
|
return 1;
|
|
|
|
} else if (tc1->minutes < tc2->minutes) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tc1->seconds > tc2->seconds) {
|
|
|
|
return 1;
|
|
|
|
} else if (tc1->seconds < tc2->seconds) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsec1 =
|
|
|
|
gst_util_uint64_scale (GST_SECOND,
|
|
|
|
tc1->frames * tc1->config.fps_n, tc1->config.fps_d);
|
|
|
|
nsec2 =
|
|
|
|
gst_util_uint64_scale (GST_SECOND,
|
|
|
|
tc2->frames * tc2->config.fps_n, tc2->config.fps_d);
|
|
|
|
if (nsec1 > nsec2) {
|
|
|
|
return 1;
|
|
|
|
} else if (nsec1 < nsec2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (tc1->config.flags & GST_VIDEO_TIME_CODE_FLAGS_INTERLACED) {
|
|
|
|
if (tc1->field_count > tc2->field_count)
|
|
|
|
return 1;
|
|
|
|
else if (tc1->field_count < tc2->field_count)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
GDateTime *dt1, *dt2;
|
|
|
|
gint ret;
|
|
|
|
|
|
|
|
dt1 = gst_video_time_code_to_date_time (tc1);
|
|
|
|
dt2 = gst_video_time_code_to_date_time (tc2);
|
|
|
|
|
|
|
|
ret = g_date_time_compare (dt1, dt2);
|
|
|
|
|
|
|
|
g_date_time_unref (dt1);
|
|
|
|
g_date_time_unref (dt2);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_new:
|
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
|
|
|
* @latest_daily_jam: The latest daily jam of the #GstVideoTimeCode
|
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @hours: the hours field of #GstVideoTimeCode
|
|
|
|
* @minutes: the minutes field of #GstVideoTimeCode
|
|
|
|
* @seconds: the seconds field of #GstVideoTimeCode
|
|
|
|
* @frames: the frames field of #GstVideoTimeCode
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
|
|
|
* @field_count is 0 for progressive, 1 or 2 for interlaced.
|
|
|
|
* @latest_daiy_jam reference is stolen from caller.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstVideoTimeCode with the given values.
|
2017-02-21 10:59:12 +00:00
|
|
|
* The values are not checked for being in a valid range. To see if your
|
2018-12-14 18:58:40 +00:00
|
|
|
* timecode actually has valid content, use gst_video_time_code_is_valid().
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_new (guint fps_n, guint fps_d, GDateTime * latest_daily_jam,
|
|
|
|
GstVideoTimeCodeFlags flags, guint hours, guint minutes, guint seconds,
|
|
|
|
guint frames, guint field_count)
|
|
|
|
{
|
|
|
|
GstVideoTimeCode *tc;
|
|
|
|
|
|
|
|
tc = g_new0 (GstVideoTimeCode, 1);
|
|
|
|
gst_video_time_code_init (tc, fps_n, fps_d, latest_daily_jam, flags, hours,
|
|
|
|
minutes, seconds, frames, field_count);
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_new_empty:
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: a new empty, invalid #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_new_empty (void)
|
|
|
|
{
|
|
|
|
GstVideoTimeCode *tc;
|
|
|
|
|
|
|
|
tc = g_new0 (GstVideoTimeCode, 1);
|
|
|
|
gst_video_time_code_clear (tc);
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:08:16 +00:00
|
|
|
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);
|
|
|
|
|
2018-12-14 19:18:34 +00:00
|
|
|
if (tc == NULL) {
|
2016-12-30 17:08:16 +00:00
|
|
|
return FALSE;
|
2018-12-14 19:18:34 +00:00
|
|
|
}
|
2016-12-30 17:08:16 +00:00
|
|
|
|
|
|
|
g_value_take_boxed (dest, tc);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_new_from_string:
|
|
|
|
* @tc_str: The string that represents the #GstVideoTimeCode
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: (nullable): a new #GstVideoTimeCode from the given string or %NULL
|
|
|
|
* if the string could not be passed.
|
2016-12-30 17:08:16 +00:00
|
|
|
*
|
|
|
|
* 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) {
|
|
|
|
tc = gst_video_time_code_new (0, 1, NULL, GST_VIDEO_TIME_CODE_FLAGS_NONE,
|
|
|
|
hours, minutes, seconds, frames, 0);
|
|
|
|
|
2018-12-17 17:41:26 +00:00
|
|
|
return tc;
|
|
|
|
} else 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) {
|
|
|
|
tc = gst_video_time_code_new (0, 1, NULL,
|
|
|
|
GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME, hours, minutes, seconds, frames,
|
|
|
|
0);
|
|
|
|
|
2016-12-30 17:08:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:40:21 +00:00
|
|
|
/**
|
|
|
|
* gst_video_time_code_new_from_date_time:
|
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
|
|
|
* @dt: #GDateTime to convert
|
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
|
|
|
* The resulting config->latest_daily_jam is set to
|
|
|
|
* midnight, and timecode is set to the given time.
|
|
|
|
*
|
2018-12-14 15:56:45 +00:00
|
|
|
* This might return a completely invalid timecode, use
|
|
|
|
* gst_video_time_code_new_from_date_time_full() to ensure
|
|
|
|
* that you would get %NULL instead in that case.
|
|
|
|
*
|
2018-04-02 06:34:58 +00:00
|
|
|
* Returns: the #GstVideoTimeCode representation of @dt.
|
2017-02-15 17:40:21 +00:00
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_new_from_date_time (guint fps_n, guint fps_d,
|
|
|
|
GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count)
|
|
|
|
{
|
|
|
|
GstVideoTimeCode *tc;
|
|
|
|
tc = gst_video_time_code_new_empty ();
|
2018-12-14 15:56:45 +00:00
|
|
|
gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt, flags,
|
2017-02-15 17:40:21 +00:00
|
|
|
field_count);
|
|
|
|
return tc;
|
|
|
|
}
|
2016-12-30 17:08:16 +00:00
|
|
|
|
2018-12-14 15:56:45 +00:00
|
|
|
/**
|
|
|
|
* gst_video_time_code_new_from_date_time_full:
|
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
|
|
|
* @dt: #GDateTime to convert
|
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
|
|
|
* The resulting config->latest_daily_jam is set to
|
|
|
|
* midnight, and timecode is set to the given time.
|
|
|
|
*
|
|
|
|
* Returns: the #GstVideoTimeCode representation of @dt, or %NULL if
|
|
|
|
* no valid timecode could be created.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_new_from_date_time_full (guint fps_n, guint fps_d,
|
|
|
|
GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count)
|
|
|
|
{
|
|
|
|
GstVideoTimeCode *tc;
|
|
|
|
tc = gst_video_time_code_new_empty ();
|
|
|
|
if (!gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt,
|
|
|
|
flags, field_count)) {
|
|
|
|
gst_video_time_code_free (tc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2016-05-14 09:20:38 +00:00
|
|
|
/**
|
|
|
|
* gst_video_time_code_init:
|
|
|
|
* @tc: a #GstVideoTimeCode
|
|
|
|
* @fps_n: Numerator of the frame rate
|
|
|
|
* @fps_d: Denominator of the frame rate
|
2019-05-01 21:22:24 +00:00
|
|
|
* @latest_daily_jam: (allow-none): The latest daily jam of the #GstVideoTimeCode
|
2016-05-14 09:20:38 +00:00
|
|
|
* @flags: #GstVideoTimeCodeFlags
|
|
|
|
* @hours: the hours field of #GstVideoTimeCode
|
|
|
|
* @minutes: the minutes field of #GstVideoTimeCode
|
|
|
|
* @seconds: the seconds field of #GstVideoTimeCode
|
|
|
|
* @frames: the frames field of #GstVideoTimeCode
|
|
|
|
* @field_count: Interlaced video field count
|
|
|
|
*
|
|
|
|
* @field_count is 0 for progressive, 1 or 2 for interlaced.
|
|
|
|
* @latest_daiy_jam reference is stolen from caller.
|
|
|
|
*
|
|
|
|
* Initializes @tc with the given values.
|
2017-02-21 10:59:12 +00:00
|
|
|
* The values are not checked for being in a valid range. To see if your
|
2018-12-14 18:58:40 +00:00
|
|
|
* timecode actually has valid content, use gst_video_time_code_is_valid().
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_init (GstVideoTimeCode * tc, guint fps_n, guint fps_d,
|
|
|
|
GDateTime * latest_daily_jam, GstVideoTimeCodeFlags flags, guint hours,
|
|
|
|
guint minutes, guint seconds, guint frames, guint field_count)
|
|
|
|
{
|
|
|
|
tc->hours = hours;
|
|
|
|
tc->minutes = minutes;
|
|
|
|
tc->seconds = seconds;
|
|
|
|
tc->frames = frames;
|
|
|
|
tc->field_count = field_count;
|
|
|
|
tc->config.fps_n = fps_n;
|
|
|
|
tc->config.fps_d = fps_d;
|
|
|
|
if (latest_daily_jam != NULL)
|
|
|
|
tc->config.latest_daily_jam = g_date_time_ref (latest_daily_jam);
|
|
|
|
else
|
|
|
|
tc->config.latest_daily_jam = NULL;
|
|
|
|
tc->config.flags = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_clear:
|
|
|
|
* @tc: a #GstVideoTimeCode
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Initializes @tc with empty/zero/NULL values and frees any memory
|
|
|
|
* it might currently use.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_clear (GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
tc->hours = 0;
|
|
|
|
tc->minutes = 0;
|
|
|
|
tc->seconds = 0;
|
|
|
|
tc->frames = 0;
|
|
|
|
tc->field_count = 0;
|
|
|
|
tc->config.fps_n = 0;
|
|
|
|
tc->config.fps_d = 1;
|
|
|
|
if (tc->config.latest_daily_jam != NULL)
|
|
|
|
g_date_time_unref (tc->config.latest_daily_jam);
|
|
|
|
tc->config.latest_daily_jam = NULL;
|
|
|
|
tc->config.flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_copy:
|
|
|
|
* @tc: a #GstVideoTimeCode
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: a new #GstVideoTimeCode with the same values as @tc.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_copy (const GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
return gst_video_time_code_new (tc->config.fps_n, tc->config.fps_d,
|
|
|
|
tc->config.latest_daily_jam, tc->config.flags, tc->hours, tc->minutes,
|
|
|
|
tc->seconds, tc->frames, tc->field_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_free:
|
|
|
|
* @tc: a #GstVideoTimeCode
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Frees @tc.
|
2016-05-14 09:20:38 +00:00
|
|
|
*
|
|
|
|
* Since: 1.10
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_free (GstVideoTimeCode * tc)
|
|
|
|
{
|
|
|
|
if (tc->config.latest_daily_jam != NULL)
|
|
|
|
g_date_time_unref (tc->config.latest_daily_jam);
|
|
|
|
|
|
|
|
g_free (tc);
|
|
|
|
}
|
2016-12-29 12:42:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_add_interval:
|
2017-02-21 10:59:12 +00:00
|
|
|
* @tc: The #GstVideoTimeCode where the diff should be added. This
|
|
|
|
* must contain valid timecode values.
|
|
|
|
* @tc_inter: The #GstVideoTimeCodeInterval to add to @tc.
|
|
|
|
* The interval must contain valid values, except that for drop-frame
|
|
|
|
* timecode, it may also contain timecodes which would normally
|
|
|
|
* be dropped. These are then corrected to the next reasonable timecode.
|
2016-12-29 12:42:52 +00:00
|
|
|
*
|
|
|
|
* This makes a component-wise addition of @tc_inter to @tc. For example,
|
|
|
|
* adding ("01:02:03:04", "00:01:00:00") will return "01:03:03:04".
|
|
|
|
* When it comes to drop-frame timecodes,
|
|
|
|
* adding ("00:00:00;00", "00:01:00:00") will return "00:01:00;02"
|
|
|
|
* because of drop-frame oddities. However,
|
|
|
|
* adding ("00:09:00;02", "00:01:00:00") will return "00:10:00;00"
|
|
|
|
* because this time we can have an exact minute.
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: (nullable): A new #GstVideoTimeCode with @tc_inter added or %NULL
|
|
|
|
* if the interval can't be added.
|
2016-12-29 12:42:52 +00:00
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
GstVideoTimeCode *
|
|
|
|
gst_video_time_code_add_interval (const GstVideoTimeCode * tc,
|
|
|
|
const GstVideoTimeCodeInterval * tc_inter)
|
|
|
|
{
|
2017-02-21 10:59:12 +00:00
|
|
|
GstVideoTimeCode *ret;
|
|
|
|
guint frames_to_add;
|
|
|
|
guint df;
|
|
|
|
gboolean needs_correction;
|
|
|
|
|
|
|
|
g_return_val_if_fail (gst_video_time_code_is_valid (tc), NULL);
|
|
|
|
|
|
|
|
ret = gst_video_time_code_new (tc->config.fps_n, tc->config.fps_d,
|
2016-12-29 12:42:52 +00:00
|
|
|
tc->config.latest_daily_jam, tc->config.flags, tc_inter->hours,
|
|
|
|
tc_inter->minutes, tc_inter->seconds, tc_inter->frames, 0);
|
2017-02-21 10:59:12 +00:00
|
|
|
|
|
|
|
df = (tc->config.fps_n + (tc->config.fps_d >> 1)) / (tc->config.fps_d * 15);
|
|
|
|
|
|
|
|
/* Drop-frame compensation: Create a valid timecode from the
|
|
|
|
* interval */
|
|
|
|
needs_correction = (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME)
|
|
|
|
&& ret->minutes % 10 && ret->seconds == 0 && ret->frames < df;
|
|
|
|
if (needs_correction) {
|
|
|
|
ret->minutes--;
|
|
|
|
ret->seconds = 59;
|
|
|
|
ret->frames = df * 14;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_time_code_is_valid (ret)) {
|
|
|
|
GST_ERROR ("Unsupported time code interval");
|
|
|
|
gst_video_time_code_free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
frames_to_add = gst_video_time_code_frames_since_daily_jam (tc);
|
2016-12-29 12:42:52 +00:00
|
|
|
|
|
|
|
/* Drop-frame compensation: 00:01:00;00 is falsely interpreted as
|
|
|
|
* 00:00:59;28 */
|
2017-02-21 10:59:12 +00:00
|
|
|
if (needs_correction) {
|
2016-12-29 12:42:52 +00:00
|
|
|
/* User wants us to split at invalid timecodes */
|
2017-02-21 10:59:12 +00:00
|
|
|
if (tc->minutes % 10 == 0 && tc->frames <= df) {
|
2016-12-29 12:42:52 +00:00
|
|
|
/* Apply compensation every 10th minute: before adding the frames,
|
|
|
|
* but only if we are before the "invalid frame" mark */
|
2017-02-21 10:59:12 +00:00
|
|
|
frames_to_add += df;
|
|
|
|
needs_correction = FALSE;
|
2016-12-29 12:42:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_video_time_code_add_frames (ret, frames_to_add);
|
2017-02-21 10:59:12 +00:00
|
|
|
if (needs_correction && ret->minutes % 10 == 0 && tc->frames > df) {
|
|
|
|
gst_video_time_code_add_frames (ret, df);
|
2016-12-29 12:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (GstVideoTimeCodeInterval, gst_video_time_code_interval,
|
|
|
|
(GBoxedCopyFunc) gst_video_time_code_interval_copy,
|
|
|
|
(GBoxedFreeFunc) gst_video_time_code_interval_free);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_new:
|
|
|
|
* @hours: the hours field of #GstVideoTimeCodeInterval
|
|
|
|
* @minutes: the minutes field of #GstVideoTimeCodeInterval
|
|
|
|
* @seconds: the seconds field of #GstVideoTimeCodeInterval
|
|
|
|
* @frames: the frames field of #GstVideoTimeCodeInterval
|
|
|
|
*
|
|
|
|
* Returns: a new #GstVideoTimeCodeInterval with the given values.
|
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
GstVideoTimeCodeInterval *
|
|
|
|
gst_video_time_code_interval_new (guint hours, guint minutes, guint seconds,
|
|
|
|
guint frames)
|
|
|
|
{
|
|
|
|
GstVideoTimeCodeInterval *tc;
|
|
|
|
|
|
|
|
tc = g_new0 (GstVideoTimeCodeInterval, 1);
|
|
|
|
gst_video_time_code_interval_init (tc, hours, minutes, seconds, frames);
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_new_from_string:
|
|
|
|
* @tc_inter_str: The string that represents the #GstVideoTimeCodeInterval
|
|
|
|
*
|
|
|
|
* @tc_inter_str must only have ":" as separators.
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: (nullable): a new #GstVideoTimeCodeInterval from the given string
|
|
|
|
* or %NULL if the string could not be passed.
|
2016-12-29 12:42:52 +00:00
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
GstVideoTimeCodeInterval *
|
|
|
|
gst_video_time_code_interval_new_from_string (const gchar * tc_inter_str)
|
|
|
|
{
|
|
|
|
GstVideoTimeCodeInterval *tc;
|
|
|
|
guint hours, minutes, seconds, frames;
|
|
|
|
|
|
|
|
if (sscanf (tc_inter_str, "%02u:%02u:%02u:%02u", &hours, &minutes, &seconds,
|
|
|
|
&frames)
|
|
|
|
== 4
|
|
|
|
|| sscanf (tc_inter_str, "%02u:%02u:%02u;%02u", &hours, &minutes,
|
|
|
|
&seconds, &frames)
|
|
|
|
== 4
|
|
|
|
|| sscanf (tc_inter_str, "%02u:%02u:%02u.%02u", &hours, &minutes,
|
|
|
|
&seconds, &frames)
|
|
|
|
== 4
|
|
|
|
|| sscanf (tc_inter_str, "%02u:%02u:%02u,%02u", &hours, &minutes,
|
|
|
|
&seconds, &frames)
|
|
|
|
== 4) {
|
|
|
|
tc = gst_video_time_code_interval_new (hours, minutes, seconds, frames);
|
|
|
|
|
|
|
|
return tc;
|
|
|
|
} else {
|
|
|
|
GST_ERROR ("Warning: Could not parse timecode %s. "
|
|
|
|
"Please input a timecode in the form 00:00:00:00", tc_inter_str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_init:
|
|
|
|
* @tc: a #GstVideoTimeCodeInterval
|
|
|
|
* @hours: the hours field of #GstVideoTimeCodeInterval
|
|
|
|
* @minutes: the minutes field of #GstVideoTimeCodeInterval
|
|
|
|
* @seconds: the seconds field of #GstVideoTimeCodeInterval
|
|
|
|
* @frames: the frames field of #GstVideoTimeCodeInterval
|
|
|
|
*
|
|
|
|
* Initializes @tc with the given values.
|
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_interval_init (GstVideoTimeCodeInterval * tc, guint hours,
|
|
|
|
guint minutes, guint seconds, guint frames)
|
|
|
|
{
|
|
|
|
tc->hours = hours;
|
|
|
|
tc->minutes = minutes;
|
|
|
|
tc->seconds = seconds;
|
|
|
|
tc->frames = frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_clear:
|
|
|
|
* @tc: a #GstVideoTimeCodeInterval
|
|
|
|
*
|
|
|
|
* Initializes @tc with empty/zero/NULL values.
|
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_interval_clear (GstVideoTimeCodeInterval * tc)
|
|
|
|
{
|
|
|
|
tc->hours = 0;
|
|
|
|
tc->minutes = 0;
|
|
|
|
tc->seconds = 0;
|
|
|
|
tc->frames = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_copy:
|
|
|
|
* @tc: a #GstVideoTimeCodeInterval
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Returns: a new #GstVideoTimeCodeInterval with the same values as @tc.
|
2016-12-29 12:42:52 +00:00
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
GstVideoTimeCodeInterval *
|
|
|
|
gst_video_time_code_interval_copy (const GstVideoTimeCodeInterval * tc)
|
|
|
|
{
|
|
|
|
return gst_video_time_code_interval_new (tc->hours, tc->minutes,
|
|
|
|
tc->seconds, tc->frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_video_time_code_interval_free:
|
|
|
|
* @tc: a #GstVideoTimeCodeInterval
|
|
|
|
*
|
2018-12-14 18:58:40 +00:00
|
|
|
* Frees @tc.
|
2016-12-29 12:42:52 +00:00
|
|
|
*
|
|
|
|
* Since: 1.12
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_video_time_code_interval_free (GstVideoTimeCodeInterval * tc)
|
|
|
|
{
|
|
|
|
g_free (tc);
|
|
|
|
}
|