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