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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, 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/gstvideopool.h>
300 #include <gst/video/gstvideometa.h>
303 GST_DEBUG_CATEGORY (videodecoder_debug);
304 #define GST_CAT_DEFAULT videodecoder_debug
306 #define GST_VIDEO_DECODER_GET_PRIVATE(obj) \
307 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_DECODER, \
308 GstVideoDecoderPrivate))
310 struct _GstVideoDecoderPrivate
312 /* FIXME introduce a context ? */
315 GstAllocator *allocator;
316 GstAllocationParams params;
320 GstAdapter *input_adapter;
321 /* assembles current frame */
322 GstAdapter *output_adapter;
324 /* Whether we attempt to convert newsegment from bytes to
325 * time using a bitrate estimation */
326 gboolean do_estimate_rate;
328 /* Whether input is considered packetized or not */
337 /* ... being tracked here;
338 * only available during parsing */
339 GstVideoCodecFrame *current_frame;
340 /* events that should apply to the current frame */
341 GList *current_frame_events;
343 /* relative offset of input data */
344 guint64 input_offset;
345 /* relative offset of frame */
346 guint64 frame_offset;
347 /* tracking ts and offsets */
350 /* last outgoing ts */
351 GstClockTime last_timestamp_out;
353 /* reverse playback */
358 /* collected parsed frames */
360 /* frames to be handled == decoded */
362 /* collected output - of buffer objects, not frames */
363 GList *output_queued;
366 /* base_picture_number is the picture number of the reference picture */
367 guint64 base_picture_number;
368 /* combine with base_picture_number, framerate and calcs to yield (presentation) ts */
369 GstClockTime base_timestamp;
371 /* FIXME : reorder_depth is never set */
373 int distance_from_sync;
375 guint64 system_frame_number;
376 guint64 decode_frame_number;
378 GList *frames; /* Protected with OBJECT_LOCK */
379 GstVideoCodecState *input_state;
380 GstVideoCodecState *output_state;
381 gboolean output_state_changed;
385 GstClockTime earliest_time;
387 /* qos messages: frames dropped/processed */
391 /* Outgoing byte size ? */
399 static GstElementClass *parent_class = NULL;
400 static void gst_video_decoder_class_init (GstVideoDecoderClass * klass);
401 static void gst_video_decoder_init (GstVideoDecoder * dec,
402 GstVideoDecoderClass * klass);
404 static void gst_video_decoder_finalize (GObject * object);
406 static gboolean gst_video_decoder_setcaps (GstVideoDecoder * dec,
408 static gboolean gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
410 static gboolean gst_video_decoder_src_event (GstPad * pad, GstObject * parent,
412 static GstFlowReturn gst_video_decoder_chain (GstPad * pad, GstObject * parent,
414 static gboolean gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
416 static GstStateChangeReturn gst_video_decoder_change_state (GstElement *
417 element, GstStateChange transition);
418 static gboolean gst_video_decoder_src_query (GstPad * pad, GstObject * parent,
420 static void gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full);
422 static GstFlowReturn gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
423 GstVideoCodecFrame * frame);
425 static void gst_video_decoder_release_frame (GstVideoDecoder * dec,
426 GstVideoCodecFrame * frame);
427 static GstClockTime gst_video_decoder_get_frame_duration (GstVideoDecoder *
428 decoder, GstVideoCodecFrame * frame);
429 static GstVideoCodecFrame *gst_video_decoder_new_frame (GstVideoDecoder *
431 static GstFlowReturn gst_video_decoder_clip_and_push_buf (GstVideoDecoder *
432 decoder, GstBuffer * buf);
433 static GstFlowReturn gst_video_decoder_flush_parse (GstVideoDecoder * dec,
436 static void gst_video_decoder_clear_queues (GstVideoDecoder * dec);
438 static gboolean gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
440 static gboolean gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
442 static gboolean gst_video_decoder_decide_allocation_default (GstVideoDecoder *
443 decoder, GstQuery * query);
444 static gboolean gst_video_decoder_propose_allocation_default (GstVideoDecoder *
445 decoder, GstQuery * query);
447 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
448 * method to get to the padtemplates */
450 gst_video_decoder_get_type (void)
452 static volatile gsize type = 0;
454 if (g_once_init_enter (&type)) {
456 static const GTypeInfo info = {
457 sizeof (GstVideoDecoderClass),
460 (GClassInitFunc) gst_video_decoder_class_init,
463 sizeof (GstVideoDecoder),
465 (GInstanceInitFunc) gst_video_decoder_init,
468 _type = g_type_register_static (GST_TYPE_ELEMENT,
469 "GstVideoDecoder", &info, G_TYPE_FLAG_ABSTRACT);
470 g_once_init_leave (&type, _type);
476 gst_video_decoder_class_init (GstVideoDecoderClass * klass)
478 GObjectClass *gobject_class;
479 GstElementClass *gstelement_class;
481 gobject_class = G_OBJECT_CLASS (klass);
482 gstelement_class = GST_ELEMENT_CLASS (klass);
484 GST_DEBUG_CATEGORY_INIT (videodecoder_debug, "videodecoder", 0,
485 "Base Video Decoder");
487 parent_class = g_type_class_peek_parent (klass);
488 g_type_class_add_private (klass, sizeof (GstVideoDecoderPrivate));
490 gobject_class->finalize = gst_video_decoder_finalize;
492 gstelement_class->change_state =
493 GST_DEBUG_FUNCPTR (gst_video_decoder_change_state);
495 klass->sink_event = gst_video_decoder_sink_event_default;
496 klass->src_event = gst_video_decoder_src_event_default;
497 klass->decide_allocation = gst_video_decoder_decide_allocation_default;
498 klass->propose_allocation = gst_video_decoder_propose_allocation_default;
502 gst_video_decoder_init (GstVideoDecoder * decoder, GstVideoDecoderClass * klass)
504 GstPadTemplate *pad_template;
507 GST_DEBUG_OBJECT (decoder, "gst_video_decoder_init");
509 decoder->priv = GST_VIDEO_DECODER_GET_PRIVATE (decoder);
512 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
513 g_return_if_fail (pad_template != NULL);
515 decoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
517 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_decoder_chain));
518 gst_pad_set_event_function (pad,
519 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_event));
520 gst_pad_set_query_function (pad,
521 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_query));
522 gst_element_add_pad (GST_ELEMENT (decoder), decoder->sinkpad);
525 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
526 g_return_if_fail (pad_template != NULL);
528 decoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
530 gst_pad_set_event_function (pad,
531 GST_DEBUG_FUNCPTR (gst_video_decoder_src_event));
532 gst_pad_set_query_function (pad,
533 GST_DEBUG_FUNCPTR (gst_video_decoder_src_query));
534 gst_pad_use_fixed_caps (pad);
535 gst_element_add_pad (GST_ELEMENT (decoder), decoder->srcpad);
537 gst_segment_init (&decoder->input_segment, GST_FORMAT_TIME);
538 gst_segment_init (&decoder->output_segment, GST_FORMAT_TIME);
540 g_rec_mutex_init (&decoder->stream_lock);
542 decoder->priv->input_adapter = gst_adapter_new ();
543 decoder->priv->output_adapter = gst_adapter_new ();
544 decoder->priv->packetized = TRUE;
546 gst_video_decoder_reset (decoder, TRUE);
550 gst_video_rawvideo_convert (GstVideoCodecState * state,
551 GstFormat src_format, gint64 src_value,
552 GstFormat * dest_format, gint64 * dest_value)
554 gboolean res = FALSE;
558 g_return_val_if_fail (dest_format != NULL, FALSE);
559 g_return_val_if_fail (dest_value != NULL, FALSE);
561 if (src_format == *dest_format || src_value == 0 || src_value == -1) {
562 *dest_value = src_value;
566 vidsize = GST_VIDEO_INFO_SIZE (&state->info);
567 fps_n = GST_VIDEO_INFO_FPS_N (&state->info);
568 fps_d = GST_VIDEO_INFO_FPS_D (&state->info);
570 if (src_format == GST_FORMAT_BYTES &&
571 *dest_format == GST_FORMAT_DEFAULT && vidsize) {
572 /* convert bytes to frames */
573 *dest_value = gst_util_uint64_scale_int (src_value, 1, vidsize);
575 } else if (src_format == GST_FORMAT_DEFAULT &&
576 *dest_format == GST_FORMAT_BYTES && vidsize) {
577 /* convert bytes to frames */
578 *dest_value = src_value * vidsize;
580 } else if (src_format == GST_FORMAT_DEFAULT &&
581 *dest_format == GST_FORMAT_TIME && fps_n) {
582 /* convert frames to time */
583 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND * fps_d, fps_n);
585 } else if (src_format == GST_FORMAT_TIME &&
586 *dest_format == GST_FORMAT_DEFAULT && fps_d) {
587 /* convert time to frames */
588 *dest_value = gst_util_uint64_scale (src_value, fps_n, GST_SECOND * fps_d);
590 } else if (src_format == GST_FORMAT_TIME &&
591 *dest_format == GST_FORMAT_BYTES && fps_d && vidsize) {
592 /* convert time to frames */
593 *dest_value = gst_util_uint64_scale (src_value,
594 fps_n * vidsize, GST_SECOND * fps_d);
596 } else if (src_format == GST_FORMAT_BYTES &&
597 *dest_format == GST_FORMAT_TIME && fps_n && vidsize) {
598 /* convert frames to time */
599 *dest_value = gst_util_uint64_scale (src_value,
600 GST_SECOND * fps_d, fps_n * vidsize);
608 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
609 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
612 gboolean res = FALSE;
614 g_return_val_if_fail (dest_format != NULL, FALSE);
615 g_return_val_if_fail (dest_value != NULL, FALSE);
617 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
620 *dest_value = src_value;
624 if (bytes <= 0 || time <= 0) {
625 GST_DEBUG ("not enough metadata yet to convert");
629 switch (src_format) {
630 case GST_FORMAT_BYTES:
631 switch (*dest_format) {
632 case GST_FORMAT_TIME:
633 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
640 case GST_FORMAT_TIME:
641 switch (*dest_format) {
642 case GST_FORMAT_BYTES:
643 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
651 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
660 static GstVideoCodecState *
661 _new_input_state (GstCaps * caps)
663 GstVideoCodecState *state;
664 GstStructure *structure;
665 const GValue *codec_data;
667 state = g_slice_new0 (GstVideoCodecState);
668 state->ref_count = 1;
669 gst_video_info_init (&state->info);
670 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
672 state->caps = gst_caps_ref (caps);
674 structure = gst_caps_get_structure (caps, 0);
676 codec_data = gst_structure_get_value (structure, "codec_data");
677 if (codec_data && G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER)
678 state->codec_data = GST_BUFFER (g_value_dup_boxed (codec_data));
684 g_slice_free (GstVideoCodecState, state);
689 static GstVideoCodecState *
690 _new_output_state (GstVideoFormat fmt, guint width, guint height,
691 GstVideoCodecState * reference)
693 GstVideoCodecState *state;
695 state = g_slice_new0 (GstVideoCodecState);
696 state->ref_count = 1;
697 gst_video_info_init (&state->info);
698 gst_video_info_set_format (&state->info, fmt, width, height);
701 GstVideoInfo *tgt, *ref;
704 ref = &reference->info;
706 /* Copy over extra fields from reference state */
707 tgt->interlace_mode = ref->interlace_mode;
708 tgt->flags = ref->flags;
709 tgt->chroma_site = ref->chroma_site;
710 /* only copy values that are not unknown so that we don't override the
711 * defaults. subclasses should really fill these in when they know. */
712 if (ref->colorimetry.range)
713 tgt->colorimetry.range = ref->colorimetry.range;
714 if (ref->colorimetry.matrix)
715 tgt->colorimetry.matrix = ref->colorimetry.matrix;
716 if (ref->colorimetry.transfer)
717 tgt->colorimetry.transfer = ref->colorimetry.transfer;
718 if (ref->colorimetry.primaries)
719 tgt->colorimetry.primaries = ref->colorimetry.primaries;
720 GST_DEBUG ("reference par %d/%d fps %d/%d",
721 ref->par_n, ref->par_d, ref->fps_n, ref->fps_d);
722 tgt->par_n = ref->par_n;
723 tgt->par_d = ref->par_d;
724 tgt->fps_n = ref->fps_n;
725 tgt->fps_d = ref->fps_d;
728 GST_DEBUG ("reference par %d/%d fps %d/%d",
729 state->info.par_n, state->info.par_d,
730 state->info.fps_n, state->info.fps_d);
736 gst_video_decoder_setcaps (GstVideoDecoder * decoder, GstCaps * caps)
738 GstVideoDecoderClass *decoder_class;
739 GstVideoCodecState *state;
742 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
744 GST_DEBUG_OBJECT (decoder, "setcaps %" GST_PTR_FORMAT, caps);
746 state = _new_input_state (caps);
748 if (G_UNLIKELY (state == NULL))
751 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
753 if (decoder_class->set_format)
754 ret = decoder_class->set_format (decoder, state);
759 if (decoder->priv->input_state)
760 gst_video_codec_state_unref (decoder->priv->input_state);
761 decoder->priv->input_state = state;
763 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
771 GST_WARNING_OBJECT (decoder, "Failed to parse caps");
777 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
778 GST_WARNING_OBJECT (decoder, "Subclass refused caps");
779 gst_video_codec_state_unref (state);
785 gst_video_decoder_finalize (GObject * object)
787 GstVideoDecoder *decoder;
789 decoder = GST_VIDEO_DECODER (object);
791 GST_DEBUG_OBJECT (object, "finalize");
793 g_rec_mutex_clear (&decoder->stream_lock);
795 if (decoder->priv->input_adapter) {
796 g_object_unref (decoder->priv->input_adapter);
797 decoder->priv->input_adapter = NULL;
799 if (decoder->priv->output_adapter) {
800 g_object_unref (decoder->priv->output_adapter);
801 decoder->priv->output_adapter = NULL;
804 if (decoder->priv->input_state)
805 gst_video_codec_state_unref (decoder->priv->input_state);
806 if (decoder->priv->output_state)
807 gst_video_codec_state_unref (decoder->priv->output_state);
809 if (decoder->priv->pool) {
810 gst_object_unref (decoder->priv->pool);
811 decoder->priv->pool = NULL;
814 if (decoder->priv->allocator) {
815 gst_object_unref (decoder->priv->allocator);
816 decoder->priv->allocator = NULL;
819 G_OBJECT_CLASS (parent_class)->finalize (object);
822 /* hard == FLUSH, otherwise discont */
824 gst_video_decoder_flush (GstVideoDecoder * dec, gboolean hard)
826 GstVideoDecoderClass *klass;
827 GstVideoDecoderPrivate *priv = dec->priv;
828 GstFlowReturn ret = GST_FLOW_OK;
830 klass = GST_VIDEO_DECODER_GET_CLASS (dec);
832 GST_LOG_OBJECT (dec, "flush hard %d", hard);
834 /* Inform subclass */
836 klass->reset (dec, hard);
838 /* FIXME make some more distinction between hard and soft,
839 * but subclass may not be prepared for that */
840 /* FIXME perhaps also clear pending frames ?,
841 * but again, subclass may still come up with one of those */
843 /* TODO ? finish/drain some stuff */
845 gst_segment_init (&dec->input_segment, GST_FORMAT_UNDEFINED);
846 gst_segment_init (&dec->output_segment, GST_FORMAT_UNDEFINED);
847 gst_video_decoder_clear_queues (dec);
848 priv->error_count = 0;
849 g_list_free_full (priv->current_frame_events,
850 (GDestroyNotify) gst_event_unref);
851 priv->current_frame_events = NULL;
853 /* and get (re)set for the sequel */
854 gst_video_decoder_reset (dec, FALSE);
860 gst_video_decoder_push_event (GstVideoDecoder * decoder, GstEvent * event)
862 switch (GST_EVENT_TYPE (event)) {
863 case GST_EVENT_SEGMENT:
867 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
869 gst_event_copy_segment (event, &segment);
871 GST_DEBUG_OBJECT (decoder, "segment %" GST_SEGMENT_FORMAT, &segment);
873 if (segment.format != GST_FORMAT_TIME) {
874 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
875 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
879 decoder->output_segment = segment;
880 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
887 return gst_pad_push_event (decoder->srcpad, event);
891 gst_video_decoder_handle_eos (GstVideoDecoder * dec)
893 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
894 GstVideoDecoderPrivate *priv = dec->priv;
895 GstFlowReturn ret = GST_FLOW_OK;
897 GST_VIDEO_DECODER_STREAM_LOCK (dec);
899 if (dec->input_segment.rate > 0.0) {
900 /* Forward mode, if unpacketized, give the child class
901 * a final chance to flush out packets */
902 if (!priv->packetized) {
903 if (priv->current_frame == NULL)
904 priv->current_frame = gst_video_decoder_new_frame (dec);
906 while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter))
907 ret = decoder_class->parse (dec, priv->current_frame,
908 priv->input_adapter, TRUE);
911 /* Reverse playback mode */
912 ret = gst_video_decoder_flush_parse (dec, TRUE);
915 if (decoder_class->finish)
916 ret = decoder_class->finish (dec);
918 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
924 gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
927 GstVideoDecoderPrivate *priv;
928 gboolean ret = FALSE;
930 priv = decoder->priv;
932 switch (GST_EVENT_TYPE (event)) {
937 gst_event_parse_caps (event, &caps);
939 decoder->priv->do_caps = TRUE;
940 gst_event_unref (event);
946 GstFlowReturn flow_ret = GST_FLOW_OK;
948 flow_ret = gst_video_decoder_handle_eos (decoder);
949 ret = (flow_ret == GST_FLOW_OK);
953 case GST_EVENT_SEGMENT:
957 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
959 gst_event_copy_segment (event, &segment);
961 if (segment.format == GST_FORMAT_TIME) {
962 GST_DEBUG_OBJECT (decoder,
963 "received TIME SEGMENT %" GST_SEGMENT_FORMAT, &segment);
967 GST_DEBUG_OBJECT (decoder,
968 "received SEGMENT %" GST_SEGMENT_FORMAT, &segment);
970 /* handle newsegment as a result from our legacy simple seeking */
971 /* note that initial 0 should convert to 0 in any case */
972 if (priv->do_estimate_rate &&
973 gst_pad_query_convert (decoder->sinkpad, GST_FORMAT_BYTES,
974 segment.start, GST_FORMAT_TIME, &start)) {
975 /* best attempt convert */
976 /* as these are only estimates, stop is kept open-ended to avoid
977 * premature cutting */
978 GST_DEBUG_OBJECT (decoder,
979 "converted to TIME start %" GST_TIME_FORMAT,
980 GST_TIME_ARGS (start));
981 segment.start = start;
982 segment.stop = GST_CLOCK_TIME_NONE;
983 segment.time = start;
985 gst_event_unref (event);
986 event = gst_event_new_segment (&segment);
988 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
989 goto newseg_wrong_format;
993 gst_video_decoder_flush (decoder, FALSE);
995 priv->base_timestamp = GST_CLOCK_TIME_NONE;
996 priv->base_picture_number = 0;
998 decoder->input_segment = segment;
1000 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1003 case GST_EVENT_FLUSH_STOP:
1005 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1006 /* well, this is kind of worse than a DISCONT */
1007 gst_video_decoder_flush (decoder, TRUE);
1008 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1014 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1015 * For EOS this is required because no buffer or serialized event
1016 * will come after EOS and nothing could trigger another
1017 * _finish_frame() call. *
1018 * If the subclass handles sending of EOS manually it can return
1019 * _DROPPED from ::finish() and all other subclasses should have
1020 * decoded/flushed all remaining data before this
1022 * For FLUSH_STOP this is required because it is expected
1023 * to be forwarded immediately and no buffers are queued anyway.
1026 if (!GST_EVENT_IS_SERIALIZED (event)
1027 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1028 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1029 ret = gst_video_decoder_push_event (decoder, event);
1031 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1032 decoder->priv->current_frame_events =
1033 g_list_prepend (decoder->priv->current_frame_events, event);
1034 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1041 newseg_wrong_format:
1043 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
1044 gst_event_unref (event);
1051 gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
1054 GstVideoDecoder *decoder;
1055 GstVideoDecoderClass *decoder_class;
1056 gboolean ret = FALSE;
1058 decoder = GST_VIDEO_DECODER (parent);
1059 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1061 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1062 GST_EVENT_TYPE_NAME (event));
1064 if (decoder_class->sink_event)
1065 ret = decoder_class->sink_event (decoder, event);
1070 /* perform upstream byte <-> time conversion (duration, seeking)
1071 * if subclass allows and if enough data for moderately decent conversion */
1072 static inline gboolean
1073 gst_video_decoder_do_byte (GstVideoDecoder * dec)
1075 return dec->priv->do_estimate_rate && (dec->priv->bytes_out > 0)
1076 && (dec->priv->time > GST_SECOND);
1080 gst_video_decoder_do_seek (GstVideoDecoder * dec, GstEvent * event)
1084 GstSeekType start_type, end_type;
1086 gint64 start, start_time, end_time;
1087 GstSegment seek_segment;
1090 gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
1091 &start_time, &end_type, &end_time);
1093 /* we'll handle plain open-ended flushing seeks with the simple approach */
1095 GST_DEBUG_OBJECT (dec, "unsupported seek: rate");
1099 if (start_type != GST_SEEK_TYPE_SET) {
1100 GST_DEBUG_OBJECT (dec, "unsupported seek: start time");
1104 if (end_type != GST_SEEK_TYPE_NONE ||
1105 (end_type == GST_SEEK_TYPE_SET && end_time != GST_CLOCK_TIME_NONE)) {
1106 GST_DEBUG_OBJECT (dec, "unsupported seek: end time");
1110 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1111 GST_DEBUG_OBJECT (dec, "unsupported seek: not flushing");
1115 memcpy (&seek_segment, &dec->output_segment, sizeof (seek_segment));
1116 gst_segment_do_seek (&seek_segment, rate, format, flags, start_type,
1117 start_time, end_type, end_time, NULL);
1118 start_time = seek_segment.position;
1120 if (!gst_pad_query_convert (dec->sinkpad, GST_FORMAT_TIME, start_time,
1121 GST_FORMAT_BYTES, &start)) {
1122 GST_DEBUG_OBJECT (dec, "conversion failed");
1126 seqnum = gst_event_get_seqnum (event);
1127 event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
1128 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
1129 gst_event_set_seqnum (event, seqnum);
1131 GST_DEBUG_OBJECT (dec, "seeking to %" GST_TIME_FORMAT " at byte offset %"
1132 G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
1134 return gst_pad_push_event (dec->sinkpad, event);
1138 gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
1141 GstVideoDecoderPrivate *priv;
1142 gboolean res = FALSE;
1144 priv = decoder->priv;
1146 GST_DEBUG_OBJECT (decoder,
1147 "received event %d, %s", GST_EVENT_TYPE (event),
1148 GST_EVENT_TYPE_NAME (event));
1150 switch (GST_EVENT_TYPE (event)) {
1151 case GST_EVENT_SEEK:
1156 GstSeekType cur_type, stop_type;
1161 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
1163 seqnum = gst_event_get_seqnum (event);
1165 /* upstream gets a chance first */
1166 if ((res = gst_pad_push_event (decoder->sinkpad, event)))
1169 /* if upstream fails for a time seek, maybe we can help if allowed */
1170 if (format == GST_FORMAT_TIME) {
1171 if (gst_video_decoder_do_byte (decoder))
1172 res = gst_video_decoder_do_seek (decoder, event);
1176 /* ... though a non-time seek can be aided as well */
1177 /* First bring the requested format to time */
1179 gst_pad_query_convert (decoder->srcpad, format, cur,
1180 GST_FORMAT_TIME, &tcur)))
1183 gst_pad_query_convert (decoder->srcpad, format, stop,
1184 GST_FORMAT_TIME, &tstop)))
1187 /* then seek with time on the peer */
1188 event = gst_event_new_seek (rate, GST_FORMAT_TIME,
1189 flags, cur_type, tcur, stop_type, tstop);
1190 gst_event_set_seqnum (event, seqnum);
1192 res = gst_pad_push_event (decoder->sinkpad, event);
1199 GstClockTimeDiff diff;
1200 GstClockTime timestamp;
1201 GstClockTime duration;
1203 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1205 GST_OBJECT_LOCK (decoder);
1206 priv->proportion = proportion;
1207 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) {
1208 if (G_UNLIKELY (diff > 0)) {
1209 if (priv->output_state->info.fps_n > 0)
1211 gst_util_uint64_scale (GST_SECOND,
1212 priv->output_state->info.fps_d, priv->output_state->info.fps_n);
1215 priv->earliest_time = timestamp + 2 * diff + duration;
1217 priv->earliest_time = timestamp + diff;
1220 priv->earliest_time = GST_CLOCK_TIME_NONE;
1222 GST_OBJECT_UNLOCK (decoder);
1224 GST_DEBUG_OBJECT (decoder,
1225 "got QoS %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT ", %g",
1226 GST_TIME_ARGS (timestamp), diff, proportion);
1228 res = gst_pad_push_event (decoder->sinkpad, event);
1232 res = gst_pad_push_event (decoder->sinkpad, event);
1239 GST_DEBUG_OBJECT (decoder, "could not convert format");
1244 gst_video_decoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1246 GstVideoDecoder *decoder;
1247 GstVideoDecoderClass *decoder_class;
1248 gboolean ret = FALSE;
1250 decoder = GST_VIDEO_DECODER (parent);
1251 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1253 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1254 GST_EVENT_TYPE_NAME (event));
1256 if (decoder_class->src_event)
1257 ret = decoder_class->src_event (decoder, event);
1263 gst_video_decoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1265 GstVideoDecoder *dec;
1266 gboolean res = TRUE;
1268 dec = GST_VIDEO_DECODER (parent);
1270 GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
1272 switch (GST_QUERY_TYPE (query)) {
1273 case GST_QUERY_POSITION:
1278 /* upstream gets a chance first */
1279 if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
1280 GST_LOG_OBJECT (dec, "returning peer response");
1284 /* we start from the last seen time */
1285 time = dec->priv->last_timestamp_out;
1286 /* correct for the segment values */
1287 time = gst_segment_to_stream_time (&dec->output_segment,
1288 GST_FORMAT_TIME, time);
1290 GST_LOG_OBJECT (dec,
1291 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
1293 /* and convert to the final format */
1294 gst_query_parse_position (query, &format, NULL);
1295 if (!(res = gst_pad_query_convert (pad, GST_FORMAT_TIME, time,
1299 gst_query_set_position (query, format, value);
1301 GST_LOG_OBJECT (dec,
1302 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
1306 case GST_QUERY_DURATION:
1310 /* upstream in any case */
1311 if ((res = gst_pad_query_default (pad, parent, query)))
1314 gst_query_parse_duration (query, &format, NULL);
1315 /* try answering TIME by converting from BYTE if subclass allows */
1316 if (format == GST_FORMAT_TIME && gst_video_decoder_do_byte (dec)) {
1319 if (gst_pad_peer_query_duration (dec->sinkpad, GST_FORMAT_BYTES,
1321 GST_LOG_OBJECT (dec, "upstream size %" G_GINT64_FORMAT, value);
1322 if (gst_pad_query_convert (dec->sinkpad,
1323 GST_FORMAT_BYTES, value, GST_FORMAT_TIME, &value)) {
1324 gst_query_set_duration (query, GST_FORMAT_TIME, value);
1331 case GST_QUERY_CONVERT:
1333 GstFormat src_fmt, dest_fmt;
1334 gint64 src_val, dest_val;
1336 GST_DEBUG_OBJECT (dec, "convert query");
1338 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1339 res = gst_video_rawvideo_convert (dec->priv->output_state,
1340 src_fmt, src_val, &dest_fmt, &dest_val);
1343 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1346 case GST_QUERY_LATENCY:
1349 GstClockTime min_latency, max_latency;
1351 res = gst_pad_peer_query (dec->sinkpad, query);
1353 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1354 GST_DEBUG_OBJECT (dec, "Peer latency: live %d, min %"
1355 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1356 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1358 GST_OBJECT_LOCK (dec);
1359 min_latency += dec->priv->min_latency;
1360 if (dec->priv->max_latency == GST_CLOCK_TIME_NONE) {
1361 max_latency = GST_CLOCK_TIME_NONE;
1362 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1363 max_latency += dec->priv->max_latency;
1365 GST_OBJECT_UNLOCK (dec);
1367 gst_query_set_latency (query, live, min_latency, max_latency);
1372 res = gst_pad_query_default (pad, parent, query);
1377 GST_ERROR_OBJECT (dec, "query failed");
1382 gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
1385 GstVideoDecoder *decoder;
1386 GstVideoDecoderPrivate *priv;
1387 gboolean res = FALSE;
1389 decoder = GST_VIDEO_DECODER (parent);
1390 priv = decoder->priv;
1392 GST_LOG_OBJECT (decoder, "handling query: %" GST_PTR_FORMAT, query);
1394 switch (GST_QUERY_TYPE (query)) {
1395 case GST_QUERY_CONVERT:
1397 GstFormat src_fmt, dest_fmt;
1398 gint64 src_val, dest_val;
1400 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1402 gst_video_encoded_video_convert (priv->bytes_out, priv->time, src_fmt,
1403 src_val, &dest_fmt, &dest_val);
1406 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1409 case GST_QUERY_ALLOCATION:{
1410 GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1412 if (klass->propose_allocation)
1413 res = klass->propose_allocation (decoder, query);
1417 res = gst_pad_query_default (pad, parent, query);
1424 GST_DEBUG_OBJECT (decoder, "query failed");
1428 typedef struct _Timestamp Timestamp;
1434 GstClockTime duration;
1438 timestamp_free (Timestamp * ts)
1440 g_slice_free (Timestamp, ts);
1444 gst_video_decoder_add_timestamp (GstVideoDecoder * decoder, GstBuffer * buffer)
1446 GstVideoDecoderPrivate *priv = decoder->priv;
1449 ts = g_slice_new (Timestamp);
1451 GST_LOG_OBJECT (decoder,
1452 "adding PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT
1453 " (offset:%" G_GUINT64_FORMAT ")",
1454 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1455 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), priv->input_offset);
1457 ts->offset = priv->input_offset;
1458 ts->pts = GST_BUFFER_PTS (buffer);
1459 ts->dts = GST_BUFFER_DTS (buffer);
1460 ts->duration = GST_BUFFER_DURATION (buffer);
1462 priv->timestamps = g_list_append (priv->timestamps, ts);
1466 gst_video_decoder_get_timestamp_at_offset (GstVideoDecoder *
1467 decoder, guint64 offset, GstClockTime * pts, GstClockTime * dts,
1468 GstClockTime * duration)
1470 #ifndef GST_DISABLE_GST_DEBUG
1471 guint64 got_offset = 0;
1476 *pts = GST_CLOCK_TIME_NONE;
1477 *dts = GST_CLOCK_TIME_NONE;
1478 *duration = GST_CLOCK_TIME_NONE;
1480 g = decoder->priv->timestamps;
1483 if (ts->offset <= offset) {
1484 #ifndef GST_DISABLE_GST_DEBUG
1485 got_offset = ts->offset;
1489 *duration = ts->duration;
1490 timestamp_free (ts);
1492 decoder->priv->timestamps = g_list_remove (decoder->priv->timestamps, ts);
1498 GST_LOG_OBJECT (decoder,
1499 "got PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT " @ offs %"
1500 G_GUINT64_FORMAT " (wanted offset:%" G_GUINT64_FORMAT ")",
1501 GST_TIME_ARGS (*pts), GST_TIME_ARGS (*dts), got_offset, offset);
1505 gst_video_decoder_clear_queues (GstVideoDecoder * dec)
1507 GstVideoDecoderPrivate *priv = dec->priv;
1509 g_list_free_full (priv->output_queued,
1510 (GDestroyNotify) gst_mini_object_unref);
1511 priv->output_queued = NULL;
1513 g_list_free_full (priv->gather, (GDestroyNotify) gst_mini_object_unref);
1514 priv->gather = NULL;
1515 g_list_free_full (priv->decode, (GDestroyNotify) gst_video_codec_frame_unref);
1516 priv->decode = NULL;
1517 g_list_free_full (priv->parse, (GDestroyNotify) gst_mini_object_unref);
1519 g_list_free_full (priv->parse_gather,
1520 (GDestroyNotify) gst_video_codec_frame_unref);
1521 priv->parse_gather = NULL;
1522 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1523 priv->frames = NULL;
1527 gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full)
1529 GstVideoDecoderPrivate *priv = decoder->priv;
1531 GST_DEBUG_OBJECT (decoder, "reset full %d", full);
1533 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1536 gst_segment_init (&decoder->input_segment, GST_FORMAT_UNDEFINED);
1537 gst_segment_init (&decoder->output_segment, GST_FORMAT_UNDEFINED);
1538 gst_video_decoder_clear_queues (decoder);
1539 priv->error_count = 0;
1540 priv->max_errors = GST_VIDEO_DECODER_MAX_ERRORS;
1541 if (priv->input_state)
1542 gst_video_codec_state_unref (priv->input_state);
1543 priv->input_state = NULL;
1544 if (priv->output_state)
1545 gst_video_codec_state_unref (priv->output_state);
1546 priv->output_state = NULL;
1547 priv->min_latency = 0;
1548 priv->max_latency = 0;
1551 priv->discont = TRUE;
1553 priv->base_timestamp = GST_CLOCK_TIME_NONE;
1554 priv->last_timestamp_out = GST_CLOCK_TIME_NONE;
1556 priv->input_offset = 0;
1557 priv->frame_offset = 0;
1558 gst_adapter_clear (priv->input_adapter);
1559 gst_adapter_clear (priv->output_adapter);
1560 g_list_free_full (priv->timestamps, (GDestroyNotify) timestamp_free);
1561 priv->timestamps = NULL;
1563 if (priv->current_frame) {
1564 gst_video_codec_frame_unref (priv->current_frame);
1565 priv->current_frame = NULL;
1569 priv->processed = 0;
1571 priv->decode_frame_number = 0;
1572 priv->base_picture_number = 0;
1574 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1575 priv->frames = NULL;
1577 priv->bytes_out = 0;
1580 priv->earliest_time = GST_CLOCK_TIME_NONE;
1581 priv->proportion = 0.5;
1583 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1586 static GstFlowReturn
1587 gst_video_decoder_chain_forward (GstVideoDecoder * decoder,
1588 GstBuffer * buf, gboolean at_eos)
1590 GstVideoDecoderPrivate *priv;
1591 GstVideoDecoderClass *klass;
1592 GstFlowReturn ret = GST_FLOW_OK;
1594 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1595 priv = decoder->priv;
1597 g_return_val_if_fail (priv->packetized || klass->parse, GST_FLOW_ERROR);
1599 if (priv->current_frame == NULL)
1600 priv->current_frame = gst_video_decoder_new_frame (decoder);
1602 if (GST_BUFFER_PTS_IS_VALID (buf)) {
1603 gst_video_decoder_add_timestamp (decoder, buf);
1605 priv->input_offset += gst_buffer_get_size (buf);
1607 if (priv->packetized) {
1608 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1609 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (priv->current_frame);
1612 priv->current_frame->input_buffer = buf;
1614 if (decoder->input_segment.rate < 0.0) {
1615 priv->parse_gather =
1616 g_list_prepend (priv->parse_gather, priv->current_frame);
1618 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
1620 priv->current_frame = NULL;
1623 gst_adapter_push (priv->input_adapter, buf);
1625 if (G_UNLIKELY (!gst_adapter_available (priv->input_adapter)))
1629 ret = klass->parse (decoder, priv->current_frame,
1630 priv->input_adapter, at_eos);
1631 } while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter));
1635 if (ret == GST_VIDEO_DECODER_FLOW_NEED_DATA)
1641 static GstFlowReturn
1642 gst_video_decoder_flush_decode (GstVideoDecoder * dec)
1644 GstVideoDecoderPrivate *priv = dec->priv;
1645 GstFlowReturn res = GST_FLOW_OK;
1648 GST_DEBUG_OBJECT (dec, "flushing buffers to decode");
1650 /* clear buffer and decoder state */
1651 gst_video_decoder_flush (dec, FALSE);
1653 walk = priv->decode;
1656 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1657 GstBuffer *buf = frame->input_buffer;
1659 GST_DEBUG_OBJECT (dec, "decoding frame %p buffer %p, PTS %" GST_TIME_FORMAT
1660 ", DTS %" GST_TIME_FORMAT, frame, buf,
1661 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1662 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
1666 priv->decode = g_list_delete_link (priv->decode, walk);
1668 /* decode buffer, resulting data prepended to queue */
1669 res = gst_video_decoder_decode_frame (dec, frame);
1670 if (res != GST_FLOW_OK)
1679 /* gst_video_decoder_flush_parse is called from the
1680 * chain_reverse() function when a buffer containing
1681 * a DISCONT - indicating that reverse playback
1682 * looped back to the next data block, and therefore
1683 * all available data should be fed through the
1684 * decoder and frames gathered for reversed output
1686 static GstFlowReturn
1687 gst_video_decoder_flush_parse (GstVideoDecoder * dec, gboolean at_eos)
1689 GstVideoDecoderPrivate *priv = dec->priv;
1690 GstFlowReturn res = GST_FLOW_OK;
1693 GST_DEBUG_OBJECT (dec, "flushing buffers to parsing");
1695 /* Reverse the gather list, and prepend it to the parse list,
1696 * then flush to parse whatever we can */
1697 priv->gather = g_list_reverse (priv->gather);
1698 priv->parse = g_list_concat (priv->gather, priv->parse);
1699 priv->gather = NULL;
1701 /* clear buffer and decoder state */
1702 gst_video_decoder_flush (dec, FALSE);
1706 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1707 GList *next = walk->next;
1709 GST_DEBUG_OBJECT (dec, "parsing buffer %p, PTS %" GST_TIME_FORMAT
1710 ", DTS %" GST_TIME_FORMAT, buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1711 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
1713 /* parse buffer, resulting frames prepended to parse_gather queue */
1714 gst_buffer_ref (buf);
1715 res = gst_video_decoder_chain_forward (dec, buf, at_eos);
1717 /* if we generated output, we can discard the buffer, else we
1718 * keep it in the queue */
1719 if (priv->parse_gather) {
1720 GST_DEBUG_OBJECT (dec, "parsed buffer to %p", priv->parse_gather->data);
1721 priv->parse = g_list_delete_link (priv->parse, walk);
1722 gst_buffer_unref (buf);
1724 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1729 /* now we can process frames. Start by moving each frame from the parse_gather
1730 * to the decode list, reverse the order as we go, and stopping when/if we
1731 * copy a keyframe. */
1732 GST_DEBUG_OBJECT (dec, "checking parsed frames for a keyframe to decode");
1733 walk = priv->parse_gather;
1735 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1737 /* remove from the gather list */
1738 priv->parse_gather = g_list_remove_link (priv->parse_gather, walk);
1740 /* move it to the front of the decode queue */
1741 priv->decode = g_list_concat (walk, priv->decode);
1743 /* if we copied a keyframe, flush and decode the decode queue */
1744 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1745 GST_DEBUG_OBJECT (dec, "found keyframe %p with PTS %" GST_TIME_FORMAT
1746 ", DTS %" GST_TIME_FORMAT, frame,
1747 GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
1748 GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
1749 res = gst_video_decoder_flush_decode (dec);
1750 if (res != GST_FLOW_OK)
1754 walk = priv->parse_gather;
1757 /* now send queued data downstream */
1758 walk = priv->output_queued;
1760 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1762 if (G_LIKELY (res == GST_FLOW_OK)) {
1763 /* avoid stray DISCONT from forward processing,
1764 * which have no meaning in reverse pushing */
1765 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
1767 /* Last chance to calculate a timestamp as we loop backwards
1768 * through the list */
1769 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE)
1770 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1771 else if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
1772 GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
1773 GST_BUFFER_TIMESTAMP (buf) =
1774 priv->last_timestamp_out - GST_BUFFER_DURATION (buf);
1775 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1776 GST_LOG_OBJECT (dec,
1777 "Calculated TS %" GST_TIME_FORMAT " working backwards",
1778 GST_TIME_ARGS (priv->last_timestamp_out));
1781 res = gst_video_decoder_clip_and_push_buf (dec, buf);
1783 gst_buffer_unref (buf);
1786 priv->output_queued =
1787 g_list_delete_link (priv->output_queued, priv->output_queued);
1788 walk = priv->output_queued;
1795 static GstFlowReturn
1796 gst_video_decoder_chain_reverse (GstVideoDecoder * dec, GstBuffer * buf)
1798 GstVideoDecoderPrivate *priv = dec->priv;
1799 GstFlowReturn result = GST_FLOW_OK;
1801 /* if we have a discont, move buffers to the decode list */
1802 if (!buf || GST_BUFFER_IS_DISCONT (buf)) {
1803 GST_DEBUG_OBJECT (dec, "received discont");
1805 /* parse and decode stuff in the gather and parse queues */
1806 gst_video_decoder_flush_parse (dec, FALSE);
1809 if (G_LIKELY (buf)) {
1810 GST_DEBUG_OBJECT (dec, "gathering buffer %p of size %" G_GSIZE_FORMAT ", "
1811 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
1812 GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1813 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1814 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1815 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1817 /* add buffer to gather queue */
1818 priv->gather = g_list_prepend (priv->gather, buf);
1824 static GstFlowReturn
1825 gst_video_decoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1827 GstVideoDecoder *decoder;
1828 GstFlowReturn ret = GST_FLOW_OK;
1830 decoder = GST_VIDEO_DECODER (parent);
1832 if (G_UNLIKELY (decoder->priv->do_caps)) {
1833 GstCaps *caps = gst_pad_get_current_caps (decoder->sinkpad);
1835 if (!gst_video_decoder_setcaps (decoder, caps)) {
1836 gst_caps_unref (caps);
1837 goto not_negotiated;
1839 gst_caps_unref (caps);
1841 decoder->priv->do_caps = FALSE;
1844 GST_LOG_OBJECT (decoder,
1845 "chain PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT " duration %"
1846 GST_TIME_FORMAT " size %" G_GSIZE_FORMAT,
1847 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1848 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1849 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), gst_buffer_get_size (buf));
1851 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1854 * requiring the pad to be negotiated makes it impossible to use
1855 * oggdemux or filesrc ! decoder */
1857 if (decoder->input_segment.format == GST_FORMAT_UNDEFINED) {
1859 GstSegment *segment = &decoder->input_segment;
1861 GST_WARNING_OBJECT (decoder,
1862 "Received buffer without a new-segment. "
1863 "Assuming timestamps start from 0.");
1865 gst_segment_init (segment, GST_FORMAT_TIME);
1867 event = gst_event_new_segment (segment);
1869 decoder->priv->current_frame_events =
1870 g_list_prepend (decoder->priv->current_frame_events, event);
1873 if (decoder->input_segment.rate > 0.0)
1874 ret = gst_video_decoder_chain_forward (decoder, buf, FALSE);
1876 ret = gst_video_decoder_chain_reverse (decoder, buf);
1878 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1884 GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION, (NULL),
1885 ("encoder not initialized"));
1886 gst_buffer_unref (buf);
1887 return GST_FLOW_NOT_NEGOTIATED;
1891 static GstStateChangeReturn
1892 gst_video_decoder_change_state (GstElement * element, GstStateChange transition)
1894 GstVideoDecoder *decoder;
1895 GstVideoDecoderClass *decoder_class;
1896 GstStateChangeReturn ret;
1898 decoder = GST_VIDEO_DECODER (element);
1899 decoder_class = GST_VIDEO_DECODER_GET_CLASS (element);
1901 switch (transition) {
1902 case GST_STATE_CHANGE_NULL_TO_READY:
1903 /* open device/library if needed */
1904 if (decoder_class->open && !decoder_class->open (decoder))
1907 case GST_STATE_CHANGE_READY_TO_PAUSED:
1908 /* Initialize device/library if needed */
1909 if (decoder_class->start && !decoder_class->start (decoder))
1916 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1918 switch (transition) {
1919 case GST_STATE_CHANGE_PAUSED_TO_READY:
1920 if (decoder_class->stop && !decoder_class->stop (decoder))
1923 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1924 gst_video_decoder_reset (decoder, TRUE);
1925 g_list_free_full (decoder->priv->current_frame_events,
1926 (GDestroyNotify) gst_event_unref);
1927 decoder->priv->current_frame_events = NULL;
1928 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1930 case GST_STATE_CHANGE_READY_TO_NULL:
1931 /* close device/library if needed */
1932 if (decoder_class->close && !decoder_class->close (decoder))
1944 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1945 ("Failed to open decoder"));
1946 return GST_STATE_CHANGE_FAILURE;
1951 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1952 ("Failed to start decoder"));
1953 return GST_STATE_CHANGE_FAILURE;
1958 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1959 ("Failed to stop decoder"));
1960 return GST_STATE_CHANGE_FAILURE;
1965 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1966 ("Failed to close decoder"));
1967 return GST_STATE_CHANGE_FAILURE;
1971 static GstVideoCodecFrame *
1972 gst_video_decoder_new_frame (GstVideoDecoder * decoder)
1974 GstVideoDecoderPrivate *priv = decoder->priv;
1975 GstVideoCodecFrame *frame;
1977 frame = g_slice_new0 (GstVideoCodecFrame);
1979 frame->ref_count = 1;
1981 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1982 frame->system_frame_number = priv->system_frame_number;
1983 priv->system_frame_number++;
1984 frame->decode_frame_number = priv->decode_frame_number;
1985 priv->decode_frame_number++;
1987 frame->dts = GST_CLOCK_TIME_NONE;
1988 frame->pts = GST_CLOCK_TIME_NONE;
1989 frame->duration = GST_CLOCK_TIME_NONE;
1990 frame->events = priv->current_frame_events;
1991 priv->current_frame_events = NULL;
1992 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1994 GST_LOG_OBJECT (decoder, "Created new frame %p (sfn:%d)",
1995 frame, frame->system_frame_number);
2001 gst_video_decoder_prepare_finish_frame (GstVideoDecoder *
2002 decoder, GstVideoCodecFrame * frame, gboolean dropping)
2004 GstVideoDecoderPrivate *priv = decoder->priv;
2005 GList *l, *events = NULL;
2007 #ifndef GST_DISABLE_GST_DEBUG
2008 GST_LOG_OBJECT (decoder, "n %d in %" G_GSIZE_FORMAT " out %" G_GSIZE_FORMAT,
2009 g_list_length (priv->frames),
2010 gst_adapter_available (priv->input_adapter),
2011 gst_adapter_available (priv->output_adapter));
2014 GST_LOG_OBJECT (decoder,
2015 "finish frame %p (#%d) sync:%d PTS:%" GST_TIME_FORMAT " DTS:%"
2017 frame, frame->system_frame_number,
2018 GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame), GST_TIME_ARGS (frame->pts),
2019 GST_TIME_ARGS (frame->dts));
2021 /* Push all pending events that arrived before this frame */
2022 for (l = priv->frames; l; l = l->next) {
2023 GstVideoCodecFrame *tmp = l->data;
2026 events = g_list_concat (events, tmp->events);
2034 for (l = g_list_last (events); l; l = g_list_previous (l)) {
2035 GST_LOG_OBJECT (decoder, "pushing %s event", GST_EVENT_TYPE_NAME (l->data));
2036 gst_video_decoder_push_event (decoder, l->data);
2038 g_list_free (events);
2040 /* Check if the data should not be displayed. For example altref/invisible
2041 * frame in vp8. In this case we should not update the timestamps. */
2042 if (GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame))
2045 /* If the frame is meant to be output but we don't have an output_buffer
2046 * we have a problem :) */
2047 if (G_UNLIKELY ((frame->output_buffer == NULL) && !dropping))
2048 goto no_output_buffer;
2050 if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
2051 if (frame->pts != priv->base_timestamp) {
2052 GST_DEBUG_OBJECT (decoder,
2053 "sync timestamp %" GST_TIME_FORMAT " diff %" GST_TIME_FORMAT,
2054 GST_TIME_ARGS (frame->pts),
2055 GST_TIME_ARGS (frame->pts - decoder->output_segment.start));
2056 priv->base_timestamp = frame->pts;
2057 priv->base_picture_number = frame->decode_frame_number;
2061 if (frame->duration == GST_CLOCK_TIME_NONE) {
2062 frame->duration = gst_video_decoder_get_frame_duration (decoder, frame);
2063 GST_LOG_OBJECT (decoder,
2064 "Guessing duration %" GST_TIME_FORMAT " for frame...",
2065 GST_TIME_ARGS (frame->duration));
2068 if (frame->pts == GST_CLOCK_TIME_NONE) {
2069 /* Last ditch timestamp guess: Just add the duration to the previous
2071 if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
2072 frame->duration != GST_CLOCK_TIME_NONE) {
2073 frame->pts = priv->last_timestamp_out + frame->duration;
2074 GST_LOG_OBJECT (decoder,
2075 "Guessing timestamp %" GST_TIME_FORMAT " for frame...",
2076 GST_TIME_ARGS (frame->pts));
2080 if (GST_CLOCK_TIME_IS_VALID (priv->last_timestamp_out)) {
2081 if (frame->pts < priv->last_timestamp_out) {
2082 GST_WARNING_OBJECT (decoder,
2083 "decreasing timestamp (%" GST_TIME_FORMAT " < %"
2084 GST_TIME_FORMAT ")",
2085 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (priv->last_timestamp_out));
2089 if (GST_CLOCK_TIME_IS_VALID (frame->pts))
2090 priv->last_timestamp_out = frame->pts;
2097 GST_ERROR_OBJECT (decoder, "No buffer to output !");
2102 gst_video_decoder_release_frame (GstVideoDecoder * dec,
2103 GstVideoCodecFrame * frame)
2107 /* unref once from the list */
2108 link = g_list_find (dec->priv->frames, frame);
2110 gst_video_codec_frame_unref (frame);
2111 dec->priv->frames = g_list_delete_link (dec->priv->frames, link);
2114 /* unref because this function takes ownership */
2115 gst_video_codec_frame_unref (frame);
2119 * gst_video_decoder_drop_frame:
2120 * @dec: a #GstVideoDecoder
2121 * @frame: (transfer full): the #GstVideoCodecFrame to drop
2123 * Similar to gst_video_decoder_finish_frame(), but drops @frame in any
2124 * case and posts a QoS message with the frame's details on the bus.
2125 * In any case, the frame is considered finished and released.
2127 * Returns: a #GstFlowReturn, usually GST_FLOW_OK.
2130 gst_video_decoder_drop_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame)
2132 GstClockTime stream_time, jitter, earliest_time, qostime, timestamp;
2133 GstSegment *segment;
2134 GstMessage *qos_msg;
2137 GST_LOG_OBJECT (dec, "drop frame %p", frame);
2139 GST_VIDEO_DECODER_STREAM_LOCK (dec);
2141 gst_video_decoder_prepare_finish_frame (dec, frame, TRUE);
2143 GST_DEBUG_OBJECT (dec, "dropping frame %" GST_TIME_FORMAT,
2144 GST_TIME_ARGS (frame->pts));
2146 dec->priv->dropped++;
2148 /* post QoS message */
2149 timestamp = frame->pts;
2150 proportion = dec->priv->proportion;
2151 segment = &dec->output_segment;
2153 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, timestamp);
2154 qostime = gst_segment_to_running_time (segment, GST_FORMAT_TIME, timestamp);
2155 earliest_time = dec->priv->earliest_time;
2156 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
2158 gst_message_new_qos (GST_OBJECT_CAST (dec), FALSE, qostime, stream_time,
2159 timestamp, GST_CLOCK_TIME_NONE);
2160 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2161 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2162 dec->priv->processed, dec->priv->dropped);
2163 gst_element_post_message (GST_ELEMENT_CAST (dec), qos_msg);
2165 /* now free the frame */
2166 gst_video_decoder_release_frame (dec, frame);
2168 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
2174 * gst_video_decoder_finish_frame:
2175 * @decoder: a #GstVideoDecoder
2176 * @frame: (transfer full): a decoded #GstVideoCodecFrame
2178 * @frame should have a valid decoded data buffer, whose metadata fields
2179 * are then appropriately set according to frame data and pushed downstream.
2180 * If no output data is provided, @frame is considered skipped.
2181 * In any case, the frame is considered finished and released.
2183 * After calling this function the output buffer of the frame is to be
2184 * considered read-only. This function will also change the metadata
2187 * Returns: a #GstFlowReturn resulting from sending data downstream
2190 gst_video_decoder_finish_frame (GstVideoDecoder * decoder,
2191 GstVideoCodecFrame * frame)
2193 GstFlowReturn ret = GST_FLOW_OK;
2194 GstVideoDecoderPrivate *priv = decoder->priv;
2195 GstBuffer *output_buffer;
2197 GST_LOG_OBJECT (decoder, "finish frame %p", frame);
2199 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2201 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
2202 && gst_pad_check_reconfigure (decoder->srcpad))))
2203 gst_video_decoder_negotiate (decoder);
2205 gst_video_decoder_prepare_finish_frame (decoder, frame, FALSE);
2208 /* no buffer data means this frame is skipped */
2209 if (!frame->output_buffer || GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame)) {
2210 GST_DEBUG_OBJECT (decoder, "skipping frame %" GST_TIME_FORMAT,
2211 GST_TIME_ARGS (frame->pts));
2215 output_buffer = frame->output_buffer;
2217 GST_BUFFER_FLAG_UNSET (output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
2219 /* set PTS and DTS to both the PTS for decoded frames */
2220 GST_BUFFER_PTS (output_buffer) = frame->pts;
2221 GST_BUFFER_DTS (output_buffer) = frame->pts;
2222 GST_BUFFER_DURATION (output_buffer) = frame->duration;
2224 GST_BUFFER_OFFSET (output_buffer) = GST_BUFFER_OFFSET_NONE;
2225 GST_BUFFER_OFFSET_END (output_buffer) = GST_BUFFER_OFFSET_NONE;
2227 if (priv->discont) {
2228 GST_BUFFER_FLAG_SET (output_buffer, GST_BUFFER_FLAG_DISCONT);
2229 priv->discont = FALSE;
2232 /* Get an additional ref to the buffer, which is going to be pushed
2233 * downstream, the original ref is owned by the frame
2235 * FIXME: clip_and_push_buf() changes buffer metadata but the buffer
2236 * might have a refcount > 1 */
2237 output_buffer = gst_buffer_ref (output_buffer);
2238 if (decoder->output_segment.rate < 0.0) {
2239 GST_LOG_OBJECT (decoder, "queued frame");
2240 priv->output_queued = g_list_prepend (priv->output_queued, output_buffer);
2242 ret = gst_video_decoder_clip_and_push_buf (decoder, output_buffer);
2246 gst_video_decoder_release_frame (decoder, frame);
2247 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2252 /* With stream lock, takes the frame reference */
2253 static GstFlowReturn
2254 gst_video_decoder_clip_and_push_buf (GstVideoDecoder * decoder, GstBuffer * buf)
2256 GstFlowReturn ret = GST_FLOW_OK;
2257 GstVideoDecoderPrivate *priv = decoder->priv;
2258 guint64 start, stop;
2259 guint64 cstart, cstop;
2260 GstSegment *segment;
2261 GstClockTime duration;
2263 /* Check for clipping */
2264 start = GST_BUFFER_PTS (buf);
2265 duration = GST_BUFFER_DURATION (buf);
2267 stop = GST_CLOCK_TIME_NONE;
2269 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (duration)) {
2270 stop = start + duration;
2273 segment = &decoder->output_segment;
2274 if (gst_segment_clip (segment, GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
2276 GST_BUFFER_PTS (buf) = cstart;
2278 if (stop != GST_CLOCK_TIME_NONE)
2279 GST_BUFFER_DURATION (buf) = cstop - cstart;
2281 GST_LOG_OBJECT (decoder,
2282 "accepting buffer inside segment: %" GST_TIME_FORMAT " %"
2283 GST_TIME_FORMAT " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2284 " time %" GST_TIME_FORMAT,
2285 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2286 GST_TIME_ARGS (GST_BUFFER_PTS (buf) +
2287 GST_BUFFER_DURATION (buf)),
2288 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
2289 GST_TIME_ARGS (segment->time));
2291 GST_LOG_OBJECT (decoder,
2292 "dropping buffer outside segment: %" GST_TIME_FORMAT
2293 " %" GST_TIME_FORMAT
2294 " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2295 " time %" GST_TIME_FORMAT,
2296 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2297 GST_TIME_ARGS (segment->start),
2298 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time));
2299 gst_buffer_unref (buf);
2303 /* update rate estimate */
2304 priv->bytes_out += gst_buffer_get_size (buf);
2305 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2306 priv->time += duration;
2308 /* FIXME : Use difference between current and previous outgoing
2309 * timestamp, and relate to difference between current and previous
2311 /* better none than nothing valid */
2312 priv->time = GST_CLOCK_TIME_NONE;
2315 GST_DEBUG_OBJECT (decoder, "pushing buffer %p of size %" G_GSIZE_FORMAT ", "
2316 "PTS %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
2317 gst_buffer_get_size (buf),
2318 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2319 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2321 /* we got data, so note things are looking up again, reduce
2322 * the error count, if there is one */
2323 if (G_UNLIKELY (priv->error_count))
2324 priv->error_count--;
2326 ret = gst_pad_push (decoder->srcpad, buf);
2333 * gst_video_decoder_add_to_frame:
2334 * @decoder: a #GstVideoDecoder
2335 * @n_bytes: the number of bytes to add
2337 * Removes next @n_bytes of input data and adds it to currently parsed frame.
2340 gst_video_decoder_add_to_frame (GstVideoDecoder * decoder, int n_bytes)
2342 GstVideoDecoderPrivate *priv = decoder->priv;
2345 GST_LOG_OBJECT (decoder, "add %d bytes to frame", n_bytes);
2350 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2351 if (gst_adapter_available (priv->output_adapter) == 0) {
2352 priv->frame_offset =
2353 priv->input_offset - gst_adapter_available (priv->input_adapter);
2355 buf = gst_adapter_take_buffer (priv->input_adapter, n_bytes);
2357 gst_adapter_push (priv->output_adapter, buf);
2358 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2362 gst_video_decoder_get_frame_duration (GstVideoDecoder * decoder,
2363 GstVideoCodecFrame * frame)
2365 GstVideoCodecState *state = decoder->priv->output_state;
2367 /* it's possible that we don't have a state yet when we are dropping the
2368 * initial buffers */
2370 return GST_CLOCK_TIME_NONE;
2372 if (state->info.fps_d == 0 || state->info.fps_n == 0) {
2373 return GST_CLOCK_TIME_NONE;
2376 /* FIXME: For interlaced frames this needs to take into account
2377 * the number of valid fields in the frame
2380 return gst_util_uint64_scale (GST_SECOND, state->info.fps_d,
2385 * gst_video_decoder_have_frame:
2386 * @decoder: a #GstVideoDecoder
2388 * Gathers all data collected for currently parsed frame, gathers corresponding
2389 * metadata and passes it along for further processing, i.e. @handle_frame.
2391 * Returns: a #GstFlowReturn
2394 gst_video_decoder_have_frame (GstVideoDecoder * decoder)
2396 GstVideoDecoderPrivate *priv = decoder->priv;
2399 GstClockTime pts, dts, duration;
2400 GstFlowReturn ret = GST_FLOW_OK;
2402 GST_LOG_OBJECT (decoder, "have_frame");
2404 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2406 n_available = gst_adapter_available (priv->output_adapter);
2408 buffer = gst_adapter_take_buffer (priv->output_adapter, n_available);
2410 buffer = gst_buffer_new_and_alloc (0);
2413 priv->current_frame->input_buffer = buffer;
2415 gst_video_decoder_get_timestamp_at_offset (decoder,
2416 priv->frame_offset, &pts, &dts, &duration);
2418 GST_BUFFER_PTS (buffer) = pts;
2419 GST_BUFFER_DTS (buffer) = dts;
2420 GST_BUFFER_DURATION (buffer) = duration;
2422 GST_LOG_OBJECT (decoder, "collected frame size %d, "
2423 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
2424 GST_TIME_FORMAT, n_available, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
2425 GST_TIME_ARGS (duration));
2427 /* In reverse playback, just capture and queue frames for later processing */
2428 if (decoder->output_segment.rate < 0.0) {
2429 priv->parse_gather =
2430 g_list_prepend (priv->parse_gather, priv->current_frame);
2432 /* Otherwise, decode the frame, which gives away our ref */
2433 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
2435 /* Current frame is gone now, either way */
2436 priv->current_frame = NULL;
2438 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2443 /* Pass the frame in priv->current_frame through the
2444 * handle_frame() callback for decoding and passing to gvd_finish_frame(),
2445 * or dropping by passing to gvd_drop_frame() */
2446 static GstFlowReturn
2447 gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
2448 GstVideoCodecFrame * frame)
2450 GstVideoDecoderPrivate *priv = decoder->priv;
2451 GstVideoDecoderClass *decoder_class;
2452 GstFlowReturn ret = GST_FLOW_OK;
2454 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
2456 /* FIXME : This should only have to be checked once (either the subclass has an
2457 * implementation, or it doesn't) */
2458 g_return_val_if_fail (decoder_class->handle_frame != NULL, GST_FLOW_ERROR);
2460 frame->distance_from_sync = priv->distance_from_sync;
2461 priv->distance_from_sync++;
2462 frame->pts = GST_BUFFER_PTS (frame->input_buffer);
2463 frame->dts = GST_BUFFER_DTS (frame->input_buffer);
2464 frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
2466 /* For keyframes, PTS = DTS */
2467 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
2468 if (!GST_CLOCK_TIME_IS_VALID (frame->pts))
2469 frame->pts = frame->dts;
2472 GST_LOG_OBJECT (decoder, "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT,
2473 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->dts));
2474 GST_LOG_OBJECT (decoder, "dist %d", frame->distance_from_sync);
2476 gst_video_codec_frame_ref (frame);
2477 priv->frames = g_list_append (priv->frames, frame);
2479 gst_segment_to_running_time (&decoder->input_segment, GST_FORMAT_TIME,
2482 /* do something with frame */
2483 ret = decoder_class->handle_frame (decoder, frame);
2484 if (ret != GST_FLOW_OK)
2485 GST_DEBUG_OBJECT (decoder, "flow error %s", gst_flow_get_name (ret));
2487 /* the frame has either been added to parse_gather or sent to
2488 handle frame so there is no need to unref it */
2494 * gst_video_decoder_get_output_state:
2495 * @decoder: a #GstVideoDecoder
2497 * Get the #GstVideoCodecState currently describing the output stream.
2499 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
2501 GstVideoCodecState *
2502 gst_video_decoder_get_output_state (GstVideoDecoder * decoder)
2504 GstVideoCodecState *state = NULL;
2506 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2507 if (decoder->priv->output_state)
2508 state = gst_video_codec_state_ref (decoder->priv->output_state);
2509 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2515 * gst_video_decoder_set_output_state:
2516 * @decoder: a #GstVideoDecoder
2517 * @fmt: a #GstVideoFormat
2518 * @width: The width in pixels
2519 * @height: The height in pixels
2520 * @reference: (allow-none) (transfer none): An optional reference #GstVideoCodecState
2522 * Creates a new #GstVideoCodecState with the specified @fmt, @width and @height
2523 * as the output state for the decoder.
2524 * Any previously set output state on @decoder will be replaced by the newly
2527 * If the subclass wishes to copy over existing fields (like pixel aspec ratio,
2528 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
2531 * If the subclass wishes to override some fields from the output state (like
2532 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
2534 * The new output state will only take effect (set on pads and buffers) starting
2535 * from the next call to #gst_video_decoder_finish_frame().
2537 * Returns: (transfer full): the newly configured output state.
2539 GstVideoCodecState *
2540 gst_video_decoder_set_output_state (GstVideoDecoder * decoder,
2541 GstVideoFormat fmt, guint width, guint height,
2542 GstVideoCodecState * reference)
2544 GstVideoDecoderPrivate *priv = decoder->priv;
2545 GstVideoCodecState *state;
2547 GST_DEBUG_OBJECT (decoder, "fmt:%d, width:%d, height:%d, reference:%p",
2548 fmt, width, height, reference);
2550 /* Create the new output state */
2551 state = _new_output_state (fmt, width, height, reference);
2553 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2554 /* Replace existing output state by new one */
2555 if (priv->output_state)
2556 gst_video_codec_state_unref (priv->output_state);
2557 priv->output_state = gst_video_codec_state_ref (state);
2559 priv->output_state_changed = TRUE;
2560 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2567 * gst_video_decoder_get_oldest_frame:
2568 * @decoder: a #GstVideoDecoder
2570 * Get the oldest pending unfinished #GstVideoCodecFrame
2572 * Returns: (transfer full): oldest pending unfinished #GstVideoCodecFrame.
2574 GstVideoCodecFrame *
2575 gst_video_decoder_get_oldest_frame (GstVideoDecoder * decoder)
2577 GstVideoCodecFrame *frame = NULL;
2579 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2580 if (decoder->priv->frames)
2581 frame = gst_video_codec_frame_ref (decoder->priv->frames->data);
2582 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2584 return (GstVideoCodecFrame *) frame;
2588 * gst_video_decoder_get_frame:
2589 * @decoder: a #GstVideoDecoder
2590 * @frame_number: system_frame_number of a frame
2592 * Get a pending unfinished #GstVideoCodecFrame
2594 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2596 GstVideoCodecFrame *
2597 gst_video_decoder_get_frame (GstVideoDecoder * decoder, int frame_number)
2600 GstVideoCodecFrame *frame = NULL;
2602 GST_DEBUG_OBJECT (decoder, "frame_number : %d", frame_number);
2604 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2605 for (g = decoder->priv->frames; g; g = g->next) {
2606 GstVideoCodecFrame *tmp = g->data;
2608 if (tmp->system_frame_number == frame_number) {
2609 frame = gst_video_codec_frame_ref (tmp);
2613 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2619 gst_video_decoder_decide_allocation_default (GstVideoDecoder * decoder,
2623 GstBufferPool *pool = NULL;
2624 guint size, min, max;
2625 GstAllocator *allocator = NULL;
2626 GstAllocationParams params;
2627 GstStructure *config;
2628 gboolean update_pool, update_allocator;
2631 gst_query_parse_allocation (query, &outcaps, NULL);
2632 gst_video_info_init (&vinfo);
2633 gst_video_info_from_caps (&vinfo, outcaps);
2635 /* we got configuration from our peer or the decide_allocation method,
2637 if (gst_query_get_n_allocation_params (query) > 0) {
2638 /* try the allocator */
2639 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2640 update_allocator = TRUE;
2643 gst_allocation_params_init (¶ms);
2644 update_allocator = FALSE;
2647 if (gst_query_get_n_allocation_pools (query) > 0) {
2648 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2649 size = MAX (size, vinfo.size);
2656 update_pool = FALSE;
2660 /* no pool, we can make our own */
2661 GST_DEBUG_OBJECT (decoder, "no pool, making new pool");
2662 pool = gst_video_buffer_pool_new ();
2666 config = gst_buffer_pool_get_config (pool);
2667 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
2668 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2669 gst_buffer_pool_set_config (pool, config);
2671 if (update_allocator)
2672 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2674 gst_query_add_allocation_param (query, allocator, ¶ms);
2676 gst_object_unref (allocator);
2679 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2681 gst_query_add_allocation_pool (query, pool, size, min, max);
2684 gst_object_unref (pool);
2690 gst_video_decoder_propose_allocation_default (GstVideoDecoder * decoder,
2697 * gst_video_decoder_negotiate:
2698 * @decoder: a #GstVideoDecoder
2700 * Negotiate with downstreame elements to currently configured #GstVideoCodecState.
2702 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
2705 gst_video_decoder_negotiate (GstVideoDecoder * decoder)
2707 GstVideoCodecState *state = decoder->priv->output_state;
2708 GstVideoDecoderClass *klass;
2709 GstQuery *query = NULL;
2710 GstBufferPool *pool = NULL;
2711 GstAllocator *allocator;
2712 GstAllocationParams params;
2713 gboolean ret = TRUE;
2715 g_return_val_if_fail (GST_VIDEO_INFO_WIDTH (&state->info) != 0, FALSE);
2716 g_return_val_if_fail (GST_VIDEO_INFO_HEIGHT (&state->info) != 0, FALSE);
2718 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2720 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
2722 GST_DEBUG_OBJECT (decoder, "output_state par %d/%d fps %d/%d",
2723 state->info.par_n, state->info.par_d,
2724 state->info.fps_n, state->info.fps_d);
2726 if (G_UNLIKELY (state->caps == NULL))
2727 state->caps = gst_video_info_to_caps (&state->info);
2729 GST_DEBUG_OBJECT (decoder, "setting caps %" GST_PTR_FORMAT, state->caps);
2731 ret = gst_pad_set_caps (decoder->srcpad, state->caps);
2734 decoder->priv->output_state_changed = FALSE;
2736 /* Negotiate pool */
2737 query = gst_query_new_allocation (state->caps, TRUE);
2739 if (!gst_pad_peer_query (decoder->srcpad, query)) {
2740 GST_DEBUG_OBJECT (decoder, "didn't get downstream ALLOCATION hints");
2743 g_assert (klass->decide_allocation != NULL);
2744 ret = klass->decide_allocation (decoder, query);
2746 GST_DEBUG_OBJECT (decoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
2750 goto no_decide_allocation;
2752 /* we got configuration from our peer or the decide_allocation method,
2754 if (gst_query_get_n_allocation_params (query) > 0) {
2755 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2758 gst_allocation_params_init (¶ms);
2761 if (gst_query_get_n_allocation_pools (query) > 0)
2762 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
2765 gst_object_unref (allocator);
2767 goto no_decide_allocation;
2770 if (decoder->priv->allocator)
2771 gst_object_unref (decoder->priv->allocator);
2772 decoder->priv->allocator = allocator;
2773 decoder->priv->params = params;
2775 if (decoder->priv->pool) {
2776 gst_buffer_pool_set_active (decoder->priv->pool, FALSE);
2777 gst_object_unref (decoder->priv->pool);
2779 decoder->priv->pool = pool;
2782 gst_buffer_pool_set_active (pool, TRUE);
2786 gst_query_unref (query);
2788 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2793 no_decide_allocation:
2795 GST_WARNING_OBJECT (decoder, "Subclass failed to decide allocation");
2801 * gst_video_decoder_allocate_output_buffer:
2802 * @decoder: a #GstVideoDecoder
2804 * Helper function that allocates a buffer to hold a video frame for @decoder's
2805 * current #GstVideoCodecState.
2807 * Returns: (transfer full): allocated buffer
2810 gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder)
2814 GST_DEBUG ("alloc src buffer");
2816 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2817 if (G_UNLIKELY (decoder->priv->output_state_changed
2818 || (decoder->priv->output_state
2819 && gst_pad_check_reconfigure (decoder->srcpad))))
2820 gst_video_decoder_negotiate (decoder);
2822 gst_buffer_pool_acquire_buffer (decoder->priv->pool, &buffer, NULL);
2824 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2830 * gst_video_decoder_allocate_output_frame:
2831 * @decoder: a #GstVideoDecoder
2832 * @frame: a #GstVideoCodecFrame
2834 * Helper function that allocates a buffer to hold a video frame for @decoder's
2835 * current #GstVideoCodecState. Subclass should already have configured video
2836 * state and set src pad caps.
2838 * The buffer allocated here is owned by the frame and you should only
2839 * keep references to the frame, not the buffer.
2841 * Returns: %GST_FLOW_OK if an output buffer could be allocated
2844 gst_video_decoder_allocate_output_frame (GstVideoDecoder *
2845 decoder, GstVideoCodecFrame * frame)
2847 GstFlowReturn flow_ret;
2848 GstVideoCodecState *state = decoder->priv->output_state;
2849 int num_bytes = GST_VIDEO_INFO_SIZE (&state->info);
2851 g_return_val_if_fail (num_bytes != 0, GST_FLOW_ERROR);
2852 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
2854 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2855 if (G_UNLIKELY (decoder->priv->output_state_changed
2856 || (decoder->priv->output_state
2857 && gst_pad_check_reconfigure (decoder->srcpad))))
2858 gst_video_decoder_negotiate (decoder);
2860 GST_LOG_OBJECT (decoder, "alloc buffer size %d", num_bytes);
2862 flow_ret = gst_buffer_pool_acquire_buffer (decoder->priv->pool,
2863 &frame->output_buffer, NULL);
2865 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2871 * gst_video_decoder_get_max_decode_time:
2872 * @decoder: a #GstVideoDecoder
2873 * @frame: a #GstVideoCodecFrame
2875 * Determines maximum possible decoding time for @frame that will
2876 * allow it to decode and arrive in time (as determined by QoS events).
2877 * In particular, a negative result means decoding in time is no longer possible
2878 * and should therefore occur as soon/skippy as possible.
2880 * Returns: max decoding time.
2883 gst_video_decoder_get_max_decode_time (GstVideoDecoder *
2884 decoder, GstVideoCodecFrame * frame)
2886 GstClockTimeDiff deadline;
2887 GstClockTime earliest_time;
2889 GST_OBJECT_LOCK (decoder);
2890 earliest_time = decoder->priv->earliest_time;
2891 if (GST_CLOCK_TIME_IS_VALID (earliest_time)
2892 && GST_CLOCK_TIME_IS_VALID (frame->deadline))
2893 deadline = GST_CLOCK_DIFF (earliest_time, frame->deadline);
2895 deadline = G_MAXINT64;
2897 GST_LOG_OBJECT (decoder, "earliest %" GST_TIME_FORMAT
2898 ", frame deadline %" GST_TIME_FORMAT ", deadline %" GST_TIME_FORMAT,
2899 GST_TIME_ARGS (earliest_time), GST_TIME_ARGS (frame->deadline),
2900 GST_TIME_ARGS (deadline));
2902 GST_OBJECT_UNLOCK (decoder);
2908 _gst_video_decoder_error (GstVideoDecoder * dec, gint weight,
2909 GQuark domain, gint code, gchar * txt, gchar * dbg, const gchar * file,
2910 const gchar * function, gint line)
2913 GST_WARNING_OBJECT (dec, "error: %s", txt);
2915 GST_WARNING_OBJECT (dec, "error: %s", dbg);
2916 dec->priv->error_count += weight;
2917 dec->priv->discont = TRUE;
2918 if (dec->priv->max_errors < dec->priv->error_count) {
2919 gst_element_message_full (GST_ELEMENT (dec), GST_MESSAGE_ERROR,
2920 domain, code, txt, dbg, file, function, line);
2921 return GST_FLOW_ERROR;
2928 * gst_video_decoder_set_max_errors:
2929 * @dec: a #GstVideoDecoder
2930 * @num: max tolerated errors
2932 * Sets numbers of tolerated decoder errors, where a tolerated one is then only
2933 * warned about, but more than tolerated will lead to fatal error. Default
2934 * is set to GST_VIDEO_DECODER_MAX_ERRORS.
2937 gst_video_decoder_set_max_errors (GstVideoDecoder * dec, gint num)
2939 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
2941 dec->priv->max_errors = num;
2945 * gst_video_decoder_get_max_errors:
2946 * @dec: a #GstVideoDecoder
2948 * Returns: currently configured decoder tolerated error count.
2951 gst_video_decoder_get_max_errors (GstVideoDecoder * dec)
2953 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
2955 return dec->priv->max_errors;
2959 * gst_video_decoder_set_packetized:
2960 * @decoder: a #GstVideoDecoder
2961 * @packetized: whether the input data should be considered as packetized.
2963 * Allows baseclass to consider input data as packetized or not. If the
2964 * input is packetized, then the @parse method will not be called.
2967 gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
2968 gboolean packetized)
2970 decoder->priv->packetized = packetized;
2974 * gst_video_decoder_get_packetized:
2975 * @decoder: a #GstVideoDecoder
2977 * Queries whether input data is considered packetized or not by the
2980 * Returns: TRUE if input data is considered packetized.
2983 gst_video_decoder_get_packetized (GstVideoDecoder * decoder)
2985 return decoder->priv->packetized;
2989 * gst_video_decoder_set_estimate_rate:
2990 * @dec: a #GstVideoDecoder
2991 * @enabled: whether to enable byte to time conversion
2993 * Allows baseclass to perform byte to time estimated conversion.
2996 gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec, gboolean enabled)
2998 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
3000 dec->priv->do_estimate_rate = enabled;
3004 * gst_video_decoder_get_estimate_rate:
3005 * @dec: a #GstVideoDecoder
3007 * Returns: currently configured byte to time conversion setting
3010 gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec)
3012 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
3014 return dec->priv->do_estimate_rate;
3018 * gst_video_decoder_set_latency:
3019 * @decoder: a #GstVideoDecoder
3020 * @min_latency: minimum latency
3021 * @max_latency: maximum latency
3023 * Lets #GstVideoDecoder sub-classes tell the baseclass what the decoder
3024 * latency is. Will also post a LATENCY message on the bus so the pipeline
3025 * can reconfigure its global latency.
3028 gst_video_decoder_set_latency (GstVideoDecoder * decoder,
3029 GstClockTime min_latency, GstClockTime max_latency)
3031 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3032 g_return_if_fail (max_latency >= min_latency);
3034 GST_OBJECT_LOCK (decoder);
3035 decoder->priv->min_latency = min_latency;
3036 decoder->priv->max_latency = max_latency;
3037 GST_OBJECT_UNLOCK (decoder);
3039 gst_element_post_message (GST_ELEMENT_CAST (decoder),
3040 gst_message_new_latency (GST_OBJECT_CAST (decoder)));
3044 * gst_video_decoder_get_latency:
3045 * @decoder: a #GstVideoDecoder
3046 * @min_latency: (out) (allow-none): address of variable in which to store the
3047 * configured minimum latency, or %NULL
3048 * @max_latency: (out) (allow-none): address of variable in which to store the
3049 * configured mximum latency, or %NULL
3051 * Query the configured decoder latency. Results will be returned via
3052 * @min_latency and @max_latency.
3055 gst_video_decoder_get_latency (GstVideoDecoder * decoder,
3056 GstClockTime * min_latency, GstClockTime * max_latency)
3058 GST_OBJECT_LOCK (decoder);
3060 *min_latency = decoder->priv->min_latency;
3062 *max_latency = decoder->priv->max_latency;
3063 GST_OBJECT_UNLOCK (decoder);