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
57 PROP_DIRECT_RENDERING,
64 /* A number of function prototypes are given so we can refer to them later. */
65 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
66 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
67 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
68 static void gst_ffmpegviddec_finalize (GObject * object);
70 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
71 GstVideoCodecState * state);
72 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
73 GstVideoCodecFrame * frame);
74 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
75 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
76 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
77 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
79 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
82 static void gst_ffmpegviddec_set_property (GObject * object,
83 guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_ffmpegviddec_get_property (GObject * object,
85 guint prop_id, GValue * value, GParamSpec * pspec);
87 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
88 AVCodecContext * context, AVFrame * picture, gboolean force);
90 /* some sort of bufferpool handling, but different */
91 static int gst_ffmpegviddec_get_buffer (AVCodecContext * context,
93 static int gst_ffmpegviddec_reget_buffer (AVCodecContext * context,
95 static void gst_ffmpegviddec_release_buffer (AVCodecContext * context,
98 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
99 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
101 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
103 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
104 AVCodecContext * context);
106 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
108 static GstElementClass *parent_class = NULL;
110 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
112 gst_ffmpegviddec_lowres_get_type (void)
114 static GType ffmpegdec_lowres_type = 0;
116 if (!ffmpegdec_lowres_type) {
117 static const GEnumValue ffmpegdec_lowres[] = {
119 {1, "1", "1/2-size"},
120 {2, "2", "1/4-size"},
124 ffmpegdec_lowres_type =
125 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
128 return ffmpegdec_lowres_type;
131 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
133 gst_ffmpegviddec_skipframe_get_type (void)
135 static GType ffmpegdec_skipframe_type = 0;
137 if (!ffmpegdec_skipframe_type) {
138 static const GEnumValue ffmpegdec_skipframe[] = {
139 {0, "0", "Skip nothing"},
140 {1, "1", "Skip B-frames"},
141 {2, "2", "Skip IDCT/Dequantization"},
142 {5, "5", "Skip everything"},
146 ffmpegdec_skipframe_type =
147 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
150 return ffmpegdec_skipframe_type;
154 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
156 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
157 GstPadTemplate *sinktempl, *srctempl;
158 GstCaps *sinkcaps, *srccaps;
160 gchar *longname, *description;
163 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
164 GST_FFDEC_PARAMS_QDATA);
165 g_assert (in_plugin != NULL);
167 /* construct the element details struct */
168 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
169 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
170 gst_element_class_set_metadata (element_class, longname,
171 "Codec/Decoder/Video", description,
172 "Wim Taymans <wim.taymans@gmail.com>, "
173 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
174 "Edward Hervey <bilboed@bilboed.com>");
176 g_free (description);
179 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
181 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
182 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
184 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
185 in_plugin->id, FALSE, in_plugin);
187 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
188 srccaps = gst_caps_from_string ("video/x-raw");
192 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
193 GST_PAD_ALWAYS, sinkcaps);
194 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
196 gst_element_class_add_pad_template (element_class, srctempl);
197 gst_element_class_add_pad_template (element_class, sinktempl);
199 klass->in_plugin = in_plugin;
203 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
206 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
209 parent_class = g_type_class_peek_parent (klass);
211 gobject_class->finalize = gst_ffmpegviddec_finalize;
213 gobject_class->set_property = gst_ffmpegviddec_set_property;
214 gobject_class->get_property = gst_ffmpegviddec_get_property;
216 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
217 g_param_spec_enum ("skip-frame", "Skip frames",
218 "Which types of frames to skip during decoding",
219 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_LOWRES,
222 g_param_spec_enum ("lowres", "Low resolution",
223 "At which resolution to decode images",
224 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
227 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
228 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
229 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
231 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
232 "Whether libav should print motion vectors on top of the image",
233 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
235 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
236 "Whether libav should output frames even if corrupted",
237 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 caps = klass->in_plugin->capabilities;
240 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
241 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
242 g_param_spec_int ("max-threads", "Maximum decode threads",
243 "Maximum number of worker threads to spawn. (0 = auto)",
244 0, G_MAXINT, DEFAULT_MAX_THREADS,
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 viddec_class->set_format = gst_ffmpegviddec_set_format;
249 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
250 viddec_class->start = gst_ffmpegviddec_start;
251 viddec_class->stop = gst_ffmpegviddec_stop;
252 viddec_class->flush = gst_ffmpegviddec_flush;
253 viddec_class->finish = gst_ffmpegviddec_finish;
254 viddec_class->drain = gst_ffmpegviddec_finish; /* drain and finish are the same to us */
255 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
256 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
260 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
262 GstFFMpegVidDecClass *klass =
263 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
265 /* some ffmpeg data */
266 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
267 ffmpegdec->context->opaque = ffmpegdec;
268 ffmpegdec->picture = avcodec_alloc_frame ();
269 ffmpegdec->opened = FALSE;
270 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
271 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
272 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
273 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
274 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
276 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
280 gst_ffmpegviddec_finalize (GObject * object)
282 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
284 if (ffmpegdec->context != NULL) {
285 gst_ffmpeg_avcodec_close (ffmpegdec->context);
286 av_free (ffmpegdec->context);
287 ffmpegdec->context = NULL;
290 avcodec_free_frame (&ffmpegdec->picture);
292 G_OBJECT_CLASS (parent_class)->finalize (object);
296 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
299 g_return_if_fail (context != NULL);
302 context->flags |= flags;
304 context->flags &= ~flags;
309 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
311 GstFFMpegVidDecClass *oclass;
314 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
316 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
318 gst_caps_replace (&ffmpegdec->last_caps, NULL);
320 gst_ffmpeg_avcodec_close (ffmpegdec->context);
321 ffmpegdec->opened = FALSE;
323 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
324 ffmpegdec->stride[i] = -1;
326 gst_buffer_replace (&ffmpegdec->palette, NULL);
328 if (ffmpegdec->context->extradata) {
329 av_free (ffmpegdec->context->extradata);
330 ffmpegdec->context->extradata = NULL;
334 if (avcodec_get_context_defaults3 (ffmpegdec->context,
335 oclass->in_plugin) < 0) {
336 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
339 ffmpegdec->context->opaque = ffmpegdec;
346 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
348 GstFFMpegVidDecClass *oclass;
351 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
353 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
356 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
357 ffmpegdec->stride[i] = -1;
359 ffmpegdec->opened = TRUE;
360 ffmpegdec->is_realvideo = FALSE;
362 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
363 oclass->in_plugin->name, oclass->in_plugin->id);
365 switch (oclass->in_plugin->id) {
366 case AV_CODEC_ID_RV10:
367 case AV_CODEC_ID_RV30:
368 case AV_CODEC_ID_RV20:
369 case AV_CODEC_ID_RV40:
370 ffmpegdec->is_realvideo = TRUE;
373 GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
377 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
378 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
385 gst_ffmpegviddec_close (ffmpegdec, TRUE);
386 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
387 oclass->in_plugin->name);
393 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
394 GstVideoCodecState * state)
396 GstStructure *str = gst_caps_get_structure (state->caps, 0);
397 const GValue *palette_v;
400 /* do we have a palette? */
401 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
402 palette = gst_value_get_buffer (palette_v);
403 GST_DEBUG ("got palette data %p", palette);
404 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
405 gst_buffer_replace (&ffmpegdec->palette, palette);
412 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
413 GstVideoCodecState * state)
415 GstFFMpegVidDec *ffmpegdec;
416 GstFFMpegVidDecClass *oclass;
417 GstClockTime latency = GST_CLOCK_TIME_NONE;
418 gboolean ret = FALSE;
420 ffmpegdec = (GstFFMpegVidDec *) decoder;
421 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
423 if (ffmpegdec->last_caps != NULL &&
424 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
428 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
430 GST_OBJECT_LOCK (ffmpegdec);
431 /* stupid check for VC1 */
432 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
433 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
434 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
436 /* close old session */
437 if (ffmpegdec->opened) {
438 GST_OBJECT_UNLOCK (ffmpegdec);
439 gst_ffmpegviddec_drain (ffmpegdec);
440 GST_OBJECT_LOCK (ffmpegdec);
441 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
442 GST_OBJECT_UNLOCK (ffmpegdec);
445 ffmpegdec->pic_pix_fmt = 0;
446 ffmpegdec->pic_width = 0;
447 ffmpegdec->pic_height = 0;
448 ffmpegdec->pic_par_n = 0;
449 ffmpegdec->pic_par_d = 0;
450 ffmpegdec->ctx_ticks = 0;
451 ffmpegdec->ctx_time_n = 0;
452 ffmpegdec->ctx_time_d = 0;
455 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
457 /* set buffer functions */
458 ffmpegdec->context->get_buffer = gst_ffmpegviddec_get_buffer;
459 ffmpegdec->context->reget_buffer = gst_ffmpegviddec_reget_buffer;
460 ffmpegdec->context->release_buffer = gst_ffmpegviddec_release_buffer;
461 ffmpegdec->context->draw_horiz_band = NULL;
463 /* reset coded_width/_height to prevent it being reused from last time when
464 * the codec is opened again, causing a mismatch and possible
465 * segfault/corruption. (Common scenario when renegotiating caps) */
466 ffmpegdec->context->coded_width = 0;
467 ffmpegdec->context->coded_height = 0;
469 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
470 ffmpegdec->context->height);
472 /* FIXME : Create a method that takes GstVideoCodecState instead */
473 /* get size and so */
474 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
475 oclass->in_plugin->type, state->caps, ffmpegdec->context);
477 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
478 ffmpegdec->context->height);
480 gst_ffmpegviddec_get_palette (ffmpegdec, state);
482 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
483 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
484 ffmpegdec->context->time_base.num = 1;
485 ffmpegdec->context->time_base.den = 25;
488 /* workaround encoder bugs */
489 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
490 ffmpegdec->context->err_recognition = 1;
493 ffmpegdec->context->lowres = ffmpegdec->lowres;
494 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
496 /* ffmpeg can draw motion vectors on top of the image (not every decoder
498 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
504 if (ffmpegdec->max_threads == 0) {
505 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
506 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
508 ffmpegdec->context->thread_count = 0;
510 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
512 query = gst_query_new_latency ();
514 /* Check if upstream is live. If it isn't we can enable frame based
515 * threading, which is adding latency */
516 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
517 gst_query_parse_latency (query, &is_live, NULL, NULL);
519 gst_query_unref (query);
522 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
524 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
527 /* open codec - we don't select an output pix_fmt yet,
528 * simply because we don't know! We only get it
529 * during playback... */
530 if (!gst_ffmpegviddec_open (ffmpegdec))
533 if (ffmpegdec->input_state)
534 gst_video_codec_state_unref (ffmpegdec->input_state);
535 ffmpegdec->input_state = gst_video_codec_state_ref (state);
537 if (ffmpegdec->input_state->info.fps_n) {
538 GstVideoInfo *info = &ffmpegdec->input_state->info;
539 latency = gst_util_uint64_scale_ceil (
540 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
547 GST_OBJECT_UNLOCK (ffmpegdec);
549 if (GST_CLOCK_TIME_IS_VALID (latency))
550 gst_video_decoder_set_latency (decoder, latency, latency);
557 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
564 GstFFMpegVidDec *ffmpegdec;
565 GstVideoCodecFrame *frame;
567 GstVideoFrame vframe;
569 AVBufferRef *avbuffer;
570 } GstFFMpegVidDecVideoFrame;
572 static GstFFMpegVidDecVideoFrame *
573 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
574 GstVideoCodecFrame * frame)
576 GstFFMpegVidDecVideoFrame *dframe;
578 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
579 dframe->ffmpegdec = ffmpegdec;
580 dframe->frame = frame;
582 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
588 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
589 GstFFMpegVidDecVideoFrame * frame)
591 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
594 gst_video_frame_unmap (&frame->vframe);
595 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
596 gst_buffer_replace (&frame->buffer, NULL);
597 if (frame->avbuffer) {
598 av_buffer_unref (&frame->avbuffer);
600 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
604 dummy_free_buffer (void *opaque, uint8_t * data)
606 GstFFMpegVidDecVideoFrame *frame = opaque;
608 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
611 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
612 * into. We try to give it memory from our pool */
614 gst_ffmpegviddec_get_buffer (AVCodecContext * context, AVFrame * picture)
616 GstVideoCodecFrame *frame;
617 GstFFMpegVidDecVideoFrame *dframe;
618 GstFFMpegVidDec *ffmpegdec;
623 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
625 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
627 /* apply the last info we have seen to this picture, when we get the
628 * picture back from ffmpeg we can use this to correctly timestamp the output
630 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
631 (gint32) picture->reordered_opaque);
634 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
635 picture->reordered_opaque);
636 if (G_UNLIKELY (frame == NULL))
639 /* now it has a buffer allocated, so it is real and will also
641 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
642 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
644 if (G_UNLIKELY (frame->output_buffer != NULL))
645 goto duplicate_frame;
647 /* GstFFMpegVidDecVideoFrame receives the frame ref */
648 picture->opaque = dframe =
649 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
651 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
653 /* If the picture format changed but we already negotiated before,
654 * we will have to do fallback allocation until output and input
655 * formats are in sync again. We will renegotiate on the output
657 if (ffmpegdec->pic_width != 0 && picture_changed (ffmpegdec, picture))
660 /* see if we need renegotiation */
661 if (G_UNLIKELY (!gst_ffmpegviddec_negotiate (ffmpegdec, context, picture,
663 goto negotiate_failed;
665 if (TRUE || !ffmpegdec->current_dr)
669 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
671 if (ret != GST_FLOW_OK)
674 /* piggy-backed alloc'ed on the frame,
675 * and there was much rejoicing and we are grateful.
676 * Now take away buffer from frame, we will give it back later when decoded.
677 * This allows multiple request for a buffer per frame; unusual but possible. */
678 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
679 gst_buffer_replace (&frame->output_buffer, NULL);
682 info = &ffmpegdec->output_state->info;
683 if (!gst_video_frame_map (&dframe->vframe, info, dframe->buffer,
686 dframe->mapped = TRUE;
688 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
689 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
690 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
691 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
693 /* libav does not allow stride changes currently, fall back to
694 * non-direct rendering here:
695 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
696 * https://bugzilla.libav.org/show_bug.cgi?id=556
698 if (ffmpegdec->stride[c] == -1) {
699 ffmpegdec->stride[c] = picture->linesize[c];
700 } else if (picture->linesize[c] != ffmpegdec->stride[c]) {
701 GST_LOG_OBJECT (ffmpegdec,
702 "No direct rendering, stride changed c=%d %d->%d", c,
703 ffmpegdec->stride[c], picture->linesize[c]);
705 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
706 picture->data[c] = NULL;
707 picture->linesize[c] = 0;
709 gst_video_frame_unmap (&dframe->vframe);
710 dframe->mapped = FALSE;
711 gst_buffer_replace (&dframe->buffer, NULL);
712 ffmpegdec->current_dr = FALSE;
717 picture->data[c] = NULL;
718 picture->linesize[c] = 0;
720 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
724 /* tell ffmpeg we own this buffer, tranfer the ref we have on the buffer to
725 * the opaque data. */
726 picture->type = FF_BUFFER_TYPE_USER;
728 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
735 GST_DEBUG_OBJECT (ffmpegdec, "negotiate failed");
740 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
745 /* alloc default buffer when we can't get one from downstream */
746 GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
751 /* alloc default buffer when we can't get one from downstream */
752 GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
753 gst_buffer_replace (&dframe->buffer, NULL);
759 int ret = avcodec_default_get_buffer (context, picture);
761 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
762 ffmpegdec->stride[c] = picture->linesize[c];
764 /* Wrap our buffer around the default one to be able to have a callback
765 * when our data can be freed. Just putting our data into the first free
766 * buffer might not work if there are too many allocated already
769 if (picture->buf[c]) {
770 dframe->avbuffer = picture->buf[c];
772 av_buffer_create (picture->buf[c]->data, picture->buf[c]->size,
773 dummy_free_buffer, dframe, 0);
776 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
785 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
786 gst_video_codec_frame_unref (frame);
791 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
796 /* this should havesame effect as _get_buffer wrt opaque metadata,
797 * but preserving current content, if any */
799 gst_ffmpegviddec_reget_buffer (AVCodecContext * context, AVFrame * picture)
801 GstVideoCodecFrame *frame;
802 GstFFMpegVidDecVideoFrame *dframe;
803 GstFFMpegVidDec *ffmpegdec;
805 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
807 GST_DEBUG_OBJECT (ffmpegdec, "regetting buffer picture %p", picture);
809 /* if there is no opaque, we didn't yet attach any frame to it. What usually
810 * happens is that avcodec_default_reget_buffer will call the getbuffer
812 dframe = picture->opaque;
817 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
818 picture->reordered_opaque);
819 if (G_UNLIKELY (frame == NULL))
822 if (G_UNLIKELY (frame->output_buffer != NULL))
823 goto duplicate_frame;
825 /* replace the frame, this one contains the pts/dts for the correspoding input
826 * buffer, which we need after decoding. */
827 gst_video_codec_frame_unref (dframe->frame);
828 dframe->frame = frame;
831 return avcodec_default_reget_buffer (context, picture);
836 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
841 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
846 /* called when ffmpeg is done with our buffer */
848 gst_ffmpegviddec_release_buffer (AVCodecContext * context, AVFrame * picture)
851 GstFFMpegVidDecVideoFrame *frame;
852 GstFFMpegVidDec *ffmpegdec;
854 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
855 frame = (GstFFMpegVidDecVideoFrame *) picture->opaque;
856 GST_DEBUG_OBJECT (ffmpegdec, "release frame SN %d",
857 frame->frame->system_frame_number);
859 /* check if it was our buffer */
860 if (picture->type != FF_BUFFER_TYPE_USER) {
861 GST_DEBUG_OBJECT (ffmpegdec, "default release buffer");
862 avcodec_default_release_buffer (context, picture);
865 /* we remove the opaque data now */
866 picture->opaque = NULL;
868 gst_ffmpegviddec_video_frame_free (ffmpegdec, frame);
870 /* zero out the reference in ffmpeg */
871 for (i = 0; i < 4; i++) {
872 picture->data[i] = NULL;
873 picture->linesize[i] = 0;
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,
898 AVFrame * picture, gboolean force)
900 if (!force && !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, gboolean force)
1007 GstVideoInfo *in_info, *out_info;
1008 GstVideoCodecState *output_state;
1011 if (!update_video_context (ffmpegdec, context, picture, force))
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;
1269 /* in case we skip frames */
1270 ffmpegdec->picture->pict_type = -1;
1272 /* run QoS code, we don't stop decoding the frame when we are late because
1273 * else we might skip a reference frame */
1274 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1276 if (ffmpegdec->is_realvideo && data != NULL) {
1280 /* setup the slice table for realvideo */
1281 if (ffmpegdec->context->slice_offset == NULL)
1282 ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1284 slice_count = (*data++) + 1;
1285 ffmpegdec->context->slice_count = slice_count;
1287 for (i = 0; i < slice_count; i++) {
1289 ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1295 /* save reference to the timing info */
1296 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1297 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1299 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1300 frame->system_frame_number);
1303 /* now decode the frame */
1304 gst_avpacket_init (&packet, data, size);
1306 if (ffmpegdec->palette) {
1309 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1311 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1312 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1315 /* This might call into get_buffer() from another thread,
1316 * which would cause a deadlock. Release the lock here
1317 * and taking it again later seems safe
1318 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1320 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1321 len = avcodec_decode_video2 (ffmpegdec->context,
1322 ffmpegdec->picture, have_data, &packet);
1323 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1325 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1328 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1329 * no data because we told it to skip stuff. */
1330 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1333 /* no data, we're done */
1334 if (len < 0 || *have_data == 0)
1337 /* get the output picture timing info again */
1338 out_dframe = ffmpegdec->picture->opaque;
1339 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1341 /* also give back a buffer allocated by the frame, if any */
1342 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1343 gst_buffer_replace (&out_dframe->buffer, NULL);
1345 GST_DEBUG_OBJECT (ffmpegdec,
1346 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1347 out_frame->pts, out_frame->duration);
1348 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1349 (guint64) ffmpegdec->picture->pts);
1350 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1351 ffmpegdec->picture->coded_picture_number);
1352 GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1353 ffmpegdec->picture->reference);
1354 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1355 ffmpegdec->picture->display_picture_number);
1356 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1357 ffmpegdec->picture->opaque);
1358 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1359 (guint64) ffmpegdec->picture->reordered_opaque);
1360 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1361 ffmpegdec->picture->repeat_pict);
1362 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1363 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1365 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1366 ffmpegdec->picture, FALSE))
1367 goto negotiation_error;
1369 if (G_UNLIKELY (out_frame->output_buffer == NULL))
1370 *ret = get_output_buffer (ffmpegdec, out_frame);
1372 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1375 /* Mark corrupted frames as corrupted */
1376 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1377 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1379 if (ffmpegdec->pic_interlaced) {
1380 /* set interlaced flags */
1381 if (ffmpegdec->picture->repeat_pict)
1382 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1383 if (ffmpegdec->picture->top_field_first)
1384 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1385 if (ffmpegdec->picture->interlaced_frame)
1386 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1387 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1391 /* so we decoded this frame, frames preceding it in decoding order
1392 * that still do not have a buffer allocated seem rather useless,
1393 * and can be discarded, due to e.g. misparsed bogus frame
1394 * or non-keyframe in skipped decoding, ...
1395 * In any case, not likely to be seen again, so discard those,
1396 * before they pile up and/or mess with timestamping */
1399 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1400 gboolean old = TRUE;
1402 ol = l = gst_video_decoder_get_frames (dec);
1404 GstVideoCodecFrame *tmp = l->data;
1409 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1410 GST_LOG_OBJECT (dec,
1411 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1412 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1413 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1414 /* drop extra ref and remove from frame list */
1415 gst_video_decoder_release_frame (dec, tmp);
1417 /* drop extra ref we got */
1418 gst_video_codec_frame_unref (tmp);
1426 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1429 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1430 gst_flow_get_name (*ret), len);
1436 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1437 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1444 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1445 *ret = GST_FLOW_NOT_NEGOTIATED;
1451 /* gst_ffmpegviddec_frame:
1453 * data: pointer to the data to decode
1454 * size: size of data in bytes
1455 * got_data: 0 if no data was decoded, != 0 otherwise.
1456 * in_time: timestamp of data
1457 * in_duration: duration of data
1458 * ret: GstFlowReturn to return in the chain function
1460 * Decode the given frame and pushes it downstream.
1462 * Returns: Number of bytes used in decoding, -1 on error/failure.
1466 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1467 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1468 GstFlowReturn * ret)
1470 GstFFMpegVidDecClass *oclass;
1473 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1476 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1479 ffmpegdec->context->frame_number++;
1481 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1484 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1488 GST_WARNING_OBJECT (ffmpegdec,
1489 "avdec_%s: decoding error (len: %d, have_data: %d)",
1490 oclass->in_plugin->name, len, *have_data);
1498 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1499 *ret = GST_FLOW_NOT_NEGOTIATED;
1505 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1507 GstFFMpegVidDecClass *oclass;
1509 if (!ffmpegdec->opened)
1512 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1514 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1515 gint have_data, len;
1518 GST_LOG_OBJECT (ffmpegdec,
1519 "codec has delay capabilities, calling until ffmpeg has drained everything");
1522 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1523 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1524 avcodec_flush_buffers (ffmpegdec->context);
1528 static GstFlowReturn
1529 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1530 GstVideoCodecFrame * frame)
1532 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1533 guint8 *data, *bdata;
1534 gint size, len, have_data, bsize;
1536 GstFlowReturn ret = GST_FLOW_OK;
1537 gboolean do_padding;
1539 GST_LOG_OBJECT (ffmpegdec,
1540 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1541 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1542 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1543 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1545 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1546 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1547 ("Failed to map buffer for reading"));
1548 return GST_FLOW_ERROR;
1551 /* treat frame as void until a buffer is requested for it */
1552 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1553 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1558 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1559 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1561 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1562 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1563 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1564 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1565 ffmpegdec->padded_size);
1567 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1568 "Copy input to add padding");
1569 memcpy (ffmpegdec->padded, bdata, bsize);
1570 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1572 bdata = ffmpegdec->padded;
1579 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1581 /* parse, if at all possible */
1586 /* add temporary padding */
1587 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1588 "Add temporary input padding");
1589 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1590 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1593 /* decode a frame of audio/video now */
1595 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1597 if (ret != GST_FLOW_OK) {
1598 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1599 gst_flow_get_name (ret));
1600 /* bad flow return, make sure we discard all data and exit */
1606 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1609 if (len == 0 && have_data == 0) {
1610 /* nothing was decoded, this could be because no data was available or
1611 * because we were skipping frames.
1612 * If we have no context we must exit and wait for more data, we keep the
1614 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1619 /* a decoding error happened, we must break and try again with next data. */
1620 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1625 /* prepare for the next round, for codecs with a context we did this
1626 * already when using the parser. */
1632 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1634 } while (bsize > 0);
1637 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1639 gst_buffer_unmap (frame->input_buffer, &minfo);
1640 gst_video_codec_frame_unref (frame);
1647 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1649 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1650 GstFFMpegVidDecClass *oclass;
1652 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1654 GST_OBJECT_LOCK (ffmpegdec);
1655 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1656 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1657 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1658 GST_OBJECT_UNLOCK (ffmpegdec);
1661 ffmpegdec->context->opaque = ffmpegdec;
1662 GST_OBJECT_UNLOCK (ffmpegdec);
1668 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1670 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1672 GST_OBJECT_LOCK (ffmpegdec);
1673 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1674 GST_OBJECT_UNLOCK (ffmpegdec);
1675 g_free (ffmpegdec->padded);
1676 ffmpegdec->padded = NULL;
1677 ffmpegdec->padded_size = 0;
1678 if (ffmpegdec->input_state)
1679 gst_video_codec_state_unref (ffmpegdec->input_state);
1680 ffmpegdec->input_state = NULL;
1681 if (ffmpegdec->output_state)
1682 gst_video_codec_state_unref (ffmpegdec->output_state);
1683 ffmpegdec->output_state = NULL;
1685 ffmpegdec->pic_pix_fmt = 0;
1686 ffmpegdec->pic_width = 0;
1687 ffmpegdec->pic_height = 0;
1688 ffmpegdec->pic_par_n = 0;
1689 ffmpegdec->pic_par_d = 0;
1690 ffmpegdec->ctx_ticks = 0;
1691 ffmpegdec->ctx_time_n = 0;
1692 ffmpegdec->ctx_time_d = 0;
1697 static GstFlowReturn
1698 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1700 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1702 gst_ffmpegviddec_drain (ffmpegdec);
1708 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1710 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1712 if (ffmpegdec->opened)
1713 avcodec_flush_buffers (ffmpegdec->context);
1719 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1721 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1722 GstVideoCodecState *state;
1723 GstBufferPool *pool;
1724 guint size, min, max;
1725 GstStructure *config;
1726 gboolean have_videometa, have_alignment, update_pool = FALSE;
1727 GstAllocator *allocator = NULL;
1728 GstAllocationParams params = { 0, 15, 0, 0, };
1730 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1734 state = gst_video_decoder_get_output_state (decoder);
1736 if (gst_query_get_n_allocation_params (query) > 0) {
1737 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1738 params.align = MAX (params.align, 15);
1740 gst_query_add_allocation_param (query, allocator, ¶ms);
1743 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1745 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1746 * need, otherwise we may stall */
1747 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1748 gst_object_unref (pool);
1749 pool = gst_video_buffer_pool_new ();
1753 /* if there is an allocator, also drop it, as it might be the reason we
1754 * have this limit. Default will be used */
1756 gst_object_unref (allocator);
1761 config = gst_buffer_pool_get_config (pool);
1762 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1763 /* we are happy with the default allocator but we would like to have 16 bytes
1764 * aligned and padded memory */
1765 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1768 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1770 gst_buffer_pool_config_add_option (config,
1771 GST_BUFFER_POOL_OPTION_VIDEO_META);
1774 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1776 /* we can only enable the alignment if downstream supports the
1778 if (have_alignment && have_videometa) {
1779 GstVideoAlignment align;
1781 gint linesize_align[4];
1785 width = GST_VIDEO_INFO_WIDTH (&state->info);
1786 height = GST_VIDEO_INFO_HEIGHT (&state->info);
1787 /* let ffmpeg find the alignment and padding */
1788 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
1792 context->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width ();
1793 /* increase the size for the padding */
1795 height += edge << 1;
1797 align.padding_top = edge;
1798 align.padding_left = edge;
1799 align.padding_right = width - GST_VIDEO_INFO_WIDTH (&state->info) - edge;
1800 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (&state->info) - edge;
1802 /* add extra padding to match libav buffer allocation sizes */
1803 align.padding_bottom++;
1805 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
1806 align.stride_align[i] =
1807 (linesize_align[i] > 0 ? linesize_align[i] - 1 : 0);
1809 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
1810 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
1811 GST_VIDEO_INFO_WIDTH (&state->info),
1812 GST_VIDEO_INFO_HEIGHT (&state->info), width, height, align.padding_top,
1813 align.padding_left, align.padding_right, align.padding_bottom,
1814 align.stride_align[0], align.stride_align[1], align.stride_align[2],
1815 align.stride_align[3]);
1817 gst_buffer_pool_config_add_option (config,
1818 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1819 gst_buffer_pool_config_set_video_alignment (config, &align);
1821 if (ffmpegdec->direct_rendering) {
1822 GstFFMpegVidDecClass *oclass;
1824 GST_DEBUG_OBJECT (ffmpegdec, "trying to enable direct rendering");
1826 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1828 if (oclass->in_plugin->capabilities & CODEC_CAP_DR1) {
1829 GST_DEBUG_OBJECT (ffmpegdec, "enabled direct rendering");
1830 ffmpegdec->current_dr = TRUE;
1832 GST_DEBUG_OBJECT (ffmpegdec, "direct rendering not supported");
1836 GST_DEBUG_OBJECT (ffmpegdec,
1837 "alignment or videometa not supported, disable direct rendering");
1839 /* disable direct rendering. This will make us use the fallback ffmpeg
1840 * picture allocation code with padding etc. We will then do the final
1841 * copy (with cropping) into a buffer from our pool */
1842 ffmpegdec->current_dr = FALSE;
1846 gst_buffer_pool_set_config (pool, config);
1849 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1851 gst_object_unref (pool);
1853 gst_object_unref (allocator);
1854 gst_video_codec_state_unref (state);
1860 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1863 GstAllocationParams params;
1865 gst_allocation_params_init (¶ms);
1866 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1868 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1869 /* we would like to have some padding so that we don't have to
1870 * memcpy. We don't suggest an allocator. */
1871 gst_query_add_allocation_param (query, NULL, ¶ms);
1873 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1878 gst_ffmpegviddec_set_property (GObject * object,
1879 guint prop_id, const GValue * value, GParamSpec * pspec)
1881 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1885 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1887 case PROP_SKIPFRAME:
1888 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1889 g_value_get_enum (value);
1891 case PROP_DIRECT_RENDERING:
1892 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1895 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1896 g_value_get_boolean (value);
1898 case PROP_MAX_THREADS:
1899 ffmpegdec->max_threads = g_value_get_int (value);
1901 case PROP_OUTPUT_CORRUPT:
1902 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1905 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1911 gst_ffmpegviddec_get_property (GObject * object,
1912 guint prop_id, GValue * value, GParamSpec * pspec)
1914 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1918 g_value_set_enum (value, ffmpegdec->context->lowres);
1920 case PROP_SKIPFRAME:
1921 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1923 case PROP_DIRECT_RENDERING:
1924 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1927 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1929 case PROP_MAX_THREADS:
1930 g_value_set_int (value, ffmpegdec->max_threads);
1932 case PROP_OUTPUT_CORRUPT:
1933 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1942 gst_ffmpegviddec_register (GstPlugin * plugin)
1944 GTypeInfo typeinfo = {
1945 sizeof (GstFFMpegVidDecClass),
1946 (GBaseInitFunc) gst_ffmpegviddec_base_init,
1948 (GClassInitFunc) gst_ffmpegviddec_class_init,
1951 sizeof (GstFFMpegVidDec),
1953 (GInstanceInitFunc) gst_ffmpegviddec_init,
1959 in_plugin = av_codec_next (NULL);
1961 GST_LOG ("Registering decoders");
1967 /* only video decoders */
1968 if (!av_codec_is_decoder (in_plugin)
1969 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1972 /* no quasi-codecs, please */
1973 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
1974 in_plugin->id == AV_CODEC_ID_V210 ||
1975 in_plugin->id == AV_CODEC_ID_V210X ||
1976 in_plugin->id == AV_CODEC_ID_R210 ||
1977 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
1978 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
1982 /* No decoders depending on external libraries (we don't build them, but
1983 * people who build against an external ffmpeg might have them.
1984 * We have native gstreamer plugins for all of those libraries anyway. */
1985 if (!strncmp (in_plugin->name, "lib", 3)) {
1987 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
1992 /* No vdpau plugins until we can figure out how to properly use them
1993 * outside of ffmpeg. */
1994 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
1996 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2001 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2003 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2008 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2010 /* no codecs for which we're GUARANTEED to have better alternatives */
2011 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2012 /* MP1 : Use MP3 for decoding */
2013 /* MP2 : Use MP3 for decoding */
2014 /* Theora: Use libtheora based theoradec */
2015 if (!strcmp (in_plugin->name, "gif") ||
2016 !strcmp (in_plugin->name, "theora") ||
2017 !strcmp (in_plugin->name, "mpeg1video") ||
2018 strstr (in_plugin->name, "crystalhd") != NULL ||
2019 !strcmp (in_plugin->name, "ass") ||
2020 !strcmp (in_plugin->name, "srt") ||
2021 !strcmp (in_plugin->name, "pgssub") ||
2022 !strcmp (in_plugin->name, "dvdsub") ||
2023 !strcmp (in_plugin->name, "dvbsub")) {
2024 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2028 /* construct the type */
2029 if (!strcmp (in_plugin->name, "hevc")) {
2030 plugin_name = g_strdup ("h265");
2032 plugin_name = g_strdup ((gchar *) in_plugin->name);
2034 g_strdelimit (plugin_name, NULL, '_');
2035 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2036 g_free (plugin_name);
2038 type = g_type_from_name (type_name);
2041 /* create the gtype now */
2043 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2045 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2048 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2049 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2050 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2051 * VC1/WMV3 are not working and thus unpreferred for now. */
2052 switch (in_plugin->id) {
2053 case AV_CODEC_ID_MPEG4:
2054 case AV_CODEC_ID_MSMPEG4V3:
2055 case AV_CODEC_ID_H264:
2056 case AV_CODEC_ID_HEVC:
2057 case AV_CODEC_ID_RV10:
2058 case AV_CODEC_ID_RV20:
2059 case AV_CODEC_ID_RV30:
2060 case AV_CODEC_ID_RV40:
2061 rank = GST_RANK_PRIMARY;
2063 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2064 * They say libdv's quality is better though. leave as secondary.
2065 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2067 case AV_CODEC_ID_DVVIDEO:
2068 rank = GST_RANK_SECONDARY;
2071 rank = GST_RANK_MARGINAL;
2074 if (!gst_element_register (plugin, type_name, rank, type)) {
2075 g_warning ("Failed to register %s", type_name);
2083 in_plugin = av_codec_next (in_plugin);
2086 GST_LOG ("Finished Registering decoders");