-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.c
738 lines (679 loc) · 22.2 KB
/
decoder.c
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
* MP3/MPlayer plugin to VDR (C++)
*
* (C) 2001-2005 Stefan Huelswitt <s.huelswitt@gmx.de>
*
* This code 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 code 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <vdr/videodir.h>
#include "common.h"
#include "data-mp3.h"
//#include "data-src.h"
#include "decoder.h"
#include "decoder-core.h"
#include "decoder-mp3.h"
#include "ultrastarsong.h"
#define CACHEFILENAME "id3info.cache"
#define CACHESAVETIMEOUT 120 // secs
#define CACHEPURGETIMEOUT 120 // days
cInfoCache InfoCache;
char *cachedir = 0;
int MakeHashBuff( const char *buff, int len )
{
int h = len;
while( len-- )
h = ( h * 13 + *buff++ ) & 0x7ff;
return h;
}
// --- cSongInfo ---------------------------------------------------------------
cSongInfo::cSongInfo( void )
{
Title = Artist = Album = 0;
Clear( );
}
cSongInfo::~cSongInfo( )
{
Clear( );
}
void cSongInfo::Clear( void )
{
Frames = 0;
Total = -1;
DecoderID = 0;
SampleFreq = Channels = Bitrate = MaxBitrate = ChMode = -1;
free( Title );
Title = 0;
free( Artist );
Artist = 0;
free( Album );
Album = 0;
Year = -1;
Level = Peak = 0.0;
infoDone = false;
}
void cSongInfo::Set( cSongInfo * si )
{
Clear( );
InfoDone( );
Frames = si->Frames;
Total = si->Total;
SampleFreq = si->SampleFreq;
Channels = si->Channels;
Bitrate = si->Bitrate;
MaxBitrate = si->MaxBitrate;
ChMode = si->ChMode;
Year = si->Year;
Title = si->Title ? strdup( si->Title ) : 0;
Artist = si->Artist ? strdup( si->Artist ) : 0;
Album = si->Album ? strdup( si->Album ) : 0;
if( si->Level > 0.0 )
{ // preserve old level
Level = si->Level;
Peak = si->Peak;
}
DecoderID = si->DecoderID;
}
void cSongInfo::FakeTitle( const char *filename, const char *extention )
{
// if no title, try to build a reasonable from the filename
if( !Title && filename )
{
char *s = rindex( filename, '/' );
if( s && *s == '/' )
{
s++;
Title = strdup( s );
strreplace( Title, '_', ' ' );
if( extention )
{ // strip given extention
int l = strlen( Title ) - strlen( extention );
if( l > 0 && !strcasecmp( Title + l, extention ) )
Title[l] = 0;
}
else
{ // strip any extention
s = rindex( Title, '.' );
if( s && *s == '.' && strlen( s ) <= 5 )
*s = 0;
}
d( printf( "[karaoke] karaoke: faking title '%s' from filename '%s'\n", Title, filename ) )}
}
}
// --- cFileInfo ---------------------------------------------------------------
cFileInfo::cFileInfo( void )
{
Filename = FsID = 0;
Clear( );
}
cFileInfo::cFileInfo( const char *Name )
{
Filename = FsID = 0;
Clear( );
Filename = strdup( Name );
}
cFileInfo::~cFileInfo( )
{
Clear( );
}
void cFileInfo::Clear( void )
{
free( Filename );
Filename = 0;
free( FsID );
FsID = 0;
Filesize = 0;
CTime = 0;
FsType = 0;
removable = -1;
infoDone = false;
}
bool cFileInfo::Removable( void )
{
if( removable < 0 && Filename )
{
cFileSource *src = KaraokeSources.FindSource( Filename );
if( src )
removable = src->NeedsMount( );
else
removable = 1;
}
return ( removable != 0 );
}
void cFileInfo::Set( cFileInfo * fi )
{
Clear( );
InfoDone( );
Filename = fi->Filename ? strdup( fi->Filename ) : 0;
FsID = fi->FsID ? strdup( fi->FsID ) : 0;
Filesize = fi->Filesize;
CTime = fi->CTime;
}
bool cFileInfo::FileInfo( bool log )
{
if( Filename )
{
struct stat64 ds;
if( !stat64( Filename, &ds ) )
{
if( S_ISREG( ds.st_mode ) )
{
free( FsID );
FsID = 0;
FsType = 0;
struct statfs64 sfs;
if( !statfs64( Filename, &sfs ) )
{
if( Removable( ) )
asprintf( &FsID, "%llx:%llx", sfs.f_blocks, sfs.f_files );
FsType = sfs.f_type;
}
else if( errno != ENOSYS && log )
{
esyslog( "[vdr-karaoke] ERROR: can't statfs %s: %s", Filename, strerror( errno ) );
}
Filesize = ds.st_size;
CTime = ds.st_ctime;
#ifdef CDFS_MAGIC
if( FsType == CDFS_MAGIC )
CTime = 0; // CDFS returns mount time as ctime
#endif
InfoDone( );
return true;
}
else if( log )
{
esyslog( "[vdr-karaoke] ERROR: %s is not a regular file", Filename );
}
}
else if( log )
{
esyslog( "[vdr-karaoke] ERROR: can't stat %s: %s", Filename, strerror( errno ) );
}
}
return false;
}
// --- cDecoders ---------------------------------------------------------------
cUltraStarSong mySong;
char tmp[200];
cDecoder *cDecoders::FindDecoder( cFileObj * Obj )
{
const char *filename=Obj->FullPath();
mySong.ReadSong(filename);
strcpy(tmp,mySong.m_mp3FileName);
const char *full=tmp;
printf("[karaoke] %s ->\n%s\n[karaoke] %s",filename,Obj->Subdir(),full);
cFileInfo fi(full);
cCacheData *dat;
cDecoder *decoder = 0;
if( fi.FileInfo( false ) && ( dat = InfoCache.Search( &fi ) ) )
{
if( dat->DecoderID )
{
//d(printf("karaoke: found DecoderID '%s' for %s from cache\n",cDecoders::ID2Str(dat->DecoderID),Filename))
switch ( dat->DecoderID )
{
case DEC_MP3:
decoder = new cMP3Decoder( full );
break;
default:
esyslog( "[vdr-karaoke] ERROR: bad DecoderID '%s' from info cache: %s", cDecoders::ID2Str( dat->DecoderID ),
full );
break;
}
}
dat->Unlock( );
}
if( !decoder || !decoder->Valid( ) )
{
// no decoder in cache or cached decoder doesn't matches.
// try to detect a decoder
delete decoder;
decoder = 0;
if( !decoder )
{
decoder = new cMP3Decoder( full );
if( !decoder || !decoder->Valid( ) )
{
delete decoder;
decoder = 0;
}
}
if( !decoder )
esyslog( "[vdr-karaoke] ERROR: no decoder found for %s", Obj->Name( ) );
}
return decoder;
}
const char *cDecoders::ID2Str( int id )
{
switch ( id )
{
case DEC_MP3:
return DEC_MP3_STR;
}
return 0;
}
int cDecoders::Str2ID( const char *str )
{
if( !strcmp( str, DEC_MP3_STR ) )
return DEC_MP3;
return 0;
}
// --- cDecoder ----------------------------------------------------------------
cDecoder::cDecoder( const char *Filename )
{
filename = strdup( Filename );
locked = 0;
urgentLock = playing = false;
}
cDecoder::~cDecoder( )
{
free( filename );
}
void cDecoder::Lock( bool urgent )
{
locklock.Lock( );
if( urgent && locked )
urgentLock = true; // signal other locks to release quickly
locked++;
locklock.Unlock( ); // don't hold the "locklock" when locking "lock", may cause a deadlock
lock.Lock( );
urgentLock = false;
}
void cDecoder::Unlock( void )
{
locklock.Lock( );
locked--;
lock.Unlock( );
locklock.Unlock( );
}
bool cDecoder::TryLock( void )
{
bool res = false;
locklock.Lock( );
if( !locked && !playing )
{
Lock( );
res = true;
}
locklock.Unlock( );
return res;
}
// --- cCacheData -----------------------------------------------------
cCacheData::cCacheData( void )
{
touch = 0;
version = 0;
}
void cCacheData::Touch( void )
{
touch = time( 0 );
}
#define SECS_PER_DAY (24*60*60)
bool cCacheData::Purge( void )
{
time_t now = time( 0 );
//XXX does this realy made sense?
//if(touch+CACHEPURGETIMEOUT*SECS_PER_DAY < now) {
// d(printf("cache: purged: timeout %s\n",Filename))
// return true;
// }
if( touch + CACHEPURGETIMEOUT * SECS_PER_DAY / 10 < now )
{
if( !Removable( ) )
{ // is this a permant source?
struct stat64 ds; // does the file exists? if not, purge
if( stat64( Filename, &ds ) || !S_ISREG( ds.st_mode ) || access( Filename, R_OK ) )
{
d( printf( "[karaoke] cache: purged: file not found %s\n", Filename ) ) return true;
}
}
}
return false;
}
bool cCacheData::Upgrade( void )
{
if( version < 7 )
{
if( Removable( ) )
{
if( !FsID )
FsID = strdup( "old" ); // Dummy entry, will be replaced in InfoCache::Search()
}
else
{
free( FsID );
FsID = 0;
}
}
if( version < 4 )
{
Touch( ); // Touch entry
}
if( version < 3 && !Title )
{
FakeTitle( Filename, ".mp3" ); // Fake title
}
if( version < 2 && Bitrate <= 0 )
{
return false; // Trash entry without bitrate
}
return true;
}
void cCacheData::Create( cFileInfo * fi, cSongInfo * si )
{
cFileInfo::Set( fi );
cSongInfo::Set( si );
hash = MakeHash( Filename );
Touch( );
}
bool cCacheData::Save( FILE * f )
{
fprintf( f, "##BEGIN\n"
"Filename=%s\n"
"Filesize=%lld\n"
"Timestamp=%ld\n"
"Touch=%ld\n"
"Version=%d\n"
"Frames=%d\n"
"Total=%d\n"
"SampleFreq=%d\n"
"Channels=%d\n"
"Bitrate=%d\n"
"MaxBitrate=%d\n"
"ChMode=%d\n"
"Year=%d\n"
"Level=%.4f\n"
"Peak=%.4f\n",
Filename, Filesize, CTime, touch, CACHE_VERSION, Frames, Total, SampleFreq, Channels, Bitrate, MaxBitrate,
ChMode, Year, Level, Peak );
if( Title )
fprintf( f, "Title=%s\n", Title );
if( Artist )
fprintf( f, "Artist=%s\n", Artist );
if( Album )
fprintf( f, "Album=%s\n", Album );
if( DecoderID )
fprintf( f, "DecoderID=%s\n", cDecoders::ID2Str( DecoderID ) );
if( FsID )
fprintf( f, "FsID=%s\n", FsID );
fprintf( f, "##END\n" );
return ferror( f ) == 0;
}
bool cCacheData::Load( FILE * f )
{
char buf[1024];
const char *delimiters = "=\n";
cFileInfo::Clear( );
cSongInfo::Clear( );
while( fgets( buf, sizeof( buf ), f ) )
{
char *ptrptr;
char *name = strtok_r( buf, delimiters, &ptrptr );
char *value = strtok_r( 0, delimiters, &ptrptr );
if( name )
{
if( !strcasecmp( name, "##END" ) )
break;
if( value )
{
if( !strcasecmp( name, "Filename" ) )
Filename = strdup( value );
else if( !strcasecmp( name, "Filesize" ) || !strcasecmp( name, "Size" ) )
Filesize = atoll( value );
else if( !strcasecmp( name, "FsID" ) )
FsID = strdup( value );
else if( !strcasecmp( name, "Timestamp" ) )
CTime = atol( value );
else if( !strcasecmp( name, "Touch" ) )
touch = atol( value );
else if( !strcasecmp( name, "Version" ) )
version = atoi( value );
else if( !strcasecmp( name, "DecoderID" ) )
DecoderID = cDecoders::Str2ID( value );
else if( !strcasecmp( name, "Frames" ) )
Frames = atoi( value );
else if( !strcasecmp( name, "Total" ) )
Total = atoi( value );
else if( !strcasecmp( name, "SampleFreq" ) )
SampleFreq = atoi( value );
else if( !strcasecmp( name, "Channels" ) )
Channels = atoi( value );
else if( !strcasecmp( name, "Bitrate" ) )
Bitrate = atoi( value );
else if( !strcasecmp( name, "MaxBitrate" ) )
MaxBitrate = atoi( value );
else if( !strcasecmp( name, "ChMode" ) )
ChMode = atoi( value );
else if( !strcasecmp( name, "Year" ) )
Year = atoi( value );
else if( !strcasecmp( name, "Title" ) )
Title = strdup( value );
else if( !strcasecmp( name, "Artist" ) )
Artist = strdup( value );
else if( !strcasecmp( name, "Album" ) )
Album = strdup( value );
else if( !strcasecmp( name, "Level" ) )
Level = atof( value );
else if( !strcasecmp( name, "Peak" ) )
Peak = atof( value );
else
d( printf( "[karaoke] cache: ignoring bad token '%s' from cache file\n", name ) )}
}
}
if( ferror( f ) || !Filename )
return false;
hash = MakeHash( Filename );
return true;
}
// --- cInfoCache ----------------------------------------------------
cInfoCache::cInfoCache( void )
{
lasttime = lastpurge = 0;
modified = false;
}
void cInfoCache::Cache( cSongInfo * info, cFileInfo * file )
{
lock.Lock( );
cCacheData *dat = Search( file );
if( dat )
{
dat->Create( file, info );
Modified( );
dat->Unlock( );
d( printf( "[karaoke] cache: updating infos for %s\n", file->Filename ) )}
else
{
dat = new cCacheData;
dat->Create( file, info );
AddEntry( dat );
d( printf( "[karaoke] cache: caching infos for %s\n", file->Filename ) )}
lock.Unlock( );
}
cCacheData *cInfoCache::Search( cFileInfo * file )
{
int hash = MakeHash( file->Filename );
lock.Lock( );
cCacheData *dat = FirstEntry( hash );
while( dat )
{
if( dat->hash == hash && !strcmp( dat->Filename, file->Filename )
&& dat->Filesize == file->Filesize )
{
dat->Lock( );
if( file->FsID && dat->FsID && !strcmp( dat->FsID, "old" ) )
{ // duplicate FsID for old entries
dat->FsID = strdup( file->FsID );
dat->Touch( );
Modified( );
//d(printf("adding FsID for %s\n",dat->Filename))
}
if( ( !file->FsID && !dat->FsID )
|| ( file->FsID && dat->FsID && !strcmp( dat->FsID, file->FsID ) ) )
{
//d(printf("cache: found cache entry for %s\n",dat->Filename))
dat->Touch( );
Modified( );
if( dat->CTime != file->CTime )
{
d( printf( "[karaoke] cache: ctime differs, removing from cache: %s\n", dat->Filename ) )
DelEntry( dat );
dat = 0;
}
break;
}
dat->Unlock( );
}
dat = ( cCacheData * ) dat->Next( );
}
lock.Unlock( );
return dat;
}
void cInfoCache::AddEntry( cCacheData * dat )
{
lists[dat->hash % CACHELINES].Add( dat );
Modified( );
}
void cInfoCache::DelEntry( cCacheData * dat )
{
dat->Lock( );
lists[dat->hash % CACHELINES].Del( dat );
Modified( );
}
cCacheData *cInfoCache::FirstEntry( int hash )
{
return lists[hash % CACHELINES].First( );
}
void cInfoCache::Purge( void )
{
time_t now = time( 0 );
struct tm tm_r;
struct tm *ptm = localtime_r( &now, &tm_r );
if( now - lastpurge > 3600 && ptm->tm_hour == 5 )
{
isyslog( "cleaning up id3 cache" );
lock.Lock( );
for( int i = 0; i < CACHELINES; i++ )
{
cCacheData *dat = FirstEntry( i );
while( dat )
{
cCacheData *ndat = ( cCacheData * ) dat->Next( );
if( dat->Purge( ) )
DelEntry( dat );
dat = ndat;
}
}
lock.Unlock( );
lastpurge = now;
}
}
char *cInfoCache::CacheFile( void )
{
return AddPath( cachedir ? cachedir : VideoDirectory, CACHEFILENAME );
}
void cInfoCache::Save( bool force )
{
Purge( );
if( modified && ( force || time( 0 ) > lasttime ) )
{
char *name = CacheFile( );
cSafeFile f( name );
free( name );
if( f.Open( ) )
{
lock.Lock( );
fprintf( f, "## This is a generated file. DO NOT EDIT!!\n"
"## This file will be OVERWRITTEN WITHOUT WARNING!!\n" );
for( int i = 0; i < CACHELINES; i++ )
{
cCacheData *dat = FirstEntry( i );
while( dat )
{
if( !dat->Save( f ) )
{
i = CACHELINES + 1;
break;
}
dat = ( cCacheData * ) dat->Next( );
}
}
lock.Unlock( );
f.Close( );
modified = false;
lasttime = time( 0 ) + CACHESAVETIMEOUT;
d( printf( "[karaoke] cache: saved cache to file\n" ) )}
}
}
void cInfoCache::Load( void )
{
char *name = CacheFile( );
if( access( name, F_OK ) == 0 )
{
isyslog( "loading id3 cache from %s", name );
FILE *f = fopen( name, "r" );
if( f )
{
char buf[256];
bool mod = false;
lock.Lock( );
for( int i = 0; i < CACHELINES; i++ )
lists[i].Clear( );
while( fgets( buf, sizeof( buf ), f ) )
{
if( !strcasecmp( buf, "##BEGIN\n" ) )
{
cCacheData *dat = new cCacheData;
if( dat->Load( f ) )
{
if( dat->version != CACHE_VERSION )
{
if( dat->Upgrade( ) )
mod = true;
else
{
delete dat;
continue;
}
}
AddEntry( dat );
}
else
{
delete dat;
if( ferror( f ) )
{
esyslog( "[vdr-karaoke] ERROR: failed to load id3 cache" );
break;
}
}
}
}
lock.Unlock( );
fclose( f );
modified = false;
if( mod )
Modified( );
}
else
LOG_ERROR_STR( name );
}
free( name );
}