[b74e2d]: tagutils / tagutils-aac.c Maximize Restore History

Download this file

tagutils-aac.c    369 lines (311 with data), 9.7 kB

  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//=========================================================================
// FILENAME : tagutils-aac.c
// DESCRIPTION : AAC metadata reader
//=========================================================================
// Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved.
//=========================================================================
/*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This file is derived from mt-daap project.
*/
// _mac_to_unix_time
static time_t
_mac_to_unix_time(int t)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (t - (365L * 66L * 24L * 60L * 60L + 17L * 60L * 60L * 24L) +
(tz.tz_minuteswest * 60));
}
// _aac_findatom:
static long
_aac_findatom(FILE *fin, long max_offset, char *which_atom, int *atom_size)
{
long current_offset = 0;
int size;
char atom[4];
while(current_offset < max_offset)
{
if(fread((void*)&size, 1, sizeof(int), fin) != sizeof(int))
return -1;
size = ntohl(size);
if(size <= 7)
return -1;
if(fread(atom, 1, 4, fin) != 4)
return -1;
if(strncasecmp(atom, which_atom, 4) == 0)
{
*atom_size = size;
return current_offset;
}
fseek(fin, size - 8, SEEK_CUR);
current_offset += size;
}
return -1;
}
// _get_aactags
static int
_get_aactags(char *file, struct song_metadata *psong)
{
FILE *fin;
long atom_offset;
unsigned int atom_length;
long current_offset = 0;
int current_size;
char current_atom[4];
char *current_data;
int genre;
int len;
if(!(fin = fopen(file, "rb")))
{
DPRINTF(E_ERROR, L_SCANNER, "Cannot open file %s for reading\n", file);
return -1;
}
fseek(fin, 0, SEEK_SET);
atom_offset = _aac_lookforatom(fin, "moov:udta:meta:ilst", &atom_length);
if(atom_offset != -1)
{
while(current_offset < atom_length)
{
if(fread((void*)&current_size, 1, sizeof(int), fin) != sizeof(int))
break;
current_size = ntohl(current_size);
if(current_size <= 7) // something not right
break;
if(fread(current_atom, 1, 4, fin) != 4)
break;
len = current_size - 7; // too short
if(len < 22)
len = 22;
current_data = (char*)malloc(len); // extra byte
memset(current_data, 0x00, len);
if(fread(current_data, 1, current_size - 8, fin) != current_size - 8)
break;
if(!memcmp(current_atom, "\xA9" "nam", 4))
psong->title = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "ART", 4) ||
!memcmp(current_atom, "\xA9" "art", 4))
psong->contributor[ROLE_ARTIST] = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "alb", 4))
psong->album = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "cmt", 4))
psong->comment = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "dir", 4))
psong->contributor[ROLE_CONDUCTOR] = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "wrt", 4))
psong->contributor[ROLE_COMPOSER] = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "grp", 4))
psong->grouping = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "gen", 4))
psong->genre = strdup((char*)&current_data[16]);
else if(!memcmp(current_atom, "\xA9" "day", 4))
psong->year = atoi((char*)&current_data[16]);
else if(!memcmp(current_atom, "tmpo", 4))
psong->bpm = (current_data[16] << 8) | current_data[17];
else if(!memcmp(current_atom, "trkn", 4))
{
psong->track = (current_data[18] << 8) | current_data[19];
psong->total_tracks = (current_data[20] << 8) | current_data[21];
}
else if(!memcmp(current_atom, "disk", 4))
{
psong->disc = (current_data[18] << 8) | current_data[19];
psong->total_discs = (current_data[20] << 8) | current_data[21];
}
else if(!memcmp(current_atom, "gnre", 4))
{
genre = current_data[17] - 1;
if((genre < 0) || (genre > WINAMP_GENRE_UNKNOWN))
genre = WINAMP_GENRE_UNKNOWN;
psong->genre = strdup(winamp_genre[genre]);
}
else if(!memcmp(current_atom, "cpil", 4))
{
psong->compilation = current_data[16];
}
free(current_data);
current_offset += current_size;
}
}
fclose(fin);
if(atom_offset == -1)
return -1;
return 0;
}
// aac_lookforatom
static off_t
_aac_lookforatom(FILE *aac_fp, char *atom_path, unsigned int *atom_length)
{
long atom_offset;
off_t file_size;
char *cur_p, *end_p;
char atom_name[5];
fseek(aac_fp, 0, SEEK_END);
file_size = ftell(aac_fp);
rewind(aac_fp);
end_p = atom_path;
while(*end_p != '\0')
{
end_p++;
}
atom_name[4] = '\0';
cur_p = atom_path;
while(cur_p)
{
if((end_p - cur_p) < 4)
{
return -1;
}
strncpy(atom_name, cur_p, 4);
atom_offset = _aac_findatom(aac_fp, file_size, atom_name, (int*)atom_length);
if(atom_offset == -1)
{
return -1;
}
cur_p = strchr(cur_p, ':');
if(cur_p != NULL)
{
cur_p++;
if(!strcmp(atom_name, "meta"))
{
fseek(aac_fp, 4, SEEK_CUR);
}
else if(!strcmp(atom_name, "stsd"))
{
fseek(aac_fp, 8, SEEK_CUR);
}
else if(!strcmp(atom_name, "mp4a"))
{
fseek(aac_fp, 28, SEEK_CUR);
}
}
}
// return position of 'size:atom'
return ftell(aac_fp) - 8;
}
// _get_aacfileinfo
int
_get_aacfileinfo(char *file, struct song_metadata *psong)
{
FILE *infile;
long atom_offset;
int atom_length;
int sample_size;
int samples;
unsigned int bitrate;
off_t file_size;
int ms;
unsigned char buffer[2];
int time = 0;
aac_object_type_t profile_id = 0;
psong->vbr_scale = -1;
if(!(infile = fopen(file, "rb")))
{
DPRINTF(E_ERROR, L_SCANNER, "Could not open %s for reading\n", file);
return -1;
}
fseek(infile, 0, SEEK_END);
file_size = ftell(infile);
fseek(infile, 0, SEEK_SET);
// move to 'mvhd' atom
atom_offset = _aac_lookforatom(infile, "moov:mvhd", (unsigned int*)&atom_length);
if(atom_offset != -1)
{
fseek(infile, 8, SEEK_CUR);
fread((void *)&time, sizeof(int), 1, infile);
time = ntohl(time);
// slimserver prefer to use filesystem time
//psong->time_modified = _mac_to_unix_time(time);
fread((void*)&sample_size, 1, sizeof(int), infile);
fread((void*)&samples, 1, sizeof(int), infile);
sample_size = ntohl(sample_size);
samples = ntohl(samples);
// avoid overflowing on large sample_sizes (90000)
ms = 1000;
while((ms > 9) && (!(sample_size % 10)))
{
sample_size /= 10;
ms /= 10;
}
// unit = ms
psong->song_length = (int)((samples * ms) / sample_size);
}
psong->bitrate = 0;
// get samplerate from 'mp4a' (not from 'mdhd')
atom_offset = _aac_lookforatom(infile, "moov:trak:mdia:minf:stbl:stsd:mp4a", (unsigned int*)&atom_length);
if(atom_offset != -1)
{
fseek(infile, atom_offset + 32, SEEK_SET);
fread(buffer, sizeof(unsigned char), 2, infile);
psong->samplerate = (buffer[0] << 8) | (buffer[1]);
fseek(infile, 2, SEEK_CUR);
// get bitrate fomr 'esds'
atom_offset = _aac_findatom(infile, atom_length - (ftell(infile) - atom_offset), "esds", &atom_length);
if(atom_offset != -1)
{
fseek(infile, atom_offset + 26, SEEK_CUR); // +22 is max bitrate, +26 is average bitrate
fread((void *)&bitrate, sizeof(unsigned int), 1, infile);
psong->bitrate = ntohl(bitrate);
fseek(infile, 5, SEEK_CUR); // 5 bytes past bitrate is setup data
fread((void *)&buffer, 2, 1, infile);
profile_id = (buffer[0] >> 3); // first 5 bits of setup data is the Audo Profile ID
/* Frequency index: (((buffer[0] & 0x7) << 1) | (buffer[1] >> 7))) */
samples = ((buffer[1] >> 3) & 0xF);
psong->channels = (samples == 7 ? 8 : samples);
}
}
atom_offset = _aac_lookforatom(infile, "mdat", (unsigned int*)&atom_length);
psong->audio_size = atom_length - 8;
psong->audio_offset = atom_offset;
if(!psong->bitrate)
{
DPRINTF(E_DEBUG, L_SCANNER, "No 'esds' atom. Guess bitrate.\n");
if((atom_offset != -1) && (psong->song_length))
{
psong->bitrate = atom_length * 1000 / psong->song_length / 128;
}
}
//DPRINTF(E_DEBUG, L_METADATA, "Profile ID: %u\n", profile_id);
switch( profile_id )
{
case AAC_LC:
case AAC_LC_ER:
if( psong->samplerate < 8000 || psong->samplerate > 48000 )
{
DPRINTF(E_DEBUG, L_METADATA, "Unsupported AAC: sample rate is not 8000 < %d < 48000\n",
psong->samplerate);
break;
}
/* AAC @ Level 1/2 */
if( psong->channels <= 2 && psong->bitrate <= 320000 )
asprintf(&(psong->dlna_pn), "AAC_ISO_320");
else if( psong->channels <= 2 && psong->bitrate <= 576000 )
asprintf(&(psong->dlna_pn), "AAC_ISO");
else if( psong->channels <= 6 && psong->bitrate <= 1440000 )
asprintf(&(psong->dlna_pn), "AAC_MULT5_ISO");
else
DPRINTF(E_DEBUG, L_METADATA, "Unhandled AAC: %d channels, %d bitrate\n",
psong->channels, psong->bitrate);
break;
default:
DPRINTF(E_DEBUG, L_METADATA, "Unhandled AAC type [%d]\n", profile_id);
break;
}
fclose(infile);
return 0;
}