]> git.sesse.net Git - vlc/blob - modules/meta_engine/id3tag.c
Split file path functions out of vlc_charset.h into vlc_fs.h
[vlc] / modules / meta_engine / id3tag.c
1 /*****************************************************************************
2  * id3tag.c: id3/ape 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 <config.h>
28
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_interface.h>
37 #include <vlc_demux.h>
38 #include <vlc_playlist.h>
39 #include <vlc_charset.h>
40
41 #include <sys/types.h>
42
43 #include <vlc_meta.h>
44
45 #include <id3tag.h>
46 #include "id3genres.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int  ParseTags ( vlc_object_t * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 vlc_module_begin ()
57     set_description( N_("ID3v1/2 and APEv1/2 tags parser" ) )
58     set_capability( "meta reader", 70 )
59     set_callbacks( ParseTags, NULL )
60 vlc_module_end ()
61
62 /*****************************************************************************
63  * ParseID3Tag : parse an id3tag into the info structures
64  *****************************************************************************/
65 static void ParseID3Tag( demux_meta_t *p_demux_meta, const uint8_t *p_data, int i_size )
66 {
67     struct id3_tag   *p_id3_tag;
68     struct id3_frame *p_frame;
69     vlc_meta_t       *p_meta;
70     int i;
71
72     p_id3_tag = id3_tag_parse( p_data, i_size );
73     if( !p_id3_tag )
74         return;
75
76     if( !p_demux_meta->p_meta )
77         p_demux_meta->p_meta = vlc_meta_New();
78     p_meta = p_demux_meta->p_meta;
79
80 #define ID_IS( a ) (!strcmp(  p_frame->id, a ))
81 #define DESCR_IS( a) strstr( (char*)p_frame->description, a )
82 #define GET_STRING(frame,fidx) id3_ucs4_latin1duplicate( id3_field_getstring( &(frame)->fields[fidx] ) )
83
84     /* */
85     for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "UFID", i )) != NULL; i++ )
86     {
87         const char *psz_owner = id3_field_getlatin1( &p_frame->fields[0] );
88
89         if( !strncmp( psz_owner, "http://musicbrainz.org", 22 ) )
90         {
91             id3_byte_t const * p_ufid;
92             id3_length_t i_ufidlen;
93
94             p_ufid = id3_field_getbinarydata( &p_frame->fields[1], &i_ufidlen );
95             char *psz_ufid = strndup( p_ufid, i_ufidlen );
96
97             vlc_meta_SetTrackID( p_meta, psz_ufid );
98             free( psz_ufid );
99         }
100     }
101
102     /* User defined text (TXXX) */
103     for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "TXXX", i )) != NULL; i++ )
104     {
105         /* 3 fields: 'encoding', 'description', 'value' */
106         char *psz_name = GET_STRING( p_frame, 1 );
107         char *psz_value = GET_STRING( p_frame, 2 );
108
109         vlc_meta_AddExtra( p_meta, psz_name, psz_value );
110 #if 0
111         if( !strncmp( psz_name, "MusicBrainz Artist Id", 21 ) )
112             vlc_meta_SetArtistID( p_meta, psz_value );
113         if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
114             vlc_meta_SetAlbumID( p_meta, psz_value );
115 #endif
116         free( psz_name );
117         free( psz_value );
118     }
119
120     /* Relative volume adjustment */
121     for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "RVA2", i )) != NULL; i++ )
122     {
123         /* 2 fields: 'latin1', 'binary' */
124         const char *psz_type = id3_field_getlatin1( &p_frame->fields[0] );
125         if( !strcasecmp( psz_type, "track" ) || !strcasecmp( psz_type, "album" ) ||
126             !strcasecmp( psz_type, "normalize" ) )
127         {
128             id3_byte_t const * p_data;
129             id3_length_t i_data;
130
131             p_data = id3_field_getbinarydata( &p_frame->fields[1], &i_data );
132             while( i_data >= 4 )
133             {
134                 const unsigned int i_peak_size = p_data[3];
135                 const float f_temp = GetWBE( &p_data[1] );
136                 const float f_gain = f_temp / 512.0;
137                 char psz_value[32];
138
139                 if( i_data < i_peak_size + 4 )
140                     break;
141                 /* only master volume */
142                 if( p_data[0] == 0x01 )
143                 {
144                     snprintf( psz_value, sizeof(psz_value), "%f", f_gain );
145                     if( !strcasecmp( psz_type, "album" ) )
146                         vlc_meta_AddExtra( p_meta, "REPLAYGAIN_ALBUM_GAIN", psz_value );
147                     else
148                         vlc_meta_AddExtra( p_meta, "REPLAYGAIN_TRACK_GAIN", psz_value );
149                     /* XXX I have no idea what peak unit is ... */
150                 }
151                 i_data -= 4+i_peak_size;
152             }
153         }
154     }
155
156     /* TODO 'RGAD' if it is used somewhere */
157
158     /* T--- Text informations */
159     for( i = 0; (p_frame = id3_tag_findframe( p_id3_tag, "T", i )) != NULL; i++ )
160     {
161         unsigned i_strings;
162  
163         /* Special case TXXX is not the same beast */
164         if( ID_IS( "TXXX" ) )
165             continue;
166
167         i_strings = id3_field_getnstrings( &p_frame->fields[1] );
168         while( i_strings > 0 )
169         {
170             char *psz_temp = id3_ucs4_utf8duplicate(
171                 id3_field_getstrings( &p_frame->fields[1], --i_strings ) );
172
173             if( ID_IS( ID3_FRAME_GENRE ) )
174             {
175                 char *psz_endptr;
176                 int i_genre = strtol( psz_temp, &psz_endptr, 10 );
177
178                 if( psz_temp != psz_endptr &&
179                     i_genre >= 0 && i_genre < NUM_GENRES )
180                 {
181                     vlc_meta_SetGenre( p_meta, ppsz_genres[atoi(psz_temp)]);
182                 }
183                 else
184                 {
185                     /* Unknown genre */
186                     vlc_meta_SetGenre( p_meta,psz_temp );
187                 }
188             }
189             else if( ID_IS( ID3_FRAME_TITLE ) )
190             {
191                 vlc_meta_SetTitle( p_meta, psz_temp );
192             }
193             else if( ID_IS( ID3_FRAME_ARTIST ) )
194             {
195                 vlc_meta_SetArtist( p_meta, psz_temp );
196             }
197             else if( ID_IS( ID3_FRAME_YEAR ) )
198             {
199                 vlc_meta_SetDate( p_meta, psz_temp );
200             }
201             else if( ID_IS( ID3_FRAME_COMMENT ) )
202             {
203                 vlc_meta_SetDescription( p_meta, psz_temp );
204             }
205             else if( DESCR_IS( "Copyright" ) )
206             {
207                 vlc_meta_SetCopyright( p_meta, psz_temp );
208             }
209             else if( DESCR_IS( "Publisher" ) )
210             {
211                 vlc_meta_SetPublisher( p_meta, psz_temp );
212             }
213             else if( DESCR_IS( "Track number/position in set" ) )
214             {
215                 vlc_meta_SetTrackNum( p_meta, psz_temp );
216             }
217             else if( DESCR_IS( "Album/movie/show title" ) )
218             {
219                 vlc_meta_SetAlbum( p_meta, psz_temp );
220             }
221             else if( DESCR_IS( "Encoded by" ) )
222             {
223                 vlc_meta_SetEncodedBy( p_meta, psz_temp );
224             }
225             else if( ID_IS ( "APIC" ) )
226             {
227                 msg_Dbg( p_demux_meta, "** Has APIC **" );
228             }
229             else if( p_frame->description )
230             {
231                 /* Unhandled meta */
232                 vlc_meta_AddExtra( p_meta, (char*)p_frame->description, psz_temp );
233             }
234             free( psz_temp );
235         }
236     }
237     id3_tag_delete( p_id3_tag );
238 #undef GET_STRING
239 #undef DESCR_IS
240 #undef ID_IS
241 }
242 /*****************************************************************************
243  * APEv1/2
244  *****************************************************************************/
245 #define APE_TAG_HEADERSIZE (32)
246 static size_t GetAPEvXSize( const uint8_t *p_data, int i_data )
247 {
248     uint32_t flags;
249     size_t i_body;
250
251     if( i_data < APE_TAG_HEADERSIZE ||
252         ( GetDWLE( &p_data[8] ) != 1000 && GetDWLE( &p_data[8] ) != 2000 ) || /* v1/v2 only */
253         strncmp( (char*)p_data, "APETAGEX", 8 ) ||
254         GetDWLE( &p_data[8+4+4] ) <= 0 )
255         return 0;
256
257     i_body = GetDWLE( &p_data[8+4] );
258     flags = GetDWLE( &p_data[8+4+4] );
259
260     /* is it the header */
261     if( flags & (1<<29) )
262         return i_body + ( (flags&(1<<30)) ? APE_TAG_HEADERSIZE : 0 );
263
264     /* it is the footer */
265     return i_body + ( (flags&(1<<31)) ? APE_TAG_HEADERSIZE : 0 );
266 }
267 static void ParseAPEvXTag( demux_meta_t *p_demux_meta, const uint8_t *p_data, int i_data )
268 {
269     vlc_meta_t       *p_meta;
270     bool b_start;
271     bool b_end;
272     const uint8_t *p_header = NULL;
273     int i_entry;
274
275     if( i_data < APE_TAG_HEADERSIZE )
276         return;
277
278     b_start = !strncmp( (char*)&p_data[0], "APETAGEX", 8 );
279     b_end = !strncmp( (char*)&p_data[i_data-APE_TAG_HEADERSIZE], "APETAGEX", 8 );
280     if( !b_end && !b_start )
281         return;
282
283     if( !p_demux_meta->p_meta )
284         p_demux_meta->p_meta = vlc_meta_New();
285     p_meta = p_demux_meta->p_meta;
286
287     if( b_start )
288     {
289         p_header = &p_data[0];
290         p_data += APE_TAG_HEADERSIZE;
291         i_data -= APE_TAG_HEADERSIZE;
292     }
293     if( b_end )
294     {
295         p_header = &p_data[i_data-APE_TAG_HEADERSIZE];
296         i_data -= APE_TAG_HEADERSIZE;
297     }
298     if( i_data <= 0 )
299         return;
300
301     i_entry = GetDWLE( &p_header[8+4+4] );
302     if( i_entry <= 0 )
303         return;
304
305     while( i_entry > 0 && i_data >= 10 )
306     {
307         const int i_size = GetDWLE( &p_data[0] );
308         const uint32_t flags = GetDWLE( &p_data[4] );
309         char psz_name[256];
310         int n;
311
312         strlcpy( psz_name, (char*)&p_data[8], sizeof(psz_name) );
313         n = strlen( psz_name );
314         if( n <= 0 )
315             break;
316
317         p_data += 8+n+1;
318         i_data -= 8+n+1;
319         if( i_data < i_size )
320             break;
321
322         /* Retreive UTF-8 fields only */
323         if( ((flags>>1) & 0x03) == 0x00 )
324         {
325             /* FIXME list are separated by '\0' */
326             char *psz_value = strndup( (char*)&p_data[0], i_size );
327
328             EnsureUTF8( psz_name );
329             EnsureUTF8( psz_value );
330 #define IS(s) (!strcasecmp( psz_name, s ) )
331             if( IS( "Title" ) )
332                 vlc_meta_SetTitle( p_meta, psz_value );
333             else  if( IS( "Artist" ) )
334                 vlc_meta_SetArtist( p_meta, psz_value );
335             else  if( IS( "Album" ) )
336                 vlc_meta_SetAlbum( p_meta, psz_value );
337             else  if( IS( "Publisher" ) )
338                 vlc_meta_SetPublisher( p_meta, psz_value );
339             else  if( IS( "Track" ) )
340             {
341                 char *p = strchr( psz_value, '/' );
342                 if( p )
343                     *p++ = '\0';
344                 vlc_meta_SetTrackNum( p_meta, psz_value );
345             }
346             else  if( IS( "Comment" ) )
347                 vlc_meta_SetDescription( p_meta, psz_value );
348             else  if( IS( "Copyright" ) )
349                 vlc_meta_SetCopyright( p_meta, psz_value );
350             else  if( IS( "Year" ) )
351                 vlc_meta_SetDate( p_meta, psz_value );
352             else  if( IS( "Genre" ) )
353                 vlc_meta_SetGenre( p_meta, psz_value );
354             else  if( IS( "Language" ) )
355                 vlc_meta_SetLanguage( p_meta, psz_value );
356             else
357                 vlc_meta_AddExtra( p_meta, psz_name, psz_value );
358 #undef IS
359             free( psz_value );
360         }
361
362         p_data += i_size;
363         i_data -= i_size;
364         i_entry--;
365     }
366 }
367
368 /*****************************************************************************
369  * CheckFooter: check for ID3/APE at the end of the file
370  * CheckHeader: check for ID3/APE at the begining of the file
371  *****************************************************************************/
372 static void CheckFooter( demux_meta_t *p_demux_meta )
373 {
374     demux_t      *p_demux = (demux_t *)p_demux_meta->p_demux;
375     const int64_t i_pos = stream_Size( p_demux->s );
376     const size_t i_peek = 128+APE_TAG_HEADERSIZE;
377     const uint8_t *p_peek;
378     const uint8_t *p_peek_id3;
379     int64_t i_id3v2_pos = -1;
380     int64_t i_apevx_pos = -1;
381     int i_id3v2_size;
382     int i_apevx_size;
383     size_t i_id3v1_size;
384
385     if( i_pos < i_peek )
386         return;
387     if( stream_Seek( p_demux->s, i_pos - i_peek ) )
388         return;
389
390     if( stream_Peek( p_demux->s, &p_peek, i_peek ) < i_peek )
391         return;
392     p_peek_id3 = &p_peek[APE_TAG_HEADERSIZE];
393
394     /* Check/Parse ID3v1 */
395     i_id3v1_size = id3_tag_query( p_peek_id3, ID3_TAG_QUERYSIZE );
396     if( i_id3v1_size == 128 )
397     {
398         msg_Dbg( p_demux, "found ID3v1 tag" );
399         ParseID3Tag( p_demux_meta, p_peek_id3, i_id3v1_size );
400     }
401
402     /* Compute ID3v2 position */
403     i_id3v2_size = -id3_tag_query( &p_peek_id3[128-ID3_TAG_QUERYSIZE], ID3_TAG_QUERYSIZE );
404     if( i_id3v2_size > 0 )
405         i_id3v2_pos = i_pos - i_id3v2_size;
406
407     /* Compute APE2v2 position */
408     i_apevx_size = GetAPEvXSize( &p_peek[128+0], APE_TAG_HEADERSIZE );
409     if( i_apevx_size > 0 )
410     {
411         i_apevx_pos = i_pos - i_apevx_size;
412     }
413     else if( i_id3v1_size > 0 )
414     {
415         /* it can be before ID3v1 */
416         i_apevx_size = GetAPEvXSize( p_peek, APE_TAG_HEADERSIZE );
417         if( i_apevx_size > 0 )
418             i_apevx_pos = i_pos - 128 - i_apevx_size;
419     }
420
421     if( i_id3v2_pos > 0 && i_apevx_pos > 0 )
422     {
423         msg_Warn( p_demux,
424                   "Both ID3v2 and APEv1/2 at the end of file, ignoring APEv1/2" );
425         i_apevx_pos = -1;
426     }
427
428     /* Parse ID3v2.4 */
429     if( i_id3v2_pos > 0 )
430     {
431         if( !stream_Seek( p_demux->s, i_id3v2_pos ) &&
432             stream_Peek( p_demux->s, &p_peek, i_id3v2_size ) == i_id3v2_size )
433         {
434             msg_Dbg( p_demux, "found ID3v2 tag at end of file" );
435             ParseID3Tag( p_demux_meta, p_peek, i_id3v2_size );
436         }
437     }
438
439     /* Parse APEv1/2 */
440     if( i_apevx_pos > 0 )
441     {
442         if( !stream_Seek( p_demux->s, i_apevx_pos ) &&
443             stream_Peek( p_demux->s, &p_peek, i_apevx_size ) == i_apevx_size )
444         {
445             msg_Dbg( p_demux, "found APEvx tag at end of file" );
446             ParseAPEvXTag( p_demux_meta, p_peek, i_apevx_size );
447         }
448     }
449 }
450 static void CheckHeader( demux_meta_t *p_demux_meta )
451 {
452     const uint8_t *p_peek;
453     int i_size;
454
455     demux_t      *p_demux = (demux_t *)p_demux_meta->p_demux;
456
457     if( stream_Seek( p_demux->s, 0 ) )
458         return;
459
460     /* Test ID3v2 first */
461     if( stream_Peek( p_demux->s, &p_peek, ID3_TAG_QUERYSIZE ) != ID3_TAG_QUERYSIZE )
462         return;
463     i_size = id3_tag_query( p_peek, ID3_TAG_QUERYSIZE );
464     if( i_size > 0 &&
465         stream_Peek( p_demux->s, &p_peek, i_size ) == i_size )
466     {
467         msg_Dbg( p_demux, "found ID3v2 tag" );
468         ParseID3Tag( p_demux_meta, p_peek, i_size );
469         return;
470     }
471
472     /* Test APEv1 */
473     if( stream_Peek( p_demux->s, &p_peek, APE_TAG_HEADERSIZE ) != APE_TAG_HEADERSIZE )
474         return;
475     i_size = GetAPEvXSize( p_peek, APE_TAG_HEADERSIZE );
476     if( i_size > 0 &&
477         stream_Peek( p_demux->s, &p_peek, i_size ) == i_size )
478     {
479         msg_Dbg( p_demux, "found APEv1/2 tag" );
480         ParseAPEvXTag( p_demux_meta, p_peek, i_size );
481     }
482 }
483
484 /*****************************************************************************
485  * ParseTags: check if ID3/APE tags at common locations.
486  ****************************************************************************/
487 static int ParseTags( vlc_object_t *p_this )
488 {
489     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
490     demux_t      *p_demux = (demux_t *)p_demux_meta->p_demux;
491     bool    b_seekable;
492     int64_t       i_init;
493
494     msg_Dbg( p_demux_meta, "checking for ID3v1/2 and APEv1/2 tags" );
495     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
496     if( !b_seekable )
497         return VLC_EGENERIC;
498
499     i_init = stream_Tell( p_demux->s );
500
501     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
502     p_demux_meta->p_meta = NULL;
503
504     /* */
505     CheckFooter( p_demux_meta );
506
507     /* */
508     CheckHeader( p_demux_meta );
509
510     /* Restore position
511      *  Demuxer will not see tags at the start as src/input/demux.c skips it
512      *  for them
513      */
514     stream_Seek( p_demux->s, i_init );
515     if( !p_demux_meta->p_meta && p_demux_meta->i_attachments <= 0 )
516         return VLC_EGENERIC;
517     return VLC_SUCCESS;
518 }
519