a1942b46601bc1062114b96e874d23adfb2769b8
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideodecoder.c
1 /* GStreamer
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>
8  *
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.
13  *
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.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 /**
26  * SECTION:gstvideodecoder
27  * @title: GstVideoDecoder
28  * @short_description: Base class for video decoders
29  *
30  * This base class is for video decoders turning encoded data into raw video
31  * frames.
32  *
33  * The GstVideoDecoder base class and derived subclasses should cooperate as
34  * follows:
35  *
36  * ## Configuration
37  *
38  *   * Initially, GstVideoDecoder calls @start when the decoder element
39  *     is activated, which allows the subclass to perform any global setup.
40  *
41  *   * GstVideoDecoder calls @set_format to inform the subclass of caps
42  *     describing input video data that it is about to receive, including
43  *     possibly configuration data.
44  *     While unlikely, it might be called more than once, if changing input
45  *     parameters require reconfiguration.
46  *
47  *   * Incoming data buffers are processed as needed, described in Data
48  *     Processing below.
49  *
50  *   * GstVideoDecoder calls @stop at end of all processing.
51  *
52  * ## Data processing
53  *
54  *   * The base class gathers input data, and optionally allows subclass
55  *     to parse this into subsequently manageable chunks, typically
56  *     corresponding to and referred to as 'frames'.
57  *
58  *   * Each input frame is provided in turn to the subclass' @handle_frame
59  *     callback.
60  *     The ownership of the frame is given to the @handle_frame callback.
61  *
62  *   * If codec processing results in decoded data, the subclass should call
63  *     @gst_video_decoder_finish_frame to have decoded data pushed.
64  *     downstream. Otherwise, the subclass must call
65  *     @gst_video_decoder_drop_frame, to allow the base class to do timestamp
66  *     and offset tracking, and possibly to requeue the frame for a later
67  *     attempt in the case of reverse playback.
68  *
69  * ## Shutdown phase
70  *
71  *   * The GstVideoDecoder class calls @stop to inform the subclass that data
72  *     parsing will be stopped.
73  *
74  * ## Additional Notes
75  *
76  *   * Seeking/Flushing
77  *
78  *     * When the pipeline is seeked or otherwise flushed, the subclass is
79  *       informed via a call to its @reset callback, with the hard parameter
80  *       set to true. This indicates the subclass should drop any internal data
81  *       queues and timestamps and prepare for a fresh set of buffers to arrive
82  *       for parsing and decoding.
83  *
84  *   * End Of Stream
85  *
86  *     * At end-of-stream, the subclass @parse function may be called some final
87  *       times with the at_eos parameter set to true, indicating that the element
88  *       should not expect any more data to be arriving, and it should parse and
89  *       remaining frames and call gst_video_decoder_have_frame() if possible.
90  *
91  * The subclass is responsible for providing pad template caps for
92  * source and sink pads. The pads need to be named "sink" and "src". It also
93  * needs to provide information about the output caps, when they are known.
94  * This may be when the base class calls the subclass' @set_format function,
95  * though it might be during decoding, before calling
96  * @gst_video_decoder_finish_frame. This is done via
97  * @gst_video_decoder_set_output_state
98  *
99  * The subclass is also responsible for providing (presentation) timestamps
100  * (likely based on corresponding input ones).  If that is not applicable
101  * or possible, the base class provides limited framerate based interpolation.
102  *
103  * Similarly, the base class provides some limited (legacy) seeking support
104  * if specifically requested by the subclass, as full-fledged support
105  * should rather be left to upstream demuxer, parser or alike.  This simple
106  * approach caters for seeking and duration reporting using estimated input
107  * bitrates. To enable it, a subclass should call
108  * @gst_video_decoder_set_estimate_rate to enable handling of incoming
109  * byte-streams.
110  *
111  * The base class provides some support for reverse playback, in particular
112  * in case incoming data is not packetized or upstream does not provide
113  * fragments on keyframe boundaries.  However, the subclass should then be
114  * prepared for the parsing and frame processing stage to occur separately
115  * (in normal forward processing, the latter immediately follows the former),
116  * The subclass also needs to ensure the parsing stage properly marks
117  * keyframes, unless it knows the upstream elements will do so properly for
118  * incoming data.
119  *
120  * The bare minimum that a functional subclass needs to implement is:
121  *
122  *   * Provide pad templates
123  *   * Inform the base class of output caps via
124  *      @gst_video_decoder_set_output_state
125  *
126  *   * Parse input data, if it is not considered packetized from upstream
127  *      Data will be provided to @parse which should invoke
128  *      @gst_video_decoder_add_to_frame and @gst_video_decoder_have_frame to
129  *      separate the data belonging to each video frame.
130  *
131  *   * Accept data in @handle_frame and provide decoded results to
132  *      @gst_video_decoder_finish_frame, or call @gst_video_decoder_drop_frame.
133  */
134
135 #ifdef HAVE_CONFIG_H
136 #include "config.h"
137 #endif
138
139 /* TODO
140  *
141  * * Add a flag/boolean for I-frame-only/image decoders so we can do extra
142  *   features, like applying QoS on input (as opposed to after the frame is
143  *   decoded).
144  * * Add a flag/boolean for decoders that require keyframes, so the base
145  *   class can automatically discard non-keyframes before one has arrived
146  * * Detect reordered frame/timestamps and fix the pts/dts
147  * * Support for GstIndex (or shall we not care ?)
148  * * Calculate actual latency based on input/output timestamp/frame_number
149  *   and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
150  * * Emit latency message when it changes
151  *
152  */
153
154 /* Implementation notes:
155  * The Video Decoder base class operates in 2 primary processing modes, depending
156  * on whether forward or reverse playback is requested.
157  *
158  * Forward playback:
159  *   * Incoming buffer -> @parse() -> add_to_frame()/have_frame() ->
160  *     handle_frame() -> push downstream
161  *
162  * Reverse playback is more complicated, since it involves gathering incoming
163  * data regions as we loop backwards through the upstream data. The processing
164  * concept (using incoming buffers as containing one frame each to simplify
165  * things) is:
166  *
167  * Upstream data we want to play:
168  *  Buffer encoded order:  1  2  3  4  5  6  7  8  9  EOS
169  *  Keyframe flag:            K        K
170  *  Groupings:             AAAAAAA  BBBBBBB  CCCCCCC
171  *
172  * Input:
173  *  Buffer reception order:  7  8  9  4  5  6  1  2  3  EOS
174  *  Keyframe flag:                       K        K
175  *  Discont flag:            D        D        D
176  *
177  * - Each Discont marks a discont in the decoding order.
178  * - The keyframes mark where we can start decoding.
179  *
180  * Initially, we prepend incoming buffers to the gather queue. Whenever the
181  * discont flag is set on an incoming buffer, the gather queue is flushed out
182  * before the new buffer is collected.
183  *
184  * The above data will be accumulated in the gather queue like this:
185  *
186  *   gather queue:  9  8  7
187  *                        D
188  *
189  * When buffer 4 is received (with a DISCONT), we flush the gather queue like
190  * this:
191  *
192  *   while (gather)
193  *     take head of queue and prepend to parse queue (this reverses the
194  *     sequence, so parse queue is 7 -> 8 -> 9)
195  *
196  *   Next, we process the parse queue, which now contains all un-parsed packets
197  *   (including any leftover ones from the previous decode section)
198  *
199  *   for each buffer now in the parse queue:
200  *     Call the subclass parse function, prepending each resulting frame to
201  *     the parse_gather queue. Buffers which precede the first one that
202  *     produces a parsed frame are retained in the parse queue for
203  *     re-processing on the next cycle of parsing.
204  *
205  *   The parse_gather queue now contains frame objects ready for decoding,
206  *   in reverse order.
207  *   parse_gather: 9 -> 8 -> 7
208  *
209  *   while (parse_gather)
210  *     Take the head of the queue and prepend it to the decode queue
211  *     If the frame was a keyframe, process the decode queue
212  *   decode is now 7-8-9
213  *
214  *  Processing the decode queue results in frames with attached output buffers
215  *  stored in the 'output_queue' ready for outputting in reverse order.
216  *
217  * After we flushed the gather queue and parsed it, we add 4 to the (now empty)
218  * gather queue. We get the following situation:
219  *
220  *  gather queue:    4
221  *  decode queue:    7  8  9
222  *
223  * After we received 5 (Keyframe) and 6:
224  *
225  *  gather queue:    6  5  4
226  *  decode queue:    7  8  9
227  *
228  * When we receive 1 (DISCONT) which triggers a flush of the gather queue:
229  *
230  *   Copy head of the gather queue (6) to decode queue:
231  *
232  *    gather queue:    5  4
233  *    decode queue:    6  7  8  9
234  *
235  *   Copy head of the gather queue (5) to decode queue. This is a keyframe so we
236  *   can start decoding.
237  *
238  *    gather queue:    4
239  *    decode queue:    5  6  7  8  9
240  *
241  *   Decode frames in decode queue, store raw decoded data in output queue, we
242  *   can take the head of the decode queue and prepend the decoded result in the
243  *   output queue:
244  *
245  *    gather queue:    4
246  *    decode queue:
247  *    output queue:    9  8  7  6  5
248  *
249  *   Now output all the frames in the output queue, picking a frame from the
250  *   head of the queue.
251  *
252  *   Copy head of the gather queue (4) to decode queue, we flushed the gather
253  *   queue and can now store input buffer in the gather queue:
254  *
255  *    gather queue:    1
256  *    decode queue:    4
257  *
258  *  When we receive EOS, the queue looks like:
259  *
260  *    gather queue:    3  2  1
261  *    decode queue:    4
262  *
263  *  Fill decode queue, first keyframe we copy is 2:
264  *
265  *    gather queue:    1
266  *    decode queue:    2  3  4
267  *
268  *  Decoded output:
269  *
270  *    gather queue:    1
271  *    decode queue:
272  *    output queue:    4  3  2
273  *
274  *  Leftover buffer 1 cannot be decoded and must be discarded.
275  */
276
277 #include "gstvideodecoder.h"
278 #include "gstvideoutils.h"
279 #include "gstvideoutilsprivate.h"
280
281 #include <gst/video/video.h>
282 #include <gst/video/video-event.h>
283 #include <gst/video/gstvideopool.h>
284 #include <gst/video/gstvideometa.h>
285 #include <string.h>
286
287 GST_DEBUG_CATEGORY (videodecoder_debug);
288 #define GST_CAT_DEFAULT videodecoder_debug
289
290 /* properties */
291 #define DEFAULT_QOS                 TRUE
292 #define DEFAULT_MAX_ERRORS          GST_VIDEO_DECODER_MAX_ERRORS
293 #define DEFAULT_MIN_FORCE_KEY_UNIT_INTERVAL 0
294 #define DEFAULT_DISCARD_CORRUPTED_FRAMES FALSE
295
296 /* Used for request_sync_point_frame_number. These are out of range for the
297  * frame numbers and can be given special meaning */
298 #define REQUEST_SYNC_POINT_PENDING G_MAXUINT + 1
299 #define REQUEST_SYNC_POINT_UNSET G_MAXUINT64
300
301 enum
302 {
303   PROP_0,
304   PROP_QOS,
305   PROP_MAX_ERRORS,
306   PROP_MIN_FORCE_KEY_UNIT_INTERVAL,
307   PROP_DISCARD_CORRUPTED_FRAMES
308 };
309
310 struct _GstVideoDecoderPrivate
311 {
312   /* FIXME introduce a context ? */
313
314   GstBufferPool *pool;
315   GstAllocator *allocator;
316   GstAllocationParams params;
317
318   /* parse tracking */
319   /* input data */
320   GstAdapter *input_adapter;
321   /* assembles current frame */
322   GstAdapter *output_adapter;
323
324   /* Whether we attempt to convert newsegment from bytes to
325    * time using a bitrate estimation */
326   gboolean do_estimate_rate;
327
328   /* Whether input is considered packetized or not */
329   gboolean packetized;
330
331   /* Error handling */
332   gint max_errors;
333   gint error_count;
334   gboolean had_output_data;
335   gboolean had_input_data;
336
337   gboolean needs_format;
338   /* input_segment are output_segment identical */
339   gboolean in_out_segment_sync;
340
341   /* TRUE if we have an active set of instant rate flags */
342   gboolean decode_flags_override;
343   GstSegmentFlags decode_flags;
344
345   /* ... being tracked here;
346    * only available during parsing */
347   GstVideoCodecFrame *current_frame;
348   /* events that should apply to the current frame */
349   /* FIXME 2.0: Use a GQueue or similar, see GstVideoCodecFrame::events */
350   GList *current_frame_events;
351   /* events that should be pushed before the next frame */
352   /* FIXME 2.0: Use a GQueue or similar, see GstVideoCodecFrame::events */
353   GList *pending_events;
354
355   /* relative offset of input data */
356   guint64 input_offset;
357   /* relative offset of frame */
358   guint64 frame_offset;
359   /* tracking ts and offsets */
360   GQueue timestamps;
361
362   /* last outgoing ts */
363   GstClockTime last_timestamp_out;
364   /* incoming pts - dts */
365   GstClockTime pts_delta;
366   gboolean reordered_output;
367
368   /* FIXME: Consider using a GQueue or other better fitting data structure */
369   /* reverse playback */
370   /* collect input */
371   GList *gather;
372   /* to-be-parsed */
373   GList *parse;
374   /* collected parsed frames */
375   GList *parse_gather;
376   /* frames to be handled == decoded */
377   GList *decode;
378   /* collected output - of buffer objects, not frames */
379   GList *output_queued;
380
381
382   /* base_picture_number is the picture number of the reference picture */
383   guint64 base_picture_number;
384   /* combine with base_picture_number, framerate and calcs to yield (presentation) ts */
385   GstClockTime base_timestamp;
386
387   /* Properties */
388   GstClockTime min_force_key_unit_interval;
389   gboolean discard_corrupted_frames;
390
391   /* Key unit related state */
392   gboolean needs_sync_point;
393   GstVideoDecoderRequestSyncPointFlags request_sync_point_flags;
394   guint64 request_sync_point_frame_number;
395   GstClockTime last_force_key_unit_time;
396   /* -1 if we saw no sync point yet */
397   guint64 distance_from_sync;
398
399   guint32 system_frame_number;
400   guint32 decode_frame_number;
401
402   GQueue frames;                /* Protected with OBJECT_LOCK */
403   GstVideoCodecState *input_state;
404   GstVideoCodecState *output_state;     /* OBJECT_LOCK and STREAM_LOCK */
405   gboolean output_state_changed;
406
407   /* QoS properties */
408   gboolean do_qos;
409   gdouble proportion;           /* OBJECT_LOCK */
410   GstClockTime earliest_time;   /* OBJECT_LOCK */
411   GstClockTime qos_frame_duration;      /* OBJECT_LOCK */
412   gboolean discont;
413   /* qos messages: frames dropped/processed */
414   guint dropped;
415   guint processed;
416
417   /* Outgoing byte size ? */
418   gint64 bytes_out;
419   gint64 time;
420
421   gint64 min_latency;
422   gint64 max_latency;
423
424   /* upstream stream tags (global tags are passed through as-is) */
425   GstTagList *upstream_tags;
426
427   /* subclass tags */
428   GstTagList *tags;
429   GstTagMergeMode tags_merge_mode;
430
431   gboolean tags_changed;
432
433   /* flags */
434   gboolean use_default_pad_acceptcaps;
435
436 #ifndef GST_DISABLE_DEBUG
437   /* Diagnostic time for reporting the time
438    * from flush to first output */
439   GstClockTime last_reset_time;
440 #endif
441 };
442
443 static GstElementClass *parent_class = NULL;
444 static gint private_offset = 0;
445
446 /* cached quark to avoid contention on the global quark table lock */
447 #define META_TAG_VIDEO meta_tag_video_quark
448 static GQuark meta_tag_video_quark;
449
450 static void gst_video_decoder_class_init (GstVideoDecoderClass * klass);
451 static void gst_video_decoder_init (GstVideoDecoder * dec,
452     GstVideoDecoderClass * klass);
453
454 static void gst_video_decoder_finalize (GObject * object);
455 static void gst_video_decoder_get_property (GObject * object, guint property_id,
456     GValue * value, GParamSpec * pspec);
457 static void gst_video_decoder_set_property (GObject * object, guint property_id,
458     const GValue * value, GParamSpec * pspec);
459
460 static gboolean gst_video_decoder_setcaps (GstVideoDecoder * dec,
461     GstCaps * caps);
462 static gboolean gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
463     GstEvent * event);
464 static gboolean gst_video_decoder_src_event (GstPad * pad, GstObject * parent,
465     GstEvent * event);
466 static GstFlowReturn gst_video_decoder_chain (GstPad * pad, GstObject * parent,
467     GstBuffer * buf);
468 static gboolean gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
469     GstQuery * query);
470 static GstStateChangeReturn gst_video_decoder_change_state (GstElement *
471     element, GstStateChange transition);
472 static gboolean gst_video_decoder_src_query (GstPad * pad, GstObject * parent,
473     GstQuery * query);
474 static void gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full,
475     gboolean flush_hard);
476
477 static GstFlowReturn gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
478     GstVideoCodecFrame * frame);
479
480 static void gst_video_decoder_push_event_list (GstVideoDecoder * decoder,
481     GList * events);
482 static GstClockTime gst_video_decoder_get_frame_duration (GstVideoDecoder *
483     decoder, GstVideoCodecFrame * frame);
484 static GstVideoCodecFrame *gst_video_decoder_new_frame (GstVideoDecoder *
485     decoder);
486 static GstFlowReturn gst_video_decoder_clip_and_push_buf (GstVideoDecoder *
487     decoder, GstBuffer * buf);
488 static GstFlowReturn gst_video_decoder_flush_parse (GstVideoDecoder * dec,
489     gboolean at_eos);
490
491 static void gst_video_decoder_clear_queues (GstVideoDecoder * dec);
492
493 static gboolean gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
494     GstEvent * event);
495 static gboolean gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
496     GstEvent * event);
497 static gboolean gst_video_decoder_decide_allocation_default (GstVideoDecoder *
498     decoder, GstQuery * query);
499 static gboolean gst_video_decoder_propose_allocation_default (GstVideoDecoder *
500     decoder, GstQuery * query);
501 static gboolean gst_video_decoder_negotiate_default (GstVideoDecoder * decoder);
502 static GstFlowReturn gst_video_decoder_parse_available (GstVideoDecoder * dec,
503     gboolean at_eos, gboolean new_buffer);
504 static gboolean gst_video_decoder_negotiate_unlocked (GstVideoDecoder *
505     decoder);
506 static gboolean gst_video_decoder_sink_query_default (GstVideoDecoder * decoder,
507     GstQuery * query);
508 static gboolean gst_video_decoder_src_query_default (GstVideoDecoder * decoder,
509     GstQuery * query);
510
511 static gboolean gst_video_decoder_transform_meta_default (GstVideoDecoder *
512     decoder, GstVideoCodecFrame * frame, GstMeta * meta);
513
514 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
515  * method to get to the padtemplates */
516 GType
517 gst_video_decoder_get_type (void)
518 {
519   static volatile gsize type = 0;
520
521   if (g_once_init_enter (&type)) {
522     GType _type;
523     static const GTypeInfo info = {
524       sizeof (GstVideoDecoderClass),
525       NULL,
526       NULL,
527       (GClassInitFunc) gst_video_decoder_class_init,
528       NULL,
529       NULL,
530       sizeof (GstVideoDecoder),
531       0,
532       (GInstanceInitFunc) gst_video_decoder_init,
533     };
534
535     _type = g_type_register_static (GST_TYPE_ELEMENT,
536         "GstVideoDecoder", &info, G_TYPE_FLAG_ABSTRACT);
537
538     private_offset =
539         g_type_add_instance_private (_type, sizeof (GstVideoDecoderPrivate));
540
541     g_once_init_leave (&type, _type);
542   }
543   return type;
544 }
545
546 static inline GstVideoDecoderPrivate *
547 gst_video_decoder_get_instance_private (GstVideoDecoder * self)
548 {
549   return (G_STRUCT_MEMBER_P (self, private_offset));
550 }
551
552 static void
553 gst_video_decoder_class_init (GstVideoDecoderClass * klass)
554 {
555   GObjectClass *gobject_class;
556   GstElementClass *gstelement_class;
557
558   gobject_class = G_OBJECT_CLASS (klass);
559   gstelement_class = GST_ELEMENT_CLASS (klass);
560
561   GST_DEBUG_CATEGORY_INIT (videodecoder_debug, "videodecoder", 0,
562       "Base Video Decoder");
563
564   parent_class = g_type_class_peek_parent (klass);
565
566   if (private_offset != 0)
567     g_type_class_adjust_private_offset (klass, &private_offset);
568
569   gobject_class->finalize = gst_video_decoder_finalize;
570   gobject_class->get_property = gst_video_decoder_get_property;
571   gobject_class->set_property = gst_video_decoder_set_property;
572
573   gstelement_class->change_state =
574       GST_DEBUG_FUNCPTR (gst_video_decoder_change_state);
575
576   klass->sink_event = gst_video_decoder_sink_event_default;
577   klass->src_event = gst_video_decoder_src_event_default;
578   klass->decide_allocation = gst_video_decoder_decide_allocation_default;
579   klass->propose_allocation = gst_video_decoder_propose_allocation_default;
580   klass->negotiate = gst_video_decoder_negotiate_default;
581   klass->sink_query = gst_video_decoder_sink_query_default;
582   klass->src_query = gst_video_decoder_src_query_default;
583   klass->transform_meta = gst_video_decoder_transform_meta_default;
584
585   /**
586    * GstVideoDecoder:qos:
587    *
588    * If set to %TRUE the decoder will handle QoS events received
589    * from downstream elements.
590    * This includes dropping output frames which are detected as late
591    * using the metrics reported by those events.
592    *
593    * Since: 1.18
594    */
595   g_object_class_install_property (gobject_class, PROP_QOS,
596       g_param_spec_boolean ("qos", "Quality of Service",
597           "Handle Quality-of-Service events from downstream",
598           DEFAULT_QOS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
599
600   /**
601    * GstVideoDecoder:max-errors:
602    *
603    * Maximum number of tolerated consecutive decode errors. See
604    * gst_video_decoder_set_max_errors() for more details.
605    *
606    * Since: 1.18
607    */
608   g_object_class_install_property (gobject_class, PROP_MAX_ERRORS,
609       g_param_spec_int ("max-errors", "Max errors",
610           "Max consecutive decoder errors before returning flow error",
611           -1, G_MAXINT, DEFAULT_MAX_ERRORS,
612           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
613
614   /**
615    * GstVideoDecoder:min-force-key-unit-interval:
616    *
617    * Minimum interval between force-key-unit events sent upstream by the
618    * decoder. Setting this to 0 will cause every event to be handled, setting
619    * this to %GST_CLOCK_TIME_NONE will cause every event to be ignored.
620    *
621    * See gst_video_event_new_upstream_force_key_unit() for more details about
622    * force-key-unit events.
623    *
624    * Since: 1.20
625    */
626   g_object_class_install_property (gobject_class,
627       PROP_MIN_FORCE_KEY_UNIT_INTERVAL,
628       g_param_spec_uint64 ("min-force-key-unit-interval",
629           "Minimum Force Keyunit Interval",
630           "Minimum interval between force-keyunit requests in nanoseconds", 0,
631           G_MAXUINT64, DEFAULT_MIN_FORCE_KEY_UNIT_INTERVAL,
632           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
633
634   /**
635    * GstVideoDecoder:discard-corrupted-frames:
636    *
637    * If set to %TRUE the decoder will discard frames that are marked as
638    * corrupted instead of outputting them.
639    *
640    * Since: 1.20
641    */
642   g_object_class_install_property (gobject_class, PROP_DISCARD_CORRUPTED_FRAMES,
643       g_param_spec_boolean ("discard-corrupted-frames",
644           "Discard Corrupted Frames",
645           "Discard frames marked as corrupted instead of outputting them",
646           DEFAULT_DISCARD_CORRUPTED_FRAMES,
647           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
648
649   meta_tag_video_quark = g_quark_from_static_string (GST_META_TAG_VIDEO_STR);
650 }
651
652 static void
653 gst_video_decoder_init (GstVideoDecoder * decoder, GstVideoDecoderClass * klass)
654 {
655   GstPadTemplate *pad_template;
656   GstPad *pad;
657
658   GST_DEBUG_OBJECT (decoder, "gst_video_decoder_init");
659
660   decoder->priv = gst_video_decoder_get_instance_private (decoder);
661
662   pad_template =
663       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
664   g_return_if_fail (pad_template != NULL);
665
666   decoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
667
668   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_decoder_chain));
669   gst_pad_set_event_function (pad,
670       GST_DEBUG_FUNCPTR (gst_video_decoder_sink_event));
671   gst_pad_set_query_function (pad,
672       GST_DEBUG_FUNCPTR (gst_video_decoder_sink_query));
673   gst_element_add_pad (GST_ELEMENT (decoder), decoder->sinkpad);
674
675   pad_template =
676       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
677   g_return_if_fail (pad_template != NULL);
678
679   decoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
680
681   gst_pad_set_event_function (pad,
682       GST_DEBUG_FUNCPTR (gst_video_decoder_src_event));
683   gst_pad_set_query_function (pad,
684       GST_DEBUG_FUNCPTR (gst_video_decoder_src_query));
685   gst_element_add_pad (GST_ELEMENT (decoder), decoder->srcpad);
686
687   gst_segment_init (&decoder->input_segment, GST_FORMAT_TIME);
688   gst_segment_init (&decoder->output_segment, GST_FORMAT_TIME);
689
690   g_rec_mutex_init (&decoder->stream_lock);
691
692   decoder->priv->input_adapter = gst_adapter_new ();
693   decoder->priv->output_adapter = gst_adapter_new ();
694   decoder->priv->packetized = TRUE;
695   decoder->priv->needs_format = FALSE;
696
697   g_queue_init (&decoder->priv->frames);
698   g_queue_init (&decoder->priv->timestamps);
699
700   /* properties */
701   decoder->priv->do_qos = DEFAULT_QOS;
702   decoder->priv->max_errors = GST_VIDEO_DECODER_MAX_ERRORS;
703
704   decoder->priv->min_latency = 0;
705   decoder->priv->max_latency = 0;
706
707   gst_video_decoder_reset (decoder, TRUE, TRUE);
708 }
709
710 static GstVideoCodecState *
711 _new_input_state (GstCaps * caps)
712 {
713   GstVideoCodecState *state;
714   GstStructure *structure;
715   const GValue *codec_data;
716
717   state = g_slice_new0 (GstVideoCodecState);
718   state->ref_count = 1;
719   gst_video_info_init (&state->info);
720   if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
721     goto parse_fail;
722   state->caps = gst_caps_ref (caps);
723
724   structure = gst_caps_get_structure (caps, 0);
725
726   codec_data = gst_structure_get_value (structure, "codec_data");
727   if (codec_data && G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER)
728     state->codec_data = GST_BUFFER (g_value_dup_boxed (codec_data));
729
730   return state;
731
732 parse_fail:
733   {
734     g_slice_free (GstVideoCodecState, state);
735     return NULL;
736   }
737 }
738
739 static GstVideoCodecState *
740 _new_output_state (GstVideoFormat fmt, GstVideoInterlaceMode interlace_mode,
741     guint width, guint height, GstVideoCodecState * reference,
742     gboolean copy_interlace_mode)
743 {
744   GstVideoCodecState *state;
745
746   state = g_slice_new0 (GstVideoCodecState);
747   state->ref_count = 1;
748   gst_video_info_init (&state->info);
749   if (!gst_video_info_set_interlaced_format (&state->info, fmt, interlace_mode,
750           width, height)) {
751     g_slice_free (GstVideoCodecState, state);
752     return NULL;
753   }
754
755   if (reference) {
756     GstVideoInfo *tgt, *ref;
757
758     tgt = &state->info;
759     ref = &reference->info;
760
761     /* Copy over extra fields from reference state */
762     if (copy_interlace_mode)
763       tgt->interlace_mode = ref->interlace_mode;
764     tgt->flags = ref->flags;
765     /* only copy values that are not unknown so that we don't override the
766      * defaults. subclasses should really fill these in when they know. */
767     if (ref->chroma_site)
768       tgt->chroma_site = ref->chroma_site;
769     if (ref->colorimetry.range)
770       tgt->colorimetry.range = ref->colorimetry.range;
771     if (ref->colorimetry.matrix)
772       tgt->colorimetry.matrix = ref->colorimetry.matrix;
773     if (ref->colorimetry.transfer)
774       tgt->colorimetry.transfer = ref->colorimetry.transfer;
775     if (ref->colorimetry.primaries)
776       tgt->colorimetry.primaries = ref->colorimetry.primaries;
777     GST_DEBUG ("reference par %d/%d fps %d/%d",
778         ref->par_n, ref->par_d, ref->fps_n, ref->fps_d);
779     tgt->par_n = ref->par_n;
780     tgt->par_d = ref->par_d;
781     tgt->fps_n = ref->fps_n;
782     tgt->fps_d = ref->fps_d;
783     tgt->views = ref->views;
784
785     GST_VIDEO_INFO_FIELD_ORDER (tgt) = GST_VIDEO_INFO_FIELD_ORDER (ref);
786
787     if (GST_VIDEO_INFO_MULTIVIEW_MODE (ref) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
788       GST_VIDEO_INFO_MULTIVIEW_MODE (tgt) = GST_VIDEO_INFO_MULTIVIEW_MODE (ref);
789       GST_VIDEO_INFO_MULTIVIEW_FLAGS (tgt) =
790           GST_VIDEO_INFO_MULTIVIEW_FLAGS (ref);
791     } else {
792       /* Default to MONO, overridden as needed by sub-classes */
793       GST_VIDEO_INFO_MULTIVIEW_MODE (tgt) = GST_VIDEO_MULTIVIEW_MODE_MONO;
794       GST_VIDEO_INFO_MULTIVIEW_FLAGS (tgt) = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
795     }
796   }
797
798   GST_DEBUG ("reference par %d/%d fps %d/%d",
799       state->info.par_n, state->info.par_d,
800       state->info.fps_n, state->info.fps_d);
801
802   return state;
803 }
804
805 static gboolean
806 gst_video_decoder_setcaps (GstVideoDecoder * decoder, GstCaps * caps)
807 {
808   GstVideoDecoderClass *decoder_class;
809   GstVideoCodecState *state;
810   gboolean ret = TRUE;
811
812   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
813
814   GST_DEBUG_OBJECT (decoder, "setcaps %" GST_PTR_FORMAT, caps);
815
816   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
817
818   if (decoder->priv->input_state) {
819     GST_DEBUG_OBJECT (decoder,
820         "Checking if caps changed old %" GST_PTR_FORMAT " new %" GST_PTR_FORMAT,
821         decoder->priv->input_state->caps, caps);
822     if (gst_caps_is_equal (decoder->priv->input_state->caps, caps))
823       goto caps_not_changed;
824   }
825
826   state = _new_input_state (caps);
827
828   if (G_UNLIKELY (state == NULL))
829     goto parse_fail;
830
831   if (decoder_class->set_format)
832     ret = decoder_class->set_format (decoder, state);
833
834   if (!ret)
835     goto refused_format;
836
837   if (decoder->priv->input_state)
838     gst_video_codec_state_unref (decoder->priv->input_state);
839   decoder->priv->input_state = state;
840
841   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
842
843   return ret;
844
845 caps_not_changed:
846   {
847     GST_DEBUG_OBJECT (decoder, "Caps did not change - ignore");
848     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
849     return TRUE;
850   }
851
852   /* ERRORS */
853 parse_fail:
854   {
855     GST_WARNING_OBJECT (decoder, "Failed to parse caps");
856     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
857     return FALSE;
858   }
859
860 refused_format:
861   {
862     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
863     GST_WARNING_OBJECT (decoder, "Subclass refused caps");
864     gst_video_codec_state_unref (state);
865     return FALSE;
866   }
867 }
868
869 static void
870 gst_video_decoder_finalize (GObject * object)
871 {
872   GstVideoDecoder *decoder;
873
874   decoder = GST_VIDEO_DECODER (object);
875
876   GST_DEBUG_OBJECT (object, "finalize");
877
878   g_rec_mutex_clear (&decoder->stream_lock);
879
880   if (decoder->priv->input_adapter) {
881     g_object_unref (decoder->priv->input_adapter);
882     decoder->priv->input_adapter = NULL;
883   }
884   if (decoder->priv->output_adapter) {
885     g_object_unref (decoder->priv->output_adapter);
886     decoder->priv->output_adapter = NULL;
887   }
888
889   if (decoder->priv->input_state)
890     gst_video_codec_state_unref (decoder->priv->input_state);
891   if (decoder->priv->output_state)
892     gst_video_codec_state_unref (decoder->priv->output_state);
893
894   if (decoder->priv->pool) {
895     gst_object_unref (decoder->priv->pool);
896     decoder->priv->pool = NULL;
897   }
898
899   if (decoder->priv->allocator) {
900     gst_object_unref (decoder->priv->allocator);
901     decoder->priv->allocator = NULL;
902   }
903
904   G_OBJECT_CLASS (parent_class)->finalize (object);
905 }
906
907 static void
908 gst_video_decoder_get_property (GObject * object, guint property_id,
909     GValue * value, GParamSpec * pspec)
910 {
911   GstVideoDecoder *dec = GST_VIDEO_DECODER (object);
912   GstVideoDecoderPrivate *priv = dec->priv;
913
914   switch (property_id) {
915     case PROP_QOS:
916       g_value_set_boolean (value, priv->do_qos);
917       break;
918     case PROP_MAX_ERRORS:
919       g_value_set_int (value, gst_video_decoder_get_max_errors (dec));
920       break;
921     case PROP_MIN_FORCE_KEY_UNIT_INTERVAL:
922       g_value_set_uint64 (value, priv->min_force_key_unit_interval);
923       break;
924     case PROP_DISCARD_CORRUPTED_FRAMES:
925       g_value_set_boolean (value, priv->discard_corrupted_frames);
926       break;
927     default:
928       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
929       break;
930   }
931 }
932
933 static void
934 gst_video_decoder_set_property (GObject * object, guint property_id,
935     const GValue * value, GParamSpec * pspec)
936 {
937   GstVideoDecoder *dec = GST_VIDEO_DECODER (object);
938   GstVideoDecoderPrivate *priv = dec->priv;
939
940   switch (property_id) {
941     case PROP_QOS:
942       priv->do_qos = g_value_get_boolean (value);
943       break;
944     case PROP_MAX_ERRORS:
945       gst_video_decoder_set_max_errors (dec, g_value_get_int (value));
946       break;
947     case PROP_MIN_FORCE_KEY_UNIT_INTERVAL:
948       priv->min_force_key_unit_interval = g_value_get_uint64 (value);
949       break;
950     case PROP_DISCARD_CORRUPTED_FRAMES:
951       priv->discard_corrupted_frames = g_value_get_boolean (value);
952       break;
953     default:
954       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
955       break;
956   }
957 }
958
959 /* hard == FLUSH, otherwise discont */
960 static GstFlowReturn
961 gst_video_decoder_flush (GstVideoDecoder * dec, gboolean hard)
962 {
963   GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (dec);
964   GstFlowReturn ret = GST_FLOW_OK;
965
966   GST_LOG_OBJECT (dec, "flush hard %d", hard);
967
968   /* Inform subclass */
969   if (klass->reset) {
970     GST_FIXME_OBJECT (dec, "GstVideoDecoder::reset() is deprecated");
971     klass->reset (dec, hard);
972   }
973
974   if (klass->flush)
975     klass->flush (dec);
976
977   /* and get (re)set for the sequel */
978   gst_video_decoder_reset (dec, FALSE, hard);
979
980   return ret;
981 }
982
983 static GstEvent *
984 gst_video_decoder_create_merged_tags_event (GstVideoDecoder * dec)
985 {
986   GstTagList *merged_tags;
987
988   GST_LOG_OBJECT (dec, "upstream : %" GST_PTR_FORMAT, dec->priv->upstream_tags);
989   GST_LOG_OBJECT (dec, "decoder  : %" GST_PTR_FORMAT, dec->priv->tags);
990   GST_LOG_OBJECT (dec, "mode     : %d", dec->priv->tags_merge_mode);
991
992   merged_tags =
993       gst_tag_list_merge (dec->priv->upstream_tags, dec->priv->tags,
994       dec->priv->tags_merge_mode);
995
996   GST_DEBUG_OBJECT (dec, "merged   : %" GST_PTR_FORMAT, merged_tags);
997
998   if (merged_tags == NULL)
999     return NULL;
1000
1001   if (gst_tag_list_is_empty (merged_tags)) {
1002     gst_tag_list_unref (merged_tags);
1003     return NULL;
1004   }
1005
1006   return gst_event_new_tag (merged_tags);
1007 }
1008
1009 static gboolean
1010 gst_video_decoder_push_event (GstVideoDecoder * decoder, GstEvent * event)
1011 {
1012   switch (GST_EVENT_TYPE (event)) {
1013     case GST_EVENT_SEGMENT:
1014     {
1015       GstSegment segment;
1016
1017       gst_event_copy_segment (event, &segment);
1018
1019       GST_DEBUG_OBJECT (decoder, "segment %" GST_SEGMENT_FORMAT, &segment);
1020
1021       if (segment.format != GST_FORMAT_TIME) {
1022         GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
1023         break;
1024       }
1025
1026       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1027       decoder->output_segment = segment;
1028       decoder->priv->in_out_segment_sync =
1029           gst_segment_is_equal (&decoder->input_segment, &segment);
1030       decoder->priv->last_timestamp_out = GST_CLOCK_TIME_NONE;
1031       decoder->priv->earliest_time = GST_CLOCK_TIME_NONE;
1032       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1033       break;
1034     }
1035     default:
1036       break;
1037   }
1038
1039   GST_DEBUG_OBJECT (decoder, "pushing event %s",
1040       gst_event_type_get_name (GST_EVENT_TYPE (event)));
1041
1042   return gst_pad_push_event (decoder->srcpad, event);
1043 }
1044
1045 static GstFlowReturn
1046 gst_video_decoder_parse_available (GstVideoDecoder * dec, gboolean at_eos,
1047     gboolean new_buffer)
1048 {
1049   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
1050   GstVideoDecoderPrivate *priv = dec->priv;
1051   GstFlowReturn ret = GST_FLOW_OK;
1052   gsize was_available, available;
1053   guint inactive = 0;
1054
1055   available = gst_adapter_available (priv->input_adapter);
1056
1057   while (available || new_buffer) {
1058     new_buffer = FALSE;
1059     /* current frame may have been parsed and handled,
1060      * so we need to set up a new one when asking subclass to parse */
1061     if (priv->current_frame == NULL)
1062       priv->current_frame = gst_video_decoder_new_frame (dec);
1063
1064     was_available = available;
1065     ret = decoder_class->parse (dec, priv->current_frame,
1066         priv->input_adapter, at_eos);
1067     if (ret != GST_FLOW_OK)
1068       break;
1069
1070     /* if the subclass returned success (GST_FLOW_OK), it is expected
1071      * to have collected and submitted a frame, i.e. it should have
1072      * called gst_video_decoder_have_frame(), or at least consumed a
1073      * few bytes through gst_video_decoder_add_to_frame().
1074      *
1075      * Otherwise, this is an implementation bug, and we error out
1076      * after 2 failed attempts */
1077     available = gst_adapter_available (priv->input_adapter);
1078     if (!priv->current_frame || available != was_available)
1079       inactive = 0;
1080     else if (++inactive == 2)
1081       goto error_inactive;
1082   }
1083
1084   return ret;
1085
1086   /* ERRORS */
1087 error_inactive:
1088   {
1089     GST_ERROR_OBJECT (dec, "Failed to consume data. Error in subclass?");
1090     return GST_FLOW_ERROR;
1091   }
1092 }
1093
1094 /* This function has to be called with the stream lock taken. */
1095 static GstFlowReturn
1096 gst_video_decoder_drain_out (GstVideoDecoder * dec, gboolean at_eos)
1097 {
1098   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
1099   GstVideoDecoderPrivate *priv = dec->priv;
1100   GstFlowReturn ret = GST_FLOW_OK;
1101
1102   if (dec->input_segment.rate > 0.0) {
1103     /* Forward mode, if unpacketized, give the child class
1104      * a final chance to flush out packets */
1105     if (!priv->packetized) {
1106       ret = gst_video_decoder_parse_available (dec, TRUE, FALSE);
1107     }
1108
1109     if (at_eos) {
1110       if (decoder_class->finish)
1111         ret = decoder_class->finish (dec);
1112     } else {
1113       if (decoder_class->drain) {
1114         ret = decoder_class->drain (dec);
1115       } else {
1116         GST_FIXME_OBJECT (dec, "Sub-class should implement drain()");
1117       }
1118     }
1119   } else {
1120     /* Reverse playback mode */
1121     ret = gst_video_decoder_flush_parse (dec, TRUE);
1122   }
1123
1124   return ret;
1125 }
1126
1127 static GList *
1128 _flush_events (GstPad * pad, GList * events)
1129 {
1130   GList *tmp;
1131
1132   for (tmp = events; tmp; tmp = tmp->next) {
1133     if (GST_EVENT_TYPE (tmp->data) != GST_EVENT_EOS &&
1134         GST_EVENT_TYPE (tmp->data) != GST_EVENT_SEGMENT &&
1135         GST_EVENT_IS_STICKY (tmp->data)) {
1136       gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
1137     }
1138     gst_event_unref (tmp->data);
1139   }
1140   g_list_free (events);
1141
1142   return NULL;
1143 }
1144
1145 /* Must be called holding the GST_VIDEO_DECODER_STREAM_LOCK */
1146 static gboolean
1147 gst_video_decoder_negotiate_default_caps (GstVideoDecoder * decoder)
1148 {
1149   GstCaps *caps, *templcaps;
1150   GstVideoCodecState *state;
1151   GstVideoInfo info;
1152   gint i;
1153   gint caps_size;
1154   GstStructure *structure;
1155
1156   templcaps = gst_pad_get_pad_template_caps (decoder->srcpad);
1157   caps = gst_pad_peer_query_caps (decoder->srcpad, templcaps);
1158   if (caps)
1159     gst_caps_unref (templcaps);
1160   else
1161     caps = templcaps;
1162   templcaps = NULL;
1163
1164   if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1165     goto caps_error;
1166
1167   GST_LOG_OBJECT (decoder, "peer caps %" GST_PTR_FORMAT, caps);
1168
1169   /* before fixating, try to use whatever upstream provided */
1170   caps = gst_caps_make_writable (caps);
1171   caps_size = gst_caps_get_size (caps);
1172   if (decoder->priv->input_state && decoder->priv->input_state->caps) {
1173     GstCaps *sinkcaps = decoder->priv->input_state->caps;
1174     GstStructure *structure = gst_caps_get_structure (sinkcaps, 0);
1175     gint width, height;
1176
1177     if (gst_structure_get_int (structure, "width", &width)) {
1178       for (i = 0; i < caps_size; i++) {
1179         gst_structure_set (gst_caps_get_structure (caps, i), "width",
1180             G_TYPE_INT, width, NULL);
1181       }
1182     }
1183
1184     if (gst_structure_get_int (structure, "height", &height)) {
1185       for (i = 0; i < caps_size; i++) {
1186         gst_structure_set (gst_caps_get_structure (caps, i), "height",
1187             G_TYPE_INT, height, NULL);
1188       }
1189     }
1190   }
1191
1192   for (i = 0; i < caps_size; i++) {
1193     structure = gst_caps_get_structure (caps, i);
1194     /* Random I420 1280x720 for fixation */
1195     if (gst_structure_has_field (structure, "format"))
1196       gst_structure_fixate_field_string (structure, "format", "I420");
1197     else
1198       gst_structure_set (structure, "format", G_TYPE_STRING, "I420", NULL);
1199
1200     if (gst_structure_has_field (structure, "width"))
1201       gst_structure_fixate_field_nearest_int (structure, "width", 1280);
1202     else
1203       gst_structure_set (structure, "width", G_TYPE_INT, 1280, NULL);
1204
1205     if (gst_structure_has_field (structure, "height"))
1206       gst_structure_fixate_field_nearest_int (structure, "height", 720);
1207     else
1208       gst_structure_set (structure, "height", G_TYPE_INT, 720, NULL);
1209   }
1210   caps = gst_caps_fixate (caps);
1211
1212   if (!caps || !gst_video_info_from_caps (&info, caps))
1213     goto caps_error;
1214
1215   GST_INFO_OBJECT (decoder,
1216       "Chose default caps %" GST_PTR_FORMAT " for initial gap", caps);
1217   state =
1218       gst_video_decoder_set_output_state (decoder, info.finfo->format,
1219       info.width, info.height, decoder->priv->input_state);
1220   gst_video_codec_state_unref (state);
1221   gst_caps_unref (caps);
1222
1223   return TRUE;
1224
1225 caps_error:
1226   {
1227     if (caps)
1228       gst_caps_unref (caps);
1229     return FALSE;
1230   }
1231 }
1232
1233 static gboolean
1234 gst_video_decoder_sink_event_default (GstVideoDecoder * decoder,
1235     GstEvent * event)
1236 {
1237   GstVideoDecoderPrivate *priv;
1238   gboolean ret = FALSE;
1239   gboolean forward_immediate = FALSE;
1240
1241   priv = decoder->priv;
1242
1243   switch (GST_EVENT_TYPE (event)) {
1244     case GST_EVENT_STREAM_START:
1245     {
1246       GstFlowReturn flow_ret = GST_FLOW_OK;
1247
1248       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1249       flow_ret = gst_video_decoder_drain_out (decoder, FALSE);
1250       ret = (flow_ret == GST_FLOW_OK);
1251
1252       GST_DEBUG_OBJECT (decoder, "received STREAM_START. Clearing taglist");
1253       /* Flush upstream tags after a STREAM_START */
1254       if (priv->upstream_tags) {
1255         gst_tag_list_unref (priv->upstream_tags);
1256         priv->upstream_tags = NULL;
1257         priv->tags_changed = TRUE;
1258       }
1259       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1260
1261       /* Forward STREAM_START immediately. Everything is drained after
1262        * the STREAM_START event and we can forward this event immediately
1263        * now without having buffers out of order.
1264        */
1265       forward_immediate = TRUE;
1266       break;
1267     }
1268     case GST_EVENT_CAPS:
1269     {
1270       GstCaps *caps;
1271
1272       gst_event_parse_caps (event, &caps);
1273       ret = gst_video_decoder_setcaps (decoder, caps);
1274       gst_event_unref (event);
1275       event = NULL;
1276       break;
1277     }
1278     case GST_EVENT_SEGMENT_DONE:
1279     {
1280       GstFlowReturn flow_ret = GST_FLOW_OK;
1281
1282       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1283       flow_ret = gst_video_decoder_drain_out (decoder, TRUE);
1284       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1285       ret = (flow_ret == GST_FLOW_OK);
1286
1287       /* Forward SEGMENT_DONE immediately. This is required
1288        * because no buffer or serialized event might come
1289        * after SEGMENT_DONE and nothing could trigger another
1290        * _finish_frame() call.
1291        *
1292        * The subclass can override this behaviour by overriding
1293        * the ::sink_event() vfunc and not chaining up to the
1294        * parent class' ::sink_event() until a later time.
1295        */
1296       forward_immediate = TRUE;
1297       break;
1298     }
1299     case GST_EVENT_EOS:
1300     {
1301       GstFlowReturn flow_ret = GST_FLOW_OK;
1302
1303       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1304       flow_ret = gst_video_decoder_drain_out (decoder, TRUE);
1305       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1306       ret = (flow_ret == GST_FLOW_OK);
1307
1308       /* Error out even if EOS was ok when we had input, but no output */
1309       if (ret && priv->had_input_data && !priv->had_output_data) {
1310         GST_ELEMENT_ERROR (decoder, STREAM, DECODE,
1311             ("No valid frames decoded before end of stream"),
1312             ("no valid frames found"));
1313       }
1314
1315       /* Forward EOS immediately. This is required because no
1316        * buffer or serialized event will come after EOS and
1317        * nothing could trigger another _finish_frame() call.
1318        *
1319        * The subclass can override this behaviour by overriding
1320        * the ::sink_event() vfunc and not chaining up to the
1321        * parent class' ::sink_event() until a later time.
1322        */
1323       forward_immediate = TRUE;
1324       break;
1325     }
1326     case GST_EVENT_GAP:
1327     {
1328       GstFlowReturn flow_ret = GST_FLOW_OK;
1329       gboolean needs_reconfigure = FALSE;
1330       GList *events;
1331       GList *frame_events;
1332
1333       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1334       if (decoder->input_segment.flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS)
1335         flow_ret = gst_video_decoder_drain_out (decoder, FALSE);
1336       ret = (flow_ret == GST_FLOW_OK);
1337
1338       /* Ensure we have caps before forwarding the event */
1339       if (!decoder->priv->output_state) {
1340         if (!gst_video_decoder_negotiate_default_caps (decoder)) {
1341           GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1342           GST_ELEMENT_ERROR (decoder, STREAM, FORMAT, (NULL),
1343               ("Decoder output not negotiated before GAP event."));
1344           forward_immediate = TRUE;
1345           break;
1346         }
1347         needs_reconfigure = TRUE;
1348       }
1349
1350       needs_reconfigure = gst_pad_check_reconfigure (decoder->srcpad)
1351           || needs_reconfigure;
1352       if (decoder->priv->output_state_changed || needs_reconfigure) {
1353         if (!gst_video_decoder_negotiate_unlocked (decoder)) {
1354           GST_WARNING_OBJECT (decoder, "Failed to negotiate with downstream");
1355           gst_pad_mark_reconfigure (decoder->srcpad);
1356         }
1357       }
1358
1359       GST_DEBUG_OBJECT (decoder, "Pushing all pending serialized events"
1360           " before the gap");
1361       events = decoder->priv->pending_events;
1362       frame_events = decoder->priv->current_frame_events;
1363       decoder->priv->pending_events = NULL;
1364       decoder->priv->current_frame_events = NULL;
1365
1366       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1367
1368       gst_video_decoder_push_event_list (decoder, events);
1369       gst_video_decoder_push_event_list (decoder, frame_events);
1370
1371       /* Forward GAP immediately. Everything is drained after
1372        * the GAP event and we can forward this event immediately
1373        * now without having buffers out of order.
1374        */
1375       forward_immediate = TRUE;
1376       break;
1377     }
1378     case GST_EVENT_CUSTOM_DOWNSTREAM:
1379     {
1380       gboolean in_still;
1381       GstFlowReturn flow_ret = GST_FLOW_OK;
1382
1383       if (gst_video_event_parse_still_frame (event, &in_still)) {
1384         if (in_still) {
1385           GST_DEBUG_OBJECT (decoder, "draining current data for still-frame");
1386           GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1387           flow_ret = gst_video_decoder_drain_out (decoder, FALSE);
1388           GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1389           ret = (flow_ret == GST_FLOW_OK);
1390         }
1391         /* Forward STILL_FRAME immediately. Everything is drained after
1392          * the STILL_FRAME event and we can forward this event immediately
1393          * now without having buffers out of order.
1394          */
1395         forward_immediate = TRUE;
1396       }
1397       break;
1398     }
1399     case GST_EVENT_SEGMENT:
1400     {
1401       GstSegment segment;
1402
1403       gst_event_copy_segment (event, &segment);
1404
1405       if (segment.format == GST_FORMAT_TIME) {
1406         GST_DEBUG_OBJECT (decoder,
1407             "received TIME SEGMENT %" GST_SEGMENT_FORMAT, &segment);
1408       } else {
1409         gint64 start;
1410
1411         GST_DEBUG_OBJECT (decoder,
1412             "received SEGMENT %" GST_SEGMENT_FORMAT, &segment);
1413
1414         /* handle newsegment as a result from our legacy simple seeking */
1415         /* note that initial 0 should convert to 0 in any case */
1416         if (priv->do_estimate_rate &&
1417             gst_pad_query_convert (decoder->sinkpad, GST_FORMAT_BYTES,
1418                 segment.start, GST_FORMAT_TIME, &start)) {
1419           /* best attempt convert */
1420           /* as these are only estimates, stop is kept open-ended to avoid
1421            * premature cutting */
1422           GST_DEBUG_OBJECT (decoder,
1423               "converted to TIME start %" GST_TIME_FORMAT,
1424               GST_TIME_ARGS (start));
1425           segment.start = start;
1426           segment.stop = GST_CLOCK_TIME_NONE;
1427           segment.time = start;
1428           /* replace event */
1429           gst_event_unref (event);
1430           event = gst_event_new_segment (&segment);
1431         } else {
1432           goto newseg_wrong_format;
1433         }
1434       }
1435
1436       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1437
1438       /* Update the decode flags in the segment if we have an instant-rate
1439        * override active */
1440       GST_OBJECT_LOCK (decoder);
1441       if (!priv->decode_flags_override)
1442         priv->decode_flags = segment.flags;
1443       else {
1444         segment.flags &= ~GST_SEGMENT_INSTANT_FLAGS;
1445         segment.flags |= priv->decode_flags & GST_SEGMENT_INSTANT_FLAGS;
1446       }
1447
1448       priv->base_timestamp = GST_CLOCK_TIME_NONE;
1449       priv->base_picture_number = 0;
1450
1451       decoder->input_segment = segment;
1452       decoder->priv->in_out_segment_sync = FALSE;
1453
1454       GST_OBJECT_UNLOCK (decoder);
1455       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1456
1457       break;
1458     }
1459     case GST_EVENT_INSTANT_RATE_CHANGE:
1460     {
1461       GstSegmentFlags flags;
1462       GstSegment *seg;
1463
1464       gst_event_parse_instant_rate_change (event, NULL, &flags);
1465
1466       GST_OBJECT_LOCK (decoder);
1467       priv->decode_flags_override = TRUE;
1468       priv->decode_flags = flags;
1469
1470       /* Update the input segment flags */
1471       seg = &decoder->input_segment;
1472       seg->flags &= ~GST_SEGMENT_INSTANT_FLAGS;
1473       seg->flags |= priv->decode_flags & GST_SEGMENT_INSTANT_FLAGS;
1474       GST_OBJECT_UNLOCK (decoder);
1475       break;
1476     }
1477     case GST_EVENT_FLUSH_STOP:
1478     {
1479       GList *l;
1480
1481       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1482       for (l = priv->frames.head; l; l = l->next) {
1483         GstVideoCodecFrame *frame = l->data;
1484
1485         frame->events = _flush_events (decoder->srcpad, frame->events);
1486       }
1487       priv->current_frame_events = _flush_events (decoder->srcpad,
1488           decoder->priv->current_frame_events);
1489
1490       /* well, this is kind of worse than a DISCONT */
1491       gst_video_decoder_flush (decoder, TRUE);
1492       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1493       /* Forward FLUSH_STOP immediately. This is required because it is
1494        * expected to be forwarded immediately and no buffers are queued
1495        * anyway.
1496        */
1497       forward_immediate = TRUE;
1498       break;
1499     }
1500     case GST_EVENT_TAG:
1501     {
1502       GstTagList *tags;
1503
1504       gst_event_parse_tag (event, &tags);
1505
1506       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1507         GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1508         if (priv->upstream_tags != tags) {
1509           if (priv->upstream_tags)
1510             gst_tag_list_unref (priv->upstream_tags);
1511           priv->upstream_tags = gst_tag_list_ref (tags);
1512           GST_INFO_OBJECT (decoder, "upstream tags: %" GST_PTR_FORMAT, tags);
1513         }
1514         gst_event_unref (event);
1515         event = gst_video_decoder_create_merged_tags_event (decoder);
1516         GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1517         if (!event)
1518           ret = TRUE;
1519       }
1520       break;
1521     }
1522     default:
1523       break;
1524   }
1525
1526   /* Forward non-serialized events immediately, and all other
1527    * events which can be forwarded immediately without potentially
1528    * causing the event to go out of order with other events and
1529    * buffers as decided above.
1530    */
1531   if (event) {
1532     if (!GST_EVENT_IS_SERIALIZED (event) || forward_immediate) {
1533       ret = gst_video_decoder_push_event (decoder, event);
1534     } else {
1535       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
1536       decoder->priv->current_frame_events =
1537           g_list_prepend (decoder->priv->current_frame_events, event);
1538       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1539       ret = TRUE;
1540     }
1541   }
1542
1543   return ret;
1544
1545 newseg_wrong_format:
1546   {
1547     GST_DEBUG_OBJECT (decoder, "received non TIME newsegment");
1548     gst_event_unref (event);
1549     /* SWALLOW EVENT */
1550     return TRUE;
1551   }
1552 }
1553
1554 static gboolean
1555 gst_video_decoder_sink_event (GstPad * pad, GstObject * parent,
1556     GstEvent * event)
1557 {
1558   GstVideoDecoder *decoder;
1559   GstVideoDecoderClass *decoder_class;
1560   gboolean ret = FALSE;
1561
1562   decoder = GST_VIDEO_DECODER (parent);
1563   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1564
1565   GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1566       GST_EVENT_TYPE_NAME (event));
1567
1568   if (decoder_class->sink_event)
1569     ret = decoder_class->sink_event (decoder, event);
1570
1571   return ret;
1572 }
1573
1574 /* perform upstream byte <-> time conversion (duration, seeking)
1575  * if subclass allows and if enough data for moderately decent conversion */
1576 static inline gboolean
1577 gst_video_decoder_do_byte (GstVideoDecoder * dec)
1578 {
1579   gboolean ret;
1580
1581   GST_OBJECT_LOCK (dec);
1582   ret = dec->priv->do_estimate_rate && (dec->priv->bytes_out > 0)
1583       && (dec->priv->time > GST_SECOND);
1584   GST_OBJECT_UNLOCK (dec);
1585
1586   return ret;
1587 }
1588
1589 static gboolean
1590 gst_video_decoder_do_seek (GstVideoDecoder * dec, GstEvent * event)
1591 {
1592   GstFormat format;
1593   GstSeekFlags flags;
1594   GstSeekType start_type, end_type;
1595   gdouble rate;
1596   gint64 start, start_time, end_time;
1597   GstSegment seek_segment;
1598   guint32 seqnum;
1599
1600   gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
1601       &start_time, &end_type, &end_time);
1602
1603   /* we'll handle plain open-ended flushing seeks with the simple approach */
1604   if (rate != 1.0) {
1605     GST_DEBUG_OBJECT (dec, "unsupported seek: rate");
1606     return FALSE;
1607   }
1608
1609   if (start_type != GST_SEEK_TYPE_SET) {
1610     GST_DEBUG_OBJECT (dec, "unsupported seek: start time");
1611     return FALSE;
1612   }
1613
1614   if ((end_type != GST_SEEK_TYPE_SET && end_type != GST_SEEK_TYPE_NONE) ||
1615       (end_type == GST_SEEK_TYPE_SET && end_time != GST_CLOCK_TIME_NONE)) {
1616     GST_DEBUG_OBJECT (dec, "unsupported seek: end time");
1617     return FALSE;
1618   }
1619
1620   if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1621     GST_DEBUG_OBJECT (dec, "unsupported seek: not flushing");
1622     return FALSE;
1623   }
1624
1625   memcpy (&seek_segment, &dec->output_segment, sizeof (seek_segment));
1626   gst_segment_do_seek (&seek_segment, rate, format, flags, start_type,
1627       start_time, end_type, end_time, NULL);
1628   start_time = seek_segment.position;
1629
1630   if (!gst_pad_query_convert (dec->sinkpad, GST_FORMAT_TIME, start_time,
1631           GST_FORMAT_BYTES, &start)) {
1632     GST_DEBUG_OBJECT (dec, "conversion failed");
1633     return FALSE;
1634   }
1635
1636   seqnum = gst_event_get_seqnum (event);
1637   event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
1638       GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
1639   gst_event_set_seqnum (event, seqnum);
1640
1641   GST_DEBUG_OBJECT (dec, "seeking to %" GST_TIME_FORMAT " at byte offset %"
1642       G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
1643
1644   return gst_pad_push_event (dec->sinkpad, event);
1645 }
1646
1647 static gboolean
1648 gst_video_decoder_src_event_default (GstVideoDecoder * decoder,
1649     GstEvent * event)
1650 {
1651   GstVideoDecoderPrivate *priv;
1652   gboolean res = FALSE;
1653
1654   priv = decoder->priv;
1655
1656   GST_DEBUG_OBJECT (decoder,
1657       "received event %d, %s", GST_EVENT_TYPE (event),
1658       GST_EVENT_TYPE_NAME (event));
1659
1660   switch (GST_EVENT_TYPE (event)) {
1661     case GST_EVENT_SEEK:
1662     {
1663       GstFormat format;
1664       gdouble rate;
1665       GstSeekFlags flags;
1666       GstSeekType start_type, stop_type;
1667       gint64 start, stop;
1668       gint64 tstart, tstop;
1669       guint32 seqnum;
1670
1671       gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1672           &stop_type, &stop);
1673       seqnum = gst_event_get_seqnum (event);
1674
1675       /* upstream gets a chance first */
1676       if ((res = gst_pad_push_event (decoder->sinkpad, event)))
1677         break;
1678
1679       /* if upstream fails for a time seek, maybe we can help if allowed */
1680       if (format == GST_FORMAT_TIME) {
1681         if (gst_video_decoder_do_byte (decoder))
1682           res = gst_video_decoder_do_seek (decoder, event);
1683         break;
1684       }
1685
1686       /* ... though a non-time seek can be aided as well */
1687       /* First bring the requested format to time */
1688       if (!(res =
1689               gst_pad_query_convert (decoder->srcpad, format, start,
1690                   GST_FORMAT_TIME, &tstart)))
1691         goto convert_error;
1692       if (!(res =
1693               gst_pad_query_convert (decoder->srcpad, format, stop,
1694                   GST_FORMAT_TIME, &tstop)))
1695         goto convert_error;
1696
1697       /* then seek with time on the peer */
1698       event = gst_event_new_seek (rate, GST_FORMAT_TIME,
1699           flags, start_type, tstart, stop_type, tstop);
1700       gst_event_set_seqnum (event, seqnum);
1701
1702       res = gst_pad_push_event (decoder->sinkpad, event);
1703       break;
1704     }
1705     case GST_EVENT_QOS:
1706     {
1707       GstQOSType type;
1708       gdouble proportion;
1709       GstClockTimeDiff diff;
1710       GstClockTime timestamp;
1711
1712       gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
1713
1714       GST_OBJECT_LOCK (decoder);
1715       priv->proportion = proportion;
1716       if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) {
1717         if (G_UNLIKELY (diff > 0)) {
1718           priv->earliest_time = timestamp + 2 * diff + priv->qos_frame_duration;
1719         } else {
1720           priv->earliest_time = timestamp + diff;
1721         }
1722       } else {
1723         priv->earliest_time = GST_CLOCK_TIME_NONE;
1724       }
1725       GST_OBJECT_UNLOCK (decoder);
1726
1727       GST_DEBUG_OBJECT (decoder,
1728           "got QoS %" GST_TIME_FORMAT ", %" GST_STIME_FORMAT ", %g",
1729           GST_TIME_ARGS (timestamp), GST_STIME_ARGS (diff), proportion);
1730
1731       res = gst_pad_push_event (decoder->sinkpad, event);
1732       break;
1733     }
1734     default:
1735       res = gst_pad_push_event (decoder->sinkpad, event);
1736       break;
1737   }
1738 done:
1739   return res;
1740
1741 convert_error:
1742   GST_DEBUG_OBJECT (decoder, "could not convert format");
1743   goto done;
1744 }
1745
1746 static gboolean
1747 gst_video_decoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1748 {
1749   GstVideoDecoder *decoder;
1750   GstVideoDecoderClass *decoder_class;
1751   gboolean ret = FALSE;
1752
1753   decoder = GST_VIDEO_DECODER (parent);
1754   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1755
1756   GST_DEBUG_OBJECT (decoder, "received event %d, %s", GST_EVENT_TYPE (event),
1757       GST_EVENT_TYPE_NAME (event));
1758
1759   if (decoder_class->src_event)
1760     ret = decoder_class->src_event (decoder, event);
1761
1762   return ret;
1763 }
1764
1765 static gboolean
1766 gst_video_decoder_src_query_default (GstVideoDecoder * dec, GstQuery * query)
1767 {
1768   GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (dec);
1769   gboolean res = TRUE;
1770
1771   GST_LOG_OBJECT (dec, "handling query: %" GST_PTR_FORMAT, query);
1772
1773   switch (GST_QUERY_TYPE (query)) {
1774     case GST_QUERY_POSITION:
1775     {
1776       GstFormat format;
1777       gint64 time, value;
1778
1779       /* upstream gets a chance first */
1780       if ((res = gst_pad_peer_query (dec->sinkpad, query))) {
1781         GST_LOG_OBJECT (dec, "returning peer response");
1782         break;
1783       }
1784
1785       /* Refuse BYTES format queries. If it made sense to
1786        * answer them, upstream would have already */
1787       gst_query_parse_position (query, &format, NULL);
1788
1789       if (format == GST_FORMAT_BYTES) {
1790         GST_LOG_OBJECT (dec, "Ignoring BYTES position query");
1791         break;
1792       }
1793
1794       /* we start from the last seen time */
1795       time = dec->priv->last_timestamp_out;
1796       /* correct for the segment values */
1797       time = gst_segment_to_stream_time (&dec->output_segment,
1798           GST_FORMAT_TIME, time);
1799
1800       GST_LOG_OBJECT (dec,
1801           "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
1802
1803       /* and convert to the final format */
1804       if (!(res = gst_pad_query_convert (pad, GST_FORMAT_TIME, time,
1805                   format, &value)))
1806         break;
1807
1808       gst_query_set_position (query, format, value);
1809
1810       GST_LOG_OBJECT (dec,
1811           "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
1812           format);
1813       break;
1814     }
1815     case GST_QUERY_DURATION:
1816     {
1817       GstFormat format;
1818
1819       /* upstream in any case */
1820       if ((res = gst_pad_query_default (pad, GST_OBJECT (dec), query)))
1821         break;
1822
1823       gst_query_parse_duration (query, &format, NULL);
1824       /* try answering TIME by converting from BYTE if subclass allows  */
1825       if (format == GST_FORMAT_TIME && gst_video_decoder_do_byte (dec)) {
1826         gint64 value;
1827
1828         if (gst_pad_peer_query_duration (dec->sinkpad, GST_FORMAT_BYTES,
1829                 &value)) {
1830           GST_LOG_OBJECT (dec, "upstream size %" G_GINT64_FORMAT, value);
1831           if (gst_pad_query_convert (dec->sinkpad,
1832                   GST_FORMAT_BYTES, value, GST_FORMAT_TIME, &value)) {
1833             gst_query_set_duration (query, GST_FORMAT_TIME, value);
1834             res = TRUE;
1835           }
1836         }
1837       }
1838       break;
1839     }
1840     case GST_QUERY_CONVERT:
1841     {
1842       GstFormat src_fmt, dest_fmt;
1843       gint64 src_val, dest_val;
1844
1845       GST_DEBUG_OBJECT (dec, "convert query");
1846
1847       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1848       GST_OBJECT_LOCK (dec);
1849       if (dec->priv->output_state != NULL)
1850         res = __gst_video_rawvideo_convert (dec->priv->output_state,
1851             src_fmt, src_val, &dest_fmt, &dest_val);
1852       else
1853         res = FALSE;
1854       GST_OBJECT_UNLOCK (dec);
1855       if (!res)
1856         goto error;
1857       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1858       break;
1859     }
1860     case GST_QUERY_LATENCY:
1861     {
1862       gboolean live;
1863       GstClockTime min_latency, max_latency;
1864
1865       res = gst_pad_peer_query (dec->sinkpad, query);
1866       if (res) {
1867         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1868         GST_DEBUG_OBJECT (dec, "Peer qlatency: live %d, min %"
1869             GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1870             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1871
1872         GST_OBJECT_LOCK (dec);
1873         min_latency += dec->priv->min_latency;
1874         if (max_latency == GST_CLOCK_TIME_NONE
1875             || dec->priv->max_latency == GST_CLOCK_TIME_NONE)
1876           max_latency = GST_CLOCK_TIME_NONE;
1877         else
1878           max_latency += dec->priv->max_latency;
1879         GST_OBJECT_UNLOCK (dec);
1880
1881         gst_query_set_latency (query, live, min_latency, max_latency);
1882       }
1883     }
1884       break;
1885     default:
1886       res = gst_pad_query_default (pad, GST_OBJECT (dec), query);
1887   }
1888   return res;
1889
1890 error:
1891   GST_ERROR_OBJECT (dec, "query failed");
1892   return res;
1893 }
1894
1895 static gboolean
1896 gst_video_decoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1897 {
1898   GstVideoDecoder *decoder;
1899   GstVideoDecoderClass *decoder_class;
1900   gboolean ret = FALSE;
1901
1902   decoder = GST_VIDEO_DECODER (parent);
1903   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
1904
1905   GST_DEBUG_OBJECT (decoder, "received query %d, %s", GST_QUERY_TYPE (query),
1906       GST_QUERY_TYPE_NAME (query));
1907
1908   if (decoder_class->src_query)
1909     ret = decoder_class->src_query (decoder, query);
1910
1911   return ret;
1912 }
1913
1914 /**
1915  * gst_video_decoder_proxy_getcaps:
1916  * @decoder: a #GstVideoDecoder
1917  * @caps: (allow-none): initial caps
1918  * @filter: (allow-none): filter caps
1919  *
1920  * Returns caps that express @caps (or sink template caps if @caps == NULL)
1921  * restricted to resolution/format/... combinations supported by downstream
1922  * elements.
1923  *
1924  * Returns: (transfer full): a #GstCaps owned by caller
1925  *
1926  * Since: 1.6
1927  */
1928 GstCaps *
1929 gst_video_decoder_proxy_getcaps (GstVideoDecoder * decoder, GstCaps * caps,
1930     GstCaps * filter)
1931 {
1932   return __gst_video_element_proxy_getcaps (GST_ELEMENT_CAST (decoder),
1933       GST_VIDEO_DECODER_SINK_PAD (decoder),
1934       GST_VIDEO_DECODER_SRC_PAD (decoder), caps, filter);
1935 }
1936
1937 static GstCaps *
1938 gst_video_decoder_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
1939 {
1940   GstVideoDecoderClass *klass;
1941   GstCaps *caps;
1942
1943   klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1944
1945   if (klass->getcaps)
1946     caps = klass->getcaps (decoder, filter);
1947   else
1948     caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
1949
1950   GST_LOG_OBJECT (decoder, "Returning caps %" GST_PTR_FORMAT, caps);
1951
1952   return caps;
1953 }
1954
1955 static gboolean
1956 gst_video_decoder_sink_query_default (GstVideoDecoder * decoder,
1957     GstQuery * query)
1958 {
1959   GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
1960   GstVideoDecoderPrivate *priv;
1961   gboolean res = FALSE;
1962
1963   priv = decoder->priv;
1964
1965   GST_LOG_OBJECT (decoder, "handling query: %" GST_PTR_FORMAT, query);
1966
1967   switch (GST_QUERY_TYPE (query)) {
1968     case GST_QUERY_CONVERT:
1969     {
1970       GstFormat src_fmt, dest_fmt;
1971       gint64 src_val, dest_val;
1972
1973       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1974       GST_OBJECT_LOCK (decoder);
1975       res =
1976           __gst_video_encoded_video_convert (priv->bytes_out, priv->time,
1977           src_fmt, src_val, &dest_fmt, &dest_val);
1978       GST_OBJECT_UNLOCK (decoder);
1979       if (!res)
1980         goto error;
1981       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1982       break;
1983     }
1984     case GST_QUERY_ALLOCATION:{
1985       GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
1986
1987       if (klass->propose_allocation)
1988         res = klass->propose_allocation (decoder, query);
1989       break;
1990     }
1991     case GST_QUERY_CAPS:{
1992       GstCaps *filter, *caps;
1993
1994       gst_query_parse_caps (query, &filter);
1995       caps = gst_video_decoder_sink_getcaps (decoder, filter);
1996       gst_query_set_caps_result (query, caps);
1997       gst_caps_unref (caps);
1998       res = TRUE;
1999       break;
2000     }
2001     case GST_QUERY_ACCEPT_CAPS:{
2002       if (decoder->priv->use_default_pad_acceptcaps) {
2003         res =
2004             gst_pad_query_default (GST_VIDEO_DECODER_SINK_PAD (decoder),
2005             GST_OBJECT_CAST (decoder), query);
2006       } else {
2007         GstCaps *caps;
2008         GstCaps *allowed_caps;
2009         GstCaps *template_caps;
2010         gboolean accept;
2011
2012         gst_query_parse_accept_caps (query, &caps);
2013
2014         template_caps = gst_pad_get_pad_template_caps (pad);
2015         accept = gst_caps_is_subset (caps, template_caps);
2016         gst_caps_unref (template_caps);
2017
2018         if (accept) {
2019           allowed_caps =
2020               gst_pad_query_caps (GST_VIDEO_DECODER_SINK_PAD (decoder), caps);
2021
2022           accept = gst_caps_can_intersect (caps, allowed_caps);
2023
2024           gst_caps_unref (allowed_caps);
2025         }
2026
2027         gst_query_set_accept_caps_result (query, accept);
2028         res = TRUE;
2029       }
2030       break;
2031     }
2032     default:
2033       res = gst_pad_query_default (pad, GST_OBJECT (decoder), query);
2034       break;
2035   }
2036 done:
2037
2038   return res;
2039 error:
2040   GST_DEBUG_OBJECT (decoder, "query failed");
2041   goto done;
2042
2043 }
2044
2045 static gboolean
2046 gst_video_decoder_sink_query (GstPad * pad, GstObject * parent,
2047     GstQuery * query)
2048 {
2049   GstVideoDecoder *decoder;
2050   GstVideoDecoderClass *decoder_class;
2051   gboolean ret = FALSE;
2052
2053   decoder = GST_VIDEO_DECODER (parent);
2054   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
2055
2056   GST_DEBUG_OBJECT (decoder, "received query %d, %s", GST_QUERY_TYPE (query),
2057       GST_QUERY_TYPE_NAME (query));
2058
2059   if (decoder_class->sink_query)
2060     ret = decoder_class->sink_query (decoder, query);
2061
2062   return ret;
2063 }
2064
2065 typedef struct _Timestamp Timestamp;
2066 struct _Timestamp
2067 {
2068   guint64 offset;
2069   GstClockTime pts;
2070   GstClockTime dts;
2071   GstClockTime duration;
2072   guint flags;
2073 };
2074
2075 static void
2076 timestamp_free (Timestamp * ts)
2077 {
2078   g_slice_free (Timestamp, ts);
2079 }
2080
2081 static void
2082 gst_video_decoder_add_buffer_info (GstVideoDecoder * decoder,
2083     GstBuffer * buffer)
2084 {
2085   GstVideoDecoderPrivate *priv = decoder->priv;
2086   Timestamp *ts;
2087
2088   if (!GST_BUFFER_PTS_IS_VALID (buffer) &&
2089       !GST_BUFFER_DTS_IS_VALID (buffer) &&
2090       !GST_BUFFER_DURATION_IS_VALID (buffer) &&
2091       GST_BUFFER_FLAGS (buffer) == 0) {
2092     /* Save memory - don't bother storing info
2093      * for buffers with no distinguishing info */
2094     return;
2095   }
2096
2097   ts = g_slice_new (Timestamp);
2098
2099   GST_LOG_OBJECT (decoder,
2100       "adding PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT
2101       " (offset:%" G_GUINT64_FORMAT ")",
2102       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2103       GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), priv->input_offset);
2104
2105   ts->offset = priv->input_offset;
2106   ts->pts = GST_BUFFER_PTS (buffer);
2107   ts->dts = GST_BUFFER_DTS (buffer);
2108   ts->duration = GST_BUFFER_DURATION (buffer);
2109   ts->flags = GST_BUFFER_FLAGS (buffer);
2110
2111   g_queue_push_tail (&priv->timestamps, ts);
2112 }
2113
2114 static void
2115 gst_video_decoder_get_buffer_info_at_offset (GstVideoDecoder *
2116     decoder, guint64 offset, GstClockTime * pts, GstClockTime * dts,
2117     GstClockTime * duration, guint * flags)
2118 {
2119 #ifndef GST_DISABLE_GST_DEBUG
2120   guint64 got_offset = 0;
2121 #endif
2122   Timestamp *ts;
2123   GList *g;
2124
2125   *pts = GST_CLOCK_TIME_NONE;
2126   *dts = GST_CLOCK_TIME_NONE;
2127   *duration = GST_CLOCK_TIME_NONE;
2128   *flags = 0;
2129
2130   g = decoder->priv->timestamps.head;
2131   while (g) {
2132     ts = g->data;
2133     if (ts->offset <= offset) {
2134       GList *next = g->next;
2135 #ifndef GST_DISABLE_GST_DEBUG
2136       got_offset = ts->offset;
2137 #endif
2138       *pts = ts->pts;
2139       *dts = ts->dts;
2140       *duration = ts->duration;
2141       *flags = ts->flags;
2142       g_queue_delete_link (&decoder->priv->timestamps, g);
2143       g = next;
2144       timestamp_free (ts);
2145     } else {
2146       break;
2147     }
2148   }
2149
2150   GST_LOG_OBJECT (decoder,
2151       "got PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT " flags %x @ offs %"
2152       G_GUINT64_FORMAT " (wanted offset:%" G_GUINT64_FORMAT ")",
2153       GST_TIME_ARGS (*pts), GST_TIME_ARGS (*dts), *flags, got_offset, offset);
2154 }
2155
2156 #if !GLIB_CHECK_VERSION(2, 60, 0)
2157 #define g_queue_clear_full queue_clear_full
2158 static void
2159 queue_clear_full (GQueue * queue, GDestroyNotify free_func)
2160 {
2161   gpointer data;
2162
2163   while ((data = g_queue_pop_head (queue)) != NULL)
2164     free_func (data);
2165 }
2166 #endif
2167
2168 static void
2169 gst_video_decoder_clear_queues (GstVideoDecoder * dec)
2170 {
2171   GstVideoDecoderPrivate *priv = dec->priv;
2172
2173   g_list_free_full (priv->output_queued,
2174       (GDestroyNotify) gst_mini_object_unref);
2175   priv->output_queued = NULL;
2176
2177   g_list_free_full (priv->gather, (GDestroyNotify) gst_mini_object_unref);
2178   priv->gather = NULL;
2179   g_list_free_full (priv->decode, (GDestroyNotify) gst_video_codec_frame_unref);
2180   priv->decode = NULL;
2181   g_list_free_full (priv->parse, (GDestroyNotify) gst_mini_object_unref);
2182   priv->parse = NULL;
2183   g_list_free_full (priv->parse_gather,
2184       (GDestroyNotify) gst_video_codec_frame_unref);
2185   priv->parse_gather = NULL;
2186   g_queue_clear_full (&priv->frames,
2187       (GDestroyNotify) gst_video_codec_frame_unref);
2188 }
2189
2190 static void
2191 gst_video_decoder_reset (GstVideoDecoder * decoder, gboolean full,
2192     gboolean flush_hard)
2193 {
2194   GstVideoDecoderPrivate *priv = decoder->priv;
2195
2196   GST_DEBUG_OBJECT (decoder, "reset full %d", full);
2197
2198   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2199
2200   if (full || flush_hard) {
2201     gst_segment_init (&decoder->input_segment, GST_FORMAT_UNDEFINED);
2202     gst_segment_init (&decoder->output_segment, GST_FORMAT_UNDEFINED);
2203     gst_video_decoder_clear_queues (decoder);
2204     decoder->priv->in_out_segment_sync = TRUE;
2205
2206     if (priv->current_frame) {
2207       gst_video_codec_frame_unref (priv->current_frame);
2208       priv->current_frame = NULL;
2209     }
2210
2211     g_list_free_full (priv->current_frame_events,
2212         (GDestroyNotify) gst_event_unref);
2213     priv->current_frame_events = NULL;
2214     g_list_free_full (priv->pending_events, (GDestroyNotify) gst_event_unref);
2215     priv->pending_events = NULL;
2216
2217     priv->error_count = 0;
2218     priv->had_output_data = FALSE;
2219     priv->had_input_data = FALSE;
2220
2221     GST_OBJECT_LOCK (decoder);
2222     priv->earliest_time = GST_CLOCK_TIME_NONE;
2223     priv->proportion = 0.5;
2224     priv->decode_flags_override = FALSE;
2225
2226     priv->request_sync_point_flags = 0;
2227     priv->request_sync_point_frame_number = REQUEST_SYNC_POINT_UNSET;
2228     priv->last_force_key_unit_time = GST_CLOCK_TIME_NONE;
2229     GST_OBJECT_UNLOCK (decoder);
2230     priv->distance_from_sync = -1;
2231   }
2232
2233   if (full) {
2234     if (priv->input_state)
2235       gst_video_codec_state_unref (priv->input_state);
2236     priv->input_state = NULL;
2237     GST_OBJECT_LOCK (decoder);
2238     if (priv->output_state)
2239       gst_video_codec_state_unref (priv->output_state);
2240     priv->output_state = NULL;
2241
2242     priv->qos_frame_duration = 0;
2243     GST_OBJECT_UNLOCK (decoder);
2244
2245     if (priv->tags)
2246       gst_tag_list_unref (priv->tags);
2247     priv->tags = NULL;
2248     priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
2249     if (priv->upstream_tags) {
2250       gst_tag_list_unref (priv->upstream_tags);
2251       priv->upstream_tags = NULL;
2252     }
2253     priv->tags_changed = FALSE;
2254     priv->reordered_output = FALSE;
2255
2256     priv->dropped = 0;
2257     priv->processed = 0;
2258
2259     priv->decode_frame_number = 0;
2260     priv->base_picture_number = 0;
2261
2262     if (priv->pool) {
2263       GST_DEBUG_OBJECT (decoder, "deactivate pool %" GST_PTR_FORMAT,
2264           priv->pool);
2265       gst_buffer_pool_set_active (priv->pool, FALSE);
2266       gst_object_unref (priv->pool);
2267       priv->pool = NULL;
2268     }
2269
2270     if (priv->allocator) {
2271       gst_object_unref (priv->allocator);
2272       priv->allocator = NULL;
2273     }
2274   }
2275
2276   priv->discont = TRUE;
2277
2278   priv->base_timestamp = GST_CLOCK_TIME_NONE;
2279   priv->last_timestamp_out = GST_CLOCK_TIME_NONE;
2280   priv->pts_delta = GST_CLOCK_TIME_NONE;
2281
2282   priv->input_offset = 0;
2283   priv->frame_offset = 0;
2284   gst_adapter_clear (priv->input_adapter);
2285   gst_adapter_clear (priv->output_adapter);
2286   g_queue_clear_full (&priv->timestamps, (GDestroyNotify) timestamp_free);
2287
2288   GST_OBJECT_LOCK (decoder);
2289   priv->bytes_out = 0;
2290   priv->time = 0;
2291   GST_OBJECT_UNLOCK (decoder);
2292
2293 #ifndef GST_DISABLE_DEBUG
2294   priv->last_reset_time = gst_util_get_timestamp ();
2295 #endif
2296
2297   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2298 }
2299
2300 static GstFlowReturn
2301 gst_video_decoder_chain_forward (GstVideoDecoder * decoder,
2302     GstBuffer * buf, gboolean at_eos)
2303 {
2304   GstVideoDecoderPrivate *priv;
2305   GstVideoDecoderClass *klass;
2306   GstFlowReturn ret = GST_FLOW_OK;
2307
2308   klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
2309   priv = decoder->priv;
2310
2311   g_return_val_if_fail (priv->packetized || klass->parse, GST_FLOW_ERROR);
2312
2313   /* Draining on DISCONT is handled in chain_reverse() for reverse playback,
2314    * and this function would only be called to get everything collected GOP
2315    * by GOP in the parse_gather list */
2316   if (decoder->input_segment.rate > 0.0 && GST_BUFFER_IS_DISCONT (buf)
2317       && (decoder->input_segment.flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS))
2318     ret = gst_video_decoder_drain_out (decoder, FALSE);
2319
2320   if (priv->current_frame == NULL)
2321     priv->current_frame = gst_video_decoder_new_frame (decoder);
2322
2323   if (!priv->packetized)
2324     gst_video_decoder_add_buffer_info (decoder, buf);
2325
2326   priv->input_offset += gst_buffer_get_size (buf);
2327
2328   if (priv->packetized) {
2329     gboolean was_keyframe = FALSE;
2330     if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2331       was_keyframe = TRUE;
2332       GST_DEBUG_OBJECT (decoder, "Marking current_frame as sync point");
2333       GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (priv->current_frame);
2334     }
2335
2336     priv->current_frame->input_buffer = buf;
2337
2338     if (decoder->input_segment.rate < 0.0) {
2339       priv->parse_gather =
2340           g_list_prepend (priv->parse_gather, priv->current_frame);
2341     } else {
2342       ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
2343     }
2344     priv->current_frame = NULL;
2345     /* If in trick mode and it was a keyframe, drain decoder to avoid extra
2346      * latency. Only do this for forwards playback as reverse playback handles
2347      * draining on keyframes in flush_parse(), and would otherwise call back
2348      * from drain_out() to here causing an infinite loop.
2349      * Also this function is only called for reverse playback to gather frames
2350      * GOP by GOP, and does not do any actual decoding. That would be done by
2351      * flush_decode() */
2352     if (ret == GST_FLOW_OK && was_keyframe && decoder->input_segment.rate > 0.0
2353         && (decoder->input_segment.flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS))
2354       ret = gst_video_decoder_drain_out (decoder, FALSE);
2355   } else {
2356     gst_adapter_push (priv->input_adapter, buf);
2357
2358     ret = gst_video_decoder_parse_available (decoder, at_eos, TRUE);
2359   }
2360
2361   if (ret == GST_VIDEO_DECODER_FLOW_NEED_DATA)
2362     return GST_FLOW_OK;
2363
2364   return ret;
2365 }
2366
2367 static GstFlowReturn
2368 gst_video_decoder_flush_decode (GstVideoDecoder * dec)
2369 {
2370   GstVideoDecoderPrivate *priv = dec->priv;
2371   GstFlowReturn res = GST_FLOW_OK;
2372   GList *walk;
2373
2374   GST_DEBUG_OBJECT (dec, "flushing buffers to decode");
2375
2376   walk = priv->decode;
2377   while (walk) {
2378     GList *next;
2379     GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
2380
2381     GST_DEBUG_OBJECT (dec, "decoding frame %p buffer %p, PTS %" GST_TIME_FORMAT
2382         ", DTS %" GST_TIME_FORMAT, frame, frame->input_buffer,
2383         GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
2384         GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
2385
2386     next = walk->next;
2387
2388     priv->decode = g_list_delete_link (priv->decode, walk);
2389
2390     /* decode buffer, resulting data prepended to queue */
2391     res = gst_video_decoder_decode_frame (dec, frame);
2392     if (res != GST_FLOW_OK)
2393       break;
2394
2395     walk = next;
2396   }
2397
2398   return res;
2399 }
2400
2401 /* gst_video_decoder_flush_parse is called from the
2402  * chain_reverse() function when a buffer containing
2403  * a DISCONT - indicating that reverse playback
2404  * looped back to the next data block, and therefore
2405  * all available data should be fed through the
2406  * decoder and frames gathered for reversed output
2407  */
2408 static GstFlowReturn
2409 gst_video_decoder_flush_parse (GstVideoDecoder * dec, gboolean at_eos)
2410 {
2411   GstVideoDecoderPrivate *priv = dec->priv;
2412   GstFlowReturn res = GST_FLOW_OK;
2413   GList *walk;
2414   GstVideoDecoderClass *decoder_class;
2415
2416   decoder_class = GST_VIDEO_DECODER_GET_CLASS (dec);
2417
2418   GST_DEBUG_OBJECT (dec, "flushing buffers to parsing");
2419
2420   /* Reverse the gather list, and prepend it to the parse list,
2421    * then flush to parse whatever we can */
2422   priv->gather = g_list_reverse (priv->gather);
2423   priv->parse = g_list_concat (priv->gather, priv->parse);
2424   priv->gather = NULL;
2425
2426   /* clear buffer and decoder state */
2427   gst_video_decoder_flush (dec, FALSE);
2428
2429   walk = priv->parse;
2430   while (walk) {
2431     GstBuffer *buf = GST_BUFFER_CAST (walk->data);
2432     GList *next = walk->next;
2433
2434     GST_DEBUG_OBJECT (dec, "parsing buffer %p, PTS %" GST_TIME_FORMAT
2435         ", DTS %" GST_TIME_FORMAT " flags %x", buf,
2436         GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2437         GST_TIME_ARGS (GST_BUFFER_DTS (buf)), GST_BUFFER_FLAGS (buf));
2438
2439     /* parse buffer, resulting frames prepended to parse_gather queue */
2440     gst_buffer_ref (buf);
2441     res = gst_video_decoder_chain_forward (dec, buf, at_eos);
2442
2443     /* if we generated output, we can discard the buffer, else we
2444      * keep it in the queue */
2445     if (priv->parse_gather) {
2446       GST_DEBUG_OBJECT (dec, "parsed buffer to %p", priv->parse_gather->data);
2447       priv->parse = g_list_delete_link (priv->parse, walk);
2448       gst_buffer_unref (buf);
2449     } else {
2450       GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
2451     }
2452     walk = next;
2453   }
2454
2455   walk = priv->parse_gather;
2456   while (walk) {
2457     GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
2458     GList *walk2;
2459
2460     /* this is reverse playback, check if we need to apply some segment
2461      * to the output before decoding, as during decoding the segment.rate
2462      * must be used to determine if a buffer should be pushed or added to
2463      * the output list for reverse pushing.
2464      *
2465      * The new segment is not immediately pushed here because we must
2466      * wait for negotiation to happen before it can be pushed to avoid
2467      * pushing a segment before caps event. Negotiation only happens
2468      * when finish_frame is called.
2469      */
2470     for (walk2 = frame->events; walk2;) {
2471       GList *cur = walk2;
2472       GstEvent *event = walk2->data;
2473
2474       walk2 = g_list_next (walk2);
2475       if (GST_EVENT_TYPE (event) <= GST_EVENT_SEGMENT) {
2476
2477         if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2478           GstSegment segment;
2479
2480           GST_DEBUG_OBJECT (dec, "Segment at frame %p %" GST_TIME_FORMAT,
2481               frame, GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)));
2482           gst_event_copy_segment (event, &segment);
2483           if (segment.format == GST_FORMAT_TIME) {
2484             dec->output_segment = segment;
2485             dec->priv->in_out_segment_sync =
2486                 gst_segment_is_equal (&dec->input_segment, &segment);
2487           }
2488         }
2489         dec->priv->pending_events =
2490             g_list_append (dec->priv->pending_events, event);
2491         frame->events = g_list_delete_link (frame->events, cur);
2492       }
2493     }
2494
2495     walk = walk->next;
2496   }
2497
2498   /* now we can process frames. Start by moving each frame from the parse_gather
2499    * to the decode list, reverse the order as we go, and stopping when/if we
2500    * copy a keyframe. */
2501   GST_DEBUG_OBJECT (dec, "checking parsed frames for a keyframe to decode");
2502   walk = priv->parse_gather;
2503   while (walk) {
2504     GstVideoCodecFrame *frame = (GstVideoCodecFrame *) (walk->data);
2505
2506     /* remove from the gather list */
2507     priv->parse_gather = g_list_remove_link (priv->parse_gather, walk);
2508
2509     /* move it to the front of the decode queue */
2510     priv->decode = g_list_concat (walk, priv->decode);
2511
2512     /* if we copied a keyframe, flush and decode the decode queue */
2513     if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
2514       GST_DEBUG_OBJECT (dec, "found keyframe %p with PTS %" GST_TIME_FORMAT
2515           ", DTS %" GST_TIME_FORMAT, frame,
2516           GST_TIME_ARGS (GST_BUFFER_PTS (frame->input_buffer)),
2517           GST_TIME_ARGS (GST_BUFFER_DTS (frame->input_buffer)));
2518       res = gst_video_decoder_flush_decode (dec);
2519       if (res != GST_FLOW_OK)
2520         goto done;
2521
2522       /* We need to tell the subclass to drain now.
2523        * We prefer the drain vfunc, but for backward-compat
2524        * we use a finish() vfunc if drain isn't implemented */
2525       if (decoder_class->drain) {
2526         GST_DEBUG_OBJECT (dec, "Draining");
2527         res = decoder_class->drain (dec);
2528       } else if (decoder_class->finish) {
2529         GST_FIXME_OBJECT (dec, "Sub-class should implement drain(). "
2530             "Calling finish() for backwards-compat");
2531         res = decoder_class->finish (dec);
2532       }
2533
2534       if (res != GST_FLOW_OK)
2535         goto done;
2536
2537       /* now send queued data downstream */
2538       walk = priv->output_queued;
2539       while (walk) {
2540         GstBuffer *buf = GST_BUFFER_CAST (walk->data);
2541
2542         priv->output_queued =
2543             g_list_delete_link (priv->output_queued, priv->output_queued);
2544
2545         if (G_LIKELY (res == GST_FLOW_OK)) {
2546           /* avoid stray DISCONT from forward processing,
2547            * which have no meaning in reverse pushing */
2548           GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
2549
2550           /* Last chance to calculate a timestamp as we loop backwards
2551            * through the list */
2552           if (GST_BUFFER_TIMESTAMP (buf) != GST_CLOCK_TIME_NONE)
2553             priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
2554           else if (priv->last_timestamp_out != GST_CLOCK_TIME_NONE &&
2555               GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
2556             GST_BUFFER_TIMESTAMP (buf) =
2557                 priv->last_timestamp_out - GST_BUFFER_DURATION (buf);
2558             priv->last_timestamp_out = GST_BUFFER_TIMESTAMP (buf);
2559             GST_LOG_OBJECT (dec,
2560                 "Calculated TS %" GST_TIME_FORMAT " working backwards",
2561                 GST_TIME_ARGS (priv->last_timestamp_out));
2562           }
2563
2564           res = gst_video_decoder_clip_and_push_buf (dec, buf);
2565         } else {
2566           gst_buffer_unref (buf);
2567         }
2568
2569         walk = priv->output_queued;
2570       }
2571
2572       /* clear buffer and decoder state again
2573        * before moving to the previous keyframe */
2574       gst_video_decoder_flush (dec, FALSE);
2575     }
2576
2577     walk = priv->parse_gather;
2578   }
2579
2580 done:
2581   return res;
2582 }
2583
2584 static GstFlowReturn
2585 gst_video_decoder_chain_reverse (GstVideoDecoder * dec, GstBuffer * buf)
2586 {
2587   GstVideoDecoderPrivate *priv = dec->priv;
2588   GstFlowReturn result = GST_FLOW_OK;
2589
2590   /* if we have a discont, move buffers to the decode list */
2591   if (!buf || GST_BUFFER_IS_DISCONT (buf)) {
2592     GST_DEBUG_OBJECT (dec, "received discont");
2593
2594     /* parse and decode stuff in the gather and parse queues */
2595     result = gst_video_decoder_flush_parse (dec, FALSE);
2596   }
2597
2598   if (G_LIKELY (buf)) {
2599     GST_DEBUG_OBJECT (dec, "gathering buffer %p of size %" G_GSIZE_FORMAT ", "
2600         "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
2601         GST_TIME_FORMAT, buf, gst_buffer_get_size (buf),
2602         GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2603         GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
2604         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2605
2606     /* add buffer to gather queue */
2607     priv->gather = g_list_prepend (priv->gather, buf);
2608   }
2609
2610   return result;
2611 }
2612
2613 static GstFlowReturn
2614 gst_video_decoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2615 {
2616   GstVideoDecoder *decoder;
2617   GstFlowReturn ret = GST_FLOW_OK;
2618
2619   decoder = GST_VIDEO_DECODER (parent);
2620
2621   if (G_UNLIKELY (!decoder->priv->input_state && decoder->priv->needs_format))
2622     goto not_negotiated;
2623
2624   GST_LOG_OBJECT (decoder,
2625       "chain PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT " duration %"
2626       GST_TIME_FORMAT " size %" G_GSIZE_FORMAT " flags %x",
2627       GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2628       GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
2629       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
2630       gst_buffer_get_size (buf), GST_BUFFER_FLAGS (buf));
2631
2632   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2633
2634   /* NOTE:
2635    * requiring the pad to be negotiated makes it impossible to use
2636    * oggdemux or filesrc ! decoder */
2637
2638   if (decoder->input_segment.format == GST_FORMAT_UNDEFINED) {
2639     GstEvent *event;
2640     GstSegment *segment = &decoder->input_segment;
2641
2642     GST_WARNING_OBJECT (decoder,
2643         "Received buffer without a new-segment. "
2644         "Assuming timestamps start from 0.");
2645
2646     gst_segment_init (segment, GST_FORMAT_TIME);
2647
2648     event = gst_event_new_segment (segment);
2649
2650     decoder->priv->current_frame_events =
2651         g_list_prepend (decoder->priv->current_frame_events, event);
2652   }
2653
2654   decoder->priv->had_input_data = TRUE;
2655
2656   if (decoder->input_segment.rate > 0.0)
2657     ret = gst_video_decoder_chain_forward (decoder, buf, FALSE);
2658   else
2659     ret = gst_video_decoder_chain_reverse (decoder, buf);
2660
2661   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2662   return ret;
2663
2664   /* ERRORS */
2665 not_negotiated:
2666   {
2667     GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION, (NULL),
2668         ("decoder not initialized"));
2669     gst_buffer_unref (buf);
2670     return GST_FLOW_NOT_NEGOTIATED;
2671   }
2672 }
2673
2674 static GstStateChangeReturn
2675 gst_video_decoder_change_state (GstElement * element, GstStateChange transition)
2676 {
2677   GstVideoDecoder *decoder;
2678   GstVideoDecoderClass *decoder_class;
2679   GstStateChangeReturn ret;
2680
2681   decoder = GST_VIDEO_DECODER (element);
2682   decoder_class = GST_VIDEO_DECODER_GET_CLASS (element);
2683
2684   switch (transition) {
2685     case GST_STATE_CHANGE_NULL_TO_READY:
2686       /* open device/library if needed */
2687       if (decoder_class->open && !decoder_class->open (decoder))
2688         goto open_failed;
2689       break;
2690     case GST_STATE_CHANGE_READY_TO_PAUSED:
2691       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2692       gst_video_decoder_reset (decoder, TRUE, TRUE);
2693       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2694
2695       /* Initialize device/library if needed */
2696       if (decoder_class->start && !decoder_class->start (decoder))
2697         goto start_failed;
2698       break;
2699     default:
2700       break;
2701   }
2702
2703   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2704
2705   switch (transition) {
2706     case GST_STATE_CHANGE_PAUSED_TO_READY:{
2707       gboolean stopped = TRUE;
2708
2709       if (decoder_class->stop)
2710         stopped = decoder_class->stop (decoder);
2711
2712       GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2713       gst_video_decoder_reset (decoder, TRUE, TRUE);
2714       GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2715
2716       if (!stopped)
2717         goto stop_failed;
2718
2719       break;
2720     }
2721     case GST_STATE_CHANGE_READY_TO_NULL:
2722       /* close device/library if needed */
2723       if (decoder_class->close && !decoder_class->close (decoder))
2724         goto close_failed;
2725       break;
2726     default:
2727       break;
2728   }
2729
2730   return ret;
2731
2732   /* Errors */
2733 open_failed:
2734   {
2735     GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2736         ("Failed to open decoder"));
2737     return GST_STATE_CHANGE_FAILURE;
2738   }
2739
2740 start_failed:
2741   {
2742     GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2743         ("Failed to start decoder"));
2744     return GST_STATE_CHANGE_FAILURE;
2745   }
2746
2747 stop_failed:
2748   {
2749     GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2750         ("Failed to stop decoder"));
2751     return GST_STATE_CHANGE_FAILURE;
2752   }
2753
2754 close_failed:
2755   {
2756     GST_ELEMENT_ERROR (decoder, LIBRARY, INIT, (NULL),
2757         ("Failed to close decoder"));
2758     return GST_STATE_CHANGE_FAILURE;
2759   }
2760 }
2761
2762 static GstVideoCodecFrame *
2763 gst_video_decoder_new_frame (GstVideoDecoder * decoder)
2764 {
2765   GstVideoDecoderPrivate *priv = decoder->priv;
2766   GstVideoCodecFrame *frame;
2767
2768   frame = g_slice_new0 (GstVideoCodecFrame);
2769
2770   frame->ref_count = 1;
2771
2772   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
2773   frame->system_frame_number = priv->system_frame_number;
2774   priv->system_frame_number++;
2775   frame->decode_frame_number = priv->decode_frame_number;
2776   priv->decode_frame_number++;
2777
2778   frame->dts = GST_CLOCK_TIME_NONE;
2779   frame->pts = GST_CLOCK_TIME_NONE;
2780   frame->duration = GST_CLOCK_TIME_NONE;
2781   frame->events = priv->current_frame_events;
2782   priv->current_frame_events = NULL;
2783
2784   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
2785
2786   GST_LOG_OBJECT (decoder, "Created new frame %p (sfn:%d)",
2787       frame, frame->system_frame_number);
2788
2789   return frame;
2790 }
2791
2792 static void
2793 gst_video_decoder_push_event_list (GstVideoDecoder * decoder, GList * events)
2794 {
2795   GList *l;
2796
2797   /* events are stored in reverse order */
2798   for (l = g_list_last (events); l; l = g_list_previous (l)) {
2799     GST_LOG_OBJECT (decoder, "pushing %s event", GST_EVENT_TYPE_NAME (l->data));
2800     gst_video_decoder_push_event (decoder, l->data);
2801   }
2802   g_list_free (events);
2803 }
2804
2805 static void
2806 gst_video_decoder_prepare_finish_frame (GstVideoDecoder *
2807     decoder, GstVideoCodecFrame * frame, gboolean dropping)
2808 {
2809   GstVideoDecoderPrivate *priv = decoder->priv;
2810   GList *l, *events = NULL;
2811   gboolean sync;
2812
2813 #ifndef GST_DISABLE_GST_DEBUG
2814   GST_LOG_OBJECT (decoder, "n %d in %" G_GSIZE_FORMAT " out %" G_GSIZE_FORMAT,
2815       priv->frames.length,
2816       gst_adapter_available (priv->input_adapter),
2817       gst_adapter_available (priv->output_adapter));
2818 #endif
2819
2820   sync = GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame);
2821
2822   GST_LOG_OBJECT (decoder,
2823       "finish frame %p (#%d) sync:%d PTS:%" GST_TIME_FORMAT " DTS:%"
2824       GST_TIME_FORMAT,
2825       frame, frame->system_frame_number,
2826       sync, GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->dts));
2827
2828   /* Push all pending events that arrived before this frame */
2829   for (l = priv->frames.head; l; l = l->next) {
2830     GstVideoCodecFrame *tmp = l->data;
2831
2832     if (tmp->events) {
2833       events = g_list_concat (tmp->events, events);
2834       tmp->events = NULL;
2835     }
2836
2837     if (tmp == frame)
2838       break;
2839   }
2840
2841   if (dropping || !decoder->priv->output_state) {
2842     /* Push before the next frame that is not dropped */
2843     decoder->priv->pending_events =
2844         g_list_concat (events, decoder->priv->pending_events);
2845   } else {
2846     gst_video_decoder_push_event_list (decoder, decoder->priv->pending_events);
2847     decoder->priv->pending_events = NULL;
2848
2849     gst_video_decoder_push_event_list (decoder, events);
2850   }
2851
2852   /* Check if the data should not be displayed. For example altref/invisible
2853    * frame in vp8. In this case we should not update the timestamps. */
2854   if (GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame))
2855     return;
2856
2857   /* If the frame is meant to be output but we don't have an output_buffer
2858    * we have a problem :) */
2859   if (G_UNLIKELY ((frame->output_buffer == NULL) && !dropping))
2860     goto no_output_buffer;
2861
2862   if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
2863     if (frame->pts != priv->base_timestamp) {
2864       GST_DEBUG_OBJECT (decoder,
2865           "sync timestamp %" GST_TIME_FORMAT " diff %" GST_STIME_FORMAT,
2866           GST_TIME_ARGS (frame->pts),
2867           GST_STIME_ARGS (GST_CLOCK_DIFF (frame->pts,
2868                   decoder->output_segment.start)));
2869       priv->base_timestamp = frame->pts;
2870       priv->base_picture_number = frame->decode_frame_number;
2871     }
2872   }
2873
2874   if (frame->duration == GST_CLOCK_TIME_NONE) {
2875     frame->duration = gst_video_decoder_get_frame_duration (decoder, frame);
2876     GST_LOG_OBJECT (decoder,
2877         "Guessing duration %" GST_TIME_FORMAT " for frame...",
2878         GST_TIME_ARGS (frame->duration));
2879   }
2880
2881   /* PTS is expected montone ascending,
2882    * so a good guess is lowest unsent DTS */
2883   {
2884     GstClockTime min_ts = GST_CLOCK_TIME_NONE;
2885     GstVideoCodecFrame *oframe = NULL;
2886     gboolean seen_none = FALSE;
2887
2888     /* some maintenance regardless */
2889     for (l = priv->frames.head; l; l = l->next) {
2890       GstVideoCodecFrame *tmp = l->data;
2891
2892       if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
2893         seen_none = TRUE;
2894         continue;
2895       }
2896
2897       if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
2898         min_ts = tmp->abidata.ABI.ts;
2899         oframe = tmp;
2900       }
2901     }
2902     /* save a ts if needed */
2903     if (oframe && oframe != frame) {
2904       oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
2905     }
2906
2907     /* and set if needed;
2908      * valid delta means we have reasonable DTS input */
2909     /* also, if we ended up reordered, means this approach is conflicting
2910      * with some sparse existing PTS, and so it does not work out */
2911     if (!priv->reordered_output &&
2912         !GST_CLOCK_TIME_IS_VALID (frame->pts) && !seen_none &&
2913         GST_CLOCK_TIME_IS_VALID (priv->pts_delta)) {
2914       frame->pts = min_ts + priv->pts_delta;
2915       GST_DEBUG_OBJECT (decoder,
2916           "no valid PTS, using oldest DTS %" GST_TIME_FORMAT,
2917           GST_TIME_ARGS (frame->pts));
2918     }
2919
2920     /* some more maintenance, ts2 holds PTS */
2921     min_ts = GST_CLOCK_TIME_NONE;
2922     seen_none = FALSE;
2923     for (l = priv->frames.head; l; l = l->next) {
2924       GstVideoCodecFrame *tmp = l->data;
2925
2926       if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts2)) {
2927         seen_none = TRUE;
2928         continue;
2929       }
2930
2931       if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts2 < min_ts) {
2932         min_ts = tmp->abidata.ABI.ts2;
2933         oframe = tmp;
2934       }
2935     }
2936     /* save a ts if needed */
2937     if (oframe && oframe != frame) {
2938       oframe->abidata.ABI.ts2 = frame->abidata.ABI.ts2;
2939     }
2940
2941     /* if we detected reordered output, then PTS are void,
2942      * however those were obtained; bogus input, subclass etc */
2943     if (priv->reordered_output && !seen_none) {
2944       GST_DEBUG_OBJECT (decoder, "invalidating PTS");
2945       frame->pts = GST_CLOCK_TIME_NONE;
2946     }
2947
2948     if (!GST_CLOCK_TIME_IS_VALID (frame->pts) && !seen_none) {
2949       frame->pts = min_ts;
2950       GST_DEBUG_OBJECT (decoder,
2951           "no valid PTS, using oldest PTS %" GST_TIME_FORMAT,
2952           GST_TIME_ARGS (frame->pts));
2953     }
2954   }
2955
2956
2957   if (frame->pts == GST_CLOCK_TIME_NONE) {
2958     /* Last ditch timestamp guess: Just add the duration to the previous
2959      * frame. If it's the first frame, just use the segment start. */
2960     if (frame->duration != GST_CLOCK_TIME_NONE) {
2961       if (GST_CLOCK_TIME_IS_VALID (priv->last_timestamp_out))
2962         frame->pts = priv->last_timestamp_out + frame->duration;
2963       else if (decoder->output_segment.rate > 0.0)
2964         frame->pts = decoder->output_segment.start;
2965       GST_LOG_OBJECT (decoder,
2966           "Guessing timestamp %" GST_TIME_FORMAT " for frame...",
2967           GST_TIME_ARGS (frame->pts));
2968     } else if (sync && frame->dts != GST_CLOCK_TIME_NONE) {
2969       frame->pts = frame->dts;
2970       GST_LOG_OBJECT (decoder,
2971           "Setting DTS as PTS %" GST_TIME_FORMAT " for frame...",
2972           GST_TIME_ARGS (frame->pts));
2973     }
2974   }
2975
2976   if (GST_CLOCK_TIME_IS_VALID (priv->last_timestamp_out)) {
2977     if (frame->pts < priv->last_timestamp_out) {
2978       GST_WARNING_OBJECT (decoder,
2979           "decreasing timestamp (%" GST_TIME_FORMAT " < %"
2980           GST_TIME_FORMAT ")",
2981           GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (priv->last_timestamp_out));
2982       priv->reordered_output = TRUE;
2983       /* make it a bit less weird downstream */
2984       frame->pts = priv->last_timestamp_out;
2985     }
2986   }
2987
2988   if (GST_CLOCK_TIME_IS_VALID (frame->pts))
2989     priv->last_timestamp_out = frame->pts;
2990
2991   return;
2992
2993   /* ERRORS */
2994 no_output_buffer:
2995   {
2996     GST_ERROR_OBJECT (decoder, "No buffer to output !");
2997   }
2998 }
2999
3000 /**
3001  * gst_video_decoder_release_frame:
3002  * @dec: a #GstVideoDecoder
3003  * @frame: (transfer full): the #GstVideoCodecFrame to release
3004  *
3005  * Similar to gst_video_decoder_drop_frame(), but simply releases @frame
3006  * without any processing other than removing it from list of pending frames,
3007  * after which it is considered finished and released.
3008  *
3009  * Since: 1.2.2
3010  */
3011 void
3012 gst_video_decoder_release_frame (GstVideoDecoder * dec,
3013     GstVideoCodecFrame * frame)
3014 {
3015   GList *link;
3016
3017   /* unref once from the list */
3018   GST_VIDEO_DECODER_STREAM_LOCK (dec);
3019   link = g_queue_find (&dec->priv->frames, frame);
3020   if (link) {
3021     gst_video_codec_frame_unref (frame);
3022     g_queue_delete_link (&dec->priv->frames, link);
3023   }
3024   if (frame->events) {
3025     dec->priv->pending_events =
3026         g_list_concat (frame->events, dec->priv->pending_events);
3027     frame->events = NULL;
3028   }
3029   GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
3030
3031   /* unref because this function takes ownership */
3032   gst_video_codec_frame_unref (frame);
3033 }
3034
3035 /* called with STREAM_LOCK */
3036 static void
3037 gst_video_decoder_post_qos_drop (GstVideoDecoder * dec, GstClockTime timestamp)
3038 {
3039   GstClockTime stream_time, jitter, earliest_time, qostime;
3040   GstSegment *segment;
3041   GstMessage *qos_msg;
3042   gdouble proportion;
3043   dec->priv->dropped++;
3044
3045   /* post QoS message */
3046   GST_OBJECT_LOCK (dec);
3047   proportion = dec->priv->proportion;
3048   earliest_time = dec->priv->earliest_time;
3049   GST_OBJECT_UNLOCK (dec);
3050
3051   segment = &dec->output_segment;
3052   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
3053     segment = &dec->input_segment;
3054   stream_time =
3055       gst_segment_to_stream_time (segment, GST_FORMAT_TIME, timestamp);
3056   qostime = gst_segment_to_running_time (segment, GST_FORMAT_TIME, timestamp);
3057   jitter = GST_CLOCK_DIFF (qostime, earliest_time);
3058   qos_msg =
3059       gst_message_new_qos (GST_OBJECT_CAST (dec), FALSE, qostime, stream_time,
3060       timestamp, GST_CLOCK_TIME_NONE);
3061   gst_message_set_qos_values (qos_msg, jitter, proportion, 1000000);
3062   gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
3063       dec->priv->processed, dec->priv->dropped);
3064   gst_element_post_message (GST_ELEMENT_CAST (dec), qos_msg);
3065 }
3066
3067 /**
3068  * gst_video_decoder_drop_frame:
3069  * @dec: a #GstVideoDecoder
3070  * @frame: (transfer full): the #GstVideoCodecFrame to drop
3071  *
3072  * Similar to gst_video_decoder_finish_frame(), but drops @frame in any
3073  * case and posts a QoS message with the frame's details on the bus.
3074  * In any case, the frame is considered finished and released.
3075  *
3076  * Returns: a #GstFlowReturn, usually GST_FLOW_OK.
3077  */
3078 GstFlowReturn
3079 gst_video_decoder_drop_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame)
3080 {
3081   GST_LOG_OBJECT (dec, "drop frame %p", frame);
3082
3083   GST_VIDEO_DECODER_STREAM_LOCK (dec);
3084
3085   gst_video_decoder_prepare_finish_frame (dec, frame, TRUE);
3086
3087   GST_DEBUG_OBJECT (dec, "dropping frame %" GST_TIME_FORMAT,
3088       GST_TIME_ARGS (frame->pts));
3089
3090   gst_video_decoder_post_qos_drop (dec, frame->pts);
3091
3092   /* now free the frame */
3093   gst_video_decoder_release_frame (dec, frame);
3094
3095   GST_VIDEO_DECODER_STREAM_UNLOCK (dec);
3096
3097   return GST_FLOW_OK;
3098 }
3099
3100 static gboolean
3101 gst_video_decoder_transform_meta_default (GstVideoDecoder *
3102     decoder, GstVideoCodecFrame * frame, GstMeta * meta)
3103 {
3104   const GstMetaInfo *info = meta->info;
3105   const gchar *const *tags;
3106   const gchar *const supported_tags[] = {
3107     GST_META_TAG_VIDEO_STR,
3108     GST_META_TAG_VIDEO_ORIENTATION_STR,
3109     GST_META_TAG_VIDEO_SIZE_STR,
3110     NULL,
3111   };
3112
3113   tags = gst_meta_api_type_get_tags (info->api);
3114
3115   if (!tags)
3116     return TRUE;
3117
3118   while (*tags) {
3119     if (!g_strv_contains (supported_tags, *tags))
3120       return FALSE;
3121     tags++;
3122   }
3123
3124   return TRUE;
3125 }
3126
3127 typedef struct
3128 {
3129   GstVideoDecoder *decoder;
3130   GstVideoCodecFrame *frame;
3131 } CopyMetaData;
3132
3133 static gboolean
3134 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
3135 {
3136   CopyMetaData *data = user_data;
3137   GstVideoDecoder *decoder = data->decoder;
3138   GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
3139   GstVideoCodecFrame *frame = data->frame;
3140   const GstMetaInfo *info = (*meta)->info;
3141   gboolean do_copy = FALSE;
3142
3143   if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
3144     /* never call the transform_meta with memory specific metadata */
3145     GST_DEBUG_OBJECT (decoder, "not copying memory specific metadata %s",
3146         g_type_name (info->api));
3147     do_copy = FALSE;
3148   } else if (klass->transform_meta) {
3149     do_copy = klass->transform_meta (decoder, frame, *meta);
3150     GST_DEBUG_OBJECT (decoder, "transformed metadata %s: copy: %d",
3151         g_type_name (info->api), do_copy);
3152   }
3153
3154   /* we only copy metadata when the subclass implemented a transform_meta
3155    * function and when it returns %TRUE */
3156   if (do_copy && info->transform_func) {
3157     GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
3158     GST_DEBUG_OBJECT (decoder, "copy metadata %s", g_type_name (info->api));
3159     /* simply copy then */
3160     info->transform_func (frame->output_buffer, *meta, inbuf,
3161         _gst_meta_transform_copy, &copy_data);
3162   }
3163   return TRUE;
3164 }
3165
3166 /**
3167  * gst_video_decoder_finish_frame:
3168  * @decoder: a #GstVideoDecoder
3169  * @frame: (transfer full): a decoded #GstVideoCodecFrame
3170  *
3171  * @frame should have a valid decoded data buffer, whose metadata fields
3172  * are then appropriately set according to frame data and pushed downstream.
3173  * If no output data is provided, @frame is considered skipped.
3174  * In any case, the frame is considered finished and released.
3175  *
3176  * After calling this function the output buffer of the frame is to be
3177  * considered read-only. This function will also change the metadata
3178  * of the buffer.
3179  *
3180  * Returns: a #GstFlowReturn resulting from sending data downstream
3181  */
3182 GstFlowReturn
3183 gst_video_decoder_finish_frame (GstVideoDecoder * decoder,
3184     GstVideoCodecFrame * frame)
3185 {
3186   GstFlowReturn ret = GST_FLOW_OK;
3187   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
3188   GstVideoDecoderPrivate *priv = decoder->priv;
3189   GstBuffer *output_buffer;
3190   gboolean needs_reconfigure = FALSE;
3191
3192   GST_LOG_OBJECT (decoder, "finish frame %p", frame);
3193
3194   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3195
3196   needs_reconfigure = gst_pad_check_reconfigure (decoder->srcpad);
3197   if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
3198               && needs_reconfigure))) {
3199     if (!gst_video_decoder_negotiate_unlocked (decoder)) {
3200       gst_pad_mark_reconfigure (decoder->srcpad);
3201       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
3202         ret = GST_FLOW_FLUSHING;
3203       else
3204         ret = GST_FLOW_NOT_NEGOTIATED;
3205       goto done;
3206     }
3207   }
3208
3209   gst_video_decoder_prepare_finish_frame (decoder, frame, FALSE);
3210   priv->processed++;
3211
3212   if (priv->tags_changed) {
3213     GstEvent *tags_event;
3214
3215     tags_event = gst_video_decoder_create_merged_tags_event (decoder);
3216
3217     if (tags_event != NULL)
3218       gst_video_decoder_push_event (decoder, tags_event);
3219
3220     priv->tags_changed = FALSE;
3221   }
3222
3223   /* no buffer data means this frame is skipped */
3224   if (!frame->output_buffer || GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (frame)) {
3225     GST_DEBUG_OBJECT (decoder,
3226         "skipping frame %" GST_TIME_FORMAT " because not output was produced",
3227         GST_TIME_ARGS (frame->pts));
3228     goto done;
3229   }
3230
3231   /* Mark output as corrupted if the subclass requested so and we're either
3232    * still before the sync point after the request, or we don't even know the
3233    * frame number of the sync point yet (it is 0) */
3234   GST_OBJECT_LOCK (decoder);
3235   if (frame->system_frame_number <= priv->request_sync_point_frame_number
3236       && priv->request_sync_point_frame_number != REQUEST_SYNC_POINT_UNSET) {
3237     if (priv->request_sync_point_flags &
3238         GST_VIDEO_DECODER_REQUEST_SYNC_POINT_CORRUPT_OUTPUT) {
3239       GST_DEBUG_OBJECT (decoder,
3240           "marking frame %" GST_TIME_FORMAT
3241           " as corrupted because it is still before the sync point",
3242           GST_TIME_ARGS (frame->pts));
3243       GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
3244           GST_VIDEO_CODEC_FRAME_FLAG_CORRUPTED);
3245     }
3246   } else {
3247     /* Reset to -1 to mark it as unset now that we've reached the frame */
3248     priv->request_sync_point_frame_number = REQUEST_SYNC_POINT_UNSET;
3249   }
3250   GST_OBJECT_UNLOCK (decoder);
3251
3252   if (priv->discard_corrupted_frames
3253       && (GST_VIDEO_CODEC_FRAME_FLAG_IS_SET (frame,
3254               GST_VIDEO_CODEC_FRAME_FLAG_CORRUPTED)
3255           || GST_BUFFER_FLAG_IS_SET (frame->output_buffer,
3256               GST_BUFFER_FLAG_CORRUPTED))) {
3257     GST_DEBUG_OBJECT (decoder,
3258         "skipping frame %" GST_TIME_FORMAT " because it is corrupted",
3259         GST_TIME_ARGS (frame->pts));
3260     goto done;
3261   }
3262
3263   /* We need a writable buffer for the metadata changes below */
3264   output_buffer = frame->output_buffer =
3265       gst_buffer_make_writable (frame->output_buffer);
3266
3267   GST_BUFFER_FLAG_UNSET (output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
3268
3269   GST_BUFFER_PTS (output_buffer) = frame->pts;
3270   GST_BUFFER_DTS (output_buffer) = GST_CLOCK_TIME_NONE;
3271   GST_BUFFER_DURATION (output_buffer) = frame->duration;
3272
3273   GST_BUFFER_OFFSET (output_buffer) = GST_BUFFER_OFFSET_NONE;
3274   GST_BUFFER_OFFSET_END (output_buffer) = GST_BUFFER_OFFSET_NONE;
3275
3276   if (priv->discont) {
3277     GST_BUFFER_FLAG_SET (output_buffer, GST_BUFFER_FLAG_DISCONT);
3278   }
3279
3280   if (GST_VIDEO_CODEC_FRAME_FLAG_IS_SET (frame,
3281           GST_VIDEO_CODEC_FRAME_FLAG_CORRUPTED)) {
3282     GST_DEBUG_OBJECT (decoder,
3283         "marking frame %" GST_TIME_FORMAT " as corrupted",
3284         GST_TIME_ARGS (frame->pts));
3285     GST_BUFFER_FLAG_SET (output_buffer, GST_BUFFER_FLAG_CORRUPTED);
3286   }
3287
3288   if (decoder_class->transform_meta) {
3289     if (G_LIKELY (frame->input_buffer)) {
3290       CopyMetaData data;
3291
3292       data.decoder = decoder;
3293       data.frame = frame;
3294       gst_buffer_foreach_meta (frame->input_buffer, foreach_metadata, &data);
3295     } else {
3296       GST_WARNING_OBJECT (decoder,
3297           "Can't copy metadata because input frame disappeared");
3298     }
3299   }
3300
3301   /* Get an additional ref to the buffer, which is going to be pushed
3302    * downstream, the original ref is owned by the frame
3303    */
3304   output_buffer = gst_buffer_ref (output_buffer);
3305
3306   /* Release frame so the buffer is writable when we push it downstream
3307    * if possible, i.e. if the subclass does not hold additional references
3308    * to the frame
3309    */
3310   gst_video_decoder_release_frame (decoder, frame);
3311   frame = NULL;
3312
3313   if (decoder->output_segment.rate < 0.0
3314       && !(decoder->output_segment.flags & GST_SEEK_FLAG_TRICKMODE_KEY_UNITS)) {
3315     GST_LOG_OBJECT (decoder, "queued frame");
3316     priv->output_queued = g_list_prepend (priv->output_queued, output_buffer);
3317   } else {
3318     ret = gst_video_decoder_clip_and_push_buf (decoder, output_buffer);
3319   }
3320
3321 done:
3322   if (frame)
3323     gst_video_decoder_release_frame (decoder, frame);
3324   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3325   return ret;
3326 }
3327
3328 /* With stream lock, takes the frame reference */
3329 static GstFlowReturn
3330 gst_video_decoder_clip_and_push_buf (GstVideoDecoder * decoder, GstBuffer * buf)
3331 {
3332   GstFlowReturn ret = GST_FLOW_OK;
3333   GstVideoDecoderPrivate *priv = decoder->priv;
3334   guint64 start, stop;
3335   guint64 cstart, cstop;
3336   GstSegment *segment;
3337   GstClockTime duration;
3338
3339   /* Check for clipping */
3340   start = GST_BUFFER_PTS (buf);
3341   duration = GST_BUFFER_DURATION (buf);
3342
3343   /* store that we have valid decoded data */
3344   priv->had_output_data = TRUE;
3345
3346   stop = GST_CLOCK_TIME_NONE;
3347
3348   if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (duration)) {
3349     stop = start + duration;
3350   } else if (GST_CLOCK_TIME_IS_VALID (start)
3351       && !GST_CLOCK_TIME_IS_VALID (duration)) {
3352     /* If we don't clip away buffers that far before the segment we
3353      * can cause the pipeline to lockup. This can happen if audio is
3354      * properly clipped, and thus the audio sink does not preroll yet
3355      * but the video sink prerolls because we already outputted a
3356      * buffer here... and then queues run full.
3357      *
3358      * In the worst case we will clip one buffer too many here now if no
3359      * framerate is given, no buffer duration is given and the actual
3360      * framerate is lower than 25fps */
3361     stop = start + 40 * GST_MSECOND;
3362   }
3363
3364   segment = &decoder->output_segment;
3365   if (gst_segment_clip (segment, GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
3366     GST_BUFFER_PTS (buf) = cstart;
3367
3368     if (stop != GST_CLOCK_TIME_NONE && GST_CLOCK_TIME_IS_VALID (duration))
3369       GST_BUFFER_DURATION (buf) = cstop - cstart;
3370
3371     GST_LOG_OBJECT (decoder,
3372         "accepting buffer inside segment: %" GST_TIME_FORMAT " %"
3373         GST_TIME_FORMAT " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
3374         " time %" GST_TIME_FORMAT,
3375         GST_TIME_ARGS (cstart),
3376         GST_TIME_ARGS (cstop),
3377         GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
3378         GST_TIME_ARGS (segment->time));
3379   } else {
3380     GST_LOG_OBJECT (decoder,
3381         "dropping buffer outside segment: %" GST_TIME_FORMAT
3382         " %" GST_TIME_FORMAT
3383         " seg %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT
3384         " time %" GST_TIME_FORMAT,
3385         GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
3386         GST_TIME_ARGS (segment->start),
3387         GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time));
3388     /* only check and return EOS if upstream still
3389      * in the same segment and interested as such */
3390     if (decoder->priv->in_out_segment_sync) {
3391       if (segment->rate >= 0) {
3392         if (GST_BUFFER_PTS (buf) >= segment->stop)
3393           ret = GST_FLOW_EOS;
3394       } else if (GST_BUFFER_PTS (buf) < segment->start) {
3395         ret = GST_FLOW_EOS;
3396       }
3397     }
3398     gst_buffer_unref (buf);
3399     goto done;
3400   }
3401
3402   /* Is buffer too late (QoS) ? */
3403   if (priv->do_qos && GST_CLOCK_TIME_IS_VALID (priv->earliest_time)
3404       && GST_CLOCK_TIME_IS_VALID (cstart)) {
3405     GstClockTime deadline =
3406         gst_segment_to_running_time (segment, GST_FORMAT_TIME, cstart);
3407     if (GST_CLOCK_TIME_IS_VALID (deadline) && deadline < priv->earliest_time) {
3408       GST_WARNING_OBJECT (decoder,
3409           "Dropping frame due to QoS. start:%" GST_TIME_FORMAT " deadline:%"
3410           GST_TIME_FORMAT " earliest_time:%" GST_TIME_FORMAT,
3411           GST_TIME_ARGS (start), GST_TIME_ARGS (deadline),
3412           GST_TIME_ARGS (priv->earliest_time));
3413       gst_video_decoder_post_qos_drop (decoder, cstart);
3414       gst_buffer_unref (buf);
3415       priv->discont = TRUE;
3416       goto done;
3417     }
3418   }
3419
3420   /* Set DISCONT flag here ! */
3421
3422   if (priv->discont) {
3423     GST_DEBUG_OBJECT (decoder, "Setting discont on output buffer");
3424     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3425     priv->discont = FALSE;
3426   }
3427
3428   /* update rate estimate */
3429   GST_OBJECT_LOCK (decoder);
3430   priv->bytes_out += gst_buffer_get_size (buf);
3431   if (GST_CLOCK_TIME_IS_VALID (duration)) {
3432     priv->time += duration;
3433   } else {
3434     /* FIXME : Use difference between current and previous outgoing
3435      * timestamp, and relate to difference between current and previous
3436      * bytes */
3437     /* better none than nothing valid */
3438     priv->time = GST_CLOCK_TIME_NONE;
3439   }
3440   GST_OBJECT_UNLOCK (decoder);
3441
3442   GST_DEBUG_OBJECT (decoder, "pushing buffer %p of size %" G_GSIZE_FORMAT ", "
3443       "PTS %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT, buf,
3444       gst_buffer_get_size (buf),
3445       GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
3446       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
3447
3448   /* we got data, so note things are looking up again, reduce
3449    * the error count, if there is one */
3450   if (G_UNLIKELY (priv->error_count))
3451     priv->error_count = 0;
3452
3453 #ifndef GST_DISABLE_DEBUG
3454   if (G_UNLIKELY (priv->last_reset_time != GST_CLOCK_TIME_NONE)) {
3455     GstClockTime elapsed = gst_util_get_timestamp () - priv->last_reset_time;
3456
3457     /* First buffer since reset, report how long we took */
3458     GST_INFO_OBJECT (decoder, "First buffer since flush took %" GST_TIME_FORMAT
3459         " to produce", GST_TIME_ARGS (elapsed));
3460     priv->last_reset_time = GST_CLOCK_TIME_NONE;
3461   }
3462 #endif
3463
3464   /* release STREAM_LOCK not to block upstream 
3465    * while pushing buffer downstream */
3466   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3467   ret = gst_pad_push (decoder->srcpad, buf);
3468   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3469
3470 done:
3471   return ret;
3472 }
3473
3474 /**
3475  * gst_video_decoder_add_to_frame:
3476  * @decoder: a #GstVideoDecoder
3477  * @n_bytes: the number of bytes to add
3478  *
3479  * Removes next @n_bytes of input data and adds it to currently parsed frame.
3480  */
3481 void
3482 gst_video_decoder_add_to_frame (GstVideoDecoder * decoder, int n_bytes)
3483 {
3484   GstVideoDecoderPrivate *priv = decoder->priv;
3485   GstBuffer *buf;
3486
3487   GST_LOG_OBJECT (decoder, "add %d bytes to frame", n_bytes);
3488
3489   if (n_bytes == 0)
3490     return;
3491
3492   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3493   if (gst_adapter_available (priv->output_adapter) == 0) {
3494     priv->frame_offset =
3495         priv->input_offset - gst_adapter_available (priv->input_adapter);
3496   }
3497   buf = gst_adapter_take_buffer (priv->input_adapter, n_bytes);
3498
3499   gst_adapter_push (priv->output_adapter, buf);
3500   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3501 }
3502
3503 /**
3504  * gst_video_decoder_get_pending_frame_size:
3505  * @decoder: a #GstVideoDecoder
3506  *
3507  * Returns the number of bytes previously added to the current frame
3508  * by calling gst_video_decoder_add_to_frame().
3509  *
3510  * Returns: The number of bytes pending for the current frame
3511  *
3512  * Since: 1.4
3513  */
3514 gsize
3515 gst_video_decoder_get_pending_frame_size (GstVideoDecoder * decoder)
3516 {
3517   GstVideoDecoderPrivate *priv = decoder->priv;
3518   gsize ret;
3519
3520   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3521   ret = gst_adapter_available (priv->output_adapter);
3522   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3523
3524   GST_LOG_OBJECT (decoder, "Current pending frame has %" G_GSIZE_FORMAT "bytes",
3525       ret);
3526
3527   return ret;
3528 }
3529
3530 static guint64
3531 gst_video_decoder_get_frame_duration (GstVideoDecoder * decoder,
3532     GstVideoCodecFrame * frame)
3533 {
3534   GstVideoCodecState *state = decoder->priv->output_state;
3535
3536   /* it's possible that we don't have a state yet when we are dropping the
3537    * initial buffers */
3538   if (state == NULL)
3539     return GST_CLOCK_TIME_NONE;
3540
3541   if (state->info.fps_d == 0 || state->info.fps_n == 0) {
3542     return GST_CLOCK_TIME_NONE;
3543   }
3544
3545   /* FIXME: For interlaced frames this needs to take into account
3546    * the number of valid fields in the frame
3547    */
3548
3549   return gst_util_uint64_scale (GST_SECOND, state->info.fps_d,
3550       state->info.fps_n);
3551 }
3552
3553 /**
3554  * gst_video_decoder_have_frame:
3555  * @decoder: a #GstVideoDecoder
3556  *
3557  * Gathers all data collected for currently parsed frame, gathers corresponding
3558  * metadata and passes it along for further processing, i.e. @handle_frame.
3559  *
3560  * Returns: a #GstFlowReturn
3561  */
3562 GstFlowReturn
3563 gst_video_decoder_have_frame (GstVideoDecoder * decoder)
3564 {
3565   GstVideoDecoderPrivate *priv = decoder->priv;
3566   GstBuffer *buffer;
3567   int n_available;
3568   GstClockTime pts, dts, duration;
3569   guint flags;
3570   GstFlowReturn ret = GST_FLOW_OK;
3571
3572   GST_LOG_OBJECT (decoder, "have_frame at offset %" G_GUINT64_FORMAT,
3573       priv->frame_offset);
3574
3575   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3576
3577   n_available = gst_adapter_available (priv->output_adapter);
3578   if (n_available) {
3579     buffer = gst_adapter_take_buffer (priv->output_adapter, n_available);
3580   } else {
3581     buffer = gst_buffer_new_and_alloc (0);
3582   }
3583
3584   priv->current_frame->input_buffer = buffer;
3585
3586   gst_video_decoder_get_buffer_info_at_offset (decoder,
3587       priv->frame_offset, &pts, &dts, &duration, &flags);
3588
3589   GST_BUFFER_PTS (buffer) = pts;
3590   GST_BUFFER_DTS (buffer) = dts;
3591   GST_BUFFER_DURATION (buffer) = duration;
3592   GST_BUFFER_FLAGS (buffer) = flags;
3593
3594   GST_LOG_OBJECT (decoder, "collected frame size %d, "
3595       "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT ", dur %"
3596       GST_TIME_FORMAT, n_available, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
3597       GST_TIME_ARGS (duration));
3598
3599   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
3600     GST_DEBUG_OBJECT (decoder, "Marking as sync point");
3601     GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (priv->current_frame);
3602   }
3603
3604   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_CORRUPTED)) {
3605     GST_DEBUG_OBJECT (decoder, "Marking as corrupted");
3606     GST_VIDEO_CODEC_FRAME_FLAG_SET (priv->current_frame,
3607         GST_VIDEO_CODEC_FRAME_FLAG_CORRUPTED);
3608   }
3609
3610   /* In reverse playback, just capture and queue frames for later processing */
3611   if (decoder->input_segment.rate < 0.0) {
3612     priv->parse_gather =
3613         g_list_prepend (priv->parse_gather, priv->current_frame);
3614   } else {
3615     /* Otherwise, decode the frame, which gives away our ref */
3616     ret = gst_video_decoder_decode_frame (decoder, priv->current_frame);
3617   }
3618   /* Current frame is gone now, either way */
3619   priv->current_frame = NULL;
3620
3621   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3622
3623   return ret;
3624 }
3625
3626 /* Pass the frame in priv->current_frame through the
3627  * handle_frame() callback for decoding and passing to gvd_finish_frame(),
3628  * or dropping by passing to gvd_drop_frame() */
3629 static GstFlowReturn
3630 gst_video_decoder_decode_frame (GstVideoDecoder * decoder,
3631     GstVideoCodecFrame * frame)
3632 {
3633   GstVideoDecoderPrivate *priv = decoder->priv;
3634   GstVideoDecoderClass *decoder_class;
3635   GstFlowReturn ret = GST_FLOW_OK;
3636
3637   decoder_class = GST_VIDEO_DECODER_GET_CLASS (decoder);
3638
3639   /* FIXME : This should only have to be checked once (either the subclass has an
3640    * implementation, or it doesn't) */
3641   g_return_val_if_fail (decoder_class->handle_frame != NULL, GST_FLOW_ERROR);
3642
3643   frame->pts = GST_BUFFER_PTS (frame->input_buffer);
3644   frame->dts = GST_BUFFER_DTS (frame->input_buffer);
3645   frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
3646
3647   /* For keyframes, PTS = DTS + constant_offset, usually 0 to 3 frame
3648    * durations. */
3649   /* FIXME upstream can be quite wrong about the keyframe aspect,
3650    * so we could be going off here as well,
3651    * maybe let subclass decide if it really is/was a keyframe */
3652   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
3653     priv->distance_from_sync = 0;
3654
3655     GST_OBJECT_LOCK (decoder);
3656     priv->request_sync_point_flags &=
3657         ~GST_VIDEO_DECODER_REQUEST_SYNC_POINT_DISCARD_INPUT;
3658     if (priv->request_sync_point_frame_number == REQUEST_SYNC_POINT_PENDING)
3659       priv->request_sync_point_frame_number = frame->system_frame_number;
3660     GST_OBJECT_UNLOCK (decoder);
3661
3662     if (GST_CLOCK_TIME_IS_VALID (frame->pts)
3663         && GST_CLOCK_TIME_IS_VALID (frame->dts)) {
3664       /* just in case they are not equal as might ideally be,
3665        * e.g. quicktime has a (positive) delta approach */
3666       priv->pts_delta = frame->pts - frame->dts;
3667       GST_DEBUG_OBJECT (decoder, "PTS delta %d ms",
3668           (gint) (priv->pts_delta / GST_MSECOND));
3669     }
3670   } else {
3671     GST_OBJECT_LOCK (decoder);
3672     if ((priv->needs_sync_point && priv->distance_from_sync == -1)
3673         || (priv->request_sync_point_flags &
3674             GST_VIDEO_DECODER_REQUEST_SYNC_POINT_DISCARD_INPUT)) {
3675       GST_WARNING_OBJECT (decoder,
3676           "Subclass requires a sync point but we didn't receive one yet, discarding input");
3677       GST_OBJECT_UNLOCK (decoder);
3678       gst_video_decoder_release_frame (decoder, frame);
3679       return GST_FLOW_OK;
3680     }
3681     GST_OBJECT_UNLOCK (decoder);
3682
3683     priv->distance_from_sync++;
3684   }
3685
3686   frame->distance_from_sync = priv->distance_from_sync;
3687
3688   frame->abidata.ABI.ts = frame->dts;
3689   frame->abidata.ABI.ts2 = frame->pts;
3690
3691   GST_LOG_OBJECT (decoder, "PTS %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
3692       ", dist %d", GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->dts),
3693       frame->distance_from_sync);
3694
3695   g_queue_push_tail (&priv->frames, gst_video_codec_frame_ref (frame));
3696
3697   if (priv->frames.length > 10) {
3698     GST_DEBUG_OBJECT (decoder, "decoder frame list getting long: %d frames,"
3699         "possible internal leaking?", priv->frames.length);
3700   }
3701
3702   frame->deadline =
3703       gst_segment_to_running_time (&decoder->input_segment, GST_FORMAT_TIME,
3704       frame->pts);
3705
3706   /* do something with frame */
3707   ret = decoder_class->handle_frame (decoder, frame);
3708   if (ret != GST_FLOW_OK)
3709     GST_DEBUG_OBJECT (decoder, "flow error %s", gst_flow_get_name (ret));
3710
3711   /* the frame has either been added to parse_gather or sent to
3712      handle frame so there is no need to unref it */
3713   return ret;
3714 }
3715
3716
3717 /**
3718  * gst_video_decoder_get_output_state:
3719  * @decoder: a #GstVideoDecoder
3720  *
3721  * Get the #GstVideoCodecState currently describing the output stream.
3722  *
3723  * Returns: (transfer full): #GstVideoCodecState describing format of video data.
3724  */
3725 GstVideoCodecState *
3726 gst_video_decoder_get_output_state (GstVideoDecoder * decoder)
3727 {
3728   GstVideoCodecState *state = NULL;
3729
3730   GST_OBJECT_LOCK (decoder);
3731   if (decoder->priv->output_state)
3732     state = gst_video_codec_state_ref (decoder->priv->output_state);
3733   GST_OBJECT_UNLOCK (decoder);
3734
3735   return state;
3736 }
3737
3738 static GstVideoCodecState *
3739 _set_interlaced_output_state (GstVideoDecoder * decoder,
3740     GstVideoFormat fmt, GstVideoInterlaceMode interlace_mode, guint width,
3741     guint height, GstVideoCodecState * reference, gboolean copy_interlace_mode)
3742 {
3743   GstVideoDecoderPrivate *priv = decoder->priv;
3744   GstVideoCodecState *state;
3745
3746   g_assert ((copy_interlace_mode
3747           && interlace_mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE)
3748       || !copy_interlace_mode);
3749
3750   GST_DEBUG_OBJECT (decoder,
3751       "fmt:%d, width:%d, height:%d, interlace-mode: %s, reference:%p", fmt,
3752       width, height, gst_video_interlace_mode_to_string (interlace_mode),
3753       reference);
3754
3755   /* Create the new output state */
3756   state =
3757       _new_output_state (fmt, interlace_mode, width, height, reference,
3758       copy_interlace_mode);
3759   if (!state)
3760     return NULL;
3761
3762   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3763
3764   GST_OBJECT_LOCK (decoder);
3765   /* Replace existing output state by new one */
3766   if (priv->output_state)
3767     gst_video_codec_state_unref (priv->output_state);
3768   priv->output_state = gst_video_codec_state_ref (state);
3769
3770   if (priv->output_state != NULL && priv->output_state->info.fps_n > 0) {
3771     priv->qos_frame_duration =
3772         gst_util_uint64_scale (GST_SECOND, priv->output_state->info.fps_d,
3773         priv->output_state->info.fps_n);
3774   } else {
3775     priv->qos_frame_duration = 0;
3776   }
3777   priv->output_state_changed = TRUE;
3778   GST_OBJECT_UNLOCK (decoder);
3779
3780   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3781
3782   return state;
3783 }
3784
3785 /**
3786  * gst_video_decoder_set_output_state:
3787  * @decoder: a #GstVideoDecoder
3788  * @fmt: a #GstVideoFormat
3789  * @width: The width in pixels
3790  * @height: The height in pixels
3791  * @reference: (allow-none) (transfer none): An optional reference #GstVideoCodecState
3792  *
3793  * Creates a new #GstVideoCodecState with the specified @fmt, @width and @height
3794  * as the output state for the decoder.
3795  * Any previously set output state on @decoder will be replaced by the newly
3796  * created one.
3797  *
3798  * If the subclass wishes to copy over existing fields (like pixel aspec ratio,
3799  * or framerate) from an existing #GstVideoCodecState, it can be provided as a
3800  * @reference.
3801  *
3802  * If the subclass wishes to override some fields from the output state (like
3803  * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
3804  *
3805  * The new output state will only take effect (set on pads and buffers) starting
3806  * from the next call to #gst_video_decoder_finish_frame().
3807  *
3808  * Returns: (transfer full): the newly configured output state.
3809  */
3810 GstVideoCodecState *
3811 gst_video_decoder_set_output_state (GstVideoDecoder * decoder,
3812     GstVideoFormat fmt, guint width, guint height,
3813     GstVideoCodecState * reference)
3814 {
3815   return _set_interlaced_output_state (decoder, fmt,
3816       GST_VIDEO_INTERLACE_MODE_PROGRESSIVE, width, height, reference, TRUE);
3817 }
3818
3819 /**
3820  * gst_video_decoder_set_interlaced_output_state:
3821  * @decoder: a #GstVideoDecoder
3822  * @fmt: a #GstVideoFormat
3823  * @width: The width in pixels
3824  * @height: The height in pixels
3825  * @interlace_mode: A #GstVideoInterlaceMode
3826  * @reference: (allow-none) (transfer none): An optional reference #GstVideoCodecState
3827  *
3828  * Same as #gst_video_decoder_set_output_state() but also allows you to also set
3829  * the interlacing mode.
3830  *
3831  * Returns: (transfer full): the newly configured output state.
3832  *
3833  * Since: 1.16.
3834  */
3835 GstVideoCodecState *
3836 gst_video_decoder_set_interlaced_output_state (GstVideoDecoder * decoder,
3837     GstVideoFormat fmt, GstVideoInterlaceMode interlace_mode, guint width,
3838     guint height, GstVideoCodecState * reference)
3839 {
3840   return _set_interlaced_output_state (decoder, fmt, interlace_mode, width,
3841       height, reference, FALSE);
3842 }
3843
3844
3845 /**
3846  * gst_video_decoder_get_oldest_frame:
3847  * @decoder: a #GstVideoDecoder
3848  *
3849  * Get the oldest pending unfinished #GstVideoCodecFrame
3850  *
3851  * Returns: (transfer full): oldest pending unfinished #GstVideoCodecFrame.
3852  */
3853 GstVideoCodecFrame *
3854 gst_video_decoder_get_oldest_frame (GstVideoDecoder * decoder)
3855 {
3856   GstVideoCodecFrame *frame = NULL;
3857
3858   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3859   if (decoder->priv->frames.head)
3860     frame = gst_video_codec_frame_ref (decoder->priv->frames.head->data);
3861   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3862
3863   return (GstVideoCodecFrame *) frame;
3864 }
3865
3866 /**
3867  * gst_video_decoder_get_frame:
3868  * @decoder: a #GstVideoDecoder
3869  * @frame_number: system_frame_number of a frame
3870  *
3871  * Get a pending unfinished #GstVideoCodecFrame
3872  *
3873  * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
3874  */
3875 GstVideoCodecFrame *
3876 gst_video_decoder_get_frame (GstVideoDecoder * decoder, int frame_number)
3877 {
3878   GList *g;
3879   GstVideoCodecFrame *frame = NULL;
3880
3881   GST_DEBUG_OBJECT (decoder, "frame_number : %d", frame_number);
3882
3883   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3884   for (g = decoder->priv->frames.head; g; g = g->next) {
3885     GstVideoCodecFrame *tmp = g->data;
3886
3887     if (tmp->system_frame_number == frame_number) {
3888       frame = gst_video_codec_frame_ref (tmp);
3889       break;
3890     }
3891   }
3892   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3893
3894   return frame;
3895 }
3896
3897 /**
3898  * gst_video_decoder_get_frames:
3899  * @decoder: a #GstVideoDecoder
3900  *
3901  * Get all pending unfinished #GstVideoCodecFrame
3902  *
3903  * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
3904  */
3905 GList *
3906 gst_video_decoder_get_frames (GstVideoDecoder * decoder)
3907 {
3908   GList *frames;
3909
3910   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
3911   frames =
3912       g_list_copy_deep (decoder->priv->frames.head,
3913       (GCopyFunc) gst_video_codec_frame_ref, NULL);
3914   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
3915
3916   return frames;
3917 }
3918
3919 static gboolean
3920 gst_video_decoder_decide_allocation_default (GstVideoDecoder * decoder,
3921     GstQuery * query)
3922 {
3923   GstCaps *outcaps = NULL;
3924   GstBufferPool *pool = NULL;
3925   guint size, min, max;
3926   GstAllocator *allocator = NULL;
3927   GstAllocationParams params;
3928   GstStructure *config;
3929   gboolean update_pool, update_allocator;
3930   GstVideoInfo vinfo;
3931
3932   gst_query_parse_allocation (query, &outcaps, NULL);
3933   gst_video_info_init (&vinfo);
3934   if (outcaps)
3935     gst_video_info_from_caps (&vinfo, outcaps);
3936
3937   /* we got configuration from our peer or the decide_allocation method,
3938    * parse them */
3939   if (gst_query_get_n_allocation_params (query) > 0) {
3940     /* try the allocator */
3941     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
3942     update_allocator = TRUE;
3943   } else {
3944     allocator = NULL;
3945     gst_allocation_params_init (&params);
3946     update_allocator = FALSE;
3947   }
3948
3949   if (gst_query_get_n_allocation_pools (query) > 0) {
3950     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3951     size = MAX (size, vinfo.size);
3952     update_pool = TRUE;
3953   } else {
3954     pool = NULL;
3955     size = vinfo.size;
3956     min = max = 0;
3957
3958     update_pool = FALSE;
3959   }
3960
3961   if (pool == NULL) {
3962     /* no pool, we can make our own */
3963     GST_DEBUG_OBJECT (decoder, "no pool, making new pool");
3964     pool = gst_video_buffer_pool_new ();
3965   }
3966
3967   /* now configure */
3968   config = gst_buffer_pool_get_config (pool);
3969   gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3970   gst_buffer_pool_config_set_allocator (config, allocator, &params);
3971
3972   GST_DEBUG_OBJECT (decoder,
3973       "setting config %" GST_PTR_FORMAT " in pool %" GST_PTR_FORMAT, config,
3974       pool);
3975   if (!gst_buffer_pool_set_config (pool, config)) {
3976     config = gst_buffer_pool_get_config (pool);
3977
3978     /* If change are not acceptable, fallback to generic pool */
3979     if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3980             max)) {
3981       GST_DEBUG_OBJECT (decoder, "unsupported pool, making new pool");
3982
3983       gst_object_unref (pool);
3984       pool = gst_video_buffer_pool_new ();
3985       gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3986       gst_buffer_pool_config_set_allocator (config, allocator, &params);
3987     }
3988
3989     if (!gst_buffer_pool_set_config (pool, config))
3990       goto config_failed;
3991   }
3992
3993   if (update_allocator)
3994     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
3995   else
3996     gst_query_add_allocation_param (query, allocator, &params);
3997   if (allocator)
3998     gst_object_unref (allocator);
3999
4000   if (update_pool)
4001     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
4002   else
4003     gst_query_add_allocation_pool (query, pool, size, min, max);
4004
4005   if (pool)
4006     gst_object_unref (pool);
4007
4008   return TRUE;
4009
4010 config_failed:
4011   if (allocator)
4012     gst_object_unref (allocator);
4013   if (pool)
4014     gst_object_unref (pool);
4015   GST_ELEMENT_ERROR (decoder, RESOURCE, SETTINGS,
4016       ("Failed to configure the buffer pool"),
4017       ("Configuration is most likely invalid, please report this issue."));
4018   return FALSE;
4019 }
4020
4021 static gboolean
4022 gst_video_decoder_propose_allocation_default (GstVideoDecoder * decoder,
4023     GstQuery * query)
4024 {
4025   return TRUE;
4026 }
4027
4028 static gboolean
4029 gst_video_decoder_negotiate_pool (GstVideoDecoder * decoder, GstCaps * caps)
4030 {
4031   GstVideoDecoderClass *klass;
4032   GstQuery *query = NULL;
4033   GstBufferPool *pool = NULL;
4034   GstAllocator *allocator;
4035   GstAllocationParams params;
4036   gboolean ret = TRUE;
4037
4038   klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
4039
4040   query = gst_query_new_allocation (caps, TRUE);
4041
4042   GST_DEBUG_OBJECT (decoder, "do query ALLOCATION");
4043
4044   if (!gst_pad_peer_query (decoder->srcpad, query)) {
4045     GST_DEBUG_OBJECT (decoder, "didn't get downstream ALLOCATION hints");
4046   }
4047
4048   g_assert (klass->decide_allocation != NULL);
4049   ret = klass->decide_allocation (decoder, query);
4050
4051   GST_DEBUG_OBJECT (decoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
4052       query);
4053
4054   if (!ret)
4055     goto no_decide_allocation;
4056
4057   /* we got configuration from our peer or the decide_allocation method,
4058    * parse them */
4059   if (gst_query_get_n_allocation_params (query) > 0) {
4060     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
4061   } else {
4062     allocator = NULL;
4063     gst_allocation_params_init (&params);
4064   }
4065
4066   if (gst_query_get_n_allocation_pools (query) > 0)
4067     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
4068   if (!pool) {
4069     if (allocator)
4070       gst_object_unref (allocator);
4071     ret = FALSE;
4072     goto no_decide_allocation;
4073   }
4074
4075   if (decoder->priv->allocator)
4076     gst_object_unref (decoder->priv->allocator);
4077   decoder->priv->allocator = allocator;
4078   decoder->priv->params = params;
4079
4080   if (decoder->priv->pool) {
4081     /* do not set the bufferpool to inactive here, it will be done
4082      * on its finalize function. As videodecoder do late renegotiation
4083      * it might happen that some element downstream is already using this
4084      * same bufferpool and deactivating it will make it fail.
4085      * Happens when a downstream element changes from passthrough to
4086      * non-passthrough and gets this same bufferpool to use */
4087     GST_DEBUG_OBJECT (decoder, "unref pool %" GST_PTR_FORMAT,
4088         decoder->priv->pool);
4089     gst_object_unref (decoder->priv->pool);
4090   }
4091   decoder->priv->pool = pool;
4092
4093   /* and activate */
4094   GST_DEBUG_OBJECT (decoder, "activate pool %" GST_PTR_FORMAT, pool);
4095   gst_buffer_pool_set_active (pool, TRUE);
4096
4097 done:
4098   if (query)
4099     gst_query_unref (query);
4100
4101   return ret;
4102
4103   /* Errors */
4104 no_decide_allocation:
4105   {
4106     GST_WARNING_OBJECT (decoder, "Subclass failed to decide allocation");
4107     goto done;
4108   }
4109 }
4110
4111 static gboolean
4112 gst_video_decoder_negotiate_default (GstVideoDecoder * decoder)
4113 {
4114   GstVideoCodecState *state = decoder->priv->output_state;
4115   gboolean ret = TRUE;
4116   GstVideoCodecFrame *frame;
4117   GstCaps *prevcaps;
4118   GstCaps *incaps;
4119
4120   if (!state) {
4121     GST_DEBUG_OBJECT (decoder,
4122         "Trying to negotiate the pool with out setting the o/p format");
4123     ret = gst_video_decoder_negotiate_pool (decoder, NULL);
4124     goto done;
4125   }
4126
4127   g_return_val_if_fail (GST_VIDEO_INFO_WIDTH (&state->info) != 0, FALSE);
4128   g_return_val_if_fail (GST_VIDEO_INFO_HEIGHT (&state->info) != 0, FALSE);
4129
4130   /* If the base class didn't set any multiview params, assume mono
4131    * now */
4132   if (GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) ==
4133       GST_VIDEO_MULTIVIEW_MODE_NONE) {
4134     GST_VIDEO_INFO_MULTIVIEW_MODE (&state->info) =
4135         GST_VIDEO_MULTIVIEW_MODE_MONO;
4136     GST_VIDEO_INFO_MULTIVIEW_FLAGS (&state->info) =
4137         GST_VIDEO_MULTIVIEW_FLAGS_NONE;
4138   }
4139
4140   GST_DEBUG_OBJECT (decoder, "output_state par %d/%d fps %d/%d",
4141       state->info.par_n, state->info.par_d,
4142       state->info.fps_n, state->info.fps_d);
4143
4144   if (state->caps == NULL)
4145     state->caps = gst_video_info_to_caps (&state->info);
4146
4147   incaps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SINK_PAD (decoder));
4148   if (incaps) {
4149     GstStructure *in_struct;
4150
4151     in_struct = gst_caps_get_structure (incaps, 0);
4152     if (gst_structure_has_field (in_struct, "mastering-display-info") ||
4153         gst_structure_has_field (in_struct, "content-light-level")) {
4154       const gchar *s;
4155
4156       /* prefer upstream information */
4157       state->caps = gst_caps_make_writable (state->caps);
4158       if ((s = gst_structure_get_string (in_struct, "mastering-display-info"))) {
4159         gst_caps_set_simple (state->caps,
4160             "mastering-display-info", G_TYPE_STRING, s, NULL);
4161       }
4162
4163       if ((s = gst_structure_get_string (in_struct, "content-light-level"))) {
4164         gst_caps_set_simple (state->caps,
4165             "content-light-level", G_TYPE_STRING, s, NULL);
4166       }
4167     }
4168
4169     gst_caps_unref (incaps);
4170   }
4171
4172   if (state->allocation_caps == NULL)
4173     state->allocation_caps = gst_caps_ref (state->caps);
4174
4175   GST_DEBUG_OBJECT (decoder, "setting caps %" GST_PTR_FORMAT, state->caps);
4176
4177   /* Push all pending pre-caps events of the oldest frame before
4178    * setting caps */
4179   frame = decoder->priv->frames.head ? decoder->priv->frames.head->data : NULL;
4180   if (frame || decoder->priv->current_frame_events) {
4181     GList **events, *l;
4182
4183     if (frame) {
4184       events = &frame->events;
4185     } else {
4186       events = &decoder->priv->current_frame_events;
4187     }
4188
4189     for (l = g_list_last (*events); l;) {
4190       GstEvent *event = GST_EVENT (l->data);
4191       GList *tmp;
4192
4193       if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
4194         gst_video_decoder_push_event (decoder, event);
4195         tmp = l;
4196         l = l->prev;
4197         *events = g_list_delete_link (*events, tmp);
4198       } else {
4199         l = l->prev;
4200       }
4201     }
4202   }
4203
4204   prevcaps = gst_pad_get_current_caps (decoder->srcpad);
4205   if (!prevcaps || !gst_caps_is_equal (prevcaps, state->caps)) {
4206     if (!prevcaps) {
4207       GST_DEBUG_OBJECT (decoder, "decoder src pad has currently NULL caps");
4208     }
4209     ret = gst_pad_set_caps (decoder->srcpad, state->caps);
4210   } else {
4211     ret = TRUE;
4212     GST_DEBUG_OBJECT (decoder,
4213         "current src pad and output state caps are the same");
4214   }
4215   if (prevcaps)
4216     gst_caps_unref (prevcaps);
4217
4218   if (!ret)
4219     goto done;
4220   decoder->priv->output_state_changed = FALSE;
4221   /* Negotiate pool */
4222   ret = gst_video_decoder_negotiate_pool (decoder, state->allocation_caps);
4223
4224 done:
4225   return ret;
4226 }
4227
4228 static gboolean
4229 gst_video_decoder_negotiate_unlocked (GstVideoDecoder * decoder)
4230 {
4231   GstVideoDecoderClass *klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
4232   gboolean ret = TRUE;
4233
4234   if (G_LIKELY (klass->negotiate))
4235     ret = klass->negotiate (decoder);
4236
4237   return ret;
4238 }
4239
4240 /**
4241  * gst_video_decoder_negotiate:
4242  * @decoder: a #GstVideoDecoder
4243  *
4244  * Negotiate with downstream elements to currently configured #GstVideoCodecState.
4245  * Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
4246  * negotiate fails.
4247  *
4248  * Returns: %TRUE if the negotiation succeeded, else %FALSE.
4249  */
4250 gboolean
4251 gst_video_decoder_negotiate (GstVideoDecoder * decoder)
4252 {
4253   GstVideoDecoderClass *klass;
4254   gboolean ret = TRUE;
4255
4256   g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), FALSE);
4257
4258   klass = GST_VIDEO_DECODER_GET_CLASS (decoder);
4259
4260   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
4261   gst_pad_check_reconfigure (decoder->srcpad);
4262   if (klass->negotiate) {
4263     ret = klass->negotiate (decoder);
4264     if (!ret)
4265       gst_pad_mark_reconfigure (decoder->srcpad);
4266   }
4267   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4268
4269   return ret;
4270 }
4271
4272 /**
4273  * gst_video_decoder_allocate_output_buffer:
4274  * @decoder: a #GstVideoDecoder
4275  *
4276  * Helper function that allocates a buffer to hold a video frame for @decoder's
4277  * current #GstVideoCodecState.
4278  *
4279  * You should use gst_video_decoder_allocate_output_frame() instead of this
4280  * function, if possible at all.
4281  *
4282  * Returns: (transfer full): allocated buffer, or NULL if no buffer could be
4283  *     allocated (e.g. when downstream is flushing or shutting down)
4284  */
4285 GstBuffer *
4286 gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder)
4287 {
4288   GstFlowReturn flow;
4289   GstBuffer *buffer = NULL;
4290   gboolean needs_reconfigure = FALSE;
4291
4292   GST_DEBUG ("alloc src buffer");
4293
4294   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
4295   needs_reconfigure = gst_pad_check_reconfigure (decoder->srcpad);
4296   if (G_UNLIKELY (!decoder->priv->output_state
4297           || decoder->priv->output_state_changed || needs_reconfigure)) {
4298     if (!gst_video_decoder_negotiate_unlocked (decoder)) {
4299       if (decoder->priv->output_state) {
4300         GST_DEBUG_OBJECT (decoder, "Failed to negotiate, fallback allocation");
4301         gst_pad_mark_reconfigure (decoder->srcpad);
4302         goto fallback;
4303       } else {
4304         GST_DEBUG_OBJECT (decoder, "Failed to negotiate, output_buffer=NULL");
4305         goto failed_allocation;
4306       }
4307     }
4308   }
4309
4310   flow = gst_buffer_pool_acquire_buffer (decoder->priv->pool, &buffer, NULL);
4311
4312   if (flow != GST_FLOW_OK) {
4313     GST_INFO_OBJECT (decoder, "couldn't allocate output buffer, flow %s",
4314         gst_flow_get_name (flow));
4315     if (decoder->priv->output_state && decoder->priv->output_state->info.size)
4316       goto fallback;
4317     else
4318       goto failed_allocation;
4319   }
4320   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4321
4322   return buffer;
4323
4324 fallback:
4325   GST_INFO_OBJECT (decoder,
4326       "Fallback allocation, creating new buffer which doesn't belongs to any buffer pool");
4327   buffer =
4328       gst_buffer_new_allocate (NULL, decoder->priv->output_state->info.size,
4329       NULL);
4330
4331 failed_allocation:
4332   GST_ERROR_OBJECT (decoder, "Failed to allocate the buffer..");
4333   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4334
4335   return buffer;
4336 }
4337
4338 /**
4339  * gst_video_decoder_allocate_output_frame:
4340  * @decoder: a #GstVideoDecoder
4341  * @frame: a #GstVideoCodecFrame
4342  *
4343  * Helper function that allocates a buffer to hold a video frame for @decoder's
4344  * current #GstVideoCodecState.  Subclass should already have configured video
4345  * state and set src pad caps.
4346  *
4347  * The buffer allocated here is owned by the frame and you should only
4348  * keep references to the frame, not the buffer.
4349  *
4350  * Returns: %GST_FLOW_OK if an output buffer could be allocated
4351  */
4352 GstFlowReturn
4353 gst_video_decoder_allocate_output_frame (GstVideoDecoder *
4354     decoder, GstVideoCodecFrame * frame)
4355 {
4356   return gst_video_decoder_allocate_output_frame_with_params (decoder, frame,
4357       NULL);
4358 }
4359
4360 /**
4361  * gst_video_decoder_allocate_output_frame_with_params:
4362  * @decoder: a #GstVideoDecoder
4363  * @frame: a #GstVideoCodecFrame
4364  * @params: a #GstBufferPoolAcquireParams
4365  *
4366  * Same as #gst_video_decoder_allocate_output_frame except it allows passing
4367  * #GstBufferPoolAcquireParams to the sub call gst_buffer_pool_acquire_buffer.
4368  *
4369  * Returns: %GST_FLOW_OK if an output buffer could be allocated
4370  *
4371  * Since: 1.12
4372  */
4373 GstFlowReturn
4374 gst_video_decoder_allocate_output_frame_with_params (GstVideoDecoder *
4375     decoder, GstVideoCodecFrame * frame, GstBufferPoolAcquireParams * params)
4376 {
4377   GstFlowReturn flow_ret;
4378   GstVideoCodecState *state;
4379   int num_bytes;
4380   gboolean needs_reconfigure = FALSE;
4381
4382   g_return_val_if_fail (decoder->priv->output_state, GST_FLOW_NOT_NEGOTIATED);
4383   g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
4384
4385   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
4386
4387   state = decoder->priv->output_state;
4388   if (state == NULL) {
4389     g_warning ("Output state should be set before allocating frame");
4390     goto error;
4391   }
4392   num_bytes = GST_VIDEO_INFO_SIZE (&state->info);
4393   if (num_bytes == 0) {
4394     g_warning ("Frame size should not be 0");
4395     goto error;
4396   }
4397
4398   needs_reconfigure = gst_pad_check_reconfigure (decoder->srcpad);
4399   if (G_UNLIKELY (decoder->priv->output_state_changed || needs_reconfigure)) {
4400     if (!gst_video_decoder_negotiate_unlocked (decoder)) {
4401       GST_DEBUG_OBJECT (decoder, "Failed to negotiate, fallback allocation");
4402       gst_pad_mark_reconfigure (decoder->srcpad);
4403     }
4404   }
4405
4406   GST_LOG_OBJECT (decoder, "alloc buffer size %d", num_bytes);
4407
4408   flow_ret = gst_buffer_pool_acquire_buffer (decoder->priv->pool,
4409       &frame->output_buffer, params);
4410
4411   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4412
4413   return flow_ret;
4414
4415 error:
4416   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4417   return GST_FLOW_ERROR;
4418 }
4419
4420 /**
4421  * gst_video_decoder_get_max_decode_time:
4422  * @decoder: a #GstVideoDecoder
4423  * @frame: a #GstVideoCodecFrame
4424  *
4425  * Determines maximum possible decoding time for @frame that will
4426  * allow it to decode and arrive in time (as determined by QoS events).
4427  * In particular, a negative result means decoding in time is no longer possible
4428  * and should therefore occur as soon/skippy as possible.
4429  *
4430  * Returns: max decoding time.
4431  */
4432 GstClockTimeDiff
4433 gst_video_decoder_get_max_decode_time (GstVideoDecoder *
4434     decoder, GstVideoCodecFrame * frame)
4435 {
4436   GstClockTimeDiff deadline;
4437   GstClockTime earliest_time;
4438
4439   GST_OBJECT_LOCK (decoder);
4440   earliest_time = decoder->priv->earliest_time;
4441   if (GST_CLOCK_TIME_IS_VALID (earliest_time)
4442       && GST_CLOCK_TIME_IS_VALID (frame->deadline))
4443     deadline = GST_CLOCK_DIFF (earliest_time, frame->deadline);
4444   else
4445     deadline = G_MAXINT64;
4446
4447   GST_LOG_OBJECT (decoder, "earliest %" GST_TIME_FORMAT
4448       ", frame deadline %" GST_TIME_FORMAT ", deadline %" GST_STIME_FORMAT,
4449       GST_TIME_ARGS (earliest_time), GST_TIME_ARGS (frame->deadline),
4450       GST_STIME_ARGS (deadline));
4451
4452   GST_OBJECT_UNLOCK (decoder);
4453
4454   return deadline;
4455 }
4456
4457 /**
4458  * gst_video_decoder_get_qos_proportion:
4459  * @decoder: a #GstVideoDecoder
4460  *     current QoS proportion, or %NULL
4461  *
4462  * Returns: The current QoS proportion.
4463  *
4464  * Since: 1.0.3
4465  */
4466 gdouble
4467 gst_video_decoder_get_qos_proportion (GstVideoDecoder * decoder)
4468 {
4469   gdouble proportion;
4470
4471   g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), 1.0);
4472
4473   GST_OBJECT_LOCK (decoder);
4474   proportion = decoder->priv->proportion;
4475   GST_OBJECT_UNLOCK (decoder);
4476
4477   return proportion;
4478 }
4479
4480 GstFlowReturn
4481 _gst_video_decoder_error (GstVideoDecoder * dec, gint weight,
4482     GQuark domain, gint code, gchar * txt, gchar * dbg, const gchar * file,
4483     const gchar * function, gint line)
4484 {
4485   if (txt)
4486     GST_WARNING_OBJECT (dec, "error: %s", txt);
4487   if (dbg)
4488     GST_WARNING_OBJECT (dec, "error: %s", dbg);
4489   dec->priv->error_count += weight;
4490   dec->priv->discont = TRUE;
4491   if (dec->priv->max_errors >= 0 &&
4492       dec->priv->error_count > dec->priv->max_errors) {
4493     gst_element_message_full (GST_ELEMENT (dec), GST_MESSAGE_ERROR,
4494         domain, code, txt, dbg, file, function, line);
4495     return GST_FLOW_ERROR;
4496   } else {
4497     g_free (txt);
4498     g_free (dbg);
4499     return GST_FLOW_OK;
4500   }
4501 }
4502
4503 /**
4504  * gst_video_decoder_set_max_errors:
4505  * @dec: a #GstVideoDecoder
4506  * @num: max tolerated errors
4507  *
4508  * Sets numbers of tolerated decoder errors, where a tolerated one is then only
4509  * warned about, but more than tolerated will lead to fatal error.  You can set
4510  * -1 for never returning fatal errors. Default is set to
4511  * GST_VIDEO_DECODER_MAX_ERRORS.
4512  *
4513  * The '-1' option was added in 1.4
4514  */
4515 void
4516 gst_video_decoder_set_max_errors (GstVideoDecoder * dec, gint num)
4517 {
4518   g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
4519
4520   dec->priv->max_errors = num;
4521 }
4522
4523 /**
4524  * gst_video_decoder_get_max_errors:
4525  * @dec: a #GstVideoDecoder
4526  *
4527  * Returns: currently configured decoder tolerated error count.
4528  */
4529 gint
4530 gst_video_decoder_get_max_errors (GstVideoDecoder * dec)
4531 {
4532   g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
4533
4534   return dec->priv->max_errors;
4535 }
4536
4537 /**
4538  * gst_video_decoder_set_needs_format:
4539  * @dec: a #GstVideoDecoder
4540  * @enabled: new state
4541  *
4542  * Configures decoder format needs.  If enabled, subclass needs to be
4543  * negotiated with format caps before it can process any data.  It will then
4544  * never be handed any data before it has been configured.
4545  * Otherwise, it might be handed data without having been configured and
4546  * is then expected being able to do so either by default
4547  * or based on the input data.
4548  *
4549  * Since: 1.4
4550  */
4551 void
4552 gst_video_decoder_set_needs_format (GstVideoDecoder * dec, gboolean enabled)
4553 {
4554   g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
4555
4556   dec->priv->needs_format = enabled;
4557 }
4558
4559 /**
4560  * gst_video_decoder_get_needs_format:
4561  * @dec: a #GstVideoDecoder
4562  *
4563  * Queries decoder required format handling.
4564  *
4565  * Returns: %TRUE if required format handling is enabled.
4566  *
4567  * Since: 1.4
4568  */
4569 gboolean
4570 gst_video_decoder_get_needs_format (GstVideoDecoder * dec)
4571 {
4572   gboolean result;
4573
4574   g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), FALSE);
4575
4576   result = dec->priv->needs_format;
4577
4578   return result;
4579 }
4580
4581 /**
4582  * gst_video_decoder_set_packetized:
4583  * @decoder: a #GstVideoDecoder
4584  * @packetized: whether the input data should be considered as packetized.
4585  *
4586  * Allows baseclass to consider input data as packetized or not. If the
4587  * input is packetized, then the @parse method will not be called.
4588  */
4589 void
4590 gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
4591     gboolean packetized)
4592 {
4593   decoder->priv->packetized = packetized;
4594 }
4595
4596 /**
4597  * gst_video_decoder_get_packetized:
4598  * @decoder: a #GstVideoDecoder
4599  *
4600  * Queries whether input data is considered packetized or not by the
4601  * base class.
4602  *
4603  * Returns: TRUE if input data is considered packetized.
4604  */
4605 gboolean
4606 gst_video_decoder_get_packetized (GstVideoDecoder * decoder)
4607 {
4608   return decoder->priv->packetized;
4609 }
4610
4611 /**
4612  * gst_video_decoder_set_estimate_rate:
4613  * @dec: a #GstVideoDecoder
4614  * @enabled: whether to enable byte to time conversion
4615  *
4616  * Allows baseclass to perform byte to time estimated conversion.
4617  */
4618 void
4619 gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec, gboolean enabled)
4620 {
4621   g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
4622
4623   dec->priv->do_estimate_rate = enabled;
4624 }
4625
4626 /**
4627  * gst_video_decoder_get_estimate_rate:
4628  * @dec: a #GstVideoDecoder
4629  *
4630  * Returns: currently configured byte to time conversion setting
4631  */
4632 gboolean
4633 gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec)
4634 {
4635   g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), 0);
4636
4637   return dec->priv->do_estimate_rate;
4638 }
4639
4640 /**
4641  * gst_video_decoder_set_latency:
4642  * @decoder: a #GstVideoDecoder
4643  * @min_latency: minimum latency
4644  * @max_latency: maximum latency
4645  *
4646  * Lets #GstVideoDecoder sub-classes tell the baseclass what the decoder
4647  * latency is. Will also post a LATENCY message on the bus so the pipeline
4648  * can reconfigure its global latency.
4649  */
4650 void
4651 gst_video_decoder_set_latency (GstVideoDecoder * decoder,
4652     GstClockTime min_latency, GstClockTime max_latency)
4653 {
4654   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
4655   g_return_if_fail (max_latency >= min_latency);
4656
4657   GST_OBJECT_LOCK (decoder);
4658   decoder->priv->min_latency = min_latency;
4659   decoder->priv->max_latency = max_latency;
4660   GST_OBJECT_UNLOCK (decoder);
4661
4662   gst_element_post_message (GST_ELEMENT_CAST (decoder),
4663       gst_message_new_latency (GST_OBJECT_CAST (decoder)));
4664 }
4665
4666 /**
4667  * gst_video_decoder_get_latency:
4668  * @decoder: a #GstVideoDecoder
4669  * @min_latency: (out) (allow-none): address of variable in which to store the
4670  *     configured minimum latency, or %NULL
4671  * @max_latency: (out) (allow-none): address of variable in which to store the
4672  *     configured mximum latency, or %NULL
4673  *
4674  * Query the configured decoder latency. Results will be returned via
4675  * @min_latency and @max_latency.
4676  */
4677 void
4678 gst_video_decoder_get_latency (GstVideoDecoder * decoder,
4679     GstClockTime * min_latency, GstClockTime * max_latency)
4680 {
4681   GST_OBJECT_LOCK (decoder);
4682   if (min_latency)
4683     *min_latency = decoder->priv->min_latency;
4684   if (max_latency)
4685     *max_latency = decoder->priv->max_latency;
4686   GST_OBJECT_UNLOCK (decoder);
4687 }
4688
4689 /**
4690  * gst_video_decoder_merge_tags:
4691  * @decoder: a #GstVideoDecoder
4692  * @tags: (allow-none): a #GstTagList to merge, or NULL to unset
4693  *     previously-set tags
4694  * @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
4695  *
4696  * Sets the audio decoder tags and how they should be merged with any
4697  * upstream stream tags. This will override any tags previously-set
4698  * with gst_audio_decoder_merge_tags().
4699  *
4700  * Note that this is provided for convenience, and the subclass is
4701  * not required to use this and can still do tag handling on its own.
4702  *
4703  * MT safe.
4704  */
4705 void
4706 gst_video_decoder_merge_tags (GstVideoDecoder * decoder,
4707     const GstTagList * tags, GstTagMergeMode mode)
4708 {
4709   g_return_if_fail (GST_IS_VIDEO_DECODER (decoder));
4710   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
4711   g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED);
4712
4713   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
4714   if (decoder->priv->tags != tags) {
4715     if (decoder->priv->tags) {
4716       gst_tag_list_unref (decoder->priv->tags);
4717       decoder->priv->tags = NULL;
4718       decoder->priv->tags_merge_mode = GST_TAG_MERGE_APPEND;
4719     }
4720     if (tags) {
4721       decoder->priv->tags = gst_tag_list_ref ((GstTagList *) tags);
4722       decoder->priv->tags_merge_mode = mode;
4723     }
4724
4725     GST_DEBUG_OBJECT (decoder, "set decoder tags to %" GST_PTR_FORMAT, tags);
4726     decoder->priv->tags_changed = TRUE;
4727   }
4728   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
4729 }
4730
4731 /**
4732  * gst_video_decoder_get_buffer_pool:
4733  * @decoder: a #GstVideoDecoder
4734  *
4735  * Returns: (transfer full): the instance of the #GstBufferPool used
4736  * by the decoder; free it after use it
4737  */
4738 GstBufferPool *
4739 gst_video_decoder_get_buffer_pool (GstVideoDecoder * decoder)
4740 {
4741   g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), NULL);
4742
4743   if (decoder->priv->pool)
4744     return gst_object_ref (decoder->priv->pool);
4745
4746   return NULL;
4747 }
4748
4749 /**
4750  * gst_video_decoder_get_allocator:
4751  * @decoder: a #GstVideoDecoder
4752  * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
4753  * used
4754  * @params: (out) (allow-none) (transfer full): the
4755  * #GstAllocationParams of @allocator
4756  *
4757  * Lets #GstVideoDecoder sub-classes to know the memory @allocator
4758  * used by the base class and its @params.
4759  *
4760  * Unref the @allocator after use it.
4761  */
4762 void
4763 gst_video_decoder_get_allocator (GstVideoDecoder * decoder,
4764     GstAllocator ** allocator, GstAllocationParams * params)
4765 {
4766   g_return_if_fail (GST_IS_VIDEO_DECODER (decoder));
4767
4768   if (allocator)
4769     *allocator = decoder->priv->allocator ?
4770         gst_object_ref (decoder->priv->allocator) : NULL;
4771
4772   if (params)
4773     *params = decoder->priv->params;
4774 }
4775
4776 /**
4777  * gst_video_decoder_set_use_default_pad_acceptcaps:
4778  * @decoder: a #GstVideoDecoder
4779  * @use: if the default pad accept-caps query handling should be used
4780  *
4781  * Lets #GstVideoDecoder sub-classes decide if they want the sink pad
4782  * to use the default pad query handler to reply to accept-caps queries.
4783  *
4784  * By setting this to true it is possible to further customize the default
4785  * handler with %GST_PAD_SET_ACCEPT_INTERSECT and
4786  * %GST_PAD_SET_ACCEPT_TEMPLATE
4787  *
4788  * Since: 1.6
4789  */
4790 void
4791 gst_video_decoder_set_use_default_pad_acceptcaps (GstVideoDecoder * decoder,
4792     gboolean use)
4793 {
4794   decoder->priv->use_default_pad_acceptcaps = use;
4795 }
4796
4797 /**
4798  * gst_video_decoder_request_sync_point:
4799  * @dec: a #GstVideoDecoder
4800  * @frame: a #GstVideoCodecFrame
4801  * @flags: #GstVideoDecoderRequestSyncPointFlags
4802  *
4803  * Allows the #GstVideoDecoder subclass to request from the base class that
4804  * a new sync should be requested from upstream, and that @frame was the frame
4805  * when the subclass noticed that a new sync point is required. A reason for
4806  * the subclass to do this could be missing reference frames, for example.
4807  *
4808  * The base class will then request a new sync point from upstream as long as
4809  * the time that passed since the last one is exceeding
4810  * #GstVideoDecoder:min-force-key-unit-interval.
4811  *
4812  * The subclass can signal via @flags how the frames until the next sync point
4813  * should be handled:
4814  *
4815  *   * If %GST_VIDEO_DECODER_REQUEST_SYNC_POINT_DISCARD_INPUT is selected then
4816  *     all following input frames until the next sync point are discarded.
4817  *     This can be useful if the lack of a sync point will prevent all further
4818  *     decoding and the decoder implementation is not very robust in handling
4819  *     missing references frames.
4820  *   * If %GST_VIDEO_DECODER_REQUEST_SYNC_POINT_CORRUPT_OUTPUT is selected
4821  *     then all output frames following @frame are marked as corrupted via
4822  *     %GST_BUFFER_FLAG_CORRUPTED. Corrupted frames can be automatically
4823  *     dropped by the base class, see #GstVideoDecoder:discard-corrupted-frames.
4824  *     Subclasses can manually mark frames as corrupted via %GST_VIDEO_CODEC_FRAME_FLAG_CORRUPTED
4825  *     before calling gst_video_decoder_finish_frame().
4826  *
4827  * Since: 1.20
4828  */
4829 void
4830 gst_video_decoder_request_sync_point (GstVideoDecoder * dec,
4831     GstVideoCodecFrame * frame, GstVideoDecoderRequestSyncPointFlags flags)
4832 {
4833   GstEvent *fku = NULL;
4834   GstVideoDecoderPrivate *priv;
4835
4836   g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
4837   g_return_if_fail (frame != NULL);
4838
4839   priv = dec->priv;
4840
4841   GST_OBJECT_LOCK (dec);
4842
4843   /* Check if we're allowed to send a new force-keyunit event.
4844    * frame->deadline is set to the running time of the PTS. */
4845   if (priv->min_force_key_unit_interval == 0 ||
4846       frame->deadline == GST_CLOCK_TIME_NONE ||
4847       (priv->min_force_key_unit_interval != GST_CLOCK_TIME_NONE &&
4848           (priv->last_force_key_unit_time == GST_CLOCK_TIME_NONE
4849               || (priv->last_force_key_unit_time +
4850                   priv->min_force_key_unit_interval >= frame->deadline)))) {
4851     GST_DEBUG_OBJECT (dec,
4852         "Requesting a new key-unit for frame with PTS %" GST_TIME_FORMAT,
4853         GST_TIME_ARGS (frame->pts));
4854     fku =
4855         gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE, FALSE,
4856         0);
4857     priv->last_force_key_unit_time = frame->deadline;
4858   } else {
4859     GST_DEBUG_OBJECT (dec,
4860         "Can't request a new key-unit for frame with PTS %" GST_TIME_FORMAT,
4861         GST_TIME_ARGS (frame->pts));
4862   }
4863   priv->request_sync_point_flags |= flags;
4864   /* We don't know yet the frame number of the sync point so set it to a
4865    * frame number higher than any allowed frame number */
4866   priv->request_sync_point_frame_number = REQUEST_SYNC_POINT_PENDING;
4867   GST_OBJECT_UNLOCK (dec);
4868
4869   if (fku)
4870     gst_pad_push_event (dec->sinkpad, fku);
4871 }
4872
4873 /**
4874  * gst_video_decoder_set_needs_sync_point:
4875  * @dec: a #GstVideoDecoder
4876  * @enabled: new state
4877  *
4878  * Configures whether the decoder requires a sync point before it starts
4879  * outputting data in the beginning. If enabled, the base class will discard
4880  * all non-sync point frames in the beginning and after a flush and does not
4881  * pass it to the subclass.
4882  *
4883  * If the first frame is not a sync point, the base class will request a sync
4884  * point via the force-key-unit event.
4885  *
4886  * Since: 1.20
4887  */
4888 void
4889 gst_video_decoder_set_needs_sync_point (GstVideoDecoder * dec, gboolean enabled)
4890 {
4891   g_return_if_fail (GST_IS_VIDEO_DECODER (dec));
4892
4893   dec->priv->needs_sync_point = enabled;
4894 }
4895
4896 /**
4897  * gst_video_decoder_get_needs_sync_point:
4898  * @dec: a #GstVideoDecoder
4899  *
4900  * Queries if the decoder requires a sync point before it starts outputting
4901  * data in the beginning.
4902  *
4903  * Returns: %TRUE if a sync point is required in the beginning.
4904  *
4905  * Since: 1.20
4906  */
4907 gboolean
4908 gst_video_decoder_get_needs_sync_point (GstVideoDecoder * dec)
4909 {
4910   gboolean result;
4911
4912   g_return_val_if_fail (GST_IS_VIDEO_DECODER (dec), FALSE);
4913
4914   result = dec->priv->needs_sync_point;
4915
4916   return result;
4917 }