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