1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qvideosurfacegstsink.h"
#include "qgstvideobuffer.h"
#include <QtMultimedia/QAbstractVideoSurface>
#include <QtMultimedia/QVideoFrame>
#include <QDebug>
#include <QMap>
#include <QDebug>
#include <QThread>
Q_DECLARE_METATYPE(QVideoSurfaceFormat)
QVideoSurfaceGstDelegate::QVideoSurfaceGstDelegate(QAbstractVideoSurface *surface)
: m_surface(surface)
, m_renderReturn(GST_FLOW_ERROR)
, m_bytesPerLine(0)
{
m_supportedPixelFormats = m_surface->supportedPixelFormats();
connect(m_surface, SIGNAL(supportedFormatsChanged()), this, SLOT(supportedFormatsChanged()));
}
QList<QVideoFrame::PixelFormat> QVideoSurfaceGstDelegate::supportedPixelFormats() const
{
QMutexLocker locker(const_cast<QMutex *>(&m_mutex));
return m_supportedPixelFormats;
}
bool QVideoSurfaceGstDelegate::start(const QVideoSurfaceFormat &format, int bytesPerLine)
{
QMutexLocker locker(&m_mutex);
m_format = format;
m_bytesPerLine = bytesPerLine;
if (QThread::currentThread() == thread()) {
m_started = !m_surface.isNull() ? m_surface->start(m_format) : false;
} else {
QMetaObject::invokeMethod(this, "queuedStart", Qt::QueuedConnection);
m_setupCondition.wait(&m_mutex);
}
return m_started;
}
void QVideoSurfaceGstDelegate::stop()
{
QMutexLocker locker(&m_mutex);
if (QThread::currentThread() == thread()) {
if (!m_surface.isNull())
m_surface->stop();
} else {
QMetaObject::invokeMethod(this, "queuedStop", Qt::QueuedConnection);
m_setupCondition.wait(&m_mutex);
}
m_started = false;
}
GstFlowReturn QVideoSurfaceGstDelegate::render(GstBuffer *buffer)
{
QMutexLocker locker(&m_mutex);
m_frame = QVideoFrame(
new QGstVideoBuffer(buffer, m_bytesPerLine),
m_format.frameSize(),
m_format.pixelFormat());
qint64 startTime = GST_BUFFER_TIMESTAMP(buffer);
if (startTime >= 0) {
m_frame.setStartTime(startTime/G_GINT64_CONSTANT (1000000));
qint64 duration = GST_BUFFER_DURATION(buffer);
if (duration >= 0)
m_frame.setEndTime((startTime + duration)/G_GINT64_CONSTANT (1000000));
}
QMetaObject::invokeMethod(this, "queuedRender", Qt::QueuedConnection);
if (!m_renderCondition.wait(&m_mutex, 300)) {
m_frame = QVideoFrame();
return GST_FLOW_OK;
} else {
return m_renderReturn;
}
}
void QVideoSurfaceGstDelegate::queuedStart()
{
QMutexLocker locker(&m_mutex);
m_started = m_surface->start(m_format);
m_setupCondition.wakeAll();
}
void QVideoSurfaceGstDelegate::queuedStop()
{
QMutexLocker locker(&m_mutex);
m_surface->stop();
m_setupCondition.wakeAll();
}
void QVideoSurfaceGstDelegate::queuedRender()
{
QMutexLocker locker(&m_mutex);
if (m_surface.isNull()) {
m_renderReturn = GST_FLOW_ERROR;
} else if (m_surface->present(m_frame)) {
m_renderReturn = GST_FLOW_OK;
} else {
switch (m_surface->error()) {
case QAbstractVideoSurface::NoError:
m_renderReturn = GST_FLOW_OK;
break;
case QAbstractVideoSurface::StoppedError:
m_renderReturn = GST_FLOW_NOT_NEGOTIATED;
break;
default:
m_renderReturn = GST_FLOW_ERROR;
break;
}
}
m_renderCondition.wakeAll();
}
void QVideoSurfaceGstDelegate::supportedFormatsChanged()
{
QMutexLocker locker(&m_mutex);
m_supportedPixelFormats = m_surface->supportedPixelFormats();
}
struct YuvFormat
{
QVideoFrame::PixelFormat pixelFormat;
guint32 fourcc;
int bitsPerPixel;
};
static const YuvFormat qt_yuvColorLookup[] =
{
{ QVideoFrame::Format_YUV420P, GST_MAKE_FOURCC('I','4','2','0'), 8 },
{ QVideoFrame::Format_YV12, GST_MAKE_FOURCC('Y','V','1','2'), 8 },
{ QVideoFrame::Format_UYVY, GST_MAKE_FOURCC('U','Y','V','Y'), 8 },
{ QVideoFrame::Format_YUYV, GST_MAKE_FOURCC('Y','U','Y','V'), 8 },
{ QVideoFrame::Format_YUYV, GST_MAKE_FOURCC('Y','U','Y','2'), 8 },
{ QVideoFrame::Format_NV12, GST_MAKE_FOURCC('N','V','1','2'), 8 },
{ QVideoFrame::Format_NV21, GST_MAKE_FOURCC('N','V','2','1'), 8 }
};
static int indexOfYuvColor(QVideoFrame::PixelFormat format)
{
const int count = sizeof(qt_yuvColorLookup) / sizeof(YuvFormat);
for (int i = 0; i < count; ++i)
if (qt_yuvColorLookup[i].pixelFormat == format)
return i;
return -1;
}
static int indexOfYuvColor(guint32 fourcc)
{
const int count = sizeof(YuvFormat) / sizeof(YuvFormat);
for (int i = 0; i < count; ++i)
if (qt_yuvColorLookup[i].fourcc == fourcc)
return i;
return -1;
}
struct RgbFormat
{
QVideoFrame::PixelFormat pixelFormat;
int bitsPerPixel;
int depth;
int endianness;
int red;
int green;
int blue;
int alpha;
};
static const RgbFormat qt_rgbColorLookup[] =
{
{ QVideoFrame::Format_RGB32 , 32, 24, 4321, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 },
{ QVideoFrame::Format_RGB32 , 32, 24, 1234, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
{ QVideoFrame::Format_ARGB32, 32, 24, 4321, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF },
{ QVideoFrame::Format_ARGB32, 32, 24, 1234, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
{ QVideoFrame::Format_RGB24 , 24, 24, 4321, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
{ QVideoFrame::Format_RGB565, 16, 16, 1234, 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }
};
static int indexOfRgbColor(
int bits, int depth, int endianness, int red, int green, int blue, int alpha)
{
const int count = sizeof(qt_rgbColorLookup) / sizeof(RgbFormat);
for (int i = 0; i < count; ++i) {
if (qt_rgbColorLookup[i].bitsPerPixel == bits
&& qt_rgbColorLookup[i].depth == depth
&& qt_rgbColorLookup[i].endianness == endianness
&& qt_rgbColorLookup[i].red == red
&& qt_rgbColorLookup[i].green == green
&& qt_rgbColorLookup[i].blue == blue
&& qt_rgbColorLookup[i].alpha == alpha) {
return i;
}
}
return -1;
}
static GstVideoSinkClass *sink_parent_class;
#define VO_SINK(s) QVideoSurfaceGstSink *sink(reinterpret_cast<QVideoSurfaceGstSink *>(s))
QVideoSurfaceGstSink *QVideoSurfaceGstSink::createSink(QAbstractVideoSurface *surface)
{
QVideoSurfaceGstSink *sink = reinterpret_cast<QVideoSurfaceGstSink *>(
g_object_new(QVideoSurfaceGstSink::get_type(), 0));
sink->delegate = new QVideoSurfaceGstDelegate(surface);
return sink;
}
GType QVideoSurfaceGstSink::get_type()
{
static GType type = 0;
if (type == 0) {
static const GTypeInfo info =
{
sizeof(QVideoSurfaceGstSinkClass), // class_size
base_init, // base_init
NULL, // base_finalize
class_init, // class_init
NULL, // class_finalize
NULL, // class_data
sizeof(QVideoSurfaceGstSink), // instance_size
0, // n_preallocs
instance_init, // instance_init
0 // value_table
};
type = g_type_register_static(
GST_TYPE_VIDEO_SINK, "QVideoSurfaceGstSink", &info, GTypeFlags(0));
}
return type;
}
void QVideoSurfaceGstSink::class_init(gpointer g_class, gpointer class_data)
{
Q_UNUSED(class_data);
sink_parent_class = reinterpret_cast<GstVideoSinkClass *>(g_type_class_peek_parent(g_class));
GstBaseSinkClass *base_sink_class = reinterpret_cast<GstBaseSinkClass *>(g_class);
base_sink_class->get_caps = QVideoSurfaceGstSink::get_caps;
base_sink_class->set_caps = QVideoSurfaceGstSink::set_caps;
// base_sink_class->buffer_alloc = QVideoSurfaceGstSink::buffer_alloc; // Not implemented.
base_sink_class->start = QVideoSurfaceGstSink::start;
base_sink_class->stop = QVideoSurfaceGstSink::stop;
// base_sink_class->unlock = QVideoSurfaceGstSink::unlock; // Not implemented.
// base_sink_class->event = QVideoSurfaceGstSink::event; // Not implemented.
base_sink_class->preroll = QVideoSurfaceGstSink::preroll;
base_sink_class->render = QVideoSurfaceGstSink::render;
GstElementClass *element_class = reinterpret_cast<GstElementClass *>(g_class);
element_class->change_state = QVideoSurfaceGstSink::change_state;
GObjectClass *object_class = reinterpret_cast<GObjectClass *>(g_class);
object_class->finalize = QVideoSurfaceGstSink::finalize;
}
void QVideoSurfaceGstSink::base_init(gpointer g_class)
{
static GstStaticPadTemplate sink_pad_template = GST_STATIC_PAD_TEMPLATE(
"sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS(
"video/x-raw-rgb, "
"framerate = (fraction) [ 0, MAX ], "
"width = (int) [ 1, MAX ], "
"height = (int) [ 1, MAX ]; "
"video/x-raw-yuv, "
"framerate = (fraction) [ 0, MAX ], "
"width = (int) [ 1, MAX ], "
"height = (int) [ 1, MAX ]"));
gst_element_class_add_pad_template(
GST_ELEMENT_CLASS(g_class), gst_static_pad_template_get(&sink_pad_template));
}
void QVideoSurfaceGstSink::instance_init(GTypeInstance *instance, gpointer g_class)
{
VO_SINK(instance);
Q_UNUSED(g_class);
sink->delegate = 0;
}
void QVideoSurfaceGstSink::finalize(GObject *object)
{
Q_UNUSED(object);
}
GstStateChangeReturn QVideoSurfaceGstSink::change_state(
GstElement *element, GstStateChange transition)
{
Q_UNUSED(element);
return GST_ELEMENT_CLASS(sink_parent_class)->change_state(
element, transition);
}
GstCaps *QVideoSurfaceGstSink::get_caps(GstBaseSink *base)
{
VO_SINK(base);
GstCaps *caps = gst_caps_new_empty();
foreach (QVideoFrame::PixelFormat format, sink->delegate->supportedPixelFormats()) {
int index = indexOfYuvColor(format);
if (index != -1) {
gst_caps_append_structure(caps, gst_structure_new(
"video/x-raw-yuv",
"framerate", GST_TYPE_FRACTION_RANGE, 0, 1, INT_MAX, 1,
"width" , GST_TYPE_INT_RANGE, 1, INT_MAX,
"height" , GST_TYPE_INT_RANGE, 1, INT_MAX,
"format" , GST_TYPE_FOURCC, qt_yuvColorLookup[index].fourcc,
NULL));
continue;
}
const int count = sizeof(qt_rgbColorLookup) / sizeof(RgbFormat);
for (int i = 0; i < count; ++i) {
if (qt_rgbColorLookup[i].pixelFormat == format) {
GstStructure *structure = gst_structure_new(
"video/x-raw-rgb",
"framerate" , GST_TYPE_FRACTION_RANGE, 0, 1, INT_MAX, 1,
"width" , GST_TYPE_INT_RANGE, 1, INT_MAX,
"height" , GST_TYPE_INT_RANGE, 1, INT_MAX,
"bpp" , G_TYPE_INT, qt_rgbColorLookup[i].bitsPerPixel,
"depth" , G_TYPE_INT, qt_rgbColorLookup[i].depth,
"endianness", G_TYPE_INT, qt_rgbColorLookup[i].endianness,
"red_mask" , G_TYPE_INT, qt_rgbColorLookup[i].red,
"green_mask", G_TYPE_INT, qt_rgbColorLookup[i].green,
"blue_mask" , G_TYPE_INT, qt_rgbColorLookup[i].blue,
NULL);
if (qt_rgbColorLookup[i].alpha != 0) {
gst_structure_set(
structure, "alpha_mask", G_TYPE_INT, qt_rgbColorLookup[i].alpha, NULL);
}
gst_caps_append_structure(caps, structure);
}
}
}
return caps;
}
gboolean QVideoSurfaceGstSink::set_caps(GstBaseSink *base, GstCaps *caps)
{
VO_SINK(base);
if (!caps) {
sink->delegate->stop();
return TRUE;
} else {
const GstStructure *structure = gst_caps_get_structure(caps, 0);
//qDebug() << gst_caps_to_string(caps);
QVideoFrame::PixelFormat pixelFormat = QVideoFrame::Format_Invalid;
int bitsPerPixel = 0;
QSize size;
gst_structure_get_int(structure, "width", &size.rwidth());
gst_structure_get_int(structure, "height", &size.rheight());
if (qstrcmp(gst_structure_get_name(structure), "video/x-raw-yuv") == 0) {
guint32 fourcc = 0;
gst_structure_get_fourcc(structure, "format", &fourcc);
int index = indexOfYuvColor(fourcc);
if (index != -1) {
pixelFormat = qt_yuvColorLookup[index].pixelFormat;
bitsPerPixel = qt_yuvColorLookup[index].bitsPerPixel;
}
} else if (qstrcmp(gst_structure_get_name(structure), "video/x-raw-rgb") == 0) {
int depth = 0;
int endianness = 0;
int red = 0;
int green = 0;
int blue = 0;
int alpha = 0;
gst_structure_get_int(structure, "bpp", &bitsPerPixel);
gst_structure_get_int(structure, "depth", &depth);
gst_structure_get_int(structure, "endianness", &endianness);
gst_structure_get_int(structure, "red_mask", &red);
gst_structure_get_int(structure, "green_mask", &green);
gst_structure_get_int(structure, "blue_mask", &blue);
gst_structure_get_int(structure, "alpha_mask", &alpha);
int index = indexOfRgbColor(bitsPerPixel, depth, endianness, red, green, blue, alpha);
if (index != -1)
pixelFormat = qt_rgbColorLookup[index].pixelFormat;
}
if (pixelFormat != QVideoFrame::Format_Invalid) {
QVideoSurfaceFormat format(size, pixelFormat);
QPair<int, int> rate;
gst_structure_get_fraction(structure, "framerate", &rate.first, &rate.second);
if (rate.second)
format.setFrameRate(qreal(rate.first)/rate.second);
gint aspectNum = 0;
gint aspectDenum = 0;
if (gst_structure_get_fraction(
structure, "pixel-aspect-ratio", &aspectNum, &aspectDenum)) {
if (aspectDenum > 0)
format.setPixelAspectRatio(aspectNum, aspectDenum);
}
int bytesPerLine = ((size.width() * bitsPerPixel / 8) + 3) & ~3;
if (sink->delegate->start(format, bytesPerLine))
return TRUE;
}
}
return FALSE;
}
GstFlowReturn QVideoSurfaceGstSink::buffer_alloc(
GstBaseSink *base, guint64 offset, guint size, GstCaps *caps, GstBuffer **buffer)
{
// Should implement this when the buffer pool situation is sorted.
Q_UNUSED(base);
Q_UNUSED(offset);
Q_UNUSED(size);
Q_UNUSED(caps);
Q_UNUSED(buffer);
return GST_FLOW_NOT_SUPPORTED;
}
gboolean QVideoSurfaceGstSink::start(GstBaseSink *base)
{
Q_UNUSED(base);
return TRUE;
}
gboolean QVideoSurfaceGstSink::stop(GstBaseSink *base)
{
Q_UNUSED(base);
return TRUE;
}
gboolean QVideoSurfaceGstSink::unlock(GstBaseSink *base)
{
Q_UNUSED(base);
return TRUE;
}
gboolean QVideoSurfaceGstSink::event(GstBaseSink *base, GstEvent *event)
{
Q_UNUSED(base);
Q_UNUSED(event);
return TRUE;
}
GstFlowReturn QVideoSurfaceGstSink::preroll(GstBaseSink *base, GstBuffer *buffer)
{
VO_SINK(base);
return sink->delegate->render(buffer);
}
GstFlowReturn QVideoSurfaceGstSink::render(GstBaseSink *base, GstBuffer *buffer)
{
VO_SINK(base);
return sink->delegate->render(buffer);
}
|