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
|
#include "Metadata.h"
#include "main.h"
#include "api__in_mp3.h"
#include "LAMEInfo.h"
#include "AACFrame.h"
#include "config.h"
#include "LAMEInfo.h"
#include <shlwapi.h>
#include <assert.h>
#include <foundation/error.h>
#include <strsafe.h>
#define INFO_READ_SIZE 32768
Metadata::Metadata( CGioFile *_file, const wchar_t *_filename )
{
if ( !PathIsURL( _filename ) )
filename = _wcsdup( _filename );
ReadTags( _file );
if ( bitrate = _file->GetAvgVBRBitrate() * 1000 )
{
length_ms = _file->m_vbr_ms;
vbr = _file->m_vbr_flag || _file->m_vbr_hdr;
}
}
void GetFileDescription(const wchar_t *file, CGioFile &_file, wchar_t *data, size_t datalen);
void GetAudioInfo(const wchar_t *filename, CGioFile *file, int *len, int *channels, int *bitrate, int *vbr, int *sr);
int Metadata::Open(const wchar_t *_filename)
{
if ( filename && *filename )
free( filename );
filename = _wcsdup(_filename);
if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
return 1;
GetAudioInfo(filename, &file, &length_ms, &channels, &bitrate, &vbr, &sampleRate);
ReadTags(&file);
file.Close();
return METADATA_SUCCESS;
}
Metadata::~Metadata()
{
if (filename)
{
free(filename);
filename=0;
}
}
void Metadata::ReadTags(CGioFile *_file)
{
// Process ID3v1
if (config_parse_id3v1)
{
void *id3v1_data = _file->GetID3v1();
if (id3v1_data)
id3v1.Decode(id3v1_data);
}
if (config_parse_id3v2)
{
uint32_t len = 0;
void *id3v2_data = _file->GetID3v2(&len);
if (id3v2_data)
id3v2.Decode(id3v2_data, len);
}
if (config_parse_lyrics3)
{
uint32_t len = 0;
void *lyrics3_data = _file->GetLyrics3(&len);
if (lyrics3_data)
lyrics3.Decode(lyrics3_data, len);
}
if (config_parse_apev2)
{
uint32_t len = 0;
void *apev2_data = _file->GetAPEv2(&len);
if (apev2_data)
apev2.Decode(apev2_data, len);
}
}
static int ID3Write(const wchar_t *filename, HANDLE infile, DWORD offset, void *data, DWORD len)
{
wchar_t tempFile[MAX_PATH] = {0};
StringCchCopyW(tempFile, MAX_PATH, filename);
PathRemoveExtension(tempFile);
StringCchCatW(tempFile, MAX_PATH, L".tmp");
// check to make sure the filename was actually different!
// benski> TODO: we should just try to mangle the filename more rather than totally bail out
if (!_wcsicmp(tempFile, filename))
return SAVE_ERROR_CANT_OPEN_TEMPFILE;
// TODO: overlapped I/O
HANDLE outfile = CreateFile(tempFile, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, 0);
if (outfile != INVALID_HANDLE_VALUE)
{
DWORD written=0;
if (data && len)
WriteFile(outfile, data, len, &written, NULL);
SetFilePointer(infile, offset, 0, FILE_BEGIN);
DWORD read=0;
do
{
char data[4096] = {0};
written = read = 0;
ReadFile(infile, data, 4096, &read, NULL);
if (read) WriteFile(outfile, data, read, &written, NULL);
}
while (read != 0);
CloseHandle(outfile);
CloseHandle(infile);
if (!MoveFile(tempFile, filename))
{
if (!CopyFile(tempFile, filename, FALSE))
{
DeleteFile(tempFile);
return SAVE_ERROR_ERROR_OVERWRITING;
}
DeleteFile(tempFile);
}
return SAVE_SUCCESS;
}
return SAVE_ERROR_CANT_OPEN_TEMPFILE;
}
bool Metadata::IsDirty()
{
return id3v1.IsDirty() || id3v2.IsDirty() || lyrics3.IsDirty() || apev2.IsDirty();
}
int Metadata::Save()
{
if (!IsDirty())
return SAVE_SUCCESS;
int err=SAVE_SUCCESS;
if (GetFileAttributes(filename)&FILE_ATTRIBUTE_READONLY)
return SAVE_ERROR_READONLY;
HANDLE metadataFile = CreateFile(filename, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
if (metadataFile == INVALID_HANDLE_VALUE)
return SAVE_ERROR_OPENING_FILE;
if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
{
CloseHandle(metadataFile);
return SAVE_ERROR_OPENING_FILE;
}
bool strippedID3v1=false; // this flag will get set to true when we remove ID3v1 as a side effect of removing APEv2 or Lyrics3 (or ID3v2.4 end-tag if/when we implement)
bool strippedLyrics3=false;
/* Strip APEv2 */
if (config_parse_apev2 && config_write_apev2 && apev2.IsDirty())
{
uint32_t len = 0;
void *apev2_data = file.GetAPEv2(&len);
if (apev2_data)
{
uint32_t lyrics3_len = 0;
void *lyrics3_data = file.GetLyrics3(&lyrics3_len);
if (lyrics3_data)
SetFilePointer(metadataFile, -(LONG)(len + 15 + lyrics3_len + (file.GetID3v1()?128:0)), NULL, FILE_END);
else
SetFilePointer(metadataFile, -(LONG)(len + (file.GetID3v1()?128:0)), NULL, FILE_END);
SetEndOfFile(metadataFile);
strippedLyrics3=true;
strippedID3v1=true;
}
}
/* Strip Lyrics3 tag */
if (!strippedLyrics3 && config_parse_lyrics3 && lyrics3.IsDirty())
{
uint32_t len = 0;
void *lyrics3_data = file.GetLyrics3(&len);
if (lyrics3_data)
{
SetFilePointer(metadataFile, -(LONG)(len + 15 + (file.GetID3v1()?128:0)), NULL, FILE_END);
SetEndOfFile(metadataFile);
strippedID3v1=true;
}
}
/* Strip ID3v1(.1) tag */
if (!strippedID3v1 /* if we stripped lyrics3 tag, then we ended up stripping id3v1 also */
&& config_parse_id3v1 && config_write_id3v1 && id3v1.IsDirty())
{
if (file.GetID3v1()) // see if we have ID3v1
{
SetFilePointer(metadataFile, -128, NULL, FILE_END);
SetEndOfFile(metadataFile);
}
}
/* Write APEv2 */
if (config_parse_apev2 && config_write_apev2 && apev2.IsDirty() && apev2.HasData())
{
switch(config_apev2_header)
{
case ADD_HEADER:
apev2.SetFlags(APEv2::FLAG_HEADER_HAS_HEADER, APEv2::FLAG_HEADER_HAS_HEADER);
break;
case REMOVE_HEADER:
apev2.SetFlags(0, APEv2::FLAG_HEADER_HAS_HEADER);
break;
}
size_t apev2_len = apev2.EncodeSize();
void *apev2_data = malloc(apev2_len);
if (apev2_data && apev2.Encode(apev2_data, apev2_len) == APEv2::APEV2_SUCCESS)
{
SetFilePointer(metadataFile, 0, NULL, FILE_END);
DWORD bytesWritten=0;
WriteFile(metadataFile, apev2_data, (DWORD)apev2_len, &bytesWritten, 0);
free(apev2_data);
apev2_data = 0;
if (bytesWritten != apev2_len)
{
err=SAVE_APEV2_WRITE_ERROR;
goto fail;
}
}
else
{
free(apev2_data);
apev2_data = 0;
err=SAVE_APEV2_WRITE_ERROR;
goto fail;
}
}
/* Write Lyrics3 */
if (strippedLyrics3) /* if we need to rewrite it because we stripped it (e.g. removing an APEv2 tag)*/
{
/* since we don't modify lyrics3 (yet) we'll just rewrite the original binary data */
uint32_t len = 0;
void *lyrics3_data = file.GetLyrics3(&len);
if (lyrics3_data)
{
SetFilePointer(metadataFile, 0, NULL, FILE_END);
DWORD bytesWritten=0;
WriteFile(metadataFile, lyrics3_data, len, &bytesWritten, NULL);
if (bytesWritten != len)
{
err=SAVE_LYRICS3_WRITE_ERROR;
goto fail;
}
char temp[7] = {0};
StringCchPrintfA(temp, 7, "%06u", len);
bytesWritten = 0;
WriteFile(metadataFile, temp, 6, &bytesWritten, NULL);
if (bytesWritten != 6)
{
err=SAVE_LYRICS3_WRITE_ERROR;
goto fail;
}
bytesWritten = 0;
WriteFile(metadataFile, "LYRICS200", 9, &bytesWritten, NULL);
if (bytesWritten != 9)
{
err=SAVE_LYRICS3_WRITE_ERROR;
goto fail;
}
}
}
/* Write ID3v1 */
if (config_parse_id3v1 && config_write_id3v1 && id3v1.IsDirty())
{
uint8_t id3v1_data[128] = {0};
if (id3v1.Encode(id3v1_data) == METADATA_SUCCESS)
{
SetFilePointer(metadataFile, 0, NULL, FILE_END);
DWORD bytesWritten=0;
WriteFile(metadataFile, id3v1_data, 128, &bytesWritten, NULL);
if (bytesWritten != 128)
{
err=SAVE_ID3V1_WRITE_ERROR;
goto fail;
}
}
}
else if (strippedID3v1)
{
/** if we stripped lyrics3 or apev2 but didn't modify id3v1 (or are configured not to use it),
** we need to rewrite it back to the original data
**/
void *id3v1_data=file.GetID3v1();
if (id3v1_data)
{
SetFilePointer(metadataFile, 0, NULL, FILE_END);
DWORD bytesWritten=0;
WriteFile(metadataFile, id3v1_data, 128, &bytesWritten, NULL);
if (bytesWritten != 128)
{
err=SAVE_ID3V1_WRITE_ERROR;
goto fail;
}
}
}
/* Write ID3v2 */
if (config_parse_id3v2 && config_write_id3v2 && id3v2.IsDirty())
{
uint32_t oldlen=0;
void *old_id3v2_data = file.GetID3v2(&oldlen);
id3v2.id3v2.SetPadding(false); // turn off padding to see if we can get away with non re-writing the file
uint32_t newlen = id3v2.EncodeSize();
if (old_id3v2_data && !newlen) // there's an old tag, but no new tag
{
err = ID3Write(filename, metadataFile, oldlen, 0, 0);
if (err == SAVE_SUCCESS)
metadataFile = INVALID_HANDLE_VALUE; // ID3Write returns true if it closed the handle
else
goto fail;
}
else if (!old_id3v2_data && !newlen) // no old tag, no new tag.. easy :)
{
}
else
{
id3v2.id3v2.SetPadding(true);
if (newlen <= oldlen) // if we can fit in the old tag
{
if (oldlen != newlen)
id3v2.id3v2.ForcePading(oldlen-newlen); // pad out the rest of the tag
else
id3v2.id3v2.SetPadding(false);
assert(id3v2.EncodeSize() == oldlen);
newlen = oldlen;
uint8_t *new_id3v2_data = (uint8_t *)calloc(newlen, sizeof(uint8_t));
if (new_id3v2_data && id3v2.Encode(new_id3v2_data, newlen) == METADATA_SUCCESS)
{
// TODO: deal with files with multiple starting id3v2 tags
SetFilePointer(metadataFile, 0, NULL, FILE_BEGIN);
DWORD bytesWritten=0;
WriteFile(metadataFile, new_id3v2_data, newlen, &bytesWritten, NULL);
free(new_id3v2_data);
new_id3v2_data = 0;
if (bytesWritten != newlen)
{
err = SAVE_ID3V2_WRITE_ERROR;
goto fail;
}
}
else
{
free(new_id3v2_data);
new_id3v2_data = 0;
err = SAVE_ID3V2_WRITE_ERROR;
goto fail;
}
}
else // otherwise we have to pad out the start
{
newlen = id3v2.EncodeSize();
uint8_t *new_id3v2_data = (uint8_t *)calloc(newlen, sizeof(uint8_t));
if (new_id3v2_data && id3v2.Encode(new_id3v2_data, newlen) == METADATA_SUCCESS)
{
// TODO: deal with files with multiple starting id3v2 tags
SetFilePointer(metadataFile, 0, NULL, FILE_BEGIN);
DWORD bytesWritten=0;
err = ID3Write(filename, metadataFile, oldlen, new_id3v2_data, newlen);
free(new_id3v2_data);
new_id3v2_data = 0;
if (err == SAVE_SUCCESS)
metadataFile = INVALID_HANDLE_VALUE; // ID3Write returns true if it closed the handle
else
goto fail;
}
else
{
free(new_id3v2_data);
new_id3v2_data = 0;
err = SAVE_ID3V2_WRITE_ERROR;
goto fail;
}
}
}
}
fail:
file.Close();
if (metadataFile != INVALID_HANDLE_VALUE)
CloseHandle(metadataFile);
return err;
}
int Metadata::GetExtendedData(const char *tag, wchar_t *data, int dataLen)
{
int understood=0;
switch (id3v2.GetString(tag, data, dataLen))
{
case -1:
data[0]=0;
understood=1;
break;
case 1:
return 1;
}
switch (apev2.GetString(tag, data, dataLen))
{
case -1:
data[0]=0;
understood=1;
break;
case 1:
return 1;
}
switch (lyrics3.GetString(tag, data, dataLen))
{
case -1:
data[0]=0;
understood=1;
break;
case 1:
return 1;
}
switch (id3v1.GetString(tag, data, dataLen))
{
case -1:
data[0]=0;
understood=1;
break;
case 1:
return 1;
}
switch (GetString(tag, data, dataLen))
{
case -1:
data[0]=0;
understood=1;
break;
case 1:
return 1;
}
return understood;
}
int Metadata::SetExtendedData(const char *tag, const wchar_t *data)
{
int understood=0;
if (config_create_id3v2 || id3v2.HasData())
understood |= id3v2.SetString(tag, data);
if (config_create_apev2 || apev2.HasData())
understood |= apev2.SetString(tag, data);
if (config_create_id3v1 || id3v1.HasData())
understood |= id3v1.SetString(tag, data);
return understood;
}
int Metadata::GetString(const char *tag, wchar_t *data, int dataLen)
{
if (!_stricmp(tag, "formatinformation"))
{
data[0]=0;
if (filename)
{
if (file.Open(filename, INFO_READ_SIZE/1024) == NErr_Success)
GetFileDescription(filename, file, data, dataLen);
file.Close();
}
}
else if (!_stricmp(tag, "length"))
{
StringCchPrintfW(data, dataLen, L"%d", length_ms);
}
else if (!_stricmp(tag, "stereo"))
{
StringCchPrintfW(data, dataLen, L"%d", channels==2);
}
else if (!_stricmp(tag, "vbr"))
{
StringCchPrintfW(data, dataLen, L"%d", vbr);
}
else if (!_stricmp(tag, "bitrate"))
{
StringCchPrintfW(data, dataLen, L"%d", bitrate/1000);
}
else if (!_stricmp(tag, "gain"))
{
StringCchPrintfW(data, dataLen, L"%-+.2f dB", file.GetGain());
}
else if (!_stricmp(tag, "pregap"))
{
if (file.prepad)
{
StringCchPrintfW(data, dataLen, L"%u", file.prepad);
return 1;
}
return -1;
}
else if (!_stricmp(tag, "postgap"))
{
if (file.prepad) // yes, we check for this because postpad could legitimately be 0
{
StringCchPrintfW(data, dataLen, L"%u", file.postpad);
return 1;
}
return -1;
}
else if (!_stricmp(tag, "numsamples"))
{
if (file.m_vbr_samples)
{
StringCchPrintfW(data, dataLen, L"%I64u", file.m_vbr_samples);
return 1;
}
return -1;
}
else if (!_stricmp(tag, "endoffset"))
{
if (file.m_vbr_frames)
{
int totalFrames = file.m_vbr_frames;
if (totalFrames > 8)
{
int seekPoint = 0;
// we're using m_vbr_bytes here instead of file.ContentLength(), because we're already trusting the other LAME header info
#define MAX_SIZE_8_FRAMES (1448 * 8) // mp3 frames won't be ever be any bigger than this (320kbps 32000Hz + padding)
if (file.m_vbr_bytes > MAX_SIZE_8_FRAMES)
seekPoint = (int)(file.m_vbr_bytes - MAX_SIZE_8_FRAMES);
else
seekPoint = 0;
size_t offsets[8] = {0};
size_t offsetsRead = 0;
size_t offsetPosition = 0;
unsigned char header[6] = {0};
MPEGFrame frame;
if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
return -1;
// first we need to sync
while (1)
{
file.SetCurrentPosition(seekPoint, CGioFile::GIO_FILE_BEGIN);
int read = 0;
file.Read(header, 6, &read);
if (read != 6)
break;
frame.ReadBuffer(header);
if (frame.IsSync() && frame.GetLayer() == 3)
{
// make sure this isn't false sync - see if we can get another sync...
int nextPoint = seekPoint + frame.FrameSize();
file.SetCurrentPosition(nextPoint, CGioFile::GIO_FILE_BEGIN);
file.Read(header, 6, &read);
if (read != 6) // must be EOF
break;
frame.ReadBuffer(header);
if (frame.IsSync() && frame.GetLayer() == 3)
break;
}
seekPoint++;
}
while (1)
{
file.SetCurrentPosition(seekPoint, CGioFile::GIO_FILE_BEGIN);
int read = 0;
file.Read(header, 6, &read);
if (read != 6)
break;
frame.ReadBuffer(header);
if (frame.IsSync() && frame.GetLayer() == 3)
{
offsets[offsetPosition] = seekPoint;
offsetPosition = (offsetPosition + 1) % 8;
offsetsRead++;
seekPoint += frame.FrameSize();
}
else
break;
}
if (offsetsRead >= 8)
{
StringCchPrintfW(data, dataLen, L"%I32d", offsets[offsetPosition] + file.m_vbr_frame_len);
file.Close();
return 1;
}
file.Close();
}
}
return -1;
}
else
return 0;
return 1;
}
int fixAACCBRbitrate(int br);
|