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

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
11
by the membership of KDE e.V.), which shall act as a proxy
12
13
14
15
16
17
18
19
20
21
22
23
defined in Section 14 of version 3 of the license.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
66
67
68
69
70
71
72
#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>
73

Nicolas Carion's avatar
Nicolas Carion committed
74
75
76
77
78
79
80
#pragma GCC diagnostic pop
RTTR_REGISTRATION
{
    using namespace rttr;
    registration::class_<ProjectClip>("ProjectClip");
}

Nicolas Carion's avatar
Nicolas Carion committed
81
ProjectClip::ProjectClip(const QString &id, const QIcon &thumb, const std::shared_ptr<ProjectItemModel> &model, std::shared_ptr<Mlt::Producer> producer)
82
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
Nicolas Carion's avatar
Nicolas Carion committed
83
    , ClipController(id, std::move(producer))
84
    , m_resetTimelineOccurences(false)
85
{
86
    m_markerModel = std::make_shared<MarkerListModel>(id, pCore->projectManager()->undoStack());
87
    if (producer->get_int("_placeholder") == 1) {
88
        m_clipStatus = FileStatus::StatusMissing;
89
    } else if (producer->get_int("_missingsource") == 1) {
90
91
92
        m_clipStatus = FileStatus::StatusProxyOnly;
    } else if (m_usesProxy) {
        m_clipStatus = FileStatus::StatusProxy;
93
    } else {
94
        m_clipStatus = FileStatus::StatusReady;
95
    }
96
97
    m_name = clipName();
    m_duration = getStringDuration();
98
    m_inPoint = 0;
99
    m_outPoint = 0;
100
101
    m_date = date;
    m_description = ClipController::description();
102
    if (m_clipType == ClipType::Audio) {
103
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
104
105
106
    } else {
        m_thumbnail = thumb;
    }
107
108
    // Make sure we have a hash for this clip
    hash();
Vincent Pinon's avatar
Vincent Pinon committed
109
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, this, [&]() {
110
111
        setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson());
    });
112
113
    QString markers = getProducerProperty(QStringLiteral("kdenlive:markers"));
    if (!markers.isEmpty()) {
Vincent Pinon's avatar
Vincent Pinon committed
114
        QMetaObject::invokeMethod(m_markerModel.get(), "importFromJson", Qt::QueuedConnection, Q_ARG(QString, markers), Q_ARG(bool, true),
115
                                  Q_ARG(bool, false));
116
    }
117
118
    setTags(getProducerProperty(QStringLiteral("kdenlive:tags")));
    AbstractProjectItem::setRating((uint) getProducerIntProperty(QStringLiteral("kdenlive:rating")));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
119
    connectEffectStack();
120
121
}

122
// static
Nicolas Carion's avatar
Nicolas Carion committed
123
124
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)
125
{
126
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
127
    baseFinishConstruct(self);
Vincent Pinon's avatar
Vincent Pinon committed
128
    QMetaObject::invokeMethod(model.get(), "loadSubClips", Qt::QueuedConnection, Q_ARG(QString, id), Q_ARG(QString, self->getProducerProperty(QStringLiteral("kdenlive:clipzones"))));
129
130
131
    return self;
}

132
void ProjectClip::importEffects(const std::shared_ptr<Mlt::Producer> &producer, QString originalDecimalPoint)
133
{
134
    m_effectStack->importEffects(producer, PlaylistState::Disabled, true, originalDecimalPoint);
135
136
}

Nicolas Carion's avatar
Nicolas Carion committed
137
ProjectClip::ProjectClip(const QString &id, const QDomElement &description, const QIcon &thumb, const std::shared_ptr<ProjectItemModel> &model)
138
    : AbstractProjectItem(AbstractProjectItem::ClipItem, id, model)
139
    , ClipController(id)
140
    , m_resetTimelineOccurences(false)
141
{
142
    m_clipStatus = FileStatus::StatusWaiting;
143
    m_thumbnail = thumb;
144
    m_markerModel = std::make_shared<MarkerListModel>(m_binId, pCore->projectManager()->undoStack());
145
    if (description.hasAttribute(QStringLiteral("type"))) {
146
        m_clipType = (ClipType::ProducerType)description.attribute(QStringLiteral("type")).toInt();
147
        if (m_clipType == ClipType::Audio) {
148
            m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
149
        }
150
    }
151
    m_temporaryUrl = getXmlProperty(description, QStringLiteral("resource"));
152
    QString clipName = getXmlProperty(description, QStringLiteral("kdenlive:clipname"));
153
154
    if (!clipName.isEmpty()) {
        m_name = clipName;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
155
    } else if (!m_temporaryUrl.isEmpty()) {
156
        m_name = QFileInfo(m_temporaryUrl).fileName();
157
158
    } else {
        m_name = i18n("Untitled");
159
    }
Vincent Pinon's avatar
Vincent Pinon committed
160
    connect(m_markerModel.get(), &MarkerListModel::modelChanged, this, [&]() { setProducerProperty(QStringLiteral("kdenlive:markers"), m_markerModel->toJson()); });
161
162
}

163
164
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
165
{
Nicolas Carion's avatar
Nicolas Carion committed
166
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, std::move(model)));
167
168
169
170
    baseFinishConstruct(self);
    return self;
}

171
172
ProjectClip::~ProjectClip()
{
173
    // controller is deleted in bincontroller
174
    m_thumbMutex.lock();
175
    m_requestedThumbs.clear();
176
    m_thumbMutex.unlock();
177
    m_thumbThread.waitForFinished();
178
179
}

180
181
void ProjectClip::connectEffectStack()
{
Vincent Pinon's avatar
Vincent Pinon committed
182
    connect(m_effectStack.get(), &EffectStackModel::dataChanged, this, [&]() {
183
184
185
186
187
        if (auto ptr = m_model.lock()) {
            std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                           AbstractProjectItem::IconOverlay);
        }
    });
188
189
}

190
191
QString ProjectClip::getToolTip() const
{
192
193
194
195
    if (m_clipType == ClipType::Color && m_path.contains(QLatin1Char('/'))) {
        return m_path.section(QLatin1Char('/'), -1);
    }
    return m_path;
196
197
}

198
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
199
{
200
    QString value = defaultValue;
201
    QDomNodeList props = producer.elementsByTagName(QStringLiteral("property"));
202
    for (int i = 0; i < props.count(); ++i) {
203
        if (props.at(i).toElement().attribute(QStringLiteral("name")) == propertyName) {
204
205
206
207
208
209
210
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

211
void ProjectClip::updateAudioThumbnail()
212
{
Vincent Pinon's avatar
Vincent Pinon committed
213
    emit audioThumbReady();
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    if (m_clipType == ClipType::Audio) {
        QImage thumb = ThumbnailCache::get()->getThumbnail(m_binId, 0);
        if (thumb.isNull()) {
            int iconHeight = QFontInfo(qApp->font()).pixelSize() * 3.5;
            QImage img(QSize(iconHeight * pCore->getCurrentDar(), iconHeight), QImage::Format_ARGB32);
            img.fill(Qt::darkGray);
            QMap <int, QString> streams = audioInfo()->streams();
            QMap <int, int> channelsList = audioInfo()->streamChannels();
            QPainter painter(&img);
            QPen pen = painter.pen();
            pen.setColor(Qt::white);
            painter.setPen(pen);
            int streamCount = 0;
            if (streams.count() > 0) {
                double streamHeight = iconHeight / streams.count();
                QMapIterator<int, QString> st(streams);
                while (st.hasNext()) {
                    st.next();
                    int channels = channelsList.value(st.key());
                    double channelHeight = (double) streamHeight / channels;
                    const QVector <uint8_t> audioLevels = audioFrameCache(st.key());
235
236
                    qreal indicesPrPixel = qreal(audioLevels.length()) / img.width();
                    int idx;
237
238
239
                    for (int channel = 0; channel < channels; channel++) {
                        double y = (streamHeight * streamCount) + (channel * channelHeight) + channelHeight / 2;
                        for (int i = 0; i <= img.width(); i++) {
240
                            idx = ceil(i * indicesPrPixel);
241
242
                            idx += idx % channels;
                            idx += channel;
243
244
                            if (idx >= audioLevels.length() || idx < 0) {
                                break;
245
                            }
246
247
248
                            double level = audioLevels.at(idx) * channelHeight / 510.; // divide height by 510 (2*255) to get height
                            painter.drawLine(i, y - level, i, y + level);
                        }
249
250
251
252
253
254
255
256
257
258
                    }
                    streamCount++;
                }
            }
            thumb = img;
            // Cache thumbnail
            ThumbnailCache::get()->storeThumbnail(m_binId, 0, thumb, true);
        }
        setThumbnail(thumb);
    }
259
260
261
    if (!KdenliveSettings::audiothumbnails()) {
        return;
    }
262
    m_audioThumbCreated = true;
263
    updateTimelineClips({TimelineModel::ReloadThumbRole});
264
265
266
267
}

bool ProjectClip::audioThumbCreated() const
{
268
    return (m_audioThumbCreated);
269
270
}

271
ClipType::ProducerType ProjectClip::clipType() const
272
{
273
    return m_clipType;
274
275
}

276
277
bool ProjectClip::hasParent(const QString &id) const
{
278
279
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
280
281
282
283
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
284
285
286
287
    }
    return false;
}

288
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
289
{
Nicolas Carion's avatar
Nicolas Carion committed
290
    if (id == m_binId) {
291
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
292
    }
293
    return std::shared_ptr<ProjectClip>();
294
295
}

296
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
297
{
298
    Q_UNUSED(id)
299
    return std::shared_ptr<ProjectFolder>();
300
301
}

302
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
303
{
304
    for (int i = 0; i < childCount(); ++i) {
305
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
306
307
308
309
        if (clip) {
            return clip;
        }
    }
310
    return std::shared_ptr<ProjectSubClip>();
311
312
}

313
314
315
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
316
    for (int i = 0; i < childCount(); ++i) {
317
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
318
319
320
321
322
323
324
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

325
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
326
{
Nicolas Carion's avatar
Nicolas Carion committed
327
    if (ix == row()) {
328
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
329
    }
330
    return std::shared_ptr<ProjectClip>();
331
332
}

333
/*bool ProjectClip::isValid() const
334
{
335
336
    return m_controller->isValid();
}*/
337

338
339
bool ProjectClip::hasUrl() const
{
340
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
341
        return (!clipUrl().isEmpty());
342
    }
343
344
345
    return false;
}

346
const QString ProjectClip::url() const
347
{
348
    return clipUrl();
349
350
}

351
GenTime ProjectClip::duration() const
352
{
353
    return getPlaytime();
354
355
}

356
size_t ProjectClip::frameDuration() const
357
{
358
    return (size_t)getFramePlaytime();
359
360
}

361
void ProjectClip::reloadProducer(bool refreshOnly, bool isProxy, bool forceAudioReload)
362
{
363
364
365
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
366
    QMutexLocker lock(&m_thumbMutex);
367
    if (refreshOnly) {
368
369
        // 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
370
        // Clear cache first
371
        ThumbnailCache::get()->invalidateThumbsForClip(clipId());
372
373
        pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
        m_thumbsProducer.reset();
Vincent Pinon's avatar
Vincent Pinon committed
374
        emit pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), -1, true, true);
375
    } else {
376
        // If another load job is running?
377
        pCore->jobManager()->discardJobs(clipId());
378
379
380
        if (QFile::exists(m_path) && !hasProxy()) {
            clearBackupProperties();
        }
381
382
383
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
384
            bool hashChanged = false;
385
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
386
            m_thumbsProducer.reset();
387
            ClipType::ProducerType type = clipType();
388
            if (type != ClipType::Color && type != ClipType::Image && type != ClipType::SlideShow) {
389
390
                xml.removeAttribute("out");
            }
391
392
393
394
395
396
397
398
399
400
401
            if (type == ClipType::Audio || type == ClipType::AV) {
                // Check if source file was changed and rebuild audio data if necessary
                QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
                if (!clipHash.isEmpty()) {
                    if (clipHash != getFileHash()) {
                        // Source clip has changed, rebuild data
                        hashChanged = true;
                    }
                }
                
            }
402
            ThumbnailCache::get()->invalidateThumbsForClip(clipId());
403
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, loadjobId, QString(), xml);
Vincent Pinon's avatar
Vincent Pinon committed
404
            emit pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), -1, true, true);
405
            if (forceAudioReload || (!isProxy && hashChanged)) {
406
407
                discardAudioThumb();
            }
408
            if (KdenliveSettings::audiothumbnails()) {
409
                emit pCore->jobManager()->startJob<AudioThumbJob>({clipId()}, loadJob, QString());
410
            }
411
        }
412
    }
413
414
}

415
QDomElement ProjectClip::toXml(QDomDocument &document, bool includeMeta, bool includeProfile)
416
{
417
    getProducerXML(document, includeMeta, includeProfile);
418
419
420
421
422
423
    QDomElement prod;
    if (document.documentElement().tagName() == QLatin1String("producer")) {
        prod = document.documentElement();
    } else {
        prod = document.documentElement().firstChildElement(QStringLiteral("producer"));
    }
424
    if (m_clipType != ClipType::Unknown) {
425
        prod.setAttribute(QStringLiteral("type"), (int)m_clipType);
426
    }
427
    return prod;
428
429
}

Laurent Montel's avatar
Laurent Montel committed
430
void ProjectClip::setThumbnail(const QImage &img)
431
{
432
433
434
    if (img.isNull()) {
        return;
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
435
436
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
437
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
438
        QPainter p(&thumb);
439
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
440
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
441
442
443
444
445
446
447
448
        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
449
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
450
    if (auto ptr = m_model.lock()) {
451
452
        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
453
    }
454
455
}

456
457
bool ProjectClip::hasAudioAndVideo() const
{
458
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0 && m_masterProducer->get_int("set.test_audio") == 0;
459
460
461
462
463
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
464
465
466
467
468
469
    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;
470
471
472
    }
}

473
474
475
476
477
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

478
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
479
{
Vincent Pinon's avatar
Vincent Pinon committed
480
    Q_UNUSED(replaceProducer)
481
    qDebug() << "################### ProjectClip::setproducer";
482
    QMutexLocker locker(&m_producerMutex);
483
    FileStatus::ClipStatus currentStatus = m_clipStatus;
Nicolas Carion's avatar
Nicolas Carion committed
484
    updateProducer(producer);
485
    emit producerChanged(m_binId, producer);
486
    m_thumbsProducer.reset();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
487
    connectEffectStack();
488

489
490
491
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
492
    }
493
494
495
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
496
    if (m_clipType == ClipType::Audio) {
497
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
498
    } else if (m_clipType == ClipType::Image) {
499
        if (producer->get_int("meta.media.width") < 8 || producer->get_int("meta.media.height") < 8) {
500
501
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
502
        }
503
    }
504
    m_duration = getStringDuration();
505
506
507
508
    m_clipStatus = m_usesProxy ? FileStatus::StatusProxy : FileStatus::StatusReady;
    if (m_clipStatus != currentStatus) {
        updateTimelineClips({TimelineModel::StatusRole});
    }
509
510
    setTags(getProducerProperty(QStringLiteral("kdenlive:tags")));
    AbstractProjectItem::setRating((uint) getProducerIntProperty(QStringLiteral("kdenlive:rating")));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
511
    if (auto ptr = m_model.lock()) {
512
513
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                       AbstractProjectItem::DataDuration);
514
        std::static_pointer_cast<ProjectItemModel>(ptr)->updateWatcher(std::static_pointer_cast<ProjectClip>(shared_from_this()));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
515
    }
516
    // Make sure we have a hash for this clip
517
    getFileHash();
518
519
    // set parent again (some info need to be stored in producer)
    updateParent(parentItem().lock());
520

521
    if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableproxy")).toInt() == 1) {
522
523
        QList<std::shared_ptr<ProjectClip>> clipList;
        // automatic proxy generation enabled
524
        if (m_clipType == ClipType::Image && pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateimageproxy")).toInt() == 1) {
525
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyimageminsize() &&
Vincent Pinon's avatar
Vincent Pinon committed
526
                getProducerProperty(QStringLiteral("kdenlive:proxy")) == QLatin1String()) {
527
528
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
529
        } else if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateproxy")).toInt() == 1 &&
Vincent Pinon's avatar
Vincent Pinon committed
530
                   (m_clipType == ClipType::AV || m_clipType == ClipType::Video) && getProducerProperty(QStringLiteral("kdenlive:proxy")) == QLatin1String()) {
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
            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()) {
554
555
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
556
        }
557
558
        if (!clipList.isEmpty()) {
            pCore->currentDoc()->slotProxyCurrentItem(true, clipList, false);
559
560
561
        }
    }
    pCore->bin()->reloadMonitorIfActive(clipId());
562
563
564
    for (auto &p : m_audioProducers) {
        m_effectStack->removeService(p.second);
    }
565
566
567
    for (auto &p : m_videoProducers) {
        m_effectStack->removeService(p.second);
    }
568
569
570
571
572
    for (auto &p : m_timewarpProducers) {
        m_effectStack->removeService(p.second);
    }
    // Release audio producers
    m_audioProducers.clear();
573
    m_videoProducers.clear();
574
    m_timewarpProducers.clear();
575
    emit refreshPropertiesPanel();
576
    if (KdenliveSettings::hoverPreview() && (m_clipType == ClipType::AV || m_clipType == ClipType::Video || m_clipType == ClipType::Playlist)) {
577
        QTimer::singleShot(1000, this, [this]() {
578
579
            int loadjobId;
            if (!pCore->jobManager()->hasPendingJob(m_binId, AbstractClipJob::CACHEJOB, &loadjobId)) {
Vincent Pinon's avatar
Vincent Pinon committed
580
                emit pCore->jobManager()->startJob<CacheJob>({m_binId}, -1, QString());
581
            }
582
583
        });
    }
584
    replaceInTimeline();
585
    updateTimelineClips({TimelineModel::IsProxyRole});
586
    return true;
587
588
}

589
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
590
591
592
593
{
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
594
    if (clipType() == ClipType::Unknown) {
Laurent Montel's avatar
Laurent Montel committed
595
        return nullptr;
596
    }
597
    QMutexLocker lock(&m_thumbMutex);
598
599
    std::shared_ptr<Mlt::Producer> prod = originalProducer();
    if (!prod->is_valid()) {
Laurent Montel's avatar
Laurent Montel committed
600
        return nullptr;
601
    }
602
    if (KdenliveSettings::gpu_accel()) {
603
        // TODO: when the original producer changes, we must reload this thumb producer
604
        m_thumbsProducer = softClone(ClipController::getPassPropertiesList());
605
    } else {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
        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);
620
621
            // Required to make get_playtime() return > 1
            m_thumbsProducer->set("out", m_thumbsProducer->get_length() -1);
622
623
624
625
            m_thumbsProducer->attach(scaler);
            m_thumbsProducer->attach(padder);
            m_thumbsProducer->attach(converter);
        }
626
627
628
629
    }
    return m_thumbsProducer;
}

Nicolas Carion's avatar
Nicolas Carion committed
630
631
632
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
633
        m_disabledProducer = cloneProducer();
Nicolas Carion's avatar
Nicolas Carion committed
634
635
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
636
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
637
638
    }
}
639

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
int ProjectClip::getRecordTime()
{
    if (m_masterProducer && (m_clipType == ClipType::AV || m_clipType == ClipType::Video || m_clipType == ClipType::Audio)) {
        int recTime = m_masterProducer->get_int("kdenlive:record_date");
        if (recTime > 0) {
            return recTime;
        }
        if (recTime < 0) {
            // Cannot read record date on this clip, abort
            return 0;
        }
        // Try to get record date metadata
        if (KdenliveSettings::mediainfopath().isEmpty()) {
        }
        QProcess extractInfo;
        extractInfo.start(KdenliveSettings::mediainfopath(), {url(),QStringLiteral("--output=XML")});
        extractInfo.waitForFinished();
        if(extractInfo.exitStatus() != QProcess::NormalExit || extractInfo.exitCode() != 0) {
            KMessageBox::error(QApplication::activeWindow(), i18n("Cannot extract metadata from %1\n%2", url(),
                        QString(extractInfo.readAllStandardError())));
            return 0;
        }
        QDomDocument doc;
        doc.setContent(extractInfo.readAllStandardOutput());
664
665
666
667
668
669
        bool dateFormat = false;
        QDomNodeList nodes = doc.documentElement().elementsByTagName(QStringLiteral("TimeCode_FirstFrame"));
        if (nodes.isEmpty()) {
            nodes = doc.documentElement().elementsByTagName(QStringLiteral("Recorded_Date"));
            dateFormat = true;
        }
670
        if (!nodes.isEmpty()) {
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
            // Parse recorded time (HH:MM:SS)
            QString recInfo = nodes.at(0).toElement().text();
            if (!recInfo.isEmpty()) {
                if (dateFormat) {
                    if (recInfo.contains(QLatin1Char('+'))) {
                        recInfo = recInfo.section(QLatin1Char('+'), 0, 0);
                    } else if (recInfo.contains(QLatin1Char('-'))) {
                        recInfo = recInfo.section(QLatin1Char('-'), 0, 0);
                    }
                    QDateTime date = QDateTime::fromString(recInfo, "yyyy-MM-dd hh:mm:ss");
                    recTime = date.time().msecsSinceStartOfDay();
                } else {
                    // Timecode Format HH:MM:SS:FF
                    // Check if we have a different fps
                    double producerFps = m_masterProducer->get_double("meta.media.frame_rate_num") / m_masterProducer->get_double("meta.media.frame_rate_den");
                    if (!qFuzzyCompare(producerFps, pCore->getCurrentFps())) {
                        // Producer and project have a different fps
                        bool ok;
                        int frames = recInfo.section(QLatin1Char(':'), -1).toInt(&ok);
                        if (ok) {
                            frames *= pCore->getCurrentFps() / producerFps;
                            recInfo.chop(2);
                            recInfo.append(QString::number(frames).rightJustified(1, QChar('0')));
                        }
                    }
                    recTime = 1000 * pCore->timecode().getFrameCount(recInfo) / pCore->getCurrentFps();
697
698
699
700
701
702
703
704
705
706
707
708
                }
                m_masterProducer->set("kdenlive:record_date", recTime);
                return recTime;
            }
        } else {
            m_masterProducer->set("kdenlive:record_date", -1);
            return 0;
        }
    }
    return 0;
}

709
std::shared_ptr<Mlt::Producer> ProjectClip::getTimelineProducer(int trackId, int clipId, PlaylistState::ClipState state, int audioStream, double speed, bool secondPlaylist)
710
{
711
712
713
    if (!m_masterProducer) {
        return nullptr;
    }
714
715
    if (qFuzzyCompare(speed, 1.0)) {
        // we are requesting a normal speed producer
716
717
718
719
720
        bool byPassTrackProducer = false;
        if (trackId == -1 && (state != PlaylistState::AudioOnly || audioStream == m_masterProducer->get_int("audio_index"))) {
            byPassTrackProducer = true;
        }
        if (byPassTrackProducer ||
721
            (state == PlaylistState::VideoOnly && (m_clipType == ClipType::Color || m_clipType == ClipType::Image || m_clipType == ClipType::Text|| m_clipType == ClipType::TextTemplate || m_clipType == ClipType::Qml))) {
722
            // Temporary copy, return clone of master
723
            int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
724
            return std::shared_ptr<Mlt::Producer>(m_masterProducer->cut(-1, duration > 0 ? duration - 1 : -1));
725
        }
726
727
728
729
        if (m_timewarpProducers.count(clipId) > 0) {
            m_effectStack->removeService(m_timewarpProducers[clipId]);
            m_timewarpProducers.erase(clipId);
        }
730
731
        if (state == PlaylistState::AudioOnly) {
            // We need to get an audio producer, if none exists
732
733
734
735
736
737
738
            if (audioStream > -1) {
                if (trackId >= 0) {
                    trackId += 100 * audioStream;
                } else {
                    trackId -= 100 * audioStream;
                }
            }
739
740
741
742
            // second playlist producers use negative trackId
            if (secondPlaylist) {
                trackId = -trackId;
            }
743
744
745
746
            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);
747
748
                if (m_streamEffects.contains(audioStream)) {
                    QStringList effects = m_streamEffects.value(audioStream);
Vincent Pinon's avatar
Vincent Pinon committed
749
                    for (const QString &effect : qAsConst(effects)) {
750
751
752
753
754
755
756
757
                        Mlt::Filter filt(*m_audioProducers[trackId]->profile(), effect.toUtf8().constData());
                        if (filt.is_valid()) {
                            // Add stream effect markup
                            filt.set("kdenlive:stream", 1);
                            m_audioProducers[trackId]->attach(filt);
                        }
                    }
                }
758
759
760
                if (audioStream > -1) {
                    m_audioProducers[trackId]->set("audio_index", audioStream);
                }
761
                m_effectStack->addService(m_audioProducers[trackId]);
762
            }
763
            return std::shared_ptr<Mlt::Producer>(m_audioProducers[trackId]->cut());
764
        }
765
766
767
        if (m_audioProducers.count(trackId) > 0) {
            m_effectStack->removeService(m_audioProducers[trackId]);
            m_audioProducers.erase(trackId);
768
        }
Nicolas Carion's avatar
Nicolas Carion committed
769
770
        if (state == PlaylistState::VideoOnly) {
            // we return the video producer
771
            // We need to get an video producer, if none exists
772
773
774
775
            // second playlist producers use negative trackId
            if (secondPlaylist) {
                trackId = -trackId;
            }
776
777
            if (m_videoProducers.count(trackId) == 0) {
                m_videoProducers[trackId] = cloneProducer(true);
778
                // Let audio enabled so that we can use audio visualization filters ?
779
780
781
                m_videoProducers[trackId]->set("set.test_audio", 1);
                m_videoProducers[trackId]->set("set.test_image", 0);
                m_effectStack->addService(m_videoProducers[trackId]);
782
            }
783
            int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
784
            return std::shared_ptr<Mlt::Producer>(m_videoProducers[trackId]->cut(-1, duration > 0 ? duration - 1: -1));
785
        }
786
787
788
        if (m_videoProducers.count(trackId) > 0) {
            m_effectStack->removeService(m_videoProducers[trackId]);
            m_videoProducers.erase(trackId);
Nicolas Carion's avatar
Nicolas Carion committed
789
790
791
        }
        Q_ASSERT(state == PlaylistState::Disabled);
        createDisabledMasterProducer();
792
        int duration = m_masterProducer->time_to_frames(m_masterProducer->get("kdenlive:duration"));
793
        return std::shared_ptr<Mlt::Producer>(m_disabledProducer->cut(-1, duration > 0 ? duration - 1: -1));
794
795
    }

796
    // For timewarp clips, we keep one separate producer for each clip.
797
798
    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
799
800
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
801
802
803
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
804
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
805
806
        } else {
            m_timewarpProducers.erase(clipId);
807
808
809
        }
    }
    if (!warpProducer) {
810
811
812
813
        QString resource(originalProducer()->get("resource"));
        if (resource.isEmpty() || resource == QLatin1String("<producer>")) {
            resource = m_service;
        }
Vincent Pinon's avatar
Vincent Pinon committed
814
        QString url = QString("timewarp:%1:%2").arg(QString::fromStdString(std::to_string(speed)), resource);
Nicolas Carion's avatar
Nicolas Carion committed
815
        warpProducer.reset(new Mlt::Producer(*originalProducer()->profile(), url.toUtf8().constData()));
816
        qDebug() << "new producer: " << url;
817
818
819
        qDebug() << "warp LENGTH before" << warpProducer->get_length();
        int original_length = originalProducer()->get_length();
        // this is a workaround to cope with Mlt erroneous rounding
820
821
822
        Mlt::Properties original(m_masterProducer->get_properties());
        Mlt::Properties cloneProps(warpProducer->get_properties());
        cloneProps.pass_list(original, ClipController::getPassPropertiesList(false));
823
        warpProducer->set("length", (int) (original_length / std::abs(speed) + 0.5));
824
825
    }

826
    qDebug() << "warp LENGTH" << warpProducer->get_length();
827
828
    warpProducer->set("set.test_audio", 1);
    warpProducer->set("set.test_image", 1);
829
    warpProducer->set("kdenlive:id", binId().toUtf8().constData());
830
831
832
833
834
835
836
    if (state == PlaylistState::AudioOnly) {
        warpProducer->set("set.test_audio", 0);
    }
    if (state == PlaylistState::VideoOnly) {
        warpProducer->set("set.test_image", 0);
    }
    m_timewarpProducers[clipId] = warpProducer;
837
    m_effectStack->addService(m_timewarpProducers[clipId]);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
838
    return std::shared_ptr<Mlt::Producer>(warpProducer->cut());
839
840
}

841
std::pair<std::shared_ptr<Mlt::Producer>, bool> ProjectClip::giveMasterAndGetTimelineProducer(int clipId, std::shared_ptr<Mlt::Producer> master, PlaylistState::ClipState state, int tid, bool secondPlaylist)
842
843
844
845
846
847
848
{
    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;
849
        bool timeWarp = false;
850
851
852
853
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
            speed = master->parent().get_double("warp_speed");
            timeWarp = true;
        }
854
        if (master->parent().get_int("_loaded") == 1) {