• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
gst_nv_vp9_dec_class_init(GstNvVp9DecClass * klass)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
gst_nv_vp9_dec_init(GstNvVp9Dec * self)125 gst_nv_vp9_dec_init (GstNvVp9Dec * self)
126 {
127 }
128 
129 static void
gst_nv_vp9_dec_set_context(GstElement * element,GstContext * context)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
gst_nv_vp9_dec_open(GstVideoDecoder * decoder)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
gst_nv_vp9_dec_close(GstVideoDecoder * decoder)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
gst_nv_vp9_dec_negotiate(GstVideoDecoder * decoder)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
gst_nv_vp9_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)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
gst_nv_vp9_dec_src_query(GstVideoDecoder * decoder,GstQuery * query)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
gst_nv_vp9_dec_new_sequence(GstVp9Decoder * decoder,const GstVp9FrameHeader * frame_hdr)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 
266   if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
267     GST_ERROR_OBJECT (self, "Could not support profile %d", self->profile);
268     return GST_FLOW_NOT_NEGOTIATED;
269   }
270 
271   gst_video_info_set_format (&info, out_format, self->width, self->height);
272   if (!gst_nv_decoder_configure (self->decoder,
273           cudaVideoCodec_VP9, &info, self->width, self->height,
274           frame_hdr->bit_depth,
275           /* +4 for render delay */
276           NUM_OUTPUT_VIEW + 4)) {
277     GST_ERROR_OBJECT (self, "Failed to configure decoder");
278     return GST_FLOW_NOT_NEGOTIATED;
279   }
280 
281   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
282     GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
283     return GST_FLOW_NOT_NEGOTIATED;
284   }
285 
286   memset (&self->params, 0, sizeof (CUVIDPICPARAMS));
287 
288   self->params.CodecSpecific.vp9.colorSpace = frame_hdr->color_space;
289 
290   return GST_FLOW_OK;
291 }
292 
293 static GstFlowReturn
gst_nv_vp9_dec_new_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)294 gst_nv_vp9_dec_new_picture (GstVp9Decoder * decoder,
295     GstVideoCodecFrame * frame, GstVp9Picture * picture)
296 {
297   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
298   GstNvDecoderFrame *nv_frame;
299 
300   nv_frame = gst_nv_decoder_new_frame (self->decoder);
301   if (!nv_frame) {
302     GST_ERROR_OBJECT (self, "No available decoder frame");
303     return GST_FLOW_ERROR;
304   }
305 
306   GST_LOG_OBJECT (self,
307       "New decoder frame %p (index %d)", nv_frame, nv_frame->index);
308 
309   gst_vp9_picture_set_user_data (picture,
310       nv_frame, (GDestroyNotify) gst_nv_decoder_frame_unref);
311 
312   return GST_FLOW_OK;
313 }
314 
315 static GstNvDecoderFrame *
gst_nv_vp9_dec_get_decoder_frame_from_picture(GstNvVp9Dec * self,GstVp9Picture * picture)316 gst_nv_vp9_dec_get_decoder_frame_from_picture (GstNvVp9Dec * self,
317     GstVp9Picture * picture)
318 {
319   GstNvDecoderFrame *frame;
320 
321   frame = (GstNvDecoderFrame *) gst_vp9_picture_get_user_data (picture);
322 
323   if (!frame)
324     GST_DEBUG_OBJECT (self, "current picture does not have decoder frame");
325 
326   return frame;
327 }
328 
329 static GstVp9Picture *
gst_nv_vp9_dec_duplicate_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)330 gst_nv_vp9_dec_duplicate_picture (GstVp9Decoder * decoder,
331     GstVideoCodecFrame * frame, GstVp9Picture * picture)
332 {
333   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
334   GstNvDecoderFrame *nv_frame;
335   GstVp9Picture *new_picture;
336 
337   nv_frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self, picture);
338 
339   if (!nv_frame) {
340     GST_ERROR_OBJECT (self, "Parent picture does not have decoder frame");
341     return NULL;
342   }
343 
344   new_picture = gst_vp9_picture_new ();
345   new_picture->frame_hdr = picture->frame_hdr;
346 
347   gst_vp9_picture_set_user_data (new_picture,
348       gst_nv_decoder_frame_ref (nv_frame),
349       (GDestroyNotify) gst_nv_decoder_frame_unref);
350 
351   return new_picture;
352 }
353 
354 static GstFlowReturn
gst_nv_vp9_dec_decode_picture(GstVp9Decoder * decoder,GstVp9Picture * picture,GstVp9Dpb * dpb)355 gst_nv_vp9_dec_decode_picture (GstVp9Decoder * decoder,
356     GstVp9Picture * picture, GstVp9Dpb * dpb)
357 {
358   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
359   const GstVp9FrameHeader *frame_hdr = &picture->frame_hdr;
360   const GstVp9LoopFilterParams *lfp = &frame_hdr->loop_filter_params;
361   const GstVp9SegmentationParams *sp = &frame_hdr->segmentation_params;
362   const GstVp9QuantizationParams *qp = &frame_hdr->quantization_params;
363   CUVIDPICPARAMS *params = &self->params;
364   CUVIDVP9PICPARAMS *vp9_params = &params->CodecSpecific.vp9;
365   GstNvDecoderFrame *frame;
366   GstNvDecoderFrame *other_frame;
367   guint offset = 0;
368   guint8 ref_frame_map[GST_VP9_REF_FRAMES];
369   gint i;
370 
371   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mbRefLfDelta) ==
372       GST_VP9_MAX_REF_LF_DELTAS);
373   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mbModeLfDelta) ==
374       GST_VP9_MAX_MODE_LF_DELTAS);
375   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->mb_segment_tree_probs) ==
376       GST_VP9_SEG_TREE_PROBS);
377   G_STATIC_ASSERT (sizeof (vp9_params->mb_segment_tree_probs) ==
378       sizeof (sp->segmentation_tree_probs));
379   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segment_pred_probs) ==
380       GST_VP9_PREDICTION_PROBS);
381   G_STATIC_ASSERT (sizeof (vp9_params->segment_pred_probs) ==
382       sizeof (sp->segmentation_pred_prob));
383   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->refFrameSignBias) ==
384       GST_VP9_REFS_PER_FRAME + 1);
385   G_STATIC_ASSERT (sizeof (vp9_params->refFrameSignBias) ==
386       sizeof (frame_hdr->ref_frame_sign_bias));
387   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->activeRefIdx) ==
388       GST_VP9_REFS_PER_FRAME);
389   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segmentFeatureEnable) ==
390       GST_VP9_MAX_SEGMENTS);
391   G_STATIC_ASSERT (sizeof (vp9_params->segmentFeatureEnable) ==
392       sizeof (sp->feature_enabled));
393   G_STATIC_ASSERT (G_N_ELEMENTS (vp9_params->segmentFeatureData) ==
394       GST_VP9_MAX_SEGMENTS);
395   G_STATIC_ASSERT (sizeof (vp9_params->segmentFeatureData) ==
396       sizeof (sp->feature_data));
397 
398   GST_LOG_OBJECT (self, "Decode picture, size %" G_GSIZE_FORMAT, picture->size);
399 
400   frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self, picture);
401   if (!frame) {
402     GST_ERROR_OBJECT (self, "Decoder frame is unavailable");
403     return GST_FLOW_ERROR;
404   }
405 
406   params->nBitstreamDataLen = picture->size;
407   params->pBitstreamData = picture->data;
408   params->nNumSlices = 1;
409   params->pSliceDataOffsets = &offset;
410 
411   params->PicWidthInMbs = GST_ROUND_UP_16 (frame_hdr->width) >> 4;
412   params->FrameHeightInMbs = GST_ROUND_UP_16 (frame_hdr->height) >> 4;
413   params->CurrPicIdx = frame->index;
414 
415   vp9_params->width = frame_hdr->width;
416   vp9_params->height = frame_hdr->height;
417 
418   for (i = 0; i < GST_VP9_REF_FRAMES; i++) {
419     if (dpb->pic_list[i]) {
420       other_frame = gst_nv_vp9_dec_get_decoder_frame_from_picture (self,
421           dpb->pic_list[i]);
422       if (!other_frame) {
423         GST_ERROR_OBJECT (self, "Couldn't get decoder frame from picture");
424         return FALSE;
425       }
426 
427       ref_frame_map[i] = other_frame->index;
428     } else {
429       ref_frame_map[i] = 0xff;
430     }
431   }
432 
433   vp9_params->LastRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[0]];
434   vp9_params->GoldenRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[1]];
435   vp9_params->AltRefIdx = ref_frame_map[frame_hdr->ref_frame_idx[2]];
436 
437   vp9_params->profile = frame_hdr->profile;
438   vp9_params->frameContextIdx = frame_hdr->frame_context_idx;
439   vp9_params->frameType = frame_hdr->frame_type;
440   vp9_params->showFrame = frame_hdr->show_frame;
441   vp9_params->errorResilient = frame_hdr->error_resilient_mode;
442   vp9_params->frameParallelDecoding = frame_hdr->frame_parallel_decoding_mode;
443   vp9_params->subSamplingX = frame_hdr->subsampling_x;
444   vp9_params->subSamplingY = frame_hdr->subsampling_y;
445   vp9_params->intraOnly = frame_hdr->intra_only;
446   vp9_params->allow_high_precision_mv = frame_hdr->allow_high_precision_mv;
447   vp9_params->refreshEntropyProbs = frame_hdr->refresh_frame_context;
448   vp9_params->bitDepthMinus8Luma = frame_hdr->bit_depth - 8;
449   vp9_params->bitDepthMinus8Chroma = frame_hdr->bit_depth - 8;
450 
451   vp9_params->loopFilterLevel = lfp->loop_filter_level;
452   vp9_params->loopFilterSharpness = lfp->loop_filter_sharpness;
453   vp9_params->modeRefLfEnabled = lfp->loop_filter_delta_enabled;
454 
455   vp9_params->log2_tile_columns = frame_hdr->tile_cols_log2;
456   vp9_params->log2_tile_rows = frame_hdr->tile_rows_log2;
457 
458   vp9_params->segmentEnabled = sp->segmentation_enabled;
459   vp9_params->segmentMapUpdate = sp->segmentation_update_map;
460   vp9_params->segmentMapTemporalUpdate = sp->segmentation_temporal_update;
461   vp9_params->segmentFeatureMode = sp->segmentation_abs_or_delta_update;
462 
463   vp9_params->qpYAc = qp->base_q_idx;
464   vp9_params->qpYDc = qp->delta_q_y_dc;
465   vp9_params->qpChDc = qp->delta_q_uv_dc;
466   vp9_params->qpChAc = qp->delta_q_uv_ac;
467 
468   vp9_params->resetFrameContext = frame_hdr->reset_frame_context;
469   vp9_params->mcomp_filter_type = frame_hdr->interpolation_filter;
470   vp9_params->frameTagSize = frame_hdr->frame_header_length_in_bytes;
471   vp9_params->offsetToDctParts = frame_hdr->header_size_in_bytes;
472 
473   for (i = 0; i < GST_VP9_MAX_REF_LF_DELTAS; i++)
474     vp9_params->mbRefLfDelta[i] = lfp->loop_filter_ref_deltas[i];
475 
476   for (i = 0; i < GST_VP9_MAX_MODE_LF_DELTAS; i++)
477     vp9_params->mbModeLfDelta[i] = lfp->loop_filter_mode_deltas[i];
478 
479   memcpy (vp9_params->mb_segment_tree_probs, sp->segmentation_tree_probs,
480       sizeof (sp->segmentation_tree_probs));
481   memcpy (vp9_params->segment_pred_probs, sp->segmentation_pred_prob,
482       sizeof (sp->segmentation_pred_prob));
483   memcpy (vp9_params->refFrameSignBias, frame_hdr->ref_frame_sign_bias,
484       sizeof (frame_hdr->ref_frame_sign_bias));
485 
486   for (i = 0; i < GST_VP9_REFS_PER_FRAME; i++) {
487     vp9_params->activeRefIdx[i] = frame_hdr->ref_frame_idx[i];
488   }
489 
490   memcpy (vp9_params->segmentFeatureEnable, sp->feature_enabled,
491       sizeof (sp->feature_enabled));
492   memcpy (vp9_params->segmentFeatureData, sp->feature_data,
493       sizeof (sp->feature_data));
494 
495   if (!gst_nv_decoder_decode_picture (self->decoder, &self->params))
496     return GST_FLOW_ERROR;
497 
498   return GST_FLOW_OK;
499 }
500 
501 static GstFlowReturn
gst_nv_vp9_dec_output_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)502 gst_nv_vp9_dec_output_picture (GstVp9Decoder * decoder,
503     GstVideoCodecFrame * frame, GstVp9Picture * picture)
504 {
505   GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
506   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
507   GstNvDecoderFrame *decoder_frame;
508 
509   GST_LOG_OBJECT (self, "Outputting picture %p", picture);
510 
511   decoder_frame = (GstNvDecoderFrame *) gst_vp9_picture_get_user_data (picture);
512   if (!decoder_frame) {
513     GST_ERROR_OBJECT (self, "No decoder frame in picture %p", picture);
514     goto error;
515   }
516 
517   if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
518           &frame->output_buffer)) {
519     GST_ERROR_OBJECT (self, "Failed to handle output picture");
520     goto error;
521   }
522 
523   gst_vp9_picture_unref (picture);
524 
525   return gst_video_decoder_finish_frame (vdec, frame);
526 
527 error:
528   gst_video_decoder_drop_frame (vdec, frame);
529   gst_vp9_picture_unref (picture);
530 
531   return GST_FLOW_ERROR;
532 }
533 
534 static guint
gst_nv_vp9_dec_get_preferred_output_delay(GstVp9Decoder * decoder,gboolean is_live)535 gst_nv_vp9_dec_get_preferred_output_delay (GstVp9Decoder * decoder,
536     gboolean is_live)
537 {
538   /* Prefer to zero latency for live pipeline */
539   if (is_live)
540     return 0;
541 
542   /* NVCODEC SDK uses 4 frame delay for better throughput performance */
543   return 4;
544 }
545 
546 typedef struct
547 {
548   GstCaps *sink_caps;
549   GstCaps *src_caps;
550   guint cuda_device_id;
551   gboolean is_default;
552 } GstNvVp9DecClassData;
553 
554 static void
gst_nv_vp9_dec_subclass_init(gpointer klass,GstNvVp9DecClassData * cdata)555 gst_nv_vp9_dec_subclass_init (gpointer klass, GstNvVp9DecClassData * cdata)
556 {
557   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
558   GstNvVp9DecClass *nvdec_class = (GstNvVp9DecClass *) (klass);
559   gchar *long_name;
560 
561   if (cdata->is_default) {
562     long_name = g_strdup_printf ("NVDEC VP9 Stateless Decoder");
563   } else {
564     long_name = g_strdup_printf ("NVDEC VP9 Stateless Decoder with device %d",
565         cdata->cuda_device_id);
566   }
567 
568   gst_element_class_set_metadata (element_class, long_name,
569       "Codec/Decoder/Video/Hardware",
570       "NVIDIA VP9 video decoder", "Seungha Yang <seungha@centricular.com>");
571   g_free (long_name);
572 
573   gst_element_class_add_pad_template (element_class,
574       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
575           cdata->sink_caps));
576   gst_element_class_add_pad_template (element_class,
577       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
578           cdata->src_caps));
579 
580   nvdec_class->cuda_device_id = cdata->cuda_device_id;
581 
582   gst_caps_unref (cdata->sink_caps);
583   gst_caps_unref (cdata->src_caps);
584   g_free (cdata);
585 }
586 
587 void
gst_nv_vp9_dec_register(GstPlugin * plugin,guint device_id,guint rank,GstCaps * sink_caps,GstCaps * src_caps,gboolean is_primary)588 gst_nv_vp9_dec_register (GstPlugin * plugin, guint device_id, guint rank,
589     GstCaps * sink_caps, GstCaps * src_caps, gboolean is_primary)
590 {
591   GTypeQuery type_query;
592   GTypeInfo type_info = { 0, };
593   GType subtype;
594   gchar *type_name;
595   gchar *feature_name;
596   GstNvVp9DecClassData *cdata;
597   gboolean is_default = TRUE;
598 
599   /**
600    * element-nvvp9sldec:
601    *
602    * Since: 1.20
603    */
604 
605   cdata = g_new0 (GstNvVp9DecClassData, 1);
606   cdata->sink_caps = gst_caps_copy (sink_caps);
607   gst_caps_set_simple (cdata->sink_caps,
608       "alignment", G_TYPE_STRING, "frame", NULL);
609   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
610       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
611   cdata->src_caps = gst_caps_ref (src_caps);
612   cdata->cuda_device_id = device_id;
613 
614   g_type_query (GST_TYPE_NV_VP9_DEC, &type_query);
615   memset (&type_info, 0, sizeof (type_info));
616   type_info.class_size = type_query.class_size;
617   type_info.instance_size = type_query.instance_size;
618   type_info.class_init = (GClassInitFunc) gst_nv_vp9_dec_subclass_init;
619   type_info.class_data = cdata;
620 
621   if (is_primary) {
622     type_name = g_strdup ("GstNvVP9StatelessPrimaryDec");
623     feature_name = g_strdup ("nvvp9dec");
624   } else {
625     type_name = g_strdup ("GstNvVP9StatelessDec");
626     feature_name = g_strdup ("nvvp9sldec");
627   }
628 
629   if (g_type_from_name (type_name) != 0) {
630     g_free (type_name);
631     g_free (feature_name);
632     if (is_primary) {
633       type_name =
634           g_strdup_printf ("GstNvVP9StatelessPrimaryDevice%dDec", device_id);
635       feature_name = g_strdup_printf ("nvvp9device%ddec", device_id);
636     } else {
637       type_name = g_strdup_printf ("GstNvVP9StatelessDevice%dDec", device_id);
638       feature_name = g_strdup_printf ("nvvp9sldevice%ddec", device_id);
639     }
640 
641     is_default = FALSE;
642   }
643 
644   cdata->is_default = is_default;
645   subtype = g_type_register_static (GST_TYPE_NV_VP9_DEC,
646       type_name, &type_info, 0);
647 
648   /* make lower rank than default device */
649   if (rank > 0 && !is_default)
650     rank--;
651 
652   if (!gst_element_register (plugin, feature_name, rank, subtype))
653     GST_WARNING ("Failed to register plugin '%s'", type_name);
654 
655   g_free (type_name);
656   g_free (feature_name);
657 }
658