glwidget.cpp 52.2 KB
Newer Older
1
/*
2
3
4
 * Copyright (c) 2011-2016 Meltytech, LLC
 * Original author: Dan Dennedy <dan@dennedy.org>
 * Modified for Kdenlive: Jean-Baptiste Mardelle
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * GL shader based on BSD licensed code from Peter Bengtsson:
 * http://www.fourcc.org/source/YUV420P-OpenGL-GLSLang.c
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtWidgets>
#include <QOpenGLFunctions_3_2_Core>
#include <QUrl>
#include <QtQml>
27
#include <QQuickItem>
28
29
30
31

#include <mlt++/Mlt.h>
#include "glwidget.h"
#include "core.h"
32
#include "qml/qmlaudiothumb.h"
33
34
35
#include "kdenlivesettings.h"
#include "mltcontroller/bincontroller.h"

36
37
38
39
40
41
42
43
#ifndef GL_UNPACK_ROW_LENGTH
# ifdef GL_UNPACK_ROW_LENGTH_EXT
#  define GL_UNPACK_ROW_LENGTH GL_UNPACK_ROW_LENGTH_EXT
# else
#  error GL_UNPACK_ROW_LENGTH undefined
# endif
#endif

44
45
46
47
48
#ifdef QT_NO_DEBUG
#define check_error(fn) {}
#else
#define check_error(fn) { int err = fn->glGetError(); if (err != GL_NO_ERROR) { qCritical() << "GL error"  << hex << err << dec << "at" << __FILE__ << ":" << __LINE__; } }
#endif
49
50
51
52
53
54
55
56
57
58
59
60

#ifndef GL_TIMEOUT_IGNORED
#define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
#endif

#ifndef Q_OS_WIN
typedef GLenum (*ClientWaitSync_fp) (GLsync sync, GLbitfield flags, GLuint64 timeout);
static ClientWaitSync_fp ClientWaitSync = 0;
#endif

using namespace Mlt;

61
GLWidget::GLWidget(int id, QObject *parent)
62
    : QQuickView((QWindow*) parent)
Vincent Pinon's avatar
Vincent Pinon committed
63
    , sendFrameForAnalysis(false)
64
    , m_id(id)
65
66
    , m_shader(0)
    , m_glslManager(0)
Vincent Pinon's avatar
Vincent Pinon committed
67
68
    , m_consumer(0)
    , m_producer(0)
69
    , m_initSem(0)
70
    , m_analyseSem(1)
71
72
73
74
75
    , m_isInitialized(false)
    , m_threadStartEvent(0)
    , m_threadStopEvent(0)
    , m_threadCreateEvent(0)
    , m_threadJoinEvent(0)
76
    , m_displayEvent(0)
77
    , m_frameRenderer(0)
78
79
80
81
82
    , m_projectionLocation(0)
    , m_modelViewLocation(0)
    , m_vertexLocation(0)
    , m_texCoordLocation(0)
    , m_colorspaceLocation(0)
83
    , m_zoom(1.0f)
84
    , m_openGLSync(false)
85
    , m_offset(QPoint(0, 0))
86
    , m_shareContext(0)
87
    , m_audioWaveDisplayed(false)
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
88
    , m_fbo(NULL)
89
90
91
92
93
{
    m_texture[0] = m_texture[1] = m_texture[2] = 0;
    qRegisterMetaType<Mlt::Frame>("Mlt::Frame");
    qRegisterMetaType<SharedFrame>("SharedFrame");

94
    qmlRegisterType<QmlAudioThumb>("AudioThumb", 1, 0, "QmlAudioThumb");
95
96
97
98
    setPersistentOpenGLContext(true);
    setPersistentSceneGraph(true);
    setClearBeforeRendering(false);
    setResizeMode(QQuickView::SizeRootObjectToView);
99

100
    m_monitorProfile = new Mlt::Profile();
101
102

    if (KdenliveSettings::gpu_accel())
103
        m_glslManager = new Mlt::Filter(*m_monitorProfile, "glsl.manager");
104
105
106
    if ((m_glslManager && !m_glslManager->is_valid())) {
        delete m_glslManager;
        m_glslManager = 0;
107
108
109
110
        KdenliveSettings::setGpu_accel(false);
        // Need to destroy MLT global reference to prevent filters from trying to use GPU.
        mlt_properties_set_data(mlt_global_properties(), "glslManager", NULL, 0, NULL, NULL);
        emit gpuNotSupported();
111
    }
112
    connect(this, SIGNAL(sceneGraphInitialized()), SLOT(createOffscreen()));
113
    connect(this, SIGNAL(sceneGraphInitialized()), SLOT(initializeGL()), Qt::DirectConnection);
114
115
116
117
118
119
120
121
122
123
    connect(this, SIGNAL(beforeRendering()), SLOT(paintGL()), Qt::DirectConnection);
}

GLWidget::~GLWidget()
{
    delete m_glslManager;
    delete m_threadStartEvent;
    delete m_threadStopEvent;
    delete m_threadCreateEvent;
    delete m_threadJoinEvent;
124
    delete m_displayEvent;
125
126
127
128
129
130
131
132
133
    if (m_frameRenderer) {
        if (m_frameRenderer->isRunning()) {
            QMetaObject::invokeMethod(m_frameRenderer, "cleanup");
            m_frameRenderer->quit();
            m_frameRenderer->wait();
            m_frameRenderer->deleteLater();
        } else {
            delete m_frameRenderer;
        }
134
    }
135
    delete m_shareContext;
136
    delete m_shader;
137
    delete m_monitorProfile;
138
139
}

140
void GLWidget::updateAudioForAnalysis()
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
141
{
142
143
    if (m_frameRenderer) 
	m_frameRenderer->sendAudioForAnalysis = KdenliveSettings::monitor_audio();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
144
145
}

146
void GLWidget::createOffscreen()
147
{
148
149
150
151
    if (!m_offscreenSurface.isValid()) {
        m_offscreenSurface.setFormat(openglContext()->format());
        m_offscreenSurface.create();
    }
152
153
154
155
156
}

void GLWidget::initializeGL()
{
    if (m_isInitialized || !isVisible() || !openglContext()) return;
157
    initializeOpenGLFunctions();
158
159
    qDebug() << "OpenGL vendor: " << QString::fromUtf8((const char*) glGetString(GL_VENDOR));
    qDebug() << "OpenGL renderer: " << QString::fromUtf8((const char*) glGetString(GL_RENDERER));
160
161
162
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
    qDebug() << "OpenGL Threaded: "<<openglContext()->supportsThreadedOpenGL();
#endif
163
164
165
    qDebug() << "OpenGL ARG_SYNC: "<<openglContext()->hasExtension("GL_ARB_sync");
    qDebug() << "OpenGL OpenGLES: "<<openglContext()->isOpenGLES();

166
167
168
169
170
171
172
173
    if (m_glslManager && openglContext()->isOpenGLES()) {
        delete m_glslManager;
        m_glslManager = 0;
        KdenliveSettings::setGpu_accel(false);
        // Need to destroy MLT global reference to prevent filters from trying to use GPU.
        mlt_properties_set_data(mlt_global_properties(), "glslManager", NULL, 0, NULL, NULL);
        emit gpuNotSupported();
    }
174
175
    createShader();

176
#if !defined(Q_OS_WIN)
177
178
    // getProcAddress is not working for me on Windows.
    if (KdenliveSettings::gpu_accel()) {
179
        m_openGLSync = false;
180
181
        if (m_glslManager && openglContext()->hasExtension("GL_ARB_sync")) {
            ClientWaitSync = (ClientWaitSync_fp) openglContext()->getProcAddress("glClientWaitSync");
182
183
            if (ClientWaitSync) {
                m_openGLSync = true;
184
185
186
187
188
            } else {
                qDebug()<<"  / / // NO GL SYNC, ERROR";
                emit gpuNotSupported();
                delete m_glslManager;
                m_glslManager = 0;
189
            }
190
191
192
193
194
        }
    }
#endif

    openglContext()->doneCurrent();
195
196
197
198
199
200
201
202
203
204
    if (m_glslManager) {
        // Create a context sharing with this context for the RenderThread context.
        // This is needed because openglContext() is active in another thread
        // at the time that RenderThread is created.
        // See this Qt bug for more info: https://bugreports.qt.io/browse/QTBUG-44677
        m_shareContext = new QOpenGLContext;
        m_shareContext->setFormat(openglContext()->format());
        m_shareContext->setShareContext(openglContext());
        m_shareContext->create();
    }
205
    m_frameRenderer = new FrameRenderer(openglContext(), &m_offscreenSurface);
206
    m_frameRenderer->sendAudioForAnalysis = KdenliveSettings::monitor_audio();
207
    openglContext()->makeCurrent(this);
208
    //openglContext()->blockSignals(false);
209
    connect(m_frameRenderer, SIGNAL(frameDisplayed(const SharedFrame&)), this, SIGNAL(frameDisplayed(const SharedFrame&)), Qt::QueuedConnection);
210
211
212
213
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
    if (KdenliveSettings::gpu_accel() || openglContext()->supportsThreadedOpenGL())
        connect(m_frameRenderer, SIGNAL(textureReady(GLuint,GLuint,GLuint)), SLOT(updateTexture(GLuint,GLuint,GLuint)), Qt::DirectConnection);
    else
214
        connect(m_frameRenderer, SIGNAL(frameDisplayed(const SharedFrame&)), SLOT(onFrameDisplayed(const SharedFrame&)), Qt::QueuedConnection);
215
216
217
#else
    connect(m_frameRenderer, SIGNAL(frameDisplayed(const SharedFrame&)), SLOT(onFrameDisplayed(const SharedFrame&)), Qt::QueuedConnection);
#endif
218
219

    connect(m_frameRenderer, SIGNAL(audioSamplesSignal(const audioShortVector&,int,int,int)), this, SIGNAL(audioSamplesSignal(const audioShortVector&,int,int,int)), Qt::QueuedConnection);
220
    connect(this, &GLWidget::textureUpdated, this, &GLWidget::update, Qt::QueuedConnection);
221
    m_initSem.release();
222
    m_isInitialized = true;
223
224
225
226
227
228
}

void GLWidget::resizeGL(int width, int height)
{
    int x, y, w, h;
    double this_aspect = (double) width / height;
229
    double video_aspect = m_monitorProfile->dar();
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

    // Special case optimisation to negate odd effect of sample aspect ratio
    // not corresponding exactly with image resolution.
    if ((int) (this_aspect * 1000) == (int) (video_aspect * 1000))
    {
        w = width;
        h = height;
    }
    // Use OpenGL to normalise sample aspect ratio
    else if (height * video_aspect > width)
    {
        w = width;
        h = width / video_aspect;
    }
    else
    {
        w = height * video_aspect;
        h = height;
    }
    x = (width - w) / 2;
    y = (height - h) / 2;
    m_rect.setRect(x, y, w, h);
252
253
    double scalex = (double) m_rect.width() / m_monitorProfile->width() * m_zoom;
    double scaley = (double) m_rect.width() / ((double) m_monitorProfile->height() * m_monitorProfile->dar() / m_monitorProfile->width()) / m_monitorProfile->width() * m_zoom;
254
    QPoint center = m_rect.center();
255
256
257
    QQuickItem* rootQml = rootObject();
    if (rootQml) {
        rootQml->setProperty("center", center);
258
259
        rootQml->setProperty("scalex", scalex);
        rootQml->setProperty("scaley", scaley);
260
        if (rootQml->objectName() == QLatin1String("rootsplit")) {
261
262
263
            // Adjust splitter pos
            rootQml->setProperty("splitterPos", x + (rootQml->property("realpercent").toDouble() * w));
        }
264
    }
265
266
267
268
269
270
271
272
273
274
275
276
277
    emit rectChanged();
}

void GLWidget::resizeEvent(QResizeEvent* event)
{
    QQuickView::resizeEvent(event);
    resizeGL(event->size().width(), event->size().height());
}

void GLWidget::createShader()
{
    m_shader = new QOpenGLShaderProgram;
    m_shader->addShaderFromSourceCode(QOpenGLShader::Vertex,
278
279
280
281
282
                                     "uniform highp mat4 projection;"
                                      "uniform highp mat4 modelView;"
                                     "attribute highp vec4 vertex;"
                                     "attribute highp vec2 texCoord;"
                                     "varying highp vec2 coordinates;"
283
284
285
286
287
288
289
                                     "void main(void) {"
                                     "  gl_Position = projection * modelView * vertex;"
                                     "  coordinates = texCoord;"
                                     "}");
    if (m_glslManager) {
        m_shader->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                          "uniform sampler2D tex;"
290
                                          "varying highp vec2 coordinates;"
291
292
293
294
295
296
297
298
                                          "void main(void) {"
                                          "  gl_FragColor = texture2D(tex, coordinates);"
                                          "}");
        m_shader->link();
        m_textureLocation[0] = m_shader->uniformLocation("tex");
    } else {
        m_shader->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                          "uniform sampler2D Ytex, Utex, Vtex;"
299
300
                                          "uniform lowp int colorspace;"
                                          "varying highp vec2 coordinates;"
301
                                          "void main(void) {"
302
                                          "  mediump vec3 texel;"
303
304
305
                                          "  texel.r = texture2D(Ytex, coordinates).r - 0.0625;" // Y
                                          "  texel.g = texture2D(Utex, coordinates).r - 0.5;"    // U
                                          "  texel.b = texture2D(Vtex, coordinates).r - 0.5;"    // V
306
                                          "  mediump mat3 coefficients;"
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
                                          "  if (colorspace == 601) {"
                                          "    coefficients = mat3("
                                          "      1.1643,  1.1643,  1.1643," // column 1
                                          "      0.0,    -0.39173, 2.017," // column 2
                                          "      1.5958, -0.8129,  0.0);" // column 3
                                          "  } else {" // ITU-R 709
                                          "    coefficients = mat3("
                                          "      1.1643, 1.1643, 1.1643," // column 1
                                          "      0.0,   -0.213,  2.112," // column 2
                                          "      1.793, -0.533,  0.0);" // column 3
                                          "  }"
                                          "  gl_FragColor = vec4(coefficients * texel, 1.0);"
                                          "}");
        m_shader->link();
        m_textureLocation[0] = m_shader->uniformLocation("Ytex");
        m_textureLocation[1] = m_shader->uniformLocation("Utex");
        m_textureLocation[2] = m_shader->uniformLocation("Vtex");
        m_colorspaceLocation = m_shader->uniformLocation("colorspace");
    }
    m_projectionLocation = m_shader->uniformLocation("projection");
    m_modelViewLocation = m_shader->uniformLocation("modelView");
    m_vertexLocation = m_shader->attributeLocation("vertex");
    m_texCoordLocation = m_shader->attributeLocation("texCoord");
}

332
static void uploadTextures(QOpenGLContext* context, const SharedFrame& frame, GLuint texture[])
333
334
335
336
337
338
339
{
    int width = frame.get_image_width();
    int height = frame.get_image_height();
    const uint8_t* image = frame.get_image();
    QOpenGLFunctions* f = context->functions();

    // Upload each plane of YUV to a texture.
340
    if (texture[0])
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        f->glDeleteTextures(3, texture);
    check_error(f);
    f->glGenTextures(3, texture);
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[0]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
Vincent Pinon's avatar
Vincent Pinon committed
356
357
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
358
359
360
361
362
363
364
365
366
367
368
369
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[1]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
370
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0,
Vincent Pinon's avatar
Vincent Pinon committed
371
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height);
372
373
374
375
376
377
378
379
380
381
382
383
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[2]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
384
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0,
Vincent Pinon's avatar
Vincent Pinon committed
385
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height + width/2 * height/2);
386
387
388
    check_error(f);
}

389
390
391
392
393
394
void GLWidget::clear()
{
    stopGlsl();
    update();
}

395
396
397
398
399
void GLWidget::releaseAnalyse()
{
    m_analyseSem.release();
}

400
401
void GLWidget::paintGL()
{
402
    if (m_glslManager && !m_texture[0]) return;
403
    QOpenGLFunctions* f = openglContext()->functions();
404
405
406
    int width = this->width() * devicePixelRatio();
    int height = this->height() * devicePixelRatio();

407
408
409
410
    f->glDisable(GL_BLEND);
    f->glDisable(GL_DEPTH_TEST);
    f->glDepthMask(GL_FALSE);
    f->glViewport(0, 0, width, height);
411
    check_error(f);
412
    QColor color(KdenliveSettings::window_background());
413
414
    f->glClearColor(color.redF(), color.greenF(), color.blueF(), color.alphaF());
    f->glClear(GL_COLOR_BUFFER_BIT);
415
    check_error(f);
416

417
418
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
    if (!(m_glslManager || openglContext()->supportsThreadedOpenGL())) {
419
420
421
422
423
424
425
        m_mutex.lock();
        if (!m_sharedFrame.is_valid()) {
            m_mutex.unlock();
            return;
        }
        uploadTextures(openglContext(), m_sharedFrame, m_texture);
        m_mutex.unlock();
426
427
    }
#endif
428
429
430
431

    // Bind textures.
    for (int i = 0; i < 3; ++i) {
        if (m_texture[i]) {
432
433
            f->glActiveTexture(GL_TEXTURE0 + i);
            f->glBindTexture(GL_TEXTURE_2D, m_texture[i]);
434
            check_error(f);
435
436
437
438
439
440
441
442
443
444
        }
    }
    // Init shader program.
    m_shader->bind();
    if (m_glslManager) {
        m_shader->setUniformValue(m_textureLocation[0], 0);
    } else {
        m_shader->setUniformValue(m_textureLocation[0], 0);
        m_shader->setUniformValue(m_textureLocation[1], 1);
        m_shader->setUniformValue(m_textureLocation[2], 2);
445
        m_shader->setUniformValue(m_colorspaceLocation, m_monitorProfile->colorspace());
446
    }
447
    check_error(f);
448
449
450
451
452

    // Setup an orthographic projection.
    QMatrix4x4 projection;
    projection.scale(2.0f / width, 2.0f / height);
    m_shader->setUniformValue(m_projectionLocation, projection);
453
    check_error(f);
454
455
456

    // Set model view.
    QMatrix4x4 modelView;
457
    if (m_zoom != 1.0) {
458
459
460
461
462
463
        if (offset().x() || offset().y())
            modelView.translate(-offset().x() * devicePixelRatio(),
                                 offset().y() * devicePixelRatio());
        modelView.scale(zoom(), zoom());
    }
    m_shader->setUniformValue(m_modelViewLocation, modelView);
464
    check_error(f);
465
466
467
468
469
470
471
472
473
474

    // Provide vertices of triangle strip.
    QVector<QVector2D> vertices;
    width = m_rect.width() * devicePixelRatio();
    height = m_rect.height() * devicePixelRatio();
    vertices << QVector2D(float(-width)/2.0f, float(-height)/2.0f);
    vertices << QVector2D(float(-width)/2.0f, float( height)/2.0f);
    vertices << QVector2D(float( width)/2.0f, float(-height)/2.0f);
    vertices << QVector2D(float( width)/2.0f, float( height)/2.0f);
    m_shader->enableAttributeArray(m_vertexLocation);
475
    check_error(f);
476
    m_shader->setAttributeArray(m_vertexLocation, vertices.constData());
477
    check_error(f);
478
479
480
481
482
483
484
485

    // Provide texture coordinates.
    QVector<QVector2D> texCoord;
    texCoord << QVector2D(0.0f, 1.0f);
    texCoord << QVector2D(0.0f, 0.0f);
    texCoord << QVector2D(1.0f, 1.0f);
    texCoord << QVector2D(1.0f, 0.0f);
    m_shader->enableAttributeArray(m_texCoordLocation);
486
    check_error(f);
487
    m_shader->setAttributeArray(m_texCoordLocation, texCoord.constData());
488
    check_error(f);
489
490

    // Render
491
    f->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
492
    check_error(f);
493

494
    if (sendFrameForAnalysis && m_analyseSem.tryAcquire(1)) {
495
496
497
498
        // Render RGB frame for analysis
        int fullWidth = m_monitorProfile->width();
        int fullHeight = m_monitorProfile->height();
        if (!m_fbo || m_fbo->size() != QSize(fullWidth, fullHeight)) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
499
            delete m_fbo;
500
            QOpenGLFramebufferObjectFormat fmt;
501
            fmt.setSamples(1);
502
503
            fmt.setInternalTextureFormat(GL_RGB); //GL_RGBA32F);  // which one is the fastest ?
            m_fbo = new QOpenGLFramebufferObject(fullWidth, fullHeight, fmt); //GL_TEXTURE_2D);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
504
505
        }
        m_fbo->bind();
506
        f->glViewport(0, 0, fullWidth, fullHeight);
507
508
509
510
511

        QMatrix4x4 projection2;
        projection2.scale(2.0f / width, 2.0f / height);
        m_shader->setUniformValue(m_projectionLocation, projection2);

512
        f->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
513
        check_error(f);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
514
515
516
        m_fbo->release();
        emit analyseFrame(m_fbo->toImage());
    }
517
518
519
520
521
522
    // Cleanup
    m_shader->disableAttributeArray(m_vertexLocation);
    m_shader->disableAttributeArray(m_texCoordLocation);
    m_shader->release();
    for (int i = 0; i < 3; ++i) {
        if (m_texture[i]) {
523
524
            f->glActiveTexture(GL_TEXTURE0 + i);
            f->glBindTexture(GL_TEXTURE_2D, 0);
525
            check_error(f);
526
527
        }
    }
528
    f->glActiveTexture(GL_TEXTURE0);
529
    check_error(f);
530
531
}

532
533
534
535
536
537
538
539
540
541
542
543
544
void GLWidget::slotZoomScene(double value)
{
    if (value >= 3) {
        setZoom(value - 2.0f);
    } else if (value == 2) {
        setZoom(0.5);
    } else if (value == 1) {
        setZoom(0.25);
    } else if (value == 0) {
        setZoom(0.125);
    }
}

545
546
void GLWidget::wheelEvent(QWheelEvent * event)
{
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    if (event->modifiers() & Qt::ControlModifier && event->modifiers() & Qt::ShiftModifier) {
        if (event->delta() > 0) {
            if (m_zoom == 1.0f) {
                setZoom(2.0f);
            }
            else if (m_zoom == 2.0f) {
                setZoom(3.0f);
            }
            else if (m_zoom < 1.0f) {
                setZoom(m_zoom * 2);
            }
        }
        else {
            if (m_zoom == 3.0f) {
                setZoom(2.0f);
            }
            else if (m_zoom == 2.0f) {
                setZoom(1.0f);
            }
566
            else if (m_zoom > 0.2) {
567
568
569
570
571
                setZoom(m_zoom / 2);
            }
        }
        return;
    }
572
    emit mouseSeek(event->delta(), (int) event->modifiers());
573
574
575
576
577
578
    event->accept();
}


void GLWidget::mousePressEvent(QMouseEvent* event)
{
579
    if (rootObject() && rootObject()->objectName() != QLatin1String("root") && !(event->modifiers() & Qt::ControlModifier) && !(event->buttons() & Qt::MiddleButton)) {
580
        event->ignore();
581
        QQuickView::mousePressEvent(event);
582
583
        return;
    }
584
    if (event->button() & Qt::LeftButton) {
585
586
587
588
589
590
591
        if (event->modifiers() & Qt::ControlModifier) {
            // Pan view
            m_panStart = event->pos();
            setCursor(Qt::ClosedHandCursor);
        } else {
            m_dragStart = event->pos();
        }
592
593
594
    }
    else if (event->button() & Qt::RightButton) {
        emit showContextMenu(event->globalPos());
595
596
597
    } else if (event->button() & Qt::MiddleButton) {
        m_panStart = event->pos();
        setCursor(Qt::ClosedHandCursor);
598
    }
599
    event->accept();
600
    QQuickView::mousePressEvent(event);
601
602
603
604
}

void GLWidget::mouseMoveEvent(QMouseEvent* event)
{
605
    if (rootObject() && rootObject()->objectName() != QLatin1String("root") && !(event->modifiers() & Qt::ControlModifier) && !(event->buttons() & Qt::MiddleButton)) {
606
        event->ignore();
607
        QQuickView::mouseMoveEvent(event);
608
609
        return;
    }
610
    /*    if (event->modifiers() == Qt::ShiftModifier && m_producer) {
611
612
613
        emit seekTo(m_producer->get_length() *  event->x() / width());
        return;
    }*/
614
615
616
617
618
619
620
621
    QQuickView::mouseMoveEvent(event);
    if (!m_panStart.isNull()) {
        emit panView(m_panStart - event->pos());
        m_panStart = event->pos();
        event->accept();
        QQuickView::mouseMoveEvent(event);
        return;
    }
622
623
    if (!(event->buttons() & Qt::LeftButton)) {
        QQuickView::mouseMoveEvent(event);
624
        return;
625
    }
626
    if (!event->isAccepted() && !m_dragStart.isNull() &&  (event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) {
627
628
629
        m_dragStart = QPoint();
        emit startDrag();
    }
630
631
632
633
}

void GLWidget::keyPressEvent(QKeyEvent* event)
{
634
    QQuickView::keyPressEvent(event);
635
636
637
    if (!event->isAccepted()) {
        emit passKeyEvent(event);
    }
638
639
640
641
642
643
644
645
646
647
648
649
650
}

void GLWidget::createThread(RenderThread **thread, thread_function_t function, void *data)
{
#ifdef Q_OS_WIN
    // On Windows, MLT event consumer-thread-create is fired from the Qt main thread.
    while (!m_isInitialized)
        qApp->processEvents();
#else
    if (!m_isInitialized) {
        m_initSem.acquire();
    }
#endif
651
    (*thread) = new RenderThread(function, data, m_shareContext, &m_offscreenSurface);
652
653
654
655
656
657
658
659
    (*thread)->start();
}

static void onThreadCreate(mlt_properties owner, GLWidget* self,
    RenderThread** thread, int* priority, thread_function_t function, void* data )
{
    Q_UNUSED(owner)
    Q_UNUSED(priority)
660
    //self->clearFrameRenderer();
661
    self->createThread(thread, function, data);
662
    self->lockMonitor();
663
664
665
666
667
668
669
670
671
}

static void onThreadJoin(mlt_properties owner, GLWidget* self, RenderThread* thread)
{
    Q_UNUSED(owner)
    if (thread) {
        thread->quit();
        thread->wait();
        delete thread;
672
        //self->clearFrameRenderer();
673
        self->releaseMonitor();
674
675
676
677
678
679
    }
}

void GLWidget::startGlsl()
{
    if (m_glslManager) {
680
        //clearFrameRenderer();
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
        m_glslManager->fire_event("init glsl");
        if (!m_glslManager->get_int("glsl_supported")) {
            delete m_glslManager;
            m_glslManager = 0;
            // Need to destroy MLT global reference to prevent filters from trying to use GPU.
            mlt_properties_set_data(mlt_global_properties(), "glslManager", NULL, 0, NULL, NULL);
            emit gpuNotSupported();
        }
        else {
            emit started();
        }
    }
}

static void onThreadStarted(mlt_properties owner, GLWidget* self)
{
    Q_UNUSED(owner)
    self->startGlsl();
}

701
702
703
704
705
706
707
708
709
710
void GLWidget::releaseMonitor()
{
    emit lockMonitor(false);
}

void GLWidget::lockMonitor()
{
    emit lockMonitor(true);
}

711
712
void GLWidget::stopGlsl()
{
713
714
715
716
    if (m_consumer)
        m_consumer->purge();
    if (m_frameRenderer)
        m_frameRenderer->clearFrame();
717

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
718
719
720
721
722
    //TODO This is commented out for now because it is causing crashes.
    //Technically, this should be the correct thing to do, but it appears
    //some changes have created regression (see shotcut)
    //with respect to restarting the consumer in GPU mode.
    //m_glslManager->fire_event("close glsl");
723
724
725
726
727
728
729
730
731
    m_texture[0] = 0;
}

static void onThreadStopped(mlt_properties owner, GLWidget* self)
{
    Q_UNUSED(owner)
    self->stopGlsl();
}

732
733
734
735
736
737
738
739
740
741
742
743
744
745
void GLWidget::slotSwitchAudioOverlay(bool enable)
{
    KdenliveSettings::setDisplayAudioOverlay(enable);
    if (m_audioWaveDisplayed && enable == false) {
        if (m_producer && m_producer->get_int("video_index") != -1) {
            // We have a video producer, disable filter
            removeAudioOverlay();
        }
    }
    if (enable && !m_audioWaveDisplayed) {
        createAudioOverlay(m_producer->get_int("video_index") == -1);
    }
}

Vincent Pinon's avatar
Vincent Pinon committed
746
int GLWidget::setProducer(Mlt::Producer* producer)
747
748
749
{
    int error = 0;//Controller::setProducer(producer, isMulti);
    m_producer = producer;
Vincent Pinon's avatar
Vincent Pinon committed
750
    if (m_producer) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
751
        error = reconfigure();
752
753
754
755
        if (!error) {
            // The profile display aspect ratio may have changed.
            resizeGL(width(), height());
        }
Vincent Pinon's avatar
Vincent Pinon committed
756
757
    } else return error;
    if (!m_consumer) return error;
758
    if (m_producer->get_int("video_index") == -1) {
759
760
761
762
        // This is an audio only clip, attach visualization filter. Currently, the filter crashes MLT when Movit accel is used
        if (!m_audioWaveDisplayed) {
            createAudioOverlay(true);
        }
763
        else if (m_consumer) {
764
765
            if (KdenliveSettings::gpu_accel()) removeAudioOverlay();
            else adjustAudioOverlay(true);
766
767
        }
    }
768
    else if (m_audioWaveDisplayed && m_consumer) {
769
770
771
772
773
774
775
776
        // This is not an audio clip, hide wave
        if (KdenliveSettings::displayAudioOverlay()) {
            adjustAudioOverlay(m_producer->get_int("video_index") == -1);
        }
        else {
            removeAudioOverlay();
        }
    }
777
778
779
    else if (KdenliveSettings::displayAudioOverlay()) {
        createAudioOverlay(false);
    }
780
781
782
    return error;
}

783
784
785
786
787
788
789
790
791
792
int GLWidget::droppedFrames() const
{
    return (m_consumer ? m_consumer->get_int("drop_count") : 0);
}

void GLWidget::resetDrops()
{
    if (m_consumer) m_consumer->set("drop_count", 0);
}

793
794
void GLWidget::createAudioOverlay(bool isAudio)
{
Vincent Pinon's avatar
Vincent Pinon committed
795
    if (!m_consumer) return;
796
797
798
799
    if (isAudio && KdenliveSettings::gpu_accel()) {
        // Audiowaveform filter crashes on Movit + audio clips)
        return;
    }
800
    Mlt::Filter f(*m_monitorProfile, "audiowaveform");
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
    if (f.is_valid()) {
        //f.set("show_channel", 1);
        f.set("color.1", "0xffff0099");
        f.set("fill", 1);
        if (isAudio) {
            // Fill screen
            f.set("rect", "0,0,100%,100%");
        } else {
            // Overlay on lower part of the screen
            f.set("rect", "0,80%,100%,20%");
        }
        m_consumer->attach(f);
        m_audioWaveDisplayed = true;
    }
}

void GLWidget::removeAudioOverlay()
{
    Mlt::Service sourceService(m_consumer->get_service());
    // move all effects to the correct producer
    int ct = 0;
    Mlt::Filter *filter = sourceService.filter(ct);
    while (filter) {
        QString srv = filter->get("mlt_service");
825
        if (srv == QLatin1String("audiowaveform")) {
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
            sourceService.detach(*filter);
            delete filter;
            break;
        } else ct++;
        filter = sourceService.filter(ct);
    }
    m_audioWaveDisplayed = false;
}

void GLWidget::adjustAudioOverlay(bool isAudio)
{
    Mlt::Service sourceService(m_consumer->get_service());
    // move all effects to the correct producer
    int ct = 0;
    Mlt::Filter *filter = sourceService.filter(ct);
    while (filter) {
        QString srv = filter->get("mlt_service");
843
        if (srv == QLatin1String("audiowaveform")) {
844
845
846
847
848
849
850
851
852
853
854
855
            if (isAudio) {
                filter->set("rect", "0,0,100%,100%");
            }
            else {
                filter->set("rect", "0,80%,100%,20%");
            }
            break;
        } else ct++;
        filter = sourceService.filter(ct);
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
856
857
858
void GLWidget::stopCapture()
{
    if (strcmp(m_consumer->get("mlt_service"), "multi") == 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
859
        m_consumer->set("refresh", 0);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
860
861
862
863
        m_consumer->purge();
        m_consumer->stop();
    }
}
864

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
865
866
867
868
int GLWidget::reconfigureMulti(QString params, QString path, Mlt::Profile *profile)
{
    QString serviceName = property("mlt_service").toString();
    if (!m_consumer || !m_consumer->is_valid() || strcmp(m_consumer->get("mlt_service"), "multi") != 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
869
870
871
872
873
874
875
        if (m_consumer) {
            m_consumer->purge();
            m_consumer->stop();
            delete m_consumer;
        }
        m_consumer = new Mlt::FilteredConsumer(*profile, "multi");
                delete m_threadStartEvent;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
876
877
878
879
880
881
882
883
884
885
886
887
        m_threadStartEvent = 0;
        delete m_threadStopEvent;
        m_threadStopEvent = 0;

        delete m_threadCreateEvent;
        delete m_threadJoinEvent;
        if (m_consumer) {
            m_threadCreateEvent = m_consumer->listen("consumer-thread-create", this, (mlt_listener) onThreadCreate);
            m_threadJoinEvent = m_consumer->listen("consumer-thread-join", this, (mlt_listener) onThreadJoin);
        }
    }
    if (m_consumer->is_valid()) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
888
889
890
891
892
        // buid sub consumers
        //m_consumer->set("mlt_image_format", "yuv422");
        reloadProfile(*profile);
        int volume = KdenliveSettings::volume();
        m_consumer->set("0", serviceName.toUtf8().constData());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
893
894
        m_consumer->set("0.mlt_image_format", "yuv422");
        m_consumer->set("0.terminate_on_pause", 0);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
895
896
897
        //m_consumer->set("0.preview_off", 1);
        m_consumer->set("0.real_time", 0);
        m_consumer->set("0.volume", (double)volume / 100);
898

899
        if (serviceName == QLatin1String("sdl_audio")) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
900
#ifdef Q_OS_WIN
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
901
            m_consumer->set("0.audio_buffer", 2048);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
902
#else
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
903
            m_consumer->set("0.audio_buffer", 512);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
904
#endif
905
906
907
908
909
910
911
            QString audioDevice = KdenliveSettings::audiodevicename();
            if (!audioDevice.isEmpty())
                m_consumer->set("audio_device", audioDevice.toUtf8().constData());

            QString audioDriver = KdenliveSettings::audiodrivername();
            if (!audioDriver.isEmpty())
                m_consumer->set("audio_driver", audioDriver.toUtf8().constData());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
912
        }
913

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
914
915
916
917
918
        m_consumer->set("1", "avformat");
        m_consumer->set("1.target", path.toUtf8().constData());
        //m_consumer->set("1.real_time", -KdenliveSettings::mltthreads());
        m_consumer->set("terminate_on_pause", 0);
        m_consumer->set("1.terminate_on_pause", 0);
919

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
920
921
922
923
924
        //m_consumer->set("1.terminate_on_pause", 0);// was commented out. restoring it  fixes mantis#3415 - FFmpeg recording freezes
        QStringList paramList = params.split(' ', QString::SkipEmptyParts);
        for (int i = 0; i < paramList.count(); ++i) {
            QString key = "1." + paramList.at(i).section('=', 0, 0);
            QString value = paramList.at(i).section('=', 1, 1);
925
            if (value == QLatin1String("%threads")) value = QString::number(QThread::idealThreadCount());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
926
            m_consumer->set(key.toUtf8().constData(), value.toUtf8().constData());
927
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
928
        // Connect the producer to the consumer - tell it to "run" later
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
929
        delete m_displayEvent;
930
        if (m_glslManager) {
931
932
933
934
935
936
            if (m_openGLSync) {
                m_displayEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) on_gl_frame_show);
            }
            else {
                m_displayEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) on_gl_nosync_frame_show);
            }
937
938
939
        } else {
            m_displayEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) on_frame_show);
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
940
        m_consumer->connect(*m_producer);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
941
942
        m_consumer->start();
        return 0;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
943
944
945
946
947
    }
    else return -1;
}

int GLWidget::reconfigure(Mlt::Profile *profile)
948
949
950
951
{
    int error = 0;
    // use SDL for audio, OpenGL for video
    QString serviceName = property("mlt_service").toString();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
952
953
    if (profile) reloadProfile(*profile);
    if (!m_consumer || !m_consumer->is_valid() || strcmp(m_consumer->get("mlt_service"),"multi") == 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
954
955
956
957
958
        if (m_consumer) {
            m_consumer->purge();
            m_consumer->stop();
            delete m_consumer;
        }
959
        // Force rtaudio backend for movit, because with SDL it crashes on stop/start
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
960
961
        //QString audioBackend = m_glslManager == NULL ? KdenliveSettings::audiobackend() : QStringLiteral("rtaudio");
        QString audioBackend = KdenliveSettings::audiobackend();
962
963
        if (serviceName.isEmpty() || serviceName != audioBackend) {
            m_consumer = new Mlt::FilteredConsumer(*m_monitorProfile, audioBackend.toUtf8().constData());
964
            if (m_consumer->is_valid())
965
                serviceName = audioBackend;
966
            else {
967
                // Fallback
968
                serviceName = QStringLiteral("sdl_audio");
969
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
970
971
            delete m_consumer;
            m_consumer = NULL;
972
            setProperty("mlt_service", serviceName);
973
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
974
        m_consumer = new Mlt::FilteredConsumer(*m_monitorProfile, serviceName.toLatin1().constData());
975
976
977
978
979
980
981
        delete m_threadStartEvent;
        m_threadStartEvent = 0;
        delete m_threadStopEvent;
        m_threadStopEvent = 0;

        delete m_threadCreateEvent;
        delete m_threadJoinEvent;
982
        if (m_consumer) {
983
            int dropFrames = realTime();
984
985
986
987
988
            if (!KdenliveSettings::monitor_dropframes()) dropFrames = -dropFrames;
            m_consumer->set("real_time", dropFrames);
            m_threadCreateEvent = m_consumer->listen("consumer-thread-create", this, (mlt_listener) onThreadCreate);
            m_threadJoinEvent = m_consumer->listen("consumer-thread-join", this, (mlt_listener) onThreadJoin);
        }
989
990
991
    }
    if (m_consumer->is_valid()) {
        // Connect the producer to the consumer - tell it to "run" later
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
992
        if (m_producer) m_consumer->connect(*m_producer);
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
        if (m_glslManager) {
            if (!m_threadStartEvent)
                m_threadStartEvent = m_consumer->listen("consumer-thread-started", this, (mlt_listener) onThreadStarted);
            if (!m_threadStopEvent)
                m_threadStopEvent = m_consumer->listen("consumer-thread-stopped", this, (mlt_listener) onThreadStopped);
            if (!serviceName.startsWith(QLatin1String("decklink")))
                m_consumer->set("mlt_image_format", "glsl");
        } else {
            m_consumer->set("mlt_image_format", "yuv422");
        }

        delete m_displayEvent;
        if (m_glslManager) {
            m_displayEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) on_gl_frame_show);
        } else {
            m_displayEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) on_frame_show);
        }

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1011
        int volume = KdenliveSettings::volume();
1012
        if (serviceName == QLatin1String("sdl_audio")) {
1013
/*#ifdef Q_OS_WIN
1014
1015
1016
                m_consumer->set("audio_buffer", 2048);
#else
                m_consumer->set("audio_buffer", 512);
1017
#endif*/
1018
1019
1020
1021
1022
1023
1024
1025
            QString audioDevice = KdenliveSettings::audiodevicename();
            if (!audioDevice.isEmpty())
                m_consumer->set("audio_device", audioDevice.toUtf8().constData());

            QString audioDriver = KdenliveSettings::audiodrivername();
            if (!audioDriver.isEmpty())
                m_consumer->set("audio_driver", audioDriver.toUtf8().constData());
        }
1026
            /*if (!m_monitorProfile->progressive())
1027
                m_consumer->set("progressive", property("progressive").toBool());*/
1028
        m_consumer->set("volume", (double)volume / 100);
1029
        //m_consumer->set("progressive", 1);
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
        m_consumer->set("rescale", KdenliveSettings::mltinterpolation().toUtf8().constData());
        m_consumer->set("deinterlace_method", KdenliveSettings::mltdeinterlacer().toUtf8().constData());
        m_consumer->set("buffer", 25);
        m_consumer->set("prefill", 1);
        m_consumer->set("scrub_audio", 1);
        if (KdenliveSettings::monitor_gamma() == 0) {
            m_consumer->set("color_trc", "iec61966_2_1");
        }
        else {
            m_consumer->set("color_trc", "bt709");
1040
1041
1042
1043
1044
1045
1046
1047
1048
        }
    }
    else {
        // Cleanup on error
        error = 2;
    }
    return error;
}

1049

1050
float GLWidget::zoom() const
1051
{ 
1052
    return m_zoom;
1053
1054
}

1055
1056
1057
1058
1059
float GLWidget::scale() const
{ 
    return (double) m_rect.width() / m_monitorProfile->width() * m_zoom;
}

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
Mlt::Profile *GLWidget::profile()
{
    return m_monitorProfile;
}

void GLWidget::resetProfile(MltVideoProfile profile)
{
    if (m_consumer && !m_consumer->is_stopped()) {
        m_consumer->stop();
        m_consumer->purge();
    }
1071
1072
    free(m_monitorProfile->get_profile()->description );
    m_monitorProfile->get_profile()->description = strdup(profile.description.toUtf8().data());
1073
1074
1075
1076
1077
1078
1079
1080
    m_monitorProfile->set_colorspace(profile.colorspace);
    m_monitorProfile->set_frame_rate(profile.frame_rate_num, profile.frame_rate_den);
    m_monitorProfile->set_height(profile.height);
    m_monitorProfile->set_width(profile.width);
    m_monitorProfile->set_progressive(profile.progressive);
    m_monitorProfile->set_sample_aspect(profile.sample_aspect_num, profile.sample_aspect_den);
    m_monitorProfile->set_display_aspect(profile.display_aspect_num, profile.display_aspect_den);
    m_monitorProfile->set_explicit(true);
1081
    reconfigure();
1082
    refreshSceneLayout();
1083
1084
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1085
1086
void GLWidget::reloadProfile(Mlt::Profile &profile)
{
1087
    m_monitorProfile->get_profile()->description = strdup(profile.description());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
    m_monitorProfile->set_colorspace(profile.colorspace());
    m_monitorProfile->set_frame_rate(profile.frame_rate_num(), profile.frame_rate_den());
    m_monitorProfile->set_height(profile.height());
    m_monitorProfile->set_width(profile.width());
    m_monitorProfile->set_progressive(profile.progressive());
    m_monitorProfile->set_sample_aspect(profile.sample_aspect_num(), profile.sample_aspect_den());
    m_monitorProfile->set_display_aspect(profile.display_aspect_num(), profile.display_aspect_den());
    m_monitorProfile->set_explicit(true);
    // The profile display aspect ratio may have changed.
    resizeGL(width(), height());
1098
    refreshSceneLayout();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
1099
1100
}

1101
1102
QSize GLWidget::profileSize() const
{
1103
    return QSize(m_monitorProfile->width(), m_monitorProfile->height());
1104
1105
}

1106
1107
1108
1109
1110
QRect GLWidget::displayRect() const
{
    return m_rect;
}

1111
1112
QPoint GLWidget::offset() const
{
1113
1114
    return QPoint(m_offset.x() - (m_monitorProfile->width()  * m_zoom -  width()) / 2,
                  m_offset.y() - (m_monitorProfile->height() * m_zoom - height()) / 2);
1115
1116
1117
1118
}

void GLWidget::setZoom(float zoom)
{
1119
    double zoomRatio = zoom / m_zoom;
1120
1121
    m_zoom = zoom;
    emit zoomChanged();
1122
    if (rootObject()) {
1123
        rootObject()->setProperty("zoom", m_zoom);
1124
1125
1126
1127
        double scalex = rootObject()->property("scalex").toDouble() * zoomRatio;
        rootObject()->setProperty("scalex", scalex);
        double scaley = rootObject()->property("scaley").toDouble() * zoomRatio;
        rootObject()->setProperty("scaley", scaley);
1128
    }
1129
1130
1131
    update();
}

1132
1133
1134
1135
1136
void GLWidget::onFrameDisplayed(const SharedFrame &frame)
{
    m_mutex.lock();
    m_sharedFrame = frame;
    m_mutex.unlock();
1137
    update();
1138
1139
}

1140
1141
void GLWidget::mouseReleaseEvent(QMouseEvent * event)
{
1142
    QQuickView::mouseReleaseEvent(event);
1143
1144
    if (m_dragStart.isNull() && m_panStart.isNull() && rootObject() && rootObject()->objectName() != QLatin1String("root") && !(event->modifiers() & Qt::ControlModifier)) {
        event->ignore();
1145
1146
        return;
    }
1147
    if (!m_dragStart.isNull() && m_panStart.isNull() && event->button() & Qt::LeftButton && !event->isAccepted()) {
1148
1149
        emit monitorPlay();
    }
1150
1151
1152
    m_dragStart = QPoint();
    m_panStart = QPoint();
    setCursor(Qt::ArrowCursor);
1