va: Add and use gst_va_base_dec_prepare_output_frame().
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvavp9dec.c
1 /* GStreamer
2  * Copyright (C) 2020 Igalia, S.L.
3  *     Author: Víctor Jáquez <vjaquez@igalia.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-vavp9dec
23  * @title: vavp9dec
24  * @short_description: A VA-API based VP9 video decoder
25  *
26  * vavp9dec decodes VP9 bitstreams to VA surfaces using the
27  * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
28  * driver.
29  *
30  * The decoding surfaces can be mapped onto main memory as video
31  * frames.
32  *
33  * ## Example launch line
34  * ```
35  * gst-launch-1.0 filesrc location=sample.webm ! parsebin ! vavp9dec ! autovideosink
36  * ```
37  *
38  * Since: 1.20
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gstvavp9dec.h"
47
48 #include "gstvabasedec.h"
49
50 GST_DEBUG_CATEGORY_STATIC (gst_va_vp9dec_debug);
51 #ifndef GST_DISABLE_GST_DEBUG
52 #define GST_CAT_DEFAULT gst_va_vp9dec_debug
53 #else
54 #define GST_CAT_DEFAULT NULL
55 #endif
56
57 #define GST_VA_VP9_DEC(obj)           ((GstVaVp9Dec *) obj)
58 #define GST_VA_VP9_DEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaVp9DecClass))
59 #define GST_VA_VP9_DEC_CLASS(klass)   ((GstVaVp9DecClass *) klass)
60
61 typedef struct _GstVaVp9Dec GstVaVp9Dec;
62 typedef struct _GstVaVp9DecClass GstVaVp9DecClass;
63
64 struct _GstVaVp9DecClass
65 {
66   GstVaBaseDecClass parent_class;
67 };
68
69 struct _GstVaVp9Dec
70 {
71   GstVaBaseDec parent;
72   GstVp9Segmentation segmentation[GST_VP9_MAX_SEGMENTS];
73 };
74
75 static GstElementClass *parent_class = NULL;
76
77 /* *INDENT-OFF* */
78 static const gchar *src_caps_str =
79     GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
80         "{ NV12 }") " ;"
81     GST_VIDEO_CAPS_MAKE ("{ NV12 }");
82 /* *INDENT-ON* */
83
84 static const gchar *sink_caps_str = "video/x-vp9";
85
86 static guint
87 _get_rtformat (GstVaVp9Dec * self, GstVP9Profile profile,
88     GstVp9BitDepth bit_depth, gint subsampling_x, gint subsampling_y)
89 {
90   switch (profile) {
91     case GST_VP9_PROFILE_0:
92       return VA_RT_FORMAT_YUV420;
93     case GST_VP9_PROFILE_1:
94       if (subsampling_x == 1 && subsampling_y == 0)
95         return VA_RT_FORMAT_YUV422;
96       else if (subsampling_x == 0 && subsampling_y == 0)
97         return VA_RT_FORMAT_YUV444;
98       break;
99     case GST_VP9_PROFILE_2:
100       if (bit_depth == GST_VP9_BIT_DEPTH_10)
101         return VA_RT_FORMAT_YUV420_10;
102       else if (bit_depth == GST_VP9_BIT_DEPTH_12)
103         return VA_RT_FORMAT_YUV420_12;
104       break;
105     case GST_VP9_PROFILE_3:
106       if (subsampling_x == 1 && subsampling_y == 0) {
107         if (bit_depth == GST_VP9_BIT_DEPTH_10)
108           return VA_RT_FORMAT_YUV422_10;
109         else if (bit_depth == GST_VP9_BIT_DEPTH_12)
110           return VA_RT_FORMAT_YUV422_12;
111       } else if (subsampling_x == 0 && subsampling_y == 0) {
112         if (bit_depth == GST_VP9_BIT_DEPTH_10)
113           return VA_RT_FORMAT_YUV444_10;
114         else if (bit_depth == GST_VP9_BIT_DEPTH_12)
115           return VA_RT_FORMAT_YUV444_12;
116       }
117       break;
118     default:
119       break;
120   }
121
122   GST_ERROR_OBJECT (self, "Unsupported chroma format");
123   return 0;
124 }
125
126 static VAProfile
127 _get_profile (GstVaVp9Dec * self, GstVP9Profile profile)
128 {
129   switch (profile) {
130     case GST_VP9_PROFILE_0:
131       return VAProfileVP9Profile0;
132     case GST_VP9_PROFILE_1:
133       return VAProfileVP9Profile1;
134     case GST_VP9_PROFILE_2:
135       return VAProfileVP9Profile2;
136     case GST_VP9_PROFILE_3:
137       return VAProfileVP9Profile3;
138     default:
139       break;
140   }
141
142   GST_ERROR_OBJECT (self, "Unsupported profile");
143   return VAProfileNone;
144 }
145
146 static GstFlowReturn
147 gst_va_vp9_new_sequence (GstVp9Decoder * decoder,
148     const GstVp9FrameHeader * frame_hdr, gint max_dpb_size)
149 {
150   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
151   GstVaVp9Dec *self = GST_VA_VP9_DEC (decoder);
152   VAProfile profile;
153   gboolean negotiation_needed = FALSE;
154   guint rt_format;
155
156   profile = _get_profile (self, frame_hdr->profile);
157   if (profile == VAProfileNone)
158     return GST_FLOW_NOT_NEGOTIATED;
159
160   if (!gst_va_decoder_has_profile (base->decoder, profile)) {
161     GST_ERROR_OBJECT (self, "Profile %s is not supported",
162         gst_va_profile_name (profile));
163     return GST_FLOW_NOT_NEGOTIATED;
164   }
165
166   rt_format = _get_rtformat (self, frame_hdr->profile, frame_hdr->bit_depth,
167       frame_hdr->subsampling_x, frame_hdr->subsampling_y);
168   if (rt_format == 0)
169     return GST_FLOW_NOT_NEGOTIATED;
170
171   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
172           rt_format, frame_hdr->width, frame_hdr->height)) {
173     base->profile = profile;
174     base->width = frame_hdr->width;
175     base->height = frame_hdr->height;
176     base->rt_format = rt_format;
177     negotiation_needed = TRUE;
178   }
179
180   base->min_buffers = GST_VP9_REF_FRAMES;
181
182   base->need_negotiation = negotiation_needed;
183
184   return GST_FLOW_OK;
185 }
186
187 static GstFlowReturn
188 _check_resolution_change (GstVaVp9Dec * self, GstVp9Picture * picture)
189 {
190   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
191   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
192
193   if ((base->width != frame_hdr->width) || base->height != frame_hdr->height) {
194     base->width = frame_hdr->width;
195     base->height = frame_hdr->height;
196
197     base->need_negotiation = TRUE;
198     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
199       GST_ERROR_OBJECT (self, "Resolution changed, but failed to"
200           " negotiate with downstream");
201       return GST_FLOW_NOT_NEGOTIATED;
202
203       /* @TODO: if negotiation fails, decoder should resize output
204        * frame. For that we would need an auxiliar allocator, and
205        * later use GstVaFilter or GstVideoConverter. */
206     }
207   }
208
209   return GST_FLOW_OK;
210 }
211
212 static GstFlowReturn
213 gst_va_vp9_dec_new_picture (GstVp9Decoder * decoder,
214     GstVideoCodecFrame * frame, GstVp9Picture * picture)
215 {
216   GstFlowReturn ret;
217   GstVaVp9Dec *self = GST_VA_VP9_DEC (decoder);
218   GstVaDecodePicture *pic;
219   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
220
221   ret = _check_resolution_change (self, picture);
222   if (ret != GST_FLOW_OK)
223     return ret;
224
225   ret = gst_va_base_dec_prepare_output_frame (base, frame);
226   if (ret != GST_FLOW_OK)
227     goto error;
228
229   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
230
231   gst_vp9_picture_set_user_data (picture, pic,
232       (GDestroyNotify) gst_va_decode_picture_free);
233
234   GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
235       gst_va_decode_picture_get_surface (pic));
236
237   return GST_FLOW_OK;
238
239 error:
240   {
241     GST_WARNING_OBJECT (self, "Failed to allocated output buffer, return %s",
242         gst_flow_get_name (ret));
243     return ret;
244   }
245 }
246
247 static inline gboolean
248 _fill_param (GstVp9Decoder * decoder, GstVp9Picture * picture, GstVp9Dpb * dpb)
249 {
250   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
251   GstVaDecodePicture *va_pic;
252   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
253   const GstVp9LoopFilterParams *lfp = &frame_hdr->loop_filter_params;
254   const GstVp9SegmentationParams *sp = &frame_hdr->segmentation_params;
255   VADecPictureParameterBufferVP9 pic_param;
256   guint i;
257
258   /* *INDENT-OFF* */
259   pic_param = (VADecPictureParameterBufferVP9) {
260     .frame_width = base->width,
261     .frame_height = base->height,
262
263     .pic_fields.bits = {
264       .subsampling_x = frame_hdr->subsampling_x,
265       .subsampling_y = frame_hdr->subsampling_x,
266       .frame_type = frame_hdr->frame_type,
267       .show_frame = frame_hdr->show_frame,
268       .error_resilient_mode = frame_hdr->error_resilient_mode,
269       .intra_only = frame_hdr->intra_only,
270       .allow_high_precision_mv = frame_hdr->allow_high_precision_mv,
271       .mcomp_filter_type = frame_hdr->interpolation_filter,
272       .frame_parallel_decoding_mode = frame_hdr->frame_parallel_decoding_mode,
273       .reset_frame_context = frame_hdr->reset_frame_context,
274       .refresh_frame_context = frame_hdr->refresh_frame_context,
275       .frame_context_idx = frame_hdr->frame_context_idx,
276
277       .segmentation_enabled = sp->segmentation_enabled,
278       .segmentation_temporal_update = sp->segmentation_temporal_update,
279       .segmentation_update_map = sp->segmentation_update_map,
280
281       .last_ref_frame =
282           frame_hdr->ref_frame_idx[GST_VP9_REF_FRAME_LAST - 1],
283       .last_ref_frame_sign_bias =
284           frame_hdr->ref_frame_sign_bias[GST_VP9_REF_FRAME_LAST],
285       .golden_ref_frame =
286           frame_hdr->ref_frame_idx[GST_VP9_REF_FRAME_GOLDEN - 1],
287       .golden_ref_frame_sign_bias =
288           frame_hdr->ref_frame_sign_bias[GST_VP9_REF_FRAME_GOLDEN],
289       .alt_ref_frame =
290           frame_hdr->ref_frame_idx[GST_VP9_REF_FRAME_ALTREF - 1],
291       .alt_ref_frame_sign_bias =
292           frame_hdr->ref_frame_sign_bias[GST_VP9_REF_FRAME_ALTREF],
293
294       .lossless_flag = frame_hdr->lossless_flag,
295     },
296
297     .filter_level = lfp->loop_filter_level,
298     .sharpness_level = lfp->loop_filter_sharpness,
299     .log2_tile_rows = frame_hdr->tile_rows_log2,
300     .log2_tile_columns = frame_hdr->tile_cols_log2,
301
302     .frame_header_length_in_bytes = frame_hdr->frame_header_length_in_bytes,
303     .first_partition_size = frame_hdr->header_size_in_bytes,
304
305     .profile = frame_hdr->profile,
306     .bit_depth = frame_hdr->bit_depth
307   };
308   /* *INDENT-ON* */
309
310   memcpy (pic_param.mb_segment_tree_probs, sp->segmentation_tree_probs,
311       sizeof (sp->segmentation_tree_probs));
312
313   if (sp->segmentation_temporal_update) {
314     memcpy (pic_param.segment_pred_probs, sp->segmentation_pred_prob,
315         sizeof (sp->segmentation_pred_prob));
316   } else {
317     memset (pic_param.segment_pred_probs, 255,
318         sizeof (pic_param.segment_pred_probs));
319   }
320
321   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
322     if (dpb->pic_list[i]) {
323       GstVaDecodePicture *va_pic =
324           gst_vp9_picture_get_user_data (dpb->pic_list[i]);
325
326       pic_param.reference_frames[i] =
327           gst_va_decode_picture_get_surface (va_pic);
328     } else {
329       pic_param.reference_frames[i] = VA_INVALID_ID;
330     }
331   }
332
333   va_pic = gst_vp9_picture_get_user_data (picture);
334
335   return gst_va_decoder_add_param_buffer (base->decoder, va_pic,
336       VAPictureParameterBufferType, &pic_param, sizeof (pic_param));
337 }
338
339 static void
340 _update_segmentation (GstVaVp9Dec * self, GstVp9FrameHeader * header)
341 {
342   const GstVp9LoopFilterParams *lfp = &header->loop_filter_params;
343   const GstVp9QuantizationParams *qp = &header->quantization_params;
344   const GstVp9SegmentationParams *sp = &header->segmentation_params;
345   guint8 n_shift = lfp->loop_filter_level >> 5;
346   guint i;
347
348   for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
349     gint16 luma_dc_quant_scale;
350     gint16 luma_ac_quant_scale;
351     gint16 chroma_dc_quant_scale;
352     gint16 chroma_ac_quant_scale;
353     guint8 qindex;
354     guint8 lvl_lookup[GST_VP9_MAX_REF_LF_DELTAS][GST_VP9_MAX_MODE_LF_DELTAS];
355     gint lvl_seg = lfp->loop_filter_level;
356
357     /* 8.6.1 Dequantization functions */
358     qindex = gst_vp9_get_qindex (sp, qp, i);
359     luma_dc_quant_scale =
360         gst_vp9_get_dc_quant (qindex, qp->delta_q_y_dc, header->bit_depth);
361     luma_ac_quant_scale = gst_vp9_get_ac_quant (qindex, 0, header->bit_depth);
362     chroma_dc_quant_scale =
363         gst_vp9_get_dc_quant (qindex, qp->delta_q_uv_dc, header->bit_depth);
364     chroma_ac_quant_scale =
365         gst_vp9_get_ac_quant (qindex, qp->delta_q_uv_ac, header->bit_depth);
366
367     if (!lfp->loop_filter_level) {
368       memset (lvl_lookup, 0, sizeof (lvl_lookup));
369     } else {
370       /* 8.8.1 Loop filter frame init process */
371       if (gst_vp9_seg_feature_active (sp, i, GST_VP9_SEG_LVL_ALT_L)) {
372         if (sp->segmentation_abs_or_delta_update) {
373           lvl_seg = sp->feature_data[i][GST_VP9_SEG_LVL_ALT_L];
374         } else {
375           lvl_seg += sp->feature_data[i][GST_VP9_SEG_LVL_ALT_L];
376         }
377
378         lvl_seg = CLAMP (lvl_seg, 0, GST_VP9_MAX_LOOP_FILTER);
379       }
380
381       if (!lfp->loop_filter_delta_enabled) {
382         memset (lvl_lookup, lvl_seg, sizeof (lvl_lookup));
383       } else {
384         guint8 ref, mode;
385         gint intra_lvl = lvl_seg +
386             (lfp->loop_filter_ref_deltas[GST_VP9_REF_FRAME_INTRA] << n_shift);
387
388         memcpy (lvl_lookup, self->segmentation[i].filter_level,
389             sizeof (lvl_lookup));
390
391         lvl_lookup[GST_VP9_REF_FRAME_INTRA][0] =
392             CLAMP (intra_lvl, 0, GST_VP9_MAX_LOOP_FILTER);
393         for (ref = GST_VP9_REF_FRAME_LAST; ref < GST_VP9_REF_FRAME_MAX; ref++) {
394           for (mode = 0; mode < GST_VP9_MAX_MODE_LF_DELTAS; mode++) {
395             intra_lvl = lvl_seg + (lfp->loop_filter_ref_deltas[ref] << n_shift)
396                 + (lfp->loop_filter_mode_deltas[mode] << n_shift);
397             lvl_lookup[ref][mode] =
398                 CLAMP (intra_lvl, 0, GST_VP9_MAX_LOOP_FILTER);
399           }
400         }
401       }
402     }
403
404     /* *INDENT-OFF* */
405     self->segmentation[i] = (GstVp9Segmentation) {
406       .luma_dc_quant_scale = luma_dc_quant_scale,
407       .luma_ac_quant_scale = luma_ac_quant_scale,
408       .chroma_dc_quant_scale = chroma_dc_quant_scale,
409       .chroma_ac_quant_scale = chroma_ac_quant_scale,
410
411       .reference_frame_enabled = sp->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME],
412       .reference_frame = sp->feature_data[i][GST_VP9_SEG_LVL_REF_FRAME],
413       .reference_skip = sp->feature_enabled[i][GST_VP9_SEG_SEG_LVL_SKIP],
414     };
415     /* *INDENT-ON* */
416
417     memcpy (self->segmentation[i].filter_level, lvl_lookup,
418         sizeof (lvl_lookup));
419   }
420 }
421
422 static inline gboolean
423 _fill_slice (GstVp9Decoder * decoder, GstVp9Picture * picture)
424 {
425   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
426   GstVaVp9Dec *self = GST_VA_VP9_DEC (decoder);
427   GstVaDecodePicture *va_pic;
428   const GstVp9Segmentation *seg;
429   VASliceParameterBufferVP9 slice_param;
430   guint i;
431
432   _update_segmentation (self, &picture->frame_hdr);
433
434   /* *INDENT-OFF* */
435   slice_param = (VASliceParameterBufferVP9) {
436     .slice_data_size = picture->size,
437     .slice_data_offset = 0,
438     .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
439   };
440   /* *INDENT-ON* */
441
442   for (i = 0; i < GST_VP9_MAX_SEGMENTS; i++) {
443     seg = &self->segmentation[i];
444
445     /* *INDENT-OFF* */
446     slice_param.seg_param[i] = (VASegmentParameterVP9) {
447       .segment_flags.fields = {
448         .segment_reference_enabled = seg->reference_frame_enabled,
449         .segment_reference = seg->reference_frame,
450         .segment_reference_skipped = seg->reference_skip,
451       },
452       .luma_dc_quant_scale = seg->luma_dc_quant_scale,
453       .luma_ac_quant_scale = seg->luma_ac_quant_scale,
454       .chroma_dc_quant_scale = seg->chroma_dc_quant_scale,
455       .chroma_ac_quant_scale = seg->chroma_ac_quant_scale,
456      };
457      /* *INDENT-ON* */
458
459     memcpy (slice_param.seg_param[i].filter_level, seg->filter_level,
460         sizeof (slice_param.seg_param[i].filter_level));
461   }
462
463   va_pic = gst_vp9_picture_get_user_data (picture);
464
465   return gst_va_decoder_add_slice_buffer (base->decoder, va_pic, &slice_param,
466       sizeof (slice_param), (gpointer) picture->data, picture->size);
467 }
468
469 static GstFlowReturn
470 gst_va_vp9_decode_picture (GstVp9Decoder * decoder, GstVp9Picture * picture,
471     GstVp9Dpb * dpb)
472 {
473   if (_fill_param (decoder, picture, dpb) && _fill_slice (decoder, picture))
474     return GST_FLOW_OK;
475
476   return GST_FLOW_ERROR;
477 }
478
479 static GstFlowReturn
480 gst_va_vp9_dec_end_picture (GstVp9Decoder * decoder, GstVp9Picture * picture)
481 {
482   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
483   GstVaDecodePicture *va_pic;
484
485   GST_LOG_OBJECT (base, "end picture %p", picture);
486
487   va_pic = gst_vp9_picture_get_user_data (picture);
488
489   if (!gst_va_decoder_decode (base->decoder, va_pic))
490     return GST_FLOW_ERROR;
491
492   return GST_FLOW_OK;
493 }
494
495 static GstFlowReturn
496 gst_va_vp9_dec_output_picture (GstVp9Decoder * decoder,
497     GstVideoCodecFrame * frame, GstVp9Picture * picture)
498 {
499   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
500   GstVaVp9Dec *self = GST_VA_VP9_DEC (decoder);
501   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
502   gboolean ret;
503
504   GST_LOG_OBJECT (self, "Outputting picture %p", picture);
505
506   ret = gst_va_base_dec_process_output (base, frame, 0);
507   gst_vp9_picture_unref (picture);
508
509   if (ret)
510     return gst_video_decoder_finish_frame (vdec, frame);
511   return GST_FLOW_ERROR;
512 }
513
514 static GstVp9Picture *
515 gst_va_vp9_dec_duplicate_picture (GstVp9Decoder * decoder,
516     GstVideoCodecFrame * frame, GstVp9Picture * picture)
517 {
518   GstVaDecodePicture *va_pic, *va_dup;
519   GstVp9Picture *new_picture;
520
521   if (_check_resolution_change (GST_VA_VP9_DEC (decoder), picture) !=
522       GST_FLOW_OK) {
523     return NULL;
524   }
525
526   va_pic = gst_vp9_picture_get_user_data (picture);
527   va_dup = gst_va_decode_picture_dup (va_pic);
528
529   new_picture = gst_vp9_picture_new ();
530   new_picture->frame_hdr = picture->frame_hdr;
531
532   frame->output_buffer = gst_buffer_ref (va_dup->gstbuffer);
533
534   gst_vp9_picture_set_user_data (picture, va_dup,
535       (GDestroyNotify) gst_va_decode_picture_free);
536
537   return new_picture;
538 }
539
540 static gboolean
541 gst_va_vp9_dec_negotiate (GstVideoDecoder * decoder)
542 {
543   GstCapsFeatures *capsfeatures = NULL;
544   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
545   GstVaVp9Dec *self = GST_VA_VP9_DEC (decoder);
546   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
547   GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
548   gboolean need_open;
549
550   /* Ignore downstream renegotiation request. */
551   if (!base->need_negotiation)
552     return TRUE;
553
554   base->need_negotiation = FALSE;
555
556   need_open = TRUE;
557   /* VP9 profile entry should have the ability to handle dynamical
558    * resolution changes. If only the resolution changes, we should not
559    * re-create the config and context. */
560   if (gst_va_decoder_is_open (base->decoder)) {
561     VAProfile cur_profile;
562     guint cur_rtformat;
563     gint cur_width, cur_height;
564
565     if (!gst_va_decoder_get_config (base->decoder, &cur_profile,
566             &cur_rtformat, &cur_width, &cur_height))
567       return FALSE;
568
569     if (base->profile == cur_profile && base->rt_format == cur_rtformat) {
570       if (!gst_va_decoder_update_frame_size (base->decoder, base->width,
571               base->height))
572         return FALSE;
573
574       GST_INFO_OBJECT (self, "dynamical resolution changes from %dx%d to"
575           " %dx%d", cur_width, cur_height, base->width, base->height);
576
577       need_open = FALSE;
578     } else {
579       if (!gst_va_decoder_close (base->decoder))
580         return FALSE;
581     }
582   }
583
584   if (need_open) {
585     if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
586       return FALSE;
587
588     if (!gst_va_decoder_set_frame_size (base->decoder, base->width,
589             base->height))
590       return FALSE;
591   }
592
593   if (base->output_state)
594     gst_video_codec_state_unref (base->output_state);
595
596   gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
597       &capsfeatures);
598   if (format == GST_VIDEO_FORMAT_UNKNOWN)
599     return FALSE;
600
601   base->output_state =
602       gst_video_decoder_set_output_state (decoder, format,
603       base->width, base->height, vp9dec->input_state);
604
605   base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
606   if (capsfeatures)
607     gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
608
609   GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
610       base->output_state->caps);
611
612   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
613 }
614
615 static void
616 gst_va_vp9_dec_dispose (GObject * object)
617 {
618   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
619   G_OBJECT_CLASS (parent_class)->dispose (object);
620 }
621
622 static void
623 gst_va_vp9_dec_class_init (gpointer g_class, gpointer class_data)
624 {
625   GstCaps *src_doc_caps, *sink_doc_caps;
626   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
627   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
628   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
629   GstVp9DecoderClass *vp9_class = GST_VP9_DECODER_CLASS (g_class);
630   struct CData *cdata = class_data;
631   gchar *long_name;
632
633   if (cdata->description) {
634     long_name = g_strdup_printf ("VA-API VP9 Decoder in %s",
635         cdata->description);
636   } else {
637     long_name = g_strdup ("VA-API VP9 Decoder");
638   }
639
640   gst_element_class_set_metadata (element_class, long_name,
641       "Codec/Decoder/Video/Hardware", "VA-API based VP9 video decoder",
642       "Víctor Jáquez <vjaquez@igalia.com>");
643
644   sink_doc_caps = gst_caps_from_string (sink_caps_str);
645   src_doc_caps = gst_caps_from_string (src_caps_str);
646
647   parent_class = g_type_class_peek_parent (g_class);
648
649   /**
650    * GstVaVp9Dec:device-path:
651    *
652    * It shows the DRM device path used for the VA operation, if any.
653    *
654    * Since: 1.22
655    */
656   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), VP9,
657       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
658       src_doc_caps, sink_doc_caps);
659
660   gobject_class->dispose = gst_va_vp9_dec_dispose;
661
662   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_vp9_dec_negotiate);
663
664   vp9_class->new_sequence = GST_DEBUG_FUNCPTR (gst_va_vp9_new_sequence);
665   vp9_class->new_picture = GST_DEBUG_FUNCPTR (gst_va_vp9_dec_new_picture);
666   vp9_class->decode_picture = GST_DEBUG_FUNCPTR (gst_va_vp9_decode_picture);
667   vp9_class->end_picture = GST_DEBUG_FUNCPTR (gst_va_vp9_dec_end_picture);
668   vp9_class->output_picture = GST_DEBUG_FUNCPTR (gst_va_vp9_dec_output_picture);
669   vp9_class->duplicate_picture =
670       GST_DEBUG_FUNCPTR (gst_va_vp9_dec_duplicate_picture);
671
672   g_free (long_name);
673   g_free (cdata->description);
674   g_free (cdata->render_device_path);
675   gst_caps_unref (cdata->src_caps);
676   gst_caps_unref (cdata->sink_caps);
677   g_free (cdata);
678 }
679
680 static void
681 gst_va_vp9_dec_init (GTypeInstance * instance, gpointer g_class)
682 {
683   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
684 }
685
686 /* This element doesn't parse supreframes. Let's delegate it to the
687  * parser. */
688 static GstCaps *
689 _complete_sink_caps (GstCaps * sinkcaps)
690 {
691   gst_caps_set_simple (sinkcaps, "alignment", G_TYPE_STRING, "frame", NULL);
692   return gst_caps_ref (sinkcaps);
693 }
694
695 static gpointer
696 _register_debug_category (gpointer data)
697 {
698   GST_DEBUG_CATEGORY_INIT (gst_va_vp9dec_debug, "vavp9dec", 0,
699       "VA VP9 decoder");
700
701   return NULL;
702 }
703
704 gboolean
705 gst_va_vp9_dec_register (GstPlugin * plugin, GstVaDevice * device,
706     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
707 {
708   static GOnce debug_once = G_ONCE_INIT;
709   GType type;
710   GTypeInfo type_info = {
711     .class_size = sizeof (GstVaVp9DecClass),
712     .class_init = gst_va_vp9_dec_class_init,
713     .instance_size = sizeof (GstVaVp9Dec),
714     .instance_init = gst_va_vp9_dec_init,
715   };
716   struct CData *cdata;
717   gboolean ret;
718   gchar *type_name, *feature_name;
719
720   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
721   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
722   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
723   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
724
725   cdata = g_new (struct CData, 1);
726   cdata->description = NULL;
727   cdata->render_device_path = g_strdup (device->render_device_path);
728   cdata->sink_caps = _complete_sink_caps (sink_caps);
729   cdata->src_caps = gst_caps_ref (src_caps);
730
731   /* class data will be leaked if the element never gets instantiated */
732   GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
733   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
734
735   type_info.class_data = cdata;
736
737   /* The first decoder to be registered should use a constant name,
738    * like vavp9dec, for any additional decoders, we create unique
739    * names, using inserting the render device name. */
740   if (device->index == 0) {
741     type_name = g_strdup ("GstVaVp9Dec");
742     feature_name = g_strdup ("vavp9dec");
743   } else {
744     gchar *basename = g_path_get_basename (device->render_device_path);
745     type_name = g_strdup_printf ("GstVa%sVp9Dec", basename);
746     feature_name = g_strdup_printf ("va%svp9dec", basename);
747     cdata->description = basename;
748
749     /* lower rank for non-first device */
750     if (rank > 0)
751       rank--;
752   }
753
754   g_once (&debug_once, _register_debug_category, NULL);
755
756   type = g_type_register_static (GST_TYPE_VP9_DECODER,
757       type_name, &type_info, 0);
758
759   ret = gst_element_register (plugin, feature_name, rank, type);
760
761   g_free (type_name);
762   g_free (feature_name);
763
764   return ret;
765 }