vpxdec: Initialize buffer variable to NULL
[platform/upstream/gst-plugins-good.git] / ext / vpx / gstvpxdec.c
1 /* VPX
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #if defined(HAVE_VP8_DECODER) || defined(HAVE_VP9_DECODER)
26
27 #include <string.h>
28
29 #include "gstvpxdec.h"
30 #include "gstvp8utils.h"
31
32 #include <gst/video/gstvideometa.h>
33 #include <gst/video/gstvideopool.h>
34
35 GST_DEBUG_CATEGORY_STATIC (gst_vpxdec_debug);
36 #define GST_CAT_DEFAULT gst_vpxdec_debug
37
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
45
46 enum
47 {
48   PROP_0,
49   PROP_POST_PROCESSING,
50   PROP_POST_PROCESSING_FLAGS,
51   PROP_DEBLOCKING_LEVEL,
52   PROP_NOISE_LEVEL,
53   PROP_THREADS
54 };
55
56 #define C_FLAGS(v) ((guint) v)
57 #define GST_VPX_DEC_TYPE_POST_PROCESSING_FLAGS (gst_vpx_dec_post_processing_flags_get_type())
58 static GType
59 gst_vpx_dec_post_processing_flags_get_type (void)
60 {
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_MFQE), "Multi-frame quality enhancement", "mfqe"},
66     {0, NULL, NULL}
67   };
68   static volatile GType id = 0;
69
70   if (g_once_init_enter ((gsize *) & id)) {
71     GType _id;
72
73     _id = g_flags_register_static ("GstVPXDecPostProcessingFlags", values);
74
75     g_once_init_leave ((gsize *) & id, _id);
76   }
77
78   return id;
79 }
80
81 #undef C_FLAGS
82
83 static void gst_vpx_dec_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_vpx_dec_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87
88 static gboolean gst_vpx_dec_start (GstVideoDecoder * decoder);
89 static gboolean gst_vpx_dec_stop (GstVideoDecoder * decoder);
90 static gboolean gst_vpx_dec_set_format (GstVideoDecoder * decoder,
91     GstVideoCodecState * state);
92 static gboolean gst_vpx_dec_flush (GstVideoDecoder * decoder);
93 static GstFlowReturn
94 gst_vpx_dec_handle_frame (GstVideoDecoder * decoder,
95     GstVideoCodecFrame * frame);
96 static gboolean gst_vpx_dec_decide_allocation (GstVideoDecoder * decoder,
97     GstQuery * query);
98
99 static void gst_vpx_dec_image_to_buffer (GstVPXDec * dec,
100     const vpx_image_t * img, GstBuffer * buffer);
101 static GstFlowReturn gst_vpx_dec_open_codec (GstVPXDec * dec,
102     GstVideoCodecFrame * frame);
103 static void gst_vpx_dec_default_send_tags (GstVPXDec * dec);
104 static void gst_vpx_dec_set_stream_info (GstVPXDec * dec,
105     vpx_codec_stream_info_t * stream_info);
106 static void gst_vpx_dec_set_default_format (GstVPXDec * dec, GstVideoFormat fmt,
107     int width, int height);
108 static gboolean gst_vpx_dec_default_frame_format (GstVPXDec * dec,
109     vpx_image_t * img, GstVideoFormat * fmt);
110 static void gst_vpx_dec_handle_resolution_change (GstVPXDec * dec,
111     vpx_image_t * img, GstVideoFormat fmt);
112
113 #define parent_class gst_vpx_dec_parent_class
114 G_DEFINE_TYPE (GstVPXDec, gst_vpx_dec, GST_TYPE_VIDEO_DECODER);
115
116 static void
117 gst_vpx_dec_class_init (GstVPXDecClass * klass)
118 {
119   GObjectClass *gobject_class;
120   GstVideoDecoderClass *base_video_decoder_class;
121
122   gobject_class = G_OBJECT_CLASS (klass);
123   base_video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
124
125   gobject_class->set_property = gst_vpx_dec_set_property;
126   gobject_class->get_property = gst_vpx_dec_get_property;
127
128   g_object_class_install_property (gobject_class, PROP_POST_PROCESSING,
129       g_param_spec_boolean ("post-processing", "Post Processing",
130           "Enable post processing", DEFAULT_POST_PROCESSING,
131           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132
133   g_object_class_install_property (gobject_class, PROP_POST_PROCESSING_FLAGS,
134       g_param_spec_flags ("post-processing-flags", "Post Processing Flags",
135           "Flags to control post processing",
136           GST_VPX_DEC_TYPE_POST_PROCESSING_FLAGS, DEFAULT_POST_PROCESSING_FLAGS,
137           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138
139   g_object_class_install_property (gobject_class, PROP_DEBLOCKING_LEVEL,
140       g_param_spec_uint ("deblocking-level", "Deblocking Level",
141           "Deblocking level",
142           0, 16, DEFAULT_DEBLOCKING_LEVEL,
143           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144
145   g_object_class_install_property (gobject_class, PROP_NOISE_LEVEL,
146       g_param_spec_uint ("noise-level", "Noise Level",
147           "Noise level",
148           0, 16, DEFAULT_NOISE_LEVEL,
149           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150
151   g_object_class_install_property (gobject_class, PROP_THREADS,
152       g_param_spec_uint ("threads", "Max Threads",
153           "Maximum number of decoding threads",
154           0, 16, DEFAULT_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155
156   base_video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_vpx_dec_start);
157   base_video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_vpx_dec_stop);
158   base_video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_vpx_dec_flush);
159   base_video_decoder_class->set_format =
160       GST_DEBUG_FUNCPTR (gst_vpx_dec_set_format);
161   base_video_decoder_class->handle_frame =
162       GST_DEBUG_FUNCPTR (gst_vpx_dec_handle_frame);;
163   base_video_decoder_class->decide_allocation =
164       GST_DEBUG_FUNCPTR (gst_vpx_dec_decide_allocation);
165
166   klass->video_codec_tag = DEFAULT_VIDEO_CODEC_TAG;
167   klass->codec_algo = DEFAULT_CODEC_ALGO;
168   klass->open_codec = GST_DEBUG_FUNCPTR (gst_vpx_dec_open_codec);
169   klass->send_tags = GST_DEBUG_FUNCPTR (gst_vpx_dec_default_send_tags);
170   klass->set_stream_info = NULL;
171   klass->set_default_format = NULL;
172   klass->handle_resolution_change = NULL;
173   klass->get_frame_format =
174       GST_DEBUG_FUNCPTR (gst_vpx_dec_default_frame_format);
175
176   GST_DEBUG_CATEGORY_INIT (gst_vpxdec_debug, "vpxdec", 0, "VPX Decoder");
177 }
178
179 static void
180 gst_vpx_dec_init (GstVPXDec * gst_vpx_dec)
181 {
182   GstVideoDecoder *decoder = (GstVideoDecoder *) gst_vpx_dec;
183
184   GST_DEBUG_OBJECT (gst_vpx_dec, "gst_vpx_dec_init");
185   gst_video_decoder_set_packetized (decoder, TRUE);
186   gst_vpx_dec->post_processing = DEFAULT_POST_PROCESSING;
187   gst_vpx_dec->post_processing_flags = DEFAULT_POST_PROCESSING_FLAGS;
188   gst_vpx_dec->deblocking_level = DEFAULT_DEBLOCKING_LEVEL;
189   gst_vpx_dec->noise_level = DEFAULT_NOISE_LEVEL;
190
191   gst_video_decoder_set_needs_format (decoder, TRUE);
192   gst_video_decoder_set_use_default_pad_acceptcaps (decoder, TRUE);
193   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (decoder));
194 }
195
196 static void
197 gst_vpx_dec_set_property (GObject * object, guint prop_id,
198     const GValue * value, GParamSpec * pspec)
199 {
200   GstVPXDec *dec;
201
202   g_return_if_fail (GST_IS_VPX_DEC (object));
203   dec = GST_VPX_DEC (object);
204
205   GST_DEBUG_OBJECT (object, "gst_vpx_dec_set_property");
206   switch (prop_id) {
207     case PROP_POST_PROCESSING:
208       dec->post_processing = g_value_get_boolean (value);
209       break;
210     case PROP_POST_PROCESSING_FLAGS:
211       dec->post_processing_flags = g_value_get_flags (value);
212       break;
213     case PROP_DEBLOCKING_LEVEL:
214       dec->deblocking_level = g_value_get_uint (value);
215       break;
216     case PROP_NOISE_LEVEL:
217       dec->noise_level = g_value_get_uint (value);
218       break;
219     case PROP_THREADS:
220       dec->threads = g_value_get_uint (value);
221       break;
222     default:
223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224       break;
225   }
226 }
227
228 static void
229 gst_vpx_dec_get_property (GObject * object, guint prop_id, GValue * value,
230     GParamSpec * pspec)
231 {
232   GstVPXDec *dec;
233
234   g_return_if_fail (GST_IS_VPX_DEC (object));
235   dec = GST_VPX_DEC (object);
236
237   switch (prop_id) {
238     case PROP_POST_PROCESSING:
239       g_value_set_boolean (value, dec->post_processing);
240       break;
241     case PROP_POST_PROCESSING_FLAGS:
242       g_value_set_flags (value, dec->post_processing_flags);
243       break;
244     case PROP_DEBLOCKING_LEVEL:
245       g_value_set_uint (value, dec->deblocking_level);
246       break;
247     case PROP_NOISE_LEVEL:
248       g_value_set_uint (value, dec->noise_level);
249       break;
250     case PROP_THREADS:
251       g_value_set_uint (value, dec->threads);
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256   }
257 }
258
259 static gboolean
260 gst_vpx_dec_start (GstVideoDecoder * decoder)
261 {
262   GstVPXDec *gst_vpx_dec = GST_VPX_DEC (decoder);
263
264   GST_DEBUG_OBJECT (gst_vpx_dec, "start");
265   gst_vpx_dec->decoder_inited = FALSE;
266
267   return TRUE;
268 }
269
270 static gboolean
271 gst_vpx_dec_stop (GstVideoDecoder * base_video_decoder)
272 {
273   GstVPXDec *gst_vpx_dec = GST_VPX_DEC (base_video_decoder);
274
275   GST_DEBUG_OBJECT (gst_vpx_dec, "stop");
276
277   if (gst_vpx_dec->output_state) {
278     gst_video_codec_state_unref (gst_vpx_dec->output_state);
279     gst_vpx_dec->output_state = NULL;
280   }
281
282   if (gst_vpx_dec->input_state) {
283     gst_video_codec_state_unref (gst_vpx_dec->input_state);
284     gst_vpx_dec->input_state = NULL;
285   }
286
287   if (gst_vpx_dec->decoder_inited)
288     vpx_codec_destroy (&gst_vpx_dec->decoder);
289   gst_vpx_dec->decoder_inited = FALSE;
290
291   if (gst_vpx_dec->pool) {
292     gst_buffer_pool_set_active (gst_vpx_dec->pool, FALSE);
293     gst_object_unref (gst_vpx_dec->pool);
294     gst_vpx_dec->pool = NULL;
295     gst_vpx_dec->buf_size = 0;
296   }
297
298   return TRUE;
299 }
300
301 static gboolean
302 gst_vpx_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
303 {
304   GstVPXDec *gst_vpx_dec = GST_VPX_DEC (decoder);
305
306   GST_DEBUG_OBJECT (gst_vpx_dec, "set_format");
307
308   if (gst_vpx_dec->decoder_inited)
309     vpx_codec_destroy (&gst_vpx_dec->decoder);
310   gst_vpx_dec->decoder_inited = FALSE;
311
312   if (gst_vpx_dec->output_state) {
313     gst_video_codec_state_unref (gst_vpx_dec->output_state);
314     gst_vpx_dec->output_state = NULL;
315   }
316
317   if (gst_vpx_dec->input_state) {
318     gst_video_codec_state_unref (gst_vpx_dec->input_state);
319   }
320
321   gst_vpx_dec->input_state = gst_video_codec_state_ref (state);
322
323   return TRUE;
324 }
325
326 static gboolean
327 gst_vpx_dec_flush (GstVideoDecoder * base_video_decoder)
328 {
329   GstVPXDec *decoder;
330
331   GST_DEBUG_OBJECT (base_video_decoder, "flush");
332
333   decoder = GST_VPX_DEC (base_video_decoder);
334
335   if (decoder->output_state) {
336     gst_video_codec_state_unref (decoder->output_state);
337     decoder->output_state = NULL;
338   }
339
340   if (decoder->decoder_inited)
341     vpx_codec_destroy (&decoder->decoder);
342   decoder->decoder_inited = FALSE;
343
344   return TRUE;
345 }
346
347 static void
348 gst_vpx_dec_default_send_tags (GstVPXDec * dec)
349 {
350   GstTagList *list;
351   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
352
353   if (vpxclass->video_codec_tag == NULL)
354     return;
355
356   list = gst_tag_list_new_empty ();
357   gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
358       GST_TAG_VIDEO_CODEC, vpxclass->video_codec_tag, NULL);
359
360   gst_pad_push_event (GST_VIDEO_DECODER_SRC_PAD (dec),
361       gst_event_new_tag (list));
362 }
363
364 #ifdef HAVE_VPX_1_4
365 struct Frame
366 {
367   GstMapInfo info;
368   GstBuffer *buffer;
369 };
370
371 static GstBuffer *
372 gst_vpx_dec_prepare_image (GstVPXDec * dec, const vpx_image_t * img)
373 {
374   gint comp;
375   GstVideoMeta *vmeta;
376   GstBuffer *buffer;
377   struct Frame *frame = img->fb_priv;
378   GstVideoInfo *info = &dec->output_state->info;
379
380   buffer = gst_buffer_ref (frame->buffer);
381
382   vmeta = gst_buffer_get_video_meta (buffer);
383   vmeta->format = GST_VIDEO_INFO_FORMAT (info);
384   vmeta->width = GST_VIDEO_INFO_WIDTH (info);
385   vmeta->height = GST_VIDEO_INFO_HEIGHT (info);
386   vmeta->n_planes = GST_VIDEO_INFO_N_PLANES (info);
387
388   for (comp = 0; comp < 4; comp++) {
389     vmeta->stride[comp] = img->stride[comp];
390     vmeta->offset[comp] =
391         img->planes[comp] ? img->planes[comp] - frame->info.data : 0;
392   }
393
394   /* FIXME This is a READ/WRITE mapped buffer see bug #754826 */
395
396   return buffer;
397 }
398
399 static int
400 gst_vpx_dec_get_buffer_cb (gpointer priv, gsize min_size,
401     vpx_codec_frame_buffer_t * fb)
402 {
403   GstVPXDec *dec = priv;
404   GstBuffer *buffer = NULL;
405   struct Frame *frame;
406   GstFlowReturn ret;
407
408   if (!dec->pool || dec->buf_size != min_size) {
409     GstBufferPool *pool;
410     GstStructure *config;
411     GstCaps *caps;
412     GstAllocator *allocator;
413     GstAllocationParams params;
414
415     if (dec->pool) {
416       gst_buffer_pool_set_active (dec->pool, FALSE);
417       gst_object_unref (dec->pool);
418       dec->pool = NULL;
419       dec->buf_size = 0;
420     }
421
422     gst_video_decoder_get_allocator (GST_VIDEO_DECODER (dec), &allocator,
423         &params);
424
425     if (allocator &&
426         GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC)) {
427       gst_object_unref (allocator);
428       allocator = NULL;
429     }
430
431     pool = gst_buffer_pool_new ();
432     config = gst_buffer_pool_get_config (pool);
433     gst_buffer_pool_config_set_allocator (config, allocator, &params);
434     caps = gst_caps_from_string ("video/internal");
435     gst_buffer_pool_config_set_params (config, caps, min_size, 2, 0);
436     gst_caps_unref (caps);
437     gst_buffer_pool_set_config (pool, config);
438
439     if (allocator)
440       gst_object_unref (allocator);
441
442     if (!gst_buffer_pool_set_active (pool, TRUE)) {
443       GST_WARNING ("Failed to create internal pool");
444       gst_object_unref (pool);
445       return -1;
446     }
447
448     dec->pool = pool;
449     dec->buf_size = min_size;
450   }
451
452   ret = gst_buffer_pool_acquire_buffer (dec->pool, &buffer, NULL);
453   if (ret != GST_FLOW_OK) {
454     GST_WARNING ("Failed to acquire buffer from internal pool.");
455     return -1;
456   }
457
458   /* Add it now, while the buffer is writable */
459   gst_buffer_add_video_meta (buffer, GST_VIDEO_FRAME_FLAG_NONE,
460       GST_VIDEO_FORMAT_ENCODED, 0, 0);
461
462   frame = g_new0 (struct Frame, 1);
463   if (!gst_buffer_map (buffer, &frame->info, GST_MAP_READWRITE)) {
464     gst_buffer_unref (buffer);
465     g_free (frame);
466     GST_WARNING ("Failed to map buffer from internal pool.");
467     return -1;
468   }
469
470   fb->size = frame->info.size;
471   fb->data = frame->info.data;
472   frame->buffer = buffer;
473   fb->priv = frame;
474
475   GST_TRACE_OBJECT (priv, "Allocated buffer %p", frame->buffer);
476
477   return 0;
478 }
479
480 static int
481 gst_vpx_dec_release_buffer_cb (gpointer priv, vpx_codec_frame_buffer_t * fb)
482 {
483   struct Frame *frame = fb->priv;
484
485   GST_TRACE_OBJECT (priv, "Release buffer %p", frame->buffer);
486
487   g_assert (frame);
488   gst_buffer_unmap (frame->buffer, &frame->info);
489   gst_buffer_unref (frame->buffer);
490   g_free (frame);
491
492   return 0;
493 }
494 #endif
495
496 static void
497 gst_vpx_dec_image_to_buffer (GstVPXDec * dec, const vpx_image_t * img,
498     GstBuffer * buffer)
499 {
500   int deststride, srcstride, height, width, line, comp;
501   guint8 *dest, *src;
502   GstVideoFrame frame;
503   GstVideoInfo *info = &dec->output_state->info;
504
505   if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
506     GST_ERROR_OBJECT (dec, "Could not map video buffer");
507     return;
508   }
509
510   for (comp = 0; comp < 3; comp++) {
511     dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
512     src = img->planes[comp];
513     width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp)
514         * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
515     height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
516     deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
517     srcstride = img->stride[comp];
518
519     if (srcstride == deststride) {
520       GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
521           comp, srcstride);
522       memcpy (dest, src, srcstride * height);
523     } else {
524       GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
525           "line by line.", comp, srcstride, deststride);
526       for (line = 0; line < height; line++) {
527         memcpy (dest, src, width);
528         dest += deststride;
529         src += srcstride;
530       }
531     }
532   }
533
534   gst_video_frame_unmap (&frame);
535 }
536
537 static GstFlowReturn
538 gst_vpx_dec_open_codec (GstVPXDec * dec, GstVideoCodecFrame * frame)
539 {
540   int flags = 0;
541   vpx_codec_stream_info_t stream_info;
542   vpx_codec_caps_t caps;
543   vpx_codec_dec_cfg_t cfg;
544   vpx_codec_err_t status;
545   GstMapInfo minfo;
546   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
547
548   g_return_val_if_fail (vpxclass->codec_algo != NULL, GST_FLOW_ERROR);
549
550   memset (&stream_info, 0, sizeof (stream_info));
551   memset (&cfg, 0, sizeof (cfg));
552   stream_info.sz = sizeof (stream_info);
553
554   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
555     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
556     return GST_FLOW_ERROR;
557   }
558
559   status = vpx_codec_peek_stream_info (vpxclass->codec_algo,
560       minfo.data, minfo.size, &stream_info);
561
562   gst_buffer_unmap (frame->input_buffer, &minfo);
563
564   if (status != VPX_CODEC_OK) {
565     GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
566         gst_vpx_error_name (status));
567     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
568     return GST_FLOW_CUSTOM_SUCCESS_1;
569   }
570   if (!stream_info.is_kf) {
571     GST_WARNING_OBJECT (dec, "No keyframe, skipping");
572     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), frame);
573     return GST_FLOW_CUSTOM_SUCCESS_1;
574   }
575
576   gst_vpx_dec_set_stream_info (dec, &stream_info);
577   gst_vpx_dec_set_default_format (dec, GST_VIDEO_FORMAT_I420, stream_info.w,
578       stream_info.h);
579
580   cfg.w = stream_info.w;
581   cfg.h = stream_info.h;
582   cfg.threads = dec->threads;
583
584   caps = vpx_codec_get_caps (vpxclass->codec_algo);
585
586   if (dec->post_processing) {
587     if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
588       GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
589     } else {
590       flags |= VPX_CODEC_USE_POSTPROC;
591     }
592   }
593
594   status =
595       vpx_codec_dec_init (&dec->decoder, vpxclass->codec_algo, &cfg, flags);
596   if (status != VPX_CODEC_OK) {
597     GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
598         ("Failed to initialize VP8 decoder"), ("%s",
599             gst_vpx_error_name (status)));
600     return GST_FLOW_ERROR;
601   }
602
603   if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
604     vp8_postproc_cfg_t pp_cfg = { 0, };
605
606     pp_cfg.post_proc_flag = dec->post_processing_flags;
607     pp_cfg.deblocking_level = dec->deblocking_level;
608     pp_cfg.noise_level = dec->noise_level;
609
610     status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
611     if (status != VPX_CODEC_OK) {
612       GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
613           gst_vpx_error_name (status));
614     }
615   }
616 #ifdef HAVE_VPX_1_4
617   vpx_codec_set_frame_buffer_functions (&dec->decoder,
618       gst_vpx_dec_get_buffer_cb, gst_vpx_dec_release_buffer_cb, dec);
619 #endif
620
621   dec->decoder_inited = TRUE;
622
623   return GST_FLOW_OK;
624 }
625
626 static GstFlowReturn
627 gst_vpx_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
628 {
629   GstVPXDec *dec;
630   GstFlowReturn ret = GST_FLOW_OK;
631   vpx_codec_err_t status;
632   vpx_codec_iter_t iter = NULL;
633   vpx_image_t *img;
634   long decoder_deadline = 0;
635   GstClockTimeDiff deadline;
636   GstMapInfo minfo;
637   GstVPXDecClass *vpxclass;
638   GstVideoFormat fmt;
639
640   GST_LOG_OBJECT (decoder, "handle_frame");
641
642   dec = GST_VPX_DEC (decoder);
643   vpxclass = GST_VPX_DEC_GET_CLASS (dec);
644
645   if (!dec->decoder_inited) {
646     ret = vpxclass->open_codec (dec, frame);
647     if (ret == GST_FLOW_CUSTOM_SUCCESS_1)
648       return GST_FLOW_OK;
649     else if (ret != GST_FLOW_OK)
650       return ret;
651   }
652
653   deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
654   if (deadline < 0) {
655     decoder_deadline = 1;
656   } else if (deadline == G_MAXINT64) {
657     decoder_deadline = 0;
658   } else {
659     decoder_deadline = MAX (1, deadline / GST_MSECOND);
660   }
661
662   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
663     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
664     return GST_FLOW_ERROR;
665   }
666
667   status = vpx_codec_decode (&dec->decoder,
668       minfo.data, minfo.size, NULL, decoder_deadline);
669
670   gst_buffer_unmap (frame->input_buffer, &minfo);
671
672   if (status) {
673     GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
674         ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
675     return ret;
676   }
677
678   img = vpx_codec_get_frame (&dec->decoder, &iter);
679   if (img) {
680     if (vpxclass->get_frame_format (dec, img, &fmt) == FALSE) {
681       vpx_img_free (img);
682       GST_ELEMENT_ERROR (decoder, LIBRARY, ENCODE,
683           ("Failed to decode frame"), ("Unsupported color format %d",
684               img->fmt));
685       return GST_FLOW_ERROR;
686     }
687
688     if (deadline < 0) {
689       GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
690           (double) -deadline / GST_SECOND);
691       gst_video_decoder_drop_frame (decoder, frame);
692     } else {
693       gst_vpx_dec_handle_resolution_change (dec, img, fmt);
694 #ifdef HAVE_VPX_1_4
695       if (img->fb_priv && dec->have_video_meta) {
696         frame->output_buffer = gst_vpx_dec_prepare_image (dec, img);
697         ret = gst_video_decoder_finish_frame (decoder, frame);
698       } else
699 #endif
700       {
701         ret = gst_video_decoder_allocate_output_frame (decoder, frame);
702
703         if (ret == GST_FLOW_OK) {
704           gst_vpx_dec_image_to_buffer (dec, img, frame->output_buffer);
705           ret = gst_video_decoder_finish_frame (decoder, frame);
706         } else {
707           gst_video_decoder_drop_frame (decoder, frame);
708         }
709       }
710     }
711
712     vpx_img_free (img);
713
714     while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
715       GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
716       vpx_img_free (img);
717     }
718   } else {
719     /* Invisible frame */
720     GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
721     gst_video_decoder_finish_frame (decoder, frame);
722   }
723
724   return ret;
725 }
726
727 static gboolean
728 gst_vpx_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
729 {
730   GstVPXDec *dec = GST_VPX_DEC (bdec);
731   GstBufferPool *pool;
732   GstStructure *config;
733
734   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
735     return FALSE;
736
737   g_assert (gst_query_get_n_allocation_pools (query) > 0);
738   gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
739   g_assert (pool != NULL);
740
741   config = gst_buffer_pool_get_config (pool);
742   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
743     gst_buffer_pool_config_add_option (config,
744         GST_BUFFER_POOL_OPTION_VIDEO_META);
745     dec->have_video_meta = TRUE;
746   }
747   gst_buffer_pool_set_config (pool, config);
748   gst_object_unref (pool);
749
750   return TRUE;
751 }
752
753 static void
754 gst_vpx_dec_set_stream_info (GstVPXDec * dec,
755     vpx_codec_stream_info_t * stream_info)
756 {
757   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
758   if (vpxclass->set_stream_info != NULL) {
759     vpxclass->set_stream_info (dec, stream_info);
760   }
761 }
762
763 static void
764 gst_vpx_dec_set_default_format (GstVPXDec * dec, GstVideoFormat fmt, int width,
765     int height)
766 {
767   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
768   if (vpxclass->set_default_format != NULL) {
769     vpxclass->set_default_format (dec, fmt, width, height);
770   }
771 }
772
773 static gboolean
774 gst_vpx_dec_default_frame_format (GstVPXDec * dec, vpx_image_t * img,
775     GstVideoFormat * fmt)
776 {
777   if (img->fmt == VPX_IMG_FMT_I420) {
778     *fmt = GST_VIDEO_FORMAT_I420;
779     return TRUE;
780   } else {
781     return FALSE;
782   }
783
784 }
785
786 static void
787 gst_vpx_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
788     GstVideoFormat fmt)
789 {
790   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
791   if (vpxclass->handle_resolution_change != NULL) {
792     vpxclass->handle_resolution_change (dec, img, fmt);
793   }
794 }
795
796 #endif /* HAVE_VP8_DECODER ||  HAVE_VP9_DECODER */