vpxdec: Set fb->priv to NULL after freeing just in case
[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   /* We're sometimes called without a frame */
486   if (!frame)
487     return 0;
488
489   GST_TRACE_OBJECT (priv, "Release buffer %p", frame->buffer);
490
491   gst_buffer_unmap (frame->buffer, &frame->info);
492   gst_buffer_unref (frame->buffer);
493   g_free (frame);
494   fb->priv = NULL;
495
496   return 0;
497 }
498 #endif
499
500 static void
501 gst_vpx_dec_image_to_buffer (GstVPXDec * dec, const vpx_image_t * img,
502     GstBuffer * buffer)
503 {
504   int deststride, srcstride, height, width, line, comp;
505   guint8 *dest, *src;
506   GstVideoFrame frame;
507   GstVideoInfo *info = &dec->output_state->info;
508
509   if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
510     GST_ERROR_OBJECT (dec, "Could not map video buffer");
511     return;
512   }
513
514   for (comp = 0; comp < 3; comp++) {
515     dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
516     src = img->planes[comp];
517     width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp)
518         * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
519     height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
520     deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
521     srcstride = img->stride[comp];
522
523     if (srcstride == deststride) {
524       GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
525           comp, srcstride);
526       memcpy (dest, src, srcstride * height);
527     } else {
528       GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
529           "line by line.", comp, srcstride, deststride);
530       for (line = 0; line < height; line++) {
531         memcpy (dest, src, width);
532         dest += deststride;
533         src += srcstride;
534       }
535     }
536   }
537
538   gst_video_frame_unmap (&frame);
539 }
540
541 static GstFlowReturn
542 gst_vpx_dec_open_codec (GstVPXDec * dec, GstVideoCodecFrame * frame)
543 {
544   int flags = 0;
545   vpx_codec_stream_info_t stream_info;
546   vpx_codec_caps_t caps;
547   vpx_codec_dec_cfg_t cfg;
548   vpx_codec_err_t status;
549   GstMapInfo minfo;
550   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
551
552   g_return_val_if_fail (vpxclass->codec_algo != NULL, GST_FLOW_ERROR);
553
554   memset (&stream_info, 0, sizeof (stream_info));
555   memset (&cfg, 0, sizeof (cfg));
556   stream_info.sz = sizeof (stream_info);
557
558   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
559     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
560     return GST_FLOW_ERROR;
561   }
562
563   status = vpx_codec_peek_stream_info (vpxclass->codec_algo,
564       minfo.data, minfo.size, &stream_info);
565
566   gst_buffer_unmap (frame->input_buffer, &minfo);
567
568   if (status != VPX_CODEC_OK) {
569     GST_WARNING_OBJECT (dec, "VPX preprocessing error: %s",
570         gst_vpx_error_name (status));
571     return GST_FLOW_CUSTOM_SUCCESS_1;
572   }
573   if (!stream_info.is_kf) {
574     GST_WARNING_OBJECT (dec, "No keyframe, skipping");
575     return GST_FLOW_CUSTOM_SUCCESS_1;
576   }
577
578   gst_vpx_dec_set_stream_info (dec, &stream_info);
579   gst_vpx_dec_set_default_format (dec, GST_VIDEO_FORMAT_I420, stream_info.w,
580       stream_info.h);
581
582   cfg.w = stream_info.w;
583   cfg.h = stream_info.h;
584
585   if (dec->threads > 0)
586     cfg.threads = dec->threads;
587   else
588     cfg.threads = g_get_num_processors ();
589
590   caps = vpx_codec_get_caps (vpxclass->codec_algo);
591
592   if (dec->post_processing) {
593     if (!(caps & VPX_CODEC_CAP_POSTPROC)) {
594       GST_WARNING_OBJECT (dec, "Decoder does not support post processing");
595     } else {
596       flags |= VPX_CODEC_USE_POSTPROC;
597     }
598   }
599
600   status =
601       vpx_codec_dec_init (&dec->decoder, vpxclass->codec_algo, &cfg, flags);
602   if (status != VPX_CODEC_OK) {
603     GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
604         ("Failed to initialize VP8 decoder"), ("%s",
605             gst_vpx_error_name (status)));
606     return GST_FLOW_ERROR;
607   }
608
609   if ((caps & VPX_CODEC_CAP_POSTPROC) && dec->post_processing) {
610     vp8_postproc_cfg_t pp_cfg = { 0, };
611
612     pp_cfg.post_proc_flag = dec->post_processing_flags;
613     pp_cfg.deblocking_level = dec->deblocking_level;
614     pp_cfg.noise_level = dec->noise_level;
615
616     status = vpx_codec_control (&dec->decoder, VP8_SET_POSTPROC, &pp_cfg);
617     if (status != VPX_CODEC_OK) {
618       GST_WARNING_OBJECT (dec, "Couldn't set postprocessing settings: %s",
619           gst_vpx_error_name (status));
620     }
621   }
622 #ifdef HAVE_VPX_1_4
623   vpx_codec_set_frame_buffer_functions (&dec->decoder,
624       gst_vpx_dec_get_buffer_cb, gst_vpx_dec_release_buffer_cb, dec);
625 #endif
626
627   dec->decoder_inited = TRUE;
628
629   return GST_FLOW_OK;
630 }
631
632 static GstFlowReturn
633 gst_vpx_dec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
634 {
635   GstVPXDec *dec;
636   GstFlowReturn ret = GST_FLOW_OK;
637   vpx_codec_err_t status;
638   vpx_codec_iter_t iter = NULL;
639   vpx_image_t *img;
640   long decoder_deadline = 0;
641   GstClockTimeDiff deadline;
642   GstMapInfo minfo;
643   GstVPXDecClass *vpxclass;
644   GstVideoFormat fmt;
645
646   GST_LOG_OBJECT (decoder, "handle_frame");
647
648   dec = GST_VPX_DEC (decoder);
649   vpxclass = GST_VPX_DEC_GET_CLASS (dec);
650
651   if (!dec->decoder_inited) {
652     ret = vpxclass->open_codec (dec, frame);
653     if (ret == GST_FLOW_CUSTOM_SUCCESS_1) {
654       gst_video_decoder_drop_frame (decoder, frame);
655       return GST_FLOW_OK;
656     } else if (ret != GST_FLOW_OK) {
657       gst_video_codec_frame_unref (frame);
658       return ret;
659     }
660   }
661
662   deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
663   if (deadline < 0) {
664     decoder_deadline = 1;
665   } else if (deadline == G_MAXINT64) {
666     decoder_deadline = 0;
667   } else {
668     decoder_deadline = MAX (1, deadline / GST_MSECOND);
669   }
670
671   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
672     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
673     gst_video_codec_frame_unref (frame);
674     return GST_FLOW_ERROR;
675   }
676
677   status = vpx_codec_decode (&dec->decoder,
678       minfo.data, minfo.size, NULL, decoder_deadline);
679
680   gst_buffer_unmap (frame->input_buffer, &minfo);
681
682   if (status) {
683     GST_VIDEO_DECODER_ERROR (decoder, 1, LIBRARY, ENCODE,
684         ("Failed to decode frame"), ("%s", gst_vpx_error_name (status)), ret);
685     gst_video_codec_frame_unref (frame);
686     return ret;
687   }
688
689   img = vpx_codec_get_frame (&dec->decoder, &iter);
690   if (img) {
691     if (vpxclass->get_frame_format (dec, img, &fmt) == FALSE) {
692       vpx_img_free (img);
693       GST_ELEMENT_ERROR (decoder, LIBRARY, ENCODE,
694           ("Failed to decode frame"), ("Unsupported color format %d",
695               img->fmt));
696       gst_video_codec_frame_unref (frame);
697       return GST_FLOW_ERROR;
698     }
699
700     if (deadline < 0) {
701       GST_LOG_OBJECT (dec, "Skipping late frame (%f s past deadline)",
702           (double) -deadline / GST_SECOND);
703       gst_video_decoder_drop_frame (decoder, frame);
704     } else {
705       gst_vpx_dec_handle_resolution_change (dec, img, fmt);
706 #ifdef HAVE_VPX_1_4
707       if (img->fb_priv && dec->have_video_meta) {
708         frame->output_buffer = gst_vpx_dec_prepare_image (dec, img);
709         ret = gst_video_decoder_finish_frame (decoder, frame);
710       } else
711 #endif
712       {
713         ret = gst_video_decoder_allocate_output_frame (decoder, frame);
714
715         if (ret == GST_FLOW_OK) {
716           gst_vpx_dec_image_to_buffer (dec, img, frame->output_buffer);
717           ret = gst_video_decoder_finish_frame (decoder, frame);
718         } else {
719           gst_video_decoder_drop_frame (decoder, frame);
720         }
721       }
722     }
723
724     vpx_img_free (img);
725
726     while ((img = vpx_codec_get_frame (&dec->decoder, &iter))) {
727       GST_WARNING_OBJECT (decoder, "Multiple decoded frames... dropping");
728       vpx_img_free (img);
729     }
730   } else {
731     /* Invisible frame */
732     GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
733     gst_video_decoder_finish_frame (decoder, frame);
734   }
735
736   return ret;
737 }
738
739 static gboolean
740 gst_vpx_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
741 {
742   GstVPXDec *dec = GST_VPX_DEC (bdec);
743   GstBufferPool *pool;
744   GstStructure *config;
745
746   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
747     return FALSE;
748
749   g_assert (gst_query_get_n_allocation_pools (query) > 0);
750   gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
751   g_assert (pool != NULL);
752
753   config = gst_buffer_pool_get_config (pool);
754   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
755     gst_buffer_pool_config_add_option (config,
756         GST_BUFFER_POOL_OPTION_VIDEO_META);
757     dec->have_video_meta = TRUE;
758   }
759   gst_buffer_pool_set_config (pool, config);
760   gst_object_unref (pool);
761
762   return TRUE;
763 }
764
765 static void
766 gst_vpx_dec_set_stream_info (GstVPXDec * dec,
767     vpx_codec_stream_info_t * stream_info)
768 {
769   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
770   if (vpxclass->set_stream_info != NULL) {
771     vpxclass->set_stream_info (dec, stream_info);
772   }
773 }
774
775 static void
776 gst_vpx_dec_set_default_format (GstVPXDec * dec, GstVideoFormat fmt, int width,
777     int height)
778 {
779   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
780   if (vpxclass->set_default_format != NULL) {
781     vpxclass->set_default_format (dec, fmt, width, height);
782   }
783 }
784
785 static gboolean
786 gst_vpx_dec_default_frame_format (GstVPXDec * dec, vpx_image_t * img,
787     GstVideoFormat * fmt)
788 {
789   if (img->fmt == VPX_IMG_FMT_I420) {
790     *fmt = GST_VIDEO_FORMAT_I420;
791     return TRUE;
792   } else {
793     return FALSE;
794   }
795
796 }
797
798 static void
799 gst_vpx_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
800     GstVideoFormat fmt)
801 {
802   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
803   if (vpxclass->handle_resolution_change != NULL) {
804     vpxclass->handle_resolution_change (dec, img, fmt);
805   }
806 }
807
808 #endif /* HAVE_VP8_DECODER ||  HAVE_VP9_DECODER */