| 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 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include <config.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <os.h> |
| 30 |
#include <limits.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <ucs2char.h> |
| 37 |
#include <h10db.h> |
| 38 |
#include <getmediainfo.h> |
| 39 |
|
| 40 |
#include <id3tag.h> |
| 41 |
|
| 42 |
struct tag_mp3header { |
| 43 |
int version; |
| 44 |
int protection; |
| 45 |
int bitrate; |
| 46 |
int sample_rate; |
| 47 |
int padding; |
| 48 |
int channel_mode; |
| 49 |
|
| 50 |
int frame_size; |
| 51 |
int samples_per_frame; |
| 52 |
|
| 53 |
|
| 54 |
int number_of_frames; |
| 55 |
int data_size; |
| 56 |
|
| 57 |
int duration; |
| 58 |
}; |
| 59 |
typedef struct tag_mp3header mp3header_t; |
| 60 |
|
| •61 |
static const int bitrate_table[3][16] = { |
| 62 |
{-1, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1}, |
| 63 |
{-1, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1}, |
| 64 |
{-1, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1}, |
| 65 |
}; |
| 66 |
|
| •67 |
static const int samplerate_table[3][4] = { |
| 68 |
{44100, 48000, 32000, -1}, |
| 69 |
{22050, 24000, 16000, -1}, |
| 70 |
{11025, 1200, 8000, -1}, |
| 71 |
}; |
| 72 |
|
| 73 |
static int parse_mp3header(mp3header_t* header, uint8_t* data) |
| 74 |
{ |
| 75 |
int bitrate_index, samplerate_index; |
| 76 |
|
| 77 |
|
| 78 |
if (data[0] != 0xFF && (data[1] & 0xE6) != 0xE6) { |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
switch ((data[1] >> 3) & 0x03) { |
| 84 |
case 3: |
| 85 |
header->version = 0; |
| 86 |
break; |
| 87 |
case 2: |
| 88 |
header->version = 1; |
| 89 |
break; |
| 90 |
case 0: |
| 91 |
header->version = 2; |
| 92 |
break; |
| 93 |
default: |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
bitrate_index = data[2] >> 4; |
| 99 |
header->bitrate = bitrate_table[header->version][bitrate_index] * 1000; |
| 100 |
|
| 101 |
samplerate_index = (data[2] >> 2) & 0x03; |
| 102 |
header->sample_rate = samplerate_table[header->version][samplerate_index]; |
| 103 |
|
| 104 |
if (header->bitrate < 0 || header->sample_rate < 0) { |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
header->padding = (data[2] >> 1) & 0x01; |
| 109 |
|
| 110 |
header->channel_mode = (data[3] >> 6) & 0x03; |
| 111 |
|
| 112 |
if (header->version == 0) { |
| 113 |
header->frame_size = 144 * header->bitrate / header->sample_rate + header->padding; |
| 114 |
header->samples_per_frame = 1152; |
| 115 |
|
| 116 |
} else { |
| 117 |
header->frame_size = 72 * header->bitrate / header->sample_rate + header->padding; |
| 118 |
header->samples_per_frame = 576; |
| 119 |
} |
| 120 |
|
| 121 |
header->number_of_frames = 0; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static int get_xing_offset(const mp3header_t* header) |
| 126 |
{ |
| 127 |
if (header->version == 0) { |
| 128 |
|
| 129 |
return (header->channel_mode == 3 ? 17 : 32); |
| 130 |
} else { |
| 131 |
|
| 132 |
return (header->channel_mode == 3 ? 9 : 17); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static int parse_xing_header(mp3header_t* header, FILE *fp) |
| 137 |
{ |
| 138 |
uint8_t xing_header[16]; |
| 139 |
if (fread(xing_header, sizeof(uint8_t), 16, fp) != 16) { |
| 140 |
return -1; |
| 141 |
} |
| 142 |
if (memcmp(xing_header, "Xing", 4) != 0) { |
| 143 |
return -1; |
| 144 |
} |
| 145 |
if (xing_header[7] & 0x01) { |
| 146 |
header->number_of_frames = |
| 147 |
(uint32_t)xing_header[ 8] << 24 | |
| 148 |
(uint32_t)xing_header[ 9] << 16 | |
| 149 |
(uint32_t)xing_header[10] << 8 | |
| 150 |
(uint32_t)xing_header[11] |
| 151 |
; |
| 152 |
} |
| 153 |
if (xing_header[7] & 0x02) { |
| 154 |
header->data_size = |
| 155 |
(uint32_t)xing_header[12] << 24 | |
| 156 |
(uint32_t)xing_header[13] << 16 | |
| 157 |
(uint32_t)xing_header[14] << 8 | |
| 158 |
(uint32_t)xing_header[15] |
| 159 |
; |
| 160 |
} |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static int skip_id3v2(FILE *fp) |
| 166 |
{ |
| 167 |
uint8_t buffer[10]; |
| 168 |
uint32_t id3v2_size = 0; |
| 169 |
long offset = ftell(fp); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
for (;;) { |
| 176 |
|
| 177 |
if (fread(buffer, sizeof(uint8_t), 10, fp) != 10) { |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if (buffer[0] != 'I' || buffer[1] != 'D' || buffer[2] != '3') { |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
id3v2_size = |
| 188 |
(uint32_t)(buffer[6] & 0x7F) << 21 | |
| 189 |
(uint32_t)(buffer[7] & 0x7F) << 14 | |
| 190 |
(uint32_t)(buffer[8] & 0x7F) << 7 | |
| 191 |
(uint32_t)(buffer[9] & 0x7F); |
| 192 |
|
| 193 |
|
| 194 |
if (fseek(fp, (long)id3v2_size, SEEK_CUR) != 0) { |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
offset = ftell(fp); |
| 199 |
} |
| 200 |
|
| 201 |
fseek(fp, offset, SEEK_SET); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static int get_mp3stream_info(mp3header_t* header, const ucs2_char_t *filename, uint32_t filesize) |
| 206 |
{ |
| 207 |
int channels = 0; |
| 208 |
uint8_t buffer[4]; |
| 209 |
FILE *fp = ucs2fopen(filename, "rb"); |
| 210 |
if (!fp) { |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
skip_id3v2(fp); |
| 216 |
|
| 217 |
if (fread(buffer, sizeof(uint8_t), 4, fp) != 4) { |
| 218 |
goto error_get_mp3stream_info; |
| 219 |
} |
| 220 |
|
| 221 |
for (;;) { |
| 222 |
if (parse_mp3header(header, buffer) == 0) { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
long content_pos = ftell(fp); |
| 227 |
content_pos -= 4; |
| 228 |
if (content_pos < 0) { |
| 229 |
content_pos = 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
header->data_size = (filesize-content_pos); |
| 234 |
header->number_of_frames = header->data_size / header->frame_size; |
| 235 |
|
| 236 |
|
| 237 |
if (fseek(fp, get_xing_offset(header), SEEK_CUR) != 0) { |
| 238 |
goto error_get_mp3stream_info; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
if (parse_xing_header(header, fp) == 0) { |
| 243 |
double duration = (double)header->samples_per_frame * header->number_of_frames / header->sample_rate; |
| 244 |
double bitrate = header->data_size * 8.0 / duration; |
| 245 |
header->duration = (int)duration; |
| 246 |
header->bitrate = (int)bitrate; |
| 247 |
} else { |
| 248 |
double duration = (double)header->samples_per_frame * header->number_of_frames / header->sample_rate; |
| 249 |
header->duration = (int)duration; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
memmove(&buffer[0], &buffer[1], sizeof(uint8_t)*3); |
| 258 |
if (fread(&buffer[3], sizeof(uint8_t), 1, fp) != 1) { |
| 259 |
goto error_get_mp3stream_info; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
fclose(fp); |
| 264 |
return 0; |
| 265 |
|
| 266 |
error_get_mp3stream_info: |
| 267 |
fclose(fp); |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
static ucs2_char_t* get_frame_value(struct id3_tag *id3tag, const char *name) |
| 273 |
{ |
| 274 |
ucs2_char_t* ret = NULL; |
| 275 |
const id3_ucs4_t *value = NULL; |
| 276 |
struct id3_frame *frame = NULL; |
| 277 |
union id3_field *field = NULL; |
| 278 |
enum id3_field_textencoding encoding = ID3_FIELD_TEXTENCODING_ISO_8859_1; |
| 279 |
|
| 280 |
|
| 281 |
frame = id3_tag_findframe(id3tag, name, 0); |
| 282 |
if (!frame) { |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
field = id3_frame_field(frame, 0); |
| 288 |
if (!field) { |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
if (id3_field_type(field) == ID3_FIELD_TYPE_TEXTENCODING) { |
| 293 |
encoding = field->number.value; |
| 294 |
} |
| 295 |
|
| 296 |
field = id3_frame_field(frame, 1); |
| 297 |
if (!field) { |
| 298 |
return NULL; |
| 299 |
} |
| 300 |
|
| 301 |
value = id3_field_getstrings(field, 0); |
| 302 |
if (!value) { |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
if (strcmp(name, "TCON") == 0) { |
| 307 |
value = id3_genre_name(value); |
| 308 |
} |
| 309 |
|
| 310 |
if (encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1) { |
| 311 |
char *mbs = id3_ucs4_latin1duplicate(value); |
| 312 |
ret = mbsdupucs2_music(mbs); |
| 313 |
free(mbs); |
| 314 |
return ret; |
| 315 |
} else { |
| 316 |
return id3_ucs4_utf16duplicate(value); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
int gettag_mp3(media_info* info, const ucs2_char_t *path, const ucs2_char_t *file) |
| 321 |
{ |
| 322 |
int i, num_tags = 0; |
| 323 |
ucs2_char_t pathname[MAX_PATH]; |
| 324 |
FILE *fp = NULL; |
| 325 |
mp3header_t mp3header; |
| 326 |
uint32_t tn = 0; |
| 327 |
static const ucs2_char_t ucs2_empty[1] = {0}; |
| 328 |
ucs2_char_t* ucs2 = NULL; |
| 329 |
struct id3_file *id3file = NULL; |
| 330 |
struct id3_tag *id3tag = NULL; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
ucs2cpy(pathname, path); |
| 335 |
ucs2cat(pathname, file); |
| 336 |
|
| 337 |
|
| 338 |
info->pathname = ucs2dup(path); |
| 339 |
info->filename = ucs2dup(file); |
| 340 |
|
| 341 |
|
| 342 |
free(info->title); |
| 343 |
info->title = gettag_setfilename(file); |
| 344 |
|
| 345 |
|
| 346 |
fp = ucs2fopen(pathname, "rb"); |
| 347 |
if (!fp) { |
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
id3file = id3_file_fdopen(fileno(fp), ID3_FILE_MODE_READONLY); |
| 354 |
if (!id3file) { |
| 355 |
fclose(fp); |
| 356 |
return -1; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
#ifdef HAVE_LIBID3TAG_GETNTAG |
| 373 |
num_tags = id3_file_getnumtag(id3file); |
| 374 |
for (i = num_tags-1;i >= 0;i--) { |
| 375 |
id3tag = id3_file_gettag(id3file, i); |
| 376 |
#else |
| 377 |
num_tags = 1; |
| 378 |
for (i = 0;i < num_tags;i++) { |
| 379 |
id3tag = id3_file_tag(id3file); |
| 380 |
#endif |
| 381 |
if (!id3tag) { |
| 382 |
continue; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
ucs2 = get_frame_value(id3tag, "TRCK"); |
| 387 |
if (ucs2 && *ucs2) { |
| 388 |
info->tracknumer = ucs2toi(ucs2); |
| 389 |
} else { |
| 390 |
info->tracknumer = 0; |
| 391 |
} |
| 392 |
free(ucs2); |
| 393 |
|
| 394 |
|
| 395 |
ucs2 = get_frame_value(id3tag, "TIT2"); |
| 396 |
if (ucs2 && *ucs2) { |
| 397 |
free(info->title); |
| 398 |
info->title = ucs2dup(ucs2); |
| 399 |
} |
| 400 |
free(ucs2); |
| 401 |
|
| 402 |
|
| 403 |
if (1) ucs2 = get_frame_value(id3tag, "TPE1"); |
| 404 |
if (!ucs2) ucs2 = get_frame_value(id3tag, "TPE2"); |
| 405 |
if (!ucs2) ucs2 = get_frame_value(id3tag, "TPE3"); |
| 406 |
if (!ucs2) ucs2 = get_frame_value(id3tag, "TPE4"); |
| 407 |
if (!ucs2) ucs2 = get_frame_value(id3tag, "TCOM"); |
| 408 |
if (ucs2 && *ucs2) { |
| 409 |
free(info->artist); |
| 410 |
info->artist = ucs2dup(ucs2); |
| 411 |
} |
| 412 |
free(ucs2); |
| 413 |
|
| 414 |
|
| 415 |
ucs2 = get_frame_value(id3tag, "TALB"); |
| 416 |
if (ucs2 && *ucs2) { |
| 417 |
free(info->album); |
| 418 |
info->album = ucs2dup(ucs2); |
| 419 |
} |
| 420 |
free(ucs2); |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
ucs2 = get_frame_value(id3tag, "TCMP"); |
| 427 |
if (ucs2 && *ucs2) { |
| 428 |
int length = 0; |
| 429 |
ucs2_char_t* title = NULL; |
| 430 |
static ucs2_char_t separator[] = {' ','-',' ',0}; |
| 431 |
static ucs2_char_t va[] = {'V','a','r','i','o','u','s',' ','a','r','t','i','s','t','s',0}; |
| 432 |
|
| 433 |
|
| 434 |
if (info->artist) { |
| 435 |
length += ucs2len(info->artist); |
| 436 |
} |
| 437 |
length += ucs2len(separator); |
| 438 |
if (info->title) { |
| 439 |
length += ucs2len(info->title); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
title = ucs2malloc(sizeof(ucs2_char_t) * (length+1)); |
| 444 |
memset(title, 0, sizeof(ucs2_char_t) * (length+1)); |
| 445 |
if (info->artist) { |
| 446 |
ucs2cpy(title, info->artist); |
| 447 |
} |
| 448 |
ucs2cat(title, separator); |
| 449 |
if (info->title) { |
| 450 |
ucs2cat(title, info->title); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
ucs2free(info->title); |
| 455 |
ucs2free(info->artist); |
| 456 |
|
| 457 |
|
| 458 |
info->title = title; |
| 459 |
info->artist = ucs2dup(va); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
ucs2 = get_frame_value(id3tag, "TCON"); |
| 464 |
if (ucs2 && *ucs2) { |
| 465 |
free(info->genre); |
| 466 |
info->genre = ucs2dup(ucs2); |
| 467 |
} |
| 468 |
free(ucs2); |
| 469 |
|
| 470 |
|
| 471 |
if (1) ucs2 = get_frame_value(id3tag, "TYER"); |
| 472 |
if (!ucs2) ucs2 = get_frame_value(id3tag, "TDRC"); |
| 473 |
if (ucs2 && *ucs2) { |
| 474 |
info->year = ucs2toi(ucs2); |
| 475 |
} else { |
| 476 |
info->year = 0; |
| 477 |
} |
| 478 |
free(ucs2); |
| 479 |
} |
| 480 |
|
| 481 |
info->filesize = ucs2stat_size(pathname); |
| 482 |
|
| 483 |
|
| 484 |
if (get_mp3stream_info(&mp3header, pathname, info->filesize) == 0) { |
| 485 |
int channels = mp3header.channel_mode == 3 ? 1 : 2; |
| 486 |
info->samplerate = mp3header.sample_rate; |
| 487 |
info->bitrate = mp3header.bitrate; |
| 488 |
info->duration = mp3header.duration; |
| 489 |
} |
| 490 |
|
| 491 |
id3_file_close(id3file); |
| 492 |
fclose(fp); |
| 493 |
|
| 494 |
if (!info->title || !*info->title) { |
| 495 |
ucs2free(info->title); |
| 496 |
info->title = gettag_setfilename(file); |
| 497 |
} |
| 498 |
|
| 499 |
return 0; |
| 500 |
} |