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