]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
Options as infos were bad in several ways: it broke PLAYLIST_GO, used
[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: id3tag.c,v 1.20 2004/01/29 17:51:07 zorglub 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 "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     playlist_t            *p_playlist;
65     struct id3_tag        *p_id3_tag;
66     struct id3_frame      *p_frame;
67     input_info_category_t *p_category;
68     char                  *psz_temp;
69     vlc_value_t val;
70     int i;
71
72     var_Get( p_input, "demuxed-id3", &val );
73     if( val.b_bool )
74     {
75         msg_Dbg( p_input, "the ID3 tag was already parsed" );
76         return;
77     }
78
79     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
80
81     val.b_bool = VLC_FALSE;
82     p_id3_tag = id3_tag_parse( p_data, i_size );
83     p_category = input_InfoCategory( p_input, "ID3" );
84     i = 0;
85
86     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
87     {
88         playlist_item_t *p_item = playlist_ItemGetByPos( p_playlist, -1 );
89         if( !p_item )
90         {
91             msg_Err( p_input, "Unable to get item" );
92             return;
93         }
94
95         vlc_mutex_lock( &p_item->lock );
96
97         int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
98
99         while ( i_strings > 0 )
100         {
101             psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
102             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
103             {
104                 int i_genre;
105                 char *psz_endptr;
106                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
107                 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
108                 {
109                     input_AddInfo( p_category, (char *)p_frame->description,
110                                    ppsz_genres[atoi(psz_temp)]);
111                     playlist_ItemAddInfo( p_item, "ID3",
112                                     (char *)p_frame->description,
113                                     ppsz_genres[atoi(psz_temp)]);
114                 }
115                 else
116                 {
117                     input_AddInfo( p_category, (char *)p_frame->description,
118                                                 psz_temp );
119                     playlist_ItemAddInfo( p_item, "ID3",
120                                     (char *)p_frame->description,
121                                     psz_temp);
122                 }
123             }
124             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
125             {
126                 if( p_item )
127                 {
128                     if( p_item->psz_name )
129                     {
130                         free( p_item->psz_name );
131                     }
132                     p_item->psz_name = strdup( psz_temp );;
133
134                     val.b_bool = VLC_TRUE;
135                 }
136                 input_AddInfo( p_category, (char *)p_frame->description,
137                                             psz_temp );
138                 playlist_ItemAddInfo( p_item, "ID3",
139                                         (char *)p_frame->description,
140                                     psz_temp);
141             }
142             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
143             {
144                 if( p_item )
145                 {
146                     playlist_ItemAddInfo( p_item,
147                                           _("General"), _("Author"), psz_temp);
148                     val.b_bool = VLC_TRUE;
149                 }
150                 input_AddInfo( p_category, (char *)p_frame->description,
151                                             psz_temp );
152                 playlist_ItemAddInfo( p_item, "ID3",
153                                            (char *)p_frame->description,
154                                            psz_temp);
155             }
156             else
157             {
158                 input_AddInfo( p_category, (char *)p_frame->description,
159                                             psz_temp );
160                 playlist_ItemAddInfo( p_item, "ID3",
161                                            (char *)p_frame->description,
162                                            psz_temp);
163             }
164             free( psz_temp );
165         }
166         i++;
167         vlc_mutex_unlock( &p_item->lock );
168     }
169     id3_tag_delete( p_id3_tag );
170     if(val.b_bool == VLC_TRUE )
171     {
172         if( p_playlist )
173         {
174             val.b_bool = p_playlist->i_index;
175             var_Set( p_playlist, "item-change", val );
176         }
177     }
178     val.b_bool = VLC_TRUE;
179     var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
180
181     if( p_playlist )
182     {
183         vlc_object_release( p_playlist );
184     }
185 }
186
187 /*****************************************************************************
188  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
189  * if it's at the start of the file
190  ****************************************************************************/
191 static int ParseID3Tags( vlc_object_t *p_this )
192 {
193     input_thread_t *p_input;
194     uint8_t *p_peek;
195     int i_size;
196     int i_size2;
197
198     if ( p_this->i_object_type != VLC_OBJECT_INPUT )
199     {
200         return( VLC_EGENERIC );
201     }
202     p_input = (input_thread_t *)p_this;
203
204     msg_Dbg( p_input, "checking for ID3 tag" );
205
206     if ( p_input->stream.b_seekable &&
207          p_input->stream.i_method != INPUT_METHOD_NETWORK )
208     {
209         int64_t i_pos;
210
211         /*look for a ID3v1 tag at the end of the file*/
212         i_pos = stream_Size( p_input->s );
213
214         if ( i_pos >128 )
215         {
216             input_AccessReinit( p_input );
217             p_input->pf_seek( p_input, i_pos - 128 );
218
219             /* get 10 byte id3 header */
220             if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
221             {
222                 msg_Err( p_input, "cannot peek()" );
223                 return( VLC_EGENERIC );
224             }
225
226             i_size2 = id3_tag_query( p_peek, 10 );
227             if ( i_size2 == 128 )
228             {
229                 /* peek the entire tag */
230                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
231                 {
232                     msg_Err( p_input, "cannot peek()" );
233                     return( VLC_EGENERIC );
234                 }
235                 msg_Dbg( p_input, "found ID3v1 tag" );
236                 ParseID3Tag( p_input, p_peek, i_size2 );
237             }
238
239             /* look for ID3v2.4 tag at end of file */
240             /* get 10 byte ID3 footer */
241             if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
242             {
243                 msg_Err( p_input, "cannot peek()" );
244                 return( VLC_EGENERIC );
245             }
246             i_size2 = id3_tag_query( p_peek + 118, 10 );
247             if ( i_size2 < 0  && i_pos > -i_size2 )
248             {                                        /* id3v2.4 footer found */
249                 input_AccessReinit( p_input );
250                 p_input->pf_seek( p_input, i_pos + i_size2 );
251                 /* peek the entire tag */
252                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
253                 {
254                     msg_Err( p_input, "cannot peek()" );
255                     return( VLC_EGENERIC );
256                 }
257                 msg_Dbg( p_input, "found ID3v2 tag at end of file" );
258                 ParseID3Tag( p_input, p_peek, i_size2 );
259             }
260         }
261         input_AccessReinit( p_input );
262         p_input->pf_seek( p_input, 0 );
263     }
264     /* get 10 byte id3 header */
265     if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
266     {
267         msg_Err( p_input, "cannot peek()" );
268         return( VLC_EGENERIC );
269     }
270
271     i_size = id3_tag_query( p_peek, 10 );
272     if ( i_size <= 0 )
273     {
274         return( VLC_SUCCESS );
275     }
276
277     /* Read the entire tag */
278     p_peek = malloc( i_size );
279     if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
280     {
281         msg_Err( p_input, "cannot read ID3 tag" );
282         if( p_peek ) free( p_peek );
283         return( VLC_EGENERIC );
284     }
285
286     ParseID3Tag( p_input, p_peek, i_size );
287     msg_Dbg( p_input, "found ID3v2 tag" );
288
289     free( p_peek );
290     return( VLC_SUCCESS );
291 }