2 * Copyright (C) 2006 David Schleef <ds@schleef.org>
3 * Copyright (C) 2008,2009,2010 Entropy Wave Inc
4 * Copyright (C) 2010-2012 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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-vp8dec
24 * @see_also: vp8enc, matroskademux
26 * This element decodes VP8 streams into raw video.
27 * <ulink url="http://www.webmproject.org">VP8</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 -v filesrc location=videotestsrc.webm ! matroskademux ! vp8dec ! xvimagesink
36 * ]| This example pipeline will decode a WebM stream and decodes the VP8 video.
44 #ifdef HAVE_VP8_DECODER
48 #include "gstvp8dec.h"
49 #include "gstvp8utils.h"
51 #include <gst/video/gstvideometa.h>
52 #include <gst/video/gstvideopool.h>
54 GST_DEBUG_CATEGORY_STATIC (gst_vp8dec_debug);
55 #define GST_CAT_DEFAULT gst_vp8dec_debug
57 #define DEFAULT_POST_PROCESSING FALSE
58 #define DEFAULT_POST_PROCESSING_FLAGS (VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE)
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_VP8_DEC_TYPE_POST_PROCESSING_FLAGS (gst_vp8_dec_post_processing_flags_get_type())
76 gst_vp8_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 ("GstVP8DecPostProcessingFlags", values);
92 g_once_init_leave ((gsize *) & id, _id);
100 static void gst_vp8_dec_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_vp8_dec_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
105 static gboolean gst_vp8_dec_start (GstVideoDecoder * decoder);
106 static gboolean gst_vp8_dec_stop (GstVideoDecoder * decoder);
107 static gboolean gst_vp8_dec_set_format (GstVideoDecoder * decoder,
108 GstVideoCodecState * state);
109 static gboolean gst_vp8_dec_reset (GstVideoDecoder * decoder, gboolean hard);
110 static GstFlowReturn gst_vp8_dec_handle_frame (GstVideoDecoder * decoder,
111 GstVideoCodecFrame * frame);
112 static gboolean gst_vp8_dec_decide_allocation (GstVideoDecoder * decoder,
115 static GstStaticPadTemplate gst_vp8_dec_sink_template =
116 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_STATIC_CAPS ("video/x-vp8")
122 static GstStaticPadTemplate gst_vp8_dec_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
126 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
129 #define parent_class gst_vp8_dec_parent_class
130 G_DEFINE_TYPE (GstVP8Dec, gst_vp8_dec, GST_TYPE_VIDEO_DECODER);
133 gst_vp8_dec_class_init (GstVP8DecClass * 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_vp8_dec_set_property;
144 gobject_class->get_property = gst_vp8_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_VP8_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_vp8_dec_src_template));
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&gst_vp8_dec_sink_template));
179 gst_element_class_set_static_metadata (element_class,
181 "Codec/Decoder/Video",
182 "Decode VP8 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_vp8_dec_start);
186 base_video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vp8_dec_stop);
187 base_video_decoder_class->reset = GST_DEBUG_FUNCPTR (gst_vp8_dec_reset);
188 base_video_decoder_class->set_format =
189 GST_DEBUG_FUNCPTR (gst_vp8_dec_set_format);
190 base_video_decoder_class->handle_frame =
191 GST_DEBUG_FUNCPTR (gst_vp8_dec_handle_frame);
192 base_video_decoder_class->decide_allocation = gst_vp8_dec_decide_allocation;
194 GST_DEBUG_CATEGORY_INIT (gst_vp8dec_debug, "vp8dec", 0, "VP8 Decoder");
198 gst_vp8_dec_init (GstVP8Dec * gst_vp8_dec)
200 GstVideoDecoder *decoder = (GstVideoDecoder *) gst_vp8_dec;
202 GST_DEBUG_OBJECT (gst_vp8_dec, "gst_vp8_dec_init");
203 gst_video_decoder_set_packetized (decoder, TRUE);
204 gst_vp8_dec->post_processing = DEFAULT_POST_PROCESSING;
205 gst_vp8_dec->post_processing_flags = DEFAULT_POST_PROCESSING_FLAGS;
206 gst_vp8_dec->deblocking_level = DEFAULT_DEBLOCKING_LEVEL;
207 gst_vp8_dec->noise_level = DEFAULT_NOISE_LEVEL;
211 gst_vp8_dec_set_property (GObject * object, guint prop_id,
212 const GValue * value, GParamSpec * pspec)
216 g_return_if_fail (GST_IS_VP8_DEC (object));
217 dec = GST_VP8_DEC (object);
219 GST_DEBUG_OBJECT (object, "gst_vp8_dec_set_property");
221 case PROP_POST_PROCESSING:
222 dec->post_processing = g_value_get_boolean (value);
224 case PROP_POST_PROCESSING_FLAGS:
225 dec->post_processing_flags = g_value_get_flags (value);
227 case PROP_DEBLOCKING_LEVEL:
228 dec->deblocking_level = g_value_get_uint (value);
230 case PROP_NOISE_LEVEL:
231 dec->noise_level = g_value_get_uint (value);
234 dec->threads = g_value_get_uint (value);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gst_vp8_dec_get_property (GObject * object, guint prop_id, GValue * value,
248 g_return_if_fail (GST_IS_VP8_DEC (object));
249 dec = GST_VP8_DEC (object);
252 case PROP_POST_PROCESSING:
253 g_value_set_boolean (value, dec->post_processing);
255 case PROP_POST_PROCESSING_FLAGS:
256 g_value_set_flags (value, dec->post_processing_flags);
258 case PROP_DEBLOCKING_LEVEL:
259 g_value_set_uint (value, dec->deblocking_level);
261 case PROP_NOISE_LEVEL:
262 g_value_set_uint (value, dec->noise_level);
265 g_value_set_uint (value, dec->threads);
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 gst_vp8_dec_start (GstVideoDecoder * decoder)
276 GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (decoder);
278 GST_DEBUG_OBJECT (gst_vp8_dec, "start");
279 gst_vp8_dec->decoder_inited = FALSE;
285 gst_vp8_dec_stop (GstVideoDecoder * base_video_decoder)
287 GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (base_video_decoder);
289 GST_DEBUG_OBJECT (gst_vp8_dec, "stop");
291 if (gst_vp8_dec->output_state) {
292 gst_video_codec_state_unref (gst_vp8_dec->output_state);
293 gst_vp8_dec->output_state = NULL;
296 if (gst_vp8_dec->input_state) {
297 gst_video_codec_state_unref (gst_vp8_dec->input_state);
298 gst_vp8_dec->input_state = NULL;
301 if (gst_vp8_dec->decoder_inited)
302 vpx_codec_destroy (&gst_vp8_dec->decoder);
303 gst_vp8_dec->decoder_inited = FALSE;
309 gst_vp8_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
311 GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (decoder);
313 GST_DEBUG_OBJECT (gst_vp8_dec, "set_format");
315 if (gst_vp8_dec->decoder_inited)
316 vpx_codec_destroy (&gst_vp8_dec->decoder);
317 gst_vp8_dec->decoder_inited = FALSE;
319 if (gst_vp8_dec->input_state)
320 gst_video_codec_state_unref (gst_vp8_dec->input_state);
321 gst_vp8_dec->input_state = gst_video_codec_state_ref (state);
327 gst_vp8_dec_reset (GstVideoDecoder * base_video_decoder, gboolean hard)
331 GST_DEBUG_OBJECT (base_video_decoder, "reset");
333 decoder = GST_VP8_DEC (base_video_decoder);
335 if (decoder->output_state) {
336 gst_video_codec_state_unref (decoder->output_state);
337 decoder->output_state = NULL;
340 if (decoder->decoder_inited)
341 vpx_codec_destroy (&decoder->decoder);
342 decoder->decoder_inited = FALSE;
348 gst_vp8_dec_send_tags (GstVP8Dec * dec)
352 list = gst_tag_list_new_empty ();
353 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
354 GST_TAG_VIDEO_CODEC, "VP8 video", NULL);
356 gst_pad_push_event (GST_VIDEO_DECODER_SRC_PAD (dec),
357 gst_event_new_tag (list));
361 gst_vp8_dec_image_to_buffer (GstVP8Dec * dec, const vpx_image_t * img,
364 int deststride, srcstride, height, width, line, comp;
367 GstVideoInfo *info = &dec->output_state->info;
369 if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
370 GST_ERROR_OBJECT (dec, "Could not map video buffer");
373 for (comp = 0; comp < 3; comp++) {
374 dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
375 src = img->planes[comp];
376 width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp);
377 height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
378 deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
379 srcstride = img->stride[comp];
381 /* FIXME (Edward) : Do a plane memcpy is srcstride == deststride instead
382 * of copying line by line */
383 for (line = 0; line < height; line++) {
384 memcpy (dest, src, width);
390 gst_video_frame_unmap (&frame);
394 open_codec (GstVP8Dec * dec, GstVideoCodecFrame * frame)
397 vpx_codec_stream_info_t stream_info;
398 vpx_codec_caps_t caps;
399 vpx_codec_dec_cfg_t cfg;
400 GstVideoCodecState *state = dec->input_state;
401 vpx_codec_err_t status;
404 memset (&stream_info, 0, sizeof (stream_info));
405 memset (&cfg, 0, sizeof (cfg));
406 stream_info.sz = sizeof (stream_info);
408 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
409 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
410 return GST_FLOW_ERROR;
413 status = vpx_codec_peek_stream_info (&vpx_codec_vp8_dx_algo,
414 minfo.data, minfo.size, &stream_info);
416 gst_buffer_unmap (frame->input_buffer, &minfo);
418 if (status != VPX_CODEC_OK) {
419 GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
420 gst_vpx_error_name (status));
421 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (dec), frame);
422 return GST_FLOW_CUSTOM_SUCCESS_1;
424 if (!stream_info.is_kf) {
425 GST_WARNING_OBJECT (dec, "No keyframe, skipping");
426 gst_video_decoder_finish_frame (GST_VIDEO_DECODER (dec), frame);
427 return GST_FLOW_CUSTOM_SUCCESS_1;
430 g_assert (dec->output_state == NULL);
432 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
433 GST_VIDEO_FORMAT_I420, stream_info.w, stream_info.h, state);
434 gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
435 gst_vp8_dec_send_tags (dec);
437 cfg.w = stream_info.w;
438 cfg.h = stream_info.h;
439 cfg.threads = dec->threads;
441 caps = vpx_codec_get_caps (&vpx_codec_vp8_dx_algo);
443 if (dec->post_processing) {
444 if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
445 GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
447 flags |= VPX_CODEC_USE_POSTPROC;
452 vpx_codec_dec_init (&dec->decoder, &vpx_codec_vp8_dx_algo, &cfg, flags);
453 if (status != VPX_CODEC_OK) {
454 GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
455 ("Failed to initialize VP8 decoder"), ("%s",
456 gst_vpx_error_name (status)));
457 return GST_FLOW_ERROR;
460 if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
461 vp8_postproc_cfg_t pp_cfg = { 0, };
463 pp_cfg.post_proc_flag = dec->post_processing_flags;
464 pp_cfg.deblocking_level = dec->deblocking_level;
465 pp_cfg.noise_level = dec->noise_level;
467 status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
468 if (status != VPX_CODEC_OK) {
469 GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
470 gst_vpx_error_name (status));
474 dec->decoder_inited = TRUE;
480 gst_vp8_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
483 GstFlowReturn ret = GST_FLOW_OK;
484 vpx_codec_err_t status;
485 vpx_codec_iter_t iter = NULL;
487 long decoder_deadline = 0;
488 GstClockTimeDiff deadline;
491 GST_DEBUG_OBJECT (decoder, "handle_frame");
493 dec = GST_VP8_DEC (decoder);
495 if (!dec->decoder_inited) {
496 ret = open_codec (dec, frame);
497 if (ret == GST_FLOW_CUSTOM_SUCCESS_1)
499 else if (ret != GST_FLOW_OK)
503 deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
505 decoder_deadline = 1;
506 } else if (deadline == G_MAXINT64) {
507 decoder_deadline = 0;
509 decoder_deadline = MAX (1, deadline / GST_MSECOND);
512 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
513 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
514 return GST_FLOW_ERROR;
517 status = vpx_codec_decode (&dec->decoder,
518 minfo.data, minfo.size, NULL, decoder_deadline);
520 gst_buffer_unmap (frame->input_buffer, &minfo);
523 GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
524 ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
528 img = vpx_codec_get_frame (&dec->decoder, &iter);
531 GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
532 (double) -deadline / GST_SECOND);
533 gst_video_decoder_drop_frame (decoder, frame);
535 ret = gst_video_decoder_allocate_output_frame (decoder, frame);
537 if (ret == GST_FLOW_OK) {
538 gst_vp8_dec_image_to_buffer (dec, img, frame->output_buffer);
539 ret = gst_video_decoder_finish_frame (decoder, frame);
541 gst_video_decoder_finish_frame (decoder, frame);
547 while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
548 GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
552 /* Invisible frame */
553 GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
554 gst_video_decoder_finish_frame (decoder, frame);
561 gst_vp8_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
564 GstStructure *config;
566 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
569 g_assert (gst_query_get_n_allocation_pools (query) > 0);
570 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
571 g_assert (pool != NULL);
573 config = gst_buffer_pool_get_config (pool);
574 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
575 gst_buffer_pool_config_add_option (config,
576 GST_BUFFER_POOL_OPTION_VIDEO_META);
578 gst_buffer_pool_set_config (pool, config);
579 gst_object_unref (pool);
584 #endif /* HAVE_VP8_DECODER */