2 * HEVC HW decode acceleration through NVDEC
4 * Copyright (c) 2017 Anton Khirnov
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "hevc_data.h"
33 static void dpb_add(CUVIDHEVCPICPARAMS *pp, int idx, const HEVCFrame *src)
35 FrameDecodeData *fdd = (FrameDecodeData*)src->frame->private_ref->data;
36 const NVDECFrame *cf = fdd->hwaccel_priv;
38 pp->RefPicIdx[idx] = cf ? cf->idx : -1;
39 pp->PicOrderCntVal[idx] = src->poc;
40 pp->IsLongTerm[idx] = !!(src->flags & HEVC_FRAME_FLAG_LONG_REF);
43 static void fill_scaling_lists(CUVIDHEVCPICPARAMS *ppc, const HEVCContext *s)
45 const ScalingList *sl = s->ps.pps->scaling_list_data_present_flag ?
46 &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
49 for (i = 0; i < 6; i++) {
50 for (j = 0; j < 16; j++) {
51 pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
52 ppc->ScalingList4x4[i][j] = sl->sl[0][i][pos];
55 for (j = 0; j < 64; j++) {
56 pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
57 ppc->ScalingList8x8[i][j] = sl->sl[1][i][pos];
58 ppc->ScalingList16x16[i][j] = sl->sl[2][i][pos];
61 ppc->ScalingList32x32[i][j] = sl->sl[3][i][pos];
65 memcpy(ppc->ScalingListDCCoeff16x16, sl->sl_dc[0], sizeof(ppc->ScalingListDCCoeff16x16));
66 memcpy(ppc->ScalingListDCCoeff32x32, sl->sl_dc[1], sizeof(ppc->ScalingListDCCoeff32x32));
69 static int nvdec_hevc_start_frame(AVCodecContext *avctx,
70 const uint8_t *buffer, uint32_t size)
72 const HEVCContext *s = avctx->priv_data;
73 const HEVCPPS *pps = s->ps.pps;
74 const HEVCSPS *sps = s->ps.sps;
76 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
77 CUVIDPICPARAMS *pp = &ctx->pic_params;
78 CUVIDHEVCPICPARAMS *ppc = &pp->CodecSpecific.hevc;
82 int i, j, dpb_size, ret;
84 ret = ff_nvdec_start_frame(avctx, s->ref->frame);
88 fdd = (FrameDecodeData*)s->ref->frame->private_ref->data;
89 cf = (NVDECFrame*)fdd->hwaccel_priv;
91 *pp = (CUVIDPICPARAMS) {
92 .PicWidthInMbs = sps->width / 16,
93 .FrameHeightInMbs = sps->height / 16,
94 .CurrPicIdx = cf->idx,
98 .CodecSpecific.hevc = {
99 .pic_width_in_luma_samples = sps->width,
100 .pic_height_in_luma_samples = sps->height,
101 .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
102 .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
103 .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
104 .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
105 .pcm_enabled_flag = sps->pcm_enabled_flag,
106 .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm_enabled_flag ? sps->pcm.log2_min_pcm_cb_size - 3 : 0,
107 .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
108 .pcm_sample_bit_depth_luma_minus1 = sps->pcm_enabled_flag ? sps->pcm.bit_depth - 1 : 0,
109 .pcm_sample_bit_depth_chroma_minus1 = sps->pcm_enabled_flag ? sps->pcm.bit_depth_chroma - 1 : 0,
110 .pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag,
111 .strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag,
112 .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
113 .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
114 .amp_enabled_flag = sps->amp_enabled_flag,
115 .separate_colour_plane_flag = sps->separate_colour_plane_flag,
116 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
117 .num_short_term_ref_pic_sets = sps->nb_st_rps,
118 .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag,
119 .num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps,
120 .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag,
121 .sample_adaptive_offset_enabled_flag = sps->sao_enabled,
122 .scaling_list_enable_flag = sps->scaling_list_enable_flag,
123 .IrapPicFlag = IS_IRAP(s),
124 .IdrPicFlag = IS_IDR(s),
125 .bit_depth_luma_minus8 = sps->bit_depth - 8,
126 .bit_depth_chroma_minus8 = sps->bit_depth - 8,
128 .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag,
129 .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
130 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag,
131 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
132 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
133 .init_qp_minus26 = pps->pic_init_qp_minus26,
134 .pps_cb_qp_offset = pps->cb_qp_offset,
135 .pps_cr_qp_offset = pps->cr_qp_offset,
136 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
137 .weighted_pred_flag = pps->weighted_pred_flag,
138 .weighted_bipred_flag = pps->weighted_bipred_flag,
139 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
140 .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag,
141 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
142 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
143 .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
144 .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag,
145 .loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
146 .output_flag_present_flag = pps->output_flag_present_flag,
147 .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
148 .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
149 .lists_modification_present_flag = pps->lists_modification_present_flag,
150 .cabac_init_present_flag = pps->cabac_init_present_flag,
151 .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag,
152 .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag,
153 .pps_deblocking_filter_disabled_flag = pps->disable_dbf,
154 .pps_beta_offset_div2 = pps->beta_offset / 2,
155 .pps_tc_offset_div2 = pps->tc_offset / 2,
156 .tiles_enabled_flag = pps->tiles_enabled_flag,
157 .uniform_spacing_flag = pps->uniform_spacing_flag,
158 .num_tile_columns_minus1 = pps->num_tile_columns - 1,
159 .num_tile_rows_minus1 = pps->num_tile_rows - 1,
161 .NumBitsForShortTermRPSInSlice = s->sh.short_term_rps ? s->sh.short_term_ref_pic_set_size : 0,
162 .NumDeltaPocsOfRefRpsIdx = s->sh.short_term_rps ? s->sh.short_term_rps->rps_idx_num_delta_pocs : 0,
163 .NumPocTotalCurr = s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
164 s->rps[LT_CURR].nb_refs,
165 .NumPocStCurrBefore = s->rps[ST_CURR_BEF].nb_refs,
166 .NumPocStCurrAfter = s->rps[ST_CURR_AFT].nb_refs,
167 .NumPocLtCurr = s->rps[LT_CURR].nb_refs,
168 .CurrPicOrderCntVal = s->ref->poc,
172 if (pps->num_tile_columns > FF_ARRAY_ELEMS(ppc->column_width_minus1) ||
173 pps->num_tile_rows > FF_ARRAY_ELEMS(ppc->row_height_minus1)) {
174 av_log(avctx, AV_LOG_ERROR, "Too many tiles\n");
175 return AVERROR(ENOSYS);
177 for (i = 0; i < pps->num_tile_columns; i++)
178 ppc->column_width_minus1[i] = pps->column_width[i] - 1;
179 for (i = 0; i < pps->num_tile_rows; i++)
180 ppc->row_height_minus1[i] = pps->row_height[i] - 1;
182 if (s->rps[LT_CURR].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetLtCurr) ||
183 s->rps[ST_CURR_BEF].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrBefore) ||
184 s->rps[ST_CURR_AFT].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrAfter)) {
185 av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
186 return AVERROR(ENOSYS);
190 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
191 const HEVCFrame *ref = &s->DPB[i];
192 if (!(ref->flags & (HEVC_FRAME_FLAG_SHORT_REF | HEVC_FRAME_FLAG_LONG_REF)))
194 if (dpb_size >= FF_ARRAY_ELEMS(ppc->RefPicIdx)) {
195 av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
196 return AVERROR_INVALIDDATA;
198 dpb_add(ppc, dpb_size++, ref);
201 for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->RefPicIdx); i++)
202 ppc->RefPicIdx[i] = -1;
204 for (i = 0; i < s->rps[ST_CURR_BEF].nb_refs; i++) {
205 for (j = 0; j < dpb_size; j++) {
206 if (ppc->PicOrderCntVal[j] == s->rps[ST_CURR_BEF].list[i]) {
207 ppc->RefPicSetStCurrBefore[i] = j;
212 for (i = 0; i < s->rps[ST_CURR_AFT].nb_refs; i++) {
213 for (j = 0; j < dpb_size; j++) {
214 if (ppc->PicOrderCntVal[j] == s->rps[ST_CURR_AFT].list[i]) {
215 ppc->RefPicSetStCurrAfter[i] = j;
220 for (i = 0; i < s->rps[LT_CURR].nb_refs; i++) {
221 for (j = 0; j < dpb_size; j++) {
222 if (ppc->PicOrderCntVal[j] == s->rps[LT_CURR].list[i]) {
223 ppc->RefPicSetLtCurr[i] = j;
229 fill_scaling_lists(ppc, s);
234 static int nvdec_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
237 NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
240 tmp = av_fast_realloc(ctx->bitstream, &ctx->bitstream_allocated,
241 ctx->bitstream_len + size + 3);
243 return AVERROR(ENOMEM);
244 ctx->bitstream = tmp;
246 tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
247 (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
249 return AVERROR(ENOMEM);
250 ctx->slice_offsets = tmp;
252 AV_WB24(ctx->bitstream + ctx->bitstream_len, 1);
253 memcpy(ctx->bitstream + ctx->bitstream_len + 3, buffer, size);
254 ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
255 ctx->bitstream_len += size + 3;
261 static int nvdec_hevc_frame_params(AVCodecContext *avctx,
262 AVBufferRef *hw_frames_ctx)
264 const HEVCContext *s = avctx->priv_data;
265 const HEVCSPS *sps = s->ps.sps;
266 return ff_nvdec_frame_params(avctx, hw_frames_ctx, sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering + 1);
269 AVHWAccel ff_hevc_nvdec_hwaccel = {
270 .name = "hevc_nvdec",
271 .type = AVMEDIA_TYPE_VIDEO,
272 .id = AV_CODEC_ID_HEVC,
273 .pix_fmt = AV_PIX_FMT_CUDA,
274 .start_frame = nvdec_hevc_start_frame,
275 .end_frame = ff_nvdec_end_frame,
276 .decode_slice = nvdec_hevc_decode_slice,
277 .frame_params = nvdec_hevc_frame_params,
278 .init = ff_nvdec_decode_init,
279 .uninit = ff_nvdec_decode_uninit,
280 .priv_data_size = sizeof(NVDECContext),