]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
* src/input/control.c: added INPUT_ADD_INFO/INPUT_SET_NAME to input_Control().
[vlc] / modules / demux / util / id3tag.c
1 /*****************************************************************************
2  * id3tag.c: id3 tag parser/skipper based on libid3tag
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
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 "ninput.h"
35
36 #include <sys/types.h>
37
38 #include <id3tag.h>
39 #include "id3genres.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  ParseID3Tags ( vlc_object_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50 set_description( _("ID3 tag parser using libid3tag" ) );
51 set_capability( "id3", 70 );
52 set_callbacks( ParseID3Tags, NULL );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Definitions of structures  and functions used by this plugins
57  *****************************************************************************/
58
59 /*****************************************************************************
60  * ParseID3Tag : parse an id3tag into the info structures
61  *****************************************************************************/
62 static void ParseID3Tag( input_thread_t *p_input, uint8_t *p_data, int i_size )
63 {
64     struct id3_tag        *p_id3_tag;
65     struct id3_frame      *p_frame;
66     char                  *psz_temp;
67     vlc_value_t val;
68     int i;
69
70     var_Get( p_input, "demuxed-id3", &val );
71     if( val.b_bool )
72     {
73         msg_Dbg( p_input, "the ID3 tag was already parsed" );
74         return;
75     }
76
77     val.b_bool = VLC_FALSE;
78     p_id3_tag = id3_tag_parse( p_data, i_size );
79     i = 0;
80
81     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
82     {
83         int i_strings;
84
85         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
86
87         while ( i_strings > 0 )
88         {
89             psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
90             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
91             {
92                 int i_genre;
93                 char *psz_endptr;
94                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
95                 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
96                 {
97                     input_Control( p_input, INPUT_ADD_INFO, "ID3",
98                                    (char *)p_frame->description,
99                                    ppsz_genres[atoi(psz_temp)]);
100                 }
101                 else
102                 {
103                     input_Control( p_input, INPUT_ADD_INFO, "ID3",
104                                    (char *)p_frame->description, psz_temp);
105                 }
106             }
107             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
108             {
109                 input_Control( p_input, INPUT_SET_NAME, psz_temp );
110                 input_Control( p_input, INPUT_ADD_INFO, "ID3",
111                                (char *)p_frame->description, psz_temp );
112             }
113             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
114             {
115                 input_Control( p_input, INPUT_ADD_INFO,
116                                _("General"), _("Author"), psz_temp );
117                 input_Control( p_input, INPUT_ADD_INFO, "ID3",
118                                (char *)p_frame->description, psz_temp );
119             }
120             else
121             {
122                 input_Control( p_input, INPUT_ADD_INFO, "ID3",
123                                (char *)p_frame->description, psz_temp );
124             }
125             free( psz_temp );
126         }
127         i++;
128     }
129     id3_tag_delete( p_id3_tag );
130
131     val.b_bool = VLC_TRUE;
132     var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
133 }
134
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 )
140 {
141     input_thread_t *p_input = NULL;
142     uint8_t *p_peek;
143     int i_size;
144     int i_size2;
145
146     if ( p_this->i_object_type == VLC_OBJECT_INPUT )
147     {
148         p_input = (input_thread_t *)p_this;
149     }
150     if( p_input == NULL )
151     {
152         p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
153         if( p_input == NULL )
154         {
155             return VLC_EGENERIC;
156         }
157     }
158
159     msg_Dbg( p_input, "checking for ID3 tag" );
160
161     if ( p_input->stream.b_seekable &&
162          p_input->stream.i_method != INPUT_METHOD_NETWORK )
163     {
164         int64_t i_pos;
165
166         /*look for a ID3v1 tag at the end of the file*/
167         i_pos = stream_Size( p_input->s );
168
169         if ( i_pos >128 )
170         {
171             input_AccessReinit( p_input );
172             p_input->pf_seek( p_input, i_pos - 128 );
173
174             /* get 10 byte id3 header */
175             if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
176             {
177                 msg_Err( p_input, "cannot peek()" );
178                 vlc_object_release( p_input );
179                 return( VLC_EGENERIC );
180             }
181
182             i_size2 = id3_tag_query( p_peek, 10 );
183             if ( i_size2 == 128 )
184             {
185                 /* peek the entire tag */
186                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
187                 {
188                     msg_Err( p_input, "cannot peek()" );
189                     vlc_object_release( p_input );
190                     return( VLC_EGENERIC );
191                 }
192                 msg_Dbg( p_input, "found ID3v1 tag" );
193                 ParseID3Tag( p_input, p_peek, i_size2 );
194             }
195
196             /* look for ID3v2.4 tag at end of file */
197             /* get 10 byte ID3 footer */
198             if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
199             {
200                 msg_Err( p_input, "cannot peek()" );
201                 vlc_object_release( p_input );
202                 return( VLC_EGENERIC );
203             }
204             i_size2 = id3_tag_query( p_peek + 118, 10 );
205             if ( i_size2 < 0  && i_pos > -i_size2 )
206             {                                        /* id3v2.4 footer found */
207                 input_AccessReinit( p_input );
208                 p_input->pf_seek( p_input, i_pos + i_size2 );
209                 /* peek the entire tag */
210                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
211                 {
212                     msg_Err( p_input, "cannot peek()" );
213                     vlc_object_release( p_input );
214                     return( VLC_EGENERIC );
215                 }
216                 msg_Dbg( p_input, "found ID3v2 tag at end of file" );
217                 ParseID3Tag( p_input, p_peek, i_size2 );
218             }
219         }
220         input_AccessReinit( p_input );
221         p_input->pf_seek( p_input, 0 );
222     }
223     /* get 10 byte id3 header */
224     if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
225     {
226         msg_Err( p_input, "cannot peek()" );
227         vlc_object_release( p_input );
228         return( VLC_EGENERIC );
229     }
230
231     i_size = id3_tag_query( p_peek, 10 );
232     if ( i_size <= 0 )
233     {
234         vlc_object_release( p_input );
235         return( VLC_SUCCESS );
236     }
237
238     /* Read the entire tag */
239     p_peek = malloc( i_size );
240     if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
241     {
242         msg_Err( p_input, "cannot read ID3 tag" );
243         if( p_peek ) free( p_peek );
244         vlc_object_release( p_input );
245         return( VLC_EGENERIC );
246     }
247
248     ParseID3Tag( p_input, p_peek, i_size );
249     msg_Dbg( p_input, "found ID3v2 tag" );
250
251     free( p_peek );
252     vlc_object_release( p_input );
253     return( VLC_SUCCESS );
254 }