1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qgstreamervideoencode.h" |
41 | #include "qgstreamercapturesession.h" |
42 | #include "qgstreamermediacontainercontrol.h" |
43 | #include <private/qgstutils_p.h> |
44 | #include <QtCore/qdebug.h> |
45 | |
46 | #include <math.h> |
47 | |
48 | QGstreamerVideoEncode::QGstreamerVideoEncode(QGstreamerCaptureSession *session) |
49 | :QVideoEncoderSettingsControl(session), m_session(session) |
50 | , m_codecs(QGstCodecsInfo::VideoEncoder) |
51 | { |
52 | } |
53 | |
54 | QGstreamerVideoEncode::~QGstreamerVideoEncode() |
55 | { |
56 | } |
57 | |
58 | QList<QSize> QGstreamerVideoEncode::supportedResolutions(const QVideoEncoderSettings &, bool *continuous) const |
59 | { |
60 | if (continuous) |
61 | *continuous = m_session->videoInput() != 0; |
62 | |
63 | return m_session->videoInput() ? m_session->videoInput()->supportedResolutions() : QList<QSize>(); |
64 | } |
65 | |
66 | QList< qreal > QGstreamerVideoEncode::supportedFrameRates(const QVideoEncoderSettings &, bool *continuous) const |
67 | { |
68 | if (continuous) |
69 | *continuous = false; |
70 | |
71 | return m_session->videoInput() ? m_session->videoInput()->supportedFrameRates() : QList<qreal>(); |
72 | } |
73 | |
74 | QStringList QGstreamerVideoEncode::supportedVideoCodecs() const |
75 | { |
76 | return m_codecs.supportedCodecs(); |
77 | } |
78 | |
79 | QString QGstreamerVideoEncode::videoCodecDescription(const QString &codecName) const |
80 | { |
81 | return m_codecs.codecDescription(codec: codecName); |
82 | } |
83 | |
84 | QStringList QGstreamerVideoEncode::supportedEncodingOptions(const QString &codec) const |
85 | { |
86 | return m_codecs.codecOptions(codec); |
87 | } |
88 | |
89 | QVariant QGstreamerVideoEncode::encodingOption(const QString &codec, const QString &name) const |
90 | { |
91 | return m_options[codec].value(akey: name); |
92 | } |
93 | |
94 | void QGstreamerVideoEncode::setEncodingOption( |
95 | const QString &codec, const QString &name, const QVariant &value) |
96 | { |
97 | m_options[codec][name] = value; |
98 | } |
99 | |
100 | QVideoEncoderSettings QGstreamerVideoEncode::videoSettings() const |
101 | { |
102 | return m_videoSettings; |
103 | } |
104 | |
105 | void QGstreamerVideoEncode::setVideoSettings(const QVideoEncoderSettings &settings) |
106 | { |
107 | m_videoSettings = settings; |
108 | } |
109 | |
110 | GstElement *QGstreamerVideoEncode::createEncoder() |
111 | { |
112 | QString codec = m_videoSettings.codec(); |
113 | GstElement *encoderElement = gst_element_factory_make(factoryname: m_codecs.codecElement(codec).constData(), name: "video-encoder" ); |
114 | if (!encoderElement) |
115 | return 0; |
116 | |
117 | GstBin *encoderBin = GST_BIN(gst_bin_new("video-encoder-bin" )); |
118 | |
119 | GstElement *sinkCapsFilter = gst_element_factory_make(factoryname: "capsfilter" , name: "capsfilter-video" ); |
120 | GstElement *srcCapsFilter = gst_element_factory_make(factoryname: "capsfilter" , name: "capsfilter-video" ); |
121 | gst_bin_add_many(bin: encoderBin, element_1: sinkCapsFilter, srcCapsFilter, NULL); |
122 | |
123 | GstElement *colorspace = gst_element_factory_make(QT_GSTREAMER_COLORCONVERSION_ELEMENT_NAME, NULL); |
124 | gst_bin_add(bin: encoderBin, element: colorspace); |
125 | gst_bin_add(bin: encoderBin, element: encoderElement); |
126 | |
127 | gst_element_link_many(element_1: sinkCapsFilter, element_2: colorspace, encoderElement, srcCapsFilter, NULL); |
128 | |
129 | // add ghostpads |
130 | GstPad *pad = gst_element_get_static_pad(element: sinkCapsFilter, name: "sink" ); |
131 | gst_element_add_pad(GST_ELEMENT(encoderBin), pad: gst_ghost_pad_new(name: "sink" , target: pad)); |
132 | gst_object_unref(GST_OBJECT(pad)); |
133 | |
134 | pad = gst_element_get_static_pad(element: srcCapsFilter, name: "src" ); |
135 | gst_element_add_pad(GST_ELEMENT(encoderBin), pad: gst_ghost_pad_new(name: "src" , target: pad)); |
136 | gst_object_unref(GST_OBJECT(pad)); |
137 | |
138 | if (encoderElement) { |
139 | if (m_videoSettings.encodingMode() == QMultimedia::ConstantQualityEncoding) { |
140 | QMultimedia::EncodingQuality qualityValue = m_videoSettings.quality(); |
141 | |
142 | if (codec == QLatin1String("video/x-h264" )) { |
143 | //constant quantizer mode |
144 | g_object_set(G_OBJECT(encoderElement), first_property_name: "pass" , 4, NULL); |
145 | int qualityTable[] = { |
146 | 50, //VeryLow |
147 | 35, //Low |
148 | 21, //Normal |
149 | 15, //High |
150 | 8 //VeryHigh |
151 | }; |
152 | g_object_set(G_OBJECT(encoderElement), first_property_name: "quantizer" , qualityTable[qualityValue], NULL); |
153 | } else if (codec == QLatin1String("video/x-xvid" )) { |
154 | //constant quantizer mode |
155 | g_object_set(G_OBJECT(encoderElement), first_property_name: "pass" , 3, NULL); |
156 | int qualityTable[] = { |
157 | 32, //VeryLow |
158 | 12, //Low |
159 | 5, //Normal |
160 | 3, //High |
161 | 2 //VeryHigh |
162 | }; |
163 | int quant = qualityTable[qualityValue]; |
164 | g_object_set(G_OBJECT(encoderElement), first_property_name: "quantizer" , quant, NULL); |
165 | } else if (codec.startsWith(s: QLatin1String("video/mpeg" ))) { |
166 | //constant quantizer mode |
167 | g_object_set(G_OBJECT(encoderElement), first_property_name: "pass" , 2, NULL); |
168 | //quant from 1 to 30, default ~3 |
169 | double qualityTable[] = { |
170 | 20, //VeryLow |
171 | 8.0, //Low |
172 | 3.0, //Normal |
173 | 2.5, //High |
174 | 2.0 //VeryHigh |
175 | }; |
176 | double quant = qualityTable[qualityValue]; |
177 | g_object_set(G_OBJECT(encoderElement), first_property_name: "quantizer" , quant, NULL); |
178 | } else if (codec == QLatin1String("video/x-theora" )) { |
179 | int qualityTable[] = { |
180 | 8, //VeryLow |
181 | 16, //Low |
182 | 32, //Normal |
183 | 45, //High |
184 | 60 //VeryHigh |
185 | }; |
186 | //quality from 0 to 63 |
187 | int quality = qualityTable[qualityValue]; |
188 | g_object_set(G_OBJECT(encoderElement), first_property_name: "quality" , quality, NULL); |
189 | } |
190 | } else { |
191 | int bitrate = m_videoSettings.bitRate(); |
192 | if (bitrate > 0) { |
193 | g_object_set(G_OBJECT(encoderElement), first_property_name: "bitrate" , bitrate, NULL); |
194 | } |
195 | } |
196 | |
197 | QMap<QString,QVariant> options = m_options.value(akey: codec); |
198 | for (auto it = options.cbegin(), end = options.cend(); it != end; ++it) { |
199 | const QString &option = it.key(); |
200 | const QVariant &value = it.value(); |
201 | |
202 | switch (value.type()) { |
203 | case QVariant::Int: |
204 | g_object_set(G_OBJECT(encoderElement), first_property_name: option.toLatin1(), value.toInt(), NULL); |
205 | break; |
206 | case QVariant::Bool: |
207 | g_object_set(G_OBJECT(encoderElement), first_property_name: option.toLatin1(), value.toBool(), NULL); |
208 | break; |
209 | case QVariant::Double: |
210 | g_object_set(G_OBJECT(encoderElement), first_property_name: option.toLatin1(), value.toDouble(), NULL); |
211 | break; |
212 | case QVariant::String: |
213 | g_object_set(G_OBJECT(encoderElement), first_property_name: option.toLatin1(), value.toString().toUtf8().constData(), NULL); |
214 | break; |
215 | default: |
216 | qWarning() << "unsupported option type:" << option << value; |
217 | break; |
218 | } |
219 | |
220 | } |
221 | } |
222 | |
223 | if (!m_videoSettings.resolution().isEmpty() || m_videoSettings.frameRate() > 0.001) { |
224 | GstCaps *caps = QGstUtils::videoFilterCaps(); |
225 | |
226 | if (!m_videoSettings.resolution().isEmpty()) { |
227 | gst_caps_set_simple( |
228 | caps, |
229 | field: "width" , G_TYPE_INT, m_videoSettings.resolution().width(), |
230 | "height" , G_TYPE_INT, m_videoSettings.resolution().height(), |
231 | NULL); |
232 | } |
233 | |
234 | if (m_videoSettings.frameRate() > 0.001) { |
235 | QPair<int,int> rate = rateAsRational(); |
236 | gst_caps_set_simple( |
237 | caps, |
238 | field: "framerate" , GST_TYPE_FRACTION, rate.first, rate.second, |
239 | NULL); |
240 | } |
241 | |
242 | //qDebug() << "set video caps filter:" << gst_caps_to_string(caps); |
243 | |
244 | g_object_set(G_OBJECT(sinkCapsFilter), first_property_name: "caps" , caps, NULL); |
245 | |
246 | gst_caps_unref(caps); |
247 | } |
248 | |
249 | // Some encoders support several codecs. Setting a caps filter downstream with the desired |
250 | // codec (which is actually a string representation of the caps) will make sure we use the |
251 | // correct codec. |
252 | GstCaps *caps = gst_caps_from_string(string: codec.toUtf8().constData()); |
253 | g_object_set(G_OBJECT(srcCapsFilter), first_property_name: "caps" , caps, NULL); |
254 | gst_caps_unref(caps); |
255 | |
256 | return GST_ELEMENT(encoderBin); |
257 | } |
258 | |
259 | QPair<int,int> QGstreamerVideoEncode::rateAsRational() const |
260 | { |
261 | qreal frameRate = m_videoSettings.frameRate(); |
262 | |
263 | if (frameRate > 0.001) { |
264 | //convert to rational number |
265 | QList<int> denumCandidates; |
266 | denumCandidates << 1 << 2 << 3 << 5 << 10 << 1001 << 1000; |
267 | |
268 | qreal error = 1.0; |
269 | int num = 1; |
270 | int denum = 1; |
271 | |
272 | for (int curDenum : qAsConst(t&: denumCandidates)) { |
273 | int curNum = qRound(d: frameRate*curDenum); |
274 | qreal curError = qAbs(t: qreal(curNum)/curDenum - frameRate); |
275 | |
276 | if (curError < error) { |
277 | error = curError; |
278 | num = curNum; |
279 | denum = curDenum; |
280 | } |
281 | |
282 | if (curError < 1e-8) |
283 | break; |
284 | } |
285 | |
286 | return QPair<int,int>(num,denum); |
287 | } |
288 | |
289 | return QPair<int,int>(); |
290 | } |
291 | |
292 | |
293 | QSet<QString> QGstreamerVideoEncode::supportedStreamTypes(const QString &codecName) const |
294 | { |
295 | return m_codecs.supportedStreamTypes(codec: codecName); |
296 | } |
297 | |