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