1 //=========================================================================
2 // FILENAME : tagutils-mp3.c
3 // DESCRIPTION : MP3 metadata reader
4 //=========================================================================
5 // Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved.
6 //=========================================================================
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * This file is derived from mt-daap project.
28 _get_mp3tags(char *file
, struct song_metadata
*psong
)
30 struct id3_file
*pid3file
;
31 struct id3_tag
*pid3tag
;
32 struct id3_frame
*pid3frame
;
36 unsigned char *utf8_text
;
37 int genre
= WINAMP_GENRE_UNKNOWN
;
40 id3_ucs4_t
const *native_text
;
42 int got_numeric_genre
;
43 id3_byte_t
const *image
;
44 id3_length_t image_size
= 0;
46 pid3file
= id3_file_open(file
, ID3_FILE_MODE_READONLY
);
49 DPRINTF(E_ERROR
, L_SCANNER
, "Cannot open %s\n", file
);
53 pid3tag
= id3_file_tag(pid3file
);
58 id3_file_close(pid3file
);
60 DPRINTF(E_WARN
, L_SCANNER
, "Cannot get ID3 tag for %s\n", file
);
65 while((pid3frame
= id3_tag_findframe(pid3tag
, "", index
)))
73 if(!strcmp(pid3frame
->id
, "YTCP")) /* for id3v2.2 */
75 psong
->compilation
= 1;
76 DPRINTF(E_DEBUG
, L_SCANNER
, "Compilation: %d [%s]\n", psong
->compilation
, basename(file
));
78 else if(!strcmp(pid3frame
->id
, "APIC") && !image_size
)
80 if( (strcmp((char*)id3_field_getlatin1(&pid3frame
->fields
[1]), "image/jpeg") == 0) ||
81 (strcmp((char*)id3_field_getlatin1(&pid3frame
->fields
[1]), "image/jpg") == 0) ||
82 (strcmp((char*)id3_field_getlatin1(&pid3frame
->fields
[1]), "jpeg") == 0) )
84 image
= id3_field_getbinarydata(&pid3frame
->fields
[4], &image_size
);
87 psong
->image
= malloc(image_size
);
88 memcpy(psong
->image
, image
, image_size
);
89 psong
->image_size
= image_size
;
90 //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "Found thumbnail: %d\n", psong->image_size);
95 if(((pid3frame
->id
[0] == 'T') || (strcmp(pid3frame
->id
, "COMM") == 0)) &&
96 (id3_field_getnstrings(&pid3frame
->fields
[1])))
101 native_text
= id3_field_getstrings(&pid3frame
->fields
[1], 0);
107 utf8_text
= _get_utf8_text(native_text
); // through iconv
109 utf8_text
= (unsigned char*)id3_ucs4_utf8duplicate(native_text
);
111 if(!strcmp(pid3frame
->id
, "TIT2"))
114 psong
->title
= (char*)utf8_text
;
116 else if(!strcmp(pid3frame
->id
, "TPE1"))
119 psong
->contributor
[ROLE_ARTIST
] = (char*)utf8_text
;
121 else if(!strcmp(pid3frame
->id
, "TALB"))
124 psong
->album
= (char*)utf8_text
;
126 else if(!strcmp(pid3frame
->id
, "TCOM"))
129 psong
->contributor
[ROLE_COMPOSER
] = (char*)utf8_text
;
131 else if(!strcmp(pid3frame
->id
, "TIT1"))
134 psong
->grouping
= (char*)utf8_text
;
136 else if(!strcmp(pid3frame
->id
, "TPE2"))
139 psong
->contributor
[ROLE_BAND
] = (char*)utf8_text
;
141 else if(!strcmp(pid3frame
->id
, "TPE3"))
144 psong
->contributor
[ROLE_CONDUCTOR
] = (char*)utf8_text
;
146 else if(!strcmp(pid3frame
->id
, "TCON"))
149 psong
->genre
= (char*)utf8_text
;
150 got_numeric_genre
= 0;
153 if(!strlen(psong
->genre
))
155 genre
= WINAMP_GENRE_UNKNOWN
;
156 got_numeric_genre
= 1;
158 else if(isdigit(psong
->genre
[0]))
160 genre
= atoi(psong
->genre
);
161 got_numeric_genre
= 1;
163 else if((psong
->genre
[0] == '(') && (isdigit(psong
->genre
[1])))
165 genre
= atoi((char*)&psong
->genre
[1]);
166 got_numeric_genre
= 1;
169 if(got_numeric_genre
)
171 if((genre
< 0) || (genre
> WINAMP_GENRE_UNKNOWN
))
172 genre
= WINAMP_GENRE_UNKNOWN
;
174 psong
->genre
= strdup(winamp_genre
[genre
]);
178 else if(!strcmp(pid3frame
->id
, "COMM"))
181 psong
->comment
= (char*)utf8_text
;
183 else if(!strcmp(pid3frame
->id
, "TPOS"))
185 tmp
= (char*)utf8_text
;
189 psong
->total_discs
= atoi(tmp
);
191 psong
->disc
= atoi((char*)utf8_text
);
193 else if(!strcmp(pid3frame
->id
, "TRCK"))
195 tmp
= (char*)utf8_text
;
199 psong
->total_tracks
= atoi(tmp
);
201 psong
->track
= atoi((char*)utf8_text
);
203 else if(!strcmp(pid3frame
->id
, "TDRC"))
205 psong
->year
= atoi((char*)utf8_text
);
207 else if(!strcmp(pid3frame
->id
, "TLEN"))
209 psong
->song_length
= atoi((char*)utf8_text
);
211 else if(!strcmp(pid3frame
->id
, "TBPM"))
213 psong
->bpm
= atoi((char*)utf8_text
);
215 else if(!strcmp(pid3frame
->id
, "TCMP"))
217 psong
->compilation
= (char)atoi((char*)utf8_text
);
223 if((!used
) && (have_utf8
) && (utf8_text
))
227 if((!strcmp(pid3frame
->id
, "COMM")) && (pid3frame
->nfields
== 4))
229 native_text
= id3_field_getstring(&pid3frame
->fields
[2]);
232 utf8_text
= (unsigned char*)id3_ucs4_utf8duplicate(native_text
);
233 if((utf8_text
) && (strncasecmp((char*)utf8_text
, "iTun", 4) != 0))
238 native_text
= id3_field_getfullstring(&pid3frame
->fields
[3]);
241 utf8_text
= (unsigned char*)id3_ucs4_utf8duplicate(native_text
);
244 free(psong
->comment
);
245 psong
->comment
= (char*)utf8_text
;
259 id3_file_close(pid3file
);
260 //DEBUG DPRINTF(E_INFO, L_SCANNER, "Got id3 tag successfully for file=%s\n", file);
266 _decode_mp3_frame(unsigned char *frame
, struct mp3_frameinfo
*pfi
)
272 int samplerate_index
;
274 if((frame
[0] != 0xFF) || (frame
[1] < 224))
280 ver
= (frame
[1] & 0x18) >> 3;
281 pfi
->layer
= 4 - ((frame
[1] & 0x6) >> 1);
283 layer_index
= sample_index
= -1;
288 pfi
->mpeg_version
= 0x25; // 2.5
292 if((pfi
->layer
== 2) || (pfi
->layer
== 3))
296 pfi
->mpeg_version
= 0x20; // 2.0
300 if((pfi
->layer
== 2) || (pfi
->layer
== 3))
304 pfi
->mpeg_version
= 0x10; // 1.0
315 if((layer_index
< 0) || (layer_index
> 4))
321 if((sample_index
< 0) || (sample_index
>= 2))
327 if(pfi
->layer
== 1) pfi
->samples_per_frame
= 384;
328 if(pfi
->layer
== 2) pfi
->samples_per_frame
= 1152;
331 if(pfi
->mpeg_version
== 0x10)
332 pfi
->samples_per_frame
= 1152;
334 pfi
->samples_per_frame
= 576;
337 bitrate_index
= (frame
[2] & 0xF0) >> 4;
338 samplerate_index
= (frame
[2] & 0x0C) >> 2;
340 if((bitrate_index
== 0xF) || (bitrate_index
== 0x0))
346 if(samplerate_index
== 3)
353 pfi
->bitrate
= bitrate_tbl
[layer_index
][bitrate_index
];
354 pfi
->samplerate
= sample_rate_tbl
[sample_index
][samplerate_index
];
356 if((frame
[3] & 0xC0 >> 6) == 3)
366 if(pfi
->mpeg_version
== 0x10)
369 pfi
->xing_offset
= 32;
371 pfi
->xing_offset
= 17;
376 pfi
->xing_offset
= 17;
378 pfi
->xing_offset
= 9;
381 pfi
->crc_protected
= frame
[1] & 0xFE;
384 pfi
->frame_length
= (12 * pfi
->bitrate
* 1000 / pfi
->samplerate
+ pfi
->padding
) * 4;
386 pfi
->frame_length
= 144 * pfi
->bitrate
* 1000 / pfi
->samplerate
+ pfi
->padding
;
388 if((pfi
->frame_length
> 2880) || (pfi
->frame_length
<= 0))
398 // _mp3_get_average_bitrate
399 // read from midle of file, and estimate
400 static void _mp3_get_average_bitrate(FILE *infile
, struct mp3_frameinfo
*pfi
, const char *fname
)
403 unsigned char frame_buffer
[2900];
404 unsigned char header
[4];
408 struct mp3_frameinfo fi
;
410 int bitrate_total
= 0;
412 fseek(infile
, 0, SEEK_END
);
413 file_size
= ftell(infile
);
415 pos
= file_size
>> 1;
417 /* now, find the first frame */
418 fseek(infile
, pos
, SEEK_SET
);
419 if(fread(frame_buffer
, 1, sizeof(frame_buffer
), infile
) != sizeof(frame_buffer
))
424 while((frame_buffer
[index
] != 0xFF) && (index
< (sizeof(frame_buffer
) - 4)))
427 if(index
>= (sizeof(frame_buffer
) - 4)) // max mp3 framesize = 2880
429 DPRINTF(E_DEBUG
, L_SCANNER
, "Could not find frame for %s\n", basename((char *)fname
));
433 if(!_decode_mp3_frame(&frame_buffer
[index
], &fi
))
435 /* see if next frame is valid */
436 fseek(infile
, pos
+ index
+ fi
.frame_length
, SEEK_SET
);
437 if(fread(header
, 1, sizeof(header
), infile
) != sizeof(header
))
439 DPRINTF(E_DEBUG
, L_SCANNER
, "Could not read frame header for %s\n", basename((char *)fname
));
443 if(!_decode_mp3_frame(header
, &fi
))
454 while(frame_count
< 10)
456 fseek(infile
, pos
, SEEK_SET
);
457 if(fread(header
, 1, sizeof(header
), infile
) != sizeof(header
))
459 DPRINTF(E_DEBUG
, L_SCANNER
, "Could not read frame header for %s\n", basename((char *)fname
));
462 if(_decode_mp3_frame(header
, &fi
))
464 DPRINTF(E_DEBUG
, L_SCANNER
, "Invalid frame header while averaging %s\n", basename((char *)fname
));
468 bitrate_total
+= fi
.bitrate
;
470 pos
+= fi
.frame_length
;
473 pfi
->bitrate
= bitrate_total
/ frame_count
;
478 // _mp3_get_frame_count
480 static void __attribute__((unused
))
481 _mp3_get_frame_count(FILE *infile
, struct mp3_frameinfo
*pfi
)
485 unsigned char frame_buffer
[4];
486 struct mp3_frameinfo fi
;
490 int last_bitrate
= 0;
492 fseek(infile
, 0, SEEK_END
);
493 file_size
= ftell(infile
);
495 pos
= pfi
->frame_offset
;
501 fseek(infile
, pos
, SEEK_SET
);
502 if(fread(frame_buffer
, 1, sizeof(frame_buffer
), infile
) == sizeof(frame_buffer
))
505 if(!_decode_mp3_frame(frame_buffer
, &fi
))
508 pos
+= fi
.frame_length
;
511 if((last_bitrate
) && (fi
.bitrate
!= last_bitrate
))
513 last_bitrate
= fi
.bitrate
;
515 // no sense to scan cbr
516 if(cbr
&& (frames
> 100))
518 DPRINTF(E_DEBUG
, L_SCANNER
, "File appears to be CBR... quitting frame _mp3_get_frame_count()\n");
526 if(pos
> (file_size
- 4096))
528 pfi
->number_of_frames
= frames
;
533 DPRINTF(E_ERROR
, L_SCANNER
, "Frame count aborted on error. Pos=%d, Count=%d\n",
543 _get_mp3fileinfo(char *file
, struct song_metadata
*psong
)
546 struct id3header
*pid3
;
547 struct mp3_frameinfo fi
;
548 unsigned int size
= 0;
552 unsigned char buffer
[1024];
559 char frame_buffer
[4];
563 if(!(infile
= fopen(file
, "rb")))
565 DPRINTF(E_ERROR
, L_SCANNER
, "Could not open %s for reading\n", file
);
569 memset((void*)&fi
, 0, sizeof(fi
));
571 fseek(infile
, 0, SEEK_END
);
572 file_size
= ftell(infile
);
573 fseek(infile
, 0, SEEK_SET
);
575 if(fread(buffer
, 1, sizeof(buffer
), infile
) != sizeof(buffer
))
579 DPRINTF(E_ERROR
, L_SCANNER
, "Error reading: %s [%s]\n", strerror(errno
), file
);
583 DPRINTF(E_WARN
, L_SCANNER
, "File too small. Probably corrupted. [%s]\n", file
);
589 pid3
= (struct id3header
*)buffer
;
594 if(strncmp((char*)pid3
->id
, "ID3", 3) == 0)
598 /* found an ID3 header... */
599 size
= (pid3
->size
[0] << 21 | pid3
->size
[1] << 14 |
600 pid3
->size
[2] << 7 | pid3
->size
[3]);
601 fp_size
= size
+ sizeof(struct id3header
);
604 snprintf(tagversion
, sizeof(tagversion
), "ID3v2.%d.%d",
605 pid3
->version
[0], pid3
->version
[1]);
606 psong
->tagversion
= strdup(tagversion
);
611 /* Here we start the brute-force header seeking. Sure wish there
612 * weren't so many crappy mp3 files out there
617 fseek(infile
, fp_size
, SEEK_SET
);
618 if((n_read
= fread(buffer
, 1, sizeof(buffer
), infile
)) < 4) // at least mp3 frame header size (i.e. 4 bytes)
627 while((buffer
[index
] != 0xFF) && (index
< (n_read
- 50)))
630 if((first_check
) && (index
))
634 if(n_read
< sizeof(buffer
))
642 if(index
> (n_read
- 50))
645 if(n_read
< sizeof(buffer
))
653 if(!_decode_mp3_frame(&buffer
[index
], &fi
))
655 if(!strncasecmp((char*)&buffer
[index
+ fi
.xing_offset
+ 4], "XING", 4))
657 /* no need to check further... if there is a xing header there,
658 * this is definately a valid frame */
664 /* No Xing... check for next frame to validate current fram is correct */
665 fseek(infile
, fp_size
+ index
+ fi
.frame_length
, SEEK_SET
);
666 if(fread(frame_buffer
, 1, sizeof(frame_buffer
), infile
) == sizeof(frame_buffer
))
668 if(!_decode_mp3_frame((unsigned char*)frame_buffer
, &fi
))
676 DPRINTF(E_ERROR
, L_SCANNER
, "Could not read frame header: %s\n", file
);
683 // cannot find second frame. Song may be too short. So assume first frame is valid.
695 DPRINTF(E_INFO
, L_SCANNER
, "Bad header... dropping back for full frame search [%s]\n", psong
->path
);
704 fi
.frame_offset
= fp_size
;
706 psong
->audio_offset
= fp_size
;
707 psong
->audio_size
= file_size
- fp_size
;
708 // check if last 128 bytes is ID3v1.0 ID3v1.1 tag
709 fseek(infile
, file_size
- 128, SEEK_SET
);
710 if(fread(id3v1taghdr
, 1, 4, infile
) == 4)
712 if(id3v1taghdr
[0] == 'T' && id3v1taghdr
[1] == 'A' && id3v1taghdr
[2] == 'G')
714 psong
->audio_size
-= 128;
718 if(_decode_mp3_frame(&buffer
[index
], &fi
))
721 DPRINTF(E_ERROR
, L_SCANNER
, "Could not find sync frame: %s\n", file
);
725 /* now check for an XING header */
726 psong
->vbr_scale
= -1;
727 if(!strncasecmp((char*)&buffer
[index
+ fi
.xing_offset
+ 4], "XING", 4))
729 xing_flags
= buffer
[index
+fi
.xing_offset
+4+4] << 24 |
730 buffer
[index
+fi
.xing_offset
+4+5] << 16 |
731 buffer
[index
+fi
.xing_offset
+4+6] << 8 |
732 buffer
[index
+fi
.xing_offset
+4+7];
733 psong
->vbr_scale
= 78;
737 /* Frames field is valid... */
738 fi
.number_of_frames
= buffer
[index
+fi
.xing_offset
+4+8] << 24 |
739 buffer
[index
+fi
.xing_offset
+4+9] << 16 |
740 buffer
[index
+fi
.xing_offset
+4+10] << 8 |
741 buffer
[index
+fi
.xing_offset
+4+11];
745 if((fi
.number_of_frames
== 0) && (!psong
->song_length
))
747 _mp3_get_average_bitrate(infile
, &fi
, file
);
750 psong
->bitrate
= fi
.bitrate
* 1000;
751 psong
->samplerate
= fi
.samplerate
;
753 if(!psong
->song_length
)
755 if(fi
.number_of_frames
)
757 psong
->song_length
= (int)((double)(fi
.number_of_frames
* fi
.samples_per_frame
* 1000.) /
758 (double)fi
.samplerate
);
759 psong
->vbr_scale
= 78;
763 psong
->song_length
= (int)((double)(file_size
- fp_size
) * 8. /
767 psong
->channels
= fi
.stereo
? 2 : 1;
770 //DEBUG DPRINTF(E_INFO, L_SCANNER, "Got fileinfo successfully for file=%s song_length=%d\n", file, psong->song_length);
772 psong
->blockalignment
= 1;
773 xasprintf(&(psong
->dlna_pn
), "MP3");