1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: id3tag.c,v 1.6 2003/03/18 00:33:29 sigmunau 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() */
31 #include <vlc/input.h>
33 #include <sys/types.h>
36 #include "id3genres.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int ParseID3Tags ( vlc_object_t * );
43 /*****************************************************************************
45 *****************************************************************************/
47 set_description( _("id3 tag parser using libid3tag" ) );
48 set_capability( "id3", 70 );
49 set_callbacks( ParseID3Tags, NULL );
52 /*****************************************************************************
53 * Definitions of structures and functions used by this plugins
54 *****************************************************************************/
56 /*****************************************************************************
57 * ParseID3Tag : parse an id3tag into the info structures
58 *****************************************************************************/
59 static void ParseID3Tag( input_thread_t *p_input, u8 *p_data, int i_size )
61 struct id3_tag * p_id3_tag;
62 struct id3_frame * p_frame;
63 input_info_category_t * p_category;
68 p_id3_tag = id3_tag_parse( p_data, i_size );
69 p_category = input_InfoCategory( p_input, "ID3" );
71 while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
73 i_strings = id3_field_getnstrings( &p_frame->fields[1] );
74 while ( i_strings > 0 )
76 psz_temp = id3_ucs4_latin1duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
77 if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
81 i_genre = strtol( psz_temp, &psz_endptr, 10 );
82 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
84 input_AddInfo( p_category, (char *)p_frame->description, ppsz_genres[atoi(psz_temp)]);
88 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
93 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
99 id3_tag_delete( p_id3_tag );
102 /*****************************************************************************
103 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
104 * if it's at the start of the file
105 ****************************************************************************/
106 static int ParseID3Tags( vlc_object_t *p_this )
108 input_thread_t *p_input;
112 stream_position_t * p_pos;
114 if ( p_this->i_object_type != VLC_OBJECT_INPUT )
116 return( VLC_EGENERIC );
118 p_input = (input_thread_t *)p_this;
120 msg_Dbg( p_input, "Checking for ID3 tag" );
122 if ( p_input->stream.b_seekable )
124 /*look for a id3v1 tag at the end of the file*/
125 p_pos = malloc( sizeof( stream_position_t ) );
128 msg_Err( p_input, "no mem" );
130 input_Tell( p_input, p_pos );
131 if ( p_pos->i_size >128 )
133 input_AccessReinit( p_input );
134 p_input->pf_seek( p_input, p_pos->i_size - 128 );
136 /* get 10 byte id3 header */
137 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
139 msg_Err( p_input, "cannot peek()" );
140 return( VLC_EGENERIC );
142 i_size2 = id3_tag_query( p_peek, 10 );
143 if ( i_size2 == 128 )
145 /* peek the entire tag */
146 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
148 msg_Err( p_input, "cannot peek()" );
149 return( VLC_EGENERIC );
151 ParseID3Tag( p_input, p_peek, i_size2 );
154 /* look for id3v2.4 tag at end of file */
155 /* get 10 byte id3 footer */
156 if( input_Peek( p_input, &p_peek, 128 ) < 128 )
158 msg_Err( p_input, "cannot peek()" );
159 return( VLC_EGENERIC );
161 i_size2 = id3_tag_query( p_peek + 118, 10 );
162 if ( i_size2 < 0 && p_pos->i_size > -i_size2 )
163 { /* id3v2.4 footer found */
164 input_AccessReinit( p_input );
165 p_input->pf_seek( p_input, p_pos->i_size + i_size2 );
166 /* peek the entire tag */
167 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
169 msg_Err( p_input, "cannot peek()" );
170 return( VLC_EGENERIC );
172 ParseID3Tag( p_input, p_peek, i_size2 );
176 input_AccessReinit( p_input );
177 p_input->pf_seek( p_input, 0 );
179 /* get 10 byte id3 header */
180 if( input_Peek( p_input, &p_peek, 10 ) < 10 )
182 msg_Err( p_input, "cannot peek()" );
183 return( VLC_EGENERIC );
186 i_size = id3_tag_query( p_peek, 10 );
189 return( VLC_SUCCESS );
192 /* peek the entire tag */
193 if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
195 msg_Err( p_input, "cannot peek()" );
196 return( VLC_EGENERIC );
199 ParseID3Tag( p_input, p_peek, i_size );
200 msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
201 p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
202 return( VLC_SUCCESS );