d1724578b81c61540230398a9a856628de00b5c2
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / nvcodec / gstnvvp9dec.c
1 /* GStreamer
2  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/cuda/gstcudautils.h>
25
26 #include "gstnvvp9dec.h"
27 #include "gstnvdecoder.h"
28
29 #include <string.h>
30
31 GST_DEBUG_CATEGORY_STATIC (gst_nv_vp9_dec_debug);
32 #define GST_CAT_DEFAULT gst_nv_vp9_dec_debug
33
34 struct _GstNvVp9Dec
35 {
36   GstVp9Decoder parent;
37
38   GstVideoCodecState *output_state;
39
40   GstCudaContext *context;
41   GstNvDecoder *decoder;
42   CUVIDPICPARAMS params;
43
44   guint width, height;
45   GstVP9Profile profile;
46 };
47
48 struct _GstNvVp9DecClass
49 {
50   GstVp9DecoderClass parent_class;
51   guint cuda_device_id;
52 };
53
54 #define gst_nv_vp9_dec_parent_class parent_class
55
56 /**
57  * GstNvVp9Dec:
58  *
59  * Since: 1.20
60  */
61 G_DEFINE_TYPE (GstNvVp9Dec, gst_nv_vp9_dec, GST_TYPE_VP9_DECODER);
62
63 static void gst_nv_vp9_dec_set_context (GstElement * element,
64     GstContext * context);
65 static gboolean gst_nv_vp9_dec_open (GstVideoDecoder * decoder);
66 static gboolean gst_nv_vp9_dec_close (GstVideoDecoder * decoder);
67 static gboolean gst_nv_vp9_dec_negotiate (GstVideoDecoder * decoder);
68 static gboolean gst_nv_vp9_dec_decide_allocation (GstVideoDecoder *
69     decoder, GstQuery * query);
70 static gboolean gst_nv_vp9_dec_src_query (GstVideoDecoder * decoder,
71     GstQuery * query);
72
73 /* GstVp9Decoder */
74 static GstFlowReturn gst_nv_vp9_dec_new_sequence (GstVp9Decoder * decoder,
75     const GstVp9FrameHeader * frame_hdr, gint max_dpb_size);
76 static GstFlowReturn gst_nv_vp9_dec_new_picture (GstVp9Decoder * decoder,
77     GstVideoCodecFrame * frame, GstVp9Picture * picture);
78 static GstVp9Picture *gst_nv_vp9_dec_duplicate_picture (GstVp9Decoder *
79     decoder, GstVideoCodecFrame * frame, GstVp9Picture * picture);
80 static GstFlowReturn gst_nv_vp9_dec_decode_picture (GstVp9Decoder * decoder,
81     GstVp9Picture * picture, GstVp9Dpb * dpb);
82 static GstFlowReturn gst_nv_vp9_dec_output_picture (GstVp9Decoder *
83     decoder, GstVideoCodecFrame * frame, GstVp9Picture * picture);
84 static guint gst_nv_vp9_dec_get_preferred_output_delay (GstVp9Decoder * decoder,
85     gboolean is_live);
86
87 static void
88 gst_nv_vp9_dec_class_init (GstNvVp9DecClass * klass)
89 {
90   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
92   GstVp9DecoderClass *vp9decoder_class = GST_VP9_DECODER_CLASS (klass);
93
94   element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_set_context);
95
96   decoder_class->open = GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_open);
97   decoder_class->close = GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_close);
98   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_negotiate);
99   decoder_class->decide_allocation =
100       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_decide_allocation);
101   decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_src_query);
102
103   vp9decoder_class->new_sequence =
104       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_new_sequence);
105   vp9decoder_class->new_picture =
106       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_new_picture);
107   vp9decoder_class->duplicate_picture =
108       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_duplicate_picture);
109   vp9decoder_class->decode_picture =
110       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_decode_picture);
111   vp9decoder_class->output_picture =
112       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_output_picture);
113   vp9decoder_class->get_preferred_output_delay =
114       GST_DEBUG_FUNCPTR (gst_nv_vp9_dec_get_preferred_output_delay);
115
116   GST_DEBUG_CATEGORY_INIT (gst_nv_vp9_dec_debug,
117       "nvvp9dec", 0, "NVIDIA VP9 Decoder");
118
119   gst_type_mark_as_plugin_api (GST_TYPE_NV_VP9_DEC, 0);
120 }
121
122 static void
123 gst_nv_vp9_dec_init (GstNvVp9Dec * self)
124 {
125 }
126
127 static void
128 gst_nv_vp9_dec_set_context (GstElement * element, GstContext * context)
129 {
130   GstNvVp9Dec *self = GST_NV_VP9_DEC (element);
131   GstNvVp9DecClass *klass = GST_NV_VP9_DEC_GET_CLASS (self);
132
133   GST_DEBUG_OBJECT (self, "set context %s",
134       gst_context_get_context_type (context));
135
136   if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
137           &self->context)) {
138     goto done;
139   }
140
141   if (self->decoder)
142     gst_nv_decoder_handle_set_context (self->decoder, element, context);
143
144 done:
145   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
146 }
147
148 static gboolean
149 gst_nv_vp9_dec_open (GstVideoDecoder * decoder)
150 {
151   GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
152   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
153   GstNvVp9DecClass *klass = GST_NV_VP9_DEC_GET_CLASS (self);
154
155   if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
156           klass->cuda_device_id, &self->context)) {
157     GST_ERROR_OBJECT (self, "Required element data is unavailable");
158     return FALSE;
159   }
160
161   self->decoder = gst_nv_decoder_new (self->context);
162   if (!self->decoder) {
163     GST_ERROR_OBJECT (self, "Failed to create decoder object");
164     gst_clear_object (&self->context);
165
166     return FALSE;
167   }
168
169   /* NVDEC doesn't support non-keyframe resolution change and it will result
170    * in outputting broken frames */
171   gst_vp9_decoder_set_non_keyframe_format_change_support (vp9dec, FALSE);
172
173   return TRUE;
174 }
175
176 static gboolean
177 gst_nv_vp9_dec_close (GstVideoDecoder * decoder)
178 {
179   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
180
181   g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
182   gst_clear_object (&self->decoder);
183   gst_clear_object (&self->context);
184
185   return TRUE;
186 }
187
188 static gboolean
189 gst_nv_vp9_dec_negotiate (GstVideoDecoder * decoder)
190 {
191   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
192   GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
193
194   GST_DEBUG_OBJECT (self, "negotiate");
195
196   gst_nv_decoder_negotiate (self->decoder, decoder, vp9dec->input_state,
197       &self->output_state);
198
199   /* TODO: add support D3D11 memory */
200
201   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
202 }
203
204 static gboolean
205 gst_nv_vp9_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
206 {
207   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
208
209   if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
210     GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
211     return FALSE;
212   }
213
214   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
215       (decoder, query);
216 }
217
218 static gboolean
219 gst_nv_vp9_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
220 {
221   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
222
223   switch (GST_QUERY_TYPE (query)) {
224     case GST_QUERY_CONTEXT:
225       if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
226               self->context)) {
227         return TRUE;
228       } else if (self->decoder &&
229           gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
230         return TRUE;
231       }
232       break;
233     default:
234       break;
235   }
236
237   return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
238 }
239
240 static GstFlowReturn
241 gst_nv_vp9_dec_new_sequence (GstVp9Decoder * decoder,
242     const GstVp9FrameHeader * frame_hdr, gint max_dpb_size)
243 {
244   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
245   GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
246   GstVideoInfo info;
247
248   GST_LOG_OBJECT (self, "new sequence");
249
250   self->width = frame_hdr->width;
251   self->height = frame_hdr->height;
252   self->profile = frame_hdr->profile;
253
254   if (self->profile == GST_VP9_PROFILE_0) {
255     out_format = GST_VIDEO_FORMAT_NV12;
256   } else if (self->profile == GST_VP9_PROFILE_2) {
257     if (frame_hdr->bit_depth == 10) {
258       out_format = GST_VIDEO_FORMAT_P010_10LE;
259     } else {
260       out_format = GST_VIDEO_FORMAT_P016_LE;
261     }
262   }
263
264   if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
265     GST_ERROR_OBJECT (self, "Could not support profile %d", self->profile);
266     return GST_FLOW_NOT_NEGOTIATED;
267   }
268
269   gst_video_info_set_format (&info, out_format, self->width, self->height);
270   if (!gst_nv_decoder_configure (self->decoder,
271           cudaVideoCodec_VP9, &info, self->width, self->height,
272           frame_hdr->bit_depth, max_dpb_size)) {
273     GST_ERROR_OBJECT (self, "Failed to configure decoder");
274     return GST_FLOW_NOT_NEGOTIATED;
275   }
276
277   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
278     GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
279     return GST_FLOW_NOT_NEGOTIATED;
280   }
281
282   memset (&self->params, 0, sizeof (CUVIDPICPARAMS));
283
284   self->params.CodecSpecific.vp9.colorSpace = frame_hdr->color_space;
285
286   return GST_FLOW_OK;
287 }
288
289 static GstFlowReturn
290 gst_nv_vp9_dec_new_picture (GstVp9Decoder * decoder,
291     GstVideoCodecFrame * frame, GstVp9Picture * picture)
292 {
293   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
294   GstNvDecoderFrame *nv_frame;
295
296   nv_frame = gst_nv_decoder_new_frame (self->decoder);
297   if (!nv_frame) {
298     GST_ERROR_OBJECT (self, "No available decoder frame");
299     return GST_FLOW_ERROR;
300   }
301
302   GST_LOG_OBJECT (self,
303       "New decoder frame %p (index %d)", nv_frame, nv_frame->index);
304
305   gst_vp9_picture_set_user_data (picture,
306       nv_frame, (GDestroyNotify) gst_nv_decoder_frame_unref);
307
308   return GST_FLOW_OK;
309 }
310
311 static GstNvDecoderFrame *
312 gst_nv_vp9_dec_get_decoder_frame_from_picture (GstNvVp9Dec * self,
313     GstVp9Picture * picture)
314 {
315   GstNvDecoderFrame *frame;
316
317   frame = (GstNvDecoderFrame *) gst_vp9_picture_get_user_data (picture);
318
319   if (!frame)
320     GST_DEBUG_OBJECT (self, "current picture does not have decoder frame");
321
322   return frame;
323 }
324
325 static GstVp9Picture *
326 gst_nv_vp9_dec_duplicate_picture (GstVp9Decoder * decoder,
327     GstVideoCodecFrame * frame, GstVp9Picture * picture)
328 {
329   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
330   GstNvDecoderFrame *nv_frame;
331   GstVp9Picture *new_picture;
332
333   nv_frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self, picture);
334
335   if (!nv_frame) {
336     GST_ERROR_OBJECT (self, "Parent picture does not have decoder frame");
337     return NULL;
338   }
339
340   new_picture = gst_vp9_picture_new ();
341   new_picture->frame_hdr = picture->frame_hdr;
342
343   gst_vp9_picture_set_user_data (new_picture,
344       gst_nv_decoder_frame_ref (nv_frame),
345       (GDestroyNotify) gst_nv_decoder_frame_unref);
346
347   return new_picture;
348 }
349
350 static GstFlowReturn
351 gst_nv_vp9_dec_decode_picture (GstVp9Decoder * decoder,
352     GstVp9Picture * picture, GstVp9Dpb * dpb)
353 {
354   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
355   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
356   const GstVp9LoopFilterParams *lfp = &frame_hdr->loop_filter_params;
357   const GstVp9SegmentationParams *sp = &frame_hdr->segmentation_params;
358   const GstVp9QuantizationParams *qp = &frame_hdr->quantization_params;
359   CUVIDPICPARAMS *params = &self->params;
360   CUVIDVP9PICPARAMS *vp9_params = &params->CodecSpecific.vp9;
361   GstNvDecoderFrame *frame;
362   GstNvDecoderFrame *other_frame;
363   guint offset = 0;
364   guint8 ref_frame_map[GST_VP9_REF_FRAMES];
365   gint i;
366
367   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mbRefLfDelta) ==
368       GST_VP9_MAX_REF_LF_DELTAS);
369   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mbModeLfDelta) ==
370       GST_VP9_MAX_MODE_LF_DELTAS);
371   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mb_segment_tree_probs) ==
372       GST_VP9_SEG_TREE_PROBS);
373   G_STATIC_ASSERT (sizeof (vp9_params->mb_segment_tree_probs) ==
374       sizeof (sp->segmentation_tree_probs));
375   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segment_pred_probs) ==
376       GST_VP9_PREDICTION_PROBS);
377   G_STATIC_ASSERT (sizeof (vp9_params->segment_pred_probs) ==
378       sizeof (sp->segmentation_pred_prob));
379   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->refFrameSignBias) ==
380       GST_VP9_REFS_PER_FRAME + 1);
381   G_STATIC_ASSERT (sizeof (vp9_params->refFrameSignBias) ==
382       sizeof (frame_hdr->ref_frame_sign_bias));
383   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->activeRefIdx) ==
384       GST_VP9_REFS_PER_FRAME);
385   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segmentFeatureEnable) ==
386       GST_VP9_MAX_SEGMENTS);
387   G_STATIC_ASSERT (sizeof (vp9_params->segmentFeatureEnable) ==
388       sizeof (sp->feature_enabled));
389   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segmentFeatureData) ==
390       GST_VP9_MAX_SEGMENTS);
391   G_STATIC_ASSERT (sizeof (vp9_params->segmentFeatureData) ==
392       sizeof (sp->feature_data));
393
394   GST_LOG_OBJECT (self, "Decode picture, size %" G_GSIZE_FORMAT, picture->size);
395
396   frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self, picture);
397   if (!frame) {
398     GST_ERROR_OBJECT (self, "Decoder frame is unavailable");
399     return GST_FLOW_ERROR;
400   }
401
402   params->nBitstreamDataLen = picture->size;
403   params->pBitstreamData = picture->data;
404   params->nNumSlices = 1;
405   params->pSliceDataOffsets = &offset;
406
407   params->PicWidthInMbs = GST_ROUND_UP_16 (frame_hdr->width) >> 4;
408   params->FrameHeightInMbs = GST_ROUND_UP_16 (frame_hdr->height) >> 4;
409   params->CurrPicIdx = frame->index;
410
411   vp9_params->width = frame_hdr->width;
412   vp9_params->height = frame_hdr->height;
413
414   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
415     if (dpb->pic_list[i]) {
416       other_frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self,
417           dpb->pic_list[i]);
418       if (!other_frame) {
419         GST_ERROR_OBJECT (self, "Couldn't get decoder frame from picture");
420         return FALSE;
421       }
422
423       ref_frame_map[i] = other_frame->index;
424     } else {
425       ref_frame_map[i] = 0xff;
426     }
427   }
428
429   vp9_params->LastRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[0]];
430   vp9_params->GoldenRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[1]];
431   vp9_params->AltRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[2]];
432
433   vp9_params->profile = frame_hdr->profile;
434   vp9_params->frameContextIdx = frame_hdr->frame_context_idx;
435   vp9_params->frameType = frame_hdr->frame_type;
436   vp9_params->showFrame = frame_hdr->show_frame;
437   vp9_params->errorResilient = frame_hdr->error_resilient_mode;
438   vp9_params->frameParallelDecoding = frame_hdr->frame_parallel_decoding_mode;
439   vp9_params->subSamplingX = frame_hdr->subsampling_x;
440   vp9_params->subSamplingY = frame_hdr->subsampling_y;
441   vp9_params->intraOnly = frame_hdr->intra_only;
442   vp9_params->allow_high_precision_mv = frame_hdr->allow_high_precision_mv;
443   vp9_params->refreshEntropyProbs = frame_hdr->refresh_frame_context;
444   vp9_params->bitDepthMinus8Luma = frame_hdr->bit_depth - 8;
445   vp9_params->bitDepthMinus8Chroma = frame_hdr->bit_depth - 8;
446
447   vp9_params->loopFilterLevel = lfp->loop_filter_level;
448   vp9_params->loopFilterSharpness = lfp->loop_filter_sharpness;
449   vp9_params->modeRefLfEnabled = lfp->loop_filter_delta_enabled;
450
451   vp9_params->log2_tile_columns = frame_hdr->tile_cols_log2;
452   vp9_params->log2_tile_rows = frame_hdr->tile_rows_log2;
453
454   vp9_params->segmentEnabled = sp->segmentation_enabled;
455   vp9_params->segmentMapUpdate = sp->segmentation_update_map;
456   vp9_params->segmentMapTemporalUpdate = sp->segmentation_temporal_update;
457   vp9_params->segmentFeatureMode = sp->segmentation_abs_or_delta_update;
458
459   vp9_params->qpYAc = qp->base_q_idx;
460   vp9_params->qpYDc = qp->delta_q_y_dc;
461   vp9_params->qpChDc = qp->delta_q_uv_dc;
462   vp9_params->qpChAc = qp->delta_q_uv_ac;
463
464   vp9_params->resetFrameContext = frame_hdr->reset_frame_context;
465   vp9_params->mcomp_filter_type = frame_hdr->interpolation_filter;
466   vp9_params->frameTagSize = frame_hdr->frame_header_length_in_bytes;
467   vp9_params->offsetToDctParts = frame_hdr->header_size_in_bytes;
468
469   for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++)
470     vp9_params->mbRefLfDelta[i] = lfp->loop_filter_ref_deltas[i];
471
472   for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++)
473     vp9_params->mbModeLfDelta[i] = lfp->loop_filter_mode_deltas[i];
474
475   memcpy (vp9_params->mb_segment_tree_probs, sp->segmentation_tree_probs,
476       sizeof (sp->segmentation_tree_probs));
477   memcpy (vp9_params->segment_pred_probs, sp->segmentation_pred_prob,
478       sizeof (sp->segmentation_pred_prob));
479   memcpy (vp9_params->refFrameSignBias, frame_hdr->ref_frame_sign_bias,
480       sizeof (frame_hdr->ref_frame_sign_bias));
481
482   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
483     vp9_params->activeRefIdx[i] = frame_hdr->ref_frame_idx[i];
484   }
485
486   memcpy (vp9_params->segmentFeatureEnable, sp->feature_enabled,
487       sizeof (sp->feature_enabled));
488   memcpy (vp9_params->segmentFeatureData, sp->feature_data,
489       sizeof (sp->feature_data));
490
491   if (!gst_nv_decoder_decode_picture (self->decoder, &self->params))
492     return GST_FLOW_ERROR;
493
494   return GST_FLOW_OK;
495 }
496
497 static GstFlowReturn
498 gst_nv_vp9_dec_output_picture (GstVp9Decoder * decoder,
499     GstVideoCodecFrame * frame, GstVp9Picture * picture)
500 {
501   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
502   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
503   GstNvDecoderFrame *decoder_frame;
504
505   GST_LOG_OBJECT (self, "Outputting picture %p", picture);
506
507   decoder_frame = (GstNvDecoderFrame *) gst_vp9_picture_get_user_data (picture);
508   if (!decoder_frame) {
509     GST_ERROR_OBJECT (self, "No decoder frame in picture %p", picture);
510     goto error;
511   }
512
513   if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
514           &frame->output_buffer)) {
515     GST_ERROR_OBJECT (self, "Failed to handle output picture");
516     goto error;
517   }
518
519   gst_vp9_picture_unref (picture);
520
521   return gst_video_decoder_finish_frame (vdec, frame);
522
523 error:
524   gst_video_decoder_drop_frame (vdec, frame);
525   gst_vp9_picture_unref (picture);
526
527   return GST_FLOW_ERROR;
528 }
529
530 static guint
531 gst_nv_vp9_dec_get_preferred_output_delay (GstVp9Decoder * decoder,
532     gboolean is_live)
533 {
534   /* Prefer to zero latency for live pipeline */
535   if (is_live)
536     return 0;
537
538   /* NVCODEC SDK uses 4 frame delay for better throughput performance */
539   return 4;
540 }
541
542 typedef struct
543 {
544   GstCaps *sink_caps;
545   GstCaps *src_caps;
546   guint cuda_device_id;
547   gboolean is_default;
548 } GstNvVp9DecClassData;
549
550 static void
551 gst_nv_vp9_dec_subclass_init (gpointer klass, GstNvVp9DecClassData * cdata)
552 {
553   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
554   GstNvVp9DecClass *nvdec_class = (GstNvVp9DecClass *) (klass);
555   gchar *long_name;
556
557   if (cdata->is_default) {
558     long_name = g_strdup_printf ("NVDEC VP9 Stateless Decoder");
559   } else {
560     long_name = g_strdup_printf ("NVDEC VP9 Stateless Decoder with device %d",
561         cdata->cuda_device_id);
562   }
563
564   gst_element_class_set_metadata (element_class, long_name,
565       "Codec/Decoder/Video/Hardware",
566       "NVIDIA VP9 video decoder", "Seungha Yang <seungha@centricular.com>");
567   g_free (long_name);
568
569   gst_element_class_add_pad_template (element_class,
570       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
571           cdata->sink_caps));
572   gst_element_class_add_pad_template (element_class,
573       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
574           cdata->src_caps));
575
576   nvdec_class->cuda_device_id = cdata->cuda_device_id;
577
578   gst_caps_unref (cdata->sink_caps);
579   gst_caps_unref (cdata->src_caps);
580   g_free (cdata);
581 }
582
583 void
584 gst_nv_vp9_dec_register (GstPlugin * plugin, guint device_id, guint rank,
585     GstCaps * sink_caps, GstCaps * src_caps, gboolean is_primary)
586 {
587   GTypeQuery type_query;
588   GTypeInfo type_info = { 0, };
589   GType subtype;
590   gchar *type_name;
591   gchar *feature_name;
592   GstNvVp9DecClassData *cdata;
593   gboolean is_default = TRUE;
594
595   /**
596    * element-nvvp9sldec:
597    *
598    * Since: 1.20
599    */
600
601   cdata = g_new0 (GstNvVp9DecClassData, 1);
602   cdata->sink_caps = gst_caps_copy (sink_caps);
603   gst_caps_set_simple (cdata->sink_caps,
604       "alignment", G_TYPE_STRING, "frame", NULL);
605   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
606       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
607   cdata->src_caps = gst_caps_ref (src_caps);
608   cdata->cuda_device_id = device_id;
609
610   g_type_query (GST_TYPE_NV_VP9_DEC, &type_query);
611   memset (&type_info, 0, sizeof (type_info));
612   type_info.class_size = type_query.class_size;
613   type_info.instance_size = type_query.instance_size;
614   type_info.class_init = (GClassInitFunc) gst_nv_vp9_dec_subclass_init;
615   type_info.class_data = cdata;
616
617   if (is_primary) {
618     type_name = g_strdup ("GstNvVP9StatelessPrimaryDec");
619     feature_name = g_strdup ("nvvp9dec");
620   } else {
621     type_name = g_strdup ("GstNvVP9StatelessDec");
622     feature_name = g_strdup ("nvvp9sldec");
623   }
624
625   if (g_type_from_name (type_name) != 0) {
626     g_free (type_name);
627     g_free (feature_name);
628     if (is_primary) {
629       type_name =
630           g_strdup_printf ("GstNvVP9StatelessPrimaryDevice%dDec", device_id);
631       feature_name = g_strdup_printf ("nvvp9device%ddec", device_id);
632     } else {
633       type_name = g_strdup_printf ("GstNvVP9StatelessDevice%dDec", device_id);
634       feature_name = g_strdup_printf ("nvvp9sldevice%ddec", device_id);
635     }
636
637     is_default = FALSE;
638   }
639
640   cdata->is_default = is_default;
641   subtype = g_type_register_static (GST_TYPE_NV_VP9_DEC,
642       type_name, &type_info, 0);
643
644   /* make lower rank than default device */
645   if (rank > 0 && !is_default)
646     rank--;
647
648   if (!gst_element_register (plugin, feature_name, rank, subtype))
649     GST_WARNING ("Failed to register plugin '%s'", type_name);
650
651   g_free (type_name);
652   g_free (feature_name);
653 }