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