projectclip.cpp 31.7 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 23 24
/*
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
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 "projectclip.h"
#include "projectfolder.h"
25
#include "projectsubclip.h"
26
#include "bin.h"
27
#include "timecode.h"
28 29
#include "doc/kthumb.h"
#include "kdenlivesettings.h"
30
#include "project/projectcommands.h"
31
#include "mltcontroller/clipcontroller.h"
32
#include "lib/audio/audioStreamInfo.h"
33
#include "mltcontroller/clippropertiescontroller.h"
34 35 36

#include <QDomElement>
#include <QFile>
37
#include <QDir>
38 39
#include <QDebug>
#include <QCryptographicHash>
40
#include <QtConcurrent>
41
#include <KLocalizedString>
42
#include <KMessageBox>
43 44


Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
45
ProjectClip::ProjectClip(const QString &id, QIcon thumb, ClipController *controller, ProjectFolder* parent) :
46 47
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent)
    , audioFrameCache()
Vincent Pinon's avatar
Vincent Pinon committed
48
    , m_controller(controller)
49
    , m_gpuProducer(NULL)
50
    , m_abortAudioThumb(false)
51
{
52
    m_clipStatus = StatusReady;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
53
    m_thumbnail = thumb;
54 55
    m_name = m_controller->clipName();
    m_duration = m_controller->getStringDuration();
56
    m_date = m_controller->date;
57
    m_description = m_controller->description();
58
    m_type = m_controller->clipType();
59
    getFileHash();
60
    setParent(parent);
61
    bin()->loadSubClips(id, m_controller->getPropertiesFromPrefix("kdenlive:clipzone."));
62
    if (KdenliveSettings::audiothumbnails()) {
63
        m_audioThumbsThread = QtConcurrent::run(this, &ProjectClip::slotCreateAudioThumbs);
64
    }
65 66
}

67
ProjectClip::ProjectClip(const QDomElement& description, QIcon thumb, ProjectFolder* parent) :
68
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
69
    , audioFrameCache()
Vincent Pinon's avatar
Vincent Pinon committed
70
    , m_controller(NULL)
71
    , m_gpuProducer(NULL)
72
    , m_abortAudioThumb(false)
73
    , m_type(Unknown)
74 75
{
    Q_ASSERT(description.hasAttribute("id"));
76
    m_clipStatus = StatusWaiting;
77
    m_thumbnail = thumb;
78 79 80
    if (description.hasAttribute("type")) {
        m_type = (ClipType) description.attribute("type").toInt();
    }
81
    m_temporaryUrl = QUrl::fromLocalFile(getXmlProperty(description, "resource"));
82
    QString clipName = getXmlProperty(description, "kdenlive:clipname");
83 84 85
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
86 87
    else if (m_temporaryUrl.isValid()) {
        m_name = m_temporaryUrl.fileName();
88
    }
89
    else m_name = i18n("Untitled");
90
    setParent(parent);
91 92 93 94 95
}


ProjectClip::~ProjectClip()
{
96
    // controller is deleted in bincontroller
97
    abortAudioThumbs();
98 99
}

100 101 102 103 104
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

105
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName, const QString &defaultValue)
106
{
107
    QString value = defaultValue;
108 109 110 111 112 113 114 115 116 117
    QDomNodeList props = producer.elementsByTagName("property");
    for (int i = 0; i < props.count(); ++i) {
        if (props.at(i).toElement().attribute("name") == propertyName) {
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

118
void ProjectClip::updateAudioThumbnail(QVariantList* audioLevels)
119 120
{
    ////qDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
121 122
    audioFrameCache = audioLevels;
    m_controller->audioThumbCreated = true;
123 124 125 126 127 128 129 130 131 132 133
    emit gotAudioData();
}

QList < CommentedTime > ProjectClip::commentedSnapMarkers() const
{
    if (m_controller) return m_controller->commentedSnapMarkers();
    return QList < CommentedTime > ();
}

bool ProjectClip::audioThumbCreated() const
{
134
    return (m_controller && m_controller->audioThumbCreated);
135 136
}

137 138
ClipType ProjectClip::clipType() const
{
139
    return m_type;
140 141 142 143 144 145 146 147 148 149
}

ProjectClip* ProjectClip::clip(const QString &id)
{
    if (id == m_id) {
        return this;
    }
    return NULL;
}

150 151 152 153 154
ProjectFolder* ProjectClip::folder(const QString &id)
{
    return NULL;
}

155 156 157 158 159 160 161 162 163 164 165 166
ProjectSubClip* ProjectClip::getSubClip(int in, int out)
{
    ProjectSubClip *clip;
    for (int i = 0; i < count(); ++i) {
        clip = static_cast<ProjectSubClip *>(at(i))->subClip(in, out);
        if (clip) {
            return clip;
        }
    }
    return NULL;
}

167 168 169 170 171 172 173 174
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

175
/*bool ProjectClip::isValid() const
176
{
177 178
    return m_controller->isValid();
}*/
179 180 181

QUrl ProjectClip::url() const
{
182
    if (m_controller) return m_controller->clipUrl();
183
    return m_temporaryUrl;
184 185 186 187
}

bool ProjectClip::hasLimitedDuration() const
{
188 189 190 191
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
192 193
}

194
GenTime ProjectClip::duration() const
195
{
196 197
    if (m_controller) {
	return m_controller->getPlaytime();
198
    }
199
    return GenTime();
200 201 202 203 204 205 206 207 208 209 210
}

QString ProjectClip::serializeClip()
{
    /*Mlt::Consumer *consumer = bin()->project()->xmlConsumer();
    consumer->connect(*m_baseProducer);
    consumer->run();
    return QString::fromUtf8(consumer->get("kdenlive_clip"));*/
    return QString();
}

211
void ProjectClip::reloadProducer(bool thumbnailOnly)
212 213 214
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
215 216 217 218
    if (thumbnailOnly) {
        // set a special flag to request thumbnail only
        xml.setAttribute("thumbnailOnly", "1");
    }
219
    bin()->reloadProducer(m_id, xml);
220 221 222 223
}

void ProjectClip::setCurrent(bool current, bool notify)
{
224
    //AbstractProjectItem::setCurrent(current, notify);
225
    if (current && m_controller) {
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
226
        bin()->openProducer(m_controller);
227
        bin()->editMasterEffect(m_controller);
228 229 230 231 232
    }
}

QDomElement ProjectClip::toXml(QDomDocument& document)
{
233 234 235
    if (m_controller) {
        m_controller->getProducerXML(document);
        return document.documentElement().firstChildElement("producer");
236
    }
237
    return QDomElement();
238 239 240 241
}

void ProjectClip::setThumbnail(QImage img)
{
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
242 243
    QPixmap thumb = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !thumb.isNull()) {
244
        // Overlay proxy icon
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
245
        QPainter p(&thumb);
246
        QColor c(220, 220, 10, 200);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
247
        QRect r(0, 0, thumb.height() / 2.5, thumb.height() / 2.5);
248 249 250 251 252 253 254 255
        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
256
    m_thumbnail = QIcon(thumb);
257 258 259
    bin()->emitItemUpdated(this);
}

260
void ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
261
{
262
    if (!replaceProducer && m_controller) {
263 264 265
        qDebug()<<"// RECIEVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
        return;
    }
266 267
    if (m_controller) {
        // Replace clip for this controller
268
        //m_controller->updateProducer(m_id, &(controller->originalProducer()));
269
        delete controller;
270
    }
271 272 273 274 275
    else {
        // We did not yet have the controller, update info
        m_controller = controller;
        if (m_name.isEmpty()) m_name = m_controller->clipName();
        m_duration = m_controller->getStringDuration();
276
        m_date = m_controller->date;
277
        m_description = m_controller->description();
278
        m_temporaryUrl.clear();
279
        if (m_type == Unknown) m_type = m_controller->clipType();
280
    }
281
    m_clipStatus = StatusReady;
282 283
    bin()->emitItemUpdated(this);
    getFileHash();
284 285 286 287 288 289 290 291 292 293 294 295 296 297
    createAudioThumbs();
}

void ProjectClip::createAudioThumbs()
{
    if (KdenliveSettings::audiothumbnails() && !m_audioThumbsThread.isRunning()) {
        m_audioThumbsThread = QtConcurrent::run(this, &ProjectClip::slotCreateAudioThumbs);
    }
}

void ProjectClip::abortAudioThumbs()
{
    if (m_audioThumbsThread.isRunning()) {
        m_abortAudioThumb = true;
298
        m_audioThumbsThread.waitForFinished();
299
    }
300 301 302 303
}

Mlt::Producer *ProjectClip::producer()
{
304 305
    if (!m_controller) return NULL;
    return m_controller->masterProducer();
306 307
}

308 309 310 311 312
ClipController *ProjectClip::controller()
{
    return m_controller;
}

313
bool ProjectClip::isReady() const
314
{
315
    return m_controller != NULL && m_clipStatus == StatusReady;
316 317
}

318
/*void ProjectClip::setZone(const QPoint &zone)
319 320
{
    m_zone = zone;
321
}*/
322 323 324

QPoint ProjectClip::zone() const
{
325 326 327
    int x = getProducerIntProperty("kdenlive:zone_in");
    int y = getProducerIntProperty("kdenlive:zone_out");
    return QPoint(x, y);
328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

void ProjectClip::addMarker(int position)
{
    m_markers << position;
    //bin()->markersUpdated(m_id, m_markers);
}

void ProjectClip::removeMarker(int position)
{
    m_markers.removeAll(position);
    //bin()->markersUpdated(m_id, m_markers);
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
342 343 344 345 346 347 348 349
void ProjectClip::resetProducerProperty(const QString &name)
{
    if (m_controller) {
        m_controller->resetProperty(name);
    }
}

void ProjectClip::setProducerProperty(const QString &name, int data)
350
{
351 352
    if (m_controller) {
	m_controller->setProperty(name, data);
353 354 355
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
356
void ProjectClip::setProducerProperty(const QString &name, double data)
357
{
358 359
    if (m_controller) {
        m_controller->setProperty(name, data);
360 361 362
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
363
void ProjectClip::setProducerProperty(const QString &name, const QString &data)
364
{
365 366
    if (m_controller) {
        m_controller->setProperty(name, data);
367 368 369
    }
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383
QMap <QString, QString> ProjectClip::currentProperties(const QMap <QString, QString> &props)
{
    QMap <QString, QString> currentProps;
    if (!m_controller) {
        return currentProps;
    }
    QMap<QString, QString>::const_iterator i = props.constBegin();
    while (i != props.constEnd()) {
        currentProps.insert(i.key(), m_controller->property(i.key()));
        ++i;
    }
    return currentProps;
}

384 385 386 387 388 389 390 391
QColor ProjectClip::getProducerColorProperty(const QString &key) const
{
    if (m_controller) {
        return m_controller->color_property(key);
    }
    return QColor();
}

392 393 394 395 396 397 398 399
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
400

401
QString ProjectClip::getProducerProperty(const QString &key) const
402 403
{
    QString value;
404 405
    if (m_controller) {
	value = m_controller->property(key);
406 407 408 409 410 411
    }
    return value;
}

const QString ProjectClip::hash()
{
412 413
    if (m_controller) {
        QString clipHash = m_controller->property("kdenlive:file_hash");
414 415
        if (!clipHash.isEmpty()) {
            return clipHash;
416
        }
417
    }
418
    return getFileHash();
419 420
}

421
const QString ProjectClip::getFileHash() const
422
{
423 424 425 426 427 428 429 430
    QByteArray fileData;
    QByteArray fileHash;
    switch (m_type) {
      case SlideShow:
          fileData = m_controller ? m_controller->clipUrl().toLocalFile().toUtf8() : m_temporaryUrl.toLocalFile().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
      case Text:
431 432 433
          fileData = m_controller ? m_controller->property("xmldata").toUtf8() : name().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          break;
434
      case Color:
435 436
          fileData = m_controller ? m_controller->property("resource").toUtf8() : name().toUtf8();
          fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
437 438 439 440 441
          break;
      default:
          QFile file(m_controller ? m_controller->clipUrl().toLocalFile() : m_temporaryUrl.toLocalFile());
          if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
              /*
442 443 444
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
            if (file.size() > 2000000) {
                fileData = file.read(1000000);
                if (file.seek(file.size() - 1000000))
                    fileData.append(file.readAll());
            } else
                fileData = file.readAll();
            file.close();
            if (m_controller) m_controller->setProperty("kdenlive:file_size", QString::number(file.size()));
            fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          }
          break;
    }
    if (fileHash.isEmpty()) return QString();
    QString result = fileHash.toHex();
    if (m_controller) {
	m_controller->setProperty("kdenlive:file_hash", result);
461
    }
462
    return result;
463
}
464 465 466 467 468 469

double ProjectClip::getOriginalFps() const
{
    if (!m_controller) return 0;
    return m_controller->originalFps();
}
470 471 472

bool ProjectClip::hasProxy() const
{
473
    QString proxy = getProducerProperty("kdenlive:proxy");
474 475 476 477
    if (proxy.isEmpty() || proxy == "-") return false;
    return true;
}

478
void ProjectClip::setProperties(QMap <QString, QString> properties, bool refreshPanel)
479 480
{
    QMapIterator<QString, QString> i(properties);
481
    QMap <QString, QString> passProperties;
482
    bool refreshProducer = false;
483
    bool refreshAnalysis = false;
484 485 486
    bool reload = false;
    // Some properties also need to be passed to track producers
    QStringList timelineProperties;
487
    timelineProperties << "force_aspect_ratio" << "video_index" << "audio_index" << "set.force_full_luma"<< "full_luma" <<"threads" <<"force_colorspace"<<"force_tff"<<"force_progressive"<<"force_fps";
488 489 490 491
    QStringList keys;
    keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
    while (i.hasNext()) {
        i.next();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
492
        setProducerProperty(i.key(), i.value());
493 494 495
        if (m_type == SlideShow && keys.contains(i.key())) {
            reload = true;
        }
496
        if (i.key().startsWith("kdenlive:clipanalysis")) refreshAnalysis = true;
497 498 499
        if (timelineProperties.contains(i.key())) {
            passProperties.insert(i.key(), i.value());
        }
500
    }
501 502
    if (properties.contains("kdenlive:proxy")) {
        QString value = properties.value("kdenlive:proxy");
503 504 505 506 507 508
        // If value is "-", that means user manually disabled proxy on this clip
        if (value.isEmpty() || value == "-") {
            // reset proxy
            if (bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
            }
509 510 511
            else {
                reloadProducer();
            }
512 513
        }
        else {
514
            // A proxy was requested, make sure to keep original url
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
515
            setProducerProperty("kdenlive:originalurl", url().toLocalFile());
516
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
517 518
        }
    }
519 520
    else if (properties.contains("resource")) {
        // Clip resource changed, update thumbnail
521
        if (m_type != Color) {
522
            reloadProducer();
523
        }
524
        reload = true;
525
    }
526
    if (properties.contains("xmldata") || !passProperties.isEmpty()) {
527 528
        refreshProducer = true;
    }
529
    if (refreshAnalysis) emit refreshAnalysisPanel();
530 531 532 533 534
    if (properties.contains("length")) {
        m_duration = m_controller->getStringDuration();
        bin()->emitItemUpdated(this);
    }

535 536 537 538
    if (properties.contains("kdenlive:clipname")) {
        m_name = properties.value("kdenlive:clipname");
        bin()->emitItemUpdated(this);
    }
539 540 541 542
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
543
    if (refreshProducer || reload) {
544
        // producer has changed, refresh monitor and thumbnail
545
        if (reload) reloadProducer(true);
546
        bin()->refreshClip(m_id);
547
    }
548 549 550
    if (!passProperties.isEmpty()) {
        bin()->updateTimelineProducers(m_id, passProperties);
    }
551 552 553 554 555 556
}

void ProjectClip::setJobStatus(AbstractClipJob::JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
{
    m_jobType = jobType;
    if (progress > 0) {
557
        if (m_jobProgress == progress) return;
558 559 560 561 562 563
	m_jobProgress = progress;
    }
    else {
	m_jobProgress = status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty()) {
	    m_jobMessage = statusMessage;
564
            bin()->emitMessage(statusMessage, OperationCompletedMessage);
565 566 567 568
	}
    }
    bin()->emitItemUpdated(this);
}
569

570 571 572

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
573
    ClipPropertiesController *panel = new ClipPropertiesController(bin()->projectTimecode(), m_controller, parent);
574
    connect(this, SIGNAL(refreshPropertiesPanel()), panel, SLOT(slotReloadProperties()));
575
    connect(this, SIGNAL(refreshAnalysisPanel()), panel, SLOT(slotFillAnalysisData()));
576 577 578
    return panel;
}

579 580
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
581
    m_controller->setProperty("kdenlive:folderid", folderid);
582 583
}

584 585 586 587 588 589
bool ProjectClip::matches(QString condition)
{
    //TODO
    return true;
}

590
const QString ProjectClip::codec(bool audioCodec) const
591
{
592 593
    if (!m_controller) return QString();
    return m_controller->codec(audioCodec);
594
}
595

596
bool ProjectClip::rename(const QString &name, int column)
597 598 599
{
    QMap <QString, QString> newProperites;
    QMap <QString, QString> oldProperites;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
    bool edited = false;
    switch (column) {
      case 0:
        if (m_name == name) return false;
        // Rename clip
        oldProperites.insert("kdenlive:clipname", m_name);
        newProperites.insert("kdenlive:clipname", name);
        m_name = name;
        edited = true;
        break;
      case 2:
        if (m_description == name) return false;
        // Rename clip
        oldProperites.insert("kdenlive:description", m_description);
        newProperites.insert("kdenlive:description", name);
        m_description = name;
        edited = true;
        break;
    }
    if (edited) {
        bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
    }
    return edited;
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
void ProjectClip::addClipMarker(QList <CommentedTime> newMarkers, QUndoCommand *groupCommand)
{
    if (!m_controller) return;
    QList <CommentedTime> oldMarkers;
    for (int i = 0; i < newMarkers.count(); ++i) {
        CommentedTime oldMarker = m_controller->markerAt(newMarkers.at(i).time());
        if (oldMarker == CommentedTime()) {
            oldMarker = newMarkers.at(i);
            oldMarker.setMarkerType(-1);
        }
        oldMarkers << oldMarker;
    }
    (void) new AddMarkerCommand(this, oldMarkers, newMarkers, groupCommand);
}

bool ProjectClip::deleteClipMarkers(QUndoCommand *command)
{
    QList <CommentedTime> markers = commentedSnapMarkers();
    if (markers.isEmpty()) {
        return false;
    }
    QList <CommentedTime> newMarkers;
    for (int i = 0; i < markers.size(); ++i) {
        CommentedTime marker = markers.at(i);
        marker.setMarkerType(-1);
        newMarkers << marker;
    }
    new AddMarkerCommand(this, markers, newMarkers, command);
653
    return true;
654 655 656 657 658 659 660 661 662 663 664 665 666 667
}

void ProjectClip::addMarkers(QList <CommentedTime> &markers)
{
    if (!m_controller) return;
    for (int i = 0; i < markers.count(); ++i) {
      if (markers.at(i).markerType() < 0) m_controller->deleteSnapMarker(markers.at(i).time());
      else m_controller->addSnapMarker(markers.at(i));
    }
    // refresh markers in clip monitor
    bin()->refreshClipMarkers(m_id);
    // refresh markers in timeline clips
    emit refreshClipDisplay();
}
668

669
void ProjectClip::addEffect(const ProfileInfo pInfo, QDomElement &effect)
670
{
671
    m_controller->addEffect(pInfo, effect);
672
    bin()->editMasterEffect(m_controller);
673
    bin()->emitItemUpdated(this);
674 675
}

676 677 678 679
void ProjectClip::removeEffect(const ProfileInfo pInfo, int ix)
{
    m_controller->removeEffect(pInfo, ix);
    bin()->editMasterEffect(m_controller);
680 681 682 683 684 685 686
    bin()->emitItemUpdated(this);
}

QVariant ProjectClip::data(DataType type) const
{
    switch (type) {
      case AbstractProjectItem::IconOverlay:
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
687
            return m_controller != NULL ? (m_controller->hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant()) : QVariant();
688 689 690 691 692
            break;
        default:
	    break;
    }
    return AbstractProjectItem::data(type);
693
}
694 695 696

void ProjectClip::slotExtractImage(QList <int> frames)
{
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    Mlt::Producer *prod = producer();
    if (prod == NULL) return;
    // Check if we are using GPU accel, then we need to use alternate producer
    if (KdenliveSettings::gpu_accel()) {
	if (m_gpuProducer == NULL) {
            QString service = prod->get("mlt_service");
            m_gpuProducer = new Mlt::Producer(*prod->profile(), service.toUtf8().constData(), prod->get("resource"));
            Mlt::Filter scaler(*prod->profile(), "swscale");
	    Mlt::Filter converter(*prod->profile(), "avcolor_space");
	    m_gpuProducer->attach(scaler);
	    m_gpuProducer->attach(converter);
        }
	prod = m_gpuProducer;
    }
    int fullWidth = (int)((double) 150 * prod->profile()->dar() + 0.5);
    QDir thumbFolder(bin()->projectFolder().path() + "/thumbs/");
    for (int i = 0; i < frames.count(); i++) {
        int pos = frames.at(i);
715 716
        if (thumbFolder.exists(hash() + '#' + QString::number(pos) + ".png")) {
            emit thumbReady(pos, QImage(thumbFolder.absoluteFilePath(hash() + '#' + QString::number(pos) + ".png")));
717 718 719 720 721 722 723
            continue;
        }
	int max = prod->get_out();
	if (pos >= max) pos = max - 1;
	prod->seek(pos);
	Mlt::Frame *frame = prod->get_frame();
	if (frame && frame->is_valid()) {
724
            QImage img = KThumb::getFrame(frame, fullWidth, 150);
725 726 727 728
            emit thumbReady(frames.at(i), img);
        }
        delete frame;
    }
729
}
730

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
void ProjectClip::slotExtractSubImage(QList <int> frames)
{
    Mlt::Producer *prod = producer();
    if (prod == NULL) return;
    // Check if we are using GPU accel, then we need to use alternate producer
    if (KdenliveSettings::gpu_accel()) {
        if (m_gpuProducer == NULL) {
            QString service = prod->get("mlt_service");
            m_gpuProducer = new Mlt::Producer(*prod->profile(), service.toUtf8().constData(), prod->get("resource"));
            Mlt::Filter scaler(*prod->profile(), "swscale");
            Mlt::Filter converter(*prod->profile(), "avcolor_space");
            m_gpuProducer->attach(scaler);
            m_gpuProducer->attach(converter);
        }
        prod = m_gpuProducer;
    }
    int fullWidth = (int)((double) 150 * prod->profile()->dar() + 0.5);
    QDir thumbFolder(bin()->projectFolder().path() + "/thumbs/");
    for (int i = 0; i < frames.count(); i++) {
        int pos = frames.at(i);
751 752
        QString path = thumbFolder.absoluteFilePath(hash() + "#" + QString::number(pos) + ".png");
        QImage img(path);
753 754 755 756 757 758 759 760 761 762 763 764
        if (!img.isNull()) {
            ProjectSubClip *clip;
            for (int i = 0; i < count(); ++i) {
                clip = static_cast<ProjectSubClip *>(at(i));
                if (clip && clip->zone().x() == pos) {
                    clip->setThumbnail(img);
                }
            }
            continue;
        }
        int max = prod->get_out();
        if (pos >= max) pos = max - 1;
765
        if (pos < 0) pos = 0;
766 767 768
        prod->seek(pos);
        Mlt::Frame *frame = prod->get_frame();
        if (frame && frame->is_valid()) {
769
            QImage img = KThumb::getFrame(frame, fullWidth, 150);
770
            if (!img.isNull()) {
771
                img.save(path);
772 773 774 775 776 777 778 779 780 781 782 783 784
                ProjectSubClip *clip;
                for (int i = 0; i < count(); ++i) {
                    clip = static_cast<ProjectSubClip *>(at(i));
                    if (clip && clip->zone().x() == pos) {
                        clip->setThumbnail(img);
                    }
                }
            }
        }
        delete frame;
    }
}

785 786 787 788 789 790
int ProjectClip::audioChannels() const
{
    if (!m_controller || !m_controller->audioInfo()) return 0;
    return m_controller->audioInfo()->channels();
}

791 792 793 794 795 796 797
void ProjectClip::slotCreateAudioThumbs()
{
    Mlt::Producer *prod = producer();
    AudioStreamInfo *audioInfo = m_controller->audioInfo();
    if (audioInfo == NULL) return;
    QString clipHash = hash();
    if (clipHash.isEmpty()) return;
798
    QString audioPath = bin()->projectFolder().path() + "/thumbs/" + clipHash + "_audio.png";
799 800 801 802 803 804
    double lengthInFrames = prod->get_playtime();
    int frequency = audioInfo->samplingRate();
    if (frequency <= 0) frequency = 48000;
    int channels = audioInfo->channels();
    if (channels <= 0) channels = 2;
    double frame = 0.0;
805 806 807 808 809 810 811 812 813 814 815
    QVariantList* audioLevels = new QVariantList;
    QImage image(audioPath);
    if (!image.isNull()) {
        // convert cached image
        int n = image.width() * image.height();
        for (int i = 0; i < n; i++) {
            QRgb p = image.pixel(i / 2, i % channels);
            *audioLevels << qRed(p);
            *audioLevels << qGreen(p);
            *audioLevels << qBlue(p);
            *audioLevels << qAlpha(p);
816 817
        }
    }
818 819
    if (audioLevels->size() > 0) {
        updateAudioThumbnail(audioLevels);
820 821 822
        return;
    }
    QString service = prod->get("mlt_service");
823 824 825 826
    if (service == "avformat-novalidate")
        service = "avformat";
    else if (service.startsWith("xml"))
        service = "xml-nogl";
827 828 829 830 831
    Mlt::Producer *audioProducer = new Mlt::Producer(*prod->profile(), service.toUtf8().constData(), prod->get("resource"));
    if (!audioProducer->is_valid()) {
        delete audioProducer;
        return;
    }
832 833 834 835 836 837
    Mlt::Filter chans(*prod->profile(), "audiochannels");
    Mlt::Filter converter(*prod->profile(), "audioconvert");
    Mlt::Filter levels(*prod->profile(), "audiolevel");
    audioProducer->attach(chans);
    audioProducer->attach(converter);
    audioProducer->attach(levels);
838

839
    audioProducer->set("video_index", "-1");
840 841 842 843
    int last_val = 0;
    setJobStatus(AbstractClipJob::THUMBJOB, JobWaiting, 0, i18n("Creating audio thumbnails"));
    double framesPerSecond = audioProducer->get_fps();
    mlt_audio_format audioFormat = mlt_audio_s16;
844 845 846 847 848
    QStringList keys;
    for (int i = 0; i < channels; i++) {
        keys << "meta.media.audio_level." + QString::number(i);
    }
    for (int z = (int) frame; z < (int)(frame + lengthInFrames) && !m_abortAudioThumb; ++z) {
849 850 851 852 853 854
        int val = (int)((z - frame) / (frame + lengthInFrames) * 100.0);
        if (last_val != val && val > 1) {
            setJobStatus(AbstractClipJob::THUMBJOB, JobWorking, val);
            last_val = val;
        }
        Mlt::Frame *mlt_frame = audioProducer->get_frame();
855 856 857 858 859 860
        if (mlt_frame && mlt_frame->is_valid() && !mlt_frame->get_int("test_audio")) {
            int samples = mlt_sample_calculator(framesPerSecond, frequency, z);
            mlt_frame->get_audio(audioFormat, frequency, channels, samples);
            for (int channel = 0; channel < channels; ++channel) {
                double level = 256 * qMin(mlt_frame->get_double(keys.at(channel).toUtf8().constData()) * 0.9, 1.0);
                *audioLevels << level;
861
            }
862 863 864
        } else if (!audioLevels->isEmpty()) {
            for (int channel = 0; channel < channels; channel++)
                *audioLevels << audioLevels->last();
865 866
        }
        delete mlt_frame;
867
        if (m_abortAudioThumb) break;
868
    }
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

    if (!m_abortAudioThumb && audioLevels->size() > 0) {
        // Put into an image for caching.
        int count = audioLevels->size();
        QImage image((count + 3) / 4, channels, QImage::Format_ARGB32);
        int n = image.width() * image.height();
        for (int i = 0; i < n; i ++) {
            QRgb p; 
            if ((4*i + 3) < count) {
                p = qRgba(audioLevels->at(4*i).toInt(), audioLevels->at(4*i+1).toInt(), audioLevels->at(4*i+2).toInt(), audioLevels->at(4*i+3).toInt());
            } else {
                int last = audioLevels->last().toInt();
                int r = (4*i+0) < count? audioLevels->at(4*i+0).toInt() : last;
                int g = (4*i+1) < count? audioLevels->at(4*i+1).toInt() : last;
                int b = (4*i+2) < count? audioLevels->at(4*i+2).toInt() : last;
                int a = last;
                p = qRgba(r, g, b, a);
            }
            image.setPixel(i / 2, i % channels, p);
        }
        image.save(audioPath);
    }
891 892
    delete audioProducer;
    setJobStatus(AbstractClipJob::THUMBJOB, JobDone, 0, i18n("Audio thumbnails done"));
893 894
    if (!m_abortAudioThumb) {
        updateAudioThumbnail(audioLevels);
895
    }
896
    m_abortAudioThumb = false;
897
}
898 899 900 901 902 903 904

bool ProjectClip::isTransparent() const
{
    if (m_type == Text) return true;
    if (m_type == Image && m_controller->int_property("kdenlive:transparency") == 1) return true;
    return false;
}
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

QStringList ProjectClip::updatedAnalysisData(const QString &name, const QString &data, int offset)
{
    if (data.isEmpty()) {
        // Remove data
        return QStringList() << QString("kdenlive:clipanalysis." + name) << QString();
        //m_controller->resetProperty("kdenlive:clipanalysis." + name);
    }
    else {
        QString current = m_controller->property("kdenlive:clipanalysis." + name);
        if (!current.isEmpty()) {
            if (KMessageBox::questionYesNo(QApplication::activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
                // Merge data
                Mlt::Profile *profile = m_controller->profile();
                Mlt::Geometry geometry(current.toUtf8().data(), duration().frames(profile->fps()), profile->width(), profile->height());
                Mlt::Geometry newGeometry(data.toUtf8().data(), duration().frames(profile->fps()), profile->width(), profile->height());
                Mlt::GeometryItem item;
                int pos = 0;
                while (!newGeometry.next_key(&item, pos)) {
                    pos = item.frame();
                    item.frame(pos + offset);
                    pos++;
                    geometry.insert(item);
                }
                return QStringList() << QString("kdenlive:clipanalysis." + name) << geometry.serialise();
                //m_controller->setProperty("kdenlive:clipanalysis." + name, geometry.serialise());
            }
            else {
                // Add data with another name
                int i = 1;
                QString data = m_controller->property("kdenlive:clipanalysis." + name + ' ' + QString::number(i));
                while (!data.isEmpty()) {
                    ++i;
                    data = m_controller->property("kdenlive:clipanalysis." + name + ' ' + QString::number(i));
                }
                return QStringList() << QString("kdenlive:clipanalysis." + name + ' ' + QString::number(i)) << geometryWithOffset(data, offset);
                //m_controller->setProperty("kdenlive:clipanalysis." + name + ' ' + QString::number(i), geometryWithOffset(data, offset));
            }
        }
        else {
            return QStringList() << QString("kdenlive:clipanalysis." + name) << geometryWithOffset(data, offset);
            //m_controller->setProperty("kdenlive:clipanalysis." + name, geometryWithOffset(data, offset));
        }
    }
}

QMap <QString, QString> ProjectClip::analysisData(bool withPrefix)
{
    return m_controller->getPropertiesFromPrefix("kdenlive:clipanalysis.", withPrefix);
}

const QString ProjectClip::geometryWithOffset(const QString &data, int offset)
{
    if (offset == 0) return data;
    Mlt::Profile *profile = m_controller->profile();
    Mlt::Geometry geometry(data.toUtf8().data(), duration().frames(profile->fps()), profile->width(), profile->height());
    Mlt::Geometry newgeometry(NULL, duration().frames(profile->fps()), profile->width(), profile->height());
    Mlt::GeometryItem item;
    int pos = 0;
    while (!geometry.next_key(&item, pos)) {
        pos = item.frame();
        item.frame(pos + offset);
        pos++;
        newgeometry.insert(item);
    }
    return newgeometry.serialise();
}