• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2017 Ericsson AB. All rights reserved.
3  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
4  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
42  * Boston, MA 02110-1301, USA.
43  *
44  * Copyright 2015 The Chromium Authors. All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions are
48  * met:
49  *
50  *    * Redistributions of source code must retain the above copyright
51  * notice, this list of conditions and the following disclaimer.
52  *    * Redistributions in binary form must reproduce the above
53  * copyright notice, this list of conditions and the following disclaimer
54  * in the documentation and/or other materials provided with the
55  * distribution.
56  *    * Neither the name of Google Inc. nor the names of its
57  * contributors may be used to endorse or promote products derived from
58  * this software without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
61  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
62  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
63  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
64  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
66  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
67  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
68  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71  */
72 
73 #ifdef HAVE_CONFIG_H
74 #include "config.h"
75 #endif
76 
77 #include "gstnvh265dec.h"
78 #include "gstcudautils.h"
79 #include "gstnvdecoder.h"
80 
81 #include <string.h>
82 
83 GST_DEBUG_CATEGORY_STATIC (gst_nv_h265_dec_debug);
84 #define GST_CAT_DEFAULT gst_nv_h265_dec_debug
85 
86 struct _GstNvH265Dec
87 {
88   GstH265Decoder parent;
89 
90   GstVideoCodecState *output_state;
91 
92   GstCudaContext *context;
93   GstNvDecoder *decoder;
94   CUVIDPICPARAMS params;
95 
96   /* slice buffer which will be passed to CUVIDPICPARAMS::pBitstreamData */
97   guint8 *bitstream_buffer;
98   /* allocated memory size of bitstream_buffer */
99   gsize bitstream_buffer_alloc_size;
100   /* current offset of bitstream_buffer (per frame) */
101   gsize bitstream_buffer_offset;
102 
103   guint *slice_offsets;
104   guint slice_offsets_alloc_len;
105   guint num_slices;
106 
107   guint width, height;
108   guint coded_width, coded_height;
109   guint bitdepth;
110   guint chroma_format_idc;
111 };
112 
113 struct _GstNvH265DecClass
114 {
115   GstH265DecoderClass parent_class;
116   guint cuda_device_id;
117 };
118 
119 #define gst_nv_h265_dec_parent_class parent_class
120 G_DEFINE_TYPE (GstNvH265Dec, gst_nv_h265_dec, GST_TYPE_H265_DECODER);
121 
122 static void gst_nv_h265_dec_set_context (GstElement * element,
123     GstContext * context);
124 static gboolean gst_nv_h265_dec_open (GstVideoDecoder * decoder);
125 static gboolean gst_nv_h265_dec_close (GstVideoDecoder * decoder);
126 static gboolean gst_nv_h265_dec_negotiate (GstVideoDecoder * decoder);
127 static gboolean gst_nv_h265_dec_decide_allocation (GstVideoDecoder *
128     decoder, GstQuery * query);
129 static gboolean gst_nv_h265_dec_src_query (GstVideoDecoder * decoder,
130     GstQuery * query);
131 
132 /* GstH265Decoder */
133 static GstFlowReturn gst_nv_h265_dec_new_sequence (GstH265Decoder * decoder,
134     const GstH265SPS * sps, gint max_dpb_size);
135 static GstFlowReturn gst_nv_h265_dec_new_picture (GstH265Decoder * decoder,
136     GstVideoCodecFrame * frame, GstH265Picture * picture);
137 static GstFlowReturn gst_nv_h265_dec_output_picture (GstH265Decoder *
138     decoder, GstVideoCodecFrame * frame, GstH265Picture * picture);
139 static GstFlowReturn gst_nv_h265_dec_start_picture (GstH265Decoder * decoder,
140     GstH265Picture * picture, GstH265Slice * slice, GstH265Dpb * dpb);
141 static GstFlowReturn gst_nv_h265_dec_decode_slice (GstH265Decoder * decoder,
142     GstH265Picture * picture, GstH265Slice * slice,
143     GArray * ref_pic_list0, GArray * ref_pic_list1);
144 static GstFlowReturn gst_nv_h265_dec_end_picture (GstH265Decoder * decoder,
145     GstH265Picture * picture);
146 
147 static void
gst_nv_h265_dec_class_init(GstNvH265DecClass * klass)148 gst_nv_h265_dec_class_init (GstNvH265DecClass * klass)
149 {
150   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
152   GstH265DecoderClass *h265decoder_class = GST_H265_DECODER_CLASS (klass);
153 
154   /**
155    * GstNvH265Dec
156    *
157    * Since: 1.18
158    */
159 
160   element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_set_context);
161 
162   decoder_class->open = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_open);
163   decoder_class->close = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_close);
164   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_negotiate);
165   decoder_class->decide_allocation =
166       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_decide_allocation);
167   decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_src_query);
168 
169   h265decoder_class->new_sequence =
170       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_new_sequence);
171   h265decoder_class->new_picture =
172       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_new_picture);
173   h265decoder_class->output_picture =
174       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_output_picture);
175   h265decoder_class->start_picture =
176       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_start_picture);
177   h265decoder_class->decode_slice =
178       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_decode_slice);
179   h265decoder_class->end_picture =
180       GST_DEBUG_FUNCPTR (gst_nv_h265_dec_end_picture);
181 
182   GST_DEBUG_CATEGORY_INIT (gst_nv_h265_dec_debug,
183       "nvh265dec", 0, "Nvidia H.265 Decoder");
184 
185   gst_type_mark_as_plugin_api (GST_TYPE_NV_H265_DEC, 0);
186 }
187 
188 static void
gst_nv_h265_dec_init(GstNvH265Dec * self)189 gst_nv_h265_dec_init (GstNvH265Dec * self)
190 {
191 }
192 
193 static void
gst_nv_h265_dec_set_context(GstElement * element,GstContext * context)194 gst_nv_h265_dec_set_context (GstElement * element, GstContext * context)
195 {
196   GstNvH265Dec *self = GST_NV_H265_DEC (element);
197   GstNvH265DecClass *klass = GST_NV_H265_DEC_GET_CLASS (self);
198 
199   GST_DEBUG_OBJECT (self, "set context %s",
200       gst_context_get_context_type (context));
201 
202   if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
203           &self->context)) {
204     goto done;
205   }
206 
207   if (self->decoder)
208     gst_nv_decoder_handle_set_context (self->decoder, element, context);
209 
210 done:
211   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
212 }
213 
214 static gboolean
gst_nv_h265_dec_open(GstVideoDecoder * decoder)215 gst_nv_h265_dec_open (GstVideoDecoder * decoder)
216 {
217   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
218   GstNvH265DecClass *klass = GST_NV_H265_DEC_GET_CLASS (self);
219 
220   if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
221           klass->cuda_device_id, &self->context)) {
222     GST_ERROR_OBJECT (self, "Required element data is unavailable");
223     return FALSE;
224   }
225 
226   self->decoder = gst_nv_decoder_new (self->context);
227   if (!self->decoder) {
228     GST_ERROR_OBJECT (self, "Failed to create decoder object");
229     gst_clear_object (&self->context);
230 
231     return FALSE;
232   }
233 
234   return TRUE;
235 }
236 
237 static gboolean
gst_nv_h265_dec_close(GstVideoDecoder * decoder)238 gst_nv_h265_dec_close (GstVideoDecoder * decoder)
239 {
240   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
241 
242   g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
243   gst_clear_object (&self->decoder);
244   gst_clear_object (&self->context);
245 
246   g_clear_pointer (&self->bitstream_buffer, g_free);
247   g_clear_pointer (&self->slice_offsets, g_free);
248 
249   self->bitstream_buffer_alloc_size = 0;
250   self->slice_offsets_alloc_len = 0;
251 
252   return TRUE;
253 }
254 
255 static gboolean
gst_nv_h265_dec_negotiate(GstVideoDecoder * decoder)256 gst_nv_h265_dec_negotiate (GstVideoDecoder * decoder)
257 {
258   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
259   GstH265Decoder *h265dec = GST_H265_DECODER (decoder);
260 
261   GST_DEBUG_OBJECT (self, "negotiate");
262 
263   gst_nv_decoder_negotiate (self->decoder, decoder, h265dec->input_state,
264       &self->output_state);
265 
266   /* TODO: add support D3D11 memory */
267 
268   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
269 }
270 
271 static gboolean
gst_nv_h265_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)272 gst_nv_h265_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
273 {
274   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
275 
276   if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
277     GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
278     return FALSE;
279   }
280 
281   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
282       (decoder, query);
283 }
284 
285 static gboolean
gst_nv_h265_dec_src_query(GstVideoDecoder * decoder,GstQuery * query)286 gst_nv_h265_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
287 {
288   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
289 
290   switch (GST_QUERY_TYPE (query)) {
291     case GST_QUERY_CONTEXT:
292       if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
293               self->context)) {
294         return TRUE;
295       } else if (self->decoder &&
296           gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
297         return TRUE;
298       }
299       break;
300     default:
301       break;
302   }
303 
304   return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
305 }
306 
307 static GstFlowReturn
gst_nv_h265_dec_new_sequence(GstH265Decoder * decoder,const GstH265SPS * sps,gint max_dpb_size)308 gst_nv_h265_dec_new_sequence (GstH265Decoder * decoder, const GstH265SPS * sps,
309     gint max_dpb_size)
310 {
311   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
312   gint crop_width, crop_height;
313   gboolean modified = FALSE;
314 
315   GST_LOG_OBJECT (self, "new sequence");
316 
317   if (sps->conformance_window_flag) {
318     crop_width = sps->crop_rect_width;
319     crop_height = sps->crop_rect_height;
320   } else {
321     crop_width = sps->width;
322     crop_height = sps->height;
323   }
324 
325   if (self->width != crop_width || self->height != crop_height ||
326       self->coded_width != sps->width || self->coded_height != sps->height) {
327     GST_INFO_OBJECT (self, "resolution changed %dx%d (%dx%d)",
328         crop_width, crop_height, sps->width, sps->height);
329     self->width = crop_width;
330     self->height = crop_height;
331     self->coded_width = sps->width;
332     self->coded_height = sps->height;
333     modified = TRUE;
334   }
335 
336   if (self->bitdepth != sps->bit_depth_luma_minus8 + 8) {
337     GST_INFO_OBJECT (self, "bitdepth changed");
338     self->bitdepth = sps->bit_depth_luma_minus8 + 8;
339     modified = TRUE;
340   }
341 
342   if (self->chroma_format_idc != sps->chroma_format_idc) {
343     GST_INFO_OBJECT (self, "chroma format changed");
344     self->chroma_format_idc = sps->chroma_format_idc;
345     modified = TRUE;
346   }
347 
348   if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
349     GstVideoInfo info;
350     GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
351 
352     if (self->bitdepth == 8) {
353       if (self->chroma_format_idc == 1) {
354         out_format = GST_VIDEO_FORMAT_NV12;
355       } else if (self->chroma_format_idc == 3) {
356         out_format = GST_VIDEO_FORMAT_Y444;
357       } else {
358         GST_FIXME_OBJECT (self, "8 bits supports only 4:2:0 or 4:4:4 format");
359       }
360     } else if (self->bitdepth == 10) {
361       if (self->chroma_format_idc == 1) {
362         out_format = GST_VIDEO_FORMAT_P010_10LE;
363       } else if (self->chroma_format_idc == 3) {
364         out_format = GST_VIDEO_FORMAT_Y444_16LE;
365       } else {
366         GST_FIXME_OBJECT (self, "10 bits supports only 4:2:0 or 4:4:4 format");
367       }
368     } else if (self->bitdepth == 12 || self->bitdepth == 16) {
369       if (self->chroma_format_idc == 1) {
370         out_format = GST_VIDEO_FORMAT_P016_LE;
371       } else if (self->chroma_format_idc == 3) {
372         out_format = GST_VIDEO_FORMAT_Y444_16LE;
373       } else {
374         GST_FIXME_OBJECT (self, "%d bits supports only 4:2:0 or 4:4:4 format",
375             self->bitdepth);
376       }
377     }
378 
379     if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
380       GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format");
381       return GST_FLOW_NOT_NEGOTIATED;
382     }
383 
384     gst_video_info_set_format (&info, out_format, self->width, self->height);
385 
386     if (!gst_nv_decoder_configure (self->decoder,
387             cudaVideoCodec_HEVC, &info, self->coded_width, self->coded_height,
388             self->bitdepth,
389             /* Additional 2 buffers for margin */
390             max_dpb_size + 2)) {
391       GST_ERROR_OBJECT (self, "Failed to configure decoder");
392       return GST_FLOW_NOT_NEGOTIATED;
393     }
394 
395     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
396       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
397       return GST_FLOW_NOT_NEGOTIATED;
398     }
399 
400     memset (&self->params, 0, sizeof (CUVIDPICPARAMS));
401   }
402 
403   return GST_FLOW_OK;
404 }
405 
406 static GstFlowReturn
gst_nv_h265_dec_new_picture(GstH265Decoder * decoder,GstVideoCodecFrame * cframe,GstH265Picture * picture)407 gst_nv_h265_dec_new_picture (GstH265Decoder * decoder,
408     GstVideoCodecFrame * cframe, GstH265Picture * picture)
409 {
410   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
411   GstNvDecoderFrame *frame;
412 
413   frame = gst_nv_decoder_new_frame (self->decoder);
414   if (!frame) {
415     GST_ERROR_OBJECT (self, "No available decoder frame");
416     return GST_FLOW_ERROR;
417   }
418 
419   GST_LOG_OBJECT (self, "New decoder frame %p (index %d)", frame, frame->index);
420 
421   gst_h265_picture_set_user_data (picture,
422       frame, (GDestroyNotify) gst_nv_decoder_frame_unref);
423 
424   return GST_FLOW_OK;
425 }
426 
427 static GstFlowReturn
gst_nv_h265_dec_output_picture(GstH265Decoder * decoder,GstVideoCodecFrame * frame,GstH265Picture * picture)428 gst_nv_h265_dec_output_picture (GstH265Decoder * decoder,
429     GstVideoCodecFrame * frame, GstH265Picture * picture)
430 {
431   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
432   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
433   GstNvDecoderFrame *decoder_frame;
434 
435   GST_LOG_OBJECT (self,
436       "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
437 
438   decoder_frame =
439       (GstNvDecoderFrame *) gst_h265_picture_get_user_data (picture);
440   if (!decoder_frame) {
441     GST_ERROR_OBJECT (self, "No decoder frame in picture %p", picture);
442     goto error;
443   }
444 
445   if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
446           &frame->output_buffer)) {
447     GST_ERROR_OBJECT (self, "Failed to handle output picture");
448     goto error;
449   }
450 
451   gst_h265_picture_unref (picture);
452 
453   return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
454 
455 error:
456   gst_video_decoder_drop_frame (vdec, frame);
457   gst_h265_picture_unref (picture);
458 
459   return GST_FLOW_ERROR;
460 }
461 
462 static GstNvDecoderFrame *
gst_nv_h265_dec_get_decoder_frame_from_picture(GstNvH265Dec * self,GstH265Picture * picture)463 gst_nv_h265_dec_get_decoder_frame_from_picture (GstNvH265Dec * self,
464     GstH265Picture * picture)
465 {
466   GstNvDecoderFrame *frame;
467 
468   frame = (GstNvDecoderFrame *) gst_h265_picture_get_user_data (picture);
469 
470   if (!frame)
471     GST_DEBUG_OBJECT (self, "current picture does not have decoder frame");
472 
473   return frame;
474 }
475 
476 static void
gst_nv_h265_dec_picture_params_from_sps(GstNvH265Dec * self,const GstH265SPS * sps,CUVIDHEVCPICPARAMS * params)477 gst_nv_h265_dec_picture_params_from_sps (GstNvH265Dec * self,
478     const GstH265SPS * sps, CUVIDHEVCPICPARAMS * params)
479 {
480 #define COPY_FIELD(f) \
481   (params)->f = (sps)->f
482 #define COPY_FIELD_WITH_PREFIX(f) \
483   (params)->G_PASTE(sps_,f) = (sps)->f
484 #define COPY_FIELD_EXTENSION(f) \
485   (params)->f = (sps)->sps_extnsion_params.f
486 
487   params->pic_width_in_luma_samples = sps->width;
488   params->pic_height_in_luma_samples = sps->height;
489   COPY_FIELD (log2_min_luma_coding_block_size_minus3);
490   COPY_FIELD (log2_diff_max_min_luma_coding_block_size);
491   COPY_FIELD (log2_min_transform_block_size_minus2);
492   COPY_FIELD (log2_diff_max_min_transform_block_size);
493   COPY_FIELD (pcm_enabled_flag);
494   COPY_FIELD (log2_min_pcm_luma_coding_block_size_minus3);
495   COPY_FIELD (log2_diff_max_min_pcm_luma_coding_block_size);
496   COPY_FIELD (pcm_sample_bit_depth_luma_minus1);
497   COPY_FIELD (pcm_sample_bit_depth_chroma_minus1);
498   COPY_FIELD (pcm_loop_filter_disabled_flag);
499   COPY_FIELD (strong_intra_smoothing_enabled_flag);
500   COPY_FIELD (max_transform_hierarchy_depth_intra);
501   COPY_FIELD (max_transform_hierarchy_depth_inter);
502   COPY_FIELD (max_transform_hierarchy_depth_inter);
503   COPY_FIELD (amp_enabled_flag);
504   COPY_FIELD (separate_colour_plane_flag);
505   COPY_FIELD (log2_max_pic_order_cnt_lsb_minus4);
506   COPY_FIELD (num_short_term_ref_pic_sets);
507   COPY_FIELD (long_term_ref_pics_present_flag);
508   COPY_FIELD (num_long_term_ref_pics_sps);
509   COPY_FIELD_WITH_PREFIX (temporal_mvp_enabled_flag);
510   COPY_FIELD (sample_adaptive_offset_enabled_flag);
511 
512   params->scaling_list_enable_flag = sps->scaling_list_enabled_flag;
513 
514   COPY_FIELD (bit_depth_luma_minus8);
515   COPY_FIELD (bit_depth_chroma_minus8);
516 
517   /* Extension fields */
518   COPY_FIELD (sps_range_extension_flag);
519   if (sps->sps_range_extension_flag) {
520     COPY_FIELD_EXTENSION (high_precision_offsets_enabled_flag);
521     COPY_FIELD_EXTENSION (transform_skip_rotation_enabled_flag);
522     COPY_FIELD_EXTENSION (implicit_rdpcm_enabled_flag);
523     COPY_FIELD_EXTENSION (explicit_rdpcm_enabled_flag);
524     COPY_FIELD_EXTENSION (extended_precision_processing_flag);
525     COPY_FIELD_EXTENSION (intra_smoothing_disabled_flag);
526     COPY_FIELD_EXTENSION (persistent_rice_adaptation_enabled_flag);
527     COPY_FIELD_EXTENSION (cabac_bypass_alignment_enabled_flag);
528   }
529 #undef COPY_FIELD
530 #undef COPY_FIELD_WITH_PREFIX
531 #undef COPY_FIELD_EXTENSION
532 }
533 
534 static gboolean
gst_nv_h265_dec_picture_params_from_pps(GstNvH265Dec * self,const GstH265PPS * pps,CUVIDHEVCPICPARAMS * params)535 gst_nv_h265_dec_picture_params_from_pps (GstNvH265Dec * self,
536     const GstH265PPS * pps, CUVIDHEVCPICPARAMS * params)
537 {
538   gint i;
539 
540 #define COPY_FIELD(f) \
541   (params)->f = (pps)->f
542 #define COPY_FIELD_WITH_PREFIX(f) \
543   (params)->G_PASTE(pps_,f) = (pps)->f
544 #define COPY_FIELD_EXTENSION(f) \
545   (params)->f = (pps)->pps_extension_params.f
546 
547   COPY_FIELD (dependent_slice_segments_enabled_flag);
548   COPY_FIELD (slice_segment_header_extension_present_flag);
549   COPY_FIELD (sign_data_hiding_enabled_flag);
550   COPY_FIELD (cu_qp_delta_enabled_flag);
551   COPY_FIELD (diff_cu_qp_delta_depth);
552   COPY_FIELD (init_qp_minus26);
553   COPY_FIELD_WITH_PREFIX (cb_qp_offset);
554   COPY_FIELD_WITH_PREFIX (cr_qp_offset);
555   COPY_FIELD (constrained_intra_pred_flag);
556   COPY_FIELD (weighted_pred_flag);
557   COPY_FIELD (weighted_bipred_flag);
558   COPY_FIELD (transform_skip_enabled_flag);
559   COPY_FIELD (transquant_bypass_enabled_flag);
560   COPY_FIELD (entropy_coding_sync_enabled_flag);
561   COPY_FIELD (log2_parallel_merge_level_minus2);
562   COPY_FIELD (num_extra_slice_header_bits);
563   COPY_FIELD (loop_filter_across_tiles_enabled_flag);
564   COPY_FIELD (loop_filter_across_slices_enabled_flag);
565   COPY_FIELD (output_flag_present_flag);
566   COPY_FIELD (num_ref_idx_l0_default_active_minus1);
567   COPY_FIELD (num_ref_idx_l1_default_active_minus1);
568   COPY_FIELD (lists_modification_present_flag);
569   COPY_FIELD (cabac_init_present_flag);
570   COPY_FIELD_WITH_PREFIX (slice_chroma_qp_offsets_present_flag);
571   COPY_FIELD (deblocking_filter_override_enabled_flag);
572   COPY_FIELD_WITH_PREFIX (deblocking_filter_disabled_flag);
573   COPY_FIELD_WITH_PREFIX (beta_offset_div2);
574   COPY_FIELD_WITH_PREFIX (tc_offset_div2);
575   COPY_FIELD (tiles_enabled_flag);
576   COPY_FIELD (uniform_spacing_flag);
577 
578   if (pps->tiles_enabled_flag) {
579     guint num_tile_columns;
580     guint num_tile_rows;
581 
582     COPY_FIELD (num_tile_columns_minus1);
583     COPY_FIELD (num_tile_rows_minus1);
584 
585     if (pps->num_tile_columns_minus1 >
586         G_N_ELEMENTS (params->column_width_minus1)) {
587       GST_ERROR_OBJECT (self,
588           "Too large column_width_minus1 %d", pps->num_tile_columns_minus1);
589       return FALSE;
590     }
591 
592     if (pps->num_tile_rows_minus1 > G_N_ELEMENTS (params->row_height_minus1)) {
593       GST_ERROR_OBJECT (self,
594           "Too large num_tile_rows_minus1 %d", pps->num_tile_rows_minus1);
595       return FALSE;
596     }
597 
598     /* XXX: The size of column_width_minus1 array in CUVIDHEVCPICPARAMS struct
599      * is 21 which is inconsistent with the spec.
600      * Just copy values as many as possible */
601     num_tile_columns = MIN (pps->num_tile_columns_minus1,
602         G_N_ELEMENTS (pps->column_width_minus1));
603     num_tile_rows = MIN (pps->num_tile_rows_minus1,
604         G_N_ELEMENTS (pps->row_height_minus1));
605 
606     for (i = 0; i < num_tile_columns; i++)
607       COPY_FIELD (column_width_minus1[i]);
608 
609     for (i = 0; i < num_tile_rows; i++)
610       COPY_FIELD (row_height_minus1[i]);
611   }
612 
613   COPY_FIELD (pps_range_extension_flag);
614   if (pps->pps_range_extension_flag) {
615     COPY_FIELD_EXTENSION (cross_component_prediction_enabled_flag);
616     COPY_FIELD_EXTENSION (chroma_qp_offset_list_enabled_flag);
617     COPY_FIELD_EXTENSION (diff_cu_chroma_qp_offset_depth);
618     COPY_FIELD_EXTENSION (chroma_qp_offset_list_len_minus1);
619     for (i = 0; i < G_N_ELEMENTS (params->cb_qp_offset_list); i++)
620       COPY_FIELD_EXTENSION (cb_qp_offset_list[i]);
621     for (i = 0; i < G_N_ELEMENTS (params->cr_qp_offset_list); i++)
622       COPY_FIELD_EXTENSION (cr_qp_offset_list[i]);
623     COPY_FIELD_EXTENSION (log2_sao_offset_scale_luma);
624     COPY_FIELD_EXTENSION (log2_sao_offset_scale_chroma);
625   }
626 #undef COPY_FIELD
627 #undef COPY_FIELD_WITH_PREFIX
628 #undef COPY_FIELD_EXTENSION
629 
630   return TRUE;
631 }
632 
633 static void
gst_nv_h265_dec_reset_bitstream_params(GstNvH265Dec * self)634 gst_nv_h265_dec_reset_bitstream_params (GstNvH265Dec * self)
635 {
636   self->bitstream_buffer_offset = 0;
637   self->num_slices = 0;
638 
639   self->params.nBitstreamDataLen = 0;
640   self->params.pBitstreamData = NULL;
641   self->params.nNumSlices = 0;
642   self->params.pSliceDataOffsets = NULL;
643 }
644 
645 static GstFlowReturn
gst_nv_h265_dec_start_picture(GstH265Decoder * decoder,GstH265Picture * picture,GstH265Slice * slice,GstH265Dpb * dpb)646 gst_nv_h265_dec_start_picture (GstH265Decoder * decoder,
647     GstH265Picture * picture, GstH265Slice * slice, GstH265Dpb * dpb)
648 {
649   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
650   CUVIDPICPARAMS *params = &self->params;
651   CUVIDHEVCPICPARAMS *h265_params = &params->CodecSpecific.hevc;
652   const GstH265SliceHdr *slice_header = &slice->header;
653   const GstH265SPS *sps;
654   const GstH265PPS *pps;
655   GstNvDecoderFrame *frame;
656   GArray *dpb_array;
657   gint num_ref_pic;
658   gint i, j;
659   const GstH265ScalingList *scaling_list = NULL;
660 
661   /* both NVDEC and h265parser are using the same order */
662   G_STATIC_ASSERT (sizeof (scaling_list->scaling_lists_4x4) ==
663       sizeof (h265_params->ScalingList4x4));
664   G_STATIC_ASSERT (sizeof (scaling_list->scaling_lists_8x8) ==
665       sizeof (h265_params->ScalingList8x8));
666   G_STATIC_ASSERT (sizeof (scaling_list->scaling_lists_16x16) ==
667       sizeof (h265_params->ScalingList16x16));
668   G_STATIC_ASSERT (sizeof (scaling_list->scaling_lists_32x32) ==
669       sizeof (h265_params->ScalingList32x32));
670 
671   g_return_val_if_fail (slice_header->pps != NULL, GST_FLOW_ERROR);
672   g_return_val_if_fail (slice_header->pps->sps != NULL, GST_FLOW_ERROR);
673 
674   frame = gst_nv_h265_dec_get_decoder_frame_from_picture (self, picture);
675 
676   if (!frame) {
677     GST_ERROR_OBJECT (self,
678         "Couldn't get decoder frame frame picture %p", picture);
679     return GST_FLOW_ERROR;
680   }
681 
682   gst_nv_h265_dec_reset_bitstream_params (self);
683 
684   pps = slice_header->pps;
685   sps = pps->sps;
686 
687   /* FIXME: update sps/pps related params only when it's required */
688   params->PicWidthInMbs = sps->pic_width_in_luma_samples / 16;
689   params->FrameHeightInMbs = sps->pic_height_in_luma_samples / 16;
690   params->CurrPicIdx = frame->index;
691 
692   /* nBitstreamDataLen, pBitstreamData, nNumSlices and pSliceDataOffsets
693    * will be set later */
694   params->ref_pic_flag = picture->ref;
695   params->intra_pic_flag = GST_H265_IS_NAL_TYPE_IRAP (slice->nalu.type);
696 
697   h265_params->IrapPicFlag = GST_H265_IS_NAL_TYPE_IRAP (slice->nalu.type);
698   h265_params->IdrPicFlag = GST_H265_IS_NAL_TYPE_IDR (slice->nalu.type);
699 
700   gst_nv_h265_dec_picture_params_from_sps (self, sps, h265_params);
701   if (!gst_nv_h265_dec_picture_params_from_pps (self, pps, h265_params)) {
702     GST_ERROR_OBJECT (self, "Couldn't copy pps");
703     return GST_FLOW_ERROR;
704   }
705 
706   /* Fill reference */
707   if (decoder->NumPocStCurrBefore >
708       G_N_ELEMENTS (h265_params->RefPicSetStCurrBefore)) {
709     GST_ERROR_OBJECT (self, "Too many RefPicSetStCurrBefore");
710     return GST_FLOW_ERROR;
711   }
712 
713   if (decoder->NumPocStCurrAfter >
714       G_N_ELEMENTS (h265_params->RefPicSetStCurrAfter)) {
715     GST_ERROR_OBJECT (self, "Too many RefPicSetStCurrAfter");
716     return GST_FLOW_ERROR;
717   }
718 
719   if (decoder->NumPocLtCurr > G_N_ELEMENTS (h265_params->RefPicSetLtCurr)) {
720     GST_ERROR_OBJECT (self, "Too many RefPicSetLtCurr");
721     return GST_FLOW_ERROR;
722   }
723 
724   /* Fill ref list */
725   h265_params->NumBitsForShortTermRPSInSlice =
726       slice_header->short_term_ref_pic_set_size;
727   h265_params->NumDeltaPocsOfRefRpsIdx =
728       slice_header->short_term_ref_pic_sets.NumDeltaPocsOfRefRpsIdx;
729   h265_params->NumPocTotalCurr = decoder->NumPicTotalCurr;
730   h265_params->NumPocStCurrBefore = decoder->NumPocStCurrBefore;
731   h265_params->NumPocStCurrAfter = decoder->NumPocStCurrAfter;
732   h265_params->NumPocLtCurr = decoder->NumPocLtCurr;
733   h265_params->CurrPicOrderCntVal = picture->pic_order_cnt;
734 
735   dpb_array = gst_h265_dpb_get_pictures_all (dpb);
736   /* count only referenced frame */
737   num_ref_pic = 0;
738   for (i = 0; i < dpb_array->len; i++) {
739     GstH265Picture *other = g_array_index (dpb_array, GstH265Picture *, i);
740     GstNvDecoderFrame *other_frame;
741     gint picture_index = -1;
742 
743     if (!other->ref)
744       continue;
745 
746     if (num_ref_pic >= G_N_ELEMENTS (h265_params->RefPicIdx)) {
747       GST_ERROR_OBJECT (self, "Too many reference frames");
748       return GST_FLOW_ERROR;
749     }
750 
751     other_frame = gst_nv_h265_dec_get_decoder_frame_from_picture (self, other);
752 
753     if (other_frame)
754       picture_index = other_frame->index;
755 
756     h265_params->RefPicIdx[num_ref_pic] = picture_index;
757     h265_params->PicOrderCntVal[num_ref_pic] = other->pic_order_cnt;
758     h265_params->IsLongTerm[num_ref_pic] = other->long_term;
759 
760     num_ref_pic++;
761   }
762 
763   g_array_unref (dpb_array);
764 
765   for (i = num_ref_pic; i < G_N_ELEMENTS (h265_params->RefPicIdx); i++)
766     h265_params->RefPicIdx[i] = -1;
767 
768   for (i = 0; i < decoder->NumPocStCurrBefore; i++) {
769     GstH265Picture *other;
770 
771     if (!decoder->RefPicSetStCurrBefore[i]) {
772       GST_ERROR_OBJECT (self, "Empty RefPicSetStCurrBefore[%d]", i);
773       return GST_FLOW_ERROR;
774     }
775 
776     other = decoder->RefPicSetStCurrBefore[i];
777 
778     for (j = 0; j < num_ref_pic; j++) {
779       if (h265_params->PicOrderCntVal[j] == other->pic_order_cnt) {
780         h265_params->RefPicSetStCurrBefore[i] = j;
781         break;
782       }
783     }
784   }
785 
786   for (i = 0; i < decoder->NumPocStCurrAfter; i++) {
787     GstH265Picture *other;
788 
789     if (!decoder->RefPicSetStCurrAfter[i]) {
790       GST_ERROR_OBJECT (self, "Empty RefPicSetStCurrAfter[%d]", i);
791       return GST_FLOW_ERROR;
792     }
793 
794     other = decoder->RefPicSetStCurrAfter[i];
795 
796     for (j = 0; j < num_ref_pic; j++) {
797       if (h265_params->PicOrderCntVal[j] == other->pic_order_cnt) {
798         h265_params->RefPicSetStCurrAfter[i] = j;
799         break;
800       }
801     }
802   }
803 
804   for (i = 0; i < decoder->NumPocLtCurr; i++) {
805     GstH265Picture *other;
806 
807     if (!decoder->RefPicSetLtCurr[i]) {
808       GST_ERROR_OBJECT (self, "Empty RefPicSetLtCurr[%d]", i);
809       return GST_FLOW_ERROR;
810     }
811 
812     other = decoder->RefPicSetLtCurr[i];
813 
814     for (j = 0; j < num_ref_pic; j++) {
815       if (h265_params->PicOrderCntVal[j] == other->pic_order_cnt) {
816         h265_params->RefPicSetLtCurr[i] = j;
817         break;
818       }
819     }
820   }
821 
822   /* Fill scaling list */
823   if (pps->scaling_list_data_present_flag ||
824       (sps->scaling_list_enabled_flag
825           && !sps->scaling_list_data_present_flag)) {
826     scaling_list = &pps->scaling_list;
827   } else {
828     scaling_list = &sps->scaling_list;
829   }
830 
831   memcpy (h265_params->ScalingList4x4, scaling_list->scaling_lists_4x4,
832       sizeof (scaling_list->scaling_lists_4x4));
833   memcpy (h265_params->ScalingList8x8, scaling_list->scaling_lists_8x8,
834       sizeof (scaling_list->scaling_lists_8x8));
835   memcpy (h265_params->ScalingList16x16, scaling_list->scaling_lists_16x16,
836       sizeof (scaling_list->scaling_lists_16x16));
837   memcpy (h265_params->ScalingList32x32, scaling_list->scaling_lists_32x32,
838       sizeof (scaling_list->scaling_lists_32x32));
839 
840   for (i = 0; i < G_N_ELEMENTS (h265_params->ScalingListDCCoeff16x16); i++) {
841     h265_params->ScalingListDCCoeff16x16[i] =
842         scaling_list->scaling_list_dc_coef_minus8_16x16[i] + 8;
843   }
844 
845   for (i = 0; i < G_N_ELEMENTS (h265_params->ScalingListDCCoeff32x32); i++) {
846     h265_params->ScalingListDCCoeff32x32[i] =
847         scaling_list->scaling_list_dc_coef_minus8_32x32[i] + 8;
848   }
849 
850   return GST_FLOW_OK;
851 }
852 
853 static GstFlowReturn
gst_nv_h265_dec_decode_slice(GstH265Decoder * decoder,GstH265Picture * picture,GstH265Slice * slice,GArray * ref_pic_list0,GArray * ref_pic_list1)854 gst_nv_h265_dec_decode_slice (GstH265Decoder * decoder,
855     GstH265Picture * picture, GstH265Slice * slice,
856     GArray * ref_pic_list0, GArray * ref_pic_list1)
857 {
858   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
859   gsize new_size;
860 
861   GST_LOG_OBJECT (self, "Decode slice, nalu size %u", slice->nalu.size);
862 
863   if (self->slice_offsets_alloc_len < self->num_slices + 1) {
864     self->slice_offsets_alloc_len = 2 * (self->num_slices + 1);
865 
866     self->slice_offsets = (guint *) g_realloc_n (self->slice_offsets,
867         self->slice_offsets_alloc_len, sizeof (guint));
868   }
869   self->slice_offsets[self->num_slices] = self->bitstream_buffer_offset;
870   GST_LOG_OBJECT (self, "Slice offset %u for slice %d",
871       self->slice_offsets[self->num_slices], self->num_slices);
872 
873   self->num_slices++;
874 
875   new_size = self->bitstream_buffer_offset + slice->nalu.size + 3;
876   if (self->bitstream_buffer_alloc_size < new_size) {
877     self->bitstream_buffer_alloc_size = 2 * new_size;
878 
879     self->bitstream_buffer = (guint8 *) g_realloc (self->bitstream_buffer,
880         self->bitstream_buffer_alloc_size);
881   }
882 
883   self->bitstream_buffer[self->bitstream_buffer_offset] = 0;
884   self->bitstream_buffer[self->bitstream_buffer_offset + 1] = 0;
885   self->bitstream_buffer[self->bitstream_buffer_offset + 2] = 1;
886 
887   memcpy (self->bitstream_buffer + self->bitstream_buffer_offset + 3,
888       slice->nalu.data + slice->nalu.offset, slice->nalu.size);
889   self->bitstream_buffer_offset = new_size;
890 
891   return GST_FLOW_OK;
892 }
893 
894 static GstFlowReturn
gst_nv_h265_dec_end_picture(GstH265Decoder * decoder,GstH265Picture * picture)895 gst_nv_h265_dec_end_picture (GstH265Decoder * decoder, GstH265Picture * picture)
896 {
897   GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
898   gboolean ret;
899   CUVIDPICPARAMS *params = &self->params;
900 
901   params->nBitstreamDataLen = self->bitstream_buffer_offset;
902   params->pBitstreamData = self->bitstream_buffer;
903   params->nNumSlices = self->num_slices;
904   params->pSliceDataOffsets = self->slice_offsets;
905 
906   GST_LOG_OBJECT (self, "End picture, bitstream len: %" G_GSIZE_FORMAT
907       ", num slices %d", self->bitstream_buffer_offset, self->num_slices);
908 
909   ret = gst_nv_decoder_decode_picture (self->decoder, &self->params);
910 
911   if (!ret) {
912     GST_ERROR_OBJECT (self, "Failed to decode picture");
913     return GST_FLOW_ERROR;
914   }
915 
916   return GST_FLOW_OK;
917 }
918 
919 typedef struct
920 {
921   GstCaps *sink_caps;
922   GstCaps *src_caps;
923   guint cuda_device_id;
924   gboolean is_default;
925 } GstNvH265DecClassData;
926 
927 static void
gst_nv_h265_dec_subclass_init(gpointer klass,GstNvH265DecClassData * cdata)928 gst_nv_h265_dec_subclass_init (gpointer klass, GstNvH265DecClassData * cdata)
929 {
930   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
931   GstNvH265DecClass *nvdec_class = (GstNvH265DecClass *) (klass);
932   gchar *long_name;
933 
934   if (cdata->is_default) {
935     long_name = g_strdup_printf ("NVDEC H.265 Stateless Decoder");
936   } else {
937     long_name = g_strdup_printf ("NVDEC H.265 Stateless Decoder with device %d",
938         cdata->cuda_device_id);
939   }
940 
941   gst_element_class_set_metadata (element_class, long_name,
942       "Codec/Decoder/Video/Hardware",
943       "Nvidia H.265 video decoder", "Seungha Yang <seungha@centricular.com>");
944   g_free (long_name);
945 
946   gst_element_class_add_pad_template (element_class,
947       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
948           cdata->sink_caps));
949   gst_element_class_add_pad_template (element_class,
950       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
951           cdata->src_caps));
952 
953   nvdec_class->cuda_device_id = cdata->cuda_device_id;
954 
955   gst_caps_unref (cdata->sink_caps);
956   gst_caps_unref (cdata->src_caps);
957   g_free (cdata);
958 }
959 
960 void
gst_nv_h265_dec_register(GstPlugin * plugin,guint device_id,guint rank,GstCaps * sink_caps,GstCaps * src_caps,gboolean is_primary)961 gst_nv_h265_dec_register (GstPlugin * plugin, guint device_id, guint rank,
962     GstCaps * sink_caps, GstCaps * src_caps, gboolean is_primary)
963 {
964   GTypeQuery type_query;
965   GTypeInfo type_info = { 0, };
966   GType subtype;
967   gchar *type_name;
968   gchar *feature_name;
969   GstNvH265DecClassData *cdata;
970   gboolean is_default = TRUE;
971   GValue value_list = G_VALUE_INIT;
972   GValue value = G_VALUE_INIT;
973 
974   /**
975    * element-nvh265sldec
976    *
977    * Since: 1.18
978    */
979 
980   cdata = g_new0 (GstNvH265DecClassData, 1);
981   cdata->sink_caps = gst_caps_copy (sink_caps);
982 
983   /* Update stream-format since we support packetized format as well */
984   g_value_init (&value_list, GST_TYPE_LIST);
985   g_value_init (&value, G_TYPE_STRING);
986 
987   g_value_set_static_string (&value, "hev1");
988   gst_value_list_append_value (&value_list, &value);
989 
990   g_value_set_static_string (&value, "hvc1");
991   gst_value_list_append_value (&value_list, &value);
992 
993   g_value_set_static_string (&value, "byte-stream");
994   gst_value_list_append_value (&value_list, &value);
995 
996   gst_caps_set_value (cdata->sink_caps, "stream-format", &value_list);
997   g_value_unset (&value);
998   g_value_unset (&value_list);
999 
1000   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
1001       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1002   cdata->src_caps = gst_caps_ref (src_caps);
1003   cdata->cuda_device_id = device_id;
1004 
1005   g_type_query (GST_TYPE_NV_H265_DEC, &type_query);
1006   memset (&type_info, 0, sizeof (type_info));
1007   type_info.class_size = type_query.class_size;
1008   type_info.instance_size = type_query.instance_size;
1009   type_info.class_init = (GClassInitFunc) gst_nv_h265_dec_subclass_init;
1010   type_info.class_data = cdata;
1011 
1012   if (is_primary) {
1013     type_name = g_strdup ("GstNvH265StatelessPrimaryDec");
1014     feature_name = g_strdup ("nvh265dec");
1015   } else {
1016     type_name = g_strdup ("GstNvH265StatelessDec");
1017     feature_name = g_strdup ("nvh265sldec");
1018   }
1019 
1020   if (g_type_from_name (type_name) != 0) {
1021     g_free (type_name);
1022     g_free (feature_name);
1023     if (is_primary) {
1024       type_name =
1025           g_strdup_printf ("GstNvH265StatelessPrimaryDevice%dDec", device_id);
1026       feature_name = g_strdup_printf ("nvh265device%ddec", device_id);
1027     } else {
1028       type_name = g_strdup_printf ("GstNvH265StatelessDevice%dDec", device_id);
1029       feature_name = g_strdup_printf ("nvh265sldevice%ddec", device_id);
1030     }
1031 
1032     is_default = FALSE;
1033   }
1034 
1035   cdata->is_default = is_default;
1036   subtype = g_type_register_static (GST_TYPE_NV_H265_DEC,
1037       type_name, &type_info, 0);
1038 
1039   /* make lower rank than default device */
1040   if (rank > 0 && !is_default)
1041     rank--;
1042 
1043   if (!gst_element_register (plugin, feature_name, rank, subtype))
1044     GST_WARNING ("Failed to register plugin '%s'", type_name);
1045 
1046   g_free (type_name);
1047   g_free (feature_name);
1048 }
1049