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>
28 #include <libavutil/stereo3d.h>
31 #include "gstavcodecmap.h"
32 #include "gstavutils.h"
33 #include "gstavviddec.h"
35 #define MAX_TS_MASK 0xff
37 #define DEFAULT_LOWRES 0
38 #define DEFAULT_SKIPFRAME 0
39 #define DEFAULT_DIRECT_RENDERING TRUE
40 #define DEFAULT_DEBUG_MV FALSE
41 #define DEFAULT_MAX_THREADS 0
42 #define DEFAULT_OUTPUT_CORRUPT TRUE
43 #define REQUIRED_POOL_MAX_BUFFERS 32
44 #define DEFAULT_STRIDE_ALIGN 31
45 #define DEFAULT_ALLOC_PARAM { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
52 PROP_DIRECT_RENDERING,
59 /* A number of function prototypes are given so we can refer to them later. */
60 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
61 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
62 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
63 static void gst_ffmpegviddec_finalize (GObject * object);
65 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
66 GstVideoCodecState * state);
67 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
68 GstVideoCodecFrame * frame);
69 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
70 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
71 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
72 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
74 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
77 static void gst_ffmpegviddec_set_property (GObject * object,
78 guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_ffmpegviddec_get_property (GObject * object,
80 guint prop_id, GValue * value, GParamSpec * pspec);
82 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
83 AVCodecContext * context, AVFrame * picture);
85 /* some sort of bufferpool handling, but different */
86 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
87 AVFrame * picture, int flags);
89 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
90 static GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
92 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
94 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
95 AVCodecContext * context);
97 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
99 static GstElementClass *parent_class = NULL;
101 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
103 gst_ffmpegviddec_lowres_get_type (void)
105 static GType ffmpegdec_lowres_type = 0;
107 if (!ffmpegdec_lowres_type) {
108 static const GEnumValue ffmpegdec_lowres[] = {
110 {1, "1", "1/2-size"},
111 {2, "2", "1/4-size"},
115 ffmpegdec_lowres_type =
116 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
119 return ffmpegdec_lowres_type;
122 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
124 gst_ffmpegviddec_skipframe_get_type (void)
126 static GType ffmpegdec_skipframe_type = 0;
128 if (!ffmpegdec_skipframe_type) {
129 static const GEnumValue ffmpegdec_skipframe[] = {
130 {0, "0", "Skip nothing"},
131 {1, "1", "Skip B-frames"},
132 {2, "2", "Skip IDCT/Dequantization"},
133 {5, "5", "Skip everything"},
137 ffmpegdec_skipframe_type =
138 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
141 return ffmpegdec_skipframe_type;
145 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
147 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
148 GstPadTemplate *sinktempl, *srctempl;
149 GstCaps *sinkcaps, *srccaps;
151 gchar *longname, *description;
154 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
155 GST_FFDEC_PARAMS_QDATA);
156 g_assert (in_plugin != NULL);
158 /* construct the element details struct */
159 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
160 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
161 gst_element_class_set_metadata (element_class, longname,
162 "Codec/Decoder/Video", description,
163 "Wim Taymans <wim.taymans@gmail.com>, "
164 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
165 "Edward Hervey <bilboed@bilboed.com>");
167 g_free (description);
170 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
172 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
173 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
175 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
176 in_plugin->id, FALSE, in_plugin);
178 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
179 srccaps = gst_caps_from_string ("video/x-raw");
183 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
184 GST_PAD_ALWAYS, sinkcaps);
185 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
187 gst_element_class_add_pad_template (element_class, srctempl);
188 gst_element_class_add_pad_template (element_class, sinktempl);
190 gst_caps_unref (sinkcaps);
191 gst_caps_unref (srccaps);
193 klass->in_plugin = in_plugin;
197 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
203 parent_class = g_type_class_peek_parent (klass);
205 gobject_class->finalize = gst_ffmpegviddec_finalize;
207 gobject_class->set_property = gst_ffmpegviddec_set_property;
208 gobject_class->get_property = gst_ffmpegviddec_get_property;
210 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
211 g_param_spec_enum ("skip-frame", "Skip frames",
212 "Which types of frames to skip during decoding",
213 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class, PROP_LOWRES,
216 g_param_spec_enum ("lowres", "Low resolution",
217 "At which resolution to decode images",
218 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
219 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
221 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
222 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
225 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
226 "Whether libav should print motion vectors on top of the image",
227 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
229 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
230 "Whether libav should output frames even if corrupted",
231 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 caps = klass->in_plugin->capabilities;
234 if (caps & (AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS)) {
235 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
236 g_param_spec_int ("max-threads", "Maximum decode threads",
237 "Maximum number of worker threads to spawn. (0 = auto)",
238 0, G_MAXINT, DEFAULT_MAX_THREADS,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 viddec_class->set_format = gst_ffmpegviddec_set_format;
243 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
244 viddec_class->start = gst_ffmpegviddec_start;
245 viddec_class->stop = gst_ffmpegviddec_stop;
246 viddec_class->flush = gst_ffmpegviddec_flush;
247 viddec_class->finish = gst_ffmpegviddec_finish;
248 viddec_class->drain = gst_ffmpegviddec_drain;
249 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
250 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
254 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
256 GstFFMpegVidDecClass *klass =
257 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
259 /* some ffmpeg data */
260 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
261 ffmpegdec->context->opaque = ffmpegdec;
262 ffmpegdec->picture = av_frame_alloc ();
263 ffmpegdec->opened = FALSE;
264 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
265 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
266 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
267 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
268 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
270 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
271 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
274 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
278 gst_ffmpegviddec_finalize (GObject * object)
280 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
282 av_frame_free (&ffmpegdec->picture);
284 if (ffmpegdec->context != NULL) {
285 gst_ffmpeg_avcodec_close (ffmpegdec->context);
286 av_free (ffmpegdec->context);
287 ffmpegdec->context = NULL;
290 G_OBJECT_CLASS (parent_class)->finalize (object);
294 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
297 g_return_if_fail (context != NULL);
300 context->flags |= flags;
302 context->flags &= ~flags;
307 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
309 GstFFMpegVidDecClass *oclass;
312 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
314 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
316 gst_caps_replace (&ffmpegdec->last_caps, NULL);
318 gst_ffmpeg_avcodec_close (ffmpegdec->context);
319 ffmpegdec->opened = FALSE;
321 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
322 ffmpegdec->stride[i] = -1;
324 gst_buffer_replace (&ffmpegdec->palette, NULL);
326 if (ffmpegdec->context->extradata) {
327 av_free (ffmpegdec->context->extradata);
328 ffmpegdec->context->extradata = NULL;
331 if (avcodec_get_context_defaults3 (ffmpegdec->context,
332 oclass->in_plugin) < 0) {
333 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
336 ffmpegdec->context->opaque = ffmpegdec;
343 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
345 GstFFMpegVidDecClass *oclass;
348 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
350 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
353 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
354 ffmpegdec->stride[i] = -1;
356 ffmpegdec->opened = TRUE;
358 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
359 oclass->in_plugin->name, oclass->in_plugin->id);
361 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
362 AV_CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
369 gst_ffmpegviddec_close (ffmpegdec, TRUE);
370 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
371 oclass->in_plugin->name);
377 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
378 GstVideoCodecState * state)
380 GstStructure *str = gst_caps_get_structure (state->caps, 0);
381 const GValue *palette_v;
384 /* do we have a palette? */
385 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
386 palette = gst_value_get_buffer (palette_v);
387 GST_DEBUG ("got palette data %p", palette);
388 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
389 gst_buffer_replace (&ffmpegdec->palette, palette);
396 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
397 GstVideoCodecState * state)
399 GstFFMpegVidDec *ffmpegdec;
400 GstFFMpegVidDecClass *oclass;
401 GstClockTime latency = GST_CLOCK_TIME_NONE;
402 gboolean ret = FALSE;
404 ffmpegdec = (GstFFMpegVidDec *) decoder;
405 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
407 if (ffmpegdec->last_caps != NULL &&
408 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
412 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
414 GST_OBJECT_LOCK (ffmpegdec);
415 /* stupid check for VC1 */
416 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
417 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
418 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
420 /* close old session */
421 if (ffmpegdec->opened) {
422 GST_OBJECT_UNLOCK (ffmpegdec);
423 gst_ffmpegviddec_finish (decoder);
424 GST_OBJECT_LOCK (ffmpegdec);
425 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
426 GST_OBJECT_UNLOCK (ffmpegdec);
429 ffmpegdec->pic_pix_fmt = 0;
430 ffmpegdec->pic_width = 0;
431 ffmpegdec->pic_height = 0;
432 ffmpegdec->pic_par_n = 0;
433 ffmpegdec->pic_par_d = 0;
434 ffmpegdec->pic_interlaced = 0;
435 ffmpegdec->pic_field_order = 0;
436 ffmpegdec->pic_field_order_changed = FALSE;
437 ffmpegdec->ctx_ticks = 0;
438 ffmpegdec->ctx_time_n = 0;
439 ffmpegdec->ctx_time_d = 0;
440 ffmpegdec->cur_multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
441 ffmpegdec->cur_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
444 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
446 /* set buffer functions */
447 ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
448 ffmpegdec->context->draw_horiz_band = NULL;
450 /* reset coded_width/_height to prevent it being reused from last time when
451 * the codec is opened again, causing a mismatch and possible
452 * segfault/corruption. (Common scenario when renegotiating caps) */
453 ffmpegdec->context->coded_width = 0;
454 ffmpegdec->context->coded_height = 0;
456 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
457 ffmpegdec->context->height);
459 /* FIXME : Create a method that takes GstVideoCodecState instead */
460 /* get size and so */
461 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
462 oclass->in_plugin->type, state->caps, ffmpegdec->context);
464 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
465 ffmpegdec->context->height);
467 gst_ffmpegviddec_get_palette (ffmpegdec, state);
469 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
470 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
471 ffmpegdec->context->time_base.num = 1;
472 ffmpegdec->context->time_base.den = 25;
475 /* workaround encoder bugs */
476 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
477 ffmpegdec->context->err_recognition = 1;
480 ffmpegdec->context->lowres = ffmpegdec->lowres;
481 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
483 /* ffmpeg can draw motion vectors on top of the image (not every decoder
485 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
491 if (ffmpegdec->max_threads == 0) {
492 if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_AUTO_THREADS))
493 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
495 ffmpegdec->context->thread_count = 0;
497 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
499 query = gst_query_new_latency ();
501 /* Check if upstream is live. If it isn't we can enable frame based
502 * threading, which is adding latency */
503 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
504 gst_query_parse_latency (query, &is_live, NULL, NULL);
506 gst_query_unref (query);
509 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
511 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
514 /* open codec - we don't select an output pix_fmt yet,
515 * simply because we don't know! We only get it
516 * during playback... */
517 if (!gst_ffmpegviddec_open (ffmpegdec))
520 if (ffmpegdec->input_state)
521 gst_video_codec_state_unref (ffmpegdec->input_state);
522 ffmpegdec->input_state = gst_video_codec_state_ref (state);
524 if (ffmpegdec->input_state->info.fps_n) {
525 GstVideoInfo *info = &ffmpegdec->input_state->info;
526 latency = gst_util_uint64_scale_ceil (
527 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
534 GST_OBJECT_UNLOCK (ffmpegdec);
536 if (GST_CLOCK_TIME_IS_VALID (latency))
537 gst_video_decoder_set_latency (decoder, latency, latency);
544 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
551 GstFFMpegVidDec *ffmpegdec;
552 GstVideoCodecFrame *frame;
554 GstVideoFrame vframe;
556 AVBufferRef *avbuffer;
557 } GstFFMpegVidDecVideoFrame;
559 static GstFFMpegVidDecVideoFrame *
560 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
561 GstVideoCodecFrame * frame)
563 GstFFMpegVidDecVideoFrame *dframe;
565 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
566 dframe->ffmpegdec = ffmpegdec;
567 dframe->frame = frame;
569 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
575 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
576 GstFFMpegVidDecVideoFrame * frame)
578 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
581 gst_video_frame_unmap (&frame->vframe);
582 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
583 gst_buffer_replace (&frame->buffer, NULL);
584 if (frame->avbuffer) {
585 av_buffer_unref (&frame->avbuffer);
587 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
591 dummy_free_buffer (void *opaque, uint8_t * data)
593 GstFFMpegVidDecVideoFrame *frame = opaque;
595 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
598 /* This function prepares the pool configuration for direct rendering. To use
599 * this method, the codec should support direct rendering and the pool should
600 * support video meta and video alignment */
602 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
603 GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
605 GstAllocationParams params;
606 GstVideoAlignment align;
607 GstAllocator *allocator = NULL;
609 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 align.padding_top = 0;
621 align.padding_left = 0;
622 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info);
623 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info);
625 /* add extra padding to match libav buffer allocation sizes */
626 align.padding_bottom++;
628 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
630 max_align = DEFAULT_STRIDE_ALIGN;
631 max_align |= params.align;
633 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
634 if (linesize_align[i] > 0)
635 max_align |= linesize_align[i] - 1;
638 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
639 align.stride_align[i] = max_align;
641 params.align = max_align;
643 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
645 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
646 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
647 GST_VIDEO_INFO_WIDTH (info),
648 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
649 align.padding_left, align.padding_right, align.padding_bottom,
650 align.stride_align[0], align.stride_align[1], align.stride_align[2],
651 align.stride_align[3]);
653 gst_buffer_pool_config_add_option (config,
654 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
655 gst_buffer_pool_config_set_video_alignment (config, &align);
659 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
662 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
664 GstVideoFormat format;
666 GstStructure *config;
669 if (ffmpegdec->internal_pool != NULL &&
670 ffmpegdec->pool_width == picture->width &&
671 ffmpegdec->pool_height == picture->height &&
672 ffmpegdec->pool_format == picture->format)
675 GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
676 picture->width, picture->height);
678 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
679 gst_video_info_set_format (&info, format, picture->width, picture->height);
681 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
682 ffmpegdec->stride[i] = -1;
684 if (ffmpegdec->internal_pool)
685 gst_object_unref (ffmpegdec->internal_pool);
687 ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
688 config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
690 caps = gst_video_info_to_caps (&info);
691 gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
692 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
693 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
695 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
696 ffmpegdec->internal_pool, &info, config);
697 /* generic video pool never fails */
698 gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
699 gst_caps_unref (caps);
701 gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
703 /* Remember pool size so we can detect changes */
704 ffmpegdec->pool_width = picture->width;
705 ffmpegdec->pool_height = picture->height;
706 ffmpegdec->pool_format = picture->format;
707 ffmpegdec->pool_info = info;
711 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
713 GstFFMpegVidDecClass *oclass;
715 if (!ffmpegdec->direct_rendering)
718 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
719 return ((oclass->in_plugin->capabilities & AV_CODEC_CAP_DR1) ==
723 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
724 * into. We try to give it memory from our pool */
726 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
729 GstVideoCodecFrame *frame;
730 GstFFMpegVidDecVideoFrame *dframe;
731 GstFFMpegVidDec *ffmpegdec;
735 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
737 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
739 /* apply the last info we have seen to this picture, when we get the
740 * picture back from ffmpeg we can use this to correctly timestamp the output
742 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
743 (gint32) picture->reordered_opaque);
746 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
747 picture->reordered_opaque);
748 if (G_UNLIKELY (frame == NULL))
751 /* now it has a buffer allocated, so it is real and will also
753 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
754 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
756 if (G_UNLIKELY (frame->output_buffer != NULL))
757 goto duplicate_frame;
759 /* GstFFMpegVidDecVideoFrame receives the frame ref */
760 if (picture->opaque) {
761 dframe = picture->opaque;
762 dframe->frame = frame;
764 picture->opaque = dframe =
765 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
768 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
770 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
773 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
775 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
776 &frame->output_buffer, NULL);
777 if (ret != GST_FLOW_OK)
780 /* piggy-backed alloc'ed on the frame,
781 * and there was much rejoicing and we are grateful.
782 * Now take away buffer from frame, we will give it back later when decoded.
783 * This allows multiple request for a buffer per frame; unusual but possible. */
784 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
785 gst_buffer_replace (&frame->output_buffer, NULL);
788 if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
789 dframe->buffer, GST_MAP_READWRITE))
791 dframe->mapped = TRUE;
793 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
794 if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
795 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
796 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
798 if (ffmpegdec->stride[c] == -1)
799 ffmpegdec->stride[c] = picture->linesize[c];
801 /* libav does not allow stride changes, decide allocation should check
802 * before replacing the internal pool with a downstream pool.
803 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
804 * https://bugzilla.libav.org/show_bug.cgi?id=556
806 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
808 picture->data[c] = NULL;
809 picture->linesize[c] = 0;
811 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
815 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
817 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
824 int ret = avcodec_default_get_buffer2 (context, picture, flags);
826 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
828 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
829 ffmpegdec->stride[c] = picture->linesize[c];
831 /* Wrap our buffer around the default one to be able to have a callback
832 * when our data can be freed. Just putting our data into the first free
833 * buffer might not work if there are too many allocated already
835 if (picture->buf[0]) {
836 dframe->avbuffer = picture->buf[0];
838 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
839 dummy_free_buffer, dframe, 0);
842 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
849 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
850 ("Unable to allocate memory"),
851 ("The downstream pool failed to allocated buffer."));
856 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
857 ("Cannot access memory for read and write operation."),
858 ("The video memory allocated from downstream pool could not mapped for"
864 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
865 gst_video_codec_frame_unref (frame);
870 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
876 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
878 gint pic_field_order = 0;
880 if (picture->interlaced_frame) {
881 if (picture->repeat_pict)
882 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
883 if (picture->top_field_first)
884 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
887 return !(ffmpegdec->pic_width == picture->width
888 && ffmpegdec->pic_height == picture->height
889 && ffmpegdec->pic_pix_fmt == picture->format
890 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
891 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
892 && ffmpegdec->pic_interlaced == picture->interlaced_frame
893 && ffmpegdec->pic_field_order == pic_field_order
894 && ffmpegdec->cur_multiview_mode == ffmpegdec->picture_multiview_mode
895 && ffmpegdec->cur_multiview_flags == ffmpegdec->picture_multiview_flags);
899 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
901 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
902 && ffmpegdec->ctx_time_n == context->time_base.num
903 && ffmpegdec->ctx_time_d == context->time_base.den);
907 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
910 gint pic_field_order = 0;
912 if (picture->interlaced_frame) {
913 if (picture->repeat_pict)
914 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
915 if (picture->top_field_first)
916 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
919 if (!picture_changed (ffmpegdec, picture)
920 && !context_changed (ffmpegdec, context))
923 GST_DEBUG_OBJECT (ffmpegdec,
924 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
925 ffmpegdec->pic_width, ffmpegdec->pic_height,
926 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
927 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
928 ffmpegdec->pic_pix_fmt,
929 picture->width, picture->height,
930 picture->sample_aspect_ratio.num,
931 picture->sample_aspect_ratio.den,
932 context->time_base.num, context->time_base.den, picture->format);
934 ffmpegdec->pic_pix_fmt = picture->format;
935 ffmpegdec->pic_width = picture->width;
936 ffmpegdec->pic_height = picture->height;
937 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
938 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
939 ffmpegdec->cur_multiview_mode = ffmpegdec->picture_multiview_mode;
940 ffmpegdec->cur_multiview_flags = ffmpegdec->picture_multiview_flags;
942 /* Remember if we have interlaced content and the field order changed
943 * at least once. If that happens, we must be interlace-mode=mixed
945 if (ffmpegdec->pic_field_order_changed ||
946 (ffmpegdec->pic_field_order != pic_field_order &&
947 ffmpegdec->pic_interlaced))
948 ffmpegdec->pic_field_order_changed = TRUE;
950 ffmpegdec->pic_field_order = pic_field_order;
951 ffmpegdec->pic_interlaced = picture->interlaced_frame;
953 if (!ffmpegdec->pic_interlaced)
954 ffmpegdec->pic_field_order_changed = FALSE;
956 ffmpegdec->ctx_ticks = context->ticks_per_frame;
957 ffmpegdec->ctx_time_n = context->time_base.num;
958 ffmpegdec->ctx_time_d = context->time_base.den;
964 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
965 GstVideoInfo * in_info, GstVideoInfo * out_info)
967 gboolean demuxer_par_set = FALSE;
968 gboolean decoder_par_set = FALSE;
969 gint demuxer_num = 1, demuxer_denom = 1;
970 gint decoder_num = 1, decoder_denom = 1;
972 if (in_info->par_n && in_info->par_d) {
973 demuxer_num = in_info->par_n;
974 demuxer_denom = in_info->par_d;
975 demuxer_par_set = TRUE;
976 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
980 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
981 decoder_num = ffmpegdec->pic_par_n;
982 decoder_denom = ffmpegdec->pic_par_d;
983 decoder_par_set = TRUE;
984 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
988 if (!demuxer_par_set && !decoder_par_set)
991 if (demuxer_par_set && !decoder_par_set)
992 goto use_demuxer_par;
994 if (decoder_par_set && !demuxer_par_set)
995 goto use_decoder_par;
997 /* Both the demuxer and the decoder provide a PAR. If one of
998 * the two PARs is 1:1 and the other one is not, use the one
999 * that is not 1:1. */
1000 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
1001 goto use_decoder_par;
1003 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
1004 goto use_demuxer_par;
1006 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
1007 goto use_demuxer_par;
1011 GST_DEBUG_OBJECT (ffmpegdec,
1012 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
1014 out_info->par_n = decoder_num;
1015 out_info->par_d = decoder_denom;
1020 GST_DEBUG_OBJECT (ffmpegdec,
1021 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
1023 out_info->par_n = demuxer_num;
1024 out_info->par_d = demuxer_denom;
1029 GST_DEBUG_OBJECT (ffmpegdec,
1030 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1031 out_info->par_n = 1;
1032 out_info->par_d = 1;
1037 static GstVideoMultiviewMode
1038 stereo_av_to_gst (enum AVStereo3DType type)
1041 case AV_STEREO3D_SIDEBYSIDE:
1042 return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
1043 case AV_STEREO3D_TOPBOTTOM:
1044 return GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
1045 case AV_STEREO3D_FRAMESEQUENCE:
1046 return GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
1047 case AV_STEREO3D_CHECKERBOARD:
1048 return GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
1049 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
1050 return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX;
1051 case AV_STEREO3D_LINES:
1052 return GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED;
1053 case AV_STEREO3D_COLUMNS:
1054 return GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED;
1059 return GST_VIDEO_MULTIVIEW_MODE_NONE;
1063 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1064 AVCodecContext * context, AVFrame * picture)
1067 GstVideoInfo *in_info, *out_info;
1068 GstVideoCodecState *output_state;
1070 GstClockTime latency;
1073 if (!update_video_context (ffmpegdec, context, picture))
1076 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1077 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1078 goto unknown_format;
1081 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1082 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1083 if (ffmpegdec->output_state)
1084 gst_video_codec_state_unref (ffmpegdec->output_state);
1085 ffmpegdec->output_state = output_state;
1087 in_info = &ffmpegdec->input_state->info;
1088 out_info = &ffmpegdec->output_state->info;
1090 /* set the interlaced flag */
1091 in_s = gst_caps_get_structure (ffmpegdec->input_state->caps, 0);
1093 if (!gst_structure_has_field (in_s, "interlace-mode")) {
1094 if (ffmpegdec->pic_interlaced) {
1095 if (ffmpegdec->pic_field_order_changed ||
1096 (ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_RFF)) {
1097 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1099 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1100 if ((ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_TFF))
1101 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1102 GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
1104 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1105 GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST;
1108 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1112 if (!gst_structure_has_field (in_s, "chroma-site")) {
1113 switch (context->chroma_sample_location) {
1114 case AVCHROMA_LOC_LEFT:
1115 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1117 case AVCHROMA_LOC_CENTER:
1118 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1120 case AVCHROMA_LOC_TOPLEFT:
1121 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1123 case AVCHROMA_LOC_TOP:
1124 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1131 if (!gst_structure_has_field (in_s, "colorimetry")
1132 || in_info->colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN) {
1133 switch (context->color_primaries) {
1134 case AVCOL_PRI_BT709:
1135 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1137 case AVCOL_PRI_BT470M:
1138 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1140 case AVCOL_PRI_BT470BG:
1141 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1143 case AVCOL_PRI_SMPTE170M:
1144 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1146 case AVCOL_PRI_SMPTE240M:
1147 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1149 case AVCOL_PRI_FILM:
1150 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
1152 case AVCOL_PRI_BT2020:
1153 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1160 if (!gst_structure_has_field (in_s, "colorimetry")
1161 || in_info->colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN) {
1162 switch (context->color_trc) {
1163 case AVCOL_TRC_BT2020_10:
1164 case AVCOL_TRC_BT709:
1165 case AVCOL_TRC_SMPTE170M:
1166 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
1168 case AVCOL_TRC_GAMMA22:
1169 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
1171 case AVCOL_TRC_GAMMA28:
1172 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
1174 case AVCOL_TRC_SMPTE240M:
1175 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1177 case AVCOL_TRC_LINEAR:
1178 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
1181 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG100;
1183 case AVCOL_TRC_LOG_SQRT:
1184 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG316;
1186 case AVCOL_TRC_BT2020_12:
1187 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT2020_12;
1194 if (!gst_structure_has_field (in_s, "colorimetry")
1195 || in_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
1196 switch (context->colorspace) {
1198 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
1200 case AVCOL_SPC_BT709:
1201 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1204 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
1206 case AVCOL_SPC_BT470BG:
1207 case AVCOL_SPC_SMPTE170M:
1208 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1210 case AVCOL_SPC_SMPTE240M:
1211 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1213 case AVCOL_SPC_BT2020_NCL:
1214 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1221 if (!gst_structure_has_field (in_s, "colorimetry")
1222 || in_info->colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN) {
1223 if (context->color_range == AVCOL_RANGE_JPEG) {
1224 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
1226 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
1230 /* try to find a good framerate */
1231 if ((in_info->fps_d && in_info->fps_n) ||
1232 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1233 /* take framerate from input when it was specified (#313970) */
1234 fps_n = in_info->fps_n;
1235 fps_d = in_info->fps_d;
1237 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1238 fps_d = ffmpegdec->ctx_time_n;
1241 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1245 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1246 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1253 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1254 out_info->fps_n = fps_n;
1255 out_info->fps_d = fps_d;
1257 /* calculate and update par now */
1258 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1260 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) = ffmpegdec->cur_multiview_mode;
1261 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) = ffmpegdec->cur_multiview_flags;
1263 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1264 goto negotiate_failed;
1266 /* The decoder is configured, we now know the true latency */
1269 gst_util_uint64_scale_ceil (ffmpegdec->context->has_b_frames *
1270 GST_SECOND, fps_d, fps_n);
1271 gst_video_decoder_set_latency (GST_VIDEO_DECODER (ffmpegdec), latency,
1280 GST_ERROR_OBJECT (ffmpegdec,
1281 "decoder requires a video format unsupported by GStreamer");
1286 /* Reset so we try again next time even if force==FALSE */
1287 ffmpegdec->pic_pix_fmt = 0;
1288 ffmpegdec->pic_width = 0;
1289 ffmpegdec->pic_height = 0;
1290 ffmpegdec->pic_par_n = 0;
1291 ffmpegdec->pic_par_d = 0;
1292 ffmpegdec->pic_interlaced = 0;
1293 ffmpegdec->pic_field_order = 0;
1294 ffmpegdec->pic_field_order_changed = FALSE;
1295 ffmpegdec->ctx_ticks = 0;
1296 ffmpegdec->ctx_time_n = 0;
1297 ffmpegdec->ctx_time_d = 0;
1299 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1304 /* perform qos calculations before decoding the next frame.
1306 * Sets the skip_frame flag and if things are really bad, skips to the next
1311 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1312 GstVideoCodecFrame * frame, gboolean * mode_switch)
1314 GstClockTimeDiff diff;
1315 GstSegmentFlags skip_flags =
1316 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1318 *mode_switch = FALSE;
1323 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1324 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1325 *mode_switch = TRUE;
1326 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1327 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1328 *mode_switch = TRUE;
1331 if (*mode_switch == TRUE) {
1332 /* We've already switched mode, we can return straight away
1333 * without any further calculation */
1338 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1341 /* if we don't have timing info, then we don't do QoS */
1342 if (G_UNLIKELY (diff == G_MAXINT64)) {
1343 /* Ensure the skipping strategy is the default one */
1344 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
1348 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1350 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1351 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1352 *mode_switch = TRUE;
1353 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1356 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1357 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1358 *mode_switch = TRUE;
1359 GST_DEBUG_OBJECT (ffmpegdec,
1360 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1364 /* get an outbuf buffer with the current picture */
1365 static GstFlowReturn
1366 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1368 GstFlowReturn ret = GST_FLOW_OK;
1369 AVFrame pic, *outpic;
1370 GstVideoFrame vframe;
1374 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1376 if (!ffmpegdec->output_state)
1377 goto not_negotiated;
1380 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1382 if (G_UNLIKELY (ret != GST_FLOW_OK))
1385 /* original ffmpeg code does not handle odd sizes correctly.
1386 * This patched up version does */
1387 /* Fill avpicture */
1388 info = &ffmpegdec->output_state->info;
1389 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1390 GST_MAP_READ | GST_MAP_WRITE))
1393 memset (&pic, 0, sizeof (pic));
1394 pic.format = ffmpegdec->pic_pix_fmt;
1395 pic.width = GST_VIDEO_FRAME_WIDTH (&vframe);
1396 pic.height = GST_VIDEO_FRAME_HEIGHT (&vframe);
1397 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1398 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1399 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1400 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1401 GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1402 pic.linesize[c], pic.data[c]);
1405 pic.linesize[c] = 0;
1409 outpic = ffmpegdec->picture;
1411 if (av_frame_copy (&pic, outpic) != 0) {
1412 GST_ERROR_OBJECT (ffmpegdec, "Failed to copy output frame");
1413 ret = GST_FLOW_ERROR;
1416 gst_video_frame_unmap (&vframe);
1418 ffmpegdec->picture->reordered_opaque = -1;
1425 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
1426 ("Unable to allocate memory"),
1427 ("The downstream pool failed to allocated buffer."));
1432 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
1433 ("Cannot access memory for read and write operation."),
1434 ("The video memory allocated from downstream pool could not mapped for"
1435 "read and write."));
1440 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1441 return GST_FLOW_NOT_NEGOTIATED;
1446 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1448 memset (packet, 0, sizeof (AVPacket));
1449 packet->data = data;
1450 packet->size = size;
1453 /* gst_ffmpegviddec_[video|audio]_frame:
1455 * data: pointer to the data to decode
1456 * size: size of data in bytes
1457 * in_timestamp: incoming timestamp.
1458 * in_duration: incoming duration.
1459 * in_offset: incoming offset (frame number).
1462 * Returns: number of bytes used in decoding. The check for successful decode is
1463 * outbuf being non-NULL.
1466 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1467 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1468 GstFlowReturn * ret)
1471 gboolean mode_switch;
1472 GstVideoCodecFrame *out_frame;
1473 GstFFMpegVidDecVideoFrame *out_dframe;
1475 GstBufferPool *pool;
1479 /* in case we skip frames */
1480 ffmpegdec->picture->pict_type = -1;
1482 /* run QoS code, we don't stop decoding the frame when we are late because
1483 * else we might skip a reference frame */
1484 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1487 /* save reference to the timing info */
1488 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1489 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1491 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1492 frame->system_frame_number);
1495 /* now decode the frame */
1496 gst_avpacket_init (&packet, data, size);
1498 if (ffmpegdec->palette) {
1501 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1503 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1504 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1507 /* This might call into get_buffer() from another thread,
1508 * which would cause a deadlock. Release the lock here
1509 * and taking it again later seems safe
1510 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1512 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1513 len = avcodec_decode_video2 (ffmpegdec->context,
1514 ffmpegdec->picture, have_data, &packet);
1515 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1517 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1520 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1521 * no data because we told it to skip stuff. */
1522 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1525 /* no data, we're done */
1526 if (len < 0 || *have_data == 0)
1529 /* get the output picture timing info again */
1530 out_dframe = ffmpegdec->picture->opaque;
1531 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1533 /* also give back a buffer allocated by the frame, if any */
1534 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1535 gst_buffer_replace (&out_dframe->buffer, NULL);
1537 /* Extract auxilliary info not stored in the main AVframe */
1539 GstVideoInfo *in_info = &ffmpegdec->input_state->info;
1540 /* Take multiview mode from upstream if present */
1541 ffmpegdec->picture_multiview_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1542 ffmpegdec->picture_multiview_flags =
1543 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1545 /* Otherwise, see if there's info in the frame */
1546 if (ffmpegdec->picture_multiview_mode == GST_VIDEO_MULTIVIEW_MODE_NONE) {
1547 AVFrameSideData *side_data =
1548 av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_STEREO3D);
1550 AVStereo3D *stereo = (AVStereo3D *) side_data->data;
1551 ffmpegdec->picture_multiview_mode = stereo_av_to_gst (stereo->type);
1552 if (stereo->flags & AV_STEREO3D_FLAG_INVERT) {
1553 ffmpegdec->picture_multiview_flags =
1554 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
1556 ffmpegdec->picture_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
1562 GST_DEBUG_OBJECT (ffmpegdec,
1563 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1564 out_frame->pts, out_frame->duration);
1565 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1566 (guint64) ffmpegdec->picture->pts);
1567 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1568 ffmpegdec->picture->coded_picture_number);
1569 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1570 ffmpegdec->picture->display_picture_number);
1571 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1572 ffmpegdec->picture->opaque);
1573 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1574 (guint64) ffmpegdec->picture->reordered_opaque);
1575 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1576 ffmpegdec->picture->repeat_pict);
1577 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1578 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1580 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1581 ffmpegdec->picture))
1582 goto negotiation_error;
1584 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1585 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1586 *ret = get_output_buffer (ffmpegdec, out_frame);
1587 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1588 GstBuffer *tmp = out_frame->output_buffer;
1589 out_frame->output_buffer = NULL;
1590 *ret = get_output_buffer (ffmpegdec, out_frame);
1591 gst_buffer_unref (tmp);
1593 #ifndef G_DISABLE_ASSERT
1595 GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1597 GstVideoInfo *info = &ffmpegdec->output_state->info;
1598 g_assert (vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1599 g_assert (vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1603 gst_object_unref (pool);
1605 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1608 /* Mark corrupted frames as corrupted */
1609 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1610 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1612 if (ffmpegdec->pic_interlaced) {
1613 /* set interlaced flags */
1614 if (ffmpegdec->picture->repeat_pict)
1615 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1616 if (ffmpegdec->picture->top_field_first)
1617 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1618 if (ffmpegdec->picture->interlaced_frame)
1619 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1620 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1624 AVFrameSideData *side_data =
1625 av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_A53_CC);
1627 GST_LOG_OBJECT (ffmpegdec, "Found CC of size %d", side_data->size);
1628 GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
1629 out_frame->output_buffer =
1630 gst_buffer_make_writable (out_frame->output_buffer);
1631 gst_buffer_add_video_caption_meta (out_frame->output_buffer,
1632 GST_VIDEO_CAPTION_TYPE_CEA708_RAW, side_data->data, side_data->size);
1637 /* so we decoded this frame, frames preceding it in decoding order
1638 * that still do not have a buffer allocated seem rather useless,
1639 * and can be discarded, due to e.g. misparsed bogus frame
1640 * or non-keyframe in skipped decoding, ...
1641 * In any case, not likely to be seen again, so discard those,
1642 * before they pile up and/or mess with timestamping */
1645 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1646 gboolean old = TRUE;
1648 ol = l = gst_video_decoder_get_frames (dec);
1650 GstVideoCodecFrame *tmp = l->data;
1655 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1656 GST_LOG_OBJECT (dec,
1657 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1658 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1659 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1660 /* drop extra ref and remove from frame list */
1661 gst_video_decoder_release_frame (dec, tmp);
1663 /* drop extra ref we got */
1664 gst_video_codec_frame_unref (tmp);
1671 av_frame_unref (ffmpegdec->picture);
1673 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1674 * libav might still have a reference to it!
1677 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1680 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1681 gst_flow_get_name (*ret), len);
1687 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1688 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1695 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1696 *ret = GST_FLOW_FLUSHING;
1699 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1700 *ret = GST_FLOW_NOT_NEGOTIATED;
1706 /* gst_ffmpegviddec_frame:
1708 * data: pointer to the data to decode
1709 * size: size of data in bytes
1710 * got_data: 0 if no data was decoded, != 0 otherwise.
1711 * in_time: timestamp of data
1712 * in_duration: duration of data
1713 * ret: GstFlowReturn to return in the chain function
1715 * Decode the given frame and pushes it downstream.
1717 * Returns: Number of bytes used in decoding, -1 on error/failure.
1721 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1722 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1723 GstFlowReturn * ret)
1725 GstFFMpegVidDecClass *oclass;
1728 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1731 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1734 ffmpegdec->context->frame_number++;
1736 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1739 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1743 GST_WARNING_OBJECT (ffmpegdec,
1744 "avdec_%s: decoding error (len: %d, have_data: %d)",
1745 oclass->in_plugin->name, len, *have_data);
1753 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1754 *ret = GST_FLOW_NOT_NEGOTIATED;
1759 static GstFlowReturn
1760 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1762 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1763 GstFFMpegVidDecClass *oclass;
1765 if (!ffmpegdec->opened)
1768 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1770 if (oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) {
1771 gint have_data, len;
1774 GST_LOG_OBJECT (ffmpegdec,
1775 "codec has delay capabilities, calling until ffmpeg has drained everything");
1778 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1779 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1785 static GstFlowReturn
1786 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1787 GstVideoCodecFrame * frame)
1789 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1790 guint8 *data, *bdata;
1791 gint size, len, have_data, bsize;
1793 GstFlowReturn ret = GST_FLOW_OK;
1794 gboolean do_padding;
1796 GST_LOG_OBJECT (ffmpegdec,
1797 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1798 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1799 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1800 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1802 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1803 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1804 ("Failed to map buffer for reading"));
1805 return GST_FLOW_ERROR;
1808 /* treat frame as void until a buffer is requested for it */
1809 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1810 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1815 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1816 || (minfo.maxsize - minfo.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
1818 if (ffmpegdec->padded_size < bsize + AV_INPUT_BUFFER_PADDING_SIZE) {
1819 ffmpegdec->padded_size = bsize + AV_INPUT_BUFFER_PADDING_SIZE;
1820 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1821 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1822 ffmpegdec->padded_size);
1824 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1825 "Copy input to add padding");
1826 memcpy (ffmpegdec->padded, bdata, bsize);
1827 memset (ffmpegdec->padded + bsize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1829 bdata = ffmpegdec->padded;
1836 guint8 tmp_padding[AV_INPUT_BUFFER_PADDING_SIZE];
1838 /* parse, if at all possible */
1843 /* add temporary padding */
1844 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1845 "Add temporary input padding");
1846 memcpy (tmp_padding, data + size, AV_INPUT_BUFFER_PADDING_SIZE);
1847 memset (data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1850 /* decode a frame of audio/video now */
1852 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1854 if (ret != GST_FLOW_OK) {
1855 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1856 gst_flow_get_name (ret));
1857 /* bad flow return, make sure we discard all data and exit */
1863 memcpy (data + size, tmp_padding, AV_INPUT_BUFFER_PADDING_SIZE);
1866 if (len == 0 && have_data == 0) {
1867 /* nothing was decoded, this could be because no data was available or
1868 * because we were skipping frames.
1869 * If we have no context we must exit and wait for more data, we keep the
1871 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1876 /* a decoding error happened, we must break and try again with next data. */
1877 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1882 /* prepare for the next round, for codecs with a context we did this
1883 * already when using the parser. */
1889 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1891 } while (bsize > 0);
1894 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1896 gst_buffer_unmap (frame->input_buffer, &minfo);
1897 gst_video_codec_frame_unref (frame);
1903 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1905 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1906 GstFFMpegVidDecClass *oclass;
1908 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1910 GST_OBJECT_LOCK (ffmpegdec);
1911 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1912 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1913 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1914 GST_OBJECT_UNLOCK (ffmpegdec);
1917 ffmpegdec->context->opaque = ffmpegdec;
1918 GST_OBJECT_UNLOCK (ffmpegdec);
1924 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1926 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1928 GST_OBJECT_LOCK (ffmpegdec);
1929 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1930 GST_OBJECT_UNLOCK (ffmpegdec);
1931 g_free (ffmpegdec->padded);
1932 ffmpegdec->padded = NULL;
1933 ffmpegdec->padded_size = 0;
1934 if (ffmpegdec->input_state)
1935 gst_video_codec_state_unref (ffmpegdec->input_state);
1936 ffmpegdec->input_state = NULL;
1937 if (ffmpegdec->output_state)
1938 gst_video_codec_state_unref (ffmpegdec->output_state);
1939 ffmpegdec->output_state = NULL;
1941 if (ffmpegdec->internal_pool)
1942 gst_object_unref (ffmpegdec->internal_pool);
1943 ffmpegdec->internal_pool = NULL;
1945 ffmpegdec->pic_pix_fmt = 0;
1946 ffmpegdec->pic_width = 0;
1947 ffmpegdec->pic_height = 0;
1948 ffmpegdec->pic_par_n = 0;
1949 ffmpegdec->pic_par_d = 0;
1950 ffmpegdec->pic_interlaced = 0;
1951 ffmpegdec->pic_field_order = 0;
1952 ffmpegdec->pic_field_order_changed = FALSE;
1953 ffmpegdec->ctx_ticks = 0;
1954 ffmpegdec->ctx_time_n = 0;
1955 ffmpegdec->ctx_time_d = 0;
1957 ffmpegdec->pool_width = 0;
1958 ffmpegdec->pool_height = 0;
1959 ffmpegdec->pool_format = 0;
1964 static GstFlowReturn
1965 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1967 gst_ffmpegviddec_drain (decoder);
1968 /* note that finish can and should clean up more drastically,
1969 * but drain is also invoked on e.g. packet loss in GAP handling */
1970 gst_ffmpegviddec_flush (decoder);
1976 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1978 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1980 if (ffmpegdec->opened) {
1981 GST_LOG_OBJECT (decoder, "flushing buffers");
1982 avcodec_flush_buffers (ffmpegdec->context);
1989 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1991 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1992 GstVideoCodecState *state;
1993 GstBufferPool *pool;
1994 guint size, min, max;
1995 GstStructure *config;
1996 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1997 GstAllocator *allocator = NULL;
1998 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
2000 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
2002 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
2006 state = gst_video_decoder_get_output_state (decoder);
2008 if (gst_query_get_n_allocation_params (query) > 0) {
2009 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2010 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
2012 gst_query_add_allocation_param (query, allocator, ¶ms);
2015 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2017 /* Don't use pool that can't grow, as we don't know how many buffer we'll
2018 * need, otherwise we may stall */
2019 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
2020 gst_object_unref (pool);
2021 pool = gst_video_buffer_pool_new ();
2026 /* if there is an allocator, also drop it, as it might be the reason we
2027 * have this limit. Default will be used */
2029 gst_object_unref (allocator);
2034 config = gst_buffer_pool_get_config (pool);
2035 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2036 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2039 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2042 gst_buffer_pool_config_add_option (config,
2043 GST_BUFFER_POOL_OPTION_VIDEO_META);
2046 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
2048 /* If we have videometa, we never have to copy */
2049 if (have_videometa && have_pool && have_alignment &&
2050 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
2051 GstStructure *config_copy = gst_structure_copy (config);
2053 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
2056 /* FIXME validate and retry */
2057 if (gst_buffer_pool_set_config (pool, config_copy)) {
2061 gst_buffer_pool_set_active (pool, TRUE);
2062 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
2063 if (ret == GST_FLOW_OK) {
2064 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
2065 gboolean same_stride = TRUE;
2068 for (i = 0; i < vmeta->n_planes; i++) {
2069 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
2070 same_stride = FALSE;
2075 gst_buffer_unref (tmp);
2078 if (ffmpegdec->internal_pool)
2079 gst_object_unref (ffmpegdec->internal_pool);
2080 ffmpegdec->internal_pool = gst_object_ref (pool);
2081 ffmpegdec->pool_info = state->info;
2082 gst_structure_free (config);
2089 if (have_videometa && ffmpegdec->internal_pool
2090 && ffmpegdec->pool_width == state->info.width
2091 && ffmpegdec->pool_height == state->info.height) {
2093 gst_object_unref (pool);
2094 pool = gst_object_ref (ffmpegdec->internal_pool);
2095 gst_structure_free (config);
2100 if (!gst_buffer_pool_set_config (pool, config)) {
2101 gboolean working_pool = FALSE;
2102 config = gst_buffer_pool_get_config (pool);
2104 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
2106 working_pool = gst_buffer_pool_set_config (pool, config);
2108 gst_structure_free (config);
2111 if (!working_pool) {
2112 gst_object_unref (pool);
2113 pool = gst_video_buffer_pool_new ();
2114 config = gst_buffer_pool_get_config (pool);
2115 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2116 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
2117 gst_buffer_pool_set_config (pool, config);
2125 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2127 gst_object_unref (pool);
2129 gst_object_unref (allocator);
2130 gst_video_codec_state_unref (state);
2136 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
2139 GstAllocationParams params;
2141 gst_allocation_params_init (¶ms);
2142 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
2143 params.align = DEFAULT_STRIDE_ALIGN;
2144 params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
2145 /* we would like to have some padding so that we don't have to
2146 * memcpy. We don't suggest an allocator. */
2147 gst_query_add_allocation_param (query, NULL, ¶ms);
2149 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
2154 gst_ffmpegviddec_set_property (GObject * object,
2155 guint prop_id, const GValue * value, GParamSpec * pspec)
2157 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2161 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
2163 case PROP_SKIPFRAME:
2164 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
2165 g_value_get_enum (value);
2167 case PROP_DIRECT_RENDERING:
2168 ffmpegdec->direct_rendering = g_value_get_boolean (value);
2171 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
2172 g_value_get_boolean (value);
2174 case PROP_MAX_THREADS:
2175 ffmpegdec->max_threads = g_value_get_int (value);
2177 case PROP_OUTPUT_CORRUPT:
2178 ffmpegdec->output_corrupt = g_value_get_boolean (value);
2181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2187 gst_ffmpegviddec_get_property (GObject * object,
2188 guint prop_id, GValue * value, GParamSpec * pspec)
2190 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2194 g_value_set_enum (value, ffmpegdec->context->lowres);
2196 case PROP_SKIPFRAME:
2197 g_value_set_enum (value, ffmpegdec->context->skip_frame);
2199 case PROP_DIRECT_RENDERING:
2200 g_value_set_boolean (value, ffmpegdec->direct_rendering);
2203 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
2205 case PROP_MAX_THREADS:
2206 g_value_set_int (value, ffmpegdec->max_threads);
2208 case PROP_OUTPUT_CORRUPT:
2209 g_value_set_boolean (value, ffmpegdec->output_corrupt);
2212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2218 gst_ffmpegviddec_register (GstPlugin * plugin)
2220 GTypeInfo typeinfo = {
2221 sizeof (GstFFMpegVidDecClass),
2222 (GBaseInitFunc) gst_ffmpegviddec_base_init,
2224 (GClassInitFunc) gst_ffmpegviddec_class_init,
2227 sizeof (GstFFMpegVidDec),
2229 (GInstanceInitFunc) gst_ffmpegviddec_init,
2236 GST_LOG ("Registering decoders");
2238 while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
2242 /* only video decoders */
2243 if (!av_codec_is_decoder (in_plugin)
2244 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2247 /* no quasi codecs, please */
2248 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2249 in_plugin->id == AV_CODEC_ID_V210 ||
2250 in_plugin->id == AV_CODEC_ID_V210X ||
2251 in_plugin->id == AV_CODEC_ID_V308 ||
2252 in_plugin->id == AV_CODEC_ID_V408 ||
2253 in_plugin->id == AV_CODEC_ID_V410 ||
2254 in_plugin->id == AV_CODEC_ID_R210
2255 || in_plugin->id == AV_CODEC_ID_AYUV
2256 || in_plugin->id == AV_CODEC_ID_Y41P
2257 || in_plugin->id == AV_CODEC_ID_012V
2258 || in_plugin->id == AV_CODEC_ID_YUV4
2259 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
2260 AV_VERSION_INT (57,4,0)
2261 || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
2263 || in_plugin->id == AV_CODEC_ID_ZLIB) {
2267 /* No decoders depending on external libraries (we don't build them, but
2268 * people who build against an external ffmpeg might have them.
2269 * We have native gstreamer plugins for all of those libraries anyway. */
2270 if (!strncmp (in_plugin->name, "lib", 3)) {
2272 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2277 /* No vdpau plugins until we can figure out how to properly use them
2278 * outside of ffmpeg. */
2279 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2281 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2286 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2288 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2293 if (strstr (in_plugin->name, "vaapi")) {
2295 ("Ignoring VAAPI decoder %s. We can't handle this outside of ffmpeg",
2300 if (g_str_has_suffix (in_plugin->name, "_qsv")) {
2302 ("Ignoring qsv decoder %s. We can't handle this outside of ffmpeg",
2307 if (g_str_has_suffix (in_plugin->name, "_cuvid")) {
2309 ("Ignoring CUVID decoder %s. We can't handle this outside of ffmpeg",
2314 if (g_str_has_suffix (in_plugin->name, "_v4l2m2m")) {
2316 ("Ignoring V4L2 mem-to-mem decoder %s. We can't handle this outside of ffmpeg",
2321 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2323 /* no codecs for which we're GUARANTEED to have better alternatives */
2324 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2325 /* MP1 : Use MP3 for decoding */
2326 /* MP2 : Use MP3 for decoding */
2327 /* Theora: Use libtheora based theoradec */
2328 if (!strcmp (in_plugin->name, "theora") ||
2329 !strcmp (in_plugin->name, "mpeg1video") ||
2330 strstr (in_plugin->name, "crystalhd") != NULL ||
2331 !strcmp (in_plugin->name, "ass") ||
2332 !strcmp (in_plugin->name, "srt") ||
2333 !strcmp (in_plugin->name, "pgssub") ||
2334 !strcmp (in_plugin->name, "dvdsub") ||
2335 !strcmp (in_plugin->name, "dvbsub")) {
2336 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2340 /* construct the type */
2341 if (!strcmp (in_plugin->name, "hevc")) {
2342 plugin_name = g_strdup ("h265");
2344 plugin_name = g_strdup ((gchar *) in_plugin->name);
2346 g_strdelimit (plugin_name, NULL, '_');
2347 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2348 g_free (plugin_name);
2350 type = g_type_from_name (type_name);
2353 /* create the gtype now */
2355 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2357 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2360 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2361 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2362 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2363 * VC1/WMV3 are not working and thus unpreferred for now. */
2364 switch (in_plugin->id) {
2365 case AV_CODEC_ID_MPEG1VIDEO:
2366 case AV_CODEC_ID_MPEG2VIDEO:
2367 case AV_CODEC_ID_MPEG4:
2368 case AV_CODEC_ID_MSMPEG4V3:
2369 case AV_CODEC_ID_H264:
2370 case AV_CODEC_ID_HEVC:
2371 case AV_CODEC_ID_RV10:
2372 case AV_CODEC_ID_RV20:
2373 case AV_CODEC_ID_RV30:
2374 case AV_CODEC_ID_RV40:
2375 rank = GST_RANK_PRIMARY;
2377 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2378 * They say libdv's quality is better though. leave as secondary.
2379 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2381 case AV_CODEC_ID_DVVIDEO:
2382 rank = GST_RANK_SECONDARY;
2385 rank = GST_RANK_MARGINAL;
2388 if (!gst_element_register (plugin, type_name, rank, type)) {
2389 g_warning ("Failed to register %s", type_name);
2397 GST_LOG ("Finished Registering decoders");