1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
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 struct id3_tag *p_id3_tag;
65 struct id3_frame *p_frame;
70 var_Get( p_input, "demuxed-id3", &val );
73 msg_Dbg( p_input, "the ID3 tag was already parsed" );
77 val.b_bool = VLC_FALSE;
78 p_id3_tag = id3_tag_parse( p_data, i_size );
81 while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
85 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_Control( p_input, INPUT_ADD_INFO, "ID3",
98 (char *)p_frame->description,
99 ppsz_genres[atoi(psz_temp)]);
103 input_Control( p_input, INPUT_ADD_INFO, "ID3",
104 (char *)p_frame->description, psz_temp);
107 else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
109 input_Control( p_input, INPUT_SET_NAME, psz_temp );
110 input_Control( p_input, INPUT_ADD_INFO, "ID3",
111 (char *)p_frame->description, psz_temp );
113 else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
115 input_Control( p_input, INPUT_ADD_INFO,
116 _("General"), _("Author"), psz_temp );
117 input_Control( p_input, INPUT_ADD_INFO, "ID3",
118 (char *)p_frame->description, psz_temp );
122 input_Control( p_input, INPUT_ADD_INFO, "ID3",
123 (char *)p_frame->description, psz_temp );
129 id3_tag_delete( p_id3_tag );
131 val.b_bool = VLC_TRUE;
132 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
135 /*****************************************************************************
136 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
137 * if it's at the start of the file
138 ****************************************************************************/
139 static int ParseID3Tags( vlc_object_t *p_this )
141 input_thread_t *p_input = NULL;
146 if ( p_this->i_object_type == VLC_OBJECT_INPUT )
148 p_input = (input_thread_t *)p_this;
150 if( p_input == NULL )
152 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
153 if( p_input == NULL )
159 msg_Dbg( p_input, "checking for ID3 tag" );
161 if ( p_input->stream.b_seekable &&
162 p_input->stream.i_method != INPUT_METHOD_NETWORK )
166 /*look for a ID3v1 tag at the end of the file*/
167 i_pos = stream_Size( p_input->s );
171 input_AccessReinit( p_input );
172 p_input->pf_seek( p_input, i_pos - 128 );
174 /* get 10 byte id3 header */
175 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
177 msg_Err( p_input, "cannot peek()" );
178 vlc_object_release( p_input );
179 return( VLC_EGENERIC );
182 i_size2 = id3_tag_query( p_peek, 10 );
183 if ( i_size2 == 128 )
185 /* peek the entire tag */
186 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
188 msg_Err( p_input, "cannot peek()" );
189 vlc_object_release( p_input );
190 return( VLC_EGENERIC );
192 msg_Dbg( p_input, "found ID3v1 tag" );
193 ParseID3Tag( p_input, p_peek, i_size2 );
196 /* look for ID3v2.4 tag at end of file */
197 /* get 10 byte ID3 footer */
198 if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
200 msg_Err( p_input, "cannot peek()" );
201 vlc_object_release( p_input );
202 return( VLC_EGENERIC );
204 i_size2 = id3_tag_query( p_peek + 118, 10 );
205 if ( i_size2 < 0 && i_pos > -i_size2 )
206 { /* id3v2.4 footer found */
207 input_AccessReinit( p_input );
208 p_input->pf_seek( p_input, i_pos + i_size2 );
209 /* peek the entire tag */
210 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
212 msg_Err( p_input, "cannot peek()" );
213 vlc_object_release( p_input );
214 return( VLC_EGENERIC );
216 msg_Dbg( p_input, "found ID3v2 tag at end of file" );
217 ParseID3Tag( p_input, p_peek, i_size2 );
220 input_AccessReinit( p_input );
221 p_input->pf_seek( p_input, 0 );
223 /* get 10 byte id3 header */
224 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
226 msg_Err( p_input, "cannot peek()" );
227 vlc_object_release( p_input );
228 return( VLC_EGENERIC );
231 i_size = id3_tag_query( p_peek, 10 );
234 vlc_object_release( p_input );
235 return( VLC_SUCCESS );
238 /* Read the entire tag */
239 p_peek = malloc( i_size );
240 if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
242 msg_Err( p_input, "cannot read ID3 tag" );
243 if( p_peek ) free( p_peek );
244 vlc_object_release( p_input );
245 return( VLC_EGENERIC );
248 ParseID3Tag( p_input, p_peek, i_size );
249 msg_Dbg( p_input, "found ID3v2 tag" );
252 vlc_object_release( p_input );
253 return( VLC_SUCCESS );