fix some small glitches for backup:
[Rockbox.git] / apps / metadata / a52.c
blobf427d99c7ba7580df48961431831cc3773fcc5d8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "id3.h"
21 #include "logf.h"
23 #include "metadata_parsers.h"
25 static const unsigned short a52_bitrates[] =
27 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
28 192, 224, 256, 320, 384, 448, 512, 576, 640
31 /* Only store frame sizes for 44.1KHz - others are simply multiples
32 of the bitrate */
33 static const unsigned short a52_441framesizes[] =
35 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
36 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
37 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
38 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
39 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
40 1254 * 2, 1393 * 2, 1394 * 2
43 bool get_a52_metadata(int fd, struct mp3entry *id3)
45 /* Use the trackname part of the id3 structure as a temporary buffer */
46 unsigned char* buf = (unsigned char *)id3->path;
47 unsigned long totalsamples;
48 int i;
50 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5))
52 return false;
55 if ((buf[0] != 0x0b) || (buf[1] != 0x77))
57 logf("not an A52/AC3 file\n");
58 return false;
61 i = buf[4] & 0x3e;
63 if (i > 36)
65 logf("A52: Invalid frmsizecod: %d\n",i);
66 return false;
69 id3->bitrate = a52_bitrates[i >> 1];
70 id3->vbr = false;
71 id3->filesize = filesize(fd);
73 switch (buf[4] & 0xc0)
75 case 0x00:
76 id3->frequency = 48000;
77 id3->bytesperframe=id3->bitrate * 2 * 2;
78 break;
80 case 0x40:
81 id3->frequency = 44100;
82 id3->bytesperframe = a52_441framesizes[i];
83 break;
85 case 0x80:
86 id3->frequency = 32000;
87 id3->bytesperframe = id3->bitrate * 3 * 2;
88 break;
90 default:
91 logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0);
92 return false;
93 break;
96 /* One A52 frame contains 6 blocks, each containing 256 samples */
97 totalsamples = id3->filesize / id3->bytesperframe * 6 * 256;
98 id3->length = totalsamples / id3->frequency * 1000;
99 return true;