avviddec: drain frames until libav doesn't have more data
[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 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
41
42 #define MAX_TS_MASK 0xff
43
44 #define DEFAULT_LOWRES                  0
45 #define DEFAULT_SKIPFRAME               0
46 #define DEFAULT_DIRECT_RENDERING        TRUE
47 #define DEFAULT_DEBUG_MV                FALSE
48 #define DEFAULT_MAX_THREADS             0
49 #define DEFAULT_OUTPUT_CORRUPT          TRUE
50 #define REQUIRED_POOL_MAX_BUFFERS       32
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, gboolean force);
89
90 /* some sort of bufferpool handling, but different */
91 static int gst_ffmpegviddec_get_buffer (AVCodecContext * context,
92     AVFrame * picture);
93 static int gst_ffmpegviddec_reget_buffer (AVCodecContext * context,
94     AVFrame * picture);
95 static void gst_ffmpegviddec_release_buffer (AVCodecContext * context,
96     AVFrame * picture);
97
98 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
99 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
100
101 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
102
103 static GstElementClass *parent_class = NULL;
104
105 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
106 static GType
107 gst_ffmpegviddec_lowres_get_type (void)
108 {
109   static GType ffmpegdec_lowres_type = 0;
110
111   if (!ffmpegdec_lowres_type) {
112     static const GEnumValue ffmpegdec_lowres[] = {
113       {0, "0", "full"},
114       {1, "1", "1/2-size"},
115       {2, "2", "1/4-size"},
116       {0, NULL, NULL},
117     };
118
119     ffmpegdec_lowres_type =
120         g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
121   }
122
123   return ffmpegdec_lowres_type;
124 }
125
126 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
127 static GType
128 gst_ffmpegviddec_skipframe_get_type (void)
129 {
130   static GType ffmpegdec_skipframe_type = 0;
131
132   if (!ffmpegdec_skipframe_type) {
133     static const GEnumValue ffmpegdec_skipframe[] = {
134       {0, "0", "Skip nothing"},
135       {1, "1", "Skip B-frames"},
136       {2, "2", "Skip IDCT/Dequantization"},
137       {5, "5", "Skip everything"},
138       {0, NULL, NULL},
139     };
140
141     ffmpegdec_skipframe_type =
142         g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
143   }
144
145   return ffmpegdec_skipframe_type;
146 }
147
148 static void
149 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
150 {
151   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
152   GstPadTemplate *sinktempl, *srctempl;
153   GstCaps *sinkcaps, *srccaps;
154   AVCodec *in_plugin;
155   gchar *longname, *description;
156
157   in_plugin =
158       (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
159       GST_FFDEC_PARAMS_QDATA);
160   g_assert (in_plugin != NULL);
161
162   /* construct the element details struct */
163   longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
164   description = g_strdup_printf ("libav %s decoder", in_plugin->name);
165   gst_element_class_set_metadata (element_class, longname,
166       "Codec/Decoder/Video", description,
167       "Wim Taymans <wim.taymans@gmail.com>, "
168       "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
169       "Edward Hervey <bilboed@bilboed.com>");
170   g_free (longname);
171   g_free (description);
172
173   /* get the caps */
174   sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
175   if (!sinkcaps) {
176     GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
177     sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
178   }
179   srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
180       in_plugin->id, FALSE, in_plugin);
181   if (!srccaps) {
182     GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
183     srccaps = gst_caps_from_string ("video/x-raw");
184   }
185
186   /* pad templates */
187   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
188       GST_PAD_ALWAYS, sinkcaps);
189   srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
190
191   gst_element_class_add_pad_template (element_class, srctempl);
192   gst_element_class_add_pad_template (element_class, sinktempl);
193
194   klass->in_plugin = in_plugin;
195 }
196
197 static void
198 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
199 {
200   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
201   GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
202   int caps;
203
204   parent_class = g_type_class_peek_parent (klass);
205
206   gobject_class->finalize = gst_ffmpegviddec_finalize;
207
208   gobject_class->set_property = gst_ffmpegviddec_set_property;
209   gobject_class->get_property = gst_ffmpegviddec_get_property;
210
211   g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
212       g_param_spec_enum ("skip-frame", "Skip frames",
213           "Which types of frames to skip during decoding",
214           GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
215           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216   g_object_class_install_property (gobject_class, PROP_LOWRES,
217       g_param_spec_enum ("lowres", "Low resolution",
218           "At which resolution to decode images",
219           GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
220           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221   g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
222       g_param_spec_boolean ("direct-rendering", "Direct Rendering",
223           "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
224           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225   g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
226       g_param_spec_boolean ("debug-mv", "Debug motion vectors",
227           "Whether libav should print motion vectors on top of the image",
228           DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229   g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
230       g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
231           "Whether libav should output frames even if corrupted",
232           DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234   caps = klass->in_plugin->capabilities;
235   if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
236     g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
237         g_param_spec_int ("max-threads", "Maximum decode threads",
238             "Maximum number of worker threads to spawn. (0 = auto)",
239             0, G_MAXINT, DEFAULT_MAX_THREADS,
240             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241   }
242
243   viddec_class->set_format = gst_ffmpegviddec_set_format;
244   viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
245   viddec_class->start = gst_ffmpegviddec_start;
246   viddec_class->stop = gst_ffmpegviddec_stop;
247   viddec_class->flush = gst_ffmpegviddec_flush;
248   viddec_class->finish = gst_ffmpegviddec_finish;
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 = avcodec_alloc_frame ();
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_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
271 }
272
273 static void
274 gst_ffmpegviddec_finalize (GObject * object)
275 {
276   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
277
278   if (ffmpegdec->context != NULL) {
279     gst_ffmpeg_avcodec_close (ffmpegdec->context);
280     av_free (ffmpegdec->context);
281     ffmpegdec->context = NULL;
282   }
283
284   avcodec_free_frame (&ffmpegdec->picture);
285
286   G_OBJECT_CLASS (parent_class)->finalize (object);
287 }
288
289 static void
290 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
291     gboolean enable)
292 {
293   g_return_if_fail (context != NULL);
294
295   if (enable)
296     context->flags |= flags;
297   else
298     context->flags &= ~flags;
299 }
300
301 /* with LOCK */
302 static gboolean
303 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
304 {
305   GstFFMpegVidDecClass *oclass;
306   gint i;
307
308   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
309
310   GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
311
312   gst_caps_replace (&ffmpegdec->last_caps, NULL);
313
314   gst_ffmpeg_avcodec_close (ffmpegdec->context);
315   ffmpegdec->opened = FALSE;
316
317   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
318     ffmpegdec->stride[i] = -1;
319
320   gst_buffer_replace (&ffmpegdec->palette, NULL);
321
322   if (ffmpegdec->context->extradata) {
323     av_free (ffmpegdec->context->extradata);
324     ffmpegdec->context->extradata = NULL;
325   }
326
327   if (reset) {
328     if (avcodec_get_context_defaults3 (ffmpegdec->context,
329             oclass->in_plugin) < 0) {
330       GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
331       return FALSE;
332     }
333     ffmpegdec->context->opaque = ffmpegdec;
334   }
335   return TRUE;
336 }
337
338 /* with LOCK */
339 static gboolean
340 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
341 {
342   GstFFMpegVidDecClass *oclass;
343   gint i;
344
345   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
346
347   if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
348     goto could_not_open;
349
350   for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
351     ffmpegdec->stride[i] = -1;
352
353   ffmpegdec->opened = TRUE;
354   ffmpegdec->is_realvideo = FALSE;
355
356   GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
357       oclass->in_plugin->name, oclass->in_plugin->id);
358
359   switch (oclass->in_plugin->id) {
360     case AV_CODEC_ID_RV10:
361     case AV_CODEC_ID_RV30:
362     case AV_CODEC_ID_RV20:
363     case AV_CODEC_ID_RV40:
364       ffmpegdec->is_realvideo = TRUE;
365       break;
366     default:
367       GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
368       break;
369   }
370
371   gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
372       CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
373
374   return TRUE;
375
376   /* ERRORS */
377 could_not_open:
378   {
379     gst_ffmpegviddec_close (ffmpegdec, TRUE);
380     GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
381         oclass->in_plugin->name);
382     return FALSE;
383   }
384 }
385
386 static void
387 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
388     GstVideoCodecState * state)
389 {
390   GstStructure *str = gst_caps_get_structure (state->caps, 0);
391   const GValue *palette_v;
392   GstBuffer *palette;
393
394   /* do we have a palette? */
395   if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
396     palette = gst_value_get_buffer (palette_v);
397     GST_DEBUG ("got palette data %p", palette);
398     if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
399       gst_buffer_replace (&ffmpegdec->palette, palette);
400     }
401   }
402 }
403
404
405 static gboolean
406 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
407     GstVideoCodecState * state)
408 {
409   GstFFMpegVidDec *ffmpegdec;
410   GstFFMpegVidDecClass *oclass;
411   GstClockTime latency = GST_CLOCK_TIME_NONE;
412   gboolean ret = FALSE;
413
414   ffmpegdec = (GstFFMpegVidDec *) decoder;
415   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
416
417   if (ffmpegdec->last_caps != NULL &&
418       gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
419     return TRUE;
420   }
421
422   GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
423
424   GST_OBJECT_LOCK (ffmpegdec);
425   /* stupid check for VC1 */
426   if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
427       (oclass->in_plugin->id == AV_CODEC_ID_VC1))
428     oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
429
430   /* close old session */
431   if (ffmpegdec->opened) {
432     GST_OBJECT_UNLOCK (ffmpegdec);
433     gst_ffmpegviddec_drain (ffmpegdec);
434     GST_OBJECT_LOCK (ffmpegdec);
435     if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
436       GST_OBJECT_UNLOCK (ffmpegdec);
437       return FALSE;
438     }
439   }
440
441   gst_caps_replace (&ffmpegdec->last_caps, state->caps);
442
443   /* set buffer functions */
444   ffmpegdec->context->get_buffer = gst_ffmpegviddec_get_buffer;
445   ffmpegdec->context->reget_buffer = gst_ffmpegviddec_reget_buffer;
446   ffmpegdec->context->release_buffer = gst_ffmpegviddec_release_buffer;
447   ffmpegdec->context->draw_horiz_band = NULL;
448
449   /* reset coded_width/_height to prevent it being reused from last time when
450    * the codec is opened again, causing a mismatch and possible
451    * segfault/corruption. (Common scenario when renegotiating caps) */
452   ffmpegdec->context->coded_width = 0;
453   ffmpegdec->context->coded_height = 0;
454
455   GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
456       ffmpegdec->context->height);
457
458   /* FIXME : Create a method that takes GstVideoCodecState instead */
459   /* get size and so */
460   gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
461       oclass->in_plugin->type, state->caps, ffmpegdec->context);
462
463   GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
464       ffmpegdec->context->height);
465
466   gst_ffmpegviddec_get_palette (ffmpegdec, state);
467
468   if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
469     GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
470     ffmpegdec->context->time_base.num = 1;
471     ffmpegdec->context->time_base.den = 25;
472   }
473
474   /* workaround encoder bugs */
475   ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
476   ffmpegdec->context->err_recognition = 1;
477
478   /* for slow cpus */
479   ffmpegdec->context->lowres = ffmpegdec->lowres;
480   ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
481
482   /* ffmpeg can draw motion vectors on top of the image (not every decoder
483    * supports it) */
484   ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
485
486   {
487     GstQuery *query;
488     gboolean is_live;
489
490     if (ffmpegdec->max_threads == 0) {
491       if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
492         ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
493       else
494         ffmpegdec->context->thread_count = 0;
495     } else
496       ffmpegdec->context->thread_count = ffmpegdec->max_threads;
497
498     query = gst_query_new_latency ();
499     is_live = FALSE;
500     /* Check if upstream is live. If it isn't we can enable frame based
501      * threading, which is adding latency */
502     if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
503       gst_query_parse_latency (query, &is_live, NULL, NULL);
504     }
505     gst_query_unref (query);
506
507     if (is_live)
508       ffmpegdec->context->thread_type = FF_THREAD_SLICE;
509     else
510       ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
511   }
512
513   /* open codec - we don't select an output pix_fmt yet,
514    * simply because we don't know! We only get it
515    * during playback... */
516   if (!gst_ffmpegviddec_open (ffmpegdec))
517     goto open_failed;
518
519   if (ffmpegdec->input_state)
520     gst_video_codec_state_unref (ffmpegdec->input_state);
521   ffmpegdec->input_state = gst_video_codec_state_ref (state);
522
523   if (ffmpegdec->input_state->info.fps_n) {
524     GstVideoInfo *info = &ffmpegdec->input_state->info;
525     latency = gst_util_uint64_scale_ceil (
526         (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
527         info->fps_n);
528   }
529
530   ret = TRUE;
531
532 done:
533   GST_OBJECT_UNLOCK (ffmpegdec);
534
535   if (GST_CLOCK_TIME_IS_VALID (latency))
536     gst_video_decoder_set_latency (decoder, latency, latency);
537
538   return ret;
539
540   /* ERRORS */
541 open_failed:
542   {
543     GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
544     goto done;
545   }
546 }
547
548 typedef struct
549 {
550   GstFFMpegVidDec *ffmpegdec;
551   GstVideoCodecFrame *frame;
552   gboolean mapped;
553   GstVideoFrame vframe;
554   GstBuffer *buffer;
555 } GstFFMpegVidDecVideoFrame;
556
557 static GstFFMpegVidDecVideoFrame *
558 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
559     GstVideoCodecFrame * frame)
560 {
561   GstFFMpegVidDecVideoFrame *dframe;
562
563   dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
564   dframe->ffmpegdec = ffmpegdec;
565   dframe->frame = frame;
566
567   GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
568
569   return dframe;
570 }
571
572 static void
573 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
574     GstFFMpegVidDecVideoFrame * frame)
575 {
576   GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
577
578   if (frame->mapped)
579     gst_video_frame_unmap (&frame->vframe);
580   gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
581   gst_buffer_replace (&frame->buffer, NULL);
582   g_slice_free (GstFFMpegVidDecVideoFrame, frame);
583 }
584
585 static void
586 dummy_free_buffer (void *opaque, uint8_t * data)
587 {
588   GstFFMpegVidDecVideoFrame *frame = opaque;
589
590   gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
591 }
592
593 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
594  * into. We try to give it memory from our pool */
595 static int
596 gst_ffmpegviddec_get_buffer (AVCodecContext * context, AVFrame * picture)
597 {
598   GstVideoCodecFrame *frame;
599   GstFFMpegVidDecVideoFrame *dframe;
600   GstFFMpegVidDec *ffmpegdec;
601   gint c;
602   GstVideoInfo *info;
603   GstFlowReturn ret;
604
605   ffmpegdec = (GstFFMpegVidDec *) context->opaque;
606
607   GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
608
609   /* apply the last info we have seen to this picture, when we get the
610    * picture back from ffmpeg we can use this to correctly timestamp the output
611    * buffer */
612   picture->reordered_opaque = context->reordered_opaque;
613   GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
614       (gint32) picture->reordered_opaque);
615
616   frame =
617       gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
618       picture->reordered_opaque);
619   if (G_UNLIKELY (frame == NULL))
620     goto no_frame;
621
622   /* now it has a buffer allocated, so it is real and will also
623    * be _released */
624   GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
625       GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
626
627   if (G_UNLIKELY (frame->output_buffer != NULL))
628     goto duplicate_frame;
629
630   /* GstFFMpegVidDecVideoFrame receives the frame ref */
631   picture->opaque = dframe =
632       gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
633
634   GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
635
636   ffmpegdec->context->pix_fmt = context->pix_fmt;
637
638   /* see if we need renegotiation */
639   if (G_UNLIKELY (!gst_ffmpegviddec_negotiate (ffmpegdec, context, FALSE)))
640     goto negotiate_failed;
641
642   if (!ffmpegdec->current_dr)
643     goto no_dr;
644
645   ret =
646       gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
647       frame);
648   if (ret != GST_FLOW_OK)
649     goto alloc_failed;
650
651   /* piggy-backed alloc'ed on the frame,
652    * and there was much rejoicing and we are grateful.
653    * Now take away buffer from frame, we will give it back later when decoded.
654    * This allows multiple request for a buffer per frame; unusual but possible. */
655   gst_buffer_replace (&dframe->buffer, frame->output_buffer);
656   gst_buffer_replace (&frame->output_buffer, NULL);
657
658   /* Fill avpicture */
659   info = &ffmpegdec->output_state->info;
660   if (!gst_video_frame_map (&dframe->vframe, info, dframe->buffer,
661           GST_MAP_READWRITE))
662     goto invalid_frame;
663   dframe->mapped = TRUE;
664
665   for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
666     if (c < GST_VIDEO_INFO_N_PLANES (info)) {
667       picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
668       picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
669
670       /* libav does not allow stride changes currently, fall back to
671        * non-direct rendering here:
672        * https://bugzilla.gnome.org/show_bug.cgi?id=704769
673        * https://bugzilla.libav.org/show_bug.cgi?id=556
674        */
675       if (ffmpegdec->stride[c] == -1) {
676         ffmpegdec->stride[c] = picture->linesize[c];
677       } else if (picture->linesize[c] != ffmpegdec->stride[c]) {
678         GST_LOG_OBJECT (ffmpegdec,
679             "No direct rendering, stride changed c=%d %d->%d", c,
680             ffmpegdec->stride[c], picture->linesize[c]);
681
682         for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
683           picture->data[c] = NULL;
684           picture->linesize[c] = 0;
685         }
686         gst_video_frame_unmap (&dframe->vframe);
687         dframe->mapped = FALSE;
688         gst_buffer_replace (&dframe->buffer, NULL);
689         ffmpegdec->current_dr = FALSE;
690
691         goto no_dr;
692       }
693     } else {
694       picture->data[c] = NULL;
695       picture->linesize[c] = 0;
696     }
697     GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
698         picture->data[c]);
699   }
700
701   /* tell ffmpeg we own this buffer, tranfer the ref we have on the buffer to
702    * the opaque data. */
703   picture->type = FF_BUFFER_TYPE_USER;
704
705   GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
706
707   return 0;
708
709   /* fallbacks */
710 negotiate_failed:
711   {
712     GST_DEBUG_OBJECT (ffmpegdec, "negotiate failed");
713     goto fallback;
714   }
715 no_dr:
716   {
717     GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
718     goto fallback;
719   }
720 alloc_failed:
721   {
722     /* alloc default buffer when we can't get one from downstream */
723     GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
724     goto fallback;
725   }
726 invalid_frame:
727   {
728     /* alloc default buffer when we can't get one from downstream */
729     GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
730     gst_buffer_replace (&dframe->buffer, NULL);
731     goto fallback;
732   }
733 fallback:
734   {
735     int c;
736     gboolean first = TRUE;
737     int ret = avcodec_default_get_buffer (context, picture);
738
739     for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
740       ffmpegdec->stride[c] = picture->linesize[c];
741
742       if (picture->buf[c] == NULL && first) {
743         picture->buf[c] =
744             av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
745         first = FALSE;
746       }
747     }
748
749     return ret;
750   }
751 duplicate_frame:
752   {
753     GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
754     gst_video_codec_frame_unref (frame);
755     return -1;
756   }
757 no_frame:
758   {
759     GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
760     return -1;
761   }
762 }
763
764 /* this should havesame effect as _get_buffer wrt opaque metadata,
765  * but preserving current content, if any */
766 static int
767 gst_ffmpegviddec_reget_buffer (AVCodecContext * context, AVFrame * picture)
768 {
769   GstVideoCodecFrame *frame;
770   GstFFMpegVidDecVideoFrame *dframe;
771   GstFFMpegVidDec *ffmpegdec;
772
773   ffmpegdec = (GstFFMpegVidDec *) context->opaque;
774
775   GST_DEBUG_OBJECT (ffmpegdec, "regetting buffer picture %p", picture);
776
777   picture->reordered_opaque = context->reordered_opaque;
778
779   /* if there is no opaque, we didn't yet attach any frame to it. What usually
780    * happens is that avcodec_default_reget_buffer will call the getbuffer
781    * function. */
782   dframe = picture->opaque;
783   if (dframe == NULL)
784     goto done;
785
786   frame =
787       gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
788       picture->reordered_opaque);
789   if (G_UNLIKELY (frame == NULL))
790     goto no_frame;
791
792   if (G_UNLIKELY (frame->output_buffer != NULL))
793     goto duplicate_frame;
794
795   /* replace the frame, this one contains the pts/dts for the correspoding input
796    * buffer, which we need after decoding. */
797   gst_video_codec_frame_unref (dframe->frame);
798   dframe->frame = frame;
799
800 done:
801   return avcodec_default_reget_buffer (context, picture);
802
803   /* ERRORS */
804 no_frame:
805   {
806     GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
807     return -1;
808   }
809 duplicate_frame:
810   {
811     GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
812     return -1;
813   }
814 }
815
816 /* called when ffmpeg is done with our buffer */
817 static void
818 gst_ffmpegviddec_release_buffer (AVCodecContext * context, AVFrame * picture)
819 {
820   gint i;
821   GstFFMpegVidDecVideoFrame *frame;
822   GstFFMpegVidDec *ffmpegdec;
823
824   ffmpegdec = (GstFFMpegVidDec *) context->opaque;
825   frame = (GstFFMpegVidDecVideoFrame *) picture->opaque;
826   GST_DEBUG_OBJECT (ffmpegdec, "release frame SN %d",
827       frame->frame->system_frame_number);
828
829   /* check if it was our buffer */
830   if (picture->type != FF_BUFFER_TYPE_USER) {
831     GST_DEBUG_OBJECT (ffmpegdec, "default release buffer");
832     avcodec_default_release_buffer (context, picture);
833   }
834
835   /* we remove the opaque data now */
836   picture->opaque = NULL;
837
838   gst_ffmpegviddec_video_frame_free (ffmpegdec, frame);
839
840   /* zero out the reference in ffmpeg */
841   for (i = 0; i < 4; i++) {
842     picture->data[i] = NULL;
843     picture->linesize[i] = 0;
844   }
845 }
846
847 static gboolean
848 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
849     gboolean force)
850 {
851   if (!force && ffmpegdec->ctx_width == context->width
852       && ffmpegdec->ctx_height == context->height
853       && ffmpegdec->ctx_ticks == context->ticks_per_frame
854       && ffmpegdec->ctx_time_n == context->time_base.num
855       && ffmpegdec->ctx_time_d == context->time_base.den
856       && ffmpegdec->ctx_pix_fmt == context->pix_fmt
857       && ffmpegdec->ctx_par_n == context->sample_aspect_ratio.num
858       && ffmpegdec->ctx_par_d == context->sample_aspect_ratio.den)
859     return FALSE;
860
861   GST_DEBUG_OBJECT (ffmpegdec,
862       "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
863       ffmpegdec->ctx_width, ffmpegdec->ctx_height,
864       ffmpegdec->ctx_par_n, ffmpegdec->ctx_par_d,
865       ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
866       context->width, context->height,
867       context->sample_aspect_ratio.num,
868       context->sample_aspect_ratio.den,
869       context->time_base.num, context->time_base.den, context->pix_fmt);
870
871   ffmpegdec->ctx_width = context->width;
872   ffmpegdec->ctx_height = context->height;
873   ffmpegdec->ctx_ticks = context->ticks_per_frame;
874   ffmpegdec->ctx_time_n = context->time_base.num;
875   ffmpegdec->ctx_time_d = context->time_base.den;
876   ffmpegdec->ctx_pix_fmt = context->pix_fmt;
877   ffmpegdec->ctx_par_n = context->sample_aspect_ratio.num;
878   ffmpegdec->ctx_par_d = context->sample_aspect_ratio.den;
879
880   return TRUE;
881 }
882
883 static void
884 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
885     GstVideoInfo * in_info, GstVideoInfo * out_info)
886 {
887   gboolean demuxer_par_set = FALSE;
888   gboolean decoder_par_set = FALSE;
889   gint demuxer_num = 1, demuxer_denom = 1;
890   gint decoder_num = 1, decoder_denom = 1;
891
892   if (in_info->par_n && in_info->par_d) {
893     demuxer_num = in_info->par_n;
894     demuxer_denom = in_info->par_d;
895     demuxer_par_set = TRUE;
896     GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
897         demuxer_denom);
898   }
899
900   if (ffmpegdec->ctx_par_n && ffmpegdec->ctx_par_d) {
901     decoder_num = ffmpegdec->ctx_par_n;
902     decoder_denom = ffmpegdec->ctx_par_d;
903     decoder_par_set = TRUE;
904     GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
905         decoder_denom);
906   }
907
908   if (!demuxer_par_set && !decoder_par_set)
909     goto no_par;
910
911   if (demuxer_par_set && !decoder_par_set)
912     goto use_demuxer_par;
913
914   if (decoder_par_set && !demuxer_par_set)
915     goto use_decoder_par;
916
917   /* Both the demuxer and the decoder provide a PAR. If one of
918    * the two PARs is 1:1 and the other one is not, use the one
919    * that is not 1:1. */
920   if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
921     goto use_decoder_par;
922
923   if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
924     goto use_demuxer_par;
925
926   /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
927   goto use_demuxer_par;
928
929 use_decoder_par:
930   {
931     GST_DEBUG_OBJECT (ffmpegdec,
932         "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
933         decoder_denom);
934     out_info->par_n = decoder_num;
935     out_info->par_d = decoder_denom;
936     return;
937   }
938 use_demuxer_par:
939   {
940     GST_DEBUG_OBJECT (ffmpegdec,
941         "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
942         demuxer_denom);
943     out_info->par_n = demuxer_num;
944     out_info->par_d = demuxer_denom;
945     return;
946   }
947 no_par:
948   {
949     GST_DEBUG_OBJECT (ffmpegdec,
950         "Neither demuxer nor codec provide a pixel-aspect-ratio");
951     out_info->par_n = 1;
952     out_info->par_d = 1;
953     return;
954   }
955 }
956
957 static gboolean
958 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
959     AVCodecContext * context, gboolean force)
960 {
961   GstVideoFormat fmt;
962   GstVideoInfo *in_info, *out_info;
963   GstVideoCodecState *output_state;
964   gint fps_n, fps_d;
965
966   if (!update_video_context (ffmpegdec, context, force))
967     return TRUE;
968
969   fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->ctx_pix_fmt);
970   if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
971     goto unknown_format;
972
973   output_state =
974       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
975       ffmpegdec->ctx_width, ffmpegdec->ctx_height, ffmpegdec->input_state);
976   if (ffmpegdec->output_state)
977     gst_video_codec_state_unref (ffmpegdec->output_state);
978   ffmpegdec->output_state = output_state;
979
980   in_info = &ffmpegdec->input_state->info;
981   out_info = &ffmpegdec->output_state->info;
982
983   /* set the interlaced flag */
984   if (ffmpegdec->ctx_interlaced)
985     out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
986   else
987     out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
988
989   switch (context->chroma_sample_location) {
990     case 1:
991       out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
992       break;
993     case 2:
994       out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
995       break;
996     case 3:
997       out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
998       break;
999     case 4:
1000       out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1001       break;
1002     default:
1003       break;
1004   }
1005
1006   /* try to find a good framerate */
1007   if ((in_info->fps_d && in_info->fps_n) ||
1008       GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1009     /* take framerate from input when it was specified (#313970) */
1010     fps_n = in_info->fps_n;
1011     fps_d = in_info->fps_d;
1012   } else {
1013     fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1014     fps_d = ffmpegdec->ctx_time_n;
1015
1016     if (!fps_d) {
1017       GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1018           fps_n);
1019       fps_d = 1;
1020     }
1021     if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1022       GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1023           fps_d);
1024       fps_n = 0;
1025       fps_d = 1;
1026     }
1027   }
1028   GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1029   out_info->fps_n = fps_n;
1030   out_info->fps_d = fps_d;
1031
1032   /* calculate and update par now */
1033   gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1034
1035   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1036     goto negotiate_failed;
1037
1038   return TRUE;
1039
1040   /* ERRORS */
1041 unknown_format:
1042   {
1043     GST_ERROR_OBJECT (ffmpegdec,
1044         "decoder requires a video format unsupported by GStreamer");
1045     return FALSE;
1046   }
1047 negotiate_failed:
1048   {
1049     /* Reset so we try again next time even if force==FALSE */
1050     ffmpegdec->ctx_width = 0;
1051     ffmpegdec->ctx_height = 0;
1052     ffmpegdec->ctx_ticks = 0;
1053     ffmpegdec->ctx_time_n = 0;
1054     ffmpegdec->ctx_time_d = 0;
1055     ffmpegdec->ctx_pix_fmt = 0;
1056     ffmpegdec->ctx_par_n = 0;
1057     ffmpegdec->ctx_par_d = 0;
1058
1059     GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1060     return FALSE;
1061   }
1062 }
1063
1064 /* perform qos calculations before decoding the next frame.
1065  *
1066  * Sets the skip_frame flag and if things are really bad, skips to the next
1067  * keyframe.
1068  *
1069  */
1070 static void
1071 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1072     GstVideoCodecFrame * frame, gboolean * mode_switch)
1073 {
1074   GstClockTimeDiff diff;
1075
1076   *mode_switch = FALSE;
1077
1078   if (frame == NULL)
1079     return;
1080
1081   diff =
1082       gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1083       frame);
1084
1085   /* if we don't have timing info, then we don't do QoS */
1086   if (G_UNLIKELY (diff == G_MAXINT64))
1087     return;
1088
1089   GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1090
1091   if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1092     ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1093     *mode_switch = TRUE;
1094     GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1095   }
1096
1097   else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1098     ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1099     *mode_switch = TRUE;
1100     GST_DEBUG_OBJECT (ffmpegdec,
1101         "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1102   }
1103 }
1104
1105 /* get an outbuf buffer with the current picture */
1106 static GstFlowReturn
1107 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1108 {
1109   GstFlowReturn ret = GST_FLOW_OK;
1110   AVPicture pic, *outpic;
1111   GstVideoFrame vframe;
1112   GstVideoInfo *info;
1113   gint c;
1114
1115   GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1116
1117   ret =
1118       gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1119       frame);
1120   if (G_UNLIKELY (ret != GST_FLOW_OK))
1121     goto alloc_failed;
1122
1123   /* original ffmpeg code does not handle odd sizes correctly.
1124    * This patched up version does */
1125   /* Fill avpicture */
1126   info = &ffmpegdec->output_state->info;
1127   if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1128           GST_MAP_READ | GST_MAP_WRITE))
1129     goto alloc_failed;
1130
1131   for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1132     if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1133       pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1134       pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1135     } else {
1136       pic.data[c] = NULL;
1137       pic.linesize[c] = 0;
1138     }
1139     GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", pic.linesize[c],
1140         pic.data[c]);
1141   }
1142
1143   outpic = (AVPicture *) ffmpegdec->picture;
1144
1145   av_picture_copy (&pic, outpic, ffmpegdec->context->pix_fmt,
1146       GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
1147
1148   gst_video_frame_unmap (&vframe);
1149
1150   ffmpegdec->picture->reordered_opaque = -1;
1151
1152   return ret;
1153
1154   /* special cases */
1155 alloc_failed:
1156   {
1157     GST_DEBUG_OBJECT (ffmpegdec, "pad_alloc failed");
1158     return ret;
1159   }
1160 }
1161
1162 static void
1163 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1164 {
1165   memset (packet, 0, sizeof (AVPacket));
1166   packet->data = data;
1167   packet->size = size;
1168 }
1169
1170 /* gst_ffmpegviddec_[video|audio]_frame:
1171  * ffmpegdec:
1172  * data: pointer to the data to decode
1173  * size: size of data in bytes
1174  * in_timestamp: incoming timestamp.
1175  * in_duration: incoming duration.
1176  * in_offset: incoming offset (frame number).
1177  * ret: Return flow.
1178  *
1179  * Returns: number of bytes used in decoding. The check for successful decode is
1180  *   outbuf being non-NULL.
1181  */
1182 static gint
1183 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1184     guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1185     GstFlowReturn * ret)
1186 {
1187   gint len = -1;
1188   gboolean mode_switch;
1189   GstVideoCodecFrame *out_frame;
1190   GstFFMpegVidDecVideoFrame *out_dframe;
1191   AVPacket packet;
1192
1193   *ret = GST_FLOW_OK;
1194
1195   /* in case we skip frames */
1196   ffmpegdec->picture->pict_type = -1;
1197
1198   /* run QoS code, we don't stop decoding the frame when we are late because
1199    * else we might skip a reference frame */
1200   gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1201
1202   if (ffmpegdec->is_realvideo && data != NULL) {
1203     gint slice_count;
1204     gint i;
1205
1206     /* setup the slice table for realvideo */
1207     if (ffmpegdec->context->slice_offset == NULL)
1208       ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1209
1210     slice_count = (*data++) + 1;
1211     ffmpegdec->context->slice_count = slice_count;
1212
1213     for (i = 0; i < slice_count; i++) {
1214       data += 4;
1215       ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1216       data += 4;
1217     }
1218   }
1219
1220   if (frame) {
1221     /* save reference to the timing info */
1222     ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1223     ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1224
1225     GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1226         frame->system_frame_number);
1227   }
1228
1229   /* now decode the frame */
1230   gst_avpacket_init (&packet, data, size);
1231
1232   if (ffmpegdec->palette) {
1233     guint8 *pal;
1234
1235     pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1236         AVPALETTE_SIZE);
1237     gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1238     GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1239   }
1240
1241   len = avcodec_decode_video2 (ffmpegdec->context,
1242       ffmpegdec->picture, have_data, &packet);
1243
1244   GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1245       len, *have_data);
1246
1247   /* when we are in skip_frame mode, don't complain when ffmpeg returned
1248    * no data because we told it to skip stuff. */
1249   if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1250     len = 0;
1251
1252   /* no data, we're done */
1253   if (len < 0 || *have_data == 0)
1254     goto beach;
1255
1256   /* get the output picture timing info again */
1257   out_dframe = ffmpegdec->picture->opaque;
1258   out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1259
1260   /* also give back a buffer allocated by the frame, if any */
1261   gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1262   gst_buffer_replace (&out_dframe->buffer, NULL);
1263
1264   GST_DEBUG_OBJECT (ffmpegdec,
1265       "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1266       out_frame->pts, out_frame->duration);
1267   GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1268       (guint64) ffmpegdec->picture->pts);
1269   GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1270       ffmpegdec->picture->coded_picture_number);
1271   GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1272       ffmpegdec->picture->reference);
1273   GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1274       ffmpegdec->picture->display_picture_number);
1275   GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1276       ffmpegdec->picture->opaque);
1277   GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1278       (guint64) ffmpegdec->picture->reordered_opaque);
1279   GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1280       ffmpegdec->picture->repeat_pict);
1281   GST_DEBUG_OBJECT (ffmpegdec, "interlaced_frame:%d (current:%d)",
1282       ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1283   GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1284       ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1285
1286   if (G_UNLIKELY (ffmpegdec->picture->interlaced_frame !=
1287           ffmpegdec->ctx_interlaced)) {
1288     GST_WARNING ("Change in interlacing ! picture:%d, recorded:%d",
1289         ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1290     ffmpegdec->ctx_interlaced = ffmpegdec->picture->interlaced_frame;
1291     if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context, TRUE))
1292       goto negotiation_error;
1293   }
1294
1295   if (G_UNLIKELY (out_frame->output_buffer == NULL))
1296     *ret = get_output_buffer (ffmpegdec, out_frame);
1297
1298   if (G_UNLIKELY (*ret != GST_FLOW_OK))
1299     goto no_output;
1300
1301   /* Mark corrupted frames as corrupted */
1302   if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1303     GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1304
1305   if (ffmpegdec->ctx_interlaced) {
1306     /* set interlaced flags */
1307     if (ffmpegdec->picture->repeat_pict)
1308       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1309     if (ffmpegdec->picture->top_field_first)
1310       GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1311     if (ffmpegdec->picture->interlaced_frame)
1312       GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1313           GST_VIDEO_BUFFER_FLAG_INTERLACED);
1314   }
1315
1316   /* cleaning time */
1317   /* so we decoded this frame, frames preceding it in decoding order
1318    * that still do not have a buffer allocated seem rather useless,
1319    * and can be discarded, due to e.g. misparsed bogus frame
1320    * or non-keyframe in skipped decoding, ...
1321    * In any case, not likely to be seen again, so discard those,
1322    * before they pile up and/or mess with timestamping */
1323   {
1324     GList *l, *ol;
1325     GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1326     gboolean old = TRUE;
1327
1328     ol = l = gst_video_decoder_get_frames (dec);
1329     while (l) {
1330       GstVideoCodecFrame *tmp = l->data;
1331
1332       if (tmp == frame)
1333         old = FALSE;
1334
1335       if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1336         GST_LOG_OBJECT (dec,
1337             "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1338             GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1339             GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1340         /* drop extra ref and remove from frame list */
1341         gst_video_decoder_release_frame (dec, tmp);
1342       } else {
1343         /* drop extra ref we got */
1344         gst_video_codec_frame_unref (tmp);
1345       }
1346       l = l->next;
1347     }
1348     g_list_free (ol);
1349   }
1350
1351   *ret =
1352       gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1353
1354 beach:
1355   GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1356       gst_flow_get_name (*ret), len);
1357   return len;
1358
1359   /* special cases */
1360 no_output:
1361   {
1362     GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1363     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1364     len = -1;
1365     goto beach;
1366   }
1367
1368 negotiation_error:
1369   {
1370     GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1371     *ret = GST_FLOW_NOT_NEGOTIATED;
1372     goto beach;
1373   }
1374 }
1375
1376
1377 /* gst_ffmpegviddec_frame:
1378  * ffmpegdec:
1379  * data: pointer to the data to decode
1380  * size: size of data in bytes
1381  * got_data: 0 if no data was decoded, != 0 otherwise.
1382  * in_time: timestamp of data
1383  * in_duration: duration of data
1384  * ret: GstFlowReturn to return in the chain function
1385  *
1386  * Decode the given frame and pushes it downstream.
1387  *
1388  * Returns: Number of bytes used in decoding, -1 on error/failure.
1389  */
1390
1391 static gint
1392 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1393     guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1394     GstFlowReturn * ret)
1395 {
1396   GstFFMpegVidDecClass *oclass;
1397   gint len = 0;
1398
1399   if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1400     goto no_codec;
1401
1402   GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1403
1404   *ret = GST_FLOW_OK;
1405   ffmpegdec->context->frame_number++;
1406
1407   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1408
1409   len =
1410       gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1411       ret);
1412
1413   if (len < 0) {
1414     GST_WARNING_OBJECT (ffmpegdec,
1415         "avdec_%s: decoding error (len: %d, have_data: %d)",
1416         oclass->in_plugin->name, len, *have_data);
1417   }
1418
1419   return len;
1420
1421   /* ERRORS */
1422 no_codec:
1423   {
1424     GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1425     *ret = GST_FLOW_NOT_NEGOTIATED;
1426     return -1;
1427   }
1428 }
1429
1430 static void
1431 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1432 {
1433   GstFFMpegVidDecClass *oclass;
1434
1435   if (!ffmpegdec->opened)
1436     return;
1437
1438   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1439
1440   if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1441     gint have_data, len;
1442
1443     GST_LOG_OBJECT (ffmpegdec,
1444         "codec has delay capabilities, calling until ffmpeg has drained everything");
1445
1446     do {
1447       GstFlowReturn ret;
1448
1449       len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1450
1451     } while (len >= 0 && have_data == 1);
1452     avcodec_flush_buffers (ffmpegdec->context);
1453   }
1454 }
1455
1456 static GstFlowReturn
1457 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1458     GstVideoCodecFrame * frame)
1459 {
1460   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1461   guint8 *data, *bdata;
1462   gint size, len, have_data, bsize;
1463   GstMapInfo minfo;
1464   GstFlowReturn ret = GST_FLOW_OK;
1465   gboolean do_padding;
1466
1467   GST_LOG_OBJECT (ffmpegdec,
1468       "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1469       ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1470       gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1471       GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1472
1473   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1474     GST_ERROR_OBJECT (ffmpegdec, "Failed to map buffer");
1475     return GST_FLOW_ERROR;
1476   }
1477
1478   /* treat frame as void until a buffer is requested for it */
1479   GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1480       GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1481
1482   bdata = minfo.data;
1483   bsize = minfo.size;
1484
1485   if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1486           || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1487     /* add padding */
1488     if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1489       ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1490       ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1491       GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1492           ffmpegdec->padded_size);
1493     }
1494     GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1495         "Copy input to add padding");
1496     memcpy (ffmpegdec->padded, bdata, bsize);
1497     memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1498
1499     bdata = ffmpegdec->padded;
1500     do_padding = TRUE;
1501   } else {
1502     do_padding = FALSE;
1503   }
1504
1505   do {
1506     guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1507
1508     /* parse, if at all possible */
1509     data = bdata;
1510     size = bsize;
1511
1512     if (do_padding) {
1513       /* add temporary padding */
1514       GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1515           "Add temporary input padding");
1516       memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1517       memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1518     }
1519
1520     /* decode a frame of audio/video now */
1521     len =
1522         gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1523
1524     if (do_padding) {
1525       memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1526     }
1527
1528     if (ret != GST_FLOW_OK) {
1529       GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1530           gst_flow_get_name (ret));
1531       /* bad flow retun, make sure we discard all data and exit */
1532       bsize = 0;
1533       break;
1534     }
1535
1536     if (len == 0 && have_data == 0) {
1537       /* nothing was decoded, this could be because no data was available or
1538        * because we were skipping frames.
1539        * If we have no context we must exit and wait for more data, we keep the
1540        * data we tried. */
1541       GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1542       break;
1543     }
1544
1545     if (len < 0) {
1546       /* a decoding error happened, we must break and try again with next data. */
1547       GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1548       bsize = 0;
1549       break;
1550     }
1551
1552     /* prepare for the next round, for codecs with a context we did this
1553      * already when using the parser. */
1554     bsize -= len;
1555     bdata += len;
1556
1557     do_padding = TRUE;
1558
1559     GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0).  bsize:%d , bdata:%p",
1560         bsize, bdata);
1561   } while (bsize > 0);
1562
1563   if (bsize > 0)
1564     GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1565
1566   gst_buffer_unmap (frame->input_buffer, &minfo);
1567   gst_video_codec_frame_unref (frame);
1568
1569   return ret;
1570 }
1571
1572
1573 static gboolean
1574 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1575 {
1576   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1577   GstFFMpegVidDecClass *oclass;
1578
1579   oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1580
1581   GST_OBJECT_LOCK (ffmpegdec);
1582   gst_ffmpeg_avcodec_close (ffmpegdec->context);
1583   if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1584     GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1585     GST_OBJECT_UNLOCK (ffmpegdec);
1586     return FALSE;
1587   }
1588   ffmpegdec->context->opaque = ffmpegdec;
1589   GST_OBJECT_UNLOCK (ffmpegdec);
1590
1591   return TRUE;
1592 }
1593
1594 static gboolean
1595 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1596 {
1597   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1598
1599   GST_OBJECT_LOCK (ffmpegdec);
1600   gst_ffmpegviddec_close (ffmpegdec, FALSE);
1601   GST_OBJECT_UNLOCK (ffmpegdec);
1602   g_free (ffmpegdec->padded);
1603   ffmpegdec->padded = NULL;
1604   ffmpegdec->padded_size = 0;
1605   if (ffmpegdec->input_state)
1606     gst_video_codec_state_unref (ffmpegdec->input_state);
1607   ffmpegdec->input_state = NULL;
1608   if (ffmpegdec->output_state)
1609     gst_video_codec_state_unref (ffmpegdec->output_state);
1610   ffmpegdec->output_state = NULL;
1611
1612   ffmpegdec->ctx_width = 0;
1613   ffmpegdec->ctx_height = 0;
1614   ffmpegdec->ctx_ticks = 0;
1615   ffmpegdec->ctx_time_n = 0;
1616   ffmpegdec->ctx_time_d = 0;
1617   ffmpegdec->ctx_pix_fmt = 0;
1618   ffmpegdec->ctx_par_n = 0;
1619   ffmpegdec->ctx_par_d = 0;
1620
1621   return TRUE;
1622 }
1623
1624 static GstFlowReturn
1625 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1626 {
1627   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1628
1629   gst_ffmpegviddec_drain (ffmpegdec);
1630
1631   return GST_FLOW_OK;
1632 }
1633
1634 static gboolean
1635 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1636 {
1637   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1638
1639   if (ffmpegdec->opened)
1640     avcodec_flush_buffers (ffmpegdec->context);
1641
1642   return TRUE;
1643 }
1644
1645 static gboolean
1646 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1647 {
1648   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1649   GstVideoCodecState *state;
1650   GstBufferPool *pool;
1651   guint size, min, max;
1652   GstStructure *config;
1653   gboolean have_videometa, have_alignment, update_pool;
1654   GstAllocator *allocator = NULL;
1655   GstAllocationParams params = { 0, 15, 0, 0, };
1656
1657   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1658           query))
1659     return FALSE;
1660
1661   state = gst_video_decoder_get_output_state (decoder);
1662
1663   if (gst_query_get_n_allocation_params (query) > 0) {
1664     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
1665     params.align = MAX (params.align, 15);
1666   } else {
1667     gst_query_add_allocation_param (query, allocator, &params);
1668   }
1669
1670   gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1671
1672   /* Don't use pool that can't grow, as we don't know how many buffer we'll
1673    * need, otherwise we may stall */
1674   if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1675     gst_object_unref (pool);
1676     pool = gst_video_buffer_pool_new ();
1677     max = 0;
1678     update_pool = TRUE;
1679
1680     /* if there is an allocator, also drop it, as it might be the reason we
1681      * have this limit. Default will be used */
1682     if (allocator) {
1683       gst_object_unref (allocator);
1684       allocator = NULL;
1685     }
1686   }
1687
1688   config = gst_buffer_pool_get_config (pool);
1689   gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1690   /* we are happy with the default allocator but we would like to have 16 bytes
1691    * aligned and padded memory */
1692   gst_buffer_pool_config_set_allocator (config, allocator, &params);
1693
1694   have_videometa =
1695       gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1696   if (have_videometa)
1697     gst_buffer_pool_config_add_option (config,
1698         GST_BUFFER_POOL_OPTION_VIDEO_META);
1699
1700   have_alignment =
1701       gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1702
1703   /* we can only enable the alignment if downstream supports the
1704    * videometa api */
1705   if (have_alignment && have_videometa) {
1706     GstVideoAlignment align;
1707     gint width, height;
1708     gint linesize_align[4];
1709     gint i;
1710     guint edge;
1711
1712     width = GST_VIDEO_INFO_WIDTH (&state->info);
1713     height = GST_VIDEO_INFO_HEIGHT (&state->info);
1714     /* let ffmpeg find the alignment and padding */
1715     avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
1716         linesize_align);
1717     edge =
1718         ffmpegdec->
1719         context->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width ();
1720     /* increase the size for the padding */
1721     width += edge << 1;
1722     height += edge << 1;
1723
1724     align.padding_top = edge;
1725     align.padding_left = edge;
1726     align.padding_right = width - GST_VIDEO_INFO_WIDTH (&state->info) - edge;
1727     align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (&state->info) - edge;
1728
1729     /* add extra padding to match libav buffer allocation sizes */
1730     align.padding_bottom++;
1731
1732     for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
1733       align.stride_align[i] =
1734           (linesize_align[i] > 0 ? linesize_align[i] - 1 : 0);
1735
1736     GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
1737         "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
1738         GST_VIDEO_INFO_WIDTH (&state->info),
1739         GST_VIDEO_INFO_HEIGHT (&state->info), width, height, align.padding_top,
1740         align.padding_left, align.padding_right, align.padding_bottom,
1741         align.stride_align[0], align.stride_align[1], align.stride_align[2],
1742         align.stride_align[3]);
1743
1744     gst_buffer_pool_config_add_option (config,
1745         GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1746     gst_buffer_pool_config_set_video_alignment (config, &align);
1747
1748     if (ffmpegdec->direct_rendering) {
1749       GstFFMpegVidDecClass *oclass;
1750
1751       GST_DEBUG_OBJECT (ffmpegdec, "trying to enable direct rendering");
1752
1753       oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1754
1755       if (oclass->in_plugin->capabilities & CODEC_CAP_DR1) {
1756         GST_DEBUG_OBJECT (ffmpegdec, "enabled direct rendering");
1757         ffmpegdec->current_dr = TRUE;
1758       } else {
1759         GST_DEBUG_OBJECT (ffmpegdec, "direct rendering not supported");
1760       }
1761     }
1762   } else {
1763     GST_DEBUG_OBJECT (ffmpegdec,
1764         "alignment or videometa not supported, disable direct rendering");
1765
1766     /* disable direct rendering. This will make us use the fallback ffmpeg
1767      * picture allocation code with padding etc. We will then do the final
1768      * copy (with cropping) into a buffer from our pool */
1769     ffmpegdec->current_dr = FALSE;
1770   }
1771
1772   /* and store */
1773   gst_buffer_pool_set_config (pool, config);
1774
1775   if (update_pool)
1776     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1777
1778   gst_object_unref (pool);
1779   if (allocator)
1780     gst_object_unref (allocator);
1781   gst_video_codec_state_unref (state);
1782
1783   return TRUE;
1784 }
1785
1786 static gboolean
1787 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1788     GstQuery * query)
1789 {
1790   GstAllocationParams params;
1791
1792   gst_allocation_params_init (&params);
1793   params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1794   params.align = 15;
1795   params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1796   /* we would like to have some padding so that we don't have to
1797    * memcpy. We don't suggest an allocator. */
1798   gst_query_add_allocation_param (query, NULL, &params);
1799
1800   return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1801       query);
1802 }
1803
1804 static void
1805 gst_ffmpegviddec_set_property (GObject * object,
1806     guint prop_id, const GValue * value, GParamSpec * pspec)
1807 {
1808   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1809
1810   switch (prop_id) {
1811     case PROP_LOWRES:
1812       ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1813       break;
1814     case PROP_SKIPFRAME:
1815       ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1816           g_value_get_enum (value);
1817       break;
1818     case PROP_DIRECT_RENDERING:
1819       ffmpegdec->direct_rendering = g_value_get_boolean (value);
1820       break;
1821     case PROP_DEBUG_MV:
1822       ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1823           g_value_get_boolean (value);
1824       break;
1825     case PROP_MAX_THREADS:
1826       ffmpegdec->max_threads = g_value_get_int (value);
1827       break;
1828     case PROP_OUTPUT_CORRUPT:
1829       ffmpegdec->output_corrupt = g_value_get_boolean (value);
1830       break;
1831     default:
1832       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1833       break;
1834   }
1835 }
1836
1837 static void
1838 gst_ffmpegviddec_get_property (GObject * object,
1839     guint prop_id, GValue * value, GParamSpec * pspec)
1840 {
1841   GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1842
1843   switch (prop_id) {
1844     case PROP_LOWRES:
1845       g_value_set_enum (value, ffmpegdec->context->lowres);
1846       break;
1847     case PROP_SKIPFRAME:
1848       g_value_set_enum (value, ffmpegdec->context->skip_frame);
1849       break;
1850     case PROP_DIRECT_RENDERING:
1851       g_value_set_boolean (value, ffmpegdec->direct_rendering);
1852       break;
1853     case PROP_DEBUG_MV:
1854       g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1855       break;
1856     case PROP_MAX_THREADS:
1857       g_value_set_int (value, ffmpegdec->max_threads);
1858       break;
1859     case PROP_OUTPUT_CORRUPT:
1860       g_value_set_boolean (value, ffmpegdec->output_corrupt);
1861       break;
1862     default:
1863       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1864       break;
1865   }
1866 }
1867
1868 gboolean
1869 gst_ffmpegviddec_register (GstPlugin * plugin)
1870 {
1871   GTypeInfo typeinfo = {
1872     sizeof (GstFFMpegVidDecClass),
1873     (GBaseInitFunc) gst_ffmpegviddec_base_init,
1874     NULL,
1875     (GClassInitFunc) gst_ffmpegviddec_class_init,
1876     NULL,
1877     NULL,
1878     sizeof (GstFFMpegVidDec),
1879     0,
1880     (GInstanceInitFunc) gst_ffmpegviddec_init,
1881   };
1882   GType type;
1883   AVCodec *in_plugin;
1884   gint rank;
1885
1886   in_plugin = av_codec_next (NULL);
1887
1888   GST_LOG ("Registering decoders");
1889
1890   while (in_plugin) {
1891     gchar *type_name;
1892     gchar *plugin_name;
1893
1894     /* only video decoders */
1895     if (!av_codec_is_decoder (in_plugin)
1896         || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1897       goto next;
1898
1899     /* no quasi-codecs, please */
1900     if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
1901         in_plugin->id == AV_CODEC_ID_V210 ||
1902         in_plugin->id == AV_CODEC_ID_V210X ||
1903         in_plugin->id == AV_CODEC_ID_R210 ||
1904         (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
1905             in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
1906       goto next;
1907     }
1908
1909     /* No decoders depending on external libraries (we don't build them, but
1910      * people who build against an external ffmpeg might have them.
1911      * We have native gstreamer plugins for all of those libraries anyway. */
1912     if (!strncmp (in_plugin->name, "lib", 3)) {
1913       GST_DEBUG
1914           ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
1915           in_plugin->name);
1916       goto next;
1917     }
1918
1919     /* No vdpau plugins until we can figure out how to properly use them
1920      * outside of ffmpeg. */
1921     if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
1922       GST_DEBUG
1923           ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
1924           in_plugin->name);
1925       goto next;
1926     }
1927
1928     if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
1929       GST_DEBUG
1930           ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
1931           in_plugin->name);
1932       goto next;
1933     }
1934
1935     GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
1936
1937     /* no codecs for which we're GUARANTEED to have better alternatives */
1938     /* MPEG1VIDEO : the mpeg2video decoder is preferred */
1939     /* MP1 : Use MP3 for decoding */
1940     /* MP2 : Use MP3 for decoding */
1941     /* Theora: Use libtheora based theoradec */
1942     if (!strcmp (in_plugin->name, "gif") ||
1943         !strcmp (in_plugin->name, "theora") ||
1944         !strcmp (in_plugin->name, "mpeg1video") ||
1945         strstr (in_plugin->name, "crystalhd") != NULL ||
1946         !strcmp (in_plugin->name, "ass") ||
1947         !strcmp (in_plugin->name, "srt") ||
1948         !strcmp (in_plugin->name, "pgssub") ||
1949         !strcmp (in_plugin->name, "dvdsub") ||
1950         !strcmp (in_plugin->name, "dvbsub")) {
1951       GST_LOG ("Ignoring decoder %s", in_plugin->name);
1952       goto next;
1953     }
1954
1955     /* construct the type */
1956     if (!strcmp (in_plugin->name, "hevc")) {
1957       plugin_name = g_strdup ("h265");
1958     } else {
1959       plugin_name = g_strdup ((gchar *) in_plugin->name);
1960     }
1961     g_strdelimit (plugin_name, NULL, '_');
1962     type_name = g_strdup_printf ("avdec_%s", plugin_name);
1963     g_free (plugin_name);
1964
1965     type = g_type_from_name (type_name);
1966
1967     if (!type) {
1968       /* create the gtype now */
1969       type =
1970           g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
1971           0);
1972       g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
1973     }
1974
1975     /* (Ronald) MPEG-4 gets a higher priority because it has been well-
1976      * tested and by far outperforms divxdec/xviddec - so we prefer it.
1977      * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
1978      * VC1/WMV3 are not working and thus unpreferred for now. */
1979     switch (in_plugin->id) {
1980       case AV_CODEC_ID_MPEG4:
1981       case AV_CODEC_ID_MSMPEG4V3:
1982       case AV_CODEC_ID_H264:
1983       case AV_CODEC_ID_HEVC:
1984       case AV_CODEC_ID_RV10:
1985       case AV_CODEC_ID_RV20:
1986       case AV_CODEC_ID_RV30:
1987       case AV_CODEC_ID_RV40:
1988         rank = GST_RANK_PRIMARY;
1989         break;
1990         /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
1991          * They say libdv's quality is better though. leave as secondary.
1992          * note: if you change this, see the code in gstdv.c in good/ext/dv.
1993          */
1994       case AV_CODEC_ID_DVVIDEO:
1995         rank = GST_RANK_SECONDARY;
1996         break;
1997       default:
1998         rank = GST_RANK_MARGINAL;
1999         break;
2000     }
2001     if (!gst_element_register (plugin, type_name, rank, type)) {
2002       g_warning ("Failed to register %s", type_name);
2003       g_free (type_name);
2004       return FALSE;
2005     }
2006
2007     g_free (type_name);
2008
2009   next:
2010     in_plugin = av_codec_next (in_plugin);
2011   }
2012
2013   GST_LOG ("Finished Registering decoders");
2014
2015   return TRUE;
2016 }