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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
39 #include "id3genres.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int ParseID3Tags ( vlc_object_t * );
46 /*****************************************************************************
48 *****************************************************************************/
50 set_description( _("ID3 tags parser" ) );
51 set_capability( "meta reader", 70 );
52 set_callbacks( ParseID3Tags, NULL );
55 /*****************************************************************************
56 * Definitions of structures and functions used by this plugins
57 *****************************************************************************/
59 /*****************************************************************************
60 * ParseID3Tag : parse an id3tag into the info structures
61 *****************************************************************************/
62 static void ParseID3Tag( demux_t *p_demux, uint8_t *p_data, int i_size )
64 struct id3_tag *p_id3_tag;
65 struct id3_frame *p_frame;
68 p_id3_tag = id3_tag_parse( p_data, i_size );
69 if( !p_id3_tag ) return;
71 if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
73 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);
74 #define ID_IS( a ) (!strcmp( p_frame->id, a ))
75 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
77 while ( ( p_frame = id3_tag_findframe( p_id3_tag, "UFID", i ) ) )
79 char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
81 if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
83 id3_byte_t const * p_ufid;
84 id3_length_t i_ufidlen;
86 p_ufid = id3_field_getbinarydata( &p_frame->fields[1], &i_ufidlen );
87 char *psz_ufid = strndup( p_ufid, i_ufidlen );
89 vlc_meta_SetTrackID( p_meta, psz_ufid );
97 while( ( p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i ) ) )
99 char *psz_desc = id3_ucs4_latin1duplicate(
100 id3_field_getstring( &p_frame->fields[1] ) );
102 if ( ! strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
104 char *psz_artistid = id3_ucs4_latin1duplicate(
105 id3_field_getstring( &p_frame->fields[2] ) );
106 vlc_meta_SetArtistID( p_meta, psz_artistid );
107 free( psz_artistid );
110 if ( ! strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
112 char *psz_albumid = id3_ucs4_latin1duplicate(
113 id3_field_getstring( &p_frame->fields[2] ) );
114 vlc_meta_SetAlbumID( p_meta, psz_albumid );
124 while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
126 int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
128 while( i_strings > 0 )
130 char *psz_temp = id3_ucs4_utf8duplicate(
131 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
133 if( ID_IS( ID3_FRAME_GENRE ) )
136 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
138 if( psz_temp != psz_endptr &&
139 i_genre >= 0 && i_genre < NUM_GENRES )
141 vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
146 vlc_meta_SetGenre( p_meta,psz_temp );
149 else if( ID_IS( ID3_FRAME_TITLE ) )
151 vlc_meta_SetTitle( p_meta, psz_temp );
153 else if( ID_IS( ID3_FRAME_ARTIST ) )
155 vlc_meta_SetArtist( p_meta, psz_temp );
157 else if( ID_IS( ID3_FRAME_YEAR ) )
159 vlc_meta_SetDate( p_meta, psz_temp );
161 else if( ID_IS( ID3_FRAME_COMMENT ) )
163 vlc_meta_SetDescription( p_meta, psz_temp );
165 else if( DESCR_IS( "Copyright" ) )
167 vlc_meta_SetCopyright( p_meta, psz_temp );
169 else if( DESCR_IS( "Publisher" ) )
171 vlc_meta_SetPublisher( p_meta, psz_temp );
173 else if( DESCR_IS( "Track number/position in set" ) )
175 vlc_meta_SetTracknum( p_meta, psz_temp );
177 else if( DESCR_IS( "Album/movie/show title" ) )
179 vlc_meta_SetAlbum( p_meta, psz_temp );
181 else if( DESCR_IS( "Encoded by" ) )
183 vlc_meta_SetEncodedBy( p_meta, psz_temp );
185 else if( ID_IS ( "APIC" ) )
187 msg_Dbg( p_demux, "** Has APIC **" );
189 else if( p_frame->description )
190 { /* Unhandled meta*/
191 msg_Warn( p_demux, "Fixme: unhandled ID3 metatag, %s", p_frame->description );
197 id3_tag_delete( p_id3_tag );
200 /*****************************************************************************
201 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
202 * if it's at the start of the file
203 ****************************************************************************/
204 static int ParseID3Tags( vlc_object_t *p_this )
206 demux_t *p_demux = (demux_t *)p_this;
208 vlc_bool_t b_seekable;
209 int64_t i_init, i_pos;
212 p_demux->p_private = NULL;
214 msg_Dbg( p_demux, "checking for ID3 tag" );
216 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
217 if( !b_seekable ) return VLC_SUCCESS;
219 i_init = stream_Tell( p_demux->s );
222 * Look for a ID3v1 tag at the end of the file
224 i_init = stream_Tell( p_demux->s );
225 i_pos = stream_Size( p_demux->s );
227 while( i_pos > 128 ) /* while used so we can break; */
229 stream_Seek( p_demux->s, i_pos - 128 );
231 /* get 10 byte id3 header */
232 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
234 i_size = id3_tag_query( p_peek, 10 );
237 /* peek the entire tag */
238 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
240 msg_Dbg( p_demux, "found ID3v1 tag" );
241 ParseID3Tag( p_demux, p_peek, i_size );
244 /* look for ID3v2.4 tag at end of file */
245 /* get 10 byte ID3 footer */
246 if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
248 i_size = id3_tag_query( p_peek + 118, 10 );
249 if( i_size < 0 && i_pos > -i_size )
251 /* id3v2.4 footer found */
252 stream_Seek( p_demux->s , i_pos + i_size );
253 /* peek the entire tag */
254 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
256 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
257 ParseID3Tag( p_demux, p_peek, i_size );
263 * Get 10 byte id3 header
265 stream_Seek( p_demux->s, 0 );
266 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
268 if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
270 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
272 msg_Dbg( p_demux, "found ID3v2 tag" );
273 ParseID3Tag( p_demux, p_peek, i_size );
276 stream_Seek( p_demux->s, i_init );