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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
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.
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.
44 * Copyright 2015 The Chromium Authors. All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions are
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
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.
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.
74 * SECTION:element-nvh264sldec
77 * GstCodecs based NVIDIA H.264 video decoder
79 * ## Example launch line
81 * gst-launch-1.0 filesrc location=/path/to/h264/file ! parsebin ! nvh264sldec ! videoconvert ! autovideosink
92 #include <gst/cuda/gstcudautils.h>
94 #include "gstnvh264dec.h"
95 #include "gstnvdecoder.h"
99 GST_DEBUG_CATEGORY_STATIC (gst_nv_h264_dec_debug);
100 #define GST_CAT_DEFAULT gst_nv_h264_dec_debug
102 typedef struct _GstNvH264Dec
104 GstH264Decoder parent;
106 GstVideoCodecState *output_state;
108 GstCudaContext *context;
109 GstNvDecoder *decoder;
110 CUVIDPICPARAMS params;
112 /* slice buffer which will be passed to CUVIDPICPARAMS::pBitstreamData */
113 guint8 *bitstream_buffer;
114 /* allocated memory size of bitstream_buffer */
115 gsize bitstream_buffer_alloc_size;
116 /* current offset of bitstream_buffer (per frame) */
117 gsize bitstream_buffer_offset;
119 guint *slice_offsets;
120 guint slice_offsets_alloc_len;
124 guint coded_width, coded_height;
126 guint chroma_format_idc;
134 typedef struct _GstNvH264DecClass
136 GstH264DecoderClass parent_class;
137 guint cuda_device_id;
146 static GTypeClass *parent_class = NULL;
148 #define GST_NV_H264_DEC(object) ((GstNvH264Dec *) (object))
149 #define GST_NV_H264_DEC_GET_CLASS(object) \
150 (G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object),GstNvH264DecClass))
152 static void gst_nv_h264_decoder_dispose (GObject * object);
153 static void gst_nv_h264_dec_get_property (GObject * object, guint prop_id,
154 GValue * value, GParamSpec * pspec);
156 static void gst_nv_h264_dec_set_context (GstElement * element,
157 GstContext * context);
158 static gboolean gst_nv_h264_dec_open (GstVideoDecoder * decoder);
159 static gboolean gst_nv_h264_dec_close (GstVideoDecoder * decoder);
160 static gboolean gst_nv_h264_dec_negotiate (GstVideoDecoder * decoder);
161 static gboolean gst_nv_h264_dec_decide_allocation (GstVideoDecoder *
162 decoder, GstQuery * query);
163 static gboolean gst_nv_h264_dec_src_query (GstVideoDecoder * decoder,
167 static GstFlowReturn gst_nv_h264_dec_new_sequence (GstH264Decoder * decoder,
168 const GstH264SPS * sps, gint max_dpb_size);
169 static GstFlowReturn gst_nv_h264_dec_new_picture (GstH264Decoder * decoder,
170 GstVideoCodecFrame * frame, GstH264Picture * picture);
172 gst_nv_h264_dec_new_field_picture (GstH264Decoder * decoder,
173 GstH264Picture * first_field, GstH264Picture * second_field);
174 static GstFlowReturn gst_nv_h264_dec_output_picture (GstH264Decoder *
175 decoder, GstVideoCodecFrame * frame, GstH264Picture * picture);
176 static GstFlowReturn gst_nv_h264_dec_start_picture (GstH264Decoder * decoder,
177 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb);
178 static GstFlowReturn gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
179 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
180 GArray * ref_pic_list1);
181 static GstFlowReturn gst_nv_h264_dec_end_picture (GstH264Decoder * decoder,
182 GstH264Picture * picture);
184 gst_nv_h264_dec_get_preferred_output_delay (GstH264Decoder * decoder,
188 gst_nv_h264_dec_class_init (GstNvH264DecClass * klass,
189 GstNvDecoderClassData * cdata)
191 GObjectClass *object_class = G_OBJECT_CLASS (klass);
192 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
193 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
194 GstH264DecoderClass *h264decoder_class = GST_H264_DECODER_CLASS (klass);
196 object_class->dispose = gst_nv_h264_decoder_dispose;
197 object_class->get_property = gst_nv_h264_dec_get_property;
200 * GstNvH264SLDec:cuda-device-id:
202 * Assigned CUDA device id
206 g_object_class_install_property (object_class, PROP_CUDA_DEVICE_ID,
207 g_param_spec_uint ("cuda-device-id", "CUDA device id",
208 "Assigned CUDA device id", 0, G_MAXINT, 0,
209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
211 element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_set_context);
213 parent_class = (GTypeClass *) g_type_class_peek_parent (klass);
214 gst_element_class_set_static_metadata (element_class,
215 "NVDEC H.264 Stateless Decoder",
216 "Codec/Decoder/Video/Hardware",
217 "NVIDIA H.264 video decoder", "Seungha Yang <seungha@centricular.com>");
219 gst_element_class_add_pad_template (element_class,
220 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
222 gst_element_class_add_pad_template (element_class,
223 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
226 decoder_class->open = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_open);
227 decoder_class->close = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_close);
228 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_negotiate);
229 decoder_class->decide_allocation =
230 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_decide_allocation);
231 decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_src_query);
233 h264decoder_class->new_sequence =
234 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_sequence);
235 h264decoder_class->new_picture =
236 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_picture);
237 h264decoder_class->new_field_picture =
238 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_field_picture);
239 h264decoder_class->output_picture =
240 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_output_picture);
241 h264decoder_class->start_picture =
242 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_start_picture);
243 h264decoder_class->decode_slice =
244 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_decode_slice);
245 h264decoder_class->end_picture =
246 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_end_picture);
247 h264decoder_class->get_preferred_output_delay =
248 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_get_preferred_output_delay);
250 klass->cuda_device_id = cdata->cuda_device_id;
252 gst_caps_unref (cdata->sink_caps);
253 gst_caps_unref (cdata->src_caps);
258 gst_nv_h264_dec_init (GstNvH264Dec * self)
260 self->ref_list = g_array_sized_new (FALSE, TRUE,
261 sizeof (GstH264Picture *), 16);
262 g_array_set_clear_func (self->ref_list,
263 (GDestroyNotify) gst_clear_h264_picture);
267 gst_nv_h264_decoder_dispose (GObject * object)
269 GstNvH264Dec *self = GST_NV_H264_DEC (object);
271 g_clear_pointer (&self->ref_list, g_array_unref);
273 G_OBJECT_CLASS (parent_class)->dispose (object);
277 gst_nv_h264_dec_get_property (GObject * object, guint prop_id, GValue * value,
280 GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (object);
283 case PROP_CUDA_DEVICE_ID:
284 g_value_set_uint (value, klass->cuda_device_id);
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293 gst_nv_h264_dec_set_context (GstElement * element, GstContext * context)
295 GstNvH264Dec *self = GST_NV_H264_DEC (element);
296 GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self);
298 GST_DEBUG_OBJECT (self, "set context %s",
299 gst_context_get_context_type (context));
301 if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
307 gst_nv_decoder_handle_set_context (self->decoder, element, context);
310 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
313 /* Clear all codec specific (e.g., SPS) data */
315 gst_d3d11_h264_dec_reset (GstNvH264Dec * self)
319 self->coded_width = 0;
320 self->coded_height = 0;
322 self->chroma_format_idc = 0;
323 self->max_dpb_size = 0;
324 self->interlaced = FALSE;
328 gst_nv_h264_dec_open (GstVideoDecoder * decoder)
330 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
331 GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self);
333 if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
334 klass->cuda_device_id, &self->context)) {
335 GST_ERROR_OBJECT (self, "Required element data is unavailable");
339 self->decoder = gst_nv_decoder_new (self->context);
340 if (!self->decoder) {
341 GST_ERROR_OBJECT (self, "Failed to create decoder object");
342 gst_clear_object (&self->context);
347 gst_d3d11_h264_dec_reset (self);
353 gst_nv_h264_dec_close (GstVideoDecoder * decoder)
355 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
357 g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
358 gst_clear_object (&self->decoder);
359 gst_clear_object (&self->context);
361 g_clear_pointer (&self->bitstream_buffer, g_free);
362 g_clear_pointer (&self->slice_offsets, g_free);
364 self->bitstream_buffer_alloc_size = 0;
365 self->slice_offsets_alloc_len = 0;
371 gst_nv_h264_dec_negotiate (GstVideoDecoder * decoder)
373 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
374 GstH264Decoder *h264dec = GST_H264_DECODER (decoder);
376 GST_DEBUG_OBJECT (self, "negotiate");
378 gst_nv_decoder_negotiate (self->decoder, decoder, h264dec->input_state,
379 &self->output_state);
381 /* TODO: add support D3D11 memory */
383 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
387 gst_nv_h264_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
389 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
391 if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
392 GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
396 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
401 gst_nv_h264_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
403 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
405 switch (GST_QUERY_TYPE (query)) {
406 case GST_QUERY_CONTEXT:
407 if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
410 } else if (self->decoder &&
411 gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
419 return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
423 gst_nv_h264_dec_new_sequence (GstH264Decoder * decoder, const GstH264SPS * sps,
426 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
427 gint crop_width, crop_height;
428 gboolean modified = FALSE;
431 GST_LOG_OBJECT (self, "new sequence");
433 if (sps->frame_cropping_flag) {
434 crop_width = sps->crop_rect_width;
435 crop_height = sps->crop_rect_height;
437 crop_width = sps->width;
438 crop_height = sps->height;
441 if (self->width != crop_width || self->height != crop_height ||
442 self->coded_width != sps->width || self->coded_height != sps->height) {
443 GST_INFO_OBJECT (self, "resolution changed %dx%d (%dx%d)",
444 crop_width, crop_height, sps->width, sps->height);
445 self->width = crop_width;
446 self->height = crop_height;
447 self->coded_width = sps->width;
448 self->coded_height = sps->height;
452 if (self->bitdepth != sps->bit_depth_luma_minus8 + 8) {
453 GST_INFO_OBJECT (self, "bitdepth changed");
454 self->bitdepth = sps->bit_depth_luma_minus8 + 8;
458 if (self->chroma_format_idc != sps->chroma_format_idc) {
459 GST_INFO_OBJECT (self, "chroma format changed");
460 self->chroma_format_idc = sps->chroma_format_idc;
464 interlaced = !sps->frame_mbs_only_flag;
465 if (self->interlaced != interlaced) {
466 GST_INFO_OBJECT (self, "interlaced sequence changed");
467 self->interlaced = interlaced;
471 if (self->max_dpb_size < max_dpb_size) {
472 GST_INFO_OBJECT (self, "Requires larger DPB size (%d -> %d)",
473 self->max_dpb_size, max_dpb_size);
477 if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
479 GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
481 if (self->bitdepth == 8) {
482 if (self->chroma_format_idc == 1)
483 out_format = GST_VIDEO_FORMAT_NV12;
485 GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format");
487 } else if (self->bitdepth == 10) {
488 if (self->chroma_format_idc == 1)
489 out_format = GST_VIDEO_FORMAT_P010_10LE;
491 GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format");
495 if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
496 GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format");
497 return GST_FLOW_NOT_NEGOTIATED;
500 gst_video_info_set_format (&info, out_format, self->width, self->height);
501 if (self->interlaced)
502 GST_VIDEO_INFO_INTERLACE_MODE (&info) = GST_VIDEO_INTERLACE_MODE_MIXED;
504 self->max_dpb_size = max_dpb_size;
505 /* FIXME: add support cudaVideoCodec_H264_SVC and cudaVideoCodec_H264_MVC */
506 if (!gst_nv_decoder_configure (self->decoder,
507 cudaVideoCodec_H264, &info, self->coded_width, self->coded_height,
508 self->bitdepth, max_dpb_size, FALSE)) {
509 GST_ERROR_OBJECT (self, "Failed to configure decoder");
510 return GST_FLOW_NOT_NEGOTIATED;
513 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
514 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
515 return GST_FLOW_NOT_NEGOTIATED;
518 memset (&self->params, 0, sizeof (CUVIDPICPARAMS));
525 gst_nv_h264_dec_new_picture (GstH264Decoder * decoder,
526 GstVideoCodecFrame * frame, GstH264Picture * picture)
528 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
529 GstNvDecoderFrame *nv_frame;
531 nv_frame = gst_nv_decoder_new_frame (self->decoder);
533 GST_ERROR_OBJECT (self, "No available decoder frame");
534 return GST_FLOW_ERROR;
537 GST_LOG_OBJECT (self,
538 "New decoder frame %p (index %d)", nv_frame, nv_frame->index);
540 gst_h264_picture_set_user_data (picture,
541 nv_frame, (GDestroyNotify) gst_nv_decoder_frame_unref);
547 gst_nv_h264_dec_new_field_picture (GstH264Decoder * decoder,
548 GstH264Picture * first_field, GstH264Picture * second_field)
550 GstNvDecoderFrame *nv_frame;
552 nv_frame = (GstNvDecoderFrame *)
553 gst_h264_picture_get_user_data (first_field);
555 GST_ERROR_OBJECT (decoder,
556 "No decoder frame in the first picture %p", first_field);
557 return GST_FLOW_ERROR;
560 gst_h264_picture_set_user_data (second_field,
561 gst_nv_decoder_frame_ref (nv_frame),
562 (GDestroyNotify) gst_nv_decoder_frame_unref);
568 gst_nv_h264_dec_output_picture (GstH264Decoder * decoder,
569 GstVideoCodecFrame * frame, GstH264Picture * picture)
571 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
572 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
573 GstNvDecoderFrame *decoder_frame;
575 GST_LOG_OBJECT (self,
576 "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
579 (GstNvDecoderFrame *) gst_h264_picture_get_user_data (picture);
580 if (!decoder_frame) {
581 GST_ERROR_OBJECT (self, "No decoder frame in picture %p", picture);
585 if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
586 &frame->output_buffer)) {
587 GST_ERROR_OBJECT (self, "Failed to handle output picture");
591 if (picture->buffer_flags != 0) {
592 gboolean interlaced =
593 (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_INTERLACED) != 0;
594 gboolean tff = (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_TFF) != 0;
596 GST_TRACE_OBJECT (self,
597 "apply buffer flags 0x%x (interlaced %d, top-field-first %d)",
598 picture->buffer_flags, interlaced, tff);
599 GST_BUFFER_FLAG_SET (frame->output_buffer, picture->buffer_flags);
602 gst_h264_picture_unref (picture);
604 return gst_video_decoder_finish_frame (vdec, frame);
607 gst_h264_picture_unref (picture);
608 gst_video_decoder_release_frame (vdec, frame);
610 return GST_FLOW_ERROR;
613 static GstNvDecoderFrame *
614 gst_nv_h264_dec_get_decoder_frame_from_picture (GstNvH264Dec * self,
615 GstH264Picture * picture)
617 GstNvDecoderFrame *frame;
619 frame = (GstNvDecoderFrame *) gst_h264_picture_get_user_data (picture);
622 GST_DEBUG_OBJECT (self, "current picture does not have decoder frame");
628 gst_nv_h264_dec_fill_scaling_list_4x4 (const GstH264PPS * pps,
629 CUVIDH264PICPARAMS * params)
633 for (i = 0; i < G_N_ELEMENTS (params->WeightScale4x4); i++)
634 gst_h264_quant_matrix_4x4_get_raster_from_zigzag (params->WeightScale4x4[i],
635 pps->scaling_lists_4x4[i]);
639 gst_nv_h264_dec_fill_scaling_list_8x8 (const GstH264PPS * pps,
640 CUVIDH264PICPARAMS * params)
644 for (i = 0; i < G_N_ELEMENTS (params->WeightScale8x8); i++) {
645 gst_h264_quant_matrix_8x8_get_raster_from_zigzag (params->WeightScale8x8[i],
646 pps->scaling_lists_8x8[i]);
651 gst_nv_h264_dec_picture_params_from_sps (GstNvH264Dec * self,
652 const GstH264SPS * sps, gboolean field_pic, CUVIDH264PICPARAMS * params)
654 params->residual_colour_transform_flag = sps->separate_colour_plane_flag;
655 params->MbaffFrameFlag = sps->mb_adaptive_frame_field_flag && !field_pic;
657 #define COPY_FIELD(f) \
658 (params)->f = (sps)->f
660 COPY_FIELD (log2_max_frame_num_minus4);
661 COPY_FIELD (pic_order_cnt_type);
662 COPY_FIELD (log2_max_pic_order_cnt_lsb_minus4);
663 COPY_FIELD (delta_pic_order_always_zero_flag);
664 COPY_FIELD (frame_mbs_only_flag);
665 COPY_FIELD (direct_8x8_inference_flag);
666 COPY_FIELD (num_ref_frames);
667 COPY_FIELD (bit_depth_luma_minus8);
668 COPY_FIELD (bit_depth_chroma_minus8);
669 COPY_FIELD (qpprime_y_zero_transform_bypass_flag);
675 gst_nv_h264_dec_picture_params_from_pps (GstNvH264Dec * self,
676 const GstH264PPS * pps, CUVIDH264PICPARAMS * params)
678 params->second_chroma_qp_index_offset =
679 (gint8) pps->second_chroma_qp_index_offset;
681 #define COPY_FIELD(f) \
682 (params)->f = (pps)->f
684 COPY_FIELD (entropy_coding_mode_flag);
685 COPY_FIELD (pic_order_present_flag);
686 COPY_FIELD (num_ref_idx_l0_active_minus1);
687 COPY_FIELD (num_ref_idx_l1_active_minus1);
688 COPY_FIELD (pic_init_qp_minus26);
689 COPY_FIELD (weighted_pred_flag);
690 COPY_FIELD (weighted_bipred_idc);
691 COPY_FIELD (pic_init_qp_minus26);
692 COPY_FIELD (deblocking_filter_control_present_flag);
693 COPY_FIELD (redundant_pic_cnt_present_flag);
694 COPY_FIELD (transform_8x8_mode_flag);
695 COPY_FIELD (constrained_intra_pred_flag);
696 COPY_FIELD (chroma_qp_index_offset);
699 gst_nv_h264_dec_fill_scaling_list_4x4 (pps, params);
700 gst_nv_h264_dec_fill_scaling_list_8x8 (pps, params);
704 gst_nv_h264_dec_reset_bitstream_params (GstNvH264Dec * self)
706 self->bitstream_buffer_offset = 0;
707 self->num_slices = 0;
709 self->params.nBitstreamDataLen = 0;
710 self->params.pBitstreamData = NULL;
711 self->params.nNumSlices = 0;
712 self->params.pSliceDataOffsets = NULL;
716 gst_nv_h264_dec_fill_dpb (GstNvH264Dec * self, GstH264Picture * ref,
717 CUVIDH264DPBENTRY * dpb)
719 GstNvDecoderFrame *frame;
721 dpb->not_existing = ref->nonexisting;
724 frame = gst_nv_h264_dec_get_decoder_frame_from_picture (self, ref);
726 dpb->not_existing = 1;
727 } else if (!dpb->not_existing) {
728 dpb->PicIdx = frame->index;
731 if (dpb->not_existing)
734 if (GST_H264_PICTURE_IS_LONG_TERM_REF (ref)) {
735 dpb->FrameIdx = ref->long_term_frame_idx;
736 dpb->is_long_term = 1;
738 dpb->FrameIdx = ref->frame_num;
739 dpb->is_long_term = 0;
742 switch (ref->field) {
743 case GST_H264_PICTURE_FIELD_FRAME:
744 dpb->FieldOrderCnt[0] = ref->top_field_order_cnt;
745 dpb->FieldOrderCnt[1] = ref->bottom_field_order_cnt;
746 dpb->used_for_reference = 0x3;
748 case GST_H264_PICTURE_FIELD_TOP_FIELD:
749 dpb->FieldOrderCnt[0] = ref->top_field_order_cnt;
750 dpb->used_for_reference = 0x1;
751 if (ref->other_field) {
752 dpb->FieldOrderCnt[1] = ref->other_field->bottom_field_order_cnt;
753 dpb->used_for_reference |= 0x2;
755 dpb->FieldOrderCnt[1] = 0;
758 case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
759 dpb->FieldOrderCnt[1] = ref->bottom_field_order_cnt;
760 dpb->used_for_reference = 0x2;
761 if (ref->other_field) {
762 dpb->FieldOrderCnt[0] = ref->other_field->bottom_field_order_cnt;
763 dpb->used_for_reference |= 0x1;
765 dpb->FieldOrderCnt[0] = 0;
769 dpb->FieldOrderCnt[0] = 0;
770 dpb->FieldOrderCnt[1] = 0;
771 dpb->used_for_reference = 0;
777 gst_nv_h264_dec_start_picture (GstH264Decoder * decoder,
778 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
780 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
781 CUVIDPICPARAMS *params = &self->params;
782 CUVIDH264PICPARAMS *h264_params = ¶ms->CodecSpecific.h264;
783 const GstH264SliceHdr *slice_header = &slice->header;
784 const GstH264SPS *sps;
785 const GstH264PPS *pps;
786 GstNvDecoderFrame *frame;
787 GArray *ref_list = self->ref_list;
788 guint i, ref_frame_idx;
790 g_return_val_if_fail (slice_header->pps != NULL, FALSE);
791 g_return_val_if_fail (slice_header->pps->sequence != NULL, FALSE);
793 frame = gst_nv_h264_dec_get_decoder_frame_from_picture (self, picture);
796 GST_ERROR_OBJECT (self,
797 "Couldn't get decoder frame frame picture %p", picture);
798 return GST_FLOW_ERROR;
801 gst_nv_h264_dec_reset_bitstream_params (self);
803 sps = slice_header->pps->sequence;
804 pps = slice_header->pps;
806 /* FIXME: update sps/pps related params only when it's required */
807 params->PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
808 if (!sps->frame_mbs_only_flag) {
809 params->FrameHeightInMbs = (sps->pic_height_in_map_units_minus1 + 1) << 1;
811 params->FrameHeightInMbs = sps->pic_height_in_map_units_minus1 + 1;
813 params->CurrPicIdx = frame->index;
814 params->field_pic_flag = slice_header->field_pic_flag;
815 params->bottom_field_flag =
816 picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
817 params->second_field = picture->second_field;
819 if (picture->field == GST_H264_PICTURE_FIELD_TOP_FIELD) {
820 h264_params->CurrFieldOrderCnt[0] = picture->top_field_order_cnt;
821 h264_params->CurrFieldOrderCnt[1] = 0;
822 } else if (picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
823 h264_params->CurrFieldOrderCnt[0] = 0;
824 h264_params->CurrFieldOrderCnt[1] = picture->bottom_field_order_cnt;
826 h264_params->CurrFieldOrderCnt[0] = picture->top_field_order_cnt;
827 h264_params->CurrFieldOrderCnt[1] = picture->bottom_field_order_cnt;
830 /* nBitstreamDataLen, pBitstreamData, nNumSlices and pSliceDataOffsets
831 * will be set later */
833 params->ref_pic_flag = GST_H264_PICTURE_IS_REF (picture);
834 /* will be updated later, if any slices belong to this frame is not
836 params->intra_pic_flag = 1;
838 h264_params->frame_num = picture->frame_num;
839 h264_params->ref_pic_flag = GST_H264_PICTURE_IS_REF (picture);
841 gst_nv_h264_dec_picture_params_from_sps (self,
842 sps, slice_header->field_pic_flag, h264_params);
843 gst_nv_h264_dec_picture_params_from_pps (self, pps, h264_params);
846 g_array_set_size (ref_list, 0);
848 memset (&h264_params->dpb, 0, sizeof (h264_params->dpb));
849 gst_h264_dpb_get_pictures_short_term_ref (dpb, FALSE, FALSE, ref_list);
850 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
851 GstH264Picture *other = g_array_index (ref_list, GstH264Picture *, i);
852 gst_nv_h264_dec_fill_dpb (self, other, &h264_params->dpb[ref_frame_idx]);
855 g_array_set_size (ref_list, 0);
857 gst_h264_dpb_get_pictures_long_term_ref (dpb, FALSE, ref_list);
858 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
859 GstH264Picture *other = g_array_index (ref_list, GstH264Picture *, i);
860 gst_nv_h264_dec_fill_dpb (self, other, &h264_params->dpb[ref_frame_idx]);
863 g_array_set_size (ref_list, 0);
865 for (i = ref_frame_idx; i < 16; i++)
866 h264_params->dpb[i].PicIdx = -1;
872 gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
873 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
874 GArray * ref_pic_list1)
876 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
879 GST_LOG_OBJECT (self, "Decode slice, nalu size %u", slice->nalu.size);
881 if (self->slice_offsets_alloc_len < self->num_slices + 1) {
882 self->slice_offsets_alloc_len = 2 * (self->num_slices + 1);
884 self->slice_offsets = (guint *) g_realloc_n (self->slice_offsets,
885 self->slice_offsets_alloc_len, sizeof (guint));
887 self->slice_offsets[self->num_slices] = self->bitstream_buffer_offset;
888 GST_LOG_OBJECT (self, "Slice offset %u for slice %d",
889 self->slice_offsets[self->num_slices], self->num_slices);
893 new_size = self->bitstream_buffer_offset + slice->nalu.size + 3;
894 if (self->bitstream_buffer_alloc_size < new_size) {
895 self->bitstream_buffer_alloc_size = 2 * new_size;
897 self->bitstream_buffer = (guint8 *) g_realloc (self->bitstream_buffer,
898 self->bitstream_buffer_alloc_size);
901 self->bitstream_buffer[self->bitstream_buffer_offset] = 0;
902 self->bitstream_buffer[self->bitstream_buffer_offset + 1] = 0;
903 self->bitstream_buffer[self->bitstream_buffer_offset + 2] = 1;
905 memcpy (self->bitstream_buffer + self->bitstream_buffer_offset + 3,
906 slice->nalu.data + slice->nalu.offset, slice->nalu.size);
907 self->bitstream_buffer_offset = new_size;
909 if (!GST_H264_IS_I_SLICE (&slice->header) &&
910 !GST_H264_IS_SI_SLICE (&slice->header))
911 self->params.intra_pic_flag = 0;
917 gst_nv_h264_dec_end_picture (GstH264Decoder * decoder, GstH264Picture * picture)
919 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
921 CUVIDPICPARAMS *params = &self->params;
923 params->nBitstreamDataLen = self->bitstream_buffer_offset;
924 params->pBitstreamData = self->bitstream_buffer;
925 params->nNumSlices = self->num_slices;
926 params->pSliceDataOffsets = self->slice_offsets;
928 GST_LOG_OBJECT (self, "End picture, bitstream len: %" G_GSIZE_FORMAT
929 ", num slices %d", self->bitstream_buffer_offset, self->num_slices);
931 ret = gst_nv_decoder_decode_picture (self->decoder, &self->params);
934 GST_ERROR_OBJECT (self, "Failed to decode picture");
935 return GST_FLOW_ERROR;
942 gst_nv_h264_dec_get_preferred_output_delay (GstH264Decoder * decoder,
945 /* Prefer to zero latency for live pipeline */
949 /* NVCODEC SDK uses 4 frame delay for better throughput performance */
954 gst_nv_h264_dec_register (GstPlugin * plugin, guint device_id, guint rank,
955 GstCaps * sink_caps, GstCaps * src_caps, gboolean is_primary)
960 GstNvDecoderClassData *cdata;
964 GTypeInfo type_info = {
965 sizeof (GstNvH264DecClass),
968 (GClassInitFunc) gst_nv_h264_dec_class_init,
971 sizeof (GstNvH264Dec),
973 (GInstanceInitFunc) gst_nv_h264_dec_init,
976 GST_DEBUG_CATEGORY_INIT (gst_nv_h264_dec_debug, "nvh264dec", 0, "nvh264dec");
978 cdata = g_new0 (GstNvDecoderClassData, 1);
979 cdata->sink_caps = gst_caps_from_string ("video/x-h264, "
980 "stream-format= (string) { avc, avc3, byte-stream }, "
981 "alignment= (string) au, "
982 "profile = (string) { high, main, constrained-high, constrained-baseline, baseline }, "
983 "framerate = " GST_VIDEO_FPS_RANGE);
985 s = gst_caps_get_structure (sink_caps, 0);
986 value = gst_structure_get_value (s, "width");
987 gst_caps_set_value (cdata->sink_caps, "width", value);
989 value = gst_structure_get_value (s, "height");
990 gst_caps_set_value (cdata->sink_caps, "height", value);
992 GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
993 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
994 cdata->src_caps = gst_caps_ref (src_caps);
995 cdata->cuda_device_id = device_id;
998 type_name = g_strdup ("GstNvH264Dec");
999 feature_name = g_strdup ("nvh264dec");
1001 type_name = g_strdup ("GstNvH264SLDec");
1002 feature_name = g_strdup ("nvh264sldec");
1005 while (g_type_from_name (type_name)) {
1008 g_free (feature_name);
1010 type_name = g_strdup_printf ("GstNvH264Device%dDec", index);
1011 feature_name = g_strdup_printf ("nvh264device%ddec", index);
1013 type_name = g_strdup_printf ("GstNvH264SLDevice%dDec", index);
1014 feature_name = g_strdup_printf ("nvh264sldevice%ddec", index);
1018 type_info.class_data = cdata;
1020 type = g_type_register_static (GST_TYPE_H264_DECODER,
1021 type_name, &type_info, 0);
1023 /* make lower rank than default device */
1024 if (rank > 0 && index > 0)
1027 if (!gst_element_register (plugin, feature_name, rank, type))
1028 GST_WARNING ("Failed to register plugin '%s'", type_name);
1031 g_free (feature_name);