2 * Copyright (C) 2008 David Schleef <ds@schleef.org>
3 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
4 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * Copyright (C) 2012 Collabora Ltd.
7 * Author : Edward Hervey <edward@collabora.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:gstvideodecoder
27 * @short_description: Base class for video decoders
30 * This base class is for video decoders turning encoded data into raw video
33 * The GstVideoDecoder base class and derived subclasses should cooperate as follows:
36 * <itemizedlist><title>Configuration</title>
38 * Initially, GstVideoDecoder calls @start when the decoder element
39 * is activated, which allows the subclass to perform any global setup.
42 * GstVideoDecoder calls @set_format to inform the subclass of caps
43 * describing input video data that it is about to receive, including
44 * possibly configuration data.
45 * While unlikely, it might be called more than once, if changing input
46 * parameters require reconfiguration.
49 * Incoming data buffers are processed as needed, described in Data Processing below.
52 * GstVideoDecoder calls @stop at end of all processing.
58 * <title>Data processing</title>
60 * The base class gathers input data, and optionally allows subclass
61 * to parse this into subsequently manageable chunks, typically
62 * corresponding to and referred to as 'frames'.
65 * Each input frame is provided in turn to the subclass' @handle_frame callback.
66 * The ownership of the frame is given to the @handle_frame callback.
69 * If codec processing results in decoded data, the subclass should call
70 * @gst_video_decoder_finish_frame to have decoded data pushed.
71 * downstream. Otherwise, the subclass must call @gst_video_decoder_drop_frame, to
72 * allow the base class to do timestamp and offset tracking, and possibly to
73 * requeue the frame for a later attempt in the case of reverse playback.
78 * <itemizedlist><title>Shutdown phase</title>
80 * The GstVideoDecoder class calls @stop to inform the subclass that data
81 * parsing will be stopped.
86 * <itemizedlist><title>Additional Notes</title>
88 * <itemizedlist><title>Seeking/Flushing</title>
90 * When the pipeline is seeked or otherwise flushed, the subclass is informed via a call
91 * to its @reset callback, with the hard parameter set to true. This indicates the
92 * subclass should drop any internal data queues and timestamps and prepare for a fresh
93 * set of buffers to arrive for parsing and decoding.
98 * <itemizedlist><title>End Of Stream</title>
100 * At end-of-stream, the subclass @parse function may be called some final times with the
101 * at_eos parameter set to true, indicating that the element should not expect any more data
102 * to be arriving, and it should parse and remaining frames and call
103 * gst_video_decoder_have_frame() if possible.
111 * The subclass is responsible for providing pad template caps for
112 * source and sink pads. The pads need to be named "sink" and "src". It also
113 * needs to provide information about the ouptput caps, when they are known.
114 * This may be when the base class calls the subclass' @set_format function,
115 * though it might be during decoding, before calling
116 * @gst_video_decoder_finish_frame. This is done via
117 * @gst_video_decoder_set_output_state
119 * The subclass is also responsible for providing (presentation) timestamps
120 * (likely based on corresponding input ones). If that is not applicable
121 * or possible, the base class provides limited framerate based interpolation.
123 * Similarly, the base class provides some limited (legacy) seeking support
124 * if specifically requested by the subclass, as full-fledged support
125 * should rather be left to upstream demuxer, parser or alike. This simple
126 * approach caters for seeking and duration reporting using estimated input
127 * bitrates. To enable it, a subclass should call
128 * @gst_video_decoder_set_estimate_rate to enable handling of incoming byte-streams.
130 * The base class provides some support for reverse playback, in particular
131 * in case incoming data is not packetized or upstream does not provide
132 * fragments on keyframe boundaries. However, the subclass should then be prepared
133 * for the parsing and frame processing stage to occur separately (in normal
134 * forward processing, the latter immediately follows the former),
135 * The subclass also needs to ensure the parsing stage properly marks keyframes,
136 * unless it knows the upstream elements will do so properly for incoming data.
138 * The bare minimum that a functional subclass needs to implement is:
140 * <listitem><para>Provide pad templates</para></listitem>
142 * Inform the base class of output caps via @gst_video_decoder_set_output_state
145 * Parse input data, if it is not considered packetized from upstream
146 * Data will be provided to @parse which should invoke @gst_video_decoder_add_to_frame and
147 * @gst_video_decoder_have_frame to separate the data belonging to each video frame.
150 * Accept data in @handle_frame and provide decoded results to
151 * @gst_video_decoder_finish_frame, or call @gst_video_decoder_drop_frame.
162 * * Add a flag/boolean for I-frame-only/image decoders so we can do extra
163 * features, like applying QoS on input (as opposed to after the frame is
165 * * Add a flag/boolean for decoders that require keyframes, so the base
166 * class can automatically discard non-keyframes before one has arrived
167 * * Detect reordered frame/timestamps and fix the pts/dts
168 * * Support for GstIndex (or shall we not care ?)
169 * * Calculate actual latency based on input/output timestamp/frame_number
170 * and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
171 * * Emit latency message when it changes
175 /* Implementation notes:
176 * The Video Decoder base class operates in 2 primary processing modes, depending
177 * on whether forward or reverse playback is requested.
180 * * Incoming buffer -> @parse() -> add_to_frame()/have_frame() -> handle_frame() ->
183 * Reverse playback is more complicated, since it involves gathering incoming data regions
184 * as we loop backwards through the upstream data. The processing concept (using incoming
185 * buffers as containing one frame each to simplify things) is:
187 * Upstream data we want to play:
188 * Buffer encoded order: 1 2 3 4 5 6 7 8 9 EOS
190 * Groupings: AAAAAAA BBBBBBB CCCCCCC
193 * Buffer reception order: 7 8 9 4 5 6 1 2 3 EOS
195 * Discont flag: D D D
197 * - Each Discont marks a discont in the decoding order.
198 * - The keyframes mark where we can start decoding.
200 * Initially, we prepend incoming buffers to the gather queue. Whenever the
201 * discont flag is set on an incoming buffer, the gather queue is flushed out
202 * before the new buffer is collected.
204 * The above data will be accumulated in the gather queue like this:
206 * gather queue: 9 8 7
209 * Whe buffer 4 is received (with a DISCONT), we flush the gather queue like
213 * take head of queue and prepend to parse queue (this reverses the sequence,
214 * so parse queue is 7 -> 8 -> 9)
216 * Next, we process the parse queue, which now contains all un-parsed packets (including
217 * any leftover ones from the previous decode section)
219 * for each buffer now in the parse queue:
220 * Call the subclass parse function, prepending each resulting frame to
221 * the parse_gather queue. Buffers which precede the first one that
222 * produces a parsed frame are retained in the parse queue for re-processing on
223 * the next cycle of parsing.
225 * The parse_gather queue now contains frame objects ready for decoding, in reverse order.
226 * parse_gather: 9 -> 8 -> 7
228 * while (parse_gather)
229 * Take the head of the queue and prepend it to the decode queue
230 * If the frame was a keyframe, process the decode queue
231 * decode is now 7-8-9
233 * Processing the decode queue results in frames with attached output buffers
234 * stored in the 'output_queue' ready for outputting in reverse order.
236 * After we flushed the gather queue and parsed it, we add 4 to the (now empty) gather queue.
237 * We get the following situation:
240 * decode queue: 7 8 9
242 * After we received 5 (Keyframe) and 6:
244 * gather queue: 6 5 4
245 * decode queue: 7 8 9
247 * When we receive 1 (DISCONT) which triggers a flush of the gather queue:
249 * Copy head of the gather queue (6) to decode queue:
252 * decode queue: 6 7 8 9
254 * Copy head of the gather queue (5) to decode queue. This is a keyframe so we
255 * can start decoding.
258 * decode queue: 5 6 7 8 9
260 * Decode frames in decode queue, store raw decoded data in output queue, we
261 * can take the head of the decode queue and prepend the decoded result in the
266 * output queue: 9 8 7 6 5
268 * Now output all the frames in the output queue, picking a frame from the
271 * Copy head of the gather queue (4) to decode queue, we flushed the gather
272 * queue and can now store input buffer in the gather queue:
277 * When we receive EOS, the queue looks like:
279 * gather queue: 3 2 1
282 * Fill decode queue, first keyframe we copy is 2:
285 * decode queue: 2 3 4
291 * output queue: 4 3 2
293 * Leftover buffer 1 cannot be decoded and must be discarded.
296 #include "gstvideodecoder.h"
297 #include "gstvideoutils.h"
299 #include <gst/video/video.h>
300 #include <gst/video/video-event.h>
301 #include <gst/video/gstvideopool.h>
302 #include <gst/video/gstvideometa.h>
305 GST_DEBUG_CATEGORY (videodecoder_debug);
306 #define GST_CAT_DEFAULT videodecoder_debug
308 #define GST_VIDEO_DECODER_GET_PRIVATE(obj) \
309 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_DECODER, \
310 GstVideoDecoderPrivate))
312 struct _GstVideoDecoderPrivate
314 /* FIXME introduce a context ? */
317 GstAllocator *allocator;
318 GstAllocationParams params;
322 GstAdapter *input_adapter;
323 /* assembles current frame */
324 GstAdapter *output_adapter;
326 /* Whether we attempt to convert newsegment from bytes to
327 * time using a bitrate estimation */
328 gboolean do_estimate_rate;
330 /* Whether input is considered packetized or not */
339 /* ... being tracked here;
340 * only available during parsing */
341 GstVideoCodecFrame *current_frame;
342 /* events that should apply to the current frame */
343 GList *current_frame_events;
345 /* relative offset of input data */
346 guint64 input_offset;
347 /* relative offset of frame */
348 guint64 frame_offset;
349 /* tracking ts and offsets */
352 /* last outgoing ts */
353 GstClockTime last_timestamp_out;
354 /* incoming pts - dts */
355 GstClockTime pts_delta;
356 gboolean reordered_output;
358 /* reverse playback */
363 /* collected parsed frames */
365 /* frames to be handled == decoded */
367 /* collected output - of buffer objects, not frames */
368 GList *output_queued;
371 /* base_picture_number is the picture number of the reference picture */
372 guint64 base_picture_number;
373 /* combine with base_picture_number, framerate and calcs to yield (presentation) ts */
374 GstClockTime base_timestamp;
376 /* FIXME : reorder_depth is never set */
378 int distance_from_sync;
380 guint32 system_frame_number;
381 guint32 decode_frame_number;
383 GList *frames; /* Protected with OBJECT_LOCK */
384 GstVideoCodecState *input_state;
385 GstVideoCodecState *output_state; /* OBJECT_LOCK and STREAM_LOCK */
386 gboolean output_state_changed;
389 gdouble proportion; /* OBJECT_LOCK */
390 GstClockTime earliest_time; /* OBJECT_LOCK */
391 GstClockTime qos_frame_duration; /* OBJECT_LOCK */
393 /* qos messages: frames dropped/processed */
397 /* Outgoing byte size ? */
405 gboolean tags_changed;
408 static GstElementClass *parent_class = NULL;
409 static void gst_video_decoder_class_init (GstVideoDecoderClass * klass);
410 static void gst_video_decoder_init (GstVideoDecoder * dec,
411 GstVideoDecoderClass * klass);
413 static void gst_video_decoder_finalize (GObject * object);
415 static gboolean gst_video_decoder_setcaps (GstVideoDecoder * dec,
417 static gboolean gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
419 static gboolean gst_video_decoder_src_event (GstPad * pad, GstObject * parent,
421 static GstFlowReturn gst_video_decoder_chain (GstPad * pad, GstObject * parent,
423 static gboolean gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
425 static GstStateChangeReturn gst_video_decoder_change_state (GstElement *
426 element, GstStateChange transition);
427 static gboolean gst_video_decoder_src_query (GstPad * pad, GstObject * parent,
429 static void gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full);
431 static GstFlowReturn gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
432 GstVideoCodecFrame * frame);
434 static void gst_video_decoder_release_frame (GstVideoDecoder * dec,
435 GstVideoCodecFrame * frame);
436 static GstClockTime gst_video_decoder_get_frame_duration (GstVideoDecoder *
437 decoder, GstVideoCodecFrame * frame);
438 static GstVideoCodecFrame *gst_video_decoder_new_frame (GstVideoDecoder *
440 static GstFlowReturn gst_video_decoder_clip_and_push_buf (GstVideoDecoder *
441 decoder, GstBuffer * buf);
442 static GstFlowReturn gst_video_decoder_flush_parse (GstVideoDecoder * dec,
445 static void gst_video_decoder_clear_queues (GstVideoDecoder * dec);
447 static gboolean gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
449 static gboolean gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
451 static gboolean gst_video_decoder_decide_allocation_default (GstVideoDecoder *
452 decoder, GstQuery * query);
453 static gboolean gst_video_decoder_propose_allocation_default (GstVideoDecoder *
454 decoder, GstQuery * query);
455 static gboolean gst_video_decoder_negotiate_default (GstVideoDecoder * decoder);
457 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
458 * method to get to the padtemplates */
460 gst_video_decoder_get_type (void)
462 static volatile gsize type = 0;
464 if (g_once_init_enter (&type)) {
466 static const GTypeInfo info = {
467 sizeof (GstVideoDecoderClass),
470 (GClassInitFunc) gst_video_decoder_class_init,
473 sizeof (GstVideoDecoder),
475 (GInstanceInitFunc) gst_video_decoder_init,
478 _type = g_type_register_static (GST_TYPE_ELEMENT,
479 "GstVideoDecoder", &info, G_TYPE_FLAG_ABSTRACT);
480 g_once_init_leave (&type, _type);
486 gst_video_decoder_class_init (GstVideoDecoderClass * klass)
488 GObjectClass *gobject_class;
489 GstElementClass *gstelement_class;
491 gobject_class = G_OBJECT_CLASS (klass);
492 gstelement_class = GST_ELEMENT_CLASS (klass);
494 GST_DEBUG_CATEGORY_INIT (videodecoder_debug, "videodecoder", 0,
495 "Base Video Decoder");
497 parent_class = g_type_class_peek_parent (klass);
498 g_type_class_add_private (klass, sizeof (GstVideoDecoderPrivate));
500 gobject_class->finalize = gst_video_decoder_finalize;
502 gstelement_class->change_state =
503 GST_DEBUG_FUNCPTR (gst_video_decoder_change_state);
505 klass->sink_event = gst_video_decoder_sink_event_default;
506 klass->src_event = gst_video_decoder_src_event_default;
507 klass->decide_allocation = gst_video_decoder_decide_allocation_default;
508 klass->propose_allocation = gst_video_decoder_propose_allocation_default;
509 klass->negotiate = gst_video_decoder_negotiate_default;
513 gst_video_decoder_init (GstVideoDecoder * decoder, GstVideoDecoderClass * klass)
515 GstPadTemplate *pad_template;
518 GST_DEBUG_OBJECT (decoder, "gst_video_decoder_init");
520 decoder->priv = GST_VIDEO_DECODER_GET_PRIVATE (decoder);
523 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
524 g_return_if_fail (pad_template != NULL);
526 decoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
528 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_decoder_chain));
529 gst_pad_set_event_function (pad,
530 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_event));
531 gst_pad_set_query_function (pad,
532 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_query));
533 gst_element_add_pad (GST_ELEMENT (decoder), decoder->sinkpad);
536 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
537 g_return_if_fail (pad_template != NULL);
539 decoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
541 gst_pad_set_event_function (pad,
542 GST_DEBUG_FUNCPTR (gst_video_decoder_src_event));
543 gst_pad_set_query_function (pad,
544 GST_DEBUG_FUNCPTR (gst_video_decoder_src_query));
545 gst_pad_use_fixed_caps (pad);
546 gst_element_add_pad (GST_ELEMENT (decoder), decoder->srcpad);
548 gst_segment_init (&decoder->input_segment, GST_FORMAT_TIME);
549 gst_segment_init (&decoder->output_segment, GST_FORMAT_TIME);
551 g_rec_mutex_init (&decoder->stream_lock);
553 decoder->priv->input_adapter = gst_adapter_new ();
554 decoder->priv->output_adapter = gst_adapter_new ();
555 decoder->priv->packetized = TRUE;
557 gst_video_decoder_reset (decoder, TRUE);
561 gst_video_rawvideo_convert (GstVideoCodecState * state,
562 GstFormat src_format, gint64 src_value,
563 GstFormat * dest_format, gint64 * dest_value)
565 gboolean res = FALSE;
569 g_return_val_if_fail (dest_format != NULL, FALSE);
570 g_return_val_if_fail (dest_value != NULL, FALSE);
572 if (src_format == *dest_format || src_value == 0 || src_value == -1) {
573 *dest_value = src_value;
577 vidsize = GST_VIDEO_INFO_SIZE (&state->info);
578 fps_n = GST_VIDEO_INFO_FPS_N (&state->info);
579 fps_d = GST_VIDEO_INFO_FPS_D (&state->info);
581 if (src_format == GST_FORMAT_BYTES &&
582 *dest_format == GST_FORMAT_DEFAULT && vidsize) {
583 /* convert bytes to frames */
584 *dest_value = gst_util_uint64_scale_int (src_value, 1, vidsize);
586 } else if (src_format == GST_FORMAT_DEFAULT &&
587 *dest_format == GST_FORMAT_BYTES && vidsize) {
588 /* convert bytes to frames */
589 *dest_value = src_value * vidsize;
591 } else if (src_format == GST_FORMAT_DEFAULT &&
592 *dest_format == GST_FORMAT_TIME && fps_n) {
593 /* convert frames to time */
594 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND * fps_d, fps_n);
596 } else if (src_format == GST_FORMAT_TIME &&
597 *dest_format == GST_FORMAT_DEFAULT && fps_d) {
598 /* convert time to frames */
599 *dest_value = gst_util_uint64_scale (src_value, fps_n, GST_SECOND * fps_d);
601 } else if (src_format == GST_FORMAT_TIME &&
602 *dest_format == GST_FORMAT_BYTES && fps_d && vidsize) {
603 /* convert time to frames */
604 *dest_value = gst_util_uint64_scale (src_value,
605 fps_n * vidsize, GST_SECOND * fps_d);
607 } else if (src_format == GST_FORMAT_BYTES &&
608 *dest_format == GST_FORMAT_TIME && fps_n && vidsize) {
609 /* convert frames to time */
610 *dest_value = gst_util_uint64_scale (src_value,
611 GST_SECOND * fps_d, fps_n * vidsize);
619 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
620 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
623 gboolean res = FALSE;
625 g_return_val_if_fail (dest_format != NULL, FALSE);
626 g_return_val_if_fail (dest_value != NULL, FALSE);
628 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
631 *dest_value = src_value;
635 if (bytes <= 0 || time <= 0) {
636 GST_DEBUG ("not enough metadata yet to convert");
640 switch (src_format) {
641 case GST_FORMAT_BYTES:
642 switch (*dest_format) {
643 case GST_FORMAT_TIME:
644 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
651 case GST_FORMAT_TIME:
652 switch (*dest_format) {
653 case GST_FORMAT_BYTES:
654 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
662 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
671 static GstVideoCodecState *
672 _new_input_state (GstCaps * caps)
674 GstVideoCodecState *state;
675 GstStructure *structure;
676 const GValue *codec_data;
678 state = g_slice_new0 (GstVideoCodecState);
679 state->ref_count = 1;
680 gst_video_info_init (&state->info);
681 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
683 state->caps = gst_caps_ref (caps);
685 structure = gst_caps_get_structure (caps, 0);
687 codec_data = gst_structure_get_value (structure, "codec_data");
688 if (codec_data && G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER)
689 state->codec_data = GST_BUFFER (g_value_dup_boxed (codec_data));
695 g_slice_free (GstVideoCodecState, state);
700 static GstVideoCodecState *
701 _new_output_state (GstVideoFormat fmt, guint width, guint height,
702 GstVideoCodecState * reference)
704 GstVideoCodecState *state;
706 state = g_slice_new0 (GstVideoCodecState);
707 state->ref_count = 1;
708 gst_video_info_init (&state->info);
709 gst_video_info_set_format (&state->info, fmt, width, height);
712 GstVideoInfo *tgt, *ref;
715 ref = &reference->info;
717 /* Copy over extra fields from reference state */
718 tgt->interlace_mode = ref->interlace_mode;
719 tgt->flags = ref->flags;
720 tgt->chroma_site = ref->chroma_site;
721 /* only copy values that are not unknown so that we don't override the
722 * defaults. subclasses should really fill these in when they know. */
723 if (ref->colorimetry.range)
724 tgt->colorimetry.range = ref->colorimetry.range;
725 if (ref->colorimetry.matrix)
726 tgt->colorimetry.matrix = ref->colorimetry.matrix;
727 if (ref->colorimetry.transfer)
728 tgt->colorimetry.transfer = ref->colorimetry.transfer;
729 if (ref->colorimetry.primaries)
730 tgt->colorimetry.primaries = ref->colorimetry.primaries;
731 GST_DEBUG ("reference par %d/%d fps %d/%d",
732 ref->par_n, ref->par_d, ref->fps_n, ref->fps_d);
733 tgt->par_n = ref->par_n;
734 tgt->par_d = ref->par_d;
735 tgt->fps_n = ref->fps_n;
736 tgt->fps_d = ref->fps_d;
739 GST_DEBUG ("reference par %d/%d fps %d/%d",
740 state->info.par_n, state->info.par_d,
741 state->info.fps_n, state->info.fps_d);
747 gst_video_decoder_setcaps (GstVideoDecoder * decoder, GstCaps * caps)
749 GstVideoDecoderClass *decoder_class;
750 GstVideoCodecState *state;
753 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
755 GST_DEBUG_OBJECT (decoder, "setcaps %" GST_PTR_FORMAT, caps);
757 state = _new_input_state (caps);
759 if (G_UNLIKELY (state == NULL))
762 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
764 if (decoder_class->set_format)
765 ret = decoder_class->set_format (decoder, state);
770 if (decoder->priv->input_state)
771 gst_video_codec_state_unref (decoder->priv->input_state);
772 decoder->priv->input_state = state;
774 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
782 GST_WARNING_OBJECT (decoder, "Failed to parse caps");
788 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
789 GST_WARNING_OBJECT (decoder, "Subclass refused caps");
790 gst_video_codec_state_unref (state);
796 gst_video_decoder_finalize (GObject * object)
798 GstVideoDecoder *decoder;
800 decoder = GST_VIDEO_DECODER (object);
802 GST_DEBUG_OBJECT (object, "finalize");
804 g_rec_mutex_clear (&decoder->stream_lock);
806 if (decoder->priv->input_adapter) {
807 g_object_unref (decoder->priv->input_adapter);
808 decoder->priv->input_adapter = NULL;
810 if (decoder->priv->output_adapter) {
811 g_object_unref (decoder->priv->output_adapter);
812 decoder->priv->output_adapter = NULL;
815 if (decoder->priv->input_state)
816 gst_video_codec_state_unref (decoder->priv->input_state);
817 if (decoder->priv->output_state)
818 gst_video_codec_state_unref (decoder->priv->output_state);
820 if (decoder->priv->pool) {
821 gst_object_unref (decoder->priv->pool);
822 decoder->priv->pool = NULL;
825 if (decoder->priv->allocator) {
826 gst_object_unref (decoder->priv->allocator);
827 decoder->priv->allocator = NULL;
830 G_OBJECT_CLASS (parent_class)->finalize (object);
833 /* hard == FLUSH, otherwise discont */
835 gst_video_decoder_flush (GstVideoDecoder * dec, gboolean hard)
837 GstVideoDecoderClass *klass;
838 GstVideoDecoderPrivate *priv = dec->priv;
839 GstFlowReturn ret = GST_FLOW_OK;
841 klass = GST_VIDEO_DECODER_GET_CLASS (dec);
843 GST_LOG_OBJECT (dec, "flush hard %d", hard);
845 /* Inform subclass */
847 klass->reset (dec, hard);
849 /* FIXME make some more distinction between hard and soft,
850 * but subclass may not be prepared for that */
851 /* FIXME perhaps also clear pending frames ?,
852 * but again, subclass may still come up with one of those */
854 /* TODO ? finish/drain some stuff */
856 gst_segment_init (&dec->input_segment, GST_FORMAT_UNDEFINED);
857 gst_segment_init (&dec->output_segment, GST_FORMAT_UNDEFINED);
858 gst_video_decoder_clear_queues (dec);
859 priv->error_count = 0;
860 g_list_free_full (priv->current_frame_events,
861 (GDestroyNotify) gst_event_unref);
862 priv->current_frame_events = NULL;
864 /* and get (re)set for the sequel */
865 gst_video_decoder_reset (dec, FALSE);
871 gst_video_decoder_push_event (GstVideoDecoder * decoder, GstEvent * event)
873 switch (GST_EVENT_TYPE (event)) {
874 case GST_EVENT_SEGMENT:
878 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
880 gst_event_copy_segment (event, &segment);
882 GST_DEBUG_OBJECT (decoder, "segment %" GST_SEGMENT_FORMAT, &segment);
884 if (segment.format != GST_FORMAT_TIME) {
885 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
886 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
890 decoder->output_segment = segment;
891 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
898 return gst_pad_push_event (decoder->srcpad, event);
902 gst_video_decoder_drain_out (GstVideoDecoder * dec, gboolean at_eos)
904 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
905 GstVideoDecoderPrivate *priv = dec->priv;
906 GstFlowReturn ret = GST_FLOW_OK;
908 GST_VIDEO_DECODER_STREAM_LOCK (dec);
910 if (dec->input_segment.rate > 0.0) {
911 /* Forward mode, if unpacketized, give the child class
912 * a final chance to flush out packets */
913 if (!priv->packetized) {
914 while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter)) {
915 if (priv->current_frame == NULL)
916 priv->current_frame = gst_video_decoder_new_frame (dec);
918 ret = decoder_class->parse (dec, priv->current_frame,
919 priv->input_adapter, TRUE);
923 /* Reverse playback mode */
924 ret = gst_video_decoder_flush_parse (dec, TRUE);
928 if (decoder_class->finish)
929 ret = decoder_class->finish (dec);
932 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
938 gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
941 GstVideoDecoderPrivate *priv;
942 gboolean ret = FALSE;
943 gboolean forward_immediate = FALSE;
945 priv = decoder->priv;
947 switch (GST_EVENT_TYPE (event)) {
952 gst_event_parse_caps (event, &caps);
954 decoder->priv->do_caps = TRUE;
955 gst_event_unref (event);
961 GstFlowReturn flow_ret = GST_FLOW_OK;
963 flow_ret = gst_video_decoder_drain_out (decoder, TRUE);
964 ret = (flow_ret == GST_FLOW_OK);
965 /* Forward EOS immediately. This is required because no
966 * buffer or serialized event will come after EOS and
967 * nothing could trigger another _finish_frame() call.
969 * The subclass can override this behaviour by overriding
970 * the ::sink_event() vfunc and not chaining up to the
971 * parent class' ::sink_event() until a later time.
973 forward_immediate = TRUE;
978 GstFlowReturn flow_ret = GST_FLOW_OK;
980 flow_ret = gst_video_decoder_drain_out (decoder, FALSE);
981 ret = (flow_ret == GST_FLOW_OK);
983 /* Forward GAP immediately. Everything is drained after
984 * the GAP event and we can forward this event immediately
985 * now without having buffers out of order.
987 forward_immediate = TRUE;
990 case GST_EVENT_CUSTOM_DOWNSTREAM:
993 GstFlowReturn flow_ret = GST_FLOW_OK;
995 if (gst_video_event_parse_still_frame (event, &in_still)) {
997 GST_DEBUG_OBJECT (decoder, "draining current data for still-frame");
998 flow_ret = gst_video_decoder_drain_out (decoder, FALSE);
999 ret = (flow_ret == GST_FLOW_OK);
1001 /* Forward STILL_FRAME immediately. Everything is drained after
1002 * the STILL_FRAME event and we can forward this event immediately
1003 * now without having buffers out of order.
1005 forward_immediate = TRUE;
1009 case GST_EVENT_SEGMENT:
1013 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1015 gst_event_copy_segment (event, &segment);
1017 if (segment.format == GST_FORMAT_TIME) {
1018 GST_DEBUG_OBJECT (decoder,
1019 "received TIME SEGMENT %" GST_SEGMENT_FORMAT, &segment);
1023 GST_DEBUG_OBJECT (decoder,
1024 "received SEGMENT %" GST_SEGMENT_FORMAT, &segment);
1026 /* handle newsegment as a result from our legacy simple seeking */
1027 /* note that initial 0 should convert to 0 in any case */
1028 if (priv->do_estimate_rate &&
1029 gst_pad_query_convert (decoder->sinkpad, GST_FORMAT_BYTES,
1030 segment.start, GST_FORMAT_TIME, &start)) {
1031 /* best attempt convert */
1032 /* as these are only estimates, stop is kept open-ended to avoid
1033 * premature cutting */
1034 GST_DEBUG_OBJECT (decoder,
1035 "converted to TIME start %" GST_TIME_FORMAT,
1036 GST_TIME_ARGS (start));
1037 segment.start = start;
1038 segment.stop = GST_CLOCK_TIME_NONE;
1039 segment.time = start;
1041 gst_event_unref (event);
1042 event = gst_event_new_segment (&segment);
1044 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1045 goto newseg_wrong_format;
1049 gst_video_decoder_flush (decoder, FALSE);
1051 priv->base_timestamp = GST_CLOCK_TIME_NONE;
1052 priv->base_picture_number = 0;
1054 decoder->input_segment = segment;
1056 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1059 case GST_EVENT_FLUSH_STOP:
1061 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1062 /* well, this is kind of worse than a DISCONT */
1063 gst_video_decoder_flush (decoder, TRUE);
1064 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1065 /* Forward FLUSH_STOP immediately. This is required because it is
1066 * expected to be forwarded immediately and no buffers are queued
1069 forward_immediate = TRUE;
1076 gst_event_parse_tag (event, &tags);
1078 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1079 gst_video_decoder_merge_tags (decoder, tags, GST_TAG_MERGE_REPLACE);
1080 gst_event_unref (event);
1090 /* Forward non-serialized events immediately, and all other
1091 * events which can be forwarded immediately without potentially
1092 * causing the event to go out of order with other events and
1093 * buffers as decided above.
1096 if (!GST_EVENT_IS_SERIALIZED (event) || forward_immediate) {
1097 ret = gst_video_decoder_push_event (decoder, event);
1099 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1100 decoder->priv->current_frame_events =
1101 g_list_prepend (decoder->priv->current_frame_events, event);
1102 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1109 newseg_wrong_format:
1111 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
1112 gst_event_unref (event);
1119 gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
1122 GstVideoDecoder *decoder;
1123 GstVideoDecoderClass *decoder_class;
1124 gboolean ret = FALSE;
1126 decoder = GST_VIDEO_DECODER (parent);
1127 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1129 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1130 GST_EVENT_TYPE_NAME (event));
1132 if (decoder_class->sink_event)
1133 ret = decoder_class->sink_event (decoder, event);
1138 /* perform upstream byte <-> time conversion (duration, seeking)
1139 * if subclass allows and if enough data for moderately decent conversion */
1140 static inline gboolean
1141 gst_video_decoder_do_byte (GstVideoDecoder * dec)
1143 return dec->priv->do_estimate_rate && (dec->priv->bytes_out > 0)
1144 && (dec->priv->time > GST_SECOND);
1148 gst_video_decoder_do_seek (GstVideoDecoder * dec, GstEvent * event)
1152 GstSeekType start_type, end_type;
1154 gint64 start, start_time, end_time;
1155 GstSegment seek_segment;
1158 gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
1159 &start_time, &end_type, &end_time);
1161 /* we'll handle plain open-ended flushing seeks with the simple approach */
1163 GST_DEBUG_OBJECT (dec, "unsupported seek: rate");
1167 if (start_type != GST_SEEK_TYPE_SET) {
1168 GST_DEBUG_OBJECT (dec, "unsupported seek: start time");
1172 if (end_type != GST_SEEK_TYPE_NONE ||
1173 (end_type == GST_SEEK_TYPE_SET && end_time != GST_CLOCK_TIME_NONE)) {
1174 GST_DEBUG_OBJECT (dec, "unsupported seek: end time");
1178 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1179 GST_DEBUG_OBJECT (dec, "unsupported seek: not flushing");
1183 memcpy (&seek_segment, &dec->output_segment, sizeof (seek_segment));
1184 gst_segment_do_seek (&seek_segment, rate, format, flags, start_type,
1185 start_time, end_type, end_time, NULL);
1186 start_time = seek_segment.position;
1188 if (!gst_pad_query_convert (dec->sinkpad, GST_FORMAT_TIME, start_time,
1189 GST_FORMAT_BYTES, &start)) {
1190 GST_DEBUG_OBJECT (dec, "conversion failed");
1194 seqnum = gst_event_get_seqnum (event);
1195 event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
1196 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
1197 gst_event_set_seqnum (event, seqnum);
1199 GST_DEBUG_OBJECT (dec, "seeking to %" GST_TIME_FORMAT " at byte offset %"
1200 G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
1202 return gst_pad_push_event (dec->sinkpad, event);
1206 gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
1209 GstVideoDecoderPrivate *priv;
1210 gboolean res = FALSE;
1212 priv = decoder->priv;
1214 GST_DEBUG_OBJECT (decoder,
1215 "received event %d, %s", GST_EVENT_TYPE (event),
1216 GST_EVENT_TYPE_NAME (event));
1218 switch (GST_EVENT_TYPE (event)) {
1219 case GST_EVENT_SEEK:
1224 GstSeekType start_type, stop_type;
1226 gint64 tstart, tstop;
1229 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1231 seqnum = gst_event_get_seqnum (event);
1233 /* upstream gets a chance first */
1234 if ((res = gst_pad_push_event (decoder->sinkpad, event)))
1237 /* if upstream fails for a time seek, maybe we can help if allowed */
1238 if (format == GST_FORMAT_TIME) {
1239 if (gst_video_decoder_do_byte (decoder))
1240 res = gst_video_decoder_do_seek (decoder, event);
1244 /* ... though a non-time seek can be aided as well */
1245 /* First bring the requested format to time */
1247 gst_pad_query_convert (decoder->srcpad, format, start,
1248 GST_FORMAT_TIME, &tstart)))
1251 gst_pad_query_convert (decoder->srcpad, format, stop,
1252 GST_FORMAT_TIME, &tstop)))
1255 /* then seek with time on the peer */
1256 event = gst_event_new_seek (rate, GST_FORMAT_TIME,
1257 flags, start_type, tstart, stop_type, tstop);
1258 gst_event_set_seqnum (event, seqnum);
1260 res = gst_pad_push_event (decoder->sinkpad, event);
1267 GstClockTimeDiff diff;
1268 GstClockTime timestamp;
1270 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1272 GST_OBJECT_LOCK (decoder);
1273 priv->proportion = proportion;
1274 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) {
1275 if (G_UNLIKELY (diff > 0)) {
1276 priv->earliest_time = timestamp + 2 * diff + priv->qos_frame_duration;
1278 priv->earliest_time = timestamp + diff;
1281 priv->earliest_time = GST_CLOCK_TIME_NONE;
1283 GST_OBJECT_UNLOCK (decoder);
1285 GST_DEBUG_OBJECT (decoder,
1286 "got QoS %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT ", %g",
1287 GST_TIME_ARGS (timestamp), diff, proportion);
1289 res = gst_pad_push_event (decoder->sinkpad, event);
1293 res = gst_pad_push_event (decoder->sinkpad, event);
1300 GST_DEBUG_OBJECT (decoder, "could not convert format");
1305 gst_video_decoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1307 GstVideoDecoder *decoder;
1308 GstVideoDecoderClass *decoder_class;
1309 gboolean ret = FALSE;
1311 decoder = GST_VIDEO_DECODER (parent);
1312 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1314 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1315 GST_EVENT_TYPE_NAME (event));
1317 if (decoder_class->src_event)
1318 ret = decoder_class->src_event (decoder, event);
1324 gst_video_decoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1326 GstVideoDecoder *dec;
1327 gboolean res = TRUE;
1329 dec = GST_VIDEO_DECODER (parent);
1331 GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
1333 switch (GST_QUERY_TYPE (query)) {
1334 case GST_QUERY_POSITION:
1339 /* upstream gets a chance first */
1340 if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
1341 GST_LOG_OBJECT (dec, "returning peer response");
1345 /* we start from the last seen time */
1346 time = dec->priv->last_timestamp_out;
1347 /* correct for the segment values */
1348 time = gst_segment_to_stream_time (&dec->output_segment,
1349 GST_FORMAT_TIME, time);
1351 GST_LOG_OBJECT (dec,
1352 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
1354 /* and convert to the final format */
1355 gst_query_parse_position (query, &format, NULL);
1356 if (!(res = gst_pad_query_convert (pad, GST_FORMAT_TIME, time,
1360 gst_query_set_position (query, format, value);
1362 GST_LOG_OBJECT (dec,
1363 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
1367 case GST_QUERY_DURATION:
1371 /* upstream in any case */
1372 if ((res = gst_pad_query_default (pad, parent, query)))
1375 gst_query_parse_duration (query, &format, NULL);
1376 /* try answering TIME by converting from BYTE if subclass allows */
1377 if (format == GST_FORMAT_TIME && gst_video_decoder_do_byte (dec)) {
1380 if (gst_pad_peer_query_duration (dec->sinkpad, GST_FORMAT_BYTES,
1382 GST_LOG_OBJECT (dec, "upstream size %" G_GINT64_FORMAT, value);
1383 if (gst_pad_query_convert (dec->sinkpad,
1384 GST_FORMAT_BYTES, value, GST_FORMAT_TIME, &value)) {
1385 gst_query_set_duration (query, GST_FORMAT_TIME, value);
1392 case GST_QUERY_CONVERT:
1394 GstFormat src_fmt, dest_fmt;
1395 gint64 src_val, dest_val;
1397 GST_DEBUG_OBJECT (dec, "convert query");
1399 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1400 GST_OBJECT_LOCK (dec);
1401 if (dec->priv->output_state != NULL)
1402 res = gst_video_rawvideo_convert (dec->priv->output_state,
1403 src_fmt, src_val, &dest_fmt, &dest_val);
1406 GST_OBJECT_UNLOCK (dec);
1409 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1412 case GST_QUERY_LATENCY:
1415 GstClockTime min_latency, max_latency;
1417 res = gst_pad_peer_query (dec->sinkpad, query);
1419 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1420 GST_DEBUG_OBJECT (dec, "Peer qlatency: live %d, min %"
1421 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1422 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1424 GST_OBJECT_LOCK (dec);
1425 min_latency += dec->priv->min_latency;
1426 if (dec->priv->max_latency == GST_CLOCK_TIME_NONE) {
1427 max_latency = GST_CLOCK_TIME_NONE;
1428 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1429 max_latency += dec->priv->max_latency;
1431 GST_OBJECT_UNLOCK (dec);
1433 gst_query_set_latency (query, live, min_latency, max_latency);
1438 res = gst_pad_query_default (pad, parent, query);
1443 GST_ERROR_OBJECT (dec, "query failed");
1448 gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
1451 GstVideoDecoder *decoder;
1452 GstVideoDecoderPrivate *priv;
1453 gboolean res = FALSE;
1455 decoder = GST_VIDEO_DECODER (parent);
1456 priv = decoder->priv;
1458 GST_LOG_OBJECT (decoder, "handling query: %" GST_PTR_FORMAT, query);
1460 switch (GST_QUERY_TYPE (query)) {
1461 case GST_QUERY_CONVERT:
1463 GstFormat src_fmt, dest_fmt;
1464 gint64 src_val, dest_val;
1466 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1468 gst_video_encoded_video_convert (priv->bytes_out, priv->time, src_fmt,
1469 src_val, &dest_fmt, &dest_val);
1472 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1475 case GST_QUERY_ALLOCATION:{
1476 GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1478 if (klass->propose_allocation)
1479 res = klass->propose_allocation (decoder, query);
1483 res = gst_pad_query_default (pad, parent, query);
1490 GST_DEBUG_OBJECT (decoder, "query failed");
1494 typedef struct _Timestamp Timestamp;
1500 GstClockTime duration;
1504 timestamp_free (Timestamp * ts)
1506 g_slice_free (Timestamp, ts);
1510 gst_video_decoder_add_timestamp (GstVideoDecoder * decoder, GstBuffer * buffer)
1512 GstVideoDecoderPrivate *priv = decoder->priv;
1515 ts = g_slice_new (Timestamp);
1517 GST_LOG_OBJECT (decoder,
1518 "adding PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT
1519 " (offset:%" G_GUINT64_FORMAT ")",
1520 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1521 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), priv->input_offset);
1523 ts->offset = priv->input_offset;
1524 ts->pts = GST_BUFFER_PTS (buffer);
1525 ts->dts = GST_BUFFER_DTS (buffer);
1526 ts->duration = GST_BUFFER_DURATION (buffer);
1528 priv->timestamps = g_list_append (priv->timestamps, ts);
1532 gst_video_decoder_get_timestamp_at_offset (GstVideoDecoder *
1533 decoder, guint64 offset, GstClockTime * pts, GstClockTime * dts,
1534 GstClockTime * duration)
1536 #ifndef GST_DISABLE_GST_DEBUG
1537 guint64 got_offset = 0;
1542 *pts = GST_CLOCK_TIME_NONE;
1543 *dts = GST_CLOCK_TIME_NONE;
1544 *duration = GST_CLOCK_TIME_NONE;
1546 g = decoder->priv->timestamps;
1549 if (ts->offset <= offset) {
1550 #ifndef GST_DISABLE_GST_DEBUG
1551 got_offset = ts->offset;
1555 *duration = ts->duration;
1556 timestamp_free (ts);
1558 decoder->priv->timestamps = g_list_remove (decoder->priv->timestamps, ts);
1564 GST_LOG_OBJECT (decoder,
1565 "got PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT " @ offs %"
1566 G_GUINT64_FORMAT " (wanted offset:%" G_GUINT64_FORMAT ")",
1567 GST_TIME_ARGS (*pts), GST_TIME_ARGS (*dts), got_offset, offset);
1571 gst_video_decoder_clear_queues (GstVideoDecoder * dec)
1573 GstVideoDecoderPrivate *priv = dec->priv;
1575 g_list_free_full (priv->output_queued,
1576 (GDestroyNotify) gst_mini_object_unref);
1577 priv->output_queued = NULL;
1579 g_list_free_full (priv->gather, (GDestroyNotify) gst_mini_object_unref);
1580 priv->gather = NULL;
1581 g_list_free_full (priv->decode, (GDestroyNotify) gst_video_codec_frame_unref);
1582 priv->decode = NULL;
1583 g_list_free_full (priv->parse, (GDestroyNotify) gst_mini_object_unref);
1585 g_list_free_full (priv->parse_gather,
1586 (GDestroyNotify) gst_video_codec_frame_unref);
1587 priv->parse_gather = NULL;
1588 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1589 priv->frames = NULL;
1593 gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full)
1595 GstVideoDecoderPrivate *priv = decoder->priv;
1597 GST_DEBUG_OBJECT (decoder, "reset full %d", full);
1599 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1602 gst_segment_init (&decoder->input_segment, GST_FORMAT_UNDEFINED);
1603 gst_segment_init (&decoder->output_segment, GST_FORMAT_UNDEFINED);
1604 gst_video_decoder_clear_queues (decoder);
1605 priv->error_count = 0;
1606 priv->max_errors = GST_VIDEO_DECODER_MAX_ERRORS;
1607 if (priv->input_state)
1608 gst_video_codec_state_unref (priv->input_state);
1609 priv->input_state = NULL;
1610 GST_OBJECT_LOCK (decoder);
1611 if (priv->output_state)
1612 gst_video_codec_state_unref (priv->output_state);
1613 priv->output_state = NULL;
1615 priv->qos_frame_duration = 0;
1616 GST_OBJECT_UNLOCK (decoder);
1618 priv->min_latency = 0;
1619 priv->max_latency = 0;
1622 gst_tag_list_unref (priv->tags);
1624 priv->tags_changed = FALSE;
1625 priv->reordered_output = FALSE;
1628 priv->discont = TRUE;
1630 priv->base_timestamp = GST_CLOCK_TIME_NONE;
1631 priv->last_timestamp_out = GST_CLOCK_TIME_NONE;
1632 priv->pts_delta = GST_CLOCK_TIME_NONE;
1634 priv->input_offset = 0;
1635 priv->frame_offset = 0;
1636 gst_adapter_clear (priv->input_adapter);
1637 gst_adapter_clear (priv->output_adapter);
1638 g_list_free_full (priv->timestamps, (GDestroyNotify) timestamp_free);
1639 priv->timestamps = NULL;
1641 if (priv->current_frame) {
1642 gst_video_codec_frame_unref (priv->current_frame);
1643 priv->current_frame = NULL;
1647 priv->processed = 0;
1649 priv->decode_frame_number = 0;
1650 priv->base_picture_number = 0;
1652 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1653 priv->frames = NULL;
1655 priv->bytes_out = 0;
1658 GST_OBJECT_LOCK (decoder);
1659 priv->earliest_time = GST_CLOCK_TIME_NONE;
1660 priv->proportion = 0.5;
1661 GST_OBJECT_UNLOCK (decoder);
1663 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1666 static GstFlowReturn
1667 gst_video_decoder_chain_forward (GstVideoDecoder * decoder,
1668 GstBuffer * buf, gboolean at_eos)
1670 GstVideoDecoderPrivate *priv;
1671 GstVideoDecoderClass *klass;
1672 GstFlowReturn ret = GST_FLOW_OK;
1674 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1675 priv = decoder->priv;
1677 g_return_val_if_fail (priv->packetized || klass->parse, GST_FLOW_ERROR);
1679 if (priv->current_frame == NULL)
1680 priv->current_frame = gst_video_decoder_new_frame (decoder);
1682 if (GST_BUFFER_PTS_IS_VALID (buf) && !priv->packetized) {
1683 gst_video_decoder_add_timestamp (decoder, buf);
1685 priv->input_offset += gst_buffer_get_size (buf);
1687 if (priv->packetized) {
1688 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1689 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (priv->current_frame);
1692 priv->current_frame->input_buffer = buf;
1694 if (decoder->input_segment.rate < 0.0) {
1695 priv->parse_gather =
1696 g_list_prepend (priv->parse_gather, priv->current_frame);
1698 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
1700 priv->current_frame = NULL;
1703 gst_adapter_push (priv->input_adapter, buf);
1705 if (G_UNLIKELY (!gst_adapter_available (priv->input_adapter)))
1709 /* current frame may have been parsed and handled,
1710 * so we need to set up a new one when asking subclass to parse */
1711 if (priv->current_frame == NULL)
1712 priv->current_frame = gst_video_decoder_new_frame (decoder);
1714 ret = klass->parse (decoder, priv->current_frame,
1715 priv->input_adapter, at_eos);
1716 } while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter));
1720 if (ret == GST_VIDEO_DECODER_FLOW_NEED_DATA)
1726 static GstFlowReturn
1727 gst_video_decoder_flush_decode (GstVideoDecoder * dec)
1729 GstVideoDecoderPrivate *priv = dec->priv;
1730 GstFlowReturn res = GST_FLOW_OK;
1733 GST_DEBUG_OBJECT (dec, "flushing buffers to decode");
1735 /* clear buffer and decoder state */
1736 gst_video_decoder_flush (dec, FALSE);
1738 walk = priv->decode;
1741 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1743 GST_DEBUG_OBJECT (dec, "decoding frame %p buffer %p, PTS %" GST_TIME_FORMAT
1744 ", DTS %" GST_TIME_FORMAT, frame, frame->input_buffer,
1745 GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
1746 GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
1750 priv->decode = g_list_delete_link (priv->decode, walk);
1752 /* decode buffer, resulting data prepended to queue */
1753 res = gst_video_decoder_decode_frame (dec, frame);
1754 if (res != GST_FLOW_OK)
1763 /* gst_video_decoder_flush_parse is called from the
1764 * chain_reverse() function when a buffer containing
1765 * a DISCONT - indicating that reverse playback
1766 * looped back to the next data block, and therefore
1767 * all available data should be fed through the
1768 * decoder and frames gathered for reversed output
1770 static GstFlowReturn
1771 gst_video_decoder_flush_parse (GstVideoDecoder * dec, gboolean at_eos)
1773 GstVideoDecoderPrivate *priv = dec->priv;
1774 GstFlowReturn res = GST_FLOW_OK;
1777 GST_DEBUG_OBJECT (dec, "flushing buffers to parsing");
1779 /* Reverse the gather list, and prepend it to the parse list,
1780 * then flush to parse whatever we can */
1781 priv->gather = g_list_reverse (priv->gather);
1782 priv->parse = g_list_concat (priv->gather, priv->parse);
1783 priv->gather = NULL;
1785 /* clear buffer and decoder state */
1786 gst_video_decoder_flush (dec, FALSE);
1790 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1791 GList *next = walk->next;
1793 GST_DEBUG_OBJECT (dec, "parsing buffer %p, PTS %" GST_TIME_FORMAT
1794 ", DTS %" GST_TIME_FORMAT, buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1795 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
1797 /* parse buffer, resulting frames prepended to parse_gather queue */
1798 gst_buffer_ref (buf);
1799 res = gst_video_decoder_chain_forward (dec, buf, at_eos);
1801 /* if we generated output, we can discard the buffer, else we
1802 * keep it in the queue */
1803 if (priv->parse_gather) {
1804 GST_DEBUG_OBJECT (dec, "parsed buffer to %p", priv->parse_gather->data);
1805 priv->parse = g_list_delete_link (priv->parse, walk);
1806 gst_buffer_unref (buf);
1808 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1813 /* now we can process frames. Start by moving each frame from the parse_gather
1814 * to the decode list, reverse the order as we go, and stopping when/if we
1815 * copy a keyframe. */
1816 GST_DEBUG_OBJECT (dec, "checking parsed frames for a keyframe to decode");
1817 walk = priv->parse_gather;
1819 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1821 /* remove from the gather list */
1822 priv->parse_gather = g_list_remove_link (priv->parse_gather, walk);
1824 /* move it to the front of the decode queue */
1825 priv->decode = g_list_concat (walk, priv->decode);
1827 /* if we copied a keyframe, flush and decode the decode queue */
1828 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1829 GST_DEBUG_OBJECT (dec, "found keyframe %p with PTS %" GST_TIME_FORMAT
1830 ", DTS %" GST_TIME_FORMAT, frame,
1831 GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
1832 GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
1833 res = gst_video_decoder_flush_decode (dec);
1834 if (res != GST_FLOW_OK)
1838 walk = priv->parse_gather;
1841 /* now send queued data downstream */
1842 walk = priv->output_queued;
1844 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1846 if (G_LIKELY (res == GST_FLOW_OK)) {
1847 /* avoid stray DISCONT from forward processing,
1848 * which have no meaning in reverse pushing */
1849 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
1851 /* Last chance to calculate a timestamp as we loop backwards
1852 * through the list */
1853 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE)
1854 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1855 else if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
1856 GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
1857 GST_BUFFER_TIMESTAMP (buf) =
1858 priv->last_timestamp_out - GST_BUFFER_DURATION (buf);
1859 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1860 GST_LOG_OBJECT (dec,
1861 "Calculated TS %" GST_TIME_FORMAT " working backwards",
1862 GST_TIME_ARGS (priv->last_timestamp_out));
1865 res = gst_video_decoder_clip_and_push_buf (dec, buf);
1867 gst_buffer_unref (buf);
1870 priv->output_queued =
1871 g_list_delete_link (priv->output_queued, priv->output_queued);
1872 walk = priv->output_queued;
1879 static GstFlowReturn
1880 gst_video_decoder_chain_reverse (GstVideoDecoder * dec, GstBuffer * buf)
1882 GstVideoDecoderPrivate *priv = dec->priv;
1883 GstFlowReturn result = GST_FLOW_OK;
1885 /* if we have a discont, move buffers to the decode list */
1886 if (!buf || GST_BUFFER_IS_DISCONT (buf)) {
1887 GST_DEBUG_OBJECT (dec, "received discont");
1889 /* parse and decode stuff in the gather and parse queues */
1890 gst_video_decoder_flush_parse (dec, FALSE);
1893 if (G_LIKELY (buf)) {
1894 GST_DEBUG_OBJECT (dec, "gathering buffer %p of size %" G_GSIZE_FORMAT ", "
1895 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
1896 GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1897 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1898 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1899 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1901 /* add buffer to gather queue */
1902 priv->gather = g_list_prepend (priv->gather, buf);
1908 static GstFlowReturn
1909 gst_video_decoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1911 GstVideoDecoder *decoder;
1912 GstFlowReturn ret = GST_FLOW_OK;
1914 decoder = GST_VIDEO_DECODER (parent);
1916 if (G_UNLIKELY (decoder->priv->do_caps)) {
1917 GstCaps *caps = gst_pad_get_current_caps (decoder->sinkpad);
1919 if (!gst_video_decoder_setcaps (decoder, caps)) {
1920 gst_caps_unref (caps);
1921 goto not_negotiated;
1923 gst_caps_unref (caps);
1925 decoder->priv->do_caps = FALSE;
1928 GST_LOG_OBJECT (decoder,
1929 "chain PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT " duration %"
1930 GST_TIME_FORMAT " size %" G_GSIZE_FORMAT,
1931 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1932 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1933 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), gst_buffer_get_size (buf));
1935 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1938 * requiring the pad to be negotiated makes it impossible to use
1939 * oggdemux or filesrc ! decoder */
1941 if (decoder->input_segment.format == GST_FORMAT_UNDEFINED) {
1943 GstSegment *segment = &decoder->input_segment;
1945 GST_WARNING_OBJECT (decoder,
1946 "Received buffer without a new-segment. "
1947 "Assuming timestamps start from 0.");
1949 gst_segment_init (segment, GST_FORMAT_TIME);
1951 event = gst_event_new_segment (segment);
1953 decoder->priv->current_frame_events =
1954 g_list_prepend (decoder->priv->current_frame_events, event);
1957 if (decoder->input_segment.rate > 0.0)
1958 ret = gst_video_decoder_chain_forward (decoder, buf, FALSE);
1960 ret = gst_video_decoder_chain_reverse (decoder, buf);
1962 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1968 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION, (NULL),
1969 ("encoder not initialized"));
1970 gst_buffer_unref (buf);
1971 return GST_FLOW_NOT_NEGOTIATED;
1975 static GstStateChangeReturn
1976 gst_video_decoder_change_state (GstElement * element, GstStateChange transition)
1978 GstVideoDecoder *decoder;
1979 GstVideoDecoderClass *decoder_class;
1980 GstStateChangeReturn ret;
1982 decoder = GST_VIDEO_DECODER (element);
1983 decoder_class = GST_VIDEO_DECODER_GET_CLASS (element);
1985 switch (transition) {
1986 case GST_STATE_CHANGE_NULL_TO_READY:
1987 /* open device/library if needed */
1988 if (decoder_class->open && !decoder_class->open (decoder))
1991 case GST_STATE_CHANGE_READY_TO_PAUSED:
1992 /* Initialize device/library if needed */
1993 if (decoder_class->start && !decoder_class->start (decoder))
2000 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2002 switch (transition) {
2003 case GST_STATE_CHANGE_PAUSED_TO_READY:
2004 if (decoder_class->stop && !decoder_class->stop (decoder))
2007 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2008 gst_video_decoder_reset (decoder, TRUE);
2009 g_list_free_full (decoder->priv->current_frame_events,
2010 (GDestroyNotify) gst_event_unref);
2011 decoder->priv->current_frame_events = NULL;
2012 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2014 case GST_STATE_CHANGE_READY_TO_NULL:
2015 /* close device/library if needed */
2016 if (decoder_class->close && !decoder_class->close (decoder))
2028 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2029 ("Failed to open decoder"));
2030 return GST_STATE_CHANGE_FAILURE;
2035 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2036 ("Failed to start decoder"));
2037 return GST_STATE_CHANGE_FAILURE;
2042 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2043 ("Failed to stop decoder"));
2044 return GST_STATE_CHANGE_FAILURE;
2049 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2050 ("Failed to close decoder"));
2051 return GST_STATE_CHANGE_FAILURE;
2055 static GstVideoCodecFrame *
2056 gst_video_decoder_new_frame (GstVideoDecoder * decoder)
2058 GstVideoDecoderPrivate *priv = decoder->priv;
2059 GstVideoCodecFrame *frame;
2061 frame = g_slice_new0 (GstVideoCodecFrame);
2063 frame->ref_count = 1;
2065 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2066 frame->system_frame_number = priv->system_frame_number;
2067 priv->system_frame_number++;
2068 frame->decode_frame_number = priv->decode_frame_number;
2069 priv->decode_frame_number++;
2071 frame->dts = GST_CLOCK_TIME_NONE;
2072 frame->pts = GST_CLOCK_TIME_NONE;
2073 frame->duration = GST_CLOCK_TIME_NONE;
2074 frame->events = priv->current_frame_events;
2075 priv->current_frame_events = NULL;
2076 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2078 GST_LOG_OBJECT (decoder, "Created new frame %p (sfn:%d)",
2079 frame, frame->system_frame_number);
2085 gst_video_decoder_prepare_finish_frame (GstVideoDecoder *
2086 decoder, GstVideoCodecFrame * frame, gboolean dropping)
2088 GstVideoDecoderPrivate *priv = decoder->priv;
2089 GList *l, *events = NULL;
2091 #ifndef GST_DISABLE_GST_DEBUG
2092 GST_LOG_OBJECT (decoder, "n %d in %" G_GSIZE_FORMAT " out %" G_GSIZE_FORMAT,
2093 g_list_length (priv->frames),
2094 gst_adapter_available (priv->input_adapter),
2095 gst_adapter_available (priv->output_adapter));
2098 GST_LOG_OBJECT (decoder,
2099 "finish frame %p (#%d) sync:%d PTS:%" GST_TIME_FORMAT " DTS:%"
2101 frame, frame->system_frame_number,
2102 GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame), GST_TIME_ARGS (frame->pts),
2103 GST_TIME_ARGS (frame->dts));
2105 /* Push all pending events that arrived before this frame */
2106 for (l = priv->frames; l; l = l->next) {
2107 GstVideoCodecFrame *tmp = l->data;
2110 events = g_list_concat (events, tmp->events);
2118 for (l = g_list_last (events); l; l = g_list_previous (l)) {
2119 GST_LOG_OBJECT (decoder, "pushing %s event", GST_EVENT_TYPE_NAME (l->data));
2120 gst_video_decoder_push_event (decoder, l->data);
2122 g_list_free (events);
2124 /* Check if the data should not be displayed. For example altref/invisible
2125 * frame in vp8. In this case we should not update the timestamps. */
2126 if (GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame))
2129 /* If the frame is meant to be output but we don't have an output_buffer
2130 * we have a problem :) */
2131 if (G_UNLIKELY ((frame->output_buffer == NULL) && !dropping))
2132 goto no_output_buffer;
2134 if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
2135 if (frame->pts != priv->base_timestamp) {
2136 GST_DEBUG_OBJECT (decoder,
2137 "sync timestamp %" GST_TIME_FORMAT " diff %" GST_TIME_FORMAT,
2138 GST_TIME_ARGS (frame->pts),
2139 GST_TIME_ARGS (frame->pts - decoder->output_segment.start));
2140 priv->base_timestamp = frame->pts;
2141 priv->base_picture_number = frame->decode_frame_number;
2145 if (frame->duration == GST_CLOCK_TIME_NONE) {
2146 frame->duration = gst_video_decoder_get_frame_duration (decoder, frame);
2147 GST_LOG_OBJECT (decoder,
2148 "Guessing duration %" GST_TIME_FORMAT " for frame...",
2149 GST_TIME_ARGS (frame->duration));
2152 /* PTS is expected montone ascending,
2153 * so a good guess is lowest unsent DTS */
2155 GstClockTime min_ts = GST_CLOCK_TIME_NONE;
2156 GstVideoCodecFrame *oframe = NULL;
2157 gboolean seen_none = FALSE;
2159 /* some maintenance regardless */
2160 for (l = priv->frames; l; l = l->next) {
2161 GstVideoCodecFrame *tmp = l->data;
2163 if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
2168 if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
2169 min_ts = tmp->abidata.ABI.ts;
2173 /* save a ts if needed */
2174 if (oframe && oframe != frame) {
2175 oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
2178 /* and set if needed;
2179 * valid delta means we have reasonable DTS input */
2180 /* also, if we ended up reordered, means this approach is conflicting
2181 * with some sparse existing PTS, and so it does not work out */
2182 if (!priv->reordered_output &&
2183 !GST_CLOCK_TIME_IS_VALID (frame->pts) && !seen_none &&
2184 GST_CLOCK_TIME_IS_VALID (priv->pts_delta)) {
2185 frame->pts = min_ts + priv->pts_delta;
2186 GST_DEBUG_OBJECT (decoder,
2187 "no valid PTS, using oldest DTS %" GST_TIME_FORMAT,
2188 GST_TIME_ARGS (frame->pts));
2191 /* some more maintenance, ts2 holds PTS */
2192 min_ts = GST_CLOCK_TIME_NONE;
2194 for (l = priv->frames; l; l = l->next) {
2195 GstVideoCodecFrame *tmp = l->data;
2197 if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts2)) {
2202 if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts2 < min_ts) {
2203 min_ts = tmp->abidata.ABI.ts2;
2207 /* save a ts if needed */
2208 if (oframe && oframe != frame) {
2209 oframe->abidata.ABI.ts2 = frame->abidata.ABI.ts2;
2212 /* if we detected reordered output, then PTS are void,
2213 * however those were obtained; bogus input, subclass etc */
2214 if (priv->reordered_output && !seen_none) {
2215 GST_DEBUG_OBJECT (decoder, "invaliding PTS");
2216 frame->pts = GST_CLOCK_TIME_NONE;
2219 if (!GST_CLOCK_TIME_IS_VALID (frame->pts) && !seen_none) {
2220 frame->pts = min_ts;
2221 GST_DEBUG_OBJECT (decoder,
2222 "no valid PTS, using oldest PTS %" GST_TIME_FORMAT,
2223 GST_TIME_ARGS (frame->pts));
2228 if (frame->pts == GST_CLOCK_TIME_NONE) {
2229 /* Last ditch timestamp guess: Just add the duration to the previous
2231 if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
2232 frame->duration != GST_CLOCK_TIME_NONE) {
2233 frame->pts = priv->last_timestamp_out + frame->duration;
2234 GST_LOG_OBJECT (decoder,
2235 "Guessing timestamp %" GST_TIME_FORMAT " for frame...",
2236 GST_TIME_ARGS (frame->pts));
2240 if (GST_CLOCK_TIME_IS_VALID (priv->last_timestamp_out)) {
2241 if (frame->pts < priv->last_timestamp_out) {
2242 GST_WARNING_OBJECT (decoder,
2243 "decreasing timestamp (%" GST_TIME_FORMAT " < %"
2244 GST_TIME_FORMAT ")",
2245 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (priv->last_timestamp_out));
2246 priv->reordered_output = TRUE;
2250 if (GST_CLOCK_TIME_IS_VALID (frame->pts))
2251 priv->last_timestamp_out = frame->pts;
2258 GST_ERROR_OBJECT (decoder, "No buffer to output !");
2263 gst_video_decoder_release_frame (GstVideoDecoder * dec,
2264 GstVideoCodecFrame * frame)
2268 /* unref once from the list */
2269 link = g_list_find (dec->priv->frames, frame);
2271 gst_video_codec_frame_unref (frame);
2272 dec->priv->frames = g_list_delete_link (dec->priv->frames, link);
2275 /* unref because this function takes ownership */
2276 gst_video_codec_frame_unref (frame);
2280 * gst_video_decoder_drop_frame:
2281 * @dec: a #GstVideoDecoder
2282 * @frame: (transfer full): the #GstVideoCodecFrame to drop
2284 * Similar to gst_video_decoder_finish_frame(), but drops @frame in any
2285 * case and posts a QoS message with the frame's details on the bus.
2286 * In any case, the frame is considered finished and released.
2288 * Returns: a #GstFlowReturn, usually GST_FLOW_OK.
2291 gst_video_decoder_drop_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame)
2293 GstClockTime stream_time, jitter, earliest_time, qostime, timestamp;
2294 GstSegment *segment;
2295 GstMessage *qos_msg;
2298 GST_LOG_OBJECT (dec, "drop frame %p", frame);
2300 GST_VIDEO_DECODER_STREAM_LOCK (dec);
2302 gst_video_decoder_prepare_finish_frame (dec, frame, TRUE);
2304 GST_DEBUG_OBJECT (dec, "dropping frame %" GST_TIME_FORMAT,
2305 GST_TIME_ARGS (frame->pts));
2307 dec->priv->dropped++;
2309 /* post QoS message */
2310 GST_OBJECT_LOCK (dec);
2311 proportion = dec->priv->proportion;
2312 earliest_time = dec->priv->earliest_time;
2313 GST_OBJECT_UNLOCK (dec);
2315 timestamp = frame->pts;
2316 segment = &dec->output_segment;
2318 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, timestamp);
2319 qostime = gst_segment_to_running_time (segment, GST_FORMAT_TIME, timestamp);
2320 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
2322 gst_message_new_qos (GST_OBJECT_CAST (dec), FALSE, qostime, stream_time,
2323 timestamp, GST_CLOCK_TIME_NONE);
2324 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2325 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2326 dec->priv->processed, dec->priv->dropped);
2327 gst_element_post_message (GST_ELEMENT_CAST (dec), qos_msg);
2329 /* now free the frame */
2330 gst_video_decoder_release_frame (dec, frame);
2332 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
2338 * gst_video_decoder_finish_frame:
2339 * @decoder: a #GstVideoDecoder
2340 * @frame: (transfer full): a decoded #GstVideoCodecFrame
2342 * @frame should have a valid decoded data buffer, whose metadata fields
2343 * are then appropriately set according to frame data and pushed downstream.
2344 * If no output data is provided, @frame is considered skipped.
2345 * In any case, the frame is considered finished and released.
2347 * After calling this function the output buffer of the frame is to be
2348 * considered read-only. This function will also change the metadata
2351 * Returns: a #GstFlowReturn resulting from sending data downstream
2354 gst_video_decoder_finish_frame (GstVideoDecoder * decoder,
2355 GstVideoCodecFrame * frame)
2357 GstFlowReturn ret = GST_FLOW_OK;
2358 GstVideoDecoderPrivate *priv = decoder->priv;
2359 GstBuffer *output_buffer;
2361 GST_LOG_OBJECT (decoder, "finish frame %p", frame);
2363 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2365 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
2366 && gst_pad_check_reconfigure (decoder->srcpad))))
2367 gst_video_decoder_negotiate (decoder);
2369 gst_video_decoder_prepare_finish_frame (decoder, frame, FALSE);
2372 if (priv->tags && priv->tags_changed) {
2373 gst_video_decoder_push_event (decoder,
2374 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
2375 priv->tags_changed = FALSE;
2378 /* no buffer data means this frame is skipped */
2379 if (!frame->output_buffer || GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame)) {
2380 GST_DEBUG_OBJECT (decoder, "skipping frame %" GST_TIME_FORMAT,
2381 GST_TIME_ARGS (frame->pts));
2385 output_buffer = frame->output_buffer;
2387 GST_BUFFER_FLAG_UNSET (output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
2389 /* set PTS and DTS to both the PTS for decoded frames */
2390 GST_BUFFER_PTS (output_buffer) = frame->pts;
2391 GST_BUFFER_DTS (output_buffer) = frame->pts;
2392 GST_BUFFER_DURATION (output_buffer) = frame->duration;
2394 GST_BUFFER_OFFSET (output_buffer) = GST_BUFFER_OFFSET_NONE;
2395 GST_BUFFER_OFFSET_END (output_buffer) = GST_BUFFER_OFFSET_NONE;
2397 if (priv->discont) {
2398 GST_BUFFER_FLAG_SET (output_buffer, GST_BUFFER_FLAG_DISCONT);
2399 priv->discont = FALSE;
2402 /* Get an additional ref to the buffer, which is going to be pushed
2403 * downstream, the original ref is owned by the frame
2405 * FIXME: clip_and_push_buf() changes buffer metadata but the buffer
2406 * might have a refcount > 1 */
2407 output_buffer = gst_buffer_ref (output_buffer);
2408 if (decoder->output_segment.rate < 0.0) {
2409 GST_LOG_OBJECT (decoder, "queued frame");
2410 priv->output_queued = g_list_prepend (priv->output_queued, output_buffer);
2412 ret = gst_video_decoder_clip_and_push_buf (decoder, output_buffer);
2416 gst_video_decoder_release_frame (decoder, frame);
2417 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2422 /* With stream lock, takes the frame reference */
2423 static GstFlowReturn
2424 gst_video_decoder_clip_and_push_buf (GstVideoDecoder * decoder, GstBuffer * buf)
2426 GstFlowReturn ret = GST_FLOW_OK;
2427 GstVideoDecoderPrivate *priv = decoder->priv;
2428 guint64 start, stop;
2429 guint64 cstart, cstop;
2430 GstSegment *segment;
2431 GstClockTime duration;
2433 /* Check for clipping */
2434 start = GST_BUFFER_PTS (buf);
2435 duration = GST_BUFFER_DURATION (buf);
2437 stop = GST_CLOCK_TIME_NONE;
2439 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (duration)) {
2440 stop = start + duration;
2443 segment = &decoder->output_segment;
2444 if (gst_segment_clip (segment, GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
2446 GST_BUFFER_PTS (buf) = cstart;
2448 if (stop != GST_CLOCK_TIME_NONE)
2449 GST_BUFFER_DURATION (buf) = cstop - cstart;
2451 GST_LOG_OBJECT (decoder,
2452 "accepting buffer inside segment: %" GST_TIME_FORMAT " %"
2453 GST_TIME_FORMAT " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2454 " time %" GST_TIME_FORMAT,
2455 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2456 GST_TIME_ARGS (GST_BUFFER_PTS (buf) +
2457 GST_BUFFER_DURATION (buf)),
2458 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
2459 GST_TIME_ARGS (segment->time));
2461 GST_LOG_OBJECT (decoder,
2462 "dropping buffer outside segment: %" GST_TIME_FORMAT
2463 " %" GST_TIME_FORMAT
2464 " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2465 " time %" GST_TIME_FORMAT,
2466 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2467 GST_TIME_ARGS (segment->start),
2468 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time));
2469 gst_buffer_unref (buf);
2473 /* update rate estimate */
2474 priv->bytes_out += gst_buffer_get_size (buf);
2475 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2476 priv->time += duration;
2478 /* FIXME : Use difference between current and previous outgoing
2479 * timestamp, and relate to difference between current and previous
2481 /* better none than nothing valid */
2482 priv->time = GST_CLOCK_TIME_NONE;
2485 GST_DEBUG_OBJECT (decoder, "pushing buffer %p of size %" G_GSIZE_FORMAT ", "
2486 "PTS %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
2487 gst_buffer_get_size (buf),
2488 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2489 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2491 /* we got data, so note things are looking up again, reduce
2492 * the error count, if there is one */
2493 if (G_UNLIKELY (priv->error_count))
2494 priv->error_count = 0;
2496 ret = gst_pad_push (decoder->srcpad, buf);
2503 * gst_video_decoder_add_to_frame:
2504 * @decoder: a #GstVideoDecoder
2505 * @n_bytes: the number of bytes to add
2507 * Removes next @n_bytes of input data and adds it to currently parsed frame.
2510 gst_video_decoder_add_to_frame (GstVideoDecoder * decoder, int n_bytes)
2512 GstVideoDecoderPrivate *priv = decoder->priv;
2515 GST_LOG_OBJECT (decoder, "add %d bytes to frame", n_bytes);
2520 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2521 if (gst_adapter_available (priv->output_adapter) == 0) {
2522 priv->frame_offset =
2523 priv->input_offset - gst_adapter_available (priv->input_adapter);
2525 buf = gst_adapter_take_buffer (priv->input_adapter, n_bytes);
2527 gst_adapter_push (priv->output_adapter, buf);
2528 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2532 gst_video_decoder_get_frame_duration (GstVideoDecoder * decoder,
2533 GstVideoCodecFrame * frame)
2535 GstVideoCodecState *state = decoder->priv->output_state;
2537 /* it's possible that we don't have a state yet when we are dropping the
2538 * initial buffers */
2540 return GST_CLOCK_TIME_NONE;
2542 if (state->info.fps_d == 0 || state->info.fps_n == 0) {
2543 return GST_CLOCK_TIME_NONE;
2546 /* FIXME: For interlaced frames this needs to take into account
2547 * the number of valid fields in the frame
2550 return gst_util_uint64_scale (GST_SECOND, state->info.fps_d,
2555 * gst_video_decoder_have_frame:
2556 * @decoder: a #GstVideoDecoder
2558 * Gathers all data collected for currently parsed frame, gathers corresponding
2559 * metadata and passes it along for further processing, i.e. @handle_frame.
2561 * Returns: a #GstFlowReturn
2564 gst_video_decoder_have_frame (GstVideoDecoder * decoder)
2566 GstVideoDecoderPrivate *priv = decoder->priv;
2569 GstClockTime pts, dts, duration;
2570 GstFlowReturn ret = GST_FLOW_OK;
2572 GST_LOG_OBJECT (decoder, "have_frame");
2574 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2576 n_available = gst_adapter_available (priv->output_adapter);
2578 buffer = gst_adapter_take_buffer (priv->output_adapter, n_available);
2580 buffer = gst_buffer_new_and_alloc (0);
2583 priv->current_frame->input_buffer = buffer;
2585 gst_video_decoder_get_timestamp_at_offset (decoder,
2586 priv->frame_offset, &pts, &dts, &duration);
2588 GST_BUFFER_PTS (buffer) = pts;
2589 GST_BUFFER_DTS (buffer) = dts;
2590 GST_BUFFER_DURATION (buffer) = duration;
2592 GST_LOG_OBJECT (decoder, "collected frame size %d, "
2593 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
2594 GST_TIME_FORMAT, n_available, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
2595 GST_TIME_ARGS (duration));
2597 /* In reverse playback, just capture and queue frames for later processing */
2598 if (decoder->output_segment.rate < 0.0) {
2599 priv->parse_gather =
2600 g_list_prepend (priv->parse_gather, priv->current_frame);
2602 /* Otherwise, decode the frame, which gives away our ref */
2603 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
2605 /* Current frame is gone now, either way */
2606 priv->current_frame = NULL;
2608 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2613 /* Pass the frame in priv->current_frame through the
2614 * handle_frame() callback for decoding and passing to gvd_finish_frame(),
2615 * or dropping by passing to gvd_drop_frame() */
2616 static GstFlowReturn
2617 gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
2618 GstVideoCodecFrame * frame)
2620 GstVideoDecoderPrivate *priv = decoder->priv;
2621 GstVideoDecoderClass *decoder_class;
2622 GstFlowReturn ret = GST_FLOW_OK;
2624 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
2626 /* FIXME : This should only have to be checked once (either the subclass has an
2627 * implementation, or it doesn't) */
2628 g_return_val_if_fail (decoder_class->handle_frame != NULL, GST_FLOW_ERROR);
2630 frame->distance_from_sync = priv->distance_from_sync;
2631 priv->distance_from_sync++;
2632 frame->pts = GST_BUFFER_PTS (frame->input_buffer);
2633 frame->dts = GST_BUFFER_DTS (frame->input_buffer);
2634 frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
2636 /* For keyframes, PTS = DTS + constant_offset, usually 0 to 3 frame
2638 /* FIXME upstream can be quite wrong about the keyframe aspect,
2639 * so we could be going off here as well,
2640 * maybe let subclass decide if it really is/was a keyframe */
2641 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) &&
2642 GST_CLOCK_TIME_IS_VALID (frame->pts)
2643 && GST_CLOCK_TIME_IS_VALID (frame->dts)) {
2644 /* just in case they are not equal as might ideally be,
2645 * e.g. quicktime has a (positive) delta approach */
2646 priv->pts_delta = frame->pts - frame->dts;
2647 GST_DEBUG_OBJECT (decoder, "PTS delta %d ms",
2648 (gint) (priv->pts_delta / GST_MSECOND));
2651 frame->abidata.ABI.ts = frame->dts;
2652 frame->abidata.ABI.ts2 = frame->pts;
2654 GST_LOG_OBJECT (decoder, "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT,
2655 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->dts));
2656 GST_LOG_OBJECT (decoder, "dist %d", frame->distance_from_sync);
2658 gst_video_codec_frame_ref (frame);
2659 priv->frames = g_list_append (priv->frames, frame);
2661 if (g_list_length (priv->frames) > 10) {
2662 GST_WARNING_OBJECT (decoder, "decoder frame list getting long: %d frames,"
2663 "possible internal leaking?", g_list_length (priv->frames));
2667 gst_segment_to_running_time (&decoder->input_segment, GST_FORMAT_TIME,
2670 /* do something with frame */
2671 ret = decoder_class->handle_frame (decoder, frame);
2672 if (ret != GST_FLOW_OK)
2673 GST_DEBUG_OBJECT (decoder, "flow error %s", gst_flow_get_name (ret));
2675 /* the frame has either been added to parse_gather or sent to
2676 handle frame so there is no need to unref it */
2682 * gst_video_decoder_get_output_state:
2683 * @decoder: a #GstVideoDecoder
2685 * Get the #GstVideoCodecState currently describing the output stream.
2687 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
2689 GstVideoCodecState *
2690 gst_video_decoder_get_output_state (GstVideoDecoder * decoder)
2692 GstVideoCodecState *state = NULL;
2694 GST_OBJECT_LOCK (decoder);
2695 if (decoder->priv->output_state)
2696 state = gst_video_codec_state_ref (decoder->priv->output_state);
2697 GST_OBJECT_UNLOCK (decoder);
2703 * gst_video_decoder_set_output_state:
2704 * @decoder: a #GstVideoDecoder
2705 * @fmt: a #GstVideoFormat
2706 * @width: The width in pixels
2707 * @height: The height in pixels
2708 * @reference: (allow-none) (transfer none): An optional reference #GstVideoCodecState
2710 * Creates a new #GstVideoCodecState with the specified @fmt, @width and @height
2711 * as the output state for the decoder.
2712 * Any previously set output state on @decoder will be replaced by the newly
2715 * If the subclass wishes to copy over existing fields (like pixel aspec ratio,
2716 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
2719 * If the subclass wishes to override some fields from the output state (like
2720 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
2722 * The new output state will only take effect (set on pads and buffers) starting
2723 * from the next call to #gst_video_decoder_finish_frame().
2725 * Returns: (transfer full): the newly configured output state.
2727 GstVideoCodecState *
2728 gst_video_decoder_set_output_state (GstVideoDecoder * decoder,
2729 GstVideoFormat fmt, guint width, guint height,
2730 GstVideoCodecState * reference)
2732 GstVideoDecoderPrivate *priv = decoder->priv;
2733 GstVideoCodecState *state;
2735 GST_DEBUG_OBJECT (decoder, "fmt:%d, width:%d, height:%d, reference:%p",
2736 fmt, width, height, reference);
2738 /* Create the new output state */
2739 state = _new_output_state (fmt, width, height, reference);
2741 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2743 GST_OBJECT_LOCK (decoder);
2744 /* Replace existing output state by new one */
2745 if (priv->output_state)
2746 gst_video_codec_state_unref (priv->output_state);
2747 priv->output_state = gst_video_codec_state_ref (state);
2749 if (priv->output_state != NULL && priv->output_state->info.fps_n > 0) {
2750 priv->qos_frame_duration =
2751 gst_util_uint64_scale (GST_SECOND, priv->output_state->info.fps_d,
2752 priv->output_state->info.fps_n);
2754 priv->qos_frame_duration = 0;
2756 priv->output_state_changed = TRUE;
2757 GST_OBJECT_UNLOCK (decoder);
2759 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2766 * gst_video_decoder_get_oldest_frame:
2767 * @decoder: a #GstVideoDecoder
2769 * Get the oldest pending unfinished #GstVideoCodecFrame
2771 * Returns: (transfer full): oldest pending unfinished #GstVideoCodecFrame.
2773 GstVideoCodecFrame *
2774 gst_video_decoder_get_oldest_frame (GstVideoDecoder * decoder)
2776 GstVideoCodecFrame *frame = NULL;
2778 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2779 if (decoder->priv->frames)
2780 frame = gst_video_codec_frame_ref (decoder->priv->frames->data);
2781 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2783 return (GstVideoCodecFrame *) frame;
2787 * gst_video_decoder_get_frame:
2788 * @decoder: a #GstVideoDecoder
2789 * @frame_number: system_frame_number of a frame
2791 * Get a pending unfinished #GstVideoCodecFrame
2793 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2795 GstVideoCodecFrame *
2796 gst_video_decoder_get_frame (GstVideoDecoder * decoder, int frame_number)
2799 GstVideoCodecFrame *frame = NULL;
2801 GST_DEBUG_OBJECT (decoder, "frame_number : %d", frame_number);
2803 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2804 for (g = decoder->priv->frames; g; g = g->next) {
2805 GstVideoCodecFrame *tmp = g->data;
2807 if (tmp->system_frame_number == frame_number) {
2808 frame = gst_video_codec_frame_ref (tmp);
2812 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2818 * gst_video_decoder_get_frames:
2819 * @decoder: a #GstVideoDecoder
2821 * Get all pending unfinished #GstVideoCodecFrame
2823 * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
2826 gst_video_decoder_get_frames (GstVideoDecoder * decoder)
2830 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2831 frames = g_list_copy (decoder->priv->frames);
2832 g_list_foreach (frames, (GFunc) gst_video_codec_frame_ref, NULL);
2833 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2839 gst_video_decoder_decide_allocation_default (GstVideoDecoder * decoder,
2843 GstBufferPool *pool = NULL;
2844 guint size, min, max;
2845 GstAllocator *allocator = NULL;
2846 GstAllocationParams params;
2847 GstStructure *config;
2848 gboolean update_pool, update_allocator;
2851 gst_query_parse_allocation (query, &outcaps, NULL);
2852 gst_video_info_init (&vinfo);
2853 gst_video_info_from_caps (&vinfo, outcaps);
2855 /* we got configuration from our peer or the decide_allocation method,
2857 if (gst_query_get_n_allocation_params (query) > 0) {
2858 /* try the allocator */
2859 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2860 update_allocator = TRUE;
2863 gst_allocation_params_init (¶ms);
2864 update_allocator = FALSE;
2867 if (gst_query_get_n_allocation_pools (query) > 0) {
2868 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2869 size = MAX (size, vinfo.size);
2876 update_pool = FALSE;
2880 /* no pool, we can make our own */
2881 GST_DEBUG_OBJECT (decoder, "no pool, making new pool");
2882 pool = gst_video_buffer_pool_new ();
2886 config = gst_buffer_pool_get_config (pool);
2887 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
2888 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2889 gst_buffer_pool_set_config (pool, config);
2891 if (update_allocator)
2892 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2894 gst_query_add_allocation_param (query, allocator, ¶ms);
2896 gst_object_unref (allocator);
2899 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2901 gst_query_add_allocation_pool (query, pool, size, min, max);
2904 gst_object_unref (pool);
2910 gst_video_decoder_propose_allocation_default (GstVideoDecoder * decoder,
2917 gst_video_decoder_negotiate_default (GstVideoDecoder * decoder)
2919 GstVideoCodecState *state = decoder->priv->output_state;
2920 GstVideoDecoderClass *klass;
2921 GstQuery *query = NULL;
2922 GstBufferPool *pool = NULL;
2923 GstAllocator *allocator;
2924 GstAllocationParams params;
2925 gboolean ret = TRUE;
2927 g_return_val_if_fail (GST_VIDEO_INFO_WIDTH (&state->info) != 0, FALSE);
2928 g_return_val_if_fail (GST_VIDEO_INFO_HEIGHT (&state->info) != 0, FALSE);
2930 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
2932 GST_DEBUG_OBJECT (decoder, "output_state par %d/%d fps %d/%d",
2933 state->info.par_n, state->info.par_d,
2934 state->info.fps_n, state->info.fps_d);
2936 if (state->caps == NULL)
2937 state->caps = gst_video_info_to_caps (&state->info);
2939 GST_DEBUG_OBJECT (decoder, "setting caps %" GST_PTR_FORMAT, state->caps);
2941 ret = gst_pad_set_caps (decoder->srcpad, state->caps);
2944 decoder->priv->output_state_changed = FALSE;
2946 /* Negotiate pool */
2947 query = gst_query_new_allocation (state->caps, TRUE);
2949 if (!gst_pad_peer_query (decoder->srcpad, query)) {
2950 GST_DEBUG_OBJECT (decoder, "didn't get downstream ALLOCATION hints");
2953 g_assert (klass->decide_allocation != NULL);
2954 ret = klass->decide_allocation (decoder, query);
2956 GST_DEBUG_OBJECT (decoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
2960 goto no_decide_allocation;
2962 /* we got configuration from our peer or the decide_allocation method,
2964 if (gst_query_get_n_allocation_params (query) > 0) {
2965 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2968 gst_allocation_params_init (¶ms);
2971 if (gst_query_get_n_allocation_pools (query) > 0)
2972 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
2975 gst_object_unref (allocator);
2977 goto no_decide_allocation;
2980 if (decoder->priv->allocator)
2981 gst_object_unref (decoder->priv->allocator);
2982 decoder->priv->allocator = allocator;
2983 decoder->priv->params = params;
2985 if (decoder->priv->pool) {
2986 gst_buffer_pool_set_active (decoder->priv->pool, FALSE);
2987 gst_object_unref (decoder->priv->pool);
2989 decoder->priv->pool = pool;
2992 gst_buffer_pool_set_active (pool, TRUE);
2996 gst_query_unref (query);
3001 no_decide_allocation:
3003 GST_WARNING_OBJECT (decoder, "Subclass failed to decide allocation");
3009 * gst_video_decoder_negotiate:
3010 * @decoder: a #GstVideoDecoder
3012 * Negotiate with downstreame elements to currently configured #GstVideoCodecState.
3014 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
3017 gst_video_decoder_negotiate (GstVideoDecoder * decoder)
3019 GstVideoDecoderClass *klass;
3020 gboolean ret = TRUE;
3022 g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), FALSE);
3024 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
3026 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3027 if (klass->negotiate)
3028 ret = klass->negotiate (decoder);
3029 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3035 * gst_video_decoder_allocate_output_buffer:
3036 * @decoder: a #GstVideoDecoder
3038 * Helper function that allocates a buffer to hold a video frame for @decoder's
3039 * current #GstVideoCodecState.
3041 * You should use gst_video_decoder_allocate_output_frame() instead of this
3042 * function, if possible at all.
3044 * Returns: (transfer full): allocated buffer, or NULL if no buffer could be
3045 * allocated (e.g. when downstream is flushing or shutting down)
3048 gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder)
3053 GST_DEBUG ("alloc src buffer");
3055 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3056 if (G_UNLIKELY (decoder->priv->output_state_changed
3057 || (decoder->priv->output_state
3058 && gst_pad_check_reconfigure (decoder->srcpad))))
3059 gst_video_decoder_negotiate (decoder);
3061 flow = gst_buffer_pool_acquire_buffer (decoder->priv->pool, &buffer, NULL);
3063 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3065 if (flow != GST_FLOW_OK) {
3066 GST_INFO_OBJECT (decoder, "couldn't allocate output buffer, flow %s",
3067 gst_flow_get_name (flow));
3075 * gst_video_decoder_allocate_output_frame:
3076 * @decoder: a #GstVideoDecoder
3077 * @frame: a #GstVideoCodecFrame
3079 * Helper function that allocates a buffer to hold a video frame for @decoder's
3080 * current #GstVideoCodecState. Subclass should already have configured video
3081 * state and set src pad caps.
3083 * The buffer allocated here is owned by the frame and you should only
3084 * keep references to the frame, not the buffer.
3086 * Returns: %GST_FLOW_OK if an output buffer could be allocated
3089 gst_video_decoder_allocate_output_frame (GstVideoDecoder *
3090 decoder, GstVideoCodecFrame * frame)
3092 GstFlowReturn flow_ret;
3093 GstVideoCodecState *state;
3096 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
3098 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3100 state = decoder->priv->output_state;
3101 if (state == NULL) {
3102 g_warning ("Output state should be set before allocating frame");
3105 num_bytes = GST_VIDEO_INFO_SIZE (&state->info);
3106 if (num_bytes == 0) {
3107 g_warning ("Frame size should not be 0");
3111 if (G_UNLIKELY (decoder->priv->output_state_changed
3112 || (decoder->priv->output_state
3113 && gst_pad_check_reconfigure (decoder->srcpad))))
3114 gst_video_decoder_negotiate (decoder);
3116 GST_LOG_OBJECT (decoder, "alloc buffer size %d", num_bytes);
3118 flow_ret = gst_buffer_pool_acquire_buffer (decoder->priv->pool,
3119 &frame->output_buffer, NULL);
3121 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3126 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3127 return GST_FLOW_ERROR;
3131 * gst_video_decoder_get_max_decode_time:
3132 * @decoder: a #GstVideoDecoder
3133 * @frame: a #GstVideoCodecFrame
3135 * Determines maximum possible decoding time for @frame that will
3136 * allow it to decode and arrive in time (as determined by QoS events).
3137 * In particular, a negative result means decoding in time is no longer possible
3138 * and should therefore occur as soon/skippy as possible.
3140 * Returns: max decoding time.
3143 gst_video_decoder_get_max_decode_time (GstVideoDecoder *
3144 decoder, GstVideoCodecFrame * frame)
3146 GstClockTimeDiff deadline;
3147 GstClockTime earliest_time;
3149 GST_OBJECT_LOCK (decoder);
3150 earliest_time = decoder->priv->earliest_time;
3151 if (GST_CLOCK_TIME_IS_VALID (earliest_time)
3152 && GST_CLOCK_TIME_IS_VALID (frame->deadline))
3153 deadline = GST_CLOCK_DIFF (earliest_time, frame->deadline);
3155 deadline = G_MAXINT64;
3157 GST_LOG_OBJECT (decoder, "earliest %" GST_TIME_FORMAT
3158 ", frame deadline %" GST_TIME_FORMAT ", deadline %" GST_TIME_FORMAT,
3159 GST_TIME_ARGS (earliest_time), GST_TIME_ARGS (frame->deadline),
3160 GST_TIME_ARGS (deadline));
3162 GST_OBJECT_UNLOCK (decoder);
3168 * gst_video_decoder_get_qos_proportion:
3169 * @decoder: a #GstVideoDecoder
3170 * current QoS proportion, or %NULL
3172 * Returns: The current QoS proportion.
3177 gst_video_decoder_get_qos_proportion (GstVideoDecoder * decoder)
3181 g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), 1.0);
3183 GST_OBJECT_LOCK (decoder);
3184 proportion = decoder->priv->proportion;
3185 GST_OBJECT_UNLOCK (decoder);
3191 _gst_video_decoder_error (GstVideoDecoder * dec, gint weight,
3192 GQuark domain, gint code, gchar * txt, gchar * dbg, const gchar * file,
3193 const gchar * function, gint line)
3196 GST_WARNING_OBJECT (dec, "error: %s", txt);
3198 GST_WARNING_OBJECT (dec, "error: %s", dbg);
3199 dec->priv->error_count += weight;
3200 dec->priv->discont = TRUE;
3201 if (dec->priv->max_errors < dec->priv->error_count) {
3202 gst_element_message_full (GST_ELEMENT (dec), GST_MESSAGE_ERROR,
3203 domain, code, txt, dbg, file, function, line);
3204 return GST_FLOW_ERROR;
3213 * gst_video_decoder_set_max_errors:
3214 * @dec: a #GstVideoDecoder
3215 * @num: max tolerated errors
3217 * Sets numbers of tolerated decoder errors, where a tolerated one is then only
3218 * warned about, but more than tolerated will lead to fatal error. Default
3219 * is set to GST_VIDEO_DECODER_MAX_ERRORS.
3222 gst_video_decoder_set_max_errors (GstVideoDecoder * dec, gint num)
3224 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
3226 dec->priv->max_errors = num;
3230 * gst_video_decoder_get_max_errors:
3231 * @dec: a #GstVideoDecoder
3233 * Returns: currently configured decoder tolerated error count.
3236 gst_video_decoder_get_max_errors (GstVideoDecoder * dec)
3238 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
3240 return dec->priv->max_errors;
3244 * gst_video_decoder_set_packetized:
3245 * @decoder: a #GstVideoDecoder
3246 * @packetized: whether the input data should be considered as packetized.
3248 * Allows baseclass to consider input data as packetized or not. If the
3249 * input is packetized, then the @parse method will not be called.
3252 gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
3253 gboolean packetized)
3255 decoder->priv->packetized = packetized;
3259 * gst_video_decoder_get_packetized:
3260 * @decoder: a #GstVideoDecoder
3262 * Queries whether input data is considered packetized or not by the
3265 * Returns: TRUE if input data is considered packetized.
3268 gst_video_decoder_get_packetized (GstVideoDecoder * decoder)
3270 return decoder->priv->packetized;
3274 * gst_video_decoder_set_estimate_rate:
3275 * @dec: a #GstVideoDecoder
3276 * @enabled: whether to enable byte to time conversion
3278 * Allows baseclass to perform byte to time estimated conversion.
3281 gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec, gboolean enabled)
3283 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
3285 dec->priv->do_estimate_rate = enabled;
3289 * gst_video_decoder_get_estimate_rate:
3290 * @dec: a #GstVideoDecoder
3292 * Returns: currently configured byte to time conversion setting
3295 gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec)
3297 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
3299 return dec->priv->do_estimate_rate;
3303 * gst_video_decoder_set_latency:
3304 * @decoder: a #GstVideoDecoder
3305 * @min_latency: minimum latency
3306 * @max_latency: maximum latency
3308 * Lets #GstVideoDecoder sub-classes tell the baseclass what the decoder
3309 * latency is. Will also post a LATENCY message on the bus so the pipeline
3310 * can reconfigure its global latency.
3313 gst_video_decoder_set_latency (GstVideoDecoder * decoder,
3314 GstClockTime min_latency, GstClockTime max_latency)
3316 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3317 g_return_if_fail (max_latency >= min_latency);
3319 GST_OBJECT_LOCK (decoder);
3320 decoder->priv->min_latency = min_latency;
3321 decoder->priv->max_latency = max_latency;
3322 GST_OBJECT_UNLOCK (decoder);
3324 gst_element_post_message (GST_ELEMENT_CAST (decoder),
3325 gst_message_new_latency (GST_OBJECT_CAST (decoder)));
3329 * gst_video_decoder_get_latency:
3330 * @decoder: a #GstVideoDecoder
3331 * @min_latency: (out) (allow-none): address of variable in which to store the
3332 * configured minimum latency, or %NULL
3333 * @max_latency: (out) (allow-none): address of variable in which to store the
3334 * configured mximum latency, or %NULL
3336 * Query the configured decoder latency. Results will be returned via
3337 * @min_latency and @max_latency.
3340 gst_video_decoder_get_latency (GstVideoDecoder * decoder,
3341 GstClockTime * min_latency, GstClockTime * max_latency)
3343 GST_OBJECT_LOCK (decoder);
3345 *min_latency = decoder->priv->min_latency;
3347 *max_latency = decoder->priv->max_latency;
3348 GST_OBJECT_UNLOCK (decoder);
3352 * gst_video_decoder_merge_tags:
3353 * @decoder: a #GstVideoDecoder
3354 * @tags: a #GstTagList to merge
3355 * @mode: the #GstTagMergeMode to use
3357 * Adds tags to so-called pending tags, which will be processed
3358 * before pushing out data downstream.
3360 * Note that this is provided for convenience, and the subclass is
3361 * not required to use this and can still do tag handling on its own.
3366 gst_video_decoder_merge_tags (GstVideoDecoder * decoder,
3367 const GstTagList * tags, GstTagMergeMode mode)
3371 g_return_if_fail (GST_IS_VIDEO_DECODER (decoder));
3372 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
3374 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3376 GST_DEBUG_OBJECT (decoder, "merging tags %" GST_PTR_FORMAT, tags);
3377 otags = decoder->priv->tags;
3378 decoder->priv->tags = gst_tag_list_merge (decoder->priv->tags, tags, mode);
3380 gst_tag_list_unref (otags);
3381 decoder->priv->tags_changed = TRUE;
3382 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3386 * gst_video_decoder_get_buffer_pool:
3387 * @decoder: a #GstVideoDecoder
3389 * Returns: (transfer full): the instance of the #GstBufferPool used
3390 * by the decoder; free it after use it
3393 gst_video_decoder_get_buffer_pool (GstVideoDecoder * decoder)
3395 g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), NULL);
3397 if (decoder->priv->pool)
3398 return gst_object_ref (decoder->priv->pool);
3404 * gst_video_decoder_get_allocator:
3405 * @decoder: a #GstVideoDecoder
3406 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3408 * @params: (out) (allow-none) (transfer full): the
3409 * #GstAllocatorParams of @allocator
3411 * Lets #GstVideoDecoder sub-classes to know the memory @allocator
3412 * used by the base class and its @params.
3414 * Unref the @allocator after use it.
3417 gst_video_decoder_get_allocator (GstVideoDecoder * decoder,
3418 GstAllocator ** allocator, GstAllocationParams * params)
3420 g_return_if_fail (GST_IS_VIDEO_DECODER (decoder));
3423 *allocator = decoder->priv->allocator ?
3424 gst_object_ref (decoder->priv->allocator) : NULL;
3427 *params = decoder->priv->params;