]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
94960d4750d1603afd12d3ddfb65527e70291803
[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.18 2004/01/05 13:00:20 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 = p_playlist ? p_playlist->pp_items[p_playlist->i_index] : NULL;
89
90         int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
91
92         while ( i_strings > 0 )
93         {
94             psz_temp = id3_ucs4_utf8duplicate( id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
95             if ( !strcmp(p_frame->id, ID3_FRAME_GENRE ) )
96             {
97                 int i_genre;
98                 char *psz_endptr;
99                 i_genre = strtol( psz_temp, &psz_endptr, 10 );
100                 if( psz_temp != psz_endptr && i_genre >= 0 && i_genre < NUM_GENRES )
101                 {
102                     input_AddInfo( p_category, (char *)p_frame->description,
103                                    ppsz_genres[atoi(psz_temp)]);
104                     playlist_AddItemInfo( p_item, "ID3",
105                                     (char *)p_frame->description,
106                                     ppsz_genres[atoi(psz_temp)]);
107                 }
108                 else
109                 {
110                     input_AddInfo( p_category, (char *)p_frame->description,
111                                                 psz_temp );
112                     playlist_AddItemInfo( p_item, "ID3",
113                                     (char *)p_frame->description,
114                                     psz_temp);
115                 }
116             }
117             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
118             {
119                 if( p_item )
120                 {
121                     if( p_item->psz_name )
122                     {
123                         free( p_item->psz_name );
124                     }
125                     p_item->psz_name = strdup( psz_temp );;
126
127                     val.b_bool = VLC_TRUE;
128                 }
129                 input_AddInfo( p_category, (char *)p_frame->description,
130                                             psz_temp );
131                 playlist_AddItemInfo( p_item, "ID3",
132                                         (char *)p_frame->description,
133                                     psz_temp);
134             }
135             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
136             {
137                 if( p_item )
138                 {
139                     playlist_AddItemInfo( p_item,
140                                           _("General"), _("Author"), psz_temp);
141                     val.b_bool = VLC_TRUE;
142                 }
143                 input_AddInfo( p_category, (char *)p_frame->description,
144                                             psz_temp );
145                 playlist_AddItemInfo( p_item, "ID3",
146                                            (char *)p_frame->description,
147                                            psz_temp);
148             }
149             else
150             {
151                 input_AddInfo( p_category, (char *)p_frame->description,
152                                             psz_temp );
153                 playlist_AddItemInfo( p_item, "ID3",
154                                            (char *)p_frame->description,
155                                            psz_temp);
156             }
157             free( psz_temp );
158         }
159         i++;
160     }
161     id3_tag_delete( p_id3_tag );
162     if(val.b_bool == VLC_TRUE )
163     {
164         if( p_playlist )
165         {
166             val.b_bool = p_playlist->i_index;
167             var_Set( p_playlist, "item-change", val );
168         }
169     }
170     val.b_bool = VLC_TRUE;
171     var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
172
173     if( p_playlist )
174     {
175         vlc_object_release( p_playlist );
176     }
177 }
178
179 /*****************************************************************************
180  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
181  * if it's at the start of the file
182  ****************************************************************************/
183 static int ParseID3Tags( vlc_object_t *p_this )
184 {
185     input_thread_t *p_input;
186     uint8_t *p_peek;
187     int i_size;
188     int i_size2;
189
190     if ( p_this->i_object_type != VLC_OBJECT_INPUT )
191     {
192         return( VLC_EGENERIC );
193     }
194     p_input = (input_thread_t *)p_this;
195
196     msg_Dbg( p_input, "Checking for ID3 tag" );
197
198     if ( p_input->stream.b_seekable &&
199          p_input->stream.i_method != INPUT_METHOD_NETWORK )
200     {
201         int64_t i_pos;
202
203         /*look for a ID3v1 tag at the end of the file*/
204         i_pos = stream_Size( p_input->s );
205
206         if ( i_pos >128 )
207         {
208             input_AccessReinit( p_input );
209             p_input->pf_seek( p_input, i_pos - 128 );
210
211             /* get 10 byte id3 header */
212             if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
213             {
214                 msg_Err( p_input, "cannot peek()" );
215                 return( VLC_EGENERIC );
216             }
217
218             i_size2 = id3_tag_query( p_peek, 10 );
219             if ( i_size2 == 128 )
220             {
221                 /* peek the entire tag */
222                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
223                 {
224                     msg_Err( p_input, "cannot peek()" );
225                     return( VLC_EGENERIC );
226                 }
227                 msg_Dbg( p_input, "Found ID3v1 tag" );
228                 ParseID3Tag( p_input, p_peek, i_size2 );
229             }
230
231             /* look for ID3v2.4 tag at end of file */
232             /* get 10 byte ID3 footer */
233             if( stream_Peek( p_input->s, &p_peek, 128 ) < 128 )
234             {
235                 msg_Err( p_input, "cannot peek()" );
236                 return( VLC_EGENERIC );
237             }
238             i_size2 = id3_tag_query( p_peek + 118, 10 );
239             if ( i_size2 < 0  && i_pos > -i_size2 )
240             {                                        /* id3v2.4 footer found */
241                 input_AccessReinit( p_input );
242                 p_input->pf_seek( p_input, i_pos + i_size2 );
243                 /* peek the entire tag */
244                 if ( stream_Peek( p_input->s, &p_peek, i_size2 ) < i_size2 )
245                 {
246                     msg_Err( p_input, "cannot peek()" );
247                     return( VLC_EGENERIC );
248                 }
249                 msg_Dbg( p_input, "Found ID3v2 tag at end of file" );
250                 ParseID3Tag( p_input, p_peek, i_size2 );
251             }
252         }
253         input_AccessReinit( p_input );
254         p_input->pf_seek( p_input, 0 );
255     }
256     /* get 10 byte id3 header */
257     if( stream_Peek( p_input->s, &p_peek, 10 ) < 10 )
258     {
259         msg_Err( p_input, "cannot peek()" );
260         return( VLC_EGENERIC );
261     }
262
263     i_size = id3_tag_query( p_peek, 10 );
264     if ( i_size <= 0 )
265     {
266         return( VLC_SUCCESS );
267     }
268
269     /* Read the entire tag */
270     p_peek = malloc( i_size );
271     if( !p_peek || stream_Read( p_input->s, p_peek, i_size ) < i_size )
272     {
273         msg_Err( p_input, "Cannot read ID3 tag" );
274         if( p_peek ) free( p_peek );
275         return( VLC_EGENERIC );
276     }
277
278     ParseID3Tag( p_input, p_peek, i_size );
279     msg_Dbg( p_input, "Found ID3v2 tag" );
280
281     free( p_peek );
282     return( VLC_SUCCESS );
283 }