projectclip.cpp 56.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
Copyright (C) 2012  Till Theato <root@ttill.de>
Copyright (C) 2014  Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.org.

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 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
11
by the membership of KDE e.V.), which shall act as a proxy
12
13
14
15
16
17
18
19
20
21
22
23
defined in Section 14 of version 3 of the license.

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 "projectclip.h"
24
#include "bin.h"
Nicolas Carion's avatar
Nicolas Carion committed
25
#include "core.h"
26
27
#include "doc/docundostack.hpp"
#include "doc/kdenlivedoc.h"
28
#include "doc/kthumb.h"
Nicolas Carion's avatar
Nicolas Carion committed
29
#include "effects/effectstack/model/effectstackmodel.hpp"
30
#include "jobs/audiothumbjob.hpp"
31
#include "jobs/jobmanager.h"
32
#include "jobs/loadjob.hpp"
33
#include "jobs/thumbjob.hpp"
34
#include "jobs/cachejob.hpp"
35
#include "kdenlivesettings.h"
36
#include "lib/audio/audioStreamInfo.h"
37
#include "mltcontroller/clipcontroller.h"
38
#include "mltcontroller/clippropertiescontroller.h"
39
#include "model/markerlistmodel.hpp"
40
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
41
#include "project/projectcommands.h"
42
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
43
44
45
46
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
Nicolas Carion's avatar
style    
Nicolas Carion committed
47
#include "timeline2/model/snapmodel.hpp"
48

49
#include "utils/thumbnailcache.hpp"
50
#include "xml/xml.hpp"
51
#include <QPainter>
52
#include <jobs/proxyclipjob.h>
53
#include <kimagecache.h>
54

Laurent Montel's avatar
Laurent Montel committed
55
#include "kdenlive_debug.h"
Nicolas Carion's avatar
Nicolas Carion committed
56
#include "logger.hpp"
57
#include <KLocalizedString>
58
#include <KMessageBox>
59
#include <QApplication>
Nicolas Carion's avatar
Nicolas Carion committed
60
61
62
63
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
Nicolas Carion's avatar
Nicolas Carion committed
64
#include <memory>
65

Nicolas Carion's avatar
Nicolas Carion committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <rttr/registration>
#pragma GCC diagnostic pop
RTTR_REGISTRATION
{
    using namespace rttr;
    registration::class_<ProjectClip>("ProjectClip");
}

Nicolas Carion's avatar
Nicolas Carion committed
80
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, const std::shared_ptr<ProjectItemModel> &model, std::shared_ptr<Mlt::Producer> producer)
81
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
Nicolas Carion's avatar
Nicolas Carion committed
82
    , ClipController(id, std::move(producer))
83
    , m_thumbsProducer(nullptr)
84
{
85
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
86
    m_clipStatus = StatusReady;
87
88
    m_name = clipName();
    m_duration = getStringDuration();
89
    m_inPoint = 0;
90
    m_outPoint = 0;
91
92
    m_date = date;
    m_description = ClipController::description();
93
    if (m_clipType == ClipType::Audio) {
94
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
95
96
97
    } else {
        m_thumbnail = thumb;
    }
98
99
    // Make sure we have a hash for this clip
    hash();
100
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
101
102
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
103
104
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(const QString &, markers), Q_ARG(bool, true),
                                  Q_ARG(bool, false));
105
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
106
    connectEffectStack();
107
108
}

109
// static
Nicolas Carion's avatar
Nicolas Carion committed
110
111
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, const std::shared_ptr<ProjectItemModel> &model,
                                                    const std::shared_ptr<Mlt::Producer> &producer)
112
{
113
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
114
    baseFinishConstruct(self);
115
    QMetaObject::invokeMethod(model.get(), "loadSubClips", Qt::QueuedConnection, Q_ARG(const QString&, id), Q_ARG(const QString&, self->getProducerProperty(QStringLiteral("kdenlive:clipzones"))));
116
117
118
    return self;
}

119
120
121
122
123
void ProjectClip::importEffects(const std::shared_ptr<Mlt::Producer> &producer)
{
    m_effectStack->importEffects(producer, PlaylistState::Disabled, true);
}

Nicolas Carion's avatar
Nicolas Carion committed
124
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, const std::shared_ptr<ProjectItemModel> &model)
125
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
126
    , ClipController(id)
127
    , m_thumbsProducer(nullptr)
128
{
129
    m_clipStatus = StatusWaiting;
130
    m_thumbnail = thumb;
131
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
132
    if (description.hasAttribute(QStringLiteral("type"))) {
133
        m_clipType = (ClipType::ProducerType)description.attribute(QStringLiteral("type")).toInt();
134
        if (m_clipType == ClipType::Audio) {
135
            m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
136
        }
137
    }
138
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
139
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
140
141
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
142
    } else if (!m_temporaryUrl.isEmpty()) {
143
        m_name = QFileInfo(m_temporaryUrl).fileName();
144
145
    } else {
        m_name = i18n("Untitled");
146
    }
147
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
148
149
}

150
151
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
152
{
Nicolas Carion's avatar
Nicolas Carion committed
153
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, std::move(model)));
154
155
156
157
    baseFinishConstruct(self);
    return self;
}

158
159
ProjectClip::~ProjectClip()
{
160
    // controller is deleted in bincontroller
161
    m_thumbMutex.lock();
162
    m_requestedThumbs.clear();
163
    m_thumbMutex.unlock();
164
    m_thumbThread.waitForFinished();
165
    audioFrameCache.clear();
166
167
}

168
169
void ProjectClip::connectEffectStack()
{
170
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, [&]() {
171
172
173
174
175
        if (auto ptr = m_model.lock()) {
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                           AbstractProjectItem::IconOverlay);
        }
    });
176
177
}

178
179
QString ProjectClip::getToolTip() const
{
180
    return url();
181
182
}

183
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
184
{
185
    QString value = defaultValue;
186
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
187
    for (int i = 0; i < props.count(); ++i) {
188
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
189
190
191
192
193
194
195
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

196
void ProjectClip::updateAudioThumbnail(const QVector<double> audioLevels)
197
{
198
    audioFrameCache = audioLevels;
199
    m_audioThumbCreated = true;
200
201
202
203
}

bool ProjectClip::audioThumbCreated() const
{
204
    return (m_audioThumbCreated);
205
206
}

207
ClipType::ProducerType ProjectClip::clipType() const
208
{
209
    return m_clipType;
210
211
}

212
213
bool ProjectClip::hasParent(const QString &id) const
{
214
215
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
216
217
218
219
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
220
221
222
223
    }
    return false;
}

224
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
225
{
Nicolas Carion's avatar
Nicolas Carion committed
226
    if (id == m_binId) {
227
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
228
    }
229
    return std::shared_ptr<ProjectClip>();
230
231
}

232
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
233
{
234
    Q_UNUSED(id)
235
    return std::shared_ptr<ProjectFolder>();
236
237
}

238
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
239
{
240
    for (int i = 0; i < childCount(); ++i) {
241
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
242
243
244
245
        if (clip) {
            return clip;
        }
    }
246
    return std::shared_ptr<ProjectSubClip>();
247
248
}

249
250
251
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
252
    for (int i = 0; i < childCount(); ++i) {
253
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
254
255
256
257
258
259
260
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

261
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
262
{
Nicolas Carion's avatar
Nicolas Carion committed
263
    if (ix == row()) {
264
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
265
    }
266
    return std::shared_ptr<ProjectClip>();
267
268
}

269
/*bool ProjectClip::isValid() const
270
{
271
272
    return m_controller->isValid();
}*/
273

274
275
bool ProjectClip::hasUrl() const
{
276
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
277
        return (!clipUrl().isEmpty());
278
    }
279
280
281
    return false;
}

282
const QString ProjectClip::url() const
283
{
284
    return clipUrl();
285
286
}

287
GenTime ProjectClip::duration() const
288
{
289
    return getPlaytime();
290
291
}

292
size_t ProjectClip::frameDuration() const
293
294
{
    GenTime d = duration();
295
    return (size_t)d.frames(pCore->getCurrentFps());
296
297
}

298
void ProjectClip::reloadProducer(bool refreshOnly, bool audioStreamChanged, bool reloadAudio)
299
{
300
301
302
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
303
    QMutexLocker lock(&m_thumbMutex);
304
    if (refreshOnly) {
305
306
        // In that case, we only want a new thumbnail.
        // We thus set up a thumb job. We must make sure that there is no pending LOADJOB
307
        // Clear cache first
308
        ThumbnailCache::get()->invalidateThumbsForClip(clipId(), false);
309
310
        pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
        m_thumbsProducer.reset();
311
        pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), 150, -1, true, true);
312
    } else {
313
314
315
316
        // If another load job is running?
        if (loadjobId > -1) {
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::LOADJOB);
        }
317
318
319
        if (QFile::exists(m_path) && !hasProxy()) {
            clearBackupProperties();
        }
320
321
322
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
323
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
324
            m_thumbsProducer.reset();
325
            ClipType::ProducerType type = clipType();
326
            if (type != ClipType::Color && type != ClipType::Image && type != ClipType::SlideShow) {
327
328
                xml.removeAttribute("out");
            }
329
            ThumbnailCache::get()->invalidateThumbsForClip(clipId(), reloadAudio);
330
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, loadjobId, QString(), xml);
331
            pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), 150, -1, true, true);
332
333
334
335
            if (audioStreamChanged) {
                discardAudioThumb();
                pCore->jobManager()->startJob<AudioThumbJob>({clipId()}, loadjobId, QString());
            }
336
        }
337
    }
338
339
}

340
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta, bool includeProfile)
341
{
342
    getProducerXML(document, includeMeta, includeProfile);
343
344
345
346
347
348
    QDomElement prod;
    if (document.documentElement().tagName() == QLatin1String("producer")) {
        prod = document.documentElement();
    } else {
        prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
    }
349
    if (m_clipType != ClipType::Unknown) {
350
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
351
    }
352
    return prod;
353
354
}

Laurent Montel's avatar
Laurent Montel committed
355
void ProjectClip::setThumbnail(const QImage &img)
356
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
357
358
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
359
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
360
        QPainter p(&thumb);
361
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
362
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
363
364
365
366
367
368
369
370
        p.fillRect(r, c);
        QFont font = p.font();
        font.setPixelSize(r.height());
        font.setBold(true);
        p.setFont(font);
        p.setPen(Qt::black);
        p.drawText(r, Qt::AlignCenter, i18nc("The first letter of Proxy, used as abbreviation", "P"));
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
371
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
372
    if (auto ptr = m_model.lock()) {
373
374
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                       AbstractProjectItem::DataThumbnail);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
375
    }
376
377
}

378
379
bool ProjectClip::hasAudioAndVideo() const
{
380
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0 && m_masterProducer->get_int("set.test_audio") == 0;
381
382
383
384
385
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
386
387
388
389
390
391
    case PlaylistState::AudioOnly:
        return hasAudio() && (m_masterProducer->get_int("set.test_audio") == 0);
    case PlaylistState::VideoOnly:
        return hasVideo() && (m_masterProducer->get_int("set.test_image") == 0);
    default:
        return true;
392
393
394
    }
}

395
396
397
398
399
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

400
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
401
{
Vincent Pinon's avatar
Vincent Pinon committed
402
    Q_UNUSED(replaceProducer)
403
    qDebug() << "################### ProjectClip::setproducer";
404
    QMutexLocker locker(&m_producerMutex);
Nicolas Carion's avatar
Nicolas Carion committed
405
    updateProducer(producer);
406
    m_thumbsProducer.reset();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
407
    connectEffectStack();
408

409
410
411
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
412
    }
413
414
415
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
416
    if (m_clipType == ClipType::Audio) {
417
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
418
    } else if (m_clipType == ClipType::Image) {
419
        if (producer->get_int("meta.media.width") < 8 || producer->get_int("meta.media.height") < 8) {
420
421
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
422
        }
423
    }
424
    m_duration = getStringDuration();
425
    m_clipStatus = StatusReady;
426
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
427
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
428
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
429
    if (auto ptr = m_model.lock()) {
430
431
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                       AbstractProjectItem::DataDuration);
432
        std::static_pointer_cast<ProjectItemModel>(ptr)->updateWatcher(std::static_pointer_cast<ProjectClip>(shared_from_this()));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
433
    }
434
    // Make sure we have a hash for this clip
435
    getFileHash();
436
437
    // set parent again (some info need to be stored in producer)
    updateParent(parentItem().lock());
438

439
    if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableproxy")).toInt() == 1) {
440
441
        QList<std::shared_ptr<ProjectClip>> clipList;
        // automatic proxy generation enabled
442
        if (m_clipType == ClipType::Image && pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateimageproxy")).toInt() == 1) {
443
444
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyimageminsize() &&
                getProducerProperty(QStringLiteral("kdenlive:proxy")) == QStringLiteral()) {
445
446
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
447
        } else if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateproxy")).toInt() == 1 &&
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
                   (m_clipType == ClipType::AV || m_clipType == ClipType::Video) && getProducerProperty(QStringLiteral("kdenlive:proxy")) == QStringLiteral()) {
            bool skipProducer = false;
            if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableexternalproxy")).toInt() == 1) {
                QStringList externalParams = pCore->currentDoc()->getDocumentProperty(QStringLiteral("externalproxyparams")).split(QLatin1Char(';'));
                // We have a camcorder profile, check if we have opened a proxy clip
                if (externalParams.count() >= 6) {
                    QFileInfo info(m_path);
                    QDir dir = info.absoluteDir();
                    dir.cd(externalParams.at(3));
                    QString fileName = info.fileName();
                    if (!externalParams.at(2).isEmpty()) {
                        fileName.chop(externalParams.at(2).size());
                    }
                    fileName.append(externalParams.at(5));
                    if (dir.exists(fileName)) {
                        setProducerProperty(QStringLiteral("kdenlive:proxy"), m_path);
                        m_path = dir.absoluteFilePath(fileName);
                        setProducerProperty(QStringLiteral("kdenlive:originalurl"), m_path);
                        getFileHash();
                        skipProducer = true;
                    }
                }
            }
            if (!skipProducer && getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyminsize()) {
472
473
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
474
        }
475
476
        if (!clipList.isEmpty()) {
            pCore->currentDoc()->slotProxyCurrentItem(true, clipList, false);
477
478
479
        }
    }
    pCore->bin()->reloadMonitorIfActive(clipId());
480
481
482
    for (auto &p : m_audioProducers) {
        m_effectStack->removeService(p.second);
    }
483
484
485
    for (auto &p : m_videoProducers) {
        m_effectStack->removeService(p.second);
    }
486
487
488
489
490
    for (auto &p : m_timewarpProducers) {
        m_effectStack->removeService(p.second);
    }
    // Release audio producers
    m_audioProducers.clear();
491
    m_videoProducers.clear();
492
    m_timewarpProducers.clear();
493
    emit refreshPropertiesPanel();
494
    replaceInTimeline();
495
    return true;
496
497
}

498
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
499
500
501
502
{
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
503
    if (clipType() == ClipType::Unknown) {
Laurent Montel's avatar
Laurent Montel committed
504
        return nullptr;
505
    }
506
    QMutexLocker lock(&m_thumbMutex);
507
508
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
509
        return nullptr;
510
    }
511
    if (KdenliveSettings::gpu_accel()) {
512
        // TODO: when the original producer changes, we must reload this thumb producer
513
        m_thumbsProducer = softClone(ClipController::getPassPropertiesList());
514
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
515
        m_thumbsProducer->attach(converter);
516
    } else {
517
518
519
520
521
522
523
524
525
526
527
528
529
530
        QString mltService = m_masterProducer->get("mlt_service");
        const QString mltResource = m_masterProducer->get("resource");
        if (mltService == QLatin1String("avformat")) {
            mltService = QStringLiteral("avformat-novalidate");
        }
        m_thumbsProducer.reset(new Mlt::Producer(*pCore->thumbProfile(), mltService.toUtf8().constData(), mltResource.toUtf8().constData()));
        if (m_thumbsProducer->is_valid()) {
            Mlt::Properties original(m_masterProducer->get_properties());
            Mlt::Properties cloneProps(m_thumbsProducer->get_properties());
            cloneProps.pass_list(original, ClipController::getPassPropertiesList());
            Mlt::Filter scaler(*pCore->thumbProfile(), "swscale");
            Mlt::Filter padder(*pCore->thumbProfile(), "resize");
            Mlt::Filter converter(*pCore->thumbProfile(), "avcolor_space");
            m_thumbsProducer->set("audio_index", -1);
531
532
            // Required to make get_playtime() return > 1
            m_thumbsProducer->set("out", m_thumbsProducer->get_length() -1);
533
534
535
536
            m_thumbsProducer->attach(scaler);
            m_thumbsProducer->attach(padder);
            m_thumbsProducer->attach(converter);
        }
537
538
539
540
    }
    return m_thumbsProducer;
}

Nicolas Carion's avatar
Nicolas Carion committed
541
542
543
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
544
        m_disabledProducer = cloneProducer();
Nicolas Carion's avatar
Nicolas Carion committed
545
546
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
547
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
548
549
    }
}
550
551

std::shared_ptr<Mlt::Producer> ProjectClip::getTimelineProducer(int trackId, int clipId, PlaylistState::ClipState state, double speed)
552
{
553
554
555
    if (!m_masterProducer) {
        return nullptr;
    }
556
557
    if (qFuzzyCompare(speed, 1.0)) {
        // we are requesting a normal speed producer
558
559
        if (trackId == -1 ||
            (state == PlaylistState::VideoOnly && (m_clipType == ClipType::Color || m_clipType == ClipType::Image || m_clipType == ClipType::Text))) {
560
            // Temporary copy, return clone of master
561
562
            int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
            return std::shared_ptr<Mlt::Producer>(m_masterProducer->cut(-1, duration > 0 ? duration : -1));
563
        }
564
565
566
567
        if (m_timewarpProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_timewarpProducers[clipId]);
            m_timewarpProducers.erase(clipId);
        }
568
569
        if (state == PlaylistState::AudioOnly) {
            // We need to get an audio producer, if none exists
570
571
572
573
574
            if (m_audioProducers.count(trackId) == 0) {
                m_audioProducers[trackId] = cloneProducer(true);
                m_audioProducers[trackId]->set("set.test_audio", 0);
                m_audioProducers[trackId]->set("set.test_image", 1);
                m_effectStack->addService(m_audioProducers[trackId]);
575
            }
576
            return std::shared_ptr<Mlt::Producer>(m_audioProducers[trackId]->cut());
577
        }
578
579
580
        if (m_audioProducers.count(trackId) > 0) {
            m_effectStack->removeService(m_audioProducers[trackId]);
            m_audioProducers.erase(trackId);
581
        }
Nicolas Carion's avatar
Nicolas Carion committed
582
583
        if (state == PlaylistState::VideoOnly) {
            // we return the video producer
584
            // We need to get an audio producer, if none exists
585
586
587
588
589
            if (m_videoProducers.count(trackId) == 0) {
                m_videoProducers[trackId] = cloneProducer(true);
                m_videoProducers[trackId]->set("set.test_audio", 1);
                m_videoProducers[trackId]->set("set.test_image", 0);
                m_effectStack->addService(m_videoProducers[trackId]);
590
            }
591
            int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
592
            return std::shared_ptr<Mlt::Producer>(m_videoProducers[trackId]->cut(-1, duration > 0 ? duration : -1));
593
        }
594
595
596
        if (m_videoProducers.count(trackId) > 0) {
            m_effectStack->removeService(m_videoProducers[trackId]);
            m_videoProducers.erase(trackId);
Nicolas Carion's avatar
Nicolas Carion committed
597
598
599
        }
        Q_ASSERT(state == PlaylistState::Disabled);
        createDisabledMasterProducer();
600
        int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
601
        return std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(-1, duration > 0 ? duration : -1));
602
603
    }

604
    // For timewarp clips, we keep one separate producer for each clip.
605
606
    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
607
608
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
609
610
611
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
612
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
613
614
        } else {
            m_timewarpProducers.erase(clipId);
615
616
617
        }
    }
    if (!warpProducer) {
618
619
620
621
        QString resource(originalProducer()->get("resource"));
        if (resource.isEmpty() || resource == QLatin1String("<producer>")) {
            resource = m_service;
        }
622
        QString url = QString("timewarp:%1:%2").arg(QString::fromStdString(std::to_string(speed))).arg(resource);
Nicolas Carion's avatar
Nicolas Carion committed
623
        warpProducer.reset(new Mlt::Producer(*originalProducer()->profile(), url.toUtf8().constData()));
624
        qDebug() << "new producer: " << url;
625
626
627
        qDebug() << "warp LENGTH before" << warpProducer->get_length();
        int original_length = originalProducer()->get_length();
        // this is a workaround to cope with Mlt erroneous rounding
628
629
630
        Mlt::Properties original(m_masterProducer->get_properties());
        Mlt::Properties cloneProps(warpProducer->get_properties());
        cloneProps.pass_list(original, ClipController::getPassPropertiesList(false));
631
        warpProducer->set("length", (int) (original_length / std::abs(speed) + 0.5));
632
633
    }

634
    qDebug() << "warp LENGTH" << warpProducer->get_length();
635
636
637
638
639
640
641
642
643
    warpProducer->set("set.test_audio", 1);
    warpProducer->set("set.test_image", 1);
    if (state == PlaylistState::AudioOnly) {
        warpProducer->set("set.test_audio", 0);
    }
    if (state == PlaylistState::VideoOnly) {
        warpProducer->set("set.test_image", 0);
    }
    m_timewarpProducers[clipId] = warpProducer;
644
    m_effectStack->addService(m_timewarpProducers[clipId]);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
645
    return std::shared_ptr<Mlt::Producer>(warpProducer->cut());
646
647
648
}

std::pair<std::shared_ptr<Mlt::Producer>, bool> ProjectClip::giveMasterAndGetTimelineProducer(int clipId, std::shared_ptr<Mlt::Producer> master,
649
                                                                                              PlaylistState::ClipState state)
650
651
652
653
654
655
656
{
    int in = master->get_in();
    int out = master->get_out();
    if (master->parent().is_valid()) {
        // in that case, we have a cut
        // check whether it's a timewarp
        double speed = 1.0;
657
        bool timeWarp = false;
658
659
660
661
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
            speed = master->parent().get_double("warp_speed");
            timeWarp = true;
        }
662
        if (master->parent().get_int("_loaded") == 1) {
663
664
            // we already have a clip that shares the same master

665
            if (state != PlaylistState::Disabled || timeWarp) {
666
                // In that case, we must create copies
667
                std::shared_ptr<Mlt::Producer> prod(getTimelineProducer(-1, clipId, state, speed)->cut(in, out));
668
                return {prod, false};
669
            }
670
671
672
673
674
            if (state == PlaylistState::Disabled) {
                if (!m_disabledProducer) {
                    qDebug() << "Warning: weird, we found a disabled clip whose master is already loaded but we don't have any yet";
                    createDisabledMasterProducer();
                }
Nicolas Carion's avatar
Nicolas Carion committed
675
676
                return {std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(in, out)), false};
            }
677
678
679
            // We have a good id, this clip can be used
            return {master, true};
        } else {
680
            master->parent().set("_loaded", 1);
681
            if (timeWarp) {
Nicolas Carion's avatar
Nicolas Carion committed
682
                m_timewarpProducers[clipId] = std::make_shared<Mlt::Producer>(&master->parent());
683
                m_effectStack->loadService(m_timewarpProducers[clipId]);
684
685
                return {master, true};
            }
686
            if (state == PlaylistState::AudioOnly) {
Nicolas Carion's avatar
Nicolas Carion committed
687
                m_audioProducers[clipId] = std::make_shared<Mlt::Producer>(&master->parent());
688
689
690
                m_effectStack->loadService(m_audioProducers[clipId]);
                return {master, true};
            }
691
            if (state == PlaylistState::VideoOnly) {
692
                // good, we found a master video producer, and we didn't have any
693
694
695
696
697
                if (m_clipType != ClipType::Color && m_clipType != ClipType::Image && m_clipType != ClipType::Text) {
                    // Color, image and text clips always use master producer in timeline
                    m_videoProducers[clipId] = std::make_shared<Mlt::Producer>(&master->parent());
                    m_effectStack->loadService(m_videoProducers[clipId]);
                }
698
699
                return {master, true};
            }
700
701
702
703
704
            if (state == PlaylistState::Disabled) {
                if (!m_disabledProducer) {
                    createDisabledMasterProducer();
                }
                return {std::make_shared<Mlt::Producer>(m_disabledProducer->cut(master->get_in(), master->get_out())), true};
Nicolas Carion's avatar
Nicolas Carion committed
705
706
            }
            qDebug() << "Warning: weird, we found a clip whose master is not loaded but we already have a master";
707
            Q_ASSERT(false);
708
709
710
711
712
        }
    } else if (master->is_valid()) {
        // in that case, we have a master
        qDebug() << "Warning: weird, we received a master clip in lieue of a cut";
        double speed = 1.0;
713
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
714
715
            speed = master->get_double("warp_speed");
        }
716
        return {getTimelineProducer(-1, clipId, state, speed), false};
717
718
719
720
721
    }
    // we have a problem
    return {std::shared_ptr<Mlt::Producer>(ClipController::mediaUnavailable->cut()), false};
}
/*
722
723
724
std::shared_ptr<Mlt::Producer> ProjectClip::timelineProducer(PlaylistState::ClipState state, int track)
{
    if (!m_service.startsWith(QLatin1String("avformat"))) {
725
        std::shared_ptr<Mlt::Producer> prod(originalProducer()->cut());
726
727
728
729
730
        int length = getProducerIntProperty(QStringLiteral("kdenlive:duration"));
        if (length > 0) {
            prod->set_in_and_out(0, length);
        }
        return prod;
731
732
733
    }
    if (state == PlaylistState::VideoOnly) {
        if (m_timelineProducers.count(0) > 0) {
734
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(0)->second->cut());
735
736
        }
        std::shared_ptr<Mlt::Producer> videoProd = cloneProducer();
737
        videoProd->set("audio_index", -1);
738
        m_timelineProducers[0] = videoProd;
739
        return std::shared_ptr<Mlt::Producer>(videoProd->cut());
740
741
742
    }
    if (state == PlaylistState::AudioOnly) {
        if (m_timelineProducers.count(-track) > 0) {
743
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(-track)->second->cut());
744
745
        }
        std::shared_ptr<Mlt::Producer> audioProd = cloneProducer();
746
        audioProd->set("video_index", -1);
747
        m_timelineProducers[-track] = audioProd;
748
        return std::shared_ptr<Mlt::Producer>(audioProd->cut());
749
750
    }
    if (m_timelineProducers.count(track) > 0) {
751
        return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(track)->second->cut());
752
753
754
    }
    std::shared_ptr<Mlt::Producer> normalProd = cloneProducer();
    m_timelineProducers[track] = normalProd;
755
    return std::shared_ptr<Mlt::Producer>(normalProd->cut());
756
}*/
757

758
std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer(bool removeEffects)
759
{
760
    Mlt::Consumer c(pCore->getCurrentProfile()->profile(), "xml", "string");
761
762
763
764
765
766
767
768
769
770
771
772
    Mlt::Service s(m_masterProducer->get_service());
    int ignore = s.get_int("ignore_points");
    if (ignore) {
        s.set("ignore_points", 0);
    }
    c.connect(s);
    c.set("time_format", "frames");
    c.set("no_meta", 1);
    c.set("no_root", 1);
    c.set("no_profile", 1);
    c.set("root", "/");
    c.set("store", "kdenlive");
Nicolas Carion's avatar
Nicolas Carion committed
773
    c.run();
774
775
776
777
    if (ignore) {
        s.set("ignore_points", ignore);
    }
    const QByteArray clipXml = c.get("string");
778
    std::shared_ptr<Mlt::Producer> prod;
779
    prod.reset(new Mlt::Producer(pCore->getCurrentProfile()->profile(), "xml-string", clipXml.constData()));
780

781
782
    if (strcmp(prod->get("mlt_service"), "avformat") == 0) {
        prod->set("mlt_service", "avformat-novalidate");
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
783
        prod->set("mute_on_pause", 0);
784
    }
785
786
787
788
789
790
791
792
793
794
795

    // we pass some properties that wouldn't be passed because of the novalidate
    const char *prefix = "meta.";
    const size_t prefix_len = strlen(prefix);
    for (int i = 0; i < m_masterProducer->count(); ++i) {
        char *current = m_masterProducer->get_name(i);
        if (strlen(current) >= prefix_len && strncmp(current, prefix, prefix_len) == 0) {
            prod->set(current, m_masterProducer->get(i));
        }
    }

796
797
798
799
    if (removeEffects) {
        int ct = 0;
        Mlt::Filter *filter = prod->filter(ct);
        while (filter) {
800
            qDebug() << "// EFFECT " << ct << " : " << filter->get("mlt_service");
801
802
            QString ix = QString::fromLatin1(filter->get("kdenlive_id"));
            if (!ix.isEmpty()) {
803
                qDebug() << "/ + + DELTING";
804
805
806
807
808
809
810
811
812
813
814
                if (prod->detach(*filter) == 0) {
                } else {
                    ct++;
                }
            } else {
                ct++;
            }
            delete filter;
            filter = prod->filter(ct);
        }
    }
815
    prod->set("id", (char *)nullptr);
816
    return prod;
817
818
}

Nicolas Carion's avatar
Nicolas Carion committed
819
std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer(const std::shared_ptr<Mlt::Producer> &producer)