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 GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE);
37 #define MAX_TS_MASK 0xff
39 #define DEFAULT_LOWRES 0
40 #define DEFAULT_SKIPFRAME 0
41 #define DEFAULT_DIRECT_RENDERING TRUE
42 #define DEFAULT_DEBUG_MV FALSE
43 #define DEFAULT_MAX_THREADS 0
44 #define DEFAULT_OUTPUT_CORRUPT TRUE
45 #define REQUIRED_POOL_MAX_BUFFERS 32
46 #define DEFAULT_STRIDE_ALIGN 31
47 #define DEFAULT_ALLOC_PARAM { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
54 PROP_DIRECT_RENDERING,
61 /* A number of function prototypes are given so we can refer to them later. */
62 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
63 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
64 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
65 static void gst_ffmpegviddec_finalize (GObject * object);
67 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
68 GstVideoCodecState * state);
69 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
70 GstVideoCodecFrame * frame);
71 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
72 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
73 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
74 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
76 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
79 static void gst_ffmpegviddec_set_property (GObject * object,
80 guint prop_id, const GValue * value, GParamSpec * pspec);
81 static void gst_ffmpegviddec_get_property (GObject * object,
82 guint prop_id, GValue * value, GParamSpec * pspec);
84 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
85 AVCodecContext * context, AVFrame * picture);
87 /* some sort of bufferpool handling, but different */
88 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
89 AVFrame * picture, int flags);
91 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
92 static GstFlowReturn gst_ffmpegviddec_drain (GstVideoDecoder * decoder);
94 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
96 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
97 AVCodecContext * context);
99 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
101 static GstElementClass *parent_class = NULL;
103 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
105 gst_ffmpegviddec_lowres_get_type (void)
107 static GType ffmpegdec_lowres_type = 0;
109 if (!ffmpegdec_lowres_type) {
110 static const GEnumValue ffmpegdec_lowres[] = {
112 {1, "1", "1/2-size"},
113 {2, "2", "1/4-size"},
117 ffmpegdec_lowres_type =
118 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
121 return ffmpegdec_lowres_type;
124 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
126 gst_ffmpegviddec_skipframe_get_type (void)
128 static GType ffmpegdec_skipframe_type = 0;
130 if (!ffmpegdec_skipframe_type) {
131 static const GEnumValue ffmpegdec_skipframe[] = {
132 {0, "0", "Skip nothing"},
133 {1, "1", "Skip B-frames"},
134 {2, "2", "Skip IDCT/Dequantization"},
135 {5, "5", "Skip everything"},
139 ffmpegdec_skipframe_type =
140 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
143 return ffmpegdec_skipframe_type;
147 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
149 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
150 GstPadTemplate *sinktempl, *srctempl;
151 GstCaps *sinkcaps, *srccaps;
153 gchar *longname, *description;
156 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
157 GST_FFDEC_PARAMS_QDATA);
158 g_assert (in_plugin != NULL);
160 /* construct the element details struct */
161 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
162 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
163 gst_element_class_set_metadata (element_class, longname,
164 "Codec/Decoder/Video", description,
165 "Wim Taymans <wim.taymans@gmail.com>, "
166 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
167 "Edward Hervey <bilboed@bilboed.com>");
169 g_free (description);
172 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
174 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
175 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
177 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
178 in_plugin->id, FALSE, in_plugin);
180 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
181 srccaps = gst_caps_from_string ("video/x-raw");
185 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
186 GST_PAD_ALWAYS, sinkcaps);
187 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
189 gst_element_class_add_pad_template (element_class, srctempl);
190 gst_element_class_add_pad_template (element_class, sinktempl);
192 gst_caps_unref (sinkcaps);
193 gst_caps_unref (srccaps);
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 & (AV_CODEC_CAP_FRAME_THREADS | AV_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;
254 GST_DEBUG_CATEGORY_GET (GST_CAT_PERFORMANCE, "GST_PERFORMANCE");
258 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
260 GstFFMpegVidDecClass *klass =
261 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
263 /* some ffmpeg data */
264 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
265 ffmpegdec->context->opaque = ffmpegdec;
266 ffmpegdec->picture = av_frame_alloc ();
267 ffmpegdec->opened = FALSE;
268 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
269 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
270 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
271 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
272 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
274 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
275 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
278 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
282 gst_ffmpegviddec_finalize (GObject * object)
284 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
286 av_frame_free (&ffmpegdec->picture);
288 if (ffmpegdec->context != NULL) {
289 gst_ffmpeg_avcodec_close (ffmpegdec->context);
290 av_free (ffmpegdec->context);
291 ffmpegdec->context = NULL;
294 G_OBJECT_CLASS (parent_class)->finalize (object);
298 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
301 g_return_if_fail (context != NULL);
304 context->flags |= flags;
306 context->flags &= ~flags;
311 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
313 GstFFMpegVidDecClass *oclass;
316 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
318 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
320 gst_caps_replace (&ffmpegdec->last_caps, NULL);
322 gst_ffmpeg_avcodec_close (ffmpegdec->context);
323 ffmpegdec->opened = FALSE;
325 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
326 ffmpegdec->stride[i] = -1;
328 gst_buffer_replace (&ffmpegdec->palette, NULL);
330 if (ffmpegdec->context->extradata) {
331 av_free (ffmpegdec->context->extradata);
332 ffmpegdec->context->extradata = NULL;
335 if (avcodec_get_context_defaults3 (ffmpegdec->context,
336 oclass->in_plugin) < 0) {
337 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
340 ffmpegdec->context->opaque = ffmpegdec;
347 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
349 GstFFMpegVidDecClass *oclass;
352 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
354 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
357 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
358 ffmpegdec->stride[i] = -1;
360 ffmpegdec->opened = TRUE;
362 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
363 oclass->in_plugin->name, oclass->in_plugin->id);
365 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
366 AV_CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
373 gst_ffmpegviddec_close (ffmpegdec, TRUE);
374 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
375 oclass->in_plugin->name);
381 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
382 GstVideoCodecState * state)
384 GstStructure *str = gst_caps_get_structure (state->caps, 0);
385 const GValue *palette_v;
388 /* do we have a palette? */
389 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
390 palette = gst_value_get_buffer (palette_v);
391 GST_DEBUG ("got palette data %p", palette);
392 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
393 gst_buffer_replace (&ffmpegdec->palette, palette);
400 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
401 GstVideoCodecState * state)
403 GstFFMpegVidDec *ffmpegdec;
404 GstFFMpegVidDecClass *oclass;
405 GstClockTime latency = GST_CLOCK_TIME_NONE;
406 gboolean ret = FALSE;
408 ffmpegdec = (GstFFMpegVidDec *) decoder;
409 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
411 if (ffmpegdec->last_caps != NULL &&
412 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
416 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
418 GST_OBJECT_LOCK (ffmpegdec);
419 /* stupid check for VC1 */
420 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
421 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
422 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
424 /* close old session */
425 if (ffmpegdec->opened) {
426 GST_OBJECT_UNLOCK (ffmpegdec);
427 gst_ffmpegviddec_finish (decoder);
428 GST_OBJECT_LOCK (ffmpegdec);
429 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
430 GST_OBJECT_UNLOCK (ffmpegdec);
433 ffmpegdec->pic_pix_fmt = 0;
434 ffmpegdec->pic_width = 0;
435 ffmpegdec->pic_height = 0;
436 ffmpegdec->pic_par_n = 0;
437 ffmpegdec->pic_par_d = 0;
438 ffmpegdec->pic_interlaced = 0;
439 ffmpegdec->pic_field_order = 0;
440 ffmpegdec->pic_field_order_changed = FALSE;
441 ffmpegdec->ctx_ticks = 0;
442 ffmpegdec->ctx_time_n = 0;
443 ffmpegdec->ctx_time_d = 0;
444 ffmpegdec->cur_multiview_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
445 ffmpegdec->cur_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
448 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
450 /* set buffer functions */
451 ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
452 ffmpegdec->context->draw_horiz_band = NULL;
454 /* reset coded_width/_height to prevent it being reused from last time when
455 * the codec is opened again, causing a mismatch and possible
456 * segfault/corruption. (Common scenario when renegotiating caps) */
457 ffmpegdec->context->coded_width = 0;
458 ffmpegdec->context->coded_height = 0;
460 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
461 ffmpegdec->context->height);
463 /* FIXME : Create a method that takes GstVideoCodecState instead */
464 /* get size and so */
465 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
466 oclass->in_plugin->type, state->caps, ffmpegdec->context);
468 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
469 ffmpegdec->context->height);
471 gst_ffmpegviddec_get_palette (ffmpegdec, state);
473 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
474 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
475 ffmpegdec->context->time_base.num = 1;
476 ffmpegdec->context->time_base.den = 25;
479 /* workaround encoder bugs */
480 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
481 ffmpegdec->context->err_recognition = 1;
484 ffmpegdec->context->lowres = ffmpegdec->lowres;
485 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
487 /* ffmpeg can draw motion vectors on top of the image (not every decoder
489 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
495 if (ffmpegdec->max_threads == 0) {
496 if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_AUTO_THREADS))
497 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
499 ffmpegdec->context->thread_count = 0;
501 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
503 query = gst_query_new_latency ();
505 /* Check if upstream is live. If it isn't we can enable frame based
506 * threading, which is adding latency */
507 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
508 gst_query_parse_latency (query, &is_live, NULL, NULL);
510 gst_query_unref (query);
513 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
515 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
518 /* open codec - we don't select an output pix_fmt yet,
519 * simply because we don't know! We only get it
520 * during playback... */
521 if (!gst_ffmpegviddec_open (ffmpegdec))
524 if (ffmpegdec->input_state)
525 gst_video_codec_state_unref (ffmpegdec->input_state);
526 ffmpegdec->input_state = gst_video_codec_state_ref (state);
528 if (ffmpegdec->input_state->info.fps_n) {
529 GstVideoInfo *info = &ffmpegdec->input_state->info;
530 latency = gst_util_uint64_scale_ceil (
531 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
538 GST_OBJECT_UNLOCK (ffmpegdec);
540 if (GST_CLOCK_TIME_IS_VALID (latency))
541 gst_video_decoder_set_latency (decoder, latency, latency);
548 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
555 GstFFMpegVidDec *ffmpegdec;
556 GstVideoCodecFrame *frame;
558 GstVideoFrame vframe;
560 AVBufferRef *avbuffer;
561 } GstFFMpegVidDecVideoFrame;
563 static GstFFMpegVidDecVideoFrame *
564 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
565 GstVideoCodecFrame * frame)
567 GstFFMpegVidDecVideoFrame *dframe;
569 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
570 dframe->ffmpegdec = ffmpegdec;
571 dframe->frame = frame;
573 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
579 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
580 GstFFMpegVidDecVideoFrame * frame)
582 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
585 gst_video_frame_unmap (&frame->vframe);
586 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
587 gst_buffer_replace (&frame->buffer, NULL);
588 if (frame->avbuffer) {
589 av_buffer_unref (&frame->avbuffer);
591 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
595 dummy_free_buffer (void *opaque, uint8_t * data)
597 GstFFMpegVidDecVideoFrame *frame = opaque;
599 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
602 /* This function prepares the pool configuration for direct rendering. To use
603 * this method, the codec should support direct rendering and the pool should
604 * support video meta and video alignment */
606 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
607 GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
609 GstAllocationParams params;
610 GstVideoAlignment align;
611 GstAllocator *allocator = NULL;
613 gint linesize_align[4];
617 width = GST_VIDEO_INFO_WIDTH (info);
618 height = GST_VIDEO_INFO_HEIGHT (info);
620 /* let ffmpeg find the alignment and padding */
621 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
624 align.padding_top = 0;
625 align.padding_left = 0;
626 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info);
627 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info);
629 /* add extra padding to match libav buffer allocation sizes */
630 align.padding_bottom++;
632 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
634 max_align = DEFAULT_STRIDE_ALIGN;
635 max_align |= params.align;
637 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
638 if (linesize_align[i] > 0)
639 max_align |= linesize_align[i] - 1;
642 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
643 align.stride_align[i] = max_align;
645 params.align = max_align;
647 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
649 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
650 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
651 GST_VIDEO_INFO_WIDTH (info),
652 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
653 align.padding_left, align.padding_right, align.padding_bottom,
654 align.stride_align[0], align.stride_align[1], align.stride_align[2],
655 align.stride_align[3]);
657 gst_buffer_pool_config_add_option (config,
658 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
659 gst_buffer_pool_config_set_video_alignment (config, &align);
663 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
666 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
668 GstVideoFormat format;
670 GstStructure *config;
673 if (ffmpegdec->internal_pool != NULL &&
674 ffmpegdec->pool_width == picture->width &&
675 ffmpegdec->pool_height == picture->height &&
676 ffmpegdec->pool_format == picture->format)
679 GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
680 picture->width, picture->height);
682 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
683 gst_video_info_set_format (&info, format, picture->width, picture->height);
685 /* If we have not yet been negotiated, a NONE format here would
686 * result in invalid initial dimension alignments, and potential
687 * out of bounds writes.
689 ffmpegdec->context->pix_fmt = picture->format;
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 & AV_CODEC_CAP_DR1) ==
733 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
734 * into. We try to give it memory from our pool */
736 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
739 GstVideoCodecFrame *frame;
740 GstFFMpegVidDecVideoFrame *dframe;
741 GstFFMpegVidDec *ffmpegdec;
745 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
747 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
749 /* apply the last info we have seen to this picture, when we get the
750 * picture back from ffmpeg we can use this to correctly timestamp the output
752 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
753 (gint32) picture->reordered_opaque);
756 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
757 picture->reordered_opaque);
758 if (G_UNLIKELY (frame == NULL))
761 /* now it has a buffer allocated, so it is real and will also
763 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
764 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
766 if (G_UNLIKELY (frame->output_buffer != NULL))
767 goto duplicate_frame;
769 /* GstFFMpegVidDecVideoFrame receives the frame ref */
770 if (picture->opaque) {
771 dframe = picture->opaque;
772 dframe->frame = frame;
774 picture->opaque = dframe =
775 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
778 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
780 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
783 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
785 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
786 &frame->output_buffer, NULL);
787 if (ret != GST_FLOW_OK)
790 /* piggy-backed alloc'ed on the frame,
791 * and there was much rejoicing and we are grateful.
792 * Now take away buffer from frame, we will give it back later when decoded.
793 * This allows multiple request for a buffer per frame; unusual but possible. */
794 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
795 gst_buffer_replace (&frame->output_buffer, NULL);
798 if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
799 dframe->buffer, GST_MAP_READWRITE))
801 dframe->mapped = TRUE;
803 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
804 if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
805 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
806 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
808 if (ffmpegdec->stride[c] == -1)
809 ffmpegdec->stride[c] = picture->linesize[c];
811 /* libav does not allow stride changes, decide allocation should check
812 * before replacing the internal pool with a downstream pool.
813 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
814 * https://bugzilla.libav.org/show_bug.cgi?id=556
816 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
818 picture->data[c] = NULL;
819 picture->linesize[c] = 0;
821 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
825 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
827 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
834 int ret = avcodec_default_get_buffer2 (context, picture, flags);
836 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
838 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
839 ffmpegdec->stride[c] = picture->linesize[c];
841 /* Wrap our buffer around the default one to be able to have a callback
842 * when our data can be freed. Just putting our data into the first free
843 * buffer might not work if there are too many allocated already
845 if (picture->buf[0]) {
846 dframe->avbuffer = picture->buf[0];
848 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
849 dummy_free_buffer, dframe, 0);
852 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
859 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
860 ("Unable to allocate memory"),
861 ("The downstream pool failed to allocated buffer."));
866 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
867 ("Cannot access memory for read and write operation."),
868 ("The video memory allocated from downstream pool could not mapped for"
874 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
875 gst_video_codec_frame_unref (frame);
880 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
886 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
888 gint pic_field_order = 0;
890 if (picture->interlaced_frame) {
891 if (picture->repeat_pict)
892 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
893 if (picture->top_field_first)
894 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
897 return !(ffmpegdec->pic_width == picture->width
898 && ffmpegdec->pic_height == picture->height
899 && ffmpegdec->pic_pix_fmt == picture->format
900 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
901 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
902 && ffmpegdec->pic_interlaced == picture->interlaced_frame
903 && ffmpegdec->pic_field_order == pic_field_order
904 && ffmpegdec->cur_multiview_mode == ffmpegdec->picture_multiview_mode
905 && ffmpegdec->cur_multiview_flags == ffmpegdec->picture_multiview_flags);
909 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
911 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
912 && ffmpegdec->ctx_time_n == context->time_base.num
913 && ffmpegdec->ctx_time_d == context->time_base.den);
917 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
920 gint pic_field_order = 0;
922 if (picture->interlaced_frame) {
923 if (picture->repeat_pict)
924 pic_field_order |= GST_VIDEO_BUFFER_FLAG_RFF;
925 if (picture->top_field_first)
926 pic_field_order |= GST_VIDEO_BUFFER_FLAG_TFF;
929 if (!picture_changed (ffmpegdec, picture)
930 && !context_changed (ffmpegdec, context))
933 GST_DEBUG_OBJECT (ffmpegdec,
934 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
935 ffmpegdec->pic_width, ffmpegdec->pic_height,
936 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
937 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
938 ffmpegdec->pic_pix_fmt,
939 picture->width, picture->height,
940 picture->sample_aspect_ratio.num,
941 picture->sample_aspect_ratio.den,
942 context->time_base.num, context->time_base.den, picture->format);
944 ffmpegdec->pic_pix_fmt = picture->format;
945 ffmpegdec->pic_width = picture->width;
946 ffmpegdec->pic_height = picture->height;
947 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
948 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
949 ffmpegdec->cur_multiview_mode = ffmpegdec->picture_multiview_mode;
950 ffmpegdec->cur_multiview_flags = ffmpegdec->picture_multiview_flags;
952 /* Remember if we have interlaced content and the field order changed
953 * at least once. If that happens, we must be interlace-mode=mixed
955 if (ffmpegdec->pic_field_order_changed ||
956 (ffmpegdec->pic_field_order != pic_field_order &&
957 ffmpegdec->pic_interlaced))
958 ffmpegdec->pic_field_order_changed = TRUE;
960 ffmpegdec->pic_field_order = pic_field_order;
961 ffmpegdec->pic_interlaced = picture->interlaced_frame;
963 if (!ffmpegdec->pic_interlaced)
964 ffmpegdec->pic_field_order_changed = FALSE;
966 ffmpegdec->ctx_ticks = context->ticks_per_frame;
967 ffmpegdec->ctx_time_n = context->time_base.num;
968 ffmpegdec->ctx_time_d = context->time_base.den;
974 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
975 GstVideoInfo * in_info, GstVideoInfo * out_info)
977 gboolean demuxer_par_set = FALSE;
978 gboolean decoder_par_set = FALSE;
979 gint demuxer_num = 1, demuxer_denom = 1;
980 gint decoder_num = 1, decoder_denom = 1;
982 if (in_info->par_n && in_info->par_d) {
983 demuxer_num = in_info->par_n;
984 demuxer_denom = in_info->par_d;
985 demuxer_par_set = TRUE;
986 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
990 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
991 decoder_num = ffmpegdec->pic_par_n;
992 decoder_denom = ffmpegdec->pic_par_d;
993 decoder_par_set = TRUE;
994 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
998 if (!demuxer_par_set && !decoder_par_set)
1001 if (demuxer_par_set && !decoder_par_set)
1002 goto use_demuxer_par;
1004 if (decoder_par_set && !demuxer_par_set)
1005 goto use_decoder_par;
1007 /* Both the demuxer and the decoder provide a PAR. If one of
1008 * the two PARs is 1:1 and the other one is not, use the one
1009 * that is not 1:1. */
1010 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
1011 goto use_decoder_par;
1013 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
1014 goto use_demuxer_par;
1016 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
1017 goto use_demuxer_par;
1021 GST_DEBUG_OBJECT (ffmpegdec,
1022 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
1024 out_info->par_n = decoder_num;
1025 out_info->par_d = decoder_denom;
1030 GST_DEBUG_OBJECT (ffmpegdec,
1031 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
1033 out_info->par_n = demuxer_num;
1034 out_info->par_d = demuxer_denom;
1039 GST_DEBUG_OBJECT (ffmpegdec,
1040 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1041 out_info->par_n = 1;
1042 out_info->par_d = 1;
1047 static GstVideoMultiviewMode
1048 stereo_av_to_gst (enum AVStereo3DType type)
1051 case AV_STEREO3D_SIDEBYSIDE:
1052 return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
1053 case AV_STEREO3D_TOPBOTTOM:
1054 return GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
1055 case AV_STEREO3D_FRAMESEQUENCE:
1056 return GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
1057 case AV_STEREO3D_CHECKERBOARD:
1058 return GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
1059 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
1060 return GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX;
1061 case AV_STEREO3D_LINES:
1062 return GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED;
1063 case AV_STEREO3D_COLUMNS:
1064 return GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED;
1069 return GST_VIDEO_MULTIVIEW_MODE_NONE;
1073 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1074 AVCodecContext * context, AVFrame * picture)
1077 GstVideoInfo *in_info, *out_info;
1078 GstVideoCodecState *output_state;
1080 GstClockTime latency;
1083 if (!update_video_context (ffmpegdec, context, picture))
1086 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1087 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1088 goto unknown_format;
1091 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1092 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1093 if (ffmpegdec->output_state)
1094 gst_video_codec_state_unref (ffmpegdec->output_state);
1095 ffmpegdec->output_state = output_state;
1097 in_info = &ffmpegdec->input_state->info;
1098 out_info = &ffmpegdec->output_state->info;
1100 /* set the interlaced flag */
1101 in_s = gst_caps_get_structure (ffmpegdec->input_state->caps, 0);
1103 if (!gst_structure_has_field (in_s, "interlace-mode")) {
1104 if (ffmpegdec->pic_interlaced) {
1105 if (ffmpegdec->pic_field_order_changed ||
1106 (ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_RFF)) {
1107 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1109 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
1110 if ((ffmpegdec->pic_field_order & GST_VIDEO_BUFFER_FLAG_TFF))
1111 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1112 GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
1114 GST_VIDEO_INFO_FIELD_ORDER (out_info) =
1115 GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST;
1118 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1122 if (!gst_structure_has_field (in_s, "chroma-site")) {
1123 switch (context->chroma_sample_location) {
1124 case AVCHROMA_LOC_LEFT:
1125 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1127 case AVCHROMA_LOC_CENTER:
1128 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1130 case AVCHROMA_LOC_TOPLEFT:
1131 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1133 case AVCHROMA_LOC_TOP:
1134 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1141 if (!gst_structure_has_field (in_s, "colorimetry")
1142 || in_info->colorimetry.primaries == GST_VIDEO_COLOR_PRIMARIES_UNKNOWN) {
1143 switch (context->color_primaries) {
1144 case AVCOL_PRI_BT709:
1145 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
1147 case AVCOL_PRI_BT470M:
1148 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
1150 case AVCOL_PRI_BT470BG:
1151 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
1153 case AVCOL_PRI_SMPTE170M:
1154 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
1156 case AVCOL_PRI_SMPTE240M:
1157 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
1159 case AVCOL_PRI_FILM:
1160 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
1162 case AVCOL_PRI_BT2020:
1163 out_info->colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
1170 if (!gst_structure_has_field (in_s, "colorimetry")
1171 || in_info->colorimetry.transfer == GST_VIDEO_TRANSFER_UNKNOWN) {
1172 switch (context->color_trc) {
1173 case AVCOL_TRC_BT2020_10:
1174 case AVCOL_TRC_BT709:
1175 case AVCOL_TRC_SMPTE170M:
1176 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
1178 case AVCOL_TRC_GAMMA22:
1179 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
1181 case AVCOL_TRC_GAMMA28:
1182 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
1184 case AVCOL_TRC_SMPTE240M:
1185 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
1187 case AVCOL_TRC_LINEAR:
1188 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
1191 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG100;
1193 case AVCOL_TRC_LOG_SQRT:
1194 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_LOG316;
1196 case AVCOL_TRC_BT2020_12:
1197 out_info->colorimetry.transfer = GST_VIDEO_TRANSFER_BT2020_12;
1204 if (!gst_structure_has_field (in_s, "colorimetry")
1205 || in_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
1206 switch (context->colorspace) {
1208 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
1210 case AVCOL_SPC_BT709:
1211 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
1214 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
1216 case AVCOL_SPC_BT470BG:
1217 case AVCOL_SPC_SMPTE170M:
1218 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
1220 case AVCOL_SPC_SMPTE240M:
1221 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
1223 case AVCOL_SPC_BT2020_NCL:
1224 out_info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
1231 if (!gst_structure_has_field (in_s, "colorimetry")
1232 || in_info->colorimetry.range == GST_VIDEO_COLOR_RANGE_UNKNOWN) {
1233 if (context->color_range == AVCOL_RANGE_JPEG) {
1234 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
1236 out_info->colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
1240 /* try to find a good framerate */
1241 if ((in_info->fps_d && in_info->fps_n) ||
1242 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1243 /* take framerate from input when it was specified (#313970) */
1244 fps_n = in_info->fps_n;
1245 fps_d = in_info->fps_d;
1247 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1248 fps_d = ffmpegdec->ctx_time_n;
1251 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1255 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1256 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1263 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1264 out_info->fps_n = fps_n;
1265 out_info->fps_d = fps_d;
1267 /* calculate and update par now */
1268 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1270 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) = ffmpegdec->cur_multiview_mode;
1271 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) = ffmpegdec->cur_multiview_flags;
1273 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1274 goto negotiate_failed;
1276 /* The decoder is configured, we now know the true latency */
1279 gst_util_uint64_scale_ceil (ffmpegdec->context->has_b_frames *
1280 GST_SECOND, fps_d, fps_n);
1281 gst_video_decoder_set_latency (GST_VIDEO_DECODER (ffmpegdec), latency,
1290 GST_ERROR_OBJECT (ffmpegdec,
1291 "decoder requires a video format unsupported by GStreamer");
1296 /* Reset so we try again next time even if force==FALSE */
1297 ffmpegdec->pic_pix_fmt = 0;
1298 ffmpegdec->pic_width = 0;
1299 ffmpegdec->pic_height = 0;
1300 ffmpegdec->pic_par_n = 0;
1301 ffmpegdec->pic_par_d = 0;
1302 ffmpegdec->pic_interlaced = 0;
1303 ffmpegdec->pic_field_order = 0;
1304 ffmpegdec->pic_field_order_changed = FALSE;
1305 ffmpegdec->ctx_ticks = 0;
1306 ffmpegdec->ctx_time_n = 0;
1307 ffmpegdec->ctx_time_d = 0;
1309 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1314 /* perform qos calculations before decoding the next frame.
1316 * Sets the skip_frame flag and if things are really bad, skips to the next
1321 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1322 GstVideoCodecFrame * frame, gboolean * mode_switch)
1324 GstClockTimeDiff diff;
1325 GstSegmentFlags skip_flags =
1326 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1328 *mode_switch = FALSE;
1333 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1334 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1335 *mode_switch = TRUE;
1336 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1337 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1338 *mode_switch = TRUE;
1341 if (*mode_switch == TRUE) {
1342 /* We've already switched mode, we can return straight away
1343 * without any further calculation */
1348 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1351 /* if we don't have timing info, then we don't do QoS */
1352 if (G_UNLIKELY (diff == G_MAXINT64)) {
1353 /* Ensure the skipping strategy is the default one */
1354 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
1358 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1360 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1361 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1362 *mode_switch = TRUE;
1363 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1366 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1367 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1368 *mode_switch = TRUE;
1369 GST_DEBUG_OBJECT (ffmpegdec,
1370 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1374 /* get an outbuf buffer with the current picture */
1375 static GstFlowReturn
1376 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1378 GstFlowReturn ret = GST_FLOW_OK;
1379 AVFrame pic, *outpic;
1380 GstVideoFrame vframe;
1384 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1386 if (!ffmpegdec->output_state)
1387 goto not_negotiated;
1390 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1392 if (G_UNLIKELY (ret != GST_FLOW_OK))
1395 /* original ffmpeg code does not handle odd sizes correctly.
1396 * This patched up version does */
1397 /* Fill avpicture */
1398 info = &ffmpegdec->output_state->info;
1399 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1400 GST_MAP_READ | GST_MAP_WRITE))
1403 memset (&pic, 0, sizeof (pic));
1404 pic.format = ffmpegdec->pic_pix_fmt;
1405 pic.width = GST_VIDEO_FRAME_WIDTH (&vframe);
1406 pic.height = GST_VIDEO_FRAME_HEIGHT (&vframe);
1407 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1408 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1409 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1410 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1411 GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1412 pic.linesize[c], pic.data[c]);
1415 pic.linesize[c] = 0;
1419 outpic = ffmpegdec->picture;
1421 if (av_frame_copy (&pic, outpic) != 0) {
1422 GST_ERROR_OBJECT (ffmpegdec, "Failed to copy output frame");
1423 ret = GST_FLOW_ERROR;
1426 gst_video_frame_unmap (&vframe);
1428 ffmpegdec->picture->reordered_opaque = -1;
1435 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, FAILED,
1436 ("Unable to allocate memory"),
1437 ("The downstream pool failed to allocated buffer."));
1442 GST_ELEMENT_ERROR (ffmpegdec, RESOURCE, OPEN_READ_WRITE,
1443 ("Cannot access memory for read and write operation."),
1444 ("The video memory allocated from downstream pool could not mapped for"
1445 "read and write."));
1450 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1451 return GST_FLOW_NOT_NEGOTIATED;
1456 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1458 memset (packet, 0, sizeof (AVPacket));
1459 packet->data = data;
1460 packet->size = size;
1464 * Returns: whether a frame was decoded
1467 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1468 GstVideoCodecFrame * frame, GstFlowReturn * ret)
1471 gboolean got_frame = FALSE;
1472 gboolean mode_switch;
1473 GstVideoCodecFrame *out_frame;
1474 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);
1486 res = avcodec_receive_frame (ffmpegdec->context, ffmpegdec->picture);
1488 /* No frames available at this time */
1489 if (res == AVERROR (EAGAIN))
1491 else if (res == AVERROR_EOF) {
1492 *ret = GST_FLOW_EOS;
1493 GST_DEBUG_OBJECT (ffmpegdec, "Context was entirely flushed");
1495 } else if (res < 0) {
1497 GST_WARNING_OBJECT (ffmpegdec, "Legitimate decoding error");
1503 /* get the output picture timing info again */
1504 out_dframe = ffmpegdec->picture->opaque;
1505 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1507 /* also give back a buffer allocated by the frame, if any */
1508 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1509 gst_buffer_replace (&out_dframe->buffer, NULL);
1511 /* Extract auxilliary info not stored in the main AVframe */
1513 GstVideoInfo *in_info = &ffmpegdec->input_state->info;
1514 /* Take multiview mode from upstream if present */
1515 ffmpegdec->picture_multiview_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1516 ffmpegdec->picture_multiview_flags =
1517 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1519 /* Otherwise, see if there's info in the frame */
1520 if (ffmpegdec->picture_multiview_mode == GST_VIDEO_MULTIVIEW_MODE_NONE) {
1521 AVFrameSideData *side_data =
1522 av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_STEREO3D);
1524 AVStereo3D *stereo = (AVStereo3D *) side_data->data;
1525 ffmpegdec->picture_multiview_mode = stereo_av_to_gst (stereo->type);
1526 if (stereo->flags & AV_STEREO3D_FLAG_INVERT) {
1527 ffmpegdec->picture_multiview_flags =
1528 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
1530 ffmpegdec->picture_multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
1536 GST_DEBUG_OBJECT (ffmpegdec,
1537 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1538 out_frame->pts, out_frame->duration);
1539 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1540 (guint64) ffmpegdec->picture->pts);
1541 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1542 ffmpegdec->picture->coded_picture_number);
1543 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1544 ffmpegdec->picture->display_picture_number);
1545 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1546 ffmpegdec->picture->opaque);
1547 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1548 (guint64) ffmpegdec->picture->reordered_opaque);
1549 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1550 ffmpegdec->picture->repeat_pict);
1551 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1552 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1554 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1555 ffmpegdec->picture))
1556 goto negotiation_error;
1558 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1559 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1560 *ret = get_output_buffer (ffmpegdec, out_frame);
1561 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1562 GstBuffer *tmp = out_frame->output_buffer;
1563 out_frame->output_buffer = NULL;
1564 *ret = get_output_buffer (ffmpegdec, out_frame);
1565 gst_buffer_unref (tmp);
1567 #ifndef G_DISABLE_ASSERT
1569 GstVideoMeta *vmeta = gst_buffer_get_video_meta (out_frame->output_buffer);
1571 GstVideoInfo *info = &ffmpegdec->output_state->info;
1572 g_assert ((gint) vmeta->width == GST_VIDEO_INFO_WIDTH (info));
1573 g_assert ((gint) vmeta->height == GST_VIDEO_INFO_HEIGHT (info));
1577 gst_object_unref (pool);
1579 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1582 /* Mark corrupted frames as corrupted */
1583 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1584 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1586 if (ffmpegdec->pic_interlaced) {
1587 /* set interlaced flags */
1588 if (ffmpegdec->picture->repeat_pict)
1589 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1590 if (ffmpegdec->picture->top_field_first)
1591 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1592 if (ffmpegdec->picture->interlaced_frame)
1593 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1594 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1598 AVFrameSideData *side_data =
1599 av_frame_get_side_data (ffmpegdec->picture, AV_FRAME_DATA_A53_CC);
1601 GstVideoCaptionMeta *cc_meta = NULL;
1602 gpointer iter = NULL;
1603 gboolean found_708_raw_meta = FALSE;
1605 GST_LOG_OBJECT (ffmpegdec,
1606 "Found CC side data of type AV_FRAME_DATA_A53_CC, size %d",
1608 GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
1610 while ((cc_meta = (GstVideoCaptionMeta *)
1611 gst_buffer_iterate_meta_filtered (out_frame->input_buffer, &iter,
1612 GST_VIDEO_CAPTION_META_API_TYPE))) {
1613 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
1615 found_708_raw_meta = TRUE;
1619 /* do not add CEA 708 caption meta if it already exists */
1620 if (!found_708_raw_meta) {
1621 out_frame->output_buffer =
1622 gst_buffer_make_writable (out_frame->output_buffer);
1623 gst_buffer_add_video_caption_meta (out_frame->output_buffer,
1624 GST_VIDEO_CAPTION_TYPE_CEA708_RAW, side_data->data,
1627 GST_LOG_OBJECT (ffmpegdec,
1628 "CEA 708 caption meta already exists: will not add new caption meta");
1634 /* so we decoded this frame, frames preceding it in decoding order
1635 * that still do not have a buffer allocated seem rather useless,
1636 * and can be discarded, due to e.g. misparsed bogus frame
1637 * or non-keyframe in skipped decoding, ...
1638 * In any case, not likely to be seen again, so discard those,
1639 * before they pile up and/or mess with timestamping */
1642 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1643 gboolean old = TRUE;
1645 ol = l = gst_video_decoder_get_frames (dec);
1647 GstVideoCodecFrame *tmp = l->data;
1652 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1653 GST_LOG_OBJECT (dec,
1654 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1655 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1656 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1657 /* drop extra ref and remove from frame list */
1658 gst_video_decoder_release_frame (dec, tmp);
1660 /* drop extra ref we got */
1661 gst_video_codec_frame_unref (tmp);
1668 av_frame_unref (ffmpegdec->picture);
1670 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1671 * libav might still have a reference to it!
1674 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1677 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, got frame: %d",
1678 gst_flow_get_name (*ret), got_frame);
1684 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1685 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1691 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1692 *ret = GST_FLOW_FLUSHING;
1695 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1696 *ret = GST_FLOW_NOT_NEGOTIATED;
1702 /* Returns: Whether a frame was decoded */
1704 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame,
1705 GstFlowReturn * ret)
1707 gboolean got_frame = FALSE;
1709 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1713 ffmpegdec->context->frame_number++;
1715 got_frame = gst_ffmpegviddec_video_frame (ffmpegdec, frame, ret);
1722 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1723 *ret = GST_FLOW_NOT_NEGOTIATED;
1728 static GstFlowReturn
1729 gst_ffmpegviddec_drain (GstVideoDecoder * decoder)
1731 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1732 GstFFMpegVidDecClass *oclass;
1734 if (!ffmpegdec->opened)
1737 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1739 if (oclass->in_plugin->capabilities & AV_CODEC_CAP_DELAY) {
1741 gboolean got_frame = FALSE;
1743 GST_LOG_OBJECT (ffmpegdec,
1744 "codec has delay capabilities, calling until ffmpeg has drained everything");
1746 if (avcodec_send_packet (ffmpegdec->context, NULL))
1747 goto send_packet_failed;
1750 got_frame = gst_ffmpegviddec_frame (ffmpegdec, NULL, &ret);
1751 } while (got_frame && ret == GST_FLOW_OK);
1752 avcodec_flush_buffers (ffmpegdec->context);
1759 GST_WARNING_OBJECT (ffmpegdec, "send packet failed, could not drain decoder");
1763 static GstFlowReturn
1764 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1765 GstVideoCodecFrame * frame)
1767 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1772 GstFlowReturn ret = GST_FLOW_OK;
1775 GST_LOG_OBJECT (ffmpegdec,
1776 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1777 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1778 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1779 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1781 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1782 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1783 ("Failed to map buffer for reading"));
1784 return GST_FLOW_ERROR;
1787 /* treat frame as void until a buffer is requested for it */
1788 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1789 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1794 if (size > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1795 || (minfo.maxsize - minfo.size) < AV_INPUT_BUFFER_PADDING_SIZE)) {
1797 if (ffmpegdec->padded_size < size + AV_INPUT_BUFFER_PADDING_SIZE) {
1798 ffmpegdec->padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
1799 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1800 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1801 ffmpegdec->padded_size);
1803 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1804 "Copy input to add padding");
1805 memcpy (ffmpegdec->padded, data, size);
1806 memset (ffmpegdec->padded + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1808 data = ffmpegdec->padded;
1811 /* now decode the frame */
1812 gst_avpacket_init (&packet, data, size);
1814 if (ffmpegdec->palette) {
1817 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1819 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1820 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1826 /* save reference to the timing info */
1827 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1828 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1830 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1831 frame->system_frame_number);
1833 /* This might call into get_buffer() from another thread,
1834 * which would cause a deadlock. Release the lock here
1835 * and taking it again later seems safe
1836 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1838 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1839 if (avcodec_send_packet (ffmpegdec->context, &packet) < 0) {
1840 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1841 goto send_packet_failed;
1843 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1846 /* decode a frame of audio/video now */
1847 got_frame = gst_ffmpegviddec_frame (ffmpegdec, frame, &ret);
1849 if (ret != GST_FLOW_OK) {
1850 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1851 gst_flow_get_name (ret));
1854 } while (got_frame);
1857 gst_buffer_unmap (frame->input_buffer, &minfo);
1858 gst_video_codec_frame_unref (frame);
1864 GST_WARNING_OBJECT (ffmpegdec, "Failed to send data for decoding");
1870 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1872 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1873 GstFFMpegVidDecClass *oclass;
1875 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1877 GST_OBJECT_LOCK (ffmpegdec);
1878 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1879 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1880 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1881 GST_OBJECT_UNLOCK (ffmpegdec);
1884 ffmpegdec->context->opaque = ffmpegdec;
1885 GST_OBJECT_UNLOCK (ffmpegdec);
1891 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1893 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1895 GST_OBJECT_LOCK (ffmpegdec);
1896 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1897 GST_OBJECT_UNLOCK (ffmpegdec);
1898 g_free (ffmpegdec->padded);
1899 ffmpegdec->padded = NULL;
1900 ffmpegdec->padded_size = 0;
1901 if (ffmpegdec->input_state)
1902 gst_video_codec_state_unref (ffmpegdec->input_state);
1903 ffmpegdec->input_state = NULL;
1904 if (ffmpegdec->output_state)
1905 gst_video_codec_state_unref (ffmpegdec->output_state);
1906 ffmpegdec->output_state = NULL;
1908 if (ffmpegdec->internal_pool)
1909 gst_object_unref (ffmpegdec->internal_pool);
1910 ffmpegdec->internal_pool = NULL;
1912 ffmpegdec->pic_pix_fmt = 0;
1913 ffmpegdec->pic_width = 0;
1914 ffmpegdec->pic_height = 0;
1915 ffmpegdec->pic_par_n = 0;
1916 ffmpegdec->pic_par_d = 0;
1917 ffmpegdec->pic_interlaced = 0;
1918 ffmpegdec->pic_field_order = 0;
1919 ffmpegdec->pic_field_order_changed = FALSE;
1920 ffmpegdec->ctx_ticks = 0;
1921 ffmpegdec->ctx_time_n = 0;
1922 ffmpegdec->ctx_time_d = 0;
1924 ffmpegdec->pool_width = 0;
1925 ffmpegdec->pool_height = 0;
1926 ffmpegdec->pool_format = 0;
1931 static GstFlowReturn
1932 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1934 gst_ffmpegviddec_drain (decoder);
1935 /* note that finish can and should clean up more drastically,
1936 * but drain is also invoked on e.g. packet loss in GAP handling */
1937 gst_ffmpegviddec_flush (decoder);
1943 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1945 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1947 if (ffmpegdec->opened) {
1948 GST_LOG_OBJECT (decoder, "flushing buffers");
1949 avcodec_flush_buffers (ffmpegdec->context);
1956 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1958 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1959 GstVideoCodecState *state;
1960 GstBufferPool *pool;
1961 guint size, min, max;
1962 GstStructure *config;
1963 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1964 GstAllocator *allocator = NULL;
1965 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1967 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1969 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1973 state = gst_video_decoder_get_output_state (decoder);
1975 if (gst_query_get_n_allocation_params (query) > 0) {
1976 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1977 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1979 gst_query_add_allocation_param (query, allocator, ¶ms);
1982 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1984 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1985 * need, otherwise we may stall */
1986 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1987 gst_object_unref (pool);
1988 pool = gst_video_buffer_pool_new ();
1993 /* if there is an allocator, also drop it, as it might be the reason we
1994 * have this limit. Default will be used */
1996 gst_object_unref (allocator);
2001 config = gst_buffer_pool_get_config (pool);
2002 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2003 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2006 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2009 gst_buffer_pool_config_add_option (config,
2010 GST_BUFFER_POOL_OPTION_VIDEO_META);
2013 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
2015 /* If we have videometa, we never have to copy */
2016 if (have_videometa && have_pool && have_alignment &&
2017 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
2018 GstStructure *config_copy = gst_structure_copy (config);
2020 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
2023 /* FIXME validate and retry */
2024 if (gst_buffer_pool_set_config (pool, config_copy)) {
2028 gst_buffer_pool_set_active (pool, TRUE);
2029 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
2030 if (ret == GST_FLOW_OK) {
2031 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
2032 gboolean same_stride = TRUE;
2035 for (i = 0; i < vmeta->n_planes; i++) {
2036 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
2037 same_stride = FALSE;
2042 gst_buffer_unref (tmp);
2045 if (ffmpegdec->internal_pool)
2046 gst_object_unref (ffmpegdec->internal_pool);
2047 ffmpegdec->internal_pool = gst_object_ref (pool);
2048 ffmpegdec->pool_info = state->info;
2049 gst_structure_free (config);
2056 if (have_videometa && ffmpegdec->internal_pool
2057 && ffmpegdec->pool_width == state->info.width
2058 && ffmpegdec->pool_height == state->info.height) {
2060 gst_object_unref (pool);
2061 pool = gst_object_ref (ffmpegdec->internal_pool);
2062 gst_structure_free (config);
2067 if (!gst_buffer_pool_set_config (pool, config)) {
2068 gboolean working_pool = FALSE;
2069 config = gst_buffer_pool_get_config (pool);
2071 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
2073 working_pool = gst_buffer_pool_set_config (pool, config);
2075 gst_structure_free (config);
2078 if (!working_pool) {
2079 gst_object_unref (pool);
2080 pool = gst_video_buffer_pool_new ();
2081 config = gst_buffer_pool_get_config (pool);
2082 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
2083 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
2084 gst_buffer_pool_set_config (pool, config);
2092 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2094 gst_object_unref (pool);
2096 gst_object_unref (allocator);
2097 gst_video_codec_state_unref (state);
2103 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
2106 GstAllocationParams params;
2108 gst_allocation_params_init (¶ms);
2109 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
2110 params.align = DEFAULT_STRIDE_ALIGN;
2111 params.padding = AV_INPUT_BUFFER_PADDING_SIZE;
2112 /* we would like to have some padding so that we don't have to
2113 * memcpy. We don't suggest an allocator. */
2114 gst_query_add_allocation_param (query, NULL, ¶ms);
2116 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
2121 gst_ffmpegviddec_set_property (GObject * object,
2122 guint prop_id, const GValue * value, GParamSpec * pspec)
2124 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2128 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
2130 case PROP_SKIPFRAME:
2131 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
2132 g_value_get_enum (value);
2134 case PROP_DIRECT_RENDERING:
2135 ffmpegdec->direct_rendering = g_value_get_boolean (value);
2138 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
2139 g_value_get_boolean (value);
2141 case PROP_MAX_THREADS:
2142 ffmpegdec->max_threads = g_value_get_int (value);
2144 case PROP_OUTPUT_CORRUPT:
2145 ffmpegdec->output_corrupt = g_value_get_boolean (value);
2148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2154 gst_ffmpegviddec_get_property (GObject * object,
2155 guint prop_id, GValue * value, GParamSpec * pspec)
2157 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
2161 g_value_set_enum (value, ffmpegdec->context->lowres);
2163 case PROP_SKIPFRAME:
2164 g_value_set_enum (value, ffmpegdec->context->skip_frame);
2166 case PROP_DIRECT_RENDERING:
2167 g_value_set_boolean (value, ffmpegdec->direct_rendering);
2170 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
2172 case PROP_MAX_THREADS:
2173 g_value_set_int (value, ffmpegdec->max_threads);
2175 case PROP_OUTPUT_CORRUPT:
2176 g_value_set_boolean (value, ffmpegdec->output_corrupt);
2179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2185 gst_ffmpegviddec_register (GstPlugin * plugin)
2187 GTypeInfo typeinfo = {
2188 sizeof (GstFFMpegVidDecClass),
2189 (GBaseInitFunc) gst_ffmpegviddec_base_init,
2191 (GClassInitFunc) gst_ffmpegviddec_class_init,
2194 sizeof (GstFFMpegVidDec),
2196 (GInstanceInitFunc) gst_ffmpegviddec_init,
2203 GST_LOG ("Registering decoders");
2205 while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
2209 /* only video decoders */
2210 if (!av_codec_is_decoder (in_plugin)
2211 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2214 /* no quasi codecs, please */
2215 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2216 in_plugin->id == AV_CODEC_ID_V210 ||
2217 in_plugin->id == AV_CODEC_ID_V210X ||
2218 in_plugin->id == AV_CODEC_ID_V308 ||
2219 in_plugin->id == AV_CODEC_ID_V408 ||
2220 in_plugin->id == AV_CODEC_ID_V410 ||
2221 in_plugin->id == AV_CODEC_ID_R210
2222 || in_plugin->id == AV_CODEC_ID_AYUV
2223 || in_plugin->id == AV_CODEC_ID_Y41P
2224 || in_plugin->id == AV_CODEC_ID_012V
2225 || in_plugin->id == AV_CODEC_ID_YUV4
2226 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
2227 AV_VERSION_INT (57,4,0)
2228 || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
2230 || in_plugin->id == AV_CODEC_ID_ZLIB) {
2234 /* No decoders depending on external libraries (we don't build them, but
2235 * people who build against an external ffmpeg might have them.
2236 * We have native gstreamer plugins for all of those libraries anyway. */
2237 if (!strncmp (in_plugin->name, "lib", 3)) {
2239 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2244 /* No vdpau plugins until we can figure out how to properly use them
2245 * outside of ffmpeg. */
2246 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2248 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2253 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2255 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2260 if (strstr (in_plugin->name, "vaapi")) {
2262 ("Ignoring VAAPI decoder %s. We can't handle this outside of ffmpeg",
2267 if (g_str_has_suffix (in_plugin->name, "_qsv")) {
2269 ("Ignoring qsv decoder %s. We can't handle this outside of ffmpeg",
2274 if (g_str_has_suffix (in_plugin->name, "_cuvid")) {
2276 ("Ignoring CUVID decoder %s. We can't handle this outside of ffmpeg",
2281 if (g_str_has_suffix (in_plugin->name, "_v4l2m2m")) {
2283 ("Ignoring V4L2 mem-to-mem decoder %s. We can't handle this outside of ffmpeg",
2288 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2290 /* no codecs for which we're GUARANTEED to have better alternatives */
2291 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2292 /* MP1 : Use MP3 for decoding */
2293 /* MP2 : Use MP3 for decoding */
2294 /* Theora: Use libtheora based theoradec */
2295 if (!strcmp (in_plugin->name, "theora") ||
2296 !strcmp (in_plugin->name, "mpeg1video") ||
2297 strstr (in_plugin->name, "crystalhd") != NULL ||
2298 !strcmp (in_plugin->name, "ass") ||
2299 !strcmp (in_plugin->name, "srt") ||
2300 !strcmp (in_plugin->name, "pgssub") ||
2301 !strcmp (in_plugin->name, "dvdsub") ||
2302 !strcmp (in_plugin->name, "dvbsub")) {
2303 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2307 /* construct the type */
2308 if (!strcmp (in_plugin->name, "hevc")) {
2309 plugin_name = g_strdup ("h265");
2311 plugin_name = g_strdup ((gchar *) in_plugin->name);
2313 g_strdelimit (plugin_name, NULL, '_');
2314 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2315 g_free (plugin_name);
2317 type = g_type_from_name (type_name);
2320 /* create the gtype now */
2322 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2324 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2327 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2328 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2329 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2330 * VC1/WMV3 are not working and thus unpreferred for now. */
2331 switch (in_plugin->id) {
2332 case AV_CODEC_ID_MPEG1VIDEO:
2333 case AV_CODEC_ID_MPEG2VIDEO:
2334 case AV_CODEC_ID_MPEG4:
2335 case AV_CODEC_ID_MSMPEG4V3:
2336 case AV_CODEC_ID_H264:
2337 case AV_CODEC_ID_HEVC:
2338 case AV_CODEC_ID_RV10:
2339 case AV_CODEC_ID_RV20:
2340 case AV_CODEC_ID_RV30:
2341 case AV_CODEC_ID_RV40:
2342 rank = GST_RANK_PRIMARY;
2344 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2345 * They say libdv's quality is better though. leave as secondary.
2346 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2348 case AV_CODEC_ID_DVVIDEO:
2349 rank = GST_RANK_SECONDARY;
2352 rank = GST_RANK_MARGINAL;
2355 if (!gst_element_register (plugin, type_name, rank, type)) {
2356 g_warning ("Failed to register %s", type_name);
2364 GST_LOG ("Finished Registering decoders");