2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
27 #include <libavcodec/avcodec.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>
36 #include "gstavcodecmap.h"
37 #include "gstavutils.h"
38 #include "gstavviddec.h"
40 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
42 #define MAX_TS_MASK 0xff
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 #define DEFAULT_STRIDE_ALIGN 15
52 #define DEFAULT_ALLOC_PARAM { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
59 PROP_DIRECT_RENDERING,
66 /* A number of function prototypes are given so we can refer to them later. */
67 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
68 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
69 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
70 static void gst_ffmpegviddec_finalize (GObject * object);
72 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
73 GstVideoCodecState * state);
74 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
75 GstVideoCodecFrame * frame);
76 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
77 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
78 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
79 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
81 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
84 static void gst_ffmpegviddec_set_property (GObject * object,
85 guint prop_id, const GValue * value, GParamSpec * pspec);
86 static void gst_ffmpegviddec_get_property (GObject * object,
87 guint prop_id, GValue * value, GParamSpec * pspec);
89 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
90 AVCodecContext * context, AVFrame * picture);
92 /* some sort of bufferpool handling, but different */
93 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
94 AVFrame * picture, int flags);
96 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
97 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
99 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
101 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
102 AVCodecContext * context);
104 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
106 static GstElementClass *parent_class = NULL;
108 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
110 gst_ffmpegviddec_lowres_get_type (void)
112 static GType ffmpegdec_lowres_type = 0;
114 if (!ffmpegdec_lowres_type) {
115 static const GEnumValue ffmpegdec_lowres[] = {
117 {1, "1", "1/2-size"},
118 {2, "2", "1/4-size"},
122 ffmpegdec_lowres_type =
123 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
126 return ffmpegdec_lowres_type;
129 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
131 gst_ffmpegviddec_skipframe_get_type (void)
133 static GType ffmpegdec_skipframe_type = 0;
135 if (!ffmpegdec_skipframe_type) {
136 static const GEnumValue ffmpegdec_skipframe[] = {
137 {0, "0", "Skip nothing"},
138 {1, "1", "Skip B-frames"},
139 {2, "2", "Skip IDCT/Dequantization"},
140 {5, "5", "Skip everything"},
144 ffmpegdec_skipframe_type =
145 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
148 return ffmpegdec_skipframe_type;
152 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
154 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 GstPadTemplate *sinktempl, *srctempl;
156 GstCaps *sinkcaps, *srccaps;
158 gchar *longname, *description;
161 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
162 GST_FFDEC_PARAMS_QDATA);
163 g_assert (in_plugin != NULL);
165 /* construct the element details struct */
166 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
167 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
168 gst_element_class_set_metadata (element_class, longname,
169 "Codec/Decoder/Video", description,
170 "Wim Taymans <wim.taymans@gmail.com>, "
171 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
172 "Edward Hervey <bilboed@bilboed.com>");
174 g_free (description);
177 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
179 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
180 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
182 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
183 in_plugin->id, FALSE, in_plugin);
185 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
186 srccaps = gst_caps_from_string ("video/x-raw");
190 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
191 GST_PAD_ALWAYS, sinkcaps);
192 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
194 gst_element_class_add_pad_template (element_class, srctempl);
195 gst_element_class_add_pad_template (element_class, sinktempl);
197 klass->in_plugin = in_plugin;
201 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
203 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
204 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
207 parent_class = g_type_class_peek_parent (klass);
209 gobject_class->finalize = gst_ffmpegviddec_finalize;
211 gobject_class->set_property = gst_ffmpegviddec_set_property;
212 gobject_class->get_property = gst_ffmpegviddec_get_property;
214 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
215 g_param_spec_enum ("skip-frame", "Skip frames",
216 "Which types of frames to skip during decoding",
217 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_LOWRES,
220 g_param_spec_enum ("lowres", "Low resolution",
221 "At which resolution to decode images",
222 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
225 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
226 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
227 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
229 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
230 "Whether libav should print motion vectors on top of the image",
231 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
233 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
234 "Whether libav should output frames even if corrupted",
235 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 caps = klass->in_plugin->capabilities;
238 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
239 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
240 g_param_spec_int ("max-threads", "Maximum decode threads",
241 "Maximum number of worker threads to spawn. (0 = auto)",
242 0, G_MAXINT, DEFAULT_MAX_THREADS,
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 viddec_class->set_format = gst_ffmpegviddec_set_format;
247 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
248 viddec_class->start = gst_ffmpegviddec_start;
249 viddec_class->stop = gst_ffmpegviddec_stop;
250 viddec_class->flush = gst_ffmpegviddec_flush;
251 viddec_class->finish = gst_ffmpegviddec_finish;
252 viddec_class->drain = gst_ffmpegviddec_finish; /* drain and finish are the same to us */
253 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
254 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
258 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
260 GstFFMpegVidDecClass *klass =
261 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
263 /* some ffmpeg data */
264 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
265 ffmpegdec->context->opaque = ffmpegdec;
266 ffmpegdec->picture = av_frame_alloc ();
267 ffmpegdec->opened = FALSE;
268 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
269 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
270 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
271 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
272 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
274 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
275 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
278 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
282 gst_ffmpegviddec_finalize (GObject * object)
284 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
286 av_frame_free (&ffmpegdec->picture);
288 if (ffmpegdec->context != NULL) {
289 gst_ffmpeg_avcodec_close (ffmpegdec->context);
290 av_free (ffmpegdec->context);
291 ffmpegdec->context = NULL;
294 G_OBJECT_CLASS (parent_class)->finalize (object);
298 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
301 g_return_if_fail (context != NULL);
304 context->flags |= flags;
306 context->flags &= ~flags;
311 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
313 GstFFMpegVidDecClass *oclass;
316 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
318 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
320 gst_caps_replace (&ffmpegdec->last_caps, NULL);
322 gst_ffmpeg_avcodec_close (ffmpegdec->context);
323 ffmpegdec->opened = FALSE;
325 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
326 ffmpegdec->stride[i] = -1;
328 gst_buffer_replace (&ffmpegdec->palette, NULL);
330 if (ffmpegdec->context->extradata) {
331 av_free (ffmpegdec->context->extradata);
332 ffmpegdec->context->extradata = NULL;
334 if (ffmpegdec->context->slice_offset) {
335 g_free (ffmpegdec->context->slice_offset);
336 ffmpegdec->context->slice_offset = NULL;
339 if (avcodec_get_context_defaults3 (ffmpegdec->context,
340 oclass->in_plugin) < 0) {
341 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
344 ffmpegdec->context->opaque = ffmpegdec;
351 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
353 GstFFMpegVidDecClass *oclass;
356 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
358 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
361 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
362 ffmpegdec->stride[i] = -1;
364 ffmpegdec->opened = TRUE;
365 ffmpegdec->is_realvideo = FALSE;
367 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
368 oclass->in_plugin->name, oclass->in_plugin->id);
370 switch (oclass->in_plugin->id) {
371 case AV_CODEC_ID_RV10:
372 case AV_CODEC_ID_RV30:
373 case AV_CODEC_ID_RV20:
374 case AV_CODEC_ID_RV40:
375 ffmpegdec->is_realvideo = TRUE;
378 GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
382 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
383 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
390 gst_ffmpegviddec_close (ffmpegdec, TRUE);
391 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
392 oclass->in_plugin->name);
398 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
399 GstVideoCodecState * state)
401 GstStructure *str = gst_caps_get_structure (state->caps, 0);
402 const GValue *palette_v;
405 /* do we have a palette? */
406 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
407 palette = gst_value_get_buffer (palette_v);
408 GST_DEBUG ("got palette data %p", palette);
409 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
410 gst_buffer_replace (&ffmpegdec->palette, palette);
417 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
418 GstVideoCodecState * state)
420 GstFFMpegVidDec *ffmpegdec;
421 GstFFMpegVidDecClass *oclass;
422 GstClockTime latency = GST_CLOCK_TIME_NONE;
423 gboolean ret = FALSE;
425 ffmpegdec = (GstFFMpegVidDec *) decoder;
426 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
428 if (ffmpegdec->last_caps != NULL &&
429 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
433 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
435 GST_OBJECT_LOCK (ffmpegdec);
436 /* stupid check for VC1 */
437 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
438 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
439 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
441 /* close old session */
442 if (ffmpegdec->opened) {
443 GST_OBJECT_UNLOCK (ffmpegdec);
444 gst_ffmpegviddec_drain (ffmpegdec);
445 GST_OBJECT_LOCK (ffmpegdec);
446 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
447 GST_OBJECT_UNLOCK (ffmpegdec);
450 ffmpegdec->pic_pix_fmt = 0;
451 ffmpegdec->pic_width = 0;
452 ffmpegdec->pic_height = 0;
453 ffmpegdec->pic_par_n = 0;
454 ffmpegdec->pic_par_d = 0;
455 ffmpegdec->ctx_ticks = 0;
456 ffmpegdec->ctx_time_n = 0;
457 ffmpegdec->ctx_time_d = 0;
460 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
462 /* set buffer functions */
463 ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
464 ffmpegdec->context->get_buffer = NULL;
465 ffmpegdec->context->reget_buffer = NULL;
466 ffmpegdec->context->release_buffer = NULL;
467 ffmpegdec->context->draw_horiz_band = NULL;
469 /* reset coded_width/_height to prevent it being reused from last time when
470 * the codec is opened again, causing a mismatch and possible
471 * segfault/corruption. (Common scenario when renegotiating caps) */
472 ffmpegdec->context->coded_width = 0;
473 ffmpegdec->context->coded_height = 0;
475 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
476 ffmpegdec->context->height);
478 /* FIXME : Create a method that takes GstVideoCodecState instead */
479 /* get size and so */
480 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
481 oclass->in_plugin->type, state->caps, ffmpegdec->context);
483 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
484 ffmpegdec->context->height);
486 gst_ffmpegviddec_get_palette (ffmpegdec, state);
488 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
489 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
490 ffmpegdec->context->time_base.num = 1;
491 ffmpegdec->context->time_base.den = 25;
494 /* workaround encoder bugs */
495 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
496 ffmpegdec->context->err_recognition = 1;
499 ffmpegdec->context->lowres = ffmpegdec->lowres;
500 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
502 /* ffmpeg can draw motion vectors on top of the image (not every decoder
504 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
510 if (ffmpegdec->max_threads == 0) {
511 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
512 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
514 ffmpegdec->context->thread_count = 0;
516 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
518 query = gst_query_new_latency ();
520 /* Check if upstream is live. If it isn't we can enable frame based
521 * threading, which is adding latency */
522 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
523 gst_query_parse_latency (query, &is_live, NULL, NULL);
525 gst_query_unref (query);
528 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
530 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
533 /* open codec - we don't select an output pix_fmt yet,
534 * simply because we don't know! We only get it
535 * during playback... */
536 if (!gst_ffmpegviddec_open (ffmpegdec))
539 if (ffmpegdec->input_state)
540 gst_video_codec_state_unref (ffmpegdec->input_state);
541 ffmpegdec->input_state = gst_video_codec_state_ref (state);
543 if (ffmpegdec->input_state->info.fps_n) {
544 GstVideoInfo *info = &ffmpegdec->input_state->info;
545 latency = gst_util_uint64_scale_ceil (
546 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
553 GST_OBJECT_UNLOCK (ffmpegdec);
555 if (GST_CLOCK_TIME_IS_VALID (latency))
556 gst_video_decoder_set_latency (decoder, latency, latency);
563 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
570 GstFFMpegVidDec *ffmpegdec;
571 GstVideoCodecFrame *frame;
573 GstVideoFrame vframe;
575 AVBufferRef *avbuffer;
576 } GstFFMpegVidDecVideoFrame;
578 static GstFFMpegVidDecVideoFrame *
579 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
580 GstVideoCodecFrame * frame)
582 GstFFMpegVidDecVideoFrame *dframe;
584 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
585 dframe->ffmpegdec = ffmpegdec;
586 dframe->frame = frame;
588 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
594 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
595 GstFFMpegVidDecVideoFrame * frame)
597 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
600 gst_video_frame_unmap (&frame->vframe);
601 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
602 gst_buffer_replace (&frame->buffer, NULL);
603 if (frame->avbuffer) {
604 av_buffer_unref (&frame->avbuffer);
606 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
610 dummy_free_buffer (void *opaque, uint8_t * data)
612 GstFFMpegVidDecVideoFrame *frame = opaque;
614 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
617 /* This function prepares the pool configuration for direct rendering. To use
618 * this method, the codec should support direct rendering and the pool should
619 * support video meta and video alignement */
621 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
622 GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
624 GstVideoAlignment align;
626 gint linesize_align[4];
630 width = GST_VIDEO_INFO_WIDTH (info);
631 height = GST_VIDEO_INFO_HEIGHT (info);
633 /* let ffmpeg find the alignment and padding */
634 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
637 if (ffmpegdec->context->flags & CODEC_FLAG_EMU_EDGE)
640 edge = avcodec_get_edge_width ();
642 /* increase the size for the padding */
646 align.padding_top = edge;
647 align.padding_left = edge;
648 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info) - edge;
649 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info) - edge;
651 /* add extra padding to match libav buffer allocation sizes */
652 align.padding_bottom++;
654 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
655 align.stride_align[i] = (linesize_align[i] > 0 ? linesize_align[i] - 1 : 0);
657 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
658 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
659 GST_VIDEO_INFO_WIDTH (info),
660 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
661 align.padding_left, align.padding_right, align.padding_bottom,
662 align.stride_align[0], align.stride_align[1], align.stride_align[2],
663 align.stride_align[3]);
665 gst_buffer_pool_config_add_option (config,
666 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
667 gst_buffer_pool_config_set_video_alignment (config, &align);
671 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
672 AVFrame * picture, GstVideoInfo * info)
674 AVCodecContext *context = ffmpegdec->context;
675 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
676 GstVideoFormat format;
678 GstStructure *config;
680 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
681 gst_video_info_set_format (info, format, context->width, context->height);
683 if (ffmpegdec->internal_pool != NULL ||
684 (ffmpegdec->pic_width == context->width &&
685 ffmpegdec->pic_height == context->height &&
686 ffmpegdec->pic_pix_fmt == picture->format))
689 ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
690 config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
692 caps = gst_video_info_to_caps (info);
693 gst_buffer_pool_config_set_params (config, caps, info->size, 2, 0);
694 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
695 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
697 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
698 ffmpegdec->internal_pool, info, config);
699 /* generic video pool never fails */
700 gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
701 gst_caps_unref (caps);
703 gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
707 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
709 GstFFMpegVidDecClass *oclass;
711 if (!ffmpegdec->direct_rendering)
714 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
715 return ((oclass->in_plugin->capabilities & CODEC_CAP_DR1) == CODEC_CAP_DR1);
718 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
719 * into. We try to give it memory from our pool */
721 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
724 GstVideoCodecFrame *frame;
725 GstFFMpegVidDecVideoFrame *dframe;
726 GstFFMpegVidDec *ffmpegdec;
731 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
733 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
735 /* apply the last info we have seen to this picture, when we get the
736 * picture back from ffmpeg we can use this to correctly timestamp the output
738 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
739 (gint32) picture->reordered_opaque);
742 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
743 picture->reordered_opaque);
744 if (G_UNLIKELY (frame == NULL))
747 /* now it has a buffer allocated, so it is real and will also
749 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
750 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
752 if (G_UNLIKELY (frame->output_buffer != NULL))
753 goto duplicate_frame;
755 /* GstFFMpegVidDecVideoFrame receives the frame ref */
756 if (picture->opaque) {
757 dframe = picture->opaque;
758 dframe->frame = frame;
760 picture->opaque = dframe =
761 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
764 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
766 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
769 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture, &info);
771 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
772 &frame->output_buffer, NULL);
773 if (ret != GST_FLOW_OK)
776 /* piggy-backed alloc'ed on the frame,
777 * and there was much rejoicing and we are grateful.
778 * Now take away buffer from frame, we will give it back later when decoded.
779 * This allows multiple request for a buffer per frame; unusual but possible. */
780 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
781 gst_buffer_replace (&frame->output_buffer, NULL);
784 if (!gst_video_frame_map (&dframe->vframe, &info, dframe->buffer,
787 dframe->mapped = TRUE;
789 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
790 if (c < GST_VIDEO_INFO_N_PLANES (&info)) {
791 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
792 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
794 if (ffmpegdec->stride[c] == -1)
795 ffmpegdec->stride[c] = picture->linesize[c];
797 /* libav does not allow stride changes, decide allocation should check
798 * before replacing the internal pool with a downstream pool.
799 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
800 * https://bugzilla.libav.org/show_bug.cgi?id=556
802 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
804 picture->data[c] = NULL;
805 picture->linesize[c] = 0;
807 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
811 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
813 /* tell ffmpeg we own this buffer, transfer the ref we have on the buffer to
814 * the opaque data. */
815 picture->type = FF_BUFFER_TYPE_USER;
817 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
824 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
829 /* alloc default buffer when we can't get one from downstream */
830 GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
835 /* alloc default buffer when we can't get one from downstream */
836 GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
837 gst_buffer_replace (&dframe->buffer, NULL);
843 int ret = avcodec_default_get_buffer2 (context, picture, flags);
845 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
846 ffmpegdec->stride[c] = picture->linesize[c];
848 /* Wrap our buffer around the default one to be able to have a callback
849 * when our data can be freed. Just putting our data into the first free
850 * buffer might not work if there are too many allocated already
852 if (picture->buf[0]) {
853 dframe->avbuffer = picture->buf[0];
855 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
856 dummy_free_buffer, dframe, 0);
859 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
866 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
867 gst_video_codec_frame_unref (frame);
872 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
878 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
880 return !(ffmpegdec->pic_width == picture->width
881 && ffmpegdec->pic_height == picture->height
882 && ffmpegdec->pic_pix_fmt == picture->format
883 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
884 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
885 && ffmpegdec->pic_interlaced == picture->interlaced_frame);
889 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
891 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
892 && ffmpegdec->ctx_time_n == context->time_base.num
893 && ffmpegdec->ctx_time_d == context->time_base.den);
897 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
900 if (!picture_changed (ffmpegdec, picture)
901 && !context_changed (ffmpegdec, context))
904 GST_DEBUG_OBJECT (ffmpegdec,
905 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
906 ffmpegdec->pic_width, ffmpegdec->pic_height,
907 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
908 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
909 ffmpegdec->pic_pix_fmt,
910 picture->width, picture->height,
911 picture->sample_aspect_ratio.num,
912 picture->sample_aspect_ratio.den,
913 context->time_base.num, context->time_base.den, picture->format);
915 ffmpegdec->pic_pix_fmt = picture->format;
916 ffmpegdec->pic_width = picture->width;
917 ffmpegdec->pic_height = picture->height;
918 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
919 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
920 ffmpegdec->pic_interlaced = picture->interlaced_frame;
921 ffmpegdec->ctx_ticks = context->ticks_per_frame;
922 ffmpegdec->ctx_time_n = context->time_base.num;
923 ffmpegdec->ctx_time_d = context->time_base.den;
929 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
930 GstVideoInfo * in_info, GstVideoInfo * out_info)
932 gboolean demuxer_par_set = FALSE;
933 gboolean decoder_par_set = FALSE;
934 gint demuxer_num = 1, demuxer_denom = 1;
935 gint decoder_num = 1, decoder_denom = 1;
937 if (in_info->par_n && in_info->par_d) {
938 demuxer_num = in_info->par_n;
939 demuxer_denom = in_info->par_d;
940 demuxer_par_set = TRUE;
941 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
945 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
946 decoder_num = ffmpegdec->pic_par_n;
947 decoder_denom = ffmpegdec->pic_par_d;
948 decoder_par_set = TRUE;
949 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
953 if (!demuxer_par_set && !decoder_par_set)
956 if (demuxer_par_set && !decoder_par_set)
957 goto use_demuxer_par;
959 if (decoder_par_set && !demuxer_par_set)
960 goto use_decoder_par;
962 /* Both the demuxer and the decoder provide a PAR. If one of
963 * the two PARs is 1:1 and the other one is not, use the one
964 * that is not 1:1. */
965 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
966 goto use_decoder_par;
968 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
969 goto use_demuxer_par;
971 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
972 goto use_demuxer_par;
976 GST_DEBUG_OBJECT (ffmpegdec,
977 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
979 out_info->par_n = decoder_num;
980 out_info->par_d = decoder_denom;
985 GST_DEBUG_OBJECT (ffmpegdec,
986 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
988 out_info->par_n = demuxer_num;
989 out_info->par_d = demuxer_denom;
994 GST_DEBUG_OBJECT (ffmpegdec,
995 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1003 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1004 AVCodecContext * context, AVFrame * picture)
1007 GstVideoInfo *in_info, *out_info;
1008 GstVideoCodecState *output_state;
1011 if (!update_video_context (ffmpegdec, context, picture))
1014 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1015 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1016 goto unknown_format;
1019 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1020 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1021 if (ffmpegdec->output_state)
1022 gst_video_codec_state_unref (ffmpegdec->output_state);
1023 ffmpegdec->output_state = output_state;
1025 in_info = &ffmpegdec->input_state->info;
1026 out_info = &ffmpegdec->output_state->info;
1028 /* set the interlaced flag */
1029 if (ffmpegdec->pic_interlaced)
1030 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1032 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1034 switch (context->chroma_sample_location) {
1036 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1039 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1042 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1045 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1051 /* try to find a good framerate */
1052 if ((in_info->fps_d && in_info->fps_n) ||
1053 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1054 /* take framerate from input when it was specified (#313970) */
1055 fps_n = in_info->fps_n;
1056 fps_d = in_info->fps_d;
1058 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1059 fps_d = ffmpegdec->ctx_time_n;
1062 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1066 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1067 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1074 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1075 out_info->fps_n = fps_n;
1076 out_info->fps_d = fps_d;
1078 /* calculate and update par now */
1079 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1081 /* Copy stereo/multiview info from upstream if set */
1082 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
1083 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) =
1084 GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1085 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) =
1086 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1089 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1090 goto negotiate_failed;
1097 GST_ERROR_OBJECT (ffmpegdec,
1098 "decoder requires a video format unsupported by GStreamer");
1103 /* Reset so we try again next time even if force==FALSE */
1104 ffmpegdec->pic_pix_fmt = 0;
1105 ffmpegdec->pic_width = 0;
1106 ffmpegdec->pic_height = 0;
1107 ffmpegdec->pic_par_n = 0;
1108 ffmpegdec->pic_par_d = 0;
1109 ffmpegdec->ctx_ticks = 0;
1110 ffmpegdec->ctx_time_n = 0;
1111 ffmpegdec->ctx_time_d = 0;
1113 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1118 /* perform qos calculations before decoding the next frame.
1120 * Sets the skip_frame flag and if things are really bad, skips to the next
1125 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1126 GstVideoCodecFrame * frame, gboolean * mode_switch)
1128 GstClockTimeDiff diff;
1129 GstSegmentFlags skip_flags =
1130 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1132 *mode_switch = FALSE;
1137 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1138 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1139 *mode_switch = TRUE;
1140 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1141 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1142 *mode_switch = TRUE;
1146 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1149 /* if we don't have timing info, then we don't do QoS */
1150 if (G_UNLIKELY (diff == G_MAXINT64))
1153 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1155 if (*mode_switch == FALSE) {
1156 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1157 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1158 *mode_switch = TRUE;
1159 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1162 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1163 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1164 *mode_switch = TRUE;
1165 GST_DEBUG_OBJECT (ffmpegdec,
1166 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1171 /* get an outbuf buffer with the current picture */
1172 static GstFlowReturn
1173 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1175 GstFlowReturn ret = GST_FLOW_OK;
1176 AVPicture pic, *outpic;
1177 GstVideoFrame vframe;
1181 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1183 if (!ffmpegdec->output_state)
1184 goto not_negotiated;
1187 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1189 if (G_UNLIKELY (ret != GST_FLOW_OK))
1192 /* original ffmpeg code does not handle odd sizes correctly.
1193 * This patched up version does */
1194 /* Fill avpicture */
1195 info = &ffmpegdec->output_state->info;
1196 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1197 GST_MAP_READ | GST_MAP_WRITE))
1200 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1201 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1202 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1203 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1206 pic.linesize[c] = 0;
1208 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", pic.linesize[c],
1212 outpic = (AVPicture *) ffmpegdec->picture;
1214 av_picture_copy (&pic, outpic, ffmpegdec->context->pix_fmt,
1215 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
1217 gst_video_frame_unmap (&vframe);
1219 ffmpegdec->picture->reordered_opaque = -1;
1226 GST_DEBUG_OBJECT (ffmpegdec, "allocation failed");
1231 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1232 return GST_FLOW_NOT_NEGOTIATED;
1237 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1239 memset (packet, 0, sizeof (AVPacket));
1240 packet->data = data;
1241 packet->size = size;
1244 /* gst_ffmpegviddec_[video|audio]_frame:
1246 * data: pointer to the data to decode
1247 * size: size of data in bytes
1248 * in_timestamp: incoming timestamp.
1249 * in_duration: incoming duration.
1250 * in_offset: incoming offset (frame number).
1253 * Returns: number of bytes used in decoding. The check for successful decode is
1254 * outbuf being non-NULL.
1257 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1258 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1259 GstFlowReturn * ret)
1262 gboolean mode_switch;
1263 GstVideoCodecFrame *out_frame;
1264 GstFFMpegVidDecVideoFrame *out_dframe;
1266 GstBufferPool *pool;
1270 /* in case we skip frames */
1271 ffmpegdec->picture->pict_type = -1;
1273 /* run QoS code, we don't stop decoding the frame when we are late because
1274 * else we might skip a reference frame */
1275 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1277 if (ffmpegdec->is_realvideo && data != NULL) {
1281 /* setup the slice table for realvideo */
1282 if (ffmpegdec->context->slice_offset == NULL)
1283 ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1285 slice_count = (*data++) + 1;
1286 ffmpegdec->context->slice_count = slice_count;
1288 for (i = 0; i < slice_count; i++) {
1290 ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1296 /* save reference to the timing info */
1297 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1298 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1300 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1301 frame->system_frame_number);
1304 /* now decode the frame */
1305 gst_avpacket_init (&packet, data, size);
1307 if (ffmpegdec->palette) {
1310 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1312 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1313 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1316 /* This might call into get_buffer() from another thread,
1317 * which would cause a deadlock. Release the lock here
1318 * and taking it again later seems safe
1319 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1321 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1322 len = avcodec_decode_video2 (ffmpegdec->context,
1323 ffmpegdec->picture, have_data, &packet);
1324 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1326 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1329 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1330 * no data because we told it to skip stuff. */
1331 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1334 /* no data, we're done */
1335 if (len < 0 || *have_data == 0)
1338 /* get the output picture timing info again */
1339 out_dframe = ffmpegdec->picture->opaque;
1340 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1342 /* also give back a buffer allocated by the frame, if any */
1343 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1344 gst_buffer_replace (&out_dframe->buffer, NULL);
1346 GST_DEBUG_OBJECT (ffmpegdec,
1347 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1348 out_frame->pts, out_frame->duration);
1349 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1350 (guint64) ffmpegdec->picture->pts);
1351 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1352 ffmpegdec->picture->coded_picture_number);
1353 GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1354 ffmpegdec->picture->reference);
1355 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1356 ffmpegdec->picture->display_picture_number);
1357 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1358 ffmpegdec->picture->opaque);
1359 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1360 (guint64) ffmpegdec->picture->reordered_opaque);
1361 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1362 ffmpegdec->picture->repeat_pict);
1363 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1364 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1366 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1367 ffmpegdec->picture))
1368 goto negotiation_error;
1370 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1371 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1372 *ret = get_output_buffer (ffmpegdec, out_frame);
1373 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1374 GstBuffer *tmp = out_frame->output_buffer;
1375 out_frame->output_buffer = NULL;
1376 *ret = get_output_buffer (ffmpegdec, out_frame);
1377 gst_buffer_unref (tmp);
1379 gst_object_unref (pool);
1381 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1384 /* Mark corrupted frames as corrupted */
1385 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1386 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1388 if (ffmpegdec->pic_interlaced) {
1389 /* set interlaced flags */
1390 if (ffmpegdec->picture->repeat_pict)
1391 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1392 if (ffmpegdec->picture->top_field_first)
1393 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1394 if (ffmpegdec->picture->interlaced_frame)
1395 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1396 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1400 /* so we decoded this frame, frames preceding it in decoding order
1401 * that still do not have a buffer allocated seem rather useless,
1402 * and can be discarded, due to e.g. misparsed bogus frame
1403 * or non-keyframe in skipped decoding, ...
1404 * In any case, not likely to be seen again, so discard those,
1405 * before they pile up and/or mess with timestamping */
1408 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1409 gboolean old = TRUE;
1411 ol = l = gst_video_decoder_get_frames (dec);
1413 GstVideoCodecFrame *tmp = l->data;
1418 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1419 GST_LOG_OBJECT (dec,
1420 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1421 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1422 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1423 /* drop extra ref and remove from frame list */
1424 gst_video_decoder_release_frame (dec, tmp);
1426 /* drop extra ref we got */
1427 gst_video_codec_frame_unref (tmp);
1434 av_frame_unref (ffmpegdec->picture);
1436 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1437 * libav might still have a reference to it!
1440 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1443 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1444 gst_flow_get_name (*ret), len);
1450 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1451 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1458 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1459 *ret = GST_FLOW_FLUSHING;
1462 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1463 *ret = GST_FLOW_NOT_NEGOTIATED;
1469 /* gst_ffmpegviddec_frame:
1471 * data: pointer to the data to decode
1472 * size: size of data in bytes
1473 * got_data: 0 if no data was decoded, != 0 otherwise.
1474 * in_time: timestamp of data
1475 * in_duration: duration of data
1476 * ret: GstFlowReturn to return in the chain function
1478 * Decode the given frame and pushes it downstream.
1480 * Returns: Number of bytes used in decoding, -1 on error/failure.
1484 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1485 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1486 GstFlowReturn * ret)
1488 GstFFMpegVidDecClass *oclass;
1491 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1494 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1497 ffmpegdec->context->frame_number++;
1499 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1502 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1506 GST_WARNING_OBJECT (ffmpegdec,
1507 "avdec_%s: decoding error (len: %d, have_data: %d)",
1508 oclass->in_plugin->name, len, *have_data);
1516 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1517 *ret = GST_FLOW_NOT_NEGOTIATED;
1523 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1525 GstFFMpegVidDecClass *oclass;
1527 if (!ffmpegdec->opened)
1530 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1532 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1533 gint have_data, len;
1536 GST_LOG_OBJECT (ffmpegdec,
1537 "codec has delay capabilities, calling until ffmpeg has drained everything");
1540 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1541 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1542 avcodec_flush_buffers (ffmpegdec->context);
1546 static GstFlowReturn
1547 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1548 GstVideoCodecFrame * frame)
1550 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1551 guint8 *data, *bdata;
1552 gint size, len, have_data, bsize;
1554 GstFlowReturn ret = GST_FLOW_OK;
1555 gboolean do_padding;
1557 GST_LOG_OBJECT (ffmpegdec,
1558 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1559 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1560 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1561 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1563 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1564 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1565 ("Failed to map buffer for reading"));
1566 return GST_FLOW_ERROR;
1569 /* treat frame as void until a buffer is requested for it */
1570 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1571 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1576 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1577 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1579 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1580 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1581 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1582 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1583 ffmpegdec->padded_size);
1585 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1586 "Copy input to add padding");
1587 memcpy (ffmpegdec->padded, bdata, bsize);
1588 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1590 bdata = ffmpegdec->padded;
1597 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1599 /* parse, if at all possible */
1604 /* add temporary padding */
1605 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1606 "Add temporary input padding");
1607 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1608 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1611 /* decode a frame of audio/video now */
1613 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1615 if (ret != GST_FLOW_OK) {
1616 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1617 gst_flow_get_name (ret));
1618 /* bad flow return, make sure we discard all data and exit */
1624 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1627 if (len == 0 && have_data == 0) {
1628 /* nothing was decoded, this could be because no data was available or
1629 * because we were skipping frames.
1630 * If we have no context we must exit and wait for more data, we keep the
1632 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1637 /* a decoding error happened, we must break and try again with next data. */
1638 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1643 /* prepare for the next round, for codecs with a context we did this
1644 * already when using the parser. */
1650 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1652 } while (bsize > 0);
1655 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1657 gst_buffer_unmap (frame->input_buffer, &minfo);
1658 gst_video_codec_frame_unref (frame);
1664 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1666 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1667 GstFFMpegVidDecClass *oclass;
1669 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1671 GST_OBJECT_LOCK (ffmpegdec);
1672 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1673 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1674 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1675 GST_OBJECT_UNLOCK (ffmpegdec);
1678 ffmpegdec->context->opaque = ffmpegdec;
1679 GST_OBJECT_UNLOCK (ffmpegdec);
1685 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1687 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1689 GST_OBJECT_LOCK (ffmpegdec);
1690 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1691 GST_OBJECT_UNLOCK (ffmpegdec);
1692 g_free (ffmpegdec->padded);
1693 ffmpegdec->padded = NULL;
1694 ffmpegdec->padded_size = 0;
1695 if (ffmpegdec->input_state)
1696 gst_video_codec_state_unref (ffmpegdec->input_state);
1697 ffmpegdec->input_state = NULL;
1698 if (ffmpegdec->output_state)
1699 gst_video_codec_state_unref (ffmpegdec->output_state);
1700 ffmpegdec->output_state = NULL;
1702 if (ffmpegdec->internal_pool)
1703 gst_object_unref (ffmpegdec->internal_pool);
1704 ffmpegdec->internal_pool = NULL;
1706 ffmpegdec->pic_pix_fmt = 0;
1707 ffmpegdec->pic_width = 0;
1708 ffmpegdec->pic_height = 0;
1709 ffmpegdec->pic_par_n = 0;
1710 ffmpegdec->pic_par_d = 0;
1711 ffmpegdec->ctx_ticks = 0;
1712 ffmpegdec->ctx_time_n = 0;
1713 ffmpegdec->ctx_time_d = 0;
1718 static GstFlowReturn
1719 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1721 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1723 gst_ffmpegviddec_drain (ffmpegdec);
1729 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1731 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1733 if (ffmpegdec->opened)
1734 avcodec_flush_buffers (ffmpegdec->context);
1740 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1742 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1743 GstVideoCodecState *state;
1744 GstBufferPool *pool;
1745 guint size, min, max;
1746 GstStructure *config;
1747 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1748 GstAllocator *allocator = NULL;
1749 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1751 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1753 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1757 state = gst_video_decoder_get_output_state (decoder);
1759 if (gst_query_get_n_allocation_params (query) > 0) {
1760 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1761 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1763 gst_query_add_allocation_param (query, allocator, ¶ms);
1766 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1768 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1769 * need, otherwise we may stall */
1770 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1771 gst_object_unref (pool);
1772 pool = gst_video_buffer_pool_new ();
1777 /* if there is an allocator, also drop it, as it might be the reason we
1778 * have this limit. Default will be used */
1780 gst_object_unref (allocator);
1785 config = gst_buffer_pool_get_config (pool);
1786 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1787 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1790 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1793 gst_buffer_pool_config_add_option (config,
1794 GST_BUFFER_POOL_OPTION_VIDEO_META);
1797 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1799 /* If have videometa, we never have to copy */
1800 if (have_videometa && have_pool && have_alignment &&
1801 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
1802 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info, config);
1804 /* FIXME validate and retry */
1805 if (gst_buffer_pool_set_config (pool, config)) {
1809 gst_buffer_pool_set_active (pool, TRUE);
1810 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
1811 if (ret == GST_FLOW_OK) {
1812 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
1813 gboolean same_stride = TRUE;
1816 for (i = 0; i < vmeta->n_planes; i++) {
1817 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
1818 same_stride = FALSE;
1823 gst_buffer_unref (tmp);
1826 if (ffmpegdec->internal_pool)
1827 gst_object_unref (ffmpegdec->internal_pool);
1828 ffmpegdec->internal_pool = gst_object_ref (pool);
1835 if (have_videometa && ffmpegdec->internal_pool) {
1837 gst_object_unref (pool);
1838 pool = gst_object_ref (ffmpegdec->internal_pool);
1843 if (!gst_buffer_pool_set_config (pool, config)) {
1844 gboolean working_pool = FALSE;
1845 config = gst_buffer_pool_get_config (pool);
1847 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
1849 working_pool = gst_buffer_pool_set_config (pool, config);
1852 if (!working_pool) {
1853 gst_object_unref (pool);
1854 pool = gst_video_buffer_pool_new ();
1855 config = gst_buffer_pool_get_config (pool);
1856 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1857 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
1858 gst_buffer_pool_set_config (pool, config);
1866 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1868 gst_object_unref (pool);
1870 gst_object_unref (allocator);
1871 gst_video_codec_state_unref (state);
1877 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1880 GstAllocationParams params;
1882 gst_allocation_params_init (¶ms);
1883 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1884 params.align = DEFAULT_STRIDE_ALIGN;
1885 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1886 /* we would like to have some padding so that we don't have to
1887 * memcpy. We don't suggest an allocator. */
1888 gst_query_add_allocation_param (query, NULL, ¶ms);
1890 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1895 gst_ffmpegviddec_set_property (GObject * object,
1896 guint prop_id, const GValue * value, GParamSpec * pspec)
1898 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1902 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1904 case PROP_SKIPFRAME:
1905 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1906 g_value_get_enum (value);
1908 case PROP_DIRECT_RENDERING:
1909 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1912 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1913 g_value_get_boolean (value);
1915 case PROP_MAX_THREADS:
1916 ffmpegdec->max_threads = g_value_get_int (value);
1918 case PROP_OUTPUT_CORRUPT:
1919 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1922 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1928 gst_ffmpegviddec_get_property (GObject * object,
1929 guint prop_id, GValue * value, GParamSpec * pspec)
1931 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1935 g_value_set_enum (value, ffmpegdec->context->lowres);
1937 case PROP_SKIPFRAME:
1938 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1940 case PROP_DIRECT_RENDERING:
1941 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1944 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1946 case PROP_MAX_THREADS:
1947 g_value_set_int (value, ffmpegdec->max_threads);
1949 case PROP_OUTPUT_CORRUPT:
1950 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1959 gst_ffmpegviddec_register (GstPlugin * plugin)
1961 GTypeInfo typeinfo = {
1962 sizeof (GstFFMpegVidDecClass),
1963 (GBaseInitFunc) gst_ffmpegviddec_base_init,
1965 (GClassInitFunc) gst_ffmpegviddec_class_init,
1968 sizeof (GstFFMpegVidDec),
1970 (GInstanceInitFunc) gst_ffmpegviddec_init,
1976 in_plugin = av_codec_next (NULL);
1978 GST_LOG ("Registering decoders");
1984 /* only video decoders */
1985 if (!av_codec_is_decoder (in_plugin)
1986 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1989 /* no quasi-codecs, please */
1990 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
1991 in_plugin->id == AV_CODEC_ID_V210 ||
1992 in_plugin->id == AV_CODEC_ID_V210X ||
1993 in_plugin->id == AV_CODEC_ID_R210 ||
1994 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
1995 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
1999 /* No decoders depending on external libraries (we don't build them, but
2000 * people who build against an external ffmpeg might have them.
2001 * We have native gstreamer plugins for all of those libraries anyway. */
2002 if (!strncmp (in_plugin->name, "lib", 3)) {
2004 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2009 /* No vdpau plugins until we can figure out how to properly use them
2010 * outside of ffmpeg. */
2011 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2013 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2018 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2020 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2025 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2027 /* no codecs for which we're GUARANTEED to have better alternatives */
2028 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2029 /* MP1 : Use MP3 for decoding */
2030 /* MP2 : Use MP3 for decoding */
2031 /* Theora: Use libtheora based theoradec */
2032 if (!strcmp (in_plugin->name, "gif") ||
2033 !strcmp (in_plugin->name, "theora") ||
2034 !strcmp (in_plugin->name, "mpeg1video") ||
2035 strstr (in_plugin->name, "crystalhd") != NULL ||
2036 !strcmp (in_plugin->name, "ass") ||
2037 !strcmp (in_plugin->name, "srt") ||
2038 !strcmp (in_plugin->name, "pgssub") ||
2039 !strcmp (in_plugin->name, "dvdsub") ||
2040 !strcmp (in_plugin->name, "dvbsub")) {
2041 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2045 /* construct the type */
2046 if (!strcmp (in_plugin->name, "hevc")) {
2047 plugin_name = g_strdup ("h265");
2049 plugin_name = g_strdup ((gchar *) in_plugin->name);
2051 g_strdelimit (plugin_name, NULL, '_');
2052 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2053 g_free (plugin_name);
2055 type = g_type_from_name (type_name);
2058 /* create the gtype now */
2060 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2062 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2065 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2066 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2067 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2068 * VC1/WMV3 are not working and thus unpreferred for now. */
2069 switch (in_plugin->id) {
2070 case AV_CODEC_ID_MPEG4:
2071 case AV_CODEC_ID_MSMPEG4V3:
2072 case AV_CODEC_ID_H264:
2073 case AV_CODEC_ID_HEVC:
2074 case AV_CODEC_ID_RV10:
2075 case AV_CODEC_ID_RV20:
2076 case AV_CODEC_ID_RV30:
2077 case AV_CODEC_ID_RV40:
2078 rank = GST_RANK_PRIMARY;
2080 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2081 * They say libdv's quality is better though. leave as secondary.
2082 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2084 case AV_CODEC_ID_DVVIDEO:
2085 rank = GST_RANK_SECONDARY;
2088 rank = GST_RANK_MARGINAL;
2091 if (!gst_element_register (plugin, type_name, rank, type)) {
2092 g_warning ("Failed to register %s", type_name);
2100 in_plugin = av_codec_next (in_plugin);
2103 GST_LOG ("Finished Registering decoders");