| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AV1 HW decode acceleration through VA API | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavutil/frame.h" | ||
| 22 | #include "libavutil/mem.h" | ||
| 23 | #include "hwaccel_internal.h" | ||
| 24 | #include "vaapi_decode.h" | ||
| 25 | #include "internal.h" | ||
| 26 | #include "av1dec.h" | ||
| 27 | #include "thread.h" | ||
| 28 | |||
| 29 | typedef struct VAAPIAV1FrameRef { | ||
| 30 | AVFrame *frame; | ||
| 31 | int valid; | ||
| 32 | } VAAPIAV1FrameRef; | ||
| 33 | |||
| 34 | typedef struct VAAPIAV1DecContext { | ||
| 35 | VAAPIDecodeContext base; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * For film grain case, VAAPI generate 2 output for each frame, | ||
| 39 | * current_frame will not apply film grain, and will be used for | ||
| 40 | * references for next frames. Maintain the reference list without | ||
| 41 | * applying film grain here. And current_display_picture will be | ||
| 42 | * used to apply film grain and push to downstream. | ||
| 43 | */ | ||
| 44 | VAAPIAV1FrameRef ref_tab[AV1_NUM_REF_FRAMES]; | ||
| 45 | AVFrame *tmp_frame; | ||
| 46 | |||
| 47 | int nb_slice_params; | ||
| 48 | VASliceParameterBufferAV1 *slice_params; | ||
| 49 | } VAAPIAV1DecContext; | ||
| 50 | |||
| 51 | ✗ | static VASurfaceID vaapi_av1_surface_id(AV1Frame *vf) | |
| 52 | { | ||
| 53 | ✗ | if (vf->f) | |
| 54 | ✗ | return ff_vaapi_get_surface_id(vf->f); | |
| 55 | else | ||
| 56 | ✗ | return VA_INVALID_SURFACE; | |
| 57 | } | ||
| 58 | |||
| 59 | ✗ | static int8_t vaapi_av1_get_bit_depth_idx(AVCodecContext *avctx) | |
| 60 | { | ||
| 61 | ✗ | AV1DecContext *s = avctx->priv_data; | |
| 62 | ✗ | const AV1RawSequenceHeader *seq = s->raw_seq; | |
| 63 | ✗ | int8_t bit_depth = 8; | |
| 64 | |||
| 65 | ✗ | if (seq->seq_profile == 2 && seq->color_config.high_bitdepth) | |
| 66 | ✗ | bit_depth = seq->color_config.twelve_bit ? 12 : 10; | |
| 67 | ✗ | else if (seq->seq_profile <= 2) | |
| 68 | ✗ | bit_depth = seq->color_config.high_bitdepth ? 10 : 8; | |
| 69 | else { | ||
| 70 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 71 | ✗ | "Couldn't get bit depth from profile:%d.\n", seq->seq_profile); | |
| 72 | ✗ | return -1; | |
| 73 | } | ||
| 74 | ✗ | return bit_depth == 8 ? 0 : bit_depth == 10 ? 1 : 2; | |
| 75 | } | ||
| 76 | |||
| 77 | ✗ | static av_cold int vaapi_av1_decode_init(AVCodecContext *avctx) | |
| 78 | { | ||
| 79 | ✗ | VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 80 | |||
| 81 | ✗ | ctx->tmp_frame = av_frame_alloc(); | |
| 82 | ✗ | if (!ctx->tmp_frame) | |
| 83 | ✗ | return AVERROR(ENOMEM); | |
| 84 | |||
| 85 | ✗ | for (int i = 0; i < FF_ARRAY_ELEMS(ctx->ref_tab); i++) { | |
| 86 | ✗ | ctx->ref_tab[i].frame = av_frame_alloc(); | |
| 87 | ✗ | if (!ctx->ref_tab[i].frame) | |
| 88 | ✗ | return AVERROR(ENOMEM); | |
| 89 | ✗ | ctx->ref_tab[i].valid = 0; | |
| 90 | } | ||
| 91 | |||
| 92 | ✗ | return ff_vaapi_decode_init(avctx); | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | static av_cold int vaapi_av1_decode_uninit(AVCodecContext *avctx) | |
| 96 | { | ||
| 97 | ✗ | VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 98 | |||
| 99 | ✗ | av_frame_free(&ctx->tmp_frame); | |
| 100 | |||
| 101 | ✗ | for (int i = 0; i < FF_ARRAY_ELEMS(ctx->ref_tab); i++) | |
| 102 | ✗ | av_frame_free(&ctx->ref_tab[i].frame); | |
| 103 | |||
| 104 | ✗ | av_freep(&ctx->slice_params); | |
| 105 | |||
| 106 | ✗ | return ff_vaapi_decode_uninit(avctx); | |
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | ✗ | static int vaapi_av1_start_frame(AVCodecContext *avctx, | |
| 111 | av_unused const AVBufferRef *buffer_ref, | ||
| 112 | av_unused const uint8_t *buffer, | ||
| 113 | av_unused uint32_t size) | ||
| 114 | { | ||
| 115 | ✗ | AV1DecContext *s = avctx->priv_data; | |
| 116 | ✗ | const AV1RawSequenceHeader *seq = s->raw_seq; | |
| 117 | ✗ | const AV1RawFrameHeader *frame_header = s->raw_frame_header; | |
| 118 | ✗ | const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain; | |
| 119 | ✗ | VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private; | |
| 120 | ✗ | VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 121 | VADecPictureParameterBufferAV1 pic_param; | ||
| 122 | int8_t bit_depth_idx; | ||
| 123 | ✗ | int err = 0; | |
| 124 | ✗ | int apply_grain = !(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) && film_grain->apply_grain; | |
| 125 | ✗ | uint8_t remap_lr_type[4] = {AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ}; | |
| 126 | ✗ | uint8_t segmentation_feature_signed[AV1_SEG_LVL_MAX] = {1, 1, 1, 1, 1, 0, 0, 0}; | |
| 127 | ✗ | uint8_t segmentation_feature_max[AV1_SEG_LVL_MAX] = {255, AV1_MAX_LOOP_FILTER, | |
| 128 | AV1_MAX_LOOP_FILTER, AV1_MAX_LOOP_FILTER, AV1_MAX_LOOP_FILTER, 7 , 0 , 0 }; | ||
| 129 | |||
| 130 | ✗ | bit_depth_idx = vaapi_av1_get_bit_depth_idx(avctx); | |
| 131 | ✗ | if (bit_depth_idx < 0) | |
| 132 | ✗ | goto fail; | |
| 133 | |||
| 134 | ✗ | if (apply_grain) { | |
| 135 | ✗ | if (ctx->tmp_frame->buf[0]) | |
| 136 | ✗ | av_frame_unref(ctx->tmp_frame); | |
| 137 | ✗ | err = ff_thread_get_buffer(avctx, ctx->tmp_frame, AV_GET_BUFFER_FLAG_REF); | |
| 138 | ✗ | if (err < 0) | |
| 139 | ✗ | goto fail; | |
| 140 | ✗ | pic->output_surface = ff_vaapi_get_surface_id(ctx->tmp_frame); | |
| 141 | } else { | ||
| 142 | ✗ | pic->output_surface = ff_vaapi_get_surface_id(s->cur_frame.f); | |
| 143 | } | ||
| 144 | |||
| 145 | ✗ | memset(&pic_param, 0, sizeof(VADecPictureParameterBufferAV1)); | |
| 146 | ✗ | pic_param = (VADecPictureParameterBufferAV1) { | |
| 147 | ✗ | .profile = seq->seq_profile, | |
| 148 | ✗ | .order_hint_bits_minus_1 = seq->order_hint_bits_minus_1, | |
| 149 | .bit_depth_idx = bit_depth_idx, | ||
| 150 | ✗ | .matrix_coefficients = seq->color_config.matrix_coefficients, | |
| 151 | ✗ | .current_frame = pic->output_surface, | |
| 152 | ✗ | .current_display_picture = ff_vaapi_get_surface_id(s->cur_frame.f), | |
| 153 | ✗ | .frame_width_minus1 = frame_header->frame_width_minus_1, | |
| 154 | ✗ | .frame_height_minus1 = frame_header->frame_height_minus_1, | |
| 155 | ✗ | .primary_ref_frame = frame_header->primary_ref_frame, | |
| 156 | ✗ | .order_hint = frame_header->order_hint, | |
| 157 | ✗ | .tile_cols = frame_header->tile_cols, | |
| 158 | ✗ | .tile_rows = frame_header->tile_rows, | |
| 159 | ✗ | .context_update_tile_id = frame_header->context_update_tile_id, | |
| 160 | ✗ | .superres_scale_denominator = frame_header->use_superres ? | |
| 161 | ✗ | frame_header->coded_denom + AV1_SUPERRES_DENOM_MIN : | |
| 162 | AV1_SUPERRES_NUM, | ||
| 163 | ✗ | .interp_filter = frame_header->interpolation_filter, | |
| 164 | ✗ | .filter_level[0] = frame_header->loop_filter_level[0], | |
| 165 | ✗ | .filter_level[1] = frame_header->loop_filter_level[1], | |
| 166 | ✗ | .filter_level_u = frame_header->loop_filter_level[2], | |
| 167 | ✗ | .filter_level_v = frame_header->loop_filter_level[3], | |
| 168 | ✗ | .base_qindex = frame_header->base_q_idx, | |
| 169 | ✗ | .y_dc_delta_q = frame_header->delta_q_y_dc, | |
| 170 | ✗ | .u_dc_delta_q = frame_header->delta_q_u_dc, | |
| 171 | ✗ | .u_ac_delta_q = frame_header->delta_q_u_ac, | |
| 172 | ✗ | .v_dc_delta_q = frame_header->delta_q_v_dc, | |
| 173 | ✗ | .v_ac_delta_q = frame_header->delta_q_v_ac, | |
| 174 | ✗ | .cdef_damping_minus_3 = frame_header->cdef_damping_minus_3, | |
| 175 | ✗ | .cdef_bits = frame_header->cdef_bits, | |
| 176 | .seq_info_fields.fields = { | ||
| 177 | ✗ | .still_picture = seq->still_picture, | |
| 178 | ✗ | .use_128x128_superblock = seq->use_128x128_superblock, | |
| 179 | ✗ | .enable_filter_intra = seq->enable_filter_intra, | |
| 180 | ✗ | .enable_intra_edge_filter = seq->enable_intra_edge_filter, | |
| 181 | ✗ | .enable_interintra_compound = seq->enable_interintra_compound, | |
| 182 | ✗ | .enable_masked_compound = seq->enable_masked_compound, | |
| 183 | ✗ | .enable_dual_filter = seq->enable_dual_filter, | |
| 184 | ✗ | .enable_order_hint = seq->enable_order_hint, | |
| 185 | ✗ | .enable_jnt_comp = seq->enable_jnt_comp, | |
| 186 | ✗ | .enable_cdef = seq->enable_cdef, | |
| 187 | ✗ | .mono_chrome = seq->color_config.mono_chrome, | |
| 188 | ✗ | .color_range = seq->color_config.color_range, | |
| 189 | ✗ | .subsampling_x = seq->color_config.subsampling_x, | |
| 190 | ✗ | .subsampling_y = seq->color_config.subsampling_y, | |
| 191 | ✗ | .chroma_sample_position = seq->color_config.chroma_sample_position, | |
| 192 | ✗ | .film_grain_params_present = seq->film_grain_params_present && | |
| 193 | ✗ | !(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN), | |
| 194 | }, | ||
| 195 | .seg_info.segment_info_fields.bits = { | ||
| 196 | ✗ | .enabled = frame_header->segmentation_enabled, | |
| 197 | ✗ | .update_map = frame_header->segmentation_update_map, | |
| 198 | ✗ | .temporal_update = frame_header->segmentation_temporal_update, | |
| 199 | ✗ | .update_data = frame_header->segmentation_update_data, | |
| 200 | }, | ||
| 201 | .film_grain_info = { | ||
| 202 | .film_grain_info_fields.bits = { | ||
| 203 | .apply_grain = apply_grain, | ||
| 204 | ✗ | .chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma, | |
| 205 | ✗ | .grain_scaling_minus_8 = film_grain->grain_scaling_minus_8, | |
| 206 | ✗ | .ar_coeff_lag = film_grain->ar_coeff_lag, | |
| 207 | ✗ | .ar_coeff_shift_minus_6 = film_grain->ar_coeff_shift_minus_6, | |
| 208 | ✗ | .grain_scale_shift = film_grain->grain_scale_shift, | |
| 209 | ✗ | .overlap_flag = film_grain->overlap_flag, | |
| 210 | ✗ | .clip_to_restricted_range = film_grain->clip_to_restricted_range, | |
| 211 | }, | ||
| 212 | ✗ | .grain_seed = film_grain->grain_seed, | |
| 213 | ✗ | .num_y_points = film_grain->num_y_points, | |
| 214 | ✗ | .num_cb_points = film_grain->num_cb_points, | |
| 215 | ✗ | .num_cr_points = film_grain->num_cr_points, | |
| 216 | ✗ | .cb_mult = film_grain->cb_mult, | |
| 217 | ✗ | .cb_luma_mult = film_grain->cb_luma_mult, | |
| 218 | ✗ | .cb_offset = film_grain->cb_offset, | |
| 219 | ✗ | .cr_mult = film_grain->cr_mult, | |
| 220 | ✗ | .cr_luma_mult = film_grain->cr_luma_mult, | |
| 221 | ✗ | .cr_offset = film_grain->cr_offset, | |
| 222 | }, | ||
| 223 | .pic_info_fields.bits = { | ||
| 224 | ✗ | .frame_type = frame_header->frame_type, | |
| 225 | ✗ | .show_frame = frame_header->show_frame, | |
| 226 | ✗ | .showable_frame = frame_header->showable_frame, | |
| 227 | ✗ | .error_resilient_mode = frame_header->error_resilient_mode, | |
| 228 | ✗ | .disable_cdf_update = frame_header->disable_cdf_update, | |
| 229 | ✗ | .allow_screen_content_tools = frame_header->allow_screen_content_tools, | |
| 230 | ✗ | .force_integer_mv = s->cur_frame.force_integer_mv, | |
| 231 | ✗ | .allow_intrabc = frame_header->allow_intrabc, | |
| 232 | ✗ | .use_superres = frame_header->use_superres, | |
| 233 | ✗ | .allow_high_precision_mv = frame_header->allow_high_precision_mv, | |
| 234 | ✗ | .is_motion_mode_switchable = frame_header->is_motion_mode_switchable, | |
| 235 | ✗ | .use_ref_frame_mvs = frame_header->use_ref_frame_mvs, | |
| 236 | ✗ | .disable_frame_end_update_cdf = frame_header->disable_frame_end_update_cdf, | |
| 237 | ✗ | .uniform_tile_spacing_flag = frame_header->uniform_tile_spacing_flag, | |
| 238 | ✗ | .allow_warped_motion = frame_header->allow_warped_motion, | |
| 239 | }, | ||
| 240 | .loop_filter_info_fields.bits = { | ||
| 241 | ✗ | .sharpness_level = frame_header->loop_filter_sharpness, | |
| 242 | ✗ | .mode_ref_delta_enabled = frame_header->loop_filter_delta_enabled, | |
| 243 | ✗ | .mode_ref_delta_update = frame_header->loop_filter_delta_update, | |
| 244 | }, | ||
| 245 | .mode_control_fields.bits = { | ||
| 246 | ✗ | .delta_q_present_flag = frame_header->delta_q_present, | |
| 247 | ✗ | .log2_delta_q_res = frame_header->delta_q_res, | |
| 248 | ✗ | .delta_lf_present_flag = frame_header->delta_lf_present, | |
| 249 | ✗ | .log2_delta_lf_res = frame_header->delta_lf_res, | |
| 250 | ✗ | .delta_lf_multi = frame_header->delta_lf_multi, | |
| 251 | ✗ | .tx_mode = frame_header->tx_mode, | |
| 252 | ✗ | .reference_select = frame_header->reference_select, | |
| 253 | ✗ | .reduced_tx_set_used = frame_header->reduced_tx_set, | |
| 254 | ✗ | .skip_mode_present = frame_header->skip_mode_present, | |
| 255 | }, | ||
| 256 | .loop_restoration_fields.bits = { | ||
| 257 | ✗ | .yframe_restoration_type = remap_lr_type[frame_header->lr_type[0]], | |
| 258 | ✗ | .cbframe_restoration_type = remap_lr_type[frame_header->lr_type[1]], | |
| 259 | ✗ | .crframe_restoration_type = remap_lr_type[frame_header->lr_type[2]], | |
| 260 | ✗ | .lr_unit_shift = frame_header->lr_unit_shift, | |
| 261 | ✗ | .lr_uv_shift = frame_header->lr_uv_shift, | |
| 262 | }, | ||
| 263 | .qmatrix_fields.bits = { | ||
| 264 | ✗ | .using_qmatrix = frame_header->using_qmatrix, | |
| 265 | ✗ | .qm_y = frame_header->qm_y, | |
| 266 | ✗ | .qm_u = frame_header->qm_u, | |
| 267 | ✗ | .qm_v = frame_header->qm_v, | |
| 268 | } | ||
| 269 | }; | ||
| 270 | |||
| 271 | ✗ | for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) { | |
| 272 | ✗ | if (pic_param.pic_info_fields.bits.frame_type == AV1_FRAME_KEY && frame_header->show_frame) | |
| 273 | ✗ | pic_param.ref_frame_map[i] = VA_INVALID_ID; | |
| 274 | else | ||
| 275 | ✗ | pic_param.ref_frame_map[i] = ctx->ref_tab[i].valid ? | |
| 276 | ✗ | ff_vaapi_get_surface_id(ctx->ref_tab[i].frame) : | |
| 277 | ✗ | vaapi_av1_surface_id(&s->ref[i]); | |
| 278 | } | ||
| 279 | ✗ | for (int i = 0; i < AV1_REFS_PER_FRAME; i++) { | |
| 280 | ✗ | pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i]; | |
| 281 | } | ||
| 282 | ✗ | for (int i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) { | |
| 283 | ✗ | pic_param.ref_deltas[i] = frame_header->loop_filter_ref_deltas[i]; | |
| 284 | } | ||
| 285 | ✗ | for (int i = 0; i < 2; i++) { | |
| 286 | ✗ | pic_param.mode_deltas[i] = frame_header->loop_filter_mode_deltas[i]; | |
| 287 | } | ||
| 288 | ✗ | for (int i = 0; i < (1 << frame_header->cdef_bits); i++) { | |
| 289 | ✗ | pic_param.cdef_y_strengths[i] = | |
| 290 | ✗ | (frame_header->cdef_y_pri_strength[i] << 2) + | |
| 291 | ✗ | frame_header->cdef_y_sec_strength[i]; | |
| 292 | ✗ | pic_param.cdef_uv_strengths[i] = | |
| 293 | ✗ | (frame_header->cdef_uv_pri_strength[i] << 2) + | |
| 294 | ✗ | frame_header->cdef_uv_sec_strength[i]; | |
| 295 | } | ||
| 296 | ✗ | for (int i = 0; i < frame_header->tile_cols; i++) { | |
| 297 | ✗ | pic_param.width_in_sbs_minus_1[i] = | |
| 298 | ✗ | frame_header->width_in_sbs_minus_1[i]; | |
| 299 | } | ||
| 300 | ✗ | for (int i = 0; i < frame_header->tile_rows; i++) { | |
| 301 | ✗ | pic_param.height_in_sbs_minus_1[i] = | |
| 302 | ✗ | frame_header->height_in_sbs_minus_1[i]; | |
| 303 | } | ||
| 304 | ✗ | for (int i = AV1_REF_FRAME_LAST; i <= AV1_REF_FRAME_ALTREF; i++) { | |
| 305 | ✗ | pic_param.wm[i - 1].invalid = s->cur_frame.gm_invalid[i]; | |
| 306 | ✗ | pic_param.wm[i - 1].wmtype = s->cur_frame.gm_type[i]; | |
| 307 | ✗ | for (int j = 0; j < 6; j++) | |
| 308 | ✗ | pic_param.wm[i - 1].wmmat[j] = s->cur_frame.gm_params[i][j]; | |
| 309 | } | ||
| 310 | ✗ | for (int i = 0; i < AV1_MAX_SEGMENTS; i++) { | |
| 311 | ✗ | for (int j = 0; j < AV1_SEG_LVL_MAX; j++) { | |
| 312 | ✗ | pic_param.seg_info.feature_mask[i] |= (frame_header->feature_enabled[i][j] << j); | |
| 313 | ✗ | if (segmentation_feature_signed[j]) | |
| 314 | ✗ | pic_param.seg_info.feature_data[i][j] = av_clip(frame_header->feature_value[i][j], | |
| 315 | ✗ | -segmentation_feature_max[j], segmentation_feature_max[j]); | |
| 316 | else | ||
| 317 | ✗ | pic_param.seg_info.feature_data[i][j] = av_clip(frame_header->feature_value[i][j], | |
| 318 | ✗ | 0, segmentation_feature_max[j]); | |
| 319 | } | ||
| 320 | } | ||
| 321 | ✗ | if (apply_grain) { | |
| 322 | ✗ | for (int i = 0; i < film_grain->num_y_points; i++) { | |
| 323 | ✗ | pic_param.film_grain_info.point_y_value[i] = | |
| 324 | ✗ | film_grain->point_y_value[i]; | |
| 325 | ✗ | pic_param.film_grain_info.point_y_scaling[i] = | |
| 326 | ✗ | film_grain->point_y_scaling[i]; | |
| 327 | } | ||
| 328 | ✗ | for (int i = 0; i < film_grain->num_cb_points; i++) { | |
| 329 | ✗ | pic_param.film_grain_info.point_cb_value[i] = | |
| 330 | ✗ | film_grain->point_cb_value[i]; | |
| 331 | ✗ | pic_param.film_grain_info.point_cb_scaling[i] = | |
| 332 | ✗ | film_grain->point_cb_scaling[i]; | |
| 333 | } | ||
| 334 | ✗ | for (int i = 0; i < film_grain->num_cr_points; i++) { | |
| 335 | ✗ | pic_param.film_grain_info.point_cr_value[i] = | |
| 336 | ✗ | film_grain->point_cr_value[i]; | |
| 337 | ✗ | pic_param.film_grain_info.point_cr_scaling[i] = | |
| 338 | ✗ | film_grain->point_cr_scaling[i]; | |
| 339 | } | ||
| 340 | ✗ | for (int i = 0; i < 24; i++) { | |
| 341 | ✗ | pic_param.film_grain_info.ar_coeffs_y[i] = | |
| 342 | ✗ | film_grain->ar_coeffs_y_plus_128[i] - 128; | |
| 343 | } | ||
| 344 | ✗ | for (int i = 0; i < 25; i++) { | |
| 345 | ✗ | pic_param.film_grain_info.ar_coeffs_cb[i] = | |
| 346 | ✗ | film_grain->ar_coeffs_cb_plus_128[i] - 128; | |
| 347 | ✗ | pic_param.film_grain_info.ar_coeffs_cr[i] = | |
| 348 | ✗ | film_grain->ar_coeffs_cr_plus_128[i] - 128; | |
| 349 | } | ||
| 350 | } | ||
| 351 | ✗ | err = ff_vaapi_decode_make_param_buffer(avctx, pic, | |
| 352 | VAPictureParameterBufferType, | ||
| 353 | &pic_param, sizeof(pic_param)); | ||
| 354 | ✗ | if (err < 0) | |
| 355 | ✗ | goto fail; | |
| 356 | |||
| 357 | ✗ | return 0; | |
| 358 | |||
| 359 | ✗ | fail: | |
| 360 | ✗ | ff_vaapi_decode_cancel(avctx, pic); | |
| 361 | ✗ | return err; | |
| 362 | } | ||
| 363 | |||
| 364 | ✗ | static int vaapi_av1_end_frame(AVCodecContext *avctx) | |
| 365 | { | ||
| 366 | ✗ | const AV1DecContext *s = avctx->priv_data; | |
| 367 | ✗ | const AV1RawFrameHeader *header = s->raw_frame_header; | |
| 368 | ✗ | const AV1RawFilmGrainParams *film_grain = &s->cur_frame.film_grain; | |
| 369 | ✗ | VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private; | |
| 370 | ✗ | VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 371 | |||
| 372 | ✗ | int apply_grain = !(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) && film_grain->apply_grain; | |
| 373 | int ret; | ||
| 374 | ✗ | ret = ff_vaapi_decode_issue(avctx, pic); | |
| 375 | ✗ | if (ret < 0) | |
| 376 | ✗ | return ret; | |
| 377 | |||
| 378 | ✗ | for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) { | |
| 379 | ✗ | if (header->refresh_frame_flags & (1 << i)) { | |
| 380 | ✗ | if (ctx->ref_tab[i].frame->buf[0]) | |
| 381 | ✗ | av_frame_unref(ctx->ref_tab[i].frame); | |
| 382 | |||
| 383 | ✗ | if (apply_grain) { | |
| 384 | ✗ | ret = av_frame_ref(ctx->ref_tab[i].frame, ctx->tmp_frame); | |
| 385 | ✗ | if (ret < 0) | |
| 386 | ✗ | return ret; | |
| 387 | ✗ | ctx->ref_tab[i].valid = 1; | |
| 388 | } else { | ||
| 389 | ✗ | ctx->ref_tab[i].valid = 0; | |
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | ✗ | return 0; | |
| 395 | } | ||
| 396 | |||
| 397 | ✗ | static int vaapi_av1_decode_slice(AVCodecContext *avctx, | |
| 398 | const uint8_t *buffer, | ||
| 399 | uint32_t size) | ||
| 400 | { | ||
| 401 | ✗ | const AV1DecContext *s = avctx->priv_data; | |
| 402 | ✗ | VAAPIDecodePicture *pic = s->cur_frame.hwaccel_picture_private; | |
| 403 | ✗ | VAAPIAV1DecContext *ctx = avctx->internal->hwaccel_priv_data; | |
| 404 | int err, nb_params; | ||
| 405 | |||
| 406 | ✗ | nb_params = s->tg_end - s->tg_start + 1; | |
| 407 | ✗ | if (ctx->nb_slice_params < nb_params) { | |
| 408 | ✗ | VASliceParameterBufferAV1 *tmp = av_realloc_array(ctx->slice_params, | |
| 409 | nb_params, | ||
| 410 | sizeof(*ctx->slice_params)); | ||
| 411 | ✗ | if (!tmp) { | |
| 412 | ✗ | ctx->nb_slice_params = 0; | |
| 413 | ✗ | err = AVERROR(ENOMEM); | |
| 414 | ✗ | goto fail; | |
| 415 | } | ||
| 416 | ✗ | ctx->slice_params = tmp; | |
| 417 | ✗ | ctx->nb_slice_params = nb_params; | |
| 418 | } | ||
| 419 | |||
| 420 | ✗ | for (int i = s->tg_start; i <= s->tg_end; i++) { | |
| 421 | ✗ | ctx->slice_params[i - s->tg_start] = (VASliceParameterBufferAV1) { | |
| 422 | ✗ | .slice_data_size = s->tile_group_info[i].tile_size, | |
| 423 | ✗ | .slice_data_offset = s->tile_group_info[i].tile_offset, | |
| 424 | .slice_data_flag = VA_SLICE_DATA_FLAG_ALL, | ||
| 425 | ✗ | .tile_row = s->tile_group_info[i].tile_row, | |
| 426 | ✗ | .tile_column = s->tile_group_info[i].tile_column, | |
| 427 | ✗ | .tg_start = s->tg_start, | |
| 428 | ✗ | .tg_end = s->tg_end, | |
| 429 | }; | ||
| 430 | } | ||
| 431 | |||
| 432 | ✗ | err = ff_vaapi_decode_make_slice_buffer(avctx, pic, ctx->slice_params, nb_params, | |
| 433 | sizeof(VASliceParameterBufferAV1), | ||
| 434 | buffer, | ||
| 435 | size); | ||
| 436 | ✗ | if (err) | |
| 437 | ✗ | goto fail; | |
| 438 | |||
| 439 | ✗ | return 0; | |
| 440 | |||
| 441 | ✗ | fail: | |
| 442 | ✗ | ff_vaapi_decode_cancel(avctx, pic); | |
| 443 | ✗ | return err; | |
| 444 | } | ||
| 445 | |||
| 446 | const FFHWAccel ff_av1_vaapi_hwaccel = { | ||
| 447 | .p.name = "av1_vaapi", | ||
| 448 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 449 | .p.id = AV_CODEC_ID_AV1, | ||
| 450 | .p.pix_fmt = AV_PIX_FMT_VAAPI, | ||
| 451 | .start_frame = vaapi_av1_start_frame, | ||
| 452 | .end_frame = vaapi_av1_end_frame, | ||
| 453 | .decode_slice = vaapi_av1_decode_slice, | ||
| 454 | .frame_priv_data_size = sizeof(VAAPIDecodePicture), | ||
| 455 | .init = vaapi_av1_decode_init, | ||
| 456 | .uninit = vaapi_av1_decode_uninit, | ||
| 457 | .frame_params = ff_vaapi_common_frame_params, | ||
| 458 | .priv_data_size = sizeof(VAAPIAV1DecContext), | ||
| 459 | .caps_internal = HWACCEL_CAP_ASYNC_SAFE, | ||
| 460 | }; | ||
| 461 |