FFmpeg
Loading...
Searching...
No Matches
nvdec_hevc.c
Go to the documentation of this file.
1/*
2 * HEVC HW decode acceleration through NVDEC
3 *
4 * Copyright (c) 2017 Anton Khirnov
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include "config_components.h"
24
25#include <stdint.h>
26#include <string.h>
27
28#include "libavutil/mem.h"
29#include "avcodec.h"
30#include "nvdec.h"
31#include "decode.h"
32#include "internal.h"
33#include "hevc/hevcdec.h"
34#include "hevc/data.h"
35#include "hwaccel_internal.h"
36
37static void dpb_add(CUVIDHEVCPICPARAMS *pp, int idx, const HEVCFrame *src)
38{
39 FrameDecodeData *fdd = src->f->private_ref;
40 const NVDECFrame *cf = fdd->hwaccel_priv;
41
42 pp->RefPicIdx[idx] = cf ? cf->idx : -1;
43 pp->PicOrderCntVal[idx] = src->poc;
44 pp->IsLongTerm[idx] = !!(src->flags & HEVC_FRAME_FLAG_LONG_REF);
45}
46
47static int dpb_find(const HEVCFrame *const *dpb, int dpb_size,
48 const HEVCFrame *ref)
49{
50 for (int i = 0; i < dpb_size; i++) {
51 if (dpb[i] == ref)
52 return i;
53 }
54
55 return -1;
56}
57
58static void fill_scaling_lists(CUVIDHEVCPICPARAMS *ppc, const HEVCContext *s)
59{
60 const ScalingList *sl = s->pps->scaling_list_data_present_flag ?
61 &s->pps->scaling_list : &s->pps->sps->scaling_list;
62 int i, j, pos;
63
64 for (i = 0; i < 6; i++) {
65 for (j = 0; j < 16; j++) {
67 ppc->ScalingList4x4[i][j] = sl->sl[0][i][pos];
68 }
69
70 for (j = 0; j < 64; j++) {
72 ppc->ScalingList8x8[i][j] = sl->sl[1][i][pos];
73 ppc->ScalingList16x16[i][j] = sl->sl[2][i][pos];
74
75 if (i < 2)
76 ppc->ScalingList32x32[i][j] = sl->sl[3][i * 3][pos];
77 }
78
79 ppc->ScalingListDCCoeff16x16[i] = sl->sl_dc[0][i];
80 if (i < 2)
81 ppc->ScalingListDCCoeff32x32[i] = sl->sl_dc[1][i * 3];
82 }
83}
84
85#ifdef NVDEC_HAVE_MVHEVC_DECODE
86/* Auxiliary alpha video also codes more than one layer, but the native decoder
87 * keeps only the base layer active for pixel formats without an alpha plane,
88 * so it must not be routed through the MV-HEVC path. */
89static int nvdec_hevc_is_multiview(const HEVCContext *s)
90{
91 return s->vps && s->vps->nb_layers > 1 && !ff_hevc_is_alpha_video(s);
92}
93
94/* Number of layers that will consume a decode surface per access unit. The
95 * caller may have requested fewer views than the VPS codes, and setup_multilayer()
96 * has not run yet when the hardware pool is sized. */
97static int nvdec_hevc_nb_decode_layers(const HEVCContext *s)
98{
99 unsigned active_output;
100 int nb_layers;
101
102 if (!nvdec_hevc_is_multiview(s))
103 return 1;
104
105 nb_layers = ff_hevc_requested_layers(s, s->vps, &active_output);
106 /* An invalid selection is reported by the decoder itself; size for
107 * everything the VPS codes until then. */
108 return nb_layers < 0 ? s->vps->nb_layers : nb_layers;
109}
110
111/* DPB size to request for the selected layers, as passed to
112 * ff_nvdec_frame_params(). Returns the number of decoded layers in nb_layers. */
113static int nvdec_hevc_dpb_size(const AVCodecContext *avctx, int *nb_layers)
114{
115 const HEVCContext *s = avctx->priv_data;
116 const HEVCSPS *sps = s->pps->sps;
117 int dpb_size = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering + 1;
118
119 *nb_layers = nvdec_hevc_nb_decode_layers(s);
120 if (*nb_layers > 1) {
121 const HEVCVPS *vps = s->vps;
122 /* Unlike its SPS and VPS base-layer counterparts, the VPS extension's
123 * max_dec_pic_buffering is not range-checked when parsed, so bound it
124 * before it reaches the surface arithmetic below. */
125 unsigned vps_ext_dpb = FFMIN(vps->dpb_size.max_dec_pic_buffering,
127 int vps_dpb = FFMAX(vps->vps_max_dec_pic_buffering[vps->vps_max_sub_layers - 1],
128 vps_ext_dpb) + 1;
129 dpb_size = FFMAX(dpb_size, vps_dpb);
130 dpb_size *= *nb_layers;
131
132 /* Generic hwaccel setup adds one surface per frame thread. */
134 dpb_size += avctx->thread_count * (*nb_layers - 1);
135 }
136
137 return dpb_size;
138}
139#endif
140
142 const AVBufferRef *buffer_ref,
143 const uint8_t *buffer, uint32_t size)
144{
145 const HEVCContext *s = avctx->priv_data;
146 const HEVCPPS *pps = s->pps;
147 const HEVCSPS *sps = pps->sps;
148
150 CUVIDPICPARAMS *pp = &ctx->pic_params;
151 CUVIDHEVCPICPARAMS *ppc = &pp->CodecSpecific.hevc;
152 FrameDecodeData *fdd;
153 NVDECFrame *cf;
154 const HEVCFrame *dpb[FF_ARRAY_ELEMS(ppc->RefPicIdx)];
155
156 int i, dpb_size, ret;
157 /* Outside MV-HEVC only the layer being decoded contributes references,
158 * and it is not necessarily the base layer (e.g. auxiliary alpha). */
159 unsigned first_dpb_layer = s->cur_layer, nb_dpb_layers = 1;
160
161 /* The HEVC decoder starts every layer of an access unit before ending any
162 * of them, while NVDECContext has storage for a single pending picture, so
163 * submit the previous layer before its parameters and slices are
164 * overwritten. Only a non-base layer can have a sibling still pending: a
165 * base-layer picture always starts a new access unit, and state left behind
166 * by a picture whose decoding was aborted must be discarded rather than
167 * submitted, which ff_nvdec_start_frame() below does. Requiring the base
168 * layer to still be open rejects the leftovers of an aborted access unit
169 * whose base-layer picture was skipped entirely. */
170 if (ctx->nb_slices && s->cur_layer > 0 && s->layers[0].cur_frame) {
171 ret = ff_nvdec_end_frame(avctx);
172 ctx->bitstream_len = 0;
173 ctx->nb_slices = 0;
174 if (ret < 0)
175 return ret;
176 }
177
178 ret = ff_nvdec_start_frame(avctx, s->cur_frame->f);
179 if (ret < 0)
180 return ret;
181
182 fdd = s->cur_frame->f->private_ref;
183 cf = (NVDECFrame*)fdd->hwaccel_priv;
184
185 *pp = (CUVIDPICPARAMS) {
186 .PicWidthInMbs = sps->width / 16,
187 .FrameHeightInMbs = sps->height / 16,
188 .CurrPicIdx = cf->idx,
189 .ref_pic_flag = 1,
190 .intra_pic_flag = IS_IRAP(s),
191
192 .CodecSpecific.hevc = {
193 .pic_width_in_luma_samples = sps->width,
194 .pic_height_in_luma_samples = sps->height,
195 .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
196 .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
197 .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
198 .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
199 .pcm_enabled_flag = sps->pcm_enabled,
200 .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm_enabled ? sps->pcm.log2_min_pcm_cb_size - 3 : 0,
201 .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
202 .pcm_sample_bit_depth_luma_minus1 = sps->pcm_enabled ? sps->pcm.bit_depth - 1 : 0,
203 .pcm_sample_bit_depth_chroma_minus1 = sps->pcm_enabled ? sps->pcm.bit_depth_chroma - 1 : 0,
204#if NVDECAPI_CHECK_VERSION(8, 1)
205 .log2_max_transform_skip_block_size_minus2 = pps->log2_max_transform_skip_block_size - 2,
206 .log2_sao_offset_scale_luma = pps->log2_sao_offset_scale_luma,
207 .log2_sao_offset_scale_chroma = pps->log2_sao_offset_scale_chroma,
208 .high_precision_offsets_enabled_flag = sps->high_precision_offsets_enabled,
209#endif
210 .pcm_loop_filter_disabled_flag = sps->pcm_loop_filter_disabled,
211 .strong_intra_smoothing_enabled_flag = sps->strong_intra_smoothing_enabled,
212 .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
213 .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
214 .amp_enabled_flag = sps->amp_enabled,
215 .separate_colour_plane_flag = sps->separate_colour_plane,
216 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
217 .num_short_term_ref_pic_sets = sps->nb_st_rps,
218 .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present,
219 .num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps,
220 .sps_temporal_mvp_enabled_flag = sps->temporal_mvp_enabled,
221 .sample_adaptive_offset_enabled_flag = sps->sao_enabled,
222 .scaling_list_enable_flag = sps->scaling_list_enabled,
223 .IrapPicFlag = IS_IRAP(s),
224 .IdrPicFlag = IS_IDR(s),
225 .bit_depth_luma_minus8 = sps->bit_depth - 8,
226 .bit_depth_chroma_minus8 = sps->bit_depth - 8,
227#if NVDECAPI_CHECK_VERSION(9, 0)
228 .sps_range_extension_flag = sps->range_extension,
229 .transform_skip_rotation_enabled_flag = sps->transform_skip_rotation_enabled,
230 .transform_skip_context_enabled_flag = sps->transform_skip_context_enabled,
231 .implicit_rdpcm_enabled_flag = sps->implicit_rdpcm_enabled,
232 .explicit_rdpcm_enabled_flag = sps->explicit_rdpcm_enabled,
233 .extended_precision_processing_flag = sps->extended_precision_processing,
234 .intra_smoothing_disabled_flag = sps->intra_smoothing_disabled,
235 .persistent_rice_adaptation_enabled_flag = sps->persistent_rice_adaptation_enabled,
236 .cabac_bypass_alignment_enabled_flag = sps->cabac_bypass_alignment_enabled,
237#endif
238
239 .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag,
240 .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
241 .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag,
242 .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag,
243 .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
244 .init_qp_minus26 = pps->pic_init_qp_minus26,
245 .pps_cb_qp_offset = pps->cb_qp_offset,
246 .pps_cr_qp_offset = pps->cr_qp_offset,
247 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
248 .weighted_pred_flag = pps->weighted_pred_flag,
249 .weighted_bipred_flag = pps->weighted_bipred_flag,
250 .transform_skip_enabled_flag = pps->transform_skip_enabled_flag,
251 .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag,
252 .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag,
253 .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
254 .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
255 .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag,
256 .loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
257 .output_flag_present_flag = pps->output_flag_present_flag,
258 .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
259 .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
260 .lists_modification_present_flag = pps->lists_modification_present_flag,
261 .cabac_init_present_flag = pps->cabac_init_present_flag,
262 .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag,
263 .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag,
264 .pps_deblocking_filter_disabled_flag = pps->disable_dbf,
265 .pps_beta_offset_div2 = pps->beta_offset / 2,
266 .pps_tc_offset_div2 = pps->tc_offset / 2,
267 .tiles_enabled_flag = pps->tiles_enabled_flag,
268 .uniform_spacing_flag = pps->uniform_spacing_flag,
269 .num_tile_columns_minus1 = pps->num_tile_columns - 1,
270 .num_tile_rows_minus1 = pps->num_tile_rows - 1,
271#if NVDECAPI_CHECK_VERSION(9, 0)
272 .pps_range_extension_flag = pps->pps_range_extensions_flag,
273 .cross_component_prediction_enabled_flag = pps->cross_component_prediction_enabled_flag,
274 .chroma_qp_offset_list_enabled_flag = pps->chroma_qp_offset_list_enabled_flag,
275 .diff_cu_chroma_qp_offset_depth = pps->diff_cu_chroma_qp_offset_depth,
276 .chroma_qp_offset_list_len_minus1 = pps->chroma_qp_offset_list_len_minus1,
277#endif
278
279 .NumBitsForShortTermRPSInSlice = s->sh.short_term_rps ? s->sh.short_term_ref_pic_set_size : 0,
280 .NumDeltaPocsOfRefRpsIdx = s->sh.short_term_rps ? s->sh.short_term_rps->rps_idx_num_delta_pocs : 0,
281 .NumPocTotalCurr = ff_hevc_frame_nb_refs(&s->sh, pps, s->cur_layer),
282 .NumPocStCurrBefore = s->rps[ST_CURR_BEF].nb_refs,
283 .NumPocStCurrAfter = s->rps[ST_CURR_AFT].nb_refs,
284 .NumPocLtCurr = s->rps[LT_CURR].nb_refs,
285 .CurrPicOrderCntVal = s->cur_frame->poc,
286 },
287 };
288
289#ifdef NVDEC_HAVE_MVHEVC_DECODE
290 if (nvdec_hevc_is_multiview(s)) {
291 const HEVCVPS *vps = s->vps;
292 int layer_idx = vps->layer_idx[s->nuh_layer_id];
293 int nb_inter_layer_refs = s->rps[INTER_LAYER0].nb_refs +
294 s->rps[INTER_LAYER1].nb_refs;
295
296 if (layer_idx < 0 || layer_idx >= vps->nb_layers) {
297 av_log(avctx, AV_LOG_ERROR,
298 "Invalid MV-HEVC layer ID: %d\n", s->nuh_layer_id);
299 return AVERROR_INVALIDDATA;
300 }
301 if (s->rps[INTER_LAYER0].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetInterLayer0) ||
302 s->rps[INTER_LAYER1].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetInterLayer1)) {
303 av_log(avctx, AV_LOG_ERROR, "Too many inter-layer reference frames\n");
304 return AVERROR_INVALIDDATA;
305 }
306 if (ppc->NumPocTotalCurr < nb_inter_layer_refs) {
307 av_log(avctx, AV_LOG_ERROR, "Invalid inter-layer reference count\n");
308 return AVERROR_INVALIDDATA;
309 }
310
311 ppc->mv_hevc_enable = 1;
312 ppc->nuh_layer_id = s->nuh_layer_id;
313 ppc->default_ref_layers_active_flag = vps->default_ref_layers_active;
314 ppc->NumDirectRefLayers = vps->num_direct_ref_layers[layer_idx];
315 ppc->max_one_active_ref_layer_flag = vps->max_one_active_ref_layer;
316 ppc->poc_lsb_not_present_flag = (vps->poc_lsb_not_present >> layer_idx) & 1;
317 ppc->NumActiveRefLayerPics0 = s->rps[INTER_LAYER0].nb_refs;
318 ppc->NumActiveRefLayerPics1 = s->rps[INTER_LAYER1].nb_refs;
319 /* NVDEC counts active inter-layer pictures separately. */
320 ppc->NumPocTotalCurr -= nb_inter_layer_refs;
321 first_dpb_layer = 0;
322 nb_dpb_layers = vps->nb_layers;
323 }
324#endif
325
326 if (pps->num_tile_columns > FF_ARRAY_ELEMS(ppc->column_width_minus1) ||
327 pps->num_tile_rows > FF_ARRAY_ELEMS(ppc->row_height_minus1)) {
328 av_log(avctx, AV_LOG_ERROR, "Too many tiles\n");
329 return AVERROR(ENOSYS);
330 }
331 for (i = 0; i < pps->num_tile_columns; i++)
332 ppc->column_width_minus1[i] = pps->column_width[i] - 1;
333 for (i = 0; i < pps->num_tile_rows; i++)
334 ppc->row_height_minus1[i] = pps->row_height[i] - 1;
335
336#if NVDECAPI_CHECK_VERSION(9, 0)
337 if (pps->chroma_qp_offset_list_len_minus1 >= FF_ARRAY_ELEMS(ppc->cb_qp_offset_list) ||
338 pps->chroma_qp_offset_list_len_minus1 >= FF_ARRAY_ELEMS(ppc->cr_qp_offset_list)) {
339 av_log(avctx, AV_LOG_ERROR, "Too many chroma_qp_offsets\n");
340 return AVERROR(ENOSYS);
341 }
342 for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
343 ppc->cb_qp_offset_list[i] = pps->cb_qp_offset_list[i];
344 ppc->cr_qp_offset_list[i] = pps->cr_qp_offset_list[i];
345 }
346#endif
347
348 if (s->rps[LT_CURR].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetLtCurr) ||
349 s->rps[ST_CURR_BEF].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrBefore) ||
350 s->rps[ST_CURR_AFT].nb_refs > FF_ARRAY_ELEMS(ppc->RefPicSetStCurrAfter)) {
351 av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
352 return AVERROR(ENOSYS);
353 }
354
355 dpb_size = 0;
356 for (unsigned layer = first_dpb_layer; layer < first_dpb_layer + nb_dpb_layers; layer++) {
357 const HEVCLayerContext *layer_ctx = &s->layers[layer];
358
359 for (i = 0; i < FF_ARRAY_ELEMS(layer_ctx->DPB); i++) {
360 const HEVCFrame *ref = &layer_ctx->DPB[i];
361
362 if (!(ref->flags & (HEVC_FRAME_FLAG_SHORT_REF |
364 continue;
365 if (dpb_size >= FF_ARRAY_ELEMS(ppc->RefPicIdx)) {
366 av_log(avctx, AV_LOG_ERROR, "Too many reference frames\n");
367 return AVERROR_INVALIDDATA;
368 }
369 dpb[dpb_size] = ref;
370 dpb_add(ppc, dpb_size++, ref);
371 }
372 }
373
374#ifdef NVDEC_HAVE_MVHEVC_DECODE
375 /* Inter-layer references are long-term references for MV-HEVC. */
376 for (i = 0; i < s->rps[INTER_LAYER0].nb_refs; i++) {
377 int idx = dpb_find(dpb, dpb_size, s->rps[INTER_LAYER0].ref[i]);
378
379 if (idx < 0) {
380 av_log(avctx, AV_LOG_ERROR,
381 "Inter-layer reference frame missing from the DPB\n");
382 return AVERROR_INVALIDDATA;
383 }
384 ppc->IsLongTerm[idx] = 1;
385 ppc->RefPicSetInterLayer0[i] = idx;
386 }
387 for (i = 0; i < s->rps[INTER_LAYER1].nb_refs; i++) {
388 int idx = dpb_find(dpb, dpb_size, s->rps[INTER_LAYER1].ref[i]);
389
390 if (idx < 0) {
391 av_log(avctx, AV_LOG_ERROR,
392 "Inter-layer reference frame missing from the DPB\n");
393 return AVERROR_INVALIDDATA;
394 }
395 ppc->IsLongTerm[idx] = 1;
396 ppc->RefPicSetInterLayer1[i] = idx;
397 }
398#endif
399
400 for (i = dpb_size; i < FF_ARRAY_ELEMS(ppc->RefPicIdx); i++)
401 ppc->RefPicIdx[i] = -1;
402
403 for (i = 0; i < s->rps[ST_CURR_BEF].nb_refs; i++) {
404 int idx = dpb_find(dpb, dpb_size, s->rps[ST_CURR_BEF].ref[i]);
405
406 if (idx < 0)
407 return AVERROR_BUG;
408 ppc->RefPicSetStCurrBefore[i] = idx;
409 }
410 for (i = 0; i < s->rps[ST_CURR_AFT].nb_refs; i++) {
411 int idx = dpb_find(dpb, dpb_size, s->rps[ST_CURR_AFT].ref[i]);
412
413 if (idx < 0)
414 return AVERROR_BUG;
415 ppc->RefPicSetStCurrAfter[i] = idx;
416 }
417 for (i = 0; i < s->rps[LT_CURR].nb_refs; i++) {
418 int idx = dpb_find(dpb, dpb_size, s->rps[LT_CURR].ref[i]);
419
420 if (idx < 0)
421 return AVERROR_BUG;
422 ppc->RefPicSetLtCurr[i] = idx;
423 }
424
425 fill_scaling_lists(ppc, s);
426
427 return 0;
428}
429
430static int nvdec_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer,
431 uint32_t size)
432{
434 void *tmp;
435
436 tmp = av_fast_realloc(ctx->bitstream_internal, &ctx->bitstream_allocated,
437 ctx->bitstream_len + size + 3);
438 if (!tmp)
439 return AVERROR(ENOMEM);
440 ctx->bitstream = ctx->bitstream_internal = tmp;
441
442 tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
443 (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
444 if (!tmp)
445 return AVERROR(ENOMEM);
446 ctx->slice_offsets = tmp;
447
448 AV_WB24(ctx->bitstream_internal + ctx->bitstream_len, 1);
449 memcpy(ctx->bitstream_internal + ctx->bitstream_len + 3, buffer, size);
450 ctx->slice_offsets[ctx->nb_slices] = ctx->bitstream_len ;
451 ctx->bitstream_len += size + 3;
452 ctx->nb_slices++;
453
454 return 0;
455}
456
458{
460 int ret;
461
462 if (!ctx->nb_slices)
463 return 0;
464
465 ret = ff_nvdec_end_frame(avctx);
466 ctx->bitstream_len = 0;
467 ctx->nb_slices = 0;
468
469 return ret;
470}
471
473 AVBufferRef *hw_frames_ctx,
474 enum AVPixelFormat hw_format)
475{
476#ifdef NVDEC_HAVE_MVHEVC_DECODE
477 int nb_layers;
478 int dpb_size = nvdec_hevc_dpb_size(avctx, &nb_layers);
479#else
480 const HEVCContext *s = avctx->priv_data;
481 const HEVCSPS *sps = s->pps->sps;
482 int dpb_size = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering + 1;
483#endif
484
485 return ff_nvdec_frame_params(avctx, hw_frames_ctx, hw_format, dpb_size, 1);
486}
487
490 ctx->supports_444 = 1;
491
492#ifdef NVDEC_HAVE_MVHEVC_DECODE
493 {
494 int nb_layers;
495 int dpb_size = nvdec_hevc_dpb_size(avctx, &nb_layers);
496
497 /* Every decoded layer of an access unit occupies a decode surface, so a
498 * capped pool would truncate the output instead of failing at init.
499 * Record the requirement here as well: frame_params() is not called
500 * when the caller supplies its own frames context. */
501 if (nb_layers > 1) {
502 ctx->strict_pool_layers = nb_layers;
503 /* Mirror what avcodec_get_hw_frames_parameters() and
504 * nvdec_init_hwframes() add on top of the requested DPB size. */
505 ctx->strict_pool_min = dpb_size + 2 + 3;
507 ctx->strict_pool_min += avctx->thread_count;
508 }
509 }
510#endif
511
512 if (avctx->profile != AV_PROFILE_HEVC_MAIN &&
515#ifdef NVDEC_HAVE_MVHEVC_DECODE
517#endif
518 avctx->profile != AV_PROFILE_HEVC_REXT) {
519 av_log(avctx, AV_LOG_ERROR, "Unsupported HEVC profile: %d\n", avctx->profile);
520 return AVERROR(ENOTSUP);
521 }
522
523 return ff_nvdec_decode_init(avctx);
524}
525
526#if CONFIG_HEVC_NVDEC_HWACCEL
527static int nvdec_hevc_cuda_frame_params(AVCodecContext *avctx,
528 AVBufferRef *hw_frames_ctx)
529{
530 return nvdec_hevc_frame_params(avctx, hw_frames_ctx, AV_PIX_FMT_CUDA);
531}
532
534 .p.name = "hevc_nvdec",
535 .p.type = AVMEDIA_TYPE_VIDEO,
536 .p.id = AV_CODEC_ID_HEVC,
537 .p.pix_fmt = AV_PIX_FMT_CUDA,
538 .start_frame = nvdec_hevc_start_frame,
539 .end_frame = nvdec_hevc_end_frame,
540 .decode_slice = nvdec_hevc_decode_slice,
541 .frame_params = nvdec_hevc_cuda_frame_params,
543 .uninit = ff_nvdec_decode_uninit,
544 .priv_data_size = sizeof(NVDECContext),
545};
546#endif
547
548#if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
549static int nvdec_hevc_cuarray_frame_params(AVCodecContext *avctx,
550 AVBufferRef *hw_frames_ctx)
551{
552 return nvdec_hevc_frame_params(avctx, hw_frames_ctx, AV_PIX_FMT_CUARRAY);
553}
554
556 .p.name = "hevc_nvdec_cuarray",
557 .p.type = AVMEDIA_TYPE_VIDEO,
558 .p.id = AV_CODEC_ID_HEVC,
559 .p.pix_fmt = AV_PIX_FMT_CUARRAY,
560 .start_frame = nvdec_hevc_start_frame,
561 .end_frame = nvdec_hevc_end_frame,
562 .decode_slice = nvdec_hevc_decode_slice,
563 .frame_params = nvdec_hevc_cuarray_frame_params,
565 .uninit = ff_nvdec_decode_uninit,
566 .priv_data_size = sizeof(NVDECContext),
567};
568#endif
static AVFormatContext * ctx
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1595
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_PROFILE_HEVC_MULTIVIEW_MAIN
Definition defs.h:172
#define AV_PROFILE_HEVC_REXT
Definition defs.h:171
#define AV_PROFILE_HEVC_MAIN
Definition defs.h:168
#define AV_PROFILE_HEVC_MAIN_10
Definition defs.h:169
#define AV_PROFILE_HEVC_MAIN_STILL_PICTURE
Definition defs.h:170
uint64_t pps
Definition dovi_rpuenc.c:36
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const uint8_t ff_hevc_diag_scan8x8_x[64]
Definition data.c:39
const uint8_t ff_hevc_diag_scan4x4_x[16]
Definition data.c:25
const uint8_t ff_hevc_diag_scan8x8_y[64]
Definition data.c:58
const uint8_t ff_hevc_diag_scan4x4_y[16]
Definition data.c:32
#define HEVC_FRAME_FLAG_SHORT_REF
Definition hevcdec.h:355
#define IS_IDR(s)
Definition hevcdec.h:74
int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps, unsigned layer_idx)
Get the number of candidate references for the current frame.
Definition refs.c:617
#define HEVC_FRAME_FLAG_LONG_REF
Definition hevcdec.h:356
#define IS_IRAP(s)
Definition hevcdec.h:77
@ ST_CURR_AFT
Definition hevcdec.h:85
@ INTER_LAYER1
Definition hevcdec.h:90
@ ST_CURR_BEF
Definition hevcdec.h:84
@ LT_CURR
Definition hevcdec.h:87
@ INTER_LAYER0
Definition hevcdec.h:89
const struct FFHWAccel ff_hevc_nvdec_cuarray_hwaccel
const struct FFHWAccel ff_hevc_nvdec_hwaccel
#define AV_WB24(p, d)
@ HEVC_MAX_DPB_SIZE
Definition hevc.h:120
int ff_hevc_is_alpha_video(const HEVCContext *s)
Definition hevcdec.c:440
int ff_hevc_requested_layers(const HEVCContext *s, const HEVCVPS *vps, unsigned *active_output)
Resolve the caller's view selection into layers.
Definition hevcdec.c:459
common internal api header.
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition nvdec.c:404
int ff_nvdec_end_frame(AVCodecContext *avctx)
Definition nvdec.c:1040
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition nvdec.c:972
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition nvdec.c:297
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, enum AVPixelFormat hw_format, int dpb_size, int supports_444)
Definition nvdec.c:1109
static void fill_scaling_lists(CUVIDHEVCPICPARAMS *ppc, const HEVCContext *s)
Definition nvdec_hevc.c:58
static int nvdec_hevc_end_frame(AVCodecContext *avctx)
Definition nvdec_hevc.c:457
static int nvdec_hevc_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, enum AVPixelFormat hw_format)
Definition nvdec_hevc.c:472
static void dpb_add(CUVIDHEVCPICPARAMS *pp, int idx, const HEVCFrame *src)
Definition nvdec_hevc.c:37
static int nvdec_hevc_decode_init(AVCodecContext *avctx)
Definition nvdec_hevc.c:488
static int dpb_find(const HEVCFrame *const *dpb, int dpb_size, const HEVCFrame *ref)
Definition nvdec_hevc.c:47
static int nvdec_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition nvdec_hevc.c:430
static int nvdec_hevc_start_frame(AVCodecContext *avctx, const AVBufferRef *buffer_ref, const uint8_t *buffer, uint32_t size)
Definition nvdec_hevc.c:141
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_CUARRAY
hardware decoding through openharmony
Definition pixfmt.h:506
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1603
int profile
profile
Definition avcodec.h:1641
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1584
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition decode.h:33
void * hwaccel_priv
Definition decode.h:54
HEVCFrame DPB[32]
Definition hevcdec.h:453
Definition ps.h:371
Definition ps.h:252
Definition ps.h:168
unsigned int idx
Definition nvdec.h:61
uint8_t sl_dc[2][6]
Definition ps.h:249
uint8_t sl[4][6][64]
Definition ps.h:248
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int dpb_size
static int ref[MAX_W *MAX_W]
static char buffer[20]
Definition seek.c:32
int size