Debian Codegraph @

plugins/vorbis/encodervorbis.cpp

raw
1 /*
2 Copyright (C) 2000 Rik Hemsley (rikkus) <[email protected]>
3 Copyright (C) 2000, 2001, 2002 Michael Matz <[email protected]>
4 Copyright (C) 2001 Carsten Duvenhorst <[email protected]>
5 Copyright (C) 2001 Adrian Schroeter <[email protected]>
6 Copyright (C) 2003 Richard Lärkäng <[email protected]>
7 Copyright (C) 2003 Scott Wheeler <[email protected]>
8 Copyright (C) 2004, 2005 Benjamin Meyer <ben at meyerhome dot net>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 USA.
24 */
25
26 #include "encodervorbis.h"
27 #include "audiocd_vorbis_encoder.h"
28
29 #include <KConfig>
30 #include <QByteArray>
31 #include <QDateTime>
32 #include <QRandomGenerator>
33 #include <cstdlib>
34 #include <ctime>
35 #include <vorbis/vorbisenc.h>
36
37 extern "C" {
38 AUDIOCDPLUGINS_EXPORT void create_audiocd_encoders(KIO::WorkerBase *worker, QList<AudioCDEncoder *> &encoders)
39 {
40 encoders.append(new EncoderVorbis(worker));
41 }
42 }
43
44 // these are the approx. bitrates for the current 5 Vorbis modes
45 static const int vorbis_nominal_bitrates[] = {128, 160, 192, 256, 350};
46 static const int vorbis_bitrates[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 350};
47
48 class EncoderVorbis::Private
49 {
50 public:
51 ogg_stream_state os; /* take physical pages, weld into a logical stream of packets */
52 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
53 ogg_packet op; /* one raw packet of data for decode */
54
55 vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */
56 vorbis_comment vc; /* struct that stores all the user comments */
57
58 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
59 vorbis_block vb; /* local working space for packet->PCM decode */
60 bool write_vorbis_comments;
61 long vorbis_bitrate_lower;
62 long vorbis_bitrate_upper;
63 long vorbis_bitrate_nominal;
64 int vorbis_encode_method;
65 double vorbis_quality;
66 int vorbis_bitrate;
67 };
68
69 EncoderVorbis::EncoderVorbis(KIO::WorkerBase *worker)
70 : AudioCDEncoder(worker)
71 {
72 d = new Private();
73 }
74
75 EncoderVorbis::~EncoderVorbis()
76 {
77 vorbis_info_clear(&d->vi);
78 vorbis_comment_clear(&d->vc);
79 delete d;
80 }
81
82 QWidget *EncoderVorbis::getConfigureWidget(KConfigSkeleton **manager) const
83 {
84 (*manager) = Settings::self();
85 auto config = new EncoderVorbisConfig();
86 config->kcfg_vorbis_quality->setRange(0.0, 10.0);
87 config->kcfg_vorbis_quality->setSingleStep(0.1);
88 config->vorbis_bitrate_settings->hide();
89 return config;
90 }
91
92 bool EncoderVorbis::init()
93 {
94 vorbis_info_init(&d->vi);
95 vorbis_comment_init(&d->vc);
96
97 vorbis_comment_add_tag(&d->vc, const_cast<char *>("kde-encoder"), const_cast<char *>("kio_audiocd"));
98 return true;
99 }
100
101 void EncoderVorbis::loadSettings()
102 {
103 Settings *settings = Settings::self();
104
105 d->vorbis_encode_method = settings->vorbis_enc_method();
106 d->vorbis_quality = settings->vorbis_quality();
107
108 if (settings->set_vorbis_min_br()) {
109 d->vorbis_bitrate_lower = vorbis_bitrates[settings->vorbis_min_br()] * 1000;
110 } else {
111 d->vorbis_bitrate_lower = -1;
112 }
113
114 if (settings->set_vorbis_max_br()) {
115 d->vorbis_bitrate_upper = vorbis_bitrates[settings->vorbis_max_br()] * 1000;
116 } else {
117 d->vorbis_bitrate_upper = -1;
118 }
119
120 // this is such a hack!
121 if (d->vorbis_bitrate_upper != -1 && d->vorbis_bitrate_lower != -1) {
122 d->vorbis_bitrate = 104000; // empirically determined ...?!
123 } else {
124 d->vorbis_bitrate = 160 * 1000;
125 }
126
127 if (settings->set_vorbis_nominal_br()) {
128 d->vorbis_bitrate_nominal = vorbis_nominal_bitrates[settings->vorbis_nominal_br()] * 1000;
129 d->vorbis_bitrate = d->vorbis_bitrate_nominal;
130 } else {
131 d->vorbis_bitrate_nominal = -1;
132 }
133
134 d->write_vorbis_comments = settings->vorbis_comments();
135
136 // Now that we have read in the settings apply them to the encoder lib
137 switch (d->vorbis_encode_method) {
138 case 0:
139 vorbis_encode_init_vbr(&d->vi, 2, 44100, d->vorbis_quality / 10.0);
140 break;
141 case 1:
142 vorbis_encode_init(&d->vi, 2, 44100, d->vorbis_bitrate_upper, d->vorbis_bitrate_nominal, d->vorbis_bitrate_lower);
143 break;
144 }
145 }
146
147 long EncoderVorbis::flush_vorbis()
148 {
149 long processed(0);
150
151 while (vorbis_analysis_blockout(&d->vd, &d->vb) == 1) {
152 vorbis_analysis(&d->vb, nullptr);
153 /* Non-ancient case. */
154 vorbis_bitrate_addblock(&d->vb);
155
156 while (vorbis_bitrate_flushpacket(&d->vd, &d->op)) {
157 ogg_stream_packetin(&d->os, &d->op);
158 while (int result = ogg_stream_pageout(&d->os, &d->og)) {
159 if (!result)
160 break;
161
162 char *oggheader = reinterpret_cast<char *>(d->og.header);
163 char *oggbody = reinterpret_cast<char *>(d->og.body);
164
165 if (d->og.header_len) {
166 ioWorker->data(QByteArray::fromRawData(oggheader, d->og.header_len));
167 }
168
169 if (d->og.body_len) {
170 ioWorker->data(QByteArray::fromRawData(oggbody, d->og.body_len));
171 }
172 processed += d->og.header_len + d->og.body_len;
173 }
174 }
175 }
176 return processed;
177 }
178
179 unsigned long EncoderVorbis::size(long time_secs) const
180 {
181 long vorbis_size;
182 switch (d->vorbis_encode_method) {
183 case 0: // quality based encoding
184 {
185 // Estimated numbers based on the Vorbis FAQ:
186 // https://xiph.org/vorbis/faq/#quality
187
188 static long vorbis_q_bitrate[] = {60, 74, 86, 106, 120, 152, 183, 207, 239, 309, 440};
189 long quality = static_cast<long>(d->vorbis_quality);
190 if (quality < 0 || quality > 10)
191 quality = 3;
192 vorbis_size = (time_secs * vorbis_q_bitrate[quality] * 1000) / 8;
193
194 break;
195 }
196 default: // bitrate based encoding
197 vorbis_size = (time_secs * d->vorbis_bitrate / 8);
198 break;
199 }
200
201 return vorbis_size;
202 }
203
204 const char *EncoderVorbis::mimeType() const
205 {
206 return "audio/x-vorbis+ogg";
207 }
208
209 long EncoderVorbis::readInit(long /*size*/)
210 {
211 ogg_packet header;
212 ogg_packet header_comm;
213 ogg_packet header_code;
214
215 vorbis_analysis_init(&d->vd, &d->vi);
216 vorbis_block_init(&d->vd, &d->vb);
217
218 ogg_stream_init(&d->os, QRandomGenerator::global()->generate());
219
220 vorbis_analysis_headerout(&d->vd, &d->vc, &header, &header_comm, &header_code);
221
222 ogg_stream_packetin(&d->os, &header);
223 ogg_stream_packetin(&d->os, &header_comm);
224 ogg_stream_packetin(&d->os, &header_code);
225
226 while (int result = ogg_stream_flush(&d->os, &d->og)) {
227 if (!result)
228 break;
229
230 char *oggheader = reinterpret_cast<char *>(d->og.header);
231 char *oggbody = reinterpret_cast<char *>(d->og.body);
232
233 if (d->og.header_len) {
234 ioWorker->data(QByteArray::fromRawData(oggheader, d->og.header_len));
235 }
236
237 if (d->og.body_len) {
238 ioWorker->data(QByteArray::fromRawData(oggbody, d->og.body_len));
239 }
240 }
241 return 0;
242 }
243
244 long EncoderVorbis::read(qint16 *buf, int frames)
245 {
246 int i;
247 float **buffer = vorbis_analysis_buffer(&d->vd, frames);
248
249 /* uninterleave samples */
250 for (i = 0; i < frames; i++) {
251 buffer[0][i] = buf[2 * i] / 32768.0;
252 buffer[1][i] = buf[2 * i + 1] / 32768.0;
253 }
254
255 /* process chunk of data */
256 vorbis_analysis_wrote(&d->vd, i);
257 return flush_vorbis();
258 }
259
260 long EncoderVorbis::readCleanup()
261 {
262 // send end-of-stream and flush the encoder
263 vorbis_analysis_wrote(&d->vd, 0);
264 long processed = flush_vorbis();
265 ogg_stream_clear(&d->os);
266 vorbis_block_clear(&d->vb);
267 vorbis_dsp_clear(&d->vd);
268 vorbis_info_clear(&d->vi);
269 return processed;
270 }
271
272 void EncoderVorbis::fillSongInfo(KCDDB::CDInfo info, int track, const QString &comment)
273 {
274 if (!d->write_vorbis_comments)
275 return;
276
277 using CommentField = QPair<QByteArray, QVariant>;
278 QList<CommentField> commentFields;
279
280 commentFields.append(CommentField("TITLE", info.track(track - 1).get(Title)));
281 commentFields.append(CommentField("ARTIST", info.track(track - 1).get(Artist)));
282 commentFields.append(CommentField("ALBUM", info.get(Title)));
283 commentFields.append(CommentField("GENRE", info.get(Genre)));
284 commentFields.append(CommentField("TRACKNUMBER", QString::number(track)));
285 commentFields.append(CommentField("COMMENT", comment));
286
287 if (info.get(Year).toInt() > 0) {
288 const QDateTime dt = QDate(info.get(Year).toInt(), 1, 1).startOfDay();
289 commentFields.append(CommentField("DATE", QLatin1String(dt.toString(Qt::ISODate).toUtf8().data())));
290 }
291
292 for (QList<CommentField>::iterator it = commentFields.begin(); it != commentFields.end(); ++it) {
293 // if the value is not empty
294 if (!(*it).second.toString().isEmpty()) {
295 char *key = qstrdup((*it).first.constData());
296 char *value = qstrdup((*it).second.toString().toUtf8().data());
297
298 vorbis_comment_add_tag(&d->vc, key, value);
299
300 delete[] key;
301 delete[] value;
302 }
303 }
304 }