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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*! \file vdr_decoder_ogg.c
* \ingroup vdr
*
* The file implements a decoder which is used by the player to decode ogg vorbis audio files.
*
* Adapted from
* MP3/MPlayer plugin to VDR (C++)
* (C) 2001-2003 Stefan Huelswitt <huels@iname.com>
*/
#ifdef HAVE_VORBISFILE
#include "vdr_decoder_ogg.h"
#include <mad.h>
#include <vorbis/vorbisfile.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
// --- mgOggFile ----------------------------------------------------------------
class mgOggFile // : public mgFileInfo
{
private:
bool m_opened, m_canSeek;
OggVorbis_File vf;
void error (const char *action, const int err);
std::string m_filename;
public:
mgOggFile (std::string filename);
~mgOggFile ();
bool open (bool log = true);
void close (void);
long long seek (long long posMs = 0, bool relativ = false);
int stream (short *buffer, int samples);
bool canSeek ()
{
return m_canSeek;
}
long long indexMs (void);
};
mgOggFile::mgOggFile (std::string filename):
m_filename (filename)
{
m_canSeek = false;
m_opened = false;
}
mgOggFile::~mgOggFile ()
{
close ();
}
bool mgOggFile::open (bool log)
{
if (m_opened)
{
if (m_canSeek)
{
return (seek () >= 0);
}
return true;
}
FILE *
f = fopen (m_filename.c_str (), "r");
if (f)
{
int
r = ov_open (f, &vf, 0, 0);
if (!r)
{
m_canSeek = (ov_seekable (&vf) != 0);
m_opened = true;
}
else
{
fclose (f);
if (log)
{
error ("open", r);
}
}
}
else
{
if (log)
{
// esyslog("ERROR: failed to open file %s: %s", m_filename.c_str(), strerror(errno) );
}
}
return m_opened;
}
void
mgOggFile::close ()
{
if (m_opened)
{
ov_clear (&vf);
m_opened = false;
}
}
void
mgOggFile::error (const char *action, const int err)
{
char *errstr;
switch (err)
{
case OV_FALSE:
errstr = "false/no data available";
break;
case OV_EOF:
errstr = "EOF";
break;
case OV_HOLE:
errstr = "missing or corrupted data";
break;
case OV_EREAD:
errstr = "read error";
break;
case OV_EFAULT:
errstr = "internal error";
break;
case OV_EIMPL:
errstr = "unimplemented feature";
break;
case OV_EINVAL:
errstr = "invalid argument";
break;
case OV_ENOTVORBIS:
errstr = "no Ogg Vorbis stream";
break;
case OV_EBADHEADER:
errstr = "corrupted Ogg Vorbis stream";
break;
case OV_EVERSION:
errstr = "unsupported bitstream version";
break;
case OV_ENOTAUDIO:
errstr = "ENOTAUDIO";
break;
case OV_EBADPACKET:
errstr = "EBADPACKET";
break;
case OV_EBADLINK:
errstr = "corrupted link";
break;
case OV_ENOSEEK:
errstr = "stream not seekable";
break;
default:
errstr = "unspecified error";
break;
}
// esyslog( "ERROR: vorbisfile %s failed on %s: %s", action, m_filename.c_str(), errstr );
}
long long
mgOggFile::indexMs (void)
{
double p = ov_time_tell (&vf);
if (p < 0.0)
{
p = 0.0;
}
return (long long) (p * 1000.0);
}
long long
mgOggFile::seek (long long posMs, bool relativ)
{
if (relativ)
{
posMs += indexMs ();
}
int r = ov_time_seek (&vf, (double) posMs / 1000.0);
if (r)
{
error ("seek", r);
return -1;
}
posMs = indexMs ();
return posMs;
}
int
mgOggFile::stream (short *buffer, int samples)
{
int n;
do
{
int stream;
n = ov_read (&vf, (char *) buffer, samples * 2, 0, 2, 1, &stream);
}
while (n == OV_HOLE);
if (n < 0)
{
error ("read", n);
}
return (n / 2);
}
// --- mgOggDecoder -------------------------------------------------------------
mgOggDecoder::mgOggDecoder (mgItemGd * item):mgDecoder (item)
{
m_filename = item->getSourceFile ();
m_file = new mgOggFile (m_filename);
m_pcm = 0;
init ();
}
mgOggDecoder::~mgOggDecoder ()
{
clean ();
delete m_file;
}
bool mgOggDecoder::valid ()
{
bool
res = false;
if (tryLock ())
{
if (m_file->open (false))
{
res = true;
}
unlock ();
}
return res;
}
mgPlayInfo *
mgOggDecoder::playInfo (void)
{
if (m_playing)
{
// m_playinfo.m_index = index/1000;
// m_playinfo.m_total = info.Total;
return &m_playinfo;
}
return 0;
}
void
mgOggDecoder::init ()
{
clean ();
m_pcm = new struct mad_pcm;
m_index = 0;
}
bool mgOggDecoder::clean ()
{
m_playing = false;
delete
m_pcm;
m_pcm = 0;
m_file->close ();
return false;
}
#define SF_SAMPLES (sizeof(m_pcm->samples[0])/sizeof(mad_fixed_t))
bool mgOggDecoder::start ()
{
lock (true);
init ();
m_playing = true;
if (m_file->open () /*&& info.DoScan(true) */ )
{
// obtain from database: rate, channels
/* d(printf("ogg: open rate=%d channels=%d seek=%d\n",
info.SampleFreq,info.Channels,file.CanSeek()))
*/
if (m_item->getChannels () <= 2)
{
unlock ();
return true;
}
else
{
// esyslog( "ERROR: cannot play ogg file %s: more than 2 channels", m_filename.c_str() );
}
}
clean ();
unlock ();
return false;
}
bool mgOggDecoder::stop (void)
{
lock ();
if (m_playing)
{
clean ();
}
unlock ();
return true;
}
struct mgDecode *
mgOggDecoder::done (eDecodeStatus status)
{
m_ds.status = status;
m_ds.index = m_index;
m_ds.pcm = m_pcm;
unlock (); // release the lock from decode()
return &m_ds;
}
struct mgDecode *
mgOggDecoder::decode (void)
{
lock (); // this is released in Done()
if (m_playing)
{
short framebuff[2 * SF_SAMPLES];
int n = m_file->stream (framebuff, SF_SAMPLES);
if (n < 0)
{
return done (dsError);
}
if (n == 0)
{
return done (dsEof);
}
// should be done during initialization
// from database
m_pcm->samplerate = m_item->getSampleRate ();
m_pcm->channels = m_item->getChannels (); // from database
n /= m_pcm->channels;
m_pcm->length = n;
m_index = m_file->indexMs ();
short *data = framebuff;
mad_fixed_t *sam0 = m_pcm->samples[0], *sam1 = m_pcm->samples[1];
// shift value for mad_fixed conversion
const int s = MAD_F_FRACBITS + 1 - (sizeof (short) * 8);
if (m_pcm->channels > 1)
{
for (; n > 0; n--)
{
*sam0++ = (*data++) << s;
*sam1++ = (*data++) << s;
}
}
else
{
for (; n > 0; n--)
{
*sam0++ = (*data++) << s;
}
}
return done (dsPlay);
}
return done (dsError);
}
bool mgOggDecoder::skip (int Seconds, int Avail, int Rate)
{
lock ();
bool
res = false;
if (m_playing && m_file->canSeek ())
{
float
fsecs =
(float) Seconds - ((float) Avail / (float) (Rate * (16 / 8 * 2)));
// Byte/s = samplerate * 16 bit * 2 chan
long long
newpos = m_file->indexMs () + (long long) (fsecs * 1000.0);
if (newpos < 0)
{
newpos = 0;
}
newpos = m_file->seek (newpos, false);
if (newpos >= 0)
{
m_index = m_file->indexMs ();
#ifdef xDEBUG
int
i = index / 1000;
printf ("ogg: skipping to %02d:%02d\n", i / 60, i % 60);
#endif
res = true;
}
}
unlock ();
return res;
}
#endif //HAVE_VORBISFILE
|