projectclip.cpp 23.9 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 42 43 44
#include <KLocalizedString>



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

65
ProjectClip::ProjectClip(const QDomElement& description, QIcon thumb, ProjectFolder* parent) :
66
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
67
    , m_controller(NULL)
68 69
    , audioFrameCache()
    , m_audioThumbCreated(false)
70
    , m_gpuProducer(NULL)
71
    , m_abortAudioThumb(false)
72 73
{
    Q_ASSERT(description.hasAttribute("id"));
74
    m_clipStatus = StatusWaiting;
75
    m_thumbnail = thumb;
76
    m_temporaryUrl = QUrl::fromLocalFile(getXmlProperty(description, "resource"));
77
    QString clipName = getXmlProperty(description, "kdenlive:clipname");
78 79 80
    if (!clipName.isEmpty()) {
        m_name = clipName;
    }
81 82
    else if (m_temporaryUrl.isValid()) {
        m_name = m_temporaryUrl.fileName();
83
    }
84
    else m_name = i18n("Untitled");
85
    setParent(parent);
86 87 88 89 90
}


ProjectClip::~ProjectClip()
{
91
    // controller is deleted in bincontroller
92 93
}

94 95 96 97 98
QString ProjectClip::getToolTip() const
{
    return url().toLocalFile();
}

99 100 101 102 103 104 105 106 107 108 109 110 111
QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName)
{
    QString value;
    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;
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
void ProjectClip::updateAudioThumbnail(const audioByteArray& data)
{
    ////qDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
    audioFrameCache = data;
    m_audioThumbCreated = true;
    emit gotAudioData();
}

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

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

131 132
ClipType ProjectClip::clipType() const
{
133 134
    if (m_controller == NULL) return Unknown;
    return m_controller->clipType();
135 136 137 138 139 140 141 142 143 144
}

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

145 146 147 148 149
ProjectFolder* ProjectClip::folder(const QString &id)
{
    return NULL;
}

150 151 152 153 154 155 156 157 158 159 160 161
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;
}

162 163 164 165 166 167 168 169
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

170
/*bool ProjectClip::isValid() const
171
{
172 173
    return m_controller->isValid();
}*/
174 175 176

QUrl ProjectClip::url() const
{
177
    if (m_controller) return m_controller->clipUrl();
178
    return m_temporaryUrl;
179 180 181 182
}

bool ProjectClip::hasLimitedDuration() const
{
183 184 185 186
    if (m_controller) {
        return m_controller->hasLimitedDuration();
    }
    return true;
187 188
}

189
GenTime ProjectClip::duration() const
190
{
191 192
    if (m_controller) {
	return m_controller->getPlaytime();
193
    }
194
    return GenTime();
195 196 197 198 199 200 201 202 203 204 205
}

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

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

void ProjectClip::setCurrent(bool current, bool notify)
{
    AbstractProjectItem::setCurrent(current, notify);
220
    if (current && m_controller) {
Jean-Baptiste Mardelle's avatar
cleanup  
Jean-Baptiste Mardelle committed
221
        bin()->openProducer(m_controller);
222
        bin()->editMasterEffect(m_controller);
223 224 225 226 227
    }
}

QDomElement ProjectClip::toXml(QDomDocument& document)
{
228 229 230
    if (m_controller) {
        m_controller->getProducerXML(document);
        return document.documentElement().firstChildElement("producer");
231
    }
232
    return QDomElement();
233 234 235 236
}

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

255
void ProjectClip::setProducer(ClipController *controller, bool replaceProducer)
256
{
257
    if (!replaceProducer && m_controller) {
258 259 260
        qDebug()<<"// RECIEVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
        return;
    }
261 262
    if (m_controller) {
        // Replace clip for this controller
263
        //m_controller->updateProducer(m_id, &(controller->originalProducer()));
264
        delete controller;
265
    }
266 267 268 269 270
    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();
271
        m_temporaryUrl.clear();
272
    }
273
    m_clipStatus = StatusReady;
274 275
    bin()->emitItemUpdated(this);
    getFileHash();
276
    if (KdenliveSettings::audiothumbnails()) QtConcurrent::run(this, &ProjectClip::slotCreateAudioThumbs);
277 278 279 280
}

Mlt::Producer *ProjectClip::producer()
{
281 282
    if (!m_controller) return NULL;
    return m_controller->masterProducer();
283 284
}

285 286 287 288 289
ClipController *ProjectClip::controller()
{
    return m_controller;
}

290
bool ProjectClip::isReady() const
291
{
292
    return m_controller!= NULL;
293 294
}

295
/*void ProjectClip::setZone(const QPoint &zone)
296 297
{
    m_zone = zone;
298
}*/
299 300 301

QPoint ProjectClip::zone() const
{
302 303 304
    int x = getProducerIntProperty("kdenlive:zone_in");
    int y = getProducerIntProperty("kdenlive:zone_out");
    return QPoint(x, y);
305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

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
319 320 321 322 323 324 325 326
void ProjectClip::resetProducerProperty(const QString &name)
{
    if (m_controller) {
        m_controller->resetProperty(name);
    }
}

void ProjectClip::setProducerProperty(const QString &name, int data)
327
{
328 329
    if (m_controller) {
	m_controller->setProperty(name, data);
330 331 332
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
333
void ProjectClip::setProducerProperty(const QString &name, double data)
334
{
335 336
    if (m_controller) {
        m_controller->setProperty(name, data);
337 338 339
    }
}

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
340
void ProjectClip::setProducerProperty(const QString &name, const QString &data)
341
{
342 343
    if (m_controller) {
        m_controller->setProperty(name, data);
344 345 346
    }
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360
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;
}

361 362 363 364 365 366 367 368
QColor ProjectClip::getProducerColorProperty(const QString &key) const
{
    if (m_controller) {
        return m_controller->color_property(key);
    }
    return QColor();
}

369 370 371 372 373 374 375 376
int ProjectClip::getProducerIntProperty(const QString &key) const
{
    int value = 0;
    if (m_controller) {
        value = m_controller->int_property(key);
    }
    return value;
}
377

378
QString ProjectClip::getProducerProperty(const QString &key) const
379 380
{
    QString value;
381 382
    if (m_controller) {
	value = m_controller->property(key);
383 384 385 386 387 388
    }
    return value;
}

const QString ProjectClip::hash()
{
389 390 391 392 393 394
    if (m_controller) {
        QString clipHash = m_controller->property("kdenlive:file_hash");
        if (clipHash.isEmpty()) {
            return getFileHash();
        }
        return clipHash;
395
    }
396
    return getFileHash();
397 398
}

399
const QString ProjectClip::getFileHash() const
400
{
401
    if (clipType() == SlideShow) return QString();
402
    QFile file(m_controller ? m_controller->clipUrl().toLocalFile() : m_temporaryUrl.toLocalFile());
403 404 405 406
    if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
        QByteArray fileData;
        QByteArray fileHash;
        ////qDebug() << "SETTING HASH of" << value;
407
        //m_properties.insert("file_size", QString::number(file.size()));
408 409 410 411 412 413 414 415 416 417 418 419
        /*
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
        if (file.size() > 2000000) {
            fileData = file.read(1000000);
            if (file.seek(file.size() - 1000000))
                fileData.append(file.readAll());
        } else
            fileData = file.readAll();
        file.close();
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
420
        QString result = fileHash.toHex();
421
        if (m_controller) m_controller->setProperty("kdenlive:file_hash", result);
422
        return result;
423
    }
424
    return QString();
425
}
426 427 428 429 430 431

double ProjectClip::getOriginalFps() const
{
    if (!m_controller) return 0;
    return m_controller->originalFps();
}
432 433 434

bool ProjectClip::hasProxy() const
{
435
    QString proxy = getProducerProperty("kdenlive:proxy");
436 437 438 439
    if (proxy.isEmpty() || proxy == "-") return false;
    return true;
}

440
void ProjectClip::setProperties(QMap <QString, QString> properties, bool refreshPanel)
441 442 443 444 445 446 447
{
    QMapIterator<QString, QString> i(properties);
    bool refreshProducer = false;
    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
448
        setProducerProperty(i.key(), i.value());
449 450
        if (clipType() == SlideShow && keys.contains(i.key())) refreshProducer = true;
    }
451 452
    if (properties.contains("kdenlive:proxy")) {
        QString value = properties.value("kdenlive:proxy");
453 454 455 456 457 458
        // 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);
            }
459 460 461
            else {
                reloadProducer();
            }
462 463
        }
        else {
464
            // A proxy was requested, make sure to keep original url
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
465
            setProducerProperty("kdenlive:originalurl", url().toLocalFile());
466
            bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
467 468
        }
    }
469 470
    else if (properties.contains("resource")) {
        // Clip resource changed, update thumbnail
471 472 473 474
        if (clipType() == Color) {
            //reloadProducer(true);
        }
        else {
475
            reloadProducer();
476
        }
477 478
        refreshProducer = true;
    }
479 480 481
    if (properties.contains("xmldata")) {
        refreshProducer = true;
    }
482 483 484 485 486
    if (properties.contains("length")) {
        m_duration = m_controller->getStringDuration();
        bin()->emitItemUpdated(this);
    }

487 488 489 490
    if (properties.contains("kdenlive:clipname")) {
        m_name = properties.value("kdenlive:clipname");
        bin()->emitItemUpdated(this);
    }
491 492 493 494 495 496
    if (refreshPanel) {
        // Some of the clip properties have changed through a command, update properties panel
        emit refreshPropertiesPanel();
    }
    if (refreshProducer) {
        // producer has changed, refresh monitor
497
        bin()->refreshClip(m_id);
498
    }
499 500 501 502 503 504
}

void ProjectClip::setJobStatus(AbstractClipJob::JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
{
    m_jobType = jobType;
    if (progress > 0) {
505
        if (m_jobProgress == progress) return;
506 507 508 509 510 511
	m_jobProgress = progress;
    }
    else {
	m_jobProgress = status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty()) {
	    m_jobMessage = statusMessage;
512
            bin()->emitMessage(statusMessage, OperationCompletedMessage);
513 514 515 516
	}
    }
    bin()->emitItemUpdated(this);
}
517

518 519 520

ClipPropertiesController *ProjectClip::buildProperties(QWidget *parent)
{
521
    ClipPropertiesController *panel = new ClipPropertiesController(bin()->projectTimecode(), m_controller, parent);
522 523 524 525
    connect(this, SIGNAL(refreshPropertiesPanel()), panel, SLOT(slotReloadProperties()));
    return panel;
}

526 527
void ProjectClip::updateParentInfo(const QString &folderid, const QString &foldername)
{
528
    m_controller->setProperty("kdenlive:folderid", folderid);
529 530
}

531 532 533 534 535 536
bool ProjectClip::matches(QString condition)
{
    //TODO
    return true;
}

537
const QString ProjectClip::codec(bool audioCodec) const
538
{
539 540
    if (!m_controller) return QString();
    return m_controller->codec(audioCodec);
541
}
542 543 544 545 546 547 548 549 550 551 552 553 554 555

bool ProjectClip::rename(const QString &name)
{
    if (m_name == name) return false;
    // Rename clip
    QMap <QString, QString> newProperites;
    QMap <QString, QString> oldProperites;
    oldProperites.insert("kdenlive:clipname", m_name);
    newProperites.insert("kdenlive:clipname", name);
    bin()->slotEditClipCommand(m_id, oldProperites, newProperites);
    m_name = name;
    return true;
}

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
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);
584
    return true;
585 586 587 588 589 590 591 592 593 594 595 596 597 598
}

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();
}
599

600
void ProjectClip::addEffect(const ProfileInfo pInfo, QDomElement &effect)
601
{
602
    m_controller->addEffect(pInfo, effect);
603
    bin()->editMasterEffect(m_controller);
604
    bin()->emitItemUpdated(this);
605 606
}

607 608 609 610
void ProjectClip::removeEffect(const ProfileInfo pInfo, int ix)
{
    m_controller->removeEffect(pInfo, ix);
    bin()->editMasterEffect(m_controller);
611 612 613 614 615 616 617
    bin()->emitItemUpdated(this);
}

QVariant ProjectClip::data(DataType type) const
{
    switch (type) {
      case AbstractProjectItem::IconOverlay:
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
618
            return m_controller != NULL ? (m_controller->hasEffects() ? QVariant("kdenlive-track_has_effect") : QVariant()) : QVariant();
619 620 621 622 623
            break;
        default:
	    break;
    }
    return AbstractProjectItem::data(type);
624
}
625 626 627

void ProjectClip::slotExtractImage(QList <int> frames)
{
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
    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 imageWidth = (int)((double) 150 * prod->profile()->width() / prod->profile()->height() + 0.5);
    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);
        if (thumbFolder.exists(hash() + '_' + QString::number(pos) + ".png")) {
            emit thumbReady(pos, QImage(thumbFolder.absoluteFilePath(hash() + '_' + QString::number(pos) + ".png")));
            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()) {
            QImage img = KThumb::getFrame(frame, imageWidth, fullWidth, 150);
            emit thumbReady(frames.at(i), img);
        }
        delete frame;
    }
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

void ProjectClip::slotCreateAudioThumbs()
{
    Mlt::Producer *prod = producer();
    AudioStreamInfo *audioInfo = m_controller->audioInfo();
    if (audioInfo == NULL) return;
    QString clipHash = hash();
    if (clipHash.isEmpty()) return;
    QString audioPath = bin()->projectFolder().path() + "/thumbs/" + clipHash + ".thumb";
    double lengthInFrames = prod->get_playtime();
    int frequency = audioInfo->samplingRate();
    if (frequency <= 0) frequency = 48000;
    int channels = audioInfo->channels();
    if (channels <= 0) channels = 2;
    int arrayWidth = 20;
    double frame = 0.0;
    int maxVolume = 0;
    audioByteArray storeIn;
    QFile f(audioPath);
    if (QFileInfo(audioPath).size() > 0 && f.open(QIODevice::ReadOnly)) {
        bool reading = true;
        const QByteArray channelarray = f.readAll();
        f.close();
        if (channelarray.size() != arrayWidth*(frame + lengthInFrames) * channels) {
            //qDebug() << "--- BROKEN THUMB FOR: " << url.fileName() << " ---------------------- ";
            f.remove();
            reading = false;
        }
        //qDebug() << "reading audio thumbs from file";

        if (reading) {
            int h1 = arrayWidth * channels;
            int h2 = (int) frame * h1;
            for (int z = (int) frame; z < (int)(frame + lengthInFrames) && !m_abortAudioThumb; ++z) {
                int h3 = 0;
                for (int c = 0; c < channels; ++c) {
                    QByteArray audioArray(arrayWidth, '\x00');
                    for (int i = 0; i < arrayWidth; ++i) {
                        audioArray[i] = channelarray.at(h2 + h3 + i);
                        if (audioArray.at(i) > maxVolume) maxVolume = audioArray.at(i);
                    }
                    h3 += arrayWidth;
                    storeIn[z][c] = audioArray;
                }
                h2 += h1;
            }
            if (!m_abortAudioThumb) {
                setProducerProperty("audio_max", QString::number(maxVolume - 64));
                updateAudioThumbnail(storeIn);
            }
            return;
        }
    }
    if (!f.open(QIODevice::WriteOnly)) {
        //qDebug() << "++++++++  ERROR WRITING TO FILE: " << audioPath;
        //qDebug() << "++++++++  DISABLING AUDIO THUMBS";
        KdenliveSettings::setAudiothumbnails(false);
        return;
    }
    QString service = prod->get("mlt_service");
    Mlt::Producer *audioProducer = new Mlt::Producer(*prod->profile(), service.toUtf8().constData(), prod->get("resource"));
    if (!audioProducer->is_valid()) {
        //qDebug() << "++++++++  INVALID CLIP: " << url.path();
        delete audioProducer;
        return;
    }
    audioProducer->set("video_index", "-1");

    //if (KdenliveSettings::normaliseaudiothumbs()) {
        //Mlt::Filter m_convert(prof, "volume");
        //m_convert.set("gain", "normalise");
        //producer.attach(m_convert);
    //}

    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;
    for (int z = (int) frame; z < (int)(frame + lengthInFrames) &&  !m_abortAudioThumb; ++z) {
        int val = (int)((z - frame) / (frame + lengthInFrames) * 100.0);
        if (last_val != val && val > 1) {
            setJobStatus(AbstractClipJob::THUMBJOB, JobWorking, val);
            last_val = val;
        }
        audioProducer->seek(z);
        Mlt::Frame *mlt_frame = audioProducer->get_frame();
        if (mlt_frame && mlt_frame->is_valid()) {
            int samples = mlt_sample_calculator(framesPerSecond, frequency, mlt_frame->get_position());
            qint16* pcm = static_cast<qint16*>(mlt_frame->get_audio(audioFormat, frequency, channels, samples));
            for (int c = 0; c < channels; ++c) {
                QByteArray audioArray;
                audioArray.resize(arrayWidth);
                for (int i = 0; i < audioArray.size(); ++i) {
                    double pcmval = *(pcm + c + i * samples / audioArray.size());
                    if (pcmval >= 0) {
                        pcmval = sqrt(pcmval) / 2.83 + 64;
                        audioArray[i] = pcmval;
                        if (pcmval > maxVolume) maxVolume = pcmval;
                    }
                    else {
                        pcmval = -sqrt(-pcmval) / 2.83 + 64;
                        audioArray[i] = pcmval;
                        if (-pcmval > maxVolume) maxVolume = -pcmval;
                    }
                }
                f.write(audioArray);
                storeIn[z][c] = audioArray;
            }
        } else {
            f.write(QByteArray(arrayWidth, '\x00'));
        }
        delete mlt_frame;
    }
    f.close();
    delete audioProducer;
    setJobStatus(AbstractClipJob::THUMBJOB, JobDone, 0, i18n("Audio thumbnails done"));
    if (m_abortAudioThumb) {
        f.remove();
    } else {
        updateAudioThumbnail(storeIn);
        setProducerProperty("audio_max", QString::number(maxVolume - 64));
    }
}