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
qgstutils.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
<
common
/
qgstutils_p
.
h
>
5
#
include
<
common
/
qgst_p
.
h
>
6
7
#
include
<
QtMultimedia
/
qaudioformat
.
h
>
8
9
#
include
<
chrono
>
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
{
14
15
const
char
*
audioSampleFormatNames
[
QAudioFormat
::
NSampleFormats
] = {
16
nullptr
,
17
#
if
Q_BYTE_ORDER
==
Q_LITTLE_ENDIAN
18
"U8"
,
19
"S16LE"
,
20
"S32LE"
,
21
"F32LE"
22
#
else
23
"U8"
,
24
"S16BE"
,
25
"S32BE"
,
26
"F32BE"
27
#
endif
28
};
29
30
QAudioFormat
::
SampleFormat
gstSampleFormatToSampleFormat
(
const
char
*fmt)
31
{
32
if
(fmt) {
33
for
(
int
i = 1; i < QAudioFormat::NSampleFormats; ++i) {
34
if
(strcmp(fmt, audioSampleFormatNames[i]))
35
continue
;
36
return
QAudioFormat::SampleFormat(i);
37
}
38
}
39
return
QAudioFormat::Unknown;
40
}
41
42
}
// namespace
43
44
/*
45
Returns audio format for a sample \a sample.
46
If the buffer doesn't have a valid audio format, an empty QAudioFormat is returned.
47
*/
48
QAudioFormat
QGstUtils
::
audioFormatForSample
(GstSample *sample)
49
{
50
auto
caps = QGstCaps(gst_sample_get_caps(sample), QGstCaps::NeedsRef);
51
if
(!caps)
52
return
{};
53
return
audioFormatForCaps(caps);
54
}
55
56
QAudioFormat
QGstUtils
::
audioFormatForCaps
(
const
QGstCaps
&caps)
57
{
58
QAudioFormat
format;
59
QGstStructureView
s = caps
.
at
(
0
)
;
60
if
(s.name() !=
"audio/x-raw"
)
61
return
format;
62
63
auto
rate = s
[
"rate"
]
.toInt();
64
auto
channels = s
[
"channels"
]
.toInt();
65
QAudioFormat::SampleFormat fmt = gstSampleFormatToSampleFormat(s[
"format"
].toString());
66
if
(!rate || !channels || fmt == QAudioFormat::Unknown)
67
return
format;
68
69
format.setSampleRate(*rate);
70
format.setChannelCount(*channels);
71
format.setSampleFormat(fmt);
72
73
return
format;
74
}
75
76
/*
77
Builds GstCaps for an audio format \a format.
78
Returns 0 if the audio format is not valid.
79
80
\note Caller must unreference GstCaps.
81
*/
82
83
QGstCaps
QGstUtils
::
capsForAudioFormat
(
const
QAudioFormat
&format)
84
{
85
if
(!format.isValid())
86
return
{};
87
88
auto
sampleFormat = format.sampleFormat();
89
auto
caps = gst_caps_new_simple(
90
"audio/x-raw"
,
91
"format"
, G_TYPE_STRING, audioSampleFormatNames[sampleFormat],
92
"rate"
, G_TYPE_INT , format.sampleRate(),
93
"channels"
, G_TYPE_INT , format.channelCount(),
94
"layout"
, G_TYPE_STRING,
"interleaved"
,
95
nullptr
);
96
97
return
QGstCaps(caps, QGstCaps::HasRef);
98
}
99
100
QList
<
QAudioFormat
::
SampleFormat
>
QGValue
::
getSampleFormats
()
const
101
{
102
if
(!GST_VALUE_HOLDS_LIST(value))
103
return
{};
104
105
QList<QAudioFormat::SampleFormat> formats;
106
guint nFormats = gst_value_list_get_size(value);
107
for
(guint f = 0; f < nFormats; ++f) {
108
QGValue
v =
QGValue
{ gst_value_list_get_value(value, f) };
109
auto
*name = v
.
toString
(
)
;
110
QAudioFormat::SampleFormat fmt = gstSampleFormatToSampleFormat(name);
111
if
(fmt == QAudioFormat::Unknown)
112
continue
;
113
formats.append(fmt);
114
}
115
return
formats;
116
}
117
118
void
QGstUtils
::
setFrameTimeStampsFromBuffer
(QVideoFrame *frame, GstBuffer *buffer)
119
{
120
using
namespace
std
::chrono;
121
using
namespace
std
::chrono_literals;
122
123
// GStreamer uses nanoseconds, Qt uses microseconds
124
nanoseconds startTime{ GST_BUFFER_TIMESTAMP(buffer) };
125
if
(startTime >= 0ns) {
126
frame->setStartTime(floor<microseconds>(startTime).count());
127
128
nanoseconds duration{ GST_BUFFER_DURATION(buffer) };
129
if
(duration >= 0ns)
130
frame->setEndTime(floor<microseconds>(startTime + duration).count());
131
}
132
}
133
134
GList
*
qt_gst_video_sinks
()
135
{
136
return
gst_element_factory_list_get_elements(GST_ELEMENT_FACTORY_TYPE_SINK
137
| GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO,
138
GST_RANK_MARGINAL);
139
}
140
141
QLocale
::
Language
QGstUtils
::
codeToLanguage
(
const
gchar *lang)
142
{
143
return
QLocale::codeToLanguage(QString::fromUtf8(lang), QLocale::AnyLanguageCode);
144
}
145
146
// QTBUG-125249: gstreamer tries "to keep the input height (because of interlacing)". Can we align
147
// the behavior between GStreamer and FFmpeg?
148
QSize
QGstUtils
::
qCalculateFrameSizeGStreamer
(QSize resolution,
Fraction
par)
149
{
150
if
(!resolution.isValid()) {
151
qWarning() << Q_FUNC_INFO <<
"invalid resolution when calculating frame size"
;
152
return
resolution;
153
}
154
155
if
(par.numerator == par.denominator || par.numerator < 1 || par.denominator < 1)
156
return
resolution;
157
158
return
QSize{
159
resolution.width() * par.numerator / par.denominator,
160
resolution.height(),
161
};
162
}
163
164
QT_END_NAMESPACE
QAudioFormat
The QAudioFormat class stores audio stream parameter information.
Definition
qaudioformat.h:22
QGValue
Definition
qgst_p.h:313
QGValue::getSampleFormats
QList< QAudioFormat::SampleFormat > getSampleFormats() const
Definition
qgstutils.cpp:100
QGValue::toString
const char * toString() const
Definition
qgst.cpp:156
QGstCaps
Definition
qgst_p.h:384
QGstCaps::at
QGstStructureView at(int index) const
Definition
qgst.cpp:625
QGstStructureView
Definition
qgst_p.h:347
QGstStructureView::operator[]
QGValue operator[](const char *fieldname) const
Definition
qgst.cpp:238
QGstUtils
Definition
qgstutils_p.h:31
QGstUtils::qCalculateFrameSizeGStreamer
QSize qCalculateFrameSizeGStreamer(QSize resolution, Fraction par)
Definition
qgstutils.cpp:148
QGstUtils::codeToLanguage
QLocale::Language codeToLanguage(const gchar *)
Definition
qgstutils.cpp:141
QGstUtils::capsForAudioFormat
QGstCaps capsForAudioFormat(const QAudioFormat &format)
Definition
qgstutils.cpp:83
QGstUtils::audioFormatForCaps
QAudioFormat audioFormatForCaps(const QGstCaps &caps)
Definition
qgstutils.cpp:56
QGstUtils::setFrameTimeStampsFromBuffer
void setFrameTimeStampsFromBuffer(QVideoFrame *frame, GstBuffer *buffer)
Definition
qgstutils.cpp:118
QGstUtils::audioFormatForSample
QAudioFormat audioFormatForSample(GstSample *sample)
Definition
qgstutils.cpp:48
QT_BEGIN_NAMESPACE::audioSampleFormatNames
const char * audioSampleFormatNames[QAudioFormat::NSampleFormats]
Definition
qgstutils.cpp:15
QT_BEGIN_NAMESPACE::gstSampleFormatToSampleFormat
QAudioFormat::SampleFormat gstSampleFormatToSampleFormat(const char *fmt)
Definition
qgstutils.cpp:30
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qt_gst_video_sinks
GList * qt_gst_video_sinks()
Definition
qgstutils.cpp:134
Fraction
Definition
qmultimediautils_p.h:37
qtmultimedia
src
plugins
multimedia
gstreamer
common
qgstutils.cpp
Generated on
for Qt by
1.18.0