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