1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: id3tag.c,v 1.14 2003/10/29 17:32:54 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>
34 #include <sys/types.h>
37 #include "id3genres.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int ParseID3Tags ( vlc_object_t * );
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( _("id3 tag parser using libid3tag" ) );
49 set_capability( "id3", 70 );
50 set_callbacks( ParseID3Tags, NULL );
53 /*****************************************************************************
54 * Definitions of structures and functions used by this plugins
55 *****************************************************************************/
57 /*****************************************************************************
58 * ParseID3Tag : parse an id3tag into the info structures
59 *****************************************************************************/
60 static void ParseID3Tag( input_thread_t *p_input, uint8_t *p_data, int i_size )
62 playlist_t * p_playlist;
63 struct id3_tag * p_id3_tag;
64 struct id3_frame * p_frame;
65 input_info_category_t * p_category;
71 var_Get( p_input, "demuxed-id3", &val );
75 msg_Dbg( p_input, "The ID3 tag was already parsed" );
79 val.b_bool = VLC_FALSE;
80 p_id3_tag = id3_tag_parse( p_data, i_size );
81 p_category = input_InfoCategory( p_input, "ID3" );
84 while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
86 i_strings = id3_field_getnstrings( &p_frame->fields[1] );
87 while ( i_strings > 0 )
89 psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
90 if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
94 i_genre = strtol( psz_temp, &psz_endptr, 10 );
95 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
97 input_AddInfo( p_category, (char *)p_frame->description,
98 ppsz_genres[atoi(psz_temp)]);
102 input_AddInfo( p_category, (char *)p_frame->description,
106 else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
108 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
112 p_playlist->pp_items[p_playlist->i_index]->psz_name =
114 val.b_bool = VLC_TRUE;
115 vlc_object_release( p_playlist );
117 input_AddInfo( p_category, (char *)p_frame->description,
120 else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
122 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
126 p_playlist->pp_items[p_playlist->i_index]->psz_author =
128 val.b_bool = VLC_TRUE;
129 vlc_object_release( p_playlist );
131 input_AddInfo( p_category, (char *)p_frame->description,
136 input_AddInfo( p_category, (char *)p_frame->description,
143 id3_tag_delete( p_id3_tag );
144 if(val.b_bool == VLC_TRUE )
146 p_playlist = vlc_object_find( p_input,
147 VLC_OBJECT_PLAYLIST, FIND_PARENT );
150 val.b_bool = VLC_TRUE;
151 var_Set( p_playlist, "intf-change", val );
152 vlc_object_release( p_playlist );
155 val.b_bool = VLC_TRUE;
156 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
159 /*****************************************************************************
160 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
161 * if it's at the start of the file
162 ****************************************************************************/
163 static int ParseID3Tags( vlc_object_t *p_this )
165 input_thread_t *p_input;
170 if ( p_this->i_object_type != VLC_OBJECT_INPUT )
172 return( VLC_EGENERIC );
174 p_input = (input_thread_t *)p_this;
176 msg_Dbg( p_input, "Checking for ID3 tag" );
178 if ( p_input->stream.b_seekable &&
179 p_input->stream.i_method != INPUT_METHOD_NETWORK )
181 stream_position_t pos;
183 /*look for a id3v1 tag at the end of the file*/
184 input_Tell( p_input, &pos );
185 if ( pos.i_size >128 )
187 input_AccessReinit( p_input );
188 p_input->pf_seek( p_input, pos.i_size - 128 );
190 /* get 10 byte id3 header */
191 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
193 msg_Err( p_input, "cannot peek()" );
194 return( VLC_EGENERIC );
196 i_size2 = id3_tag_query( p_peek, 10 );
197 if ( i_size2 == 128 )
199 /* peek the entire tag */
200 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
202 msg_Err( p_input, "cannot peek()" );
203 return( VLC_EGENERIC );
205 ParseID3Tag( p_input, p_peek, i_size2 );
208 /* look for id3v2.4 tag at end of file */
209 /* get 10 byte id3 footer */
210 if( input_Peek( p_input, &p_peek, 128 ) < 128 )
212 msg_Err( p_input, "cannot peek()" );
213 return( VLC_EGENERIC );
215 i_size2 = id3_tag_query( p_peek + 118, 10 );
216 if ( i_size2 < 0 && pos.i_size > -i_size2 )
217 { /* id3v2.4 footer found */
218 input_AccessReinit( p_input );
219 p_input->pf_seek( p_input, pos.i_size + i_size2 );
220 /* peek the entire tag */
221 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
223 msg_Err( p_input, "cannot peek()" );
224 return( VLC_EGENERIC );
226 ParseID3Tag( p_input, p_peek, i_size2 );
229 input_AccessReinit( p_input );
230 p_input->pf_seek( p_input, 0 );
232 /* get 10 byte id3 header */
233 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
235 msg_Err( p_input, "cannot peek()" );
236 return( VLC_EGENERIC );
239 i_size = id3_tag_query( p_peek, 10 );
242 return( VLC_SUCCESS );
245 /* peek the entire tag */
246 if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
248 msg_Err( p_input, "cannot peek()" );
249 return( VLC_EGENERIC );
252 ParseID3Tag( p_input, p_peek, i_size );
253 msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
254 p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
255 return( VLC_SUCCESS );