Merging gst-libav
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvavp8dec.c
1 /* GStreamer
2  *  Copyright (C) 2020 Intel Corporation
3  *     Author: He Junyan <junyan.he@intel.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the0
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-vavp8dec
23  * @title: vavp8dec
24  * @short_description: A VA-API based VP8 video decoder
25  *
26  * vavp8dec decodes VP8 bitstreams to VA surfaces using the
27  * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
28  * driver.
29  *
30  * The decoding surfaces can be mapped onto main memory as video
31  * frames.
32  *
33  * ## Example launch line
34  * ```
35  * gst-launch-1.0 filesrc location=sample.webm ! parsebin ! vavp8dec ! autovideosink
36  * ```
37  *
38  * Since: 1.20
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gstvavp8dec.h"
47
48 #include "gstvabasedec.h"
49
50 GST_DEBUG_CATEGORY_STATIC (gst_va_vp8dec_debug);
51 #ifndef GST_DISABLE_GST_DEBUG
52 #define GST_CAT_DEFAULT gst_va_vp8dec_debug
53 #else
54 #define GST_CAT_DEFAULT NULL
55 #endif
56
57 #define GST_VA_VP8_DEC(obj)           ((GstVaVp8Dec *) obj)
58 #define GST_VA_VP8_DEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaVp8DecClass))
59 #define GST_VA_VP8_DEC_CLASS(klass)   ((GstVaVp8DecClass *) klass)
60
61 typedef struct _GstVaVp8Dec GstVaVp8Dec;
62 typedef struct _GstVaVp8DecClass GstVaVp8DecClass;
63
64 struct _GstVaVp8DecClass
65 {
66   GstVaBaseDecClass parent_class;
67 };
68
69 struct _GstVaVp8Dec
70 {
71   GstVaBaseDec parent;
72
73   GstFlowReturn last_ret;
74
75   gboolean need_negotiation;
76 };
77
78 /* *INDENT-OFF* */
79 static const gchar *src_caps_str =
80     GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
81         "{ NV12 }") " ;"
82     GST_VIDEO_CAPS_MAKE ("{ NV12 }");
83 /* *INDENT-ON* */
84
85 static const gchar *sink_caps_str = "video/x-vp8";
86
87 static gboolean
88 gst_va_vp8_dec_negotiate (GstVideoDecoder * decoder)
89 {
90   GstCapsFeatures *capsfeatures = NULL;
91   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
92   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
93   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
94   GstVp8Decoder *vp8dec = GST_VP8_DECODER (decoder);
95
96   /* Ignore downstream renegotiation request. */
97   if (!self->need_negotiation)
98     return TRUE;
99
100   self->need_negotiation = FALSE;
101
102   if (gst_va_decoder_is_open (base->decoder)
103       && !gst_va_decoder_close (base->decoder))
104     return FALSE;
105
106   if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
107     return FALSE;
108
109   if (!gst_va_decoder_set_frame_size (base->decoder, base->width, base->height))
110     return FALSE;
111
112   if (base->output_state)
113     gst_video_codec_state_unref (base->output_state);
114
115   gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
116       &capsfeatures);
117
118   base->output_state =
119       gst_video_decoder_set_output_state (decoder, format,
120       base->width, base->height, vp8dec->input_state);
121
122   base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
123   if (capsfeatures)
124     gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
125
126   GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
127       base->output_state->caps);
128
129   return GST_VIDEO_DECODER_CLASS (GST_VA_BASE_DEC_GET_PARENT_CLASS
130       (decoder))->negotiate (decoder);
131 }
132
133 static VAProfile
134 _get_profile (GstVaVp8Dec * self, const GstVp8FrameHdr * frame_hdr)
135 {
136
137   if (frame_hdr->version > 3) {
138     GST_ERROR_OBJECT (self, "Unsupported vp8 version: %d", frame_hdr->version);
139     return VAProfileNone;
140   }
141
142   return VAProfileVP8Version0_3;
143 }
144
145 static GstFlowReturn
146 gst_va_vp8_dec_new_sequence (GstVp8Decoder * decoder,
147     const GstVp8FrameHdr * frame_hdr)
148 {
149   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
150   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
151   VAProfile profile;
152   guint rt_format;
153   gboolean negotiation_needed = FALSE;
154
155   GST_LOG_OBJECT (self, "new sequence");
156
157   profile = _get_profile (self, frame_hdr);
158   if (profile == VAProfileNone)
159     return GST_FLOW_NOT_NEGOTIATED;
160
161   if (!gst_va_decoder_has_profile (base->decoder, profile)) {
162     GST_ERROR_OBJECT (self, "Profile %s is not supported",
163         gst_va_profile_name (profile));
164     return GST_FLOW_NOT_NEGOTIATED;
165   }
166
167   /* VP8 always use 8 bits 4:2:0 */
168   rt_format = VA_RT_FORMAT_YUV420;
169
170   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
171           rt_format, frame_hdr->width, frame_hdr->height)) {
172     base->profile = profile;
173     base->width = frame_hdr->width;
174     base->height = frame_hdr->height;
175     base->rt_format = rt_format;
176     negotiation_needed = TRUE;
177   }
178
179   base->min_buffers = 3 + 4;    /* max num pic references + scratch surfaces */
180
181   if (negotiation_needed) {
182     self->need_negotiation = TRUE;
183     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
184       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
185       return GST_FLOW_NOT_NEGOTIATED;
186     }
187   }
188
189   return GST_FLOW_OK;
190 }
191
192 static GstFlowReturn
193 gst_va_vp8_dec_new_picture (GstVp8Decoder * decoder,
194     GstVideoCodecFrame * frame, GstVp8Picture * picture)
195 {
196   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
197   GstVaDecodePicture *pic;
198   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
199   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
200
201   self->last_ret = gst_video_decoder_allocate_output_frame (vdec, frame);
202   if (self->last_ret != GST_FLOW_OK)
203     goto error;
204
205   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
206
207   gst_vp8_picture_set_user_data (picture, pic,
208       (GDestroyNotify) gst_va_decode_picture_free);
209
210   GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
211       gst_va_decode_picture_get_surface (pic));
212
213   return GST_FLOW_OK;
214
215 error:
216   {
217     GST_WARNING_OBJECT (self,
218         "Failed to allocated output buffer, return %s",
219         gst_flow_get_name (self->last_ret));
220     return self->last_ret;
221   }
222 }
223
224 static gboolean
225 _fill_quant_matrix (GstVp8Decoder * decoder, GstVp8Picture * picture,
226     GstVp8Parser * parser)
227 {
228   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
229   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
230   GstVp8Segmentation *const seg = &parser->segmentation;
231   VAIQMatrixBufferVP8 iq_matrix = { };
232   const gint8 QI_MAX = 127;
233   gint16 qi, qi_base;
234   gint i;
235
236   /* Fill in VAIQMatrixBufferVP8 */
237   for (i = 0; i < 4; i++) {
238     if (seg->segmentation_enabled) {
239       qi_base = seg->quantizer_update_value[i];
240       if (!seg->segment_feature_mode)   /* 0 means delta update */
241         qi_base += frame_hdr->quant_indices.y_ac_qi;
242     } else
243       qi_base = frame_hdr->quant_indices.y_ac_qi;
244
245     qi = qi_base;
246     iq_matrix.quantization_index[i][0] = CLAMP (qi, 0, QI_MAX);
247     qi = qi_base + frame_hdr->quant_indices.y_dc_delta;
248     iq_matrix.quantization_index[i][1] = CLAMP (qi, 0, QI_MAX);
249     qi = qi_base + frame_hdr->quant_indices.y2_dc_delta;
250     iq_matrix.quantization_index[i][2] = CLAMP (qi, 0, QI_MAX);
251     qi = qi_base + frame_hdr->quant_indices.y2_ac_delta;
252     iq_matrix.quantization_index[i][3] = CLAMP (qi, 0, QI_MAX);
253     qi = qi_base + frame_hdr->quant_indices.uv_dc_delta;
254     iq_matrix.quantization_index[i][4] = CLAMP (qi, 0, QI_MAX);
255     qi = qi_base + frame_hdr->quant_indices.uv_ac_delta;
256     iq_matrix.quantization_index[i][5] = CLAMP (qi, 0, QI_MAX);
257   }
258
259   return gst_va_decoder_add_param_buffer (base->decoder,
260       gst_vp8_picture_get_user_data (picture), VAIQMatrixBufferType, &iq_matrix,
261       sizeof (iq_matrix));
262 }
263
264 static gboolean
265 _fill_probability_table (GstVp8Decoder * decoder, GstVp8Picture * picture)
266 {
267   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
268   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
269   VAProbabilityDataBufferVP8 prob_table = { };
270
271   /* Fill in VAProbabilityDataBufferVP8 */
272   memcpy (prob_table.dct_coeff_probs, frame_hdr->token_probs.prob,
273       sizeof (frame_hdr->token_probs.prob));
274
275   return gst_va_decoder_add_param_buffer (base->decoder,
276       gst_vp8_picture_get_user_data (picture), VAProbabilityBufferType,
277       &prob_table, sizeof (prob_table));
278 }
279
280 static gboolean
281 _fill_picture (GstVp8Decoder * decoder, GstVp8Picture * picture,
282     GstVp8Parser * parser)
283 {
284   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
285   GstVaDecodePicture *va_pic;
286   VAPictureParameterBufferVP8 pic_param;
287   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
288   GstVp8Segmentation *const seg = &parser->segmentation;
289   guint i;
290
291   if (!_fill_quant_matrix (decoder, picture, parser))
292     return FALSE;
293
294   if (!_fill_probability_table (decoder, picture))
295     return FALSE;
296
297   /* *INDENT-OFF* */
298   pic_param = (VAPictureParameterBufferVP8) {
299     .frame_width = base->width,
300     .frame_height = base->height,
301     .last_ref_frame = VA_INVALID_SURFACE,
302     .golden_ref_frame = VA_INVALID_SURFACE,
303     .alt_ref_frame = VA_INVALID_SURFACE,
304     .out_of_loop_frame = VA_INVALID_SURFACE, // not used currently
305     .pic_fields.bits.key_frame = !frame_hdr->key_frame,
306     .pic_fields.bits.version = frame_hdr->version,
307     .pic_fields.bits.segmentation_enabled = seg->segmentation_enabled,
308     .pic_fields.bits.update_mb_segmentation_map =
309         seg->update_mb_segmentation_map,
310     .pic_fields.bits.update_segment_feature_data =
311         seg->update_segment_feature_data,
312     .pic_fields.bits.filter_type = frame_hdr->filter_type,
313     .pic_fields.bits.sharpness_level = frame_hdr->sharpness_level,
314     .pic_fields.bits.loop_filter_adj_enable =
315         parser->mb_lf_adjust.loop_filter_adj_enable,
316     .pic_fields.bits.mode_ref_lf_delta_update =
317         parser->mb_lf_adjust.mode_ref_lf_delta_update,
318     .pic_fields.bits.sign_bias_golden = frame_hdr->sign_bias_golden,
319     .pic_fields.bits.sign_bias_alternate = frame_hdr->sign_bias_alternate,
320     .pic_fields.bits.mb_no_coeff_skip = frame_hdr->mb_no_skip_coeff,
321     /* In decoding, the only loop filter settings that matter are those
322        in the frame header (9.1) */
323     .pic_fields.bits.loop_filter_disable = frame_hdr->loop_filter_level == 0,
324     .prob_skip_false = frame_hdr->prob_skip_false,
325     .prob_intra = frame_hdr->prob_intra,
326     .prob_last = frame_hdr->prob_last,
327     .prob_gf = frame_hdr->prob_gf,
328     .bool_coder_ctx.range = frame_hdr->rd_range,
329     .bool_coder_ctx.value = frame_hdr->rd_value,
330     .bool_coder_ctx.count = frame_hdr->rd_count,
331   };
332   /* *INDENT-ON* */
333
334   if (!frame_hdr->key_frame) {
335     if (decoder->last_picture) {
336       va_pic = gst_vp8_picture_get_user_data (decoder->last_picture);
337       pic_param.last_ref_frame = gst_va_decode_picture_get_surface (va_pic);
338     }
339     if (decoder->golden_ref_picture) {
340       va_pic = gst_vp8_picture_get_user_data (decoder->golden_ref_picture);
341       pic_param.golden_ref_frame = gst_va_decode_picture_get_surface (va_pic);
342     }
343     if (decoder->alt_ref_picture) {
344       va_pic = gst_vp8_picture_get_user_data (decoder->alt_ref_picture);
345       pic_param.alt_ref_frame = gst_va_decode_picture_get_surface (va_pic);
346     }
347   }
348
349   for (i = 0; i < 3; i++)
350     pic_param.mb_segment_tree_probs[i] = seg->segment_prob[i];
351
352   for (i = 0; i < 4; i++) {
353     gint8 level;
354     if (seg->segmentation_enabled) {
355       level = seg->lf_update_value[i];
356       /* 0 means delta update */
357       if (!seg->segment_feature_mode)
358         level += frame_hdr->loop_filter_level;
359     } else
360       level = frame_hdr->loop_filter_level;
361     pic_param.loop_filter_level[i] = CLAMP (level, 0, 63);
362
363     pic_param.loop_filter_deltas_ref_frame[i] =
364         parser->mb_lf_adjust.ref_frame_delta[i];
365     pic_param.loop_filter_deltas_mode[i] =
366         parser->mb_lf_adjust.mb_mode_delta[i];
367   }
368
369   memcpy (pic_param.y_mode_probs, frame_hdr->mode_probs.y_prob,
370       sizeof (frame_hdr->mode_probs.y_prob));
371   memcpy (pic_param.uv_mode_probs, frame_hdr->mode_probs.uv_prob,
372       sizeof (frame_hdr->mode_probs.uv_prob));
373   memcpy (pic_param.mv_probs, frame_hdr->mv_probs.prob,
374       sizeof (frame_hdr->mv_probs));
375
376   va_pic = gst_vp8_picture_get_user_data (picture);
377   return gst_va_decoder_add_param_buffer (base->decoder, va_pic,
378       VAPictureParameterBufferType, &pic_param, sizeof (pic_param));
379 }
380
381 static gboolean
382 _add_slice (GstVp8Decoder * decoder, GstVp8Picture * picture,
383     GstVp8Parser * parser)
384 {
385   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
386   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
387   VASliceParameterBufferVP8 slice_param;
388   GstVaDecodePicture *va_pic;
389   gint i;
390
391   /* *INDENT-OFF* */
392   slice_param = (VASliceParameterBufferVP8) {
393     .slice_data_size = picture->size,
394     .slice_data_offset = frame_hdr->data_chunk_size,
395     .macroblock_offset = frame_hdr->header_size,
396     .num_of_partitions = (1 << frame_hdr->log2_nbr_of_dct_partitions) + 1,
397   };
398   /* *INDENT-ON* */
399
400   slice_param.partition_size[0] =
401       frame_hdr->first_part_size - ((slice_param.macroblock_offset + 7) >> 3);
402   for (i = 1; i < slice_param.num_of_partitions; i++)
403     slice_param.partition_size[i] = frame_hdr->partition_size[i - 1];
404   for (; i < G_N_ELEMENTS (slice_param.partition_size); i++)
405     slice_param.partition_size[i] = 0;
406
407   va_pic = gst_vp8_picture_get_user_data (picture);
408   return gst_va_decoder_add_slice_buffer (base->decoder, va_pic, &slice_param,
409       sizeof (slice_param), (gpointer) picture->data, picture->size);
410 }
411
412 static gboolean
413 gst_va_vp8_dec_decode_picture (GstVp8Decoder * decoder, GstVp8Picture * picture,
414     GstVp8Parser * parser)
415 {
416   if (_fill_picture (decoder, picture, parser) &&
417       _add_slice (decoder, picture, parser))
418     return GST_FLOW_OK;
419
420   return GST_FLOW_ERROR;
421 }
422
423 static GstFlowReturn
424 gst_va_vp8_dec_end_picture (GstVp8Decoder * decoder, GstVp8Picture * picture)
425 {
426   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
427   GstVaDecodePicture *va_pic;
428
429   GST_LOG_OBJECT (base, "end picture %p, (system_frame_number %d)",
430       picture, picture->system_frame_number);
431
432   va_pic = gst_vp8_picture_get_user_data (picture);
433
434   if (!gst_va_decoder_decode (base->decoder, va_pic))
435     return GST_FLOW_ERROR;
436
437   return GST_FLOW_OK;
438 }
439
440 static GstFlowReturn
441 gst_va_vp8_dec_output_picture (GstVp8Decoder * decoder,
442     GstVideoCodecFrame * frame, GstVp8Picture * picture)
443 {
444   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
445   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
446
447   GST_LOG_OBJECT (self,
448       "Outputting picture %p (system_frame_number %d)",
449       picture, picture->system_frame_number);
450
451   if (self->last_ret != GST_FLOW_OK) {
452     gst_vp8_picture_unref (picture);
453     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self), frame);
454     return self->last_ret;
455   }
456
457   if (base->copy_frames)
458     gst_va_base_dec_copy_output_buffer (base, frame);
459
460   gst_vp8_picture_unref (picture);
461
462   return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
463 }
464
465 static void
466 gst_va_vp8_dec_init (GTypeInstance * instance, gpointer g_class)
467 {
468   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
469 }
470
471 static void
472 gst_va_vp8_dec_dispose (GObject * object)
473 {
474   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
475   G_OBJECT_CLASS (GST_VA_BASE_DEC_GET_PARENT_CLASS (object))->dispose (object);
476 }
477
478 static void
479 gst_va_vp8_dec_class_init (gpointer g_class, gpointer class_data)
480 {
481   GstCaps *src_doc_caps, *sink_doc_caps;
482   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
483   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
484   GstVp8DecoderClass *vp8decoder_class = GST_VP8_DECODER_CLASS (g_class);
485   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
486   struct CData *cdata = class_data;
487   gchar *long_name;
488
489   if (cdata->description) {
490     long_name = g_strdup_printf ("VA-API VP8 Decoder in %s",
491         cdata->description);
492   } else {
493     long_name = g_strdup ("VA-API VP8 Decoder");
494   }
495
496   gst_element_class_set_metadata (element_class, long_name,
497       "Codec/Decoder/Video/Hardware",
498       "VA-API based VP8 video decoder", "He Junyan <junyan.he@intel.com>");
499
500   sink_doc_caps = gst_caps_from_string (sink_caps_str);
501   src_doc_caps = gst_caps_from_string (src_caps_str);
502
503   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), VP8,
504       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
505       src_doc_caps, sink_doc_caps);
506
507   gobject_class->dispose = gst_va_vp8_dec_dispose;
508
509   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_vp8_dec_negotiate);
510
511   vp8decoder_class->new_sequence =
512       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_new_sequence);
513   vp8decoder_class->new_picture =
514       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_new_picture);
515   vp8decoder_class->decode_picture =
516       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_decode_picture);
517   vp8decoder_class->end_picture =
518       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_end_picture);
519   vp8decoder_class->output_picture =
520       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_output_picture);
521
522   g_free (long_name);
523   g_free (cdata->description);
524   g_free (cdata->render_device_path);
525   gst_caps_unref (cdata->src_caps);
526   gst_caps_unref (cdata->sink_caps);
527   g_free (cdata);
528 }
529
530 static gpointer
531 _register_debug_category (gpointer data)
532 {
533   GST_DEBUG_CATEGORY_INIT (gst_va_vp8dec_debug, "vavp8dec", 0,
534       "VA VP8 decoder");
535
536   return NULL;
537 }
538
539 gboolean
540 gst_va_vp8_dec_register (GstPlugin * plugin, GstVaDevice * device,
541     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
542 {
543   static GOnce debug_once = G_ONCE_INIT;
544   GType type;
545   GTypeInfo type_info = {
546     .class_size = sizeof (GstVaVp8DecClass),
547     .class_init = gst_va_vp8_dec_class_init,
548     .instance_size = sizeof (GstVaVp8Dec),
549     .instance_init = gst_va_vp8_dec_init,
550   };
551   struct CData *cdata;
552   gboolean ret;
553   gchar *type_name, *feature_name;
554
555   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
556   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
557   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
558   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
559
560   cdata = g_new (struct CData, 1);
561   cdata->description = NULL;
562   cdata->render_device_path = g_strdup (device->render_device_path);
563   cdata->sink_caps = gst_caps_ref (sink_caps);
564   cdata->src_caps = gst_caps_ref (src_caps);
565
566   /* class data will be leaked if the element never gets instantiated */
567   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
568       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
569   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
570
571   type_info.class_data = cdata;
572
573   type_name = g_strdup ("GstVaVp8dec");
574   feature_name = g_strdup ("vavp8dec");
575
576   /* The first decoder to be registered should use a constant name,
577    * like vavp8dec, for any additional decoders, we create unique
578    * names, using inserting the render device name. */
579   if (g_type_from_name (type_name)) {
580     gchar *basename = g_path_get_basename (device->render_device_path);
581     g_free (type_name);
582     g_free (feature_name);
583     type_name = g_strdup_printf ("GstVa%sVP8Dec", basename);
584     feature_name = g_strdup_printf ("va%svp8dec", basename);
585     cdata->description = basename;
586
587     /* lower rank for non-first device */
588     if (rank > 0)
589       rank--;
590   }
591
592   g_once (&debug_once, _register_debug_category, NULL);
593
594   type = g_type_register_static (GST_TYPE_VP8_DECODER,
595       type_name, &type_info, 0);
596
597   ret = gst_element_register (plugin, feature_name, rank, type);
598
599   g_free (type_name);
600   g_free (feature_name);
601
602   return ret;
603 }