]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
Seek one less time. Should increase performance with access modules where
[vlc] / modules / demux / util / id3tag.c
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 $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  * 
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include <sys/types.h>
34
35 #include <id3tag.h>
36 #include "id3genres.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  ParseID3Tags ( vlc_object_t * );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin();
47 set_description( _("id3 tag parser using libid3tag" ) );
48 set_capability( "id3", 70 );
49 set_callbacks( ParseID3Tags, NULL );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Definitions of structures  and functions used by this plugins 
54  *****************************************************************************/
55
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 )
60 {
61     struct id3_tag * p_id3_tag;
62     struct id3_frame * p_frame;
63     input_info_category_t * p_category;
64     int i_strings;
65     char * psz_temp;
66     int i;
67     
68     p_id3_tag = id3_tag_parse( p_data, i_size );
69     p_category = input_InfoCategory( p_input, "ID3" );
70     i = 0;
71     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
72     {
73         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
74         while ( i_strings > 0 )
75         {
76             psz_temp = id3_ucs4_latin1duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
77             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
78             {
79                 int i_genre;
80                 char *psz_endptr;
81                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
82                 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
83                 {
84                     input_AddInfo( p_category, (char *)p_frame->description, ppsz_genres[atoi(psz_temp)]);
85                 }
86                 else
87                 {
88                     input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
89                 }
90             }
91             else
92             {
93                 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
94             }
95             free( psz_temp ); 
96         }
97         i++;
98     }
99     id3_tag_delete( p_id3_tag );
100 }
101
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 )
107 {
108     input_thread_t *p_input;
109     u8  *p_peek;
110     int i_size;
111     int i_size2;
112     stream_position_t * p_pos;
113
114     if ( p_this->i_object_type != VLC_OBJECT_INPUT )
115     {
116         return( VLC_EGENERIC );
117     }
118     p_input = (input_thread_t *)p_this;
119
120     msg_Dbg( p_input, "Checking for ID3 tag" );
121
122     if ( p_input->stream.b_seekable )
123     {        
124         /*look for a id3v1 tag at the end of the file*/
125         p_pos = malloc( sizeof( stream_position_t ) );
126         if ( p_pos == 0 )
127         {
128             msg_Err( p_input, "no mem" );
129         }
130         input_Tell( p_input, p_pos );
131         if ( p_pos->i_size >128 )
132         {
133             input_AccessReinit( p_input );
134             p_input->pf_seek( p_input, p_pos->i_size - 128 );
135             
136             /* get 10 byte id3 header */    
137             if( input_Peek( p_input, &p_peek, 10 ) < 10 )
138             {
139                 msg_Err( p_input, "cannot peek()" );
140                 return( VLC_EGENERIC );
141             }
142             i_size2 = id3_tag_query( p_peek, 10 );
143             if ( i_size2 == 128 )
144             {
145                 /* peek the entire tag */
146                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
147                 {
148                     msg_Err( p_input, "cannot peek()" );
149                     return( VLC_EGENERIC );
150                 }
151                 ParseID3Tag( p_input, p_peek, i_size2 );
152             }
153
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 )
157             {
158                 msg_Err( p_input, "cannot peek()" );
159                 return( VLC_EGENERIC );
160             }
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 )
168                 {
169                     msg_Err( p_input, "cannot peek()" );
170                     return( VLC_EGENERIC );
171                 }
172                 ParseID3Tag( p_input, p_peek, i_size2 );
173             }
174         }
175         free( p_pos );
176         input_AccessReinit( p_input );    
177         p_input->pf_seek( p_input, 0 );
178     }
179     /* get 10 byte id3 header */    
180     if( input_Peek( p_input, &p_peek, 10 ) < 10 )
181     {
182         msg_Err( p_input, "cannot peek()" );
183         return( VLC_EGENERIC );
184     }
185
186     i_size = id3_tag_query( p_peek, 10 );
187     if ( i_size <= 0 )
188     {
189         return( VLC_SUCCESS );
190     }
191
192     /* peek the entire tag */
193     if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
194     {
195         msg_Err( p_input, "cannot peek()" );
196         return( VLC_EGENERIC );
197     }
198
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 );
203 }