1 /*****************************************************************************
2 * id3tag.c: id3/ape tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
40 #include <sys/types.h>
45 #include "id3genres.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int ParseTags ( vlc_object_t * );
52 /*****************************************************************************
54 *****************************************************************************/
56 set_description( N_("ID3v1/2 and APEv1/2 tags parser" ) )
57 set_capability( "meta reader", 70 )
58 set_callbacks( ParseTags, NULL )
61 /*****************************************************************************
62 * ParseID3Tag : parse an id3tag into the info structures
63 *****************************************************************************/
64 static void ParseID3Tag( demux_meta_t *p_demux_meta, const uint8_t *p_data, int i_size )
66 struct id3_tag *p_id3_tag;
67 struct id3_frame *p_frame;
71 p_id3_tag = id3_tag_parse( p_data, i_size );
75 if( !p_demux_meta->p_meta )
76 p_demux_meta->p_meta = vlc_meta_New();
77 p_meta = p_demux_meta->p_meta;
79 #define ID_IS( a ) (!strcmp( p_frame->id, a ))
80 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
81 #define GET_STRING(frame,fidx) id3_ucs4_latin1duplicate( id3_field_getstring( &(frame)->fields[fidx] ) )
84 for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "UFID", i )) != NULL; i++ )
86 const char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
88 if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
90 id3_byte_t const * p_ufid;
91 id3_length_t i_ufidlen;
93 p_ufid = id3_field_getbinarydata( &p_frame->fields[1], &i_ufidlen );
94 char *psz_ufid = strndup( p_ufid, i_ufidlen );
96 vlc_meta_SetTrackID( p_meta, psz_ufid );
101 /* User defined text (TXXX) */
102 for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i )) != NULL; i++ )
104 /* 3 fields: 'encoding', 'description', 'value' */
105 char *psz_name = GET_STRING( p_frame, 1 );
106 char *psz_value = GET_STRING( p_frame, 2 );
108 vlc_meta_AddExtra( p_meta, psz_name, psz_value );
110 if( !strncmp( psz_name, "MusicBrainz Artist Id", 21 ) )
111 vlc_meta_SetArtistID( p_meta, psz_value );
112 if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
113 vlc_meta_SetAlbumID( p_meta, psz_value );
119 /* Relative volume adjustment */
120 for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "RVA2", i )) != NULL; i++ )
122 /* 2 fields: 'latin1', 'binary' */
123 const char *psz_type = id3_field_getlatin1( &p_frame->fields[0] );
124 if( !strcasecmp( psz_type, "track" ) || !strcasecmp( psz_type, "album" ) ||
125 !strcasecmp( psz_type, "normalize" ) )
127 id3_byte_t const * p_data;
130 p_data = id3_field_getbinarydata( &p_frame->fields[1], &i_data );
133 const unsigned int i_peak_size = p_data[3];
134 const float f_temp = GetWBE( &p_data[1] );
135 const float f_gain = f_temp / 512.0;
138 if( i_data < i_peak_size + 4 )
140 /* only master volume */
141 if( p_data[0] == 0x01 )
143 snprintf( psz_value, sizeof(psz_value), "%f", f_gain );
144 if( !strcasecmp( psz_type, "album" ) )
145 vlc_meta_AddExtra( p_meta, "REPLAYGAIN_ALBUM_GAIN", psz_value );
147 vlc_meta_AddExtra( p_meta, "REPLAYGAIN_TRACK_GAIN", psz_value );
148 /* XXX I have no idea what peak unit is ... */
150 i_data -= 4+i_peak_size;
155 /* TODO 'RGAD' if it is used somewhere */
157 /* T--- Text informations */
158 for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "T", i )) != NULL; i++ )
162 /* Special case TXXX is not the same beast */
163 if( ID_IS( "TXXX" ) )
166 i_strings = id3_field_getnstrings( &p_frame->fields[1] );
167 while( i_strings > 0 )
169 char *psz_temp = id3_ucs4_utf8duplicate(
170 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
172 if( ID_IS( ID3_FRAME_GENRE ) )
175 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
177 if( psz_temp != psz_endptr &&
178 i_genre >= 0 && i_genre < NUM_GENRES )
180 vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
185 vlc_meta_SetGenre( p_meta,psz_temp );
188 else if( ID_IS( ID3_FRAME_TITLE ) )
190 vlc_meta_SetTitle( p_meta, psz_temp );
192 else if( ID_IS( ID3_FRAME_ARTIST ) )
194 vlc_meta_SetArtist( p_meta, psz_temp );
196 else if( ID_IS( ID3_FRAME_YEAR ) )
198 vlc_meta_SetDate( p_meta, psz_temp );
200 else if( ID_IS( ID3_FRAME_COMMENT ) )
202 vlc_meta_SetDescription( p_meta, psz_temp );
204 else if( DESCR_IS( "Copyright" ) )
206 vlc_meta_SetCopyright( p_meta, psz_temp );
208 else if( DESCR_IS( "Publisher" ) )
210 vlc_meta_SetPublisher( p_meta, psz_temp );
212 else if( DESCR_IS( "Track number/position in set" ) )
214 vlc_meta_SetTrackNum( p_meta, psz_temp );
216 else if( DESCR_IS( "Album/movie/show title" ) )
218 vlc_meta_SetAlbum( p_meta, psz_temp );
220 else if( DESCR_IS( "Encoded by" ) )
222 vlc_meta_SetEncodedBy( p_meta, psz_temp );
224 else if( ID_IS ( "APIC" ) )
226 msg_Dbg( p_demux_meta, "** Has APIC **" );
228 else if( p_frame->description )
231 vlc_meta_AddExtra( p_meta, (char*)p_frame->description, psz_temp );
236 id3_tag_delete( p_id3_tag );
241 /*****************************************************************************
243 *****************************************************************************/
244 #define APE_TAG_HEADERSIZE (32)
245 static size_t GetAPEvXSize( const uint8_t *p_data, int i_data )
250 if( i_data < APE_TAG_HEADERSIZE ||
251 ( GetDWLE( &p_data[8] ) != 1000 && GetDWLE( &p_data[8] ) != 2000 ) || /* v1/v2 only */
252 strncmp( (char*)p_data, "APETAGEX", 8 ) ||
253 GetDWLE( &p_data[8+4+4] ) <= 0 )
256 i_body = GetDWLE( &p_data[8+4] );
257 flags = GetDWLE( &p_data[8+4+4] );
259 /* is it the header */
260 if( flags & (1<<29) )
261 return i_body + ( (flags&(1<<30)) ? APE_TAG_HEADERSIZE : 0 );
263 /* it is the footer */
264 return i_body + ( (flags&(1<<31)) ? APE_TAG_HEADERSIZE : 0 );
266 static void ParseAPEvXTag( demux_meta_t *p_demux_meta, const uint8_t *p_data, int i_data )
271 const uint8_t *p_header = NULL;
274 if( i_data < APE_TAG_HEADERSIZE )
277 b_start = !strncmp( (char*)&p_data[0], "APETAGEX", 8 );
278 b_end = !strncmp( (char*)&p_data[i_data-APE_TAG_HEADERSIZE], "APETAGEX", 8 );
279 if( !b_end && !b_start )
282 if( !p_demux_meta->p_meta )
283 p_demux_meta->p_meta = vlc_meta_New();
284 p_meta = p_demux_meta->p_meta;
288 p_header = &p_data[0];
289 p_data += APE_TAG_HEADERSIZE;
290 i_data -= APE_TAG_HEADERSIZE;
294 p_header = &p_data[i_data-APE_TAG_HEADERSIZE];
295 i_data -= APE_TAG_HEADERSIZE;
300 i_entry = GetDWLE( &p_header[8+4+4] );
304 while( i_entry > 0 && i_data >= 10 )
306 const int i_size = GetDWLE( &p_data[0] );
307 const uint32_t flags = GetDWLE( &p_data[4] );
311 strlcpy( psz_name, (char*)&p_data[8], sizeof(psz_name) );
312 n = strlen( psz_name );
318 if( i_data < i_size )
321 /* Retreive UTF-8 fields only */
322 if( ((flags>>1) & 0x03) == 0x00 )
324 /* FIXME list are separated by '\0' */
325 char *psz_value = strndup( (char*)&p_data[0], i_size );
327 EnsureUTF8( psz_name );
328 EnsureUTF8( psz_value );
329 #define IS(s) (!strcasecmp( psz_name, s ) )
331 vlc_meta_SetTitle( p_meta, psz_value );
332 else if( IS( "Artist" ) )
333 vlc_meta_SetArtist( p_meta, psz_value );
334 else if( IS( "Album" ) )
335 vlc_meta_SetAlbum( p_meta, psz_value );
336 else if( IS( "Publisher" ) )
337 vlc_meta_SetPublisher( p_meta, psz_value );
338 else if( IS( "Track" ) )
340 char *p = strchr( psz_value, '/' );
343 vlc_meta_SetTrackNum( p_meta, psz_value );
345 else if( IS( "Comment" ) )
346 vlc_meta_SetDescription( p_meta, psz_value );
347 else if( IS( "Copyright" ) )
348 vlc_meta_SetCopyright( p_meta, psz_value );
349 else if( IS( "Year" ) )
350 vlc_meta_SetDate( p_meta, psz_value );
351 else if( IS( "Genre" ) )
352 vlc_meta_SetGenre( p_meta, psz_value );
353 else if( IS( "Language" ) )
354 vlc_meta_SetLanguage( p_meta, psz_value );
356 vlc_meta_AddExtra( p_meta, psz_name, psz_value );
367 /*****************************************************************************
368 * CheckFooter: check for ID3/APE at the end of the file
369 * CheckHeader: check for ID3/APE at the begining of the file
370 *****************************************************************************/
371 static void CheckFooter( demux_meta_t *p_demux_meta )
373 demux_t *p_demux = (demux_t *)p_demux_meta->p_demux;
374 const int64_t i_pos = stream_Size( p_demux->s );
375 const size_t i_peek = 128+APE_TAG_HEADERSIZE;
376 const uint8_t *p_peek;
377 const uint8_t *p_peek_id3;
378 int64_t i_id3v2_pos = -1;
379 int64_t i_apevx_pos = -1;
386 if( stream_Seek( p_demux->s, i_pos - i_peek ) )
389 if( stream_Peek( p_demux->s, &p_peek, i_peek ) < i_peek )
391 p_peek_id3 = &p_peek[APE_TAG_HEADERSIZE];
393 /* Check/Parse ID3v1 */
394 i_id3v1_size = id3_tag_query( p_peek_id3, ID3_TAG_QUERYSIZE );
395 if( i_id3v1_size == 128 )
397 msg_Dbg( p_demux, "found ID3v1 tag" );
398 ParseID3Tag( p_demux_meta, p_peek_id3, i_id3v1_size );
401 /* Compute ID3v2 position */
402 i_id3v2_size = -id3_tag_query( &p_peek_id3[128-ID3_TAG_QUERYSIZE], ID3_TAG_QUERYSIZE );
403 if( i_id3v2_size > 0 )
404 i_id3v2_pos = i_pos - i_id3v2_size;
406 /* Compute APE2v2 position */
407 i_apevx_size = GetAPEvXSize( &p_peek[128+0], APE_TAG_HEADERSIZE );
408 if( i_apevx_size > 0 )
410 i_apevx_pos = i_pos - i_apevx_size;
412 else if( i_id3v1_size > 0 )
414 /* it can be before ID3v1 */
415 i_apevx_size = GetAPEvXSize( p_peek, APE_TAG_HEADERSIZE );
416 if( i_apevx_size > 0 )
417 i_apevx_pos = i_pos - 128 - i_apevx_size;
420 if( i_id3v2_pos > 0 && i_apevx_pos > 0 )
423 "Both ID3v2 and APEv1/2 at the end of file, ignoring APEv1/2" );
428 if( i_id3v2_pos > 0 )
430 if( !stream_Seek( p_demux->s, i_id3v2_pos ) &&
431 stream_Peek( p_demux->s, &p_peek, i_id3v2_size ) == i_id3v2_size )
433 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
434 ParseID3Tag( p_demux_meta, p_peek, i_id3v2_size );
439 if( i_apevx_pos > 0 )
441 if( !stream_Seek( p_demux->s, i_apevx_pos ) &&
442 stream_Peek( p_demux->s, &p_peek, i_apevx_size ) == i_apevx_size )
444 msg_Dbg( p_demux, "found APEvx tag at end of file" );
445 ParseAPEvXTag( p_demux_meta, p_peek, i_apevx_size );
449 static void CheckHeader( demux_meta_t *p_demux_meta )
451 const uint8_t *p_peek;
454 demux_t *p_demux = (demux_t *)p_demux_meta->p_demux;
456 if( stream_Seek( p_demux->s, 0 ) )
459 /* Test ID3v2 first */
460 if( stream_Peek( p_demux->s, &p_peek, ID3_TAG_QUERYSIZE ) != ID3_TAG_QUERYSIZE )
462 i_size = id3_tag_query( p_peek, ID3_TAG_QUERYSIZE );
464 stream_Peek( p_demux->s, &p_peek, i_size ) == i_size )
466 msg_Dbg( p_demux, "found ID3v2 tag" );
467 ParseID3Tag( p_demux_meta, p_peek, i_size );
472 if( stream_Peek( p_demux->s, &p_peek, APE_TAG_HEADERSIZE ) != APE_TAG_HEADERSIZE )
474 i_size = GetAPEvXSize( p_peek, APE_TAG_HEADERSIZE );
476 stream_Peek( p_demux->s, &p_peek, i_size ) == i_size )
478 msg_Dbg( p_demux, "found APEv1/2 tag" );
479 ParseAPEvXTag( p_demux_meta, p_peek, i_size );
483 /*****************************************************************************
484 * ParseTags: check if ID3/APE tags at common locations.
485 ****************************************************************************/
486 static int ParseTags( vlc_object_t *p_this )
488 demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
489 demux_t *p_demux = (demux_t *)p_demux_meta->p_demux;
493 msg_Dbg( p_demux_meta, "checking for ID3v1/2 and APEv1/2 tags" );
494 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
498 i_init = stream_Tell( p_demux->s );
500 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
501 p_demux_meta->p_meta = NULL;
504 CheckFooter( p_demux_meta );
507 CheckHeader( p_demux_meta );
510 * Demuxer will not see tags at the start as src/input/demux.c skips it
513 stream_Seek( p_demux->s, i_init );
514 if( !p_demux_meta->p_meta && p_demux_meta->i_attachments <= 0 )