| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "aac.h" |
| 18 |
|
| 19 |
static int |
| 20 |
get_aacinfo(PerlIO *infile, char *file, HV *info, HV *tags) |
| 21 |
{ |
| 22 |
off_t file_size; |
| 23 |
Buffer buf; |
| 24 |
unsigned char *bptr; |
| 25 |
int err = 0; |
| 26 |
unsigned int id3_size = 0; |
| 27 |
unsigned int audio_offset = 0; |
| 28 |
|
| 29 |
buffer_init(&buf, AAC_BLOCK_SIZE); |
| 30 |
|
| 31 |
file_size = _file_size(infile); |
| 32 |
|
| 33 |
my_hv_store( info, "file_size", newSVuv(file_size) ); |
| 34 |
|
| 35 |
if ( !_check_buf(infile, &buf, 10, AAC_BLOCK_SIZE) ) { |
| 36 |
err = -1; |
| 37 |
goto out; |
| 38 |
} |
| 39 |
|
| 40 |
bptr = buffer_ptr(&buf); |
| 41 |
|
| 42 |
|
| 43 |
if ( |
| 44 |
(bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') && |
| 45 |
bptr[3] < 0xff && bptr[4] < 0xff && |
| 46 |
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 |
| 47 |
) { |
| 48 |
|
| 49 |
id3_size = 10 + (bptr[6]<<21) + (bptr[7]<<14) + (bptr[8]<<7) + bptr[9]; |
| 50 |
|
| 51 |
if (bptr[5] & 0x10) { |
| 52 |
|
| 53 |
id3_size += 10; |
| 54 |
} |
| 55 |
|
| 56 |
audio_offset += id3_size; |
| 57 |
|
| 58 |
DEBUG_TRACE("Found ID3 tag of size %d\n", id3_size); |
| 59 |
|
| 60 |
|
| 61 |
buffer_clear(&buf); |
| 62 |
PerlIO_seek(infile, id3_size, SEEK_SET); |
| 63 |
|
| 64 |
|
| 65 |
if ( !_check_buf(infile, &buf, 10, AAC_BLOCK_SIZE) ) { |
| 66 |
err = -1; |
| 67 |
goto out; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
while ( buffer_len(&buf) >= 6 ) { |
| 73 |
bptr = buffer_ptr(&buf); |
| 74 |
|
| 75 |
if ( (bptr[0] == 0xFF) && ((bptr[1] & 0xF6) == 0xF0) |
| 76 |
&& aac_parse_adts(infile, file, file_size - audio_offset, &buf, info)) |
| 77 |
{ |
| 78 |
break; |
| 79 |
} |
| 80 |
else { |
| 81 |
buffer_consume(&buf, 1); |
| 82 |
audio_offset++; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
my_hv_store( info, "audio_offset", newSVuv(audio_offset) ); |
| 94 |
my_hv_store( info, "audio_size", newSVuv(file_size - audio_offset) ); |
| 95 |
|
| 96 |
|
| 97 |
if (id3_size) { |
| 98 |
parse_id3(infile, file, info, tags, 0, file_size); |
| 99 |
} |
| 100 |
|
| 101 |
out: |
| 102 |
buffer_free(&buf); |
| 103 |
|
| 104 |
if (err) return err; |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
int |
| 112 |
aac_parse_adts(PerlIO *infile, char *file, off_t audio_size, Buffer *buf, HV *info) |
| 113 |
{ |
| 114 |
int frames, frame_length; |
| 115 |
int t_framelength = 0; |
| 116 |
int samplerate = 0; |
| 117 |
int bitrate; |
| 118 |
uint8_t profile = 0; |
| 119 |
uint8_t channels = 0; |
| 120 |
float frames_per_sec, bytes_per_frame, length; |
| 121 |
|
| 122 |
unsigned char *bptr; |
| 123 |
|
| 124 |
|
| 125 |
for (frames = 1; ; frames++) { |
| 126 |
if ( !_check_buf(infile, buf, audio_size > AAC_BLOCK_SIZE ? AAC_BLOCK_SIZE : audio_size, AAC_BLOCK_SIZE) ) { |
| 127 |
if (frames < 2) |
| 128 |
return 0; |
| 129 |
else |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
bptr = buffer_ptr(buf); |
| 134 |
|
| 135 |
|
| 136 |
if (!((bptr[0] == 0xFF)&&((bptr[1] & 0xF6) == 0xF0))) |
| 137 |
break; |
| 138 |
|
| 139 |
if (frames == 1) { |
| 140 |
profile = (bptr[2] & 0xc0) >> 6; |
| 141 |
samplerate = adts_sample_rates[(bptr[2]&0x3c)>>2]; |
| 142 |
channels = ((bptr[2] & 0x1) << 2) | ((bptr[3] & 0xc0) >> 6); |
| 143 |
} |
| 144 |
|
| 145 |
frame_length = ((((unsigned int)bptr[3] & 0x3)) << 11) |
| 146 |
| (((unsigned int)bptr[4]) << 3) | (bptr[5] >> 5); |
| 147 |
|
| 148 |
if (frames == 1 && _check_buf(infile, buf, frame_length + 10, AAC_BLOCK_SIZE)) { |
| 149 |
unsigned char *bptr2 = (unsigned char *)buffer_ptr(buf) + frame_length; |
| 150 |
int frame_length2; |
| 151 |
if (!((bptr2[0] == 0xFF)&&((bptr2[1] & 0xF6) == 0xF0)) |
| 152 |
|| profile != (bptr2[2] & 0xc0) >> 6 |
| 153 |
|| samplerate != adts_sample_rates[(bptr2[2]&0x3c)>>2] |
| 154 |
|| channels != (((bptr2[2] & 0x1) << 2) | ((bptr2[3] & 0xc0) >> 6))) |
| 155 |
{ |
| 156 |
DEBUG_TRACE("False sync at frame %d+1\n", frames); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
frame_length2 = ((((unsigned int)bptr2[3] & 0x3)) << 11) |
| 161 |
| (((unsigned int)bptr2[4]) << 3) | (bptr2[5] >> 5); |
| 162 |
|
| 163 |
if (_check_buf(infile, buf, frame_length + frame_length2 + 10, AAC_BLOCK_SIZE)) { |
| 164 |
bptr2 = (unsigned char *)buffer_ptr(buf) + frame_length + frame_length2; |
| 165 |
if (!((bptr2[0] == 0xFF)&&((bptr2[1] & 0xF6) == 0xF0)) |
| 166 |
|| profile != (bptr2[2] & 0xc0) >> 6 |
| 167 |
|| samplerate != adts_sample_rates[(bptr2[2]&0x3c)>>2] |
| 168 |
|| channels != (((bptr2[2] & 0x1) << 2) | ((bptr2[3] & 0xc0) >> 6))) |
| 169 |
{ |
| 170 |
DEBUG_TRACE("False sync at frame %d+2\n", frames); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
t_framelength += frame_length; |
| 177 |
|
| 178 |
if (frame_length > buffer_len(buf)) |
| 179 |
break; |
| 180 |
|
| 181 |
buffer_consume(buf, frame_length); |
| 182 |
audio_size -= frame_length; |
| 183 |
|
| 184 |
|
| 185 |
if (audio_size < 6) |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
if (frames < 2) { |
| 190 |
DEBUG_TRACE("False sync\n"); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
frames_per_sec = (float)samplerate/1024.0f; |
| 195 |
if (frames != 0) |
| 196 |
bytes_per_frame = (float)t_framelength/(float)(frames*1000); |
| 197 |
else |
| 198 |
bytes_per_frame = 0; |
| 199 |
|
| 200 |
bitrate = (int)(8. * bytes_per_frame * frames_per_sec + 0.5); |
| 201 |
|
| 202 |
if (frames_per_sec != 0) |
| 203 |
length = (float)frames/frames_per_sec; |
| 204 |
else |
| 205 |
length = 1; |
| 206 |
|
| 207 |
DEBUG_TRACE("ADTS frames=%d, frames_per_sec=%f, bytes_per_frame=%f, length=%f\n", |
| 208 |
frames, frames_per_sec, bytes_per_frame, length); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (samplerate >= 8000) { |
| 213 |
if (profile == 1) { |
| 214 |
if (channels <= 2) { |
| 215 |
if (bitrate <= 192) { |
| 216 |
if (samplerate <= 24000) |
| 217 |
my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ADTS_320", 0) ); |
| 218 |
else |
| 219 |
my_hv_store( info, "dlna_profile", newSVpv("AAC_ADTS_192", 0) ); |
| 220 |
} |
| 221 |
else if (bitrate <= 320) { |
| 222 |
if (samplerate <= 24000) |
| 223 |
my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ADTS_320", 0) ); |
| 224 |
else |
| 225 |
my_hv_store( info, "dlna_profile", newSVpv("AAC_ADTS_320", 0) ); |
| 226 |
} |
| 227 |
else { |
| 228 |
if (samplerate <= 24000) |
| 229 |
my_hv_store( info, "dlna_profile", newSVpv("HEAAC_L2_ADTS", 0) ); |
| 230 |
else |
| 231 |
my_hv_store( info, "dlna_profile", newSVpv("AAC_ADTS", 0) ); |
| 232 |
} |
| 233 |
} |
| 234 |
else if (channels <= 6) { |
| 235 |
if (samplerate <= 24000) |
| 236 |
my_hv_store( info, "dlna_profile", newSVpv("HEAAC_MULT5_ADTS", 0) ); |
| 237 |
else |
| 238 |
my_hv_store( info, "dlna_profile", newSVpv("AAC_MULT5_ADTS", 0) ); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if (samplerate <= 24000) |
| 245 |
samplerate *= 2; |
| 246 |
|
| 247 |
my_hv_store( info, "bitrate", newSVuv(bitrate * 1000) ); |
| 248 |
my_hv_store( info, "song_length_ms", newSVuv(length * 1000) ); |
| 249 |
my_hv_store( info, "samplerate", newSVuv(samplerate) ); |
| 250 |
my_hv_store( info, "profile", newSVpv( aac_profiles[profile], 0 ) ); |
| 251 |
my_hv_store( info, "channels", newSVuv(channels) ); |
| 252 |
|
| 253 |
return 1; |
| 254 |
} |