va: Remove last_ret error handling in decoders.
[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
74 static GstElementClass *parent_class = NULL;
75
76 /* *INDENT-OFF* */
77 static const gchar *src_caps_str =
78     GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
79         "{ NV12 }") " ;"
80     GST_VIDEO_CAPS_MAKE ("{ NV12 }");
81 /* *INDENT-ON* */
82
83 static const gchar *sink_caps_str = "video/x-vp8";
84
85 static gboolean
86 gst_va_vp8_dec_negotiate (GstVideoDecoder * decoder)
87 {
88   GstCapsFeatures *capsfeatures = NULL;
89   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
90   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
91   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
92   GstVp8Decoder *vp8dec = GST_VP8_DECODER (decoder);
93
94   /* Ignore downstream renegotiation request. */
95   if (!base->need_negotiation)
96     return TRUE;
97
98   base->need_negotiation = FALSE;
99
100   if (gst_va_decoder_is_open (base->decoder)
101       && !gst_va_decoder_close (base->decoder))
102     return FALSE;
103
104   if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
105     return FALSE;
106
107   if (!gst_va_decoder_set_frame_size (base->decoder, base->width, base->height))
108     return FALSE;
109
110   if (base->output_state)
111     gst_video_codec_state_unref (base->output_state);
112
113   gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
114       &capsfeatures);
115   if (format == GST_VIDEO_FORMAT_UNKNOWN)
116     return FALSE;
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 (parent_class)->negotiate (decoder);
130 }
131
132 static VAProfile
133 _get_profile (GstVaVp8Dec * self, const GstVp8FrameHdr * frame_hdr)
134 {
135
136   if (frame_hdr->version > 3) {
137     GST_ERROR_OBJECT (self, "Unsupported vp8 version: %d", frame_hdr->version);
138     return VAProfileNone;
139   }
140
141   return VAProfileVP8Version0_3;
142 }
143
144 static GstFlowReturn
145 gst_va_vp8_dec_new_sequence (GstVp8Decoder * decoder,
146     const GstVp8FrameHdr * frame_hdr, gint max_dpb_size)
147 {
148   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
149   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
150   VAProfile profile;
151   guint rt_format;
152   gboolean negotiation_needed = FALSE;
153
154   GST_LOG_OBJECT (self, "new sequence");
155
156   profile = _get_profile (self, frame_hdr);
157   if (profile == VAProfileNone)
158     return GST_FLOW_NOT_NEGOTIATED;
159
160   if (!gst_va_decoder_has_profile (base->decoder, profile)) {
161     GST_ERROR_OBJECT (self, "Profile %s is not supported",
162         gst_va_profile_name (profile));
163     return GST_FLOW_NOT_NEGOTIATED;
164   }
165
166   /* VP8 always use 8 bits 4:2:0 */
167   rt_format = VA_RT_FORMAT_YUV420;
168
169   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
170           rt_format, frame_hdr->width, frame_hdr->height)) {
171     base->profile = profile;
172     base->width = frame_hdr->width;
173     base->height = frame_hdr->height;
174     base->rt_format = rt_format;
175     negotiation_needed = TRUE;
176   }
177
178   base->min_buffers = 3 + 4;    /* max num pic references + scratch surfaces */
179
180   base->need_negotiation = negotiation_needed;
181
182   return GST_FLOW_OK;
183 }
184
185 static GstFlowReturn
186 gst_va_vp8_dec_new_picture (GstVp8Decoder * decoder,
187     GstVideoCodecFrame * frame, GstVp8Picture * picture)
188 {
189   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
190   GstVaDecodePicture *pic;
191   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
192   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
193   GstFlowReturn ret;
194
195   if (base->need_negotiation) {
196     if (!gst_video_decoder_negotiate (vdec)) {
197       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
198       return GST_FLOW_NOT_NEGOTIATED;
199     }
200   }
201
202   ret = gst_video_decoder_allocate_output_frame (vdec, frame);
203   if (ret != GST_FLOW_OK)
204     goto error;
205
206   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
207
208   gst_vp8_picture_set_user_data (picture, pic,
209       (GDestroyNotify) gst_va_decode_picture_free);
210
211   GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
212       gst_va_decode_picture_get_surface (pic));
213
214   return GST_FLOW_OK;
215
216 error:
217   {
218     GST_WARNING_OBJECT (self,
219         "Failed to allocated output buffer, return %s",
220         gst_flow_get_name (ret));
221     return ret;
222   }
223 }
224
225 static gboolean
226 _fill_quant_matrix (GstVp8Decoder * decoder, GstVp8Picture * picture,
227     GstVp8Parser * parser)
228 {
229   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
230   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
231   GstVp8Segmentation *const seg = &parser->segmentation;
232   VAIQMatrixBufferVP8 iq_matrix = { };
233   const gint8 QI_MAX = 127;
234   gint16 qi, qi_base;
235   gint i;
236
237   /* Fill in VAIQMatrixBufferVP8 */
238   for (i = 0; i < 4; i++) {
239     if (seg->segmentation_enabled) {
240       qi_base = seg->quantizer_update_value[i];
241       if (!seg->segment_feature_mode)   /* 0 means delta update */
242         qi_base += frame_hdr->quant_indices.y_ac_qi;
243     } else
244       qi_base = frame_hdr->quant_indices.y_ac_qi;
245
246     qi = qi_base;
247     iq_matrix.quantization_index[i][0] = CLAMP (qi, 0, QI_MAX);
248     qi = qi_base + frame_hdr->quant_indices.y_dc_delta;
249     iq_matrix.quantization_index[i][1] = CLAMP (qi, 0, QI_MAX);
250     qi = qi_base + frame_hdr->quant_indices.y2_dc_delta;
251     iq_matrix.quantization_index[i][2] = CLAMP (qi, 0, QI_MAX);
252     qi = qi_base + frame_hdr->quant_indices.y2_ac_delta;
253     iq_matrix.quantization_index[i][3] = CLAMP (qi, 0, QI_MAX);
254     qi = qi_base + frame_hdr->quant_indices.uv_dc_delta;
255     iq_matrix.quantization_index[i][4] = CLAMP (qi, 0, QI_MAX);
256     qi = qi_base + frame_hdr->quant_indices.uv_ac_delta;
257     iq_matrix.quantization_index[i][5] = CLAMP (qi, 0, QI_MAX);
258   }
259
260   return gst_va_decoder_add_param_buffer (base->decoder,
261       gst_vp8_picture_get_user_data (picture), VAIQMatrixBufferType, &iq_matrix,
262       sizeof (iq_matrix));
263 }
264
265 static gboolean
266 _fill_probability_table (GstVp8Decoder * decoder, GstVp8Picture * picture)
267 {
268   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
269   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
270   VAProbabilityDataBufferVP8 prob_table = { };
271
272   /* Fill in VAProbabilityDataBufferVP8 */
273   memcpy (prob_table.dct_coeff_probs, frame_hdr->token_probs.prob,
274       sizeof (frame_hdr->token_probs.prob));
275
276   return gst_va_decoder_add_param_buffer (base->decoder,
277       gst_vp8_picture_get_user_data (picture), VAProbabilityBufferType,
278       &prob_table, sizeof (prob_table));
279 }
280
281 static gboolean
282 _fill_picture (GstVp8Decoder * decoder, GstVp8Picture * picture,
283     GstVp8Parser * parser)
284 {
285   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
286   GstVaDecodePicture *va_pic;
287   VAPictureParameterBufferVP8 pic_param;
288   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
289   GstVp8Segmentation *const seg = &parser->segmentation;
290   guint i;
291
292   if (!_fill_quant_matrix (decoder, picture, parser))
293     return FALSE;
294
295   if (!_fill_probability_table (decoder, picture))
296     return FALSE;
297
298   /* *INDENT-OFF* */
299   pic_param = (VAPictureParameterBufferVP8) {
300     .frame_width = base->width,
301     .frame_height = base->height,
302     .last_ref_frame = VA_INVALID_SURFACE,
303     .golden_ref_frame = VA_INVALID_SURFACE,
304     .alt_ref_frame = VA_INVALID_SURFACE,
305     .out_of_loop_frame = VA_INVALID_SURFACE, // not used currently
306     .pic_fields.bits.key_frame = !frame_hdr->key_frame,
307     .pic_fields.bits.version = frame_hdr->version,
308     .pic_fields.bits.segmentation_enabled = seg->segmentation_enabled,
309     .pic_fields.bits.update_mb_segmentation_map =
310         seg->update_mb_segmentation_map,
311     .pic_fields.bits.update_segment_feature_data =
312         seg->update_segment_feature_data,
313     .pic_fields.bits.filter_type = frame_hdr->filter_type,
314     .pic_fields.bits.sharpness_level = frame_hdr->sharpness_level,
315     .pic_fields.bits.loop_filter_adj_enable =
316         parser->mb_lf_adjust.loop_filter_adj_enable,
317     .pic_fields.bits.mode_ref_lf_delta_update =
318         parser->mb_lf_adjust.mode_ref_lf_delta_update,
319     .pic_fields.bits.sign_bias_golden = frame_hdr->sign_bias_golden,
320     .pic_fields.bits.sign_bias_alternate = frame_hdr->sign_bias_alternate,
321     .pic_fields.bits.mb_no_coeff_skip = frame_hdr->mb_no_skip_coeff,
322     /* In decoding, the only loop filter settings that matter are those
323        in the frame header (9.1) */
324     .pic_fields.bits.loop_filter_disable = frame_hdr->loop_filter_level == 0,
325     .prob_skip_false = frame_hdr->prob_skip_false,
326     .prob_intra = frame_hdr->prob_intra,
327     .prob_last = frame_hdr->prob_last,
328     .prob_gf = frame_hdr->prob_gf,
329     .bool_coder_ctx.range = frame_hdr->rd_range,
330     .bool_coder_ctx.value = frame_hdr->rd_value,
331     .bool_coder_ctx.count = frame_hdr->rd_count,
332   };
333   /* *INDENT-ON* */
334
335   if (!frame_hdr->key_frame) {
336     if (decoder->last_picture) {
337       va_pic = gst_vp8_picture_get_user_data (decoder->last_picture);
338       pic_param.last_ref_frame = gst_va_decode_picture_get_surface (va_pic);
339     }
340     if (decoder->golden_ref_picture) {
341       va_pic = gst_vp8_picture_get_user_data (decoder->golden_ref_picture);
342       pic_param.golden_ref_frame = gst_va_decode_picture_get_surface (va_pic);
343     }
344     if (decoder->alt_ref_picture) {
345       va_pic = gst_vp8_picture_get_user_data (decoder->alt_ref_picture);
346       pic_param.alt_ref_frame = gst_va_decode_picture_get_surface (va_pic);
347     }
348   }
349
350   for (i = 0; i < 3; i++)
351     pic_param.mb_segment_tree_probs[i] = seg->segment_prob[i];
352
353   for (i = 0; i < 4; i++) {
354     gint8 level;
355     if (seg->segmentation_enabled) {
356       level = seg->lf_update_value[i];
357       /* 0 means delta update */
358       if (!seg->segment_feature_mode)
359         level += frame_hdr->loop_filter_level;
360     } else
361       level = frame_hdr->loop_filter_level;
362     pic_param.loop_filter_level[i] = CLAMP (level, 0, 63);
363
364     pic_param.loop_filter_deltas_ref_frame[i] =
365         parser->mb_lf_adjust.ref_frame_delta[i];
366     pic_param.loop_filter_deltas_mode[i] =
367         parser->mb_lf_adjust.mb_mode_delta[i];
368   }
369
370   memcpy (pic_param.y_mode_probs, frame_hdr->mode_probs.y_prob,
371       sizeof (frame_hdr->mode_probs.y_prob));
372   memcpy (pic_param.uv_mode_probs, frame_hdr->mode_probs.uv_prob,
373       sizeof (frame_hdr->mode_probs.uv_prob));
374   memcpy (pic_param.mv_probs, frame_hdr->mv_probs.prob,
375       sizeof (frame_hdr->mv_probs));
376
377   va_pic = gst_vp8_picture_get_user_data (picture);
378   return gst_va_decoder_add_param_buffer (base->decoder, va_pic,
379       VAPictureParameterBufferType, &pic_param, sizeof (pic_param));
380 }
381
382 static gboolean
383 _add_slice (GstVp8Decoder * decoder, GstVp8Picture * picture,
384     GstVp8Parser * parser)
385 {
386   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
387   GstVp8FrameHdr const *frame_hdr = &picture->frame_hdr;
388   VASliceParameterBufferVP8 slice_param;
389   GstVaDecodePicture *va_pic;
390   gint i;
391
392   /* *INDENT-OFF* */
393   slice_param = (VASliceParameterBufferVP8) {
394     .slice_data_size = picture->size,
395     .slice_data_offset = frame_hdr->data_chunk_size,
396     .macroblock_offset = frame_hdr->header_size,
397     .num_of_partitions = (1 << frame_hdr->log2_nbr_of_dct_partitions) + 1,
398   };
399   /* *INDENT-ON* */
400
401   slice_param.partition_size[0] =
402       frame_hdr->first_part_size - ((slice_param.macroblock_offset + 7) >> 3);
403   for (i = 1; i < slice_param.num_of_partitions; i++)
404     slice_param.partition_size[i] = frame_hdr->partition_size[i - 1];
405   for (; i < G_N_ELEMENTS (slice_param.partition_size); i++)
406     slice_param.partition_size[i] = 0;
407
408   va_pic = gst_vp8_picture_get_user_data (picture);
409   return gst_va_decoder_add_slice_buffer (base->decoder, va_pic, &slice_param,
410       sizeof (slice_param), (gpointer) picture->data, picture->size);
411 }
412
413 static gboolean
414 gst_va_vp8_dec_decode_picture (GstVp8Decoder * decoder, GstVp8Picture * picture,
415     GstVp8Parser * parser)
416 {
417   if (_fill_picture (decoder, picture, parser) &&
418       _add_slice (decoder, picture, parser))
419     return GST_FLOW_OK;
420
421   return GST_FLOW_ERROR;
422 }
423
424 static GstFlowReturn
425 gst_va_vp8_dec_end_picture (GstVp8Decoder * decoder, GstVp8Picture * picture)
426 {
427   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
428   GstVaDecodePicture *va_pic;
429
430   GST_LOG_OBJECT (base, "end picture %p, (system_frame_number %d)",
431       picture, picture->system_frame_number);
432
433   va_pic = gst_vp8_picture_get_user_data (picture);
434
435   if (!gst_va_decoder_decode (base->decoder, va_pic))
436     return GST_FLOW_ERROR;
437
438   return GST_FLOW_OK;
439 }
440
441 static GstFlowReturn
442 gst_va_vp8_dec_output_picture (GstVp8Decoder * decoder,
443     GstVideoCodecFrame * frame, GstVp8Picture * picture)
444 {
445   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
446   GstVaVp8Dec *self = GST_VA_VP8_DEC (decoder);
447   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
448   gboolean ret;
449
450   GST_LOG_OBJECT (self,
451       "Outputting picture %p (system_frame_number %d)",
452       picture, picture->system_frame_number);
453
454   ret = gst_va_base_dec_process_output (base, frame, 0);
455   gst_vp8_picture_unref (picture);
456
457   if (ret)
458     return gst_video_decoder_finish_frame (vdec, frame);
459   return GST_FLOW_ERROR;
460 }
461
462 static void
463 gst_va_vp8_dec_init (GTypeInstance * instance, gpointer g_class)
464 {
465   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
466 }
467
468 static void
469 gst_va_vp8_dec_dispose (GObject * object)
470 {
471   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
472   G_OBJECT_CLASS (parent_class)->dispose (object);
473 }
474
475 static void
476 gst_va_vp8_dec_class_init (gpointer g_class, gpointer class_data)
477 {
478   GstCaps *src_doc_caps, *sink_doc_caps;
479   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
480   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
481   GstVp8DecoderClass *vp8decoder_class = GST_VP8_DECODER_CLASS (g_class);
482   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
483   struct CData *cdata = class_data;
484   gchar *long_name;
485
486   if (cdata->description) {
487     long_name = g_strdup_printf ("VA-API VP8 Decoder in %s",
488         cdata->description);
489   } else {
490     long_name = g_strdup ("VA-API VP8 Decoder");
491   }
492
493   gst_element_class_set_metadata (element_class, long_name,
494       "Codec/Decoder/Video/Hardware",
495       "VA-API based VP8 video decoder", "He Junyan <junyan.he@intel.com>");
496
497   sink_doc_caps = gst_caps_from_string (sink_caps_str);
498   src_doc_caps = gst_caps_from_string (src_caps_str);
499
500   parent_class = g_type_class_peek_parent (g_class);
501
502   /**
503    * GstVaVp8Dec:device-path:
504    *
505    * It shows the DRM device path used for the VA operation, if any.
506    *
507    * Since: 1.22
508    */
509   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), VP8,
510       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
511       src_doc_caps, sink_doc_caps);
512
513   gobject_class->dispose = gst_va_vp8_dec_dispose;
514
515   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_vp8_dec_negotiate);
516
517   vp8decoder_class->new_sequence =
518       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_new_sequence);
519   vp8decoder_class->new_picture =
520       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_new_picture);
521   vp8decoder_class->decode_picture =
522       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_decode_picture);
523   vp8decoder_class->end_picture =
524       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_end_picture);
525   vp8decoder_class->output_picture =
526       GST_DEBUG_FUNCPTR (gst_va_vp8_dec_output_picture);
527
528   g_free (long_name);
529   g_free (cdata->description);
530   g_free (cdata->render_device_path);
531   gst_caps_unref (cdata->src_caps);
532   gst_caps_unref (cdata->sink_caps);
533   g_free (cdata);
534 }
535
536 static gpointer
537 _register_debug_category (gpointer data)
538 {
539   GST_DEBUG_CATEGORY_INIT (gst_va_vp8dec_debug, "vavp8dec", 0,
540       "VA VP8 decoder");
541
542   return NULL;
543 }
544
545 gboolean
546 gst_va_vp8_dec_register (GstPlugin * plugin, GstVaDevice * device,
547     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
548 {
549   static GOnce debug_once = G_ONCE_INIT;
550   GType type;
551   GTypeInfo type_info = {
552     .class_size = sizeof (GstVaVp8DecClass),
553     .class_init = gst_va_vp8_dec_class_init,
554     .instance_size = sizeof (GstVaVp8Dec),
555     .instance_init = gst_va_vp8_dec_init,
556   };
557   struct CData *cdata;
558   gboolean ret;
559   gchar *type_name, *feature_name;
560
561   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
562   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
563   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
564   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
565
566   cdata = g_new (struct CData, 1);
567   cdata->description = NULL;
568   cdata->render_device_path = g_strdup (device->render_device_path);
569   cdata->sink_caps = gst_caps_ref (sink_caps);
570   cdata->src_caps = gst_caps_ref (src_caps);
571
572   /* class data will be leaked if the element never gets instantiated */
573   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
574       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
575   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
576
577   type_info.class_data = cdata;
578
579   /* The first decoder to be registered should use a constant name,
580    * like vavp8dec, for any additional decoders, we create unique
581    * names, using inserting the render device name. */
582   if (device->index == 0) {
583     type_name = g_strdup ("GstVaVp8Dec");
584     feature_name = g_strdup ("vavp8dec");
585   } else {
586     gchar *basename = g_path_get_basename (device->render_device_path);
587     type_name = g_strdup_printf ("GstVa%sVp8Dec", basename);
588     feature_name = g_strdup_printf ("va%svp8dec", basename);
589     cdata->description = basename;
590
591     /* lower rank for non-first device */
592     if (rank > 0)
593       rank--;
594   }
595
596   g_once (&debug_once, _register_debug_category, NULL);
597
598   type = g_type_register_static (GST_TYPE_VP8_DECODER,
599       type_name, &type_info, 0);
600
601   ret = gst_element_register (plugin, feature_name, rank, type);
602
603   g_free (type_name);
604   g_free (feature_name);
605
606   return ret;
607 }