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
56 PROP_DIRECT_RENDERING,
63 /* A number of function prototypes are given so we can refer to them later. */
64 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
65 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
66 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
67 static void gst_ffmpegviddec_finalize (GObject * object);
69 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
70 GstVideoCodecState * state);
71 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
72 GstVideoCodecFrame * frame);
73 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
74 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
75 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
76 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
78 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
81 static void gst_ffmpegviddec_set_property (GObject * object,
82 guint prop_id, const GValue * value, GParamSpec * pspec);
83 static void gst_ffmpegviddec_get_property (GObject * object,
84 guint prop_id, GValue * value, GParamSpec * pspec);
86 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
87 AVCodecContext * context, gboolean force);
89 /* some sort of bufferpool handling, but different */
90 static int gst_ffmpegviddec_get_buffer (AVCodecContext * context,
92 static int gst_ffmpegviddec_reget_buffer (AVCodecContext * context,
94 static void gst_ffmpegviddec_release_buffer (AVCodecContext * context,
97 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
98 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
100 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
102 static GstElementClass *parent_class = NULL;
104 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
106 gst_ffmpegviddec_lowres_get_type (void)
108 static GType ffmpegdec_lowres_type = 0;
110 if (!ffmpegdec_lowres_type) {
111 static const GEnumValue ffmpegdec_lowres[] = {
113 {1, "1", "1/2-size"},
114 {2, "2", "1/4-size"},
118 ffmpegdec_lowres_type =
119 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
122 return ffmpegdec_lowres_type;
125 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
127 gst_ffmpegviddec_skipframe_get_type (void)
129 static GType ffmpegdec_skipframe_type = 0;
131 if (!ffmpegdec_skipframe_type) {
132 static const GEnumValue ffmpegdec_skipframe[] = {
133 {0, "0", "Skip nothing"},
134 {1, "1", "Skip B-frames"},
135 {2, "2", "Skip IDCT/Dequantization"},
136 {5, "5", "Skip everything"},
140 ffmpegdec_skipframe_type =
141 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
144 return ffmpegdec_skipframe_type;
148 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
150 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151 GstPadTemplate *sinktempl, *srctempl;
152 GstCaps *sinkcaps, *srccaps;
154 gchar *longname, *description;
157 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
158 GST_FFDEC_PARAMS_QDATA);
159 g_assert (in_plugin != NULL);
161 /* construct the element details struct */
162 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
163 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
164 gst_element_class_set_metadata (element_class, longname,
165 "Codec/Decoder/Video", description,
166 "Wim Taymans <wim.taymans@gmail.com>, "
167 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
168 "Edward Hervey <bilboed@bilboed.com>");
170 g_free (description);
173 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
175 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
176 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
178 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
179 in_plugin->id, FALSE, in_plugin);
181 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
182 srccaps = gst_caps_from_string ("video/x-raw");
186 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
187 GST_PAD_ALWAYS, sinkcaps);
188 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
190 gst_element_class_add_pad_template (element_class, srctempl);
191 gst_element_class_add_pad_template (element_class, sinktempl);
193 klass->in_plugin = in_plugin;
197 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
203 parent_class = g_type_class_peek_parent (klass);
205 gobject_class->finalize = gst_ffmpegviddec_finalize;
207 gobject_class->set_property = gst_ffmpegviddec_set_property;
208 gobject_class->get_property = gst_ffmpegviddec_get_property;
210 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
211 g_param_spec_enum ("skip-frame", "Skip frames",
212 "Which types of frames to skip during decoding",
213 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class, PROP_LOWRES,
216 g_param_spec_enum ("lowres", "Low resolution",
217 "At which resolution to decode images",
218 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
219 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
221 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
222 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
225 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
226 "Whether libav should print motion vectors on top of the image",
227 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
229 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
230 "Whether libav should output frames even if corrupted",
231 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 caps = klass->in_plugin->capabilities;
234 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
235 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
236 g_param_spec_int ("max-threads", "Maximum decode threads",
237 "Maximum number of worker threads to spawn. (0 = auto)",
238 0, G_MAXINT, DEFAULT_MAX_THREADS,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 viddec_class->set_format = gst_ffmpegviddec_set_format;
243 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
244 viddec_class->start = gst_ffmpegviddec_start;
245 viddec_class->stop = gst_ffmpegviddec_stop;
246 viddec_class->flush = gst_ffmpegviddec_flush;
247 viddec_class->finish = gst_ffmpegviddec_finish;
248 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
249 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
253 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
255 GstFFMpegVidDecClass *klass =
256 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
258 /* some ffmpeg data */
259 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
260 ffmpegdec->context->opaque = ffmpegdec;
261 ffmpegdec->picture = avcodec_alloc_frame ();
262 ffmpegdec->opened = FALSE;
263 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
264 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
265 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
266 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
267 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
269 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
273 gst_ffmpegviddec_finalize (GObject * object)
275 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
277 if (ffmpegdec->context != NULL) {
278 gst_ffmpeg_avcodec_close (ffmpegdec->context);
279 av_free (ffmpegdec->context);
280 ffmpegdec->context = NULL;
283 avcodec_free_frame (&ffmpegdec->picture);
285 G_OBJECT_CLASS (parent_class)->finalize (object);
289 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
292 g_return_if_fail (context != NULL);
295 context->flags |= flags;
297 context->flags &= ~flags;
302 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
304 GstFFMpegVidDecClass *oclass;
307 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
309 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
311 gst_caps_replace (&ffmpegdec->last_caps, NULL);
313 gst_ffmpeg_avcodec_close (ffmpegdec->context);
314 ffmpegdec->opened = FALSE;
316 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
317 ffmpegdec->stride[i] = -1;
319 gst_buffer_replace (&ffmpegdec->palette, NULL);
321 if (ffmpegdec->context->extradata) {
322 av_free (ffmpegdec->context->extradata);
323 ffmpegdec->context->extradata = NULL;
327 if (avcodec_get_context_defaults3 (ffmpegdec->context,
328 oclass->in_plugin) < 0) {
329 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
332 ffmpegdec->context->opaque = ffmpegdec;
339 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
341 GstFFMpegVidDecClass *oclass;
344 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
346 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
349 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
350 ffmpegdec->stride[i] = -1;
352 ffmpegdec->opened = TRUE;
353 ffmpegdec->is_realvideo = FALSE;
355 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
356 oclass->in_plugin->name, oclass->in_plugin->id);
358 switch (oclass->in_plugin->id) {
359 case AV_CODEC_ID_RV10:
360 case AV_CODEC_ID_RV30:
361 case AV_CODEC_ID_RV20:
362 case AV_CODEC_ID_RV40:
363 ffmpegdec->is_realvideo = TRUE;
366 GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
370 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
371 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
378 gst_ffmpegviddec_close (ffmpegdec, TRUE);
379 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
380 oclass->in_plugin->name);
386 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
387 GstVideoCodecState * state)
389 GstStructure *str = gst_caps_get_structure (state->caps, 0);
390 const GValue *palette_v;
393 /* do we have a palette? */
394 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
395 palette = gst_value_get_buffer (palette_v);
396 GST_DEBUG ("got palette data %p", palette);
397 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
398 gst_buffer_replace (&ffmpegdec->palette, palette);
405 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
406 GstVideoCodecState * state)
408 GstFFMpegVidDec *ffmpegdec;
409 GstFFMpegVidDecClass *oclass;
410 GstClockTime latency = GST_CLOCK_TIME_NONE;
411 gboolean ret = FALSE;
413 ffmpegdec = (GstFFMpegVidDec *) decoder;
414 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
416 if (ffmpegdec->last_caps != NULL &&
417 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
421 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
423 GST_OBJECT_LOCK (ffmpegdec);
424 /* stupid check for VC1 */
425 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
426 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
427 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
429 /* close old session */
430 if (ffmpegdec->opened) {
431 GST_OBJECT_UNLOCK (ffmpegdec);
432 gst_ffmpegviddec_drain (ffmpegdec);
433 GST_OBJECT_LOCK (ffmpegdec);
434 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
435 GST_OBJECT_UNLOCK (ffmpegdec);
440 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
442 /* set buffer functions */
443 ffmpegdec->context->get_buffer = gst_ffmpegviddec_get_buffer;
444 ffmpegdec->context->reget_buffer = gst_ffmpegviddec_reget_buffer;
445 ffmpegdec->context->release_buffer = gst_ffmpegviddec_release_buffer;
446 ffmpegdec->context->draw_horiz_band = NULL;
448 /* reset coded_width/_height to prevent it being reused from last time when
449 * the codec is opened again, causing a mismatch and possible
450 * segfault/corruption. (Common scenario when renegotiating caps) */
451 ffmpegdec->context->coded_width = 0;
452 ffmpegdec->context->coded_height = 0;
454 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
455 ffmpegdec->context->height);
457 /* FIXME : Create a method that takes GstVideoCodecState instead */
458 /* get size and so */
459 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
460 oclass->in_plugin->type, state->caps, ffmpegdec->context);
462 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
463 ffmpegdec->context->height);
465 gst_ffmpegviddec_get_palette (ffmpegdec, state);
467 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
468 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
469 ffmpegdec->context->time_base.num = 1;
470 ffmpegdec->context->time_base.den = 25;
473 /* workaround encoder bugs */
474 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
475 ffmpegdec->context->err_recognition = 1;
478 ffmpegdec->context->lowres = ffmpegdec->lowres;
479 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
481 /* ffmpeg can draw motion vectors on top of the image (not every decoder
483 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
489 if (ffmpegdec->max_threads == 0) {
490 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
491 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
493 ffmpegdec->context->thread_count = 0;
495 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
497 query = gst_query_new_latency ();
499 /* Check if upstream is live. If it isn't we can enable frame based
500 * threading, which is adding latency */
501 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
502 gst_query_parse_latency (query, &is_live, NULL, NULL);
504 gst_query_unref (query);
507 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
509 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
512 /* open codec - we don't select an output pix_fmt yet,
513 * simply because we don't know! We only get it
514 * during playback... */
515 if (!gst_ffmpegviddec_open (ffmpegdec))
518 if (ffmpegdec->input_state)
519 gst_video_codec_state_unref (ffmpegdec->input_state);
520 ffmpegdec->input_state = gst_video_codec_state_ref (state);
522 if (ffmpegdec->input_state->info.fps_n) {
523 GstVideoInfo *info = &ffmpegdec->input_state->info;
524 latency = gst_util_uint64_scale_ceil (
525 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
532 GST_OBJECT_UNLOCK (ffmpegdec);
534 if (GST_CLOCK_TIME_IS_VALID (latency))
535 gst_video_decoder_set_latency (decoder, latency, latency);
542 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
549 GstFFMpegVidDec *ffmpegdec;
550 GstVideoCodecFrame *frame;
552 GstVideoFrame vframe;
554 } GstFFMpegVidDecVideoFrame;
556 static GstFFMpegVidDecVideoFrame *
557 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
558 GstVideoCodecFrame * frame)
560 GstFFMpegVidDecVideoFrame *dframe;
562 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
563 dframe->ffmpegdec = ffmpegdec;
564 dframe->frame = frame;
566 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
572 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
573 GstFFMpegVidDecVideoFrame * frame)
575 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
578 gst_video_frame_unmap (&frame->vframe);
579 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
580 gst_buffer_replace (&frame->buffer, NULL);
581 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
585 dummy_free_buffer (void *opaque, uint8_t * data)
587 GstFFMpegVidDecVideoFrame *frame = opaque;
589 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
592 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
593 * into. We try to give it memory from our pool */
595 gst_ffmpegviddec_get_buffer (AVCodecContext * context, AVFrame * picture)
597 GstVideoCodecFrame *frame;
598 GstFFMpegVidDecVideoFrame *dframe;
599 GstFFMpegVidDec *ffmpegdec;
604 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
606 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
608 /* apply the last info we have seen to this picture, when we get the
609 * picture back from ffmpeg we can use this to correctly timestamp the output
611 picture->reordered_opaque = context->reordered_opaque;
612 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
613 (gint32) picture->reordered_opaque);
616 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
617 picture->reordered_opaque);
618 if (G_UNLIKELY (frame == NULL))
621 /* now it has a buffer allocated, so it is real and will also
623 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
624 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
626 if (G_UNLIKELY (frame->output_buffer != NULL))
627 goto duplicate_frame;
629 /* GstFFMpegVidDecVideoFrame receives the frame ref */
630 picture->opaque = dframe =
631 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
633 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
635 ffmpegdec->context->pix_fmt = context->pix_fmt;
637 /* see if we need renegotiation */
638 if (G_UNLIKELY (!gst_ffmpegviddec_negotiate (ffmpegdec, context, FALSE)))
639 goto negotiate_failed;
641 if (!ffmpegdec->current_dr)
645 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
647 if (ret != GST_FLOW_OK)
650 /* piggy-backed alloc'ed on the frame,
651 * and there was much rejoicing and we are grateful.
652 * Now take away buffer from frame, we will give it back later when decoded.
653 * This allows multiple request for a buffer per frame; unusual but possible. */
654 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
655 gst_buffer_replace (&frame->output_buffer, NULL);
658 info = &ffmpegdec->output_state->info;
659 if (!gst_video_frame_map (&dframe->vframe, info, dframe->buffer,
662 dframe->mapped = TRUE;
664 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
665 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
666 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
667 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
669 /* libav does not allow stride changes currently, fall back to
670 * non-direct rendering here:
671 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
672 * https://bugzilla.libav.org/show_bug.cgi?id=556
674 if (ffmpegdec->stride[c] == -1) {
675 ffmpegdec->stride[c] = picture->linesize[c];
676 } else if (picture->linesize[c] != ffmpegdec->stride[c]) {
677 GST_LOG_OBJECT (ffmpegdec,
678 "No direct rendering, stride changed c=%d %d->%d", c,
679 ffmpegdec->stride[c], picture->linesize[c]);
681 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
682 picture->data[c] = NULL;
683 picture->linesize[c] = 0;
685 gst_video_frame_unmap (&dframe->vframe);
686 dframe->mapped = FALSE;
687 gst_buffer_replace (&dframe->buffer, NULL);
688 ffmpegdec->current_dr = FALSE;
693 picture->data[c] = NULL;
694 picture->linesize[c] = 0;
696 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
700 /* tell ffmpeg we own this buffer, tranfer the ref we have on the buffer to
701 * the opaque data. */
702 picture->type = FF_BUFFER_TYPE_USER;
704 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
711 GST_DEBUG_OBJECT (ffmpegdec, "negotiate failed");
716 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
721 /* alloc default buffer when we can't get one from downstream */
722 GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
727 /* alloc default buffer when we can't get one from downstream */
728 GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
729 gst_buffer_replace (&dframe->buffer, NULL);
735 gboolean first = TRUE;
736 int ret = avcodec_default_get_buffer (context, picture);
738 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
739 ffmpegdec->stride[c] = picture->linesize[c];
741 if (picture->buf[c] == NULL && first) {
743 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
752 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
753 gst_video_codec_frame_unref (frame);
758 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
763 /* this should havesame effect as _get_buffer wrt opaque metadata,
764 * but preserving current content, if any */
766 gst_ffmpegviddec_reget_buffer (AVCodecContext * context, AVFrame * picture)
768 GstVideoCodecFrame *frame;
769 GstFFMpegVidDecVideoFrame *dframe;
770 GstFFMpegVidDec *ffmpegdec;
772 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
774 GST_DEBUG_OBJECT (ffmpegdec, "regetting buffer picture %p", picture);
776 picture->reordered_opaque = context->reordered_opaque;
778 /* if there is no opaque, we didn't yet attach any frame to it. What usually
779 * happens is that avcodec_default_reget_buffer will call the getbuffer
781 dframe = picture->opaque;
786 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
787 picture->reordered_opaque);
788 if (G_UNLIKELY (frame == NULL))
791 if (G_UNLIKELY (frame->output_buffer != NULL))
792 goto duplicate_frame;
794 /* replace the frame, this one contains the pts/dts for the correspoding input
795 * buffer, which we need after decoding. */
796 gst_video_codec_frame_unref (dframe->frame);
797 dframe->frame = frame;
800 return avcodec_default_reget_buffer (context, picture);
805 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
810 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
815 /* called when ffmpeg is done with our buffer */
817 gst_ffmpegviddec_release_buffer (AVCodecContext * context, AVFrame * picture)
820 GstFFMpegVidDecVideoFrame *frame;
821 GstFFMpegVidDec *ffmpegdec;
823 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
824 frame = (GstFFMpegVidDecVideoFrame *) picture->opaque;
825 GST_DEBUG_OBJECT (ffmpegdec, "release frame SN %d",
826 frame->frame->system_frame_number);
828 /* check if it was our buffer */
829 if (picture->type != FF_BUFFER_TYPE_USER) {
830 GST_DEBUG_OBJECT (ffmpegdec, "default release buffer");
831 avcodec_default_release_buffer (context, picture);
834 /* we remove the opaque data now */
835 picture->opaque = NULL;
837 gst_ffmpegviddec_video_frame_free (ffmpegdec, frame);
839 /* zero out the reference in ffmpeg */
840 for (i = 0; i < 4; i++) {
841 picture->data[i] = NULL;
842 picture->linesize[i] = 0;
847 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
850 if (!force && ffmpegdec->ctx_width == context->width
851 && ffmpegdec->ctx_height == context->height
852 && ffmpegdec->ctx_ticks == context->ticks_per_frame
853 && ffmpegdec->ctx_time_n == context->time_base.num
854 && ffmpegdec->ctx_time_d == context->time_base.den
855 && ffmpegdec->ctx_pix_fmt == context->pix_fmt
856 && ffmpegdec->ctx_par_n == context->sample_aspect_ratio.num
857 && ffmpegdec->ctx_par_d == context->sample_aspect_ratio.den)
860 GST_DEBUG_OBJECT (ffmpegdec,
861 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
862 ffmpegdec->ctx_width, ffmpegdec->ctx_height,
863 ffmpegdec->ctx_par_n, ffmpegdec->ctx_par_d,
864 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
865 context->width, context->height,
866 context->sample_aspect_ratio.num,
867 context->sample_aspect_ratio.den,
868 context->time_base.num, context->time_base.den, context->pix_fmt);
870 ffmpegdec->ctx_width = context->width;
871 ffmpegdec->ctx_height = context->height;
872 ffmpegdec->ctx_ticks = context->ticks_per_frame;
873 ffmpegdec->ctx_time_n = context->time_base.num;
874 ffmpegdec->ctx_time_d = context->time_base.den;
875 ffmpegdec->ctx_pix_fmt = context->pix_fmt;
876 ffmpegdec->ctx_par_n = context->sample_aspect_ratio.num;
877 ffmpegdec->ctx_par_d = context->sample_aspect_ratio.den;
883 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
884 GstVideoInfo * in_info, GstVideoInfo * out_info)
886 gboolean demuxer_par_set = FALSE;
887 gboolean decoder_par_set = FALSE;
888 gint demuxer_num = 1, demuxer_denom = 1;
889 gint decoder_num = 1, decoder_denom = 1;
891 if (in_info->par_n && in_info->par_d) {
892 demuxer_num = in_info->par_n;
893 demuxer_denom = in_info->par_d;
894 demuxer_par_set = TRUE;
895 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
899 if (ffmpegdec->ctx_par_n && ffmpegdec->ctx_par_d) {
900 decoder_num = ffmpegdec->ctx_par_n;
901 decoder_denom = ffmpegdec->ctx_par_d;
902 decoder_par_set = TRUE;
903 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
907 if (!demuxer_par_set && !decoder_par_set)
910 if (demuxer_par_set && !decoder_par_set)
911 goto use_demuxer_par;
913 if (decoder_par_set && !demuxer_par_set)
914 goto use_decoder_par;
916 /* Both the demuxer and the decoder provide a PAR. If one of
917 * the two PARs is 1:1 and the other one is not, use the one
918 * that is not 1:1. */
919 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
920 goto use_decoder_par;
922 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
923 goto use_demuxer_par;
925 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
926 goto use_demuxer_par;
930 GST_DEBUG_OBJECT (ffmpegdec,
931 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
933 out_info->par_n = decoder_num;
934 out_info->par_d = decoder_denom;
939 GST_DEBUG_OBJECT (ffmpegdec,
940 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
942 out_info->par_n = demuxer_num;
943 out_info->par_d = demuxer_denom;
948 GST_DEBUG_OBJECT (ffmpegdec,
949 "Neither demuxer nor codec provide a pixel-aspect-ratio");
957 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
958 AVCodecContext * context, gboolean force)
961 GstVideoInfo *in_info, *out_info;
962 GstVideoCodecState *output_state;
965 if (!update_video_context (ffmpegdec, context, force))
968 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->ctx_pix_fmt);
969 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
973 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
974 ffmpegdec->ctx_width, ffmpegdec->ctx_height, ffmpegdec->input_state);
975 if (ffmpegdec->output_state)
976 gst_video_codec_state_unref (ffmpegdec->output_state);
977 ffmpegdec->output_state = output_state;
979 in_info = &ffmpegdec->input_state->info;
980 out_info = &ffmpegdec->output_state->info;
982 /* set the interlaced flag */
983 if (ffmpegdec->ctx_interlaced)
984 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
986 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
988 switch (context->chroma_sample_location) {
990 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
993 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
996 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
999 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1005 /* try to find a good framerate */
1006 if ((in_info->fps_d && in_info->fps_n) ||
1007 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1008 /* take framerate from input when it was specified (#313970) */
1009 fps_n = in_info->fps_n;
1010 fps_d = in_info->fps_d;
1012 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1013 fps_d = ffmpegdec->ctx_time_n;
1016 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1020 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1021 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1027 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1028 out_info->fps_n = fps_n;
1029 out_info->fps_d = fps_d;
1031 /* calculate and update par now */
1032 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1034 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1035 goto negotiate_failed;
1042 GST_ERROR_OBJECT (ffmpegdec,
1043 "decoder requires a video format unsupported by GStreamer");
1048 /* Reset so we try again next time even if force==FALSE */
1049 ffmpegdec->ctx_width = 0;
1050 ffmpegdec->ctx_height = 0;
1051 ffmpegdec->ctx_ticks = 0;
1052 ffmpegdec->ctx_time_n = 0;
1053 ffmpegdec->ctx_time_d = 0;
1054 ffmpegdec->ctx_pix_fmt = 0;
1055 ffmpegdec->ctx_par_n = 0;
1056 ffmpegdec->ctx_par_d = 0;
1058 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1063 /* perform qos calculations before decoding the next frame.
1065 * Sets the skip_frame flag and if things are really bad, skips to the next
1068 * Returns TRUE if the frame should be decoded, FALSE if the frame can be dropped
1072 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1073 GstVideoCodecFrame * frame, gboolean * mode_switch)
1075 GstClockTimeDiff diff;
1077 *mode_switch = FALSE;
1083 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1086 /* if we don't have timing info, then we don't do QoS */
1087 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (diff)))
1090 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" GST_TIME_FORMAT,
1091 GST_TIME_ARGS (diff));
1105 if (ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1106 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1107 *mode_switch = TRUE;
1108 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1114 if (ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1115 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1116 *mode_switch = TRUE;
1117 GST_DEBUG_OBJECT (ffmpegdec,
1118 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1124 /* get an outbuf buffer with the current picture */
1125 static GstFlowReturn
1126 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1128 GstFlowReturn ret = GST_FLOW_OK;
1129 AVPicture pic, *outpic;
1130 GstVideoFrame vframe;
1134 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1137 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1139 if (G_UNLIKELY (ret != GST_FLOW_OK))
1142 /* original ffmpeg code does not handle odd sizes correctly.
1143 * This patched up version does */
1144 /* Fill avpicture */
1145 info = &ffmpegdec->output_state->info;
1146 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1147 GST_MAP_READ | GST_MAP_WRITE))
1150 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1151 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1152 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1153 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1156 pic.linesize[c] = 0;
1158 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", pic.linesize[c],
1162 outpic = (AVPicture *) ffmpegdec->picture;
1164 av_picture_copy (&pic, outpic, ffmpegdec->context->pix_fmt,
1165 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
1167 gst_video_frame_unmap (&vframe);
1169 ffmpegdec->picture->reordered_opaque = -1;
1176 GST_DEBUG_OBJECT (ffmpegdec, "pad_alloc failed");
1182 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1184 memset (packet, 0, sizeof (AVPacket));
1185 packet->data = data;
1186 packet->size = size;
1189 /* gst_ffmpegviddec_[video|audio]_frame:
1191 * data: pointer to the data to decode
1192 * size: size of data in bytes
1193 * in_timestamp: incoming timestamp.
1194 * in_duration: incoming duration.
1195 * in_offset: incoming offset (frame number).
1198 * Returns: number of bytes used in decoding. The check for successful decode is
1199 * outbuf being non-NULL.
1202 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1203 guint8 * data, guint size, GstVideoCodecFrame * frame, GstFlowReturn * ret)
1207 gboolean mode_switch;
1209 gint skip_frame = AVDISCARD_DEFAULT;
1210 GstVideoCodecFrame *out_frame;
1211 GstFFMpegVidDecVideoFrame *out_dframe;
1216 /* in case we skip frames */
1217 ffmpegdec->picture->pict_type = -1;
1219 /* run QoS code, we don't stop decoding the frame when we are late because
1220 * else we might skip a reference frame */
1221 decode = gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1223 if (ffmpegdec->is_realvideo && data != NULL) {
1227 /* setup the slice table for realvideo */
1228 if (ffmpegdec->context->slice_offset == NULL)
1229 ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1231 slice_count = (*data++) + 1;
1232 ffmpegdec->context->slice_count = slice_count;
1234 for (i = 0; i < slice_count; i++) {
1236 ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1242 /* no decoding needed, save previous skip_frame value and brutely skip
1243 * decoding everything */
1244 skip_frame = ffmpegdec->context->skip_frame;
1245 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1249 /* save reference to the timing info */
1250 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1251 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1253 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1254 frame->system_frame_number);
1257 /* now decode the frame */
1258 gst_avpacket_init (&packet, data, size);
1260 if (ffmpegdec->palette) {
1263 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1265 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1266 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1269 len = avcodec_decode_video2 (ffmpegdec->context,
1270 ffmpegdec->picture, &have_data, &packet);
1272 /* restore previous state */
1274 ffmpegdec->context->skip_frame = skip_frame;
1276 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1279 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1280 * no data because we told it to skip stuff. */
1281 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1284 /* no data, we're done */
1285 if (len < 0 || have_data <= 0)
1288 /* get the output picture timing info again */
1289 out_dframe = ffmpegdec->picture->opaque;
1290 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1292 /* also give back a buffer allocated by the frame, if any */
1293 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1294 gst_buffer_replace (&out_dframe->buffer, NULL);
1296 GST_DEBUG_OBJECT (ffmpegdec,
1297 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1298 out_frame->pts, out_frame->duration);
1299 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1300 (guint64) ffmpegdec->picture->pts);
1301 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1302 ffmpegdec->picture->coded_picture_number);
1303 GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1304 ffmpegdec->picture->reference);
1305 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1306 ffmpegdec->picture->display_picture_number);
1307 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1308 ffmpegdec->picture->opaque);
1309 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1310 (guint64) ffmpegdec->picture->reordered_opaque);
1311 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1312 ffmpegdec->picture->repeat_pict);
1313 GST_DEBUG_OBJECT (ffmpegdec, "interlaced_frame:%d (current:%d)",
1314 ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1315 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1316 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1318 if (G_UNLIKELY (ffmpegdec->picture->interlaced_frame !=
1319 ffmpegdec->ctx_interlaced)) {
1320 GST_WARNING ("Change in interlacing ! picture:%d, recorded:%d",
1321 ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1322 ffmpegdec->ctx_interlaced = ffmpegdec->picture->interlaced_frame;
1323 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context, TRUE))
1324 goto negotiation_error;
1327 if (G_UNLIKELY (out_frame->output_buffer == NULL))
1328 *ret = get_output_buffer (ffmpegdec, out_frame);
1330 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1333 /* Mark corrupted frames as corrupted */
1334 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1335 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1337 if (ffmpegdec->ctx_interlaced) {
1338 /* set interlaced flags */
1339 if (ffmpegdec->picture->repeat_pict)
1340 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1341 if (ffmpegdec->picture->top_field_first)
1342 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1343 if (ffmpegdec->picture->interlaced_frame)
1344 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1345 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1349 /* so we decoded this frame, frames preceding it in decoding order
1350 * that still do not have a buffer allocated seem rather useless,
1351 * and can be discarded, due to e.g. misparsed bogus frame
1352 * or non-keyframe in skipped decoding, ...
1353 * In any case, not likely to be seen again, so discard those,
1354 * before they pile up and/or mess with timestamping */
1357 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1358 gboolean old = TRUE;
1360 ol = l = gst_video_decoder_get_frames (dec);
1362 GstVideoCodecFrame *tmp = l->data;
1367 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1368 GST_LOG_OBJECT (dec,
1369 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1370 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1371 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1372 /* drop extra ref and remove from frame list */
1373 gst_video_decoder_release_frame (dec, tmp);
1375 /* drop extra ref we got */
1376 gst_video_codec_frame_unref (tmp);
1384 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1387 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1388 gst_flow_get_name (*ret), len);
1394 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1395 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1402 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1403 *ret = GST_FLOW_NOT_NEGOTIATED;
1409 /* gst_ffmpegviddec_frame:
1411 * data: pointer to the data to decode
1412 * size: size of data in bytes
1413 * got_data: 0 if no data was decoded, != 0 otherwise.
1414 * in_time: timestamp of data
1415 * in_duration: duration of data
1416 * ret: GstFlowReturn to return in the chain function
1418 * Decode the given frame and pushes it downstream.
1420 * Returns: Number of bytes used in decoding, -1 on error/failure.
1424 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1425 guint8 * data, guint size, gint * got_data, GstVideoCodecFrame * frame,
1426 GstFlowReturn * ret)
1428 GstFFMpegVidDecClass *oclass;
1429 gint have_data = 0, len = 0;
1431 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1434 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1437 ffmpegdec->context->frame_number++;
1439 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1441 len = gst_ffmpegviddec_video_frame (ffmpegdec, data, size, frame, ret);
1443 if (frame && frame->output_buffer)
1446 if (len < 0 || have_data < 0) {
1447 GST_WARNING_OBJECT (ffmpegdec,
1448 "avdec_%s: decoding error (len: %d, have_data: %d)",
1449 oclass->in_plugin->name, len, have_data);
1453 if (len == 0 && have_data == 0) {
1458 /* this is where I lost my last clue on ffmpeg... */
1467 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1468 *ret = GST_FLOW_NOT_NEGOTIATED;
1474 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1476 GstFFMpegVidDecClass *oclass;
1478 if (!ffmpegdec->opened)
1481 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1483 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1484 gint have_data, len, try = 0;
1486 GST_LOG_OBJECT (ffmpegdec,
1487 "codec has delay capabilities, calling until ffmpeg has drained everything");
1492 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1493 if (len < 0 || have_data == 0)
1495 } while (try++ < 10);
1499 static GstFlowReturn
1500 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1501 GstVideoCodecFrame * frame)
1503 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1504 guint8 *data, *bdata;
1505 gint size, len, have_data, bsize;
1507 GstFlowReturn ret = GST_FLOW_OK;
1508 gboolean do_padding;
1510 GST_LOG_OBJECT (ffmpegdec,
1511 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1512 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1513 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1514 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1516 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1517 GST_ERROR_OBJECT (ffmpegdec, "Failed to map buffer");
1518 return GST_FLOW_ERROR;
1521 /* treat frame as void until a buffer is requested for it */
1522 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1523 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1528 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1529 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1531 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1532 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1533 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1534 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1535 ffmpegdec->padded_size);
1537 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1538 "Copy input to add padding");
1539 memcpy (ffmpegdec->padded, bdata, bsize);
1540 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1542 bdata = ffmpegdec->padded;
1549 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1551 /* parse, if at all possible */
1556 /* add temporary padding */
1557 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1558 "Add temporary input padding");
1559 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1560 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1563 /* decode a frame of audio/video now */
1565 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1568 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1571 if (ret != GST_FLOW_OK) {
1572 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1573 gst_flow_get_name (ret));
1574 /* bad flow retun, make sure we discard all data and exit */
1579 if (len == 0 && !have_data) {
1580 /* nothing was decoded, this could be because no data was available or
1581 * because we were skipping frames.
1582 * If we have no context we must exit and wait for more data, we keep the
1584 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1589 /* a decoding error happened, we must break and try again with next data. */
1590 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1595 /* prepare for the next round, for codecs with a context we did this
1596 * already when using the parser. */
1602 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1604 } while (bsize > 0);
1607 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1609 gst_buffer_unmap (frame->input_buffer, &minfo);
1610 gst_video_codec_frame_unref (frame);
1617 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1619 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1620 GstFFMpegVidDecClass *oclass;
1622 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1624 GST_OBJECT_LOCK (ffmpegdec);
1625 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1626 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1627 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1628 GST_OBJECT_UNLOCK (ffmpegdec);
1631 ffmpegdec->context->opaque = ffmpegdec;
1632 GST_OBJECT_UNLOCK (ffmpegdec);
1638 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1640 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1642 GST_OBJECT_LOCK (ffmpegdec);
1643 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1644 GST_OBJECT_UNLOCK (ffmpegdec);
1645 g_free (ffmpegdec->padded);
1646 ffmpegdec->padded = NULL;
1647 ffmpegdec->padded_size = 0;
1648 if (ffmpegdec->input_state)
1649 gst_video_codec_state_unref (ffmpegdec->input_state);
1650 ffmpegdec->input_state = NULL;
1651 if (ffmpegdec->output_state)
1652 gst_video_codec_state_unref (ffmpegdec->output_state);
1653 ffmpegdec->output_state = NULL;
1655 ffmpegdec->ctx_width = 0;
1656 ffmpegdec->ctx_height = 0;
1657 ffmpegdec->ctx_ticks = 0;
1658 ffmpegdec->ctx_time_n = 0;
1659 ffmpegdec->ctx_time_d = 0;
1660 ffmpegdec->ctx_pix_fmt = 0;
1661 ffmpegdec->ctx_par_n = 0;
1662 ffmpegdec->ctx_par_d = 0;
1667 static GstFlowReturn
1668 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1670 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1672 gst_ffmpegviddec_drain (ffmpegdec);
1678 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1680 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1682 if (ffmpegdec->opened)
1683 avcodec_flush_buffers (ffmpegdec->context);
1689 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1691 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1692 GstVideoCodecState *state;
1693 GstBufferPool *pool;
1694 guint size, min, max;
1695 GstStructure *config;
1696 gboolean have_videometa, have_alignment;
1697 GstAllocator *allocator = NULL;
1698 GstAllocationParams params = { 0, 15, 0, 0, };
1700 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1704 state = gst_video_decoder_get_output_state (decoder);
1706 if (gst_query_get_n_allocation_params (query) > 0) {
1707 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1708 params.align = MAX (params.align, 15);
1710 gst_query_add_allocation_param (query, allocator, ¶ms);
1713 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1715 config = gst_buffer_pool_get_config (pool);
1716 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1717 /* we are happy with the default allocator but we would like to have 16 bytes
1718 * aligned and padded memory */
1719 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1722 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1724 gst_buffer_pool_config_add_option (config,
1725 GST_BUFFER_POOL_OPTION_VIDEO_META);
1728 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1730 /* we can only enable the alignment if downstream supports the
1732 if (have_alignment && have_videometa) {
1733 GstVideoAlignment align;
1735 gint linesize_align[4];
1739 width = GST_VIDEO_INFO_WIDTH (&state->info);
1740 height = GST_VIDEO_INFO_HEIGHT (&state->info);
1741 /* let ffmpeg find the alignment and padding */
1742 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
1746 context->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width ();
1747 /* increase the size for the padding */
1749 height += edge << 1;
1751 align.padding_top = edge;
1752 align.padding_left = edge;
1753 align.padding_right = width - GST_VIDEO_INFO_WIDTH (&state->info) - edge;
1754 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (&state->info) - edge;
1756 /* add extra padding to match libav buffer allocation sizes */
1757 align.padding_bottom++;
1759 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
1760 align.stride_align[i] =
1761 (linesize_align[i] > 0 ? linesize_align[i] - 1 : 0);
1763 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
1764 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
1765 GST_VIDEO_INFO_WIDTH (&state->info),
1766 GST_VIDEO_INFO_HEIGHT (&state->info), width, height, align.padding_top,
1767 align.padding_left, align.padding_right, align.padding_bottom,
1768 align.stride_align[0], align.stride_align[1], align.stride_align[2],
1769 align.stride_align[3]);
1771 gst_buffer_pool_config_add_option (config,
1772 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1773 gst_buffer_pool_config_set_video_alignment (config, &align);
1775 if (ffmpegdec->direct_rendering) {
1776 GstFFMpegVidDecClass *oclass;
1778 GST_DEBUG_OBJECT (ffmpegdec, "trying to enable direct rendering");
1780 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1782 if (oclass->in_plugin->capabilities & CODEC_CAP_DR1) {
1783 GST_DEBUG_OBJECT (ffmpegdec, "enabled direct rendering");
1784 ffmpegdec->current_dr = TRUE;
1786 GST_DEBUG_OBJECT (ffmpegdec, "direct rendering not supported");
1790 GST_DEBUG_OBJECT (ffmpegdec,
1791 "alignment or videometa not supported, disable direct rendering");
1793 /* disable direct rendering. This will make us use the fallback ffmpeg
1794 * picture allocation code with padding etc. We will then do the final
1795 * copy (with cropping) into a buffer from our pool */
1796 ffmpegdec->current_dr = FALSE;
1800 gst_buffer_pool_set_config (pool, config);
1802 gst_object_unref (pool);
1804 gst_object_unref (allocator);
1805 gst_video_codec_state_unref (state);
1811 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1814 GstAllocationParams params;
1816 gst_allocation_params_init (¶ms);
1817 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1819 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1820 /* we would like to have some padding so that we don't have to
1821 * memcpy. We don't suggest an allocator. */
1822 gst_query_add_allocation_param (query, NULL, ¶ms);
1824 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1829 gst_ffmpegviddec_set_property (GObject * object,
1830 guint prop_id, const GValue * value, GParamSpec * pspec)
1832 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1836 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1838 case PROP_SKIPFRAME:
1839 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1840 g_value_get_enum (value);
1842 case PROP_DIRECT_RENDERING:
1843 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1846 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1847 g_value_get_boolean (value);
1849 case PROP_MAX_THREADS:
1850 ffmpegdec->max_threads = g_value_get_int (value);
1852 case PROP_OUTPUT_CORRUPT:
1853 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1856 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1862 gst_ffmpegviddec_get_property (GObject * object,
1863 guint prop_id, GValue * value, GParamSpec * pspec)
1865 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1869 g_value_set_enum (value, ffmpegdec->context->lowres);
1871 case PROP_SKIPFRAME:
1872 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1874 case PROP_DIRECT_RENDERING:
1875 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1878 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1880 case PROP_MAX_THREADS:
1881 g_value_set_int (value, ffmpegdec->max_threads);
1883 case PROP_OUTPUT_CORRUPT:
1884 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1893 gst_ffmpegviddec_register (GstPlugin * plugin)
1895 GTypeInfo typeinfo = {
1896 sizeof (GstFFMpegVidDecClass),
1897 (GBaseInitFunc) gst_ffmpegviddec_base_init,
1899 (GClassInitFunc) gst_ffmpegviddec_class_init,
1902 sizeof (GstFFMpegVidDec),
1904 (GInstanceInitFunc) gst_ffmpegviddec_init,
1910 in_plugin = av_codec_next (NULL);
1912 GST_LOG ("Registering decoders");
1918 /* only video decoders */
1919 if (!av_codec_is_decoder (in_plugin)
1920 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1923 /* no quasi-codecs, please */
1924 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
1925 in_plugin->id == AV_CODEC_ID_V210 ||
1926 in_plugin->id == AV_CODEC_ID_V210X ||
1927 in_plugin->id == AV_CODEC_ID_R210 ||
1928 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
1929 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
1933 /* No decoders depending on external libraries (we don't build them, but
1934 * people who build against an external ffmpeg might have them.
1935 * We have native gstreamer plugins for all of those libraries anyway. */
1936 if (!strncmp (in_plugin->name, "lib", 3)) {
1938 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
1943 /* No vdpau plugins until we can figure out how to properly use them
1944 * outside of ffmpeg. */
1945 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
1947 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
1952 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
1954 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
1959 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
1961 /* no codecs for which we're GUARANTEED to have better alternatives */
1962 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
1963 /* MP1 : Use MP3 for decoding */
1964 /* MP2 : Use MP3 for decoding */
1965 /* Theora: Use libtheora based theoradec */
1966 if (!strcmp (in_plugin->name, "gif") ||
1967 !strcmp (in_plugin->name, "theora") ||
1968 !strcmp (in_plugin->name, "mpeg1video") ||
1969 strstr (in_plugin->name, "crystalhd") != NULL ||
1970 !strcmp (in_plugin->name, "ass") ||
1971 !strcmp (in_plugin->name, "srt") ||
1972 !strcmp (in_plugin->name, "pgssub") ||
1973 !strcmp (in_plugin->name, "dvdsub") ||
1974 !strcmp (in_plugin->name, "dvbsub")) {
1975 GST_LOG ("Ignoring decoder %s", in_plugin->name);
1979 /* construct the type */
1980 plugin_name = g_strdup ((gchar *) in_plugin->name);
1981 g_strdelimit (plugin_name, NULL, '_');
1982 type_name = g_strdup_printf ("avdec_%s", plugin_name);
1983 g_free (plugin_name);
1985 type = g_type_from_name (type_name);
1988 /* create the gtype now */
1990 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
1992 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
1995 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
1996 * tested and by far outperforms divxdec/xviddec - so we prefer it.
1997 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
1998 * VC1/WMV3 are not working and thus unpreferred for now. */
1999 switch (in_plugin->id) {
2000 case AV_CODEC_ID_MPEG4:
2001 case AV_CODEC_ID_MSMPEG4V3:
2002 case AV_CODEC_ID_H264:
2003 case AV_CODEC_ID_HEVC:
2004 case AV_CODEC_ID_RV10:
2005 case AV_CODEC_ID_RV20:
2006 case AV_CODEC_ID_RV30:
2007 case AV_CODEC_ID_RV40:
2008 rank = GST_RANK_PRIMARY;
2010 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
2011 * They say libdv's quality is better though. leave as secondary.
2012 * note: if you change this, see the code in gstdv.c in good/ext/dv.
2014 case AV_CODEC_ID_DVVIDEO:
2015 rank = GST_RANK_SECONDARY;
2018 rank = GST_RANK_MARGINAL;
2021 if (!gst_element_register (plugin, type_name, rank, type)) {
2022 g_warning ("Failed to register %s", type_name);
2030 in_plugin = av_codec_next (in_plugin);
2033 GST_LOG ("Finished Registering decoders");