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
qffmpegencodingformatcontext.cpp
Go to the documentation of this file.
1
// Copyright (C) 2024 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
"qffmpegencodingformatcontext_p.h"
5
6
#
include
<
QtFFmpegMediaPluginImpl
/
private
/
qffmpegmediaformatinfo_p
.
h
>
7
#
include
<
QtFFmpegMediaPluginImpl
/
private
/
qffmpegioutils_p
.
h
>
8
#
include
<
QtCore
/
qfile
.
h
>
9
#
include
<
QtCore
/
qloggingcategory
.
h
>
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
QFFmpeg
{
14
15
Q_STATIC_LOGGING_CATEGORY
(
qLcEncodingFormatContext
,
"qt.multimedia.ffmpeg.encodingformatcontext"
);
16
17
namespace
{
18
// In the example https://ffmpeg.org/doxygen/trunk/avio_read_callback_8c-example.html,
19
// BufferSize = 4096 is suggested, however, it might be not optimal. To be investigated.
20
constexpr
size_t DefaultBufferSize = 4096;
21
}
// namespace
22
23
EncodingFormatContext
::
EncodingFormatContext
(QMediaFormat::FileFormat fileFormat)
24
:
m_avFormatContext
(
avformat_alloc_context
())
25
{
26
const
AVOutputFormat *avFormat = QFFmpegMediaFormatInfo::outputFormatForFileFormat(fileFormat);
27
m_avFormatContext->oformat =
const_cast
<AVOutputFormat *>(avFormat);
// constness varies
28
}
29
30
EncodingFormatContext
::~
EncodingFormatContext
()
31
{
32
closeAVIO
(
)
;
33
}
34
35
bool
EncodingFormatContext
::
openAVIO
(
const
QString &filePath)
36
{
37
Q_ASSERT(!
isAVIOOpen
(
)
);
38
Q_ASSERT(!filePath.isEmpty());
39
40
const
QByteArray filePathUtf8 = filePath.toUtf8();
41
42
std::unique_ptr<
char
, AVFreeDeleter> url(
43
reinterpret_cast
<
char
*>(av_malloc(filePathUtf8.size() + 1)));
44
memcpy(url.get(), filePathUtf8.constData(), filePathUtf8.size() + 1);
45
46
// Initialize the AVIOContext for accessing the resource indicated by the url
47
auto
result = avio_open2(&m_avFormatContext->pb, url.get(), AVIO_FLAG_WRITE,
nullptr
,
nullptr
);
48
49
qCDebug(qLcEncodingFormatContext)
50
<<
"opened by file path:"
<< url.get() <<
", result:"
<< result;
51
52
Q_ASSERT(m_avFormatContext->url ==
nullptr
);
53
if
(
isAVIOOpen
(
)
) {
54
m_avFormatContext->url = url.release();
55
return
true
;
56
}
else
{
57
return
openAVIOWithQFile(filePath);
58
}
59
}
60
61
bool
EncodingFormatContext
::openAVIOWithQFile(
const
QString &filePath)
62
{
63
// QTBUG-123082, To be investigated:
64
// - should we use the logic with QFile for all file paths?
65
// - does avio_open2 handle network protocols that QFile doesn't?
66
// - which buffer size should we set to opening with QFile to ensure the best performance?
67
68
auto
file = std::make_unique<QFile>(filePath);
69
70
if
(!file->open(QFile::WriteOnly)) {
71
qCDebug(qLcEncodingFormatContext) <<
"Cannot open QFile"
<< filePath;
72
return
false
;
73
}
74
75
bool
success =
openAVIO
(
file.get()
)
;
76
if
(success)
77
m_outputFile = std::move(file);
78
return
success;
79
}
80
81
bool
EncodingFormatContext
::
openAVIO
(QIODevice *device)
82
{
83
Q_ASSERT(!
isAVIOOpen
(
)
);
84
Q_ASSERT(device);
85
86
if
(!device->isWritable())
87
return
false
;
88
89
auto
buffer =
static_cast
<uint8_t *>(av_malloc(DefaultBufferSize));
90
m_avFormatContext->pb = avio_alloc_context(buffer, DefaultBufferSize, 1, device,
nullptr
,
91
&writeQIODevice, &seekQIODevice);
92
return
bool
(m_avFormatContext->pb);
93
}
94
95
void
EncodingFormatContext
::
closeAVIO
()
96
{
97
// Close the AVIOContext and release any file handles
98
if
(
isAVIOOpen
(
)
) {
99
if
(m_avFormatContext->url && *m_avFormatContext->url !=
'\0'
) {
100
auto
closeResult = avio_closep(&m_avFormatContext->pb);
101
Q_ASSERT(closeResult == 0);
102
}
else
{
103
freeAVIOContext(m_avFormatContext->pb);
104
}
105
106
m_outputFile.reset();
107
}
else
{
108
Q_ASSERT(!m_outputFile);
109
}
110
}
111
112
}
// namespace QFFmpeg
113
114
QT_END_NAMESPACE
QFFmpeg::EncodingFormatContext
Definition
qffmpegencodingformatcontext_p.h:31
QFFmpeg::EncodingFormatContext::closeAVIO
void closeAVIO()
Definition
qffmpegencodingformatcontext.cpp:95
QFFmpeg::EncodingFormatContext::openAVIO
bool openAVIO(const QString &filePath)
Definition
qffmpegencodingformatcontext.cpp:35
QFFmpeg::EncodingFormatContext::openAVIO
bool openAVIO(QIODevice *device)
Definition
qffmpegencodingformatcontext.cpp:81
QFFmpeg::EncodingFormatContext::~EncodingFormatContext
~EncodingFormatContext()
Definition
qffmpegencodingformatcontext.cpp:30
QFFmpeg::EncodingFormatContext::EncodingFormatContext
EncodingFormatContext(QMediaFormat::FileFormat fileFormat)
Definition
qffmpegencodingformatcontext.cpp:23
QFFmpeg::EncodingFormatContext::isAVIOOpen
bool isAVIOOpen() const
Definition
qffmpegencodingformatcontext_p.h:38
QFFmpeg
Definition
qandroidcacertificates.cpp:26
QFFmpeg::Q_STATIC_LOGGING_CATEGORY
Q_STATIC_LOGGING_CATEGORY(qLCAndroidCaCertificates, "qt.multimedia.ffmpeg.android.caCertificates")
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtmultimedia
src
plugins
multimedia
ffmpeg
qffmpegencodingformatcontext.cpp
Generated on
for Qt by
1.18.0