Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
13
extern
"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
22
QT_BEGIN_NAMESPACE
23
24
namespace
QFFmpeg
{
25
26
namespace
{
27
28
class
MediaCodecTextureHandles :
public
QVideoFrameTexturesHandles
29
{
30
public
:
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
38
private
:
39
TextureConverterBackendPtr m_parentConverterBackend;
// ensures the backend is kept
40
quint64 m_handle;
41
};
42
43
void
deleteSurface(AVHWDeviceContext *ctx)
44
{
45
AndroidSurfaceTexture
* s =
reinterpret_cast
<
AndroidSurfaceTexture
*>(ctx->user_opaque);
46
delete
s;
47
}
48
49
AndroidSurfaceTexture
* 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
}
63
using
UninitializedMediaCodecContextUPtr = std::unique_ptr<AVMediaCodecContext, AVFreeDeleter>;
64
65
}
// namespace
66
67
void
MediaCodecTextureConverter
::
setupDecoderSurface
(
AVCodecContext
*
avCodecContext
)
68
{
69
std
::
unique_ptr
<
AndroidSurfaceTexture
>
androidSurfaceTexture
(
new
AndroidSurfaceTexture
(0));
70
71
UninitializedMediaCodecContextUPtr
mediacodecContext
{
av_mediacodec_alloc_context
() };
72
if
(!
mediacodecContext
)
73
return
;
74
75
if
(
av_mediacodec_default_init
(
avCodecContext
,
mediacodecContext
.
get
(),
76
androidSurfaceTexture
->
surface
())
77
< 0) {
78
qWarning
() <<
"Failed to initialize the MediaCodec surface."
;
79
return
;
80
}
81
mediacodecContext
.
release
();
82
PlaybackCodecContextDeleter
::
setFinalizer
(
avCodecContext
, +[](
AVCodecContext
*
context
) {
83
av_mediacodec_default_free
(
context
);
84
});
85
86
if
(!
avCodecContext
->
hw_device_ctx
|| !
avCodecContext
->
hw_device_ctx
->
data
)
87
return
;
88
89
AVHWDeviceContext
*
deviceContext
=
90
reinterpret_cast
<
AVHWDeviceContext
*>(
avCodecContext
->
hw_device_ctx
->
data
);
91
92
if
(!
deviceContext
->
hwctx
)
93
return
;
94
95
AVMediaCodecDeviceContext
*
mediaDeviceContext
=
96
reinterpret_cast
<
AVMediaCodecDeviceContext
*>(
deviceContext
->
hwctx
);
97
98
if
(!
mediaDeviceContext
)
99
return
;
100
101
mediaDeviceContext
->
surface
=
androidSurfaceTexture
->
surface
();
102
103
Q_ASSERT
(
deviceContext
->
user_opaque
==
nullptr
);
104
deviceContext
->
user_opaque
=
androidSurfaceTexture
.
release
();
105
deviceContext
->
free
=
deleteSurface
;
106
}
107
108
QVideoFrameTexturesUPtr
109
MediaCodecTextureConverter
::
createTextures
(
AVFrame
*
/*frame*/
)
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
117
QVideoFrameTexturesHandlesUPtr
118
MediaCodecTextureConverter
::
createTextureHandles
(
AVFrame
*
frame
)
119
{
120
// TODO: reuse oldHandles: the underlying QRhiTexture must be taken from it instead of
121
// keeping it in AndroidTextureConverter. See QTBUG-132174
122
AndroidSurfaceTexture
*
androidSurfaceTexture
=
getTextureSurface
(
frame
);
123
124
if
(!
androidSurfaceTexture
|| !
androidSurfaceTexture
->
isValid
())
125
return
{};
126
127
if
(!
externalTexture
||
m_currentSurfaceIndex
!=
androidSurfaceTexture
->
index
()) {
128
m_currentSurfaceIndex
=
androidSurfaceTexture
->
index
();
129
androidSurfaceTexture
->
detachFromGLContext
();
130
externalTexture
=
std
::
unique_ptr
<
QRhiTexture
>(
rhi
->
newTexture
(
131
QRhiTexture
::
Format
::
RGBA8
,
frameSize
(
frame
), 1,
QRhiTexture
::
ExternalOES
));
132
133
if
(!
externalTexture
->
create
()) {
134
qWarning
() <<
"Failed to create the external texture!"
;
135
return
{};
136
}
137
138
quint64
textureHandle
=
externalTexture
->
nativeTexture
().
object
;
139
androidSurfaceTexture
->
attachToGLContext
(
textureHandle
);
140
}
141
142
// release a MediaCodec buffer and render it to the surface
143
AVMediaCodecBuffer
*
buffer
= (
AVMediaCodecBuffer
*)
frame
->
data
[3];
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
152
androidSurfaceTexture
->
updateTexImage
();
153
154
return
std
::
make_unique
<
MediaCodecTextureHandles
>(
shared_from_this
(),
155
externalTexture
->
nativeTexture
().
object
);
156
}
157
}
158
159
QT_END_NAMESPACE
AndroidSurfaceTexture
Definition
androidsurfacetexture_p.h:26
QFFmpeg
Definition
qandroidcacertificates.cpp:26
qtmultimedia
src
plugins
multimedia
ffmpeg
android
qffmpeghwaccel_mediacodec.cpp
Generated on
for Qt by
1.18.0