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