1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: id3tag.c,v 1.20 2004/01/29 17:51:07 zorglub Exp $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
36 #include <sys/types.h>
39 #include "id3genres.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int ParseID3Tags ( vlc_object_t * );
46 /*****************************************************************************
48 *****************************************************************************/
50 set_description( _("ID3 tag parser using libid3tag" ) );
51 set_capability( "id3", 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( input_thread_t *p_input, uint8_t *p_data, int i_size )
64 playlist_t *p_playlist;
65 struct id3_tag *p_id3_tag;
66 struct id3_frame *p_frame;
67 input_info_category_t *p_category;
72 var_Get( p_input, "demuxed-id3", &val );
75 msg_Dbg( p_input, "the ID3 tag was already parsed" );
79 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
81 val.b_bool = VLC_FALSE;
82 p_id3_tag = id3_tag_parse( p_data, i_size );
83 p_category = input_InfoCategory( p_input, "ID3" );
86 while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
88 playlist_item_t *p_item = playlist_ItemGetByPos( p_playlist, -1 );
91 msg_Err( p_input, "Unable to get item" );
95 vlc_mutex_lock( &p_item->lock );
97 int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
99 while ( i_strings > 0 )
101 psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
102 if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
106 i_genre = strtol( psz_temp, &psz_endptr, 10 );
107 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
109 input_AddInfo( p_category, (char *)p_frame->description,
110 ppsz_genres[atoi(psz_temp)]);
111 playlist_ItemAddInfo( p_item, "ID3",
112 (char *)p_frame->description,
113 ppsz_genres[atoi(psz_temp)]);
117 input_AddInfo( p_category, (char *)p_frame->description,
119 playlist_ItemAddInfo( p_item, "ID3",
120 (char *)p_frame->description,
124 else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
128 if( p_item->psz_name )
130 free( p_item->psz_name );
132 p_item->psz_name = strdup( psz_temp );;
134 val.b_bool = VLC_TRUE;
136 input_AddInfo( p_category, (char *)p_frame->description,
138 playlist_ItemAddInfo( p_item, "ID3",
139 (char *)p_frame->description,
142 else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
146 playlist_ItemAddInfo( p_item,
147 _("General"), _("Author"), psz_temp);
148 val.b_bool = VLC_TRUE;
150 input_AddInfo( p_category, (char *)p_frame->description,
152 playlist_ItemAddInfo( p_item, "ID3",
153 (char *)p_frame->description,
158 input_AddInfo( p_category, (char *)p_frame->description,
160 playlist_ItemAddInfo( p_item, "ID3",
161 (char *)p_frame->description,
167 vlc_mutex_unlock( &p_item->lock );
169 id3_tag_delete( p_id3_tag );
170 if(val.b_bool == VLC_TRUE )
174 val.b_bool = p_playlist->i_index;
175 var_Set( p_playlist, "item-change", val );
178 val.b_bool = VLC_TRUE;
179 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
183 vlc_object_release( p_playlist );
187 /*****************************************************************************
188 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
189 * if it's at the start of the file
190 ****************************************************************************/
191 static int ParseID3Tags( vlc_object_t *p_this )
193 input_thread_t *p_input;
198 if ( p_this->i_object_type != VLC_OBJECT_INPUT )
200 return( VLC_EGENERIC );
202 p_input = (input_thread_t *)p_this;
204 msg_Dbg( p_input, "checking for ID3 tag" );
206 if ( p_input->stream.b_seekable &&
207 p_input->stream.i_method != INPUT_METHOD_NETWORK )
211 /*look for a ID3v1 tag at the end of the file*/
212 i_pos = stream_Size( p_input->s );
216 input_AccessReinit( p_input );
217 p_input->pf_seek( p_input, i_pos - 128 );
219 /* get 10 byte id3 header */
220 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
222 msg_Err( p_input, "cannot peek()" );
223 return( VLC_EGENERIC );
226 i_size2 = id3_tag_query( p_peek, 10 );
227 if ( i_size2 == 128 )
229 /* peek the entire tag */
230 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
232 msg_Err( p_input, "cannot peek()" );
233 return( VLC_EGENERIC );
235 msg_Dbg( p_input, "found ID3v1 tag" );
236 ParseID3Tag( p_input, p_peek, i_size2 );
239 /* look for ID3v2.4 tag at end of file */
240 /* get 10 byte ID3 footer */
241 if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
243 msg_Err( p_input, "cannot peek()" );
244 return( VLC_EGENERIC );
246 i_size2 = id3_tag_query( p_peek + 118, 10 );
247 if ( i_size2 < 0 && i_pos > -i_size2 )
248 { /* id3v2.4 footer found */
249 input_AccessReinit( p_input );
250 p_input->pf_seek( p_input, i_pos + i_size2 );
251 /* peek the entire tag */
252 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
254 msg_Err( p_input, "cannot peek()" );
255 return( VLC_EGENERIC );
257 msg_Dbg( p_input, "found ID3v2 tag at end of file" );
258 ParseID3Tag( p_input, p_peek, i_size2 );
261 input_AccessReinit( p_input );
262 p_input->pf_seek( p_input, 0 );
264 /* get 10 byte id3 header */
265 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
267 msg_Err( p_input, "cannot peek()" );
268 return( VLC_EGENERIC );
271 i_size = id3_tag_query( p_peek, 10 );
274 return( VLC_SUCCESS );
277 /* Read the entire tag */
278 p_peek = malloc( i_size );
279 if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
281 msg_Err( p_input, "cannot read ID3 tag" );
282 if( p_peek ) free( p_peek );
283 return( VLC_EGENERIC );
286 ParseID3Tag( p_input, p_peek, i_size );
287 msg_Dbg( p_input, "found ID3v2 tag" );
290 return( VLC_SUCCESS );