avvidec: port to new decoding API
[platform/upstream/gstreamer.git] / ext / libav / gstavviddec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <assert.h>
25 #include <string.h>
26
27 #include <libavcodec/avcodec.h>
28 #include <libavutil/stereo3d.h>
29
30 #include "gstav.h"
31 #include "gstavcodecmap.h"
32 #include "gstavutils.h"
33 #include "gstavviddec.h"
34
35 #define MAX_TS_MASK 0xff
36
37 #define DEFAULT_LOWRES                  0
38 #define DEFAULT_SKIPFRAME               0
39 #define DEFAULT_DIRECT_RENDERING        TRUE
40 #define DEFAULT_DEBUG_MV                FALSE
41 #define DEFAULT_MAX_THREADS             0
42 #define DEFAULT_OUTPUT_CORRUPT          TRUE
43 #define REQUIRED_POOL_MAX_BUFFERS       32
44 #define DEFAULT_STRIDE_ALIGN            31
45 #define DEFAULT_ALLOC_PARAM             { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
46
47 enum
48 {
49   PROP_0,
50   PROP_LOWRES,
51   PROP_SKIPFRAME,
52   PROP_DIRECT_RENDERING,
53   PROP_DEBUG_MV,
54   PROP_MAX_THREADS,
55   PROP_OUTPUT_CORRUPT,
56   PROP_LAST
57 };
58
59 /* A number of function prototypes are given so we can refer to them later. */
60 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
61 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
62 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
63 static void gst_ffmpegviddec_finalize (GObject * object);
64
65 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
66     GstVideoCodecState * state);
67 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
68     GstVideoCodecFrame * frame);
69 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
70 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
71 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
72 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
73     GstQuery * query);
74 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
75     GstQuery * query);
76
77 static void gst_ffmpegviddec_set_property (GObject * object,
78     guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_ffmpegviddec_get_property (GObject * object,
80     guint prop_id, GValue * value, GParamSpec * pspec);
81
82 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
83     AVCodecContext * context, AVFrame * picture);
84
85 /* some sort of bufferpool handling, but different */
86 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
87     AVFrame * picture, int flags);
88
89 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
90 static GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
91
92 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
93     AVFrame * picture);
94 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
95     AVCodecContext * context);
96
97 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
98
99 static GstElementClass *parent_class = NULL;
100
101 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
102 static GType
103 gst_ffmpegviddec_lowres_get_type (void)
104 {
105   static GType ffmpegdec_lowres_type = 0;
106
107   if (!ffmpegdec_lowres_type) {
108     static const GEnumValue ffmpegdec_lowres[] = {
109       {0, "0", "full"},
110       {1, "1", "1/2-size"},
111       {2, "2", "1/4-size"},
112       {0, NULL, NULL},
113     };
114
115     ffmpegdec_lowres_type =
116         g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
117   }
118
119   return ffmpegdec_lowres_type;
120 }
121
122 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
123 static GType
124 gst_ffmpegviddec_skipframe_get_type (void)
125 {
126   static GType ffmpegdec_skipframe_type = 0;
127
128   if (!ffmpegdec_skipframe_type) {
129     static const GEnumValue ffmpegdec_skipframe[] = {
130       {0, "0", "Skip nothing"},
131       {1, "1", "Skip B-frames"},
132       {2, "2", "Skip IDCT/Dequantization"},
133       {5, "5", "Skip everything"},
134       {0, NULL, NULL},
135     };
136
137     ffmpegdec_skipframe_type =
138         g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
139   }
140
141   return ffmpegdec_skipframe_type;
142 }
143
144 static void
145 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
146 {
147   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148   GstPadTemplate *sinktempl, *srctempl;
149   GstCaps *sinkcaps, *srccaps;
150   AVCodec *in_plugin;
151   gchar *longname, *description;
152
153   in_plugin =
154       (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
155       GST_FFDEC_PARAMS_QDATA);
156   g_assert (in_plugin != NULL);
157
158   /* construct the element details struct */
159   longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
160   description = g_strdup_printf ("libav %s decoder", in_plugin->name);
161   gst_element_class_set_metadata (element_class, longname,
162       "Codec/Decoder/Video", description,
163       "Wim Taymans <wim.taymans@gmail.com>, "
164       "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
165       "Edward Hervey <bilboed@bilboed.com>");
166   g_free (longname);
167   g_free (description);
168
169   /* get the caps */
170   sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
171   if (!sinkcaps) {
172     GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
173     sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
174   }
175   srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
176       in_plugin->id, FALSE, in_plugin);
177   if (!srccaps) {
178     GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
179     srccaps = gst_caps_from_string ("video/x-raw");
180   }
181
182   /* pad templates */
183   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
184       GST_PAD_ALWAYS, sinkcaps);
185   srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
186
187   gst_element_class_add_pad_template (element_class, srctempl);
188   gst_element_class_add_pad_template (element_class, sinktempl);
189
190   gst_caps_unref (sinkcaps);
191   gst_caps_unref (srccaps);
192
193   klass->in_plugin = in_plugin;
194 }
195
196 static void
197 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
198 {
199   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200   GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
201   int caps;
202
203   parent_class = g_type_class_peek_parent (klass);
204
205   gobject_class->finalize = gst_ffmpegviddec_finalize;
206
207   gobject_class->set_property = gst_ffmpegviddec_set_property;
208   gobject_class->get_property = gst_ffmpegviddec_get_property;
209
210   g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
211       g_param_spec_enum ("skip-frame", "Skip frames",
212           "Which types of frames to skip during decoding",
213           GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
214           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215   g_object_class_install_property (gobject_class, PROP_LOWRES,
216       g_param_spec_enum ("lowres", "Low resolution",
217           "At which resolution to decode images",
218           GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
219           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220   g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
221       g_param_spec_boolean ("direct-rendering", "Direct Rendering",
222           "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
223           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224   g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
225       g_param_spec_boolean ("debug-mv", "Debug motion vectors",
226           "Whether libav should print motion vectors on top of the image",
227           DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228   g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
229       g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
230           "Whether libav should output frames even if corrupted",
231           DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232
233   caps = klass->in_plugin->capabilities;
234   if (caps & (AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS)) {
235     g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
236         g_param_spec_int ("max-threads", "Maximum decode threads",
237             "Maximum number of worker threads to spawn. (0 = auto)",
238             0, G_MAXINT, DEFAULT_MAX_THREADS,
239             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240   }
241
242   viddec_class->set_format = gst_ffmpegviddec_set_format;
243   viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
244   viddec_class->start = gst_ffmpegviddec_start;
245   viddec_class->stop = gst_ffmpegviddec_stop;
246   viddec_class->flush = gst_ffmpegviddec_flush;
247   viddec_class->finish = gst_ffmpegviddec_finish;
248   viddec_class->drain = gst_ffmpegviddec_drain;
249   viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
250   viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
251 }
252
253 static void
254 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
255 {
256   GstFFMpegVidDecClass *klass =
257       (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
258
259   /* some ffmpeg data */
260   ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
261   ffmpegdec->context->opaque = ffmpegdec;
262   ffmpegdec->picture = av_frame_alloc ();
263   ffmpegdec->opened = FALSE;
264   ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
265   ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
266   ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
267   ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
268   ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
269
270   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
271   gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
272       (ffmpegdec), TRUE);
273
274   gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
275 }
276
277 static void
278 gst_ffmpegviddec_finalize (GObject * object)
279 {
280   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
281
282   av_frame_free (&ffmpegdec->picture);
283
284   if (ffmpegdec->context != NULL) {
285     gst_ffmpeg_avcodec_close (ffmpegdec->context);
286     av_free (ffmpegdec->context);
287     ffmpegdec->context = NULL;
288   }
289
290   G_OBJECT_CLASS (parent_class)->finalize (object);
291 }
292
293 static void
294 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
295     gboolean enable)
296 {
297   g_return_if_fail (context != NULL);
298
299   if (enable)
300     context->flags |= flags;
301   else
302     context->flags &= ~flags;
303 }
304
305 /* with LOCK */
306 static gboolean
307 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
308 {
309   GstFFMpegVidDecClass *oclass;
310   gint i;
311
312   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
313
314   GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
315
316   gst_caps_replace (&ffmpegdec->last_caps, NULL);
317
318   gst_ffmpeg_avcodec_close (ffmpegdec->context);
319   ffmpegdec->opened = FALSE;
320
321   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
322     ffmpegdec->stride[i] = -1;
323
324   gst_buffer_replace (&ffmpegdec->palette, NULL);
325
326   if (ffmpegdec->context->extradata) {
327     av_free (ffmpegdec->context->extradata);
328     ffmpegdec->context->extradata = NULL;
329   }
330   if (reset) {
331     if (avcodec_get_context_defaults3 (ffmpegdec->context,
332             oclass->in_plugin) < 0) {
333       GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
334       return FALSE;
335     }
336     ffmpegdec->context->opaque = ffmpegdec;
337   }
338   return TRUE;
339 }
340
341 /* with LOCK */
342 static gboolean
343 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
344 {
345   GstFFMpegVidDecClass *oclass;
346   gint i;
347
348   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
349
350   if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
351     goto could_not_open;
352
353   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
354     ffmpegdec->stride[i] = -1;
355
356   ffmpegdec->opened = TRUE;
357
358   GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
359       oclass->in_plugin->name, oclass->in_plugin->id);
360
361   gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
362       AV_CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
363
364   return TRUE;
365
366   /* ERRORS */
367 could_not_open:
368   {
369     gst_ffmpegviddec_close (ffmpegdec, TRUE);
370     GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
371         oclass->in_plugin->name);
372     return FALSE;
373   }
374 }
375
376 static void
377 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
378     GstVideoCodecState * state)
379 {
380   GstStructure *str = gst_caps_get_structure (state->caps, 0);
381   const GValue *palette_v;
382   GstBuffer *palette;
383
384   /* do we have a palette? */
385   if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
386     palette = gst_value_get_buffer (palette_v);
387     GST_DEBUG ("got palette data %p", palette);
388     if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
389       gst_buffer_replace (&ffmpegdec->palette, palette);
390     }
391   }
392 }
393
394
395 static gboolean
396 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
397     GstVideoCodecState * state)
398 {
399   GstFFMpegVidDec *ffmpegdec;
400   GstFFMpegVidDecClass *oclass;
401   GstClockTime latency = GST_CLOCK_TIME_NONE;
402   gboolean ret = FALSE;
403
404   ffmpegdec = (GstFFMpegVidDec *) decoder;
405   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
406
407   if (ffmpegdec->last_caps != NULL &&
408       gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
409     return TRUE;
410   }
411
412   GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
413
414   GST_OBJECT_LOCK (ffmpegdec);
415   /* stupid check for VC1 */
416   if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
417       (oclass->in_plugin->id == AV_CODEC_ID_VC1))
418     oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
419
420   /* close old session */
421   if (ffmpegdec->opened) {
422     GST_OBJECT_UNLOCK (ffmpegdec);
423     gst_ffmpegviddec_finish (decoder);
424     GST_OBJECT_LOCK (ffmpegdec);
425     if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
426       GST_OBJECT_UNLOCK (ffmpegdec);
427       return FALSE;
428     }
429     ffmpegdec->pic_pix_fmt = 0;
430     ffmpegdec->pic_width = 0;
431     ffmpegdec->pic_height = 0;
432     ffmpegdec->pic_par_n = 0;
433     ffmpegdec->pic_par_d = 0;
434     ffmpegdec->pic_interlaced = 0;
435     ffmpegdec->pic_field_order = 0;
436     ffmpegdec->pic_field_order_changed = FALSE;
437     ffmpegdec->ctx_ticks = 0;
438     ffmpegdec->ctx_time_n = 0;
439     ffmpegdec->ctx_time_d = 0;
440     ffmpegdec->cur_multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
441     ffmpegdec->cur_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
442   }
443
444   gst_caps_replace (&ffmpegdec->last_caps, state->caps);
445
446   /* set buffer functions */
447   ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
448   ffmpegdec->context->draw_horiz_band = NULL;
449
450   /* reset coded_width/_height to prevent it being reused from last time when
451    * the codec is opened again, causing a mismatch and possible
452    * segfault/corruption. (Common scenario when renegotiating caps) */
453   ffmpegdec->context->coded_width = 0;
454   ffmpegdec->context->coded_height = 0;
455
456   GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
457       ffmpegdec->context->height);
458
459   /* FIXME : Create a method that takes GstVideoCodecState instead */
460   /* get size and so */
461   gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
462       oclass->in_plugin->type, state->caps, ffmpegdec->context);
463
464   GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
465       ffmpegdec->context->height);
466
467   gst_ffmpegviddec_get_palette (ffmpegdec, state);
468
469   if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
470     GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
471     ffmpegdec->context->time_base.num = 1;
472     ffmpegdec->context->time_base.den = 25;
473   }
474
475   /* workaround encoder bugs */
476   ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
477   ffmpegdec->context->err_recognition = 1;
478
479   /* for slow cpus */
480   ffmpegdec->context->lowres = ffmpegdec->lowres;
481   ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
482
483   /* ffmpeg can draw motion vectors on top of the image (not every decoder
484    * supports it) */
485   ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
486
487   {
488     GstQuery *query;
489     gboolean is_live;
490
491     if (ffmpegdec->max_threads == 0) {
492       if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_AUTO_THREADS))
493         ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
494       else
495         ffmpegdec->context->thread_count = 0;
496     } else
497       ffmpegdec->context->thread_count = ffmpegdec->max_threads;
498
499     query = gst_query_new_latency ();
500     is_live = FALSE;
501     /* Check if upstream is live. If it isn't we can enable frame based
502      * threading, which is adding latency */
503     if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
504       gst_query_parse_latency (query, &is_live, NULL, NULL);
505     }
506     gst_query_unref (query);
507
508     if (is_live)
509       ffmpegdec->context->thread_type = FF_THREAD_SLICE;
510     else
511       ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
512   }
513
514   /* open codec - we don't select an output pix_fmt yet,
515    * simply because we don't know! We only get it
516    * during playback... */
517   if (!gst_ffmpegviddec_open (ffmpegdec))
518     goto open_failed;
519
520   if (ffmpegdec->input_state)
521     gst_video_codec_state_unref (ffmpegdec->input_state);
522   ffmpegdec->input_state = gst_video_codec_state_ref (state);
523
524   if (ffmpegdec->input_state->info.fps_n) {
525     GstVideoInfo *info = &ffmpegdec->input_state->info;
526     latency = gst_util_uint64_scale_ceil (
527         (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
528         info->fps_n);
529   }
530
531   ret = TRUE;
532
533 done:
534   GST_OBJECT_UNLOCK (ffmpegdec);
535
536   if (GST_CLOCK_TIME_IS_VALID (latency))
537     gst_video_decoder_set_latency (decoder, latency, latency);
538
539   return ret;
540
541   /* ERRORS */
542 open_failed:
543   {
544     GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
545     goto done;
546   }
547 }
548
549 typedef struct
550 {
551   GstFFMpegVidDec *ffmpegdec;
552   GstVideoCodecFrame *frame;
553   gboolean mapped;
554   GstVideoFrame vframe;
555   GstBuffer *buffer;
556   AVBufferRef *avbuffer;
557 } GstFFMpegVidDecVideoFrame;
558
559 static GstFFMpegVidDecVideoFrame *
560 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
561     GstVideoCodecFrame * frame)
562 {
563   GstFFMpegVidDecVideoFrame *dframe;
564
565   dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
566   dframe->ffmpegdec = ffmpegdec;
567   dframe->frame = frame;
568
569   GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
570
571   return dframe;
572 }
573
574 static void
575 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
576     GstFFMpegVidDecVideoFrame * frame)
577 {
578   GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
579
580   if (frame->mapped)
581     gst_video_frame_unmap (&frame->vframe);
582   gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
583   gst_buffer_replace (&frame->buffer, NULL);
584   if (frame->avbuffer) {
585     av_buffer_unref (&frame->avbuffer);
586   }
587   g_slice_free (GstFFMpegVidDecVideoFrame, frame);
588 }
589
590 static void
591 dummy_free_buffer (void *opaque, uint8_t * data)
592 {
593   GstFFMpegVidDecVideoFrame *frame = opaque;
594
595   gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
596 }
597
598 /* This function prepares the pool configuration for direct rendering. To use
599  * this method, the codec should support direct rendering and the pool should
600  * support video meta and video alignment */
601 static void
602 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
603     GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
604 {
605   GstAllocationParams params;
606   GstVideoAlignment align;
607   GstAllocator *allocator = NULL;
608   gint width, height;
609   gint linesize_align[4];
610   gint i;
611   gsize max_align;
612
613   width = GST_VIDEO_INFO_WIDTH (info);
614   height = GST_VIDEO_INFO_HEIGHT (info);
615
616   /* let ffmpeg find the alignment and padding */
617   avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
618       linesize_align);
619
620   align.padding_top = 0;
621   align.padding_left = 0;
622   align.padding_right = width - GST_VIDEO_INFO_WIDTH (info);
623   align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info);
624
625   /* add extra padding to match libav buffer allocation sizes */
626   align.padding_bottom++;
627
628   gst_buffer_pool_config_get_allocator (config, &allocator, &params);
629
630   max_align = DEFAULT_STRIDE_ALIGN;
631   max_align |= params.align;
632
633   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
634     if (linesize_align[i] > 0)
635       max_align |= linesize_align[i] - 1;
636   }
637
638   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
639     align.stride_align[i] = max_align;
640
641   params.align = max_align;
642
643   gst_buffer_pool_config_set_allocator (config, allocator, &params);
644
645   GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
646       "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
647       GST_VIDEO_INFO_WIDTH (info),
648       GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
649       align.padding_left, align.padding_right, align.padding_bottom,
650       align.stride_align[0], align.stride_align[1], align.stride_align[2],
651       align.stride_align[3]);
652
653   gst_buffer_pool_config_add_option (config,
654       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
655   gst_buffer_pool_config_set_video_alignment (config, &align);
656 }
657
658 static void
659 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
660     AVFrame * picture)
661 {
662   GstAllocationParams params = DEFAULT_ALLOC_PARAM;
663   GstVideoInfo info;
664   GstVideoFormat format;
665   GstCaps *caps;
666   GstStructure *config;
667   gint i;
668
669   if (ffmpegdec->internal_pool != NULL &&
670       ffmpegdec->pool_width == picture->width &&
671       ffmpegdec->pool_height == picture->height &&
672       ffmpegdec->pool_format == picture->format)
673     return;
674
675   GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
676       picture->width, picture->height);
677
678   format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
679   gst_video_info_set_format (&info, format, picture->width, picture->height);
680
681   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
682     ffmpegdec->stride[i] = -1;
683
684   if (ffmpegdec->internal_pool)
685     gst_object_unref (ffmpegdec->internal_pool);
686
687   ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
688   config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
689
690   caps = gst_video_info_to_caps (&info);
691   gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
692   gst_buffer_pool_config_set_allocator (config, NULL, &params);
693   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
694
695   gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
696       ffmpegdec->internal_pool, &info, config);
697   /* generic video pool never fails */
698   gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
699   gst_caps_unref (caps);
700
701   gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
702
703   /* Remember pool size so we can detect changes */
704   ffmpegdec->pool_width = picture->width;
705   ffmpegdec->pool_height = picture->height;
706   ffmpegdec->pool_format = picture->format;
707   ffmpegdec->pool_info = info;
708 }
709
710 static gboolean
711 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
712 {
713   GstFFMpegVidDecClass *oclass;
714
715   if (!ffmpegdec->direct_rendering)
716     return FALSE;
717
718   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
719   return ((oclass->in_plugin->capabilities & AV_CODEC_CAP_DR1) ==
720       AV_CODEC_CAP_DR1);
721 }
722
723 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
724  * into. We try to give it memory from our pool */
725 static int
726 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
727     int flags)
728 {
729   GstVideoCodecFrame *frame;
730   GstFFMpegVidDecVideoFrame *dframe;
731   GstFFMpegVidDec *ffmpegdec;
732   gint c;
733   GstFlowReturn ret;
734
735   ffmpegdec = (GstFFMpegVidDec *) context->opaque;
736
737   GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
738
739   /* apply the last info we have seen to this picture, when we get the
740    * picture back from ffmpeg we can use this to correctly timestamp the output
741    * buffer */
742   GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
743       (gint32) picture->reordered_opaque);
744
745   frame =
746       gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
747       picture->reordered_opaque);
748   if (G_UNLIKELY (frame == NULL))
749     goto no_frame;
750
751   /* now it has a buffer allocated, so it is real and will also
752    * be _released */
753   GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
754       GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
755
756   if (G_UNLIKELY (frame->output_buffer != NULL))
757     goto duplicate_frame;
758
759   /* GstFFMpegVidDecVideoFrame receives the frame ref */
760   if (picture->opaque) {
761     dframe = picture->opaque;
762     dframe->frame = frame;
763   } else {
764     picture->opaque = dframe =
765         gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
766   }
767
768   GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
769
770   if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
771     goto no_dr;
772
773   gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
774
775   ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
776       &frame->output_buffer, NULL);
777   if (ret != GST_FLOW_OK)
778     goto alloc_failed;
779
780   /* piggy-backed alloc'ed on the frame,
781    * and there was much rejoicing and we are grateful.
782    * Now take away buffer from frame, we will give it back later when decoded.
783    * This allows multiple request for a buffer per frame; unusual but possible. */
784   gst_buffer_replace (&dframe->buffer, frame->output_buffer);
785   gst_buffer_replace (&frame->output_buffer, NULL);
786
787   /* Fill avpicture */
788   if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
789           dframe->buffer, GST_MAP_READWRITE))
790     goto map_failed;
791   dframe->mapped = TRUE;
792
793   for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
794     if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
795       picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
796       picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
797
798       if (ffmpegdec->stride[c] == -1)
799         ffmpegdec->stride[c] = picture->linesize[c];
800
801       /* libav does not allow stride changes, decide allocation should check
802        * before replacing the internal pool with a downstream pool.
803        * https://bugzilla.gnome.org/show_bug.cgi?id=704769
804        * https://bugzilla.libav.org/show_bug.cgi?id=556
805        */
806       g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
807     } else {
808       picture->data[c] = NULL;
809       picture->linesize[c] = 0;
810     }
811     GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
812         picture->data[c]);
813   }
814
815   picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
816
817   GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
818
819   return 0;
820
821 no_dr:
822   {
823     int c;
824     int ret = avcodec_default_get_buffer2 (context, picture, flags);
825
826     GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
827
828     for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
829       ffmpegdec->stride[c] = picture->linesize[c];
830     }
831     /* Wrap our buffer around the default one to be able to have a callback
832      * when our data can be freed. Just putting our data into the first free
833      * buffer might not work if there are too many allocated already
834      */
835     if (picture->buf[0]) {
836       dframe->avbuffer = picture->buf[0];
837       picture->buf[0] =
838           av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
839           dummy_free_buffer, dframe, 0);
840     } else {
841       picture->buf[0] =
842           av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
843     }
844
845     return ret;
846   }
847 alloc_failed:
848   {
849     GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
850         ("Unable to allocate memory"),
851         ("The downstream pool failed to allocated buffer."));
852     return -1;
853   }
854 map_failed:
855   {
856     GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
857         ("Cannot access memory for read and write operation."),
858         ("The video memory allocated from downstream pool could not mapped for"
859             "read and write."));
860     return -1;
861   }
862 duplicate_frame:
863   {
864     GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
865     gst_video_codec_frame_unref (frame);
866     return -1;
867   }
868 no_frame:
869   {
870     GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
871     return -1;
872   }
873 }
874
875 static gboolean
876 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
877 {
878   gint pic_field_order = 0;
879
880   if (picture->interlaced_frame) {
881     if (picture->repeat_pict)
882       pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
883     if (picture->top_field_first)
884       pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
885   }
886
887   return !(ffmpegdec->pic_width == picture->width
888       && ffmpegdec->pic_height == picture->height
889       && ffmpegdec->pic_pix_fmt == picture->format
890       && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
891       && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
892       && ffmpegdec->pic_interlaced == picture->interlaced_frame
893       && ffmpegdec->pic_field_order == pic_field_order
894       && ffmpegdec->cur_multiview_mode == ffmpegdec->picture_multiview_mode
895       && ffmpegdec->cur_multiview_flags == ffmpegdec->picture_multiview_flags);
896 }
897
898 static gboolean
899 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
900 {
901   return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
902       && ffmpegdec->ctx_time_n == context->time_base.num
903       && ffmpegdec->ctx_time_d == context->time_base.den);
904 }
905
906 static gboolean
907 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
908     AVFrame * picture)
909 {
910   gint pic_field_order = 0;
911
912   if (picture->interlaced_frame) {
913     if (picture->repeat_pict)
914       pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
915     if (picture->top_field_first)
916       pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
917   }
918
919   if (!picture_changed (ffmpegdec, picture)
920       && !context_changed (ffmpegdec, context))
921     return FALSE;
922
923   GST_DEBUG_OBJECT (ffmpegdec,
924       "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
925       ffmpegdec->pic_width, ffmpegdec->pic_height,
926       ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
927       ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
928       ffmpegdec->pic_pix_fmt,
929       picture->width, picture->height,
930       picture->sample_aspect_ratio.num,
931       picture->sample_aspect_ratio.den,
932       context->time_base.num, context->time_base.den, picture->format);
933
934   ffmpegdec->pic_pix_fmt = picture->format;
935   ffmpegdec->pic_width = picture->width;
936   ffmpegdec->pic_height = picture->height;
937   ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
938   ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
939   ffmpegdec->cur_multiview_mode = ffmpegdec->picture_multiview_mode;
940   ffmpegdec->cur_multiview_flags = ffmpegdec->picture_multiview_flags;
941
942   /* Remember if we have interlaced content and the field order changed
943    * at least once. If that happens, we must be interlace-mode=mixed
944    */
945   if (ffmpegdec->pic_field_order_changed ||
946       (ffmpegdec->pic_field_order != pic_field_order &&
947           ffmpegdec->pic_interlaced))
948     ffmpegdec->pic_field_order_changed = TRUE;
949
950   ffmpegdec->pic_field_order = pic_field_order;
951   ffmpegdec->pic_interlaced = picture->interlaced_frame;
952
953   if (!ffmpegdec->pic_interlaced)
954     ffmpegdec->pic_field_order_changed = FALSE;
955
956   ffmpegdec->ctx_ticks = context->ticks_per_frame;
957   ffmpegdec->ctx_time_n = context->time_base.num;
958   ffmpegdec->ctx_time_d = context->time_base.den;
959
960   return TRUE;
961 }
962
963 static void
964 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
965     GstVideoInfo * in_info, GstVideoInfo * out_info)
966 {
967   gboolean demuxer_par_set = FALSE;
968   gboolean decoder_par_set = FALSE;
969   gint demuxer_num = 1, demuxer_denom = 1;
970   gint decoder_num = 1, decoder_denom = 1;
971
972   if (in_info->par_n && in_info->par_d) {
973     demuxer_num = in_info->par_n;
974     demuxer_denom = in_info->par_d;
975     demuxer_par_set = TRUE;
976     GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
977         demuxer_denom);
978   }
979
980   if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
981     decoder_num = ffmpegdec->pic_par_n;
982     decoder_denom = ffmpegdec->pic_par_d;
983     decoder_par_set = TRUE;
984     GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
985         decoder_denom);
986   }
987
988   if (!demuxer_par_set && !decoder_par_set)
989     goto no_par;
990
991   if (demuxer_par_set && !decoder_par_set)
992     goto use_demuxer_par;
993
994   if (decoder_par_set && !demuxer_par_set)
995     goto use_decoder_par;
996
997   /* Both the demuxer and the decoder provide a PAR. If one of
998    * the two PARs is 1:1 and the other one is not, use the one
999    * that is not 1:1. */
1000   if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
1001     goto use_decoder_par;
1002
1003   if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
1004     goto use_demuxer_par;
1005
1006   /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
1007   goto use_demuxer_par;
1008
1009 use_decoder_par:
1010   {
1011     GST_DEBUG_OBJECT (ffmpegdec,
1012         "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
1013         decoder_denom);
1014     out_info->par_n = decoder_num;
1015     out_info->par_d = decoder_denom;
1016     return;
1017   }
1018 use_demuxer_par:
1019   {
1020     GST_DEBUG_OBJECT (ffmpegdec,
1021         "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
1022         demuxer_denom);
1023     out_info->par_n = demuxer_num;
1024     out_info->par_d = demuxer_denom;
1025     return;
1026   }
1027 no_par:
1028   {
1029     GST_DEBUG_OBJECT (ffmpegdec,
1030         "Neither demuxer nor codec provide a pixel-aspect-ratio");
1031     out_info->par_n = 1;
1032     out_info->par_d = 1;
1033     return;
1034   }
1035 }
1036
1037 static GstVideoMultiviewMode
1038 stereo_av_to_gst (enum AVStereo3DType type)
1039 {
1040   switch (type) {
1041     case AV_STEREO3D_SIDEBYSIDE:
1042       return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
1043     case AV_STEREO3D_TOPBOTTOM:
1044       return GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
1045     case AV_STEREO3D_FRAMESEQUENCE:
1046       return GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
1047     case AV_STEREO3D_CHECKERBOARD:
1048       return GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
1049     case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
1050       return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX;
1051     case AV_STEREO3D_LINES:
1052       return GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED;
1053     case AV_STEREO3D_COLUMNS:
1054       return GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED;
1055     default:
1056       break;
1057   }
1058
1059   return GST_VIDEO_MULTIVIEW_MODE_NONE;
1060 }
1061
1062 static gboolean
1063 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1064     AVCodecContext * context, AVFrame * picture)
1065 {
1066   GstVideoFormat fmt;
1067   GstVideoInfo *in_info, *out_info;
1068   GstVideoCodecState *output_state;
1069   gint fps_n, fps_d;
1070   GstClockTime latency;
1071   GstStructure *in_s;
1072
1073   if (!update_video_context (ffmpegdec, context, picture))
1074     return TRUE;
1075
1076   fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1077   if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1078     goto unknown_format;
1079
1080   output_state =
1081       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1082       ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1083   if (ffmpegdec->output_state)
1084     gst_video_codec_state_unref (ffmpegdec->output_state);
1085   ffmpegdec->output_state = output_state;
1086
1087   in_info = &ffmpegdec->input_state->info;
1088   out_info = &ffmpegdec->output_state->info;
1089
1090   /* set the interlaced flag */
1091   in_s = gst_caps_get_structure (ffmpegdec->input_state->caps, 0);
1092
1093   if (!gst_structure_has_field (in_s, "interlace-mode")) {
1094     if (ffmpegdec->pic_interlaced) {
1095       if (ffmpegdec->pic_field_order_changed ||
1096           (ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_RFF)) {
1097         out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1098       } else {
1099         out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1100         if ((ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_TFF))
1101           GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1102               GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
1103         else
1104           GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1105               GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST;
1106       }
1107     } else {
1108       out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1109     }
1110   }
1111
1112   if (!gst_structure_has_field (in_s, "chroma-site")) {
1113     switch (context->chroma_sample_location) {
1114       case AVCHROMA_LOC_LEFT:
1115         out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1116         break;
1117       case AVCHROMA_LOC_CENTER:
1118         out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1119         break;
1120       case AVCHROMA_LOC_TOPLEFT:
1121         out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1122         break;
1123       case AVCHROMA_LOC_TOP:
1124         out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1125         break;
1126       default:
1127         break;
1128     }
1129   }
1130
1131   if (!gst_structure_has_field (in_s, "colorimetry")
1132       || in_info->colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN) {
1133     switch (context->color_primaries) {
1134       case AVCOL_PRI_BT709:
1135         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1136         break;
1137       case AVCOL_PRI_BT470M:
1138         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1139         break;
1140       case AVCOL_PRI_BT470BG:
1141         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1142         break;
1143       case AVCOL_PRI_SMPTE170M:
1144         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1145         break;
1146       case AVCOL_PRI_SMPTE240M:
1147         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1148         break;
1149       case AVCOL_PRI_FILM:
1150         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
1151         break;
1152       case AVCOL_PRI_BT2020:
1153         out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1154         break;
1155       default:
1156         break;
1157     }
1158   }
1159
1160   if (!gst_structure_has_field (in_s, "colorimetry")
1161       || in_info->colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN) {
1162     switch (context->color_trc) {
1163       case AVCOL_TRC_BT2020_10:
1164       case AVCOL_TRC_BT709:
1165       case AVCOL_TRC_SMPTE170M:
1166         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
1167         break;
1168       case AVCOL_TRC_GAMMA22:
1169         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
1170         break;
1171       case AVCOL_TRC_GAMMA28:
1172         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
1173         break;
1174       case AVCOL_TRC_SMPTE240M:
1175         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1176         break;
1177       case AVCOL_TRC_LINEAR:
1178         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
1179         break;
1180       case AVCOL_TRC_LOG:
1181         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG100;
1182         break;
1183       case AVCOL_TRC_LOG_SQRT:
1184         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG316;
1185         break;
1186       case AVCOL_TRC_BT2020_12:
1187         out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT2020_12;
1188         break;
1189       default:
1190         break;
1191     }
1192   }
1193
1194   if (!gst_structure_has_field (in_s, "colorimetry")
1195       || in_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
1196     switch (context->colorspace) {
1197       case AVCOL_SPC_RGB:
1198         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
1199         break;
1200       case AVCOL_SPC_BT709:
1201         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1202         break;
1203       case AVCOL_SPC_FCC:
1204         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
1205         break;
1206       case AVCOL_SPC_BT470BG:
1207       case AVCOL_SPC_SMPTE170M:
1208         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1209         break;
1210       case AVCOL_SPC_SMPTE240M:
1211         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1212         break;
1213       case AVCOL_SPC_BT2020_NCL:
1214         out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1215         break;
1216       default:
1217         break;
1218     }
1219   }
1220
1221   if (!gst_structure_has_field (in_s, "colorimetry")
1222       || in_info->colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN) {
1223     if (context->color_range == AVCOL_RANGE_JPEG) {
1224       out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
1225     } else {
1226       out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
1227     }
1228   }
1229
1230   /* try to find a good framerate */
1231   if ((in_info->fps_d && in_info->fps_n) ||
1232       GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1233     /* take framerate from input when it was specified (#313970) */
1234     fps_n = in_info->fps_n;
1235     fps_d = in_info->fps_d;
1236   } else {
1237     fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1238     fps_d = ffmpegdec->ctx_time_n;
1239
1240     if (!fps_d) {
1241       GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1242           fps_n);
1243       fps_d = 1;
1244     }
1245     if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1246       GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1247           fps_d);
1248       fps_n = 0;
1249       fps_d = 1;
1250     }
1251   }
1252
1253   GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1254   out_info->fps_n = fps_n;
1255   out_info->fps_d = fps_d;
1256
1257   /* calculate and update par now */
1258   gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1259
1260   GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) = ffmpegdec->cur_multiview_mode;
1261   GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) = ffmpegdec->cur_multiview_flags;
1262
1263   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1264     goto negotiate_failed;
1265
1266   /* The decoder is configured, we now know the true latency */
1267   if (fps_n) {
1268     latency =
1269         gst_util_uint64_scale_ceil (ffmpegdec->context->has_b_frames *
1270         GST_SECOND, fps_d, fps_n);
1271     gst_video_decoder_set_latency (GST_VIDEO_DECODER (ffmpegdec), latency,
1272         latency);
1273   }
1274
1275   return TRUE;
1276
1277   /* ERRORS */
1278 unknown_format:
1279   {
1280     GST_ERROR_OBJECT (ffmpegdec,
1281         "decoder requires a video format unsupported by GStreamer");
1282     return FALSE;
1283   }
1284 negotiate_failed:
1285   {
1286     /* Reset so we try again next time even if force==FALSE */
1287     ffmpegdec->pic_pix_fmt = 0;
1288     ffmpegdec->pic_width = 0;
1289     ffmpegdec->pic_height = 0;
1290     ffmpegdec->pic_par_n = 0;
1291     ffmpegdec->pic_par_d = 0;
1292     ffmpegdec->pic_interlaced = 0;
1293     ffmpegdec->pic_field_order = 0;
1294     ffmpegdec->pic_field_order_changed = FALSE;
1295     ffmpegdec->ctx_ticks = 0;
1296     ffmpegdec->ctx_time_n = 0;
1297     ffmpegdec->ctx_time_d = 0;
1298
1299     GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1300     return FALSE;
1301   }
1302 }
1303
1304 /* perform qos calculations before decoding the next frame.
1305  *
1306  * Sets the skip_frame flag and if things are really bad, skips to the next
1307  * keyframe.
1308  *
1309  */
1310 static void
1311 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1312     GstVideoCodecFrame * frame, gboolean * mode_switch)
1313 {
1314   GstClockTimeDiff diff;
1315   GstSegmentFlags skip_flags =
1316       GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1317
1318   *mode_switch = FALSE;
1319
1320   if (frame == NULL)
1321     return;
1322
1323   if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1324     ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1325     *mode_switch = TRUE;
1326   } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1327     ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1328     *mode_switch = TRUE;
1329   }
1330
1331   if (*mode_switch == TRUE) {
1332     /* We've already switched mode, we can return straight away
1333      * without any further calculation */
1334     return;
1335   }
1336
1337   diff =
1338       gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1339       frame);
1340
1341   /* if we don't have timing info, then we don't do QoS */
1342   if (G_UNLIKELY (diff == G_MAXINT64)) {
1343     /* Ensure the skipping strategy is the default one */
1344     ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
1345     return;
1346   }
1347
1348   GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1349
1350   if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1351     ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1352     *mode_switch = TRUE;
1353     GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1354   }
1355
1356   else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1357     ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1358     *mode_switch = TRUE;
1359     GST_DEBUG_OBJECT (ffmpegdec,
1360         "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1361   }
1362 }
1363
1364 /* get an outbuf buffer with the current picture */
1365 static GstFlowReturn
1366 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1367 {
1368   GstFlowReturn ret = GST_FLOW_OK;
1369   AVFrame pic, *outpic;
1370   GstVideoFrame vframe;
1371   GstVideoInfo *info;
1372   gint c;
1373
1374   GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1375
1376   if (!ffmpegdec->output_state)
1377     goto not_negotiated;
1378
1379   ret =
1380       gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1381       frame);
1382   if (G_UNLIKELY (ret != GST_FLOW_OK))
1383     goto alloc_failed;
1384
1385   /* original ffmpeg code does not handle odd sizes correctly.
1386    * This patched up version does */
1387   /* Fill avpicture */
1388   info = &ffmpegdec->output_state->info;
1389   if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1390           GST_MAP_READ | GST_MAP_WRITE))
1391     goto map_failed;
1392
1393   memset (&pic, 0, sizeof (pic));
1394   pic.format = ffmpegdec->pic_pix_fmt;
1395   pic.width = GST_VIDEO_FRAME_WIDTH (&vframe);
1396   pic.height = GST_VIDEO_FRAME_HEIGHT (&vframe);
1397   for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1398     if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1399       pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1400       pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1401       GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1402           pic.linesize[c], pic.data[c]);
1403     } else {
1404       pic.data[c] = NULL;
1405       pic.linesize[c] = 0;
1406     }
1407   }
1408
1409   outpic = ffmpegdec->picture;
1410
1411   if (av_frame_copy (&pic, outpic) != 0) {
1412     GST_ERROR_OBJECT (ffmpegdec, "Failed to copy output frame");
1413     ret = GST_FLOW_ERROR;
1414   }
1415
1416   gst_video_frame_unmap (&vframe);
1417
1418   ffmpegdec->picture->reordered_opaque = -1;
1419
1420   return ret;
1421
1422   /* special cases */
1423 alloc_failed:
1424   {
1425     GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
1426         ("Unable to allocate memory"),
1427         ("The downstream pool failed to allocated buffer."));
1428     return ret;
1429   }
1430 map_failed:
1431   {
1432     GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
1433         ("Cannot access memory for read and write operation."),
1434         ("The video memory allocated from downstream pool could not mapped for"
1435             "read and write."));
1436     return ret;
1437   }
1438 not_negotiated:
1439   {
1440     GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1441     return GST_FLOW_NOT_NEGOTIATED;
1442   }
1443 }
1444
1445 static void
1446 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1447 {
1448   memset (packet, 0, sizeof (AVPacket));
1449   packet->data = data;
1450   packet->size = size;
1451 }
1452
1453 /*
1454  * Returns: whether a frame was decoded
1455  */
1456 static gboolean
1457 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1458     GstVideoCodecFrame * frame, GstFlowReturn * ret)
1459 {
1460   gint res;
1461   gboolean got_frame = FALSE;
1462   gboolean mode_switch;
1463   GstVideoCodecFrame *out_frame;
1464   GstFFMpegVidDecVideoFrame *out_dframe;
1465   GstBufferPool *pool;
1466
1467   *ret = GST_FLOW_OK;
1468
1469   /* in case we skip frames */
1470   ffmpegdec->picture->pict_type = -1;
1471
1472   /* run QoS code, we don't stop decoding the frame when we are late because
1473    * else we might skip a reference frame */
1474   gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1475
1476   res = avcodec_receive_frame (ffmpegdec->context, ffmpegdec->picture);
1477
1478   /* No frames available at this time */
1479   if (res == AVERROR (EAGAIN))
1480     goto beach;
1481   else if (res == AVERROR_EOF) {        /* Should not happen */
1482     *ret = GST_FLOW_EOS;
1483     GST_WARNING_OBJECT (ffmpegdec,
1484         "Tried to receive frame on a flushed context");
1485     goto beach;
1486   } else if (res < 0) {
1487     *ret = GST_FLOW_ERROR;
1488     GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1489         ("Legitimate decoding error"));
1490     goto beach;
1491   }
1492
1493   got_frame = TRUE;
1494
1495   /* get the output picture timing info again */
1496   out_dframe = ffmpegdec->picture->opaque;
1497   out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1498
1499   /* also give back a buffer allocated by the frame, if any */
1500   gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1501   gst_buffer_replace (&out_dframe->buffer, NULL);
1502
1503   /* Extract auxilliary info not stored in the main AVframe */
1504   {
1505     GstVideoInfo *in_info = &ffmpegdec->input_state->info;
1506     /* Take multiview mode from upstream if present */
1507     ffmpegdec->picture_multiview_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1508     ffmpegdec->picture_multiview_flags =
1509         GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1510
1511     /* Otherwise, see if there's info in the frame */
1512     if (ffmpegdec->picture_multiview_mode == GST_VIDEO_MULTIVIEW_MODE_NONE) {
1513       AVFrameSideData *side_data =
1514           av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_STEREO3D);
1515       if (side_data) {
1516         AVStereo3D *stereo = (AVStereo3D *) side_data->data;
1517         ffmpegdec->picture_multiview_mode = stereo_av_to_gst (stereo->type);
1518         if (stereo->flags & AV_STEREO3D_FLAG_INVERT) {
1519           ffmpegdec->picture_multiview_flags =
1520               GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
1521         } else {
1522           ffmpegdec->picture_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
1523         }
1524       }
1525     }
1526   }
1527
1528   GST_DEBUG_OBJECT (ffmpegdec,
1529       "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1530       out_frame->pts, out_frame->duration);
1531   GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1532       (guint64) ffmpegdec->picture->pts);
1533   GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1534       ffmpegdec->picture->coded_picture_number);
1535   GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1536       ffmpegdec->picture->display_picture_number);
1537   GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1538       ffmpegdec->picture->opaque);
1539   GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1540       (guint64) ffmpegdec->picture->reordered_opaque);
1541   GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1542       ffmpegdec->picture->repeat_pict);
1543   GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1544       ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1545
1546   if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1547           ffmpegdec->picture))
1548     goto negotiation_error;
1549
1550   pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1551   if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1552     *ret = get_output_buffer (ffmpegdec, out_frame);
1553   } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1554     GstBuffer *tmp = out_frame->output_buffer;
1555     out_frame->output_buffer = NULL;
1556     *ret = get_output_buffer (ffmpegdec, out_frame);
1557     gst_buffer_unref (tmp);
1558   }
1559 #ifndef G_DISABLE_ASSERT
1560   else {
1561     GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1562     if (vmeta) {
1563       GstVideoInfo *info = &ffmpegdec->output_state->info;
1564       g_assert (vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1565       g_assert (vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1566     }
1567   }
1568 #endif
1569   gst_object_unref (pool);
1570
1571   if (G_UNLIKELY (*ret != GST_FLOW_OK))
1572     goto no_output;
1573
1574   /* Mark corrupted frames as corrupted */
1575   if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1576     GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1577
1578   if (ffmpegdec->pic_interlaced) {
1579     /* set interlaced flags */
1580     if (ffmpegdec->picture->repeat_pict)
1581       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1582     if (ffmpegdec->picture->top_field_first)
1583       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1584     if (ffmpegdec->picture->interlaced_frame)
1585       GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1586           GST_VIDEO_BUFFER_FLAG_INTERLACED);
1587   }
1588
1589   {
1590     AVFrameSideData *side_data =
1591         av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_A53_CC);
1592     if (side_data) {
1593       GST_LOG_OBJECT (ffmpegdec, "Found CC of size %d", side_data->size);
1594       GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
1595       out_frame->output_buffer =
1596           gst_buffer_make_writable (out_frame->output_buffer);
1597       gst_buffer_add_video_caption_meta (out_frame->output_buffer,
1598           GST_VIDEO_CAPTION_TYPE_CEA708_RAW, side_data->data, side_data->size);
1599     }
1600   }
1601
1602   /* cleaning time */
1603   /* so we decoded this frame, frames preceding it in decoding order
1604    * that still do not have a buffer allocated seem rather useless,
1605    * and can be discarded, due to e.g. misparsed bogus frame
1606    * or non-keyframe in skipped decoding, ...
1607    * In any case, not likely to be seen again, so discard those,
1608    * before they pile up and/or mess with timestamping */
1609   {
1610     GList *l, *ol;
1611     GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1612     gboolean old = TRUE;
1613
1614     ol = l = gst_video_decoder_get_frames (dec);
1615     while (l) {
1616       GstVideoCodecFrame *tmp = l->data;
1617
1618       if (tmp == frame)
1619         old = FALSE;
1620
1621       if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1622         GST_LOG_OBJECT (dec,
1623             "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1624             GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1625             GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1626         /* drop extra ref and remove from frame list */
1627         gst_video_decoder_release_frame (dec, tmp);
1628       } else {
1629         /* drop extra ref we got */
1630         gst_video_codec_frame_unref (tmp);
1631       }
1632       l = l->next;
1633     }
1634     g_list_free (ol);
1635   }
1636
1637   av_frame_unref (ffmpegdec->picture);
1638
1639   /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1640    * libav might still have a reference to it!
1641    */
1642   *ret =
1643       gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1644
1645 beach:
1646   GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, got frame: %d",
1647       gst_flow_get_name (*ret), got_frame);
1648   return got_frame;
1649
1650   /* special cases */
1651 no_output:
1652   {
1653     GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1654     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1655     goto beach;
1656   }
1657
1658 negotiation_error:
1659   {
1660     if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1661       *ret = GST_FLOW_FLUSHING;
1662       goto beach;
1663     }
1664     GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1665     *ret = GST_FLOW_NOT_NEGOTIATED;
1666     goto beach;
1667   }
1668 }
1669
1670
1671  /* Returns: Whether a frame was decoded */
1672 static gboolean
1673 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame,
1674     GstFlowReturn * ret)
1675 {
1676   gboolean got_frame = FALSE;
1677
1678   if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1679     goto no_codec;
1680
1681   *ret = GST_FLOW_OK;
1682   ffmpegdec->context->frame_number++;
1683
1684   got_frame = gst_ffmpegviddec_video_frame (ffmpegdec, frame, ret);
1685
1686   return got_frame;
1687
1688   /* ERRORS */
1689 no_codec:
1690   {
1691     GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1692     *ret = GST_FLOW_NOT_NEGOTIATED;
1693     return -1;
1694   }
1695 }
1696
1697 static GstFlowReturn
1698 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1699 {
1700   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1701   GstFFMpegVidDecClass *oclass;
1702
1703   if (!ffmpegdec->opened)
1704     return GST_FLOW_OK;
1705
1706   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1707
1708   if (oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) {
1709     GstFlowReturn ret;
1710     gboolean got_frame = FALSE;
1711
1712     GST_LOG_OBJECT (ffmpegdec,
1713         "codec has delay capabilities, calling until ffmpeg has drained everything");
1714
1715     do {
1716       got_frame = gst_ffmpegviddec_frame (ffmpegdec, NULL, &ret);
1717     } while (got_frame && ret == GST_FLOW_OK);
1718   }
1719
1720   return GST_FLOW_OK;
1721 }
1722
1723 static GstFlowReturn
1724 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1725     GstVideoCodecFrame * frame)
1726 {
1727   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1728   guint8 *data;
1729   gint size;
1730   gboolean got_frame;
1731   GstMapInfo minfo;
1732   GstFlowReturn ret = GST_FLOW_OK;
1733   AVPacket packet;
1734
1735   GST_LOG_OBJECT (ffmpegdec,
1736       "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1737       ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1738       gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1739       GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1740
1741   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1742     GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1743         ("Failed to map buffer for reading"));
1744     return GST_FLOW_ERROR;
1745   }
1746
1747   /* treat frame as void until a buffer is requested for it */
1748   GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1749       GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1750
1751   data = minfo.data;
1752   size = minfo.size;
1753
1754   if (size > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1755           || (minfo.maxsize - minfo.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
1756     /* add padding */
1757     if (ffmpegdec->padded_size < size + AV_INPUT_BUFFER_PADDING_SIZE) {
1758       ffmpegdec->padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
1759       ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1760       GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1761           ffmpegdec->padded_size);
1762     }
1763     GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1764         "Copy input to add padding");
1765     memcpy (ffmpegdec->padded, data, size);
1766     memset (ffmpegdec->padded + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1767
1768     data = ffmpegdec->padded;
1769   }
1770
1771   /* now decode the frame */
1772   gst_avpacket_init (&packet, data, size);
1773
1774   if (ffmpegdec->palette) {
1775     guint8 *pal;
1776
1777     pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1778         AVPALETTE_SIZE);
1779     gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1780     GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1781   }
1782
1783   if (!packet.size)
1784     goto done;
1785
1786   if (frame) {
1787     /* save reference to the timing info */
1788     ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1789     ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1790
1791     GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1792         frame->system_frame_number);
1793   }
1794
1795   /* This might call into get_buffer() from another thread,
1796    * which would cause a deadlock. Release the lock here
1797    * and taking it again later seems safe
1798    * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1799    */
1800   GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1801   if (avcodec_send_packet (ffmpegdec->context, &packet) < 0) {
1802     GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1803     goto send_packet_failed;
1804   }
1805   GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1806
1807   do {
1808     /* decode a frame of audio/video now */
1809     got_frame = gst_ffmpegviddec_frame (ffmpegdec, frame, &ret);
1810
1811     if (ret != GST_FLOW_OK) {
1812       GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1813           gst_flow_get_name (ret));
1814       break;
1815     }
1816   } while (got_frame);
1817
1818 done:
1819   gst_buffer_unmap (frame->input_buffer, &minfo);
1820   gst_video_codec_frame_unref (frame);
1821
1822   return ret;
1823
1824 send_packet_failed:
1825   {
1826     GST_WARNING_OBJECT (ffmpegdec, "Failed to send data for decoding");
1827     goto done;
1828   }
1829 }
1830
1831 static gboolean
1832 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1833 {
1834   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1835   GstFFMpegVidDecClass *oclass;
1836
1837   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1838
1839   GST_OBJECT_LOCK (ffmpegdec);
1840   gst_ffmpeg_avcodec_close (ffmpegdec->context);
1841   if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1842     GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1843     GST_OBJECT_UNLOCK (ffmpegdec);
1844     return FALSE;
1845   }
1846   ffmpegdec->context->opaque = ffmpegdec;
1847   GST_OBJECT_UNLOCK (ffmpegdec);
1848
1849   return TRUE;
1850 }
1851
1852 static gboolean
1853 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1854 {
1855   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1856
1857   GST_OBJECT_LOCK (ffmpegdec);
1858   gst_ffmpegviddec_close (ffmpegdec, FALSE);
1859   GST_OBJECT_UNLOCK (ffmpegdec);
1860   g_free (ffmpegdec->padded);
1861   ffmpegdec->padded = NULL;
1862   ffmpegdec->padded_size = 0;
1863   if (ffmpegdec->input_state)
1864     gst_video_codec_state_unref (ffmpegdec->input_state);
1865   ffmpegdec->input_state = NULL;
1866   if (ffmpegdec->output_state)
1867     gst_video_codec_state_unref (ffmpegdec->output_state);
1868   ffmpegdec->output_state = NULL;
1869
1870   if (ffmpegdec->internal_pool)
1871     gst_object_unref (ffmpegdec->internal_pool);
1872   ffmpegdec->internal_pool = NULL;
1873
1874   ffmpegdec->pic_pix_fmt = 0;
1875   ffmpegdec->pic_width = 0;
1876   ffmpegdec->pic_height = 0;
1877   ffmpegdec->pic_par_n = 0;
1878   ffmpegdec->pic_par_d = 0;
1879   ffmpegdec->pic_interlaced = 0;
1880   ffmpegdec->pic_field_order = 0;
1881   ffmpegdec->pic_field_order_changed = FALSE;
1882   ffmpegdec->ctx_ticks = 0;
1883   ffmpegdec->ctx_time_n = 0;
1884   ffmpegdec->ctx_time_d = 0;
1885
1886   ffmpegdec->pool_width = 0;
1887   ffmpegdec->pool_height = 0;
1888   ffmpegdec->pool_format = 0;
1889
1890   return TRUE;
1891 }
1892
1893 static GstFlowReturn
1894 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1895 {
1896   gst_ffmpegviddec_drain (decoder);
1897   /* note that finish can and should clean up more drastically,
1898    * but drain is also invoked on e.g. packet loss in GAP handling */
1899   gst_ffmpegviddec_flush (decoder);
1900
1901   return GST_FLOW_OK;
1902 }
1903
1904 static gboolean
1905 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1906 {
1907   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1908
1909   if (ffmpegdec->opened) {
1910     GST_LOG_OBJECT (decoder, "flushing buffers");
1911     avcodec_flush_buffers (ffmpegdec->context);
1912   }
1913
1914   return TRUE;
1915 }
1916
1917 static gboolean
1918 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1919 {
1920   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1921   GstVideoCodecState *state;
1922   GstBufferPool *pool;
1923   guint size, min, max;
1924   GstStructure *config;
1925   gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1926   GstAllocator *allocator = NULL;
1927   GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1928
1929   have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1930
1931   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1932           query))
1933     return FALSE;
1934
1935   state = gst_video_decoder_get_output_state (decoder);
1936
1937   if (gst_query_get_n_allocation_params (query) > 0) {
1938     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
1939     params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1940   } else {
1941     gst_query_add_allocation_param (query, allocator, &params);
1942   }
1943
1944   gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1945
1946   /* Don't use pool that can't grow, as we don't know how many buffer we'll
1947    * need, otherwise we may stall */
1948   if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1949     gst_object_unref (pool);
1950     pool = gst_video_buffer_pool_new ();
1951     max = 0;
1952     update_pool = TRUE;
1953     have_pool = FALSE;
1954
1955     /* if there is an allocator, also drop it, as it might be the reason we
1956      * have this limit. Default will be used */
1957     if (allocator) {
1958       gst_object_unref (allocator);
1959       allocator = NULL;
1960     }
1961   }
1962
1963   config = gst_buffer_pool_get_config (pool);
1964   gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1965   gst_buffer_pool_config_set_allocator (config, allocator, &params);
1966
1967   have_videometa =
1968       gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1969
1970   if (have_videometa)
1971     gst_buffer_pool_config_add_option (config,
1972         GST_BUFFER_POOL_OPTION_VIDEO_META);
1973
1974   have_alignment =
1975       gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1976
1977   /* If we have videometa, we never have to copy */
1978   if (have_videometa && have_pool && have_alignment &&
1979       gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
1980     GstStructure *config_copy = gst_structure_copy (config);
1981
1982     gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
1983         config_copy);
1984
1985     /* FIXME validate and retry */
1986     if (gst_buffer_pool_set_config (pool, config_copy)) {
1987       GstFlowReturn ret;
1988       GstBuffer *tmp;
1989
1990       gst_buffer_pool_set_active (pool, TRUE);
1991       ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
1992       if (ret == GST_FLOW_OK) {
1993         GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
1994         gboolean same_stride = TRUE;
1995         gint i;
1996
1997         for (i = 0; i < vmeta->n_planes; i++) {
1998           if (vmeta->stride[i] != ffmpegdec->stride[i]) {
1999             same_stride = FALSE;
2000             break;
2001           }
2002         }
2003
2004         gst_buffer_unref (tmp);
2005
2006         if (same_stride) {
2007           if (ffmpegdec->internal_pool)
2008             gst_object_unref (ffmpegdec->internal_pool);
2009           ffmpegdec->internal_pool = gst_object_ref (pool);
2010           ffmpegdec->pool_info = state->info;
2011           gst_structure_free (config);
2012           goto done;
2013         }
2014       }
2015     }
2016   }
2017
2018   if (have_videometa && ffmpegdec->internal_pool
2019       && ffmpegdec->pool_width == state->info.width
2020       && ffmpegdec->pool_height == state->info.height) {
2021     update_pool = TRUE;
2022     gst_object_unref (pool);
2023     pool = gst_object_ref (ffmpegdec->internal_pool);
2024     gst_structure_free (config);
2025     goto done;
2026   }
2027
2028   /* configure */
2029   if (!gst_buffer_pool_set_config (pool, config)) {
2030     gboolean working_pool = FALSE;
2031     config = gst_buffer_pool_get_config (pool);
2032
2033     if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
2034             max)) {
2035       working_pool = gst_buffer_pool_set_config (pool, config);
2036     } else {
2037       gst_structure_free (config);
2038     }
2039
2040     if (!working_pool) {
2041       gst_object_unref (pool);
2042       pool = gst_video_buffer_pool_new ();
2043       config = gst_buffer_pool_get_config (pool);
2044       gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2045       gst_buffer_pool_config_set_allocator (config, NULL, &params);
2046       gst_buffer_pool_set_config (pool, config);
2047       update_pool = TRUE;
2048     }
2049   }
2050
2051 done:
2052   /* and store */
2053   if (update_pool)
2054     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2055
2056   gst_object_unref (pool);
2057   if (allocator)
2058     gst_object_unref (allocator);
2059   gst_video_codec_state_unref (state);
2060
2061   return TRUE;
2062 }
2063
2064 static gboolean
2065 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
2066     GstQuery * query)
2067 {
2068   GstAllocationParams params;
2069
2070   gst_allocation_params_init (&params);
2071   params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
2072   params.align = DEFAULT_STRIDE_ALIGN;
2073   params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
2074   /* we would like to have some padding so that we don't have to
2075    * memcpy. We don't suggest an allocator. */
2076   gst_query_add_allocation_param (query, NULL, &params);
2077
2078   return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
2079       query);
2080 }
2081
2082 static void
2083 gst_ffmpegviddec_set_property (GObject * object,
2084     guint prop_id, const GValue * value, GParamSpec * pspec)
2085 {
2086   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2087
2088   switch (prop_id) {
2089     case PROP_LOWRES:
2090       ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
2091       break;
2092     case PROP_SKIPFRAME:
2093       ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
2094           g_value_get_enum (value);
2095       break;
2096     case PROP_DIRECT_RENDERING:
2097       ffmpegdec->direct_rendering = g_value_get_boolean (value);
2098       break;
2099     case PROP_DEBUG_MV:
2100       ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
2101           g_value_get_boolean (value);
2102       break;
2103     case PROP_MAX_THREADS:
2104       ffmpegdec->max_threads = g_value_get_int (value);
2105       break;
2106     case PROP_OUTPUT_CORRUPT:
2107       ffmpegdec->output_corrupt = g_value_get_boolean (value);
2108       break;
2109     default:
2110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2111       break;
2112   }
2113 }
2114
2115 static void
2116 gst_ffmpegviddec_get_property (GObject * object,
2117     guint prop_id, GValue * value, GParamSpec * pspec)
2118 {
2119   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2120
2121   switch (prop_id) {
2122     case PROP_LOWRES:
2123       g_value_set_enum (value, ffmpegdec->context->lowres);
2124       break;
2125     case PROP_SKIPFRAME:
2126       g_value_set_enum (value, ffmpegdec->context->skip_frame);
2127       break;
2128     case PROP_DIRECT_RENDERING:
2129       g_value_set_boolean (value, ffmpegdec->direct_rendering);
2130       break;
2131     case PROP_DEBUG_MV:
2132       g_value_set_boolean (value, ffmpegdec->context->debug_mv);
2133       break;
2134     case PROP_MAX_THREADS:
2135       g_value_set_int (value, ffmpegdec->max_threads);
2136       break;
2137     case PROP_OUTPUT_CORRUPT:
2138       g_value_set_boolean (value, ffmpegdec->output_corrupt);
2139       break;
2140     default:
2141       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2142       break;
2143   }
2144 }
2145
2146 gboolean
2147 gst_ffmpegviddec_register (GstPlugin * plugin)
2148 {
2149   GTypeInfo typeinfo = {
2150     sizeof (GstFFMpegVidDecClass),
2151     (GBaseInitFunc) gst_ffmpegviddec_base_init,
2152     NULL,
2153     (GClassInitFunc) gst_ffmpegviddec_class_init,
2154     NULL,
2155     NULL,
2156     sizeof (GstFFMpegVidDec),
2157     0,
2158     (GInstanceInitFunc) gst_ffmpegviddec_init,
2159   };
2160   GType type;
2161   AVCodec *in_plugin;
2162   gint rank;
2163   void *i = 0;
2164
2165   GST_LOG ("Registering decoders");
2166
2167   while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
2168     gchar *type_name;
2169     gchar *plugin_name;
2170
2171     /* only video decoders */
2172     if (!av_codec_is_decoder (in_plugin)
2173         || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2174       continue;
2175
2176     /* no quasi codecs, please */
2177     if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2178         in_plugin->id == AV_CODEC_ID_V210 ||
2179         in_plugin->id == AV_CODEC_ID_V210X ||
2180         in_plugin->id == AV_CODEC_ID_V308 ||
2181         in_plugin->id == AV_CODEC_ID_V408 ||
2182         in_plugin->id == AV_CODEC_ID_V410 ||
2183         in_plugin->id == AV_CODEC_ID_R210
2184         || in_plugin->id == AV_CODEC_ID_AYUV
2185         || in_plugin->id == AV_CODEC_ID_Y41P
2186         || in_plugin->id == AV_CODEC_ID_012V
2187         || in_plugin->id == AV_CODEC_ID_YUV4
2188 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
2189         AV_VERSION_INT (57,4,0)
2190         || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
2191 #endif
2192         || in_plugin->id == AV_CODEC_ID_ZLIB) {
2193       continue;
2194     }
2195
2196     /* No decoders depending on external libraries (we don't build them, but
2197      * people who build against an external ffmpeg might have them.
2198      * We have native gstreamer plugins for all of those libraries anyway. */
2199     if (!strncmp (in_plugin->name, "lib", 3)) {
2200       GST_DEBUG
2201           ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2202           in_plugin->name);
2203       continue;
2204     }
2205
2206     /* No vdpau plugins until we can figure out how to properly use them
2207      * outside of ffmpeg. */
2208     if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2209       GST_DEBUG
2210           ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2211           in_plugin->name);
2212       continue;
2213     }
2214
2215     if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2216       GST_DEBUG
2217           ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2218           in_plugin->name);
2219       continue;
2220     }
2221
2222     if (strstr (in_plugin->name, "vaapi")) {
2223       GST_DEBUG
2224           ("Ignoring VAAPI decoder %s. We can't handle this outside of ffmpeg",
2225           in_plugin->name);
2226       continue;
2227     }
2228
2229     if (g_str_has_suffix (in_plugin->name, "_qsv")) {
2230       GST_DEBUG
2231           ("Ignoring qsv decoder %s. We can't handle this outside of ffmpeg",
2232           in_plugin->name);
2233       continue;
2234     }
2235
2236     if (g_str_has_suffix (in_plugin->name, "_cuvid")) {
2237       GST_DEBUG
2238           ("Ignoring CUVID decoder %s. We can't handle this outside of ffmpeg",
2239           in_plugin->name);
2240       continue;
2241     }
2242
2243     if (g_str_has_suffix (in_plugin->name, "_v4l2m2m")) {
2244       GST_DEBUG
2245           ("Ignoring V4L2 mem-to-mem decoder %s. We can't handle this outside of ffmpeg",
2246           in_plugin->name);
2247       continue;
2248     }
2249
2250     GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2251
2252     /* no codecs for which we're GUARANTEED to have better alternatives */
2253     /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2254     /* MP1 : Use MP3 for decoding */
2255     /* MP2 : Use MP3 for decoding */
2256     /* Theora: Use libtheora based theoradec */
2257     if (!strcmp (in_plugin->name, "theora") ||
2258         !strcmp (in_plugin->name, "mpeg1video") ||
2259         strstr (in_plugin->name, "crystalhd") != NULL ||
2260         !strcmp (in_plugin->name, "ass") ||
2261         !strcmp (in_plugin->name, "srt") ||
2262         !strcmp (in_plugin->name, "pgssub") ||
2263         !strcmp (in_plugin->name, "dvdsub") ||
2264         !strcmp (in_plugin->name, "dvbsub")) {
2265       GST_LOG ("Ignoring decoder %s", in_plugin->name);
2266       continue;
2267     }
2268
2269     /* construct the type */
2270     if (!strcmp (in_plugin->name, "hevc")) {
2271       plugin_name = g_strdup ("h265");
2272     } else {
2273       plugin_name = g_strdup ((gchar *) in_plugin->name);
2274     }
2275     g_strdelimit (plugin_name, NULL, '_');
2276     type_name = g_strdup_printf ("avdec_%s", plugin_name);
2277     g_free (plugin_name);
2278
2279     type = g_type_from_name (type_name);
2280
2281     if (!type) {
2282       /* create the gtype now */
2283       type =
2284           g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2285           0);
2286       g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2287     }
2288
2289     /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2290      * tested and by far outperforms divxdec/xviddec - so we prefer it.
2291      * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2292      * VC1/WMV3 are not working and thus unpreferred for now. */
2293     switch (in_plugin->id) {
2294       case AV_CODEC_ID_MPEG1VIDEO:
2295       case AV_CODEC_ID_MPEG2VIDEO:
2296       case AV_CODEC_ID_MPEG4:
2297       case AV_CODEC_ID_MSMPEG4V3:
2298       case AV_CODEC_ID_H264:
2299       case AV_CODEC_ID_HEVC:
2300       case AV_CODEC_ID_RV10:
2301       case AV_CODEC_ID_RV20:
2302       case AV_CODEC_ID_RV30:
2303       case AV_CODEC_ID_RV40:
2304         rank = GST_RANK_PRIMARY;
2305         break;
2306         /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2307          * They say libdv's quality is better though. leave as secondary.
2308          * note: if you change this, see the code in gstdv.c in good/ext/dv.
2309          */
2310       case AV_CODEC_ID_DVVIDEO:
2311         rank = GST_RANK_SECONDARY;
2312         break;
2313       default:
2314         rank = GST_RANK_MARGINAL;
2315         break;
2316     }
2317     if (!gst_element_register (plugin, type_name, rank, type)) {
2318       g_warning ("Failed to register %s", type_name);
2319       g_free (type_name);
2320       return FALSE;
2321     }
2322
2323     g_free (type_name);
2324   }
2325
2326   GST_LOG ("Finished Registering decoders");
2327
2328   return TRUE;
2329 }