forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotiontrackermodel.cpp
More file actions
385 lines (344 loc) · 12.2 KB
/
Copy pathmotiontrackermodel.cpp
File metadata and controls
385 lines (344 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (c) 2023-2026 Meltytech, LLC
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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 "motiontrackermodel.h"
#include "Logger.h"
#include "mltcontroller.h"
#include "qmltypes/qmlfilter.h"
#include "shotcut_mlt_properties.h"
#include <Mlt.h>
#include <QUuid>
// This is hard-coded for now (minimum viable product).
static const int KEYFRAME_INTERVAL_FRAMES = 5;
class FindTrackersParser : public Mlt::Parser
{
private:
MotionTrackerModel &m_model;
public:
FindTrackersParser(MotionTrackerModel &model)
: Mlt::Parser()
, m_model{model}
{}
int on_start_filter(Mlt::Filter *filter)
{
if (QString::fromUtf8(filter->get("mlt_service")) == "opencv.tracker") {
auto results = QString::fromLatin1(filter->get("results"));
if (!results.isEmpty()) {
auto name = QString::fromUtf8(filter->get(kTrackNameProperty));
if (name.isEmpty()) {
name = m_model.nextName();
filter->set(kTrackNameProperty, name.toUtf8().constData());
}
auto key = m_model.add(name, results);
if (!key.isEmpty()) {
filter->set(kUuidProperty, key.toUtf8().constData());
}
}
}
return 0;
}
int on_start_producer(Mlt::Producer *) { return 0; }
int on_end_producer(Mlt::Producer *) { return 0; }
int on_start_playlist(Mlt::Playlist *) { return 0; }
int on_end_playlist(Mlt::Playlist *) { return 0; }
int on_start_tractor(Mlt::Tractor *) { return 0; }
int on_end_tractor(Mlt::Tractor *) { return 0; }
int on_start_multitrack(Mlt::Multitrack *) { return 0; }
int on_end_multitrack(Mlt::Multitrack *) { return 0; }
int on_start_track() { return 0; }
int on_end_track() { return 0; }
int on_end_filter(Mlt::Filter *) { return 0; }
int on_start_transition(Mlt::Transition *) { return 0; }
int on_end_transition(Mlt::Transition *) { return 0; }
int on_start_chain(Mlt::Chain *) { return 0; }
int on_end_chain(Mlt::Chain *) { return 0; }
int on_start_link(Mlt::Link *) { return 0; }
int on_end_link(Mlt::Link *) { return 0; }
};
/*!
\qmltype MotionTrackerModel
\inqmlmodule org.shotcut.qml
\brief A list model of motion tracker entries attached to the current producer.
\c MotionTrackerModel is available as the \c motionTrackerModel context property
in the Filters panel. Each row represents one stored motion tracker (a named bounding-box
trajectory recorded against the clip).
*/
/*!
\qmlproperty string MotionTrackerModel::nameProperty
\brief The MLT property name used to store the tracker's display name on a filter
(value: \c "shotcut:motionTracker.name"). CONSTANT.
*/
/*!
\qmlproperty string MotionTrackerModel::operationProperty
\brief The MLT property name used to store the tracker's link operation on a filter
(value: \c "shotcut:motionTracker.operation"). CONSTANT.
*/
MotionTrackerModel::MotionTrackerModel(QObject *parent)
: QAbstractListModel(parent)
{
m_data[""] = {""}; // To show nothing in a combo
}
void MotionTrackerModel::load(Mlt::Producer *producer, bool reset)
{
if (!producer)
producer = MLT.producer();
if (reset) {
beginResetModel();
m_data.clear();
beginInsertRows(QModelIndex(), 0, 0);
m_data[""] = {""};
endInsertRows();
}
if (producer && producer->is_valid()) {
FindTrackersParser(*this).start(*producer);
Mlt::Properties retainList((mlt_properties) producer->get_data("xml_retain"));
if (retainList.is_valid()) {
Mlt::Playlist playlist((mlt_playlist) retainList.get_data(kPlaylistTrackId));
if (playlist.is_valid() && playlist.type() == mlt_service_playlist_type) {
FindTrackersParser(*this).start(playlist);
}
}
}
if (reset)
endResetModel();
}
QString MotionTrackerModel::add(const QString &name, const QString &data)
{
auto key = QUuid::createUuid().toString();
if (!m_data.contains(key)) {
auto row = rowCount();
beginInsertRows(QModelIndex(), row, row);
m_data[key] = {name, data, KEYFRAME_INTERVAL_FRAMES};
LOG_DEBUG() << key << m_data[key].name;
endInsertRows();
return key;
}
return QString();
}
void MotionTrackerModel::updateData(const QString &key, const QString &data)
{
auto keys = m_data.keys();
auto row = keys.indexOf(key);
if (row >= 0) {
m_data[key].trackingData = data;
auto i = createIndex(row, 0);
emit dataChanged(i, i, {TrackingDataRole});
}
}
void MotionTrackerModel::remove(const QString &key)
{
if (m_data.contains(key)) {
auto i = m_data.constBegin();
for (int row = 0; i != m_data.constEnd(); ++i, ++row) {
if (i.key() == key) {
beginRemoveRows(QModelIndex(), row, row);
m_data.remove(key);
endInsertRows();
break;
}
}
}
}
void MotionTrackerModel::removeFromService(Mlt::Service *service)
{
// look for an attached filter with mlt_service = opencv.tracker
if (service && service->is_valid()) {
auto producer = Mlt::Producer(*service).parent();
for (int i = 0; i < producer.filter_count(); i++) {
auto filter = producer.filter(i);
if (filter && filter->is_valid()) {
if (QString::fromUtf8(filter->get("mlt_service")) == "opencv.tracker") {
auto key = keyForFilter(filter);
if (!key.isEmpty())
remove(key);
}
}
}
}
}
/*!
\qmlmethod void MotionTrackerModel::setName(Filter filter, string name)
\brief Sets the display \a name for the tracker linked to \a filter.
*/
void MotionTrackerModel::setName(QmlFilter *filter, const QString &name)
{
if (filter && filter->service().is_valid()) {
auto key = keyForFilter(&filter->service());
if (!key.isEmpty() && m_data.contains(key)) {
m_data[key].name = name;
}
}
}
/*!
\qmlmethod string MotionTrackerModel::nextName()
\brief Returns an auto-generated unique name for a new tracker (e.g. \c "Tracker 2").
*/
QString MotionTrackerModel::nextName() const
{
return tr("Tracker %1").arg(rowCount());
}
QString MotionTrackerModel::keyForRow(int row) const
{
QString key;
auto keys = m_data.keys();
if (row >= 0 && row < keys.size()) {
key = keys.at(row);
}
return key;
}
QString MotionTrackerModel::keyForFilter(Mlt::Service *service)
{
QString key;
if (service && service->is_valid())
key = service->get(kUuidProperty);
return key;
}
/*!
\qmlmethod void MotionTrackerModel::reset(Filter filter, string property, int row)
\brief Clears the keyframes for \a property on \a filter that were written by the tracker
at model row \a row.
*/
void MotionTrackerModel::reset(QmlFilter *filter, const QString &property, int row)
{
auto key = keyForRow(row);
if (!key.isEmpty() && filter && filter->service().is_valid() && !property.isEmpty()) {
auto data = trackingData(key);
if (!data.isEmpty()) {
// Use a shotcut property to backup current values
if (filter->get(kBackupProperty).isEmpty()) {
filter->set(kBackupProperty, filter->get(property));
} else {
filter->set(property, filter->get(kBackupProperty));
}
}
}
}
/*!
\qmlmethod list<rect> MotionTrackerModel::trackingData(int row)
\brief Returns the list of bounding-box rectangles recorded by the tracker at \a row,
one entry per keyframe interval frame.
*/
QList<MotionTrackerModel::TrackingItem> MotionTrackerModel::trackingData(const QString &key) const
{
QList<TrackingItem> result;
auto s = m_data.value(key, {}).trackingData;
auto l = s.split(';');
Mlt::Properties props;
auto consumer = MLT.consumer();
for (const auto &i : l) {
auto pair = i.split("~=");
if (pair.size() == 2) {
// The keyframe time is serialized either as a frame number ("5") or
// as a clock value ("00:00:00.167"), depending on the time format
// MLT used when the project was written (animated properties are
// saved as time so they adapt to a frame-rate change). time_to_frames()
// parses both forms to the correct frame using the project frame rate.
int frame = consumer ? consumer->time_to_frames(pair.at(0).toUtf8().constData())
: pair.at(0).toInt();
props.set("", pair.at(1).toLatin1().constData());
auto rect = props.get_rect("");
result << TrackingItem{frame, QRectF(rect.x, rect.y, rect.w, rect.h)};
}
}
return result;
}
QList<QRectF> MotionTrackerModel::trackingData(int row) const
{
auto key = keyForRow(row);
QList<QRectF> result;
if (!key.isEmpty() && m_data.contains(key)) {
for (const auto &a : trackingData(key)) {
result << a.rect;
}
}
return result;
}
/*!
\qmlmethod int MotionTrackerModel::keyframeIntervalFrames(int row)
\brief Returns the keyframe interval in frames for the tracker at \a row.
*/
int MotionTrackerModel::keyframeIntervalFrames(int row) const
{
auto key = keyForRow(row);
if (!key.isEmpty() && m_data.contains(key))
return m_data.value(key).intervalFrames;
return KEYFRAME_INTERVAL_FRAMES;
}
int MotionTrackerModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
return m_data.size();
}
QVariant MotionTrackerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
auto key = keyForRow(index.row());
if (!key.isEmpty()) {
switch (role) {
case Qt::DisplayRole:
return m_data.value(key).name;
case TrackingDataRole:
return m_data.value(key).trackingData;
default:
break;
}
}
return QVariant();
}
bool MotionTrackerModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
auto key = index.data(Qt::UserRole).toString();
if (m_data.contains(key)) {
switch (role) {
case Qt::DisplayRole:
m_data[key].name = value.toString();
emit dataChanged(index, index, {role});
break;
case TrackingDataRole:
m_data[key].trackingData = value.toString();
emit dataChanged(index, index, {role});
break;
default:
break;
}
return true;
}
}
return false;
}
Qt::ItemFlags MotionTrackerModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
/*!
\qmlmethod void MotionTrackerModel::undo(Filter filter, string propertyName)
\brief Static helper that removes motion-tracker–written keyframes for \a propertyName
on \a filter, typically called before re-applying a tracker.
*/
void MotionTrackerModel::undo(QmlFilter *filter, const QString &propertyName)
{
if (filter && !propertyName.isEmpty()) {
filter->set(propertyName, filter->get(kBackupProperty));
filter->resetProperty(kBackupProperty);
}
}