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