]> git.sesse.net Git - vlc/blob - modules/demux/util/id3tag.c
* src/playlist/* && Makefile.am
[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.14 2003/10/29 17:32:54 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 <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     val.b_bool = VLC_FALSE;
80     p_id3_tag = id3_tag_parse( p_data, i_size );
81     p_category = input_InfoCategory( p_input, "ID3" );
82     i = 0;
83
84     while ( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
85     {
86         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
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_AddInfo( p_category, (char *)p_frame->description,
98                                    ppsz_genres[atoi(psz_temp)]);
99                 }
100                 else
101                 {
102                     input_AddInfo( p_category, (char *)p_frame->description,
103                                                 psz_temp );
104                 }
105             }
106             else if ( !strcmp(p_frame->id, ID3_FRAME_TITLE ) )
107             {
108                 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
109                                               FIND_PARENT );
110                 if( p_playlist )
111                 {
112                     p_playlist->pp_items[p_playlist->i_index]->psz_name =
113                                                        strdup( psz_temp );
114                     val.b_bool = VLC_TRUE;
115                     vlc_object_release( p_playlist );
116                 }
117                 input_AddInfo( p_category, (char *)p_frame->description,
118                                             psz_temp );
119             }
120             else if ( !strcmp(p_frame->id, ID3_FRAME_ARTIST ) )
121             {
122                 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
123                                               FIND_PARENT );
124                 if( p_playlist )
125                 {
126                     p_playlist->pp_items[p_playlist->i_index]->psz_author =
127                                                 strdup( psz_temp );
128                     val.b_bool = VLC_TRUE;
129                     vlc_object_release( p_playlist );
130                 }
131                 input_AddInfo( p_category, (char *)p_frame->description,
132                                             psz_temp );
133             }
134             else
135             {
136                 input_AddInfo( p_category, (char *)p_frame->description,
137                                             psz_temp );
138             }
139             free( psz_temp );
140         }
141         i++;
142     }
143     id3_tag_delete( p_id3_tag );
144     if(val.b_bool == VLC_TRUE )
145     {
146         p_playlist = vlc_object_find( p_input,
147                      VLC_OBJECT_PLAYLIST, FIND_PARENT );
148         if( p_playlist )
149         {
150             val.b_bool = VLC_TRUE;
151             var_Set( p_playlist, "intf-change", val );
152             vlc_object_release( p_playlist );
153         }
154     }
155     val.b_bool = VLC_TRUE;
156     var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
157 }
158
159 /*****************************************************************************
160  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
161  * if it's at the start of the file
162  ****************************************************************************/
163 static int ParseID3Tags( vlc_object_t *p_this )
164 {
165     input_thread_t *p_input;
166     uint8_t *p_peek;
167     int i_size;
168     int i_size2;
169
170     if ( p_this->i_object_type != VLC_OBJECT_INPUT )
171     {
172         return( VLC_EGENERIC );
173     }
174     p_input = (input_thread_t *)p_this;
175
176     msg_Dbg( p_input, "Checking for ID3 tag" );
177
178     if ( p_input->stream.b_seekable &&
179          p_input->stream.i_method != INPUT_METHOD_NETWORK )
180     {
181         stream_position_t pos;
182
183         /*look for a id3v1 tag at the end of the file*/
184         input_Tell( p_input, &pos );
185         if ( pos.i_size >128 )
186         {
187             input_AccessReinit( p_input );
188             p_input->pf_seek( p_input, pos.i_size - 128 );
189
190             /* get 10 byte id3 header */
191             if( input_Peek( p_input, &p_peek, 10 ) < 10 )
192             {
193                 msg_Err( p_input, "cannot peek()" );
194                 return( VLC_EGENERIC );
195             }
196             i_size2 = id3_tag_query( p_peek, 10 );
197             if ( i_size2 == 128 )
198             {
199                 /* peek the entire tag */
200                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
201                 {
202                     msg_Err( p_input, "cannot peek()" );
203                     return( VLC_EGENERIC );
204                 }
205                 ParseID3Tag( p_input, p_peek, i_size2 );
206             }
207
208             /* look for id3v2.4 tag at end of file */
209             /* get 10 byte id3 footer */
210             if( input_Peek( p_input, &p_peek, 128 ) < 128 )
211             {
212                 msg_Err( p_input, "cannot peek()" );
213                 return( VLC_EGENERIC );
214             }
215             i_size2 = id3_tag_query( p_peek + 118, 10 );
216             if ( i_size2 < 0  && pos.i_size > -i_size2 )
217             {                                        /* id3v2.4 footer found */
218                 input_AccessReinit( p_input );
219                 p_input->pf_seek( p_input, pos.i_size + i_size2 );
220                 /* peek the entire tag */
221                 if ( input_Peek( p_input, &p_peek, i_size2 ) < i_size2 )
222                 {
223                     msg_Err( p_input, "cannot peek()" );
224                     return( VLC_EGENERIC );
225                 }
226                 ParseID3Tag( p_input, p_peek, i_size2 );
227             }
228         }
229         input_AccessReinit( p_input );
230         p_input->pf_seek( p_input, 0 );
231     }
232     /* get 10 byte id3 header */
233     if( input_Peek( p_input, &p_peek, 10 ) < 10 )
234     {
235         msg_Err( p_input, "cannot peek()" );
236         return( VLC_EGENERIC );
237     }
238
239     i_size = id3_tag_query( p_peek, 10 );
240     if ( i_size <= 0 )
241     {
242         return( VLC_SUCCESS );
243     }
244
245     /* peek the entire tag */
246     if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
247     {
248         msg_Err( p_input, "cannot peek()" );
249         return( VLC_EGENERIC );
250     }
251
252     ParseID3Tag( p_input, p_peek, i_size );
253     msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
254     p_input->p_current_data += i_size; /* seek passed end of ID3 tag */
255     return( VLC_SUCCESS );
256 }