]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
* include/vlc_common.h:
[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.13 2003/10/25 00:49:14 sam 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/intf.h>
32 #include <vlc/input.h>
33
34 #include <sys/types.h>
35
36 #include <id3tag.h>
37 #include "id3genres.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  ParseID3Tags ( vlc_object_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48 set_description( _("id3 tag parser using libid3tag" ) );
49 set_capability( "id3", 70 );
50 set_callbacks( ParseID3Tags, NULL );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Definitions of structures  and functions used by this plugins
55  *****************************************************************************/
56
57 /*****************************************************************************
58  * ParseID3Tag : parse an id3tag into the info structures
59  *****************************************************************************/
60 static void ParseID3Tag( input_thread_t *p_input, uint8_t *p_data, int i_size )
61 {
62     playlist_t * p_playlist;
63     struct id3_tag * p_id3_tag;
64     struct id3_frame * p_frame;
65     input_info_category_t * p_category;
66     int i_strings;
67     char * psz_temp;
68     int i;
69     vlc_value_t val;
70
71     var_Get( p_input, "demuxed-id3", &val );
72
73     if( val.b_bool )
74     {
75         msg_Dbg( p_input, "The ID3 tag was already parsed" );
76         return;
77     }
78
79     p_id3_tag = id3_tag_parse( p_data, i_size );
80     p_category = input_InfoCategory( p_input, "ID3" );
81     i = 0;
82
83     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
84     {
85         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
86         while ( i_strings > 0 )
87         {
88             psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
89             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
90             {
91                 int i_genre;
92                 char *psz_endptr;
93                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
94                 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
95                 {
96                     input_AddInfo( p_category, (char *)p_frame->description, ppsz_genres[atoi(psz_temp)]);
97                 }
98                 else
99                 {
100                     input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
101                 }
102             }
103             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
104             {
105                 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
106                 if( p_playlist )
107                 {
108                     p_playlist->pp_items[p_playlist->i_index]->psz_name = strdup( psz_temp );
109                     vlc_object_release( p_playlist );
110                 }
111                 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
112             }
113             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
114             {
115                 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
116                 if( p_playlist )
117                 {
118                     p_playlist->pp_items[p_playlist->i_index]->psz_author = strdup( psz_temp );
119                     vlc_object_release( p_playlist );
120                 }
121                 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
122             }
123             else
124             {
125                 input_AddInfo( p_category, (char *)p_frame->description, psz_temp );
126             }
127             free( psz_temp );
128         }
129         i++;
130     }
131     id3_tag_delete( p_id3_tag );
132     val.b_bool = VLC_TRUE;
133     var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
134 }
135
136 /*****************************************************************************
137  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
138  * if it's at the start of the file
139  ****************************************************************************/
140 static int ParseID3Tags( vlc_object_t *p_this )
141 {
142     input_thread_t *p_input;
143     uint8_t *p_peek;
144     int i_size;
145     int i_size2;
146
147     if ( p_this->i_object_type != VLC_OBJECT_INPUT )
148     {
149         return( VLC_EGENERIC );
150     }
151     p_input = (input_thread_t *)p_this;
152
153     msg_Dbg( p_input, "Checking for ID3 tag" );
154
155     if ( p_input->stream.b_seekable &&
156          p_input->stream.i_method != INPUT_METHOD_NETWORK )
157     {
158         stream_position_t pos;
159
160         /*look for a id3v1 tag at the end of the file*/
161         input_Tell( p_input, &pos );
162         if ( pos.i_size >128 )
163         {
164             input_AccessReinit( p_input );
165             p_input->pf_seek( p_input, pos.i_size - 128 );
166
167             /* get 10 byte id3 header */
168             if( input_Peek( p_input, &p_peek, 10 ) < 10 )
169             {
170                 msg_Err( p_input, "cannot peek()" );
171                 return( VLC_EGENERIC );
172             }
173             i_size2 = id3_tag_query( p_peek, 10 );
174             if ( i_size2 == 128 )
175             {
176                 /* peek the entire tag */
177                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
178                 {
179                     msg_Err( p_input, "cannot peek()" );
180                     return( VLC_EGENERIC );
181                 }
182                 ParseID3Tag( p_input, p_peek, i_size2 );
183             }
184
185             /* look for id3v2.4 tag at end of file */
186             /* get 10 byte id3 footer */
187             if( input_Peek( p_input, &p_peek, 128 ) < 128 )
188             {
189                 msg_Err( p_input, "cannot peek()" );
190                 return( VLC_EGENERIC );
191             }
192             i_size2 = id3_tag_query( p_peek + 118, 10 );
193             if ( i_size2 < 0  && pos.i_size > -i_size2 )
194             {                                        /* id3v2.4 footer found */
195                 input_AccessReinit( p_input );
196                 p_input->pf_seek( p_input, pos.i_size + i_size2 );
197                 /* peek the entire tag */
198                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
199                 {
200                     msg_Err( p_input, "cannot peek()" );
201                     return( VLC_EGENERIC );
202                 }
203                 ParseID3Tag( p_input, p_peek, i_size2 );
204             }
205         }
206         input_AccessReinit( p_input );
207         p_input->pf_seek( p_input, 0 );
208     }
209     /* get 10 byte id3 header */
210     if( input_Peek( p_input, &p_peek, 10 ) < 10 )
211     {
212         msg_Err( p_input, "cannot peek()" );
213         return( VLC_EGENERIC );
214     }
215
216     i_size = id3_tag_query( p_peek, 10 );
217     if ( i_size <= 0 )
218     {
219         return( VLC_SUCCESS );
220     }
221
222     /* peek the entire tag */
223     if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
224     {
225         msg_Err( p_input, "cannot peek()" );
226         return( VLC_EGENERIC );
227     }
228
229     ParseID3Tag( p_input, p_peek, i_size );
230     msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
231     p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
232     return( VLC_SUCCESS );
233 }