| @@ -0,0 +1,214 @@ |
| 1 | +/* |
| 2 | + * Direct3D 12 AV1 HW acceleration |
| 3 | + * |
| 4 | + * copyright (c) 2022-2023 Wu Jianhua <toqsxw@outlook.com> |
| 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 | +#include "libavutil/avassert.h" |
| 25 | +#include "libavutil/hwcontext_d3d12va_internal.h" |
| 26 | +#include "av1dec.h" |
| 27 | +#include "dxva2_internal.h" |
| 28 | +#include "d3d12va_decode.h" |
| 29 | + |
| 30 | +#define MAX_TILES 256 |
| 31 | + |
| 32 | +typedef struct D3D12AV1DecodeContext { |
| 33 | + D3D12VADecodeContext ctx; |
| 34 | + uint8_t *bitstream_buffer; |
| 35 | +} D3D12AV1DecodeContext; |
| 36 | + |
| 37 | +#define D3D12_AV1_DECODE_CONTEXT(avctx) ((D3D12AV1DecodeContext *)D3D12VA_DECODE_CONTEXT(avctx)) |
| 38 | + |
| 39 | +typedef struct AV1DecodePictureContext { |
| 40 | + DXVA_PicParams_AV1 pp; |
| 41 | + unsigned tile_count; |
| 42 | + DXVA_Tile_AV1 tiles[MAX_TILES]; |
| 43 | + uint8_t *bitstream; |
| 44 | + unsigned bitstream_size; |
| 45 | +} AV1DecodePictureContext; |
| 46 | + |
| 47 | +static int d3d12va_av1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size) |
| 48 | +{ |
| 49 | + const AV1DecContext *h = avctx->priv_data; |
| 50 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 51 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 52 | + if (!ctx) |
| 53 | + return -1; |
| 54 | + |
| 55 | + av_assert0(ctx_pic); |
| 56 | + |
| 57 | + ctx->used_mask = 0; |
| 58 | + |
| 59 | + if (ff_dxva2_av1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0) |
| 60 | + return -1; |
| 61 | + |
| 62 | + ctx_pic->bitstream = NULL; |
| 63 | + ctx_pic->bitstream_size = 0; |
| 64 | + ctx_pic->tile_count = 0; |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static int d3d12va_av1_decode_slice(AVCodecContext *avctx, |
| 70 | + const uint8_t *buffer, |
| 71 | + uint32_t size) |
| 72 | +{ |
| 73 | + const AV1DecContext *h = avctx->priv_data; |
| 74 | + const AV1RawFrameHeader *frame_header = h->raw_frame_header; |
| 75 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 76 | + int offset = 0; |
| 77 | + uint32_t tg_start, tg_end; |
| 78 | + |
| 79 | + ctx_pic->tile_count = frame_header->tile_cols * frame_header->tile_rows; |
| 80 | + |
| 81 | + if (ctx_pic->tile_count > MAX_TILES) |
| 82 | + return AVERROR(ENOSYS); |
| 83 | + |
| 84 | + if (ctx_pic->tile_count == h->tg_end - h->tg_start + 1) { |
| 85 | + tg_start = 0; |
| 86 | + tg_end = ctx_pic->tile_count - 1; |
| 87 | + ctx_pic->bitstream = (uint8_t *)buffer; |
| 88 | + ctx_pic->bitstream_size = size; |
| 89 | + } else { |
| 90 | + ctx_pic->bitstream = D3D12_AV1_DECODE_CONTEXT(avctx)->bitstream_buffer; |
| 91 | + memcpy(ctx_pic->bitstream + ctx_pic->bitstream_size, buffer, size); |
| 92 | + tg_start = h->tg_start; |
| 93 | + tg_end = h->tg_end; |
| 94 | + offset = ctx_pic->bitstream_size; |
| 95 | + ctx_pic->bitstream_size += size; |
| 96 | + } |
| 97 | + |
| 98 | + for (uint32_t tile_num = tg_start; tile_num <= tg_end; tile_num++) { |
| 99 | + ctx_pic->tiles[tile_num].DataOffset = offset + h->tile_group_info[tile_num].tile_offset; |
| 100 | + ctx_pic->tiles[tile_num].DataSize = h->tile_group_info[tile_num].tile_size; |
| 101 | + ctx_pic->tiles[tile_num].row = h->tile_group_info[tile_num].tile_row; |
| 102 | + ctx_pic->tiles[tile_num].column = h->tile_group_info[tile_num].tile_column; |
| 103 | + ctx_pic->tiles[tile_num].anchor_frame = 0xFF; |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer) |
| 110 | +{ |
| 111 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 112 | + AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx); |
| 113 | + AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx; |
| 114 | + const AV1DecContext *h = avctx->priv_data; |
| 115 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 116 | + uint8_t *mapped_data; |
| 117 | + |
| 118 | + D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++]; |
| 119 | + args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL; |
| 120 | + args->Size = sizeof(DXVA_Tile_AV1) * ctx_pic->tile_count; |
| 121 | + args->pData = ctx_pic->tiles; |
| 122 | + |
| 123 | + input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){ |
| 124 | + .pBuffer = buffer, |
| 125 | + .Offset = 0, |
| 126 | + .Size = ctx_pic->bitstream_size, |
| 127 | + }; |
| 128 | + |
| 129 | + if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) { |
| 130 | + av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n"); |
| 131 | + return AVERROR(EINVAL); |
| 132 | + } |
| 133 | + |
| 134 | + memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size); |
| 135 | + |
| 136 | + ID3D12Resource_Unmap(buffer, 0, NULL); |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +static int d3d12va_av1_end_frame(AVCodecContext *avctx) |
| 142 | +{ |
| 143 | + int ret; |
| 144 | + const AV1DecContext *h = avctx->priv_data; |
| 145 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 146 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 147 | + |
| 148 | + if (ctx_pic->tiles <= 0 || ctx_pic->bitstream_size <= 0) |
| 149 | + return -1; |
| 150 | + |
| 151 | + ret = ff_d3d12va_common_end_frame(avctx, h->cur_frame.f, &ctx_pic->pp, sizeof(ctx_pic->pp), |
| 152 | + NULL, 0, update_input_arguments); |
| 153 | + |
| 154 | + return ret; |
| 155 | +} |
| 156 | + |
| 157 | +static int d3d12va_av1_decode_init(AVCodecContext *avctx) |
| 158 | +{ |
| 159 | + const AV1DecContext *h = avctx->priv_data; |
| 160 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 161 | + D3D12AV1DecodeContext *av1_ctx = D3D12_AV1_DECODE_CONTEXT(avctx); |
| 162 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 163 | + DXVA_PicParams_AV1 pp; |
| 164 | + |
| 165 | + int ret; |
| 166 | + |
| 167 | + if (avctx->profile != FF_PROFILE_AV1_MAIN) |
| 168 | + return AVERROR(EINVAL); |
| 169 | + |
| 170 | + ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0; |
| 171 | + |
| 172 | + ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameMapTextureIndex) + 1; |
| 173 | + |
| 174 | + ret = ff_d3d12va_decode_init(avctx); |
| 175 | + if (ret < 0) |
| 176 | + return ret; |
| 177 | + |
| 178 | + if (!av1_ctx->bitstream_buffer) { |
| 179 | + av1_ctx->bitstream_buffer = av_malloc(ff_d3d12va_get_suitable_max_bitstream_size(avctx)); |
| 180 | + if (!av1_ctx->bitstream_buffer) |
| 181 | + return AVERROR(ENOMEM); |
| 182 | + } |
| 183 | + |
| 184 | + return 0; |
| 185 | +} |
| 186 | + |
| 187 | +static int d3d12va_av1_decode_uninit(AVCodecContext *avctx) |
| 188 | +{ |
| 189 | + const AV1DecContext *h = avctx->priv_data; |
| 190 | + D3D12AV1DecodeContext *ctx = D3D12_AV1_DECODE_CONTEXT(avctx); |
| 191 | + AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private; |
| 192 | + |
| 193 | + if (ctx->bitstream_buffer) |
| 194 | + av_freep(&ctx->bitstream_buffer); |
| 195 | + |
| 196 | + return ff_d3d12va_decode_uninit(avctx); |
| 197 | +} |
| 198 | + |
| 199 | +#if CONFIG_AV1_D3D12VA_HWACCEL |
| 200 | +const FFHWAccel ff_av1_d3d12va_hwaccel = { |
| 201 | + .p.name = "av1_d3d12va", |
| 202 | + .p.type = AVMEDIA_TYPE_VIDEO, |
| 203 | + .p.id = AV_CODEC_ID_AV1, |
| 204 | + .p.pix_fmt = AV_PIX_FMT_D3D12, |
| 205 | + .init = d3d12va_av1_decode_init, |
| 206 | + .uninit = d3d12va_av1_decode_uninit, |
| 207 | + .start_frame = d3d12va_av1_start_frame, |
| 208 | + .decode_slice = d3d12va_av1_decode_slice, |
| 209 | + .end_frame = d3d12va_av1_end_frame, |
| 210 | + .frame_params = ff_d3d12va_common_frame_params, |
| 211 | + .frame_priv_data_size = sizeof(AV1DecodePictureContext), |
| 212 | + .priv_data_size = sizeof(D3D12AV1DecodeContext), |
| 213 | +}; |
| 214 | +#endif |