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.
25 #if defined(HAVE_VP8_DECODER) || defined(HAVE_VP9_DECODER)
29 #include "gstvpxdec.h"
30 #include "gstvp8utils.h"
32 #include <gst/video/gstvideometa.h>
33 #include <gst/video/gstvideopool.h>
35 GST_DEBUG_CATEGORY_STATIC (gst_vpxdec_debug);
36 #define GST_CAT_DEFAULT gst_vpxdec_debug
38 #define DEFAULT_POST_PROCESSING FALSE
39 #define DEFAULT_POST_PROCESSING_FLAGS (VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE)
40 #define DEFAULT_DEBLOCKING_LEVEL 4
41 #define DEFAULT_NOISE_LEVEL 0
42 #define DEFAULT_THREADS 0
43 #define DEFAULT_VIDEO_CODEC_TAG NULL
44 #define DEFAULT_CODEC_ALGO NULL
50 PROP_POST_PROCESSING_FLAGS,
51 PROP_DEBLOCKING_LEVEL,
56 #define C_FLAGS(v) ((guint) v)
57 #define GST_VPX_DEC_TYPE_POST_PROCESSING_FLAGS (gst_vpx_dec_post_processing_flags_get_type())
59 gst_vpx_dec_post_processing_flags_get_type (void)
61 static const GFlagsValue values[] = {
62 {C_FLAGS (VP8_DEBLOCK), "Deblock", "deblock"},
63 {C_FLAGS (VP8_DEMACROBLOCK), "Demacroblock", "demacroblock"},
64 {C_FLAGS (VP8_ADDNOISE), "Add noise", "addnoise"},
65 {C_FLAGS (VP8_DEBUG_TXT_FRAME_INFO),
66 "Print frame information",
67 "visualize-frame-info"},
68 {C_FLAGS (VP8_DEBUG_TXT_MBLK_MODES),
69 "Show macroblock mode selection overlaid on image",
70 "visualize-macroblock-modes"},
71 {C_FLAGS (VP8_DEBUG_TXT_DC_DIFF),
72 "Show dc diff for each macro block overlaid on image",
74 {C_FLAGS (VP8_DEBUG_TXT_RATE_INFO),
75 "Print video rate info",
76 "visualize-rate-info"},
77 {C_FLAGS (VP8_MFQE), "Multi-frame quality enhancement", "mfqe"},
80 static volatile GType id = 0;
82 if (g_once_init_enter ((gsize *) & id)) {
85 _id = g_flags_register_static ("GstVPXDecPostProcessingFlags", values);
87 g_once_init_leave ((gsize *) & id, _id);
95 static void gst_vpx_dec_set_property (GObject * object, guint prop_id,
96 const GValue * value, GParamSpec * pspec);
97 static void gst_vpx_dec_get_property (GObject * object, guint prop_id,
98 GValue * value, GParamSpec * pspec);
100 static gboolean gst_vpx_dec_start (GstVideoDecoder * decoder);
101 static gboolean gst_vpx_dec_stop (GstVideoDecoder * decoder);
102 static gboolean gst_vpx_dec_set_format (GstVideoDecoder * decoder,
103 GstVideoCodecState * state);
104 static gboolean gst_vpx_dec_flush (GstVideoDecoder * decoder);
106 gst_vpx_dec_handle_frame (GstVideoDecoder * decoder,
107 GstVideoCodecFrame * frame);
108 static gboolean gst_vpx_dec_decide_allocation (GstVideoDecoder * decoder,
111 static void gst_vpx_dec_image_to_buffer (GstVPXDec * dec,
112 const vpx_image_t * img, GstBuffer * buffer);
113 static GstFlowReturn gst_vpx_dec_open_codec (GstVPXDec * dec,
114 GstVideoCodecFrame * frame);
115 static void gst_vpx_dec_default_send_tags (GstVPXDec * dec);
116 static void gst_vpx_dec_set_stream_info (GstVPXDec * dec,
117 vpx_codec_stream_info_t * stream_info);
118 static void gst_vpx_dec_set_default_format (GstVPXDec * dec, GstVideoFormat fmt,
119 int width, int height);
120 static gboolean gst_vpx_dec_default_frame_format (GstVPXDec * dec,
121 vpx_image_t * img, GstVideoFormat * fmt);
122 static void gst_vpx_dec_handle_resolution_change (GstVPXDec * dec,
123 vpx_image_t * img, GstVideoFormat fmt);
125 #define parent_class gst_vpx_dec_parent_class
126 G_DEFINE_TYPE (GstVPXDec, gst_vpx_dec, GST_TYPE_VIDEO_DECODER);
129 gst_vpx_dec_class_init (GstVPXDecClass * klass)
131 GObjectClass *gobject_class;
132 GstVideoDecoderClass *base_video_decoder_class;
134 gobject_class = G_OBJECT_CLASS (klass);
135 base_video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
137 gobject_class->set_property = gst_vpx_dec_set_property;
138 gobject_class->get_property = gst_vpx_dec_get_property;
140 g_object_class_install_property (gobject_class, PROP_POST_PROCESSING,
141 g_param_spec_boolean ("post-processing", "Post Processing",
142 "Enable post processing", DEFAULT_POST_PROCESSING,
143 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145 g_object_class_install_property (gobject_class, PROP_POST_PROCESSING_FLAGS,
146 g_param_spec_flags ("post-processing-flags", "Post Processing Flags",
147 "Flags to control post processing",
148 GST_VPX_DEC_TYPE_POST_PROCESSING_FLAGS, DEFAULT_POST_PROCESSING_FLAGS,
149 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (gobject_class, PROP_DEBLOCKING_LEVEL,
152 g_param_spec_uint ("deblocking-level", "Deblocking Level",
154 0, 16, DEFAULT_DEBLOCKING_LEVEL,
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_NOISE_LEVEL,
158 g_param_spec_uint ("noise-level", "Noise Level",
160 0, 16, DEFAULT_NOISE_LEVEL,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_THREADS,
164 g_param_spec_uint ("threads", "Max Threads",
165 "Maximum number of decoding threads",
166 0, 16, DEFAULT_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 base_video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_vpx_dec_start);
169 base_video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vpx_dec_stop);
170 base_video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_vpx_dec_flush);
171 base_video_decoder_class->set_format =
172 GST_DEBUG_FUNCPTR (gst_vpx_dec_set_format);
173 base_video_decoder_class->handle_frame =
174 GST_DEBUG_FUNCPTR (gst_vpx_dec_handle_frame);;
175 base_video_decoder_class->decide_allocation =
176 GST_DEBUG_FUNCPTR (gst_vpx_dec_decide_allocation);
178 klass->video_codec_tag = DEFAULT_VIDEO_CODEC_TAG;
179 klass->codec_algo = DEFAULT_CODEC_ALGO;
180 klass->open_codec = GST_DEBUG_FUNCPTR (gst_vpx_dec_open_codec);
181 klass->send_tags = GST_DEBUG_FUNCPTR (gst_vpx_dec_default_send_tags);
182 klass->set_stream_info = NULL;
183 klass->set_default_format = NULL;
184 klass->handle_resolution_change = NULL;
185 klass->get_frame_format =
186 GST_DEBUG_FUNCPTR (gst_vpx_dec_default_frame_format);
188 GST_DEBUG_CATEGORY_INIT (gst_vpxdec_debug, "vpxdec", 0, "VPX Decoder");
192 gst_vpx_dec_init (GstVPXDec * gst_vpx_dec)
194 GstVideoDecoder *decoder = (GstVideoDecoder *) gst_vpx_dec;
196 GST_DEBUG_OBJECT (gst_vpx_dec, "gst_vpx_dec_init");
197 gst_video_decoder_set_packetized (decoder, TRUE);
198 gst_vpx_dec->post_processing = DEFAULT_POST_PROCESSING;
199 gst_vpx_dec->post_processing_flags = DEFAULT_POST_PROCESSING_FLAGS;
200 gst_vpx_dec->deblocking_level = DEFAULT_DEBLOCKING_LEVEL;
201 gst_vpx_dec->noise_level = DEFAULT_NOISE_LEVEL;
203 gst_video_decoder_set_needs_format (decoder, TRUE);
204 gst_video_decoder_set_use_default_pad_acceptcaps (decoder, TRUE);
205 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (decoder));
209 gst_vpx_dec_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
214 g_return_if_fail (GST_IS_VPX_DEC (object));
215 dec = GST_VPX_DEC (object);
217 GST_DEBUG_OBJECT (object, "gst_vpx_dec_set_property");
219 case PROP_POST_PROCESSING:
220 dec->post_processing = g_value_get_boolean (value);
222 case PROP_POST_PROCESSING_FLAGS:
223 dec->post_processing_flags = g_value_get_flags (value);
225 case PROP_DEBLOCKING_LEVEL:
226 dec->deblocking_level = g_value_get_uint (value);
228 case PROP_NOISE_LEVEL:
229 dec->noise_level = g_value_get_uint (value);
232 dec->threads = g_value_get_uint (value);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gst_vpx_dec_get_property (GObject * object, guint prop_id, GValue * value,
246 g_return_if_fail (GST_IS_VPX_DEC (object));
247 dec = GST_VPX_DEC (object);
250 case PROP_POST_PROCESSING:
251 g_value_set_boolean (value, dec->post_processing);
253 case PROP_POST_PROCESSING_FLAGS:
254 g_value_set_flags (value, dec->post_processing_flags);
256 case PROP_DEBLOCKING_LEVEL:
257 g_value_set_uint (value, dec->deblocking_level);
259 case PROP_NOISE_LEVEL:
260 g_value_set_uint (value, dec->noise_level);
263 g_value_set_uint (value, dec->threads);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 gst_vpx_dec_start (GstVideoDecoder * decoder)
274 GstVPXDec *gst_vpx_dec = GST_VPX_DEC (decoder);
276 GST_DEBUG_OBJECT (gst_vpx_dec, "start");
277 gst_vpx_dec->decoder_inited = FALSE;
283 gst_vpx_dec_stop (GstVideoDecoder * base_video_decoder)
285 GstVPXDec *gst_vpx_dec = GST_VPX_DEC (base_video_decoder);
287 GST_DEBUG_OBJECT (gst_vpx_dec, "stop");
289 if (gst_vpx_dec->output_state) {
290 gst_video_codec_state_unref (gst_vpx_dec->output_state);
291 gst_vpx_dec->output_state = NULL;
294 if (gst_vpx_dec->input_state) {
295 gst_video_codec_state_unref (gst_vpx_dec->input_state);
296 gst_vpx_dec->input_state = NULL;
299 if (gst_vpx_dec->decoder_inited)
300 vpx_codec_destroy (&gst_vpx_dec->decoder);
301 gst_vpx_dec->decoder_inited = FALSE;
303 if (gst_vpx_dec->pool) {
304 gst_buffer_pool_set_active (gst_vpx_dec->pool, FALSE);
305 gst_object_unref (gst_vpx_dec->pool);
306 gst_vpx_dec->pool = NULL;
307 gst_vpx_dec->buf_size = 0;
314 gst_vpx_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
316 GstVPXDec *gst_vpx_dec = GST_VPX_DEC (decoder);
318 GST_DEBUG_OBJECT (gst_vpx_dec, "set_format");
320 if (gst_vpx_dec->decoder_inited)
321 vpx_codec_destroy (&gst_vpx_dec->decoder);
322 gst_vpx_dec->decoder_inited = FALSE;
324 if (gst_vpx_dec->output_state) {
325 gst_video_codec_state_unref (gst_vpx_dec->output_state);
326 gst_vpx_dec->output_state = NULL;
329 if (gst_vpx_dec->input_state) {
330 gst_video_codec_state_unref (gst_vpx_dec->input_state);
333 gst_vpx_dec->input_state = gst_video_codec_state_ref (state);
339 gst_vpx_dec_flush (GstVideoDecoder * base_video_decoder)
343 GST_DEBUG_OBJECT (base_video_decoder, "flush");
345 decoder = GST_VPX_DEC (base_video_decoder);
347 if (decoder->output_state) {
348 gst_video_codec_state_unref (decoder->output_state);
349 decoder->output_state = NULL;
352 if (decoder->decoder_inited)
353 vpx_codec_destroy (&decoder->decoder);
354 decoder->decoder_inited = FALSE;
360 gst_vpx_dec_default_send_tags (GstVPXDec * dec)
363 GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
365 if (vpxclass->video_codec_tag == NULL)
368 list = gst_tag_list_new_empty ();
369 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
370 GST_TAG_VIDEO_CODEC, vpxclass->video_codec_tag, NULL);
372 gst_pad_push_event (GST_VIDEO_DECODER_SRC_PAD (dec),
373 gst_event_new_tag (list));
384 gst_vpx_dec_prepare_image (GstVPXDec * dec, const vpx_image_t * img)
389 struct Frame *frame = img->fb_priv;
390 GstVideoInfo *info = &dec->output_state->info;
392 buffer = gst_buffer_ref (frame->buffer);
394 vmeta = gst_buffer_get_video_meta (buffer);
395 vmeta->format = GST_VIDEO_INFO_FORMAT (info);
396 vmeta->width = GST_VIDEO_INFO_WIDTH (info);
397 vmeta->height = GST_VIDEO_INFO_HEIGHT (info);
398 vmeta->n_planes = GST_VIDEO_INFO_N_PLANES (info);
400 for (comp = 0; comp < 4; comp++) {
401 vmeta->stride[comp] = img->stride[comp];
402 vmeta->offset[comp] =
403 img->planes[comp] ? img->planes[comp] - frame->info.data : 0;
406 /* FIXME This is a READ/WRITE mapped buffer see bug #754826 */
412 gst_vpx_dec_get_buffer_cb (gpointer priv, gsize min_size,
413 vpx_codec_frame_buffer_t * fb)
415 GstVPXDec *dec = priv;
416 GstBuffer *buffer = NULL;
420 if (!dec->pool || dec->buf_size != min_size) {
422 GstStructure *config;
424 GstAllocator *allocator;
425 GstAllocationParams params;
428 gst_buffer_pool_set_active (dec->pool, FALSE);
429 gst_object_unref (dec->pool);
434 gst_video_decoder_get_allocator (GST_VIDEO_DECODER (dec), &allocator,
438 GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC)) {
439 gst_object_unref (allocator);
443 pool = gst_buffer_pool_new ();
444 config = gst_buffer_pool_get_config (pool);
445 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
446 caps = gst_caps_from_string ("video/internal");
447 gst_buffer_pool_config_set_params (config, caps, min_size, 2, 0);
448 gst_caps_unref (caps);
449 gst_buffer_pool_set_config (pool, config);
452 gst_object_unref (allocator);
454 if (!gst_buffer_pool_set_active (pool, TRUE)) {
455 GST_WARNING ("Failed to create internal pool");
456 gst_object_unref (pool);
461 dec->buf_size = min_size;
464 ret = gst_buffer_pool_acquire_buffer (dec->pool, &buffer, NULL);
465 if (ret != GST_FLOW_OK) {
466 GST_WARNING ("Failed to acquire buffer from internal pool.");
470 /* Add it now, while the buffer is writable */
471 gst_buffer_add_video_meta (buffer, GST_VIDEO_FRAME_FLAG_NONE,
472 GST_VIDEO_FORMAT_ENCODED, 0, 0);
474 frame = g_new0 (struct Frame, 1);
475 if (!gst_buffer_map (buffer, &frame->info, GST_MAP_READWRITE)) {
476 gst_buffer_unref (buffer);
478 GST_WARNING ("Failed to map buffer from internal pool.");
482 fb->size = frame->info.size;
483 fb->data = frame->info.data;
484 frame->buffer = buffer;
487 GST_TRACE_OBJECT (priv, "Allocated buffer %p", frame->buffer);
493 gst_vpx_dec_release_buffer_cb (gpointer priv, vpx_codec_frame_buffer_t * fb)
495 struct Frame *frame = fb->priv;
497 /* We're sometimes called without a frame */
501 GST_TRACE_OBJECT (priv, "Release buffer %p", frame->buffer);
503 gst_buffer_unmap (frame->buffer, &frame->info);
504 gst_buffer_unref (frame->buffer);
513 gst_vpx_dec_image_to_buffer (GstVPXDec * dec, const vpx_image_t * img,
516 int deststride, srcstride, height, width, line, comp;
519 GstVideoInfo *info = &dec->output_state->info;
521 if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
522 GST_ERROR_OBJECT (dec, "Could not map video buffer");
526 for (comp = 0; comp < 3; comp++) {
527 dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
528 src = img->planes[comp];
529 width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp)
530 * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
531 height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
532 deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
533 srcstride = img->stride[comp];
535 if (srcstride == deststride) {
536 GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
538 memcpy (dest, src, srcstride * height);
540 GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
541 "line by line.", comp, srcstride, deststride);
542 for (line = 0; line < height; line++) {
543 memcpy (dest, src, width);
550 gst_video_frame_unmap (&frame);
554 gst_vpx_dec_open_codec (GstVPXDec * dec, GstVideoCodecFrame * frame)
557 vpx_codec_stream_info_t stream_info;
558 vpx_codec_caps_t caps;
559 vpx_codec_dec_cfg_t cfg;
560 vpx_codec_err_t status;
562 GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
564 g_return_val_if_fail (vpxclass->codec_algo != NULL, GST_FLOW_ERROR);
566 memset (&stream_info, 0, sizeof (stream_info));
567 memset (&cfg, 0, sizeof (cfg));
568 stream_info.sz = sizeof (stream_info);
570 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
571 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
572 return GST_FLOW_ERROR;
575 status = vpx_codec_peek_stream_info (vpxclass->codec_algo,
576 minfo.data, minfo.size, &stream_info);
578 gst_buffer_unmap (frame->input_buffer, &minfo);
580 if (status != VPX_CODEC_OK) {
581 GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
582 gst_vpx_error_name (status));
583 return GST_FLOW_CUSTOM_SUCCESS_1;
585 if (!stream_info.is_kf) {
586 GST_WARNING_OBJECT (dec, "No keyframe, skipping");
587 return GST_FLOW_CUSTOM_SUCCESS_1;
590 gst_vpx_dec_set_stream_info (dec, &stream_info);
591 gst_vpx_dec_set_default_format (dec, GST_VIDEO_FORMAT_I420, stream_info.w,
594 cfg.w = stream_info.w;
595 cfg.h = stream_info.h;
597 if (dec->threads > 0)
598 cfg.threads = dec->threads;
600 cfg.threads = g_get_num_processors ();
602 caps = vpx_codec_get_caps (vpxclass->codec_algo);
604 if (dec->post_processing) {
605 if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
606 GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
608 flags |= VPX_CODEC_USE_POSTPROC;
613 vpx_codec_dec_init (&dec->decoder, vpxclass->codec_algo, &cfg, flags);
614 if (status != VPX_CODEC_OK) {
615 GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
616 ("Failed to initialize VP8 decoder"), ("%s",
617 gst_vpx_error_name (status)));
618 return GST_FLOW_ERROR;
621 if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
622 vp8_postproc_cfg_t pp_cfg = { 0, };
624 pp_cfg.post_proc_flag = dec->post_processing_flags;
625 pp_cfg.deblocking_level = dec->deblocking_level;
626 pp_cfg.noise_level = dec->noise_level;
628 status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
629 if (status != VPX_CODEC_OK) {
630 GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
631 gst_vpx_error_name (status));
635 vpx_codec_set_frame_buffer_functions (&dec->decoder,
636 gst_vpx_dec_get_buffer_cb, gst_vpx_dec_release_buffer_cb, dec);
639 dec->decoder_inited = TRUE;
645 gst_vpx_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
648 GstFlowReturn ret = GST_FLOW_OK;
649 vpx_codec_err_t status;
650 vpx_codec_iter_t iter = NULL;
652 long decoder_deadline = 0;
653 GstClockTimeDiff deadline;
655 GstVPXDecClass *vpxclass;
658 GST_LOG_OBJECT (decoder, "handle_frame");
660 dec = GST_VPX_DEC (decoder);
661 vpxclass = GST_VPX_DEC_GET_CLASS (dec);
663 if (!dec->decoder_inited) {
664 ret = vpxclass->open_codec (dec, frame);
665 if (ret == GST_FLOW_CUSTOM_SUCCESS_1) {
666 gst_video_decoder_drop_frame (decoder, frame);
668 } else if (ret != GST_FLOW_OK) {
669 gst_video_codec_frame_unref (frame);
674 deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
676 decoder_deadline = 1;
677 } else if (deadline == G_MAXINT64) {
678 decoder_deadline = 0;
680 decoder_deadline = MAX (1, deadline / GST_MSECOND);
683 if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
684 GST_ERROR_OBJECT (dec, "Failed to map input buffer");
685 gst_video_codec_frame_unref (frame);
686 return GST_FLOW_ERROR;
689 status = vpx_codec_decode (&dec->decoder,
690 minfo.data, minfo.size, NULL, decoder_deadline);
692 gst_buffer_unmap (frame->input_buffer, &minfo);
695 GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
696 ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
697 gst_video_codec_frame_unref (frame);
701 img = vpx_codec_get_frame (&dec->decoder, &iter);
703 if (vpxclass->get_frame_format (dec, img, &fmt) == FALSE) {
705 GST_ELEMENT_ERROR (decoder, LIBRARY, ENCODE,
706 ("Failed to decode frame"), ("Unsupported color format %d",
708 gst_video_codec_frame_unref (frame);
709 return GST_FLOW_ERROR;
713 GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
714 (double) -deadline / GST_SECOND);
715 gst_video_decoder_drop_frame (decoder, frame);
717 gst_vpx_dec_handle_resolution_change (dec, img, fmt);
719 if (img->fb_priv && dec->have_video_meta) {
720 frame->output_buffer = gst_vpx_dec_prepare_image (dec, img);
721 ret = gst_video_decoder_finish_frame (decoder, frame);
725 ret = gst_video_decoder_allocate_output_frame (decoder, frame);
727 if (ret == GST_FLOW_OK) {
728 gst_vpx_dec_image_to_buffer (dec, img, frame->output_buffer);
729 ret = gst_video_decoder_finish_frame (decoder, frame);
731 gst_video_decoder_drop_frame (decoder, frame);
738 while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
739 GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
743 /* Invisible frame */
744 GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
745 gst_video_decoder_finish_frame (decoder, frame);
752 gst_vpx_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
754 GstVPXDec *dec = GST_VPX_DEC (bdec);
756 GstStructure *config;
758 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
761 g_assert (gst_query_get_n_allocation_pools (query) > 0);
762 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
763 g_assert (pool != NULL);
765 config = gst_buffer_pool_get_config (pool);
766 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
767 gst_buffer_pool_config_add_option (config,
768 GST_BUFFER_POOL_OPTION_VIDEO_META);
769 dec->have_video_meta = TRUE;
771 gst_buffer_pool_set_config (pool, config);
772 gst_object_unref (pool);
778 gst_vpx_dec_set_stream_info (GstVPXDec * dec,
779 vpx_codec_stream_info_t * stream_info)
781 GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
782 if (vpxclass->set_stream_info != NULL) {
783 vpxclass->set_stream_info (dec, stream_info);
788 gst_vpx_dec_set_default_format (GstVPXDec * dec, GstVideoFormat fmt, int width,
791 GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
792 if (vpxclass->set_default_format != NULL) {
793 vpxclass->set_default_format (dec, fmt, width, height);
798 gst_vpx_dec_default_frame_format (GstVPXDec * dec, vpx_image_t * img,
799 GstVideoFormat * fmt)
801 if (img->fmt == VPX_IMG_FMT_I420) {
802 *fmt = GST_VIDEO_FORMAT_I420;
811 gst_vpx_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
814 GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
815 if (vpxclass->handle_resolution_change != NULL) {
816 vpxclass->handle_resolution_change (dec, img, fmt);
820 #endif /* HAVE_VP8_DECODER || HAVE_VP9_DECODER */