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 <QtFFmpegMediaPluginImpl/private/qffmpegcodeccontext_p.h>
7
8#include <QtGui/rhi/qrhi.h>
9
10// TODO: This class is reused from the native Android media backend and breaks ODR.
11#include "../android/wrappers/jni/androidsurfacetexture_p.h"
12
13extern "C" {
14#include <libavcodec/mediacodec.h>
15#include <libavutil/hwcontext_mediacodec.h>
16}
17
18#if !defined(Q_OS_ANDROID)
19# error "Configuration error"
20#endif
21
22QT_BEGIN_NAMESPACE
23
24namespace QFFmpeg {
25
26namespace {
27
28class MediaCodecTextureHandles : public QVideoFrameTexturesHandles
29{
30public:
31 MediaCodecTextureHandles(TextureConverterBackendPtr &&converterBackend, quint64 textureHandle)
32 : m_parentConverterBackend(std::move(converterBackend)), m_handle(textureHandle)
33 {
34 }
35
36 quint64 textureHandle(QRhi &, int plane) override { return (plane == 0) ? m_handle : 0; }
37
38private:
39 TextureConverterBackendPtr m_parentConverterBackend; // ensures the backend is kept
40 quint64 m_handle;
41};
42
43void deleteSurface(AVHWDeviceContext *ctx)
44{
45 AndroidSurfaceTexture* s = reinterpret_cast<AndroidSurfaceTexture *>(ctx->user_opaque);
46 delete s;
47}
48
49AndroidSurfaceTexture* getTextureSurface(AVFrame *frame)
50{
51 if (!frame || !frame->hw_frames_ctx)
52 return nullptr;
53
54 auto *frameContext = reinterpret_cast<AVHWFramesContext *>(frame->hw_frames_ctx->data);
55
56 if (!frameContext || !frameContext->device_ctx)
57 return nullptr;
58
59 AVHWDeviceContext *deviceContext = frameContext->device_ctx;
60
61 return reinterpret_cast<AndroidSurfaceTexture *>(deviceContext->user_opaque);
62}
63using UninitializedMediaCodecContextUPtr = std::unique_ptr<AVMediaCodecContext, AVFreeDeleter>;
64
65} // namespace
66
107
110{
111 // Most likely, this method should be used instead of createTextureHandles, QRhiTexture is
112 // already created, and we don't need to convert it to handle and back. This work should be done
113 // in the scope of QTBUG-132174
114 return nullptr;
115}
116
119{
120 // TODO: reuse oldHandles: the underlying QRhiTexture must be taken from it instead of
121 // keeping it in AndroidTextureConverter. See QTBUG-132174
123
125 return {};
126
132
133 if (!externalTexture->create()) {
134 qWarning() << "Failed to create the external texture!";
135 return {};
136 }
137
140 }
141
142 // release a MediaCodec buffer and render it to the surface
144
145 if (!buffer) {
146 qWarning() << "Received a frame without AVMediaCodecBuffer.";
147 } else if (av_mediacodec_release_buffer(buffer, 1) < 0) {
148 qWarning() << "Failed to render buffer to surface.";
149 return {};
150 }
151
153
156}
157}
158
159QT_END_NAMESPACE