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.3 2003/12/06 04:24:17 rjamorim Exp $
28
#define WIN32_LEAN_AND_MEAN
36
#define ADIF_MAX_SIZE 30 /* Should be enough */
37
#define ADTS_MAX_SIZE 10 /* Should be enough */
39
static int sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000};
41
static int read_ADIF_header(FILE *file, faadAACInfo *info)
44
unsigned char buffer[ADIF_MAX_SIZE];
48
/* Get ADIF header data */
51
if (fread(buffer, 1, ADIF_MAX_SIZE, file) != ADIF_MAX_SIZE)
54
/* copyright string */
56
skip_size += 9; /* skip 9 bytes */
58
bitstream = buffer[0 + skip_size] & 0x10;
59
info->bitrate = ((unsigned int)(buffer[0 + skip_size] & 0x0F)<<19)|
60
((unsigned int)buffer[1 + skip_size]<<11)|
61
((unsigned int)buffer[2 + skip_size]<<3)|
62
((unsigned int)buffer[3 + skip_size] & 0xE0);
66
info->object_type = ((buffer[6 + skip_size]&0x01)<<1)|((buffer[7 + skip_size]&0x80)>>7);
67
sf_idx = (buffer[7 + skip_size]&0x78)>>3;
69
info->object_type = (buffer[4 + skip_size] & 0x18)>>3;
70
sf_idx = ((buffer[4 + skip_size] & 0x07)<<1)|((buffer[5 + skip_size] & 0x80)>>7);
72
info->sampling_rate = sample_rates[sf_idx];
77
static int read_ADTS_header(FILE *file, faadAACInfo *info)
79
/* Get ADTS header data */
80
unsigned char buffer[ADTS_MAX_SIZE];
81
int frames, t_framelength = 0, frame_length, sr_idx = 0, ID;
83
float frames_per_sec = 0;
85
unsigned long *tmp_seek_table = NULL;
89
/* Read all frames to ensure correct time and bitrate */
90
for (frames = 0; /* */; frames++)
92
bytes = fread(buffer, 1, ADTS_MAX_SIZE, file);
94
if (bytes != ADTS_MAX_SIZE)
98
if (!((buffer[0] == 0xFF)&&((buffer[1] & 0xF6) == 0xF0)))
103
/* fixed ADTS header is the same for every frame, so we read it only once */
104
/* Syncword found, proceed to read in the fixed ADTS header */
105
ID = buffer[1] & 0x08;
106
info->object_type = (buffer[2]&0xC0)>>6;
107
sr_idx = (buffer[2]&0x3C)>>2;
108
info->channels = ((buffer[2]&0x01)<<2)|((buffer[3]&0xC0)>>6);
110
frames_per_sec = sample_rates[sr_idx] / 1024.f;
113
/* ...and the variable ADTS header */
117
} else { /* MPEG-2 */
120
frame_length = ((((unsigned int)buffer[3] & 0x3)) << 11)
121
| (((unsigned int)buffer[4]) << 3) | (buffer[5] >> 5);
123
t_framelength += frame_length;
125
pos = ftell(file) - ADTS_MAX_SIZE;
127
fseek(file, frame_length - ADTS_MAX_SIZE, SEEK_CUR);
132
float sec_per_frame, bytes_per_frame;
133
info->sampling_rate = sample_rates[sr_idx];
134
sec_per_frame = (float)info->sampling_rate/1024.0;
135
bytes_per_frame = (float)t_framelength / (float)frames;
136
info->bitrate = 8 * (int)floor(bytes_per_frame * sec_per_frame);
137
info->length = (int)floor((float)frames/frames_per_sec)*1000;
139
info->sampling_rate = 4;
140
info->bitrate = 128000;
148
int get_AAC_format(char *filename, faadAACInfo *info)
150
unsigned long tagsize;
153
unsigned long file_len;
154
unsigned char adxx_id[5];
157
memset(info, 0, sizeof(faadAACInfo));
159
file = fopen(filename, "rb");
164
fseek(file, 0, SEEK_END);
165
file_len = ftell(file);
166
fseek(file, 0, SEEK_SET);
168
/* Skip the tag, if it's there */
169
tmp = fread(buffer, 10, 1, file);
171
if (StringComp(buffer, "ID3", 3) == 0)
173
/* high bit is not used */
174
tagsize = (buffer[6] << 21) | (buffer[7] << 14) |
175
(buffer[8] << 7) | (buffer[9] << 0);
177
fseek(file, tagsize, SEEK_CUR);
181
fseek(file, 0, SEEK_SET);
187
tmp = fread(adxx_id, 2, 1, file);
191
/* Determine the header type of the file, check the first two bytes */
192
if (StringComp(adxx_id, "AD", 2) == 0)
194
/* We think its an ADIF header, but check the rest just to make sure */
195
tmp = fread(adxx_id + 2, 2, 1, file);
197
if (StringComp(adxx_id, "ADIF", 4) == 0)
199
read_ADIF_header(file, info);
201
info->length = (int)((float)file_len*8000.0/((float)info->bitrate));
204
/* No ADIF, check for ADTS header */
205
if ((adxx_id[0] == 0xFF)&&((adxx_id[1] & 0xF6) == 0xF0))
207
/* ADTS header located */
208
fseek(file, tagsize, SEEK_SET);
209
read_ADTS_header(file, info);
211
/* Unknown/headerless AAC file, assume format: */
213
info->bitrate = 128000;
214
info->sampling_rate = 44100;
216
info->headertype = 0;
217
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.3 2003/12/06 04:24:17 rjamorim Exp $
28
#define WIN32_LEAN_AND_MEAN
36
#define ADIF_MAX_SIZE 30 /* Should be enough */
37
#define ADTS_MAX_SIZE 10 /* Should be enough */
39
static int sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000};
41
static int read_ADIF_header(FILE *file, faadAACInfo *info)
44
unsigned char buffer[ADIF_MAX_SIZE];
48
/* Get ADIF header data */
51
if (fread(buffer, 1, ADIF_MAX_SIZE, file) != ADIF_MAX_SIZE)
54
/* copyright string */
56
skip_size += 9; /* skip 9 bytes */
58
bitstream = buffer[0 + skip_size] & 0x10;
59
info->bitrate = ((unsigned int)(buffer[0 + skip_size] & 0x0F)<<19)|
60
((unsigned int)buffer[1 + skip_size]<<11)|
61
((unsigned int)buffer[2 + skip_size]<<3)|
62
((unsigned int)buffer[3 + skip_size] & 0xE0);
66
info->object_type = ((buffer[6 + skip_size]&0x01)<<1)|((buffer[7 + skip_size]&0x80)>>7);
67
sf_idx = (buffer[7 + skip_size]&0x78)>>3;
69
info->object_type = (buffer[4 + skip_size] & 0x18)>>3;
70
sf_idx = ((buffer[4 + skip_size] & 0x07)<<1)|((buffer[5 + skip_size] & 0x80)>>7);
72
info->sampling_rate = sample_rates[sf_idx];
77
static int read_ADTS_header(FILE *file, faadAACInfo *info)
79
/* Get ADTS header data */
80
unsigned char buffer[ADTS_MAX_SIZE];
81
int frames, t_framelength = 0, frame_length, sr_idx = 0, ID;
83
float frames_per_sec = 0;
85
unsigned long *tmp_seek_table = NULL;
89
/* Read all frames to ensure correct time and bitrate */
90
for (frames = 0; /* */; frames++)
92
bytes = fread(buffer, 1, ADTS_MAX_SIZE, file);
94
if (bytes != ADTS_MAX_SIZE)
98
if (!((buffer[0] == 0xFF)&&((buffer[1] & 0xF6) == 0xF0)))
103
/* fixed ADTS header is the same for every frame, so we read it only once */
104
/* Syncword found, proceed to read in the fixed ADTS header */
105
ID = buffer[1] & 0x08;
106
info->object_type = (buffer[2]&0xC0)>>6;
107
sr_idx = (buffer[2]&0x3C)>>2;
108
info->channels = ((buffer[2]&0x01)<<2)|((buffer[3]&0xC0)>>6);
110
frames_per_sec = sample_rates[sr_idx] / 1024.f;
113
/* ...and the variable ADTS header */
117
} else { /* MPEG-2 */
120
frame_length = ((((unsigned int)buffer[3] & 0x3)) << 11)
121
| (((unsigned int)buffer[4]) << 3) | (buffer[5] >> 5);
123
t_framelength += frame_length;
125
pos = ftell(file) - ADTS_MAX_SIZE;
127
fseek(file, frame_length - ADTS_MAX_SIZE, SEEK_CUR);
132
float sec_per_frame, bytes_per_frame;
133
info->sampling_rate = sample_rates[sr_idx];
134
sec_per_frame = (float)info->sampling_rate/1024.0;
135
bytes_per_frame = (float)t_framelength / (float)frames;
136
info->bitrate = 8 * (int)floor(bytes_per_frame * sec_per_frame);
137
info->length = (int)floor((float)frames/frames_per_sec)*1000;
139
info->sampling_rate = 4;
140
info->bitrate = 128000;
148
int get_AAC_format(char *filename, faadAACInfo *info)
150
unsigned long tagsize;
153
unsigned long file_len;
154
unsigned char adxx_id[5];
157
memset(info, 0, sizeof(faadAACInfo));
159
file = fopen(filename, "rb");
164
fseek(file, 0, SEEK_END);
165
file_len = ftell(file);
166
fseek(file, 0, SEEK_SET);
168
/* Skip the tag, if it's there */
169
tmp = fread(buffer, 10, 1, file);
171
if (StringComp(buffer, "ID3", 3) == 0)
173
/* high bit is not used */
174
tagsize = (buffer[6] << 21) | (buffer[7] << 14) |
175
(buffer[8] << 7) | (buffer[9] << 0);
177
fseek(file, tagsize, SEEK_CUR);
181
fseek(file, 0, SEEK_SET);
187
tmp = fread(adxx_id, 2, 1, file);
191
/* Determine the header type of the file, check the first two bytes */
192
if (StringComp(adxx_id, "AD", 2) == 0)
194
/* We think its an ADIF header, but check the rest just to make sure */
195
tmp = fread(adxx_id + 2, 2, 1, file);
197
if (StringComp(adxx_id, "ADIF", 4) == 0)
199
read_ADIF_header(file, info);
201
info->length = (int)((float)file_len*8000.0/((float)info->bitrate));
204
/* No ADIF, check for ADTS header */
205
if ((adxx_id[0] == 0xFF)&&((adxx_id[1] & 0xF6) == 0xF0))
207
/* ADTS header located */
208
fseek(file, tagsize, SEEK_SET);
209
read_ADTS_header(file, info);
211
/* Unknown/headerless AAC file, assume format: */
213
info->bitrate = 128000;
214
info->sampling_rate = 44100;
216
info->headertype = 0;
217
info->object_type = 1;