]> git.sesse.net Git - vlc/blob - modules/meta_engine/id3tag.c
Fix bogus warnings
[vlc] / modules / meta_engine / id3tag.c
1 /*****************************************************************************
2  * id3tag.c: id3 tag parser/skipper based on libid3tag
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "vlc_meta.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 tags parser" ) );
51     set_capability( "meta reader", 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( demux_t *p_demux, uint8_t *p_data, int i_size )
63 {
64     struct id3_tag   *p_id3_tag;
65     struct id3_frame *p_frame;
66     int i = 0;
67
68     p_id3_tag = id3_tag_parse( p_data, i_size );
69     if( !p_id3_tag ) return;
70
71     if( !p_demux->p_private ) p_demux->p_private = (void *)vlc_meta_New();
72
73     vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private);
74 #define ID_IS( a ) (!strcmp(  p_frame->id, a ))
75 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
76
77     while ( ( p_frame = id3_tag_findframe( p_id3_tag, "UFID", i ) ) )
78     {
79         char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
80
81         if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
82         {
83             id3_byte_t const * p_ufid;
84             id3_length_t i_ufidlen;
85
86             p_ufid = id3_field_getbinarydata( &p_frame->fields[1], &i_ufidlen );
87             char *psz_ufid = strndup( p_ufid, i_ufidlen );
88
89             vlc_meta_SetTrackID( p_meta, psz_ufid );
90             free( psz_ufid );
91         }
92         i++;
93     }
94
95     i = 0;
96
97     while( ( p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i ) ) )
98     {
99         char *psz_desc = id3_ucs4_latin1duplicate(
100                 id3_field_getstring( &p_frame->fields[1] ) );
101
102         if ( ! strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
103         {
104             char *psz_artistid = id3_ucs4_latin1duplicate(
105                     id3_field_getstring( &p_frame->fields[2] ) );
106             vlc_meta_SetArtistID( p_meta, psz_artistid );
107             free( psz_artistid );
108         }
109
110         if ( ! strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
111         {
112             char *psz_albumid = id3_ucs4_latin1duplicate(
113                     id3_field_getstring( &p_frame->fields[2] ) );
114             vlc_meta_SetAlbumID( p_meta, psz_albumid );
115             free( psz_albumid );
116         }
117
118         free( psz_desc );
119         i++;
120     }
121     
122     i = 0;
123
124     while( ( p_frame = id3_tag_findframe( p_id3_tag , "T", i ) ) )
125     {
126         int i_strings = id3_field_getnstrings( &p_frame->fields[1] );
127
128         while( i_strings > 0 )
129         {
130             char *psz_temp = id3_ucs4_utf8duplicate(
131                 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
132
133             if( ID_IS( ID3_FRAME_GENRE ) )
134             {
135                 char *psz_endptr;
136                 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
137
138                 if( psz_temp != psz_endptr &&
139                     i_genre >= 0 && i_genre < NUM_GENRES )
140                 {
141                     vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
142                 }
143                 else
144                 {
145                     /* Unknown genre */
146                     vlc_meta_SetGenre( p_meta,psz_temp );
147                 }
148             }
149             else if( ID_IS( ID3_FRAME_TITLE ) )
150             {
151                 vlc_meta_SetTitle( p_meta, psz_temp );
152             }
153             else if( ID_IS( ID3_FRAME_ARTIST ) )
154             {
155                 vlc_meta_SetArtist( p_meta, psz_temp );
156             }
157             else if( ID_IS( ID3_FRAME_YEAR ) )
158             {
159                 vlc_meta_SetDate( p_meta, psz_temp );
160             }
161             else if( ID_IS( ID3_FRAME_COMMENT ) )
162             {
163                 vlc_meta_SetDescription( p_meta, psz_temp );
164             }
165             else if( DESCR_IS( "Copyright" ) )
166             {
167                 vlc_meta_SetCopyright( p_meta, psz_temp );
168             }
169             else if( DESCR_IS( "Publisher" ) )
170             {
171                 vlc_meta_SetPublisher( p_meta, psz_temp );
172             }
173             else if( DESCR_IS( "Track number/position in set" ) )
174             {
175                 vlc_meta_SetTracknum( p_meta, psz_temp );
176             }
177             else if( DESCR_IS( "Album/movie/show title" ) )
178             {
179                 vlc_meta_SetAlbum( p_meta, psz_temp );
180             }
181             else if( DESCR_IS( "Encoded by" ) )
182             {
183                 vlc_meta_SetEncodedBy( p_meta, psz_temp );
184             }
185             else if( ID_IS ( "APIC" ) )
186             {
187                 msg_Dbg( p_demux, "** Has APIC **" );
188             }
189             else if( p_frame->description )
190             { /* Unhandled meta*/
191                 msg_Warn( p_demux, "Fixme: unhandled ID3 metatag, %s", p_frame->description );
192             }
193             free( psz_temp );
194         }
195         i++;
196     }
197     id3_tag_delete( p_id3_tag );
198 }
199
200 /*****************************************************************************
201  * ParseID3Tags: check if ID3 tags at common locations. Parse them and skip it
202  * if it's at the start of the file
203  ****************************************************************************/
204 static int ParseID3Tags( vlc_object_t *p_this )
205 {
206     demux_t *p_demux = (demux_t *)p_this;
207     uint8_t *p_peek;
208     vlc_bool_t b_seekable;
209     int64_t i_init, i_pos;
210     int i_size;
211
212     p_demux->p_private = NULL;
213
214     msg_Dbg( p_demux, "checking for ID3 tag" );
215
216     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
217     if( !b_seekable ) return VLC_SUCCESS;
218
219     i_init = stream_Tell( p_demux->s );
220
221     /*
222      * Look for a ID3v1 tag at the end of the file
223      */
224     i_init = stream_Tell( p_demux->s );
225     i_pos = stream_Size( p_demux->s );
226
227     while( i_pos > 128 ) /* while used so we can break; */
228     {
229         stream_Seek( p_demux->s, i_pos - 128 );
230
231         /* get 10 byte id3 header */
232         if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) break;
233
234         i_size = id3_tag_query( p_peek, 10 );
235         if( i_size == 128 )
236         {
237             /* peek the entire tag */
238             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
239
240             msg_Dbg( p_demux, "found ID3v1 tag" );
241             ParseID3Tag( p_demux, p_peek, i_size );
242         }
243
244         /* look for ID3v2.4 tag at end of file */
245         /* get 10 byte ID3 footer */
246         if( stream_Peek( p_demux->s, &p_peek, 128 ) < 128 ) break;
247
248         i_size = id3_tag_query( p_peek + 118, 10 );
249         if( i_size < 0  && i_pos > -i_size )
250         {
251             /* id3v2.4 footer found */
252             stream_Seek( p_demux->s , i_pos + i_size );
253             /* peek the entire tag */
254             if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) break;
255
256             msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
257             ParseID3Tag( p_demux, p_peek, i_size );
258         }
259         break;
260     }
261
262     /*
263      * Get 10 byte id3 header
264      */
265     stream_Seek( p_demux->s, 0 );
266     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) goto end;
267
268     if( (i_size = id3_tag_query( p_peek, 10 )) <= 0 ) goto end;
269
270     if( stream_Peek( p_demux->s, &p_peek, i_size ) < i_size ) goto end;
271
272     msg_Dbg( p_demux, "found ID3v2 tag" );
273     ParseID3Tag( p_demux, p_peek, i_size );
274
275  end:
276     stream_Seek( p_demux->s, i_init );
277     return VLC_SUCCESS;
278 }