1 /*****************************************************************************
2 * id3tag.c: id3 tag parser/skipper based on libid3tag
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 tags parser" ) );
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 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);
81 char *psz_temp = id3_ucs4_utf8duplicate(
82 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
84 if( !strcmp( p_frame->id, ID3_FRAME_GENRE ) )
87 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
89 if( psz_temp != psz_endptr &&
90 i_genre >= 0 && i_genre < NUM_GENRES )
92 vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
97 vlc_meta_SetGenre( p_meta,psz_temp );
100 else if( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
102 vlc_meta_SetTitle( p_meta, psz_temp );
104 else if( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
106 vlc_meta_SetArtist( p_meta, psz_temp );
108 else if( !strcmp(p_frame->id, ID3_FRAME_YEAR ) )
110 vlc_meta_SetDate( p_meta, psz_temp );
112 else if( !strcmp(p_frame->id, ID3_FRAME_COMMENT ) )
114 vlc_meta_SetDescription( p_meta, psz_temp );
116 else if( strstr( (char*)p_frame->description, "Copyright" ) )
118 vlc_meta_SetCopyright( p_meta, psz_temp );
120 else if( strstr( (char*)p_frame->description, "Publisher" ) )
122 vlc_meta_SetPublisher( p_meta, psz_temp );
126 msg_Err(p_demux, "Fixme: unhandled meta" );
132 id3_tag_delete( p_id3_tag );
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 demux_t *p_demux = (demux_t *)p_this;
143 vlc_bool_t b_seekable;
144 int64_t i_init, i_pos;
147 p_demux->p_private = NULL;
149 msg_Dbg( p_demux, "checking for ID3 tag" );
151 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
152 if( !b_seekable ) return VLC_SUCCESS;
154 i_init = stream_Tell( p_demux->s );
157 * Look for a ID3v1 tag at the end of the file
159 i_init = stream_Tell( p_demux->s );
160 i_pos = stream_Size( p_demux->s );
162 while( i_pos > 128 ) /* while used so we can break; */
164 stream_Seek( p_demux->s, i_pos - 128 );
166 /* get 10 byte id3 header */
167 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
169 i_size = id3_tag_query( p_peek, 10 );
172 /* peek the entire tag */
173 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
175 msg_Dbg( p_demux, "found ID3v1 tag" );
176 ParseID3Tag( p_demux, p_peek, i_size );
179 /* look for ID3v2.4 tag at end of file */
180 /* get 10 byte ID3 footer */
181 if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
183 i_size = id3_tag_query( p_peek + 118, 10 );
184 if( i_size < 0 && i_pos > -i_size )
186 /* id3v2.4 footer found */
187 stream_Seek( p_demux->s , i_pos + i_size );
188 /* peek the entire tag */
189 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
191 msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
192 ParseID3Tag( p_demux, p_peek, i_size );
198 * Get 10 byte id3 header
200 stream_Seek( p_demux->s, 0 );
201 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
203 if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
205 if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
207 msg_Dbg( p_demux, "found ID3v2 tag" );
208 ParseID3Tag( p_demux, p_peek, i_size );
211 stream_Seek( p_demux->s, i_init );