summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/qffmpeghwaccel_d3d11.cpp
blob: 9fec00d928ce12e35198039ad2b1e4c18ad472e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qffmpeghwaccel_d3d11_p.h"

#include <qvideoframeformat.h>
#include "qffmpegvideobuffer_p.h"


#include <private/qvideotexturehelper_p.h>
#include <private/qwindowsiupointer_p.h>
#include <private/qrhi_p.h>
#include <private/qrhid3d11_p.h>

#include <qopenglfunctions.h>
#include <qdebug.h>
#include <qloggingcategory.h>

#include <libavutil/hwcontext_d3d11va.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcMediaFFmpegHWAccel, "qt.multimedia.hwaccel")

namespace QFFmpeg {

class D3D11TextureSet : public TextureSet
{
public:
    D3D11TextureSet(QRhi *rhi, QVideoFrameFormat::PixelFormat format, QWindowsIUPointer<ID3D11Texture2D> &&tex)
        : m_rhi(rhi)
        , m_format(format)
        , m_tex(tex)
    {}

    std::unique_ptr<QRhiTexture> texture(int plane) override {
        auto desc = QVideoTextureHelper::textureDescription(m_format);
        if (!m_tex || !m_rhi || !desc || plane >= desc->nplanes)
            return {};

        D3D11_TEXTURE2D_DESC d3d11desc = {};
        m_tex->GetDesc(&d3d11desc);

        QSize planeSize(desc->widthForPlane(int(d3d11desc.Width), plane),
                        desc->heightForPlane(int(d3d11desc.Height), plane));

        std::unique_ptr<QRhiTexture> tex(m_rhi->newTextureArray(desc->textureFormat[plane],
                                                                int(d3d11desc.ArraySize),
                                                                planeSize, 1, {}));
        if (tex) {
            if (!tex->createFrom({quint64(m_tex.get()), 0}))
                tex.reset();
        }
        return tex;
    }

private:
    QRhi *m_rhi = nullptr;
    QVideoFrameFormat::PixelFormat m_format;
    QWindowsIUPointer<ID3D11Texture2D> m_tex;
};


D3D11TextureConverter::D3D11TextureConverter(QRhi *rhi)
    : TextureConverterBackend(rhi)
{
}

static QWindowsIUPointer<ID3D11Texture2D> getSharedTextureForDevice(ID3D11Device *dev, ID3D11Texture2D *tex)
{
    QWindowsIUPointer<IDXGIResource> dxgiResource;
    HRESULT hr = tex->QueryInterface(__uuidof(IDXGIResource), reinterpret_cast<void **>(dxgiResource.address()));
    if (FAILED(hr)) {
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to obtain resource handle from FFMpeg texture" << hr;
        return {};
    }
    HANDLE shared = nullptr;
    hr = dxgiResource->GetSharedHandle(&shared);
    if (FAILED(hr)) {
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to obtain shared handle for FFmpeg texture" << hr;
        return {};
    }

    QWindowsIUPointer<ID3D11Texture2D> sharedTex;
    hr = dev->OpenSharedResource(shared, __uuidof(ID3D11Texture2D), reinterpret_cast<void **>(sharedTex.address()));
    if (FAILED(hr))
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to share FFmpeg texture" << hr;
    return sharedTex;
}

static QWindowsIUPointer<ID3D11Texture2D> copyTextureFromArray(ID3D11Device *dev, ID3D11Texture2D *array, int index)
{
    D3D11_TEXTURE2D_DESC arrayDesc = {};
    array->GetDesc(&arrayDesc);

    D3D11_TEXTURE2D_DESC texDesc = {};
    texDesc.Width = arrayDesc.Width;
    texDesc.Height = arrayDesc.Height;
    texDesc.Format = arrayDesc.Format;
    texDesc.ArraySize = 1;
    texDesc.MipLevels = 1;
    texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    texDesc.MiscFlags = 0;
    texDesc.SampleDesc = { 1, 0};

    QWindowsIUPointer<ID3D11Texture2D> texCopy;
    HRESULT hr = dev->CreateTexture2D(&texDesc, nullptr, texCopy.address());
    if (FAILED(hr)) {
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to create texture" << hr;
        return {};
    }

    QWindowsIUPointer<ID3D11DeviceContext> ctx;
    dev->GetImmediateContext(ctx.address());
    ctx->CopySubresourceRegion(texCopy.get(), 0, 0, 0, 0, array, index, nullptr);

    return texCopy;
}

TextureSet *D3D11TextureConverter::getTextures(AVFrame *frame)
{
    if (!frame || !frame->hw_frames_ctx || frame->format != AV_PIX_FMT_D3D11)
        return nullptr;

    auto *fCtx = (AVHWFramesContext *)frame->hw_frames_ctx->data;
    auto *ctx = fCtx->device_ctx;
    if (!ctx || ctx->type != AV_HWDEVICE_TYPE_D3D11VA)
        return nullptr;

    auto nh = static_cast<const QRhiD3D11NativeHandles *>(rhi->nativeHandles());
    if (!nh)
        return nullptr;

    auto dev = reinterpret_cast<ID3D11Device *>(nh->dev);
    if (!dev)
        return nullptr;

    auto ffmpegTex = (ID3D11Texture2D *)frame->data[0];
    int index = (intptr_t)frame->data[1];

    auto sharedTex = getSharedTextureForDevice(dev, ffmpegTex);
    if (sharedTex) {
        auto tex = copyTextureFromArray(dev, sharedTex.get(), index);
        if (tex) {
            if (rhi->backend() == QRhi::D3D11) {
                QVideoFrameFormat::PixelFormat format = QFFmpegVideoBuffer::toQtPixelFormat(AVPixelFormat(fCtx->sw_format));
                return new D3D11TextureSet(rhi, format, std::move(tex));
            } else if (rhi->backend() == QRhi::OpenGLES2) {

            }
        }
    }

    return nullptr;
}

void D3D11TextureConverter::SetupDecoderTextures(AVCodecContext *s)
{
    int ret = avcodec_get_hw_frames_parameters(s,
                                               s->hw_device_ctx,
                                               AV_PIX_FMT_D3D11,
                                               &s->hw_frames_ctx);
    if (ret < 0) {
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to allocate HW frames context" << ret;
        return;
    }

    auto *frames_ctx = (AVHWFramesContext *)s->hw_frames_ctx->data;
    auto *hwctx = (AVD3D11VAFramesContext *)frames_ctx->hwctx;
    hwctx->MiscFlags = D3D11_RESOURCE_MISC_SHARED;
    hwctx->BindFlags = D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE;
    ret = av_hwframe_ctx_init(s->hw_frames_ctx);
    if (ret < 0) {
        qCDebug(qLcMediaFFmpegHWAccel) << "Failed to initialize HW frames context" << ret;
        av_buffer_unref(&s->hw_frames_ctx);
    }
}

}

QT_END_NAMESPACE