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

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

Laurent Montel's avatar
Laurent Montel committed
54
#include "kdenlive_debug.h"
Nicolas Carion's avatar
Nicolas Carion committed
55
#include "logger.hpp"
56
#include <KLocalizedString>
57
#include <KMessageBox>
58
#include <QApplication>
Nicolas Carion's avatar
Nicolas Carion committed
59 60 61 62 63
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
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 91
    m_date = date;
    m_description = ClipController::description();
92
    if (m_clipType == ClipType::Audio) {
93
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
94 95 96
    } else {
        m_thumbnail = thumb;
    }
97 98
    // Make sure we have a hash for this clip
    hash();
99
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
100 101
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
102 103
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(const QString &, markers), Q_ARG(bool, true),
                                  Q_ARG(bool, false));
104
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
105
    connectEffectStack();
106 107
}

108
// static
Nicolas Carion's avatar
Nicolas Carion committed
109 110
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)
111
{
112
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
113
    baseFinishConstruct(self);
114
    self->m_effectStack->importEffects(producer, PlaylistState::Disabled, true);
115
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
116 117 118
    return self;
}

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

145 146
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
147
{
Nicolas Carion's avatar
Nicolas Carion committed
148
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, std::move(model)));
149 150 151 152
    baseFinishConstruct(self);
    return self;
}

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

163 164
void ProjectClip::connectEffectStack()
{
165 166
    connect(m_effectStack.get(), &EffectStackModel::modelChanged, this, &ProjectClip::updateChildProducers);
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, &ProjectClip::updateChildProducers);
167
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, [&]() {
168 169 170 171 172
        if (auto ptr = m_model.lock()) {
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                           AbstractProjectItem::IconOverlay);
        }
    });
173 174
    /*connect(m_effectStack.get(), &EffectStackModel::modelChanged, [&](){
        qDebug()<<"/ / / STACK CHANGED";
175
        updateChildProducers();
176
    });*/
177 178
}

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

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

197
void ProjectClip::updateAudioThumbnail(QVariantList audioLevels)
198
{
199
    std::swap(audioFrameCache, audioLevels); // avoid second copy
200
    m_audioThumbCreated = true;
201 202 203 204
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
    }
    updateTimelineClips({TimelineModel::AudioLevelsRole});
205 206 207 208
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

339
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
340
{
341 342
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
343
    if (m_clipType != ClipType::Unknown) {
344
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
345
    }
346
    return prod;
347 348
}

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

372 373
bool ProjectClip::hasAudioAndVideo() const
{
374
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0 && m_masterProducer->get_int("set.test_audio") == 0;
375 376 377 378 379
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
380 381 382 383 384 385
    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;
386 387 388
    }
}

389 390 391 392 393
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

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

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
533 534 535
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
536
        m_disabledProducer = cloneProducer();
Nicolas Carion's avatar
Nicolas Carion committed
537 538
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
539
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
540 541
    }
}
542 543

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

595
    // For timewarp clips, we keep one separate producer for each clip.
596 597
    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
598 599
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
600 601 602
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
603
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
604 605
        } else {
            m_timewarpProducers.erase(clipId);
606 607 608
        }
    }
    if (!warpProducer) {
609
        QLocale locale;
610 611 612 613 614
        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);
615
        warpProducer.reset(new Mlt::Producer(*originalProducer()->profile(), url.toUtf8().constData()));
616
        qDebug() << "new producer: " << url;
617 618 619
        qDebug() << "warp LENGTH before" << warpProducer->get_length();
        int original_length = originalProducer()->get_length();
        // this is a workaround to cope with Mlt erroneous rounding
620
        warpProducer->set("length", double(original_length) / std::abs(speed));
621 622
    }

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

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

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