diff options
Diffstat (limited to 'lib/rbcodec/metadata/a52.c')
-rw-r--r-- | lib/rbcodec/metadata/a52.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/rbcodec/metadata/a52.c b/lib/rbcodec/metadata/a52.c new file mode 100644 index 0000000000..a8aad3fa4f --- /dev/null +++ b/lib/rbcodec/metadata/a52.c | |||
@@ -0,0 +1,103 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Dave Chapman | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include <stdio.h> | ||
23 | #include "metadata.h" | ||
24 | #include "logf.h" | ||
25 | |||
26 | #include "metadata_parsers.h" | ||
27 | |||
28 | static const unsigned short a52_bitrates[] = | ||
29 | { | ||
30 | 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, | ||
31 | 192, 224, 256, 320, 384, 448, 512, 576, 640 | ||
32 | }; | ||
33 | |||
34 | /* Only store frame sizes for 44.1KHz - others are simply multiples | ||
35 | of the bitrate */ | ||
36 | static const unsigned short a52_441framesizes[] = | ||
37 | { | ||
38 | 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2, | ||
39 | 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2, | ||
40 | 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2, | ||
41 | 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2, | ||
42 | 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2, | ||
43 | 1254 * 2, 1393 * 2, 1394 * 2 | ||
44 | }; | ||
45 | |||
46 | bool get_a52_metadata(int fd, struct mp3entry *id3) | ||
47 | { | ||
48 | /* Use the trackname part of the id3 structure as a temporary buffer */ | ||
49 | unsigned char* buf = (unsigned char *)id3->path; | ||
50 | unsigned long totalsamples; | ||
51 | int i; | ||
52 | |||
53 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5)) | ||
54 | { | ||
55 | return false; | ||
56 | } | ||
57 | |||
58 | if ((buf[0] != 0x0b) || (buf[1] != 0x77)) | ||
59 | { | ||
60 | logf("not an A52/AC3 file\n"); | ||
61 | return false; | ||
62 | } | ||
63 | |||
64 | i = buf[4] & 0x3e; | ||
65 | |||
66 | if (i > 36) | ||
67 | { | ||
68 | logf("A52: Invalid frmsizecod: %d\n",i); | ||
69 | return false; | ||
70 | } | ||
71 | |||
72 | id3->bitrate = a52_bitrates[i >> 1]; | ||
73 | id3->vbr = false; | ||
74 | id3->filesize = filesize(fd); | ||
75 | |||
76 | switch (buf[4] & 0xc0) | ||
77 | { | ||
78 | case 0x00: | ||
79 | id3->frequency = 48000; | ||
80 | id3->bytesperframe=id3->bitrate * 2 * 2; | ||
81 | break; | ||
82 | |||
83 | case 0x40: | ||
84 | id3->frequency = 44100; | ||
85 | id3->bytesperframe = a52_441framesizes[i]; | ||
86 | break; | ||
87 | |||
88 | case 0x80: | ||
89 | id3->frequency = 32000; | ||
90 | id3->bytesperframe = id3->bitrate * 3 * 2; | ||
91 | break; | ||
92 | |||
93 | default: | ||
94 | logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0); | ||
95 | return false; | ||
96 | break; | ||
97 | } | ||
98 | |||
99 | /* One A52 frame contains 6 blocks, each containing 256 samples */ | ||
100 | totalsamples = id3->filesize / id3->bytesperframe * 6 * 256; | ||
101 | id3->length = totalsamples / id3->frequency * 1000; | ||
102 | return true; | ||
103 | } | ||