Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qffmpeghwaccel_mediacodec.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtFFmpegMediaPluginImpl/private/qffmpeghwaccel_mediacodec_p.h>
5
6#include <QtGui/rhi/qrhi.h>
7
8// TODO: This class is reused from the native Android media backend and breaks ODR.
9#include "../android/wrappers/jni/androidsurfacetexture_p.h"
10
11extern "C" {
12#include <libavcodec/mediacodec.h>
13#include <libavutil/hwcontext_mediacodec.h>
14}
15
16#if !defined(Q_OS_ANDROID)
17# error "Configuration error"
18#endif
19
20QT_BEGIN_NAMESPACE
21
22namespace QFFmpeg {
23
24namespace {
25
26class MediaCodecTextureHandles : public QVideoFrameTexturesHandles
27{
28public:
29 MediaCodecTextureHandles(TextureConverterBackendPtr &&converterBackend, quint64 textureHandle)
30 : m_parentConverterBackend(std::move(converterBackend)), m_handle(textureHandle)
31 {
32 }
33
34 quint64 textureHandle(QRhi &, int plane) override { return (plane == 0) ? m_handle : 0; }
35
36private:
37 TextureConverterBackendPtr m_parentConverterBackend; // ensures the backend is kept
38 quint64 m_handle;
39};
40
41void deleteSurface(AVHWDeviceContext *ctx)
42{
43 AndroidSurfaceTexture* s = reinterpret_cast<AndroidSurfaceTexture *>(ctx->user_opaque);
44 delete s;
45}
46
47AndroidSurfaceTexture* getTextureSurface(AVFrame *frame)
48{
49 if (!frame || !frame->hw_frames_ctx)
50 return nullptr;
51
52 auto *frameContext = reinterpret_cast<AVHWFramesContext *>(frame->hw_frames_ctx->data);
53
54 if (!frameContext || !frameContext->device_ctx)
55 return nullptr;
56
57 AVHWDeviceContext *deviceContext = frameContext->device_ctx;
58
59 return reinterpret_cast<AndroidSurfaceTexture *>(deviceContext->user_opaque);
60}
61} // namespace
62
90
93{
94 // Most likely, this method should be used instead of createTextureHandles, QRhiTexture is
95 // already created, and we don't need to convert it to handle and back. This work should be done
96 // in the scope of QTBUG-132174
97 return nullptr;
98}
99
102{
103 // TODO: reuse oldHandles: the underlying QRhiTexture must be taken from it instead of
104 // keeping it in AndroidTextureConverter. See QTBUG-132174
106
108 return {};
109
115
116 if (!externalTexture->create()) {
117 qWarning() << "Failed to create the external texture!";
118 return {};
119 }
120
123 }
124
125 // release a MediaCodec buffer and render it to the surface
127
128 if (!buffer) {
129 qWarning() << "Received a frame without AVMediaCodecBuffer.";
130 } else if (av_mediacodec_release_buffer(buffer, 1) < 0) {
131 qWarning() << "Failed to render buffer to surface.";
132 return {};
133 }
134
136
139}
140}
141
142QT_END_NAMESPACE