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