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