Coverage Report

Created: 2026-11-23 06:49

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