1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: id3tag.c,v 1.13 2003/10/25 00:49:14 sam 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 p_id3_tag = id3_tag_parse( p_data, i_size );
80 p_category = input_InfoCategory( p_input, "ID3" );
83 while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
85 i_strings = id3_field_getnstrings( &p_frame->fields[1] );
86 while ( i_strings > 0 )
88 psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
89 if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
93 i_genre = strtol( psz_temp, &psz_endptr, 10 );
94 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
96 input_AddInfo( p_category, (char *)p_frame->description, ppsz_genres[atoi(psz_temp)]);
100 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
103 else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
105 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
108 p_playlist->pp_items[p_playlist->i_index]->psz_name = strdup( psz_temp );
109 vlc_object_release( p_playlist );
111 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
113 else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
115 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
118 p_playlist->pp_items[p_playlist->i_index]->psz_author = strdup( psz_temp );
119 vlc_object_release( p_playlist );
121 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
125 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
131 id3_tag_delete( p_id3_tag );
132 val.b_bool = VLC_TRUE;
133 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
136 /*****************************************************************************
137 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
138 * if it's at the start of the file
139 ****************************************************************************/
140 static int ParseID3Tags( vlc_object_t *p_this )
142 input_thread_t *p_input;
147 if ( p_this->i_object_type != VLC_OBJECT_INPUT )
149 return( VLC_EGENERIC );
151 p_input = (input_thread_t *)p_this;
153 msg_Dbg( p_input, "Checking for ID3 tag" );
155 if ( p_input->stream.b_seekable &&
156 p_input->stream.i_method != INPUT_METHOD_NETWORK )
158 stream_position_t pos;
160 /*look for a id3v1 tag at the end of the file*/
161 input_Tell( p_input, &pos );
162 if ( pos.i_size >128 )
164 input_AccessReinit( p_input );
165 p_input->pf_seek( p_input, pos.i_size - 128 );
167 /* get 10 byte id3 header */
168 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
170 msg_Err( p_input, "cannot peek()" );
171 return( VLC_EGENERIC );
173 i_size2 = id3_tag_query( p_peek, 10 );
174 if ( i_size2 == 128 )
176 /* peek the entire tag */
177 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
179 msg_Err( p_input, "cannot peek()" );
180 return( VLC_EGENERIC );
182 ParseID3Tag( p_input, p_peek, i_size2 );
185 /* look for id3v2.4 tag at end of file */
186 /* get 10 byte id3 footer */
187 if( input_Peek( p_input, &p_peek, 128 ) < 128 )
189 msg_Err( p_input, "cannot peek()" );
190 return( VLC_EGENERIC );
192 i_size2 = id3_tag_query( p_peek + 118, 10 );
193 if ( i_size2 < 0 && pos.i_size > -i_size2 )
194 { /* id3v2.4 footer found */
195 input_AccessReinit( p_input );
196 p_input->pf_seek( p_input, pos.i_size + i_size2 );
197 /* peek the entire tag */
198 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
200 msg_Err( p_input, "cannot peek()" );
201 return( VLC_EGENERIC );
203 ParseID3Tag( p_input, p_peek, i_size2 );
206 input_AccessReinit( p_input );
207 p_input->pf_seek( p_input, 0 );
209 /* get 10 byte id3 header */
210 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
212 msg_Err( p_input, "cannot peek()" );
213 return( VLC_EGENERIC );
216 i_size = id3_tag_query( p_peek, 10 );
219 return( VLC_SUCCESS );
222 /* peek the entire tag */
223 if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
225 msg_Err( p_input, "cannot peek()" );
226 return( VLC_EGENERIC );
229 ParseID3Tag( p_input, p_peek, i_size );
230 msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
231 p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
232 return( VLC_SUCCESS );