2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #include <libavcodec/avcodec.h>
30 #include <gst/video/video.h>
31 #include <gst/video/gstvideodecoder.h>
32 #include <gst/video/gstvideometa.h>
33 #include <gst/video/gstvideopool.h>
36 #include "gstavcodecmap.h"
37 #include "gstavutils.h"
38 #include "gstavviddec.h"
40 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
42 #define MAX_TS_MASK 0xff
44 #define DEFAULT_LOWRES 0
45 #define DEFAULT_SKIPFRAME 0
46 #define DEFAULT_DIRECT_RENDERING TRUE
47 #define DEFAULT_DEBUG_MV FALSE
48 #define DEFAULT_MAX_THREADS 0
49 #define DEFAULT_OUTPUT_CORRUPT TRUE
50 #define REQUIRED_POOL_MAX_BUFFERS 32
51 #define DEFAULT_STRIDE_ALIGN 31
52 #define DEFAULT_ALLOC_PARAM { 0, DEFAULT_STRIDE_ALIGN, 0, 0, }
59 PROP_DIRECT_RENDERING,
66 /* A number of function prototypes are given so we can refer to them later. */
67 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
68 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
69 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
70 static void gst_ffmpegviddec_finalize (GObject * object);
72 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
73 GstVideoCodecState * state);
74 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
75 GstVideoCodecFrame * frame);
76 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
77 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
78 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
79 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
81 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
84 static void gst_ffmpegviddec_set_property (GObject * object,
85 guint prop_id, const GValue * value, GParamSpec * pspec);
86 static void gst_ffmpegviddec_get_property (GObject * object,
87 guint prop_id, GValue * value, GParamSpec * pspec);
89 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
90 AVCodecContext * context, AVFrame * picture);
92 /* some sort of bufferpool handling, but different */
93 static int gst_ffmpegviddec_get_buffer2 (AVCodecContext * context,
94 AVFrame * picture, int flags);
96 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
97 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
99 static gboolean picture_changed (GstFFMpegVidDec * ffmpegdec,
101 static gboolean context_changed (GstFFMpegVidDec * ffmpegdec,
102 AVCodecContext * context);
104 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
106 static GstElementClass *parent_class = NULL;
108 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
110 gst_ffmpegviddec_lowres_get_type (void)
112 static GType ffmpegdec_lowres_type = 0;
114 if (!ffmpegdec_lowres_type) {
115 static const GEnumValue ffmpegdec_lowres[] = {
117 {1, "1", "1/2-size"},
118 {2, "2", "1/4-size"},
122 ffmpegdec_lowres_type =
123 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
126 return ffmpegdec_lowres_type;
129 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
131 gst_ffmpegviddec_skipframe_get_type (void)
133 static GType ffmpegdec_skipframe_type = 0;
135 if (!ffmpegdec_skipframe_type) {
136 static const GEnumValue ffmpegdec_skipframe[] = {
137 {0, "0", "Skip nothing"},
138 {1, "1", "Skip B-frames"},
139 {2, "2", "Skip IDCT/Dequantization"},
140 {5, "5", "Skip everything"},
144 ffmpegdec_skipframe_type =
145 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
148 return ffmpegdec_skipframe_type;
152 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
154 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 GstPadTemplate *sinktempl, *srctempl;
156 GstCaps *sinkcaps, *srccaps;
158 gchar *longname, *description;
161 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
162 GST_FFDEC_PARAMS_QDATA);
163 g_assert (in_plugin != NULL);
165 /* construct the element details struct */
166 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
167 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
168 gst_element_class_set_metadata (element_class, longname,
169 "Codec/Decoder/Video", description,
170 "Wim Taymans <wim.taymans@gmail.com>, "
171 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
172 "Edward Hervey <bilboed@bilboed.com>");
174 g_free (description);
177 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
179 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
180 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
182 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
183 in_plugin->id, FALSE, in_plugin);
185 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
186 srccaps = gst_caps_from_string ("video/x-raw");
190 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
191 GST_PAD_ALWAYS, sinkcaps);
192 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
194 gst_element_class_add_pad_template (element_class, srctempl);
195 gst_element_class_add_pad_template (element_class, sinktempl);
197 klass->in_plugin = in_plugin;
201 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
203 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
204 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
207 parent_class = g_type_class_peek_parent (klass);
209 gobject_class->finalize = gst_ffmpegviddec_finalize;
211 gobject_class->set_property = gst_ffmpegviddec_set_property;
212 gobject_class->get_property = gst_ffmpegviddec_get_property;
214 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
215 g_param_spec_enum ("skip-frame", "Skip frames",
216 "Which types of frames to skip during decoding",
217 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_LOWRES,
220 g_param_spec_enum ("lowres", "Low resolution",
221 "At which resolution to decode images",
222 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
225 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
226 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
227 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
229 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
230 "Whether libav should print motion vectors on top of the image",
231 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
233 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
234 "Whether libav should output frames even if corrupted",
235 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 caps = klass->in_plugin->capabilities;
238 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
239 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
240 g_param_spec_int ("max-threads", "Maximum decode threads",
241 "Maximum number of worker threads to spawn. (0 = auto)",
242 0, G_MAXINT, DEFAULT_MAX_THREADS,
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 viddec_class->set_format = gst_ffmpegviddec_set_format;
247 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
248 viddec_class->start = gst_ffmpegviddec_start;
249 viddec_class->stop = gst_ffmpegviddec_stop;
250 viddec_class->flush = gst_ffmpegviddec_flush;
251 viddec_class->finish = gst_ffmpegviddec_finish;
252 viddec_class->drain = gst_ffmpegviddec_finish; /* drain and finish are the same to us */
253 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
254 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
258 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
260 GstFFMpegVidDecClass *klass =
261 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
263 /* some ffmpeg data */
264 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
265 ffmpegdec->context->opaque = ffmpegdec;
266 ffmpegdec->picture = av_frame_alloc ();
267 ffmpegdec->opened = FALSE;
268 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
269 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
270 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
271 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
272 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
274 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec));
275 gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
278 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
282 gst_ffmpegviddec_finalize (GObject * object)
284 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
286 av_frame_free (&ffmpegdec->picture);
288 if (ffmpegdec->context != NULL) {
289 gst_ffmpeg_avcodec_close (ffmpegdec->context);
290 av_free (ffmpegdec->context);
291 ffmpegdec->context = NULL;
294 G_OBJECT_CLASS (parent_class)->finalize (object);
298 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
301 g_return_if_fail (context != NULL);
304 context->flags |= flags;
306 context->flags &= ~flags;
311 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
313 GstFFMpegVidDecClass *oclass;
316 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
318 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
320 gst_caps_replace (&ffmpegdec->last_caps, NULL);
322 gst_ffmpeg_avcodec_close (ffmpegdec->context);
323 ffmpegdec->opened = FALSE;
325 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
326 ffmpegdec->stride[i] = -1;
328 gst_buffer_replace (&ffmpegdec->palette, NULL);
330 if (ffmpegdec->context->extradata) {
331 av_free (ffmpegdec->context->extradata);
332 ffmpegdec->context->extradata = NULL;
334 if (ffmpegdec->context->slice_offset) {
335 g_free (ffmpegdec->context->slice_offset);
336 ffmpegdec->context->slice_offset = NULL;
339 if (avcodec_get_context_defaults3 (ffmpegdec->context,
340 oclass->in_plugin) < 0) {
341 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
344 ffmpegdec->context->opaque = ffmpegdec;
351 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
353 GstFFMpegVidDecClass *oclass;
356 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
358 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
361 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
362 ffmpegdec->stride[i] = -1;
364 ffmpegdec->opened = TRUE;
365 ffmpegdec->is_realvideo = FALSE;
367 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
368 oclass->in_plugin->name, oclass->in_plugin->id);
370 switch (oclass->in_plugin->id) {
371 case AV_CODEC_ID_RV10:
372 case AV_CODEC_ID_RV30:
373 case AV_CODEC_ID_RV20:
374 case AV_CODEC_ID_RV40:
375 ffmpegdec->is_realvideo = TRUE;
378 GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
382 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
383 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
390 gst_ffmpegviddec_close (ffmpegdec, TRUE);
391 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
392 oclass->in_plugin->name);
398 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
399 GstVideoCodecState * state)
401 GstStructure *str = gst_caps_get_structure (state->caps, 0);
402 const GValue *palette_v;
405 /* do we have a palette? */
406 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
407 palette = gst_value_get_buffer (palette_v);
408 GST_DEBUG ("got palette data %p", palette);
409 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
410 gst_buffer_replace (&ffmpegdec->palette, palette);
417 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
418 GstVideoCodecState * state)
420 GstFFMpegVidDec *ffmpegdec;
421 GstFFMpegVidDecClass *oclass;
422 GstClockTime latency = GST_CLOCK_TIME_NONE;
423 gboolean ret = FALSE;
425 ffmpegdec = (GstFFMpegVidDec *) decoder;
426 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
428 if (ffmpegdec->last_caps != NULL &&
429 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
433 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
435 GST_OBJECT_LOCK (ffmpegdec);
436 /* stupid check for VC1 */
437 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
438 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
439 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
441 /* close old session */
442 if (ffmpegdec->opened) {
443 GST_OBJECT_UNLOCK (ffmpegdec);
444 gst_ffmpegviddec_drain (ffmpegdec);
445 GST_OBJECT_LOCK (ffmpegdec);
446 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
447 GST_OBJECT_UNLOCK (ffmpegdec);
450 ffmpegdec->pic_pix_fmt = 0;
451 ffmpegdec->pic_width = 0;
452 ffmpegdec->pic_height = 0;
453 ffmpegdec->pic_par_n = 0;
454 ffmpegdec->pic_par_d = 0;
455 ffmpegdec->ctx_ticks = 0;
456 ffmpegdec->ctx_time_n = 0;
457 ffmpegdec->ctx_time_d = 0;
460 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
462 /* set buffer functions */
463 ffmpegdec->context->get_buffer2 = gst_ffmpegviddec_get_buffer2;
464 ffmpegdec->context->get_buffer = NULL;
465 ffmpegdec->context->reget_buffer = NULL;
466 ffmpegdec->context->release_buffer = NULL;
467 ffmpegdec->context->draw_horiz_band = NULL;
469 /* reset coded_width/_height to prevent it being reused from last time when
470 * the codec is opened again, causing a mismatch and possible
471 * segfault/corruption. (Common scenario when renegotiating caps) */
472 ffmpegdec->context->coded_width = 0;
473 ffmpegdec->context->coded_height = 0;
475 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
476 ffmpegdec->context->height);
478 /* FIXME : Create a method that takes GstVideoCodecState instead */
479 /* get size and so */
480 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
481 oclass->in_plugin->type, state->caps, ffmpegdec->context);
483 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
484 ffmpegdec->context->height);
486 gst_ffmpegviddec_get_palette (ffmpegdec, state);
488 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
489 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
490 ffmpegdec->context->time_base.num = 1;
491 ffmpegdec->context->time_base.den = 25;
494 /* workaround encoder bugs */
495 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
496 ffmpegdec->context->err_recognition = 1;
499 ffmpegdec->context->lowres = ffmpegdec->lowres;
500 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
502 /* ffmpeg can draw motion vectors on top of the image (not every decoder
504 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
510 if (ffmpegdec->max_threads == 0) {
511 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
512 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
514 ffmpegdec->context->thread_count = 0;
516 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
518 query = gst_query_new_latency ();
520 /* Check if upstream is live. If it isn't we can enable frame based
521 * threading, which is adding latency */
522 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
523 gst_query_parse_latency (query, &is_live, NULL, NULL);
525 gst_query_unref (query);
528 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
530 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
533 /* open codec - we don't select an output pix_fmt yet,
534 * simply because we don't know! We only get it
535 * during playback... */
536 if (!gst_ffmpegviddec_open (ffmpegdec))
539 if (ffmpegdec->input_state)
540 gst_video_codec_state_unref (ffmpegdec->input_state);
541 ffmpegdec->input_state = gst_video_codec_state_ref (state);
543 if (ffmpegdec->input_state->info.fps_n) {
544 GstVideoInfo *info = &ffmpegdec->input_state->info;
545 latency = gst_util_uint64_scale_ceil (
546 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
553 GST_OBJECT_UNLOCK (ffmpegdec);
555 if (GST_CLOCK_TIME_IS_VALID (latency))
556 gst_video_decoder_set_latency (decoder, latency, latency);
563 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
570 GstFFMpegVidDec *ffmpegdec;
571 GstVideoCodecFrame *frame;
573 GstVideoFrame vframe;
575 AVBufferRef *avbuffer;
576 } GstFFMpegVidDecVideoFrame;
578 static GstFFMpegVidDecVideoFrame *
579 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
580 GstVideoCodecFrame * frame)
582 GstFFMpegVidDecVideoFrame *dframe;
584 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
585 dframe->ffmpegdec = ffmpegdec;
586 dframe->frame = frame;
588 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
594 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
595 GstFFMpegVidDecVideoFrame * frame)
597 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
600 gst_video_frame_unmap (&frame->vframe);
601 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
602 gst_buffer_replace (&frame->buffer, NULL);
603 if (frame->avbuffer) {
604 av_buffer_unref (&frame->avbuffer);
606 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
610 dummy_free_buffer (void *opaque, uint8_t * data)
612 GstFFMpegVidDecVideoFrame *frame = opaque;
614 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
617 /* This function prepares the pool configuration for direct rendering. To use
618 * this method, the codec should support direct rendering and the pool should
619 * support video meta and video alignment */
621 gst_ffmpegvideodec_prepare_dr_pool (GstFFMpegVidDec * ffmpegdec,
622 GstBufferPool * pool, GstVideoInfo * info, GstStructure * config)
624 GstAllocationParams params;
625 GstVideoAlignment align;
626 GstAllocator *allocator = NULL;
628 gint linesize_align[4];
633 width = GST_VIDEO_INFO_WIDTH (info);
634 height = GST_VIDEO_INFO_HEIGHT (info);
636 /* let ffmpeg find the alignment and padding */
637 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
640 if (ffmpegdec->context->flags & CODEC_FLAG_EMU_EDGE)
643 edge = avcodec_get_edge_width ();
645 /* increase the size for the padding */
649 align.padding_top = edge;
650 align.padding_left = edge;
651 align.padding_right = width - GST_VIDEO_INFO_WIDTH (info) - edge;
652 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (info) - edge;
654 /* add extra padding to match libav buffer allocation sizes */
655 align.padding_bottom++;
657 gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms);
659 max_align = DEFAULT_STRIDE_ALIGN;
660 max_align |= params.align;
662 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
663 if (linesize_align[i] > 0)
664 max_align |= linesize_align[i] - 1;
667 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
668 align.stride_align[i] = max_align;
670 params.align = max_align;
672 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
674 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
675 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
676 GST_VIDEO_INFO_WIDTH (info),
677 GST_VIDEO_INFO_HEIGHT (info), width, height, align.padding_top,
678 align.padding_left, align.padding_right, align.padding_bottom,
679 align.stride_align[0], align.stride_align[1], align.stride_align[2],
680 align.stride_align[3]);
682 gst_buffer_pool_config_add_option (config,
683 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
684 gst_buffer_pool_config_set_video_alignment (config, &align);
688 gst_ffmpegviddec_ensure_internal_pool (GstFFMpegVidDec * ffmpegdec,
691 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
693 GstVideoFormat format;
695 GstStructure *config;
698 if (ffmpegdec->internal_pool != NULL &&
699 ffmpegdec->pool_width == picture->width &&
700 ffmpegdec->pool_height == picture->height &&
701 ffmpegdec->pool_format == picture->format)
704 GST_DEBUG_OBJECT (ffmpegdec, "Updating internal pool (%i, %i)",
705 picture->width, picture->height);
707 format = gst_ffmpeg_pixfmt_to_videoformat (picture->format);
708 gst_video_info_set_format (&info, format, picture->width, picture->height);
710 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
711 ffmpegdec->stride[i] = -1;
713 if (ffmpegdec->internal_pool)
714 gst_object_unref (ffmpegdec->internal_pool);
716 ffmpegdec->internal_pool = gst_video_buffer_pool_new ();
717 config = gst_buffer_pool_get_config (ffmpegdec->internal_pool);
719 caps = gst_video_info_to_caps (&info);
720 gst_buffer_pool_config_set_params (config, caps, info.size, 2, 0);
721 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
722 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
724 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec,
725 ffmpegdec->internal_pool, &info, config);
726 /* generic video pool never fails */
727 gst_buffer_pool_set_config (ffmpegdec->internal_pool, config);
728 gst_caps_unref (caps);
730 gst_buffer_pool_set_active (ffmpegdec->internal_pool, TRUE);
732 /* Remember pool size so we can detect changes */
733 ffmpegdec->pool_width = picture->width;
734 ffmpegdec->pool_height = picture->height;
735 ffmpegdec->pool_format = picture->format;
736 ffmpegdec->pool_info = info;
740 gst_ffmpegviddec_can_direct_render (GstFFMpegVidDec * ffmpegdec)
742 GstFFMpegVidDecClass *oclass;
744 if (!ffmpegdec->direct_rendering)
747 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
748 return ((oclass->in_plugin->capabilities & CODEC_CAP_DR1) == CODEC_CAP_DR1);
751 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
752 * into. We try to give it memory from our pool */
754 gst_ffmpegviddec_get_buffer2 (AVCodecContext * context, AVFrame * picture,
757 GstVideoCodecFrame *frame;
758 GstFFMpegVidDecVideoFrame *dframe;
759 GstFFMpegVidDec *ffmpegdec;
763 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
765 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
767 /* apply the last info we have seen to this picture, when we get the
768 * picture back from ffmpeg we can use this to correctly timestamp the output
770 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
771 (gint32) picture->reordered_opaque);
774 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
775 picture->reordered_opaque);
776 if (G_UNLIKELY (frame == NULL))
779 /* now it has a buffer allocated, so it is real and will also
781 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
782 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
784 if (G_UNLIKELY (frame->output_buffer != NULL))
785 goto duplicate_frame;
787 /* GstFFMpegVidDecVideoFrame receives the frame ref */
788 if (picture->opaque) {
789 dframe = picture->opaque;
790 dframe->frame = frame;
792 picture->opaque = dframe =
793 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
796 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
798 if (!gst_ffmpegviddec_can_direct_render (ffmpegdec))
801 gst_ffmpegviddec_ensure_internal_pool (ffmpegdec, picture);
803 ret = gst_buffer_pool_acquire_buffer (ffmpegdec->internal_pool,
804 &frame->output_buffer, NULL);
805 if (ret != GST_FLOW_OK)
808 /* piggy-backed alloc'ed on the frame,
809 * and there was much rejoicing and we are grateful.
810 * Now take away buffer from frame, we will give it back later when decoded.
811 * This allows multiple request for a buffer per frame; unusual but possible. */
812 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
813 gst_buffer_replace (&frame->output_buffer, NULL);
816 if (!gst_video_frame_map (&dframe->vframe, &ffmpegdec->pool_info,
817 dframe->buffer, GST_MAP_READWRITE))
819 dframe->mapped = TRUE;
821 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
822 if (c < GST_VIDEO_INFO_N_PLANES (&ffmpegdec->pool_info)) {
823 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
824 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
826 if (ffmpegdec->stride[c] == -1)
827 ffmpegdec->stride[c] = picture->linesize[c];
829 /* libav does not allow stride changes, decide allocation should check
830 * before replacing the internal pool with a downstream pool.
831 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
832 * https://bugzilla.libav.org/show_bug.cgi?id=556
834 g_assert (picture->linesize[c] == ffmpegdec->stride[c]);
836 picture->data[c] = NULL;
837 picture->linesize[c] = 0;
839 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
843 picture->buf[0] = av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
845 /* tell ffmpeg we own this buffer, transfer the ref we have on the buffer to
846 * the opaque data. */
847 picture->type = FF_BUFFER_TYPE_USER;
849 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
856 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
861 /* alloc default buffer when we can't get one from downstream */
862 GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
867 /* alloc default buffer when we can't get one from downstream */
868 GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
869 gst_buffer_replace (&dframe->buffer, NULL);
875 int ret = avcodec_default_get_buffer2 (context, picture, flags);
877 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
878 ffmpegdec->stride[c] = picture->linesize[c];
880 /* Wrap our buffer around the default one to be able to have a callback
881 * when our data can be freed. Just putting our data into the first free
882 * buffer might not work if there are too many allocated already
884 if (picture->buf[0]) {
885 dframe->avbuffer = picture->buf[0];
887 av_buffer_create (picture->buf[0]->data, picture->buf[0]->size,
888 dummy_free_buffer, dframe, 0);
891 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
898 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
899 gst_video_codec_frame_unref (frame);
904 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
910 picture_changed (GstFFMpegVidDec * ffmpegdec, AVFrame * picture)
912 return !(ffmpegdec->pic_width == picture->width
913 && ffmpegdec->pic_height == picture->height
914 && ffmpegdec->pic_pix_fmt == picture->format
915 && ffmpegdec->pic_par_n == picture->sample_aspect_ratio.num
916 && ffmpegdec->pic_par_d == picture->sample_aspect_ratio.den
917 && ffmpegdec->pic_interlaced == picture->interlaced_frame);
921 context_changed (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context)
923 return !(ffmpegdec->ctx_ticks == context->ticks_per_frame
924 && ffmpegdec->ctx_time_n == context->time_base.num
925 && ffmpegdec->ctx_time_d == context->time_base.den);
929 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
932 if (!picture_changed (ffmpegdec, picture)
933 && !context_changed (ffmpegdec, context))
936 GST_DEBUG_OBJECT (ffmpegdec,
937 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps pixfmt %d to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
938 ffmpegdec->pic_width, ffmpegdec->pic_height,
939 ffmpegdec->pic_par_n, ffmpegdec->pic_par_d,
940 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
941 ffmpegdec->pic_pix_fmt,
942 picture->width, picture->height,
943 picture->sample_aspect_ratio.num,
944 picture->sample_aspect_ratio.den,
945 context->time_base.num, context->time_base.den, picture->format);
947 ffmpegdec->pic_pix_fmt = picture->format;
948 ffmpegdec->pic_width = picture->width;
949 ffmpegdec->pic_height = picture->height;
950 ffmpegdec->pic_par_n = picture->sample_aspect_ratio.num;
951 ffmpegdec->pic_par_d = picture->sample_aspect_ratio.den;
952 ffmpegdec->pic_interlaced = picture->interlaced_frame;
953 ffmpegdec->ctx_ticks = context->ticks_per_frame;
954 ffmpegdec->ctx_time_n = context->time_base.num;
955 ffmpegdec->ctx_time_d = context->time_base.den;
961 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
962 GstVideoInfo * in_info, GstVideoInfo * out_info)
964 gboolean demuxer_par_set = FALSE;
965 gboolean decoder_par_set = FALSE;
966 gint demuxer_num = 1, demuxer_denom = 1;
967 gint decoder_num = 1, decoder_denom = 1;
969 if (in_info->par_n && in_info->par_d) {
970 demuxer_num = in_info->par_n;
971 demuxer_denom = in_info->par_d;
972 demuxer_par_set = TRUE;
973 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
977 if (ffmpegdec->pic_par_n && ffmpegdec->pic_par_d) {
978 decoder_num = ffmpegdec->pic_par_n;
979 decoder_denom = ffmpegdec->pic_par_d;
980 decoder_par_set = TRUE;
981 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
985 if (!demuxer_par_set && !decoder_par_set)
988 if (demuxer_par_set && !decoder_par_set)
989 goto use_demuxer_par;
991 if (decoder_par_set && !demuxer_par_set)
992 goto use_decoder_par;
994 /* Both the demuxer and the decoder provide a PAR. If one of
995 * the two PARs is 1:1 and the other one is not, use the one
996 * that is not 1:1. */
997 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
998 goto use_decoder_par;
1000 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
1001 goto use_demuxer_par;
1003 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
1004 goto use_demuxer_par;
1008 GST_DEBUG_OBJECT (ffmpegdec,
1009 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
1011 out_info->par_n = decoder_num;
1012 out_info->par_d = decoder_denom;
1017 GST_DEBUG_OBJECT (ffmpegdec,
1018 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
1020 out_info->par_n = demuxer_num;
1021 out_info->par_d = demuxer_denom;
1026 GST_DEBUG_OBJECT (ffmpegdec,
1027 "Neither demuxer nor codec provide a pixel-aspect-ratio");
1028 out_info->par_n = 1;
1029 out_info->par_d = 1;
1035 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
1036 AVCodecContext * context, AVFrame * picture)
1039 GstVideoInfo *in_info, *out_info;
1040 GstVideoCodecState *output_state;
1043 if (!update_video_context (ffmpegdec, context, picture))
1046 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->pic_pix_fmt);
1047 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
1048 goto unknown_format;
1051 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
1052 ffmpegdec->pic_width, ffmpegdec->pic_height, ffmpegdec->input_state);
1053 if (ffmpegdec->output_state)
1054 gst_video_codec_state_unref (ffmpegdec->output_state);
1055 ffmpegdec->output_state = output_state;
1057 in_info = &ffmpegdec->input_state->info;
1058 out_info = &ffmpegdec->output_state->info;
1060 /* set the interlaced flag */
1061 if (ffmpegdec->pic_interlaced)
1062 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
1064 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1066 switch (context->chroma_sample_location) {
1068 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
1071 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
1074 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1077 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1083 /* try to find a good framerate */
1084 if ((in_info->fps_d && in_info->fps_n) ||
1085 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1086 /* take framerate from input when it was specified (#313970) */
1087 fps_n = in_info->fps_n;
1088 fps_d = in_info->fps_d;
1090 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1091 fps_d = ffmpegdec->ctx_time_n;
1094 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1098 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1099 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1106 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1107 out_info->fps_n = fps_n;
1108 out_info->fps_d = fps_d;
1110 /* calculate and update par now */
1111 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1113 /* Copy stereo/multiview info from upstream if set */
1114 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
1115 GST_VIDEO_INFO_MULTIVIEW_MODE (out_info) =
1116 GST_VIDEO_INFO_MULTIVIEW_MODE (in_info);
1117 GST_VIDEO_INFO_MULTIVIEW_FLAGS (out_info) =
1118 GST_VIDEO_INFO_MULTIVIEW_FLAGS (in_info);
1121 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1122 goto negotiate_failed;
1129 GST_ERROR_OBJECT (ffmpegdec,
1130 "decoder requires a video format unsupported by GStreamer");
1135 /* Reset so we try again next time even if force==FALSE */
1136 ffmpegdec->pic_pix_fmt = 0;
1137 ffmpegdec->pic_width = 0;
1138 ffmpegdec->pic_height = 0;
1139 ffmpegdec->pic_par_n = 0;
1140 ffmpegdec->pic_par_d = 0;
1141 ffmpegdec->ctx_ticks = 0;
1142 ffmpegdec->ctx_time_n = 0;
1143 ffmpegdec->ctx_time_d = 0;
1145 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1150 /* perform qos calculations before decoding the next frame.
1152 * Sets the skip_frame flag and if things are really bad, skips to the next
1157 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1158 GstVideoCodecFrame * frame, gboolean * mode_switch)
1160 GstClockTimeDiff diff;
1161 GstSegmentFlags skip_flags =
1162 GST_VIDEO_DECODER_INPUT_SEGMENT (ffmpegdec).flags;
1164 *mode_switch = FALSE;
1169 if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) {
1170 ffmpegdec->context->skip_frame = AVDISCARD_NONKEY;
1171 *mode_switch = TRUE;
1172 } else if (skip_flags & GST_SEGMENT_FLAG_TRICKMODE) {
1173 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1174 *mode_switch = TRUE;
1178 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1181 /* if we don't have timing info, then we don't do QoS */
1182 if (G_UNLIKELY (diff == G_MAXINT64))
1185 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1187 if (*mode_switch == FALSE) {
1188 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1189 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1190 *mode_switch = TRUE;
1191 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1194 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1195 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1196 *mode_switch = TRUE;
1197 GST_DEBUG_OBJECT (ffmpegdec,
1198 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1203 /* get an outbuf buffer with the current picture */
1204 static GstFlowReturn
1205 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1207 GstFlowReturn ret = GST_FLOW_OK;
1208 AVPicture pic, *outpic;
1209 GstVideoFrame vframe;
1213 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1215 if (!ffmpegdec->output_state)
1216 goto not_negotiated;
1219 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1221 if (G_UNLIKELY (ret != GST_FLOW_OK))
1224 /* original ffmpeg code does not handle odd sizes correctly.
1225 * This patched up version does */
1226 /* Fill avpicture */
1227 info = &ffmpegdec->output_state->info;
1228 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1229 GST_MAP_READ | GST_MAP_WRITE))
1232 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1233 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1234 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1235 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1236 GST_LOG_OBJECT (ffmpegdec, "[%i] linesize %d, data %p", c,
1237 pic.linesize[c], pic.data[c]);
1240 pic.linesize[c] = 0;
1244 outpic = (AVPicture *) ffmpegdec->picture;
1246 av_picture_copy (&pic, outpic, ffmpegdec->context->pix_fmt,
1247 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
1249 gst_video_frame_unmap (&vframe);
1251 ffmpegdec->picture->reordered_opaque = -1;
1258 GST_DEBUG_OBJECT (ffmpegdec, "allocation failed");
1263 GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
1264 return GST_FLOW_NOT_NEGOTIATED;
1269 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1271 memset (packet, 0, sizeof (AVPacket));
1272 packet->data = data;
1273 packet->size = size;
1276 /* gst_ffmpegviddec_[video|audio]_frame:
1278 * data: pointer to the data to decode
1279 * size: size of data in bytes
1280 * in_timestamp: incoming timestamp.
1281 * in_duration: incoming duration.
1282 * in_offset: incoming offset (frame number).
1285 * Returns: number of bytes used in decoding. The check for successful decode is
1286 * outbuf being non-NULL.
1289 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1290 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1291 GstFlowReturn * ret)
1294 gboolean mode_switch;
1295 GstVideoCodecFrame *out_frame;
1296 GstFFMpegVidDecVideoFrame *out_dframe;
1298 GstBufferPool *pool;
1302 /* in case we skip frames */
1303 ffmpegdec->picture->pict_type = -1;
1305 /* run QoS code, we don't stop decoding the frame when we are late because
1306 * else we might skip a reference frame */
1307 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1309 if (ffmpegdec->is_realvideo && data != NULL) {
1313 /* setup the slice table for realvideo */
1314 if (ffmpegdec->context->slice_offset == NULL)
1315 ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1317 slice_count = (*data++) + 1;
1318 ffmpegdec->context->slice_count = slice_count;
1320 for (i = 0; i < slice_count; i++) {
1322 ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1328 /* save reference to the timing info */
1329 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1330 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1332 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1333 frame->system_frame_number);
1336 /* now decode the frame */
1337 gst_avpacket_init (&packet, data, size);
1339 if (ffmpegdec->palette) {
1342 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1344 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1345 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1348 /* This might call into get_buffer() from another thread,
1349 * which would cause a deadlock. Release the lock here
1350 * and taking it again later seems safe
1351 * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
1353 GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
1354 len = avcodec_decode_video2 (ffmpegdec->context,
1355 ffmpegdec->picture, have_data, &packet);
1356 GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
1358 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1361 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1362 * no data because we told it to skip stuff. */
1363 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1366 /* no data, we're done */
1367 if (len < 0 || *have_data == 0)
1370 /* get the output picture timing info again */
1371 out_dframe = ffmpegdec->picture->opaque;
1372 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1374 /* also give back a buffer allocated by the frame, if any */
1375 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1376 gst_buffer_replace (&out_dframe->buffer, NULL);
1378 GST_DEBUG_OBJECT (ffmpegdec,
1379 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1380 out_frame->pts, out_frame->duration);
1381 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1382 (guint64) ffmpegdec->picture->pts);
1383 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1384 ffmpegdec->picture->coded_picture_number);
1385 GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1386 ffmpegdec->picture->reference);
1387 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1388 ffmpegdec->picture->display_picture_number);
1389 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1390 ffmpegdec->picture->opaque);
1391 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1392 (guint64) ffmpegdec->picture->reordered_opaque);
1393 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1394 ffmpegdec->picture->repeat_pict);
1395 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1396 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1398 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context,
1399 ffmpegdec->picture))
1400 goto negotiation_error;
1402 pool = gst_video_decoder_get_buffer_pool (GST_VIDEO_DECODER (ffmpegdec));
1403 if (G_UNLIKELY (out_frame->output_buffer == NULL)) {
1404 *ret = get_output_buffer (ffmpegdec, out_frame);
1405 } else if (G_UNLIKELY (out_frame->output_buffer->pool != pool)) {
1406 GstBuffer *tmp = out_frame->output_buffer;
1407 out_frame->output_buffer = NULL;
1408 *ret = get_output_buffer (ffmpegdec, out_frame);
1409 gst_buffer_unref (tmp);
1411 gst_object_unref (pool);
1413 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1416 /* Mark corrupted frames as corrupted */
1417 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1418 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1420 if (ffmpegdec->pic_interlaced) {
1421 /* set interlaced flags */
1422 if (ffmpegdec->picture->repeat_pict)
1423 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1424 if (ffmpegdec->picture->top_field_first)
1425 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1426 if (ffmpegdec->picture->interlaced_frame)
1427 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1428 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1432 /* so we decoded this frame, frames preceding it in decoding order
1433 * that still do not have a buffer allocated seem rather useless,
1434 * and can be discarded, due to e.g. misparsed bogus frame
1435 * or non-keyframe in skipped decoding, ...
1436 * In any case, not likely to be seen again, so discard those,
1437 * before they pile up and/or mess with timestamping */
1440 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1441 gboolean old = TRUE;
1443 ol = l = gst_video_decoder_get_frames (dec);
1445 GstVideoCodecFrame *tmp = l->data;
1450 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1451 GST_LOG_OBJECT (dec,
1452 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1453 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1454 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1455 /* drop extra ref and remove from frame list */
1456 gst_video_decoder_release_frame (dec, tmp);
1458 /* drop extra ref we got */
1459 gst_video_codec_frame_unref (tmp);
1466 av_frame_unref (ffmpegdec->picture);
1468 /* FIXME: Ideally we would remap the buffer read-only now before pushing but
1469 * libav might still have a reference to it!
1472 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1475 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1476 gst_flow_get_name (*ret), len);
1482 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1483 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1490 if (GST_PAD_IS_FLUSHING (GST_VIDEO_DECODER_SRC_PAD (ffmpegdec))) {
1491 *ret = GST_FLOW_FLUSHING;
1494 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1495 *ret = GST_FLOW_NOT_NEGOTIATED;
1501 /* gst_ffmpegviddec_frame:
1503 * data: pointer to the data to decode
1504 * size: size of data in bytes
1505 * got_data: 0 if no data was decoded, != 0 otherwise.
1506 * in_time: timestamp of data
1507 * in_duration: duration of data
1508 * ret: GstFlowReturn to return in the chain function
1510 * Decode the given frame and pushes it downstream.
1512 * Returns: Number of bytes used in decoding, -1 on error/failure.
1516 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1517 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1518 GstFlowReturn * ret)
1520 GstFFMpegVidDecClass *oclass;
1523 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1526 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1529 ffmpegdec->context->frame_number++;
1531 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1534 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1538 GST_WARNING_OBJECT (ffmpegdec,
1539 "avdec_%s: decoding error (len: %d, have_data: %d)",
1540 oclass->in_plugin->name, len, *have_data);
1548 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1549 *ret = GST_FLOW_NOT_NEGOTIATED;
1555 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1557 GstFFMpegVidDecClass *oclass;
1559 if (!ffmpegdec->opened)
1562 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1564 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1565 gint have_data, len;
1568 GST_LOG_OBJECT (ffmpegdec,
1569 "codec has delay capabilities, calling until ffmpeg has drained everything");
1572 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1573 } while (len >= 0 && have_data == 1 && ret == GST_FLOW_OK);
1574 avcodec_flush_buffers (ffmpegdec->context);
1578 static GstFlowReturn
1579 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1580 GstVideoCodecFrame * frame)
1582 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1583 guint8 *data, *bdata;
1584 gint size, len, have_data, bsize;
1586 GstFlowReturn ret = GST_FLOW_OK;
1587 gboolean do_padding;
1589 GST_LOG_OBJECT (ffmpegdec,
1590 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1591 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1592 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1593 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1595 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1596 GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
1597 ("Failed to map buffer for reading"));
1598 return GST_FLOW_ERROR;
1601 /* treat frame as void until a buffer is requested for it */
1602 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1603 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1608 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1609 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1611 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1612 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1613 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1614 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1615 ffmpegdec->padded_size);
1617 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1618 "Copy input to add padding");
1619 memcpy (ffmpegdec->padded, bdata, bsize);
1620 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1622 bdata = ffmpegdec->padded;
1629 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1631 /* parse, if at all possible */
1636 /* add temporary padding */
1637 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1638 "Add temporary input padding");
1639 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1640 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1643 /* decode a frame of audio/video now */
1645 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1647 if (ret != GST_FLOW_OK) {
1648 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1649 gst_flow_get_name (ret));
1650 /* bad flow return, make sure we discard all data and exit */
1656 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1659 if (len == 0 && have_data == 0) {
1660 /* nothing was decoded, this could be because no data was available or
1661 * because we were skipping frames.
1662 * If we have no context we must exit and wait for more data, we keep the
1664 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1669 /* a decoding error happened, we must break and try again with next data. */
1670 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1675 /* prepare for the next round, for codecs with a context we did this
1676 * already when using the parser. */
1682 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1684 } while (bsize > 0);
1687 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1689 gst_buffer_unmap (frame->input_buffer, &minfo);
1690 gst_video_codec_frame_unref (frame);
1696 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1698 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1699 GstFFMpegVidDecClass *oclass;
1701 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1703 GST_OBJECT_LOCK (ffmpegdec);
1704 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1705 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1706 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1707 GST_OBJECT_UNLOCK (ffmpegdec);
1710 ffmpegdec->context->opaque = ffmpegdec;
1711 GST_OBJECT_UNLOCK (ffmpegdec);
1717 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1719 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1721 GST_OBJECT_LOCK (ffmpegdec);
1722 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1723 GST_OBJECT_UNLOCK (ffmpegdec);
1724 g_free (ffmpegdec->padded);
1725 ffmpegdec->padded = NULL;
1726 ffmpegdec->padded_size = 0;
1727 if (ffmpegdec->input_state)
1728 gst_video_codec_state_unref (ffmpegdec->input_state);
1729 ffmpegdec->input_state = NULL;
1730 if (ffmpegdec->output_state)
1731 gst_video_codec_state_unref (ffmpegdec->output_state);
1732 ffmpegdec->output_state = NULL;
1734 if (ffmpegdec->internal_pool)
1735 gst_object_unref (ffmpegdec->internal_pool);
1736 ffmpegdec->internal_pool = NULL;
1738 ffmpegdec->pic_pix_fmt = 0;
1739 ffmpegdec->pic_width = 0;
1740 ffmpegdec->pic_height = 0;
1741 ffmpegdec->pic_par_n = 0;
1742 ffmpegdec->pic_par_d = 0;
1743 ffmpegdec->ctx_ticks = 0;
1744 ffmpegdec->ctx_time_n = 0;
1745 ffmpegdec->ctx_time_d = 0;
1747 ffmpegdec->pool_width = 0;
1748 ffmpegdec->pool_height = 0;
1749 ffmpegdec->pool_format = 0;
1754 static GstFlowReturn
1755 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1757 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1759 gst_ffmpegviddec_drain (ffmpegdec);
1765 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1767 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1769 if (ffmpegdec->opened)
1770 avcodec_flush_buffers (ffmpegdec->context);
1776 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1778 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1779 GstVideoCodecState *state;
1780 GstBufferPool *pool;
1781 guint size, min, max;
1782 GstStructure *config;
1783 gboolean have_pool, have_videometa, have_alignment, update_pool = FALSE;
1784 GstAllocator *allocator = NULL;
1785 GstAllocationParams params = DEFAULT_ALLOC_PARAM;
1787 have_pool = (gst_query_get_n_allocation_pools (query) != 0);
1789 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1793 state = gst_video_decoder_get_output_state (decoder);
1795 if (gst_query_get_n_allocation_params (query) > 0) {
1796 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1797 params.align = MAX (params.align, DEFAULT_STRIDE_ALIGN);
1799 gst_query_add_allocation_param (query, allocator, ¶ms);
1802 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1804 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1805 * need, otherwise we may stall */
1806 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1807 gst_object_unref (pool);
1808 pool = gst_video_buffer_pool_new ();
1813 /* if there is an allocator, also drop it, as it might be the reason we
1814 * have this limit. Default will be used */
1816 gst_object_unref (allocator);
1821 config = gst_buffer_pool_get_config (pool);
1822 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1823 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1826 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1829 gst_buffer_pool_config_add_option (config,
1830 GST_BUFFER_POOL_OPTION_VIDEO_META);
1833 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1835 /* If we have videometa, we never have to copy */
1836 if (have_videometa && have_pool && have_alignment &&
1837 gst_ffmpegviddec_can_direct_render (ffmpegdec)) {
1838 GstStructure *config_copy = gst_structure_copy (config);
1840 gst_ffmpegvideodec_prepare_dr_pool (ffmpegdec, pool, &state->info,
1843 /* FIXME validate and retry */
1844 if (gst_buffer_pool_set_config (pool, gst_structure_copy (config_copy))) {
1848 gst_buffer_pool_set_active (pool, TRUE);
1849 ret = gst_buffer_pool_acquire_buffer (pool, &tmp, NULL);
1850 if (ret == GST_FLOW_OK) {
1851 GstVideoMeta *vmeta = gst_buffer_get_video_meta (tmp);
1852 gboolean same_stride = TRUE;
1855 for (i = 0; i < vmeta->n_planes; i++) {
1856 if (vmeta->stride[i] != ffmpegdec->stride[i]) {
1857 same_stride = FALSE;
1862 gst_buffer_unref (tmp);
1865 if (ffmpegdec->internal_pool)
1866 gst_object_unref (ffmpegdec->internal_pool);
1867 ffmpegdec->internal_pool = gst_object_ref (pool);
1868 ffmpegdec->pool_info = state->info;
1869 gst_structure_free (config);
1876 if (have_videometa && ffmpegdec->internal_pool) {
1878 gst_object_unref (pool);
1879 pool = gst_object_ref (ffmpegdec->internal_pool);
1880 gst_structure_free (config);
1885 if (!gst_buffer_pool_set_config (pool, config)) {
1886 gboolean working_pool = FALSE;
1887 config = gst_buffer_pool_get_config (pool);
1889 if (gst_buffer_pool_config_validate_params (config, state->caps, size, min,
1891 working_pool = gst_buffer_pool_set_config (pool, config);
1893 gst_structure_free (config);
1896 if (!working_pool) {
1897 gst_object_unref (pool);
1898 pool = gst_video_buffer_pool_new ();
1899 config = gst_buffer_pool_get_config (pool);
1900 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1901 gst_buffer_pool_config_set_allocator (config, NULL, ¶ms);
1902 gst_buffer_pool_set_config (pool, config);
1910 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1912 gst_object_unref (pool);
1914 gst_object_unref (allocator);
1915 gst_video_codec_state_unref (state);
1921 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1924 GstAllocationParams params;
1926 gst_allocation_params_init (¶ms);
1927 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1928 params.align = DEFAULT_STRIDE_ALIGN;
1929 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1930 /* we would like to have some padding so that we don't have to
1931 * memcpy. We don't suggest an allocator. */
1932 gst_query_add_allocation_param (query, NULL, ¶ms);
1934 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1939 gst_ffmpegviddec_set_property (GObject * object,
1940 guint prop_id, const GValue * value, GParamSpec * pspec)
1942 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1946 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1948 case PROP_SKIPFRAME:
1949 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1950 g_value_get_enum (value);
1952 case PROP_DIRECT_RENDERING:
1953 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1956 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1957 g_value_get_boolean (value);
1959 case PROP_MAX_THREADS:
1960 ffmpegdec->max_threads = g_value_get_int (value);
1962 case PROP_OUTPUT_CORRUPT:
1963 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1966 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1972 gst_ffmpegviddec_get_property (GObject * object,
1973 guint prop_id, GValue * value, GParamSpec * pspec)
1975 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1979 g_value_set_enum (value, ffmpegdec->context->lowres);
1981 case PROP_SKIPFRAME:
1982 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1984 case PROP_DIRECT_RENDERING:
1985 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1988 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1990 case PROP_MAX_THREADS:
1991 g_value_set_int (value, ffmpegdec->max_threads);
1993 case PROP_OUTPUT_CORRUPT:
1994 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1997 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2003 gst_ffmpegviddec_register (GstPlugin * plugin)
2005 GTypeInfo typeinfo = {
2006 sizeof (GstFFMpegVidDecClass),
2007 (GBaseInitFunc) gst_ffmpegviddec_base_init,
2009 (GClassInitFunc) gst_ffmpegviddec_class_init,
2012 sizeof (GstFFMpegVidDec),
2014 (GInstanceInitFunc) gst_ffmpegviddec_init,
2020 in_plugin = av_codec_next (NULL);
2022 GST_LOG ("Registering decoders");
2028 /* only video decoders */
2029 if (!av_codec_is_decoder (in_plugin)
2030 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
2033 /* no quasi-codecs, please */
2034 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
2035 in_plugin->id == AV_CODEC_ID_V210 ||
2036 in_plugin->id == AV_CODEC_ID_V210X ||
2037 in_plugin->id == AV_CODEC_ID_R210 ||
2038 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
2039 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
2043 /* No decoders depending on external libraries (we don't build them, but
2044 * people who build against an external ffmpeg might have them.
2045 * We have native gstreamer plugins for all of those libraries anyway. */
2046 if (!strncmp (in_plugin->name, "lib", 3)) {
2048 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
2053 /* No vdpau plugins until we can figure out how to properly use them
2054 * outside of ffmpeg. */
2055 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
2057 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
2062 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
2064 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
2069 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
2071 /* no codecs for which we're GUARANTEED to have better alternatives */
2072 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
2073 /* MP1 : Use MP3 for decoding */
2074 /* MP2 : Use MP3 for decoding */
2075 /* Theora: Use libtheora based theoradec */
2076 if (!strcmp (in_plugin->name, "gif") ||
2077 !strcmp (in_plugin->name, "theora") ||
2078 !strcmp (in_plugin->name, "mpeg1video") ||
2079 strstr (in_plugin->name, "crystalhd") != NULL ||
2080 !strcmp (in_plugin->name, "ass") ||
2081 !strcmp (in_plugin->name, "srt") ||
2082 !strcmp (in_plugin->name, "pgssub") ||
2083 !strcmp (in_plugin->name, "dvdsub") ||
2084 !strcmp (in_plugin->name, "dvbsub")) {
2085 GST_LOG ("Ignoring decoder %s", in_plugin->name);
2089 /* construct the type */
2090 if (!strcmp (in_plugin->name, "hevc")) {
2091 plugin_name = g_strdup ("h265");
2093 plugin_name = g_strdup ((gchar *) in_plugin->name);
2095 g_strdelimit (plugin_name, NULL, '_');
2096 type_name = g_strdup_printf ("avdec_%s", plugin_name);
2097 g_free (plugin_name);
2099 type = g_type_from_name (type_name);
2102 /* create the gtype now */
2104 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
2106 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
2109 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
2110 * tested and by far outperforms divxdec/xviddec - so we prefer it.
2111 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
2112 * VC1/WMV3 are not working and thus unpreferred for now. */
2113 switch (in_plugin->id) {
2114 case AV_CODEC_ID_MPEG4:
2115 case AV_CODEC_ID_MSMPEG4V3:
2116 case AV_CODEC_ID_H264:
2117 case AV_CODEC_ID_HEVC:
2118 case AV_CODEC_ID_RV10:
2119 case AV_CODEC_ID_RV20:
2120 case AV_CODEC_ID_RV30:
2121 case AV_CODEC_ID_RV40:
2122 rank = GST_RANK_PRIMARY;
2124 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2125 * They say libdv's quality is better though. leave as secondary.
2126 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2128 case AV_CODEC_ID_DVVIDEO:
2129 rank = GST_RANK_SECONDARY;
2132 rank = GST_RANK_MARGINAL;
2135 if (!gst_element_register (plugin, type_name, rank, type)) {
2136 g_warning ("Failed to register %s", type_name);
2144 in_plugin = av_codec_next (in_plugin);
2147 GST_LOG ("Finished Registering decoders");