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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Parsing ADTS and ADIF headers
*
* Written by Igor B. Poretsky
*
* This program 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "platform.h"
#include "metadata.h"
#include "metadata_common.h"
#include "metadata_parsers.h"
static const int sample_rates[] =
{
96000, 88200, 64000, 48000,
44100, 32000, 24000, 22050,
16000, 12000, 11025, 8000,
7350, 0, 0, 0
};
static bool check_adts_syncword(int fd)
{
uint16_t syncword = 0;
read_uint16be(fd, &syncword);
return (syncword & 0xFFF6) == 0xFFF0;
}
static bool find_adts_keyword(int fd, struct mp3entry *entry)
{
/* logic is copied from adts_fixed_header libfaad/syntax.c:
* try to recover from sync errors */
for (int i = 0; i < 768; ++i)
{
if (-1 == lseek(fd, entry->first_frame_offset + i, SEEK_SET))
return false;
if (check_adts_syncword(fd))
{
return true;
}
}
return false;
}
bool get_aac_metadata(int fd, struct mp3entry *entry)
{
unsigned char buf[5];
entry->title = NULL;
entry->tracknum = 0;
entry->discnum = 0;
entry->id3v1len = 0;
entry->id3v2len = getid3v2len(fd);
entry->first_frame_offset = entry->id3v2len;
entry->filesize = filesize(fd) - entry->first_frame_offset;
entry->needs_upsampling_correction = false;
if (entry->id3v2len)
setid3v2title(fd, entry);
if (-1 == lseek(fd, entry->first_frame_offset, SEEK_SET))
return false;
if (read(fd, buf, 5) != 5)
return false;
if (!memcmp(buf, "ADIF", 4))
{
if (-1 == lseek(fd, (buf[4] & 0x80) ? (entry->first_frame_offset + 9) : entry->first_frame_offset, SEEK_SET))
return false;
uint32_t bitrate;
read_uint32be(fd, &bitrate);
entry->vbr = (bitrate & 0x10000000) != 0;
entry->bitrate = ((bitrate & 0xFFFFFE0) + 16000) / 32000;
read_uint32be(fd, (uint32_t*)(&(entry->frequency)));
entry->frequency = sample_rates[(entry->frequency >> (entry->vbr ? 23 : 3)) & 0x0F];
}
else if (find_adts_keyword(fd, entry))
{
int frames;
int stat_length;
uint64_t total;
if (read(fd, buf, 5) != 5)
return false;
entry->frequency = sample_rates[(buf[0] >> 2) & 0x0F];
entry->vbr = ((buf[3] & 0x1F) == 0x1F)
&& ((buf[4] & 0xFC) == 0xFC);
stat_length = entry->frequency >> ((entry->vbr) ? 5 : 7);
for (frames = 1, total = 0; frames < stat_length; frames++)
{
unsigned int frame_length = (((unsigned int)buf[1] & 0x3) << 11)
| ((unsigned int)buf[2] << 3)
| ((unsigned int)buf[3] >> 5);
total += frame_length;
if (frame_length < 7)
break;
if (-1 == lseek(fd, frame_length - 7, SEEK_CUR))
break;
if (!check_adts_syncword(fd))
break;
if (read(fd, buf, 5) != 5)
break;
}
entry->bitrate = (unsigned int)((total * entry->frequency / frames + 64000) / 128000);
#ifdef CODEC_AAC_SBR_DEC
if (entry->frequency <= 24000)
{
entry->frequency <<= 1;
entry->needs_upsampling_correction = true;
}
#endif
}
else
{
return false;
}
entry->length = (unsigned long)((entry->filesize * 8LL + (entry->bitrate >> 1)) / entry->bitrate);
return true;
}
|