1 /*****************************************************************************
2 * id3tag.c: id3 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 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc_interface.h>
34 #include <vlc_demux.h>
35 #include <vlc_playlist.h>
37 #include <sys/types.h>
42 #include "id3genres.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int ParseID3Tags ( vlc_object_t * );
49 /*****************************************************************************
51 *****************************************************************************/
53 set_description( _("ID3 tags parser" ) );
54 set_capability( "meta reader", 70 );
55 set_callbacks( ParseID3Tags, NULL );
58 /*****************************************************************************
59 * Definitions of structures and functions used by this plugins
60 *****************************************************************************/
62 /*****************************************************************************
63 * ParseID3Tag : parse an id3tag into the info structures
64 *****************************************************************************/
65 static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
67 struct id3_tag *p_id3_tag;
68 struct id3_frame *p_frame;
71 p_id3_tag = id3_tag_parse( p_data, i_size );
72 if( !p_id3_tag ) return;
74 if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
76 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);
77 #define ID_IS( a ) (!strcmp( p_frame->id, a ))
78 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
80 while ( ( p_frame = id3_tag_findframe( p_id3_tag, "UFID", i ) ) )
82 char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
84 if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
86 id3_byte_t const * p_ufid;
87 id3_length_t i_ufidlen;
89 p_ufid = (id3_byte_t const *)
90 id3_field_getbinarydata(
93 char *psz_ufid = strndup( p_ufid, i_ufidlen );
95 vlc_meta_SetTrackID( p_meta, psz_ufid );
103 while( ( p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i ) ) )
105 char *psz_desc = id3_ucs4_latin1duplicate(
106 id3_field_getstring( &p_frame->fields[1] ) );
108 if ( ! strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
110 char *psz_artistid = id3_ucs4_latin1duplicate(
111 id3_field_getstring( &p_frame->fields[2] ) );
112 vlc_meta_SetArtistID( p_meta, psz_artistid );
113 free( psz_artistid );
116 if ( ! strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
118 char *psz_albumid = id3_ucs4_latin1duplicate(
119 id3_field_getstring( &p_frame->fields[2] ) );
120 vlc_meta_SetAlbumID( p_meta, psz_albumid );
130 while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
132 int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
134 while( i_strings > 0 )
136 char *psz_temp = id3_ucs4_utf8duplicate(
137 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
139 if( ID_IS( ID3_FRAME_GENRE ) )
142 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
144 if( psz_temp != psz_endptr &&
145 i_genre >= 0 && i_genre < NUM_GENRES )
147 vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
152 vlc_meta_SetGenre( p_meta,psz_temp );
155 else if( ID_IS( ID3_FRAME_TITLE ) )
157 vlc_meta_SetTitle( p_meta, psz_temp );
159 else if( ID_IS( ID3_FRAME_ARTIST ) )
161 vlc_meta_SetArtist( p_meta, psz_temp );
163 else if( ID_IS( ID3_FRAME_YEAR ) )
165 vlc_meta_SetDate( p_meta, psz_temp );
167 else if( ID_IS( ID3_FRAME_COMMENT ) )
169 vlc_meta_SetDescription( p_meta, psz_temp );
171 else if( DESCR_IS( "Copyright" ) )
173 vlc_meta_SetCopyright( p_meta, psz_temp );
175 else if( DESCR_IS( "Publisher" ) )
177 vlc_meta_SetPublisher( p_meta, psz_temp );
179 else if( DESCR_IS( "Track number/position in set" ) )
181 vlc_meta_SetTracknum( p_meta, psz_temp );
183 else if( DESCR_IS( "Album/movie/show title" ) )
185 vlc_meta_SetAlbum( p_meta, psz_temp );
187 else if( DESCR_IS( "Encoded by" ) )
189 vlc_meta_SetEncodedBy( p_meta, psz_temp );
191 else if( ID_IS ( "APIC" ) )
193 msg_Dbg( p_demux, "** Has APIC **" );
195 else if( p_frame->description )
196 { /* Unhandled meta*/
197 msg_Warn( p_demux, "Fixme: unhandled ID3 metatag, %s", p_frame->description );
203 id3_tag_delete( p_id3_tag );
206 /*****************************************************************************
207 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
208 * if it's at the start of the file
209 ****************************************************************************/
210 static int ParseID3Tags( vlc_object_t *p_this )
212 demux_t *p_demux = (demux_t *)p_this;
214 vlc_bool_t b_seekable;
215 int64_t i_init, i_pos;
218 p_demux->p_private = NULL;
220 msg_Dbg( p_demux, "checking for ID3 tag" );
222 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
223 if( !b_seekable ) return VLC_SUCCESS;
225 i_init = stream_Tell( p_demux->s );
228 * Look for a ID3v1 tag at the end of the file
230 i_init = stream_Tell( p_demux->s );
231 i_pos = stream_Size( p_demux->s );
233 while( i_pos > 128 ) /* while used so we can break; */
235 stream_Seek( p_demux->s, i_pos - 128 );
237 /* get 10 byte id3 header */
238 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
240 i_size = id3_tag_query( p_peek, 10 );
243 /* peek the entire tag */
244 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
246 msg_Dbg( p_demux, "found ID3v1 tag" );
247 ParseID3Tag( p_demux, p_peek, i_size );
250 /* look for ID3v2.4 tag at end of file */
251 /* get 10 byte ID3 footer */
252 if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
254 i_size = id3_tag_query( p_peek + 118, 10 );
255 if( i_size < 0 && i_pos > -i_size )
257 /* id3v2.4 footer found */
258 stream_Seek( p_demux->s , i_pos + i_size );
259 /* peek the entire tag */
260 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
262 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
263 ParseID3Tag( p_demux, p_peek, i_size );
269 * Get 10 byte id3 header
271 stream_Seek( p_demux->s, 0 );
272 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
274 if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
276 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
278 msg_Dbg( p_demux, "found ID3v2 tag" );
279 ParseID3Tag( p_demux, p_peek, i_size );
282 stream_Seek( p_demux->s, i_init );