-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmltcontroller.cpp
More file actions
1910 lines (1772 loc) · 69.7 KB
/
Copy pathmltcontroller.cpp
File metadata and controls
1910 lines (1772 loc) · 69.7 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011-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 "mltcontroller.h"
#include "Logger.h"
#include "controllers/filtercontroller.h"
#include "mainwindow.h"
#include "proxymanager.h"
#include "qmltypes/qmlmetadata.h"
#include "settings.h"
#include "shotcut_mlt_properties.h"
#include "util.h"
#include "videowidget.h"
#include <Mlt.h>
#include <QApplication>
#include <QFileInfo>
#include <QMetaType>
#include <QPalette>
#include <QProcess>
#include <QSaveFile>
#include <QTextStream>
#include <QThreadPool>
#include <QUuid>
#include <QWidget>
#include <QtGlobal>
#include <clocale>
#include <cmath>
#include <unistd.h>
namespace Mlt {
static constexpr int kThumbnailOutSeekFactor = 5;
static Controller *instance = nullptr;
const QString XmlMimeType("application/vnd.mlt+xml");
static constexpr char kMltXmlPropertyName[] = "string";
Controller::Controller()
: m_profile(kDefaultMltProfile)
, m_previewProfile(kDefaultMltProfile)
, m_blockRefresh(false)
{
LOG_DEBUG() << "begin";
if (!qEnvironmentVariableIsSet("MLT_REPOSITORY_DENY")) {
const bool experimental = qApp && qApp->property("experimental").toBool();
if (Settings.safeMode()) {
::qputenv("MLT_REPOSITORY_DENY", "libmltqt:libmltglaxnimate:libmltopenfx");
::qputenv("VST_PATH", "C:/__shotcut_safe_mode_no_vst__");
} else {
::qputenv("MLT_REPOSITORY_DENY", "libmltqt:libmltglaxnimate");
}
}
m_repo = Mlt::Factory::init();
m_processingMode = Settings.processingMode();
resetLocale();
initFiltersClipboard();
updateAvformatCaching(0);
LOG_DEBUG() << "end";
}
Controller &Controller::singleton(QObject *parent)
{
if (!instance) {
qRegisterMetaType<Mlt::Frame>("Mlt::Frame");
qRegisterMetaType<SharedFrame>("SharedFrame");
instance = new VideoWidget(parent);
}
return *instance;
}
Controller::~Controller()
{
LOG_DEBUG() << "begin";
close();
closeConsumer();
LOG_DEBUG() << "end";
}
void Controller::destroy()
{
delete instance;
}
int Controller::setProducer(Mlt::Producer *producer, bool)
{
int error = 0;
if (producer != m_producer.data())
close();
if (producer && producer->is_valid()) {
m_producer.reset(producer);
} else {
// Cleanup on error
error = 1;
delete producer;
}
return error;
}
int Controller::open(const QString &url, const QString &urlToSave, bool skipConvert)
{
int error = checkFile(url);
if (error) {
return error;
}
Mlt::Producer *newProducer = nullptr;
close();
auto myUrl = url;
if (url.endsWith(".mlt")) {
// MLT xml producer does URL decoding; so if the URL contains % it must be encoded.
myUrl = QUrl::toPercentEncoding(url).constData();
}
if (Settings.playerGPU() && !profile().is_explicit())
// Prevent loading normalizing filters, which might be Movit ones that
// may not have a proper OpenGL context when requesting a sample frame.
newProducer = new Mlt::Producer(profile(), "abnormal", myUrl.toUtf8().constData());
else
newProducer = new Mlt::Producer(profile(), myUrl.toUtf8().constData());
if (newProducer && newProducer->is_valid()) {
double fps = profile().fps();
if (!profile().is_explicit()) {
profile().from_producer(*newProducer);
profile().set_width(Util::coerceMultiple(profile().width()));
profile().set_height(Util::coerceMultiple(profile().height()));
}
updatePreviewProfile();
setPreviewScale(Settings.playerPreviewScale());
if (url.endsWith(".mlt")) {
// Load the number of audio channels being used when this project was created.
int channels = newProducer->get_int(kShotcutProjectAudioChannels);
if (!channels)
channels = 2;
m_audioChannels = channels;
// Load the processing mode
QString mode = newProducer->get(kShotcutProjectProcessingMode);
if (!mode.isEmpty()) {
m_processingMode = Settings.processingModeId(mode);
}
}
if (Util::isFpsDifferent(profile().fps(), fps)
|| (Settings.playerGPU() && !profile().is_explicit())) {
// Reload with correct FPS or with Movit normalizing filters attached.
delete newProducer;
newProducer = new Mlt::Producer(profile(), myUrl.toUtf8().constData());
}
if (m_url.isEmpty() && isProjectProducer(newProducer)) {
m_url = urlToSave;
}
Producer *producer = setupNewProducer(newProducer);
producer->set(kShotcutSkipConvertProperty, skipConvert);
delete newProducer;
newProducer = producer;
} else {
delete newProducer;
newProducer = nullptr;
error = 1;
}
m_producer.reset(newProducer);
return error;
}
bool Controller::openXML(const QString &filename)
{
bool error = true;
close();
Mlt::Producer xmlProducer = Util::openMltVirtualClip(filename);
if (xmlProducer.is_valid()) {
setProducer(new Producer(xmlProducer));
error = false;
}
return error;
}
void Controller::close()
{
m_lastSeekedPosition = -1;
if (m_profile.is_explicit()) {
pause();
} else if (m_consumer && !m_consumer->is_stopped()) {
m_consumer->stop();
}
if (isSeekableClip()) {
setSavedProducer(m_producer.data());
}
m_producer.reset();
}
void Controller::closeConsumer()
{
if (m_consumer)
m_consumer->stop();
m_consumer.reset();
m_jackFilter.reset();
}
void Controller::play(double speed)
{
m_lastSeekedPosition = -1;
if (m_jackFilter) {
if (speed == 1.0)
m_jackFilter->fire_event("jack-start");
else
stopJack();
}
if (m_producer)
m_producer->set_speed(speed);
if (m_consumer) {
m_consumer->start();
refreshConsumer(Settings.playerScrubAudio());
}
setVolume(m_volume);
}
bool Controller::isPaused() const
{
return m_producer && qAbs(m_producer->get_speed()) < 0.1;
}
static void fire_jack_seek_event(mlt_properties jack, int position)
{
mlt_events_fire(jack, "jack-seek", mlt_event_data_from_int(position));
}
void Controller::pause(int position)
{
if (m_producer && !isPaused()) {
m_producer->set_speed(0);
if (m_consumer && m_consumer->is_valid()) {
position = position > -1 ? position : m_consumer->position() + 1;
m_producer->seek(position);
m_consumer->purge();
m_consumer->start();
// The following fixes a bug with frame-dropping. It is possible a video frame rendering
// was just dropped. Then, Shotcut does not know the latest position. Next, a filter modifies
// a value, which refreshes the consumer, and the position advances. If that value change
// creates a keyframe, then a subsequent value change creates an additional keyframe one
// (or more?) frames after the previous one.
// https://forum.shotcut.org/t/2-keyframes-created-instead-of-one/11252
if (m_consumer->get_int("real_time") > 0)
refreshConsumer();
}
}
if (m_jackFilter) {
stopJack();
int position = (m_producer && m_producer->is_valid()) ? m_producer->position() : 0;
++m_skipJackEvents;
fire_jack_seek_event(m_jackFilter->get_properties(), position);
}
setVolume(m_volume);
}
void Controller::stop()
{
if (m_consumer && !m_consumer->is_stopped())
m_consumer->stop();
if (m_producer)
m_producer->seek(0);
stopJack();
}
void Controller::on_jack_started(mlt_properties, void *object, mlt_event_data data)
{
if (object)
(static_cast<Controller *>(object))->onJackStarted(Mlt::EventData(data).to_int());
}
void Controller::onJackStarted(int position)
{
if (m_producer) {
m_producer->set_speed(1);
m_producer->seek(position);
Controller::refreshConsumer();
}
}
void Controller::on_jack_stopped(mlt_properties, void *object, mlt_event_data data)
{
if (object)
(static_cast<Controller *>(object))->onJackStopped(EventData(data).to_int());
}
void Controller::onJackStopped(int position)
{
if (m_skipJackEvents) {
--m_skipJackEvents;
} else {
if (m_producer) {
if (!isPaused()) {
Event *event = m_consumer->setup_wait_for("consumer-sdl-paused");
int result = m_producer->set_speed(0);
if (result == 0 && m_consumer->is_valid() && !m_consumer->is_stopped())
m_consumer->wait_for(event);
delete event;
}
m_producer->seek(position);
}
if (m_consumer && m_consumer->get_int("real_time") >= -1)
m_consumer->purge();
refreshConsumer();
}
}
void Controller::stopJack()
{
if (m_jackFilter) {
m_skipJackEvents = 2;
m_jackFilter->fire_event("jack-stop");
}
}
void Controller::initFiltersClipboard()
{
m_filtersClipboard.reset(new Mlt::Producer(profile(), "color", "black"));
if (m_filtersClipboard->is_valid()) {
m_filtersClipboard->set(kShotcutFiltersClipboard, 1);
}
}
bool Controller::enableJack(bool enable)
{
if (!m_consumer)
return true;
if (enable && !m_jackFilter) {
m_jackFilter.reset(new Mlt::Filter(profile(), "jack", "Shotcut player"));
if (m_jackFilter->is_valid()) {
m_jackFilter->set("channels", Settings.playerAudioChannels());
switch (Settings.playerAudioChannels()) {
case 8:
m_jackFilter->set("in_8", "-");
m_jackFilter->set("out_8", "system:playback_8");
Q_FALLTHROUGH();
case 7:
m_jackFilter->set("in_7", "-");
m_jackFilter->set("out_7", "system:playback_7");
Q_FALLTHROUGH();
case 6:
m_jackFilter->set("in_6", "-");
m_jackFilter->set("out_6", "system:playback_6");
Q_FALLTHROUGH();
case 5:
m_jackFilter->set("in_5", "-");
m_jackFilter->set("out_5", "system:playback_5");
Q_FALLTHROUGH();
case 4:
m_jackFilter->set("in_4", "-");
m_jackFilter->set("out_4", "system:playback_4");
Q_FALLTHROUGH();
case 3:
m_jackFilter->set("in_3", "-");
m_jackFilter->set("out_3", "system:playback_3");
Q_FALLTHROUGH();
case 2:
m_jackFilter->set("in_2", "-");
m_jackFilter->set("out_2", "system:playback_2");
Q_FALLTHROUGH();
case 1:
m_jackFilter->set("in_1", "-");
m_jackFilter->set("out_1", "system:playback_1");
Q_FALLTHROUGH();
default:
break;
}
m_consumer->attach(*m_jackFilter);
m_consumer->set("audio_off", 1);
if (isSeekable()) {
m_jackFilter->listen("jack-started",
this,
reinterpret_cast<mlt_listener>(on_jack_started));
m_jackFilter->listen("jack-stopped",
this,
reinterpret_cast<mlt_listener>(on_jack_stopped));
}
} else {
m_jackFilter.reset();
return false;
}
} else if (!enable && m_jackFilter) {
m_consumer->detach(*m_jackFilter);
m_jackFilter.reset();
m_consumer->set("audio_off", 0);
m_consumer->stop();
m_consumer->start();
}
return true;
}
void Controller::setVolume(double volume, bool muteOnPause)
{
m_volume = volume;
// Keep the consumer muted when paused
if (muteOnPause && isPaused()) {
volume = 0.0;
}
if (m_consumer) {
if (m_consumer->get("mlt_service") == QStringLiteral("multi")) {
m_consumer->set("0.volume", volume);
} else {
m_consumer->set("volume", volume);
}
}
}
double Controller::volume() const
{
return m_volume;
}
void Controller::onWindowResize()
{
bool scrub = isPaused() ? false : Settings.playerScrubAudio();
refreshConsumer(scrub);
}
void Controller::seek(int position)
{
setVolume(m_volume, false);
if (m_producer) {
// Always pause before seeking (if not already paused).
if (Settings.playerPauseAfterSeek())
m_producer->set_speed(0);
m_producer->seek(position);
if (m_consumer && m_consumer->is_valid()) {
if (m_consumer->is_stopped()) {
m_consumer->start();
} else {
bool scrubAudio = false;
if (position != m_lastSeekedPosition) {
m_consumer->purge();
scrubAudio = Settings.playerScrubAudio();
}
Controller::refreshConsumer(scrubAudio);
}
}
}
m_lastSeekedPosition = position;
if (m_jackFilter) {
if (Settings.playerPauseAfterSeek())
stopJack();
++m_skipJackEvents;
fire_jack_seek_event(m_jackFilter->get_properties(), position);
}
}
void Controller::refreshConsumer(bool scrubAudio)
{
if (!m_blockRefresh && m_consumer) {
// need to refresh consumer when paused
m_consumer->set("scrub_audio", scrubAudio);
m_consumer->set("refresh", 1);
}
}
bool Controller::saveXML(const QString &filename,
Service *service,
bool withRelativePaths,
QTemporaryFile *tempFile,
bool proxy,
QString projectNote)
{
QMutexLocker locker(&m_saveXmlMutex);
QFileInfo fi(filename);
Consumer c(profile(), "xml", proxy ? filename.toUtf8().constData() : kMltXmlPropertyName);
Service s(service ? service->get_service() : m_producer->get_service());
if (s.is_valid()) {
// The Shotcut rule for paths in MLT XML is forward slashes as created by QFileDialog and QmlFile.
QString root = withRelativePaths ? QDir::fromNativeSeparators(fi.absolutePath()) : "";
s.set(kShotcutProjectAudioChannels, m_audioChannels);
s.set(kShotcutProjectFolder, m_projectFolder.isEmpty() ? 0 : 1);
s.set(kShotcutProjectProcessingMode,
Settings.processingModeStr(Settings.processingMode()).toUtf8().constData());
if (!projectNote.isEmpty()) {
s.set(kShotcutProjectNote, projectNote.toUtf8().constData());
} else {
s.clear(kShotcutProjectNote);
}
int ignore = s.get_int("ignore_points");
if (ignore)
s.set("ignore_points", 0);
c.set("time_format", "clock");
c.set("store", "shotcut");
c.set("root", root.toUtf8().constData());
c.set("no_root", 1);
c.set("title",
QStringLiteral("Shotcut version ").append(SHOTCUT_VERSION).toUtf8().constData());
// Save the consumer of this service so it can be restored.
auto saveConsumer = mlt_service_consumer(s.consumer()->get_service());
c.connect(s);
c.start();
if (ignore)
s.set("ignore_points", ignore);
auto xml = QString::fromUtf8(c.get(kMltXmlPropertyName));
// Restore the consumer that was previously on this service
mlt_service_set_consumer(s.get_service(), saveConsumer);
if (!proxy && ProxyManager::filterXML(xml, root)) { // also verifies
if (tempFile) {
QTextStream stream(tempFile);
stream.setEncoding(QStringConverter::Utf8);
stream << xml;
if (tempFile->error() != QFileDevice::NoError) {
LOG_ERROR() << "error while writing MLT XML file" << tempFile->fileName() << ":"
<< tempFile->errorString();
return false;
}
} else {
QSaveFile file(filename);
file.setDirectWriteFallback(true);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
LOG_ERROR() << "failed to open MLT XML file for writing" << filename;
return false;
}
QTextStream stream(&file);
stream.setEncoding(QStringConverter::Utf8);
stream << xml;
if (file.error() != QFileDevice::NoError) {
LOG_ERROR() << "error while writing MLT XML file" << filename << ":"
<< file.errorString();
return false;
}
return file.commit();
}
}
}
return false;
}
QString Controller::XML(Service *service, bool withProfile, bool withMetadata)
{
Consumer c(profile(), "xml", kMltXmlPropertyName);
Service s(service ? service->get_service()
: (m_producer && m_producer->is_valid()) ? m_producer->get_service()
: nullptr);
if (!s.is_valid())
return QString();
// Save the consumer of this service so it can be restored.
auto saveConsumer = mlt_service_consumer(s.consumer()->get_service());
int ignore = s.get_int("ignore_points");
if (ignore)
s.set("ignore_points", 0);
c.set("time_format", "clock");
if (!withMetadata)
c.set("no_meta", 1);
c.set("no_profile", !withProfile);
c.set("store", "shotcut");
c.set("root", "");
c.connect(s);
c.start();
if (ignore)
s.set("ignore_points", ignore);
// Restore the consumer that was previously on this service
mlt_service_set_consumer(s.get_service(), saveConsumer);
return QString::fromUtf8(c.get(kMltXmlPropertyName));
}
int Controller::consumerChanged()
{
int error = 0;
if (m_consumer) {
bool jackEnabled = !m_jackFilter.isNull();
m_consumer->stop();
m_consumer.reset();
m_jackFilter.reset();
error = reconfigure(false);
if (m_consumer) {
enableJack(jackEnabled);
setVolume(m_volume);
m_consumer->start();
}
}
return error;
}
void Controller::setProfile(const QString &profile_name)
{
LOG_DEBUG() << "setting to profile" << (profile_name.isEmpty() ? "Automatic" : profile_name);
if (!profile_name.isEmpty()) {
Mlt::Profile tmp(profile_name.toUtf8().constData());
m_profile.set_colorspace(tmp.colorspace());
auto gcd = Util::greatestCommonDivisor(tmp.frame_rate_num(), tmp.frame_rate_den());
m_profile.set_frame_rate(tmp.frame_rate_num() / gcd, tmp.frame_rate_den() / gcd);
m_profile.set_height(Util::coerceMultiple(tmp.height()));
m_profile.set_progressive(tmp.progressive());
m_profile.set_sample_aspect(tmp.sample_aspect_num(), tmp.sample_aspect_den());
m_profile.set_display_aspect(tmp.display_aspect_num(), tmp.display_aspect_den());
m_profile.set_width(Util::coerceMultiple(tmp.width()));
m_profile.set_explicit(true);
// Load color_trc from the profile file (custom profiles store it as an extra property).
// For built-in profile names (not file paths), load() will find no such file and
// color_trc will remain empty, which is correct for SDR built-in profiles.
Mlt::Properties profileProps;
profileProps.load(profile_name.toUtf8().constData());
const char *trc = profileProps.get(kShotcutColorTransfer);
m_colorTrc = (trc && *trc) ? QString::fromLatin1(trc) : QString();
} else {
m_colorTrc.clear();
m_profile.set_explicit(false);
if (isClosedClip()) {
// Use a default profile with the dummy hidden color producer.
Mlt::Profile tmp(kDefaultMltProfile);
m_profile.set_colorspace(tmp.colorspace());
m_profile.set_frame_rate(tmp.frame_rate_num(), tmp.frame_rate_den());
m_profile.set_height(Util::coerceMultiple(tmp.height()));
m_profile.set_progressive(tmp.progressive());
m_profile.set_sample_aspect(tmp.sample_aspect_num(), tmp.sample_aspect_den());
m_profile.set_display_aspect(tmp.display_aspect_num(), tmp.display_aspect_den());
m_profile.set_width(Util::coerceMultiple(tmp.width()));
} else {
m_profile.from_producer(*m_producer);
m_profile.set_width(Util::coerceMultiple(m_profile.width()));
}
}
updatePreviewProfile();
}
void Controller::setAudioChannels(int audioChannels)
{
LOG_DEBUG() << audioChannels;
if (audioChannels != m_audioChannels) {
m_audioChannels = audioChannels;
consumerChanged();
}
}
void Controller::setProcessingMode(ShotcutSettings::ProcessingMode mode)
{
if (m_processingMode != mode) {
m_processingMode = mode;
consumerChanged();
}
}
QString Controller::colorTrc() const
{
if (!m_colorTrc.isEmpty())
return m_colorTrc;
// Automatic mode: read the numeric transfer characteristics from the producer's selected
// video stream metadata and map to the string values VideoWidget supports.
// Numeric values are FFmpeg's AVColorTransferCharacteristic enum (same as H.273):
// 16 = SMPTE ST2084 (PQ), 18 = ARIB B67 (HLG).
if (m_producer && m_producer->is_valid()) {
if (m_producer->property_exists(kShotcutColorTransfer)) {
return QString::fromLatin1(m_producer->get(kShotcutColorTransfer));
} else {
const int n = m_producer->get_int("meta.media.nb_streams");
const int videoStreamIndex = m_producer->get_int(kVideoIndexProperty);
int videoCount = 0;
for (int i = 0; i < n; ++i) {
QString typeKey = QStringLiteral("meta.media.%1.stream.type").arg(i);
if (!::qstrcmp(m_producer->get(typeKey.toLatin1().constData()), "video")) {
if (videoCount == videoStreamIndex) {
QString trcKey = QStringLiteral("meta.media.%1.codec.color_trc").arg(i);
const int trc = m_producer->get_int(trcKey.toLatin1().constData());
if (trc == 16)
return QStringLiteral("smpte2084"); // PQ
if (trc == 18)
return QStringLiteral("arib-std-b67"); // HLG
return QString(); // SDR or unsupported TRC
}
++videoCount;
}
}
}
}
return QString();
}
void Controller::setColorTrc(const QString &trc)
{
m_colorTrc = trc;
if (m_producer && m_producer->is_valid())
m_producer->set(kShotcutColorTransfer, trc.toLatin1().constData());
}
QString Controller::resource() const
{
QString resource;
if (!m_producer)
return resource;
resource = QString::fromUtf8(m_producer->get("resource"));
return resource;
}
bool Controller::isSeekable(Producer *p) const
{
bool seekable = false;
Mlt::Producer *producer = p ? p : m_producer.data();
if (producer && producer->is_valid()) {
if (producer->get("force_seekable")) {
seekable = producer->get_int("force_seekable");
} else {
seekable = producer->get_int("seekable");
if (!seekable && producer->get("mlt_type")) {
// MLT xml producer or tractor
seekable = !strcmp(producer->get("mlt_type"), "mlt_producer");
}
if (!seekable) {
// These generators can take an out point to define their length.
// TODO: Currently, these max out at 15000 frames, which is arbitrary.
QString service(producer->get("mlt_service"));
seekable = (service == "color") || service.startsWith("frei0r.")
|| (service == "tone") || (service == "count") || (service == "noise")
|| (service == "consumer");
}
}
}
return seekable;
}
int Controller::maxFrameCount() const
{
return qRound(m_profile.fps() * 7 * 24 * 3600);
}
bool Controller::isLiveProducer(Producer *p) const
{
Mlt::Producer *producer = p ? p : m_producer.data();
if (producer && producer->is_valid()) {
return producer->get_length() > maxFrameCount();
}
return false;
}
bool Controller::isClip() const
{
return producer() && producer()->is_valid() && !isPlaylist() && !isMultitrack();
}
bool Controller::isClosedClip(Producer *producer) const
{
if (!producer)
producer = m_producer.data();
return (!producer || !producer->is_valid()
|| (!qstrcmp(producer->get("mlt_service"), "color")
&& !qstrcmp(producer->get("resource"), "_hide")));
}
bool Controller::isSeekableClip()
{
return isClip() && isSeekable();
}
bool Controller::isPlaylist() const
{
return m_producer && m_producer->is_valid() && !m_producer->get_int(kShotcutVirtualClip)
&& (m_producer->get_int("_original_type") == mlt_service_playlist_type
|| resource() == "<playlist>");
}
bool Controller::isMultitrack() const
{
return m_producer && m_producer->is_valid() && !m_producer->get_int(kShotcutVirtualClip)
&& (m_producer->get_int("_original_type") == mlt_service_tractor_type
|| resource() == "<tractor>")
&& (m_producer->get(kShotcutXmlProperty));
}
bool Controller::isImageProducer(Service *service) const
{
if (service && service->is_valid()) {
QString serviceName = service->get("mlt_service");
return (serviceName == "pixbuf" || serviceName == "qimage");
}
return false;
}
bool Controller::isFileProducer(Service *service) const
{
if (service && service->is_valid()) {
QString serviceName = service->get("mlt_service");
return (serviceName == "pixbuf" || serviceName == "qimage" || serviceName == "glaxnimate"
|| serviceName.startsWith("avformat") || serviceName.startsWith("timewarp"));
}
return false;
}
bool Controller::isProjectProducer(Service *service)
{
return service && service->is_valid() && QString(service->get("xml")) == "was here"
&& (service->get_int("_original_type") != mlt_service_tractor_type
|| service->get(kShotcutXmlProperty));
}
void Controller::rewind(bool forceChangeDirection)
{
if (!m_producer || !m_producer->is_valid())
return;
// Starting the consumer when producer at its end fails. So, first seek to
// frame before last.
if (m_producer->position() >= m_producer->get_length() - 1)
m_producer->seek(m_producer->get_length() - 2);
double speed = m_producer->get_speed();
if (speed == 0.0) {
play(-1.0);
} else {
stopJack();
if (forceChangeDirection && speed > 0.0)
speed = -0.5;
if (speed < 0.0)
m_producer->set_speed(speed * 2.0);
else
m_producer->set_speed(::floor(speed * 0.5));
if (m_consumer && m_consumer->is_valid())
m_consumer->purge();
}
}
void Controller::fastForward(bool forceChangeDirection)
{
if (!m_producer || !m_producer->is_valid())
return;
double speed = m_producer->get_speed();
if (speed == 0.0) {
play(1.0);
} else {
stopJack();
if (forceChangeDirection && speed < 0.0)
speed = 0.5;
if (speed > 0.0)
m_producer->set_speed(speed * 2.0);
else
m_producer->set_speed(::ceil(speed * 0.5));
if (m_consumer && m_consumer->is_valid())
m_consumer->purge();
}
}
void Controller::previous(int currentPosition)
{
if (isMultitrack())
return;
if (currentPosition > m_producer->get_out())
seek(MLT.producer()->get_out());
else if (currentPosition <= m_producer->get_in())
seek(0);
else
seek(m_producer->get_in());
}
void Controller::next(int currentPosition)
{
if (isMultitrack())
return;
if (currentPosition < m_producer->get_in())
seek(m_producer->get_in());
else if (currentPosition >= m_producer->get_out())
seek(m_producer->get_length() - 1);
else
seek(m_producer->get_out());
}
void Controller::setIn(int in)
{
if (m_producer && m_producer->is_valid()) {
int delta = in - m_producer->get_in();
if (!delta) {
return;
}
adjustClipFilters(*m_producer, m_producer->get_in(), m_producer->get_out(), delta, 0, delta);
m_producer->set("in", in);
Controller::refreshConsumer();
}
}
void Controller::setOut(int out)
{
if (m_producer && m_producer->is_valid()) {
int delta = out - m_producer->get_out();
if (!delta) {
return;
}
adjustClipFilters(*m_producer, m_producer->get_in(), m_producer->get_out(), 0, -delta, 0);
m_producer->set("out", out);
Controller::refreshConsumer();
}
}
class FixLengthPropertiesParser : public Mlt::Parser
{
public:
FixLengthPropertiesParser()
: Mlt::Parser()
{}
int on_start_filter(Mlt::Filter *) { return 0; }
int on_start_producer(Mlt::Producer *) { return 0; }
int on_end_producer(Mlt::Producer *s)
{
if (!::strchr(s->get("length"), ':'))
s->set("length", s->frames_to_time(s->get_int("length"), mlt_time_clock));
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 *s)
{
if (!::strchr(s->get("length"), ':'))
s->set("length", s->frames_to_time(s->get_int("length"), mlt_time_clock));
return 0;
}
int on_start_link(Mlt::Link *) { return 0; }
int on_end_link(Mlt::Link *) { return 0; }
};
void Controller::fixLengthProperties(Service &service)
{
FixLengthPropertiesParser parser;
parser.start(service);
}
void Controller::reload(const QString &xml)
{
if (!m_consumer || !m_consumer->is_valid() || !m_producer || !m_producer->is_valid())
return;
const char *position = m_consumer->frames_to_time(m_consumer->position());
double speed = m_producer->get_speed();
QString loadXml = xml;
if (loadXml.isEmpty())
loadXml = XML();
stop();
if (!setProducer(new Mlt::Producer(profile(), "xml-string", loadXml.toUtf8().constData()))) {
if (m_producer && m_producer->is_valid())
m_producer->seek(position);
play(speed);
}
}
void Controller::resetURL()
{
m_url = QString();
}
QImage Controller::image(Mlt::Frame *frame, int width, int height)
{
QImage result;
if (frame && frame->is_valid()) {
if (width > 0 && height > 0) {
frame->set("consumer.rescale", "bilinear");
frame->set("consumer.deinterlacer", "onefield");
frame->set("consumer.top_field_first", -1);
}
mlt_image_format format = mlt_image_rgba;
const uchar *image = frame->get_image(format, width, height);
if (image) {
QImage temp(width, height, QImage::Format_ARGB32);
memcpy(temp.scanLine(0), image, size_t(width * height * 4));
result = temp.rgbSwapped();
}
} else {
result = QImage(width, height, QImage::Format_ARGB32);
result.fill(QColor(Qt::red).rgb());
}
return result;
}
QImage Controller::image(Producer &producer, int frameNumber, int width, int height)
{
QImage result;
if (frameNumber > producer.get_length() - kThumbnailOutSeekFactor) {
producer.seek(frameNumber - kThumbnailOutSeekFactor - 1);
for (int i = 0; i < kThumbnailOutSeekFactor; ++i) {
QScopedPointer<Mlt::Frame> frame(producer.get_frame());
QImage temp = image(frame.data(), width, height);
if (!temp.isNull())
result = temp;
}
} else {