| @@ -0,0 +1,214 @@ |
| 1 | +/* |
| 2 | + * Direct3D12 WMV3/VC-1 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 "mpegutils.h" |
| 27 | +#include "mpegvideodec.h" |
| 28 | +#include "vc1.h" |
| 29 | +#include "vc1data.h" |
| 30 | +#include "d3d12va_decode.h" |
| 31 | +#include "dxva2_internal.h" |
| 32 | + |
| 33 | +#define MAX_SLICES 1024 |
| 34 | +#define INVALID_REF 0xffff |
| 35 | + |
| 36 | +#define REF_RESOURCE(index) if (index != INVALID_REF) { \ |
| 37 | + ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture; \ |
| 38 | +} |
| 39 | + |
| 40 | +typedef struct D3D12DecodePictureContext { |
| 41 | + DXVA_PictureParameters pp; |
| 42 | + unsigned slice_count; |
| 43 | + DXVA_SliceInfo slices[MAX_SLICES]; |
| 44 | + const uint8_t *bitstream; |
| 45 | + unsigned bitstream_size; |
| 46 | +} D3D12DecodePictureContext; |
| 47 | + |
| 48 | +static int d3d12va_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size) |
| 49 | +{ |
| 50 | + const VC1Context *v = avctx->priv_data; |
| 51 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 52 | + D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private; |
| 53 | + |
| 54 | + if (!ctx) |
| 55 | + return -1; |
| 56 | + |
| 57 | + av_assert0(ctx_pic); |
| 58 | + |
| 59 | + ctx->used_mask = 0; |
| 60 | + |
| 61 | + ff_dxva2_vc1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp); |
| 62 | + ctx_pic->pp.wDeblockedPictureIndex = INVALID_REF; |
| 63 | + |
| 64 | + ctx_pic->bitstream = NULL; |
| 65 | + ctx_pic->bitstream_size = 0; |
| 66 | + ctx_pic->slice_count = 0; |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static int d3d12va_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) |
| 72 | +{ |
| 73 | + const VC1Context *v = avctx->priv_data; |
| 74 | + D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private; |
| 75 | + |
| 76 | + if (ctx_pic->slice_count >= MAX_SLICES) { |
| 77 | + return AVERROR(ERANGE); |
| 78 | + } |
| 79 | + |
| 80 | + if (avctx->codec_id == AV_CODEC_ID_VC1 && |
| 81 | + size >= 4 && IS_MARKER(AV_RB32(buffer))) { |
| 82 | + buffer += 4; |
| 83 | + size -= 4; |
| 84 | + } |
| 85 | + |
| 86 | + if (!ctx_pic->bitstream) |
| 87 | + ctx_pic->bitstream = buffer; |
| 88 | + ctx_pic->bitstream_size += size; |
| 89 | + |
| 90 | + ff_dxva2_vc1_fill_slice(avctx, &ctx_pic->slices[ctx_pic->slice_count++], |
| 91 | + buffer - ctx_pic->bitstream, size); |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer) |
| 97 | +{ |
| 98 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 99 | + AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx); |
| 100 | + AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx; |
| 101 | + const VC1Context *v = avctx->priv_data; |
| 102 | + const MpegEncContext *s = &v->s; |
| 103 | + D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private; |
| 104 | + D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++]; |
| 105 | + |
| 106 | + const unsigned mb_count = s->mb_width * (s->mb_height >> v->field_mode); |
| 107 | + uint8_t *mapped_data, *mapped_ptr; |
| 108 | + |
| 109 | + static const uint8_t start_code[] = { 0, 0, 1, 0x0d }; |
| 110 | + |
| 111 | + if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) { |
| 112 | + av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n"); |
| 113 | + return AVERROR(EINVAL); |
| 114 | + } |
| 115 | + |
| 116 | + mapped_ptr = mapped_data; |
| 117 | + for (int i = 0; i < ctx_pic->slice_count; i++) { |
| 118 | + DXVA_SliceInfo *slice = &ctx_pic->slices[i]; |
| 119 | + unsigned position = slice->dwSliceDataLocation; |
| 120 | + unsigned size = slice->dwSliceBitsInBuffer / 8; |
| 121 | + |
| 122 | + slice->dwSliceDataLocation = mapped_ptr - mapped_data; |
| 123 | + if (i < ctx_pic->slice_count - 1) |
| 124 | + slice->wNumberMBsInSlice = slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice; |
| 125 | + else |
| 126 | + slice->wNumberMBsInSlice = mb_count - slice[0].wNumberMBsInSlice; |
| 127 | + |
| 128 | + if (avctx->codec_id == AV_CODEC_ID_VC1) { |
| 129 | + memcpy(mapped_ptr, start_code, sizeof(start_code)); |
| 130 | + if (i == 0 && v->second_field) |
| 131 | + mapped_ptr[3] = 0x0c; |
| 132 | + else if (i > 0) |
| 133 | + mapped_ptr[3] = 0x0b; |
| 134 | + |
| 135 | + mapped_ptr += sizeof(start_code); |
| 136 | + slice->dwSliceBitsInBuffer += sizeof(start_code) * 8; |
| 137 | + } |
| 138 | + |
| 139 | + memcpy(mapped_ptr, &ctx_pic->bitstream[position], size); |
| 140 | + mapped_ptr += size; |
| 141 | + } |
| 142 | + |
| 143 | + ID3D12Resource_Unmap(buffer, 0, NULL); |
| 144 | + |
| 145 | + args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL; |
| 146 | + args->Size = sizeof(DXVA_SliceInfo) * ctx_pic->slice_count; |
| 147 | + args->pData = ctx_pic->slices; |
| 148 | + |
| 149 | + input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){ |
| 150 | + .pBuffer = buffer, |
| 151 | + .Offset = 0, |
| 152 | + .Size = mapped_ptr - mapped_data, |
| 153 | + }; |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +static int d3d12va_vc1_end_frame(AVCodecContext *avctx) |
| 159 | +{ |
| 160 | + const VC1Context *v = avctx->priv_data; |
| 161 | + D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private; |
| 162 | + |
| 163 | + if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0) |
| 164 | + return -1; |
| 165 | + |
| 166 | + return ff_d3d12va_common_end_frame(avctx, v->s.current_picture_ptr->f, |
| 167 | + &ctx_pic->pp, sizeof(ctx_pic->pp), |
| 168 | + NULL, 0, |
| 169 | + update_input_arguments); |
| 170 | +} |
| 171 | + |
| 172 | +static int d3d12va_vc1_decode_init(AVCodecContext *avctx) |
| 173 | +{ |
| 174 | + D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx); |
| 175 | + ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VC1; |
| 176 | + |
| 177 | + ctx->max_num_ref = 3; |
| 178 | + |
| 179 | + return ff_d3d12va_decode_init(avctx); |
| 180 | +} |
| 181 | + |
| 182 | +#if CONFIG_WMV3_D3D12VA_HWACCEL |
| 183 | +const FFHWAccel ff_wmv3_d3d12va_hwaccel = { |
| 184 | + .p.name = "wmv3_d3d12va", |
| 185 | + .p.type = AVMEDIA_TYPE_VIDEO, |
| 186 | + .p.id = AV_CODEC_ID_WMV3, |
| 187 | + .p.pix_fmt = AV_PIX_FMT_D3D12, |
| 188 | + .init = d3d12va_vc1_decode_init, |
| 189 | + .uninit = ff_d3d12va_decode_uninit, |
| 190 | + .start_frame = d3d12va_vc1_start_frame, |
| 191 | + .decode_slice = d3d12va_vc1_decode_slice, |
| 192 | + .end_frame = d3d12va_vc1_end_frame, |
| 193 | + .frame_params = ff_d3d12va_common_frame_params, |
| 194 | + .frame_priv_data_size = sizeof(D3D12DecodePictureContext), |
| 195 | + .priv_data_size = sizeof(D3D12VADecodeContext), |
| 196 | +}; |
| 197 | +#endif |
| 198 | + |
| 199 | +#if CONFIG_VC1_D3D12VA_HWACCEL |
| 200 | +const FFHWAccel ff_vc1_d3d12va_hwaccel = { |
| 201 | + .p.name = "vc1_d3d12va", |
| 202 | + .p.type = AVMEDIA_TYPE_VIDEO, |
| 203 | + .p.id = AV_CODEC_ID_VC1, |
| 204 | + .p.pix_fmt = AV_PIX_FMT_D3D12, |
| 205 | + .init = d3d12va_vc1_decode_init, |
| 206 | + .uninit = ff_d3d12va_decode_uninit, |
| 207 | + .start_frame = d3d12va_vc1_start_frame, |
| 208 | + .decode_slice = d3d12va_vc1_decode_slice, |
| 209 | + .end_frame = d3d12va_vc1_end_frame, |
| 210 | + .frame_params = ff_d3d12va_common_frame_params, |
| 211 | + .frame_priv_data_size = sizeof(D3D12DecodePictureContext), |
| 212 | + .priv_data_size = sizeof(D3D12VADecodeContext), |
| 213 | +}; |
| 214 | +#endif |