vpxdec: Use threads on multi-core systems
[platform/upstream/gst-plugins-good.git] / ext / vpx / gstvp8dec.c
1 /* VP8
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>
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22 /**
23  * SECTION:element-vp8dec
24  * @see_also: vp8enc, matroskademux
25  *
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
30  * Theora video codec.
31  *
32  * <refsect2>
33  * <title>Example pipeline</title>
34  * |[
35  * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! vp8dec ! videoconvert ! videoscale ! autovideosink
36  * ]| This example pipeline will decode a WebM stream and decodes the VP8 video.
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #ifdef HAVE_VP8_DECODER
45
46 #include <string.h>
47
48 #include "gstvp8dec.h"
49 #include "gstvp8utils.h"
50
51 #include <gst/video/gstvideometa.h>
52 #include <gst/video/gstvideopool.h>
53
54 GST_DEBUG_CATEGORY_STATIC (gst_vp8dec_debug);
55 #define GST_CAT_DEFAULT gst_vp8dec_debug
56
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 0
62
63 enum
64 {
65   PROP_0,
66   PROP_POST_PROCESSING,
67   PROP_POST_PROCESSING_FLAGS,
68   PROP_DEBLOCKING_LEVEL,
69   PROP_NOISE_LEVEL,
70   PROP_THREADS
71 };
72
73 #define C_FLAGS(v) ((guint) v)
74 #define GST_VP8_DEC_TYPE_POST_PROCESSING_FLAGS (gst_vp8_dec_post_processing_flags_get_type())
75 static GType
76 gst_vp8_dec_post_processing_flags_get_type (void)
77 {
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"},
83     {0, NULL, NULL}
84   };
85   static volatile GType id = 0;
86
87   if (g_once_init_enter ((gsize *) & id)) {
88     GType _id;
89
90     _id = g_flags_register_static ("GstVP8DecPostProcessingFlags", values);
91
92     g_once_init_leave ((gsize *) & id, _id);
93   }
94
95   return id;
96 }
97
98 #undef C_FLAGS
99
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);
104
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_flush (GstVideoDecoder * decoder);
110 static GstFlowReturn gst_vp8_dec_handle_frame (GstVideoDecoder * decoder,
111     GstVideoCodecFrame * frame);
112 static gboolean gst_vp8_dec_decide_allocation (GstVideoDecoder * decoder,
113     GstQuery * query);
114
115 static GstStaticPadTemplate gst_vp8_dec_sink_template =
116 GST_STATIC_PAD_TEMPLATE ("sink",
117     GST_PAD_SINK,
118     GST_PAD_ALWAYS,
119     GST_STATIC_CAPS ("video/x-vp8")
120     );
121
122 static GstStaticPadTemplate gst_vp8_dec_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
124     GST_PAD_SRC,
125     GST_PAD_ALWAYS,
126     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
127     );
128
129 #define parent_class gst_vp8_dec_parent_class
130 G_DEFINE_TYPE (GstVP8Dec, gst_vp8_dec, GST_TYPE_VIDEO_DECODER);
131
132 static void
133 gst_vp8_dec_class_init (GstVP8DecClass * klass)
134 {
135   GObjectClass *gobject_class;
136   GstElementClass *element_class;
137   GstVideoDecoderClass *base_video_decoder_class;
138
139   gobject_class = G_OBJECT_CLASS (klass);
140   element_class = GST_ELEMENT_CLASS (klass);
141   base_video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
142
143   gobject_class->set_property = gst_vp8_dec_set_property;
144   gobject_class->get_property = gst_vp8_dec_get_property;
145
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));
150
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));
156
157   g_object_class_install_property (gobject_class, PROP_DEBLOCKING_LEVEL,
158       g_param_spec_uint ("deblocking-level", "Deblocking Level",
159           "Deblocking level",
160           0, 16, DEFAULT_DEBLOCKING_LEVEL,
161           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162
163   g_object_class_install_property (gobject_class, PROP_NOISE_LEVEL,
164       g_param_spec_uint ("noise-level", "Noise Level",
165           "Noise level",
166           0, 16, DEFAULT_NOISE_LEVEL,
167           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168
169   g_object_class_install_property (gobject_class, PROP_THREADS,
170       g_param_spec_uint ("threads", "Max Threads",
171           "Maximum number of decoding threads (0 = automatic)",
172           0, 16, DEFAULT_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173
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));
178
179   gst_element_class_set_static_metadata (element_class,
180       "On2 VP8 Decoder",
181       "Codec/Decoder/Video",
182       "Decode VP8 video streams", "David Schleef <ds@entropywave.com>, "
183       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
184
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->flush = GST_DEBUG_FUNCPTR (gst_vp8_dec_flush);
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;
193
194   GST_DEBUG_CATEGORY_INIT (gst_vp8dec_debug, "vp8dec", 0, "VP8 Decoder");
195 }
196
197 static void
198 gst_vp8_dec_init (GstVP8Dec * gst_vp8_dec)
199 {
200   GstVideoDecoder *decoder = (GstVideoDecoder *) gst_vp8_dec;
201
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;
208
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));
212 }
213
214 static void
215 gst_vp8_dec_set_property (GObject * object, guint prop_id,
216     const GValue * value, GParamSpec * pspec)
217 {
218   GstVP8Dec *dec;
219
220   g_return_if_fail (GST_IS_VP8_DEC (object));
221   dec = GST_VP8_DEC (object);
222
223   GST_DEBUG_OBJECT (object, "gst_vp8_dec_set_property");
224   switch (prop_id) {
225     case PROP_POST_PROCESSING:
226       dec->post_processing = g_value_get_boolean (value);
227       break;
228     case PROP_POST_PROCESSING_FLAGS:
229       dec->post_processing_flags = g_value_get_flags (value);
230       break;
231     case PROP_DEBLOCKING_LEVEL:
232       dec->deblocking_level = g_value_get_uint (value);
233       break;
234     case PROP_NOISE_LEVEL:
235       dec->noise_level = g_value_get_uint (value);
236       break;
237     case PROP_THREADS:
238       dec->threads = g_value_get_uint (value);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static void
247 gst_vp8_dec_get_property (GObject * object, guint prop_id, GValue * value,
248     GParamSpec * pspec)
249 {
250   GstVP8Dec *dec;
251
252   g_return_if_fail (GST_IS_VP8_DEC (object));
253   dec = GST_VP8_DEC (object);
254
255   switch (prop_id) {
256     case PROP_POST_PROCESSING:
257       g_value_set_boolean (value, dec->post_processing);
258       break;
259     case PROP_POST_PROCESSING_FLAGS:
260       g_value_set_flags (value, dec->post_processing_flags);
261       break;
262     case PROP_DEBLOCKING_LEVEL:
263       g_value_set_uint (value, dec->deblocking_level);
264       break;
265     case PROP_NOISE_LEVEL:
266       g_value_set_uint (value, dec->noise_level);
267       break;
268     case PROP_THREADS:
269       g_value_set_uint (value, dec->threads);
270       break;
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274   }
275 }
276
277 static gboolean
278 gst_vp8_dec_start (GstVideoDecoder * decoder)
279 {
280   GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (decoder);
281
282   GST_DEBUG_OBJECT (gst_vp8_dec, "start");
283   gst_vp8_dec->decoder_inited = FALSE;
284
285   return TRUE;
286 }
287
288 static gboolean
289 gst_vp8_dec_stop (GstVideoDecoder * base_video_decoder)
290 {
291   GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (base_video_decoder);
292
293   GST_DEBUG_OBJECT (gst_vp8_dec, "stop");
294
295   if (gst_vp8_dec->output_state) {
296     gst_video_codec_state_unref (gst_vp8_dec->output_state);
297     gst_vp8_dec->output_state = NULL;
298   }
299
300   if (gst_vp8_dec->input_state) {
301     gst_video_codec_state_unref (gst_vp8_dec->input_state);
302     gst_vp8_dec->input_state = NULL;
303   }
304
305   if (gst_vp8_dec->decoder_inited)
306     vpx_codec_destroy (&gst_vp8_dec->decoder);
307   gst_vp8_dec->decoder_inited = FALSE;
308
309   return TRUE;
310 }
311
312 static gboolean
313 gst_vp8_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
314 {
315   GstVP8Dec *gst_vp8_dec = GST_VP8_DEC (decoder);
316
317   GST_DEBUG_OBJECT (gst_vp8_dec, "set_format");
318
319   if (gst_vp8_dec->decoder_inited)
320     vpx_codec_destroy (&gst_vp8_dec->decoder);
321   gst_vp8_dec->decoder_inited = FALSE;
322
323   if (gst_vp8_dec->output_state) {
324     gst_video_codec_state_unref (gst_vp8_dec->output_state);
325     gst_vp8_dec->output_state = NULL;
326   }
327
328   if (gst_vp8_dec->input_state) {
329     gst_video_codec_state_unref (gst_vp8_dec->input_state);
330     gst_vp8_dec->input_state = NULL;
331   }
332
333   gst_vp8_dec->input_state = gst_video_codec_state_ref (state);
334
335   return TRUE;
336 }
337
338 static gboolean
339 gst_vp8_dec_flush (GstVideoDecoder * base_video_decoder)
340 {
341   GstVP8Dec *decoder;
342
343   GST_DEBUG_OBJECT (base_video_decoder, "flush");
344
345   decoder = GST_VP8_DEC (base_video_decoder);
346
347   if (decoder->output_state) {
348     gst_video_codec_state_unref (decoder->output_state);
349     decoder->output_state = NULL;
350   }
351
352   if (decoder->decoder_inited)
353     vpx_codec_destroy (&decoder->decoder);
354   decoder->decoder_inited = FALSE;
355
356   return TRUE;
357 }
358
359 static void
360 gst_vp8_dec_send_tags (GstVP8Dec * dec)
361 {
362   GstTagList *list;
363
364   list = gst_tag_list_new_empty ();
365   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
366       GST_TAG_VIDEO_CODEC, "VP8 video", NULL);
367
368   gst_pad_push_event (GST_VIDEO_DECODER_SRC_PAD (dec),
369       gst_event_new_tag (list));
370 }
371
372 static void
373 gst_vp8_dec_image_to_buffer (GstVP8Dec * dec, const vpx_image_t * img,
374     GstBuffer * buffer)
375 {
376   int deststride, srcstride, height, width, line, comp;
377   guint8 *dest, *src;
378   GstVideoFrame frame;
379   GstVideoInfo *info = &dec->output_state->info;
380
381   if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
382     GST_ERROR_OBJECT (dec, "Could not map video buffer");
383     return;
384   }
385
386   for (comp = 0; comp < 3; comp++) {
387     dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
388     src = img->planes[comp];
389     width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp)
390         * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
391     height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
392     deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
393     srcstride = img->stride[comp];
394
395     if (srcstride == deststride) {
396       GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
397           comp, srcstride);
398       memcpy (dest, src, srcstride * height);
399     } else {
400       GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
401           "line by line.", comp, srcstride, deststride);
402       for (line = 0; line < height; line++) {
403         memcpy (dest, src, width);
404         dest += deststride;
405         src += srcstride;
406       }
407     }
408   }
409
410   gst_video_frame_unmap (&frame);
411 }
412
413 static GstFlowReturn
414 open_codec (GstVP8Dec * dec, GstVideoCodecFrame * frame)
415 {
416   int flags = 0;
417   vpx_codec_stream_info_t stream_info;
418   vpx_codec_caps_t caps;
419   vpx_codec_dec_cfg_t cfg;
420   GstVideoCodecState *state = dec->input_state;
421   vpx_codec_err_t status;
422   GstMapInfo minfo;
423
424   memset (&stream_info, 0, sizeof (stream_info));
425   memset (&cfg, 0, sizeof (cfg));
426   stream_info.sz = sizeof (stream_info);
427
428   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
429     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
430     return GST_FLOW_ERROR;
431   }
432
433   status = vpx_codec_peek_stream_info (&vpx_codec_vp8_dx_algo,
434       minfo.data, minfo.size, &stream_info);
435
436   gst_buffer_unmap (frame->input_buffer, &minfo);
437
438   if (status != VPX_CODEC_OK) {
439     GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
440         gst_vpx_error_name (status));
441     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
442     return GST_FLOW_CUSTOM_SUCCESS_1;
443   }
444   if (!stream_info.is_kf) {
445     GST_WARNING_OBJECT (dec, "No keyframe, skipping");
446     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
447     return GST_FLOW_CUSTOM_SUCCESS_1;
448   }
449
450   g_assert (dec->output_state == NULL);
451   dec->output_state =
452       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
453       GST_VIDEO_FORMAT_I420, stream_info.w, stream_info.h, state);
454   gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
455   gst_vp8_dec_send_tags (dec);
456
457   cfg.w = stream_info.w;
458   cfg.h = stream_info.h;
459
460   if (dec->threads > 0)
461     cfg.threads = dec->threads;
462   else
463     cfg.threads = g_get_num_processors ();
464
465   caps = vpx_codec_get_caps (&vpx_codec_vp8_dx_algo);
466
467   if (dec->post_processing) {
468     if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
469       GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
470     } else {
471       flags |= VPX_CODEC_USE_POSTPROC;
472     }
473   }
474
475   status =
476       vpx_codec_dec_init (&dec->decoder, &vpx_codec_vp8_dx_algo, &cfg, flags);
477   if (status != VPX_CODEC_OK) {
478     GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
479         ("Failed to initialize VP8 decoder"), ("%s",
480             gst_vpx_error_name (status)));
481     return GST_FLOW_ERROR;
482   }
483
484   if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
485     vp8_postproc_cfg_t pp_cfg = { 0, };
486
487     pp_cfg.post_proc_flag = dec->post_processing_flags;
488     pp_cfg.deblocking_level = dec->deblocking_level;
489     pp_cfg.noise_level = dec->noise_level;
490
491     status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
492     if (status != VPX_CODEC_OK) {
493       GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
494           gst_vpx_error_name (status));
495     }
496   }
497
498   dec->decoder_inited = TRUE;
499
500   return GST_FLOW_OK;
501 }
502
503 static GstFlowReturn
504 gst_vp8_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
505 {
506   GstVP8Dec *dec;
507   GstFlowReturn ret = GST_FLOW_OK;
508   vpx_codec_err_t status;
509   vpx_codec_iter_t iter = NULL;
510   vpx_image_t *img;
511   long decoder_deadline = 0;
512   GstClockTimeDiff deadline;
513   GstMapInfo minfo;
514   GstVideoInfo *info;
515   GstVideoCodecState *new_output_state;
516
517   GST_LOG_OBJECT (decoder, "handle_frame");
518
519   dec = GST_VP8_DEC (decoder);
520
521   if (!dec->decoder_inited) {
522     ret = open_codec (dec, frame);
523     if (ret == GST_FLOW_CUSTOM_SUCCESS_1)
524       return GST_FLOW_OK;
525     else if (ret != GST_FLOW_OK)
526       return ret;
527   }
528
529   deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
530   if (deadline < 0) {
531     decoder_deadline = 1;
532   } else if (deadline == G_MAXINT64) {
533     decoder_deadline = 0;
534   } else {
535     decoder_deadline = MAX (1, deadline / GST_MSECOND);
536   }
537
538   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
539     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
540     return GST_FLOW_ERROR;
541   }
542
543   status = vpx_codec_decode (&dec->decoder,
544       minfo.data, minfo.size, NULL, decoder_deadline);
545
546   gst_buffer_unmap (frame->input_buffer, &minfo);
547
548   if (status) {
549     GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
550         ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
551     return ret;
552   }
553
554   img = vpx_codec_get_frame (&dec->decoder, &iter);
555   if (img) {
556     if (img->fmt != VPX_IMG_FMT_I420) {
557       vpx_img_free (img);
558       GST_ELEMENT_ERROR (decoder, LIBRARY, ENCODE,
559           ("Failed to decode frame"), ("Unsupported color format %d",
560               img->fmt));
561       return GST_FLOW_ERROR;
562     }
563
564     if (deadline < 0) {
565       GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
566           (double) -deadline / GST_SECOND);
567       gst_video_decoder_drop_frame (decoder, frame);
568     } else {
569       info = &dec->output_state->info;
570       if (GST_VIDEO_INFO_WIDTH (info) != img->d_w
571           || GST_VIDEO_INFO_HEIGHT (info) != img->d_h) {
572         GST_DEBUG_OBJECT (dec,
573             "Changed output resolution was %d x %d now is got %u x %u (display %u x %u)",
574             GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info), img->w,
575             img->h, img->d_w, img->d_h);
576
577         new_output_state =
578             gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
579             GST_VIDEO_FORMAT_I420, img->d_w, img->d_h, dec->output_state);
580         if (dec->output_state) {
581           gst_video_codec_state_unref (dec->output_state);
582         }
583         dec->output_state = new_output_state;
584         /* No need to call negotiate() here, it will be automatically called
585          * by allocate_output_frame() below */
586       }
587
588       ret = gst_video_decoder_allocate_output_frame (decoder, frame);
589
590       if (ret == GST_FLOW_OK) {
591         gst_vp8_dec_image_to_buffer (dec, img, frame->output_buffer);
592         ret = gst_video_decoder_finish_frame (decoder, frame);
593       } else {
594         gst_video_decoder_drop_frame (decoder, frame);
595       }
596     }
597
598     vpx_img_free (img);
599
600     while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
601       GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
602       vpx_img_free (img);
603     }
604   } else {
605     /* Invisible frame */
606     GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
607     gst_video_decoder_finish_frame (decoder, frame);
608   }
609
610   return ret;
611 }
612
613 static gboolean
614 gst_vp8_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
615 {
616   GstBufferPool *pool;
617   GstStructure *config;
618
619   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
620     return FALSE;
621
622   g_assert (gst_query_get_n_allocation_pools (query) > 0);
623   gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
624   g_assert (pool != NULL);
625
626   config = gst_buffer_pool_get_config (pool);
627   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
628     gst_buffer_pool_config_add_option (config,
629         GST_BUFFER_POOL_OPTION_VIDEO_META);
630   }
631   gst_buffer_pool_set_config (pool, config);
632   gst_object_unref (pool);
633
634   return TRUE;
635 }
636
637 #endif /* HAVE_VP8_DECODER */