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>
34 #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( demux_t *p_demux, uint8_t *p_data, int i_size )
64 struct id3_tag *p_id3_tag;
65 struct id3_frame *p_frame;
68 p_id3_tag = id3_tag_parse( p_data, i_size );
69 if( !p_id3_tag ) return;
71 if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
73 while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
75 int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
77 while( i_strings > 0 )
79 char *psz_temp = id3_ucs4_utf8duplicate(
80 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
82 if( !strcmp( p_frame->id, ID3_FRAME_GENRE ) )
85 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
87 if( psz_temp != psz_endptr &&
88 i_genre >= 0 && i_genre < NUM_GENRES )
90 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
91 VLC_META_GENRE, ppsz_genres[atoi(psz_temp)]);
96 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
97 VLC_META_GENRE, psz_temp );
100 else if( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
102 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
103 VLC_META_TITLE, psz_temp );
105 else if( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
107 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
108 VLC_META_ARTIST, psz_temp );
112 /* Unknown meta info */
113 vlc_meta_Add( (vlc_meta_t *)p_demux->p_private,
114 (char *)p_frame->description, psz_temp );
120 id3_tag_delete( p_id3_tag );
123 /*****************************************************************************
124 * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
125 * if it's at the start of the file
126 ****************************************************************************/
127 static int ParseID3Tags( vlc_object_t *p_this )
129 demux_t *p_demux = (demux_t *)p_this;
133 vlc_bool_t b_seekable;
135 p_demux->p_private = NULL;
137 msg_Dbg( p_demux, "checking for ID3 tag" );
139 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
145 /*look for a ID3v1 tag at the end of the file*/
146 i_init = stream_Tell( p_demux->s );
147 i_pos = stream_Size( p_demux->s );
151 stream_Seek( p_demux->s, i_pos - 128 );
153 /* get 10 byte id3 header */
154 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
156 msg_Err( p_demux, "cannot peek()" );
160 i_size2 = id3_tag_query( p_peek, 10 );
161 if ( i_size2 == 128 )
163 /* peek the entire tag */
164 if ( stream_Peek( p_demux->s, &p_peek, i_size2 ) < i_size2 )
166 msg_Err( p_demux, "cannot peek()" );
169 msg_Dbg( p_demux, "found ID3v1 tag" );
170 ParseID3Tag( p_demux, p_peek, i_size2 );
173 /* look for ID3v2.4 tag at end of file */
174 /* get 10 byte ID3 footer */
175 if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 )
177 msg_Err( p_demux, "cannot peek()" );
180 i_size2 = id3_tag_query( p_peek + 118, 10 );
181 if ( i_size2 < 0 && i_pos > -i_size2 )
182 { /* id3v2.4 footer found */
183 stream_Seek( p_demux->s , i_pos + i_size2 );
184 /* peek the entire tag */
185 if ( stream_Peek( p_demux->s, &p_peek, i_size2 ) < i_size2 )
187 msg_Err( p_demux, "cannot peek()" );
190 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
191 ParseID3Tag( p_demux, p_peek, i_size2 );
194 stream_Seek( p_demux->s, i_init );
196 /* get 10 byte id3 header */
197 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
199 msg_Err( p_demux, "cannot peek()" );
203 i_size = id3_tag_query( p_peek, 10 );
209 /* Read the entire tag */
210 p_peek = malloc( i_size );
211 if( !p_peek || stream_Read( p_demux->s, p_peek, i_size ) < i_size )
213 msg_Err( p_demux, "cannot read ID3 tag" );
214 if( p_peek ) free( p_peek );
218 msg_Dbg( p_demux, "found ID3v2 tag" );
219 ParseID3Tag( p_demux, p_peek, i_size );