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