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