2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
** GNU General Public License for more details.
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
** Any non-GPL usage of this software or parts of this software is strictly
22
** Commercial non-GPL licensing of this software is possible.
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25
** $Id: aacinfo.c,v 1.6 2004/10/18 19:25:00 menno Exp $
28
#define WIN32_LEAN_AND_MEAN
37
#define ADIF_MAX_SIZE 30 /* Should be enough */
38
#define ADTS_MAX_SIZE 10 /* Should be enough */
40
static int sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000};
42
static int read_ADIF_header(FILE *file, faadAACInfo *info)
45
unsigned char buffer[ADIF_MAX_SIZE];
49
/* Get ADIF header data */
52
if (fread(buffer, 1, ADIF_MAX_SIZE, file) != ADIF_MAX_SIZE)
55
/* copyright string */
57
skip_size += 9; /* skip 9 bytes */
59
bitstream = buffer[0 + skip_size] & 0x10;
60
info->bitrate = ((unsigned int)(buffer[0 + skip_size] & 0x0F)<<19)|
61
((unsigned int)buffer[1 + skip_size]<<11)|
62
((unsigned int)buffer[2 + skip_size]<<3)|
63
((unsigned int)buffer[3 + skip_size] & 0xE0);
67
info->object_type = ((buffer[6 + skip_size]&0x01)<<1)|((buffer[7 + skip_size]&0x80)>>7);
68
sf_idx = (buffer[7 + skip_size]&0x78)>>3;
70
info->object_type = (buffer[4 + skip_size] & 0x18)>>3;
71
sf_idx = ((buffer[4 + skip_size] & 0x07)<<1)|((buffer[5 + skip_size] & 0x80)>>7);
73
info->sampling_rate = sample_rates[sf_idx];
78
static int read_ADTS_header(FILE *file, faadAACInfo *info)
80
/* Get ADTS header data */
81
unsigned char buffer[ADTS_MAX_SIZE];
82
int frames, t_framelength = 0, frame_length, sr_idx = 0, ID;
84
float frames_per_sec = 0;
86
unsigned long *tmp_seek_table = NULL;
90
/* Read all frames to ensure correct time and bitrate */
91
for (frames = 0; /* */; frames++)
93
bytes = fread(buffer, 1, ADTS_MAX_SIZE, file);
95
if (bytes != ADTS_MAX_SIZE)
99
if (!((buffer[0] == 0xFF)&&((buffer[1] & 0xF6) == 0xF0)))
104
/* fixed ADTS header is the same for every frame, so we read it only once */
105
/* Syncword found, proceed to read in the fixed ADTS header */
106
ID = buffer[1] & 0x08;
107
info->object_type = (buffer[2]&0xC0)>>6;
108
sr_idx = (buffer[2]&0x3C)>>2;
109
info->channels = ((buffer[2]&0x01)<<2)|((buffer[3]&0xC0)>>6);
111
frames_per_sec = sample_rates[sr_idx] / 1024.f;
114
/* ...and the variable ADTS header */
118
} else { /* MPEG-2 */
121
frame_length = ((((unsigned int)buffer[3] & 0x3)) << 11)
122
| (((unsigned int)buffer[4]) << 3) | (buffer[5] >> 5);
124
t_framelength += frame_length;
126
pos = ftell(file) - ADTS_MAX_SIZE;
128
fseek(file, frame_length - ADTS_MAX_SIZE, SEEK_CUR);
133
float sec_per_frame, bytes_per_frame;
134
info->sampling_rate = sample_rates[sr_idx];
135
sec_per_frame = (float)info->sampling_rate/1024.0;
136
bytes_per_frame = (float)t_framelength / (float)frames;
137
info->bitrate = 8 * (int)floor(bytes_per_frame * sec_per_frame);
138
info->length = (int)floor((float)frames/frames_per_sec)*1000;
140
info->sampling_rate = 4;
141
info->bitrate = 128000;
149
int get_AAC_format(char *filename, faadAACInfo *info)
151
unsigned long tagsize;
154
unsigned long file_len;
155
unsigned char adxx_id[5];
158
memset(info, 0, sizeof(faadAACInfo));
160
file = fopen(filename, "rb");
165
fseek(file, 0, SEEK_END);
166
file_len = ftell(file);
167
fseek(file, 0, SEEK_SET);
169
/* Skip the tag, if it's there */
170
tmp = fread(buffer, 10, 1, file);
172
if (StringComp(buffer, "ID3", 3) == 0)
174
/* high bit is not used */
175
tagsize = (buffer[6] << 21) | (buffer[7] << 14) |
176
(buffer[8] << 7) | (buffer[9] << 0);
178
fseek(file, tagsize, SEEK_CUR);
182
fseek(file, 0, SEEK_SET);
188
tmp = fread(adxx_id, 2, 1, file);
192
/* Determine the header type of the file, check the first two bytes */
193
if (StringComp(adxx_id, "AD", 2) == 0)
195
/* We think its an ADIF header, but check the rest just to make sure */
196
tmp = fread(adxx_id + 2, 2, 1, file);
198
if (StringComp(adxx_id, "ADIF", 4) == 0)
200
read_ADIF_header(file, info);
202
info->length = (int)((float)file_len*8000.0/((float)info->bitrate));
205
/* No ADIF, check for ADTS header */
206
if ((adxx_id[0] == 0xFF)&&((adxx_id[1] & 0xF6) == 0xF0))
208
/* ADTS header located */
209
fseek(file, tagsize, SEEK_SET);
210
read_ADTS_header(file, info);
212
/* Unknown/headerless AAC file, assume format: */
214
info->bitrate = 128000;
215
info->sampling_rate = 44100;
217
info->headertype = 0;
218
info->object_type = 1;
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
** GNU General Public License for more details.
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
** Any non-GPL usage of this software or parts of this software is strictly
22
** Commercial non-GPL licensing of this software is possible.
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25
** $Id: aacinfo.c,v 1.6 2004/10/18 19:25:00 menno Exp $
28
#define WIN32_LEAN_AND_MEAN
37
#define ADIF_MAX_SIZE 30 /* Should be enough */
38
#define ADTS_MAX_SIZE 10 /* Should be enough */
40
static int sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000};
42
static int read_ADIF_header(FILE *file, faadAACInfo *info)
45
unsigned char buffer[ADIF_MAX_SIZE];
49
/* Get ADIF header data */
52
if (fread(buffer, 1, ADIF_MAX_SIZE, file) != ADIF_MAX_SIZE)
55
/* copyright string */
57
skip_size += 9; /* skip 9 bytes */
59
bitstream = buffer[0 + skip_size] & 0x10;
60
info->bitrate = ((unsigned int)(buffer[0 + skip_size] & 0x0F)<<19)|
61
((unsigned int)buffer[1 + skip_size]<<11)|
62
((unsigned int)buffer[2 + skip_size]<<3)|
63
((unsigned int)buffer[3 + skip_size] & 0xE0);
67
info->object_type = ((buffer[6 + skip_size]&0x01)<<1)|((buffer[7 + skip_size]&0x80)>>7);
68
sf_idx = (buffer[7 + skip_size]&0x78)>>3;
70
info->object_type = (buffer[4 + skip_size] & 0x18)>>3;
71
sf_idx = ((buffer[4 + skip_size] & 0x07)<<1)|((buffer[5 + skip_size] & 0x80)>>7);
73
info->sampling_rate = sample_rates[sf_idx];
78
static int read_ADTS_header(FILE *file, faadAACInfo *info)
80
/* Get ADTS header data */
81
unsigned char buffer[ADTS_MAX_SIZE];
82
int frames, t_framelength = 0, frame_length, sr_idx = 0, ID;
84
float frames_per_sec = 0;
86
unsigned long *tmp_seek_table = NULL;
90
/* Read all frames to ensure correct time and bitrate */
91
for (frames = 0; /* */; frames++)
93
bytes = fread(buffer, 1, ADTS_MAX_SIZE, file);
95
if (bytes != ADTS_MAX_SIZE)
99
if (!((buffer[0] == 0xFF)&&((buffer[1] & 0xF6) == 0xF0)))
104
/* fixed ADTS header is the same for every frame, so we read it only once */
105
/* Syncword found, proceed to read in the fixed ADTS header */
106
ID = buffer[1] & 0x08;
107
info->object_type = (buffer[2]&0xC0)>>6;
108
sr_idx = (buffer[2]&0x3C)>>2;
109
info->channels = ((buffer[2]&0x01)<<2)|((buffer[3]&0xC0)>>6);
111
frames_per_sec = sample_rates[sr_idx] / 1024.f;
114
/* ...and the variable ADTS header */
118
} else { /* MPEG-2 */
121
frame_length = ((((unsigned int)buffer[3] & 0x3)) << 11)
122
| (((unsigned int)buffer[4]) << 3) | (buffer[5] >> 5);
124
t_framelength += frame_length;
126
pos = ftell(file) - ADTS_MAX_SIZE;
128
fseek(file, frame_length - ADTS_MAX_SIZE, SEEK_CUR);
133
float sec_per_frame, bytes_per_frame;
134
info->sampling_rate = sample_rates[sr_idx];
135
sec_per_frame = (float)info->sampling_rate/1024.0;
136
bytes_per_frame = (float)t_framelength / (float)frames;
137
info->bitrate = 8 * (int)floor(bytes_per_frame * sec_per_frame);
138
info->length = (int)floor((float)frames/frames_per_sec)*1000;
140
info->sampling_rate = 4;
141
info->bitrate = 128000;
149
int get_AAC_format(char *filename, faadAACInfo *info)
151
unsigned long tagsize;
154
unsigned long file_len;
155
unsigned char adxx_id[5];
158
memset(info, 0, sizeof(faadAACInfo));
160
file = fopen(filename, "rb");
165
fseek(file, 0, SEEK_END);
166
file_len = ftell(file);
167
fseek(file, 0, SEEK_SET);
169
/* Skip the tag, if it's there */
170
tmp = fread(buffer, 10, 1, file);
172
if (StringComp(buffer, "ID3", 3) == 0)
174
/* high bit is not used */
175
tagsize = (buffer[6] << 21) | (buffer[7] << 14) |
176
(buffer[8] << 7) | (buffer[9] << 0);
178
fseek(file, tagsize, SEEK_CUR);
182
fseek(file, 0, SEEK_SET);
188
tmp = fread(adxx_id, 2, 1, file);
192
/* Determine the header type of the file, check the first two bytes */
193
if (StringComp(adxx_id, "AD", 2) == 0)
195
/* We think its an ADIF header, but check the rest just to make sure */
196
tmp = fread(adxx_id + 2, 2, 1, file);
198
if (StringComp(adxx_id, "ADIF", 4) == 0)
200
read_ADIF_header(file, info);
202
info->length = (int)((float)file_len*8000.0/((float)info->bitrate));
205
/* No ADIF, check for ADTS header */
206
if ((adxx_id[0] == 0xFF)&&((adxx_id[1] & 0xF6) == 0xF0))
208
/* ADTS header located */
209
fseek(file, tagsize, SEEK_SET);
210
read_ADTS_header(file, info);
212
/* Unknown/headerless AAC file, assume format: */
214
info->bitrate = 128000;
215
info->sampling_rate = 44100;
217
info->headertype = 0;
218
info->object_type = 1;