1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
Copyright (C) 2000 Rik Hemsley (rikkus) <[email protected]>
Copyright (C) 2000, 2001, 2002 Michael Matz <[email protected]>
Copyright (C) 2001 Carsten Duvenhorst <[email protected]>
Copyright (C) 2001 Adrian Schroeter <[email protected]>
Copyright (C) 2003 Richard Lärkäng <[email protected]>
Copyright (C) 2003 Scott Wheeler <[email protected]>
Copyright (C) 2004, 2005 Benjamin Meyer <ben at meyerhome dot net>
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 "encodervorbis.h"
#include "audiocd_vorbis_encoder.h"
#include "encodervorbisconfig.h"
#ifdef HAVE_VORBIS
#include <vorbis/vorbisenc.h>
#include <time.h>
#include <stdlib.h>
#include <tdeconfig.h>
#include <knuminput.h>
#include <tqgroupbox.h>
#include <tdeglobal.h>
#include <tdelocale.h>
extern "C"
{
KDE_EXPORT void create_audiocd_encoders(TDEIO::SlaveBase *slave, TQPtrList<AudioCDEncoder> &encoders)
{
encoders.append(new EncoderVorbis(slave));
}
}
// these are the approx. bitrates for the current 5 Vorbis modes
static int vorbis_nominal_bitrates[] = { 128, 160, 192, 256, 350 };
static int vorbis_bitrates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 350 };
class EncoderVorbis::Private {
public:
ogg_stream_state os; /* take physical pages, weld into a logical stream of packets */
ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
ogg_packet op; /* one raw packet of data for decode */
vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */
vorbis_comment vc; /* struct that stores all the user comments */
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
vorbis_block vb; /* local working space for packet->PCM decode */
bool write_vorbis_comments;
long vorbis_bitrate_lower;
long vorbis_bitrate_upper;
long vorbis_bitrate_nominal;
int vorbis_encode_method;
double vorbis_quality;
int vorbis_bitrate;
};
EncoderVorbis::EncoderVorbis(TDEIO::SlaveBase *slave) : AudioCDEncoder(slave) {
d = new Private();
}
EncoderVorbis::~EncoderVorbis(){
delete d;
}
TQWidget* EncoderVorbis::getConfigureWidget(TDEConfigSkeleton** manager) const {
(*manager) = Settings::self();
TDEGlobal::locale()->insertCatalogue("audiocd_encoder_vorbis");
EncoderVorbisConfig *config = new EncoderVorbisConfig();
config->kcfg_vorbis_quality->setRange(0.0, 10.0, 0.2, true);
config->vorbis_bitrate_settings->hide();
return config;
}
bool EncoderVorbis::init(){
vorbis_info_init(&d->vi);
vorbis_comment_init(&d->vc);
vorbis_comment_add_tag
(
&d->vc,
const_cast<char *>("kde-encoder"),
const_cast<char *>("tdeio_audiocd")
);
return true;
}
void EncoderVorbis::loadSettings(){
Settings *settings = Settings::self();
d->vorbis_encode_method = settings->vorbis_enc_method();
d->vorbis_quality = settings->vorbis_quality();
if ( settings->set_vorbis_min_br()) {
d->vorbis_bitrate_lower = vorbis_bitrates[settings->vorbis_min_br()] * 1000;
} else {
d->vorbis_bitrate_lower = -1;
}
if ( settings->set_vorbis_max_br() ) {
d->vorbis_bitrate_upper = vorbis_bitrates[settings->vorbis_max_br()] * 1000;
} else {
d->vorbis_bitrate_upper = -1;
}
// this is such a hack!
if ( d->vorbis_bitrate_upper != -1 && d->vorbis_bitrate_lower != -1 ) {
d->vorbis_bitrate = 104000; // empirically determined ...?!
} else {
d->vorbis_bitrate = 160 * 1000;
}
if ( settings->set_vorbis_nominal_br() ) {
d->vorbis_bitrate_nominal = vorbis_nominal_bitrates[settings->vorbis_nominal_br()] * 1000;
d->vorbis_bitrate = d->vorbis_bitrate_nominal;
} else {
d->vorbis_bitrate_nominal = -1;
}
d->write_vorbis_comments = settings->vorbis_comments();
// Now that we have read in the settings apply them to the encoder lib
switch (d->vorbis_encode_method) {
case 0:
/* Support very old libvorbis by simply falling through. */
#if HAVE_VORBIS >= 2
vorbis_encode_init_vbr(&d->vi, 2, 44100, d->vorbis_quality/10.0);
break;
#endif
case 1:
vorbis_encode_init(&d->vi, 2, 44100, d->vorbis_bitrate_upper, d->vorbis_bitrate_nominal, d->vorbis_bitrate_lower);
break;
}
}
long EncoderVorbis::flush_vorbis(void) {
long processed(0);
while(vorbis_analysis_blockout(&d->vd,&d->vb)==1) {
/* Support ancient libvorbis (< RC3). */
#if HAVE_VORBIS >= 2
vorbis_analysis(&d->vb,NULL);
/* Non-ancient case. */
vorbis_bitrate_addblock(&d->vb);
while(vorbis_bitrate_flushpacket(&d->vd, &d->op)) {
#else
vorbis_analysis(&d->vb,&d->op);
/* Make a lexical block to place the #ifdef's nearby. */
if (1) {
#endif
ogg_stream_packetin(&d->os,&d->op);
while(int result=ogg_stream_pageout(&d->os,&d->og)) {
if (!result) break;
TQByteArray output;
char * oggheader = reinterpret_cast<char *>(d->og.header);
char * oggbody = reinterpret_cast<char *>(d->og.body);
if (d->og.header_len) {
output.setRawData(oggheader, d->og.header_len);
ioslave->data(output);
output.resetRawData(oggheader, d->og.header_len);
}
if (d->og.body_len) {
output.setRawData(oggbody, d->og.body_len);
ioslave->data(output);
output.resetRawData(oggbody, d->og.body_len);
}
processed += d->og.header_len + d->og.body_len;
}
}
}
return processed;
}
unsigned long EncoderVorbis::size(long time_secs) const {
long vorbis_size;
switch (d->vorbis_encode_method)
{
case 0: // quality based encoding
#if HAVE_VORBIS >= 2 // If really old Vorbis is being used, skip this nicely.
{
// Estimated numbers based on the Vorbis FAQ:
// http://www.xiph.org/archives/vorbis-faq/200203/0030.html
static long vorbis_q_bitrate[] = { 60, 74, 86, 106, 120, 152,
183, 207, 239, 309, 440 };
long quality = static_cast<long>(d->vorbis_quality);
if (quality < 0 || quality > 10)
quality = 3;
vorbis_size = (time_secs * vorbis_q_bitrate[quality] * 1000) / 8;
break;
}
#endif // HAVE_VORBIS >= 2
default: // bitrate based encoding
vorbis_size = (time_secs * d->vorbis_bitrate/8);
break;
}
return vorbis_size;
}
const char * EncoderVorbis::mimeType() const{
return "audio/vorbis";
}
long EncoderVorbis::readInit(long /*size*/){
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
vorbis_analysis_init(&d->vd,&d->vi);
vorbis_block_init(&d->vd,&d->vb);
srand(time(NULL));
ogg_stream_init(&d->os,rand());
vorbis_analysis_headerout(&d->vd,&d->vc,&header,&header_comm,&header_code);
ogg_stream_packetin(&d->os,&header);
ogg_stream_packetin(&d->os,&header_comm);
ogg_stream_packetin(&d->os,&header_code);
while (int result = ogg_stream_flush(&d->os,&d->og)) {
if (!result) break;
TQByteArray output;
char * oggheader = reinterpret_cast<char *>(d->og.header);
char * oggbody = reinterpret_cast<char *>(d->og.body);
if (d->og.header_len) {
output.setRawData(oggheader, d->og.header_len);
ioslave->data(output);
output.resetRawData(oggheader, d->og.header_len);
}
if (d->og.body_len) {
output.setRawData(oggbody, d->og.body_len);
ioslave->data(output);
output.resetRawData(oggbody, d->og.body_len);
}
}
return 0;
}
long EncoderVorbis::read(int16_t * buf, int frames){
int i;
float **buffer=vorbis_analysis_buffer(&d->vd,frames);
/* uninterleave samples */
for(i=0;i<frames;i++){
buffer[0][i]=buf[2*i]/32768.0;
buffer[1][i]=buf[2*i+1]/32768.0;
}
/* process chunk of data */
vorbis_analysis_wrote(&d->vd,i);
return flush_vorbis();
}
long EncoderVorbis::readCleanup(){
// send end-of-stream and flush the encoder
vorbis_analysis_wrote(&d->vd,0);
long processed = flush_vorbis();
ogg_stream_clear(&d->os);
vorbis_block_clear(&d->vb);
vorbis_dsp_clear(&d->vd);
vorbis_info_clear(&d->vi);
return processed;
}
void EncoderVorbis::fillSongInfo( KCDDB::CDInfo info, int track, const TQString &comment )
{
if( !d->write_vorbis_comments )
return;
typedef TQPair<TQCString, TQVariant> CommentField;
TQValueList<CommentField> commentFields;
commentFields.append(CommentField("title", info.trackInfoList[track].get("title")));
commentFields.append(CommentField("artist", info.get("artist")));
commentFields.append(CommentField("album", info.get("title")));
commentFields.append(CommentField("genre", info.get("genre")));
commentFields.append(CommentField("tracknumber", TQString::number(track+1)));
commentFields.append(CommentField("comment", comment));
if (info.get("year").toInt() > 0) {
TQDateTime dt(TQDate(info.get("year").toInt(), 1, 1));
commentFields.append(CommentField("date", dt.toString(Qt::ISODate).utf8().data()));
}
for(TQValueListIterator<CommentField> it = commentFields.begin(); it != commentFields.end(); ++it) {
// if the value is not empty
if(!(*it).second.toString().isEmpty()) {
char *key = tqstrdup((*it).first);
char *value = tqstrdup((*it).second.toString().utf8().data());
vorbis_comment_add_tag(&d->vc, key, value);
delete [] key;
delete [] value;
}
}
}
#endif // HAVE_VORBIS
|