va: Fix struct empty initialization syntax
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvampeg2dec.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-vampeg2dec
23  * @title: vampeg2dec
24  * @short_description: A VA-API based Mpeg2 video decoder
25  *
26  * vampeg2dec decodes Mpeg2 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.mpg ! parsebin ! vampeg2dec ! autovideosink
36  * ```
37  *
38  * Since: 1.20
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gstvampeg2dec.h"
47
48 #include "gstvabasedec.h"
49
50 GST_DEBUG_CATEGORY_STATIC (gst_va_mpeg2dec_debug);
51 #ifndef GST_DISABLE_GST_DEBUG
52 #define GST_CAT_DEFAULT gst_va_mpeg2dec_debug
53 #else
54 #define GST_CAT_DEFAULT NULL
55 #endif
56
57 #define GST_VA_MPEG2_DEC(obj)           ((GstVaMpeg2Dec *) obj)
58 #define GST_VA_MPEG2_DEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaMpeg2DecClass))
59 #define GST_VA_MPEG2_DEC_CLASS(klass)   ((GstVaMpeg2DecClass *) klass)
60
61 typedef struct _GstVaMpeg2Dec GstVaMpeg2Dec;
62 typedef struct _GstVaMpeg2DecClass GstVaMpeg2DecClass;
63
64 struct _GstVaMpeg2DecClass
65 {
66   GstVaBaseDecClass parent_class;
67 };
68
69 struct _GstVaMpeg2Dec
70 {
71   GstVaBaseDec parent;
72
73   gboolean progressive;
74   GstMpegVideoSequenceHdr seq;
75 };
76
77 static GstElementClass *parent_class = NULL;
78
79 /* *INDENT-OFF* */
80 static const gchar *src_caps_str =
81     GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
82         "{ NV12 }") " ;"
83     GST_VIDEO_CAPS_MAKE ("{ NV12 }");
84 /* *INDENT-ON* */
85
86 static const gchar *sink_caps_str = "video/x-mpeg2";
87
88 static VAProfile
89 _map_profile (GstMpegVideoProfile profile)
90 {
91   VAProfile p = VAProfileNone;
92
93   switch (profile) {
94     case GST_MPEG_VIDEO_PROFILE_SIMPLE:
95       p = VAProfileMPEG2Simple;
96       break;
97     case GST_MPEG_VIDEO_PROFILE_MAIN:
98       p = VAProfileMPEG2Main;
99       break;
100     default:
101       p = VAProfileNone;
102       break;
103   }
104
105   return p;
106 }
107
108 static VAProfile
109 _get_profile (GstVaMpeg2Dec * self, GstMpegVideoProfile profile,
110     const GstMpegVideoSequenceExt * seq_ext,
111     const GstMpegVideoSequenceScalableExt * seq_scalable_ext)
112 {
113   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
114   VAProfile hw_profile;
115
116   hw_profile = _map_profile (profile);
117   if (hw_profile == VAProfileNone)
118     return hw_profile;
119
120   /* promote the profile if hw does not support, until we get one */
121   do {
122     if (gst_va_decoder_has_profile (base->decoder, hw_profile))
123       return hw_profile;
124
125     /* Otherwise, try to map to a higher profile */
126     switch (profile) {
127       case GST_MPEG_VIDEO_PROFILE_SIMPLE:
128         hw_profile = VAProfileMPEG2Main;
129         break;
130       case GST_MPEG_VIDEO_PROFILE_HIGH:
131         /* Try to map to main profile if no high profile specific bits used */
132         if (!seq_scalable_ext && (seq_ext && seq_ext->chroma_format == 1)) {
133           hw_profile = VAProfileMPEG2Main;
134           break;
135         }
136         /* fall-through */
137       default:
138         GST_ERROR_OBJECT (self, "profile %d is unsupported.", profile);
139         hw_profile = VAProfileNone;
140         break;
141     }
142   } while (hw_profile != VAProfileNone);
143
144   return hw_profile;
145 }
146
147 static guint
148 _get_rtformat (GstVaMpeg2Dec * self, GstMpegVideoChromaFormat chroma_format)
149 {
150   guint ret = 0;
151
152   switch (chroma_format) {
153     case GST_MPEG_VIDEO_CHROMA_420:
154       ret = VA_RT_FORMAT_YUV420;
155       break;
156     case GST_MPEG_VIDEO_CHROMA_422:
157       ret = VA_RT_FORMAT_YUV422;
158       break;
159     case GST_MPEG_VIDEO_CHROMA_444:
160       ret = VA_RT_FORMAT_YUV444;
161       break;
162     default:
163       GST_ERROR_OBJECT (self, "Unsupported chroma format: %d ", chroma_format);
164       break;
165   }
166
167   return ret;
168 }
169
170 static GstFlowReturn
171 gst_va_mpeg2_dec_new_sequence (GstMpeg2Decoder * decoder,
172     const GstMpegVideoSequenceHdr * seq,
173     const GstMpegVideoSequenceExt * seq_ext,
174     const GstMpegVideoSequenceDisplayExt * seq_display_ext,
175     const GstMpegVideoSequenceScalableExt * seq_scalable_ext, gint max_dpb_size)
176 {
177   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
178   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
179   GstVideoInfo *info = &base->output_info;
180   VAProfile profile;
181   GstMpegVideoProfile mpeg_profile;
182   gboolean negotiation_needed = FALSE;
183   guint rt_format;
184   gint width, height;
185   gboolean progressive;
186
187   self->seq = *seq;
188
189   width = seq->width;
190   height = seq->height;
191   if (seq_ext) {
192     width = (width & 0x0fff) | ((guint32) seq_ext->horiz_size_ext << 12);
193     height = (height & 0x0fff) | ((guint32) seq_ext->vert_size_ext << 12);
194   }
195
196   mpeg_profile = GST_MPEG_VIDEO_PROFILE_MAIN;
197   if (seq_ext)
198     mpeg_profile = seq_ext->profile;
199
200   profile = _get_profile (self, mpeg_profile, seq_ext, seq_scalable_ext);
201   if (profile == VAProfileNone)
202     return GST_FLOW_NOT_NEGOTIATED;
203
204   rt_format = _get_rtformat (self,
205       seq_ext ? seq_ext->chroma_format : GST_MPEG_VIDEO_CHROMA_420);
206   if (rt_format == 0)
207     return GST_FLOW_NOT_NEGOTIATED;
208
209   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
210           rt_format, width, height)) {
211     base->profile = profile;
212     base->rt_format = rt_format;
213     GST_VIDEO_INFO_WIDTH (info) = base->width = width;
214     GST_VIDEO_INFO_HEIGHT (info) = base->height = height;
215
216     negotiation_needed = TRUE;
217
218     GST_INFO_OBJECT (self, "Format changed to %s [%x] (%dx%d)",
219         gst_va_profile_name (profile), rt_format, base->width, base->height);
220   }
221
222   progressive = seq_ext ? seq_ext->progressive : 1;
223   if (self->progressive != progressive) {
224     self->progressive = progressive;
225     GST_VIDEO_INFO_INTERLACE_MODE (info) = progressive ?
226         GST_VIDEO_INTERLACE_MODE_PROGRESSIVE : GST_VIDEO_INTERLACE_MODE_MIXED;
227     negotiation_needed = TRUE;
228     GST_INFO_OBJECT (self, "Interlaced mode changed to %d", !progressive);
229   }
230
231   base->need_valign = FALSE;
232   base->min_buffers = 2 + 4;    /* max num pic references + scratch surfaces */
233   base->need_negotiation = negotiation_needed;
234   g_clear_pointer (&base->input_state, gst_video_codec_state_unref);
235   base->input_state = gst_video_codec_state_ref (decoder->input_state);
236
237   return GST_FLOW_OK;
238 }
239
240 static GstFlowReturn
241 gst_va_mpeg2_dec_new_picture (GstMpeg2Decoder * decoder,
242     GstVideoCodecFrame * frame, GstMpeg2Picture * picture)
243 {
244   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
245   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
246   GstVaDecodePicture *pic;
247   GstFlowReturn ret;
248
249   ret = gst_va_base_dec_prepare_output_frame (base, frame);
250   if (ret != GST_FLOW_OK)
251     goto error;
252
253   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
254
255   gst_mpeg2_picture_set_user_data (picture, pic,
256       (GDestroyNotify) gst_va_decode_picture_free);
257
258   GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
259       gst_va_decode_picture_get_surface (pic));
260
261   return GST_FLOW_OK;
262
263 error:
264   {
265     GST_WARNING_OBJECT (self, "Failed to allocated output buffer, return %s",
266         gst_flow_get_name (ret));
267     return ret;
268   }
269 }
270
271 static GstFlowReturn
272 gst_va_mpeg2_dec_new_field_picture (GstMpeg2Decoder * decoder,
273     GstMpeg2Picture * first_field, GstMpeg2Picture * second_field)
274 {
275   GstVaDecodePicture *first_pic, *second_pic;
276   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
277   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
278
279   first_pic = gst_mpeg2_picture_get_user_data (first_field);
280   if (!first_pic)
281     return GST_FLOW_ERROR;
282
283   second_pic = gst_va_decode_picture_new (base->decoder, first_pic->gstbuffer);
284   gst_mpeg2_picture_set_user_data (second_field, second_pic,
285       (GDestroyNotify) gst_va_decode_picture_free);
286
287   GST_LOG_OBJECT (self, "New va decode picture %p - %#x", second_pic,
288       gst_va_decode_picture_get_surface (second_pic));
289
290   return GST_FLOW_OK;
291 }
292
293 static inline guint32
294 _pack_f_code (guint8 f_code[2][2])
295 {
296   return (((guint32) f_code[0][0] << 12)
297       | ((guint32) f_code[0][1] << 8)
298       | ((guint32) f_code[1][0] << 4)
299       | (f_code[1][1]));
300 }
301
302 static inline void
303 _copy_quant_matrix (guint8 dst[64], const guint8 src[64])
304 {
305   memcpy (dst, src, 64);
306 }
307
308 static gboolean
309 gst_va_mpeg2_dec_add_quant_matrix (GstMpeg2Decoder * decoder,
310     GstMpeg2Picture * picture, GstMpeg2Slice * slice)
311 {
312   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
313   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
314   GstMpegVideoQuantMatrixExt *const quant_matrix = slice->quant_matrix;
315   guint8 *intra_quant_matrix = NULL;
316   guint8 *non_intra_quant_matrix = NULL;
317   guint8 *chroma_intra_quant_matrix = NULL;
318   guint8 *chroma_non_intra_quant_matrix = NULL;
319   VAIQMatrixBufferMPEG2 iq_matrix = { 0 };
320   GstVaDecodePicture *va_pic;
321
322   intra_quant_matrix = self->seq.intra_quantizer_matrix;
323   non_intra_quant_matrix = self->seq.non_intra_quantizer_matrix;
324
325   if (quant_matrix) {
326     if (quant_matrix->load_intra_quantiser_matrix)
327       intra_quant_matrix = quant_matrix->intra_quantiser_matrix;
328     if (quant_matrix->load_non_intra_quantiser_matrix)
329       non_intra_quant_matrix = quant_matrix->non_intra_quantiser_matrix;
330     if (quant_matrix->load_chroma_intra_quantiser_matrix)
331       chroma_intra_quant_matrix = quant_matrix->chroma_intra_quantiser_matrix;
332     if (quant_matrix->load_chroma_non_intra_quantiser_matrix)
333       chroma_non_intra_quant_matrix =
334           quant_matrix->chroma_non_intra_quantiser_matrix;
335   }
336
337   iq_matrix.load_intra_quantiser_matrix = intra_quant_matrix != NULL;
338   if (intra_quant_matrix)
339     _copy_quant_matrix (iq_matrix.intra_quantiser_matrix, intra_quant_matrix);
340
341   iq_matrix.load_non_intra_quantiser_matrix = non_intra_quant_matrix != NULL;
342   if (non_intra_quant_matrix)
343     _copy_quant_matrix (iq_matrix.non_intra_quantiser_matrix,
344         non_intra_quant_matrix);
345
346   iq_matrix.load_chroma_intra_quantiser_matrix =
347       chroma_intra_quant_matrix != NULL;
348   if (chroma_intra_quant_matrix)
349     _copy_quant_matrix (iq_matrix.chroma_intra_quantiser_matrix,
350         chroma_intra_quant_matrix);
351
352   iq_matrix.load_chroma_non_intra_quantiser_matrix =
353       chroma_non_intra_quant_matrix != NULL;
354   if (chroma_non_intra_quant_matrix)
355     _copy_quant_matrix (iq_matrix.chroma_non_intra_quantiser_matrix,
356         chroma_non_intra_quant_matrix);
357
358   va_pic = gst_mpeg2_picture_get_user_data (picture);
359   return gst_va_decoder_add_param_buffer (base->decoder, va_pic,
360       VAIQMatrixBufferType, &iq_matrix, sizeof (iq_matrix));
361 }
362
363 static inline uint32_t
364 _is_frame_start (GstMpeg2Picture * picture)
365 {
366   return (!picture->first_field
367       || (picture->structure == GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME))
368       ? 1 : 0;
369 }
370
371 static inline VASurfaceID
372 _get_surface_id (GstMpeg2Picture * picture)
373 {
374   GstVaDecodePicture *va_pic;
375
376   if (!picture)
377     return VA_INVALID_ID;
378
379   va_pic = gst_mpeg2_picture_get_user_data (picture);
380   if (!va_pic)
381     return VA_INVALID_ID;
382   return gst_va_decode_picture_get_surface (va_pic);
383 }
384
385 static GstFlowReturn
386 gst_va_mpeg2_dec_start_picture (GstMpeg2Decoder * decoder,
387     GstMpeg2Picture * picture, GstMpeg2Slice * slice,
388     GstMpeg2Picture * prev_picture, GstMpeg2Picture * next_picture)
389 {
390   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
391   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
392   GstVaDecodePicture *va_pic;
393   VAPictureParameterBufferMPEG2 pic_param;
394
395   va_pic = gst_mpeg2_picture_get_user_data (picture);
396
397   /* *INDENT-OFF* */
398   pic_param = (VAPictureParameterBufferMPEG2) {
399     .horizontal_size = base->width,
400     .vertical_size = base->height,
401     .forward_reference_picture = VA_INVALID_ID,
402     .backward_reference_picture = VA_INVALID_ID,
403     .picture_coding_type = slice->pic_hdr->pic_type,
404     .f_code = _pack_f_code (slice->pic_ext->f_code),
405     .picture_coding_extension.bits = {
406       .is_first_field = _is_frame_start (picture),
407       .intra_dc_precision = slice->pic_ext->intra_dc_precision,
408       .picture_structure = slice->pic_ext->picture_structure,
409       .top_field_first = slice->pic_ext->top_field_first,
410       .frame_pred_frame_dct = slice->pic_ext->frame_pred_frame_dct,
411       .concealment_motion_vectors = slice->pic_ext->concealment_motion_vectors,
412       .q_scale_type = slice->pic_ext->q_scale_type,
413       .intra_vlc_format = slice->pic_ext->intra_vlc_format,
414       .alternate_scan = slice->pic_ext->alternate_scan,
415       .repeat_first_field = slice->pic_ext->repeat_first_field,
416       .progressive_frame = slice->pic_ext->progressive_frame,
417     },
418   };
419   /* *INDENT-ON* */
420
421   switch (picture->type) {
422     case GST_MPEG_VIDEO_PICTURE_TYPE_B:{
423       VASurfaceID surface = _get_surface_id (next_picture);
424       if (surface == VA_INVALID_ID) {
425         GST_WARNING_OBJECT (self, "Missing the backward reference picture");
426         if (GST_VA_DISPLAY_IS_IMPLEMENTATION (base->display, MESA_GALLIUM))
427           return GST_FLOW_ERROR;
428         else if (GST_VA_DISPLAY_IS_IMPLEMENTATION (base->display, INTEL_IHD))
429           surface = gst_va_decode_picture_get_surface (va_pic);
430       }
431       pic_param.backward_reference_picture = surface;
432     }
433       /* fall-through */
434     case GST_MPEG_VIDEO_PICTURE_TYPE_P:{
435       VASurfaceID surface = _get_surface_id (prev_picture);
436       if (surface == VA_INVALID_ID) {
437         GST_WARNING_OBJECT (self, "Missing the forward reference picture");
438         if (GST_VA_DISPLAY_IS_IMPLEMENTATION (base->display, MESA_GALLIUM))
439           return GST_FLOW_ERROR;
440         else if (GST_VA_DISPLAY_IS_IMPLEMENTATION (base->display, INTEL_IHD))
441           surface = gst_va_decode_picture_get_surface (va_pic);
442       }
443       pic_param.forward_reference_picture = surface;
444     }
445     default:
446       break;
447   }
448
449   if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
450           VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
451     return GST_FLOW_ERROR;
452
453   if (!gst_va_mpeg2_dec_add_quant_matrix (decoder, picture, slice))
454     return GST_FLOW_ERROR;
455
456   return GST_FLOW_OK;
457 }
458
459 static GstFlowReturn
460 gst_va_mpeg2_dec_decode_slice (GstMpeg2Decoder * decoder,
461     GstMpeg2Picture * picture, GstMpeg2Slice * slice)
462 {
463   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
464   GstMpegVideoSliceHdr *header = &slice->header;
465   GstMpegVideoPacket *packet = &slice->packet;
466   GstVaDecodePicture *va_pic;
467   VASliceParameterBufferMPEG2 slice_param;
468
469   /* *INDENT-OFF* */
470   slice_param = (VASliceParameterBufferMPEG2) {
471     .slice_data_size = slice->size,
472     .slice_data_offset = 0,
473     .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
474     .macroblock_offset = header->header_size + 32,
475     .slice_horizontal_position = header->mb_column,
476     .slice_vertical_position = header->mb_row,
477     .quantiser_scale_code = header->quantiser_scale_code,
478     .intra_slice_flag = header->intra_slice,
479   };
480   /* *INDENT-ON* */
481
482   va_pic = gst_mpeg2_picture_get_user_data (picture);
483   if (!gst_va_decoder_add_slice_buffer (base->decoder, va_pic,
484           &slice_param, sizeof (slice_param),
485           (guint8 *) (packet->data + slice->sc_offset), slice->size))
486     return GST_FLOW_ERROR;
487
488   return GST_FLOW_OK;
489 }
490
491 static GstFlowReturn
492 gst_va_mpeg2_dec_end_picture (GstMpeg2Decoder * decoder,
493     GstMpeg2Picture * picture)
494 {
495   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
496   GstVaDecodePicture *va_pic;
497
498   GST_LOG_OBJECT (base, "end picture %p, (poc %d)",
499       picture, picture->pic_order_cnt);
500
501   va_pic = gst_mpeg2_picture_get_user_data (picture);
502
503   if (!gst_va_decoder_decode (base->decoder, va_pic))
504     return GST_FLOW_ERROR;
505
506   return GST_FLOW_OK;
507 }
508
509 static GstFlowReturn
510 gst_va_mpeg2_dec_output_picture (GstMpeg2Decoder * decoder,
511     GstVideoCodecFrame * frame, GstMpeg2Picture * picture)
512 {
513   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
514   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
515   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
516   gboolean ret;
517
518   GST_LOG_OBJECT (self,
519       "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
520
521   ret = gst_va_base_dec_process_output (base, frame, picture->discont_state,
522       picture->buffer_flags);
523   gst_mpeg2_picture_unref (picture);
524
525   if (ret)
526     return gst_video_decoder_finish_frame (vdec, frame);
527   return GST_FLOW_ERROR;
528 }
529
530 static void
531 gst_va_mpeg2_dec_init (GTypeInstance * instance, gpointer g_class)
532 {
533   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
534   GST_VA_MPEG2_DEC (instance)->progressive = 1;
535 }
536
537 static void
538 gst_va_mpeg2_dec_dispose (GObject * object)
539 {
540   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
541   G_OBJECT_CLASS (parent_class)->dispose (object);
542 }
543
544 static void
545 gst_va_mpeg2_dec_class_init (gpointer g_class, gpointer class_data)
546 {
547   GstCaps *src_doc_caps, *sink_doc_caps;
548   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
549   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
550   GstMpeg2DecoderClass *mpeg2decoder_class = GST_MPEG2_DECODER_CLASS (g_class);
551   struct CData *cdata = class_data;
552   gchar *long_name;
553
554   if (cdata->description) {
555     long_name = g_strdup_printf ("VA-API Mpeg2 Decoder in %s",
556         cdata->description);
557   } else {
558     long_name = g_strdup ("VA-API Mpeg2 Decoder");
559   }
560
561   gst_element_class_set_metadata (element_class, long_name,
562       "Codec/Decoder/Video/Hardware",
563       "VA-API based Mpeg2 video decoder", "He Junyan <junyan.he@intel.com>");
564
565   sink_doc_caps = gst_caps_from_string (sink_caps_str);
566   src_doc_caps = gst_caps_from_string (src_caps_str);
567
568   parent_class = g_type_class_peek_parent (g_class);
569
570   /**
571    * GstVaMpeg2Dec:device-path:
572    *
573    * It shows the DRM device path used for the VA operation, if any.
574    *
575    * Since: 1.22
576    */
577   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), MPEG2,
578       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
579       src_doc_caps, sink_doc_caps);
580
581   gobject_class->dispose = gst_va_mpeg2_dec_dispose;
582
583   mpeg2decoder_class->new_sequence =
584       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_sequence);
585   mpeg2decoder_class->new_picture =
586       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_picture);
587   mpeg2decoder_class->new_field_picture =
588       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_field_picture);
589   mpeg2decoder_class->start_picture =
590       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_start_picture);
591   mpeg2decoder_class->decode_slice =
592       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_decode_slice);
593   mpeg2decoder_class->end_picture =
594       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_end_picture);
595   mpeg2decoder_class->output_picture =
596       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_output_picture);
597
598   g_free (long_name);
599   g_free (cdata->description);
600   g_free (cdata->render_device_path);
601   gst_caps_unref (cdata->src_caps);
602   gst_caps_unref (cdata->sink_caps);
603   g_free (cdata);
604 }
605
606 static gpointer
607 _register_debug_category (gpointer data)
608 {
609   GST_DEBUG_CATEGORY_INIT (gst_va_mpeg2dec_debug, "vampeg2dec", 0,
610       "VA Mpeg2 decoder");
611
612   return NULL;
613 }
614
615 gboolean
616 gst_va_mpeg2_dec_register (GstPlugin * plugin, GstVaDevice * device,
617     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
618 {
619   static GOnce debug_once = G_ONCE_INIT;
620   GType type;
621   GTypeInfo type_info = {
622     .class_size = sizeof (GstVaMpeg2DecClass),
623     .class_init = gst_va_mpeg2_dec_class_init,
624     .instance_size = sizeof (GstVaMpeg2Dec),
625     .instance_init = gst_va_mpeg2_dec_init,
626   };
627   struct CData *cdata;
628   gboolean ret;
629   gchar *type_name, *feature_name;
630
631   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
632   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
633   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
634   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
635
636   cdata = g_new (struct CData, 1);
637   cdata->description = NULL;
638   cdata->render_device_path = g_strdup (device->render_device_path);
639   cdata->sink_caps = gst_caps_ref (sink_caps);
640   cdata->src_caps = gst_caps_ref (src_caps);
641
642   /* class data will be leaked if the element never gets instantiated */
643   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
644       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
645   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
646
647   type_info.class_data = cdata;
648
649   /* The first decoder to be registered should use a constant name,
650    * like vampeg2dec, for any additional decoders, we create unique
651    * names, using inserting the render device name. */
652   if (device->index == 0) {
653     type_name = g_strdup ("GstVaMpeg2Dec");
654     feature_name = g_strdup ("vampeg2dec");
655   } else {
656     gchar *basename = g_path_get_basename (device->render_device_path);
657     type_name = g_strdup_printf ("GstVa%sMpeg2Dec", basename);
658     feature_name = g_strdup_printf ("va%smpeg2dec", basename);
659     cdata->description = basename;
660
661     /* lower rank for non-first device */
662     if (rank > 0)
663       rank--;
664   }
665
666   g_once (&debug_once, _register_debug_category, NULL);
667
668   type = g_type_register_static (GST_TYPE_MPEG2_DECODER,
669       type_name, &type_info, 0);
670
671   ret = gst_element_register (plugin, feature_name, rank, type);
672
673   g_free (type_name);
674   g_free (feature_name);
675
676   return ret;
677 }