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