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.
77 #include "gstnvh264dec.h"
78 #include "gstcudautils.h"
79 #include "gstnvdecoder.h"
83 GST_DEBUG_CATEGORY_STATIC (gst_nv_h264_dec_debug);
84 #define GST_CAT_DEFAULT gst_nv_h264_dec_debug
88 GstH264Decoder parent;
90 GstVideoCodecState *output_state;
92 GstCudaContext *context;
93 GstNvDecoder *decoder;
94 CUVIDPICPARAMS params;
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;
103 guint *slice_offsets;
104 guint slice_offsets_alloc_len;
108 guint coded_width, coded_height;
110 guint chroma_format_idc;
118 struct _GstNvH264DecClass
120 GstH264DecoderClass parent_class;
121 guint cuda_device_id;
124 #define gst_nv_h264_dec_parent_class parent_class
125 G_DEFINE_TYPE (GstNvH264Dec, gst_nv_h264_dec, GST_TYPE_H264_DECODER);
127 static void gst_nv_h264_decoder_dispose (GObject * object);
128 static void gst_nv_h264_decoder_finalize (GObject * object);
129 static void gst_nv_h264_dec_set_context (GstElement * element,
130 GstContext * context);
131 static gboolean gst_nv_h264_dec_open (GstVideoDecoder * decoder);
132 static gboolean gst_nv_h264_dec_close (GstVideoDecoder * decoder);
133 static gboolean gst_nv_h264_dec_negotiate (GstVideoDecoder * decoder);
134 static gboolean gst_nv_h264_dec_decide_allocation (GstVideoDecoder *
135 decoder, GstQuery * query);
136 static gboolean gst_nv_h264_dec_src_query (GstVideoDecoder * decoder,
140 static GstFlowReturn gst_nv_h264_dec_new_sequence (GstH264Decoder * decoder,
141 const GstH264SPS * sps, gint max_dpb_size);
142 static GstFlowReturn gst_nv_h264_dec_new_picture (GstH264Decoder * decoder,
143 GstVideoCodecFrame * frame, GstH264Picture * picture);
145 gst_nv_h264_dec_new_field_picture (GstH264Decoder * decoder,
146 GstH264Picture * first_field, GstH264Picture * second_field);
147 static GstFlowReturn gst_nv_h264_dec_output_picture (GstH264Decoder *
148 decoder, GstVideoCodecFrame * frame, GstH264Picture * picture);
149 static GstFlowReturn gst_nv_h264_dec_start_picture (GstH264Decoder * decoder,
150 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb);
151 static GstFlowReturn gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
152 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
153 GArray * ref_pic_list1);
154 static GstFlowReturn gst_nv_h264_dec_end_picture (GstH264Decoder * decoder,
155 GstH264Picture * picture);
157 gst_nv_h264_dec_get_preferred_output_delay (GstH264Decoder * decoder,
161 gst_nv_h264_dec_class_init (GstNvH264DecClass * klass)
163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
164 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
166 GstH264DecoderClass *h264decoder_class = GST_H264_DECODER_CLASS (klass);
174 object_class->dispose = gst_nv_h264_decoder_dispose;
175 object_class->finalize = gst_nv_h264_decoder_finalize;
177 element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_set_context);
179 decoder_class->open = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_open);
180 decoder_class->close = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_close);
181 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_negotiate);
182 decoder_class->decide_allocation =
183 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_decide_allocation);
184 decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_src_query);
186 h264decoder_class->new_sequence =
187 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_sequence);
188 h264decoder_class->new_picture =
189 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_picture);
190 h264decoder_class->new_field_picture =
191 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_new_field_picture);
192 h264decoder_class->output_picture =
193 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_output_picture);
194 h264decoder_class->start_picture =
195 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_start_picture);
196 h264decoder_class->decode_slice =
197 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_decode_slice);
198 h264decoder_class->end_picture =
199 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_end_picture);
200 h264decoder_class->get_preferred_output_delay =
201 GST_DEBUG_FUNCPTR (gst_nv_h264_dec_get_preferred_output_delay);
203 GST_DEBUG_CATEGORY_INIT (gst_nv_h264_dec_debug,
204 "nvh264dec", 0, "Nvidia H.264 Decoder");
206 gst_type_mark_as_plugin_api (GST_TYPE_NV_H264_DEC, 0);
210 gst_nv_h264_dec_init (GstNvH264Dec * self)
212 self->ref_list = g_array_sized_new (FALSE, TRUE,
213 sizeof (GstH264Picture *), 16);
214 g_array_set_clear_func (self->ref_list,
215 (GDestroyNotify) gst_h264_picture_clear);
219 gst_nv_h264_decoder_dispose (GObject * object)
221 GstNvH264Dec *self = GST_NV_H264_DEC (object);
223 g_clear_pointer (&self->ref_list, g_array_unref);
225 G_OBJECT_CLASS (parent_class)->dispose (object);
229 gst_nv_h264_decoder_finalize (GObject * object)
231 GstNvH264Dec *self = GST_NV_H264_DEC (object);
233 g_free (self->bitstream_buffer);
234 g_free (self->slice_offsets);
236 G_OBJECT_CLASS (parent_class)->finalize (object);
240 gst_nv_h264_dec_set_context (GstElement * element, GstContext * context)
242 GstNvH264Dec *self = GST_NV_H264_DEC (element);
243 GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self);
245 GST_DEBUG_OBJECT (self, "set context %s",
246 gst_context_get_context_type (context));
248 if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
254 gst_nv_decoder_handle_set_context (self->decoder, element, context);
257 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
260 /* Clear all codec specific (e.g., SPS) data */
262 gst_d3d11_h264_dec_reset (GstNvH264Dec * self)
266 self->coded_width = 0;
267 self->coded_height = 0;
269 self->chroma_format_idc = 0;
270 self->max_dpb_size = 0;
271 self->interlaced = FALSE;
275 gst_nv_h264_dec_open (GstVideoDecoder * decoder)
277 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
278 GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self);
280 if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
281 klass->cuda_device_id, &self->context)) {
282 GST_ERROR_OBJECT (self, "Required element data is unavailable");
286 self->decoder = gst_nv_decoder_new (self->context);
287 if (!self->decoder) {
288 GST_ERROR_OBJECT (self, "Failed to create decoder object");
289 gst_clear_object (&self->context);
294 gst_d3d11_h264_dec_reset (self);
300 gst_nv_h264_dec_close (GstVideoDecoder * decoder)
302 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
304 g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
305 gst_clear_object (&self->decoder);
306 gst_clear_object (&self->context);
312 gst_nv_h264_dec_negotiate (GstVideoDecoder * decoder)
314 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
315 GstH264Decoder *h264dec = GST_H264_DECODER (decoder);
317 GST_DEBUG_OBJECT (self, "negotiate");
319 gst_nv_decoder_negotiate (self->decoder, decoder, h264dec->input_state,
320 &self->output_state);
322 /* TODO: add support D3D11 memory */
324 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
328 gst_nv_h264_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
330 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
332 if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
333 GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
337 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
342 gst_nv_h264_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
344 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
346 switch (GST_QUERY_TYPE (query)) {
347 case GST_QUERY_CONTEXT:
348 if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
351 } else if (self->decoder &&
352 gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
360 return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
364 gst_nv_h264_dec_new_sequence (GstH264Decoder * decoder, const GstH264SPS * sps,
367 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
368 gint crop_width, crop_height;
369 gboolean modified = FALSE;
372 GST_LOG_OBJECT (self, "new sequence");
374 if (sps->frame_cropping_flag) {
375 crop_width = sps->crop_rect_width;
376 crop_height = sps->crop_rect_height;
378 crop_width = sps->width;
379 crop_height = sps->height;
382 if (self->width != crop_width || self->height != crop_height ||
383 self->coded_width != sps->width || self->coded_height != sps->height) {
384 GST_INFO_OBJECT (self, "resolution changed %dx%d (%dx%d)",
385 crop_width, crop_height, sps->width, sps->height);
386 self->width = crop_width;
387 self->height = crop_height;
388 self->coded_width = sps->width;
389 self->coded_height = sps->height;
393 if (self->bitdepth != sps->bit_depth_luma_minus8 + 8) {
394 GST_INFO_OBJECT (self, "bitdepth changed");
395 self->bitdepth = sps->bit_depth_luma_minus8 + 8;
399 if (self->chroma_format_idc != sps->chroma_format_idc) {
400 GST_INFO_OBJECT (self, "chroma format changed");
401 self->chroma_format_idc = sps->chroma_format_idc;
405 interlaced = !sps->frame_mbs_only_flag;
406 if (self->interlaced != interlaced) {
407 GST_INFO_OBJECT (self, "interlaced sequence changed");
408 self->interlaced = interlaced;
412 if (self->max_dpb_size < max_dpb_size) {
413 GST_INFO_OBJECT (self, "Requires larger DPB size (%d -> %d)",
414 self->max_dpb_size, max_dpb_size);
418 if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
420 GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
422 if (self->bitdepth == 8) {
423 if (self->chroma_format_idc == 1)
424 out_format = GST_VIDEO_FORMAT_NV12;
426 GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format");
428 } else if (self->bitdepth == 10) {
429 if (self->chroma_format_idc == 1)
430 out_format = GST_VIDEO_FORMAT_P010_10LE;
432 GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format");
436 if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
437 GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format");
438 return GST_FLOW_NOT_NEGOTIATED;
441 gst_video_info_set_format (&info, out_format, self->width, self->height);
442 if (self->interlaced)
443 GST_VIDEO_INFO_INTERLACE_MODE (&info) = GST_VIDEO_INTERLACE_MODE_MIXED;
445 self->max_dpb_size = max_dpb_size;
446 /* FIXME: add support cudaVideoCodec_H264_SVC and cudaVideoCodec_H264_MVC */
447 if (!gst_nv_decoder_configure (self->decoder,
448 cudaVideoCodec_H264, &info, self->coded_width, self->coded_height,
449 /* Additional 4 buffers for render delay */
451 GST_ERROR_OBJECT (self, "Failed to configure decoder");
452 return GST_FLOW_NOT_NEGOTIATED;
455 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
456 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
457 return GST_FLOW_NOT_NEGOTIATED;
460 memset (&self->params, 0, sizeof (CUVIDPICPARAMS));
467 gst_nv_h264_dec_new_picture (GstH264Decoder * decoder,
468 GstVideoCodecFrame * frame, GstH264Picture * picture)
470 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
471 GstNvDecoderFrame *nv_frame;
473 nv_frame = gst_nv_decoder_new_frame (self->decoder);
475 GST_ERROR_OBJECT (self, "No available decoder frame");
476 return GST_FLOW_ERROR;
479 GST_LOG_OBJECT (self,
480 "New decoder frame %p (index %d)", nv_frame, nv_frame->index);
482 gst_h264_picture_set_user_data (picture,
483 nv_frame, (GDestroyNotify) gst_nv_decoder_frame_unref);
489 gst_nv_h264_dec_new_field_picture (GstH264Decoder * decoder,
490 GstH264Picture * first_field, GstH264Picture * second_field)
492 GstNvDecoderFrame *nv_frame;
494 nv_frame = (GstNvDecoderFrame *)
495 gst_h264_picture_get_user_data (first_field);
497 GST_ERROR_OBJECT (decoder,
498 "No decoder frame in the first picture %p", first_field);
499 return GST_FLOW_ERROR;
502 gst_h264_picture_set_user_data (second_field,
503 gst_nv_decoder_frame_ref (nv_frame),
504 (GDestroyNotify) gst_nv_decoder_frame_unref);
510 gst_nv_h264_dec_output_picture (GstH264Decoder * decoder,
511 GstVideoCodecFrame * frame, GstH264Picture * picture)
513 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
514 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
515 GstNvDecoderFrame *decoder_frame;
517 GST_LOG_OBJECT (self,
518 "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
521 (GstNvDecoderFrame *) gst_h264_picture_get_user_data (picture);
522 if (!decoder_frame) {
523 GST_ERROR_OBJECT (self, "No decoder frame in picture %p", picture);
527 if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
528 &frame->output_buffer)) {
529 GST_ERROR_OBJECT (self, "Failed to handle output picture");
533 if (picture->buffer_flags != 0) {
534 gboolean interlaced =
535 (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_INTERLACED) != 0;
536 gboolean tff = (picture->buffer_flags & GST_VIDEO_BUFFER_FLAG_TFF) != 0;
538 GST_TRACE_OBJECT (self,
539 "apply buffer flags 0x%x (interlaced %d, top-field-first %d)",
540 picture->buffer_flags, interlaced, tff);
541 GST_BUFFER_FLAG_SET (frame->output_buffer, picture->buffer_flags);
544 gst_h264_picture_unref (picture);
546 return gst_video_decoder_finish_frame (vdec, frame);
549 gst_h264_picture_unref (picture);
550 gst_video_decoder_release_frame (vdec, frame);
552 return GST_FLOW_ERROR;
555 static GstNvDecoderFrame *
556 gst_nv_h264_dec_get_decoder_frame_from_picture (GstNvH264Dec * self,
557 GstH264Picture * picture)
559 GstNvDecoderFrame *frame;
561 frame = (GstNvDecoderFrame *) gst_h264_picture_get_user_data (picture);
564 GST_DEBUG_OBJECT (self, "current picture does not have decoder frame");
570 gst_nv_h264_dec_fill_scaling_list_4x4 (const GstH264PPS * pps,
571 CUVIDH264PICPARAMS * params)
575 for (i = 0; i < G_N_ELEMENTS (params->WeightScale4x4); i++)
576 gst_h264_quant_matrix_4x4_get_raster_from_zigzag (params->WeightScale4x4[i],
577 pps->scaling_lists_4x4[i]);
581 gst_nv_h264_dec_fill_scaling_list_8x8 (const GstH264PPS * pps,
582 CUVIDH264PICPARAMS * params)
586 for (i = 0; i < G_N_ELEMENTS (params->WeightScale8x8); i++) {
587 gst_h264_quant_matrix_8x8_get_raster_from_zigzag (params->WeightScale8x8[i],
588 pps->scaling_lists_8x8[i]);
593 gst_nv_h264_dec_picture_params_from_sps (GstNvH264Dec * self,
594 const GstH264SPS * sps, gboolean field_pic, CUVIDH264PICPARAMS * params)
596 params->residual_colour_transform_flag = sps->separate_colour_plane_flag;
597 params->MbaffFrameFlag = sps->mb_adaptive_frame_field_flag && !field_pic;
599 #define COPY_FIELD(f) \
600 (params)->f = (sps)->f
602 COPY_FIELD (log2_max_frame_num_minus4);
603 COPY_FIELD (pic_order_cnt_type);
604 COPY_FIELD (log2_max_pic_order_cnt_lsb_minus4);
605 COPY_FIELD (delta_pic_order_always_zero_flag);
606 COPY_FIELD (frame_mbs_only_flag);
607 COPY_FIELD (direct_8x8_inference_flag);
608 COPY_FIELD (num_ref_frames);
609 COPY_FIELD (bit_depth_luma_minus8);
610 COPY_FIELD (bit_depth_chroma_minus8);
611 COPY_FIELD (qpprime_y_zero_transform_bypass_flag);
617 gst_nv_h264_dec_picture_params_from_pps (GstNvH264Dec * self,
618 const GstH264PPS * pps, CUVIDH264PICPARAMS * params)
620 params->second_chroma_qp_index_offset =
621 (gint8) pps->second_chroma_qp_index_offset;
623 #define COPY_FIELD(f) \
624 (params)->f = (pps)->f
626 COPY_FIELD (entropy_coding_mode_flag);
627 COPY_FIELD (pic_order_present_flag);
628 COPY_FIELD (num_ref_idx_l0_active_minus1);
629 COPY_FIELD (num_ref_idx_l1_active_minus1);
630 COPY_FIELD (pic_init_qp_minus26);
631 COPY_FIELD (weighted_pred_flag);
632 COPY_FIELD (weighted_bipred_idc);
633 COPY_FIELD (pic_init_qp_minus26);
634 COPY_FIELD (deblocking_filter_control_present_flag);
635 COPY_FIELD (redundant_pic_cnt_present_flag);
636 COPY_FIELD (transform_8x8_mode_flag);
637 COPY_FIELD (constrained_intra_pred_flag);
638 COPY_FIELD (chroma_qp_index_offset);
641 gst_nv_h264_dec_fill_scaling_list_4x4 (pps, params);
642 gst_nv_h264_dec_fill_scaling_list_8x8 (pps, params);
646 gst_nv_h264_dec_reset_bitstream_params (GstNvH264Dec * self)
648 self->bitstream_buffer_offset = 0;
649 self->num_slices = 0;
651 self->params.nBitstreamDataLen = 0;
652 self->params.pBitstreamData = NULL;
653 self->params.nNumSlices = 0;
654 self->params.pSliceDataOffsets = NULL;
658 gst_nv_h264_dec_fill_dpb (GstNvH264Dec * self, GstH264Picture * ref,
659 CUVIDH264DPBENTRY * dpb)
661 GstNvDecoderFrame *frame;
663 dpb->not_existing = ref->nonexisting;
666 frame = gst_nv_h264_dec_get_decoder_frame_from_picture (self, ref);
668 dpb->not_existing = 1;
669 } else if (!dpb->not_existing) {
670 dpb->PicIdx = frame->index;
673 if (dpb->not_existing)
676 if (GST_H264_PICTURE_IS_LONG_TERM_REF (ref)) {
677 dpb->FrameIdx = ref->long_term_frame_idx;
678 dpb->is_long_term = 1;
680 dpb->FrameIdx = ref->frame_num;
681 dpb->is_long_term = 0;
684 switch (ref->field) {
685 case GST_H264_PICTURE_FIELD_FRAME:
686 dpb->FieldOrderCnt[0] = ref->top_field_order_cnt;
687 dpb->FieldOrderCnt[1] = ref->bottom_field_order_cnt;
688 dpb->used_for_reference = 0x3;
690 case GST_H264_PICTURE_FIELD_TOP_FIELD:
691 dpb->FieldOrderCnt[0] = ref->top_field_order_cnt;
692 dpb->used_for_reference = 0x1;
693 if (ref->other_field) {
694 dpb->FieldOrderCnt[1] = ref->other_field->bottom_field_order_cnt;
695 dpb->used_for_reference |= 0x2;
697 dpb->FieldOrderCnt[1] = 0;
700 case GST_H264_PICTURE_FIELD_BOTTOM_FIELD:
701 dpb->FieldOrderCnt[1] = ref->bottom_field_order_cnt;
702 dpb->used_for_reference = 0x2;
703 if (ref->other_field) {
704 dpb->FieldOrderCnt[0] = ref->other_field->bottom_field_order_cnt;
705 dpb->used_for_reference |= 0x1;
707 dpb->FieldOrderCnt[0] = 0;
711 dpb->FieldOrderCnt[0] = 0;
712 dpb->FieldOrderCnt[1] = 0;
713 dpb->used_for_reference = 0;
719 gst_nv_h264_dec_start_picture (GstH264Decoder * decoder,
720 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
722 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
723 CUVIDPICPARAMS *params = &self->params;
724 CUVIDH264PICPARAMS *h264_params = ¶ms->CodecSpecific.h264;
725 const GstH264SliceHdr *slice_header = &slice->header;
726 const GstH264SPS *sps;
727 const GstH264PPS *pps;
728 GstNvDecoderFrame *frame;
729 GArray *ref_list = self->ref_list;
730 guint i, ref_frame_idx;
732 g_return_val_if_fail (slice_header->pps != NULL, FALSE);
733 g_return_val_if_fail (slice_header->pps->sequence != NULL, FALSE);
735 frame = gst_nv_h264_dec_get_decoder_frame_from_picture (self, picture);
738 GST_ERROR_OBJECT (self,
739 "Couldn't get decoder frame frame picture %p", picture);
740 return GST_FLOW_ERROR;
743 gst_nv_h264_dec_reset_bitstream_params (self);
745 sps = slice_header->pps->sequence;
746 pps = slice_header->pps;
748 /* FIXME: update sps/pps related params only when it's required */
749 params->PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
750 if (!sps->frame_mbs_only_flag) {
751 params->FrameHeightInMbs = (sps->pic_height_in_map_units_minus1 + 1) << 1;
753 params->FrameHeightInMbs = sps->pic_height_in_map_units_minus1 + 1;
755 params->CurrPicIdx = frame->index;
756 params->field_pic_flag = slice_header->field_pic_flag;
757 params->bottom_field_flag =
758 picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD;
759 params->second_field = picture->second_field;
761 if (picture->field == GST_H264_PICTURE_FIELD_TOP_FIELD) {
762 h264_params->CurrFieldOrderCnt[0] = picture->top_field_order_cnt;
763 h264_params->CurrFieldOrderCnt[1] = 0;
764 } else if (picture->field == GST_H264_PICTURE_FIELD_BOTTOM_FIELD) {
765 h264_params->CurrFieldOrderCnt[0] = 0;
766 h264_params->CurrFieldOrderCnt[1] = picture->bottom_field_order_cnt;
768 h264_params->CurrFieldOrderCnt[0] = picture->top_field_order_cnt;
769 h264_params->CurrFieldOrderCnt[1] = picture->bottom_field_order_cnt;
772 /* nBitstreamDataLen, pBitstreamData, nNumSlices and pSliceDataOffsets
773 * will be set later */
775 params->ref_pic_flag = GST_H264_PICTURE_IS_REF (picture);
776 /* will be updated later, if any slices belong to this frame is not
778 params->intra_pic_flag = 1;
780 h264_params->frame_num = picture->frame_num;
781 h264_params->ref_pic_flag = GST_H264_PICTURE_IS_REF (picture);
783 gst_nv_h264_dec_picture_params_from_sps (self,
784 sps, slice_header->field_pic_flag, h264_params);
785 gst_nv_h264_dec_picture_params_from_pps (self, pps, h264_params);
788 g_array_set_size (ref_list, 0);
790 memset (&h264_params->dpb, 0, sizeof (h264_params->dpb));
791 gst_h264_dpb_get_pictures_short_term_ref (dpb, FALSE, FALSE, ref_list);
792 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
793 GstH264Picture *other = g_array_index (ref_list, GstH264Picture *, i);
794 gst_nv_h264_dec_fill_dpb (self, other, &h264_params->dpb[ref_frame_idx]);
797 g_array_set_size (ref_list, 0);
799 gst_h264_dpb_get_pictures_long_term_ref (dpb, FALSE, ref_list);
800 for (i = 0; ref_frame_idx < 16 && i < ref_list->len; i++) {
801 GstH264Picture *other = g_array_index (ref_list, GstH264Picture *, i);
802 gst_nv_h264_dec_fill_dpb (self, other, &h264_params->dpb[ref_frame_idx]);
805 g_array_set_size (ref_list, 0);
807 for (i = ref_frame_idx; i < 16; i++)
808 h264_params->dpb[i].PicIdx = -1;
814 gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
815 GstH264Picture * picture, GstH264Slice * slice, GArray * ref_pic_list0,
816 GArray * ref_pic_list1)
818 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
821 GST_LOG_OBJECT (self, "Decode slice, nalu size %u", slice->nalu.size);
823 if (self->slice_offsets_alloc_len < self->num_slices + 1) {
824 self->slice_offsets = (guint *) g_realloc_n (self->slice_offsets,
825 self->num_slices + 1, sizeof (guint));
827 self->slice_offsets[self->num_slices] = self->bitstream_buffer_offset;
828 GST_LOG_OBJECT (self, "Slice offset %u for slice %d",
829 self->slice_offsets[self->num_slices], self->num_slices);
833 new_size = self->bitstream_buffer_offset + slice->nalu.size + 3;
834 if (self->bitstream_buffer_alloc_size < new_size) {
835 self->bitstream_buffer =
836 (guint8 *) g_realloc (self->bitstream_buffer, new_size);
839 self->bitstream_buffer[self->bitstream_buffer_offset] = 0;
840 self->bitstream_buffer[self->bitstream_buffer_offset + 1] = 0;
841 self->bitstream_buffer[self->bitstream_buffer_offset + 2] = 1;
843 memcpy (self->bitstream_buffer + self->bitstream_buffer_offset + 3,
844 slice->nalu.data + slice->nalu.offset, slice->nalu.size);
845 self->bitstream_buffer_offset = new_size;
847 if (!GST_H264_IS_I_SLICE (&slice->header) &&
848 !GST_H264_IS_SI_SLICE (&slice->header))
849 self->params.intra_pic_flag = 0;
855 gst_nv_h264_dec_end_picture (GstH264Decoder * decoder, GstH264Picture * picture)
857 GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
859 CUVIDPICPARAMS *params = &self->params;
861 params->nBitstreamDataLen = self->bitstream_buffer_offset;
862 params->pBitstreamData = self->bitstream_buffer;
863 params->nNumSlices = self->num_slices;
864 params->pSliceDataOffsets = self->slice_offsets;
866 GST_LOG_OBJECT (self, "End picture, bitstream len: %" G_GSIZE_FORMAT
867 ", num slices %d", self->bitstream_buffer_offset, self->num_slices);
869 ret = gst_nv_decoder_decode_picture (self->decoder, &self->params);
872 GST_ERROR_OBJECT (self, "Failed to decode picture");
873 return GST_FLOW_ERROR;
880 gst_nv_h264_dec_get_preferred_output_delay (GstH264Decoder * decoder,
883 /* Prefer to zero latency for live pipeline */
887 /* NVCODEC SDK uses 4 frame delay for better throughput performance */
895 guint cuda_device_id;
897 } GstNvH264DecClassData;
900 gst_nv_h264_dec_subclass_init (gpointer klass, GstNvH264DecClassData * cdata)
902 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
903 GstNvH264DecClass *nvdec_class = (GstNvH264DecClass *) (klass);
906 if (cdata->is_default) {
907 long_name = g_strdup_printf ("NVDEC H.264 Stateless Decoder");
909 long_name = g_strdup_printf ("NVDEC H.264 Stateless Decoder with device %d",
910 cdata->cuda_device_id);
913 gst_element_class_set_metadata (element_class, long_name,
914 "Codec/Decoder/Video/Hardware",
915 "Nvidia H.264 video decoder", "Seungha Yang <seungha@centricular.com>");
918 gst_element_class_add_pad_template (element_class,
919 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
921 gst_element_class_add_pad_template (element_class,
922 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
925 nvdec_class->cuda_device_id = cdata->cuda_device_id;
927 gst_caps_unref (cdata->sink_caps);
928 gst_caps_unref (cdata->src_caps);
933 gst_nv_h264_dec_register (GstPlugin * plugin, guint device_id, guint rank,
934 GstCaps * sink_caps, GstCaps * src_caps, gboolean is_primary)
936 GTypeQuery type_query;
937 GTypeInfo type_info = { 0, };
941 GstNvH264DecClassData *cdata;
942 gboolean is_default = TRUE;
947 * element-nvh264sldec
952 cdata = g_new0 (GstNvH264DecClassData, 1);
953 cdata->sink_caps = gst_caps_from_string ("video/x-h264, "
954 "stream-format= (string) { avc, avc3, byte-stream }, "
955 "alignment= (string) au, "
956 "profile = (string) { high, main, constrained-high, constrained-baseline, baseline }, "
957 "framerate = " GST_VIDEO_FPS_RANGE);
959 s = gst_caps_get_structure (sink_caps, 0);
960 value = gst_structure_get_value (s, "width");
961 gst_caps_set_value (cdata->sink_caps, "width", value);
963 value = gst_structure_get_value (s, "height");
964 gst_caps_set_value (cdata->sink_caps, "height", value);
966 GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
967 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
968 cdata->src_caps = gst_caps_ref (src_caps);
969 cdata->cuda_device_id = device_id;
971 g_type_query (GST_TYPE_NV_H264_DEC, &type_query);
972 memset (&type_info, 0, sizeof (type_info));
973 type_info.class_size = type_query.class_size;
974 type_info.instance_size = type_query.instance_size;
975 type_info.class_init = (GClassInitFunc) gst_nv_h264_dec_subclass_init;
976 type_info.class_data = cdata;
979 type_name = g_strdup ("GstNvH264StatelessPrimaryDec");
980 feature_name = g_strdup ("nvh264dec");
982 type_name = g_strdup ("GstNvH264StatelessDec");
983 feature_name = g_strdup ("nvh264sldec");
986 if (g_type_from_name (type_name) != 0) {
988 g_free (feature_name);
991 g_strdup_printf ("GstNvH264StatelessPrimaryDevice%dDec", device_id);
992 feature_name = g_strdup_printf ("nvh264device%ddec", device_id);
994 type_name = g_strdup_printf ("GstNvH264StatelessDevice%dDec", device_id);
995 feature_name = g_strdup_printf ("nvh264sldevice%ddec", device_id);
1001 cdata->is_default = is_default;
1002 subtype = g_type_register_static (GST_TYPE_NV_H264_DEC,
1003 type_name, &type_info, 0);
1005 /* make lower rank than default device */
1006 if (rank > 0 && !is_default)
1009 if (!gst_element_register (plugin, feature_name, rank, subtype))
1010 GST_WARNING ("Failed to register plugin '%s'", type_name);
1013 g_free (feature_name);