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 31
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 GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
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_drain;
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;
335 if (avcodec_get_context_defaults3 (ffmpegdec->context,
336 oclass->in_plugin) < 0) {
337 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
340 ffmpegdec->context->opaque = ffmpegdec;
347 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
349 GstFFMpegVidDecClass *oclass;
352 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
354 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
357 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
358 ffmpegdec->stride[i] = -1;
360 ffmpegdec->opened = TRUE;
362 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
363 oclass->in_plugin->name, oclass->in_plugin->id);
365 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
366 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
373 gst_ffmpegviddec_close (ffmpegdec, TRUE);
374 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
375 oclass->in_plugin->name);
381 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
382 GstVideoCodecState * state)
384 GstStructure *str = gst_caps_get_structure (state->caps, 0);
385 const GValue *palette_v;
388 /* do we have a palette? */
389 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
390 palette = gst_value_get_buffer (palette_v);
391 GST_DEBUG ("got palette data %p", palette);
392 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
393 gst_buffer_replace (&ffmpegdec->palette, palette);
400 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
401 GstVideoCodecState * state)
403 GstFFMpegVidDec *ffmpegdec;
404 GstFFMpegVidDecClass *oclass;
405 GstClockTime latency = GST_CLOCK_TIME_NONE;
406 gboolean ret = FALSE;
408 ffmpegdec = (GstFFMpegVidDec *) decoder;
409 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
411 if (ffmpegdec->last_caps != NULL &&
412 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
416 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
418 GST_OBJECT_LOCK (ffmpegdec);
419 /* stupid check for VC1 */
420 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
421 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
422 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
424 /* close old session */
425 if (ffmpegdec->opened) {
426 GST_OBJECT_UNLOCK (ffmpegdec);
427 gst_ffmpegviddec_finish (decoder);
428 GST_OBJECT_LOCK (ffmpegdec);
429 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
430 GST_OBJECT_UNLOCK (ffmpegdec);
433 ffmpegdec->pic_pix_fmt = 0;
434 ffmpegdec->pic_width = 0;
435 ffmpegdec->pic_height = 0;
436 ffmpegdec->pic_par_n = 0;
437 ffmpegdec->pic_par_d = 0;
438 ffmpegdec->ctx_ticks = 0;
439 ffmpegdec->ctx_time_n = 0;
440 ffmpegdec->ctx_time_d = 0;
443 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
445 /* set buffer functions */
446 ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
447 ffmpegdec->context->draw_horiz_band = NULL;
449 /* reset coded_width/_height to prevent it being reused from last time when
450 * the codec is opened again, causing a mismatch and possible
451 * segfault/corruption. (Common scenario when renegotiating caps) */
452 ffmpegdec->context->coded_width = 0;
453 ffmpegdec->context->coded_height = 0;
455 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
456 ffmpegdec->context->height);
458 /* FIXME : Create a method that takes GstVideoCodecState instead */
459 /* get size and so */
460 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
461 oclass->in_plugin->type, state->caps, ffmpegdec->context);
463 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
464 ffmpegdec->context->height);
466 gst_ffmpegviddec_get_palette (ffmpegdec, state);
468 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
469 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
470 ffmpegdec->context->time_base.num = 1;
471 ffmpegdec->context->time_base.den = 25;
474 /* workaround encoder bugs */
475 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
476 ffmpegdec->context->err_recognition = 1;
479 ffmpegdec->context->lowres = ffmpegdec->lowres;
480 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
482 /* ffmpeg can draw motion vectors on top of the image (not every decoder
484 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
490 if (ffmpegdec->max_threads == 0) {
491 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
492 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
494 ffmpegdec->context->thread_count = 0;
496 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
498 query = gst_query_new_latency ();
500 /* Check if upstream is live. If it isn't we can enable frame based
501 * threading, which is adding latency */
502 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
503 gst_query_parse_latency (query, &is_live, NULL, NULL);
505 gst_query_unref (query);
508 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
510 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
513 /* open codec - we don't select an output pix_fmt yet,
514 * simply because we don't know! We only get it
515 * during playback... */
516 if (!gst_ffmpegviddec_open (ffmpegdec))
519 if (ffmpegdec->input_state)
520 gst_video_codec_state_unref (ffmpegdec->input_state);
521 ffmpegdec->input_state = gst_video_codec_state_ref (state);
523 if (ffmpegdec->input_state->info.fps_n) {
524 GstVideoInfo *info = &ffmpegdec->input_state->info;
525 latency = gst_util_uint64_scale_ceil (
526 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
533 GST_OBJECT_UNLOCK (ffmpegdec);
535 if (GST_CLOCK_TIME_IS_VALID (latency))
536 gst_video_decoder_set_latency (decoder, latency, latency);
543 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
550 GstFFMpegVidDec *ffmpegdec;
551 GstVideoCodecFrame *frame;
553 GstVideoFrame vframe;
555 AVBufferRef *avbuffer;
556 } GstFFMpegVidDecVideoFrame;
558 static GstFFMpegVidDecVideoFrame *
559 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
560 GstVideoCodecFrame * frame)
562 GstFFMpegVidDecVideoFrame *dframe;
564 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
565 dframe->ffmpegdec = ffmpegdec;
566 dframe->frame = frame;
568 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
574 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
575 GstFFMpegVidDecVideoFrame * frame)
577 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
580 gst_video_frame_unmap (&frame->vframe);
581 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
582 gst_buffer_replace (&frame->buffer, NULL);
583 if (frame->avbuffer) {
584 av_buffer_unref (&frame->avbuffer);
586 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
590 dummy_free_buffer (void *opaque, uint8_t * data)
592 GstFFMpegVidDecVideoFrame *frame = opaque;
594 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
597 /* This function prepares the pool configuration for direct rendering. To use
598 * this method, the codec should support direct rendering and the pool should
599 * support video meta and video alignment */
601 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
602 GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
604 GstAllocationParams params;
605 GstVideoAlignment align;
606 GstAllocator *allocator = NULL;
608 gint linesize_align[4];
613 width = GST_VIDEO_INFO_WIDTH (info);
614 height = GST_VIDEO_INFO_HEIGHT (info);
616 /* let ffmpeg find the alignment and padding */
617 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
620 if (ffmpegdec->context->flags & CODEC_FLAG_EMU_EDGE)
623 edge = avcodec_get_edge_width ();
625 /* increase the size for the padding */
629 align.padding_top = edge;
630 align.padding_left = edge;
631 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info) - edge;
632 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info) - edge;
634 /* add extra padding to match libav buffer allocation sizes */
635 align.padding_bottom++;
637 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
639 max_align = DEFAULT_STRIDE_ALIGN;
640 max_align |= params.align;
642 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
643 if (linesize_align[i] > 0)
644 max_align |= linesize_align[i] - 1;
647 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
648 align.stride_align[i] = max_align;
650 params.align = max_align;
652 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
654 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
655 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
656 GST_VIDEO_INFO_WIDTH (info),
657 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
658 align.padding_left, align.padding_right, align.padding_bottom,
659 align.stride_align[0], align.stride_align[1], align.stride_align[2],
660 align.stride_align[3]);
662 gst_buffer_pool_config_add_option (config,
663 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
664 gst_buffer_pool_config_set_video_alignment (config, &align);
668 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
671 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
673 GstVideoFormat format;
675 GstStructure *config;
678 if (ffmpegdec->internal_pool != NULL &&
679 ffmpegdec->pool_width == picture->width &&
680 ffmpegdec->pool_height == picture->height &&
681 ffmpegdec->pool_format == picture->format)
684 GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
685 picture->width, picture->height);
687 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
688 gst_video_info_set_format (&info, format, picture->width, picture->height);
690 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
691 ffmpegdec->stride[i] = -1;
693 if (ffmpegdec->internal_pool)
694 gst_object_unref (ffmpegdec->internal_pool);
696 ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
697 config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
699 caps = gst_video_info_to_caps (&info);
700 gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
701 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
702 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
704 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
705 ffmpegdec->internal_pool, &info, config);
706 /* generic video pool never fails */
707 gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
708 gst_caps_unref (caps);
710 gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
712 /* Remember pool size so we can detect changes */
713 ffmpegdec->pool_width = picture->width;
714 ffmpegdec->pool_height = picture->height;
715 ffmpegdec->pool_format = picture->format;
716 ffmpegdec->pool_info = info;
720 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
722 GstFFMpegVidDecClass *oclass;
724 if (!ffmpegdec->direct_rendering)
727 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
728 return ((oclass->in_plugin->capabilities & CODEC_CAP_DR1) == CODEC_CAP_DR1);
731 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
732 * into. We try to give it memory from our pool */
734 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
737 GstVideoCodecFrame *frame;
738 GstFFMpegVidDecVideoFrame *dframe;
739 GstFFMpegVidDec *ffmpegdec;
743 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
745 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
747 /* apply the last info we have seen to this picture, when we get the
748 * picture back from ffmpeg we can use this to correctly timestamp the output
750 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
751 (gint32) picture->reordered_opaque);
754 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
755 picture->reordered_opaque);
756 if (G_UNLIKELY (frame == NULL))
759 /* now it has a buffer allocated, so it is real and will also
761 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
762 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
764 if (G_UNLIKELY (frame->output_buffer != NULL))
765 goto duplicate_frame;
767 /* GstFFMpegVidDecVideoFrame receives the frame ref */
768 if (picture->opaque) {
769 dframe = picture->opaque;
770 dframe->frame = frame;
772 picture->opaque = dframe =
773 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
776 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
778 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
781 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
783 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
784 &frame->output_buffer, NULL);
785 if (ret != GST_FLOW_OK)
788 /* piggy-backed alloc'ed on the frame,
789 * and there was much rejoicing and we are grateful.
790 * Now take away buffer from frame, we will give it back later when decoded.
791 * This allows multiple request for a buffer per frame; unusual but possible. */
792 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
793 gst_buffer_replace (&frame->output_buffer, NULL);
796 if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
797 dframe->buffer, GST_MAP_READWRITE))
799 dframe->mapped = TRUE;
801 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
802 if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
803 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
804 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
806 if (ffmpegdec->stride[c] == -1)
807 ffmpegdec->stride[c] = picture->linesize[c];
809 /* libav does not allow stride changes, decide allocation should check
810 * before replacing the internal pool with a downstream pool.
811 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
812 * https://bugzilla.libav.org/show_bug.cgi?id=556
814 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
816 picture->data[c] = NULL;
817 picture->linesize[c] = 0;
819 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
823 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
825 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
832 int ret = avcodec_default_get_buffer2 (context, picture, flags);
834 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
836 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
837 ffmpegdec->stride[c] = picture->linesize[c];
839 /* Wrap our buffer around the default one to be able to have a callback
840 * when our data can be freed. Just putting our data into the first free
841 * buffer might not work if there are too many allocated already
843 if (picture->buf[0]) {
844 dframe->avbuffer = picture->buf[0];
846 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
847 dummy_free_buffer, dframe, 0);
850 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
857 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
858 ("Unable to allocate memory"),
859 ("The downstream pool failed to allocated buffer."));
864 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
865 ("Cannot access memory for read and write operation."),
866 ("The video memory allocated from downstream pool could not mapped for"
872 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
873 gst_video_codec_frame_unref (frame);
878 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
884 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
886 return !(ffmpegdec->pic_width == picture->width
887 && ffmpegdec->pic_height == picture->height
888 && ffmpegdec->pic_pix_fmt == picture->format
889 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
890 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
891 && ffmpegdec->pic_interlaced == picture->interlaced_frame);
895 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
897 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
898 && ffmpegdec->ctx_time_n == context->time_base.num
899 && ffmpegdec->ctx_time_d == context->time_base.den);
903 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
906 if (!picture_changed (ffmpegdec, picture)
907 && !context_changed (ffmpegdec, context))
910 GST_DEBUG_OBJECT (ffmpegdec,
911 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
912 ffmpegdec->pic_width, ffmpegdec->pic_height,
913 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
914 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
915 ffmpegdec->pic_pix_fmt,
916 picture->width, picture->height,
917 picture->sample_aspect_ratio.num,
918 picture->sample_aspect_ratio.den,
919 context->time_base.num, context->time_base.den, picture->format);
921 ffmpegdec->pic_pix_fmt = picture->format;
922 ffmpegdec->pic_width = picture->width;
923 ffmpegdec->pic_height = picture->height;
924 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
925 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
926 ffmpegdec->pic_interlaced = picture->interlaced_frame;
927 ffmpegdec->ctx_ticks = context->ticks_per_frame;
928 ffmpegdec->ctx_time_n = context->time_base.num;
929 ffmpegdec->ctx_time_d = context->time_base.den;
935 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
936 GstVideoInfo * in_info, GstVideoInfo * out_info)
938 gboolean demuxer_par_set = FALSE;
939 gboolean decoder_par_set = FALSE;
940 gint demuxer_num = 1, demuxer_denom = 1;
941 gint decoder_num = 1, decoder_denom = 1;
943 if (in_info->par_n && in_info->par_d) {
944 demuxer_num = in_info->par_n;
945 demuxer_denom = in_info->par_d;
946 demuxer_par_set = TRUE;
947 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
951 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
952 decoder_num = ffmpegdec->pic_par_n;
953 decoder_denom = ffmpegdec->pic_par_d;
954 decoder_par_set = TRUE;
955 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
959 if (!demuxer_par_set && !decoder_par_set)
962 if (demuxer_par_set && !decoder_par_set)
963 goto use_demuxer_par;
965 if (decoder_par_set && !demuxer_par_set)
966 goto use_decoder_par;
968 /* Both the demuxer and the decoder provide a PAR. If one of
969 * the two PARs is 1:1 and the other one is not, use the one
970 * that is not 1:1. */
971 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
972 goto use_decoder_par;
974 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
975 goto use_demuxer_par;
977 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
978 goto use_demuxer_par;
982 GST_DEBUG_OBJECT (ffmpegdec,
983 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
985 out_info->par_n = decoder_num;
986 out_info->par_d = decoder_denom;
991 GST_DEBUG_OBJECT (ffmpegdec,
992 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
994 out_info->par_n = demuxer_num;
995 out_info->par_d = demuxer_denom;
1000 GST_DEBUG_OBJECT (ffmpegdec,
1001 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1002 out_info->par_n = 1;
1003 out_info->par_d = 1;
1009 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1010 AVCodecContext * context, AVFrame * picture)
1013 GstVideoInfo *in_info, *out_info;
1014 GstVideoCodecState *output_state;
1017 if (!update_video_context (ffmpegdec, context, picture))
1020 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1021 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1022 goto unknown_format;
1025 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1026 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1027 if (ffmpegdec->output_state)
1028 gst_video_codec_state_unref (ffmpegdec->output_state);
1029 ffmpegdec->output_state = output_state;
1031 in_info = &ffmpegdec->input_state->info;
1032 out_info = &ffmpegdec->output_state->info;
1034 /* set the interlaced flag */
1035 if (ffmpegdec->pic_interlaced)
1036 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1038 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1040 switch (context->chroma_sample_location) {
1042 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1045 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1048 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1051 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1057 /* try to find a good framerate */
1058 if ((in_info->fps_d && in_info->fps_n) ||
1059 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1060 /* take framerate from input when it was specified (#313970) */
1061 fps_n = in_info->fps_n;
1062 fps_d = in_info->fps_d;
1064 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1065 fps_d = ffmpegdec->ctx_time_n;
1068 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1072 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1073 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1080 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1081 out_info->fps_n = fps_n;
1082 out_info->fps_d = fps_d;
1084 /* calculate and update par now */
1085 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1087 /* Copy stereo/multiview info from upstream if set */
1088 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
1089 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) =
1090 GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1091 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) =
1092 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1095 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1096 goto negotiate_failed;
1103 GST_ERROR_OBJECT (ffmpegdec,
1104 "decoder requires a video format unsupported by GStreamer");
1109 /* Reset so we try again next time even if force==FALSE */
1110 ffmpegdec->pic_pix_fmt = 0;
1111 ffmpegdec->pic_width = 0;
1112 ffmpegdec->pic_height = 0;
1113 ffmpegdec->pic_par_n = 0;
1114 ffmpegdec->pic_par_d = 0;
1115 ffmpegdec->ctx_ticks = 0;
1116 ffmpegdec->ctx_time_n = 0;
1117 ffmpegdec->ctx_time_d = 0;
1119 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1124 /* perform qos calculations before decoding the next frame.
1126 * Sets the skip_frame flag and if things are really bad, skips to the next
1131 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1132 GstVideoCodecFrame * frame, gboolean * mode_switch)
1134 GstClockTimeDiff diff;
1135 GstSegmentFlags skip_flags =
1136 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1138 *mode_switch = FALSE;
1143 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1144 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1145 *mode_switch = TRUE;
1146 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1147 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1148 *mode_switch = TRUE;
1152 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1155 /* if we don't have timing info, then we don't do QoS */
1156 if (G_UNLIKELY (diff == G_MAXINT64))
1159 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1161 if (*mode_switch == FALSE) {
1162 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1163 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1164 *mode_switch = TRUE;
1165 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1168 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1169 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1170 *mode_switch = TRUE;
1171 GST_DEBUG_OBJECT (ffmpegdec,
1172 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1177 /* get an outbuf buffer with the current picture */
1178 static GstFlowReturn
1179 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1181 GstFlowReturn ret = GST_FLOW_OK;
1182 AVFrame pic, *outpic;
1183 GstVideoFrame vframe;
1187 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1189 if (!ffmpegdec->output_state)
1190 goto not_negotiated;
1193 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1195 if (G_UNLIKELY (ret != GST_FLOW_OK))
1198 /* original ffmpeg code does not handle odd sizes correctly.
1199 * This patched up version does */
1200 /* Fill avpicture */
1201 info = &ffmpegdec->output_state->info;
1202 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1203 GST_MAP_READ | GST_MAP_WRITE))
1206 memset (&pic, 0, sizeof (pic));
1207 pic.format = ffmpegdec->pic_pix_fmt;
1208 pic.width = GST_VIDEO_FRAME_WIDTH (&vframe);
1209 pic.height = GST_VIDEO_FRAME_HEIGHT (&vframe);
1210 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1211 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1212 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1213 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1214 GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1215 pic.linesize[c], pic.data[c]);
1218 pic.linesize[c] = 0;
1222 outpic = ffmpegdec->picture;
1224 if (av_frame_copy (&pic, outpic) != 0) {
1225 GST_ERROR_OBJECT (ffmpegdec, "Failed to copy output frame");
1226 ret = GST_FLOW_ERROR;
1229 gst_video_frame_unmap (&vframe);
1231 ffmpegdec->picture->reordered_opaque = -1;
1238 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
1239 ("Unable to allocate memory"),
1240 ("The downstream pool failed to allocated buffer."));
1245 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
1246 ("Cannot access memory for read and write operation."),
1247 ("The video memory allocated from downstream pool could not mapped for"
1248 "read and write."));
1253 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1254 return GST_FLOW_NOT_NEGOTIATED;
1259 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1261 memset (packet, 0, sizeof (AVPacket));
1262 packet->data = data;
1263 packet->size = size;
1266 /* gst_ffmpegviddec_[video|audio]_frame:
1268 * data: pointer to the data to decode
1269 * size: size of data in bytes
1270 * in_timestamp: incoming timestamp.
1271 * in_duration: incoming duration.
1272 * in_offset: incoming offset (frame number).
1275 * Returns: number of bytes used in decoding. The check for successful decode is
1276 * outbuf being non-NULL.
1279 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1280 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1281 GstFlowReturn * ret)
1284 gboolean mode_switch;
1285 GstVideoCodecFrame *out_frame;
1286 GstFFMpegVidDecVideoFrame *out_dframe;
1288 GstBufferPool *pool;
1292 /* in case we skip frames */
1293 ffmpegdec->picture->pict_type = -1;
1295 /* run QoS code, we don't stop decoding the frame when we are late because
1296 * else we might skip a reference frame */
1297 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1300 /* save reference to the timing info */
1301 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1302 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1304 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1305 frame->system_frame_number);
1308 /* now decode the frame */
1309 gst_avpacket_init (&packet, data, size);
1311 if (ffmpegdec->palette) {
1314 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1316 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1317 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1320 /* This might call into get_buffer() from another thread,
1321 * which would cause a deadlock. Release the lock here
1322 * and taking it again later seems safe
1323 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1325 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1326 len = avcodec_decode_video2 (ffmpegdec->context,
1327 ffmpegdec->picture, have_data, &packet);
1328 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1330 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1333 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1334 * no data because we told it to skip stuff. */
1335 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1338 /* no data, we're done */
1339 if (len < 0 || *have_data == 0)
1342 /* get the output picture timing info again */
1343 out_dframe = ffmpegdec->picture->opaque;
1344 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1346 /* also give back a buffer allocated by the frame, if any */
1347 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1348 gst_buffer_replace (&out_dframe->buffer, NULL);
1350 GST_DEBUG_OBJECT (ffmpegdec,
1351 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1352 out_frame->pts, out_frame->duration);
1353 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1354 (guint64) ffmpegdec->picture->pts);
1355 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1356 ffmpegdec->picture->coded_picture_number);
1357 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1358 ffmpegdec->picture->display_picture_number);
1359 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1360 ffmpegdec->picture->opaque);
1361 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1362 (guint64) ffmpegdec->picture->reordered_opaque);
1363 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1364 ffmpegdec->picture->repeat_pict);
1365 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1366 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1368 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1369 ffmpegdec->picture))
1370 goto negotiation_error;
1372 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1373 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1374 *ret = get_output_buffer (ffmpegdec, out_frame);
1375 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1376 GstBuffer *tmp = out_frame->output_buffer;
1377 out_frame->output_buffer = NULL;
1378 *ret = get_output_buffer (ffmpegdec, out_frame);
1379 gst_buffer_unref (tmp);
1381 #ifndef G_DISABLE_ASSERT
1383 GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1385 GstVideoInfo *info = &ffmpegdec->output_state->info;
1386 g_assert (vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1387 g_assert (vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1391 gst_object_unref (pool);
1393 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1396 /* Mark corrupted frames as corrupted */
1397 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1398 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1400 if (ffmpegdec->pic_interlaced) {
1401 /* set interlaced flags */
1402 if (ffmpegdec->picture->repeat_pict)
1403 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1404 if (ffmpegdec->picture->top_field_first)
1405 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1406 if (ffmpegdec->picture->interlaced_frame)
1407 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1408 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1412 /* so we decoded this frame, frames preceding it in decoding order
1413 * that still do not have a buffer allocated seem rather useless,
1414 * and can be discarded, due to e.g. misparsed bogus frame
1415 * or non-keyframe in skipped decoding, ...
1416 * In any case, not likely to be seen again, so discard those,
1417 * before they pile up and/or mess with timestamping */
1420 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1421 gboolean old = TRUE;
1423 ol = l = gst_video_decoder_get_frames (dec);
1425 GstVideoCodecFrame *tmp = l->data;
1430 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1431 GST_LOG_OBJECT (dec,
1432 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1433 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1434 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1435 /* drop extra ref and remove from frame list */
1436 gst_video_decoder_release_frame (dec, tmp);
1438 /* drop extra ref we got */
1439 gst_video_codec_frame_unref (tmp);
1446 av_frame_unref (ffmpegdec->picture);
1448 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1449 * libav might still have a reference to it!
1452 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1455 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1456 gst_flow_get_name (*ret), len);
1462 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1463 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1470 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1471 *ret = GST_FLOW_FLUSHING;
1474 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1475 *ret = GST_FLOW_NOT_NEGOTIATED;
1481 /* gst_ffmpegviddec_frame:
1483 * data: pointer to the data to decode
1484 * size: size of data in bytes
1485 * got_data: 0 if no data was decoded, != 0 otherwise.
1486 * in_time: timestamp of data
1487 * in_duration: duration of data
1488 * ret: GstFlowReturn to return in the chain function
1490 * Decode the given frame and pushes it downstream.
1492 * Returns: Number of bytes used in decoding, -1 on error/failure.
1496 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1497 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1498 GstFlowReturn * ret)
1500 GstFFMpegVidDecClass *oclass;
1503 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1506 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1509 ffmpegdec->context->frame_number++;
1511 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1514 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1518 GST_WARNING_OBJECT (ffmpegdec,
1519 "avdec_%s: decoding error (len: %d, have_data: %d)",
1520 oclass->in_plugin->name, len, *have_data);
1528 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1529 *ret = GST_FLOW_NOT_NEGOTIATED;
1534 static GstFlowReturn
1535 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1537 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1538 GstFFMpegVidDecClass *oclass;
1540 if (!ffmpegdec->opened)
1543 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1545 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1546 gint have_data, len;
1549 GST_LOG_OBJECT (ffmpegdec,
1550 "codec has delay capabilities, calling until ffmpeg has drained everything");
1553 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1554 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1560 static GstFlowReturn
1561 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1562 GstVideoCodecFrame * frame)
1564 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1565 guint8 *data, *bdata;
1566 gint size, len, have_data, bsize;
1568 GstFlowReturn ret = GST_FLOW_OK;
1569 gboolean do_padding;
1571 GST_LOG_OBJECT (ffmpegdec,
1572 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1573 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1574 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1575 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1577 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1578 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1579 ("Failed to map buffer for reading"));
1580 return GST_FLOW_ERROR;
1583 /* treat frame as void until a buffer is requested for it */
1584 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1585 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1590 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1591 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1593 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1594 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1595 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1596 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1597 ffmpegdec->padded_size);
1599 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1600 "Copy input to add padding");
1601 memcpy (ffmpegdec->padded, bdata, bsize);
1602 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1604 bdata = ffmpegdec->padded;
1611 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1613 /* parse, if at all possible */
1618 /* add temporary padding */
1619 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1620 "Add temporary input padding");
1621 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1622 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1625 /* decode a frame of audio/video now */
1627 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1629 if (ret != GST_FLOW_OK) {
1630 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1631 gst_flow_get_name (ret));
1632 /* bad flow return, make sure we discard all data and exit */
1638 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1641 if (len == 0 && have_data == 0) {
1642 /* nothing was decoded, this could be because no data was available or
1643 * because we were skipping frames.
1644 * If we have no context we must exit and wait for more data, we keep the
1646 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1651 /* a decoding error happened, we must break and try again with next data. */
1652 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1657 /* prepare for the next round, for codecs with a context we did this
1658 * already when using the parser. */
1664 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1666 } while (bsize > 0);
1669 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1671 gst_buffer_unmap (frame->input_buffer, &minfo);
1672 gst_video_codec_frame_unref (frame);
1678 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1680 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1681 GstFFMpegVidDecClass *oclass;
1683 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1685 GST_OBJECT_LOCK (ffmpegdec);
1686 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1687 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1688 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1689 GST_OBJECT_UNLOCK (ffmpegdec);
1692 ffmpegdec->context->opaque = ffmpegdec;
1693 GST_OBJECT_UNLOCK (ffmpegdec);
1699 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1701 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1703 GST_OBJECT_LOCK (ffmpegdec);
1704 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1705 GST_OBJECT_UNLOCK (ffmpegdec);
1706 g_free (ffmpegdec->padded);
1707 ffmpegdec->padded = NULL;
1708 ffmpegdec->padded_size = 0;
1709 if (ffmpegdec->input_state)
1710 gst_video_codec_state_unref (ffmpegdec->input_state);
1711 ffmpegdec->input_state = NULL;
1712 if (ffmpegdec->output_state)
1713 gst_video_codec_state_unref (ffmpegdec->output_state);
1714 ffmpegdec->output_state = NULL;
1716 if (ffmpegdec->internal_pool)
1717 gst_object_unref (ffmpegdec->internal_pool);
1718 ffmpegdec->internal_pool = NULL;
1720 ffmpegdec->pic_pix_fmt = 0;
1721 ffmpegdec->pic_width = 0;
1722 ffmpegdec->pic_height = 0;
1723 ffmpegdec->pic_par_n = 0;
1724 ffmpegdec->pic_par_d = 0;
1725 ffmpegdec->ctx_ticks = 0;
1726 ffmpegdec->ctx_time_n = 0;
1727 ffmpegdec->ctx_time_d = 0;
1729 ffmpegdec->pool_width = 0;
1730 ffmpegdec->pool_height = 0;
1731 ffmpegdec->pool_format = 0;
1736 static GstFlowReturn
1737 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1739 gst_ffmpegviddec_drain (decoder);
1740 /* note that finish can and should clean up more drastically,
1741 * but drain is also invoked on e.g. packet loss in GAP handling */
1742 gst_ffmpegviddec_flush (decoder);
1748 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1750 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1752 if (ffmpegdec->opened) {
1753 GST_LOG_OBJECT (decoder, "flushing buffers");
1754 avcodec_flush_buffers (ffmpegdec->context);
1761 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1763 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1764 GstVideoCodecState *state;
1765 GstBufferPool *pool;
1766 guint size, min, max;
1767 GstStructure *config;
1768 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1769 GstAllocator *allocator = NULL;
1770 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1772 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1774 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1778 state = gst_video_decoder_get_output_state (decoder);
1780 if (gst_query_get_n_allocation_params (query) > 0) {
1781 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1782 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1784 gst_query_add_allocation_param (query, allocator, ¶ms);
1787 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1789 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1790 * need, otherwise we may stall */
1791 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1792 gst_object_unref (pool);
1793 pool = gst_video_buffer_pool_new ();
1798 /* if there is an allocator, also drop it, as it might be the reason we
1799 * have this limit. Default will be used */
1801 gst_object_unref (allocator);
1806 config = gst_buffer_pool_get_config (pool);
1807 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1808 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1811 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1814 gst_buffer_pool_config_add_option (config,
1815 GST_BUFFER_POOL_OPTION_VIDEO_META);
1818 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1820 /* If we have videometa, we never have to copy */
1821 if (have_videometa && have_pool && have_alignment &&
1822 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
1823 GstStructure *config_copy = gst_structure_copy (config);
1825 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
1828 /* FIXME validate and retry */
1829 if (gst_buffer_pool_set_config (pool, config_copy)) {
1833 gst_buffer_pool_set_active (pool, TRUE);
1834 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
1835 if (ret == GST_FLOW_OK) {
1836 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
1837 gboolean same_stride = TRUE;
1840 for (i = 0; i < vmeta->n_planes; i++) {
1841 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
1842 same_stride = FALSE;
1847 gst_buffer_unref (tmp);
1850 if (ffmpegdec->internal_pool)
1851 gst_object_unref (ffmpegdec->internal_pool);
1852 ffmpegdec->internal_pool = gst_object_ref (pool);
1853 ffmpegdec->pool_info = state->info;
1854 gst_structure_free (config);
1861 if (have_videometa && ffmpegdec->internal_pool
1862 && ffmpegdec->pool_width == state->info.width
1863 && ffmpegdec->pool_height == state->info.height) {
1865 gst_object_unref (pool);
1866 pool = gst_object_ref (ffmpegdec->internal_pool);
1867 gst_structure_free (config);
1872 if (!gst_buffer_pool_set_config (pool, config)) {
1873 gboolean working_pool = FALSE;
1874 config = gst_buffer_pool_get_config (pool);
1876 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
1878 working_pool = gst_buffer_pool_set_config (pool, config);
1880 gst_structure_free (config);
1883 if (!working_pool) {
1884 gst_object_unref (pool);
1885 pool = gst_video_buffer_pool_new ();
1886 config = gst_buffer_pool_get_config (pool);
1887 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1888 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
1889 gst_buffer_pool_set_config (pool, config);
1897 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1899 gst_object_unref (pool);
1901 gst_object_unref (allocator);
1902 gst_video_codec_state_unref (state);
1908 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1911 GstAllocationParams params;
1913 gst_allocation_params_init (¶ms);
1914 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1915 params.align = DEFAULT_STRIDE_ALIGN;
1916 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1917 /* we would like to have some padding so that we don't have to
1918 * memcpy. We don't suggest an allocator. */
1919 gst_query_add_allocation_param (query, NULL, ¶ms);
1921 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1926 gst_ffmpegviddec_set_property (GObject * object,
1927 guint prop_id, const GValue * value, GParamSpec * pspec)
1929 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1933 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1935 case PROP_SKIPFRAME:
1936 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1937 g_value_get_enum (value);
1939 case PROP_DIRECT_RENDERING:
1940 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1943 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1944 g_value_get_boolean (value);
1946 case PROP_MAX_THREADS:
1947 ffmpegdec->max_threads = g_value_get_int (value);
1949 case PROP_OUTPUT_CORRUPT:
1950 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1959 gst_ffmpegviddec_get_property (GObject * object,
1960 guint prop_id, GValue * value, GParamSpec * pspec)
1962 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1966 g_value_set_enum (value, ffmpegdec->context->lowres);
1968 case PROP_SKIPFRAME:
1969 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1971 case PROP_DIRECT_RENDERING:
1972 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1975 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1977 case PROP_MAX_THREADS:
1978 g_value_set_int (value, ffmpegdec->max_threads);
1980 case PROP_OUTPUT_CORRUPT:
1981 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1984 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1990 gst_ffmpegviddec_register (GstPlugin * plugin)
1992 GTypeInfo typeinfo = {
1993 sizeof (GstFFMpegVidDecClass),
1994 (GBaseInitFunc) gst_ffmpegviddec_base_init,
1996 (GClassInitFunc) gst_ffmpegviddec_class_init,
1999 sizeof (GstFFMpegVidDec),
2001 (GInstanceInitFunc) gst_ffmpegviddec_init,
2007 in_plugin = av_codec_next (NULL);
2009 GST_LOG ("Registering decoders");
2015 /* only video decoders */
2016 if (!av_codec_is_decoder (in_plugin)
2017 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2020 /* no quasi-codecs, please */
2021 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2022 in_plugin->id == AV_CODEC_ID_V210 ||
2023 in_plugin->id == AV_CODEC_ID_V210X ||
2024 in_plugin->id == AV_CODEC_ID_R210 ||
2025 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
2026 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
2030 /* No decoders depending on external libraries (we don't build them, but
2031 * people who build against an external ffmpeg might have them.
2032 * We have native gstreamer plugins for all of those libraries anyway. */
2033 if (!strncmp (in_plugin->name, "lib", 3)) {
2035 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2040 /* No vdpau plugins until we can figure out how to properly use them
2041 * outside of ffmpeg. */
2042 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2044 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2049 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2051 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2056 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2058 /* no codecs for which we're GUARANTEED to have better alternatives */
2059 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2060 /* MP1 : Use MP3 for decoding */
2061 /* MP2 : Use MP3 for decoding */
2062 /* Theora: Use libtheora based theoradec */
2063 if (!strcmp (in_plugin->name, "gif") ||
2064 !strcmp (in_plugin->name, "theora") ||
2065 !strcmp (in_plugin->name, "mpeg1video") ||
2066 strstr (in_plugin->name, "crystalhd") != NULL ||
2067 !strcmp (in_plugin->name, "ass") ||
2068 !strcmp (in_plugin->name, "srt") ||
2069 !strcmp (in_plugin->name, "pgssub") ||
2070 !strcmp (in_plugin->name, "dvdsub") ||
2071 !strcmp (in_plugin->name, "dvbsub")) {
2072 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2076 /* construct the type */
2077 if (!strcmp (in_plugin->name, "hevc")) {
2078 plugin_name = g_strdup ("h265");
2080 plugin_name = g_strdup ((gchar *) in_plugin->name);
2082 g_strdelimit (plugin_name, NULL, '_');
2083 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2084 g_free (plugin_name);
2086 type = g_type_from_name (type_name);
2089 /* create the gtype now */
2091 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2093 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2096 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2097 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2098 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2099 * VC1/WMV3 are not working and thus unpreferred for now. */
2100 switch (in_plugin->id) {
2101 case AV_CODEC_ID_MPEG1VIDEO:
2102 case AV_CODEC_ID_MPEG2VIDEO:
2103 case AV_CODEC_ID_MPEG4:
2104 case AV_CODEC_ID_MSMPEG4V3:
2105 case AV_CODEC_ID_H264:
2106 case AV_CODEC_ID_HEVC:
2107 case AV_CODEC_ID_RV10:
2108 case AV_CODEC_ID_RV20:
2109 case AV_CODEC_ID_RV30:
2110 case AV_CODEC_ID_RV40:
2111 rank = GST_RANK_PRIMARY;
2113 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2114 * They say libdv's quality is better though. leave as secondary.
2115 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2117 case AV_CODEC_ID_DVVIDEO:
2118 rank = GST_RANK_SECONDARY;
2121 rank = GST_RANK_MARGINAL;
2124 if (!gst_element_register (plugin, type_name, rank, type)) {
2125 g_warning ("Failed to register %s", type_name);
2133 in_plugin = av_codec_next (in_plugin);
2136 GST_LOG ("Finished Registering decoders");