projectclip.cpp 56.4 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/jobmanager.h"
31
#include "jobs/loadjob.hpp"
32
#include "jobs/thumbjob.hpp"
33
#include "kdenlivesettings.h"
34
#include "lib/audio/audioStreamInfo.h"
35
#include "mltcontroller/clipcontroller.h"
36
#include "mltcontroller/clippropertiescontroller.h"
37
#include "model/markerlistmodel.hpp"
38
#include "profiles/profilemodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
39
#include "project/projectcommands.h"
40
#include "project/projectmanager.h"
Nicolas Carion's avatar
Nicolas Carion committed
41 42 43 44
#include "projectfolder.h"
#include "projectitemmodel.h"
#include "projectsubclip.h"
#include "timecode.h"
Nicolas Carion's avatar
style  
Nicolas Carion committed
45
#include "timeline2/model/snapmodel.hpp"
46

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

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

Nicolas Carion's avatar
Nicolas Carion committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
#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");
}

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

107 108
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
109
                                                    std::shared_ptr<Mlt::Producer> producer)
110
{
111
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
112
    baseFinishConstruct(self);
113
    self->m_effectStack->importEffects(producer, PlaylistState::Disabled, true);
114
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
115 116 117
    return self;
}

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

144 145
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
146
{
147
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, model));
148 149 150 151
    baseFinishConstruct(self);
    return self;
}

152 153
ProjectClip::~ProjectClip()
{
154
    // controller is deleted in bincontroller
155
    m_thumbMutex.lock();
156
    m_requestedThumbs.clear();
157
    m_thumbMutex.unlock();
158
    m_thumbThread.waitForFinished();
159
    audioFrameCache.clear();
160 161
}

162 163
void ProjectClip::connectEffectStack()
{
164 165
    connect(m_effectStack.get(), &EffectStackModel::modelChanged, this, &ProjectClip::updateChildProducers);
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, &ProjectClip::updateChildProducers);
166
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, [&]() {
167 168 169 170 171
        if (auto ptr = m_model.lock()) {
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                           AbstractProjectItem::IconOverlay);
        }
    });
172 173
    /*connect(m_effectStack.get(), &EffectStackModel::modelChanged, [&](){
        qDebug()<<"/ / / STACK CHANGED";
174
        updateChildProducers();
175
    });*/
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(QVariantList audioLevels)
197
{
198
    std::swap(audioFrameCache, audioLevels); // avoid second copy
199
    m_audioThumbCreated = true;
200 201 202 203
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
    }
    updateTimelineClips({TimelineModel::AudioLevelsRole});
204 205 206 207
}

bool ProjectClip::audioThumbCreated() const
{
208
    return (m_audioThumbCreated);
209 210
}

211
ClipType::ProducerType ProjectClip::clipType() const
212
{
213
    return m_clipType;
214 215
}

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

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

236
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
237
{
238
    Q_UNUSED(id)
239
    return std::shared_ptr<ProjectFolder>();
240 241
}

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

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

265
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
266
{
Nicolas Carion's avatar
Nicolas Carion committed
267
    if (ix == row()) {
268
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
269
    }
270
    return std::shared_ptr<ProjectClip>();
271 272
}

273
/*bool ProjectClip::isValid() const
274
{
275 276
    return m_controller->isValid();
}*/
277

278 279
bool ProjectClip::hasUrl() const
{
280
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
281
        return (!clipUrl().isEmpty());
282
    }
283 284 285
    return false;
}

286
const QString ProjectClip::url() const
287
{
288
    return clipUrl();
289 290
}

291
GenTime ProjectClip::duration() const
292
{
293
    return getPlaytime();
294 295
}

296
size_t ProjectClip::frameDuration() const
297 298
{
    GenTime d = duration();
299
    return (size_t)d.frames(pCore->getCurrentFps());
300 301
}

302
void ProjectClip::reloadProducer(bool refreshOnly)
303
{
304 305 306
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
307
    QMutexLocker lock(&m_thumbMutex);
308
    if (refreshOnly) {
309 310
        // 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
311 312
        // Clear cache first
        ThumbnailCache::get()->invalidateThumbsForClip(clipId());
313 314
        pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
        m_thumbsProducer.reset();
315
        pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), 150, -1, true, true);
316 317

    } else {
318 319 320 321
        // If another load job is running?
        if (loadjobId > -1) {
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::LOADJOB);
        }
322 323 324
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
325
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
326
            m_thumbsProducer.reset();
327
            ThumbnailCache::get()->invalidateThumbsForClip(clipId());
328
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, loadjobId, QString(), xml);
329
            pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), 150, -1, true, true);
330
        }
331
    }
332 333
}

334
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
335
{
336 337
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
338
    if (m_clipType != ClipType::Unknown) {
339
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
340
    }
341
    return prod;
342 343
}

Laurent Montel's avatar
Laurent Montel committed
344
void ProjectClip::setThumbnail(const QImage &img)
345
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
346 347
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
348
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
349
        QPainter p(&thumb);
350
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
351
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
352 353 354 355 356 357 358 359
        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
360
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
361
    if (auto ptr = m_model.lock()) {
362 363
        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
364
    }
365 366
}

367 368
bool ProjectClip::hasAudioAndVideo() const
{
369
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0 && m_masterProducer->get_int("set.test_audio") == 0;
370 371 372 373 374
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
375 376 377 378 379 380
    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;
381 382 383
    }
}

384 385 386 387 388
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

389
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
390
{
Vincent Pinon's avatar
Vincent Pinon committed
391
    Q_UNUSED(replaceProducer)
392
    qDebug() << "################### ProjectClip::setproducer";
393
    QMutexLocker locker(&m_producerMutex);
Nicolas Carion's avatar
Nicolas Carion committed
394
    updateProducer(std::move(producer));
395
    m_thumbsProducer.reset();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
396
    connectEffectStack();
397

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

428
    if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableproxy")).toInt() == 1) {
429 430
        QList<std::shared_ptr<ProjectClip>> clipList;
        // automatic proxy generation enabled
431
        if (m_clipType == ClipType::Image && pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateimageproxy")).toInt() == 1) {
432 433
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyimageminsize() &&
                getProducerProperty(QStringLiteral("kdenlive:proxy")) == QStringLiteral()) {
434 435
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
436
        } else if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateproxy")).toInt() == 1 &&
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
                   (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()) {
461 462
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
463
        }
464 465
        if (!clipList.isEmpty()) {
            pCore->currentDoc()->slotProxyCurrentItem(true, clipList, false);
466 467 468
        }
    }
    pCore->bin()->reloadMonitorIfActive(clipId());
469 470 471
    for (auto &p : m_audioProducers) {
        m_effectStack->removeService(p.second);
    }
472 473 474
    for (auto &p : m_videoProducers) {
        m_effectStack->removeService(p.second);
    }
475 476 477 478 479
    for (auto &p : m_timewarpProducers) {
        m_effectStack->removeService(p.second);
    }
    // Release audio producers
    m_audioProducers.clear();
480
    m_videoProducers.clear();
481
    m_timewarpProducers.clear();
482
    emit refreshPropertiesPanel();
483
    replaceInTimeline();
484
    return true;
485 486
}

487
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
488 489 490 491
{
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
492
    if (clipType() == ClipType::Unknown) {
Laurent Montel's avatar
Laurent Montel committed
493
        return nullptr;
494
    }
495
    QMutexLocker lock(&m_thumbMutex);
496 497
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
498
        return nullptr;
499
    }
500
    if (KdenliveSettings::gpu_accel()) {
501
        // TODO: when the original producer changes, we must reload this thumb producer
502
        m_thumbsProducer = softClone(ClipController::getPassPropertiesList());
503
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
504
        m_thumbsProducer->attach(converter);
505
    } else {
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        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);
            m_thumbsProducer->attach(scaler);
            m_thumbsProducer->attach(padder);
            m_thumbsProducer->attach(converter);
        }
524 525 526 527
    }
    return m_thumbsProducer;
}

Nicolas Carion's avatar
Nicolas Carion committed
528 529 530 531 532 533
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
        m_disabledProducer = cloneProducer(&pCore->getCurrentProfile()->profile());
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
534
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
535 536
    }
}
537
std::shared_ptr<Mlt::Producer> ProjectClip::getTimelineProducer(int clipId, PlaylistState::ClipState state, double speed)
538
{
539 540 541
    if (!m_masterProducer) {
        return nullptr;
    }
542 543 544
    if (qFuzzyCompare(speed, 1.0)) {
        // we are requesting a normal speed producer
        // We can first cleen the speed producers we have for the current id
545 546 547 548
        if (m_timewarpProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_timewarpProducers[clipId]);
            m_timewarpProducers.erase(clipId);
        }
549 550 551
        if (state == PlaylistState::AudioOnly) {
            // We need to get an audio producer, if none exists
            if (m_audioProducers.count(clipId) == 0) {
552
                m_audioProducers[clipId] = cloneProducer(&pCore->getCurrentProfile()->profile(), true);
553 554
                m_audioProducers[clipId]->set("set.test_audio", 0);
                m_audioProducers[clipId]->set("set.test_image", 1);
555
                m_effectStack->addService(m_audioProducers[clipId]);
556 557 558
            }
            return std::shared_ptr<Mlt::Producer>(m_audioProducers[clipId]->cut());
        }
559 560 561 562
        if (m_audioProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_audioProducers[clipId]);
            m_audioProducers.erase(clipId);
        }
Nicolas Carion's avatar
Nicolas Carion committed
563 564
        if (state == PlaylistState::VideoOnly) {
            // we return the video producer
565
            // We need to get an audio producer, if none exists
566
            if (m_clipType == ClipType::Color || m_clipType == ClipType::Image || m_clipType == ClipType::Text) {
567
                int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
Nicolas Carion's avatar
Nicolas Carion committed
568
                return std::shared_ptr<Mlt::Producer>(m_masterProducer->cut(-1, duration > 0 ? duration : -1));
569
            }
570
            if (m_videoProducers.count(clipId) == 0) {
571
                m_videoProducers[clipId] = cloneProducer(&pCore->getCurrentProfile()->profile(), true);
572 573 574 575
                m_videoProducers[clipId]->set("set.test_audio", 1);
                m_videoProducers[clipId]->set("set.test_image", 0);
                m_effectStack->addService(m_videoProducers[clipId]);
            }
576
            int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
577
            return std::shared_ptr<Mlt::Producer>(m_videoProducers[clipId]->cut(-1, duration > 0 ? duration : -1));
578 579 580 581
        }
        if (m_videoProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_videoProducers[clipId]);
            m_videoProducers.erase(clipId);
Nicolas Carion's avatar
Nicolas Carion committed
582 583 584
        }
        Q_ASSERT(state == PlaylistState::Disabled);
        createDisabledMasterProducer();
585
        int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
586
        return std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(-1, duration > 0 ? duration : -1));
587 588 589
    }

    // in that case, we need to create a warp producer, if we don't have one
590 591 592 593
    if (m_audioProducers.count(clipId) > 0) {
        m_effectStack->removeService(m_audioProducers[clipId]);
        m_audioProducers.erase(clipId);
    }
594 595 596 597
    if (m_videoProducers.count(clipId) > 0) {
        m_effectStack->removeService(m_videoProducers[clipId]);
        m_videoProducers.erase(clipId);
    }
598 599 600

    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
601 602
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
603 604 605
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
606
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
607 608
        } else {
            m_timewarpProducers.erase(clipId);
609 610 611
        }
    }
    if (!warpProducer) {
612
        QLocale locale;
613 614 615 616 617 618
        QString resource(originalProducer()->get("resource"));
        if (resource.isEmpty() || resource == QLatin1String("<producer>")) {
            resource = m_service;
        }
        QString url = QString("timewarp:%1:%2").arg(locale.toString(speed)).arg(resource);
        warpProducer.reset(new Mlt::Producer(*originalProducer()->profile(), url.toUtf8().constData()));
619
        qDebug() << "new producer: " << url;
620 621 622 623
        qDebug() << "warp LENGTH before" << warpProducer->get_length();
        int original_length = originalProducer()->get_length();
        // this is a workaround to cope with Mlt erroneous rounding
        warpProducer->set("length", double(original_length) / speed);
624 625
    }

626
    qDebug() << "warp LENGTH" << warpProducer->get_length();
627 628 629 630 631 632 633 634 635
    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;
636
    m_effectStack->addService(m_timewarpProducers[clipId]);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
637
    return std::shared_ptr<Mlt::Producer>(warpProducer->cut());
638 639 640
}

std::pair<std::shared_ptr<Mlt::Producer>, bool> ProjectClip::giveMasterAndGetTimelineProducer(int clipId, std::shared_ptr<Mlt::Producer> master,
641
                                                                                              PlaylistState::ClipState state)
642 643 644 645 646 647 648
{
    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;
649
        bool timeWarp = false;
650 651 652 653
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
            speed = master->parent().get_double("warp_speed");
            timeWarp = true;
        }
654
        if (master->parent().get_int("_loaded") == 1) {
655 656
            // we already have a clip that shares the same master

657
            if (state != PlaylistState::Disabled || timeWarp) {
658
                // In that case, we must create copies
659 660
                std::shared_ptr<Mlt::Producer> prod(getTimelineProducer(clipId, state, speed)->cut(in, out));
                return {prod, false};
661
            }
Nicolas Carion's avatar
Nicolas Carion committed
662 663 664 665 666 667 668 669 670
            if (state == PlaylistState::Disabled && !m_disabledProducer) {
                qDebug() << "Warning: weird, we found a disabled clip whose master is already loaded but we don't have any yet";
                createDisabledMasterProducer();
                return {std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(in, out)), false};
            }
            if (state == PlaylistState::Disabled && QString::fromUtf8(m_disabledProducer->get("id")) != QString::fromUtf8(master->parent().get("id"))) {
                qDebug() << "Warning: weird, we found a disabled clip whose master is already loaded but doesn't match ours";
                return {std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(in, out)), false};
            }
671 672 673
            // We have a good id, this clip can be used
            return {master, true};
        } else {
674
            master->parent().set("_loaded", 1);
675
            if (timeWarp) {
676
                m_timewarpProducers[clipId] = std::shared_ptr<Mlt::Producer>(new Mlt::Producer(&master->parent()));
677
                m_effectStack->loadService(m_timewarpProducers[clipId]);
678 679
                return {master, true};
            }
680 681 682 683 684
            if (state == PlaylistState::AudioOnly) {
                m_audioProducers[clipId] = std::shared_ptr<Mlt::Producer>(new Mlt::Producer(&master->parent()));
                m_effectStack->loadService(m_audioProducers[clipId]);
                return {master, true};
            }
685
            if (state == PlaylistState::VideoOnly) {
686
                // good, we found a master video producer, and we didn't have any
687
                m_videoProducers[clipId] = std::shared_ptr<Mlt::Producer>(new Mlt::Producer(&master->parent()));
688
                m_effectStack->loadService(m_videoProducers[clipId]);
689 690
                return {master, true};
            }
Nicolas Carion's avatar
Nicolas Carion committed
691 692
            if (state == PlaylistState::Disabled && !m_disabledProducer) {
                // good, we found a master disabled producer, and we didn't have any
693
                m_disabledProducer.reset(master->parent().cut());
694
                m_effectStack->loadService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
695 696 697
                return {master, true};
            }
            qDebug() << "Warning: weird, we found a clip whose master is not loaded but we already have a master";
698
            Q_ASSERT(false);
699 700 701 702
        }
    } 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";
703
        exit(1);
704
        double speed = 1.0;
705
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
706 707 708 709 710 711 712 713
            speed = master->get_double("warp_speed");
        }
        return {getTimelineProducer(clipId, state, speed), false};
    }
    // we have a problem
    return {std::shared_ptr<Mlt::Producer>(ClipController::mediaUnavailable->cut()), false};
}
/*
714 715 716
std::shared_ptr<Mlt::Producer> ProjectClip::timelineProducer(PlaylistState::ClipState state, int track)
{
    if (!m_service.startsWith(QLatin1String("avformat"))) {
717
        std::shared_ptr<Mlt::Producer> prod(originalProducer()->cut());
718 719 720 721 722
        int length = getProducerIntProperty(QStringLiteral("kdenlive:duration"));
        if (length > 0) {
            prod->set_in_and_out(0, length);
        }
        return prod;
723 724 725
    }
    if (state == PlaylistState::VideoOnly) {
        if (m_timelineProducers.count(0) > 0) {
726
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(0)->second->cut());
727 728
        }
        std::shared_ptr<Mlt::Producer> videoProd = cloneProducer();
729
        videoProd->set("audio_index", -1);
730
        m_timelineProducers[0] = videoProd;
731
        return std::shared_ptr<Mlt::Producer>(videoProd->cut());
732 733 734
    }
    if (state == PlaylistState::AudioOnly) {
        if (m_timelineProducers.count(-track) > 0) {
735
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(-track)->second->cut());
736 737
        }
        std::shared_ptr<Mlt::Producer> audioProd = cloneProducer();
738
        audioProd->set("video_index", -1);