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