projectclip.cpp 51.9 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"
54
#include <KLocalizedString>
55
#include <KMessageBox>
56
#include <QApplication>
Nicolas Carion's avatar
Nicolas Carion committed
57 58 59 60 61
#include <QCryptographicHash>
#include <QDir>
#include <QDomElement>
#include <QFile>
#include <QtConcurrent>
Nicolas Carion's avatar
Nicolas Carion committed
62
#include <utility>
63

64 65
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model, std::shared_ptr<Mlt::Producer> producer)
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
66
    , ClipController(id, producer)
67
    , m_thumbsProducer(nullptr)
68
{
69
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
70
    m_clipStatus = StatusReady;
71 72
    m_name = clipName();
    m_duration = getStringDuration();
73
    m_inPoint = 0;
74 75
    m_date = date;
    m_description = ClipController::description();
76
    if (m_clipType == ClipType::Audio) {
77
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
78 79 80
    } else {
        m_thumbnail = thumb;
    }
81 82
    // Make sure we have a hash for this clip
    hash();
83
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
84 85
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
86 87
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(const QString &, markers), Q_ARG(bool, true),
                                  Q_ARG(bool, false));
88
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
89
    connectEffectStack();
90 91
}

92 93
// static
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model,
94
                                                    std::shared_ptr<Mlt::Producer> producer)
95
{
96
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
97
    baseFinishConstruct(self);
98
    self->m_effectStack->importEffects(producer, PlaylistState::Disabled, true);
99
    model->loadSubClips(id, self->getPropertiesFromPrefix(QStringLiteral("kdenlive:clipzone.")));
100 101 102
    return self;
}

103
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, std::shared_ptr<ProjectItemModel> model)
104
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
105
    , ClipController(id)
106
    , m_thumbsProducer(nullptr)
107
{
108
    m_clipStatus = StatusWaiting;
109
    m_thumbnail = thumb;
110
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
111
    if (description.hasAttribute(QStringLiteral("type"))) {
112
        m_clipType = (ClipType::ProducerType)description.attribute(QStringLiteral("type")).toInt();
113
        if (m_clipType == ClipType::Audio) {
114
            m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
115
        }
116
    }
117
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
118
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
119 120
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
121
    } else if (!m_temporaryUrl.isEmpty()) {
122
        m_name = QFileInfo(m_temporaryUrl).fileName();
123 124
    } else {
        m_name = i18n("Untitled");
125
    }
126
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
127 128
}

129 130
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
131
{
132
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, model));
133 134 135 136
    baseFinishConstruct(self);
    return self;
}

137 138
ProjectClip::~ProjectClip()
{
139
    // controller is deleted in bincontroller
140
    m_thumbMutex.lock();
141
    m_requestedThumbs.clear();
142
    m_thumbMutex.unlock();
143
    m_thumbThread.waitForFinished();
144
    audioFrameCache.clear();
145 146
}

147 148
void ProjectClip::connectEffectStack()
{
149 150
    connect(m_effectStack.get(), &EffectStackModel::modelChanged, this, &ProjectClip::updateChildProducers);
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, &ProjectClip::updateChildProducers);
151 152 153 154 155 156
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, [&](){
        if (auto ptr = m_model.lock()) {
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                           AbstractProjectItem::IconOverlay);
        }
    });
157 158
    /*connect(m_effectStack.get(), &EffectStackModel::modelChanged, [&](){
        qDebug()<<"/ / / STACK CHANGED";
159
        updateChildProducers();
160
    });*/
161 162
}

163 164
QString ProjectClip::getToolTip() const
{
165
    return url();
166 167
}

168
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
169
{
170
    QString value = defaultValue;
171
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
172
    for (int i = 0; i < props.count(); ++i) {
173
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
174 175 176 177 178 179 180
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

181
void ProjectClip::updateAudioThumbnail(QVariantList audioLevels)
182
{
183
    std::swap(audioFrameCache, audioLevels); // avoid second copy
184
    m_audioThumbCreated = true;
185 186 187 188
    if (auto ptr = m_model.lock()) {
        emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshAudioThumbs(m_binId);
    }
    updateTimelineClips({TimelineModel::AudioLevelsRole});
189 190 191 192
}

bool ProjectClip::audioThumbCreated() const
{
193
    return (m_audioThumbCreated);
194 195
}

196
ClipType::ProducerType ProjectClip::clipType() const
197
{
198
    return m_clipType;
199 200
}

201 202
bool ProjectClip::hasParent(const QString &id) const
{
203 204
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
205 206 207 208
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
209 210 211 212
    }
    return false;
}

213
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
214
{
Nicolas Carion's avatar
Nicolas Carion committed
215
    if (id == m_binId) {
216
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
217
    }
218
    return std::shared_ptr<ProjectClip>();
219 220
}

221
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
222
{
223
    Q_UNUSED(id)
224
    return std::shared_ptr<ProjectFolder>();
225 226
}

227
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
228
{
229
    for (int i = 0; i < childCount(); ++i) {
230
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
231 232 233 234
        if (clip) {
            return clip;
        }
    }
235
    return std::shared_ptr<ProjectSubClip>();
236 237
}

238 239 240
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
241
    for (int i = 0; i < childCount(); ++i) {
242
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
243 244 245 246 247 248 249
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

250
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
251
{
Nicolas Carion's avatar
Nicolas Carion committed
252
    if (ix == row()) {
253
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
254
    }
255
    return std::shared_ptr<ProjectClip>();
256 257
}

258
/*bool ProjectClip::isValid() const
259
{
260 261
    return m_controller->isValid();
}*/
262

263 264
bool ProjectClip::hasUrl() const
{
265
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
266
        return (!clipUrl().isEmpty());
267
    }
268 269 270
    return false;
}

271
const QString ProjectClip::url() const
272
{
273
    return clipUrl();
274 275
}

276
GenTime ProjectClip::duration() const
277
{
278
    return getPlaytime();
279 280
}

281 282 283
int ProjectClip::frameDuration() const
{
    GenTime d = duration();
284
    return d.frames(pCore->getCurrentFps());
285 286
}

287
void ProjectClip::reloadProducer(bool refreshOnly)
288
{
289 290 291
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
292
    if (refreshOnly) {
293 294
        // 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
295
        // Clear cache first
296
        m_thumbsProducer.reset();
297 298
        ThumbnailCache::get()->invalidateThumbsForClip(clipId());
        pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), 150, -1, true, true);
299 300

    } else {
301 302 303 304
        // If another load job is running?
        if (loadjobId > -1) {
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::LOADJOB);
        }
305 306 307
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
308
            m_thumbsProducer.reset();
309
            ThumbnailCache::get()->invalidateThumbsForClip(clipId());
310
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, loadjobId, QString(), xml);
311
            pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), 150, -1, true, true);
312
        }
313
    }
314 315
}

316
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta)
317
{
318 319
    getProducerXML(document, includeMeta);
    QDomElement prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
320
    if (m_clipType != ClipType::Unknown) {
321
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
322
    }
323
    return prod;
324 325
}

Laurent Montel's avatar
Laurent Montel committed
326
void ProjectClip::setThumbnail(const QImage &img)
327
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
328 329
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
330
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
331
        QPainter p(&thumb);
332
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
333
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
334 335 336 337 338 339 340 341
        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
342
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
343
    if (auto ptr = m_model.lock()) {
344 345
        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
346
    }
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
bool ProjectClip::hasAudioAndVideo() const
{
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0  && m_masterProducer->get_int("set.test_audio") == 0;
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
        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;
    }
}

366 367 368 369 370
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

371
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
372
{
Vincent Pinon's avatar
Vincent Pinon committed
373
    Q_UNUSED(replaceProducer)
374
    qDebug() << "################### ProjectClip::setproducer";
375
    QMutexLocker locker(&m_producerMutex);
Nicolas Carion's avatar
Nicolas Carion committed
376
    updateProducer(std::move(producer));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
377
    connectEffectStack();
378

379 380 381
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
382
    }
383 384 385
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
386
    if (m_clipType == ClipType::Audio) {
387
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
388
    } else if (m_clipType == ClipType::Image) {
389
        if (getProducerIntProperty(QStringLiteral("meta.media.width")) < 8 || getProducerIntProperty(QStringLiteral("meta.media.height")) < 8) {
390 391
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
392
        }
393
    }
394
    m_duration = getStringDuration();
395
    m_clipStatus = StatusReady;
396
    if (!hasProxy()) {
Nicolas Carion's avatar
Nicolas Carion committed
397
        if (auto ptr = m_model.lock()) emit std::static_pointer_cast<ProjectItemModel>(ptr)->refreshPanel(m_binId);
398
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
399
    if (auto ptr = m_model.lock()) {
400 401
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                       AbstractProjectItem::DataDuration);
402
        std::static_pointer_cast<ProjectItemModel>(ptr)->updateWatcher(std::static_pointer_cast<ProjectClip>(shared_from_this()));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
403
    }
404
    // Make sure we have a hash for this clip
405
    getFileHash();
406 407
    // set parent again (some info need to be stored in producer)
    updateParent(parentItem().lock());
408
    if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableproxy")).toInt() == 1) {
409 410
        QList<std::shared_ptr<ProjectClip>> clipList;
        // automatic proxy generation enabled
411
        if (m_clipType == ClipType::Image && pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateimageproxy")).toInt() == 1) {
412 413 414
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyimageminsize() && getProducerProperty(QStringLiteral("kdenlive:proxy")) == QStringLiteral()) {
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
415
        } else if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateproxy")).toInt() == 1 && (m_clipType == ClipType::AV || m_clipType == ClipType::Video)) {
416 417 418
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyminsize() && getProducerProperty(QStringLiteral("kdenlive:proxy")) == QStringLiteral()) {
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
419
        }
420 421
        if (!clipList.isEmpty()) {
            pCore->currentDoc()->slotProxyCurrentItem(true, clipList, false);
422 423 424
        }
    }
    pCore->bin()->reloadMonitorIfActive(clipId());
425 426 427 428 429 430 431 432
    if (m_videoProducer) {
        m_videoProducer.reset(m_masterProducer->cut());
        if (hasAudio()) {
            // disable audio but activate video
            m_videoProducer->set("set.test_audio", 1);
            m_videoProducer->set("set.test_image", 0);
        }
    }
433 434 435 436 437 438 439 440 441
    for (auto &p : m_audioProducers) {
        m_effectStack->removeService(p.second);
    }
    for (auto &p : m_timewarpProducers) {
        m_effectStack->removeService(p.second);
    }
    // Release audio producers
    m_audioProducers.clear();
    m_timewarpProducers.clear();
442
    emit refreshPropertiesPanel();
443
    replaceInTimeline();
444
    return true;
445 446
}

447
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
448 449 450 451
{
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
452
    if (clipType() == ClipType::Unknown) {
Laurent Montel's avatar
Laurent Montel committed
453
        return nullptr;
454
    }
455 456
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
457
        return nullptr;
458
    }
459
    if (KdenliveSettings::gpu_accel()) {
460
        // TODO: when the original producer changes, we must reload this thumb producer
461
        m_thumbsProducer = softClone(ClipController::getPassPropertiesList());
462 463
        Mlt::Filter scaler(*prod->profile(), "swscale");
        Mlt::Filter converter(*prod->profile(), "avcolor_space");
464 465
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
466
    } else {
467
        m_thumbsProducer = cloneProducer(pCore->thumbProfile());
468 469 470 471
        Mlt::Filter scaler(*pCore->thumbProfile(), "swscale");
        Mlt::Filter converter(*pCore->thumbProfile(), "avcolor_space");
        m_thumbsProducer->attach(scaler);
        m_thumbsProducer->attach(converter);
472 473 474 475
    }
    return m_thumbsProducer;
}

476 477 478
void ProjectClip::createVideoMasterProducer()
{
    if (!m_videoProducer) {
479 480 481 482 483 484
        m_videoProducer = std::shared_ptr<Mlt::Producer>(m_masterProducer->cut());
        if (hasAudio()) {
            // disable audio but activate video
            m_videoProducer->set("set.test_audio", 1);
            m_videoProducer->set("set.test_image", 0);
        }
485 486
    }
}
487

Nicolas Carion's avatar
Nicolas Carion committed
488 489 490 491 492 493
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);
494
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
495 496
    }
}
497
std::shared_ptr<Mlt::Producer> ProjectClip::getTimelineProducer(int clipId, PlaylistState::ClipState state, double speed)
498
{
499
    qDebug() << "producer request"<<clipId<<speed;
500 501 502
    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
503 504 505 506
        if (m_timewarpProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_timewarpProducers[clipId]);
            m_timewarpProducers.erase(clipId);
        }
507 508 509 510 511 512
        if (state == PlaylistState::AudioOnly) {
            // We need to get an audio producer, if none exists
            if (m_audioProducers.count(clipId) == 0) {
                m_audioProducers[clipId] = cloneProducer(&pCore->getCurrentProfile()->profile());
                m_audioProducers[clipId]->set("set.test_audio", 0);
                m_audioProducers[clipId]->set("set.test_image", 1);
513
                m_effectStack->addService(m_audioProducers[clipId]);
514 515 516
            }
            return std::shared_ptr<Mlt::Producer>(m_audioProducers[clipId]->cut());
        }
517 518 519 520
        if (m_audioProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_audioProducers[clipId]);
            m_audioProducers.erase(clipId);
        }
Nicolas Carion's avatar
Nicolas Carion committed
521 522 523
        if (state == PlaylistState::VideoOnly) {
            // we return the video producer
            createVideoMasterProducer();
524 525
            int duration = m_masterProducer->get_int("kdenlive:duration");
            return std::shared_ptr<Mlt::Producer>(m_videoProducer->cut(-1, duration > 0 ? duration: -1));
Nicolas Carion's avatar
Nicolas Carion committed
526 527 528
        }
        Q_ASSERT(state == PlaylistState::Disabled);
        createDisabledMasterProducer();
529 530
        int duration = m_masterProducer->get_int("kdenlive:duration");
        return std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(-1, duration > 0 ? duration: -1));
531 532 533
    }

    // in that case, we need to create a warp producer, if we don't have one
534 535 536 537
    if (m_audioProducers.count(clipId) > 0) {
        m_effectStack->removeService(m_audioProducers[clipId]);
        m_audioProducers.erase(clipId);
    }
538 539 540

    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
541 542
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
543 544 545
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
546
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
547 548
        } else {
            m_timewarpProducers.erase(clipId);
549 550 551
        }
    }
    if (!warpProducer) {
552
        QLocale locale;
553 554 555 556 557 558 559
        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()));
        qDebug() << "new producer: "<<url;
560 561 562 563
        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);
564 565
    }

566
    qDebug() << "warp LENGTH" << warpProducer->get_length();
567 568 569 570 571 572 573 574 575
    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;
576
    m_effectStack->addService(m_timewarpProducers[clipId]);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
577
    return std::shared_ptr<Mlt::Producer>(warpProducer->cut());
578 579 580
}

std::pair<std::shared_ptr<Mlt::Producer>, bool> ProjectClip::giveMasterAndGetTimelineProducer(int clipId, std::shared_ptr<Mlt::Producer> master,
581
                                                                                              PlaylistState::ClipState state)
582 583 584 585 586 587 588 589 590 591 592 593
{
    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;
        double timeWarp = false;
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
            speed = master->parent().get_double("warp_speed");
            timeWarp = true;
        }
594
        if (master->parent().get_int("_loaded") == 1) {
595 596
            // we already have a clip that shares the same master

Vincent Pinon's avatar
Vincent Pinon committed
597
            if (state == PlaylistState::AudioOnly || !qFuzzyIsNull(timeWarp)) {
598
                // In that case, we must create copies
599 600
                std::shared_ptr<Mlt::Producer> prod(getTimelineProducer(clipId, state, speed)->cut(in, out));
                return {prod, false};
601 602
            }
            // if it's a video or disabled clip, we must make sure that its master clip matches our video master
Nicolas Carion's avatar
Nicolas Carion committed
603
            if (state == PlaylistState::VideoOnly && !m_videoProducer) {
604 605 606 607
                qDebug() << "Warning: weird, we found a video clip whose master is already loaded but we don't have any yet";
                createVideoMasterProducer();
                return {std::shared_ptr<Mlt::Producer>(m_videoProducer->cut(in, out)), false};
            }
Nicolas Carion's avatar
Nicolas Carion committed
608 609 610 611 612 613
            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::VideoOnly && QString::fromUtf8(m_videoProducer->get("id")) != QString::fromUtf8(master->parent().get("id"))) {
614 615 616
                qDebug() << "Warning: weird, we found a video clip whose master is already loaded but doesn't match ours";
                return {std::shared_ptr<Mlt::Producer>(m_videoProducer->cut(in, out)), false};
            }
Nicolas Carion's avatar
Nicolas Carion committed
617 618 619 620
            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};
            }
621 622 623
            // We have a good id, this clip can be used
            return {master, true};
        } else {
624
            master->parent().set("_loaded", 1);
625
            if (state == PlaylistState::AudioOnly) {
626
                m_audioProducers[clipId] = std::shared_ptr<Mlt::Producer>(new Mlt::Producer(&master->parent()));
627
                m_effectStack->addService(m_audioProducers[clipId]);
628 629
                return {master, true};
            }
Vincent Pinon's avatar
Vincent Pinon committed
630
            if (!qFuzzyIsNull(timeWarp)) {
631
                m_timewarpProducers[clipId] = std::shared_ptr<Mlt::Producer>(new Mlt::Producer(&master->parent()));
632
                m_effectStack->addService(m_timewarpProducers[clipId]);
633 634
                return {master, true};
            }
Nicolas Carion's avatar
Nicolas Carion committed
635
            if (state == PlaylistState::VideoOnly && !m_videoProducer) {
636
                // good, we found a master video producer, and we didn't have any
637
                m_videoProducer.reset(master->parent().cut());
638
                m_effectStack->addService(m_videoProducer);
639 640
                return {master, true};
            }
Nicolas Carion's avatar
Nicolas Carion committed
641 642
            if (state == PlaylistState::Disabled && !m_disabledProducer) {
                // good, we found a master disabled producer, and we didn't have any
643
                m_disabledProducer.reset(master->parent().cut());
644
                m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
645 646 647
                return {master, true};
            }
            qDebug() << "Warning: weird, we found a clip whose master is not loaded but we already have a master";
648 649 650 651 652
            return {std::shared_ptr<Mlt::Producer>(m_videoProducer->cut(in, out)), false};
        }
    } 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";
653
        exit(1);
654 655 656 657 658 659 660 661 662 663
        double speed = 1.0;
        if (QString::fromUtf8(master->get("mlt_service")) == QLatin1String("timewarp")) {
            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};
}
/*
664 665 666
std::shared_ptr<Mlt::Producer> ProjectClip::timelineProducer(PlaylistState::ClipState state, int track)
{
    if (!m_service.startsWith(QLatin1String("avformat"))) {
667
        std::shared_ptr<Mlt::Producer> prod(originalProducer()->cut());
668 669 670 671 672
        int length = getProducerIntProperty(QStringLiteral("kdenlive:duration"));
        if (length > 0) {
            prod->set_in_and_out(0, length);
        }
        return prod;
673 674 675
    }
    if (state == PlaylistState::VideoOnly) {
        if (m_timelineProducers.count(0) > 0) {
676
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(0)->second->cut());
677 678
        }
        std::shared_ptr<Mlt::Producer> videoProd = cloneProducer();
679
        videoProd->set("audio_index", -1);
680
        m_timelineProducers[0] = videoProd;
681
        return std::shared_ptr<Mlt::Producer>(videoProd->cut());
682 683 684
    }
    if (state == PlaylistState::AudioOnly) {
        if (m_timelineProducers.count(-track) > 0) {
685
            return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(-track)->second->cut());
686 687
        }
        std::shared_ptr<Mlt::Producer> audioProd = cloneProducer();
688
        audioProd->set("video_index", -1);
689
        m_timelineProducers[-track] = audioProd;
690
        return std::shared_ptr<Mlt::Producer>(audioProd->cut());
691 692
    }
    if (m_timelineProducers.count(track) > 0) {
693
        return std::shared_ptr<Mlt::Producer>(m_timelineProducers.find(track)->second->cut());
694 695 696
    }
    std::shared_ptr<Mlt::Producer> normalProd = cloneProducer();
    m_timelineProducers[track] = normalProd;
697
    return std::shared_ptr<Mlt::Producer>(normalProd->cut());
698
}*/
699

700
std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer(Mlt::Profile *destProfile)
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
    Mlt::Consumer c(*m_masterProducer->profile(), "xml", "string");
    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");
    c.start();
    if (ignore) {
        s.set("ignore_points", ignore);
    }
    const QByteArray clipXml = c.get("string");
720
    std::shared_ptr<Mlt::Producer> prod(new Mlt::Producer(destProfile ? *destProfile : *m_masterProducer->profile(), "xml-string", clipXml.constData()));
721 722 723
    if (strcmp(prod->get("mlt_service"), "avformat") == 0) {
        prod->set("mlt_service", "avformat-novalidate");
    }
724
    return prod;
725 726
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
std::shared_ptr<Mlt::Producer> ProjectClip::cloneProducer(std::shared_ptr<Mlt::Producer> producer)
{
    Mlt::Consumer c(*producer->profile(), "xml", "string");
    Mlt::Service s(producer->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");
    c.start();
    if (ignore) {
        s.set("ignore_points", ignore);
    }
    const QByteArray clipXml = c.get("string");
    std::shared_ptr<Mlt::Producer> prod(new Mlt::Producer(*producer->profile(), "xml-string", clipXml.constData()));
    if (strcmp(prod->get("mlt_service"), "avformat") == 0) {
        prod->set("mlt_service", "avformat-novalidate");
    }
    return prod;
}

std::shared_ptr<Mlt::Producer> ProjectClip::softClone(const char *list)
{
    QString service = QString::fromLatin1(m_masterProducer->get("mlt_service"));
    QString resource = QString::fromLatin1(m_masterProducer->get("resource"));
    std::shared_ptr<Mlt::Producer> clone(new Mlt::Producer(*m_masterProducer->profile(), service.toUtf8().constData(), resource.toUtf8().constData()));
    Mlt::Properties original(m_masterProducer->get_properties());
    Mlt::Properties cloneProps(clone->get_properties());
    cloneProps.pass_list(original, list);
    return clone;
}

765
bool ProjectClip::isReady() const
766
{
Nicolas Carion's avatar