meltBuilder.cpp 15.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***************************************************************************
 *   Copyright (C) 2017 by Nicolas Carion                                  *
 *   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  by the membership  *
 *   of KDE e.V.), which shall act as a proxy 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 "meltBuilder.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
23
#include "../clipmodel.hpp"
24
25
#include "../timelineitemmodel.hpp"
#include "../timelinemodel.hpp"
26
#include "../trackmodel.hpp"
Nicolas Carion's avatar
Nicolas Carion committed
27
#include "../undohelper.hpp"
28
#include "bin/bin.h"
29
#include "bin/projectitemmodel.h"
Nicolas Carion's avatar
Nicolas Carion committed
30
#include "core.h"
31
#include "kdenlivesettings.h"
32
33

#include <KLocalizedString>
34
#include <KMessageBox>
Nicolas Carion's avatar
Nicolas Carion committed
35
36
#include <QDebug>
#include <QSet>
37
38
#include <mlt++/MltPlaylist.h>
#include <mlt++/MltProducer.h>
39
#include <mlt++/MltTransition.h>
40
#include <mlt++/MltProfile.h>
41

42
static QStringList m_errorMessage;
43

44
bool constructTrackFromMelt(const std::shared_ptr<TimelineItemModel> &timeline, int tid, Mlt::Tractor &track,
45
                            const std::unordered_map<QString, QString> &binIdCorresp, Fun &undo, Fun &redo, bool audioTrack);
46
bool constructTrackFromMelt(const std::shared_ptr<TimelineItemModel> &timeline, int tid, Mlt::Playlist &track,
47
                            const std::unordered_map<QString, QString> &binIdCorresp, Fun &undo, Fun &redo, bool audioTrack);
48

Nicolas Carion's avatar
Nicolas Carion committed
49
bool constructTimelineFromMelt(const std::shared_ptr<TimelineItemModel> &timeline, Mlt::Tractor tractor)
50
{
Nicolas Carion's avatar
Nicolas Carion committed
51
52
53
    Fun undo = []() { return true; };
    Fun redo = []() { return true; };
    // First, we destruct the previous tracks
54
    timeline->requestReset(undo, redo);
55
    m_errorMessage.clear();
56
57
    std::unordered_map<QString, QString> binIdCorresp;
    pCore->projectItemModel()->loadBinPlaylist(&tractor, timeline->tractor(), binIdCorresp);
58

59
    QSet<QString> reserved_names{QLatin1String("playlistmain"), QLatin1String("timeline_preview"), QLatin1String("timeline_overlay"),
Nicolas Carion's avatar
Nicolas Carion committed
60
                                 QLatin1String("black_track")};
61
62

    bool ok = true;
63
    qDebug() << "//////////////////////\nTrying to construct" << tractor.count() << "tracks.\n////////////////////////////////";
64
65
66
67
68
69
    for (int i = 0; i < tractor.count() && ok; i++) {
        std::unique_ptr<Mlt::Producer> track(tractor.track(i));
        QString playlist_name = track->get("id");
        if (reserved_names.contains(playlist_name)) {
            continue;
        }
Nicolas Carion's avatar
Nicolas Carion committed
70
        switch (track->type()) {
71
        case producer_type:
Nicolas Carion's avatar
Nicolas Carion committed
72
            // TODO check that it is the black track, and otherwise log an error
73
74
75
            qDebug() << "SUSPICIOUS: we weren't expecting a producer when parsing the timeline";
            break;
        case tractor_type: {
Nicolas Carion's avatar
Nicolas Carion committed
76
            // that is a double track
77
            int tid;
78
            bool audioTrack = track->get_int("kdenlive:audio_track") == 1;
79
            ok = timeline->requestTrackInsertion(-1, tid, QString(), audioTrack, undo, redo, false);
80
            int lockState = track->get_int("kdenlive:locked_track");
81
            Mlt::Tractor local_tractor(*track);
82
            ok = ok && constructTrackFromMelt(timeline, tid, local_tractor, binIdCorresp, undo, redo, audioTrack);
83
            timeline->setTrackProperty(tid, QStringLiteral("kdenlive:thumbs_format"), track->get("kdenlive:thumbs_format"));
84
            timeline->setTrackProperty(tid, QStringLiteral("kdenlive:audio_rec"), track->get("kdenlive:audio_rec"));
85
86
87
            if (lockState > 0) {
                timeline->setTrackProperty(tid, QStringLiteral("kdenlive:locked_track"), QString::number(lockState));
            }
88
89
90
            break;
        }
        case playlist_type: {
Nicolas Carion's avatar
Nicolas Carion committed
91
92
            // that is a single track
            qDebug() << "Adding track: " << track->get("id");
93
            int tid;
Nicolas Carion's avatar
Nicolas Carion committed
94
            Mlt::Playlist local_playlist(*track);
95
            const QString trackName = local_playlist.get("kdenlive:track_name");
96
            bool audioTrack = local_playlist.get_int("kdenlive:audio_track") == 1;
97
            ok = timeline->requestTrackInsertion(-1, tid, trackName, audioTrack, undo, redo, false);
98
99
100
101
102
            int muteState = track->get_int("hide");
            if (muteState > 0 && (!audioTrack || (audioTrack && muteState != 1))) {
                timeline->setTrackProperty(tid, QStringLiteral("hide"), QString::number(muteState));
            }
            int lockState = local_playlist.get_int("kdenlive:locked_track");
103
            ok = ok && constructTrackFromMelt(timeline, tid, local_playlist, binIdCorresp, undo, redo, audioTrack);
104
            timeline->setTrackProperty(tid, QStringLiteral("kdenlive:thumbs_format"), local_playlist.get("kdenlive:thumbs_format"));
105
            timeline->setTrackProperty(tid, QStringLiteral("kdenlive:audio_rec"), track->get("kdenlive:audio_rec"));
106
107
108
            if (lockState > 0) {
                timeline->setTrackProperty(tid, QStringLiteral("kdenlive:locked_track"), QString::number(lockState));
            }
109
110
111
112
113
114
            break;
        }
        default:
            qDebug() << "ERROR: Unexpected item in the timeline";
        }
    }
115

116
    // Loading compositions
117
    QScopedPointer<Mlt::Service> service(tractor.producer());
118
    QList<Mlt::Transition *> compositions;
Nicolas Carion's avatar
Nicolas Carion committed
119
    while ((service != nullptr) && service->is_valid()) {
120
        if (service->type() == transition_type) {
Nicolas Carion's avatar
Nicolas Carion committed
121
            Mlt::Transition t((mlt_transition)service->get_service());
122
            QString id(t.get("kdenlive_id"));
123
124
            QString internal(t.get("internal_added"));
            if (internal.isEmpty()) {
125
                compositions << new Mlt::Transition(t);
126
                if (id.isEmpty()) {
Nicolas Carion's avatar
Nicolas Carion committed
127
                    qDebug() << "// Warning, this should not happen, transition without id: " << t.get("id") << " = " << t.get("mlt_service");
128
                    t.set("kdenlive_id", t.get("mlt_service"));
129
                }
130
131
132
133
            }
        }
        service.reset(service->producer());
    }
134
    // Sort compositions and insert
135
    bool compositionOk = true;
136
137
138
    if (!compositions.isEmpty()) {
        std::sort(compositions.begin(), compositions.end(), [](Mlt::Transition *a, Mlt::Transition *b) { return a->get_b_track() < b->get_b_track(); });
        while (!compositions.isEmpty()) {
139
            QScopedPointer<Mlt::Transition> t(compositions.takeFirst());
140
            auto transProps = std::make_unique<Mlt::Properties>(t->get_properties());
141
142
            QString id(t->get("kdenlive_id"));
            int compoId;
143
144
            int aTrack = t->get_a_track();
            if (aTrack > tractor.count()) {
Nicolas Carion's avatar
Nicolas Carion committed
145
146
                m_errorMessage << i18n("Invalid composition %1 found on track %2 at %3, compositing with track %4.", t->get("id"), t->get_b_track(),
                                       t->get_in(), t->get_a_track());
147
148
                continue;
            }
149

150
            compositionOk = timeline->requestCompositionInsertion(id, timeline->getTrackIndexFromPosition(t->get_b_track() - 1), t->get_a_track(), t->get_in(),
151
                                                       t->get_length(), std::move(transProps), compoId, undo, redo);
152
            if (!compositionOk) {
153
154
                qDebug() << "ERROR : failed to insert composition in track " << t->get_b_track() << ", position" << t->get_in() << ", ID: " << id
                         << ", MLT ID: " << t->get("id");
155
                //timeline->requestItemDeletion(compoId, false);
156
                m_errorMessage << i18n("Invalid composition %1 found on track %2 at %3.", t->get("id"), t->get_b_track(), t->get_in());
157
                continue;
158
            }
159
            qDebug() << "Inserted composition in track " << t->get_b_track() << ", position" << t->get_in() << "/" << t->get_out();
160
161
        }
    }
162

163
164
    // build internal track compositing
    timeline->buildTrackCompositing();
165
    timeline->updateDuration();
166

167
    if (!ok) {
Nicolas Carion's avatar
Nicolas Carion committed
168
        // TODO log error
169
        // Don't abort loading because of failed composition
170
171
        undo();
        return false;
172
    }
173
    if (!m_errorMessage.isEmpty()) {
174
        KMessageBox::sorry(qApp->activeWindow(), m_errorMessage.join("\n"), i18n("Problems found in your project file"));
175
    }
176
177
178
    return true;
}

179
bool constructTrackFromMelt(const std::shared_ptr<TimelineItemModel> &timeline, int tid, Mlt::Tractor &track,
180
                            const std::unordered_map<QString, QString> &binIdCorresp, Fun &undo, Fun &redo, bool audioTrack)
Nicolas Carion's avatar
Nicolas Carion committed
181
{
182
    if (track.count() != 2) {
Nicolas Carion's avatar
Nicolas Carion committed
183
        // we expect a tractor with two tracks (a "fake" track)
184
185
186
187
188
189
190
191
192
        qDebug() << "ERROR : wrong number of subtracks";
        return false;
    }
    for (int i = 0; i < track.count(); i++) {
        std::unique_ptr<Mlt::Producer> sub_track(track.track(i));
        if (sub_track->type() != playlist_type) {
            qDebug() << "ERROR : SubTracks must be MLT::Playlist";
            return false;
        }
Nicolas Carion's avatar
Nicolas Carion committed
193
        Mlt::Playlist playlist(*sub_track);
194
        constructTrackFromMelt(timeline, tid, playlist, binIdCorresp, undo, redo, audioTrack);
195
196
197
198
        if (i == 0) {
            // Pass track properties
            int height = track.get_int("kdenlive:trackheight");
            timeline->setTrackProperty(tid, "kdenlive:trackheight", height == 0 ? "100" : QString::number(height));
199
            timeline->setTrackProperty(tid, "kdenlive:collapsed", QString::number(track.get_int("kdenlive:collapsed")));
200
201
202
203
            QString trackName = track.get("kdenlive:track_name");
            if (!trackName.isEmpty()) {
                timeline->setTrackProperty(tid, QStringLiteral("kdenlive:track_name"), trackName.toUtf8().constData());
            }
204
            if (audioTrack) {
205
206
                // This is an audio track
                timeline->setTrackProperty(tid, QStringLiteral("kdenlive:audio_track"), QStringLiteral("1"));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
207
208
209
210
                timeline->setTrackProperty(tid, QStringLiteral("hide"), QStringLiteral("1"));
            } else {
                // video track, hide audio
                timeline->setTrackProperty(tid, QStringLiteral("hide"), QStringLiteral("2"));
211
            }
212
213
214
215
            int muteState = playlist.get_int("hide");
            if (muteState > 0 && (!audioTrack || (audioTrack && muteState != 1))) {
                timeline->setTrackProperty(tid, QStringLiteral("hide"), QString::number(muteState));
            }
216
        }
217
    }
218
219
    std::shared_ptr<Mlt::Service> serv = std::make_shared<Mlt::Service>(track.get_service());
    timeline->importTrackEffects(tid, serv);
220
221
222
    return true;
}

223
224
225
namespace {

// This function tries to recover the state of the producer (audio or video or both)
Nicolas Carion's avatar
Nicolas Carion committed
226
PlaylistState::ClipState inferState(const std::shared_ptr<Mlt::Producer> &prod, bool audioTrack)
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
    auto getProperty = [prod](const QString &name) {
        if (prod->parent().is_valid()) {
            return QString::fromUtf8(prod->parent().get(name.toUtf8().constData()));
        }
        return QString::fromUtf8(prod->get(name.toUtf8().constData()));
    };
    auto getIntProperty = [prod](const QString &name) {
        if (prod->parent().is_valid()) {
            return prod->parent().get_int(name.toUtf8().constData());
        }
        return prod->get_int(name.toUtf8().constData());
    };
    QString service = getProperty("mlt_service");
    std::pair<bool, bool> VidAud{true, true};
    VidAud.first = getIntProperty("set.test_image") == 0;
    VidAud.second = getIntProperty("set.test_audio") == 0;
244
    if (audioTrack || ((service.contains(QStringLiteral("avformat")) && getIntProperty(QStringLiteral("video_index")) == -1))) {
245
246
        VidAud.first = false;
    }
247
    if (!audioTrack || ((service.contains(QStringLiteral("avformat")) && getIntProperty(QStringLiteral("audio_index")) == -1))) {
248
249
250
251
252
253
        VidAud.second = false;
    }
    return stateFromBool(VidAud);
}
} // namespace

254
bool constructTrackFromMelt(const std::shared_ptr<TimelineItemModel> &timeline, int tid, Mlt::Playlist &track,
255
                            const std::unordered_map<QString, QString> &binIdCorresp, Fun &undo, Fun &redo, bool audioTrack)
Nicolas Carion's avatar
Nicolas Carion committed
256
257
{
    for (int i = 0; i < track.count(); i++) {
258
259
260
261
262
        if (track.is_blank(i)) {
            continue;
        }
        std::shared_ptr<Mlt::Producer> clip(track.get_clip(i));
        int position = track.clip_start(i);
Nicolas Carion's avatar
Nicolas Carion committed
263
        switch (clip->type()) {
264
265
        case unknown_type:
        case producer_type: {
Nicolas Carion's avatar
Nicolas Carion committed
266
            qDebug() << "Looking for clip clip " << clip->parent().get("kdenlive:id") << " = " << clip->parent().get("kdenlive:clipname");
267
268
269
270
271
            QString binId;
            if (clip->parent().get_int("_kdenlive_processed") == 1) {
                // This is a bin clip, already processed no need to change id
                binId = QString(clip->parent().get("kdenlive:id"));
            } else {
272
273
274
275
                QString clipId = clip->parent().get("kdenlive:id");
                if (clipId.startsWith(QStringLiteral("slowmotion"))) {
                    clipId = clipId.section(QLatin1Char(':'), 1, 1);
                }
276
277
278
                if (clipId.isEmpty()) {
                    clipId = clip->get("kdenlive:id");
                }
279
                Q_ASSERT(!clipId.isEmpty() && binIdCorresp.count(clipId) > 0);
280
                binId = binIdCorresp.at(clipId);
281
282
                clip->parent().set("kdenlive:id", binId.toUtf8().constData());
                clip->parent().set("_kdenlive_processed", 1);
283
            }
284
            bool ok = false;
285
            int cid = -1;
286
            if (pCore->bin()->getBinClip(binId)) {
287
                PlaylistState::ClipState st = inferState(clip, audioTrack);
288
                cid = ClipModel::construct(timeline, binId, clip, st);
289
                ok = timeline->requestClipMove(cid, tid, position, true, false, undo, redo);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
290
            } else {
Nicolas Carion's avatar
Nicolas Carion committed
291
                qDebug() << "// Cannot find bin clip: " << binId << " - " << clip->get("id");
292
            }
293
            if (!ok && cid > -1) {
Nicolas Carion's avatar
Nicolas Carion committed
294
                qDebug() << "ERROR : failed to insert clip in track" << tid << "position" << position;
295
                timeline->requestItemDeletion(cid, false);
296
                m_errorMessage << i18n("Invalid clip %1 found on track %2 at %3.", clip->parent().get("id"), track.get("id"), position);
297
                break;
Nicolas Carion's avatar
Nicolas Carion committed
298
299
300
            }
            qDebug() << "Inserted clip in track" << tid << "at " << position;

301
302
303
            break;
        }
        case tractor_type: {
Nicolas Carion's avatar
Nicolas Carion committed
304
            // TODO This is a nested timeline
305
306
307
308
309
310
311
312
313
            qDebug() << "NOT_IMPLEMENTED: code for parsing nested timeline is not there yet.";
            break;
        }
        default:
            qDebug() << "ERROR : unexpected object found on playlist";
            return false;
            break;
        }
    }
314
315
    std::shared_ptr<Mlt::Service> serv = std::make_shared<Mlt::Service>(track.get_service());
    timeline->importTrackEffects(tid, serv);
316
317
    return true;
}