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
57 PROP_DIRECT_RENDERING,
64 /* A number of function prototypes are given so we can refer to them later. */
65 static void gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass);
66 static void gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass);
67 static void gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec);
68 static void gst_ffmpegviddec_finalize (GObject * object);
70 static gboolean gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
71 GstVideoCodecState * state);
72 static GstFlowReturn gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
73 GstVideoCodecFrame * frame);
74 static gboolean gst_ffmpegviddec_start (GstVideoDecoder * decoder);
75 static gboolean gst_ffmpegviddec_stop (GstVideoDecoder * decoder);
76 static gboolean gst_ffmpegviddec_flush (GstVideoDecoder * decoder);
77 static gboolean gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder,
79 static gboolean gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
82 static void gst_ffmpegviddec_set_property (GObject * object,
83 guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_ffmpegviddec_get_property (GObject * object,
85 guint prop_id, GValue * value, GParamSpec * pspec);
87 static gboolean gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
88 AVCodecContext * context, gboolean force);
90 /* some sort of bufferpool handling, but different */
91 static int gst_ffmpegviddec_get_buffer (AVCodecContext * context,
93 static int gst_ffmpegviddec_reget_buffer (AVCodecContext * context,
95 static void gst_ffmpegviddec_release_buffer (AVCodecContext * context,
98 static GstFlowReturn gst_ffmpegviddec_finish (GstVideoDecoder * decoder);
99 static void gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec);
101 #define GST_FFDEC_PARAMS_QDATA g_quark_from_static_string("avdec-params")
103 static GstElementClass *parent_class = NULL;
105 #define GST_FFMPEGVIDDEC_TYPE_LOWRES (gst_ffmpegviddec_lowres_get_type())
107 gst_ffmpegviddec_lowres_get_type (void)
109 static GType ffmpegdec_lowres_type = 0;
111 if (!ffmpegdec_lowres_type) {
112 static const GEnumValue ffmpegdec_lowres[] = {
114 {1, "1", "1/2-size"},
115 {2, "2", "1/4-size"},
119 ffmpegdec_lowres_type =
120 g_enum_register_static ("GstLibAVVidDecLowres", ffmpegdec_lowres);
123 return ffmpegdec_lowres_type;
126 #define GST_FFMPEGVIDDEC_TYPE_SKIPFRAME (gst_ffmpegviddec_skipframe_get_type())
128 gst_ffmpegviddec_skipframe_get_type (void)
130 static GType ffmpegdec_skipframe_type = 0;
132 if (!ffmpegdec_skipframe_type) {
133 static const GEnumValue ffmpegdec_skipframe[] = {
134 {0, "0", "Skip nothing"},
135 {1, "1", "Skip B-frames"},
136 {2, "2", "Skip IDCT/Dequantization"},
137 {5, "5", "Skip everything"},
141 ffmpegdec_skipframe_type =
142 g_enum_register_static ("GstLibAVVidDecSkipFrame", ffmpegdec_skipframe);
145 return ffmpegdec_skipframe_type;
149 gst_ffmpegviddec_base_init (GstFFMpegVidDecClass * klass)
151 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
152 GstPadTemplate *sinktempl, *srctempl;
153 GstCaps *sinkcaps, *srccaps;
155 gchar *longname, *description;
158 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
159 GST_FFDEC_PARAMS_QDATA);
160 g_assert (in_plugin != NULL);
162 /* construct the element details struct */
163 longname = g_strdup_printf ("libav %s decoder", in_plugin->long_name);
164 description = g_strdup_printf ("libav %s decoder", in_plugin->name);
165 gst_element_class_set_metadata (element_class, longname,
166 "Codec/Decoder/Video", description,
167 "Wim Taymans <wim.taymans@gmail.com>, "
168 "Ronald Bultje <rbultje@ronald.bitfreak.net>, "
169 "Edward Hervey <bilboed@bilboed.com>");
171 g_free (description);
174 sinkcaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, FALSE);
176 GST_DEBUG ("Couldn't get sink caps for decoder '%s'", in_plugin->name);
177 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
179 srccaps = gst_ffmpeg_codectype_to_video_caps (NULL,
180 in_plugin->id, FALSE, in_plugin);
182 GST_DEBUG ("Couldn't get source caps for decoder '%s'", in_plugin->name);
183 srccaps = gst_caps_from_string ("video/x-raw");
187 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
188 GST_PAD_ALWAYS, sinkcaps);
189 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
191 gst_element_class_add_pad_template (element_class, srctempl);
192 gst_element_class_add_pad_template (element_class, sinktempl);
194 klass->in_plugin = in_plugin;
198 gst_ffmpegviddec_class_init (GstFFMpegVidDecClass * klass)
200 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
201 GstVideoDecoderClass *viddec_class = GST_VIDEO_DECODER_CLASS (klass);
204 parent_class = g_type_class_peek_parent (klass);
206 gobject_class->finalize = gst_ffmpegviddec_finalize;
208 gobject_class->set_property = gst_ffmpegviddec_set_property;
209 gobject_class->get_property = gst_ffmpegviddec_get_property;
211 g_object_class_install_property (gobject_class, PROP_SKIPFRAME,
212 g_param_spec_enum ("skip-frame", "Skip frames",
213 "Which types of frames to skip during decoding",
214 GST_FFMPEGVIDDEC_TYPE_SKIPFRAME, 0,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_LOWRES,
217 g_param_spec_enum ("lowres", "Low resolution",
218 "At which resolution to decode images",
219 GST_FFMPEGVIDDEC_TYPE_LOWRES, 0,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_DIRECT_RENDERING,
222 g_param_spec_boolean ("direct-rendering", "Direct Rendering",
223 "Enable direct rendering", DEFAULT_DIRECT_RENDERING,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_DEBUG_MV,
226 g_param_spec_boolean ("debug-mv", "Debug motion vectors",
227 "Whether libav should print motion vectors on top of the image",
228 DEFAULT_DEBUG_MV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_OUTPUT_CORRUPT,
230 g_param_spec_boolean ("output-corrupt", "Output corrupt buffers",
231 "Whether libav should output frames even if corrupted",
232 DEFAULT_OUTPUT_CORRUPT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 caps = klass->in_plugin->capabilities;
235 if (caps & (CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS)) {
236 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_THREADS,
237 g_param_spec_int ("max-threads", "Maximum decode threads",
238 "Maximum number of worker threads to spawn. (0 = auto)",
239 0, G_MAXINT, DEFAULT_MAX_THREADS,
240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 viddec_class->set_format = gst_ffmpegviddec_set_format;
244 viddec_class->handle_frame = gst_ffmpegviddec_handle_frame;
245 viddec_class->start = gst_ffmpegviddec_start;
246 viddec_class->stop = gst_ffmpegviddec_stop;
247 viddec_class->flush = gst_ffmpegviddec_flush;
248 viddec_class->finish = gst_ffmpegviddec_finish;
249 viddec_class->decide_allocation = gst_ffmpegviddec_decide_allocation;
250 viddec_class->propose_allocation = gst_ffmpegviddec_propose_allocation;
254 gst_ffmpegviddec_init (GstFFMpegVidDec * ffmpegdec)
256 GstFFMpegVidDecClass *klass =
257 (GstFFMpegVidDecClass *) G_OBJECT_GET_CLASS (ffmpegdec);
259 /* some ffmpeg data */
260 ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin);
261 ffmpegdec->context->opaque = ffmpegdec;
262 ffmpegdec->picture = avcodec_alloc_frame ();
263 ffmpegdec->opened = FALSE;
264 ffmpegdec->skip_frame = ffmpegdec->lowres = 0;
265 ffmpegdec->direct_rendering = DEFAULT_DIRECT_RENDERING;
266 ffmpegdec->debug_mv = DEFAULT_DEBUG_MV;
267 ffmpegdec->max_threads = DEFAULT_MAX_THREADS;
268 ffmpegdec->output_corrupt = DEFAULT_OUTPUT_CORRUPT;
270 gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (ffmpegdec), TRUE);
274 gst_ffmpegviddec_finalize (GObject * object)
276 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
278 if (ffmpegdec->context != NULL) {
279 gst_ffmpeg_avcodec_close (ffmpegdec->context);
280 av_free (ffmpegdec->context);
281 ffmpegdec->context = NULL;
284 avcodec_free_frame (&ffmpegdec->picture);
286 G_OBJECT_CLASS (parent_class)->finalize (object);
290 gst_ffmpegviddec_context_set_flags (AVCodecContext * context, guint flags,
293 g_return_if_fail (context != NULL);
296 context->flags |= flags;
298 context->flags &= ~flags;
303 gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
305 GstFFMpegVidDecClass *oclass;
308 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
310 GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
312 gst_caps_replace (&ffmpegdec->last_caps, NULL);
314 gst_ffmpeg_avcodec_close (ffmpegdec->context);
315 ffmpegdec->opened = FALSE;
317 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
318 ffmpegdec->stride[i] = -1;
320 gst_buffer_replace (&ffmpegdec->palette, NULL);
322 if (ffmpegdec->context->extradata) {
323 av_free (ffmpegdec->context->extradata);
324 ffmpegdec->context->extradata = NULL;
328 if (avcodec_get_context_defaults3 (ffmpegdec->context,
329 oclass->in_plugin) < 0) {
330 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
333 ffmpegdec->context->opaque = ffmpegdec;
340 gst_ffmpegviddec_open (GstFFMpegVidDec * ffmpegdec)
342 GstFFMpegVidDecClass *oclass;
345 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
347 if (gst_ffmpeg_avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
350 for (i = 0; i < G_N_ELEMENTS (ffmpegdec->stride); i++)
351 ffmpegdec->stride[i] = -1;
353 ffmpegdec->opened = TRUE;
354 ffmpegdec->is_realvideo = FALSE;
356 GST_LOG_OBJECT (ffmpegdec, "Opened libav codec %s, id %d",
357 oclass->in_plugin->name, oclass->in_plugin->id);
359 switch (oclass->in_plugin->id) {
360 case AV_CODEC_ID_RV10:
361 case AV_CODEC_ID_RV30:
362 case AV_CODEC_ID_RV20:
363 case AV_CODEC_ID_RV40:
364 ffmpegdec->is_realvideo = TRUE;
367 GST_LOG_OBJECT (ffmpegdec, "Parser deactivated for format");
371 gst_ffmpegviddec_context_set_flags (ffmpegdec->context,
372 CODEC_FLAG_OUTPUT_CORRUPT, ffmpegdec->output_corrupt);
379 gst_ffmpegviddec_close (ffmpegdec, TRUE);
380 GST_DEBUG_OBJECT (ffmpegdec, "avdec_%s: Failed to open libav codec",
381 oclass->in_plugin->name);
387 gst_ffmpegviddec_get_palette (GstFFMpegVidDec * ffmpegdec,
388 GstVideoCodecState * state)
390 GstStructure *str = gst_caps_get_structure (state->caps, 0);
391 const GValue *palette_v;
394 /* do we have a palette? */
395 if ((palette_v = gst_structure_get_value (str, "palette_data"))) {
396 palette = gst_value_get_buffer (palette_v);
397 GST_DEBUG ("got palette data %p", palette);
398 if (gst_buffer_get_size (palette) >= AVPALETTE_SIZE) {
399 gst_buffer_replace (&ffmpegdec->palette, palette);
406 gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
407 GstVideoCodecState * state)
409 GstFFMpegVidDec *ffmpegdec;
410 GstFFMpegVidDecClass *oclass;
411 GstClockTime latency = GST_CLOCK_TIME_NONE;
412 gboolean ret = FALSE;
414 ffmpegdec = (GstFFMpegVidDec *) decoder;
415 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
417 if (ffmpegdec->last_caps != NULL &&
418 gst_caps_is_equal (ffmpegdec->last_caps, state->caps)) {
422 GST_DEBUG_OBJECT (ffmpegdec, "setcaps called");
424 GST_OBJECT_LOCK (ffmpegdec);
425 /* stupid check for VC1 */
426 if ((oclass->in_plugin->id == AV_CODEC_ID_WMV3) ||
427 (oclass->in_plugin->id == AV_CODEC_ID_VC1))
428 oclass->in_plugin->id = gst_ffmpeg_caps_to_codecid (state->caps, NULL);
430 /* close old session */
431 if (ffmpegdec->opened) {
432 GST_OBJECT_UNLOCK (ffmpegdec);
433 gst_ffmpegviddec_drain (ffmpegdec);
434 GST_OBJECT_LOCK (ffmpegdec);
435 if (!gst_ffmpegviddec_close (ffmpegdec, TRUE)) {
436 GST_OBJECT_UNLOCK (ffmpegdec);
441 gst_caps_replace (&ffmpegdec->last_caps, state->caps);
443 /* set buffer functions */
444 ffmpegdec->context->get_buffer = gst_ffmpegviddec_get_buffer;
445 ffmpegdec->context->reget_buffer = gst_ffmpegviddec_reget_buffer;
446 ffmpegdec->context->release_buffer = gst_ffmpegviddec_release_buffer;
447 ffmpegdec->context->draw_horiz_band = NULL;
449 /* reset coded_width/_height to prevent it being reused from last time when
450 * the codec is opened again, causing a mismatch and possible
451 * segfault/corruption. (Common scenario when renegotiating caps) */
452 ffmpegdec->context->coded_width = 0;
453 ffmpegdec->context->coded_height = 0;
455 GST_LOG_OBJECT (ffmpegdec, "size %dx%d", ffmpegdec->context->width,
456 ffmpegdec->context->height);
458 /* FIXME : Create a method that takes GstVideoCodecState instead */
459 /* get size and so */
460 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
461 oclass->in_plugin->type, state->caps, ffmpegdec->context);
463 GST_LOG_OBJECT (ffmpegdec, "size after %dx%d", ffmpegdec->context->width,
464 ffmpegdec->context->height);
466 gst_ffmpegviddec_get_palette (ffmpegdec, state);
468 if (!ffmpegdec->context->time_base.den || !ffmpegdec->context->time_base.num) {
469 GST_DEBUG_OBJECT (ffmpegdec, "forcing 25/1 framerate");
470 ffmpegdec->context->time_base.num = 1;
471 ffmpegdec->context->time_base.den = 25;
474 /* workaround encoder bugs */
475 ffmpegdec->context->workaround_bugs |= FF_BUG_AUTODETECT;
476 ffmpegdec->context->err_recognition = 1;
479 ffmpegdec->context->lowres = ffmpegdec->lowres;
480 ffmpegdec->context->skip_frame = ffmpegdec->skip_frame;
482 /* ffmpeg can draw motion vectors on top of the image (not every decoder
484 ffmpegdec->context->debug_mv = ffmpegdec->debug_mv;
490 if (ffmpegdec->max_threads == 0) {
491 if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
492 ffmpegdec->context->thread_count = gst_ffmpeg_auto_max_threads ();
494 ffmpegdec->context->thread_count = 0;
496 ffmpegdec->context->thread_count = ffmpegdec->max_threads;
498 query = gst_query_new_latency ();
500 /* Check if upstream is live. If it isn't we can enable frame based
501 * threading, which is adding latency */
502 if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec), query)) {
503 gst_query_parse_latency (query, &is_live, NULL, NULL);
505 gst_query_unref (query);
508 ffmpegdec->context->thread_type = FF_THREAD_SLICE;
510 ffmpegdec->context->thread_type = FF_THREAD_SLICE | FF_THREAD_FRAME;
513 /* open codec - we don't select an output pix_fmt yet,
514 * simply because we don't know! We only get it
515 * during playback... */
516 if (!gst_ffmpegviddec_open (ffmpegdec))
519 if (ffmpegdec->input_state)
520 gst_video_codec_state_unref (ffmpegdec->input_state);
521 ffmpegdec->input_state = gst_video_codec_state_ref (state);
523 if (ffmpegdec->input_state->info.fps_n) {
524 GstVideoInfo *info = &ffmpegdec->input_state->info;
525 latency = gst_util_uint64_scale_ceil (
526 (ffmpegdec->context->has_b_frames) * GST_SECOND, info->fps_d,
533 GST_OBJECT_UNLOCK (ffmpegdec);
535 if (GST_CLOCK_TIME_IS_VALID (latency))
536 gst_video_decoder_set_latency (decoder, latency, latency);
543 GST_DEBUG_OBJECT (ffmpegdec, "Failed to open");
550 GstFFMpegVidDec *ffmpegdec;
551 GstVideoCodecFrame *frame;
553 GstVideoFrame vframe;
555 } GstFFMpegVidDecVideoFrame;
557 static GstFFMpegVidDecVideoFrame *
558 gst_ffmpegviddec_video_frame_new (GstFFMpegVidDec * ffmpegdec,
559 GstVideoCodecFrame * frame)
561 GstFFMpegVidDecVideoFrame *dframe;
563 dframe = g_slice_new0 (GstFFMpegVidDecVideoFrame);
564 dframe->ffmpegdec = ffmpegdec;
565 dframe->frame = frame;
567 GST_DEBUG_OBJECT (ffmpegdec, "new video frame %p", dframe);
573 gst_ffmpegviddec_video_frame_free (GstFFMpegVidDec * ffmpegdec,
574 GstFFMpegVidDecVideoFrame * frame)
576 GST_DEBUG_OBJECT (ffmpegdec, "free video frame %p", frame);
579 gst_video_frame_unmap (&frame->vframe);
580 gst_video_decoder_release_frame (GST_VIDEO_DECODER (ffmpegdec), frame->frame);
581 gst_buffer_replace (&frame->buffer, NULL);
582 g_slice_free (GstFFMpegVidDecVideoFrame, frame);
586 dummy_free_buffer (void *opaque, uint8_t * data)
588 GstFFMpegVidDecVideoFrame *frame = opaque;
590 gst_ffmpegviddec_video_frame_free (frame->ffmpegdec, frame);
593 /* called when ffmpeg wants us to allocate a buffer to write the decoded frame
594 * into. We try to give it memory from our pool */
596 gst_ffmpegviddec_get_buffer (AVCodecContext * context, AVFrame * picture)
598 GstVideoCodecFrame *frame;
599 GstFFMpegVidDecVideoFrame *dframe;
600 GstFFMpegVidDec *ffmpegdec;
605 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
607 GST_DEBUG_OBJECT (ffmpegdec, "getting buffer picture %p", picture);
609 /* apply the last info we have seen to this picture, when we get the
610 * picture back from ffmpeg we can use this to correctly timestamp the output
612 picture->reordered_opaque = context->reordered_opaque;
613 GST_DEBUG_OBJECT (ffmpegdec, "opaque value SN %d",
614 (gint32) picture->reordered_opaque);
617 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
618 picture->reordered_opaque);
619 if (G_UNLIKELY (frame == NULL))
622 /* now it has a buffer allocated, so it is real and will also
624 GST_VIDEO_CODEC_FRAME_FLAG_UNSET (frame,
625 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
627 if (G_UNLIKELY (frame->output_buffer != NULL))
628 goto duplicate_frame;
630 /* GstFFMpegVidDecVideoFrame receives the frame ref */
631 picture->opaque = dframe =
632 gst_ffmpegviddec_video_frame_new (ffmpegdec, frame);
634 GST_DEBUG_OBJECT (ffmpegdec, "storing opaque %p", dframe);
636 ffmpegdec->context->pix_fmt = context->pix_fmt;
638 /* see if we need renegotiation */
639 if (G_UNLIKELY (!gst_ffmpegviddec_negotiate (ffmpegdec, context, FALSE)))
640 goto negotiate_failed;
642 if (!ffmpegdec->current_dr)
646 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
648 if (ret != GST_FLOW_OK)
651 /* piggy-backed alloc'ed on the frame,
652 * and there was much rejoicing and we are grateful.
653 * Now take away buffer from frame, we will give it back later when decoded.
654 * This allows multiple request for a buffer per frame; unusual but possible. */
655 gst_buffer_replace (&dframe->buffer, frame->output_buffer);
656 gst_buffer_replace (&frame->output_buffer, NULL);
659 info = &ffmpegdec->output_state->info;
660 if (!gst_video_frame_map (&dframe->vframe, info, dframe->buffer,
663 dframe->mapped = TRUE;
665 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
666 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
667 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&dframe->vframe, c);
668 picture->linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe->vframe, c);
670 /* libav does not allow stride changes currently, fall back to
671 * non-direct rendering here:
672 * https://bugzilla.gnome.org/show_bug.cgi?id=704769
673 * https://bugzilla.libav.org/show_bug.cgi?id=556
675 if (ffmpegdec->stride[c] == -1) {
676 ffmpegdec->stride[c] = picture->linesize[c];
677 } else if (picture->linesize[c] != ffmpegdec->stride[c]) {
678 GST_LOG_OBJECT (ffmpegdec,
679 "No direct rendering, stride changed c=%d %d->%d", c,
680 ffmpegdec->stride[c], picture->linesize[c]);
682 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
683 picture->data[c] = NULL;
684 picture->linesize[c] = 0;
686 gst_video_frame_unmap (&dframe->vframe);
687 dframe->mapped = FALSE;
688 gst_buffer_replace (&dframe->buffer, NULL);
689 ffmpegdec->current_dr = FALSE;
694 picture->data[c] = NULL;
695 picture->linesize[c] = 0;
697 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", picture->linesize[c],
701 /* tell ffmpeg we own this buffer, tranfer the ref we have on the buffer to
702 * the opaque data. */
703 picture->type = FF_BUFFER_TYPE_USER;
705 GST_LOG_OBJECT (ffmpegdec, "returned frame %p", dframe->buffer);
712 GST_DEBUG_OBJECT (ffmpegdec, "negotiate failed");
717 GST_LOG_OBJECT (ffmpegdec, "direct rendering disabled, fallback alloc");
722 /* alloc default buffer when we can't get one from downstream */
723 GST_LOG_OBJECT (ffmpegdec, "alloc failed, fallback alloc");
728 /* alloc default buffer when we can't get one from downstream */
729 GST_LOG_OBJECT (ffmpegdec, "failed to map frame, fallback alloc");
730 gst_buffer_replace (&dframe->buffer, NULL);
736 gboolean first = TRUE;
737 int ret = avcodec_default_get_buffer (context, picture);
739 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
740 ffmpegdec->stride[c] = picture->linesize[c];
742 if (picture->buf[c] == NULL && first) {
744 av_buffer_create (NULL, 0, dummy_free_buffer, dframe, 0);
753 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
754 gst_video_codec_frame_unref (frame);
759 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
764 /* this should havesame effect as _get_buffer wrt opaque metadata,
765 * but preserving current content, if any */
767 gst_ffmpegviddec_reget_buffer (AVCodecContext * context, AVFrame * picture)
769 GstVideoCodecFrame *frame;
770 GstFFMpegVidDecVideoFrame *dframe;
771 GstFFMpegVidDec *ffmpegdec;
773 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
775 GST_DEBUG_OBJECT (ffmpegdec, "regetting buffer picture %p", picture);
777 picture->reordered_opaque = context->reordered_opaque;
779 /* if there is no opaque, we didn't yet attach any frame to it. What usually
780 * happens is that avcodec_default_reget_buffer will call the getbuffer
782 dframe = picture->opaque;
787 gst_video_decoder_get_frame (GST_VIDEO_DECODER (ffmpegdec),
788 picture->reordered_opaque);
789 if (G_UNLIKELY (frame == NULL))
792 if (G_UNLIKELY (frame->output_buffer != NULL))
793 goto duplicate_frame;
795 /* replace the frame, this one contains the pts/dts for the correspoding input
796 * buffer, which we need after decoding. */
797 gst_video_codec_frame_unref (dframe->frame);
798 dframe->frame = frame;
801 return avcodec_default_reget_buffer (context, picture);
806 GST_WARNING_OBJECT (ffmpegdec, "Couldn't get codec frame !");
811 GST_WARNING_OBJECT (ffmpegdec, "already alloc'ed output buffer for frame");
816 /* called when ffmpeg is done with our buffer */
818 gst_ffmpegviddec_release_buffer (AVCodecContext * context, AVFrame * picture)
821 GstFFMpegVidDecVideoFrame *frame;
822 GstFFMpegVidDec *ffmpegdec;
824 ffmpegdec = (GstFFMpegVidDec *) context->opaque;
825 frame = (GstFFMpegVidDecVideoFrame *) picture->opaque;
826 GST_DEBUG_OBJECT (ffmpegdec, "release frame SN %d",
827 frame->frame->system_frame_number);
829 /* check if it was our buffer */
830 if (picture->type != FF_BUFFER_TYPE_USER) {
831 GST_DEBUG_OBJECT (ffmpegdec, "default release buffer");
832 avcodec_default_release_buffer (context, picture);
835 /* we remove the opaque data now */
836 picture->opaque = NULL;
838 gst_ffmpegviddec_video_frame_free (ffmpegdec, frame);
840 /* zero out the reference in ffmpeg */
841 for (i = 0; i < 4; i++) {
842 picture->data[i] = NULL;
843 picture->linesize[i] = 0;
848 update_video_context (GstFFMpegVidDec * ffmpegdec, AVCodecContext * context,
851 if (!force && ffmpegdec->ctx_width == context->width
852 && ffmpegdec->ctx_height == context->height
853 && ffmpegdec->ctx_ticks == context->ticks_per_frame
854 && ffmpegdec->ctx_time_n == context->time_base.num
855 && ffmpegdec->ctx_time_d == context->time_base.den
856 && ffmpegdec->ctx_pix_fmt == context->pix_fmt
857 && ffmpegdec->ctx_par_n == context->sample_aspect_ratio.num
858 && ffmpegdec->ctx_par_d == context->sample_aspect_ratio.den)
861 GST_DEBUG_OBJECT (ffmpegdec,
862 "Renegotiating video from %dx%d@ %d:%d PAR %d/%d fps to %dx%d@ %d:%d PAR %d/%d fps pixfmt %d",
863 ffmpegdec->ctx_width, ffmpegdec->ctx_height,
864 ffmpegdec->ctx_par_n, ffmpegdec->ctx_par_d,
865 ffmpegdec->ctx_time_n, ffmpegdec->ctx_time_d,
866 context->width, context->height,
867 context->sample_aspect_ratio.num,
868 context->sample_aspect_ratio.den,
869 context->time_base.num, context->time_base.den, context->pix_fmt);
871 ffmpegdec->ctx_width = context->width;
872 ffmpegdec->ctx_height = context->height;
873 ffmpegdec->ctx_ticks = context->ticks_per_frame;
874 ffmpegdec->ctx_time_n = context->time_base.num;
875 ffmpegdec->ctx_time_d = context->time_base.den;
876 ffmpegdec->ctx_pix_fmt = context->pix_fmt;
877 ffmpegdec->ctx_par_n = context->sample_aspect_ratio.num;
878 ffmpegdec->ctx_par_d = context->sample_aspect_ratio.den;
884 gst_ffmpegviddec_update_par (GstFFMpegVidDec * ffmpegdec,
885 GstVideoInfo * in_info, GstVideoInfo * out_info)
887 gboolean demuxer_par_set = FALSE;
888 gboolean decoder_par_set = FALSE;
889 gint demuxer_num = 1, demuxer_denom = 1;
890 gint decoder_num = 1, decoder_denom = 1;
892 if (in_info->par_n && in_info->par_d) {
893 demuxer_num = in_info->par_n;
894 demuxer_denom = in_info->par_d;
895 demuxer_par_set = TRUE;
896 GST_DEBUG_OBJECT (ffmpegdec, "Demuxer PAR: %d:%d", demuxer_num,
900 if (ffmpegdec->ctx_par_n && ffmpegdec->ctx_par_d) {
901 decoder_num = ffmpegdec->ctx_par_n;
902 decoder_denom = ffmpegdec->ctx_par_d;
903 decoder_par_set = TRUE;
904 GST_DEBUG_OBJECT (ffmpegdec, "Decoder PAR: %d:%d", decoder_num,
908 if (!demuxer_par_set && !decoder_par_set)
911 if (demuxer_par_set && !decoder_par_set)
912 goto use_demuxer_par;
914 if (decoder_par_set && !demuxer_par_set)
915 goto use_decoder_par;
917 /* Both the demuxer and the decoder provide a PAR. If one of
918 * the two PARs is 1:1 and the other one is not, use the one
919 * that is not 1:1. */
920 if (demuxer_num == demuxer_denom && decoder_num != decoder_denom)
921 goto use_decoder_par;
923 if (decoder_num == decoder_denom && demuxer_num != demuxer_denom)
924 goto use_demuxer_par;
926 /* Both PARs are non-1:1, so use the PAR provided by the demuxer */
927 goto use_demuxer_par;
931 GST_DEBUG_OBJECT (ffmpegdec,
932 "Setting decoder provided pixel-aspect-ratio of %u:%u", decoder_num,
934 out_info->par_n = decoder_num;
935 out_info->par_d = decoder_denom;
940 GST_DEBUG_OBJECT (ffmpegdec,
941 "Setting demuxer provided pixel-aspect-ratio of %u:%u", demuxer_num,
943 out_info->par_n = demuxer_num;
944 out_info->par_d = demuxer_denom;
949 GST_DEBUG_OBJECT (ffmpegdec,
950 "Neither demuxer nor codec provide a pixel-aspect-ratio");
958 gst_ffmpegviddec_negotiate (GstFFMpegVidDec * ffmpegdec,
959 AVCodecContext * context, gboolean force)
962 GstVideoInfo *in_info, *out_info;
963 GstVideoCodecState *output_state;
966 if (!update_video_context (ffmpegdec, context, force))
969 fmt = gst_ffmpeg_pixfmt_to_videoformat (ffmpegdec->ctx_pix_fmt);
970 if (G_UNLIKELY (fmt == GST_VIDEO_FORMAT_UNKNOWN))
974 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (ffmpegdec), fmt,
975 ffmpegdec->ctx_width, ffmpegdec->ctx_height, ffmpegdec->input_state);
976 if (ffmpegdec->output_state)
977 gst_video_codec_state_unref (ffmpegdec->output_state);
978 ffmpegdec->output_state = output_state;
980 in_info = &ffmpegdec->input_state->info;
981 out_info = &ffmpegdec->output_state->info;
983 /* set the interlaced flag */
984 if (ffmpegdec->ctx_interlaced)
985 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
987 out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
989 switch (context->chroma_sample_location) {
991 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
994 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_JPEG;
997 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_DV;
1000 out_info->chroma_site = GST_VIDEO_CHROMA_SITE_V_COSITED;
1006 /* try to find a good framerate */
1007 if ((in_info->fps_d && in_info->fps_n) ||
1008 GST_VIDEO_INFO_FLAG_IS_SET (in_info, GST_VIDEO_FLAG_VARIABLE_FPS)) {
1009 /* take framerate from input when it was specified (#313970) */
1010 fps_n = in_info->fps_n;
1011 fps_d = in_info->fps_d;
1013 fps_n = ffmpegdec->ctx_time_d / ffmpegdec->ctx_ticks;
1014 fps_d = ffmpegdec->ctx_time_n;
1017 GST_LOG_OBJECT (ffmpegdec, "invalid framerate: %d/0, -> %d/1", fps_n,
1021 if (gst_util_fraction_compare (fps_n, fps_d, 1000, 1) > 0) {
1022 GST_LOG_OBJECT (ffmpegdec, "excessive framerate: %d/%d, -> 0/1", fps_n,
1028 GST_LOG_OBJECT (ffmpegdec, "setting framerate: %d/%d", fps_n, fps_d);
1029 out_info->fps_n = fps_n;
1030 out_info->fps_d = fps_d;
1032 /* calculate and update par now */
1033 gst_ffmpegviddec_update_par (ffmpegdec, in_info, out_info);
1035 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (ffmpegdec)))
1036 goto negotiate_failed;
1043 GST_ERROR_OBJECT (ffmpegdec,
1044 "decoder requires a video format unsupported by GStreamer");
1049 /* Reset so we try again next time even if force==FALSE */
1050 ffmpegdec->ctx_width = 0;
1051 ffmpegdec->ctx_height = 0;
1052 ffmpegdec->ctx_ticks = 0;
1053 ffmpegdec->ctx_time_n = 0;
1054 ffmpegdec->ctx_time_d = 0;
1055 ffmpegdec->ctx_pix_fmt = 0;
1056 ffmpegdec->ctx_par_n = 0;
1057 ffmpegdec->ctx_par_d = 0;
1059 GST_ERROR_OBJECT (ffmpegdec, "negotiation failed");
1064 /* perform qos calculations before decoding the next frame.
1066 * Sets the skip_frame flag and if things are really bad, skips to the next
1071 gst_ffmpegviddec_do_qos (GstFFMpegVidDec * ffmpegdec,
1072 GstVideoCodecFrame * frame, gboolean * mode_switch)
1074 GstClockTimeDiff diff;
1076 *mode_switch = FALSE;
1082 gst_video_decoder_get_max_decode_time (GST_VIDEO_DECODER (ffmpegdec),
1085 /* if we don't have timing info, then we don't do QoS */
1086 if (G_UNLIKELY (diff == G_MAXINT64))
1089 GST_DEBUG_OBJECT (ffmpegdec, "decoding time %" G_GINT64_FORMAT, diff);
1091 if (diff > 0 && ffmpegdec->context->skip_frame != AVDISCARD_DEFAULT) {
1092 ffmpegdec->context->skip_frame = AVDISCARD_DEFAULT;
1093 *mode_switch = TRUE;
1094 GST_DEBUG_OBJECT (ffmpegdec, "QOS: normal mode");
1097 else if (diff <= 0 && ffmpegdec->context->skip_frame != AVDISCARD_NONREF) {
1098 ffmpegdec->context->skip_frame = AVDISCARD_NONREF;
1099 *mode_switch = TRUE;
1100 GST_DEBUG_OBJECT (ffmpegdec,
1101 "QOS: hurry up, diff %" G_GINT64_FORMAT " >= 0", diff);
1105 /* get an outbuf buffer with the current picture */
1106 static GstFlowReturn
1107 get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
1109 GstFlowReturn ret = GST_FLOW_OK;
1110 AVPicture pic, *outpic;
1111 GstVideoFrame vframe;
1115 GST_LOG_OBJECT (ffmpegdec, "get output buffer");
1118 gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
1120 if (G_UNLIKELY (ret != GST_FLOW_OK))
1123 /* original ffmpeg code does not handle odd sizes correctly.
1124 * This patched up version does */
1125 /* Fill avpicture */
1126 info = &ffmpegdec->output_state->info;
1127 if (!gst_video_frame_map (&vframe, info, frame->output_buffer,
1128 GST_MAP_READ | GST_MAP_WRITE))
1131 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
1132 if (c < GST_VIDEO_INFO_N_PLANES (info)) {
1133 pic.data[c] = GST_VIDEO_FRAME_PLANE_DATA (&vframe, c);
1134 pic.linesize[c] = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, c);
1137 pic.linesize[c] = 0;
1139 GST_LOG_OBJECT (ffmpegdec, "linesize %d, data %p", pic.linesize[c],
1143 outpic = (AVPicture *) ffmpegdec->picture;
1145 av_picture_copy (&pic, outpic, ffmpegdec->context->pix_fmt,
1146 GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info));
1148 gst_video_frame_unmap (&vframe);
1150 ffmpegdec->picture->reordered_opaque = -1;
1157 GST_DEBUG_OBJECT (ffmpegdec, "pad_alloc failed");
1163 gst_avpacket_init (AVPacket * packet, guint8 * data, guint size)
1165 memset (packet, 0, sizeof (AVPacket));
1166 packet->data = data;
1167 packet->size = size;
1170 /* gst_ffmpegviddec_[video|audio]_frame:
1172 * data: pointer to the data to decode
1173 * size: size of data in bytes
1174 * in_timestamp: incoming timestamp.
1175 * in_duration: incoming duration.
1176 * in_offset: incoming offset (frame number).
1179 * Returns: number of bytes used in decoding. The check for successful decode is
1180 * outbuf being non-NULL.
1183 gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
1184 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1185 GstFlowReturn * ret)
1188 gboolean mode_switch;
1189 GstVideoCodecFrame *out_frame;
1190 GstFFMpegVidDecVideoFrame *out_dframe;
1195 /* in case we skip frames */
1196 ffmpegdec->picture->pict_type = -1;
1198 /* run QoS code, we don't stop decoding the frame when we are late because
1199 * else we might skip a reference frame */
1200 gst_ffmpegviddec_do_qos (ffmpegdec, frame, &mode_switch);
1202 if (ffmpegdec->is_realvideo && data != NULL) {
1206 /* setup the slice table for realvideo */
1207 if (ffmpegdec->context->slice_offset == NULL)
1208 ffmpegdec->context->slice_offset = g_malloc (sizeof (guint32) * 1000);
1210 slice_count = (*data++) + 1;
1211 ffmpegdec->context->slice_count = slice_count;
1213 for (i = 0; i < slice_count; i++) {
1215 ffmpegdec->context->slice_offset[i] = GST_READ_UINT32_LE (data);
1221 /* save reference to the timing info */
1222 ffmpegdec->context->reordered_opaque = (gint64) frame->system_frame_number;
1223 ffmpegdec->picture->reordered_opaque = (gint64) frame->system_frame_number;
1225 GST_DEBUG_OBJECT (ffmpegdec, "stored opaque values idx %d",
1226 frame->system_frame_number);
1229 /* now decode the frame */
1230 gst_avpacket_init (&packet, data, size);
1232 if (ffmpegdec->palette) {
1235 pal = av_packet_new_side_data (&packet, AV_PKT_DATA_PALETTE,
1237 gst_buffer_extract (ffmpegdec->palette, 0, pal, AVPALETTE_SIZE);
1238 GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
1241 len = avcodec_decode_video2 (ffmpegdec->context,
1242 ffmpegdec->picture, have_data, &packet);
1244 GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
1247 /* when we are in skip_frame mode, don't complain when ffmpeg returned
1248 * no data because we told it to skip stuff. */
1249 if (len < 0 && (mode_switch || ffmpegdec->context->skip_frame))
1252 /* no data, we're done */
1253 if (len < 0 || *have_data == 0)
1256 /* get the output picture timing info again */
1257 out_dframe = ffmpegdec->picture->opaque;
1258 out_frame = gst_video_codec_frame_ref (out_dframe->frame);
1260 /* also give back a buffer allocated by the frame, if any */
1261 gst_buffer_replace (&out_frame->output_buffer, out_dframe->buffer);
1262 gst_buffer_replace (&out_dframe->buffer, NULL);
1264 GST_DEBUG_OBJECT (ffmpegdec,
1265 "pts %" G_GUINT64_FORMAT " duration %" G_GUINT64_FORMAT,
1266 out_frame->pts, out_frame->duration);
1267 GST_DEBUG_OBJECT (ffmpegdec, "picture: pts %" G_GUINT64_FORMAT,
1268 (guint64) ffmpegdec->picture->pts);
1269 GST_DEBUG_OBJECT (ffmpegdec, "picture: num %d",
1270 ffmpegdec->picture->coded_picture_number);
1271 GST_DEBUG_OBJECT (ffmpegdec, "picture: ref %d",
1272 ffmpegdec->picture->reference);
1273 GST_DEBUG_OBJECT (ffmpegdec, "picture: display %d",
1274 ffmpegdec->picture->display_picture_number);
1275 GST_DEBUG_OBJECT (ffmpegdec, "picture: opaque %p",
1276 ffmpegdec->picture->opaque);
1277 GST_DEBUG_OBJECT (ffmpegdec, "picture: reordered opaque %" G_GUINT64_FORMAT,
1278 (guint64) ffmpegdec->picture->reordered_opaque);
1279 GST_DEBUG_OBJECT (ffmpegdec, "repeat_pict:%d",
1280 ffmpegdec->picture->repeat_pict);
1281 GST_DEBUG_OBJECT (ffmpegdec, "interlaced_frame:%d (current:%d)",
1282 ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1283 GST_DEBUG_OBJECT (ffmpegdec, "corrupted frame: %d",
1284 ! !(ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT));
1286 if (G_UNLIKELY (ffmpegdec->picture->interlaced_frame !=
1287 ffmpegdec->ctx_interlaced)) {
1288 GST_WARNING ("Change in interlacing ! picture:%d, recorded:%d",
1289 ffmpegdec->picture->interlaced_frame, ffmpegdec->ctx_interlaced);
1290 ffmpegdec->ctx_interlaced = ffmpegdec->picture->interlaced_frame;
1291 if (!gst_ffmpegviddec_negotiate (ffmpegdec, ffmpegdec->context, TRUE))
1292 goto negotiation_error;
1295 if (G_UNLIKELY (out_frame->output_buffer == NULL))
1296 *ret = get_output_buffer (ffmpegdec, out_frame);
1298 if (G_UNLIKELY (*ret != GST_FLOW_OK))
1301 /* Mark corrupted frames as corrupted */
1302 if (ffmpegdec->picture->flags & AV_FRAME_FLAG_CORRUPT)
1303 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_BUFFER_FLAG_CORRUPTED);
1305 if (ffmpegdec->ctx_interlaced) {
1306 /* set interlaced flags */
1307 if (ffmpegdec->picture->repeat_pict)
1308 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
1309 if (ffmpegdec->picture->top_field_first)
1310 GST_BUFFER_FLAG_SET (out_frame->output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
1311 if (ffmpegdec->picture->interlaced_frame)
1312 GST_BUFFER_FLAG_SET (out_frame->output_buffer,
1313 GST_VIDEO_BUFFER_FLAG_INTERLACED);
1317 /* so we decoded this frame, frames preceding it in decoding order
1318 * that still do not have a buffer allocated seem rather useless,
1319 * and can be discarded, due to e.g. misparsed bogus frame
1320 * or non-keyframe in skipped decoding, ...
1321 * In any case, not likely to be seen again, so discard those,
1322 * before they pile up and/or mess with timestamping */
1325 GstVideoDecoder *dec = GST_VIDEO_DECODER (ffmpegdec);
1326 gboolean old = TRUE;
1328 ol = l = gst_video_decoder_get_frames (dec);
1330 GstVideoCodecFrame *tmp = l->data;
1335 if (old && GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY (tmp)) {
1336 GST_LOG_OBJECT (dec,
1337 "discarding ghost frame %p (#%d) PTS:%" GST_TIME_FORMAT " DTS:%"
1338 GST_TIME_FORMAT, tmp, tmp->system_frame_number,
1339 GST_TIME_ARGS (tmp->pts), GST_TIME_ARGS (tmp->dts));
1340 /* drop extra ref and remove from frame list */
1341 gst_video_decoder_release_frame (dec, tmp);
1343 /* drop extra ref we got */
1344 gst_video_codec_frame_unref (tmp);
1352 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1355 GST_DEBUG_OBJECT (ffmpegdec, "return flow %s, len %d",
1356 gst_flow_get_name (*ret), len);
1362 GST_DEBUG_OBJECT (ffmpegdec, "no output buffer");
1363 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (ffmpegdec), out_frame);
1370 GST_WARNING_OBJECT (ffmpegdec, "Error negotiating format");
1371 *ret = GST_FLOW_NOT_NEGOTIATED;
1377 /* gst_ffmpegviddec_frame:
1379 * data: pointer to the data to decode
1380 * size: size of data in bytes
1381 * got_data: 0 if no data was decoded, != 0 otherwise.
1382 * in_time: timestamp of data
1383 * in_duration: duration of data
1384 * ret: GstFlowReturn to return in the chain function
1386 * Decode the given frame and pushes it downstream.
1388 * Returns: Number of bytes used in decoding, -1 on error/failure.
1392 gst_ffmpegviddec_frame (GstFFMpegVidDec * ffmpegdec,
1393 guint8 * data, guint size, gint * have_data, GstVideoCodecFrame * frame,
1394 GstFlowReturn * ret)
1396 GstFFMpegVidDecClass *oclass;
1399 if (G_UNLIKELY (ffmpegdec->context->codec == NULL))
1402 GST_LOG_OBJECT (ffmpegdec, "data:%p, size:%d", data, size);
1405 ffmpegdec->context->frame_number++;
1407 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1410 gst_ffmpegviddec_video_frame (ffmpegdec, data, size, have_data, frame,
1414 GST_WARNING_OBJECT (ffmpegdec,
1415 "avdec_%s: decoding error (len: %d, have_data: %d)",
1416 oclass->in_plugin->name, len, *have_data);
1424 GST_ERROR_OBJECT (ffmpegdec, "no codec context");
1425 *ret = GST_FLOW_NOT_NEGOTIATED;
1431 gst_ffmpegviddec_drain (GstFFMpegVidDec * ffmpegdec)
1433 GstFFMpegVidDecClass *oclass;
1435 if (!ffmpegdec->opened)
1438 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1440 if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) {
1441 gint have_data, len;
1443 GST_LOG_OBJECT (ffmpegdec,
1444 "codec has delay capabilities, calling until ffmpeg has drained everything");
1449 len = gst_ffmpegviddec_frame (ffmpegdec, NULL, 0, &have_data, NULL, &ret);
1451 } while (len >= 0 && have_data == 1);
1452 avcodec_flush_buffers (ffmpegdec->context);
1456 static GstFlowReturn
1457 gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
1458 GstVideoCodecFrame * frame)
1460 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1461 guint8 *data, *bdata;
1462 gint size, len, have_data, bsize;
1464 GstFlowReturn ret = GST_FLOW_OK;
1465 gboolean do_padding;
1467 GST_LOG_OBJECT (ffmpegdec,
1468 "Received new data of size %" G_GSIZE_FORMAT ", dts %" GST_TIME_FORMAT
1469 ", pts:%" GST_TIME_FORMAT ", dur:%" GST_TIME_FORMAT,
1470 gst_buffer_get_size (frame->input_buffer), GST_TIME_ARGS (frame->dts),
1471 GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
1473 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
1474 GST_ERROR_OBJECT (ffmpegdec, "Failed to map buffer");
1475 return GST_FLOW_ERROR;
1478 /* treat frame as void until a buffer is requested for it */
1479 GST_VIDEO_CODEC_FRAME_FLAG_SET (frame,
1480 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
1485 if (bsize > 0 && (!GST_MEMORY_IS_ZERO_PADDED (minfo.memory)
1486 || (minfo.maxsize - minfo.size) < FF_INPUT_BUFFER_PADDING_SIZE)) {
1488 if (ffmpegdec->padded_size < bsize + FF_INPUT_BUFFER_PADDING_SIZE) {
1489 ffmpegdec->padded_size = bsize + FF_INPUT_BUFFER_PADDING_SIZE;
1490 ffmpegdec->padded = g_realloc (ffmpegdec->padded, ffmpegdec->padded_size);
1491 GST_LOG_OBJECT (ffmpegdec, "resized padding buffer to %d",
1492 ffmpegdec->padded_size);
1494 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1495 "Copy input to add padding");
1496 memcpy (ffmpegdec->padded, bdata, bsize);
1497 memset (ffmpegdec->padded + bsize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1499 bdata = ffmpegdec->padded;
1506 guint8 tmp_padding[FF_INPUT_BUFFER_PADDING_SIZE];
1508 /* parse, if at all possible */
1513 /* add temporary padding */
1514 GST_CAT_TRACE_OBJECT (GST_CAT_PERFORMANCE, ffmpegdec,
1515 "Add temporary input padding");
1516 memcpy (tmp_padding, data + size, FF_INPUT_BUFFER_PADDING_SIZE);
1517 memset (data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1520 /* decode a frame of audio/video now */
1522 gst_ffmpegviddec_frame (ffmpegdec, data, size, &have_data, frame, &ret);
1525 memcpy (data + size, tmp_padding, FF_INPUT_BUFFER_PADDING_SIZE);
1528 if (ret != GST_FLOW_OK) {
1529 GST_LOG_OBJECT (ffmpegdec, "breaking because of flow ret %s",
1530 gst_flow_get_name (ret));
1531 /* bad flow retun, make sure we discard all data and exit */
1536 if (len == 0 && have_data == 0) {
1537 /* nothing was decoded, this could be because no data was available or
1538 * because we were skipping frames.
1539 * If we have no context we must exit and wait for more data, we keep the
1541 GST_LOG_OBJECT (ffmpegdec, "Decoding didn't return any data, breaking");
1546 /* a decoding error happened, we must break and try again with next data. */
1547 GST_LOG_OBJECT (ffmpegdec, "Decoding error, breaking");
1552 /* prepare for the next round, for codecs with a context we did this
1553 * already when using the parser. */
1559 GST_LOG_OBJECT (ffmpegdec, "Before (while bsize>0). bsize:%d , bdata:%p",
1561 } while (bsize > 0);
1564 GST_DEBUG_OBJECT (ffmpegdec, "Dropping %d bytes of data", bsize);
1566 gst_buffer_unmap (frame->input_buffer, &minfo);
1567 gst_video_codec_frame_unref (frame);
1574 gst_ffmpegviddec_start (GstVideoDecoder * decoder)
1576 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1577 GstFFMpegVidDecClass *oclass;
1579 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1581 GST_OBJECT_LOCK (ffmpegdec);
1582 gst_ffmpeg_avcodec_close (ffmpegdec->context);
1583 if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
1584 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
1585 GST_OBJECT_UNLOCK (ffmpegdec);
1588 ffmpegdec->context->opaque = ffmpegdec;
1589 GST_OBJECT_UNLOCK (ffmpegdec);
1595 gst_ffmpegviddec_stop (GstVideoDecoder * decoder)
1597 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1599 GST_OBJECT_LOCK (ffmpegdec);
1600 gst_ffmpegviddec_close (ffmpegdec, FALSE);
1601 GST_OBJECT_UNLOCK (ffmpegdec);
1602 g_free (ffmpegdec->padded);
1603 ffmpegdec->padded = NULL;
1604 ffmpegdec->padded_size = 0;
1605 if (ffmpegdec->input_state)
1606 gst_video_codec_state_unref (ffmpegdec->input_state);
1607 ffmpegdec->input_state = NULL;
1608 if (ffmpegdec->output_state)
1609 gst_video_codec_state_unref (ffmpegdec->output_state);
1610 ffmpegdec->output_state = NULL;
1612 ffmpegdec->ctx_width = 0;
1613 ffmpegdec->ctx_height = 0;
1614 ffmpegdec->ctx_ticks = 0;
1615 ffmpegdec->ctx_time_n = 0;
1616 ffmpegdec->ctx_time_d = 0;
1617 ffmpegdec->ctx_pix_fmt = 0;
1618 ffmpegdec->ctx_par_n = 0;
1619 ffmpegdec->ctx_par_d = 0;
1624 static GstFlowReturn
1625 gst_ffmpegviddec_finish (GstVideoDecoder * decoder)
1627 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1629 gst_ffmpegviddec_drain (ffmpegdec);
1635 gst_ffmpegviddec_flush (GstVideoDecoder * decoder)
1637 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1639 if (ffmpegdec->opened)
1640 avcodec_flush_buffers (ffmpegdec->context);
1646 gst_ffmpegviddec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
1648 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) decoder;
1649 GstVideoCodecState *state;
1650 GstBufferPool *pool;
1651 guint size, min, max;
1652 GstStructure *config;
1653 gboolean have_videometa, have_alignment, update_pool;
1654 GstAllocator *allocator = NULL;
1655 GstAllocationParams params = { 0, 15, 0, 0, };
1657 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
1661 state = gst_video_decoder_get_output_state (decoder);
1663 if (gst_query_get_n_allocation_params (query) > 0) {
1664 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1665 params.align = MAX (params.align, 15);
1667 gst_query_add_allocation_param (query, allocator, ¶ms);
1670 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
1672 /* Don't use pool that can't grow, as we don't know how many buffer we'll
1673 * need, otherwise we may stall */
1674 if (max != 0 && max < REQUIRED_POOL_MAX_BUFFERS) {
1675 gst_object_unref (pool);
1676 pool = gst_video_buffer_pool_new ();
1680 /* if there is an allocator, also drop it, as it might be the reason we
1681 * have this limit. Default will be used */
1683 gst_object_unref (allocator);
1688 config = gst_buffer_pool_get_config (pool);
1689 gst_buffer_pool_config_set_params (config, state->caps, size, min, max);
1690 /* we are happy with the default allocator but we would like to have 16 bytes
1691 * aligned and padded memory */
1692 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
1695 gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1697 gst_buffer_pool_config_add_option (config,
1698 GST_BUFFER_POOL_OPTION_VIDEO_META);
1701 gst_buffer_pool_has_option (pool, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1703 /* we can only enable the alignment if downstream supports the
1705 if (have_alignment && have_videometa) {
1706 GstVideoAlignment align;
1708 gint linesize_align[4];
1712 width = GST_VIDEO_INFO_WIDTH (&state->info);
1713 height = GST_VIDEO_INFO_HEIGHT (&state->info);
1714 /* let ffmpeg find the alignment and padding */
1715 avcodec_align_dimensions2 (ffmpegdec->context, &width, &height,
1719 context->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width ();
1720 /* increase the size for the padding */
1722 height += edge << 1;
1724 align.padding_top = edge;
1725 align.padding_left = edge;
1726 align.padding_right = width - GST_VIDEO_INFO_WIDTH (&state->info) - edge;
1727 align.padding_bottom = height - GST_VIDEO_INFO_HEIGHT (&state->info) - edge;
1729 /* add extra padding to match libav buffer allocation sizes */
1730 align.padding_bottom++;
1732 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
1733 align.stride_align[i] =
1734 (linesize_align[i] > 0 ? linesize_align[i] - 1 : 0);
1736 GST_DEBUG_OBJECT (ffmpegdec, "aligned dimension %dx%d -> %dx%d "
1737 "padding t:%u l:%u r:%u b:%u, stride_align %d:%d:%d:%d",
1738 GST_VIDEO_INFO_WIDTH (&state->info),
1739 GST_VIDEO_INFO_HEIGHT (&state->info), width, height, align.padding_top,
1740 align.padding_left, align.padding_right, align.padding_bottom,
1741 align.stride_align[0], align.stride_align[1], align.stride_align[2],
1742 align.stride_align[3]);
1744 gst_buffer_pool_config_add_option (config,
1745 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
1746 gst_buffer_pool_config_set_video_alignment (config, &align);
1748 if (ffmpegdec->direct_rendering) {
1749 GstFFMpegVidDecClass *oclass;
1751 GST_DEBUG_OBJECT (ffmpegdec, "trying to enable direct rendering");
1753 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
1755 if (oclass->in_plugin->capabilities & CODEC_CAP_DR1) {
1756 GST_DEBUG_OBJECT (ffmpegdec, "enabled direct rendering");
1757 ffmpegdec->current_dr = TRUE;
1759 GST_DEBUG_OBJECT (ffmpegdec, "direct rendering not supported");
1763 GST_DEBUG_OBJECT (ffmpegdec,
1764 "alignment or videometa not supported, disable direct rendering");
1766 /* disable direct rendering. This will make us use the fallback ffmpeg
1767 * picture allocation code with padding etc. We will then do the final
1768 * copy (with cropping) into a buffer from our pool */
1769 ffmpegdec->current_dr = FALSE;
1773 gst_buffer_pool_set_config (pool, config);
1776 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
1778 gst_object_unref (pool);
1780 gst_object_unref (allocator);
1781 gst_video_codec_state_unref (state);
1787 gst_ffmpegviddec_propose_allocation (GstVideoDecoder * decoder,
1790 GstAllocationParams params;
1792 gst_allocation_params_init (¶ms);
1793 params.flags = GST_MEMORY_FLAG_ZERO_PADDED;
1795 params.padding = FF_INPUT_BUFFER_PADDING_SIZE;
1796 /* we would like to have some padding so that we don't have to
1797 * memcpy. We don't suggest an allocator. */
1798 gst_query_add_allocation_param (query, NULL, ¶ms);
1800 return GST_VIDEO_DECODER_CLASS (parent_class)->propose_allocation (decoder,
1805 gst_ffmpegviddec_set_property (GObject * object,
1806 guint prop_id, const GValue * value, GParamSpec * pspec)
1808 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1812 ffmpegdec->lowres = ffmpegdec->context->lowres = g_value_get_enum (value);
1814 case PROP_SKIPFRAME:
1815 ffmpegdec->skip_frame = ffmpegdec->context->skip_frame =
1816 g_value_get_enum (value);
1818 case PROP_DIRECT_RENDERING:
1819 ffmpegdec->direct_rendering = g_value_get_boolean (value);
1822 ffmpegdec->debug_mv = ffmpegdec->context->debug_mv =
1823 g_value_get_boolean (value);
1825 case PROP_MAX_THREADS:
1826 ffmpegdec->max_threads = g_value_get_int (value);
1828 case PROP_OUTPUT_CORRUPT:
1829 ffmpegdec->output_corrupt = g_value_get_boolean (value);
1832 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1838 gst_ffmpegviddec_get_property (GObject * object,
1839 guint prop_id, GValue * value, GParamSpec * pspec)
1841 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
1845 g_value_set_enum (value, ffmpegdec->context->lowres);
1847 case PROP_SKIPFRAME:
1848 g_value_set_enum (value, ffmpegdec->context->skip_frame);
1850 case PROP_DIRECT_RENDERING:
1851 g_value_set_boolean (value, ffmpegdec->direct_rendering);
1854 g_value_set_boolean (value, ffmpegdec->context->debug_mv);
1856 case PROP_MAX_THREADS:
1857 g_value_set_int (value, ffmpegdec->max_threads);
1859 case PROP_OUTPUT_CORRUPT:
1860 g_value_set_boolean (value, ffmpegdec->output_corrupt);
1863 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1869 gst_ffmpegviddec_register (GstPlugin * plugin)
1871 GTypeInfo typeinfo = {
1872 sizeof (GstFFMpegVidDecClass),
1873 (GBaseInitFunc) gst_ffmpegviddec_base_init,
1875 (GClassInitFunc) gst_ffmpegviddec_class_init,
1878 sizeof (GstFFMpegVidDec),
1880 (GInstanceInitFunc) gst_ffmpegviddec_init,
1886 in_plugin = av_codec_next (NULL);
1888 GST_LOG ("Registering decoders");
1894 /* only video decoders */
1895 if (!av_codec_is_decoder (in_plugin)
1896 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1899 /* no quasi-codecs, please */
1900 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
1901 in_plugin->id == AV_CODEC_ID_V210 ||
1902 in_plugin->id == AV_CODEC_ID_V210X ||
1903 in_plugin->id == AV_CODEC_ID_R210 ||
1904 (in_plugin->id >= AV_CODEC_ID_PCM_S16LE &&
1905 in_plugin->id <= AV_CODEC_ID_PCM_BLURAY)) {
1909 /* No decoders depending on external libraries (we don't build them, but
1910 * people who build against an external ffmpeg might have them.
1911 * We have native gstreamer plugins for all of those libraries anyway. */
1912 if (!strncmp (in_plugin->name, "lib", 3)) {
1914 ("Not using external library decoder %s. Use the gstreamer-native ones instead.",
1919 /* No vdpau plugins until we can figure out how to properly use them
1920 * outside of ffmpeg. */
1921 if (g_str_has_suffix (in_plugin->name, "_vdpau")) {
1923 ("Ignoring VDPAU decoder %s. We can't handle this outside of ffmpeg",
1928 if (g_str_has_suffix (in_plugin->name, "_xvmc")) {
1930 ("Ignoring XVMC decoder %s. We can't handle this outside of ffmpeg",
1935 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
1937 /* no codecs for which we're GUARANTEED to have better alternatives */
1938 /* MPEG1VIDEO : the mpeg2video decoder is preferred */
1939 /* MP1 : Use MP3 for decoding */
1940 /* MP2 : Use MP3 for decoding */
1941 /* Theora: Use libtheora based theoradec */
1942 if (!strcmp (in_plugin->name, "gif") ||
1943 !strcmp (in_plugin->name, "theora") ||
1944 !strcmp (in_plugin->name, "mpeg1video") ||
1945 strstr (in_plugin->name, "crystalhd") != NULL ||
1946 !strcmp (in_plugin->name, "ass") ||
1947 !strcmp (in_plugin->name, "srt") ||
1948 !strcmp (in_plugin->name, "pgssub") ||
1949 !strcmp (in_plugin->name, "dvdsub") ||
1950 !strcmp (in_plugin->name, "dvbsub")) {
1951 GST_LOG ("Ignoring decoder %s", in_plugin->name);
1955 /* construct the type */
1956 if (!strcmp (in_plugin->name, "hevc")) {
1957 plugin_name = g_strdup ("h265");
1959 plugin_name = g_strdup ((gchar *) in_plugin->name);
1961 g_strdelimit (plugin_name, NULL, '_');
1962 type_name = g_strdup_printf ("avdec_%s", plugin_name);
1963 g_free (plugin_name);
1965 type = g_type_from_name (type_name);
1968 /* create the gtype now */
1970 g_type_register_static (GST_TYPE_VIDEO_DECODER, type_name, &typeinfo,
1972 g_type_set_qdata (type, GST_FFDEC_PARAMS_QDATA, (gpointer) in_plugin);
1975 /* (Ronald) MPEG-4 gets a higher priority because it has been well-
1976 * tested and by far outperforms divxdec/xviddec - so we prefer it.
1977 * msmpeg4v3 same, as it outperforms divxdec for divx3 playback.
1978 * VC1/WMV3 are not working and thus unpreferred for now. */
1979 switch (in_plugin->id) {
1980 case AV_CODEC_ID_MPEG4:
1981 case AV_CODEC_ID_MSMPEG4V3:
1982 case AV_CODEC_ID_H264:
1983 case AV_CODEC_ID_HEVC:
1984 case AV_CODEC_ID_RV10:
1985 case AV_CODEC_ID_RV20:
1986 case AV_CODEC_ID_RV30:
1987 case AV_CODEC_ID_RV40:
1988 rank = GST_RANK_PRIMARY;
1990 /* DVVIDEO: we have a good dv decoder, fast on both ppc as well as x86.
1991 * They say libdv's quality is better though. leave as secondary.
1992 * note: if you change this, see the code in gstdv.c in good/ext/dv.
1994 case AV_CODEC_ID_DVVIDEO:
1995 rank = GST_RANK_SECONDARY;
1998 rank = GST_RANK_MARGINAL;
2001 if (!gst_element_register (plugin, type_name, rank, type)) {
2002 g_warning ("Failed to register %s", type_name);
2010 in_plugin = av_codec_next (in_plugin);
2013 GST_LOG ("Finished Registering decoders");