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 #define MAX_TS_MASK 0xff
42 #define DEFAULT_LOWRES 0
43 #define DEFAULT_SKIPFRAME 0
44 #define DEFAULT_DIRECT_RENDERING TRUE
45 #define DEFAULT_DEBUG_MV FALSE
46 #define DEFAULT_MAX_THREADS 0
47 #define DEFAULT_OUTPUT_CORRUPT TRUE
48 #define REQUIRED_POOL_MAX_BUFFERS 32
49 #define DEFAULT_STRIDE_ALIGN 31
50 #define DEFAULT_ALLOC_PARAM { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
57 PROP_DIRECT_RENDERING,
64 /* A number of function prototypes are given so we can refer to them later. */
65 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
66 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
67 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
68 static void gst_ffmpegviddec_finalize (GObject * object);
70 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
71 GstVideoCodecState * state);
72 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
73 GstVideoCodecFrame * frame);
74 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
75 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
76 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
77 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
79 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
82 static void gst_ffmpegviddec_set_property (GObject * object,
83 guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_ffmpegviddec_get_property (GObject * object,
85 guint prop_id, GValue * value, GParamSpec * pspec);
87 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
88 AVCodecContext * context, AVFrame * picture);
90 /* some sort of bufferpool handling, but different */
91 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
92 AVFrame * picture, int flags);
94 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
95 static GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
97 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
99 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
100 AVCodecContext * context);
102 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
104 static GstElementClass *parent_class = NULL;
106 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
108 gst_ffmpegviddec_lowres_get_type (void)
110 static GType ffmpegdec_lowres_type = 0;
112 if (!ffmpegdec_lowres_type) {
113 static const GEnumValue ffmpegdec_lowres[] = {
115 {1, "1", "1/2-size"},
116 {2, "2", "1/4-size"},
120 ffmpegdec_lowres_type =
121 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
124 return ffmpegdec_lowres_type;
127 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
129 gst_ffmpegviddec_skipframe_get_type (void)
131 static GType ffmpegdec_skipframe_type = 0;
133 if (!ffmpegdec_skipframe_type) {
134 static const GEnumValue ffmpegdec_skipframe[] = {
135 {0, "0", "Skip nothing"},
136 {1, "1", "Skip B-frames"},
137 {2, "2", "Skip IDCT/Dequantization"},
138 {5, "5", "Skip everything"},
142 ffmpegdec_skipframe_type =
143 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
146 return ffmpegdec_skipframe_type;
150 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
152 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
153 GstPadTemplate *sinktempl, *srctempl;
154 GstCaps *sinkcaps, *srccaps;
156 gchar *longname, *description;
159 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
160 GST_FFDEC_PARAMS_QDATA);
161 g_assert (in_plugin != NULL);
163 /* construct the element details struct */
164 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
165 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
166 gst_element_class_set_metadata (element_class, longname,
167 "Codec/Decoder/Video", description,
168 "Wim Taymans <wim.taymans@gmail.com>, "
169 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
170 "Edward Hervey <bilboed@bilboed.com>");
172 g_free (description);
175 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
177 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
178 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
180 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
181 in_plugin->id, FALSE, in_plugin);
183 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
184 srccaps = gst_caps_from_string ("video/x-raw");
188 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
189 GST_PAD_ALWAYS, sinkcaps);
190 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
192 gst_element_class_add_pad_template (element_class, srctempl);
193 gst_element_class_add_pad_template (element_class, sinktempl);
195 klass->in_plugin = in_plugin;
199 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
201 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
202 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
205 parent_class = g_type_class_peek_parent (klass);
207 gobject_class->finalize = gst_ffmpegviddec_finalize;
209 gobject_class->set_property = gst_ffmpegviddec_set_property;
210 gobject_class->get_property = gst_ffmpegviddec_get_property;
212 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
213 g_param_spec_enum ("skip-frame", "Skip frames",
214 "Which types of frames to skip during decoding",
215 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
216 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 g_object_class_install_property (gobject_class, PROP_LOWRES,
218 g_param_spec_enum ("lowres", "Low resolution",
219 "At which resolution to decode images",
220 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
223 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
224 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
227 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
228 "Whether libav should print motion vectors on top of the image",
229 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
231 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
232 "Whether libav should output frames even if corrupted",
233 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 caps = klass->in_plugin->capabilities;
236 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
237 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
238 g_param_spec_int ("max-threads", "Maximum decode threads",
239 "Maximum number of worker threads to spawn. (0 = auto)",
240 0, G_MAXINT, DEFAULT_MAX_THREADS,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 viddec_class->set_format = gst_ffmpegviddec_set_format;
245 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
246 viddec_class->start = gst_ffmpegviddec_start;
247 viddec_class->stop = gst_ffmpegviddec_stop;
248 viddec_class->flush = gst_ffmpegviddec_flush;
249 viddec_class->finish = gst_ffmpegviddec_finish;
250 viddec_class->drain = gst_ffmpegviddec_drain;
251 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
252 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
256 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
258 GstFFMpegVidDecClass *klass =
259 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
261 /* some ffmpeg data */
262 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
263 ffmpegdec->context->opaque = ffmpegdec;
264 ffmpegdec->picture = av_frame_alloc ();
265 ffmpegdec->opened = FALSE;
266 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
267 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
268 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
269 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
270 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
272 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
273 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
276 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
280 gst_ffmpegviddec_finalize (GObject * object)
282 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
284 av_frame_free (&ffmpegdec->picture);
286 if (ffmpegdec->context != NULL) {
287 gst_ffmpeg_avcodec_close (ffmpegdec->context);
288 av_free (ffmpegdec->context);
289 ffmpegdec->context = NULL;
292 G_OBJECT_CLASS (parent_class)->finalize (object);
296 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
299 g_return_if_fail (context != NULL);
302 context->flags |= flags;
304 context->flags &= ~flags;
309 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
311 GstFFMpegVidDecClass *oclass;
314 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
316 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
318 gst_caps_replace (&ffmpegdec->last_caps, NULL);
320 gst_ffmpeg_avcodec_close (ffmpegdec->context);
321 ffmpegdec->opened = FALSE;
323 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
324 ffmpegdec->stride[i] = -1;
326 gst_buffer_replace (&ffmpegdec->palette, NULL);
328 if (ffmpegdec->context->extradata) {
329 av_free (ffmpegdec->context->extradata);
330 ffmpegdec->context->extradata = NULL;
333 if (avcodec_get_context_defaults3 (ffmpegdec->context,
334 oclass->in_plugin) < 0) {
335 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
338 ffmpegdec->context->opaque = ffmpegdec;
345 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
347 GstFFMpegVidDecClass *oclass;
350 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
352 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
355 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
356 ffmpegdec->stride[i] = -1;
358 ffmpegdec->opened = TRUE;
360 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
361 oclass->in_plugin->name, oclass->in_plugin->id);
363 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
364 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
371 gst_ffmpegviddec_close (ffmpegdec, TRUE);
372 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
373 oclass->in_plugin->name);
379 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
380 GstVideoCodecState * state)
382 GstStructure *str = gst_caps_get_structure (state->caps, 0);
383 const GValue *palette_v;
386 /* do we have a palette? */
387 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
388 palette = gst_value_get_buffer (palette_v);
389 GST_DEBUG ("got palette data %p", palette);
390 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
391 gst_buffer_replace (&ffmpegdec->palette, palette);
398 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
399 GstVideoCodecState * state)
401 GstFFMpegVidDec *ffmpegdec;
402 GstFFMpegVidDecClass *oclass;
403 GstClockTime latency = GST_CLOCK_TIME_NONE;
404 gboolean ret = FALSE;
406 ffmpegdec = (GstFFMpegVidDec *) decoder;
407 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
409 if (ffmpegdec->last_caps != NULL &&
410 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
414 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
416 GST_OBJECT_LOCK (ffmpegdec);
417 /* stupid check for VC1 */
418 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
419 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
420 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
422 /* close old session */
423 if (ffmpegdec->opened) {
424 GST_OBJECT_UNLOCK (ffmpegdec);
425 gst_ffmpegviddec_finish (decoder);
426 GST_OBJECT_LOCK (ffmpegdec);
427 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
428 GST_OBJECT_UNLOCK (ffmpegdec);
431 ffmpegdec->pic_pix_fmt = 0;
432 ffmpegdec->pic_width = 0;
433 ffmpegdec->pic_height = 0;
434 ffmpegdec->pic_par_n = 0;
435 ffmpegdec->pic_par_d = 0;
436 ffmpegdec->pic_interlaced = 0;
437 ffmpegdec->pic_field_order = 0;
438 ffmpegdec->pic_field_order_changed = FALSE;
439 ffmpegdec->ctx_ticks = 0;
440 ffmpegdec->ctx_time_n = 0;
441 ffmpegdec->ctx_time_d = 0;
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 & 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];
614 width = GST_VIDEO_INFO_WIDTH (info);
615 height = GST_VIDEO_INFO_HEIGHT (info);
617 /* let ffmpeg find the alignment and padding */
618 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
621 if (ffmpegdec->context->flags & CODEC_FLAG_EMU_EDGE)
624 edge = avcodec_get_edge_width ();
626 /* increase the size for the padding */
630 align.padding_top = edge;
631 align.padding_left = edge;
632 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info) - edge;
633 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info) - edge;
635 /* add extra padding to match libav buffer allocation sizes */
636 align.padding_bottom++;
638 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
640 max_align = DEFAULT_STRIDE_ALIGN;
641 max_align |= params.align;
643 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
644 if (linesize_align[i] > 0)
645 max_align |= linesize_align[i] - 1;
648 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
649 align.stride_align[i] = max_align;
651 params.align = max_align;
653 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
655 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
656 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
657 GST_VIDEO_INFO_WIDTH (info),
658 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
659 align.padding_left, align.padding_right, align.padding_bottom,
660 align.stride_align[0], align.stride_align[1], align.stride_align[2],
661 align.stride_align[3]);
663 gst_buffer_pool_config_add_option (config,
664 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
665 gst_buffer_pool_config_set_video_alignment (config, &align);
669 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
672 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
674 GstVideoFormat format;
676 GstStructure *config;
679 if (ffmpegdec->internal_pool != NULL &&
680 ffmpegdec->pool_width == picture->width &&
681 ffmpegdec->pool_height == picture->height &&
682 ffmpegdec->pool_format == picture->format)
685 GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
686 picture->width, picture->height);
688 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
689 gst_video_info_set_format (&info, format, picture->width, picture->height);
691 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
692 ffmpegdec->stride[i] = -1;
694 if (ffmpegdec->internal_pool)
695 gst_object_unref (ffmpegdec->internal_pool);
697 ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
698 config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
700 caps = gst_video_info_to_caps (&info);
701 gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
702 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
703 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
705 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
706 ffmpegdec->internal_pool, &info, config);
707 /* generic video pool never fails */
708 gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
709 gst_caps_unref (caps);
711 gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
713 /* Remember pool size so we can detect changes */
714 ffmpegdec->pool_width = picture->width;
715 ffmpegdec->pool_height = picture->height;
716 ffmpegdec->pool_format = picture->format;
717 ffmpegdec->pool_info = info;
721 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
723 GstFFMpegVidDecClass *oclass;
725 if (!ffmpegdec->direct_rendering)
728 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
729 return ((oclass->in_plugin->capabilities & CODEC_CAP_DR1) == CODEC_CAP_DR1);
732 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
733 * into. We try to give it memory from our pool */
735 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
738 GstVideoCodecFrame *frame;
739 GstFFMpegVidDecVideoFrame *dframe;
740 GstFFMpegVidDec *ffmpegdec;
744 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
746 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
748 /* apply the last info we have seen to this picture, when we get the
749 * picture back from ffmpeg we can use this to correctly timestamp the output
751 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
752 (gint32) picture->reordered_opaque);
755 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
756 picture->reordered_opaque);
757 if (G_UNLIKELY (frame == NULL))
760 /* now it has a buffer allocated, so it is real and will also
762 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
763 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
765 if (G_UNLIKELY (frame->output_buffer != NULL))
766 goto duplicate_frame;
768 /* GstFFMpegVidDecVideoFrame receives the frame ref */
769 if (picture->opaque) {
770 dframe = picture->opaque;
771 dframe->frame = frame;
773 picture->opaque = dframe =
774 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
777 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
779 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
782 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
784 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
785 &frame->output_buffer, NULL);
786 if (ret != GST_FLOW_OK)
789 /* piggy-backed alloc'ed on the frame,
790 * and there was much rejoicing and we are grateful.
791 * Now take away buffer from frame, we will give it back later when decoded.
792 * This allows multiple request for a buffer per frame; unusual but possible. */
793 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
794 gst_buffer_replace (&frame->output_buffer, NULL);
797 if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
798 dframe->buffer, GST_MAP_READWRITE))
800 dframe->mapped = TRUE;
802 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
803 if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
804 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
805 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
807 if (ffmpegdec->stride[c] == -1)
808 ffmpegdec->stride[c] = picture->linesize[c];
810 /* libav does not allow stride changes, decide allocation should check
811 * before replacing the internal pool with a downstream pool.
812 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
813 * https://bugzilla.libav.org/show_bug.cgi?id=556
815 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
817 picture->data[c] = NULL;
818 picture->linesize[c] = 0;
820 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
824 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
826 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
833 int ret = avcodec_default_get_buffer2 (context, picture, flags);
835 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
837 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
838 ffmpegdec->stride[c] = picture->linesize[c];
840 /* Wrap our buffer around the default one to be able to have a callback
841 * when our data can be freed. Just putting our data into the first free
842 * buffer might not work if there are too many allocated already
844 if (picture->buf[0]) {
845 dframe->avbuffer = picture->buf[0];
847 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
848 dummy_free_buffer, dframe, 0);
851 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
858 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
859 ("Unable to allocate memory"),
860 ("The downstream pool failed to allocated buffer."));
865 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
866 ("Cannot access memory for read and write operation."),
867 ("The video memory allocated from downstream pool could not mapped for"
873 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
874 gst_video_codec_frame_unref (frame);
879 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
885 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
887 gint pic_field_order = 0;
889 if (picture->interlaced_frame) {
890 if (picture->repeat_pict)
891 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
892 if (picture->top_field_first)
893 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
896 return !(ffmpegdec->pic_width == picture->width
897 && ffmpegdec->pic_height == picture->height
898 && ffmpegdec->pic_pix_fmt == picture->format
899 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
900 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
901 && ffmpegdec->pic_interlaced == picture->interlaced_frame
902 && ffmpegdec->pic_field_order == pic_field_order);
906 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
908 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
909 && ffmpegdec->ctx_time_n == context->time_base.num
910 && ffmpegdec->ctx_time_d == context->time_base.den);
914 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
917 gint pic_field_order = 0;
919 if (picture->interlaced_frame) {
920 if (picture->repeat_pict)
921 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
922 if (picture->top_field_first)
923 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
926 if (!picture_changed (ffmpegdec, picture)
927 && !context_changed (ffmpegdec, context))
930 GST_DEBUG_OBJECT (ffmpegdec,
931 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
932 ffmpegdec->pic_width, ffmpegdec->pic_height,
933 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
934 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
935 ffmpegdec->pic_pix_fmt,
936 picture->width, picture->height,
937 picture->sample_aspect_ratio.num,
938 picture->sample_aspect_ratio.den,
939 context->time_base.num, context->time_base.den, picture->format);
941 ffmpegdec->pic_pix_fmt = picture->format;
942 ffmpegdec->pic_width = picture->width;
943 ffmpegdec->pic_height = picture->height;
944 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
945 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
947 /* Remember if we have interlaced content and the field order changed
948 * at least once. If that happens, we must be interlaced-mode=mixed
950 if (ffmpegdec->pic_field_order_changed ||
951 (ffmpegdec->pic_field_order != pic_field_order &&
952 ffmpegdec->pic_interlaced))
953 ffmpegdec->pic_field_order_changed = TRUE;
955 ffmpegdec->pic_field_order = pic_field_order;
956 ffmpegdec->pic_interlaced = picture->interlaced_frame;
958 if (!ffmpegdec->pic_interlaced)
959 ffmpegdec->pic_field_order_changed = FALSE;
961 ffmpegdec->ctx_ticks = context->ticks_per_frame;
962 ffmpegdec->ctx_time_n = context->time_base.num;
963 ffmpegdec->ctx_time_d = context->time_base.den;
969 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
970 GstVideoInfo * in_info, GstVideoInfo * out_info)
972 gboolean demuxer_par_set = FALSE;
973 gboolean decoder_par_set = FALSE;
974 gint demuxer_num = 1, demuxer_denom = 1;
975 gint decoder_num = 1, decoder_denom = 1;
977 if (in_info->par_n && in_info->par_d) {
978 demuxer_num = in_info->par_n;
979 demuxer_denom = in_info->par_d;
980 demuxer_par_set = TRUE;
981 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
985 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
986 decoder_num = ffmpegdec->pic_par_n;
987 decoder_denom = ffmpegdec->pic_par_d;
988 decoder_par_set = TRUE;
989 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
993 if (!demuxer_par_set && !decoder_par_set)
996 if (demuxer_par_set && !decoder_par_set)
997 goto use_demuxer_par;
999 if (decoder_par_set && !demuxer_par_set)
1000 goto use_decoder_par;
1002 /* Both the demuxer and the decoder provide a PAR. If one of
1003 * the two PARs is 1:1 and the other one is not, use the one
1004 * that is not 1:1. */
1005 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
1006 goto use_decoder_par;
1008 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
1009 goto use_demuxer_par;
1011 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
1012 goto use_demuxer_par;
1016 GST_DEBUG_OBJECT (ffmpegdec,
1017 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
1019 out_info->par_n = decoder_num;
1020 out_info->par_d = decoder_denom;
1025 GST_DEBUG_OBJECT (ffmpegdec,
1026 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
1028 out_info->par_n = demuxer_num;
1029 out_info->par_d = demuxer_denom;
1034 GST_DEBUG_OBJECT (ffmpegdec,
1035 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1036 out_info->par_n = 1;
1037 out_info->par_d = 1;
1043 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1044 AVCodecContext * context, AVFrame * picture)
1047 GstVideoInfo *in_info, *out_info;
1048 GstVideoCodecState *output_state;
1050 GstClockTime latency;
1053 if (!update_video_context (ffmpegdec, context, picture))
1056 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1057 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1058 goto unknown_format;
1061 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1062 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1063 if (ffmpegdec->output_state)
1064 gst_video_codec_state_unref (ffmpegdec->output_state);
1065 ffmpegdec->output_state = output_state;
1067 in_info = &ffmpegdec->input_state->info;
1068 out_info = &ffmpegdec->output_state->info;
1070 /* set the interlaced flag */
1071 in_s = gst_caps_get_structure (ffmpegdec->input_state->caps, 0);
1073 if (!gst_structure_has_field (in_s, "interlace-mode")) {
1074 if (ffmpegdec->pic_interlaced) {
1075 if (ffmpegdec->pic_field_order_changed ||
1076 (ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_RFF)) {
1077 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1079 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1080 if ((ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_TFF))
1081 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1082 GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
1084 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1085 GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST;
1088 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1092 if (!gst_structure_has_field (in_s, "chroma-site")) {
1093 switch (context->chroma_sample_location) {
1094 case AVCHROMA_LOC_LEFT:
1095 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1097 case AVCHROMA_LOC_CENTER:
1098 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1100 case AVCHROMA_LOC_TOPLEFT:
1101 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1103 case AVCHROMA_LOC_TOP:
1104 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1111 if (!gst_structure_has_field (in_s, "colorimetry")
1112 || in_info->colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN) {
1113 switch (context->color_primaries) {
1114 case AVCOL_PRI_BT709:
1115 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1117 case AVCOL_PRI_BT470M:
1118 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1120 case AVCOL_PRI_BT470BG:
1121 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1123 case AVCOL_PRI_SMPTE170M:
1124 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1126 case AVCOL_PRI_SMPTE240M:
1127 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1129 case AVCOL_PRI_FILM:
1130 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
1132 case AVCOL_PRI_BT2020:
1133 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1140 if (!gst_structure_has_field (in_s, "colorimetry")
1141 || in_info->colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN) {
1142 switch (context->color_trc) {
1143 case AVCOL_TRC_BT2020_10:
1144 case AVCOL_TRC_BT709:
1145 case AVCOL_TRC_SMPTE170M:
1146 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
1148 case AVCOL_TRC_GAMMA22:
1149 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
1151 case AVCOL_TRC_GAMMA28:
1152 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
1154 case AVCOL_TRC_SMPTE240M:
1155 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1157 case AVCOL_TRC_LINEAR:
1158 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
1161 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG100;
1163 case AVCOL_TRC_LOG_SQRT:
1164 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG316;
1166 case AVCOL_TRC_BT2020_12:
1167 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT2020_12;
1174 if (!gst_structure_has_field (in_s, "colorimetry")
1175 || in_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
1176 switch (context->colorspace) {
1178 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
1180 case AVCOL_SPC_BT709:
1181 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1184 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
1186 case AVCOL_SPC_BT470BG:
1187 case AVCOL_SPC_SMPTE170M:
1188 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1190 case AVCOL_SPC_SMPTE240M:
1191 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1193 case AVCOL_SPC_BT2020_NCL:
1194 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1201 if (!gst_structure_has_field (in_s, "colorimetry")
1202 || in_info->colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN) {
1203 if (context->color_range == AVCOL_RANGE_JPEG) {
1204 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
1206 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
1210 /* try to find a good framerate */
1211 if ((in_info->fps_d && in_info->fps_n) ||
1212 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1213 /* take framerate from input when it was specified (#313970) */
1214 fps_n = in_info->fps_n;
1215 fps_d = in_info->fps_d;
1217 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1218 fps_d = ffmpegdec->ctx_time_n;
1221 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1225 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1226 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1233 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1234 out_info->fps_n = fps_n;
1235 out_info->fps_d = fps_d;
1237 /* calculate and update par now */
1238 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1240 /* Copy stereo/multiview info from upstream if set */
1241 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
1242 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) =
1243 GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1244 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) =
1245 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1248 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1249 goto negotiate_failed;
1251 /* The decoder is configured, we now know the true latency */
1254 gst_util_uint64_scale_ceil (ffmpegdec->context->has_b_frames *
1255 GST_SECOND, fps_d, fps_n);
1256 gst_video_decoder_set_latency (GST_VIDEO_DECODER (ffmpegdec), latency,
1265 GST_ERROR_OBJECT (ffmpegdec,
1266 "decoder requires a video format unsupported by GStreamer");
1271 /* Reset so we try again next time even if force==FALSE */
1272 ffmpegdec->pic_pix_fmt = 0;
1273 ffmpegdec->pic_width = 0;
1274 ffmpegdec->pic_height = 0;
1275 ffmpegdec->pic_par_n = 0;
1276 ffmpegdec->pic_par_d = 0;
1277 ffmpegdec->pic_interlaced = 0;
1278 ffmpegdec->pic_field_order = 0;
1279 ffmpegdec->pic_field_order_changed = FALSE;
1280 ffmpegdec->ctx_ticks = 0;
1281 ffmpegdec->ctx_time_n = 0;
1282 ffmpegdec->ctx_time_d = 0;
1284 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1289 /* perform qos calculations before decoding the next frame.
1291 * Sets the skip_frame flag and if things are really bad, skips to the next
1296 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1297 GstVideoCodecFrame * frame, gboolean * mode_switch)
1299 GstClockTimeDiff diff;
1300 GstSegmentFlags skip_flags =
1301 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1303 *mode_switch = FALSE;
1308 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1309 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1310 *mode_switch = TRUE;
1311 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1312 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1313 *mode_switch = TRUE;
1316 if (*mode_switch == TRUE) {
1317 /* We've already switched mode, we can return straight away
1318 * without any further calculation */
1323 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1326 /* if we don't have timing info, then we don't do QoS */
1327 if (G_UNLIKELY (diff == G_MAXINT64)) {
1328 /* Ensure the skipping strategy is the default one */
1329 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
1333 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1335 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1336 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1337 *mode_switch = TRUE;
1338 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1341 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1342 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1343 *mode_switch = TRUE;
1344 GST_DEBUG_OBJECT (ffmpegdec,
1345 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1349 /* get an outbuf buffer with the current picture */
1350 static GstFlowReturn
1351 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1353 GstFlowReturn ret = GST_FLOW_OK;
1354 AVFrame pic, *outpic;
1355 GstVideoFrame vframe;
1359 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1361 if (!ffmpegdec->output_state)
1362 goto not_negotiated;
1365 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1367 if (G_UNLIKELY (ret != GST_FLOW_OK))
1370 /* original ffmpeg code does not handle odd sizes correctly.
1371 * This patched up version does */
1372 /* Fill avpicture */
1373 info = &ffmpegdec->output_state->info;
1374 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1375 GST_MAP_READ | GST_MAP_WRITE))
1378 memset (&pic, 0, sizeof (pic));
1379 pic.format = ffmpegdec->pic_pix_fmt;
1380 pic.width = GST_VIDEO_FRAME_WIDTH (&vframe);
1381 pic.height = GST_VIDEO_FRAME_HEIGHT (&vframe);
1382 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1383 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1384 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1385 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1386 GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1387 pic.linesize[c], pic.data[c]);
1390 pic.linesize[c] = 0;
1394 outpic = ffmpegdec->picture;
1396 if (av_frame_copy (&pic, outpic) != 0) {
1397 GST_ERROR_OBJECT (ffmpegdec, "Failed to copy output frame");
1398 ret = GST_FLOW_ERROR;
1401 gst_video_frame_unmap (&vframe);
1403 ffmpegdec->picture->reordered_opaque = -1;
1410 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
1411 ("Unable to allocate memory"),
1412 ("The downstream pool failed to allocated buffer."));
1417 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
1418 ("Cannot access memory for read and write operation."),
1419 ("The video memory allocated from downstream pool could not mapped for"
1420 "read and write."));
1425 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1426 return GST_FLOW_NOT_NEGOTIATED;
1431 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1433 memset (packet, 0, sizeof (AVPacket));
1434 packet->data = data;
1435 packet->size = size;
1438 /* gst_ffmpegviddec_[video|audio]_frame:
1440 * data: pointer to the data to decode
1441 * size: size of data in bytes
1442 * in_timestamp: incoming timestamp.
1443 * in_duration: incoming duration.
1444 * in_offset: incoming offset (frame number).
1447 * Returns: number of bytes used in decoding. The check for successful decode is
1448 * outbuf being non-NULL.
1451 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1452 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1453 GstFlowReturn * ret)
1456 gboolean mode_switch;
1457 GstVideoCodecFrame *out_frame;
1458 GstFFMpegVidDecVideoFrame *out_dframe;
1460 GstBufferPool *pool;
1464 /* in case we skip frames */
1465 ffmpegdec->picture->pict_type = -1;
1467 /* run QoS code, we don't stop decoding the frame when we are late because
1468 * else we might skip a reference frame */
1469 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1472 /* save reference to the timing info */
1473 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1474 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1476 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1477 frame->system_frame_number);
1480 /* now decode the frame */
1481 gst_avpacket_init (&packet, data, size);
1483 if (ffmpegdec->palette) {
1486 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1488 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1489 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1492 /* This might call into get_buffer() from another thread,
1493 * which would cause a deadlock. Release the lock here
1494 * and taking it again later seems safe
1495 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1497 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1498 len = avcodec_decode_video2 (ffmpegdec->context,
1499 ffmpegdec->picture, have_data, &packet);
1500 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1502 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1505 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1506 * no data because we told it to skip stuff. */
1507 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1510 /* no data, we're done */
1511 if (len < 0 || *have_data == 0)
1514 /* get the output picture timing info again */
1515 out_dframe = ffmpegdec->picture->opaque;
1516 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1518 /* also give back a buffer allocated by the frame, if any */
1519 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1520 gst_buffer_replace (&out_dframe->buffer, NULL);
1522 GST_DEBUG_OBJECT (ffmpegdec,
1523 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1524 out_frame->pts, out_frame->duration);
1525 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1526 (guint64) ffmpegdec->picture->pts);
1527 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1528 ffmpegdec->picture->coded_picture_number);
1529 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1530 ffmpegdec->picture->display_picture_number);
1531 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1532 ffmpegdec->picture->opaque);
1533 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1534 (guint64) ffmpegdec->picture->reordered_opaque);
1535 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1536 ffmpegdec->picture->repeat_pict);
1537 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1538 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1540 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1541 ffmpegdec->picture))
1542 goto negotiation_error;
1544 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1545 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1546 *ret = get_output_buffer (ffmpegdec, out_frame);
1547 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1548 GstBuffer *tmp = out_frame->output_buffer;
1549 out_frame->output_buffer = NULL;
1550 *ret = get_output_buffer (ffmpegdec, out_frame);
1551 gst_buffer_unref (tmp);
1553 #ifndef G_DISABLE_ASSERT
1555 GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1557 GstVideoInfo *info = &ffmpegdec->output_state->info;
1558 g_assert (vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1559 g_assert (vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1563 gst_object_unref (pool);
1565 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1568 /* Mark corrupted frames as corrupted */
1569 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1570 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1572 if (ffmpegdec->pic_interlaced) {
1573 /* set interlaced flags */
1574 if (ffmpegdec->picture->repeat_pict)
1575 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1576 if (ffmpegdec->picture->top_field_first)
1577 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1578 if (ffmpegdec->picture->interlaced_frame)
1579 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1580 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1584 /* so we decoded this frame, frames preceding it in decoding order
1585 * that still do not have a buffer allocated seem rather useless,
1586 * and can be discarded, due to e.g. misparsed bogus frame
1587 * or non-keyframe in skipped decoding, ...
1588 * In any case, not likely to be seen again, so discard those,
1589 * before they pile up and/or mess with timestamping */
1592 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1593 gboolean old = TRUE;
1595 ol = l = gst_video_decoder_get_frames (dec);
1597 GstVideoCodecFrame *tmp = l->data;
1602 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1603 GST_LOG_OBJECT (dec,
1604 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1605 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1606 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1607 /* drop extra ref and remove from frame list */
1608 gst_video_decoder_release_frame (dec, tmp);
1610 /* drop extra ref we got */
1611 gst_video_codec_frame_unref (tmp);
1618 av_frame_unref (ffmpegdec->picture);
1620 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1621 * libav might still have a reference to it!
1624 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1627 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1628 gst_flow_get_name (*ret), len);
1634 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1635 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1642 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1643 *ret = GST_FLOW_FLUSHING;
1646 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1647 *ret = GST_FLOW_NOT_NEGOTIATED;
1653 /* gst_ffmpegviddec_frame:
1655 * data: pointer to the data to decode
1656 * size: size of data in bytes
1657 * got_data: 0 if no data was decoded, != 0 otherwise.
1658 * in_time: timestamp of data
1659 * in_duration: duration of data
1660 * ret: GstFlowReturn to return in the chain function
1662 * Decode the given frame and pushes it downstream.
1664 * Returns: Number of bytes used in decoding, -1 on error/failure.
1668 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1669 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1670 GstFlowReturn * ret)
1672 GstFFMpegVidDecClass *oclass;
1675 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1678 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1681 ffmpegdec->context->frame_number++;
1683 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1686 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1690 GST_WARNING_OBJECT (ffmpegdec,
1691 "avdec_%s: decoding error (len: %d, have_data: %d)",
1692 oclass->in_plugin->name, len, *have_data);
1700 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1701 *ret = GST_FLOW_NOT_NEGOTIATED;
1706 static GstFlowReturn
1707 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1709 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1710 GstFFMpegVidDecClass *oclass;
1712 if (!ffmpegdec->opened)
1715 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1717 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1718 gint have_data, len;
1721 GST_LOG_OBJECT (ffmpegdec,
1722 "codec has delay capabilities, calling until ffmpeg has drained everything");
1725 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1726 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1732 static GstFlowReturn
1733 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1734 GstVideoCodecFrame * frame)
1736 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1737 guint8 *data, *bdata;
1738 gint size, len, have_data, bsize;
1740 GstFlowReturn ret = GST_FLOW_OK;
1741 gboolean do_padding;
1743 GST_LOG_OBJECT (ffmpegdec,
1744 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1745 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1746 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1747 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1749 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1750 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1751 ("Failed to map buffer for reading"));
1752 return GST_FLOW_ERROR;
1755 /* treat frame as void until a buffer is requested for it */
1756 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1757 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1762 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1763 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1765 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1766 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1767 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1768 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1769 ffmpegdec->padded_size);
1771 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1772 "Copy input to add padding");
1773 memcpy (ffmpegdec->padded, bdata, bsize);
1774 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1776 bdata = ffmpegdec->padded;
1783 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1785 /* parse, if at all possible */
1790 /* add temporary padding */
1791 GST_CAT_TRACE_OBJECT (CAT_PERFORMANCE, ffmpegdec,
1792 "Add temporary input padding");
1793 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1794 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1797 /* decode a frame of audio/video now */
1799 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1801 if (ret != GST_FLOW_OK) {
1802 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1803 gst_flow_get_name (ret));
1804 /* bad flow return, make sure we discard all data and exit */
1810 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1813 if (len == 0 && have_data == 0) {
1814 /* nothing was decoded, this could be because no data was available or
1815 * because we were skipping frames.
1816 * If we have no context we must exit and wait for more data, we keep the
1818 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1823 /* a decoding error happened, we must break and try again with next data. */
1824 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1829 /* prepare for the next round, for codecs with a context we did this
1830 * already when using the parser. */
1836 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1838 } while (bsize > 0);
1841 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1843 gst_buffer_unmap (frame->input_buffer, &minfo);
1844 gst_video_codec_frame_unref (frame);
1850 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1852 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1853 GstFFMpegVidDecClass *oclass;
1855 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1857 GST_OBJECT_LOCK (ffmpegdec);
1858 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1859 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1860 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1861 GST_OBJECT_UNLOCK (ffmpegdec);
1864 ffmpegdec->context->opaque = ffmpegdec;
1865 GST_OBJECT_UNLOCK (ffmpegdec);
1871 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1873 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1875 GST_OBJECT_LOCK (ffmpegdec);
1876 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1877 GST_OBJECT_UNLOCK (ffmpegdec);
1878 g_free (ffmpegdec->padded);
1879 ffmpegdec->padded = NULL;
1880 ffmpegdec->padded_size = 0;
1881 if (ffmpegdec->input_state)
1882 gst_video_codec_state_unref (ffmpegdec->input_state);
1883 ffmpegdec->input_state = NULL;
1884 if (ffmpegdec->output_state)
1885 gst_video_codec_state_unref (ffmpegdec->output_state);
1886 ffmpegdec->output_state = NULL;
1888 if (ffmpegdec->internal_pool)
1889 gst_object_unref (ffmpegdec->internal_pool);
1890 ffmpegdec->internal_pool = NULL;
1892 ffmpegdec->pic_pix_fmt = 0;
1893 ffmpegdec->pic_width = 0;
1894 ffmpegdec->pic_height = 0;
1895 ffmpegdec->pic_par_n = 0;
1896 ffmpegdec->pic_par_d = 0;
1897 ffmpegdec->pic_interlaced = 0;
1898 ffmpegdec->pic_field_order = 0;
1899 ffmpegdec->pic_field_order_changed = FALSE;
1900 ffmpegdec->ctx_ticks = 0;
1901 ffmpegdec->ctx_time_n = 0;
1902 ffmpegdec->ctx_time_d = 0;
1904 ffmpegdec->pool_width = 0;
1905 ffmpegdec->pool_height = 0;
1906 ffmpegdec->pool_format = 0;
1911 static GstFlowReturn
1912 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1914 gst_ffmpegviddec_drain (decoder);
1915 /* note that finish can and should clean up more drastically,
1916 * but drain is also invoked on e.g. packet loss in GAP handling */
1917 gst_ffmpegviddec_flush (decoder);
1923 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1925 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1927 if (ffmpegdec->opened) {
1928 GST_LOG_OBJECT (decoder, "flushing buffers");
1929 avcodec_flush_buffers (ffmpegdec->context);
1936 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1938 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1939 GstVideoCodecState *state;
1940 GstBufferPool *pool;
1941 guint size, min, max;
1942 GstStructure *config;
1943 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1944 GstAllocator *allocator = NULL;
1945 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1947 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1949 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1953 state = gst_video_decoder_get_output_state (decoder);
1955 if (gst_query_get_n_allocation_params (query) > 0) {
1956 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1957 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1959 gst_query_add_allocation_param (query, allocator, ¶ms);
1962 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1964 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1965 * need, otherwise we may stall */
1966 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1967 gst_object_unref (pool);
1968 pool = gst_video_buffer_pool_new ();
1973 /* if there is an allocator, also drop it, as it might be the reason we
1974 * have this limit. Default will be used */
1976 gst_object_unref (allocator);
1981 config = gst_buffer_pool_get_config (pool);
1982 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1983 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1986 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1989 gst_buffer_pool_config_add_option (config,
1990 GST_BUFFER_POOL_OPTION_VIDEO_META);
1993 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1995 /* If we have videometa, we never have to copy */
1996 if (have_videometa && have_pool && have_alignment &&
1997 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
1998 GstStructure *config_copy = gst_structure_copy (config);
2000 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
2003 /* FIXME validate and retry */
2004 if (gst_buffer_pool_set_config (pool, config_copy)) {
2008 gst_buffer_pool_set_active (pool, TRUE);
2009 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
2010 if (ret == GST_FLOW_OK) {
2011 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
2012 gboolean same_stride = TRUE;
2015 for (i = 0; i < vmeta->n_planes; i++) {
2016 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
2017 same_stride = FALSE;
2022 gst_buffer_unref (tmp);
2025 if (ffmpegdec->internal_pool)
2026 gst_object_unref (ffmpegdec->internal_pool);
2027 ffmpegdec->internal_pool = gst_object_ref (pool);
2028 ffmpegdec->pool_info = state->info;
2029 gst_structure_free (config);
2036 if (have_videometa && ffmpegdec->internal_pool
2037 && ffmpegdec->pool_width == state->info.width
2038 && ffmpegdec->pool_height == state->info.height) {
2040 gst_object_unref (pool);
2041 pool = gst_object_ref (ffmpegdec->internal_pool);
2042 gst_structure_free (config);
2047 if (!gst_buffer_pool_set_config (pool, config)) {
2048 gboolean working_pool = FALSE;
2049 config = gst_buffer_pool_get_config (pool);
2051 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
2053 working_pool = gst_buffer_pool_set_config (pool, config);
2055 gst_structure_free (config);
2058 if (!working_pool) {
2059 gst_object_unref (pool);
2060 pool = gst_video_buffer_pool_new ();
2061 config = gst_buffer_pool_get_config (pool);
2062 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2063 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
2064 gst_buffer_pool_set_config (pool, config);
2072 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2074 gst_object_unref (pool);
2076 gst_object_unref (allocator);
2077 gst_video_codec_state_unref (state);
2083 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
2086 GstAllocationParams params;
2088 gst_allocation_params_init (¶ms);
2089 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
2090 params.align = DEFAULT_STRIDE_ALIGN;
2091 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
2092 /* we would like to have some padding so that we don't have to
2093 * memcpy. We don't suggest an allocator. */
2094 gst_query_add_allocation_param (query, NULL, ¶ms);
2096 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
2101 gst_ffmpegviddec_set_property (GObject * object,
2102 guint prop_id, const GValue * value, GParamSpec * pspec)
2104 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2108 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
2110 case PROP_SKIPFRAME:
2111 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
2112 g_value_get_enum (value);
2114 case PROP_DIRECT_RENDERING:
2115 ffmpegdec->direct_rendering = g_value_get_boolean (value);
2118 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
2119 g_value_get_boolean (value);
2121 case PROP_MAX_THREADS:
2122 ffmpegdec->max_threads = g_value_get_int (value);
2124 case PROP_OUTPUT_CORRUPT:
2125 ffmpegdec->output_corrupt = g_value_get_boolean (value);
2128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2134 gst_ffmpegviddec_get_property (GObject * object,
2135 guint prop_id, GValue * value, GParamSpec * pspec)
2137 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2141 g_value_set_enum (value, ffmpegdec->context->lowres);
2143 case PROP_SKIPFRAME:
2144 g_value_set_enum (value, ffmpegdec->context->skip_frame);
2146 case PROP_DIRECT_RENDERING:
2147 g_value_set_boolean (value, ffmpegdec->direct_rendering);
2150 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
2152 case PROP_MAX_THREADS:
2153 g_value_set_int (value, ffmpegdec->max_threads);
2155 case PROP_OUTPUT_CORRUPT:
2156 g_value_set_boolean (value, ffmpegdec->output_corrupt);
2159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2165 gst_ffmpegviddec_register (GstPlugin * plugin)
2167 GTypeInfo typeinfo = {
2168 sizeof (GstFFMpegVidDecClass),
2169 (GBaseInitFunc) gst_ffmpegviddec_base_init,
2171 (GClassInitFunc) gst_ffmpegviddec_class_init,
2174 sizeof (GstFFMpegVidDec),
2176 (GInstanceInitFunc) gst_ffmpegviddec_init,
2182 in_plugin = av_codec_next (NULL);
2184 GST_LOG ("Registering decoders");
2190 /* only video decoders */
2191 if (!av_codec_is_decoder (in_plugin)
2192 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2195 /* no quasi codecs, please */
2196 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2197 in_plugin->id == AV_CODEC_ID_V210 ||
2198 in_plugin->id == AV_CODEC_ID_V210X ||
2199 in_plugin->id == AV_CODEC_ID_V308 ||
2200 in_plugin->id == AV_CODEC_ID_V408 ||
2201 in_plugin->id == AV_CODEC_ID_V410 ||
2202 in_plugin->id == AV_CODEC_ID_R210
2203 || in_plugin->id == AV_CODEC_ID_AYUV
2204 || in_plugin->id == AV_CODEC_ID_Y41P
2205 || in_plugin->id == AV_CODEC_ID_012V
2206 || in_plugin->id == AV_CODEC_ID_YUV4
2207 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
2208 AV_VERSION_INT (57,4,0)
2209 || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
2211 || in_plugin->id == AV_CODEC_ID_ZLIB) {
2215 /* No decoders depending on external libraries (we don't build them, but
2216 * people who build against an external ffmpeg might have them.
2217 * We have native gstreamer plugins for all of those libraries anyway. */
2218 if (!strncmp (in_plugin->name, "lib", 3)) {
2220 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2225 /* No vdpau plugins until we can figure out how to properly use them
2226 * outside of ffmpeg. */
2227 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2229 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2234 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2236 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2241 if (strstr (in_plugin->name, "vaapi")) {
2243 ("Ignoring VAAPI decoder %s. We can't handle this outside of ffmpeg",
2248 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2250 /* no codecs for which we're GUARANTEED to have better alternatives */
2251 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2252 /* MP1 : Use MP3 for decoding */
2253 /* MP2 : Use MP3 for decoding */
2254 /* Theora: Use libtheora based theoradec */
2255 if (!strcmp (in_plugin->name, "gif") ||
2256 !strcmp (in_plugin->name, "theora") ||
2257 !strcmp (in_plugin->name, "mpeg1video") ||
2258 strstr (in_plugin->name, "crystalhd") != NULL ||
2259 !strcmp (in_plugin->name, "ass") ||
2260 !strcmp (in_plugin->name, "srt") ||
2261 !strcmp (in_plugin->name, "pgssub") ||
2262 !strcmp (in_plugin->name, "dvdsub") ||
2263 !strcmp (in_plugin->name, "dvbsub")) {
2264 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2268 /* construct the type */
2269 if (!strcmp (in_plugin->name, "hevc")) {
2270 plugin_name = g_strdup ("h265");
2272 plugin_name = g_strdup ((gchar *) in_plugin->name);
2274 g_strdelimit (plugin_name, NULL, '_');
2275 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2276 g_free (plugin_name);
2278 type = g_type_from_name (type_name);
2281 /* create the gtype now */
2283 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2285 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2288 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2289 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2290 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2291 * VC1/WMV3 are not working and thus unpreferred for now. */
2292 switch (in_plugin->id) {
2293 case AV_CODEC_ID_MPEG1VIDEO:
2294 case AV_CODEC_ID_MPEG2VIDEO:
2295 case AV_CODEC_ID_MPEG4:
2296 case AV_CODEC_ID_MSMPEG4V3:
2297 case AV_CODEC_ID_H264:
2298 case AV_CODEC_ID_HEVC:
2299 case AV_CODEC_ID_RV10:
2300 case AV_CODEC_ID_RV20:
2301 case AV_CODEC_ID_RV30:
2302 case AV_CODEC_ID_RV40:
2303 rank = GST_RANK_PRIMARY;
2305 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2306 * They say libdv's quality is better though. leave as secondary.
2307 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2309 case AV_CODEC_ID_DVVIDEO:
2310 rank = GST_RANK_SECONDARY;
2313 rank = GST_RANK_MARGINAL;
2316 if (!gst_element_register (plugin, type_name, rank, type)) {
2317 g_warning ("Failed to register %s", type_name);
2325 in_plugin = av_codec_next (in_plugin);
2328 GST_LOG ("Finished Registering decoders");