Debian Codegraph @

libgetmediainfo/gmi_mp3_id3tag.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Tag and audio information retrieval by libid3tag
3 *
4 * Copyright (c) 2005 Nyaochi
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
19 * http://www.gnu.org/copyleft/gpl.html .
20 *
21 */
22
23 /* $Id: gmi_mp3_id3tag.c,v 1.3 2006/10/08 18:34:11 nyaochi Exp $ */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif/*HAVE_CONFIG_H*/
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; /**< MPEG version */
44 int protection; /**< true if the frame is protected by CRC */
45 int bitrate; /**< bitrate in Kbps */
46 int sample_rate; /**< sample rate in Hz */
47 int padding; /**< true if the frame is padded */
48 int channel_mode; /**< channel mode */
49
50 int frame_size; /**< size of the frame in bytes */
51 int samples_per_frame; /**< number of audio samples in the frame */
52
53 /* From Xing VBR header for VBR files, From file size for CBR files. */
54 int number_of_frames; /**< number of frames */
55 int data_size; /**< 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 /* Make sure sync header and LayerIII */
78 if (data[0] != 0xFF && (data[1] & 0xE6) != 0xE6) {
79 return -1;
80 }
81
82 /* Read MPEG version */
83 switch ((data[1] >> 3) & 0x03) {
84 case 3:
85 header->version = 0; /* MPEG1 */
86 break;
87 case 2:
88 header->version = 1; /* MPEG2 */
89 break;
90 case 0:
91 header->version = 2; /* MPEG2.5 */
92 break;
93 default:
94 return -1;
95 }
96
97 // Read bitrate
98 bitrate_index = data[2] >> 4;
99 header->bitrate = bitrate_table[header->version][bitrate_index] * 1000;
100 // Read sample rate
101 samplerate_index = (data[2] >> 2) & 0x03;
102 header->sample_rate = samplerate_table[header->version][samplerate_index];
103 // Validate bitrate and sample rate
104 if (header->bitrate < 0 || header->sample_rate < 0) {
105 return -1;
106 }
107 // Read padding
108 header->padding = (data[2] >> 1) & 0x01;
109 // Read stereo mode
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; /* Set zero for now. */
122 return 0;
123 }
124
125 static int get_xing_offset(const mp3header_t* header)
126 {
127 if (header->version == 0) {
128 // MPEG1
129 return (header->channel_mode == 3 ? 17 : 32);
130 } else {
131 // MPEG2/2.5
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]; /* 16 bytes is enough. */
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 Loop until we find something other than ID3v2 in case the file
173 contains multiple ID3v2 tags.
174 */
175 for (;;) {
176 /* Read ten bytes to examine ID3v2 */
177 if (fread(buffer, sizeof(uint8_t), 10, fp) != 10) {
178 break;
179 }
180
181 /* Make sure the read stream is ID3v2 header. */
182 if (buffer[0] != 'I' || buffer[1] != 'D' || buffer[2] != '3') {
183 break;
184 }
185
186 /* Obtain tag size. */
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 /* Seek to the end position. */
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 /* Skip ID3v2 for unsyncronized ID3v2 tag. */
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 /* Found a MPEG Audio header... */
224
225 /* Get the offset position where the first frame begins. */
226 long content_pos = ftell(fp);
227 content_pos -= 4;
228 if (content_pos < 0) {
229 content_pos = 0;
230 }
231
232 /* Initialize data_size and number_of_frames, assuming a CBR stream. */
233 header->data_size = (filesize-content_pos);
234 header->number_of_frames = header->data_size / header->frame_size;
235
236 /* Discard side info. */
237 if (fseek(fp, get_xing_offset(header), SEEK_CUR) != 0) {
238 goto error_get_mp3stream_info;
239 }
240
241 /* Parse Xing VBR header if any. This sets number_of_frames and data_size. */
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 /* Exit with success. */
253 break;
254 }
255
256 /* Read next. */
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 /* Find the frame. */
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); /* MBS for music files. */
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 /* Obtain the filename. */
334 ucs2cpy(pathname, path);
335 ucs2cat(pathname, file);
336
337 /* Set the pathname and filename. */
338 info->pathname = ucs2dup(path);
339 info->filename = ucs2dup(file);
340
341 /* Set the filename to title in case we could not retrieve track title. */
342 free(info->title);
343 info->title = gettag_setfilename(file);
344
345 /* Open the file with UNICODE filename. This is for better compatibility. */
346 fp = ucs2fopen(pathname, "rb");
347 if (!fp) {
348 return -1;
349 }
350
351 /* Open with libid3tag */
352 /*id3file = id3_file_open(pathname, ID3_FILE_MODE_READONLY);*/
353 id3file = id3_file_fdopen(fileno(fp), ID3_FILE_MODE_READONLY);
354 if (!id3file) {
355 fclose(fp);
356 return -1;
357 }
358
359 /*
360 * Generally speaking, it is enough to read tag information from either ID3v1
361 * or ID3v2. However, I got several reports pointing that EasyH10 could not
362 * read tag information of a few MP3 files. I looked into the files and found
363 * that they have both ID3v1 and ID3v2, but the number of fields in ID3v2 was
364 * smaller than that of ID3v1. For example, a file contains the artist information
365 * in ID3v1 but not in ID3v2. I want to complain about their tagging system but
366 * iriver plus surprisingly reads this kind of tag information...
367 *
368 * libid3tag does not allow a program to choose ID3v1 or ID3v2 but prioritizes
369 * ID3v2 all the time. We made a patch to obtain all kinds of tag information
370 * stored in a file and use it when it's available.
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/*HAVE_LIBID3TAG_GETTAG*/
381 if (!id3tag) {
382 continue;
383 }
384
385 /* Obtain track number first. */
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 /* Obtain track title. */
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 /* Set artist field. */
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 /* Set album field. */
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 * TCMP (compilation flag) handling for omnibus albums.
424 * This patch was originally submitted by Espen Matheussen.
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 /* Calculate the length of the new title. */
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 /* Construct the new title. */
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 /* Free the previous title and artist. */
454 ucs2free(info->title);
455 ucs2free(info->artist);
456
457 /* Set the new title and artist. */
458 info->title = title;
459 info->artist = ucs2dup(va);
460 }
461
462 /* Set genre field. */
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 /* Set year field. */
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 /* Parse MPEG audio header. */
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 }