2 * Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * Parse POC and correct PTS if it's is unknown
22 * Reverse playback support
29 #include <gst/base/base.h>
30 #include "gstcodectimestamper.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_codec_timestamper_debug);
34 #define GST_CAT_DEFAULT gst_codec_timestamper_debug
42 } GstCodecTimestamperFrame;
44 struct _GstCodecTimestamperPrivate
48 GstSegment in_segment;
50 GList *current_frame_events;
52 GArray *timestamp_queue;
59 guint max_reorder_frames;
62 GstClockTime last_dts;
63 GstClockTime dts_offset;
64 GstClockTime time_adjustment;
69 static void gst_codec_timestamper_class_init (GstCodecTimestamperClass * klass);
70 static void gst_codec_timestamper_init (GstCodecTimestamper * self,
71 GstCodecTimestamperClass * klass);
72 static void gst_codec_timestamper_finalize (GObject * object);
74 static GstFlowReturn gst_codec_timestamper_chain (GstPad * pad,
75 GstObject * parent, GstBuffer * buffer);
76 static gboolean gst_codec_timestamper_sink_event (GstPad * pad,
77 GstObject * parent, GstEvent * event);
78 static gboolean gst_codec_timestamper_src_query (GstPad * pad,
79 GstObject * parent, GstQuery * query);
80 static GstStateChangeReturn
81 gst_codec_timestamper_change_state (GstElement * element,
82 GstStateChange transition);
84 gst_codec_timestamper_clear_frame (GstCodecTimestamperFrame * frame);
85 static void gst_codec_timestamper_reset (GstCodecTimestamper * self);
86 static void gst_codec_timestamper_drain (GstCodecTimestamper * self);
88 static GTypeClass *parent_class = NULL;
89 static gint private_offset = 0;
91 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
92 * method to get to the padtemplates */
94 gst_codec_timestamper_get_type (void)
96 static gsize type = 0;
98 if (g_once_init_enter (&type)) {
100 static const GTypeInfo info = {
101 sizeof (GstCodecTimestamperClass),
104 (GClassInitFunc) gst_codec_timestamper_class_init,
107 sizeof (GstCodecTimestamper),
109 (GInstanceInitFunc) gst_codec_timestamper_init,
112 _type = g_type_register_static (GST_TYPE_ELEMENT,
113 "GstCodecTimestamper", &info, G_TYPE_FLAG_ABSTRACT);
115 private_offset = g_type_add_instance_private (_type,
116 sizeof (GstCodecTimestamperPrivate));
118 g_once_init_leave (&type, _type);
123 static inline GstCodecTimestamperPrivate *
124 gst_codec_timestamper_get_instance_private (GstCodecTimestamper * self)
126 return (G_STRUCT_MEMBER_P (self, private_offset));
130 gst_codec_timestamper_class_init (GstCodecTimestamperClass * klass)
132 GObjectClass *object_class = G_OBJECT_CLASS (klass);
133 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
135 parent_class = g_type_class_peek_parent (klass);
137 g_type_class_adjust_private_offset (klass, &private_offset);
139 object_class->finalize = gst_codec_timestamper_finalize;
141 element_class->change_state =
142 GST_DEBUG_FUNCPTR (gst_codec_timestamper_change_state);
144 GST_DEBUG_CATEGORY_INIT (gst_codec_timestamper_debug, "codectimestamper", 0,
148 * GstCodecTimestamper:
152 gst_type_mark_as_plugin_api (GST_TYPE_CODEC_TIMESTAMPER, 0);
156 gst_codec_timestamper_init (GstCodecTimestamper * self,
157 GstCodecTimestamperClass * klass)
159 GstCodecTimestamperPrivate *priv;
160 GstPadTemplate *template;
162 self->priv = priv = gst_codec_timestamper_get_instance_private (self);
164 template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
166 self->sinkpad = gst_pad_new_from_template (template, "sink");
167 gst_pad_set_chain_function (self->sinkpad,
168 GST_DEBUG_FUNCPTR (gst_codec_timestamper_chain));
169 gst_pad_set_event_function (self->sinkpad,
170 GST_DEBUG_FUNCPTR (gst_codec_timestamper_sink_event));
171 GST_PAD_SET_PROXY_SCHEDULING (self->sinkpad);
172 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
174 template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
176 self->srcpad = gst_pad_new_from_template (template, "src");
177 gst_pad_set_query_function (self->srcpad,
178 GST_DEBUG_FUNCPTR (gst_codec_timestamper_src_query));
179 GST_PAD_SET_PROXY_SCHEDULING (self->srcpad);
181 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
184 gst_queue_array_new_for_struct (sizeof (GstCodecTimestamperFrame), 16);
185 gst_queue_array_set_clear_func (priv->queue,
186 (GDestroyNotify) gst_codec_timestamper_clear_frame);
187 priv->timestamp_queue =
188 g_array_sized_new (FALSE, FALSE, sizeof (GstClockTime), 16);
190 g_rec_mutex_init (&priv->lock);
194 gst_codec_timestamper_finalize (GObject * object)
196 GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (object);
197 GstCodecTimestamperPrivate *priv = self->priv;
199 gst_queue_array_free (priv->queue);
200 g_array_unref (priv->timestamp_queue);
201 g_rec_mutex_clear (&priv->lock);
203 G_OBJECT_CLASS (parent_class)->finalize (object);
207 gst_codec_timestamper_set_caps (GstCodecTimestamper * self, GstCaps * caps)
209 GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
210 GstCodecTimestamperPrivate *priv = self->priv;
211 GstStructure *s = gst_caps_get_structure (caps, 0);
216 gst_structure_get_fraction (s, "framerate", &priv->fps_n, &priv->fps_d);
218 if (priv->fps_n <= 0 || priv->fps_d <= 0) {
219 GST_WARNING_OBJECT (self, "Unknown frame rate, assume 25/1");
224 if (!klass->set_caps (self, caps))
231 gst_codec_timestamper_push_event (GstCodecTimestamper * self, GstEvent * event)
233 GstCodecTimestamperPrivate *priv = self->priv;
235 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
239 gst_event_copy_segment (event, &segment);
241 if (segment.format != GST_FORMAT_TIME) {
242 GST_ELEMENT_ERROR (self, CORE, EVENT, (NULL),
243 ("Non-time format segment"));
244 gst_event_unref (event);
248 if (priv->time_adjustment != GST_CLOCK_TIME_NONE) {
249 segment.start += priv->time_adjustment;
250 if (GST_CLOCK_TIME_IS_VALID (segment.position))
251 segment.position += priv->time_adjustment;
252 if (GST_CLOCK_TIME_IS_VALID (segment.stop))
253 segment.stop += priv->time_adjustment;
256 seqnum = gst_event_get_seqnum (event);
258 gst_event_unref (event);
259 event = gst_event_new_segment (&segment);
260 gst_event_set_seqnum (event, seqnum);
263 return gst_pad_push_event (self->srcpad, event);
267 gst_codec_timestamper_flush_events (GstCodecTimestamper * self, GList ** events)
271 for (iter = *events; iter; iter = g_list_next (iter)) {
272 GstEvent *ev = GST_EVENT (iter->data);
274 if (GST_EVENT_IS_STICKY (ev) && GST_EVENT_TYPE (ev) != GST_EVENT_EOS &&
275 GST_EVENT_TYPE (ev) != GST_EVENT_SEGMENT) {
276 gst_pad_store_sticky_event (self->srcpad, ev);
279 gst_event_unref (ev);
282 g_clear_pointer (events, g_list_free);
286 gst_codec_timestamper_flush (GstCodecTimestamper * self)
288 GstCodecTimestamperPrivate *priv = self->priv;
290 while (gst_queue_array_get_length (priv->queue) > 0) {
291 GstCodecTimestamperFrame *frame = (GstCodecTimestamperFrame *)
292 gst_queue_array_pop_head_struct (priv->queue);
294 gst_codec_timestamper_flush_events (self, &frame->events);
295 gst_codec_timestamper_clear_frame (frame);
298 gst_codec_timestamper_flush_events (self, &priv->current_frame_events);
300 priv->time_adjustment = GST_CLOCK_TIME_NONE;
301 priv->last_dts = GST_CLOCK_TIME_NONE;
302 g_rec_mutex_lock (&priv->lock);
303 priv->latency = GST_CLOCK_TIME_NONE;
304 g_rec_mutex_unlock (&priv->lock);
308 gst_codec_timestamper_sink_event (GstPad * pad, GstObject * parent,
311 GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
312 GstCodecTimestamperPrivate *priv = self->priv;
314 switch (GST_EVENT_TYPE (event)) {
315 case GST_EVENT_CAPS:{
318 gst_event_parse_caps (event, &caps);
319 gst_codec_timestamper_set_caps (self, caps);
322 case GST_EVENT_SEGMENT:{
325 gst_event_copy_segment (event, &segment);
326 if (segment.format != GST_FORMAT_TIME) {
327 GST_WARNING_OBJECT (self, "Not a time format segment");
328 gst_event_unref (event);
332 if (segment.rate < 0) {
333 GST_WARNING_OBJECT (self, "Negative rate is not supported");
334 gst_event_unref (event);
338 /* Drain on segment update */
339 if (memcmp (&self->in_segment, &segment, sizeof (GstSegment)))
340 gst_codec_timestamper_drain (self);
342 self->in_segment = segment;
346 gst_codec_timestamper_drain (self);
347 if (priv->current_frame_events) {
350 for (iter = priv->current_frame_events; iter; iter = g_list_next (iter))
351 gst_codec_timestamper_push_event (self, GST_EVENT (iter->data));
353 g_clear_pointer (&priv->current_frame_events, g_list_free);
356 case GST_EVENT_FLUSH_STOP:
357 gst_codec_timestamper_flush (self);
363 if (!GST_EVENT_IS_SERIALIZED (event) ||
364 GST_EVENT_TYPE (event) == GST_EVENT_EOS ||
365 GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
366 return gst_pad_event_default (pad, parent, event);
369 /* Store event to serialize queued frames */
370 priv->current_frame_events = g_list_append (priv->current_frame_events,
377 gst_codec_timestamper_frame_init (GstCodecTimestamperFrame * frame)
379 memset (frame, 0, sizeof (GstCodecTimestamperFrame));
381 frame->pts = GST_CLOCK_TIME_NONE;
385 gst_codec_timestamper_clear_frame (GstCodecTimestamperFrame * frame)
390 gst_clear_buffer (&frame->buffer);
392 g_list_free_full (frame->events, (GDestroyNotify) gst_event_unref);
393 frame->events = NULL;
398 gst_codec_timestamper_output_frame (GstCodecTimestamper * self,
399 GstCodecTimestamperFrame * frame)
401 GstCodecTimestamperPrivate *priv = self->priv;
404 GstClockTime dts = GST_CLOCK_TIME_NONE;
406 for (iter = frame->events; iter; iter = g_list_next (iter)) {
407 GstEvent *event = GST_EVENT (iter->data);
409 gst_codec_timestamper_push_event (self, event);
412 g_clear_pointer (&frame->events, g_list_free);
414 if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
415 g_assert (priv->timestamp_queue->len > 0);
416 dts = g_array_index (priv->timestamp_queue, GstClockTime, 0);
417 g_array_remove_index (priv->timestamp_queue, 0);
419 if (GST_CLOCK_TIME_IS_VALID (priv->dts_offset))
420 dts -= priv->dts_offset;
423 if (GST_CLOCK_TIME_IS_VALID (dts)) {
424 if (!GST_CLOCK_TIME_IS_VALID (priv->last_dts))
425 priv->last_dts = dts;
427 /* make sure DTS <= PTS */
428 if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
429 if (dts > frame->pts) {
430 if (frame->pts >= priv->last_dts)
433 dts = GST_CLOCK_TIME_NONE;
436 if (GST_CLOCK_TIME_IS_VALID (dts))
437 priv->last_dts = dts;
441 frame->buffer = gst_buffer_make_writable (frame->buffer);
442 GST_BUFFER_PTS (frame->buffer) = frame->pts;
443 GST_BUFFER_DTS (frame->buffer) = dts;
445 GST_TRACE_OBJECT (self, "Output %" GST_PTR_FORMAT, frame->buffer);
447 ret = gst_pad_push (self->srcpad, g_steal_pointer (&frame->buffer));
453 gst_codec_timestamper_process_output_frame (GstCodecTimestamper * self)
455 GstCodecTimestamperPrivate *priv = self->priv;
457 GstCodecTimestamperFrame *frame;
459 len = gst_queue_array_get_length (priv->queue);
460 if (len < priv->window_size) {
461 GST_TRACE_OBJECT (self, "Need more data, queued %d/%d", len,
466 frame = (GstCodecTimestamperFrame *)
467 gst_queue_array_pop_head_struct (priv->queue);
469 return gst_codec_timestamper_output_frame (self, frame);
473 gst_codec_timestamper_drain (GstCodecTimestamper * self)
475 GstCodecTimestamperPrivate *priv = self->priv;
477 while (gst_queue_array_get_length (priv->queue) > 0) {
478 GstCodecTimestamperFrame *frame = (GstCodecTimestamperFrame *)
479 gst_queue_array_pop_head_struct (priv->queue);
480 gst_codec_timestamper_output_frame (self, frame);
483 priv->time_adjustment = GST_CLOCK_TIME_NONE;
484 priv->last_dts = GST_CLOCK_TIME_NONE;
488 pts_compare_func (const GstClockTime * a, const GstClockTime * b)
494 gst_codec_timestamper_chain (GstPad * pad, GstObject * parent,
497 GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
498 GstCodecTimestamperPrivate *priv = self->priv;
499 GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
500 GstClockTime pts, dts;
501 /* The same hack as x264 for negative DTS */
502 static const GstClockTime min_pts = GST_SECOND * 60 * 60 * 1000;
503 GstCodecTimestamperFrame frame;
506 gst_codec_timestamper_frame_init (&frame);
508 GST_TRACE_OBJECT (self, "Handle %" GST_PTR_FORMAT, buffer);
510 pts = GST_BUFFER_PTS (buffer);
511 dts = GST_BUFFER_DTS (buffer);
513 if (!GST_CLOCK_TIME_IS_VALID (priv->time_adjustment)) {
514 GstClockTime start_time = GST_CLOCK_TIME_NONE;
516 if (GST_CLOCK_TIME_IS_VALID (pts))
517 start_time = MAX (pts, self->in_segment.start);
518 else if (GST_CLOCK_TIME_IS_VALID (dts))
519 start_time = MAX (dts, self->in_segment.start);
521 start_time = priv->in_segment.start;
523 if (start_time < min_pts)
524 priv->time_adjustment = min_pts - start_time;
527 if (GST_CLOCK_TIME_IS_VALID (priv->time_adjustment)) {
528 if (GST_CLOCK_TIME_IS_VALID (pts))
529 pts += priv->time_adjustment;
530 if (GST_CLOCK_TIME_IS_VALID (dts))
531 dts += priv->time_adjustment;
534 ret = klass->handle_buffer (self, buffer);
535 if (ret != GST_FLOW_OK) {
536 GST_INFO_OBJECT (self, "Handle buffer returned %s",
537 gst_flow_get_name (ret));
539 gst_buffer_unref (buffer);
544 frame.buffer = buffer;
545 frame.events = priv->current_frame_events;
546 priv->current_frame_events = NULL;
548 gst_queue_array_push_tail_struct (priv->queue, &frame);
549 if (GST_CLOCK_TIME_IS_VALID (frame.pts)) {
550 g_array_append_val (priv->timestamp_queue, frame.pts);
551 g_array_sort (priv->timestamp_queue, (GCompareFunc) pts_compare_func);
554 return gst_codec_timestamper_process_output_frame (self);
558 gst_codec_timestamper_src_query (GstPad * pad, GstObject * parent,
561 GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
562 GstCodecTimestamperPrivate *priv = self->priv;
564 switch (GST_QUERY_TYPE (query)) {
565 case GST_QUERY_LATENCY:{
568 ret = gst_pad_peer_query (self->sinkpad, query);
570 GstClockTime min, max;
573 gst_query_parse_latency (query, &live, &min, &max);
575 g_rec_mutex_lock (&priv->lock);
576 if (GST_CLOCK_TIME_IS_VALID (priv->latency))
577 min += priv->latency;
578 g_rec_mutex_unlock (&priv->lock);
580 gst_query_set_latency (query, live, min, max);
589 return gst_pad_query_default (pad, parent, query);
593 gst_codec_timestamper_reset (GstCodecTimestamper * self)
595 GstCodecTimestamperPrivate *priv = self->priv;
597 gst_queue_array_clear (priv->queue);
598 g_array_set_size (priv->timestamp_queue, 0);
601 priv->dts_offset = 0;
602 priv->time_adjustment = GST_CLOCK_TIME_NONE;
603 priv->latency = GST_CLOCK_TIME_NONE;
604 priv->window_size = 0;
605 priv->last_dts = GST_CLOCK_TIME_NONE;
607 if (priv->current_frame_events) {
608 g_list_free_full (priv->current_frame_events,
609 (GDestroyNotify) gst_event_unref);
610 priv->current_frame_events = NULL;
615 gst_codec_timestamper_start (GstCodecTimestamper * self)
617 GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
619 gst_codec_timestamper_reset (self);
622 return klass->start (self);
628 gst_codec_timestamper_stop (GstCodecTimestamper * self)
630 GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
632 gst_codec_timestamper_reset (self);
635 return klass->stop (self);
640 static GstStateChangeReturn
641 gst_codec_timestamper_change_state (GstElement * element,
642 GstStateChange transition)
644 GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (element);
645 GstStateChangeReturn ret;
647 switch (transition) {
648 case GST_STATE_CHANGE_READY_TO_PAUSED:
649 gst_codec_timestamper_start (self);
655 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
657 switch (transition) {
658 case GST_STATE_CHANGE_PAUSED_TO_READY:
659 gst_codec_timestamper_stop (self);
669 gst_codec_timestamper_set_window_size (GstCodecTimestamper * timestamper,
672 GstCodecTimestamperPrivate *priv = timestamper->priv;
673 gboolean updated = FALSE;
674 GstClockTime latency = 0;
676 g_rec_mutex_lock (&priv->lock);
677 priv->dts_offset = 0;
678 priv->window_size = 0;
681 priv->dts_offset = gst_util_uint64_scale_int (window_size * GST_SECOND,
682 priv->fps_d, priv->fps_n);
684 /* Add margin to be robust against PTS errors and in order for boundary
685 * frames' PTS can be referenced */
687 latency = gst_util_uint64_scale_int (window_size * GST_SECOND,
688 priv->fps_d, priv->fps_n);
690 priv->window_size = window_size;
693 if (priv->latency != latency) {
695 priv->latency = latency;
698 GST_DEBUG_OBJECT (timestamper,
699 "New window size %d, latency %" GST_TIME_FORMAT ", framerate %d/%d",
700 priv->window_size, GST_TIME_ARGS (latency), priv->fps_n, priv->fps_d);
701 g_rec_mutex_unlock (&priv->lock);
704 gst_codec_timestamper_drain (timestamper);
705 gst_element_post_message (GST_ELEMENT_CAST (timestamper),
706 gst_message_new_latency (GST_OBJECT_CAST (timestamper)));