]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
id3tag.c: Do not try to seek on network streams even if they have b_seekable set to
[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.8 2003/06/15 15:16:14 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_utf8duplicate( 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          p_input->stream.i_method != INPUT_METHOD_NETWORK )
124     {        
125         /*look for a id3v1 tag at the end of the file*/
126         p_pos = malloc( sizeof( stream_position_t ) );
127         if ( p_pos == 0 )
128         {
129             msg_Err( p_input, "no mem" );
130         }
131         input_Tell( p_input, p_pos );
132         if ( p_pos->i_size >128 )
133         {
134             input_AccessReinit( p_input );
135             p_input->pf_seek( p_input, p_pos->i_size - 128 );
136             
137             /* get 10 byte id3 header */    
138             if( input_Peek( p_input, &p_peek, 10 ) < 10 )
139             {
140                 msg_Err( p_input, "cannot peek()" );
141                 return( VLC_EGENERIC );
142             }
143             i_size2 = id3_tag_query( p_peek, 10 );
144             if ( i_size2 == 128 )
145             {
146                 /* peek the entire tag */
147                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
148                 {
149                     msg_Err( p_input, "cannot peek()" );
150                     return( VLC_EGENERIC );
151                 }
152                 ParseID3Tag( p_input, p_peek, i_size2 );
153             }
154
155             /* look for id3v2.4 tag at end of file */
156             /* get 10 byte id3 footer */    
157             if( input_Peek( p_input, &p_peek, 128 ) < 128 )
158             {
159                 msg_Err( p_input, "cannot peek()" );
160                 return( VLC_EGENERIC );
161             }
162             i_size2 = id3_tag_query( p_peek + 118, 10 );
163             if ( i_size2 < 0  && p_pos->i_size > -i_size2 )
164             {                                          /* id3v2.4 footer found */
165                 input_AccessReinit( p_input );
166                 p_input->pf_seek( p_input, p_pos->i_size + i_size2 );
167                 /* peek the entire tag */
168                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
169                 {
170                     msg_Err( p_input, "cannot peek()" );
171                     return( VLC_EGENERIC );
172                 }
173                 ParseID3Tag( p_input, p_peek, i_size2 );
174             }
175         }
176         free( p_pos );
177         input_AccessReinit( p_input );    
178         p_input->pf_seek( p_input, 0 );
179     }
180     /* get 10 byte id3 header */    
181     if( input_Peek( p_input, &p_peek, 10 ) < 10 )
182     {
183         msg_Err( p_input, "cannot peek()" );
184         return( VLC_EGENERIC );
185     }
186
187     i_size = id3_tag_query( p_peek, 10 );
188     if ( i_size <= 0 )
189     {
190         return( VLC_SUCCESS );
191     }
192
193     /* peek the entire tag */
194     if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
195     {
196         msg_Err( p_input, "cannot peek()" );
197         return( VLC_EGENERIC );
198     }
199
200     ParseID3Tag( p_input, p_peek, i_size );
201     msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
202     p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
203     return( VLC_SUCCESS );
204 }