Use AV_ namespace for all CODEC_ macro
[platform/upstream/gstreamer.git] / ext / libav / gstavviddec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <assert.h>
25 #include <string.h>
26
27 #include <libavcodec/avcodec.h>
28 #include <libavutil/stereo3d.h>
29
30 #include "gstav.h"
31 #include "gstavcodecmap.h"
32 #include "gstavutils.h"
33 #include "gstavviddec.h"
34
35 #define MAX_TS_MASK 0xff
36
37 #define DEFAULT_LOWRES                  0
38 #define DEFAULT_SKIPFRAME               0
39 #define DEFAULT_DIRECT_RENDERING        TRUE
40 #define DEFAULT_DEBUG_MV                FALSE
41 #define DEFAULT_MAX_THREADS             0
42 #define DEFAULT_OUTPUT_CORRUPT          TRUE
43 #define REQUIRED_POOL_MAX_BUFFERS       32
44 #define DEFAULT_STRIDE_ALIGN            31
45 #define DEFAULT_ALLOC_PARAM             { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
46
47 enum
48 {
49   PROP_0,
50   PROP_LOWRES,
51   PROP_SKIPFRAME,
52   PROP_DIRECT_RENDERING,
53   PROP_DEBUG_MV,
54   PROP_MAX_THREADS,
55   PROP_OUTPUT_CORRUPT,
56   PROP_LAST
57 };
58
59 /* A number of function prototypes are given so we can refer to them later. */
60 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
61 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
62 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
63 static void gst_ffmpegviddec_finalize (GObject * object);
64
65 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
66     GstVideoCodecState * state);
67 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
68     GstVideoCodecFrame * frame);
69 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
70 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
71 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
72 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
73     GstQuery * query);
74 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
75     GstQuery * query);
76
77 static void gst_ffmpegviddec_set_property (GObject * object,
78     guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_ffmpegviddec_get_property (GObject * object,
80     guint prop_id, GValue * value, GParamSpec * pspec);
81
82 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
83     AVCodecContext * context, AVFrame * picture);
84
85 /* some sort of bufferpool handling, but different */
86 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
87     AVFrame * picture, int flags);
88
89 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
90 static GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
91
92 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
93     AVFrame * picture);
94 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
95     AVCodecContext * context);
96
97 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
98
99 static GstElementClass *parent_class = NULL;
100
101 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
102 static GType
103 gst_ffmpegviddec_lowres_get_type (void)
104 {
105   static GType ffmpegdec_lowres_type = 0;
106
107   if (!ffmpegdec_lowres_type) {
108     static const GEnumValue ffmpegdec_lowres[] = {
109       {0, "0", "full"},
110       {1, "1", "1/2-size"},
111       {2, "2", "1/4-size"},
112       {0, NULL, NULL},
113     };
114
115     ffmpegdec_lowres_type =
116         g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
117   }
118
119   return ffmpegdec_lowres_type;
120 }
121
122 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
123 static GType
124 gst_ffmpegviddec_skipframe_get_type (void)
125 {
126   static GType ffmpegdec_skipframe_type = 0;
127
128   if (!ffmpegdec_skipframe_type) {
129     static const GEnumValue ffmpegdec_skipframe[] = {
130       {0, "0", "Skip nothing"},
131       {1, "1", "Skip B-frames"},
132       {2, "2", "Skip IDCT/Dequantization"},
133       {5, "5", "Skip everything"},
134       {0, NULL, NULL},
135     };
136
137     ffmpegdec_skipframe_type =
138         g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
139   }
140
141   return ffmpegdec_skipframe_type;
142 }
143
144 static void
145 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
146 {
147   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148   GstPadTemplate *sinktempl, *srctempl;
149   GstCaps *sinkcaps, *srccaps;
150   AVCodec *in_plugin;
151   gchar *longname, *description;
152
153   in_plugin =
154       (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
155       GST_FFDEC_PARAMS_QDATA);
156   g_assert (in_plugin != NULL);
157
158   /* construct the element details struct */
159   longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
160   description = g_strdup_printf ("libav %s decoder", in_plugin->name);
161   gst_element_class_set_metadata (element_class, longname,
162       "Codec/Decoder/Video", description,
163       "Wim Taymans <wim.taymans@gmail.com>, "
164       "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
165       "Edward Hervey <bilboed@bilboed.com>");
166   g_free (longname);
167   g_free (description);
168
169   /* get the caps */
170   sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
171   if (!sinkcaps) {
172     GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
173     sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
174   }
175   srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
176       in_plugin->id, FALSE, in_plugin);
177   if (!srccaps) {
178     GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
179     srccaps = gst_caps_from_string ("video/x-raw");
180   }
181
182   /* pad templates */
183   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
184       GST_PAD_ALWAYS, sinkcaps);
185   srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
186
187   gst_element_class_add_pad_template (element_class, srctempl);
188   gst_element_class_add_pad_template (element_class, sinktempl);
189
190   gst_caps_unref (sinkcaps);
191   gst_caps_unref (srccaps);
192
193   klass->in_plugin = in_plugin;
194 }
195
196 static void
197 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
198 {
199   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200   GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
201   int caps;
202
203   parent_class = g_type_class_peek_parent (klass);
204
205   gobject_class->finalize = gst_ffmpegviddec_finalize;
206
207   gobject_class->set_property = gst_ffmpegviddec_set_property;
208   gobject_class->get_property = gst_ffmpegviddec_get_property;
209
210   g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
211       g_param_spec_enum ("skip-frame", "Skip frames",
212           "Which types of frames to skip during decoding",
213           GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
214           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215   g_object_class_install_property (gobject_class, PROP_LOWRES,
216       g_param_spec_enum ("lowres", "Low resolution",
217           "At which resolution to decode images",
218           GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
219           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220   g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
221       g_param_spec_boolean ("direct-rendering", "Direct Rendering",
222           "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
223           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224   g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
225       g_param_spec_boolean ("debug-mv", "Debug motion vectors",
226           "Whether libav should print motion vectors on top of the image",
227           DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228   g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
229       g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
230           "Whether libav should output frames even if corrupted",
231           DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232
233   caps = klass->in_plugin->capabilities;
234   if (caps & (AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS)) {
235     g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
236         g_param_spec_int ("max-threads", "Maximum decode threads",
237             "Maximum number of worker threads to spawn. (0 = auto)",
238             0, G_MAXINT, DEFAULT_MAX_THREADS,
239             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240   }
241
242   viddec_class->set_format = gst_ffmpegviddec_set_format;
243   viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
244   viddec_class->start = gst_ffmpegviddec_start;
245   viddec_class->stop = gst_ffmpegviddec_stop;
246   viddec_class->flush = gst_ffmpegviddec_flush;
247   viddec_class->finish = gst_ffmpegviddec_finish;
248   viddec_class->drain = gst_ffmpegviddec_drain;
249   viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
250   viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
251 }
252
253 static void
254 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
255 {
256   GstFFMpegVidDecClass *klass =
257       (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
258
259   /* some ffmpeg data */
260   ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
261   ffmpegdec->context->opaque = ffmpegdec;
262   ffmpegdec->picture = av_frame_alloc ();
263   ffmpegdec->opened = FALSE;
264   ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
265   ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
266   ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
267   ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
268   ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
269
270   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
271   gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
272       (ffmpegdec), TRUE);
273
274   gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
275 }
276
277 static void
278 gst_ffmpegviddec_finalize (GObject * object)
279 {
280   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
281
282   av_frame_free (&ffmpegdec->picture);
283
284   if (ffmpegdec->context != NULL) {
285     gst_ffmpeg_avcodec_close (ffmpegdec->context);
286     av_free (ffmpegdec->context);
287     ffmpegdec->context = NULL;
288   }
289
290   G_OBJECT_CLASS (parent_class)->finalize (object);
291 }
292
293 static void
294 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
295     gboolean enable)
296 {
297   g_return_if_fail (context != NULL);
298
299   if (enable)
300     context->flags |= flags;
301   else
302     context->flags &= ~flags;
303 }
304
305 /* with LOCK */
306 static gboolean
307 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
308 {
309   GstFFMpegVidDecClass *oclass;
310   gint i;
311
312   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
313
314   GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
315
316   gst_caps_replace (&ffmpegdec->last_caps, NULL);
317
318   gst_ffmpeg_avcodec_close (ffmpegdec->context);
319   ffmpegdec->opened = FALSE;
320
321   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
322     ffmpegdec->stride[i] = -1;
323
324   gst_buffer_replace (&ffmpegdec->palette, NULL);
325
326   if (ffmpegdec->context->extradata) {
327     av_free (ffmpegdec->context->extradata);
328     ffmpegdec->context->extradata = NULL;
329   }
330   if (reset) {
331     if (avcodec_get_context_defaults3 (ffmpegdec->context,
332             oclass->in_plugin) < 0) {
333       GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
334       return FALSE;
335     }
336     ffmpegdec->context->opaque = ffmpegdec;
337   }
338   return TRUE;
339 }
340
341 /* with LOCK */
342 static gboolean
343 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
344 {
345   GstFFMpegVidDecClass *oclass;
346   gint i;
347
348   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
349
350   if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
351     goto could_not_open;
352
353   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
354     ffmpegdec->stride[i] = -1;
355
356   ffmpegdec->opened = TRUE;
357
358   GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
359       oclass->in_plugin->name, oclass->in_plugin->id);
360
361   gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
362       AV_CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
363
364   return TRUE;
365
366   /* ERRORS */
367 could_not_open:
368   {
369     gst_ffmpegviddec_close (ffmpegdec, TRUE);
370     GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
371         oclass->in_plugin->name);
372     return FALSE;
373   }
374 }
375
376 static void
377 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
378     GstVideoCodecState * state)
379 {
380   GstStructure *str = gst_caps_get_structure (state->caps, 0);
381   const GValue *palette_v;
382   GstBuffer *palette;
383
384   /* do we have a palette? */
385   if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
386     palette = gst_value_get_buffer (palette_v);
387     GST_DEBUG ("got palette data %p", palette);
388     if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
389       gst_buffer_replace (&ffmpegdec->palette, palette);
390     }
391   }
392 }
393
394
395 static gboolean
396 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
397     GstVideoCodecState * state)
398 {
399   GstFFMpegVidDec *ffmpegdec;
400   GstFFMpegVidDecClass *oclass;
401   GstClockTime latency = GST_CLOCK_TIME_NONE;
402   gboolean ret = FALSE;
403
404   ffmpegdec = (GstFFMpegVidDec *) decoder;
405   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
406
407   if (ffmpegdec->last_caps != NULL &&
408       gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
409     return TRUE;
410   }
411
412   GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
413
414   GST_OBJECT_LOCK (ffmpegdec);
415   /* stupid check for VC1 */
416   if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
417       (oclass->in_plugin->id == AV_CODEC_ID_VC1))
418     oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
419
420   /* close old session */
421   if (ffmpegdec->opened) {
422     GST_OBJECT_UNLOCK (ffmpegdec);
423     gst_ffmpegviddec_finish (decoder);
424     GST_OBJECT_LOCK (ffmpegdec);
425     if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
426       GST_OBJECT_UNLOCK (ffmpegdec);
427       return FALSE;
428     }
429     ffmpegdec->pic_pix_fmt = 0;
430     ffmpegdec->pic_width = 0;
431     ffmpegdec->pic_height = 0;
432     ffmpegdec->pic_par_n = 0;
433     ffmpegdec->pic_par_d = 0;
434     ffmpegdec->pic_interlaced = 0;
435     ffmpegdec->pic_field_order = 0;
436     ffmpegdec->pic_field_order_changed = FALSE;
437     ffmpegdec->ctx_ticks = 0;
438     ffmpegdec->ctx_time_n = 0;
439     ffmpegdec->ctx_time_d = 0;
440     ffmpegdec->cur_multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
441     ffmpegdec->cur_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
442   }
443
444   gst_caps_replace (&ffmpegdec->last_caps, state->caps);
445
446   /* set buffer functions */
447   ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
448   ffmpegdec->context->draw_horiz_band = NULL;
449
450   /* reset coded_width/_height to prevent it being reused from last time when
451    * the codec is opened again, causing a mismatch and possible
452    * segfault/corruption. (Common scenario when renegotiating caps) */
453   ffmpegdec->context->coded_width = 0;
454   ffmpegdec->context->coded_height = 0;
455
456   GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
457       ffmpegdec->context->height);
458
459   /* FIXME : Create a method that takes GstVideoCodecState instead */
460   /* get size and so */
461   gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
462       oclass->in_plugin->type, state->caps, ffmpegdec->context);
463
464   GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
465       ffmpegdec->context->height);
466
467   gst_ffmpegviddec_get_palette (ffmpegdec, state);
468
469   if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
470     GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
471     ffmpegdec->context->time_base.num = 1;
472     ffmpegdec->context->time_base.den = 25;
473   }
474
475   /* workaround encoder bugs */
476   ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
477   ffmpegdec->context->err_recognition = 1;
478
479   /* for slow cpus */
480   ffmpegdec->context->lowres = ffmpegdec->lowres;
481   ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
482
483   /* ffmpeg can draw motion vectors on top of the image (not every decoder
484    * supports it) */
485   ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
486
487   {
488     GstQuery *query;
489     gboolean is_live;
490
491     if (ffmpegdec->max_threads == 0) {
492       if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_AUTO_THREADS))
493         ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
494       else
495         ffmpegdec->context->thread_count = 0;
496     } else
497       ffmpegdec->context->thread_count = ffmpegdec->max_threads;
498
499     query = gst_query_new_latency ();
500     is_live = FALSE;
501     /* Check if upstream is live. If it isn't we can enable frame based
502      * threading, which is adding latency */
503     if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
504       gst_query_parse_latency (query, &is_live, NULL, NULL);
505     }
506     gst_query_unref (query);
507
508     if (is_live)
509       ffmpegdec->context->thread_type = FF_THREAD_SLICE;
510     else
511       ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
512   }
513
514   /* open codec - we don't select an output pix_fmt yet,
515    * simply because we don't know! We only get it
516    * during playback... */
517   if (!gst_ffmpegviddec_open (ffmpegdec))
518     goto open_failed;
519
520   if (ffmpegdec->input_state)
521     gst_video_codec_state_unref (ffmpegdec->input_state);
522   ffmpegdec->input_state = gst_video_codec_state_ref (state);
523
524   if (ffmpegdec->input_state->info.fps_n) {
525     GstVideoInfo *info = &ffmpegdec->input_state->info;
526     latency = gst_util_uint64_scale_ceil (
527         (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
528         info->fps_n);
529   }
530
531   ret = TRUE;
532
533 done:
534   GST_OBJECT_UNLOCK (ffmpegdec);
535
536   if (GST_CLOCK_TIME_IS_VALID (latency))
537     gst_video_decoder_set_latency (decoder, latency, latency);
538
539   return ret;
540
541   /* ERRORS */
542 open_failed:
543   {
544     GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
545     goto done;
546   }
547 }
548
549 typedef struct
550 {
551   GstFFMpegVidDec *ffmpegdec;
552   GstVideoCodecFrame *frame;
553   gboolean mapped;
554   GstVideoFrame vframe;
555   GstBuffer *buffer;
556   AVBufferRef *avbuffer;
557 } GstFFMpegVidDecVideoFrame;
558
559 static GstFFMpegVidDecVideoFrame *
560 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
561     GstVideoCodecFrame * frame)
562 {
563   GstFFMpegVidDecVideoFrame *dframe;
564
565   dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
566   dframe->ffmpegdec = ffmpegdec;
567   dframe->frame = frame;
568
569   GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
570
571   return dframe;
572 }
573
574 static void
575 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
576     GstFFMpegVidDecVideoFrame * frame)
577 {
578   GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
579
580   if (frame->mapped)
581     gst_video_frame_unmap (&frame->vframe);
582   gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
583   gst_buffer_replace (&frame->buffer, NULL);
584   if (frame->avbuffer) {
585     av_buffer_unref (&frame->avbuffer);
586   }
587   g_slice_free (GstFFMpegVidDecVideoFrame, frame);
588 }
589
590 static void
591 dummy_free_buffer (void *opaque, uint8_t * data)
592 {
593   GstFFMpegVidDecVideoFrame *frame = opaque;
594
595   gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
596 }
597
598 /* This function prepares the pool configuration for direct rendering. To use
599  * this method, the codec should support direct rendering and the pool should
600  * support video meta and video alignment */
601 static void
602 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
603     GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
604 {
605   GstAllocationParams params;
606   GstVideoAlignment align;
607   GstAllocator *allocator = NULL;
608   gint width, height;
609   gint linesize_align[4];
610   gint i;
611   guint edge;
612   gsize max_align;
613
614   width = GST_VIDEO_INFO_WIDTH (info);
615   height = GST_VIDEO_INFO_HEIGHT (info);
616
617   /* let ffmpeg find the alignment and padding */
618   avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
619       linesize_align);
620
621   if (ffmpegdec->context->flags & CODEC_FLAG_EMU_EDGE)
622     edge = 0;
623   else
624     edge = avcodec_get_edge_width ();
625
626   /* increase the size for the padding */
627   width += edge << 1;
628   height += edge << 1;
629
630   align.padding_top = edge;
631   align.padding_left = edge;
632   align.padding_right = width - GST_VIDEO_INFO_WIDTH (info) - edge;
633   align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info) - edge;
634
635   /* add extra padding to match libav buffer allocation sizes */
636   align.padding_bottom++;
637
638   gst_buffer_pool_config_get_allocator (config, &allocator, &params);
639
640   max_align = DEFAULT_STRIDE_ALIGN;
641   max_align |= params.align;
642
643   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
644     if (linesize_align[i] > 0)
645       max_align |= linesize_align[i] - 1;
646   }
647
648   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
649     align.stride_align[i] = max_align;
650
651   params.align = max_align;
652
653   gst_buffer_pool_config_set_allocator (config, allocator, &params);
654
655   GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
656       "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
657       GST_VIDEO_INFO_WIDTH (info),
658       GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
659       align.padding_left, align.padding_right, align.padding_bottom,
660       align.stride_align[0], align.stride_align[1], align.stride_align[2],
661       align.stride_align[3]);
662
663   gst_buffer_pool_config_add_option (config,
664       GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
665   gst_buffer_pool_config_set_video_alignment (config, &align);
666 }
667
668 static void
669 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
670     AVFrame * picture)
671 {
672   GstAllocationParams params = DEFAULT_ALLOC_PARAM;
673   GstVideoInfo info;
674   GstVideoFormat format;
675   GstCaps *caps;
676   GstStructure *config;
677   gint i;
678
679   if (ffmpegdec->internal_pool != NULL &&
680       ffmpegdec->pool_width == picture->width &&
681       ffmpegdec->pool_height == picture->height &&
682       ffmpegdec->pool_format == picture->format)
683     return;
684
685   GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
686       picture->width, picture->height);
687
688   format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
689   gst_video_info_set_format (&info, format, picture->width, picture->height);
690
691   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
692     ffmpegdec->stride[i] = -1;
693
694   if (ffmpegdec->internal_pool)
695     gst_object_unref (ffmpegdec->internal_pool);
696
697   ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
698   config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
699
700   caps = gst_video_info_to_caps (&info);
701   gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
702   gst_buffer_pool_config_set_allocator (config, NULL, &params);
703   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
704
705   gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
706       ffmpegdec->internal_pool, &info, config);
707   /* generic video pool never fails */
708   gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
709   gst_caps_unref (caps);
710
711   gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
712
713   /* Remember pool size so we can detect changes */
714   ffmpegdec->pool_width = picture->width;
715   ffmpegdec->pool_height = picture->height;
716   ffmpegdec->pool_format = picture->format;
717   ffmpegdec->pool_info = info;
718 }
719
720 static gboolean
721 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
722 {
723   GstFFMpegVidDecClass *oclass;
724
725   if (!ffmpegdec->direct_rendering)
726     return FALSE;
727
728   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
729   return ((oclass->in_plugin->capabilities & 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   gint 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   gint 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 /* gst_ffmpegviddec_[video|audio]_frame:
1464  * ffmpegdec:
1465  * data: pointer to the data to decode
1466  * size: size of data in bytes
1467  * in_timestamp: incoming timestamp.
1468  * in_duration: incoming duration.
1469  * in_offset: incoming offset (frame number).
1470  * ret: Return flow.
1471  *
1472  * Returns: number of bytes used in decoding. The check for successful decode is
1473  *   outbuf being non-NULL.
1474  */
1475 static gint
1476 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1477     guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1478     GstFlowReturn * ret)
1479 {
1480   gint len = -1;
1481   gboolean mode_switch;
1482   GstVideoCodecFrame *out_frame;
1483   GstFFMpegVidDecVideoFrame *out_dframe;
1484   AVPacket packet;
1485   GstBufferPool *pool;
1486
1487   *ret = GST_FLOW_OK;
1488
1489   /* in case we skip frames */
1490   ffmpegdec->picture->pict_type = -1;
1491
1492   /* run QoS code, we don't stop decoding the frame when we are late because
1493    * else we might skip a reference frame */
1494   gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1495
1496   if (frame) {
1497     /* save reference to the timing info */
1498     ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1499     ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1500
1501     GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1502         frame->system_frame_number);
1503   }
1504
1505   /* now decode the frame */
1506   gst_avpacket_init (&packet, data, size);
1507
1508   if (ffmpegdec->palette) {
1509     guint8 *pal;
1510
1511     pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1512         AVPALETTE_SIZE);
1513     gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1514     GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1515   }
1516
1517   /* This might call into get_buffer() from another thread,
1518    * which would cause a deadlock. Release the lock here
1519    * and taking it again later seems safe
1520    * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1521    */
1522   GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1523   len = avcodec_decode_video2 (ffmpegdec->context,
1524       ffmpegdec->picture, have_data, &packet);
1525   GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1526
1527   GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1528       len, *have_data);
1529
1530   /* when we are in skip_frame mode, don't complain when ffmpeg returned
1531    * no data because we told it to skip stuff. */
1532   if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1533     len = 0;
1534
1535   /* no data, we're done */
1536   if (len < 0 || *have_data == 0)
1537     goto beach;
1538
1539   /* get the output picture timing info again */
1540   out_dframe = ffmpegdec->picture->opaque;
1541   out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1542
1543   /* also give back a buffer allocated by the frame, if any */
1544   gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1545   gst_buffer_replace (&out_dframe->buffer, NULL);
1546
1547   /* Extract auxilliary info not stored in the main AVframe */
1548   {
1549     GstVideoInfo *in_info = &ffmpegdec->input_state->info;
1550     /* Take multiview mode from upstream if present */
1551     ffmpegdec->picture_multiview_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1552     ffmpegdec->picture_multiview_flags =
1553         GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1554
1555     /* Otherwise, see if there's info in the frame */
1556     if (ffmpegdec->picture_multiview_mode == GST_VIDEO_MULTIVIEW_MODE_NONE) {
1557       AVFrameSideData *side_data =
1558           av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_STEREO3D);
1559       if (side_data) {
1560         AVStereo3D *stereo = (AVStereo3D *) side_data->data;
1561         ffmpegdec->picture_multiview_mode = stereo_av_to_gst (stereo->type);
1562         if (stereo->flags & AV_STEREO3D_FLAG_INVERT) {
1563           ffmpegdec->picture_multiview_flags =
1564               GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
1565         } else {
1566           ffmpegdec->picture_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
1567         }
1568       }
1569     }
1570   }
1571
1572   GST_DEBUG_OBJECT (ffmpegdec,
1573       "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1574       out_frame->pts, out_frame->duration);
1575   GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1576       (guint64) ffmpegdec->picture->pts);
1577   GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1578       ffmpegdec->picture->coded_picture_number);
1579   GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1580       ffmpegdec->picture->display_picture_number);
1581   GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1582       ffmpegdec->picture->opaque);
1583   GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1584       (guint64) ffmpegdec->picture->reordered_opaque);
1585   GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1586       ffmpegdec->picture->repeat_pict);
1587   GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1588       ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1589
1590   if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1591           ffmpegdec->picture))
1592     goto negotiation_error;
1593
1594   pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1595   if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1596     *ret = get_output_buffer (ffmpegdec, out_frame);
1597   } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1598     GstBuffer *tmp = out_frame->output_buffer;
1599     out_frame->output_buffer = NULL;
1600     *ret = get_output_buffer (ffmpegdec, out_frame);
1601     gst_buffer_unref (tmp);
1602   }
1603 #ifndef G_DISABLE_ASSERT
1604   else {
1605     GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1606     if (vmeta) {
1607       GstVideoInfo *info = &ffmpegdec->output_state->info;
1608       g_assert (vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1609       g_assert (vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1610     }
1611   }
1612 #endif
1613   gst_object_unref (pool);
1614
1615   if (G_UNLIKELY (*ret != GST_FLOW_OK))
1616     goto no_output;
1617
1618   /* Mark corrupted frames as corrupted */
1619   if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1620     GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1621
1622   if (ffmpegdec->pic_interlaced) {
1623     /* set interlaced flags */
1624     if (ffmpegdec->picture->repeat_pict)
1625       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1626     if (ffmpegdec->picture->top_field_first)
1627       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1628     if (ffmpegdec->picture->interlaced_frame)
1629       GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1630           GST_VIDEO_BUFFER_FLAG_INTERLACED);
1631   }
1632
1633   {
1634     AVFrameSideData *side_data =
1635         av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_A53_CC);
1636     if (side_data) {
1637       GST_LOG_OBJECT (ffmpegdec, "Found CC of size %d", side_data->size);
1638       GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
1639       out_frame->output_buffer =
1640           gst_buffer_make_writable (out_frame->output_buffer);
1641       gst_buffer_add_video_caption_meta (out_frame->output_buffer,
1642           GST_VIDEO_CAPTION_TYPE_CEA708_RAW, side_data->data, side_data->size);
1643     }
1644   }
1645
1646   /* cleaning time */
1647   /* so we decoded this frame, frames preceding it in decoding order
1648    * that still do not have a buffer allocated seem rather useless,
1649    * and can be discarded, due to e.g. misparsed bogus frame
1650    * or non-keyframe in skipped decoding, ...
1651    * In any case, not likely to be seen again, so discard those,
1652    * before they pile up and/or mess with timestamping */
1653   {
1654     GList *l, *ol;
1655     GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1656     gboolean old = TRUE;
1657
1658     ol = l = gst_video_decoder_get_frames (dec);
1659     while (l) {
1660       GstVideoCodecFrame *tmp = l->data;
1661
1662       if (tmp == frame)
1663         old = FALSE;
1664
1665       if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1666         GST_LOG_OBJECT (dec,
1667             "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1668             GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1669             GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1670         /* drop extra ref and remove from frame list */
1671         gst_video_decoder_release_frame (dec, tmp);
1672       } else {
1673         /* drop extra ref we got */
1674         gst_video_codec_frame_unref (tmp);
1675       }
1676       l = l->next;
1677     }
1678     g_list_free (ol);
1679   }
1680
1681   av_frame_unref (ffmpegdec->picture);
1682
1683   /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1684    * libav might still have a reference to it!
1685    */
1686   *ret =
1687       gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1688
1689 beach:
1690   GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1691       gst_flow_get_name (*ret), len);
1692   return len;
1693
1694   /* special cases */
1695 no_output:
1696   {
1697     GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1698     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1699     len = -1;
1700     goto beach;
1701   }
1702
1703 negotiation_error:
1704   {
1705     if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1706       *ret = GST_FLOW_FLUSHING;
1707       goto beach;
1708     }
1709     GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1710     *ret = GST_FLOW_NOT_NEGOTIATED;
1711     goto beach;
1712   }
1713 }
1714
1715
1716 /* gst_ffmpegviddec_frame:
1717  * ffmpegdec:
1718  * data: pointer to the data to decode
1719  * size: size of data in bytes
1720  * got_data: 0 if no data was decoded, != 0 otherwise.
1721  * in_time: timestamp of data
1722  * in_duration: duration of data
1723  * ret: GstFlowReturn to return in the chain function
1724  *
1725  * Decode the given frame and pushes it downstream.
1726  *
1727  * Returns: Number of bytes used in decoding, -1 on error/failure.
1728  */
1729
1730 static gint
1731 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1732     guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1733     GstFlowReturn * ret)
1734 {
1735   GstFFMpegVidDecClass *oclass;
1736   gint len = 0;
1737
1738   if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1739     goto no_codec;
1740
1741   GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1742
1743   *ret = GST_FLOW_OK;
1744   ffmpegdec->context->frame_number++;
1745
1746   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1747
1748   len =
1749       gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1750       ret);
1751
1752   if (len < 0) {
1753     GST_WARNING_OBJECT (ffmpegdec,
1754         "avdec_%s: decoding error (len: %d, have_data: %d)",
1755         oclass->in_plugin->name, len, *have_data);
1756   }
1757
1758   return len;
1759
1760   /* ERRORS */
1761 no_codec:
1762   {
1763     GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1764     *ret = GST_FLOW_NOT_NEGOTIATED;
1765     return -1;
1766   }
1767 }
1768
1769 static GstFlowReturn
1770 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1771 {
1772   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1773   GstFFMpegVidDecClass *oclass;
1774
1775   if (!ffmpegdec->opened)
1776     return GST_FLOW_OK;
1777
1778   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1779
1780   if (oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) {
1781     gint have_data, len;
1782     GstFlowReturn ret;
1783
1784     GST_LOG_OBJECT (ffmpegdec,
1785         "codec has delay capabilities, calling until ffmpeg has drained everything");
1786
1787     do {
1788       len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1789     } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1790   }
1791
1792   return GST_FLOW_OK;
1793 }
1794
1795 static GstFlowReturn
1796 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1797     GstVideoCodecFrame * frame)
1798 {
1799   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1800   guint8 *data, *bdata;
1801   gint size, len, have_data, bsize;
1802   GstMapInfo minfo;
1803   GstFlowReturn ret = GST_FLOW_OK;
1804   gboolean do_padding;
1805
1806   GST_LOG_OBJECT (ffmpegdec,
1807       "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1808       ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1809       gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1810       GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1811
1812   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1813     GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1814         ("Failed to map buffer for reading"));
1815     return GST_FLOW_ERROR;
1816   }
1817
1818   /* treat frame as void until a buffer is requested for it */
1819   GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1820       GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1821
1822   bdata = minfo.data;
1823   bsize = minfo.size;
1824
1825   if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1826           || (minfo.maxsize - minfo.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
1827     /* add padding */
1828     if (ffmpegdec->padded_size < bsize + AV_INPUT_BUFFER_PADDING_SIZE) {
1829       ffmpegdec->padded_size = bsize + AV_INPUT_BUFFER_PADDING_SIZE;
1830       ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1831       GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1832           ffmpegdec->padded_size);
1833     }
1834     GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1835         "Copy input to add padding");
1836     memcpy (ffmpegdec->padded, bdata, bsize);
1837     memset (ffmpegdec->padded + bsize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1838
1839     bdata = ffmpegdec->padded;
1840     do_padding = TRUE;
1841   } else {
1842     do_padding = FALSE;
1843   }
1844
1845   do {
1846     guint8 tmp_padding[AV_INPUT_BUFFER_PADDING_SIZE];
1847
1848     /* parse, if at all possible */
1849     data = bdata;
1850     size = bsize;
1851
1852     if (do_padding) {
1853       /* add temporary padding */
1854       GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1855           "Add temporary input padding");
1856       memcpy (tmp_padding, data + size, AV_INPUT_BUFFER_PADDING_SIZE);
1857       memset (data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1858     }
1859
1860     /* decode a frame of audio/video now */
1861     len =
1862         gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1863
1864     if (ret != GST_FLOW_OK) {
1865       GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1866           gst_flow_get_name (ret));
1867       /* bad flow return, make sure we discard all data and exit */
1868       bsize = 0;
1869       break;
1870     }
1871
1872     if (do_padding) {
1873       memcpy (data + size, tmp_padding, AV_INPUT_BUFFER_PADDING_SIZE);
1874     }
1875
1876     if (len == 0 && have_data == 0) {
1877       /* nothing was decoded, this could be because no data was available or
1878        * because we were skipping frames.
1879        * If we have no context we must exit and wait for more data, we keep the
1880        * data we tried. */
1881       GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1882       break;
1883     }
1884
1885     if (len < 0) {
1886       /* a decoding error happened, we must break and try again with next data. */
1887       GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1888       bsize = 0;
1889       break;
1890     }
1891
1892     /* prepare for the next round, for codecs with a context we did this
1893      * already when using the parser. */
1894     bsize -= len;
1895     bdata += len;
1896
1897     do_padding = TRUE;
1898
1899     GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0).  bsize:%d , bdata:%p",
1900         bsize, bdata);
1901   } while (bsize > 0);
1902
1903   if (bsize > 0)
1904     GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1905
1906   gst_buffer_unmap (frame->input_buffer, &minfo);
1907   gst_video_codec_frame_unref (frame);
1908
1909   return ret;
1910 }
1911
1912 static gboolean
1913 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1914 {
1915   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1916   GstFFMpegVidDecClass *oclass;
1917
1918   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1919
1920   GST_OBJECT_LOCK (ffmpegdec);
1921   gst_ffmpeg_avcodec_close (ffmpegdec->context);
1922   if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1923     GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1924     GST_OBJECT_UNLOCK (ffmpegdec);
1925     return FALSE;
1926   }
1927   ffmpegdec->context->opaque = ffmpegdec;
1928   GST_OBJECT_UNLOCK (ffmpegdec);
1929
1930   return TRUE;
1931 }
1932
1933 static gboolean
1934 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1935 {
1936   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1937
1938   GST_OBJECT_LOCK (ffmpegdec);
1939   gst_ffmpegviddec_close (ffmpegdec, FALSE);
1940   GST_OBJECT_UNLOCK (ffmpegdec);
1941   g_free (ffmpegdec->padded);
1942   ffmpegdec->padded = NULL;
1943   ffmpegdec->padded_size = 0;
1944   if (ffmpegdec->input_state)
1945     gst_video_codec_state_unref (ffmpegdec->input_state);
1946   ffmpegdec->input_state = NULL;
1947   if (ffmpegdec->output_state)
1948     gst_video_codec_state_unref (ffmpegdec->output_state);
1949   ffmpegdec->output_state = NULL;
1950
1951   if (ffmpegdec->internal_pool)
1952     gst_object_unref (ffmpegdec->internal_pool);
1953   ffmpegdec->internal_pool = NULL;
1954
1955   ffmpegdec->pic_pix_fmt = 0;
1956   ffmpegdec->pic_width = 0;
1957   ffmpegdec->pic_height = 0;
1958   ffmpegdec->pic_par_n = 0;
1959   ffmpegdec->pic_par_d = 0;
1960   ffmpegdec->pic_interlaced = 0;
1961   ffmpegdec->pic_field_order = 0;
1962   ffmpegdec->pic_field_order_changed = FALSE;
1963   ffmpegdec->ctx_ticks = 0;
1964   ffmpegdec->ctx_time_n = 0;
1965   ffmpegdec->ctx_time_d = 0;
1966
1967   ffmpegdec->pool_width = 0;
1968   ffmpegdec->pool_height = 0;
1969   ffmpegdec->pool_format = 0;
1970
1971   return TRUE;
1972 }
1973
1974 static GstFlowReturn
1975 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1976 {
1977   gst_ffmpegviddec_drain (decoder);
1978   /* note that finish can and should clean up more drastically,
1979    * but drain is also invoked on e.g. packet loss in GAP handling */
1980   gst_ffmpegviddec_flush (decoder);
1981
1982   return GST_FLOW_OK;
1983 }
1984
1985 static gboolean
1986 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1987 {
1988   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1989
1990   if (ffmpegdec->opened) {
1991     GST_LOG_OBJECT (decoder, "flushing buffers");
1992     avcodec_flush_buffers (ffmpegdec->context);
1993   }
1994
1995   return TRUE;
1996 }
1997
1998 static gboolean
1999 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
2000 {
2001   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
2002   GstVideoCodecState *state;
2003   GstBufferPool *pool;
2004   guint size, min, max;
2005   GstStructure *config;
2006   gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
2007   GstAllocator *allocator = NULL;
2008   GstAllocationParams params = DEFAULT_ALLOC_PARAM;
2009
2010   have_pool = (gst_query_get_n_allocation_pools (query) != 0);
2011
2012   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
2013           query))
2014     return FALSE;
2015
2016   state = gst_video_decoder_get_output_state (decoder);
2017
2018   if (gst_query_get_n_allocation_params (query) > 0) {
2019     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
2020     params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
2021   } else {
2022     gst_query_add_allocation_param (query, allocator, &params);
2023   }
2024
2025   gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2026
2027   /* Don't use pool that can't grow, as we don't know how many buffer we'll
2028    * need, otherwise we may stall */
2029   if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
2030     gst_object_unref (pool);
2031     pool = gst_video_buffer_pool_new ();
2032     max = 0;
2033     update_pool = TRUE;
2034     have_pool = FALSE;
2035
2036     /* if there is an allocator, also drop it, as it might be the reason we
2037      * have this limit. Default will be used */
2038     if (allocator) {
2039       gst_object_unref (allocator);
2040       allocator = NULL;
2041     }
2042   }
2043
2044   config = gst_buffer_pool_get_config (pool);
2045   gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2046   gst_buffer_pool_config_set_allocator (config, allocator, &params);
2047
2048   have_videometa =
2049       gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2050
2051   if (have_videometa)
2052     gst_buffer_pool_config_add_option (config,
2053         GST_BUFFER_POOL_OPTION_VIDEO_META);
2054
2055   have_alignment =
2056       gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
2057
2058   /* If we have videometa, we never have to copy */
2059   if (have_videometa && have_pool && have_alignment &&
2060       gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
2061     GstStructure *config_copy = gst_structure_copy (config);
2062
2063     gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
2064         config_copy);
2065
2066     /* FIXME validate and retry */
2067     if (gst_buffer_pool_set_config (pool, config_copy)) {
2068       GstFlowReturn ret;
2069       GstBuffer *tmp;
2070
2071       gst_buffer_pool_set_active (pool, TRUE);
2072       ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
2073       if (ret == GST_FLOW_OK) {
2074         GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
2075         gboolean same_stride = TRUE;
2076         gint i;
2077
2078         for (i = 0; i < vmeta->n_planes; i++) {
2079           if (vmeta->stride[i] != ffmpegdec->stride[i]) {
2080             same_stride = FALSE;
2081             break;
2082           }
2083         }
2084
2085         gst_buffer_unref (tmp);
2086
2087         if (same_stride) {
2088           if (ffmpegdec->internal_pool)
2089             gst_object_unref (ffmpegdec->internal_pool);
2090           ffmpegdec->internal_pool = gst_object_ref (pool);
2091           ffmpegdec->pool_info = state->info;
2092           gst_structure_free (config);
2093           goto done;
2094         }
2095       }
2096     }
2097   }
2098
2099   if (have_videometa && ffmpegdec->internal_pool
2100       && ffmpegdec->pool_width == state->info.width
2101       && ffmpegdec->pool_height == state->info.height) {
2102     update_pool = TRUE;
2103     gst_object_unref (pool);
2104     pool = gst_object_ref (ffmpegdec->internal_pool);
2105     gst_structure_free (config);
2106     goto done;
2107   }
2108
2109   /* configure */
2110   if (!gst_buffer_pool_set_config (pool, config)) {
2111     gboolean working_pool = FALSE;
2112     config = gst_buffer_pool_get_config (pool);
2113
2114     if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
2115             max)) {
2116       working_pool = gst_buffer_pool_set_config (pool, config);
2117     } else {
2118       gst_structure_free (config);
2119     }
2120
2121     if (!working_pool) {
2122       gst_object_unref (pool);
2123       pool = gst_video_buffer_pool_new ();
2124       config = gst_buffer_pool_get_config (pool);
2125       gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2126       gst_buffer_pool_config_set_allocator (config, NULL, &params);
2127       gst_buffer_pool_set_config (pool, config);
2128       update_pool = TRUE;
2129     }
2130   }
2131
2132 done:
2133   /* and store */
2134   if (update_pool)
2135     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2136
2137   gst_object_unref (pool);
2138   if (allocator)
2139     gst_object_unref (allocator);
2140   gst_video_codec_state_unref (state);
2141
2142   return TRUE;
2143 }
2144
2145 static gboolean
2146 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
2147     GstQuery * query)
2148 {
2149   GstAllocationParams params;
2150
2151   gst_allocation_params_init (&params);
2152   params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
2153   params.align = DEFAULT_STRIDE_ALIGN;
2154   params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
2155   /* we would like to have some padding so that we don't have to
2156    * memcpy. We don't suggest an allocator. */
2157   gst_query_add_allocation_param (query, NULL, &params);
2158
2159   return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
2160       query);
2161 }
2162
2163 static void
2164 gst_ffmpegviddec_set_property (GObject * object,
2165     guint prop_id, const GValue * value, GParamSpec * pspec)
2166 {
2167   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2168
2169   switch (prop_id) {
2170     case PROP_LOWRES:
2171       ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
2172       break;
2173     case PROP_SKIPFRAME:
2174       ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
2175           g_value_get_enum (value);
2176       break;
2177     case PROP_DIRECT_RENDERING:
2178       ffmpegdec->direct_rendering = g_value_get_boolean (value);
2179       break;
2180     case PROP_DEBUG_MV:
2181       ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
2182           g_value_get_boolean (value);
2183       break;
2184     case PROP_MAX_THREADS:
2185       ffmpegdec->max_threads = g_value_get_int (value);
2186       break;
2187     case PROP_OUTPUT_CORRUPT:
2188       ffmpegdec->output_corrupt = g_value_get_boolean (value);
2189       break;
2190     default:
2191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2192       break;
2193   }
2194 }
2195
2196 static void
2197 gst_ffmpegviddec_get_property (GObject * object,
2198     guint prop_id, GValue * value, GParamSpec * pspec)
2199 {
2200   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2201
2202   switch (prop_id) {
2203     case PROP_LOWRES:
2204       g_value_set_enum (value, ffmpegdec->context->lowres);
2205       break;
2206     case PROP_SKIPFRAME:
2207       g_value_set_enum (value, ffmpegdec->context->skip_frame);
2208       break;
2209     case PROP_DIRECT_RENDERING:
2210       g_value_set_boolean (value, ffmpegdec->direct_rendering);
2211       break;
2212     case PROP_DEBUG_MV:
2213       g_value_set_boolean (value, ffmpegdec->context->debug_mv);
2214       break;
2215     case PROP_MAX_THREADS:
2216       g_value_set_int (value, ffmpegdec->max_threads);
2217       break;
2218     case PROP_OUTPUT_CORRUPT:
2219       g_value_set_boolean (value, ffmpegdec->output_corrupt);
2220       break;
2221     default:
2222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2223       break;
2224   }
2225 }
2226
2227 gboolean
2228 gst_ffmpegviddec_register (GstPlugin * plugin)
2229 {
2230   GTypeInfo typeinfo = {
2231     sizeof (GstFFMpegVidDecClass),
2232     (GBaseInitFunc) gst_ffmpegviddec_base_init,
2233     NULL,
2234     (GClassInitFunc) gst_ffmpegviddec_class_init,
2235     NULL,
2236     NULL,
2237     sizeof (GstFFMpegVidDec),
2238     0,
2239     (GInstanceInitFunc) gst_ffmpegviddec_init,
2240   };
2241   GType type;
2242   AVCodec *in_plugin;
2243   gint rank;
2244
2245   in_plugin = av_codec_next (NULL);
2246
2247   GST_LOG ("Registering decoders");
2248
2249   while (in_plugin) {
2250     gchar *type_name;
2251     gchar *plugin_name;
2252
2253     /* only video decoders */
2254     if (!av_codec_is_decoder (in_plugin)
2255         || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2256       goto next;
2257
2258     /* no quasi codecs, please */
2259     if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2260         in_plugin->id == AV_CODEC_ID_V210 ||
2261         in_plugin->id == AV_CODEC_ID_V210X ||
2262         in_plugin->id == AV_CODEC_ID_V308 ||
2263         in_plugin->id == AV_CODEC_ID_V408 ||
2264         in_plugin->id == AV_CODEC_ID_V410 ||
2265         in_plugin->id == AV_CODEC_ID_R210
2266         || in_plugin->id == AV_CODEC_ID_AYUV
2267         || in_plugin->id == AV_CODEC_ID_Y41P
2268         || in_plugin->id == AV_CODEC_ID_012V
2269         || in_plugin->id == AV_CODEC_ID_YUV4
2270 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
2271         AV_VERSION_INT (57,4,0)
2272         || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
2273 #endif
2274         || in_plugin->id == AV_CODEC_ID_ZLIB) {
2275       goto next;
2276     }
2277
2278     /* No decoders depending on external libraries (we don't build them, but
2279      * people who build against an external ffmpeg might have them.
2280      * We have native gstreamer plugins for all of those libraries anyway. */
2281     if (!strncmp (in_plugin->name, "lib", 3)) {
2282       GST_DEBUG
2283           ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2284           in_plugin->name);
2285       goto next;
2286     }
2287
2288     /* No vdpau plugins until we can figure out how to properly use them
2289      * outside of ffmpeg. */
2290     if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2291       GST_DEBUG
2292           ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2293           in_plugin->name);
2294       goto next;
2295     }
2296
2297     if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2298       GST_DEBUG
2299           ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2300           in_plugin->name);
2301       goto next;
2302     }
2303
2304     if (strstr (in_plugin->name, "vaapi")) {
2305       GST_DEBUG
2306           ("Ignoring VAAPI decoder %s. We can't handle this outside of ffmpeg",
2307           in_plugin->name);
2308       goto next;
2309     }
2310
2311     if (g_str_has_suffix (in_plugin->name, "_qsv")) {
2312       GST_DEBUG
2313           ("Ignoring qsv decoder %s. We can't handle this outside of ffmpeg",
2314           in_plugin->name);
2315       goto next;
2316     }
2317
2318     if (g_str_has_suffix (in_plugin->name, "_cuvid")) {
2319       GST_DEBUG
2320           ("Ignoring CUVID decoder %s. We can't handle this outside of ffmpeg",
2321           in_plugin->name);
2322       goto next;
2323     }
2324
2325     if (g_str_has_suffix (in_plugin->name, "_v4l2m2m")) {
2326       GST_DEBUG
2327           ("Ignoring V4L2 mem-to-mem decoder %s. We can't handle this outside of ffmpeg",
2328           in_plugin->name);
2329       goto next;
2330     }
2331
2332     GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2333
2334     /* no codecs for which we're GUARANTEED to have better alternatives */
2335     /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2336     /* MP1 : Use MP3 for decoding */
2337     /* MP2 : Use MP3 for decoding */
2338     /* Theora: Use libtheora based theoradec */
2339     if (!strcmp (in_plugin->name, "theora") ||
2340         !strcmp (in_plugin->name, "mpeg1video") ||
2341         strstr (in_plugin->name, "crystalhd") != NULL ||
2342         !strcmp (in_plugin->name, "ass") ||
2343         !strcmp (in_plugin->name, "srt") ||
2344         !strcmp (in_plugin->name, "pgssub") ||
2345         !strcmp (in_plugin->name, "dvdsub") ||
2346         !strcmp (in_plugin->name, "dvbsub")) {
2347       GST_LOG ("Ignoring decoder %s", in_plugin->name);
2348       goto next;
2349     }
2350
2351     /* construct the type */
2352     if (!strcmp (in_plugin->name, "hevc")) {
2353       plugin_name = g_strdup ("h265");
2354     } else {
2355       plugin_name = g_strdup ((gchar *) in_plugin->name);
2356     }
2357     g_strdelimit (plugin_name, NULL, '_');
2358     type_name = g_strdup_printf ("avdec_%s", plugin_name);
2359     g_free (plugin_name);
2360
2361     type = g_type_from_name (type_name);
2362
2363     if (!type) {
2364       /* create the gtype now */
2365       type =
2366           g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2367           0);
2368       g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2369     }
2370
2371     /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2372      * tested and by far outperforms divxdec/xviddec - so we prefer it.
2373      * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2374      * VC1/WMV3 are not working and thus unpreferred for now. */
2375     switch (in_plugin->id) {
2376       case AV_CODEC_ID_MPEG1VIDEO:
2377       case AV_CODEC_ID_MPEG2VIDEO:
2378       case AV_CODEC_ID_MPEG4:
2379       case AV_CODEC_ID_MSMPEG4V3:
2380       case AV_CODEC_ID_H264:
2381       case AV_CODEC_ID_HEVC:
2382       case AV_CODEC_ID_RV10:
2383       case AV_CODEC_ID_RV20:
2384       case AV_CODEC_ID_RV30:
2385       case AV_CODEC_ID_RV40:
2386         rank = GST_RANK_PRIMARY;
2387         break;
2388         /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2389          * They say libdv's quality is better though. leave as secondary.
2390          * note: if you change this, see the code in gstdv.c in good/ext/dv.
2391          */
2392       case AV_CODEC_ID_DVVIDEO:
2393         rank = GST_RANK_SECONDARY;
2394         break;
2395       default:
2396         rank = GST_RANK_MARGINAL;
2397         break;
2398     }
2399     if (!gst_element_register (plugin, type_name, rank, type)) {
2400       g_warning ("Failed to register %s", type_name);
2401       g_free (type_name);
2402       return FALSE;
2403     }
2404
2405     g_free (type_name);
2406
2407   next:
2408     in_plugin = av_codec_next (in_plugin);
2409   }
2410
2411   GST_LOG ("Finished Registering decoders");
2412
2413   return TRUE;
2414 }