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 */
335 /* ... being tracked here;
336 * only available during parsing */
337 GstVideoCodecFrame *current_frame;
338 /* events that should apply to the current frame */
339 GList *current_frame_events;
341 /* relative offset of input data */
342 guint64 input_offset;
343 /* relative offset of frame */
344 guint64 frame_offset;
345 /* tracking ts and offsets */
348 /* last outgoing ts */
349 GstClockTime last_timestamp_out;
351 /* reverse playback */
356 /* collected parsed frames */
358 /* frames to be handled == decoded */
360 /* collected output - of buffer objects, not frames */
361 GList *output_queued;
364 /* base_picture_number is the picture number of the reference picture */
365 guint64 base_picture_number;
366 /* combine with base_picture_number, framerate and calcs to yield (presentation) ts */
367 GstClockTime base_timestamp;
369 /* FIXME : reorder_depth is never set */
371 int distance_from_sync;
373 guint64 system_frame_number;
374 guint64 decode_frame_number;
376 GList *frames; /* Protected with OBJECT_LOCK */
377 GstVideoCodecState *input_state;
378 GstVideoCodecState *output_state;
379 gboolean output_state_changed;
383 GstClockTime earliest_time;
385 /* qos messages: frames dropped/processed */
389 /* Outgoing byte size ? */
397 static GstElementClass *parent_class = NULL;
398 static void gst_video_decoder_class_init (GstVideoDecoderClass * klass);
399 static void gst_video_decoder_init (GstVideoDecoder * dec,
400 GstVideoDecoderClass * klass);
402 static void gst_video_decoder_finalize (GObject * object);
404 static gboolean gst_video_decoder_setcaps (GstVideoDecoder * dec,
406 static gboolean gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
408 static gboolean gst_video_decoder_src_event (GstPad * pad, GstObject * parent,
410 static GstFlowReturn gst_video_decoder_chain (GstPad * pad, GstObject * parent,
412 static gboolean gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
414 static GstStateChangeReturn gst_video_decoder_change_state (GstElement *
415 element, GstStateChange transition);
416 static gboolean gst_video_decoder_src_query (GstPad * pad, GstObject * parent,
418 static void gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full);
420 static GstFlowReturn gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
421 GstVideoCodecFrame * frame);
423 static void gst_video_decoder_release_frame (GstVideoDecoder * dec,
424 GstVideoCodecFrame * frame);
425 static GstClockTime gst_video_decoder_get_frame_duration (GstVideoDecoder *
426 decoder, GstVideoCodecFrame * frame);
427 static GstVideoCodecFrame *gst_video_decoder_new_frame (GstVideoDecoder *
429 static GstFlowReturn gst_video_decoder_clip_and_push_buf (GstVideoDecoder *
430 decoder, GstBuffer * buf);
431 static GstFlowReturn gst_video_decoder_flush_parse (GstVideoDecoder * dec,
434 static void gst_video_decoder_clear_queues (GstVideoDecoder * dec);
436 static gboolean gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
438 static gboolean gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
440 static gboolean gst_video_decoder_decide_allocation_default (GstVideoDecoder *
441 decoder, GstQuery * query);
442 static gboolean gst_video_decoder_propose_allocation_default (GstVideoDecoder *
443 decoder, GstQuery * query);
445 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
446 * method to get to the padtemplates */
448 gst_video_decoder_get_type (void)
450 static volatile gsize type = 0;
452 if (g_once_init_enter (&type)) {
454 static const GTypeInfo info = {
455 sizeof (GstVideoDecoderClass),
458 (GClassInitFunc) gst_video_decoder_class_init,
461 sizeof (GstVideoDecoder),
463 (GInstanceInitFunc) gst_video_decoder_init,
466 _type = g_type_register_static (GST_TYPE_ELEMENT,
467 "GstVideoDecoder", &info, G_TYPE_FLAG_ABSTRACT);
468 g_once_init_leave (&type, _type);
474 gst_video_decoder_class_init (GstVideoDecoderClass * klass)
476 GObjectClass *gobject_class;
477 GstElementClass *gstelement_class;
479 gobject_class = G_OBJECT_CLASS (klass);
480 gstelement_class = GST_ELEMENT_CLASS (klass);
482 GST_DEBUG_CATEGORY_INIT (videodecoder_debug, "videodecoder", 0,
483 "Base Video Decoder");
485 parent_class = g_type_class_peek_parent (klass);
486 g_type_class_add_private (klass, sizeof (GstVideoDecoderPrivate));
488 gobject_class->finalize = gst_video_decoder_finalize;
490 gstelement_class->change_state =
491 GST_DEBUG_FUNCPTR (gst_video_decoder_change_state);
493 klass->sink_event = gst_video_decoder_sink_event_default;
494 klass->src_event = gst_video_decoder_src_event_default;
495 klass->decide_allocation = gst_video_decoder_decide_allocation_default;
496 klass->propose_allocation = gst_video_decoder_propose_allocation_default;
500 gst_video_decoder_init (GstVideoDecoder * decoder, GstVideoDecoderClass * klass)
502 GstPadTemplate *pad_template;
505 GST_DEBUG_OBJECT (decoder, "gst_video_decoder_init");
507 decoder->priv = GST_VIDEO_DECODER_GET_PRIVATE (decoder);
510 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
511 g_return_if_fail (pad_template != NULL);
513 decoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
515 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_decoder_chain));
516 gst_pad_set_event_function (pad,
517 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_event));
518 gst_pad_set_query_function (pad,
519 GST_DEBUG_FUNCPTR (gst_video_decoder_sink_query));
520 gst_element_add_pad (GST_ELEMENT (decoder), decoder->sinkpad);
523 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
524 g_return_if_fail (pad_template != NULL);
526 decoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
528 gst_pad_set_event_function (pad,
529 GST_DEBUG_FUNCPTR (gst_video_decoder_src_event));
530 gst_pad_set_query_function (pad,
531 GST_DEBUG_FUNCPTR (gst_video_decoder_src_query));
532 gst_pad_use_fixed_caps (pad);
533 gst_element_add_pad (GST_ELEMENT (decoder), decoder->srcpad);
535 gst_segment_init (&decoder->input_segment, GST_FORMAT_TIME);
536 gst_segment_init (&decoder->output_segment, GST_FORMAT_TIME);
538 g_rec_mutex_init (&decoder->stream_lock);
540 decoder->priv->input_adapter = gst_adapter_new ();
541 decoder->priv->output_adapter = gst_adapter_new ();
542 decoder->priv->packetized = TRUE;
544 gst_video_decoder_reset (decoder, TRUE);
548 gst_video_rawvideo_convert (GstVideoCodecState * state,
549 GstFormat src_format, gint64 src_value,
550 GstFormat * dest_format, gint64 * dest_value)
552 gboolean res = FALSE;
556 g_return_val_if_fail (dest_format != NULL, FALSE);
557 g_return_val_if_fail (dest_value != NULL, FALSE);
559 if (src_format == *dest_format || src_value == 0 || src_value == -1) {
560 *dest_value = src_value;
564 vidsize = GST_VIDEO_INFO_SIZE (&state->info);
565 fps_n = GST_VIDEO_INFO_FPS_N (&state->info);
566 fps_d = GST_VIDEO_INFO_FPS_D (&state->info);
568 if (src_format == GST_FORMAT_BYTES &&
569 *dest_format == GST_FORMAT_DEFAULT && vidsize) {
570 /* convert bytes to frames */
571 *dest_value = gst_util_uint64_scale_int (src_value, 1, vidsize);
573 } else if (src_format == GST_FORMAT_DEFAULT &&
574 *dest_format == GST_FORMAT_BYTES && vidsize) {
575 /* convert bytes to frames */
576 *dest_value = src_value * vidsize;
578 } else if (src_format == GST_FORMAT_DEFAULT &&
579 *dest_format == GST_FORMAT_TIME && fps_n) {
580 /* convert frames to time */
581 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND * fps_d, fps_n);
583 } else if (src_format == GST_FORMAT_TIME &&
584 *dest_format == GST_FORMAT_DEFAULT && fps_d) {
585 /* convert time to frames */
586 *dest_value = gst_util_uint64_scale (src_value, fps_n, GST_SECOND * fps_d);
588 } else if (src_format == GST_FORMAT_TIME &&
589 *dest_format == GST_FORMAT_BYTES && fps_d && vidsize) {
590 /* convert time to frames */
591 *dest_value = gst_util_uint64_scale (src_value,
592 fps_n * vidsize, GST_SECOND * fps_d);
594 } else if (src_format == GST_FORMAT_BYTES &&
595 *dest_format == GST_FORMAT_TIME && fps_n && vidsize) {
596 /* convert frames to time */
597 *dest_value = gst_util_uint64_scale (src_value,
598 GST_SECOND * fps_d, fps_n * vidsize);
606 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
607 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
610 gboolean res = FALSE;
612 g_return_val_if_fail (dest_format != NULL, FALSE);
613 g_return_val_if_fail (dest_value != NULL, FALSE);
615 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
618 *dest_value = src_value;
622 if (bytes <= 0 || time <= 0) {
623 GST_DEBUG ("not enough metadata yet to convert");
627 switch (src_format) {
628 case GST_FORMAT_BYTES:
629 switch (*dest_format) {
630 case GST_FORMAT_TIME:
631 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
638 case GST_FORMAT_TIME:
639 switch (*dest_format) {
640 case GST_FORMAT_BYTES:
641 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
649 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
658 static GstVideoCodecState *
659 _new_input_state (GstCaps * caps)
661 GstVideoCodecState *state;
662 GstStructure *structure;
663 const GValue *codec_data;
665 state = g_slice_new0 (GstVideoCodecState);
666 state->ref_count = 1;
667 gst_video_info_init (&state->info);
668 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
670 state->caps = gst_caps_ref (caps);
672 structure = gst_caps_get_structure (caps, 0);
674 codec_data = gst_structure_get_value (structure, "codec_data");
675 if (codec_data && G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER)
676 state->codec_data = GST_BUFFER (g_value_dup_boxed (codec_data));
682 g_slice_free (GstVideoCodecState, state);
687 static GstVideoCodecState *
688 _new_output_state (GstVideoFormat fmt, guint width, guint height,
689 GstVideoCodecState * reference)
691 GstVideoCodecState *state;
693 state = g_slice_new0 (GstVideoCodecState);
694 state->ref_count = 1;
695 gst_video_info_init (&state->info);
696 gst_video_info_set_format (&state->info, fmt, width, height);
699 GstVideoInfo *tgt, *ref;
702 ref = &reference->info;
704 /* Copy over extra fields from reference state */
705 tgt->interlace_mode = ref->interlace_mode;
706 tgt->flags = ref->flags;
707 tgt->chroma_site = ref->chroma_site;
708 /* only copy values that are not unknown so that we don't override the
709 * defaults. subclasses should really fill these in when they know. */
710 if (ref->colorimetry.range)
711 tgt->colorimetry.range = ref->colorimetry.range;
712 if (ref->colorimetry.matrix)
713 tgt->colorimetry.matrix = ref->colorimetry.matrix;
714 if (ref->colorimetry.transfer)
715 tgt->colorimetry.transfer = ref->colorimetry.transfer;
716 if (ref->colorimetry.primaries)
717 tgt->colorimetry.primaries = ref->colorimetry.primaries;
718 GST_DEBUG ("reference par %d/%d fps %d/%d",
719 ref->par_n, ref->par_d, ref->fps_n, ref->fps_d);
720 tgt->par_n = ref->par_n;
721 tgt->par_d = ref->par_d;
722 tgt->fps_n = ref->fps_n;
723 tgt->fps_d = ref->fps_d;
726 GST_DEBUG ("reference par %d/%d fps %d/%d",
727 state->info.par_n, state->info.par_d,
728 state->info.fps_n, state->info.fps_d);
734 gst_video_decoder_setcaps (GstVideoDecoder * decoder, GstCaps * caps)
736 GstVideoDecoderClass *decoder_class;
737 GstVideoCodecState *state;
740 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
742 GST_DEBUG_OBJECT (decoder, "setcaps %" GST_PTR_FORMAT, caps);
744 state = _new_input_state (caps);
746 if (G_UNLIKELY (state == NULL))
749 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
751 if (decoder_class->set_format)
752 ret = decoder_class->set_format (decoder, state);
757 if (decoder->priv->input_state)
758 gst_video_codec_state_unref (decoder->priv->input_state);
759 decoder->priv->input_state = state;
761 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
769 GST_WARNING_OBJECT (decoder, "Failed to parse caps");
775 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
776 GST_WARNING_OBJECT (decoder, "Subclass refused caps");
777 gst_video_codec_state_unref (state);
783 gst_video_decoder_finalize (GObject * object)
785 GstVideoDecoder *decoder;
787 decoder = GST_VIDEO_DECODER (object);
789 GST_DEBUG_OBJECT (object, "finalize");
791 g_rec_mutex_clear (&decoder->stream_lock);
793 if (decoder->priv->input_adapter) {
794 g_object_unref (decoder->priv->input_adapter);
795 decoder->priv->input_adapter = NULL;
797 if (decoder->priv->output_adapter) {
798 g_object_unref (decoder->priv->output_adapter);
799 decoder->priv->output_adapter = NULL;
802 if (decoder->priv->input_state)
803 gst_video_codec_state_unref (decoder->priv->input_state);
804 if (decoder->priv->output_state)
805 gst_video_codec_state_unref (decoder->priv->output_state);
807 if (decoder->priv->pool) {
808 gst_object_unref (decoder->priv->pool);
809 decoder->priv->pool = NULL;
812 if (decoder->priv->allocator) {
813 gst_object_unref (decoder->priv->allocator);
814 decoder->priv->allocator = NULL;
817 G_OBJECT_CLASS (parent_class)->finalize (object);
820 /* hard == FLUSH, otherwise discont */
822 gst_video_decoder_flush (GstVideoDecoder * dec, gboolean hard)
824 GstVideoDecoderClass *klass;
825 GstVideoDecoderPrivate *priv = dec->priv;
826 GstFlowReturn ret = GST_FLOW_OK;
828 klass = GST_VIDEO_DECODER_GET_CLASS (dec);
830 GST_LOG_OBJECT (dec, "flush hard %d", hard);
832 /* Inform subclass */
834 klass->reset (dec, hard);
836 /* FIXME make some more distinction between hard and soft,
837 * but subclass may not be prepared for that */
838 /* FIXME perhaps also clear pending frames ?,
839 * but again, subclass may still come up with one of those */
841 /* TODO ? finish/drain some stuff */
843 gst_segment_init (&dec->input_segment, GST_FORMAT_UNDEFINED);
844 gst_segment_init (&dec->output_segment, GST_FORMAT_UNDEFINED);
845 gst_video_decoder_clear_queues (dec);
846 priv->error_count = 0;
847 g_list_free_full (priv->current_frame_events,
848 (GDestroyNotify) gst_event_unref);
849 priv->current_frame_events = NULL;
851 /* and get (re)set for the sequel */
852 gst_video_decoder_reset (dec, FALSE);
858 gst_video_decoder_push_event (GstVideoDecoder * decoder, GstEvent * event)
860 switch (GST_EVENT_TYPE (event)) {
861 case GST_EVENT_SEGMENT:
865 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
867 gst_event_copy_segment (event, &segment);
869 GST_DEBUG_OBJECT (decoder, "segment %" GST_SEGMENT_FORMAT, &segment);
871 if (segment.format != GST_FORMAT_TIME) {
872 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
873 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
877 decoder->output_segment = segment;
878 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
885 return gst_pad_push_event (decoder->srcpad, event);
889 gst_video_decoder_handle_eos (GstVideoDecoder * dec)
891 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
892 GstVideoDecoderPrivate *priv = dec->priv;
893 GstFlowReturn ret = GST_FLOW_OK;
895 GST_VIDEO_DECODER_STREAM_LOCK (dec);
897 if (dec->input_segment.rate > 0.0) {
898 /* Forward mode, if unpacketized, give the child class
899 * a final chance to flush out packets */
900 if (!priv->packetized) {
901 if (priv->current_frame == NULL)
902 priv->current_frame = gst_video_decoder_new_frame (dec);
904 while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter))
905 ret = decoder_class->parse (dec, priv->current_frame,
906 priv->input_adapter, TRUE);
909 /* Reverse playback mode */
910 ret = gst_video_decoder_flush_parse (dec, TRUE);
913 if (decoder_class->finish)
914 ret = decoder_class->finish (dec);
916 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
922 gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
925 GstVideoDecoderPrivate *priv;
926 gboolean ret = FALSE;
928 priv = decoder->priv;
930 switch (GST_EVENT_TYPE (event)) {
935 gst_event_parse_caps (event, &caps);
936 ret = gst_video_decoder_setcaps (decoder, caps);
937 gst_event_unref (event);
943 GstFlowReturn flow_ret = GST_FLOW_OK;
945 flow_ret = gst_video_decoder_handle_eos (decoder);
946 ret = (flow_ret == GST_FLOW_OK);
950 case GST_EVENT_SEGMENT:
954 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
956 gst_event_copy_segment (event, &segment);
958 if (segment.format == GST_FORMAT_TIME) {
959 GST_DEBUG_OBJECT (decoder,
960 "received TIME SEGMENT %" GST_SEGMENT_FORMAT, &segment);
964 GST_DEBUG_OBJECT (decoder,
965 "received SEGMENT %" GST_SEGMENT_FORMAT, &segment);
967 /* handle newsegment as a result from our legacy simple seeking */
968 /* note that initial 0 should convert to 0 in any case */
969 if (priv->do_estimate_rate &&
970 gst_pad_query_convert (decoder->sinkpad, GST_FORMAT_BYTES,
971 segment.start, GST_FORMAT_TIME, &start)) {
972 /* best attempt convert */
973 /* as these are only estimates, stop is kept open-ended to avoid
974 * premature cutting */
975 GST_DEBUG_OBJECT (decoder,
976 "converted to TIME start %" GST_TIME_FORMAT,
977 GST_TIME_ARGS (start));
978 segment.start = start;
979 segment.stop = GST_CLOCK_TIME_NONE;
980 segment.time = start;
982 gst_event_unref (event);
983 event = gst_event_new_segment (&segment);
985 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
986 goto newseg_wrong_format;
990 gst_video_decoder_flush (decoder, FALSE);
992 priv->base_timestamp = GST_CLOCK_TIME_NONE;
993 priv->base_picture_number = 0;
995 decoder->input_segment = segment;
997 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1000 case GST_EVENT_FLUSH_STOP:
1002 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1003 /* well, this is kind of worse than a DISCONT */
1004 gst_video_decoder_flush (decoder, TRUE);
1005 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1011 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1012 * For EOS this is required because no buffer or serialized event
1013 * will come after EOS and nothing could trigger another
1014 * _finish_frame() call. *
1015 * If the subclass handles sending of EOS manually it can return
1016 * _DROPPED from ::finish() and all other subclasses should have
1017 * decoded/flushed all remaining data before this
1019 * For FLUSH_STOP this is required because it is expected
1020 * to be forwarded immediately and no buffers are queued anyway.
1023 if (!GST_EVENT_IS_SERIALIZED (event)
1024 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1025 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1026 ret = gst_video_decoder_push_event (decoder, event);
1028 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1029 decoder->priv->current_frame_events =
1030 g_list_prepend (decoder->priv->current_frame_events, event);
1031 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1038 newseg_wrong_format:
1040 GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
1041 gst_event_unref (event);
1048 gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
1051 GstVideoDecoder *decoder;
1052 GstVideoDecoderClass *decoder_class;
1053 gboolean ret = FALSE;
1055 decoder = GST_VIDEO_DECODER (parent);
1056 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1058 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1059 GST_EVENT_TYPE_NAME (event));
1061 if (decoder_class->sink_event)
1062 ret = decoder_class->sink_event (decoder, event);
1067 /* perform upstream byte <-> time conversion (duration, seeking)
1068 * if subclass allows and if enough data for moderately decent conversion */
1069 static inline gboolean
1070 gst_video_decoder_do_byte (GstVideoDecoder * dec)
1072 return dec->priv->do_estimate_rate && (dec->priv->bytes_out > 0)
1073 && (dec->priv->time > GST_SECOND);
1077 gst_video_decoder_do_seek (GstVideoDecoder * dec, GstEvent * event)
1081 GstSeekType start_type, end_type;
1083 gint64 start, start_time, end_time;
1084 GstSegment seek_segment;
1087 gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
1088 &start_time, &end_type, &end_time);
1090 /* we'll handle plain open-ended flushing seeks with the simple approach */
1092 GST_DEBUG_OBJECT (dec, "unsupported seek: rate");
1096 if (start_type != GST_SEEK_TYPE_SET) {
1097 GST_DEBUG_OBJECT (dec, "unsupported seek: start time");
1101 if (end_type != GST_SEEK_TYPE_NONE ||
1102 (end_type == GST_SEEK_TYPE_SET && end_time != GST_CLOCK_TIME_NONE)) {
1103 GST_DEBUG_OBJECT (dec, "unsupported seek: end time");
1107 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1108 GST_DEBUG_OBJECT (dec, "unsupported seek: not flushing");
1112 memcpy (&seek_segment, &dec->output_segment, sizeof (seek_segment));
1113 gst_segment_do_seek (&seek_segment, rate, format, flags, start_type,
1114 start_time, end_type, end_time, NULL);
1115 start_time = seek_segment.position;
1117 if (!gst_pad_query_convert (dec->sinkpad, GST_FORMAT_TIME, start_time,
1118 GST_FORMAT_BYTES, &start)) {
1119 GST_DEBUG_OBJECT (dec, "conversion failed");
1123 seqnum = gst_event_get_seqnum (event);
1124 event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
1125 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
1126 gst_event_set_seqnum (event, seqnum);
1128 GST_DEBUG_OBJECT (dec, "seeking to %" GST_TIME_FORMAT " at byte offset %"
1129 G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
1131 return gst_pad_push_event (dec->sinkpad, event);
1135 gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
1138 GstVideoDecoderPrivate *priv;
1139 gboolean res = FALSE;
1141 priv = decoder->priv;
1143 GST_DEBUG_OBJECT (decoder,
1144 "received event %d, %s", GST_EVENT_TYPE (event),
1145 GST_EVENT_TYPE_NAME (event));
1147 switch (GST_EVENT_TYPE (event)) {
1148 case GST_EVENT_SEEK:
1153 GstSeekType cur_type, stop_type;
1158 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
1160 seqnum = gst_event_get_seqnum (event);
1162 /* upstream gets a chance first */
1163 if ((res = gst_pad_push_event (decoder->sinkpad, event)))
1166 /* if upstream fails for a time seek, maybe we can help if allowed */
1167 if (format == GST_FORMAT_TIME) {
1168 if (gst_video_decoder_do_byte (decoder))
1169 res = gst_video_decoder_do_seek (decoder, event);
1173 /* ... though a non-time seek can be aided as well */
1174 /* First bring the requested format to time */
1176 gst_pad_query_convert (decoder->srcpad, format, cur,
1177 GST_FORMAT_TIME, &tcur)))
1180 gst_pad_query_convert (decoder->srcpad, format, stop,
1181 GST_FORMAT_TIME, &tstop)))
1184 /* then seek with time on the peer */
1185 event = gst_event_new_seek (rate, GST_FORMAT_TIME,
1186 flags, cur_type, tcur, stop_type, tstop);
1187 gst_event_set_seqnum (event, seqnum);
1189 res = gst_pad_push_event (decoder->sinkpad, event);
1196 GstClockTimeDiff diff;
1197 GstClockTime timestamp;
1198 GstClockTime duration;
1200 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1202 GST_OBJECT_LOCK (decoder);
1203 priv->proportion = proportion;
1204 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) {
1205 if (G_UNLIKELY (diff > 0)) {
1206 if (priv->output_state->info.fps_n > 0)
1208 gst_util_uint64_scale (GST_SECOND,
1209 priv->output_state->info.fps_d, priv->output_state->info.fps_n);
1212 priv->earliest_time = timestamp + 2 * diff + duration;
1214 priv->earliest_time = timestamp + diff;
1217 priv->earliest_time = GST_CLOCK_TIME_NONE;
1219 GST_OBJECT_UNLOCK (decoder);
1221 GST_DEBUG_OBJECT (decoder,
1222 "got QoS %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT ", %g",
1223 GST_TIME_ARGS (timestamp), diff, proportion);
1225 res = gst_pad_push_event (decoder->sinkpad, event);
1229 res = gst_pad_push_event (decoder->sinkpad, event);
1236 GST_DEBUG_OBJECT (decoder, "could not convert format");
1241 gst_video_decoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1243 GstVideoDecoder *decoder;
1244 GstVideoDecoderClass *decoder_class;
1245 gboolean ret = FALSE;
1247 decoder = GST_VIDEO_DECODER (parent);
1248 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1250 GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1251 GST_EVENT_TYPE_NAME (event));
1253 if (decoder_class->src_event)
1254 ret = decoder_class->src_event (decoder, event);
1260 gst_video_decoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1262 GstVideoDecoder *dec;
1263 gboolean res = TRUE;
1265 dec = GST_VIDEO_DECODER (parent);
1267 GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
1269 switch (GST_QUERY_TYPE (query)) {
1270 case GST_QUERY_POSITION:
1275 /* upstream gets a chance first */
1276 if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
1277 GST_LOG_OBJECT (dec, "returning peer response");
1281 /* we start from the last seen time */
1282 time = dec->priv->last_timestamp_out;
1283 /* correct for the segment values */
1284 time = gst_segment_to_stream_time (&dec->output_segment,
1285 GST_FORMAT_TIME, time);
1287 GST_LOG_OBJECT (dec,
1288 "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
1290 /* and convert to the final format */
1291 gst_query_parse_position (query, &format, NULL);
1292 if (!(res = gst_pad_query_convert (pad, GST_FORMAT_TIME, time,
1296 gst_query_set_position (query, format, value);
1298 GST_LOG_OBJECT (dec,
1299 "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
1303 case GST_QUERY_DURATION:
1307 /* upstream in any case */
1308 if ((res = gst_pad_query_default (pad, parent, query)))
1311 gst_query_parse_duration (query, &format, NULL);
1312 /* try answering TIME by converting from BYTE if subclass allows */
1313 if (format == GST_FORMAT_TIME && gst_video_decoder_do_byte (dec)) {
1316 if (gst_pad_peer_query_duration (dec->sinkpad, GST_FORMAT_BYTES,
1318 GST_LOG_OBJECT (dec, "upstream size %" G_GINT64_FORMAT, value);
1319 if (gst_pad_query_convert (dec->sinkpad,
1320 GST_FORMAT_BYTES, value, GST_FORMAT_TIME, &value)) {
1321 gst_query_set_duration (query, GST_FORMAT_TIME, value);
1328 case GST_QUERY_CONVERT:
1330 GstFormat src_fmt, dest_fmt;
1331 gint64 src_val, dest_val;
1333 GST_DEBUG_OBJECT (dec, "convert query");
1335 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1336 res = gst_video_rawvideo_convert (dec->priv->output_state,
1337 src_fmt, src_val, &dest_fmt, &dest_val);
1340 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1343 case GST_QUERY_LATENCY:
1346 GstClockTime min_latency, max_latency;
1348 res = gst_pad_peer_query (dec->sinkpad, query);
1350 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1351 GST_DEBUG_OBJECT (dec, "Peer latency: live %d, min %"
1352 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1353 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1355 GST_OBJECT_LOCK (dec);
1356 min_latency += dec->priv->min_latency;
1357 if (dec->priv->max_latency == GST_CLOCK_TIME_NONE) {
1358 max_latency = GST_CLOCK_TIME_NONE;
1359 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1360 max_latency += dec->priv->max_latency;
1362 GST_OBJECT_UNLOCK (dec);
1364 gst_query_set_latency (query, live, min_latency, max_latency);
1369 res = gst_pad_query_default (pad, parent, query);
1374 GST_ERROR_OBJECT (dec, "query failed");
1379 gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
1382 GstVideoDecoder *decoder;
1383 GstVideoDecoderPrivate *priv;
1384 gboolean res = FALSE;
1386 decoder = GST_VIDEO_DECODER (parent);
1387 priv = decoder->priv;
1389 GST_LOG_OBJECT (decoder, "handling query: %" GST_PTR_FORMAT, query);
1391 switch (GST_QUERY_TYPE (query)) {
1392 case GST_QUERY_CONVERT:
1394 GstFormat src_fmt, dest_fmt;
1395 gint64 src_val, dest_val;
1397 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1399 gst_video_encoded_video_convert (priv->bytes_out, priv->time, src_fmt,
1400 src_val, &dest_fmt, &dest_val);
1403 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1406 case GST_QUERY_ALLOCATION:{
1407 GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1409 if (klass->propose_allocation)
1410 res = klass->propose_allocation (decoder, query);
1414 res = gst_pad_query_default (pad, parent, query);
1421 GST_DEBUG_OBJECT (decoder, "query failed");
1425 typedef struct _Timestamp Timestamp;
1431 GstClockTime duration;
1435 timestamp_free (Timestamp * ts)
1437 g_slice_free (Timestamp, ts);
1441 gst_video_decoder_add_timestamp (GstVideoDecoder * decoder, GstBuffer * buffer)
1443 GstVideoDecoderPrivate *priv = decoder->priv;
1446 ts = g_slice_new (Timestamp);
1448 GST_LOG_OBJECT (decoder,
1449 "adding PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT
1450 " (offset:%" G_GUINT64_FORMAT ")",
1451 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1452 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), priv->input_offset);
1454 ts->offset = priv->input_offset;
1455 ts->pts = GST_BUFFER_PTS (buffer);
1456 ts->dts = GST_BUFFER_DTS (buffer);
1457 ts->duration = GST_BUFFER_DURATION (buffer);
1459 priv->timestamps = g_list_append (priv->timestamps, ts);
1463 gst_video_decoder_get_timestamp_at_offset (GstVideoDecoder *
1464 decoder, guint64 offset, GstClockTime * pts, GstClockTime * dts,
1465 GstClockTime * duration)
1467 #ifndef GST_DISABLE_GST_DEBUG
1468 guint64 got_offset = 0;
1473 *pts = GST_CLOCK_TIME_NONE;
1474 *dts = GST_CLOCK_TIME_NONE;
1475 *duration = GST_CLOCK_TIME_NONE;
1477 g = decoder->priv->timestamps;
1480 if (ts->offset <= offset) {
1481 #ifndef GST_DISABLE_GST_DEBUG
1482 got_offset = ts->offset;
1486 *duration = ts->duration;
1487 timestamp_free (ts);
1489 decoder->priv->timestamps = g_list_remove (decoder->priv->timestamps, ts);
1495 GST_LOG_OBJECT (decoder,
1496 "got PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT " @ offs %"
1497 G_GUINT64_FORMAT " (wanted offset:%" G_GUINT64_FORMAT ")",
1498 GST_TIME_ARGS (*pts), GST_TIME_ARGS (*dts), got_offset, offset);
1502 gst_video_decoder_clear_queues (GstVideoDecoder * dec)
1504 GstVideoDecoderPrivate *priv = dec->priv;
1506 g_list_free_full (priv->output_queued,
1507 (GDestroyNotify) gst_mini_object_unref);
1508 priv->output_queued = NULL;
1510 g_list_free_full (priv->gather, (GDestroyNotify) gst_mini_object_unref);
1511 priv->gather = NULL;
1512 g_list_free_full (priv->decode, (GDestroyNotify) gst_video_codec_frame_unref);
1513 priv->decode = NULL;
1514 g_list_free_full (priv->parse, (GDestroyNotify) gst_mini_object_unref);
1516 g_list_free_full (priv->parse_gather,
1517 (GDestroyNotify) gst_video_codec_frame_unref);
1518 priv->parse_gather = NULL;
1519 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1520 priv->frames = NULL;
1524 gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full)
1526 GstVideoDecoderPrivate *priv = decoder->priv;
1528 GST_DEBUG_OBJECT (decoder, "reset full %d", full);
1530 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1533 gst_segment_init (&decoder->input_segment, GST_FORMAT_UNDEFINED);
1534 gst_segment_init (&decoder->output_segment, GST_FORMAT_UNDEFINED);
1535 gst_video_decoder_clear_queues (decoder);
1536 priv->error_count = 0;
1537 priv->max_errors = GST_VIDEO_DECODER_MAX_ERRORS;
1538 if (priv->input_state)
1539 gst_video_codec_state_unref (priv->input_state);
1540 priv->input_state = NULL;
1541 if (priv->output_state)
1542 gst_video_codec_state_unref (priv->output_state);
1543 priv->output_state = NULL;
1544 priv->min_latency = 0;
1545 priv->max_latency = 0;
1548 priv->discont = TRUE;
1550 priv->base_timestamp = GST_CLOCK_TIME_NONE;
1551 priv->last_timestamp_out = GST_CLOCK_TIME_NONE;
1553 priv->input_offset = 0;
1554 priv->frame_offset = 0;
1555 gst_adapter_clear (priv->input_adapter);
1556 gst_adapter_clear (priv->output_adapter);
1557 g_list_free_full (priv->timestamps, (GDestroyNotify) timestamp_free);
1558 priv->timestamps = NULL;
1560 if (priv->current_frame) {
1561 gst_video_codec_frame_unref (priv->current_frame);
1562 priv->current_frame = NULL;
1566 priv->processed = 0;
1568 priv->decode_frame_number = 0;
1569 priv->base_picture_number = 0;
1571 g_list_free_full (priv->frames, (GDestroyNotify) gst_video_codec_frame_unref);
1572 priv->frames = NULL;
1574 priv->bytes_out = 0;
1577 priv->earliest_time = GST_CLOCK_TIME_NONE;
1578 priv->proportion = 0.5;
1580 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1583 static GstFlowReturn
1584 gst_video_decoder_chain_forward (GstVideoDecoder * decoder,
1585 GstBuffer * buf, gboolean at_eos)
1587 GstVideoDecoderPrivate *priv;
1588 GstVideoDecoderClass *klass;
1589 GstFlowReturn ret = GST_FLOW_OK;
1591 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1592 priv = decoder->priv;
1594 g_return_val_if_fail (priv->packetized || klass->parse, GST_FLOW_ERROR);
1596 if (priv->current_frame == NULL)
1597 priv->current_frame = gst_video_decoder_new_frame (decoder);
1599 if (GST_BUFFER_PTS_IS_VALID (buf)) {
1600 gst_video_decoder_add_timestamp (decoder, buf);
1602 priv->input_offset += gst_buffer_get_size (buf);
1604 if (priv->packetized) {
1605 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1606 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (priv->current_frame);
1609 priv->current_frame->input_buffer = buf;
1611 if (decoder->input_segment.rate < 0.0) {
1612 priv->parse_gather =
1613 g_list_prepend (priv->parse_gather, priv->current_frame);
1615 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
1617 priv->current_frame = NULL;
1620 gst_adapter_push (priv->input_adapter, buf);
1622 if (G_UNLIKELY (!gst_adapter_available (priv->input_adapter)))
1626 ret = klass->parse (decoder, priv->current_frame,
1627 priv->input_adapter, at_eos);
1628 } while (ret == GST_FLOW_OK && gst_adapter_available (priv->input_adapter));
1632 if (ret == GST_VIDEO_DECODER_FLOW_NEED_DATA)
1638 static GstFlowReturn
1639 gst_video_decoder_flush_decode (GstVideoDecoder * dec)
1641 GstVideoDecoderPrivate *priv = dec->priv;
1642 GstFlowReturn res = GST_FLOW_OK;
1645 GST_DEBUG_OBJECT (dec, "flushing buffers to decode");
1647 /* clear buffer and decoder state */
1648 gst_video_decoder_flush (dec, FALSE);
1650 walk = priv->decode;
1653 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1654 GstBuffer *buf = frame->input_buffer;
1656 GST_DEBUG_OBJECT (dec, "decoding frame %p buffer %p, PTS %" GST_TIME_FORMAT
1657 ", DTS %" GST_TIME_FORMAT, frame, buf,
1658 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1659 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
1663 priv->decode = g_list_delete_link (priv->decode, walk);
1665 /* decode buffer, resulting data prepended to queue */
1666 res = gst_video_decoder_decode_frame (dec, frame);
1667 if (res != GST_FLOW_OK)
1676 /* gst_video_decoder_flush_parse is called from the
1677 * chain_reverse() function when a buffer containing
1678 * a DISCONT - indicating that reverse playback
1679 * looped back to the next data block, and therefore
1680 * all available data should be fed through the
1681 * decoder and frames gathered for reversed output
1683 static GstFlowReturn
1684 gst_video_decoder_flush_parse (GstVideoDecoder * dec, gboolean at_eos)
1686 GstVideoDecoderPrivate *priv = dec->priv;
1687 GstFlowReturn res = GST_FLOW_OK;
1690 GST_DEBUG_OBJECT (dec, "flushing buffers to parsing");
1692 /* Reverse the gather list, and prepend it to the parse list,
1693 * then flush to parse whatever we can */
1694 priv->gather = g_list_reverse (priv->gather);
1695 priv->parse = g_list_concat (priv->gather, priv->parse);
1696 priv->gather = NULL;
1698 /* clear buffer and decoder state */
1699 gst_video_decoder_flush (dec, FALSE);
1703 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1704 GList *next = walk->next;
1706 GST_DEBUG_OBJECT (dec, "parsing buffer %p, PTS %" GST_TIME_FORMAT
1707 ", DTS %" GST_TIME_FORMAT, buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1708 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
1710 /* parse buffer, resulting frames prepended to parse_gather queue */
1711 gst_buffer_ref (buf);
1712 res = gst_video_decoder_chain_forward (dec, buf, at_eos);
1714 /* if we generated output, we can discard the buffer, else we
1715 * keep it in the queue */
1716 if (priv->parse_gather) {
1717 GST_DEBUG_OBJECT (dec, "parsed buffer to %p", priv->parse_gather->data);
1718 priv->parse = g_list_delete_link (priv->parse, walk);
1719 gst_buffer_unref (buf);
1721 GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
1726 /* now we can process frames. Start by moving each frame from the parse_gather
1727 * to the decode list, reverse the order as we go, and stopping when/if we
1728 * copy a keyframe. */
1729 GST_DEBUG_OBJECT (dec, "checking parsed frames for a keyframe to decode");
1730 walk = priv->parse_gather;
1732 GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
1734 /* remove from the gather list */
1735 priv->parse_gather = g_list_remove_link (priv->parse_gather, walk);
1737 /* move it to the front of the decode queue */
1738 priv->decode = g_list_concat (walk, priv->decode);
1740 /* if we copied a keyframe, flush and decode the decode queue */
1741 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1742 GST_DEBUG_OBJECT (dec, "found keyframe %p with PTS %" GST_TIME_FORMAT
1743 ", DTS %" GST_TIME_FORMAT, frame,
1744 GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
1745 GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
1746 res = gst_video_decoder_flush_decode (dec);
1747 if (res != GST_FLOW_OK)
1751 walk = priv->parse_gather;
1754 /* now send queued data downstream */
1755 walk = priv->output_queued;
1757 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
1759 if (G_LIKELY (res == GST_FLOW_OK)) {
1760 /* avoid stray DISCONT from forward processing,
1761 * which have no meaning in reverse pushing */
1762 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
1764 /* Last chance to calculate a timestamp as we loop backwards
1765 * through the list */
1766 if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE)
1767 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1768 else if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
1769 GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
1770 GST_BUFFER_TIMESTAMP (buf) =
1771 priv->last_timestamp_out - GST_BUFFER_DURATION (buf);
1772 priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
1773 GST_LOG_OBJECT (dec,
1774 "Calculated TS %" GST_TIME_FORMAT " working backwards",
1775 GST_TIME_ARGS (priv->last_timestamp_out));
1778 res = gst_video_decoder_clip_and_push_buf (dec, buf);
1780 gst_buffer_unref (buf);
1783 priv->output_queued =
1784 g_list_delete_link (priv->output_queued, priv->output_queued);
1785 walk = priv->output_queued;
1792 static GstFlowReturn
1793 gst_video_decoder_chain_reverse (GstVideoDecoder * dec, GstBuffer * buf)
1795 GstVideoDecoderPrivate *priv = dec->priv;
1796 GstFlowReturn result = GST_FLOW_OK;
1798 /* if we have a discont, move buffers to the decode list */
1799 if (!buf || GST_BUFFER_IS_DISCONT (buf)) {
1800 GST_DEBUG_OBJECT (dec, "received discont");
1802 /* parse and decode stuff in the gather and parse queues */
1803 gst_video_decoder_flush_parse (dec, FALSE);
1806 if (G_LIKELY (buf)) {
1807 GST_DEBUG_OBJECT (dec, "gathering buffer %p of size %" G_GSIZE_FORMAT ", "
1808 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
1809 GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
1810 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1811 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1812 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1814 /* add buffer to gather queue */
1815 priv->gather = g_list_prepend (priv->gather, buf);
1821 static GstFlowReturn
1822 gst_video_decoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1824 GstVideoDecoder *decoder;
1825 GstFlowReturn ret = GST_FLOW_OK;
1827 decoder = GST_VIDEO_DECODER (parent);
1829 GST_LOG_OBJECT (decoder,
1830 "chain PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT " duration %"
1831 GST_TIME_FORMAT " size %" G_GSIZE_FORMAT,
1832 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
1833 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1834 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), gst_buffer_get_size (buf));
1836 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1839 * requiring the pad to be negotiated makes it impossible to use
1840 * oggdemux or filesrc ! decoder */
1842 if (decoder->input_segment.format == GST_FORMAT_UNDEFINED) {
1844 GstSegment *segment = &decoder->input_segment;
1846 GST_WARNING_OBJECT (decoder,
1847 "Received buffer without a new-segment. "
1848 "Assuming timestamps start from 0.");
1850 gst_segment_init (segment, GST_FORMAT_TIME);
1852 event = gst_event_new_segment (segment);
1854 decoder->priv->current_frame_events =
1855 g_list_prepend (decoder->priv->current_frame_events, event);
1858 if (decoder->input_segment.rate > 0.0)
1859 ret = gst_video_decoder_chain_forward (decoder, buf, FALSE);
1861 ret = gst_video_decoder_chain_reverse (decoder, buf);
1863 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1867 static GstStateChangeReturn
1868 gst_video_decoder_change_state (GstElement * element, GstStateChange transition)
1870 GstVideoDecoder *decoder;
1871 GstVideoDecoderClass *decoder_class;
1872 GstStateChangeReturn ret;
1874 decoder = GST_VIDEO_DECODER (element);
1875 decoder_class = GST_VIDEO_DECODER_GET_CLASS (element);
1877 switch (transition) {
1878 case GST_STATE_CHANGE_NULL_TO_READY:
1879 /* open device/library if needed */
1880 if (decoder_class->open && !decoder_class->open (decoder))
1883 case GST_STATE_CHANGE_READY_TO_PAUSED:
1884 /* Initialize device/library if needed */
1885 if (decoder_class->start && !decoder_class->start (decoder))
1892 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1894 switch (transition) {
1895 case GST_STATE_CHANGE_PAUSED_TO_READY:
1896 if (decoder_class->stop && !decoder_class->stop (decoder))
1899 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1900 gst_video_decoder_reset (decoder, TRUE);
1901 g_list_free_full (decoder->priv->current_frame_events,
1902 (GDestroyNotify) gst_event_unref);
1903 decoder->priv->current_frame_events = NULL;
1904 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1906 case GST_STATE_CHANGE_READY_TO_NULL:
1907 /* close device/library if needed */
1908 if (decoder_class->close && !decoder_class->close (decoder))
1920 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1921 ("Failed to open decoder"));
1922 return GST_STATE_CHANGE_FAILURE;
1927 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1928 ("Failed to start decoder"));
1929 return GST_STATE_CHANGE_FAILURE;
1934 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1935 ("Failed to stop decoder"));
1936 return GST_STATE_CHANGE_FAILURE;
1941 GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
1942 ("Failed to close decoder"));
1943 return GST_STATE_CHANGE_FAILURE;
1947 static GstVideoCodecFrame *
1948 gst_video_decoder_new_frame (GstVideoDecoder * decoder)
1950 GstVideoDecoderPrivate *priv = decoder->priv;
1951 GstVideoCodecFrame *frame;
1953 frame = g_slice_new0 (GstVideoCodecFrame);
1955 frame->ref_count = 1;
1957 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1958 frame->system_frame_number = priv->system_frame_number;
1959 priv->system_frame_number++;
1960 frame->decode_frame_number = priv->decode_frame_number;
1961 priv->decode_frame_number++;
1963 frame->dts = GST_CLOCK_TIME_NONE;
1964 frame->pts = GST_CLOCK_TIME_NONE;
1965 frame->duration = GST_CLOCK_TIME_NONE;
1966 frame->events = priv->current_frame_events;
1967 priv->current_frame_events = NULL;
1968 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1970 GST_LOG_OBJECT (decoder, "Created new frame %p (sfn:%d)",
1971 frame, frame->system_frame_number);
1977 gst_video_decoder_prepare_finish_frame (GstVideoDecoder *
1978 decoder, GstVideoCodecFrame * frame, gboolean dropping)
1980 GstVideoDecoderPrivate *priv = decoder->priv;
1981 GList *l, *events = NULL;
1983 #ifndef GST_DISABLE_GST_DEBUG
1984 GST_LOG_OBJECT (decoder, "n %d in %" G_GSIZE_FORMAT " out %" G_GSIZE_FORMAT,
1985 g_list_length (priv->frames),
1986 gst_adapter_available (priv->input_adapter),
1987 gst_adapter_available (priv->output_adapter));
1990 GST_LOG_OBJECT (decoder,
1991 "finish frame %p (#%d) sync:%d PTS:%" GST_TIME_FORMAT " DTS:%"
1993 frame, frame->system_frame_number,
1994 GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame), GST_TIME_ARGS (frame->pts),
1995 GST_TIME_ARGS (frame->dts));
1997 /* Push all pending events that arrived before this frame */
1998 for (l = priv->frames; l; l = l->next) {
1999 GstVideoCodecFrame *tmp = l->data;
2002 events = g_list_concat (events, tmp->events);
2010 for (l = g_list_last (events); l; l = g_list_previous (l)) {
2011 GST_LOG_OBJECT (decoder, "pushing %s event", GST_EVENT_TYPE_NAME (l->data));
2012 gst_video_decoder_push_event (decoder, l->data);
2014 g_list_free (events);
2016 /* Check if the data should not be displayed. For example altref/invisible
2017 * frame in vp8. In this case we should not update the timestamps. */
2018 if (GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame))
2021 /* If the frame is meant to be output but we don't have an output_buffer
2022 * we have a problem :) */
2023 if (G_UNLIKELY ((frame->output_buffer == NULL) && !dropping))
2024 goto no_output_buffer;
2026 if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
2027 if (frame->pts != priv->base_timestamp) {
2028 GST_DEBUG_OBJECT (decoder,
2029 "sync timestamp %" GST_TIME_FORMAT " diff %" GST_TIME_FORMAT,
2030 GST_TIME_ARGS (frame->pts),
2031 GST_TIME_ARGS (frame->pts - decoder->output_segment.start));
2032 priv->base_timestamp = frame->pts;
2033 priv->base_picture_number = frame->decode_frame_number;
2037 if (frame->duration == GST_CLOCK_TIME_NONE) {
2038 frame->duration = gst_video_decoder_get_frame_duration (decoder, frame);
2039 GST_LOG_OBJECT (decoder,
2040 "Guessing duration %" GST_TIME_FORMAT " for frame...",
2041 GST_TIME_ARGS (frame->duration));
2044 if (frame->pts == GST_CLOCK_TIME_NONE) {
2045 /* Last ditch timestamp guess: Just add the duration to the previous
2047 if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
2048 frame->duration != GST_CLOCK_TIME_NONE) {
2049 frame->pts = priv->last_timestamp_out + frame->duration;
2050 GST_LOG_OBJECT (decoder,
2051 "Guessing timestamp %" GST_TIME_FORMAT " for frame...",
2052 GST_TIME_ARGS (frame->pts));
2056 if (GST_CLOCK_TIME_IS_VALID (priv->last_timestamp_out)) {
2057 if (frame->pts < priv->last_timestamp_out) {
2058 GST_WARNING_OBJECT (decoder,
2059 "decreasing timestamp (%" GST_TIME_FORMAT " < %"
2060 GST_TIME_FORMAT ")",
2061 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (priv->last_timestamp_out));
2065 if (GST_CLOCK_TIME_IS_VALID (frame->pts))
2066 priv->last_timestamp_out = frame->pts;
2073 GST_ERROR_OBJECT (decoder, "No buffer to output !");
2078 gst_video_decoder_release_frame (GstVideoDecoder * dec,
2079 GstVideoCodecFrame * frame)
2083 /* unref once from the list */
2084 link = g_list_find (dec->priv->frames, frame);
2086 gst_video_codec_frame_unref (frame);
2087 dec->priv->frames = g_list_delete_link (dec->priv->frames, link);
2090 /* unref because this function takes ownership */
2091 gst_video_codec_frame_unref (frame);
2095 * gst_video_decoder_drop_frame:
2096 * @dec: a #GstVideoDecoder
2097 * @frame: (transfer full): the #GstVideoCodecFrame to drop
2099 * Similar to gst_video_decoder_finish_frame(), but drops @frame in any
2100 * case and posts a QoS message with the frame's details on the bus.
2101 * In any case, the frame is considered finished and released.
2103 * Returns: a #GstFlowReturn, usually GST_FLOW_OK.
2106 gst_video_decoder_drop_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame)
2108 GstClockTime stream_time, jitter, earliest_time, qostime, timestamp;
2109 GstSegment *segment;
2110 GstMessage *qos_msg;
2113 GST_LOG_OBJECT (dec, "drop frame %p", frame);
2115 GST_VIDEO_DECODER_STREAM_LOCK (dec);
2117 gst_video_decoder_prepare_finish_frame (dec, frame, TRUE);
2119 GST_DEBUG_OBJECT (dec, "dropping frame %" GST_TIME_FORMAT,
2120 GST_TIME_ARGS (frame->pts));
2122 dec->priv->dropped++;
2124 /* post QoS message */
2125 timestamp = frame->pts;
2126 proportion = dec->priv->proportion;
2127 segment = &dec->output_segment;
2129 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, timestamp);
2130 qostime = gst_segment_to_running_time (segment, GST_FORMAT_TIME, timestamp);
2131 earliest_time = dec->priv->earliest_time;
2132 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
2134 gst_message_new_qos (GST_OBJECT_CAST (dec), FALSE, qostime, stream_time,
2135 timestamp, GST_CLOCK_TIME_NONE);
2136 gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
2137 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
2138 dec->priv->processed, dec->priv->dropped);
2139 gst_element_post_message (GST_ELEMENT_CAST (dec), qos_msg);
2141 /* now free the frame */
2142 gst_video_decoder_release_frame (dec, frame);
2144 GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
2150 * gst_video_decoder_finish_frame:
2151 * @decoder: a #GstVideoDecoder
2152 * @frame: (transfer full): a decoded #GstVideoCodecFrame
2154 * @frame should have a valid decoded data buffer, whose metadata fields
2155 * are then appropriately set according to frame data and pushed downstream.
2156 * If no output data is provided, @frame is considered skipped.
2157 * In any case, the frame is considered finished and released.
2159 * After calling this function the output buffer of the frame is to be
2160 * considered read-only. This function will also change the metadata
2163 * Returns: a #GstFlowReturn resulting from sending data downstream
2166 gst_video_decoder_finish_frame (GstVideoDecoder * decoder,
2167 GstVideoCodecFrame * frame)
2169 GstFlowReturn ret = GST_FLOW_OK;
2170 GstVideoDecoderPrivate *priv = decoder->priv;
2171 GstBuffer *output_buffer;
2173 GST_LOG_OBJECT (decoder, "finish frame %p", frame);
2175 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2177 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
2178 && gst_pad_check_reconfigure (decoder->srcpad))))
2179 gst_video_decoder_negotiate (decoder);
2181 gst_video_decoder_prepare_finish_frame (decoder, frame, FALSE);
2184 /* no buffer data means this frame is skipped */
2185 if (!frame->output_buffer || GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame)) {
2186 GST_DEBUG_OBJECT (decoder, "skipping frame %" GST_TIME_FORMAT,
2187 GST_TIME_ARGS (frame->pts));
2191 output_buffer = frame->output_buffer;
2193 GST_BUFFER_FLAG_UNSET (output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
2195 /* set PTS and DTS to both the PTS for decoded frames */
2196 GST_BUFFER_PTS (output_buffer) = frame->pts;
2197 GST_BUFFER_DTS (output_buffer) = frame->pts;
2198 GST_BUFFER_DURATION (output_buffer) = frame->duration;
2200 GST_BUFFER_OFFSET (output_buffer) = GST_BUFFER_OFFSET_NONE;
2201 GST_BUFFER_OFFSET_END (output_buffer) = GST_BUFFER_OFFSET_NONE;
2203 if (priv->discont) {
2204 GST_BUFFER_FLAG_SET (output_buffer, GST_BUFFER_FLAG_DISCONT);
2205 priv->discont = FALSE;
2208 /* Get an additional ref to the buffer, which is going to be pushed
2209 * downstream, the original ref is owned by the frame
2211 * FIXME: clip_and_push_buf() changes buffer metadata but the buffer
2212 * might have a refcount > 1 */
2213 output_buffer = gst_buffer_ref (output_buffer);
2214 if (decoder->output_segment.rate < 0.0) {
2215 GST_LOG_OBJECT (decoder, "queued frame");
2216 priv->output_queued = g_list_prepend (priv->output_queued, output_buffer);
2218 ret = gst_video_decoder_clip_and_push_buf (decoder, output_buffer);
2222 gst_video_decoder_release_frame (decoder, frame);
2223 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2228 /* With stream lock, takes the frame reference */
2229 static GstFlowReturn
2230 gst_video_decoder_clip_and_push_buf (GstVideoDecoder * decoder, GstBuffer * buf)
2232 GstFlowReturn ret = GST_FLOW_OK;
2233 GstVideoDecoderPrivate *priv = decoder->priv;
2234 guint64 start, stop;
2235 guint64 cstart, cstop;
2236 GstSegment *segment;
2237 GstClockTime duration;
2239 /* Check for clipping */
2240 start = GST_BUFFER_PTS (buf);
2241 duration = GST_BUFFER_DURATION (buf);
2243 stop = GST_CLOCK_TIME_NONE;
2245 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (duration)) {
2246 stop = start + duration;
2249 segment = &decoder->output_segment;
2250 if (gst_segment_clip (segment, GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
2252 GST_BUFFER_PTS (buf) = cstart;
2254 if (stop != GST_CLOCK_TIME_NONE)
2255 GST_BUFFER_DURATION (buf) = cstop - cstart;
2257 GST_LOG_OBJECT (decoder,
2258 "accepting buffer inside segment: %" GST_TIME_FORMAT " %"
2259 GST_TIME_FORMAT " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2260 " time %" GST_TIME_FORMAT,
2261 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2262 GST_TIME_ARGS (GST_BUFFER_PTS (buf) +
2263 GST_BUFFER_DURATION (buf)),
2264 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
2265 GST_TIME_ARGS (segment->time));
2267 GST_LOG_OBJECT (decoder,
2268 "dropping buffer outside segment: %" GST_TIME_FORMAT
2269 " %" GST_TIME_FORMAT
2270 " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
2271 " time %" GST_TIME_FORMAT,
2272 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2273 GST_TIME_ARGS (segment->start),
2274 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time));
2275 gst_buffer_unref (buf);
2279 /* update rate estimate */
2280 priv->bytes_out += gst_buffer_get_size (buf);
2281 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2282 priv->time += duration;
2284 /* FIXME : Use difference between current and previous outgoing
2285 * timestamp, and relate to difference between current and previous
2287 /* better none than nothing valid */
2288 priv->time = GST_CLOCK_TIME_NONE;
2291 GST_DEBUG_OBJECT (decoder, "pushing buffer %p of size %" G_GSIZE_FORMAT ", "
2292 "PTS %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
2293 gst_buffer_get_size (buf),
2294 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2295 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2297 /* we got data, so note things are looking up again, reduce
2298 * the error count, if there is one */
2299 if (G_UNLIKELY (priv->error_count))
2300 priv->error_count--;
2302 ret = gst_pad_push (decoder->srcpad, buf);
2309 * gst_video_decoder_add_to_frame:
2310 * @decoder: a #GstVideoDecoder
2311 * @n_bytes: the number of bytes to add
2313 * Removes next @n_bytes of input data and adds it to currently parsed frame.
2316 gst_video_decoder_add_to_frame (GstVideoDecoder * decoder, int n_bytes)
2318 GstVideoDecoderPrivate *priv = decoder->priv;
2321 GST_LOG_OBJECT (decoder, "add %d bytes to frame", n_bytes);
2326 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2327 if (gst_adapter_available (priv->output_adapter) == 0) {
2328 priv->frame_offset =
2329 priv->input_offset - gst_adapter_available (priv->input_adapter);
2331 buf = gst_adapter_take_buffer (priv->input_adapter, n_bytes);
2333 gst_adapter_push (priv->output_adapter, buf);
2334 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2338 gst_video_decoder_get_frame_duration (GstVideoDecoder * decoder,
2339 GstVideoCodecFrame * frame)
2341 GstVideoCodecState *state = decoder->priv->output_state;
2343 /* it's possible that we don't have a state yet when we are dropping the
2344 * initial buffers */
2346 return GST_CLOCK_TIME_NONE;
2348 if (state->info.fps_d == 0 || state->info.fps_n == 0) {
2349 return GST_CLOCK_TIME_NONE;
2352 /* FIXME: For interlaced frames this needs to take into account
2353 * the number of valid fields in the frame
2356 return gst_util_uint64_scale (GST_SECOND, state->info.fps_d,
2361 * gst_video_decoder_have_frame:
2362 * @decoder: a #GstVideoDecoder
2364 * Gathers all data collected for currently parsed frame, gathers corresponding
2365 * metadata and passes it along for further processing, i.e. @handle_frame.
2367 * Returns: a #GstFlowReturn
2370 gst_video_decoder_have_frame (GstVideoDecoder * decoder)
2372 GstVideoDecoderPrivate *priv = decoder->priv;
2375 GstClockTime pts, dts, duration;
2376 GstFlowReturn ret = GST_FLOW_OK;
2378 GST_LOG_OBJECT (decoder, "have_frame");
2380 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2382 n_available = gst_adapter_available (priv->output_adapter);
2384 buffer = gst_adapter_take_buffer (priv->output_adapter, n_available);
2386 buffer = gst_buffer_new_and_alloc (0);
2389 priv->current_frame->input_buffer = buffer;
2391 gst_video_decoder_get_timestamp_at_offset (decoder,
2392 priv->frame_offset, &pts, &dts, &duration);
2394 GST_BUFFER_PTS (buffer) = pts;
2395 GST_BUFFER_DTS (buffer) = dts;
2396 GST_BUFFER_DURATION (buffer) = duration;
2398 GST_LOG_OBJECT (decoder, "collected frame size %d, "
2399 "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
2400 GST_TIME_FORMAT, n_available, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
2401 GST_TIME_ARGS (duration));
2403 /* In reverse playback, just capture and queue frames for later processing */
2404 if (decoder->output_segment.rate < 0.0) {
2405 priv->parse_gather =
2406 g_list_prepend (priv->parse_gather, priv->current_frame);
2408 /* Otherwise, decode the frame, which gives away our ref */
2409 ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
2411 /* Current frame is gone now, either way */
2412 priv->current_frame = NULL;
2414 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2419 /* Pass the frame in priv->current_frame through the
2420 * handle_frame() callback for decoding and passing to gvd_finish_frame(),
2421 * or dropping by passing to gvd_drop_frame() */
2422 static GstFlowReturn
2423 gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
2424 GstVideoCodecFrame * frame)
2426 GstVideoDecoderPrivate *priv = decoder->priv;
2427 GstVideoDecoderClass *decoder_class;
2428 GstFlowReturn ret = GST_FLOW_OK;
2430 decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
2432 /* FIXME : This should only have to be checked once (either the subclass has an
2433 * implementation, or it doesn't) */
2434 g_return_val_if_fail (decoder_class->handle_frame != NULL, GST_FLOW_ERROR);
2436 frame->distance_from_sync = priv->distance_from_sync;
2437 priv->distance_from_sync++;
2438 frame->pts = GST_BUFFER_PTS (frame->input_buffer);
2439 frame->dts = GST_BUFFER_DTS (frame->input_buffer);
2440 frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
2442 /* For keyframes, PTS = DTS */
2443 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
2444 if (!GST_CLOCK_TIME_IS_VALID (frame->pts))
2445 frame->pts = frame->dts;
2448 GST_LOG_OBJECT (decoder, "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT,
2449 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->dts));
2450 GST_LOG_OBJECT (decoder, "dist %d", frame->distance_from_sync);
2452 gst_video_codec_frame_ref (frame);
2453 priv->frames = g_list_append (priv->frames, frame);
2455 gst_segment_to_running_time (&decoder->input_segment, GST_FORMAT_TIME,
2458 /* do something with frame */
2459 ret = decoder_class->handle_frame (decoder, frame);
2460 if (ret != GST_FLOW_OK)
2461 GST_DEBUG_OBJECT (decoder, "flow error %s", gst_flow_get_name (ret));
2463 /* the frame has either been added to parse_gather or sent to
2464 handle frame so there is no need to unref it */
2470 * gst_video_decoder_get_output_state:
2471 * @decoder: a #GstVideoDecoder
2473 * Get the #GstVideoCodecState currently describing the output stream.
2475 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
2477 GstVideoCodecState *
2478 gst_video_decoder_get_output_state (GstVideoDecoder * decoder)
2480 GstVideoCodecState *state = NULL;
2482 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2483 if (decoder->priv->output_state)
2484 state = gst_video_codec_state_ref (decoder->priv->output_state);
2485 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2491 * gst_video_decoder_set_output_state:
2492 * @decoder: a #GstVideoDecoder
2493 * @fmt: a #GstVideoFormat
2494 * @width: The width in pixels
2495 * @height: The height in pixels
2496 * @reference: (allow-none) (transfer none): An optional reference #GstVideoCodecState
2498 * Creates a new #GstVideoCodecState with the specified @fmt, @width and @height
2499 * as the output state for the decoder.
2500 * Any previously set output state on @decoder will be replaced by the newly
2503 * If the subclass wishes to copy over existing fields (like pixel aspec ratio,
2504 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
2507 * If the subclass wishes to override some fields from the output state (like
2508 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
2510 * The new output state will only take effect (set on pads and buffers) starting
2511 * from the next call to #gst_video_decoder_finish_frame().
2513 * Returns: (transfer full): the newly configured output state.
2515 GstVideoCodecState *
2516 gst_video_decoder_set_output_state (GstVideoDecoder * decoder,
2517 GstVideoFormat fmt, guint width, guint height,
2518 GstVideoCodecState * reference)
2520 GstVideoDecoderPrivate *priv = decoder->priv;
2521 GstVideoCodecState *state;
2523 GST_DEBUG_OBJECT (decoder, "fmt:%d, width:%d, height:%d, reference:%p",
2524 fmt, width, height, reference);
2526 /* Create the new output state */
2527 state = _new_output_state (fmt, width, height, reference);
2529 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2530 /* Replace existing output state by new one */
2531 if (priv->output_state)
2532 gst_video_codec_state_unref (priv->output_state);
2533 priv->output_state = gst_video_codec_state_ref (state);
2535 priv->output_state_changed = TRUE;
2536 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2543 * gst_video_decoder_get_oldest_frame:
2544 * @decoder: a #GstVideoDecoder
2546 * Get the oldest pending unfinished #GstVideoCodecFrame
2548 * Returns: (transfer full): oldest pending unfinished #GstVideoCodecFrame.
2550 GstVideoCodecFrame *
2551 gst_video_decoder_get_oldest_frame (GstVideoDecoder * decoder)
2553 GstVideoCodecFrame *frame = NULL;
2555 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2556 if (decoder->priv->frames)
2557 frame = gst_video_codec_frame_ref (decoder->priv->frames->data);
2558 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2560 return (GstVideoCodecFrame *) frame;
2564 * gst_video_decoder_get_frame:
2565 * @decoder: a #GstVideoDecoder
2566 * @frame_number: system_frame_number of a frame
2568 * Get a pending unfinished #GstVideoCodecFrame
2570 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2572 GstVideoCodecFrame *
2573 gst_video_decoder_get_frame (GstVideoDecoder * decoder, int frame_number)
2576 GstVideoCodecFrame *frame = NULL;
2578 GST_DEBUG_OBJECT (decoder, "frame_number : %d", frame_number);
2580 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2581 for (g = decoder->priv->frames; g; g = g->next) {
2582 GstVideoCodecFrame *tmp = g->data;
2584 if (tmp->system_frame_number == frame_number) {
2585 frame = gst_video_codec_frame_ref (tmp);
2589 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2595 gst_video_decoder_decide_allocation_default (GstVideoDecoder * decoder,
2599 GstBufferPool *pool = NULL;
2600 guint size, min, max;
2601 GstAllocator *allocator = NULL;
2602 GstAllocationParams params;
2603 GstStructure *config;
2604 gboolean update_pool, update_allocator;
2607 gst_query_parse_allocation (query, &outcaps, NULL);
2608 gst_video_info_init (&vinfo);
2609 gst_video_info_from_caps (&vinfo, outcaps);
2611 /* we got configuration from our peer or the decide_allocation method,
2613 if (gst_query_get_n_allocation_params (query) > 0) {
2614 /* try the allocator */
2615 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2616 update_allocator = TRUE;
2619 gst_allocation_params_init (¶ms);
2620 update_allocator = FALSE;
2623 if (gst_query_get_n_allocation_pools (query) > 0) {
2624 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2625 size = MAX (size, vinfo.size);
2632 update_pool = FALSE;
2636 /* no pool, we can make our own */
2637 GST_DEBUG_OBJECT (decoder, "no pool, making new pool");
2638 pool = gst_video_buffer_pool_new ();
2642 config = gst_buffer_pool_get_config (pool);
2643 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
2644 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2645 gst_buffer_pool_set_config (pool, config);
2647 if (update_allocator)
2648 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2650 gst_query_add_allocation_param (query, allocator, ¶ms);
2652 gst_object_unref (allocator);
2655 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2657 gst_query_add_allocation_pool (query, pool, size, min, max);
2660 gst_object_unref (pool);
2666 gst_video_decoder_propose_allocation_default (GstVideoDecoder * decoder,
2673 * gst_video_decoder_negotiate:
2674 * @decoder: a #GstVideoDecoder
2676 * Negotiate with downstreame elements to currently configured #GstVideoCodecState.
2678 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
2681 gst_video_decoder_negotiate (GstVideoDecoder * decoder)
2683 GstVideoCodecState *state = decoder->priv->output_state;
2684 GstVideoDecoderClass *klass;
2685 GstQuery *query = NULL;
2686 GstBufferPool *pool = NULL;
2687 GstAllocator *allocator;
2688 GstAllocationParams params;
2689 gboolean ret = TRUE;
2691 g_return_val_if_fail (GST_VIDEO_INFO_WIDTH (&state->info) != 0, FALSE);
2692 g_return_val_if_fail (GST_VIDEO_INFO_HEIGHT (&state->info) != 0, FALSE);
2694 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2696 klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
2698 GST_DEBUG_OBJECT (decoder, "output_state par %d/%d fps %d/%d",
2699 state->info.par_n, state->info.par_d,
2700 state->info.fps_n, state->info.fps_d);
2702 if (G_UNLIKELY (state->caps == NULL))
2703 state->caps = gst_video_info_to_caps (&state->info);
2705 GST_DEBUG_OBJECT (decoder, "setting caps %" GST_PTR_FORMAT, state->caps);
2707 ret = gst_pad_set_caps (decoder->srcpad, state->caps);
2710 decoder->priv->output_state_changed = FALSE;
2712 /* Negotiate pool */
2713 query = gst_query_new_allocation (state->caps, TRUE);
2715 if (!gst_pad_peer_query (decoder->srcpad, query)) {
2716 GST_DEBUG_OBJECT (decoder, "didn't get downstream ALLOCATION hints");
2719 g_assert (klass->decide_allocation != NULL);
2720 ret = klass->decide_allocation (decoder, query);
2722 GST_DEBUG_OBJECT (decoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
2726 goto no_decide_allocation;
2728 /* we got configuration from our peer or the decide_allocation method,
2730 if (gst_query_get_n_allocation_params (query) > 0) {
2731 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2734 gst_allocation_params_init (¶ms);
2737 if (gst_query_get_n_allocation_pools (query) > 0)
2738 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
2741 gst_object_unref (allocator);
2743 goto no_decide_allocation;
2746 if (decoder->priv->allocator)
2747 gst_object_unref (decoder->priv->allocator);
2748 decoder->priv->allocator = allocator;
2749 decoder->priv->params = params;
2751 if (decoder->priv->pool) {
2752 gst_buffer_pool_set_active (decoder->priv->pool, FALSE);
2753 gst_object_unref (decoder->priv->pool);
2755 decoder->priv->pool = pool;
2758 gst_buffer_pool_set_active (pool, TRUE);
2762 gst_query_unref (query);
2764 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2769 no_decide_allocation:
2771 GST_WARNING_OBJECT (decoder, "Subclass failed to decide allocation");
2777 * gst_video_decoder_allocate_output_buffer:
2778 * @decoder: a #GstVideoDecoder
2780 * Helper function that allocates a buffer to hold a video frame for @decoder's
2781 * current #GstVideoCodecState.
2783 * Returns: (transfer full): allocated buffer
2786 gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder)
2790 GST_DEBUG ("alloc src buffer");
2792 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2793 if (G_UNLIKELY (decoder->priv->output_state_changed
2794 || (decoder->priv->output_state
2795 && gst_pad_check_reconfigure (decoder->srcpad))))
2796 gst_video_decoder_negotiate (decoder);
2798 gst_buffer_pool_acquire_buffer (decoder->priv->pool, &buffer, NULL);
2800 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2806 * gst_video_decoder_allocate_output_frame:
2807 * @decoder: a #GstVideoDecoder
2808 * @frame: a #GstVideoCodecFrame
2810 * Helper function that allocates a buffer to hold a video frame for @decoder's
2811 * current #GstVideoCodecState. Subclass should already have configured video
2812 * state and set src pad caps.
2814 * The buffer allocated here is owned by the frame and you should only
2815 * keep references to the frame, not the buffer.
2817 * Returns: %GST_FLOW_OK if an output buffer could be allocated
2820 gst_video_decoder_allocate_output_frame (GstVideoDecoder *
2821 decoder, GstVideoCodecFrame * frame)
2823 GstFlowReturn flow_ret;
2824 GstVideoCodecState *state = decoder->priv->output_state;
2825 int num_bytes = GST_VIDEO_INFO_SIZE (&state->info);
2827 g_return_val_if_fail (num_bytes != 0, GST_FLOW_ERROR);
2828 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
2830 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2831 if (G_UNLIKELY (decoder->priv->output_state_changed
2832 || (decoder->priv->output_state
2833 && gst_pad_check_reconfigure (decoder->srcpad))))
2834 gst_video_decoder_negotiate (decoder);
2836 GST_LOG_OBJECT (decoder, "alloc buffer size %d", num_bytes);
2838 flow_ret = gst_buffer_pool_acquire_buffer (decoder->priv->pool,
2839 &frame->output_buffer, NULL);
2841 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2847 * gst_video_decoder_get_max_decode_time:
2848 * @decoder: a #GstVideoDecoder
2849 * @frame: a #GstVideoCodecFrame
2851 * Determines maximum possible decoding time for @frame that will
2852 * allow it to decode and arrive in time (as determined by QoS events).
2853 * In particular, a negative result means decoding in time is no longer possible
2854 * and should therefore occur as soon/skippy as possible.
2856 * Returns: max decoding time.
2859 gst_video_decoder_get_max_decode_time (GstVideoDecoder *
2860 decoder, GstVideoCodecFrame * frame)
2862 GstClockTimeDiff deadline;
2863 GstClockTime earliest_time;
2865 GST_OBJECT_LOCK (decoder);
2866 earliest_time = decoder->priv->earliest_time;
2867 if (GST_CLOCK_TIME_IS_VALID (earliest_time)
2868 && GST_CLOCK_TIME_IS_VALID (frame->deadline))
2869 deadline = GST_CLOCK_DIFF (earliest_time, frame->deadline);
2871 deadline = G_MAXINT64;
2873 GST_LOG_OBJECT (decoder, "earliest %" GST_TIME_FORMAT
2874 ", frame deadline %" GST_TIME_FORMAT ", deadline %" GST_TIME_FORMAT,
2875 GST_TIME_ARGS (earliest_time), GST_TIME_ARGS (frame->deadline),
2876 GST_TIME_ARGS (deadline));
2878 GST_OBJECT_UNLOCK (decoder);
2884 _gst_video_decoder_error (GstVideoDecoder * dec, gint weight,
2885 GQuark domain, gint code, gchar * txt, gchar * dbg, const gchar * file,
2886 const gchar * function, gint line)
2889 GST_WARNING_OBJECT (dec, "error: %s", txt);
2891 GST_WARNING_OBJECT (dec, "error: %s", dbg);
2892 dec->priv->error_count += weight;
2893 dec->priv->discont = TRUE;
2894 if (dec->priv->max_errors < dec->priv->error_count) {
2895 gst_element_message_full (GST_ELEMENT (dec), GST_MESSAGE_ERROR,
2896 domain, code, txt, dbg, file, function, line);
2897 return GST_FLOW_ERROR;
2904 * gst_video_decoder_set_max_errors:
2905 * @dec: a #GstVideoDecoder
2906 * @num: max tolerated errors
2908 * Sets numbers of tolerated decoder errors, where a tolerated one is then only
2909 * warned about, but more than tolerated will lead to fatal error. Default
2910 * is set to GST_VIDEO_DECODER_MAX_ERRORS.
2913 gst_video_decoder_set_max_errors (GstVideoDecoder * dec, gint num)
2915 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
2917 dec->priv->max_errors = num;
2921 * gst_video_decoder_get_max_errors:
2922 * @dec: a #GstVideoDecoder
2924 * Returns: currently configured decoder tolerated error count.
2927 gst_video_decoder_get_max_errors (GstVideoDecoder * dec)
2929 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
2931 return dec->priv->max_errors;
2935 * gst_video_decoder_set_packetized:
2936 * @decoder: a #GstVideoDecoder
2937 * @packetized: whether the input data should be considered as packetized.
2939 * Allows baseclass to consider input data as packetized or not. If the
2940 * input is packetized, then the @parse method will not be called.
2943 gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
2944 gboolean packetized)
2946 decoder->priv->packetized = packetized;
2950 * gst_video_decoder_get_packetized:
2951 * @decoder: a #GstVideoDecoder
2953 * Queries whether input data is considered packetized or not by the
2956 * Returns: TRUE if input data is considered packetized.
2959 gst_video_decoder_get_packetized (GstVideoDecoder * decoder)
2961 return decoder->priv->packetized;
2965 * gst_video_decoder_set_estimate_rate:
2966 * @dec: a #GstVideoDecoder
2967 * @enabled: whether to enable byte to time conversion
2969 * Allows baseclass to perform byte to time estimated conversion.
2972 gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec, gboolean enabled)
2974 g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
2976 dec->priv->do_estimate_rate = enabled;
2980 * gst_video_decoder_get_estimate_rate:
2981 * @dec: a #GstVideoDecoder
2983 * Returns: currently configured byte to time conversion setting
2986 gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec)
2988 g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
2990 return dec->priv->do_estimate_rate;
2994 * gst_video_decoder_set_latency:
2995 * @decoder: a #GstVideoDecoder
2996 * @min_latency: minimum latency
2997 * @max_latency: maximum latency
2999 * Lets #GstVideoDecoder sub-classes tell the baseclass what the decoder
3000 * latency is. Will also post a LATENCY message on the bus so the pipeline
3001 * can reconfigure its global latency.
3004 gst_video_decoder_set_latency (GstVideoDecoder * decoder,
3005 GstClockTime min_latency, GstClockTime max_latency)
3007 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
3008 g_return_if_fail (max_latency >= min_latency);
3010 GST_OBJECT_LOCK (decoder);
3011 decoder->priv->min_latency = min_latency;
3012 decoder->priv->max_latency = max_latency;
3013 GST_OBJECT_UNLOCK (decoder);
3015 gst_element_post_message (GST_ELEMENT_CAST (decoder),
3016 gst_message_new_latency (GST_OBJECT_CAST (decoder)));
3020 * gst_video_decoder_get_latency:
3021 * @decoder: a #GstVideoDecoder
3022 * @min_latency: (out) (allow-none): address of variable in which to store the
3023 * configured minimum latency, or %NULL
3024 * @max_latency: (out) (allow-none): address of variable in which to store the
3025 * configured mximum latency, or %NULL
3027 * Query the configured decoder latency. Results will be returned via
3028 * @min_latency and @max_latency.
3031 gst_video_decoder_get_latency (GstVideoDecoder * decoder,
3032 GstClockTime * min_latency, GstClockTime * max_latency)
3034 GST_OBJECT_LOCK (decoder);
3036 *min_latency = decoder->priv->min_latency;
3038 *max_latency = decoder->priv->max_latency;
3039 GST_OBJECT_UNLOCK (decoder);