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