codecs: mpeg2decoder: Use GstFlowReturn everywhere
[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   gboolean need_negotiation;
75
76   GstMpegVideoSequenceHdr seq;
77 };
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 (!self->need_negotiation)
99     return TRUE;
100
101   self->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 (GST_VA_BASE_DEC_GET_PARENT_CLASS
134       (decoder))->negotiate (decoder);
135 }
136
137 static VAProfile
138 _map_profile (GstMpegVideoProfile profile)
139 {
140   VAProfile p = VAProfileNone;
141
142   switch (profile) {
143     case GST_MPEG_VIDEO_PROFILE_SIMPLE:
144       p = VAProfileMPEG2Simple;
145       break;
146     case GST_MPEG_VIDEO_PROFILE_MAIN:
147       p = VAProfileMPEG2Main;
148       break;
149     default:
150       p = VAProfileNone;
151       break;
152   }
153
154   return p;
155 }
156
157 static VAProfile
158 _get_profile (GstVaMpeg2Dec * self, GstMpegVideoProfile profile,
159     const GstMpegVideoSequenceExt * seq_ext,
160     const GstMpegVideoSequenceScalableExt * seq_scalable_ext)
161 {
162   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
163   VAProfile hw_profile;
164
165   hw_profile = _map_profile (profile);
166   if (hw_profile == VAProfileNone)
167     return hw_profile;
168
169   /* promote the profile if hw does not support, until we get one */
170   do {
171     if (gst_va_decoder_has_profile (base->decoder, hw_profile))
172       return hw_profile;
173
174     /* Otherwise, try to map to a higher profile */
175     switch (profile) {
176       case GST_MPEG_VIDEO_PROFILE_SIMPLE:
177         hw_profile = VAProfileMPEG2Main;
178         break;
179       case GST_MPEG_VIDEO_PROFILE_HIGH:
180         /* Try to map to main profile if no high profile specific bits used */
181         if (!seq_scalable_ext && (seq_ext && seq_ext->chroma_format == 1)) {
182           hw_profile = VAProfileMPEG2Main;
183           break;
184         }
185         /* fall-through */
186       default:
187         GST_ERROR_OBJECT (self, "profile %d is unsupported.", profile);
188         hw_profile = VAProfileNone;
189         break;
190     }
191   } while (hw_profile != VAProfileNone);
192
193   return hw_profile;
194 }
195
196 static guint
197 _get_rtformat (GstVaMpeg2Dec * self, GstMpegVideoChromaFormat chroma_format)
198 {
199   guint ret = 0;
200
201   switch (chroma_format) {
202     case GST_MPEG_VIDEO_CHROMA_420:
203       ret = VA_RT_FORMAT_YUV420;
204       break;
205     case GST_MPEG_VIDEO_CHROMA_422:
206       ret = VA_RT_FORMAT_YUV422;
207       break;
208     case GST_MPEG_VIDEO_CHROMA_444:
209       ret = VA_RT_FORMAT_YUV444;
210       break;
211     default:
212       GST_ERROR_OBJECT (self, "Unsupported chroma format: %d ", chroma_format);
213       break;
214   }
215
216   return ret;
217 }
218
219 static GstFlowReturn
220 gst_va_mpeg2_dec_new_sequence (GstMpeg2Decoder * decoder,
221     const GstMpegVideoSequenceHdr * seq,
222     const GstMpegVideoSequenceExt * seq_ext,
223     const GstMpegVideoSequenceDisplayExt * seq_display_ext,
224     const GstMpegVideoSequenceScalableExt * seq_scalable_ext)
225 {
226   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
227   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
228   VAProfile profile;
229   GstMpegVideoProfile mpeg_profile;
230   gboolean negotiation_needed = FALSE;
231   guint rt_format;
232   gint width, height;
233   gboolean progressive;
234
235   self->seq = *seq;
236
237   width = seq->width;
238   height = seq->height;
239   if (seq_ext) {
240     width = (width & 0x0fff) | ((guint32) seq_ext->horiz_size_ext << 12);
241     height = (height & 0x0fff) | ((guint32) seq_ext->vert_size_ext << 12);
242   }
243
244   mpeg_profile = GST_MPEG_VIDEO_PROFILE_MAIN;
245   if (seq_ext)
246     mpeg_profile = seq_ext->profile;
247
248   profile = _get_profile (self, mpeg_profile, seq_ext, seq_scalable_ext);
249   if (profile == VAProfileNone)
250     return GST_FLOW_NOT_NEGOTIATED;
251
252   rt_format = _get_rtformat (self,
253       seq_ext ? seq_ext->chroma_format : GST_MPEG_VIDEO_CHROMA_420);
254   if (rt_format == 0)
255     return GST_FLOW_NOT_NEGOTIATED;
256
257   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
258           rt_format, width, height)) {
259     base->profile = profile;
260     base->rt_format = rt_format;
261     base->width = width;
262     base->height = height;
263
264     negotiation_needed = TRUE;
265
266     GST_INFO_OBJECT (self, "Format changed to %s [%x] (%dx%d)",
267         gst_va_profile_name (profile), rt_format, base->width, base->height);
268   }
269
270   progressive = seq_ext ? seq_ext->progressive : 1;
271   if (self->progressive != progressive) {
272     self->progressive = progressive;
273
274     negotiation_needed = TRUE;
275     GST_INFO_OBJECT (self, "Interlaced mode changed to %d", !progressive);
276   }
277
278   base->need_valign = FALSE;
279
280   base->min_buffers = 2 + 4;    /* max num pic references + scratch surfaces */
281
282   if (negotiation_needed) {
283     self->need_negotiation = TRUE;
284     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
285       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
286       return GST_FLOW_NOT_NEGOTIATED;
287     }
288   }
289
290   return GST_FLOW_OK;
291 }
292
293 static GstFlowReturn
294 gst_va_mpeg2_dec_new_picture (GstMpeg2Decoder * decoder,
295     GstVideoCodecFrame * frame, GstMpeg2Picture * picture)
296 {
297   GstFlowReturn ret;
298   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
299   GstVaDecodePicture *pic;
300   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
301   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
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   /* The slice data pass to driver should be a full packet
524      include the start code. The packet->offset return by
525      gst_mpeg_video_parse does not include the start code
526      and so we need to wrap back 4 bytes. */
527   g_assert (packet->offset >= 4);
528
529   /* *INDENT-OFF* */
530   slice_param = (VASliceParameterBufferMPEG2) {
531     .slice_data_size = packet->size + 4 /* start code */,
532     .slice_data_offset = 0,
533     .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
534     .macroblock_offset = header->header_size + 32,
535     .slice_horizontal_position = header->mb_column,
536     .slice_vertical_position = header->mb_row,
537     .quantiser_scale_code = header->quantiser_scale_code,
538     .intra_slice_flag = header->intra_slice,
539   };
540   /* *INDENT-ON* */
541
542   va_pic = gst_mpeg2_picture_get_user_data (picture);
543   if (!gst_va_decoder_add_slice_buffer (base->decoder, va_pic,
544           &slice_param, sizeof (slice_param),
545           (guint8 *) (packet->data + packet->offset - 4 /* start code */ ),
546           packet->size + 4 /* start code */ ))
547     return GST_FLOW_ERROR;
548
549   return GST_FLOW_OK;
550 }
551
552 static GstFlowReturn
553 gst_va_mpeg2_dec_end_picture (GstMpeg2Decoder * decoder,
554     GstMpeg2Picture * picture)
555 {
556   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
557   GstVaDecodePicture *va_pic;
558
559   GST_LOG_OBJECT (base, "end picture %p, (poc %d)",
560       picture, picture->pic_order_cnt);
561
562   va_pic = gst_mpeg2_picture_get_user_data (picture);
563
564   if (!gst_va_decoder_decode (base->decoder, va_pic))
565     return GST_FLOW_ERROR;
566
567   return GST_FLOW_OK;
568 }
569
570 static GstFlowReturn
571 gst_va_mpeg2_dec_output_picture (GstMpeg2Decoder * decoder,
572     GstVideoCodecFrame * frame, GstMpeg2Picture * picture)
573 {
574   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
575   GstVaMpeg2Dec *self = GST_VA_MPEG2_DEC (decoder);
576
577   GST_LOG_OBJECT (self,
578       "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
579
580   if (base->copy_frames)
581     gst_va_base_dec_copy_output_buffer (base, frame);
582
583   if (picture->buffer_flags != 0) {
584     gboolean interlaced =
585         (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_INTERLACED) != 0;
586     gboolean tff = (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_TFF) != 0;
587
588     GST_TRACE_OBJECT (self,
589         "apply buffer flags 0x%x (interlaced %d, top-field-first %d)",
590         picture->buffer_flags, interlaced, tff);
591     GST_BUFFER_FLAG_SET (frame->output_buffer, picture->buffer_flags);
592   }
593
594   gst_mpeg2_picture_unref (picture);
595
596   return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
597 }
598
599 static void
600 gst_va_mpeg2_dec_init (GTypeInstance * instance, gpointer g_class)
601 {
602   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
603 }
604
605 static void
606 gst_va_mpeg2_dec_dispose (GObject * object)
607 {
608   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
609   G_OBJECT_CLASS (GST_VA_BASE_DEC_GET_PARENT_CLASS (object))->dispose (object);
610 }
611
612 static void
613 gst_va_mpeg2_dec_class_init (gpointer g_class, gpointer class_data)
614 {
615   GstCaps *src_doc_caps, *sink_doc_caps;
616   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
617   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
618   GstMpeg2DecoderClass *mpeg2decoder_class = GST_MPEG2_DECODER_CLASS (g_class);
619   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
620   struct CData *cdata = class_data;
621   gchar *long_name;
622
623   if (cdata->description) {
624     long_name = g_strdup_printf ("VA-API Mpeg2 Decoder in %s",
625         cdata->description);
626   } else {
627     long_name = g_strdup ("VA-API Mpeg2 Decoder");
628   }
629
630   gst_element_class_set_metadata (element_class, long_name,
631       "Codec/Decoder/Video/Hardware",
632       "VA-API based Mpeg2 video decoder", "He Junyan <junyan.he@intel.com>");
633
634   sink_doc_caps = gst_caps_from_string (sink_caps_str);
635   src_doc_caps = gst_caps_from_string (src_caps_str);
636
637   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), MPEG2,
638       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
639       src_doc_caps, sink_doc_caps);
640
641   gobject_class->dispose = gst_va_mpeg2_dec_dispose;
642
643   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_negotiate);
644
645   mpeg2decoder_class->new_sequence =
646       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_sequence);
647   mpeg2decoder_class->new_picture =
648       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_picture);
649   mpeg2decoder_class->new_field_picture =
650       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_new_field_picture);
651   mpeg2decoder_class->start_picture =
652       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_start_picture);
653   mpeg2decoder_class->decode_slice =
654       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_decode_slice);
655   mpeg2decoder_class->end_picture =
656       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_end_picture);
657   mpeg2decoder_class->output_picture =
658       GST_DEBUG_FUNCPTR (gst_va_mpeg2_dec_output_picture);
659
660   g_free (long_name);
661   g_free (cdata->description);
662   g_free (cdata->render_device_path);
663   gst_caps_unref (cdata->src_caps);
664   gst_caps_unref (cdata->sink_caps);
665   g_free (cdata);
666 }
667
668 static gpointer
669 _register_debug_category (gpointer data)
670 {
671   GST_DEBUG_CATEGORY_INIT (gst_va_mpeg2dec_debug, "vampeg2dec", 0,
672       "VA Mpeg2 decoder");
673
674   return NULL;
675 }
676
677 gboolean
678 gst_va_mpeg2_dec_register (GstPlugin * plugin, GstVaDevice * device,
679     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
680 {
681   static GOnce debug_once = G_ONCE_INIT;
682   GType type;
683   GTypeInfo type_info = {
684     .class_size = sizeof (GstVaMpeg2DecClass),
685     .class_init = gst_va_mpeg2_dec_class_init,
686     .instance_size = sizeof (GstVaMpeg2Dec),
687     .instance_init = gst_va_mpeg2_dec_init,
688   };
689   struct CData *cdata;
690   gboolean ret;
691   gchar *type_name, *feature_name;
692
693   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
694   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
695   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
696   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
697
698   cdata = g_new (struct CData, 1);
699   cdata->description = NULL;
700   cdata->render_device_path = g_strdup (device->render_device_path);
701   cdata->sink_caps = gst_caps_ref (sink_caps);
702   cdata->src_caps = gst_caps_ref (src_caps);
703
704   /* class data will be leaked if the element never gets instantiated */
705   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
706       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
707   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
708
709   type_info.class_data = cdata;
710
711   type_name = g_strdup ("GstVaMpeg2dec");
712   feature_name = g_strdup ("vampeg2dec");
713
714   /* The first decoder to be registered should use a constant name,
715    * like vampeg2dec, for any additional decoders, we create unique
716    * names, using inserting the render device name. */
717   if (g_type_from_name (type_name)) {
718     gchar *basename = g_path_get_basename (device->render_device_path);
719     g_free (type_name);
720     g_free (feature_name);
721     type_name = g_strdup_printf ("GstVa%sMpeg2Dec", basename);
722     feature_name = g_strdup_printf ("va%smpeg2dec", basename);
723     cdata->description = basename;
724
725     /* lower rank for non-first device */
726     if (rank > 0)
727       rank--;
728   }
729
730   g_once (&debug_once, _register_debug_category, NULL);
731
732   type = g_type_register_static (GST_TYPE_MPEG2_DECODER,
733       type_name, &type_info, 0);
734
735   ret = gst_element_register (plugin, feature_name, rank, type);
736
737   g_free (type_name);
738   g_free (feature_name);
739
740   return ret;
741 }