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