1 /*
2 * DXVA2 AV1 HW acceleration.
3 *
4 * copyright (c) 2020 Hendrik Leppkes
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
28
32
34
37
40 };
41
43 DXVA_PicParams_AV1
pp;
48 };
49
51 {
55 return 10;
56 else
57 return 8;
58 }
59
61 DXVA_PicParams_AV1 *pp)
62 {
68
71
72 memset(pp, 0, sizeof(*pp));
73
74 pp->width = avctx->
width;
75 pp->height = avctx->
height;
76
79
83
84 /* Tiling info */
87 pp->tiles.context_update_id =
frame_header->context_update_tile_id;
88
89 for (
i = 0;
i < pp->tiles.cols;
i++)
91
92 for (
i = 0;
i < pp->tiles.rows;
i++)
94
95 /* Coding tools */
100 pp->coding.warped_motion =
frame_header->allow_warped_motion;
103 pp->coding.screen_content_tools =
frame_header->allow_screen_content_tools;
104 pp->coding.integer_mv =
h->cur_frame.force_integer_mv;
109 pp->coding.high_precision_mv =
frame_header->allow_high_precision_mv;
110 pp->coding.switchable_motion_mode =
frame_header->is_motion_mode_switchable;
112 pp->coding.disable_frame_end_update_cdf =
frame_header->disable_frame_end_update_cdf;
113 pp->coding.disable_cdf_update =
frame_header->disable_cdf_update;
114 pp->coding.reference_mode =
frame_header->reference_select;
116 pp->coding.reduced_tx_set =
frame_header->reduced_tx_set;
119 pp->coding.use_ref_frame_mvs =
frame_header->use_ref_frame_mvs;
121 pp->coding.reference_frame_update = 1; // 0 for show_existing_frame with key frames, but those are not passed to the hwaccel
122
123 /* Format & Picture Info flags */
126 pp->format.showable_frame =
frame_header->showable_frame;
130
131 /* References */
132 pp->primary_ref_frame =
frame_header->primary_ref_frame;
135
136 memset(pp->RefFrameMapTextureIndex, 0xFF, sizeof(pp->RefFrameMapTextureIndex));
140
143 pp->frame_refs[
i].Index =
ref_frame ? ref_idx : 0xFF;
144
145 /* Global Motion */
148 for (j = 0; j < 6; ++j) {
150 }
151 }
156 }
157
159
160 /* Loop filter parameters */
161 pp->loop_filter.filter_level[0] =
frame_header->loop_filter_level[0];
162 pp->loop_filter.filter_level[1] =
frame_header->loop_filter_level[1];
163 pp->loop_filter.filter_level_u =
frame_header->loop_filter_level[2];
164 pp->loop_filter.filter_level_v =
frame_header->loop_filter_level[3];
165 pp->loop_filter.sharpness_level =
frame_header->loop_filter_sharpness;
166 pp->loop_filter.mode_ref_delta_enabled =
frame_header->loop_filter_delta_enabled;
167 pp->loop_filter.mode_ref_delta_update =
frame_header->loop_filter_delta_update;
168 pp->loop_filter.delta_lf_multi =
frame_header->delta_lf_multi;
169 pp->loop_filter.delta_lf_present =
frame_header->delta_lf_present;
170 pp->loop_filter.delta_lf_res =
frame_header->delta_lf_res;
171
173 pp->loop_filter.ref_deltas[
i] =
frame_header->loop_filter_ref_deltas[
i];
174 }
175
176 pp->loop_filter.mode_deltas[0] =
frame_header->loop_filter_mode_deltas[0];
177 pp->loop_filter.mode_deltas[1] =
frame_header->loop_filter_mode_deltas[1];
178 pp->loop_filter.frame_restoration_type[0] = remap_lr_type[
frame_header->lr_type[0]];
179 pp->loop_filter.frame_restoration_type[1] = remap_lr_type[
frame_header->lr_type[1]];
180 pp->loop_filter.frame_restoration_type[2] = remap_lr_type[
frame_header->lr_type[2]];
182 pp->loop_filter.log2_restoration_unit_size[0] = uses_lr ? (6 +
frame_header->lr_unit_shift) : 8;
183 pp->loop_filter.log2_restoration_unit_size[1] = uses_lr ? (6 +
frame_header->lr_unit_shift -
frame_header->lr_uv_shift) : 8;
184 pp->loop_filter.log2_restoration_unit_size[2] = uses_lr ? (6 +
frame_header->lr_unit_shift -
frame_header->lr_uv_shift) : 8;
185
186 /* Quantization */
187 pp->quantization.delta_q_present =
frame_header->delta_q_present;
188 pp->quantization.delta_q_res =
frame_header->delta_q_res;
189 pp->quantization.base_qindex =
frame_header->base_q_idx;
190 pp->quantization.y_dc_delta_q =
frame_header->delta_q_y_dc;
191 pp->quantization.u_dc_delta_q =
frame_header->delta_q_u_dc;
192 pp->quantization.v_dc_delta_q =
frame_header->delta_q_v_dc;
193 pp->quantization.u_ac_delta_q =
frame_header->delta_q_u_ac;
194 pp->quantization.v_ac_delta_q =
frame_header->delta_q_v_ac;
198
199 /* Cdef parameters */
202 for (
i = 0;
i < 8;
i++) {
203 pp->cdef.y_strengths[
i].primary =
frame_header->cdef_y_pri_strength[
i];
204 pp->cdef.y_strengths[
i].secondary =
frame_header->cdef_y_sec_strength[
i];
205 pp->cdef.uv_strengths[
i].primary =
frame_header->cdef_uv_pri_strength[
i];
206 pp->cdef.uv_strengths[
i].secondary =
frame_header->cdef_uv_sec_strength[
i];
207 }
208
209 /* Misc flags */
211
212 /* Segmentation */
213 pp->segmentation.enabled =
frame_header->segmentation_enabled;
214 pp->segmentation.update_map =
frame_header->segmentation_update_map;
215 pp->segmentation.update_data =
frame_header->segmentation_update_data;
216 pp->segmentation.temporal_update =
frame_header->segmentation_temporal_update;
219 pp->segmentation.feature_mask[
i].mask |=
frame_header->feature_enabled[
i][j] << j;
220 pp->segmentation.feature_data[
i][j] =
frame_header->feature_value[
i][j];
221 }
222 }
223
224 /* Film grain */
225 if (apply_grain) {
226 pp->film_grain.apply_grain = 1;
235
236 pp->film_grain.grain_seed = film_grain->
grain_seed;
241 }
246 }
251 }
252 for (
i = 0;
i < 24;
i++) {
254 }
255 for (
i = 0;
i < 25;
i++) {
258 }
259 pp->film_grain.cb_mult = film_grain->
cb_mult;
261 pp->film_grain.cr_mult = film_grain->
cr_mult;
263 pp->film_grain.cb_offset = film_grain->
cb_offset;
264 pp->film_grain.cr_offset = film_grain->
cr_offset;
265 pp->film_grain.cr_offset = film_grain->
cr_offset;
266 }
267
268 // XXX: Setting the StatusReportFeedbackNumber breaks decoding on some drivers (tested on NVIDIA 457.09)
269 // Status Reporting is not used by FFmpeg, hence not providing a number does not cause any issues
270 //pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
271 return 0;
272 }
273
278 {
282
284 return -1;
286
287 /* Fill up DXVA_PicParams_AV1 */
289 return -1;
290
293 return 0;
294 }
295
299 {
305
307
308 /* too many tiles, exceeding all defined levels in the AV1 spec */
311
312 /* Shortcut if all tiles are in the same buffer */
313 if (ctx_pic->
tile_count ==
h->tg_end -
h->tg_start + 1) {
316
317 for (uint32_t tile_num = 0; tile_num < ctx_pic->
tile_count; tile_num++) {
318 ctx_pic->
tiles[tile_num].DataOffset =
h->tile_group_info[tile_num].tile_offset;
319 ctx_pic->
tiles[tile_num].DataSize =
h->tile_group_info[tile_num].tile_size;
320 ctx_pic->
tiles[tile_num].row =
h->tile_group_info[tile_num].tile_row;
321 ctx_pic->
tiles[tile_num].column =
h->tile_group_info[tile_num].tile_column;
322 ctx_pic->
tiles[tile_num].anchor_frame = 0xFF;
323 }
324
325 return 0;
326 }
327
328 /* allocate an internal buffer */
333 }
335
337
338 for (uint32_t tile_num =
h->tg_start; tile_num <= h->tg_end; tile_num++) {
339 ctx_pic->
tiles[tile_num].DataOffset = ctx_pic->
bitstream_size +
h->tile_group_info[tile_num].tile_offset;
340 ctx_pic->
tiles[tile_num].DataSize =
h->tile_group_info[tile_num].tile_size;
341 ctx_pic->
tiles[tile_num].row =
h->tile_group_info[tile_num].tile_row;
342 ctx_pic->
tiles[tile_num].column =
h->tile_group_info[tile_num].tile_column;
343 ctx_pic->
tiles[tile_num].anchor_frame = 0xFF;
344 }
345
347
348 return 0;
349 }
350
354 {
358 void *dxva_data_ptr =
NULL;
359 uint8_t *dxva_data;
360 unsigned dxva_size;
361 unsigned padding;
363
364 #if CONFIG_D3D11VA
366 type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
370 &dxva_size, &dxva_data_ptr)))
371 return -1;
372 }
373 #endif
374 #if CONFIG_DXVA2
376 type = DXVA2_BitStreamDateBufferType;
379 &dxva_data_ptr, &dxva_size)))
380 return -1;
381 }
382 #endif
383
384 dxva_data = dxva_data_ptr;
385
388 return -1;
389 }
390
392
394 if (padding > 0) {
397 }
398
399 #if CONFIG_D3D11VA
402 return -1;
403 #endif
404 #if CONFIG_DXVA2
407 return -1;
408 #endif
409
410 #if CONFIG_D3D11VA
412 D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
413 memset(dsc11, 0, sizeof(*dsc11));
414 dsc11->BufferType =
type;
416 dsc11->NumMBsInBuffer = 0;
417
418 type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
419 }
420 #endif
421 #if CONFIG_DXVA2
423 DXVA2_DecodeBufferDesc *dsc2 = bs;
424 memset(dsc2, 0, sizeof(*dsc2));
425 dsc2->CompressedBufferType =
type;
427 dsc2->NumMBsInBuffer = 0;
428
429 type = DXVA2_SliceControlBufferType;
430 }
431 #endif
432
435 }
436
438 {
442
444 return -1;
445
447 &ctx_pic->
pp,
sizeof(ctx_pic->
pp),
450
452 }
453
455 {
457
459 ctx->bitstream_allocated = 0;
460
462 }
463
464 #if CONFIG_AV1_DXVA2_HWACCEL
466 .
p.
name =
"av1_dxva2",
478 };
479 #endif
480
481 #if CONFIG_AV1_D3D11VA_HWACCEL
483 .
p.
name =
"av1_d3d11va",
495 };
496 #endif
497
498 #if CONFIG_AV1_D3D11VA2_HWACCEL
500 .
p.
name =
"av1_d3d11va2",
512 };
513 #endif