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

Vincent Pinon's avatar
Vincent Pinon committed
65
66
#ifdef CRASH_AUTO_TEST
#include "logger.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
67
68
69
70
71
72
73
#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>
74

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

Nicolas Carion's avatar
Nicolas Carion committed
83

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

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

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

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

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

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

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
219
    if (m_clipType == ClipType::Audio) {
        QImage thumb = ThumbnailCache::get()->getThumbnail(m_binId, 0);
        if (thumb.isNull()) {
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
260
261
                    }
                    streamCount++;
                }
            }
            thumb = img;
            // Cache thumbnail
            ThumbnailCache::get()->storeThumbnail(m_binId, 0, thumb, true);
        }
        setThumbnail(thumb);
    }
262
263
264
    if (!KdenliveSettings::audiothumbnails()) {
        return;
    }
265
    m_audioThumbCreated = true;
266
    updateTimelineClips({TimelineModel::ReloadThumbRole});
267
268
269
270
}

bool ProjectClip::audioThumbCreated() const
{
271
    return (m_audioThumbCreated);
272
273
}

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

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

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

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

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

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

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

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

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

349
const QString ProjectClip::url() const
350
{
351
    return clipUrl();
352
353
}

354
355
356
357
358
const QSize ProjectClip::frameSize() const
{
    return getFrameSize();
}

359
GenTime ProjectClip::duration() const
360
{
361
    return getPlaytime();
362
363
}

364
size_t ProjectClip::frameDuration() const
365
{
366
    return size_t(getFramePlaytime());
367
368
}

369
void ProjectClip::reloadProducer(bool refreshOnly, bool isProxy, bool forceAudioReload)
370
{
371
372
373
    // we find if there are some loading job on that clip
    int loadjobId = -1;
    pCore->jobManager()->hasPendingJob(clipId(), AbstractClipJob::LOADJOB, &loadjobId);
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
381
        pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
        m_thumbsProducer.reset();
Vincent Pinon's avatar
Vincent Pinon committed
382
        emit pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadjobId, QString(), -1, true, true);
383
    } else {
384
        // If another load job is running?
385
        pCore->jobManager()->discardJobs(clipId());
386
387
388
        if (QFile::exists(m_path) && !hasProxy()) {
            clearBackupProperties();
        }
389
390
391
        QDomDocument doc;
        QDomElement xml = toXml(doc);
        if (!xml.isNull()) {
392
            bool hashChanged = false;
393
            pCore->jobManager()->discardJobs(clipId(), AbstractClipJob::THUMBJOB);
394
            m_thumbsProducer.reset();
395
            ClipType::ProducerType type = clipType();
396
            if (type != ClipType::Color && type != ClipType::Image && type != ClipType::SlideShow) {
397
398
                xml.removeAttribute("out");
            }
399
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
            int loadJob = pCore->jobManager()->startJob<LoadJob>({clipId()}, loadjobId, QString(), xml);
Vincent Pinon's avatar
Vincent Pinon committed
412
            emit pCore->jobManager()->startJob<ThumbJob>({clipId()}, loadJob, QString(), -1, true, true);
413
            if (forceAudioReload || (!isProxy && hashChanged)) {
414
415
                discardAudioThumb();
            }
416
            if (KdenliveSettings::audiothumbnails()) {
417
                emit pCore->jobManager()->startJob<AudioThumbJob>({clipId()}, loadJob, QString());
418
            }
419
        }
420
    }
421
422
}

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

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

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

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

481
482
483
484
485
QPixmap ProjectClip::thumbnail(int width, int height)
{
    return m_thumbnail.pixmap(width, height);
}

486
bool ProjectClip::setProducer(std::shared_ptr<Mlt::Producer> producer, bool replaceProducer)
487
{
Vincent Pinon's avatar
Vincent Pinon committed
488
    Q_UNUSED(replaceProducer)
489
    qDebug() << "################### ProjectClip::setproducer";
490
    QMutexLocker locker(&m_producerMutex);
491
    FileStatus::ClipStatus currentStatus = m_clipStatus;
Nicolas Carion's avatar
Nicolas Carion committed
492
    updateProducer(producer);
493
    emit producerChanged(m_binId, producer);
494
    m_thumbsProducer.reset();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
495
    connectEffectStack();
496

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

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

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

Nicolas Carion's avatar
Nicolas Carion committed
638
639
640
void ProjectClip::createDisabledMasterProducer()
{
    if (!m_disabledProducer) {
641
        m_disabledProducer = cloneProducer();
Nicolas Carion's avatar
Nicolas Carion committed
642
643
        m_disabledProducer->set("set.test_audio", 1);
        m_disabledProducer->set("set.test_image", 1);
644
        m_effectStack->addService(m_disabledProducer);
Nicolas Carion's avatar
Nicolas Carion committed
645
646
    }
}
647

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

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

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

835
836
837
838
839
840
841
    //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));
    }

842
    qDebug() << "warp LENGTH" << warpProducer->get_length();