1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: id3tag.c,v 1.19 2004/01/25 20:05:29 hartman 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 = p_playlist ? p_playlist->pp_items[p_playlist->i_index] : NULL;
90 int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
92 while ( i_strings > 0 )
94 psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
95 if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
99 i_genre = strtol( psz_temp, &psz_endptr, 10 );
100 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
102 input_AddInfo( p_category, (char *)p_frame->description,
103 ppsz_genres[atoi(psz_temp)]);
104 playlist_AddItemInfo( p_item, "ID3",
105 (char *)p_frame->description,
106 ppsz_genres[atoi(psz_temp)]);
110 input_AddInfo( p_category, (char *)p_frame->description,
112 playlist_AddItemInfo( p_item, "ID3",
113 (char *)p_frame->description,
117 else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
121 if( p_item->psz_name )
123 free( p_item->psz_name );
125 p_item->psz_name = strdup( psz_temp );;
127 val.b_bool = VLC_TRUE;
129 input_AddInfo( p_category, (char *)p_frame->description,
131 playlist_AddItemInfo( p_item, "ID3",
132 (char *)p_frame->description,
135 else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
139 playlist_AddItemInfo( p_item,
140 _("General"), _("Author"), psz_temp);
141 val.b_bool = VLC_TRUE;
143 input_AddInfo( p_category, (char *)p_frame->description,
145 playlist_AddItemInfo( p_item, "ID3",
146 (char *)p_frame->description,
151 input_AddInfo( p_category, (char *)p_frame->description,
153 playlist_AddItemInfo( p_item, "ID3",
154 (char *)p_frame->description,
161 id3_tag_delete( p_id3_tag );
162 if(val.b_bool == VLC_TRUE )
166 val.b_bool = p_playlist->i_index;
167 var_Set( p_playlist, "item-change", val );
170 val.b_bool = VLC_TRUE;
171 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
175 vlc_object_release( p_playlist );
179 /*****************************************************************************
180 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
181 * if it's at the start of the file
182 ****************************************************************************/
183 static int ParseID3Tags( vlc_object_t *p_this )
185 input_thread_t *p_input;
190 if ( p_this->i_object_type != VLC_OBJECT_INPUT )
192 return( VLC_EGENERIC );
194 p_input = (input_thread_t *)p_this;
196 msg_Dbg( p_input, "checking for ID3 tag" );
198 if ( p_input->stream.b_seekable &&
199 p_input->stream.i_method != INPUT_METHOD_NETWORK )
203 /*look for a ID3v1 tag at the end of the file*/
204 i_pos = stream_Size( p_input->s );
208 input_AccessReinit( p_input );
209 p_input->pf_seek( p_input, i_pos - 128 );
211 /* get 10 byte id3 header */
212 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
214 msg_Err( p_input, "cannot peek()" );
215 return( VLC_EGENERIC );
218 i_size2 = id3_tag_query( p_peek, 10 );
219 if ( i_size2 == 128 )
221 /* peek the entire tag */
222 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
224 msg_Err( p_input, "cannot peek()" );
225 return( VLC_EGENERIC );
227 msg_Dbg( p_input, "found ID3v1 tag" );
228 ParseID3Tag( p_input, p_peek, i_size2 );
231 /* look for ID3v2.4 tag at end of file */
232 /* get 10 byte ID3 footer */
233 if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
235 msg_Err( p_input, "cannot peek()" );
236 return( VLC_EGENERIC );
238 i_size2 = id3_tag_query( p_peek + 118, 10 );
239 if ( i_size2 < 0 && i_pos > -i_size2 )
240 { /* id3v2.4 footer found */
241 input_AccessReinit( p_input );
242 p_input->pf_seek( p_input, i_pos + i_size2 );
243 /* peek the entire tag */
244 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
246 msg_Err( p_input, "cannot peek()" );
247 return( VLC_EGENERIC );
249 msg_Dbg( p_input, "found ID3v2 tag at end of file" );
250 ParseID3Tag( p_input, p_peek, i_size2 );
253 input_AccessReinit( p_input );
254 p_input->pf_seek( p_input, 0 );
256 /* get 10 byte id3 header */
257 if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
259 msg_Err( p_input, "cannot peek()" );
260 return( VLC_EGENERIC );
263 i_size = id3_tag_query( p_peek, 10 );
266 return( VLC_SUCCESS );
269 /* Read the entire tag */
270 p_peek = malloc( i_size );
271 if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
273 msg_Err( p_input, "cannot read ID3 tag" );
274 if( p_peek ) free( p_peek );
275 return( VLC_EGENERIC );
278 ParseID3Tag( p_input, p_peek, i_size );
279 msg_Dbg( p_input, "found ID3v2 tag" );
282 return( VLC_SUCCESS );