diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2019-05-02 12:25:56 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2019-05-02 12:25:56 +0000 |
| commit | 998a80dff446abea1988c613374de6fd8f975dc9 (patch) | |
| tree | f32f29be23d7d2d9bd94faaddcb2912c9ba48c1a /src/plugins | |
| parent | 1850ef8facd583b30c18901b4c3bb0e4e2be4b76 (diff) | |
| download | qmmp-998a80dff446abea1988c613374de6fd8f975dc9.tar.gz qmmp-998a80dff446abea1988c613374de6fd8f975dc9.tar.bz2 qmmp-998a80dff446abea1988c613374de6fd8f975dc9.zip | |
ffmpeg: added embedded cue sheet support
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@8834 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/Input/ffmpeg/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/plugins/Input/ffmpeg/decoder_ffmpegcue.cpp | 209 | ||||
| -rw-r--r-- | src/plugins/Input/ffmpeg/decoder_ffmpegcue.h | 63 | ||||
| -rw-r--r-- | src/plugins/Input/ffmpeg/decoderffmpegfactory.cpp | 80 | ||||
| -rw-r--r-- | src/plugins/Input/ffmpeg/ffmpeg.pro | 6 | ||||
| -rw-r--r-- | src/plugins/Input/wavpack/decoderwavpackfactory.cpp | 2 |
6 files changed, 335 insertions, 27 deletions
diff --git a/src/plugins/Input/ffmpeg/CMakeLists.txt b/src/plugins/Input/ffmpeg/CMakeLists.txt index 50bfa3d2e..e11a0d54c 100644 --- a/src/plugins/Input/ffmpeg/CMakeLists.txt +++ b/src/plugins/Input/ffmpeg/CMakeLists.txt @@ -20,6 +20,7 @@ ADD_DEFINITIONS(${FFMPEG_CFLAGS}) SET(libffmpeg_SRCS decoder_ffmpeg.cpp + decoder_ffmpegcue.cpp decoderffmpegfactory.cpp ffmpegmetadatamodel.cpp settingsdialog.cpp @@ -28,6 +29,7 @@ SET(libffmpeg_SRCS SET(libffmpeg_HDRS decoder_ffmpeg.h + decoder_ffmpegcue.h replaygainreader.h ) diff --git a/src/plugins/Input/ffmpeg/decoder_ffmpegcue.cpp b/src/plugins/Input/ffmpeg/decoder_ffmpegcue.cpp new file mode 100644 index 000000000..204f4af05 --- /dev/null +++ b/src/plugins/Input/ffmpeg/decoder_ffmpegcue.cpp @@ -0,0 +1,209 @@ +/*************************************************************************** + * Copyright (C) 2011-2019 by Ilya Kotov * + * forkotov02@ya.ru * + * * + * 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) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <QObject> +#include <QFile> +#include <QRegExp> +#include "cueparser.h" +#include "decoder_ffmpeg.h" +#include "decoder_ffmpegcue.h" + + +DecoderFFmpegCue::DecoderFFmpegCue(const QString &url) + : Decoder() +{ + m_url = url; +} + +DecoderFFmpegCue::~DecoderFFmpegCue() +{ + if(m_decoder) + delete m_decoder; + m_decoder = nullptr; + if(m_parser) + delete m_parser; + m_parser = nullptr; + if(m_buf) + delete [] m_buf; + m_buf = nullptr; + if(m_input) + m_input->deleteLater(); + m_input = nullptr; +} + +bool DecoderFFmpegCue::initialize() +{ + QString filePath = m_url; + if(!m_url.startsWith("ffmpeg://")) + { + qWarning("DecoderFFmpegCue: invalid url."); + return false; + } + filePath.remove("ffmpeg://"); + filePath.remove(QRegExp("#\\d+$")); + m_track = m_url.section("#", -1).toInt(); + + AVFormatContext *in = nullptr; +#ifdef Q_OS_WIN + if(avformat_open_input(&in, filePath.toUtf8().constData(), nullptr, nullptr) < 0) +#else + if(avformat_open_input(&in, filePath.toLocal8Bit().constData(), nullptr, nullptr) < 0) +#endif + { + qDebug("DecoderFFmpegCue: unable to open file"); + return false; + } + + avformat_find_stream_info(in, nullptr); + AVDictionaryEntry *cuesheet = av_dict_get(in->metadata, "cuesheet", nullptr, 0); + if(!cuesheet) + { + avformat_close_input(&in); + qWarning("DecoderFFmpegCue: unable to find cuesheet comment."); + return false; + } + + m_parser = new CueParser(cuesheet->value); + m_parser->setDuration(in->duration * 1000 / AV_TIME_BASE); + m_parser->setUrl("ffmpeg", filePath); + + if(m_track > m_parser->count() || m_parser->isEmpty()) + { + qWarning("DecoderFFmpegCue: invalid cuesheet"); + return false; + } + m_input = new QFile(filePath); + if(!m_input->open(QIODevice::ReadOnly)) + { + qWarning("DecoderFFmpegCue:: %s", qPrintable(m_input->errorString())); + return false; + } + QMap<Qmmp::MetaData, QString> metaData = m_parser->info(m_track)->metaData(); + addMetaData(metaData); //send metadata + + m_duration = m_parser->duration(m_track); + m_offset = m_parser->offset(m_track); + + m_decoder = new DecoderFFmpeg(filePath, m_input); + if(!m_decoder->initialize()) + { + qWarning("DecoderFFapCUE: invalid audio file"); + return false; + } + m_decoder->seek(m_offset); + + configure(m_decoder->audioParameters()); + + m_trackSize = audioParameters().sampleRate() * audioParameters().channels() * + audioParameters().sampleSize() * m_duration / 1000; + m_written = 0; + + m_frameSize = audioParameters().sampleSize() * audioParameters().channels(); + + setReplayGainInfo(m_parser->info(m_track)->replayGainInfo()); //send ReplayGaing info + addMetaData(m_parser->info(m_track)->metaData()); //send metadata + return true; +} + +qint64 DecoderFFmpegCue::totalTime() const +{ + return m_decoder ? m_duration : 0; +} + +void DecoderFFmpegCue::seek(qint64 pos) +{ + m_decoder->seek(m_offset + pos); + m_written = audioParameters().sampleRate() * + audioParameters().channels() * + audioParameters().sampleSize() * pos/1000; +} + +qint64 DecoderFFmpegCue::read(unsigned char *data, qint64 size) +{ + if(m_trackSize - m_written < m_frameSize) //end of cue track + return 0; + + qint64 len = 0; + + if(m_buf) //read remaining data first + { + len = qMin(m_bufSize, size); + memmove(data, m_buf, len); + if(size >= m_bufSize) + { + delete[] m_buf; + m_buf = nullptr; + m_bufSize = 0; + } + else + memmove(m_buf, m_buf + len, size - len); + } + else + len = m_decoder->read(data, size); + + if(len <= 0) //end of file + return 0; + + if(len + m_written <= m_trackSize) + { + m_written += len; + return len; + } + + qint64 len2 = qMax(qint64(0), m_trackSize - m_written); + len2 = (len2 / m_frameSize) * m_frameSize; //integer number of samples + m_written += len2; + //save data of the next track + if(m_buf) + delete[] m_buf; + m_bufSize = len - len2; + m_buf = new char[m_bufSize]; + memmove(m_buf, data + len2, m_bufSize); + return len2; +} + +int DecoderFFmpegCue::bitrate() const +{ + return m_decoder->bitrate(); +} + +const QString DecoderFFmpegCue::nextURL() const +{ + if(m_track +1 <= m_parser->count()) + return m_parser->url(m_track + 1); + else + return QString(); +} + +void DecoderFFmpegCue::next() +{ + if(m_track +1 <= m_parser->count()) + { + m_track++; + m_duration = m_parser->duration(m_track); + m_offset = m_parser->offset(m_track); + m_trackSize = audioParameters().sampleRate() * + audioParameters().channels() * + audioParameters().sampleSize() * m_duration/1000; + addMetaData(m_parser->info(m_track)->metaData()); + setReplayGainInfo(m_parser->info(m_track)->replayGainInfo()); + m_written = 0; + } +} diff --git a/src/plugins/Input/ffmpeg/decoder_ffmpegcue.h b/src/plugins/Input/ffmpeg/decoder_ffmpegcue.h new file mode 100644 index 000000000..97cdc9dbd --- /dev/null +++ b/src/plugins/Input/ffmpeg/decoder_ffmpegcue.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2011-2019 by Ilya Kotov * + * forkotov02@ya.ru * + * * + * 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) 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef DECODER_FFMPEGCUE_H +#define DECODER_FFMPEGCUE_H + +#include <qmmp/decoder.h> + +class Output; +class QIDevice; +class CueParser; + +/** + @author Ilya Kotov <forkotov02@ya.ru> +*/ +class DecoderFFmpegCue : public Decoder +{ +public: + DecoderFFmpegCue(const QString &url); + virtual ~DecoderFFmpegCue(); + + // Standard Decoder API + bool initialize() override; + qint64 totalTime() const override; + void seek(qint64) override; + qint64 read(unsigned char *data, qint64 size) override; + int bitrate() const override; + const QString nextURL() const override; + void next() override; + +private: + Decoder *m_decoder = nullptr; + CueParser *m_parser = nullptr; + char *m_buf = nullptr; //buffer for remainig data + int m_track = 0; + qint64 m_duration = 0; + qint64 m_offset = 0; + qint64 m_trackSize = 0; + qint64 m_written = 0; + QString m_url; + qint64 m_bufSize = 0; + qint64 m_frameSize = 0; //sample size + QIODevice *m_input = nullptr; +}; + +#endif // DECODER_FFMPEGCUE_H diff --git a/src/plugins/Input/ffmpeg/decoderffmpegfactory.cpp b/src/plugins/Input/ffmpeg/decoderffmpegfactory.cpp index fe05461b0..f22e0cfe1 100644 --- a/src/plugins/Input/ffmpeg/decoderffmpegfactory.cpp +++ b/src/plugins/Input/ffmpeg/decoderffmpegfactory.cpp @@ -20,6 +20,8 @@ #include <QSettings> #include <QMessageBox> +#include <QFileInfo> +#include <qmmp/cueparser.h> extern "C"{ #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> @@ -30,6 +32,7 @@ extern "C"{ #include "ffmpegmetadatamodel.h" #include "settingsdialog.h" #include "decoder_ffmpeg.h" +#include "decoder_ffmpegcue.h" #include "decoderffmpegfactory.h" @@ -149,18 +152,33 @@ DecoderProperties DecoderFFmpegFactory::properties() const properties.hasAbout = true; properties.hasSettings = true; properties.noInput = false; + properties.protocols << "ffmpeg"; properties.priority = 10; return properties; } Decoder *DecoderFFmpegFactory::create(const QString &path, QIODevice *input) { - return new DecoderFFmpeg(path, input); + if(path.contains("://")) + return new DecoderFFmpegCue(path); + else + return new DecoderFFmpeg(path, input); } QList<TrackInfo *> DecoderFFmpegFactory::createPlayList(const QString &path, TrackInfo::Parts parts, QStringList *) { - TrackInfo *info = new TrackInfo(path); + int track = -1; //cue track + QString filePath = path; + + if(path.contains("://")) //is it cue track? + { + filePath.remove("ffmpeg://"); + filePath.remove(QRegExp("#\\d+$")); + track = path.section("#", -1).toInt(); + parts = TrackInfo::AllParts; //extract all metadata for single cue track + } + + TrackInfo *info = new TrackInfo(filePath); if(parts == TrackInfo::NoParts) return QList<TrackInfo*>() << info; @@ -168,9 +186,9 @@ QList<TrackInfo *> DecoderFFmpegFactory::createPlayList(const QString &path, Tra AVFormatContext *in = nullptr; #ifdef Q_OS_WIN - if(avformat_open_input(&in, path.toUtf8().constData(), nullptr, nullptr) < 0) + if(avformat_open_input(&in, filePath.toUtf8().constData(), nullptr, nullptr) < 0) #else - if(avformat_open_input(&in, path.toLocal8Bit().constData(), nullptr, nullptr) < 0) + if(avformat_open_input(&in, filePath.toLocal8Bit().constData(), nullptr, nullptr) < 0) #endif { qDebug("DecoderFFmpegFactory: unable to open file"); @@ -180,8 +198,42 @@ QList<TrackInfo *> DecoderFFmpegFactory::createPlayList(const QString &path, Tra avformat_find_stream_info(in, nullptr); + if(parts & TrackInfo::Properties) + { + int idx = av_find_best_stream(in, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0); + if(idx >= 0) + { + #if (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,48,0)) //ffmpeg-3.1: 57.48.101 + AVCodecParameters *c = in->streams[idx]->codecpar; + #else + AVCodecContext *c = in->streams[idx]->codec; + #endif + info->setValue(Qmmp::BITRATE, int(c->bit_rate) / 1000); + info->setValue(Qmmp::SAMPLERATE, c->sample_rate); + info->setValue(Qmmp::CHANNELS, c->channels); + info->setValue(Qmmp::BITS_PER_SAMPLE, c->bits_per_raw_sample); + + //info->setValue(Qmmp::FORMAT_NAME, QString::fromLatin1(avcodec_get_name(c->codec_id))); + info->setValue(Qmmp::FILE_SIZE, QFileInfo(filePath).size()); //adds file size for cue tracks + info->setDuration(in->duration * 1000 / AV_TIME_BASE); + } + } + if(parts & TrackInfo::MetaData) { + AVDictionaryEntry *cuesheet = av_dict_get(in->metadata,"cuesheet",nullptr,0); + if(cuesheet) + { + CueParser parser(cuesheet->value); + parser.setDuration(info->duration()); + parser.setProperties(info->properties()); + parser.setUrl("ffmpeg", filePath); + + avformat_close_input(&in); + delete info; + return (track > 0) ? parser.createPlayList(track) : parser.createPlayList(); + } + AVDictionaryEntry *album = av_dict_get(in->metadata,"album",nullptr,0); if(!album) album = av_dict_get(in->metadata,"WM/AlbumTitle",nullptr,0); @@ -218,26 +270,6 @@ QList<TrackInfo *> DecoderFFmpegFactory::createPlayList(const QString &path, Tra info->setValue(Qmmp::TRACK, track->value); } - if(parts & TrackInfo::Properties) - { - int idx = av_find_best_stream(in, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0); - if(idx >= 0) - { - #if (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,48,0)) //ffmpeg-3.1: 57.48.101 - AVCodecParameters *c = in->streams[idx]->codecpar; - #else - AVCodecContext *c = in->streams[idx]->codec; - #endif - info->setValue(Qmmp::BITRATE, int(c->bit_rate) / 1000); - info->setValue(Qmmp::SAMPLERATE, c->sample_rate); - info->setValue(Qmmp::CHANNELS, c->channels); - info->setValue(Qmmp::BITS_PER_SAMPLE, c->bits_per_raw_sample); - - //info->setValue(Qmmp::FORMAT_NAME, QString::fromLatin1(avcodec_get_name(c->codec_id))); - info->setDuration(in->duration * 1000 / AV_TIME_BASE); - } - } - avformat_close_input(&in); return QList<TrackInfo*>() << info; } diff --git a/src/plugins/Input/ffmpeg/ffmpeg.pro b/src/plugins/Input/ffmpeg/ffmpeg.pro index 6167c1eaf..8efc547db 100644 --- a/src/plugins/Input/ffmpeg/ffmpeg.pro +++ b/src/plugins/Input/ffmpeg/ffmpeg.pro @@ -6,13 +6,15 @@ HEADERS += decoderffmpegfactory.h \ decoder_ffmpeg.h \ settingsdialog.h \ ffmpegmetadatamodel.h \ - replaygainreader.h + replaygainreader.h \ + decoder_ffmpegcue.h SOURCES += decoder_ffmpeg.cpp \ decoderffmpegfactory.cpp \ settingsdialog.cpp \ ffmpegmetadatamodel.cpp \ - replaygainreader.cpp + replaygainreader.cpp \ + decoder_ffmpegcue.cpp FORMS += settingsdialog.ui diff --git a/src/plugins/Input/wavpack/decoderwavpackfactory.cpp b/src/plugins/Input/wavpack/decoderwavpackfactory.cpp index 3b6944bcf..afa41f058 100644 --- a/src/plugins/Input/wavpack/decoderwavpackfactory.cpp +++ b/src/plugins/Input/wavpack/decoderwavpackfactory.cpp @@ -122,7 +122,7 @@ QList<TrackInfo *> DecoderWavPackFactory::createPlayList(const QString &path, Tr WavpackGetTagItem(ctx, "cuesheet", value, cue_len + 1); CueParser parser(value); - parser.setDuration((qint64)WavpackGetNumSamples(ctx) * 1000 / WavpackGetSampleRate(ctx)); + parser.setDuration(info->duration()); parser.setProperties(info->properties()); parser.setUrl("wvpack", filePath); |
