Debian Codegraph @

plugins/flac/encoderflac.cpp

raw
1 /*
2 Copyright (C) 2004 Allan Sandfeld Jensen <[email protected]>
3 Copyright (C) 2005 Benjamin Meyer <ben at meyerhome dot net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21 #include "encoderflac.h"
22
23 #include <FLAC/format.h>
24 #include <FLAC/metadata.h>
25 #include <FLAC/stream_encoder.h>
26
27 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
28 #include "audiocd_flac_encoder.h"
29 #endif
30
31 #include "audiocd_kio_debug.h"
32 #include <KConfig>
33
34 #include <QDateTime>
35 #include <QPair>
36
37 Q_LOGGING_CATEGORY(AUDIOCD_KIO_LOG, "kf.kio.workers.audiocd")
38
39 extern "C" {
40 AUDIOCDPLUGINS_EXPORT void create_audiocd_encoders(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders)
41 {
42 encoders.append(new EncoderFLAC(worker));
43 }
44 }
45
46 class EncoderFLAC::Private
47 {
48 public:
49 FLAC__StreamEncoder *encoder;
50 FLAC__StreamMetadata **metadata;
51 KIO::WorkerBase *ioWorker;
52 unsigned long data;
53 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
54 unsigned compression_level;
55 #endif
56 };
57
58 static FLAC__StreamEncoderWriteStatus WriteCallback(const FLAC__StreamEncoder *encoder,
59 const FLAC__byte buffer[],
60 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
61 unsigned bytes,
62 #else
63 size_t bytes,
64 #endif
65 unsigned samples,
66 unsigned current_frame,
67 void *client_data)
68 {
69 Q_UNUSED(encoder)
70 Q_UNUSED(samples)
71 Q_UNUSED(current_frame)
72
73 auto *d = (EncoderFLAC::Private *)client_data;
74
75 d->data += bytes;
76
77 QByteArray output;
78
79 if (bytes) {
80 output = QByteArray::fromRawData((const char *)buffer, bytes);
81 d->ioWorker->data(output);
82 output.clear();
83 }
84
85 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
86 }
87
88 static void MetadataCallback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
89 {
90 // We do not have seekable writeback so we just discard the updated metadata
91 Q_UNUSED(encoder)
92 Q_UNUSED(metadata)
93 Q_UNUSED(client_data)
94 }
95
96 /*
97 static FLAC__SeekableStreamEncoderSeekStatus SeekCallback(const
98 FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void
99 *client_data)
100 {} ; */
101
102 EncoderFLAC::EncoderFLAC(KIO::WorkerBase *worker)
103 : AudioCDEncoder(worker)
104 {
105 d = new Private();
106 d->ioWorker = worker;
107 d->encoder = nullptr;
108 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
109 d->compression_level = 5;
110 #endif
111 }
112
113 EncoderFLAC::~EncoderFLAC()
114 {
115 if (d->encoder)
116 FLAC__stream_encoder_delete(d->encoder);
117 delete d;
118 }
119
120 QWidget *EncoderFLAC::getConfigureWidget(KConfigSkeleton **manager) const
121 {
122 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
123 Q_UNUSED(manager);
124 return NULL;
125 #else
126 (*manager) = Settings::self();
127 return new EncoderFLACConfig();
128 #endif
129 }
130
131 bool EncoderFLAC::init()
132 {
133 d->encoder = FLAC__stream_encoder_new();
134 d->metadata = nullptr;
135 d->data = 0;
136 return true;
137 }
138
139 void EncoderFLAC::loadSettings()
140 {
141 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT > 7
142 Settings *settings = Settings::self();
143 d->compression_level = settings->flac_compression_level();
144 if (d->compression_level > 8)
145 d->compression_level = 5;
146 #endif
147 }
148
149 // Estimate size to be 5/8 of uncompressed size.
150 unsigned long EncoderFLAC::size(long time_secs) const
151 {
152 long uncompressed = (time_secs * (44100 * 2 * 2));
153 return (uncompressed / 8) * 5 + 1000;
154 }
155
156 long EncoderFLAC::readInit(long size)
157 {
158 qCDebug(AUDIOCD_KIO_LOG) << "EncoderFLAC::readInit() called";
159 d->data = 0;
160 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
161 FLAC__stream_encoder_set_write_callback(d->encoder, WriteCallback);
162 FLAC__stream_encoder_set_metadata_callback(d->encoder, MetadataCallback);
163 FLAC__stream_encoder_set_client_data(d->encoder, d);
164 #endif
165
166 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
167 // The options match approximately those of flac compression-level-5
168 FLAC__stream_encoder_set_do_mid_side_stereo(d->encoder, true);
169 FLAC__stream_encoder_set_max_lpc_order(d->encoder, 8); // flac -l8
170 FLAC__stream_encoder_set_min_residual_partition_order(d->encoder, 3);
171 FLAC__stream_encoder_set_max_residual_partition_order(d->encoder,
172 3); // flac -r3,3
173 #else
174 FLAC__stream_encoder_set_compression_level(d->encoder, d->compression_level);
175 #endif
176
177 FLAC__stream_encoder_set_streamable_subset(d->encoder, true);
178 if (size > 0)
179 FLAC__stream_encoder_set_total_samples_estimate(d->encoder, size / 4);
180
181 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
182 FLAC__stream_encoder_init(d->encoder);
183 #else
184 FLAC__stream_encoder_init_stream(d->encoder, WriteCallback, nullptr, nullptr, MetadataCallback, d);
185 #endif
186 return d->data;
187 }
188
189 long EncoderFLAC::read(qint16 *buf, int frames)
190 {
191 unsigned long olddata = d->data;
192 auto buffer = new FLAC__int32[frames * 2];
193 for (int i = 0; i < frames * 2; i++) {
194 buffer[i] = (FLAC__int32)buf[i];
195 }
196
197 FLAC__stream_encoder_process_interleaved(d->encoder, buffer, frames);
198 delete[] buffer;
199 return d->data - olddata;
200 }
201
202 long EncoderFLAC::readCleanup()
203 {
204 FLAC__stream_encoder_finish(d->encoder);
205 // FLAC__stream_encoder_delete(d->encoder);
206 if (d->metadata) {
207 FLAC__metadata_object_delete(d->metadata[0]);
208 delete[] d->metadata;
209 d->metadata = nullptr;
210 }
211 return 0;
212 }
213
214 void EncoderFLAC::fillSongInfo(KCDDB::CDInfo info, int track, const QString &comment)
215 {
216 d->metadata = new FLAC__StreamMetadata *[1];
217 d->metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
218 // d->metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
219 // d->metadata[2] =
220 // FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE)
221
222 using Comment = QPair<QString, QVariant>;
223 Comment comments[7] = {Comment(QLatin1String("TITLE"), info.track(track - 1).get(Title)),
224 Comment(QLatin1String("ARTIST"), info.track(track - 1).get(Artist)),
225 Comment(QLatin1String("ALBUM"), info.get(Title)),
226 Comment(QLatin1String("GENRE"), info.get(Genre)),
227 Comment(QLatin1String("TRACKNUMBER"), QString::number(track)),
228 Comment(QLatin1String("COMMENT"), comment),
229 Comment(QLatin1String("DATE"), QVariant(QString()))};
230 if (info.get(Year).toInt() > 0) {
231 const QDateTime dt = QDate(info.get(Year).toInt(), 1, 1).startOfDay();
232 comments[6] = Comment(QStringLiteral("DATE"), dt.toString(Qt::ISODate));
233 }
234
235 FLAC__StreamMetadata_VorbisComment_Entry entry;
236 QString field;
237 int num_comments = 0;
238
239 for (int i = 0; i < 7; i++) {
240 if (!comments[i].second.toString().isEmpty()) {
241 field = comments[i].first + QLatin1Char('=') + comments[i].second.toString();
242 const QByteArray cfield = field.toUtf8();
243 entry.entry = (FLAC__byte *)qstrdup(cfield.data());
244 entry.length = cfield.length();
245 // Insert in vorbiscomment and assign ownership of pointers to FLAC
246 FLAC__metadata_object_vorbiscomment_insert_comment(d->metadata[0], num_comments, entry, false);
247 num_comments++;
248 }
249 }
250
251 FLAC__stream_encoder_set_metadata(d->encoder, d->metadata, 1);
252 }