2 * Copyright (C) 2006 David Schleef <ds@schleef.org>
3 * Copyright (C) 2008,2009,2010 Entropy Wave Inc
4 * Copyright (C) 2010-2013 Sebastian Dröge <slomo@circular-chaos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-vp9dec
24 * @see_also: vp9enc, matroskademux
26 * This element decodes VP9 streams into raw video.
27 * <ulink url="http://www.webmproject.org">VP9</ulink> is a royalty-free
28 * video codec maintained by <ulink url="http://www.google.com/">Google
29 * </ulink>. It's the successor of On2 VP3, which was the base of the
33 * <title>Example pipeline</title>
35 * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! vp9dec ! videoconvert ! videoscale ! autovideosink
36 * ]| This example pipeline will decode a WebM stream and decodes the VP9 video.
44 #ifdef HAVE_VP9_DECODER
48 #include "gstvp8utils.h"
49 #include "gstvp9dec.h"
51 #include <gst/video/gstvideometa.h>
52 #include <gst/video/gstvideopool.h>
54 GST_DEBUG_CATEGORY_STATIC (gst_vp9dec_debug);
55 #define GST_CAT_DEFAULT gst_vp9dec_debug
57 #define DEFAULT_POST_PROCESSING FALSE
58 #define DEFAULT_POST_PROCESSING_FLAGS (VP8_DEBLOCK | VP8_DEMACROBLOCK)
59 #define DEFAULT_DEBLOCKING_LEVEL 4
60 #define DEFAULT_NOISE_LEVEL 0
61 #define DEFAULT_THREADS 1
67 PROP_POST_PROCESSING_FLAGS,
68 PROP_DEBLOCKING_LEVEL,
73 #define C_FLAGS(v) ((guint) v)
74 #define GST_VP9_DEC_TYPE_POST_PROCESSING_FLAGS (gst_vp9_dec_post_processing_flags_get_type())
76 gst_vp9_dec_post_processing_flags_get_type (void)
78 static const GFlagsValue values[] = {
79 {C_FLAGS (VP8_DEBLOCK), "Deblock", "deblock"},
80 {C_FLAGS (VP8_DEMACROBLOCK), "Demacroblock", "demacroblock"},
81 {C_FLAGS (VP8_ADDNOISE), "Add noise", "addnoise"},
82 {C_FLAGS (VP8_MFQE), "Multi-frame quality enhancement", "mfqe"},
85 static volatile GType id = 0;
87 if (g_once_init_enter ((gsize *) & id)) {
90 _id = g_flags_register_static ("GstVP9DecPostProcessingFlags", values);
92 g_once_init_leave ((gsize *) & id, _id);
100 static void gst_vp9_dec_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_vp9_dec_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
105 static gboolean gst_vp9_dec_start (GstVideoDecoder * decoder);
106 static gboolean gst_vp9_dec_stop (GstVideoDecoder * decoder);
107 static gboolean gst_vp9_dec_set_format (GstVideoDecoder * decoder,
108 GstVideoCodecState * state);
109 static gboolean gst_vp9_dec_flush (GstVideoDecoder * decoder);
110 static GstFlowReturn gst_vp9_dec_handle_frame (GstVideoDecoder * decoder,
111 GstVideoCodecFrame * frame);
112 static gboolean gst_vp9_dec_decide_allocation (GstVideoDecoder * decoder,
115 static GstStaticPadTemplate gst_vp9_dec_sink_template =
116 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_STATIC_CAPS ("video/x-vp9")
122 static GstStaticPadTemplate gst_vp9_dec_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
126 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }"))
129 #define parent_class gst_vp9_dec_parent_class
130 G_DEFINE_TYPE (GstVP9Dec, gst_vp9_dec, GST_TYPE_VIDEO_DECODER);
133 gst_vp9_dec_class_init (GstVP9DecClass * klass)
135 GObjectClass *gobject_class;
136 GstElementClass *element_class;
137 GstVideoDecoderClass *base_video_decoder_class;
139 gobject_class = G_OBJECT_CLASS (klass);
140 element_class = GST_ELEMENT_CLASS (klass);
141 base_video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
143 gobject_class->set_property = gst_vp9_dec_set_property;
144 gobject_class->get_property = gst_vp9_dec_get_property;
146 g_object_class_install_property (gobject_class, PROP_POST_PROCESSING,
147 g_param_spec_boolean ("post-processing", "Post Processing",
148 "Enable post processing", DEFAULT_POST_PROCESSING,
149 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (gobject_class, PROP_POST_PROCESSING_FLAGS,
152 g_param_spec_flags ("post-processing-flags", "Post Processing Flags",
153 "Flags to control post processing",
154 GST_VP9_DEC_TYPE_POST_PROCESSING_FLAGS, DEFAULT_POST_PROCESSING_FLAGS,
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_DEBLOCKING_LEVEL,
158 g_param_spec_uint ("deblocking-level", "Deblocking Level",
160 0, 16, DEFAULT_DEBLOCKING_LEVEL,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_NOISE_LEVEL,
164 g_param_spec_uint ("noise-level", "Noise Level",
166 0, 16, DEFAULT_NOISE_LEVEL,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_THREADS,
170 g_param_spec_uint ("threads", "Max Threads",
171 "Maximum number of decoding threads",
172 1, 16, DEFAULT_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 gst_element_class_add_pad_template (element_class,
175 gst_static_pad_template_get (&gst_vp9_dec_src_template));
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&gst_vp9_dec_sink_template));
179 gst_element_class_set_static_metadata (element_class,
181 "Codec/Decoder/Video",
182 "Decode VP9 video streams", "David Schleef <ds@entropywave.com>, "
183 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
185 base_video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_vp9_dec_start);
186 base_video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vp9_dec_stop);
187 base_video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_vp9_dec_flush);
188 base_video_decoder_class->set_format =
189 GST_DEBUG_FUNCPTR (gst_vp9_dec_set_format);
190 base_video_decoder_class->handle_frame =
191 GST_DEBUG_FUNCPTR (gst_vp9_dec_handle_frame);
192 base_video_decoder_class->decide_allocation = gst_vp9_dec_decide_allocation;
194 GST_DEBUG_CATEGORY_INIT (gst_vp9dec_debug, "vp9dec", 0, "VP9 Decoder");
198 gst_vp9_dec_init (GstVP9Dec * gst_vp9_dec)
200 GstVideoDecoder *decoder = (GstVideoDecoder *) gst_vp9_dec;
202 GST_DEBUG_OBJECT (gst_vp9_dec, "gst_vp9_dec_init");
203 gst_video_decoder_set_packetized (decoder, TRUE);
204 gst_vp9_dec->post_processing = DEFAULT_POST_PROCESSING;
205 gst_vp9_dec->post_processing_flags = DEFAULT_POST_PROCESSING_FLAGS;
206 gst_vp9_dec->deblocking_level = DEFAULT_DEBLOCKING_LEVEL;
207 gst_vp9_dec->noise_level = DEFAULT_NOISE_LEVEL;
209 gst_video_decoder_set_needs_format (decoder, TRUE);
210 gst_video_decoder_set_use_default_pad_acceptcaps (decoder, TRUE);
211 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (decoder));
215 gst_vp9_dec_set_property (GObject * object, guint prop_id,
216 const GValue * value, GParamSpec * pspec)
220 g_return_if_fail (GST_IS_VP9_DEC (object));
221 dec = GST_VP9_DEC (object);
223 GST_DEBUG_OBJECT (object, "gst_vp9_dec_set_property");
225 case PROP_POST_PROCESSING:
226 dec->post_processing = g_value_get_boolean (value);
228 case PROP_POST_PROCESSING_FLAGS:
229 dec->post_processing_flags = g_value_get_flags (value);
231 case PROP_DEBLOCKING_LEVEL:
232 dec->deblocking_level = g_value_get_uint (value);
234 case PROP_NOISE_LEVEL:
235 dec->noise_level = g_value_get_uint (value);
238 dec->threads = g_value_get_uint (value);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gst_vp9_dec_get_property (GObject * object, guint prop_id, GValue * value,
252 g_return_if_fail (GST_IS_VP9_DEC (object));
253 dec = GST_VP9_DEC (object);
256 case PROP_POST_PROCESSING:
257 g_value_set_boolean (value, dec->post_processing);
259 case PROP_POST_PROCESSING_FLAGS:
260 g_value_set_flags (value, dec->post_processing_flags);
262 case PROP_DEBLOCKING_LEVEL:
263 g_value_set_uint (value, dec->deblocking_level);
265 case PROP_NOISE_LEVEL:
266 g_value_set_uint (value, dec->noise_level);
269 g_value_set_uint (value, dec->threads);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_vp9_dec_start (GstVideoDecoder * decoder)
280 GstVP9Dec *gst_vp9_dec = GST_VP9_DEC (decoder);
282 GST_DEBUG_OBJECT (gst_vp9_dec, "start");
283 gst_vp9_dec->decoder_inited = FALSE;
289 gst_vp9_dec_stop (GstVideoDecoder * base_video_decoder)
291 GstVP9Dec *gst_vp9_dec = GST_VP9_DEC (base_video_decoder);
293 GST_DEBUG_OBJECT (gst_vp9_dec, "stop");
295 if (gst_vp9_dec->output_state) {
296 gst_video_codec_state_unref (gst_vp9_dec->output_state);
297 gst_vp9_dec->output_state = NULL;
300 if (gst_vp9_dec->input_state) {
301 gst_video_codec_state_unref (gst_vp9_dec->input_state);
302 gst_vp9_dec->input_state = NULL;
305 if (gst_vp9_dec->decoder_inited)
306 vpx_codec_destroy (&gst_vp9_dec->decoder);
307 gst_vp9_dec->decoder_inited = FALSE;
313 gst_vp9_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
315 GstVP9Dec *gst_vp9_dec = GST_VP9_DEC (decoder);
317 GST_DEBUG_OBJECT (gst_vp9_dec, "set_format");
319 if (gst_vp9_dec->decoder_inited)
320 vpx_codec_destroy (&gst_vp9_dec->decoder);
321 gst_vp9_dec->decoder_inited = FALSE;
323 if (gst_vp9_dec->output_state) {
324 gst_video_codec_state_unref (gst_vp9_dec->output_state);
325 gst_vp9_dec->output_state = NULL;
328 if (gst_vp9_dec->input_state)
329 gst_video_codec_state_unref (gst_vp9_dec->input_state);
330 gst_vp9_dec->input_state = gst_video_codec_state_ref (state);
336 gst_vp9_dec_flush (GstVideoDecoder * base_video_decoder)
340 GST_DEBUG_OBJECT (base_video_decoder, "flush");
342 decoder = GST_VP9_DEC (base_video_decoder);
344 if (decoder->output_state) {
345 gst_video_codec_state_unref (decoder->output_state);
346 decoder->output_state = NULL;
349 if (decoder->decoder_inited)
350 vpx_codec_destroy (&decoder->decoder);
351 decoder->decoder_inited = FALSE;
357 gst_vp9_dec_send_tags (GstVP9Dec * dec)
361 list = gst_tag_list_new_empty ();
362 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
363 GST_TAG_VIDEO_CODEC, "VP9 video", NULL);
365 gst_pad_push_event (GST_VIDEO_DECODER_SRC_PAD (dec),
366 gst_event_new_tag (list));
370 gst_vp9_dec_image_to_buffer (GstVP9Dec * dec, const vpx_image_t * img,
373 int deststride, srcstride, height, width, line, comp;
376 GstVideoInfo *info = &dec->output_state->info;
378 if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
379 GST_ERROR_OBJECT (dec, "Could not map video buffer");
383 for (comp = 0; comp < 3; comp++) {
384 dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
385 src = img->planes[comp];
386 width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp)
387 * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
388 height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
389 deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
390 srcstride = img->stride[comp];
392 if (srcstride == deststride) {
393 GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
395 memcpy (dest, src, srcstride * height);
397 GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
398 "line by line.", comp, srcstride, deststride);
399 for (line = 0; line < height; line++) {
400 memcpy (dest, src, width);
407 gst_video_frame_unmap (&frame);
411 open_codec (GstVP9Dec * dec, GstVideoCodecFrame * frame)
414 vpx_codec_stream_info_t stream_info;
415 vpx_codec_caps_t caps;
416 vpx_codec_dec_cfg_t cfg;
417 vpx_codec_err_t status;
420 memset (&stream_info, 0, sizeof (stream_info));
421 memset (&cfg, 0, sizeof (cfg));
422 stream_info.sz = sizeof (stream_info);
424 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
425 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
426 return GST_FLOW_ERROR;
429 status = vpx_codec_peek_stream_info (&vpx_codec_vp9_dx_algo,
430 minfo.data, minfo.size, &stream_info);
432 gst_buffer_unmap (frame->input_buffer, &minfo);
434 if (status != VPX_CODEC_OK) {
435 GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
436 gst_vpx_error_name (status));
437 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
438 return GST_FLOW_CUSTOM_SUCCESS_1;
440 if (!stream_info.is_kf) {
441 GST_WARNING_OBJECT (dec, "No keyframe, skipping");
442 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
443 return GST_FLOW_CUSTOM_SUCCESS_1;
446 /* FIXME: peek_stream_info() does not return valid values, take input caps */
447 stream_info.w = dec->input_state->info.width;
448 stream_info.h = dec->input_state->info.height;
450 cfg.w = stream_info.w;
451 cfg.h = stream_info.h;
452 cfg.threads = dec->threads;
454 caps = vpx_codec_get_caps (&vpx_codec_vp9_dx_algo);
456 if (dec->post_processing) {
457 if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
458 GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
460 flags |= VPX_CODEC_USE_POSTPROC;
465 vpx_codec_dec_init (&dec->decoder, &vpx_codec_vp9_dx_algo, &cfg, flags);
466 if (status != VPX_CODEC_OK) {
467 GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
468 ("Failed to initialize VP9 decoder"), ("%s",
469 gst_vpx_error_name (status)));
470 return GST_FLOW_ERROR;
473 if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
474 vp8_postproc_cfg_t pp_cfg = { 0, };
476 pp_cfg.post_proc_flag = dec->post_processing_flags;
477 pp_cfg.deblocking_level = dec->deblocking_level;
478 pp_cfg.noise_level = dec->noise_level;
480 status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
481 if (status != VPX_CODEC_OK) {
482 GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
483 gst_vpx_error_name (status));
487 dec->decoder_inited = TRUE;
493 gst_vp9_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
496 GstFlowReturn ret = GST_FLOW_OK;
497 vpx_codec_err_t status;
498 vpx_codec_iter_t iter = NULL;
500 long decoder_deadline = 0;
501 GstClockTimeDiff deadline;
504 GST_DEBUG_OBJECT (decoder, "handle_frame");
506 dec = GST_VP9_DEC (decoder);
508 if (!dec->decoder_inited) {
509 ret = open_codec (dec, frame);
510 if (ret == GST_FLOW_CUSTOM_SUCCESS_1)
512 else if (ret != GST_FLOW_OK)
516 deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
518 decoder_deadline = 1;
519 } else if (deadline == G_MAXINT64) {
520 decoder_deadline = 0;
522 decoder_deadline = MAX (1, deadline / GST_MSECOND);
525 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
526 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
527 return GST_FLOW_ERROR;
530 status = vpx_codec_decode (&dec->decoder,
531 minfo.data, minfo.size, NULL, decoder_deadline);
533 gst_buffer_unmap (frame->input_buffer, &minfo);
536 GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
537 ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
541 img = vpx_codec_get_frame (&dec->decoder, &iter);
546 case VPX_IMG_FMT_I420:
547 fmt = GST_VIDEO_FORMAT_I420;
549 case VPX_IMG_FMT_YV12:
550 fmt = GST_VIDEO_FORMAT_YV12;
552 case VPX_IMG_FMT_I422:
553 fmt = GST_VIDEO_FORMAT_Y42B;
555 case VPX_IMG_FMT_I444:
556 fmt = GST_VIDEO_FORMAT_Y444;
560 GST_ELEMENT_ERROR (decoder, LIBRARY, ENCODE,
561 ("Failed to decode frame"), ("Unsupported color format %d",
563 return GST_FLOW_ERROR;
567 if (!dec->output_state || dec->output_state->info.finfo->format != fmt ||
568 dec->output_state->info.width != img->d_w ||
569 dec->output_state->info.height != img->d_h) {
570 gboolean send_tags = !dec->output_state;
572 if (dec->output_state)
573 gst_video_codec_state_unref (dec->output_state);
576 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
577 fmt, img->d_w, img->d_h, dec->input_state);
578 gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
581 gst_vp9_dec_send_tags (dec);
585 GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
586 (double) -deadline / GST_SECOND);
587 gst_video_decoder_drop_frame (decoder, frame);
589 ret = gst_video_decoder_allocate_output_frame (decoder, frame);
591 if (ret == GST_FLOW_OK) {
592 gst_vp9_dec_image_to_buffer (dec, img, frame->output_buffer);
593 ret = gst_video_decoder_finish_frame (decoder, frame);
595 gst_video_decoder_drop_frame (decoder, frame);
601 while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
602 GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
606 /* Invisible frame */
607 GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
608 gst_video_decoder_finish_frame (decoder, frame);
615 gst_vp9_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
618 GstStructure *config;
620 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
623 g_assert (gst_query_get_n_allocation_pools (query) > 0);
624 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
625 g_assert (pool != NULL);
627 config = gst_buffer_pool_get_config (pool);
628 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
629 gst_buffer_pool_config_add_option (config,
630 GST_BUFFER_POOL_OPTION_VIDEO_META);
632 gst_buffer_pool_set_config (pool, config);
633 gst_object_unref (pool);
638 #endif /* HAVE_VP9_DECODER */