summaryrefslogtreecommitdiff
path: root/firmware/id3.c
blob: 35b983cbee3aaa9c2bd66af5151eb2cf88551bcc (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Daniel Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*
 * Parts of this code has been stolen from the Ample project and was written
 * by David Härdeman.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "file.h"
#include "debug.h"
#include "atoi.h"

#include "id3.h"

/* Some utility macros used in getsonglength() */
#define BYTE0(x) ((x >> 24) & 0xFF)
#define BYTE1(x) ((x >> 16) & 0xFF)
#define BYTE2(x) ((x >> 8)  & 0xFF)
#define BYTE3(x) ((x >> 0)  & 0xFF)

#define UNSYNC(b1,b2,b3,b4) (((b1 & 0x7F) << (3*7)) | \
                             ((b2 & 0x7F) << (2*7)) | \
                             ((b3 & 0x7F) << (1*7)) | \
                             ((b4 & 0x7F) << (0*7)))

/* Table of bitrates for MP3 files, all values in kilo.
 * Indexed by version, layer and value of bit 15-12 in header.
 */
const int bitrate_table[2][3][16] =
{
    {
        {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
        {0,32,48,56, 64,80, 96, 112,128,160,192,224,256,320,384,0},
        {0,32,40,48, 56,64, 80, 96, 112,128,160,192,224,256,320,0}
    },
    {
        {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
        {0, 8,16,24,32,40,48, 56, 64, 80, 96,112,128,144,160,0},
        {0, 8,16,24,32,40,48, 56, 64, 80, 96,112,128,144,160,0}
    }
};

/* Table of samples per frame for MP3 files.
 * Indexed by layer. Multiplied with 1000.
 */
const int bs[4] = {0, 384000, 1152000, 1152000};

/* Table of sample frequency for MP3 files.
 * Indexed by version and layer.
 */
const int freqtab[2][4] =
{
    {44100, 48000, 32000, 0},
    {22050, 24000, 16000, 0},
};

/*
 * Removes trailing spaces from a string.
 *
 * Arguments: buffer - the string to process
 *
 * Returns: void
 */
static void 
stripspaces(char *buffer) 
{
    int i = 0;
    while(*(buffer + i) != '\0')
        i++;
    
    for(;i >= 0; i--) {
        if(*(buffer + i) == ' ')
            *(buffer + i) = '\0';
        else if(*(buffer + i) == '\0')
            continue;
        else
            break;
    }
}

/*
 * Sets the title of an MP3 entry based on its ID3v1 tag.
 *
 * Arguments: file - the MP3 file to scen for a ID3v1 tag
 *            entry - the entry to set the title in
 *
 * Returns: true if a title was found and created, else false
 */
static bool setid3v1title(int fd, struct mp3entry *entry) 
{
    char buffer[31];
    int offsets[3] = {-95,-65,-125};
    int i;

    for(i=0;i<3;i++) {
        if(-1 == lseek(fd, offsets[i], SEEK_END))
            return false;

        buffer[30]=0;
        read(fd, buffer, 30);
        stripspaces(buffer);
        
        if(buffer[0]) {
            switch(i) {
            case 0:
                strcpy(entry->id3v1buf[0], buffer);
                entry->artist = entry->id3v1buf[0];
                break;
            case 1:
                strcpy(entry->id3v1buf[1], buffer);
                entry->album = entry->id3v1buf[1];
                break;
            case 2:
                strcpy(entry->id3v1buf[2], buffer);
                entry->title = entry->id3v1buf[2];
                break;
            }
        }
    }

    return true;
}


/*
 * Sets the title of an MP3 entry based on its ID3v2 tag.
 *
 * Arguments: file - the MP3 file to scen for a ID3v2 tag
 *            entry - the entry to set the title in
 *
 * Returns: true if a title was found and created, else false
 */
static void setid3v2title(int fd, struct mp3entry *entry) 
{
    unsigned int minframesize;
    int size;
    unsigned int readsize = 0, headerlen;
    char *title = NULL;
    char *artist = NULL;
    char *album = NULL;
    char *tracknum = NULL;
    char header[10];
    unsigned short int version;
    int titlen=0, artistn=0, albumn=0, tracknumn=0;   
    char *buffer = entry->id3v2buf;
	
    /* 10 = headerlength */
    if(entry->id3v2len < 10)
        return;

    /* Check version */
    lseek(fd, 0, SEEK_SET);
    if(10 != read(fd, header, 10))
        return;

    version = (unsigned short int)header[3];
	
    /* Read all frames in the tag */
    size = entry->id3v2len - 10;

    if(size >= (int)sizeof(entry->id3v2buf))
        size = sizeof(entry->id3v2buf)-1;

    if(size != read(fd, buffer, size))
        return;

    *(buffer + size) = '\0';

    /* Set minimun frame size according to ID3v2 version */
    if(version > 2)
        minframesize = 12;
    else
        minframesize = 8;

    /* 
     * We must have at least minframesize bytes left for the 
     * remaining frames to be interesting 
     */
    while(size - readsize > minframesize) {
        
        /* Read frame header and check length */
        if(version > 2) {
            memcpy(header, (buffer + readsize), 10);
            readsize += 10;
            headerlen = UNSYNC(header[4], header[5], 
                               header[6], header[7]);
        } else {
            memcpy(header, (buffer + readsize), 6);			
            readsize += 6;
            headerlen = (header[3] << 16) + 
                (header[4] << 8) + 
                (header[5]);
        }
        if(headerlen < 1)
            continue;
        
        /* Check for certain frame headers */
        if(!strncmp(header, "TPE1", strlen("TPE1")) || 
           !strncmp(header, "TP1", strlen("TP1"))) {
            readsize++;
            headerlen--;
            if(headerlen > (size - readsize))
                headerlen = (size - readsize);
            artist = buffer + readsize;
            artistn = headerlen;
            readsize += headerlen;
        }
        else if(!strncmp(header, "TIT2", strlen("TIT2")) || 
                !strncmp(header, "TT2", strlen("TT2"))) {
            readsize++;
            headerlen--;
            if(headerlen > (size - readsize))
                headerlen = (size - readsize);
            title = buffer + readsize;
            titlen = headerlen;
            readsize += headerlen;
        }
        else if(!strncmp(header, "TALB", strlen("TALB"))) {
            readsize++;
            headerlen--;
            if(headerlen > (size - readsize))
                headerlen = (size - readsize);
            album = buffer + readsize;
            albumn = headerlen;
            readsize += headerlen;
        }
        else if(!strncmp(header, "TRCK", strlen("TRCK"))) {
            readsize++;
            headerlen--;
            if(headerlen > (size - readsize))
                headerlen = (size - readsize);
            tracknum = buffer + readsize;
            tracknumn = headerlen;
            readsize += headerlen;
        } else {
            readsize += headerlen;
        }
    }
    
    if(artist) {
        entry->artist = artist;
        artist[artistn]=0;
    }
  
    if(title) {
        entry->title = title;
        title[titlen]=0;
    }

    if(album) {
        entry->album = album;
        album[albumn]=0;
    }

    if(tracknum) {
        tracknum[tracknumn] = 0;
        entry->tracknum = atoi(tracknum);
    }
}

/*
 * Calculates the size of the ID3v2 tag.
 *
 * Arguments: file - the file to search for a tag.
 *
 * Returns: the size of the tag or 0 if none was found
 */
static int getid3v2len(int fd) 
{
    char buf[6];
    int offset;
	
    /* Make sure file has a ID3 tag */
    if((-1 == lseek(fd, 0, SEEK_SET)) ||
       (read(fd, buf, 6) != 6) ||
       (strncmp(buf, "ID3", strlen("ID3")) != 0))
        offset = 0;

    /* Now check what the ID3v2 size field says */
    else if(read(fd, buf, 4) != 4)
        offset = 0;
    else
        offset = UNSYNC(buf[0], buf[1], buf[2], buf[3]) + 10;

    return offset;
}

static int getfilesize(int fd)
{
    int size;
    
    /* seek to the end of it */
    size = lseek(fd, 0, SEEK_END);
    if(-1 == size)
        return 0; /* unknown */

    return size;
}

/*
 * Calculates the size of the ID3v1 tag.
 *
 * Arguments: file - the file to search for a tag.
 *
 * Returns: the size of the tag or 0 if none was found
 */
static int getid3v1len(int fd) 
{
    char buf[3];
    int offset;

    /* Check if we find "TAG" 128 bytes from EOF */
    if((lseek(fd, -128, SEEK_END) == -1) ||
       (read(fd, buf, 3) != 3) ||
       (strncmp(buf, "TAG", 3) != 0))
        offset = 0;
    else
        offset = 128;
	
    return offset;
}

/* check if 'head' is a valid mp3 frame header */
static bool mp3frameheader(unsigned long head)
{
    if ((head & 0xffe00000) != 0xffe00000) /* bad sync? */
        return false;
    if (!((head >> 17) & 3)) /* no layer? */
        return false;
    if (((head >> 12) & 0xf) == 0xf) /* bad bitrate? */
        return false;
    if (!((head >> 12) & 0xf)) /* no bitrate? */
        return false;
    if (((head >> 10) & 0x3) == 0x3) /* bad sample rate? */
        return false;
    if (((head >> 19) & 1) == 1 &&
        ((head >> 17) & 3) == 3 &&
        ((head >> 16) & 1) == 1)
        return false;
    if ((head & 0xffff0000) == 0xfffe0000)
        return false;
	
    return true;
}

/*
 * Calculates the length (in milliseconds) of an MP3 file. Currently this code
 * doesn't care about VBR (Variable BitRate) files since it would have to scan
 * through the entire file but this should become a config option in the
 * future.
 *
 * Modified to only use integers.
 *
 * Arguments: file - the file to calculate the length upon
 *            entry - the entry to update with the length
 *
 * Returns: the song length in milliseconds, 
 *          -1 means that it couldn't be calculated
 */
static int getsonglength(int fd, struct mp3entry *entry)
{
    unsigned int filetime = 0;
    unsigned long header=0;
    unsigned char tmp;
    unsigned char frame[156];
    unsigned char* xing;
    
    int version;
    int layer;
    int bitindex;
    int bitrate;
    int freqindex;
    int frequency;
    int chmode;
    int bytecount;

    long bpf;
    long tpf;

    /* Start searching after ID3v2 header */ 
    if(-1 == lseek(fd, entry->id3v2len, SEEK_SET))
        return -1;
	
    /* Fill up header with first 24 bits */
    for(version = 0; version < 3; version++) {
        header <<= 8;
        if(!read(fd, &tmp, 1))
            return -1;
        header |= tmp;
    }
	
    /* Loop trough file until we find a frame header */
    bytecount = 0;
  restart:
    do {
        header <<= 8;
        if(!read(fd, &tmp, 1))
            return -1;
        header |= tmp;

        /* Quit if we haven't found a valid header within 128K */
        bytecount++;
        if(bytecount > 0x20000)
            return -1;
    } while(!mp3frameheader(header));
	
    /* 
     * Some files are filled with garbage in the beginning, 
     * if the bitrate index of the header is binary 1111 
     * that is a good indicator
     */
    if((header & 0xF000) == 0xF000)
        goto restart;

#ifdef DEBUG_VERBOSE
    fprintf(stderr,
            "We found %x-%x-%x-%x and checksync %i and test %x\n", 
            BYTE0(header), BYTE1(header), BYTE2(header), BYTE3(header), 
            CHECKSYNC(header), (header & 0xF000) == 0xF000);
#endif
    /* MPEG Audio Version */
    switch((header & 0x180000) >> 19) {
        case 2:
            version = 2;
            break;
        case 3:
            version = 1;
            break;
        default:
            goto restart;
    }

    /* Layer */
    switch((header & 0x060000) >> 17) {
        case 1:
            layer = 3;
            break;
        case 2:
            layer = 2;
            break;
        case 3:
            layer = 1;
            break;
        default:
            goto restart;
    }

    /* Bitrate */
    bitindex = (header & 0xF000) >> 12;
    bitrate = bitrate_table[version-1][layer-1][bitindex];
    if(bitrate == 0)
        goto restart;

    /* Sampling frequency */
    freqindex = (header & 0x0C00) >> 10;
    frequency = freqtab[version-1][freqindex];
    if(frequency == 0)
        goto restart;

#ifdef DEBUG_VERBOSE
    DEBUGF( "Version %i, lay %i, biti %i, bitr %i, freqi %i, freq %i, chmode %d\n", 
            version, layer, bitindex, bitrate, freqindex, frequency, chmode);
#endif
    entry->version = version;
    entry->layer = layer;
    entry->frequency = frequency;
	
    /* Calculate bytes per frame, calculation depends on layer */
    switch(layer) {
        case 1:
            bpf = bitrate_table[version - 1][layer - 1][bitindex];
            bpf *= 48000;
            bpf /= freqtab[version-1][freqindex] << (version - 1);
            break;
        case 2:
        case 3:
            bpf = bitrate_table[version - 1][layer - 1][bitindex];
            bpf *= 144000;
            bpf /= freqtab[version-1][freqindex] << (version - 1);
            break;
        default:
            bpf = 1;
    }
	
    /* Calculate time per frame */
    tpf = bs[layer] / (freqtab[version-1][freqindex] << (version - 1));
        
    entry->bpf = bpf;
    entry->tpf = tpf;
 
    /* OK, we have found a frame. Let's see if it has a Xing header */
    if(read(fd, frame, sizeof frame) < 0)
        return -1;

    /* Channel mode (stereo/mono) */
    chmode = (header & 0xc0) >> 6;

    /* calculate position of Xing VBR header */
    if ( version == 1 ) {
        if ( chmode == 3 ) /* mono */
            xing = frame + 17;
        else
            xing = frame + 32;
    }
    else {
        if ( chmode == 3 ) /* mono */
            xing = frame + 9;
        else
            xing = frame + 17;
    }

    if (xing[0] == 'X' &&
        xing[1] == 'i' &&
        xing[2] == 'n' &&
        xing[3] == 'g')
    {
        /* Yes, it is a VBR file */
        entry->vbr = true;
        entry->vbrflags = xing[7];
        
        if (entry->vbrflags & VBR_FRAMES_FLAG) /* Is the frame count there? */
        {
            int framecount = (xing[8] << 24) | (xing[9] << 16) |
                (xing[10] << 8) | xing[11];
 
            filetime = framecount * tpf;
        }

        if (entry->vbrflags & VBR_BYTES_FLAG) /* is byte count there? */
        {
            int bytecount = (xing[12] << 24) | (xing[13] << 16) |
                (xing[14] << 8) | xing[15];
 
            bitrate = bytecount * 8 / filetime;
        }

        if (entry->vbrflags & VBR_TOC_FLAG) /* is table-of-contents there? */
        {
            memcpy( entry->toc, xing+16, 100 );
        }
    }

    entry->bitrate = bitrate;

    /* If the file time hasn't been established, this may be a fixed
       rate MP3, so just use the default formula */
    if(filetime == 0)
    {
        /* 
         * Now song length is 
         * ((filesize)/(bytes per frame))*(time per frame) 
         */
        filetime = entry->filesize/bpf*tpf;
    }

    return filetime;
}


/*
 * Checks all relevant information (such as ID3v1 tag, ID3v2 tag, length etc)
 * about an MP3 file and updates it's entry accordingly.
 *
 * Arguments: entry - the entry to check and update with the new information
 *
 * Returns: void
 */
bool mp3info(struct mp3entry *entry, char *filename) 
{
    int fd;
    fd = open(filename, O_RDONLY);
    if(-1 == fd)
        return true;

    memset(entry, 0, sizeof(struct mp3entry));
   
    strncpy(entry->path, filename, sizeof(entry->path));
 
    entry->title = NULL;
    entry->filesize = getfilesize(fd);
    entry->id3v2len = getid3v2len(fd);
    entry->tracknum = 0;

    if ( entry->id3v2len && entry->id3v2len <= sizeof( entry->id3v2buf ) )
        setid3v2title(fd, entry);
    entry->length = getsonglength(fd, entry);

    entry->id3v1len = getid3v1len(fd);
    if(entry->id3v1len && !entry->title)
        setid3v1title(fd, entry);

    close(fd);

    return false;
}

#ifdef DEBUG_STANDALONE

char *secs2str(int ms)
{
    static char buffer[32];
    int secs = ms/1000;
    ms %= 1000;
    snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100);
    return buffer;
}

int main(int argc, char **argv)
{
    int i;
    for(i=1; i<argc; i++) {
        struct mp3entry mp3;
        if(mp3info(&mp3, argv[i])) {
            printf("Failed to get %s\n", argv[i]);
            return 0;
        }

        printf("****** File: %s\n"
               "      Title: %s\n"
               "     Artist: %s\n"
               "      Album: %s\n"
               "     Length: %s / %d s\n"
               "    Bitrate: %d\n"
               "  Frequency: %d\n",
               argv[i],
               mp3.title?mp3.title:"<blank>",
               mp3.artist?mp3.artist:"<blank>",
               mp3.album?mp3.album:"<blank>",
               secs2str(mp3.length),
               mp3.length/1000,
               mp3.bitrate,
               mp3.frequency);
    }
    
    return 0;
}

#endif

/* -----------------------------------------------------------------
 * local variables:
 * eval: (load-file "rockbox-mode.el")
 * end:
 */