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