projectclip.cpp 83 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/audiolevelstask.h"
31
#include "jobs/cliploadtask.h"
32
#include "jobs/proxytask.h"
33
#include "jobs/cachetask.h"
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
#include "macros.hpp"
48

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

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

Vincent Pinon's avatar
Vincent Pinon committed
64
65
#ifdef CRASH_AUTO_TEST
#include "logger.hpp"
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
#pragma GCC diagnostic pop
RTTR_REGISTRATION
{
    using namespace rttr;
    registration::class_<ProjectClip>("ProjectClip");
}
Vincent Pinon's avatar
Vincent Pinon committed
80
81
#endif

Nicolas Carion's avatar
Nicolas Carion committed
82

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

130
// static
Nicolas Carion's avatar
Nicolas Carion committed
131
132
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)
133
{
134
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, thumb, model, producer));
135
    baseFinishConstruct(self);
Vincent Pinon's avatar
Vincent Pinon committed
136
    QMetaObject::invokeMethod(model.get(), "loadSubClips", Qt::QueuedConnection, Q_ARG(QString, id), Q_ARG(QString, self->getProducerProperty(QStringLiteral("kdenlive:clipzones"))));
137
138
139
    return self;
}

140
void ProjectClip::importEffects(const std::shared_ptr<Mlt::Producer> &producer, QString originalDecimalPoint)
141
{
142
    m_effectStack->importEffects(producer, PlaylistState::Disabled, true, originalDecimalPoint);
143
144
}

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

171
172
std::shared_ptr<ProjectClip> ProjectClip::construct(const QString &id, const QDomElement &description, const QIcon &thumb,
                                                    std::shared_ptr<ProjectItemModel> model)
173
{
Nicolas Carion's avatar
Nicolas Carion committed
174
    std::shared_ptr<ProjectClip> self(new ProjectClip(id, description, thumb, std::move(model)));
175
176
177
178
    baseFinishConstruct(self);
    return self;
}

179
180
181
182
ProjectClip::~ProjectClip()
{
}

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

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

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

214
void ProjectClip::updateAudioThumbnail()
215
{
Vincent Pinon's avatar
Vincent Pinon committed
216
    emit audioThumbReady();
217
218
    if (m_clipType == ClipType::Audio) {
        QImage thumb = ThumbnailCache::get()->getThumbnail(m_binId, 0);
219
        if (thumb.isNull() && !pCore->taskManager.hasPendingJob({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::AUDIOTHUMBJOB)) {
220
221
            int iconHeight = int(QFontInfo(qApp->font()).pixelSize() * 3.5);
            QImage img(QSize(int(iconHeight * pCore->getCurrentDar()), iconHeight), QImage::Format_ARGB32);
222
223
224
225
226
227
228
229
230
231
232
233
234
235
            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());
236
                    double channelHeight = double(streamHeight) / channels;
237
                    const QVector <uint8_t> audioLevels = audioFrameCache(st.key());
238
239
                    qreal indicesPrPixel = qreal(audioLevels.length()) / img.width();
                    int idx;
240
241
242
                    for (int channel = 0; channel < channels; channel++) {
                        double y = (streamHeight * streamCount) + (channel * channelHeight) + channelHeight / 2;
                        for (int i = 0; i <= img.width(); i++) {
243
                            idx = int(ceil(i * indicesPrPixel));
244
245
                            idx += idx % channels;
                            idx += channel;
246
247
                            if (idx >= audioLevels.length() || idx < 0) {
                                break;
248
                            }
249
                            double level = audioLevels.at(idx) * channelHeight / 510.; // divide height by 510 (2*255) to get height
250
                            painter.drawLine(i, int(y - level), i, int(y + level));
251
                        }
252
253
254
255
256
257
258
259
                    }
                    streamCount++;
                }
            }
            thumb = img;
            // Cache thumbnail
            ThumbnailCache::get()->storeThumbnail(m_binId, 0, thumb, true);
        }
260
261
262
        if (!thumb.isNull()) {
            setThumbnail(thumb, -1, -1);
        }
263
    }
264
265
266
    if (!KdenliveSettings::audiothumbnails()) {
        return;
    }
267
    m_audioThumbCreated = true;
268
    updateTimelineClips({TimelineModel::ReloadThumbRole});
269
270
271
272
}

bool ProjectClip::audioThumbCreated() const
{
273
    return (m_audioThumbCreated);
274
275
}

276
ClipType::ProducerType ProjectClip::clipType() const
277
{
278
    return m_clipType;
279
280
}

281
282
bool ProjectClip::hasParent(const QString &id) const
{
283
284
    std::shared_ptr<AbstractProjectItem> par = parent();
    while (par) {
285
286
287
288
        if (par->clipId() == id) {
            return true;
        }
        par = par->parent();
289
290
291
292
    }
    return false;
}

293
std::shared_ptr<ProjectClip> ProjectClip::clip(const QString &id)
294
{
Nicolas Carion's avatar
Nicolas Carion committed
295
    if (id == m_binId) {
296
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
297
    }
298
    return std::shared_ptr<ProjectClip>();
299
300
}

301
std::shared_ptr<ProjectFolder> ProjectClip::folder(const QString &id)
302
{
303
    Q_UNUSED(id)
304
    return std::shared_ptr<ProjectFolder>();
305
306
}

307
std::shared_ptr<ProjectSubClip> ProjectClip::getSubClip(int in, int out)
308
{
309
    for (int i = 0; i < childCount(); ++i) {
310
        std::shared_ptr<ProjectSubClip> clip = std::static_pointer_cast<ProjectSubClip>(child(i))->subClip(in, out);
311
312
313
314
        if (clip) {
            return clip;
        }
    }
315
    return std::shared_ptr<ProjectSubClip>();
316
317
}

318
319
320
QStringList ProjectClip::subClipIds() const
{
    QStringList subIds;
321
    for (int i = 0; i < childCount(); ++i) {
322
        std::shared_ptr<AbstractProjectItem> clip = std::static_pointer_cast<AbstractProjectItem>(child(i));
323
324
325
326
327
328
329
        if (clip) {
            subIds << clip->clipId();
        }
    }
    return subIds;
}

330
std::shared_ptr<ProjectClip> ProjectClip::clipAt(int ix)
331
{
Nicolas Carion's avatar
Nicolas Carion committed
332
    if (ix == row()) {
333
        return std::static_pointer_cast<ProjectClip>(shared_from_this());
334
    }
335
    return std::shared_ptr<ProjectClip>();
336
337
}

338
/*bool ProjectClip::isValid() const
339
{
340
341
    return m_controller->isValid();
}*/
342

343
344
bool ProjectClip::hasUrl() const
{
345
    if ((m_clipType != ClipType::Color) && (m_clipType != ClipType::Unknown)) {
346
        return (!clipUrl().isEmpty());
347
    }
348
349
350
    return false;
}

351
const QString ProjectClip::url() const
352
{
353
    return clipUrl();
354
355
}

356
357
358
359
360
const QSize ProjectClip::frameSize() const
{
    return getFrameSize();
}

361
GenTime ProjectClip::duration() const
362
{
363
    return getPlaytime();
364
365
}

366
size_t ProjectClip::frameDuration() const
367
{
368
    return size_t(getFramePlaytime());
369
370
}

371
void ProjectClip::reloadProducer(bool refreshOnly, bool isProxy, bool forceAudioReload)
372
{
373
    // we find if there are some loading job on that clip
374
    QMutexLocker lock(&m_thumbMutex);
375
    if (refreshOnly) {
376
377
        // 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
378
        // Clear cache first
379
        ThumbnailCache::get()->invalidateThumbsForClip(clipId());
380
        pCore->taskManager.discardJobs({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::LOADJOB, true);
381
        pCore->taskManager.discardJobs({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::CACHEJOB);
382
        m_thumbsProducer.reset();
383
        ClipLoadTask::start({ObjectType::BinClip,m_binId.toInt()}, QDomElement(), true, -1, -1, this);
384
    } else {
385
        // If another load job is running?
386
387
388
        pCore->taskManager.discardJobs({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::LOADJOB, true);
        pCore->taskManager.discardJobs({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::CACHEJOB);
        if (QFile::exists(m_path) && (!isProxy && !hasProxy())) {
389
390
            clearBackupProperties();
        }
391
392
393
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
394
            bool hashChanged = false;
395
            m_thumbsProducer.reset();
396
            ClipType::ProducerType type = clipType();
397
            if (type != ClipType::Color && type != ClipType::Image && type != ClipType::SlideShow) {
398
399
                xml.removeAttribute("out");
            }
400
401
402
403
404
405
406
407
408
409
            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;
                    }
                }
            }
410
            ThumbnailCache::get()->invalidateThumbsForClip(clipId());
411
            if (forceAudioReload || (!isProxy && hashChanged)) {
412
413
                discardAudioThumb();
            }
414
            ClipLoadTask::start({ObjectType::BinClip,m_binId.toInt()}, xml, false, -1, -1, this);
415
        }
416
    }
417
418
}

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

434
void ProjectClip::setThumbnail(const QImage &img, int in, int out)
435
{
436
437
438
    if (img.isNull()) {
        return;
    }
439
440
441
442
443
444
445
    if (in > -1) {
        std::shared_ptr<ProjectSubClip> sub = getSubClip(in, out);
        if (sub) {
            sub->setThumbnail(img);
        }
        return;
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
446
447
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
448
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
449
        QPainter p(&thumb);
450
        QColor c(220, 220, 10, 200);
451
        QRect r(0, 0, int(thumb.height() / 2.5), int(thumb.height() / 2.5));
452
453
454
455
456
457
458
459
        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
460
    m_thumbnail = QIcon(thumb);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
461
    if (auto ptr = m_model.lock()) {
462
463
        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
464
    }
465
466
}

467
468
bool ProjectClip::hasAudioAndVideo() const
{
469
    return hasAudio() && hasVideo() && m_masterProducer->get_int("set.test_image") == 0 && m_masterProducer->get_int("set.test_audio") == 0;
470
471
472
473
474
}

bool ProjectClip::isCompatible(PlaylistState::ClipState state) const
{
    switch (state) {
475
476
477
478
479
480
    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;
481
482
483
    }
}

484
485
486
487
488
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

489
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer)
490
{
491
    qDebug() << "################### ProjectClip::setproducer";
492
    QMutexLocker locker(&m_producerMutex);
493
    FileStatus::ClipStatus currentStatus = m_clipStatus;
Nicolas Carion's avatar
Nicolas Carion committed
494
    updateProducer(producer);
495
    emit producerChanged(m_binId, producer);
496
497
498
499
    if (producer->get_int("kdenlive:transcodingrequired") == 1) {
        pCore->bin()->requestTranscoding(clipUrl(), clipId());
        producer->set("kdenlive:transcodingrequired", nullptr);
    }
500
    m_thumbsProducer.reset();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
501
    connectEffectStack();
502

503
504
505
    // Update info
    if (m_name.isEmpty()) {
        m_name = clipName();
506
    }
507
508
509
    m_date = date;
    m_description = ClipController::description();
    m_temporaryUrl.clear();
510
    if (m_clipType == ClipType::Audio) {
511
        m_thumbnail = QIcon::fromTheme(QStringLiteral("audio-x-generic"));
512
    } else if (m_clipType == ClipType::Image) {
513
        if (producer->get_int("meta.media.width") < 8 || producer->get_int("meta.media.height") < 8) {
514
515
            KMessageBox::information(QApplication::activeWindow(),
                                     i18n("Image dimension smaller than 8 pixels.\nThis is not correctly supported by our video framework."));
516
        }
517
    }
518
    m_duration = getStringDuration();
519
520
521
522
    m_clipStatus = m_usesProxy ? FileStatus::StatusProxy : FileStatus::StatusReady;
    if (m_clipStatus != currentStatus) {
        updateTimelineClips({TimelineModel::StatusRole});
    }
523
    setTags(getProducerProperty(QStringLiteral("kdenlive:tags")));
524
    AbstractProjectItem::setRating(uint(getProducerIntProperty(QStringLiteral("kdenlive:rating"))));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
525
    if (auto ptr = m_model.lock()) {
526
527
        std::static_pointer_cast<ProjectItemModel>(ptr)->onItemUpdated(std::static_pointer_cast<ProjectClip>(shared_from_this()),
                                                                       AbstractProjectItem::DataDuration);
528
        std::static_pointer_cast<ProjectItemModel>(ptr)->updateWatcher(std::static_pointer_cast<ProjectClip>(shared_from_this()));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
529
    }
530
    // Make sure we have a hash for this clip
531
    getFileHash();
532
533
    // set parent again (some info need to be stored in producer)
    updateParent(parentItem().lock());
534
    AudioLevelsTask::start({ObjectType::BinClip, m_binId.toInt()}, this, false);
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    pCore->bin()->reloadMonitorIfActive(clipId());
    for (auto &p : m_audioProducers) {
        m_effectStack->removeService(p.second);
    }
    for (auto &p : m_videoProducers) {
        m_effectStack->removeService(p.second);
    }
    for (auto &p : m_timewarpProducers) {
        m_effectStack->removeService(p.second);
    }
    // Release audio producers
    m_audioProducers.clear();
    m_videoProducers.clear();
    m_timewarpProducers.clear();
    emit refreshPropertiesPanel();
    replaceInTimeline();
    updateTimelineClips({TimelineModel::IsProxyRole});
    bool generateProxy = false;
    QList<std::shared_ptr<ProjectClip>> clipList;
554
    if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("enableproxy")).toInt() == 1) {
555
        // automatic proxy generation enabled
556
        if (m_clipType == ClipType::Image && pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateimageproxy")).toInt() == 1) {
557
            if (getProducerIntProperty(QStringLiteral("meta.media.width")) >= KdenliveSettings::proxyimageminsize() &&
Vincent Pinon's avatar
Vincent Pinon committed
558
                getProducerProperty(QStringLiteral("kdenlive:proxy")) == QLatin1String()) {
559
560
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
561
        } else if (pCore->currentDoc()->getDocumentProperty(QStringLiteral("generateproxy")).toInt() == 1 &&
Vincent Pinon's avatar
Vincent Pinon committed
562
                   (m_clipType == ClipType::AV || m_clipType == ClipType::Video) && getProducerProperty(QStringLiteral("kdenlive:proxy")) == QLatin1String()) {
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
            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()) {
586
587
                clipList << std::static_pointer_cast<ProjectClip>(shared_from_this());
            }
588
        }
589
        if (!clipList.isEmpty()) {
590
            generateProxy = true;
591
592
        }
    }
593
    if (!generateProxy && KdenliveSettings::hoverPreview() && (m_clipType == ClipType::AV || m_clipType == ClipType::Video || m_clipType == ClipType::Playlist)) {
594
        QTimer::singleShot(1000, this, [this]() {
595
            CacheTask::start({ObjectType::BinClip,m_binId.toInt()}, 30, 0, 0, this);
596
597
        });
    }
598
599
600
    if (generateProxy) {
        QMetaObject::invokeMethod(pCore->currentDoc(), "slotProxyCurrentItem", Q_ARG(bool,true), Q_ARG(QList<std::shared_ptr<ProjectClip>>,clipList), Q_ARG(bool,false));
    }
601
    return true;
602
603
}

604
605
606
607
608
void ProjectClip::setThumbProducer(std::shared_ptr<Mlt::Producer>prod)
{
    m_thumbsProducer = std::move(prod);
}

609
std::shared_ptr<Mlt::Producer> ProjectClip::thumbProducer()
610
611
612
613
{
    if (m_thumbsProducer) {
        return m_thumbsProducer;
    }
614
    if (clipType() == ClipType::Unknown || m_masterProducer == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
615
        return nullptr;
616
    }
617
    QMutexLocker lock(&m_thumbMutex);
618
    if (KdenliveSettings::gpu_accel()) {
619
        // TODO: when the original producer changes, we must reload this thumb producer
620
        m_thumbsProducer = softClone(ClipController::getPassPropertiesList());
621
    } else {
622
623
624
625
626
627
628
629
630
631
632
633
634
635
        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);
636
637
            // Required to make get_playtime() return > 1
            m_thumbsProducer->set("out", m_thumbsProducer->get_length() -1);
638
639
640
641
            m_thumbsProducer->attach(scaler);
            m_thumbsProducer->attach(padder);
            m_thumbsProducer->attach(converter);
        }
642
643
644
645
    }
    return m_thumbsProducer;
}

Nicolas Carion's avatar
Nicolas Carion committed
646
647
648
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
649
        m_disabledProducer = cloneProducer();
Nicolas Carion's avatar
Nicolas Carion committed
650
651
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
652
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
653
654
    }
}
655

656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
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());
680
681
682
683
684
685
        bool dateFormat = false;
        QDomNodeList nodes = doc.documentElement().elementsByTagName(QStringLiteral("TimeCode_FirstFrame"));
        if (nodes.isEmpty()) {
            nodes = doc.documentElement().elementsByTagName(QStringLiteral("Recorded_Date"));
            dateFormat = true;
        }
686
        if (!nodes.isEmpty()) {
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
            // 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) {
Vincent Pinon's avatar
Vincent Pinon committed
707
                            frames *= int(pCore->getCurrentFps() / producerFps);
708
709
710
711
                            recInfo.chop(2);
                            recInfo.append(QString::number(frames).rightJustified(1, QChar('0')));
                        }
                    }
712
                    recTime = int(1000 * pCore->timecode().getFrameCount(recInfo) / pCore->getCurrentFps());
713
714
715
716
717
718
719
720
721
722
723
724
                }
                m_masterProducer->set("kdenlive:record_date", recTime);
                return recTime;
            }
        } else {
            m_masterProducer->set("kdenlive:record_date", -1);
            return 0;
        }
    }
    return 0;
}

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

812
    // For timewarp clips, we keep one separate producer for each clip.
813
814
    std::shared_ptr<Mlt::Producer> warpProducer;
    if (m_timewarpProducers.count(clipId) > 0) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
815
816
        // remove in all cases, we add it unconditionally anyways
        m_effectStack->removeService(m_timewarpProducers[clipId]);
817
818
819
        if (qFuzzyCompare(m_timewarpProducers[clipId]->get_double("warp_speed"), speed)) {
            // the producer we have is good, use it !
            warpProducer = m_timewarpProducers[clipId];
820
            qDebug() << "Reusing producer!";
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
821
822
        } else {
            m_timewarpProducers.erase(clipId);
823
824
825
        }
    }
    if (!warpProducer) {
826
827
828
829
        QString resource(originalProducer()->get("resource"));
        if (resource.isEmpty() || resource == QLatin1String("<producer>")) {
            resource = m_service;
        }
Vincent Pinon's avatar
Vincent Pinon committed
830
        QString url = QString("timewarp:%1:%2").arg(QString::fromStdString(std::to_string(speed)), resource);
Nicolas Carion's avatar
Nicolas Carion committed
831
        warpProducer.reset(new Mlt::Producer(*originalProducer()->profile(), url.toUtf8().constData()));
832
        qDebug() << "new producer: " << url;
833
834
835
        qDebug() << "warp LENGTH before" << warpProducer->get_length();
        int original_length = originalProducer()->get_length();
        // this is a workaround to cope with Mlt erroneous rounding
836
837
838
        Mlt::Properties original(m_masterProducer->get_properties());
        Mlt::Properties cloneProps(warpProducer->get_properties());
        cloneProps.pass_list(original, ClipController::getPassPropertiesList(false));
839
        warpProducer->set("length", int(original_length / std::abs(speed) + 0.5));
840
        warpProducer->set("audio_index", audioStream);
841
842
    }

843
844
845
846
847
848
849
    //if the producer has a "time-to-live" (frame duration) we need to scale it according to the speed
    int ttl = originalProducer()->get_int("ttl");
    if(ttl > 0) {
        int new_ttl = ttl / std::abs(speed) + 0.5;
        warpProducer->set("ttl", std::max(new_ttl, 1));
    }

850
    qDebug() << "warp LENGTH" << warpProducer->get_length();
851
852
    warpProducer->set("set.test_audio", 1);
    warpProducer->set("set.test_image", 1);
853
    warpProducer->set("kdenlive:id", binId().toUtf8().constData());
854
855
856
857
858
859
860
    if (state == PlaylistState::AudioOnly) {
        warpProducer->set("set.test_audio", 0);
    }
    if (state == PlaylistState::VideoOnly) {
        warpProducer->set("set.test_image", 0);
    }
    m_timewarpProducers[clipId] = warpProducer;
861
    m_effectStack->addService(m_timewarpProducers[clipId]);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
862
    return std::shared_ptr<Mlt::Producer>(warpProducer->cut());
863
864
}

865
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)
866
867
868
869
870
871
872
{
    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;
873
        bool timeWarp = false;
874
875
876
877
        if (QString::fromUtf8(master->parent().get("mlt_service")) == QLatin1String("timewarp")) {
            speed = master->parent().get_double("warp_speed");
            timeWarp = true;
        }
878
        if (master->parent().get_int("_loaded") == 1) {