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