]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
Taglib: undefined operation due to duplicate usage of var with post-increment
[vlc] / modules / meta_engine / taglib.cpp
1 /*****************************************************************************
2  * taglib.cpp: Taglib tag parser/writer
3  *****************************************************************************
4  * Copyright (C) 2003-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Rafaël Carré <funman@videolanorg>
9  *          Rémi Duraffort <ivoire@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_playlist.h>
33 #include <vlc_meta.h>
34 #include <vlc_demux.h>
35 #include <vlc_strings.h>
36 #include <vlc_charset.h>
37
38 #ifdef WIN32
39 # include <io.h>
40 #else
41 # include <unistd.h>
42 #endif
43
44
45 // Taglib headers
46 #include <fileref.h>
47 #include <tag.h>
48 #include <tbytevector.h>
49
50 #include <apetag.h>
51 #include <id3v2tag.h>
52 #include <xiphcomment.h>
53
54 #include <flacfile.h>
55 #include <mpcfile.h>
56 #include <mpegfile.h>
57 #include <oggfile.h>
58 #include <oggflacfile.h>
59 #include <speexfile.h>
60 #include <trueaudiofile.h>
61 #include <vorbisfile.h>
62 #include <wavpackfile.h>
63
64 #include <attachedpictureframe.h>
65 #include <textidentificationframe.h>
66 #include <uniquefileidentifierframe.h>
67
68
69 // Local functions
70 static int ReadMeta    ( vlc_object_t * );
71 static int DownloadArt ( vlc_object_t * );
72 static int WriteMeta   ( vlc_object_t * );
73
74 vlc_module_begin ()
75     set_capability( "meta reader", 1000 )
76     set_callbacks( ReadMeta, NULL )
77     add_submodule ()
78         set_capability( "art downloader", 50 )
79         set_callbacks( DownloadArt, NULL )
80     add_submodule ()
81         set_capability( "meta writer", 50 )
82         set_callbacks( WriteMeta, NULL )
83 vlc_module_end ()
84
85 using namespace TagLib;
86
87
88 /**
89  * Read meta informations from APE tags
90  * @param tag: the APE tag
91  * @param p_demux; the demux object
92  * @param p_demux_meta: the demuxer meta
93  * @param p_meta: the meta
94  */
95 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
96 {
97     APE::Item item;
98 #define SET( keyName, metaName ) \
99     item = tag->itemListMap()[keyName]; \
100     vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
101
102     SET( "COPYRIGHT", Copyright );
103     SET( "LANGUAGE", Language );
104     SET( "PUBLISHER", Publisher );
105
106 #undef SET
107 }
108
109
110
111 /**
112  * Read meta information from id3v2 tags
113  * @param tag: the id3v2 tag
114  * @param p_demux; the demux object
115  * @param p_demux_meta: the demuxer meta
116  * @param p_meta: the meta
117  */
118 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
119 {
120     // Get the unique file identifier
121     ID3v2::FrameList list = tag->frameListMap()["UFID"];
122     ID3v2::FrameList::Iterator iter;
123     for( iter = list.begin(); iter != list.end(); iter++ )
124     {
125         ID3v2::UniqueFileIdentifierFrame* p_ufid =
126                 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
127         const char *owner = p_ufid->owner().toCString();
128         if (!strcmp( owner, "http://musicbrainz.org" ))
129         {
130             /* ID3v2 UFID contains up to 64 bytes binary data
131              * but in our case it will be a '\0'
132              * terminated string */
133             char psz_ufid[64];
134             int j = 0;
135             int max_size = p_ufid->identifier().size() < 63 ?
136                            p_ufid->identifier().size() : 63;
137             while( j < max_size )
138             {
139                 psz_ufid[j] = p_ufid->identifier()[j];
140                 j++;
141             }
142             psz_ufid[j] = '\0';
143             vlc_meta_SetTrackID( p_meta, psz_ufid );
144         }
145     }
146
147     // Get the use text
148     list = tag->frameListMap()["TXXX"];
149     for( iter = list.begin(); iter != list.end(); iter++ )
150     {
151         ID3v2::UserTextIdentificationFrame* p_txxx =
152                 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
153         vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
154                            p_txxx->fieldList().toString().toCString( true ) );
155     }
156
157     // Get some more informations
158 #define SET( tagName, metaName )                                               \
159     list = tag->frameListMap()[tagName];                                       \
160     if( !list.isEmpty() )                                                      \
161         vlc_meta_Set##metaName( p_meta,                                        \
162                                 (*list.begin())->toString().toCString( true ) );
163
164     SET( "TCOP", Copyright );
165     SET( "TENC", EncodedBy );
166     SET( "TLAN", Language );
167     SET( "TPUB", Publisher );
168
169 #undef SET
170
171     /* Preferred type of image
172      * The 21 types are defined in id3v2 standard:
173      * http://www.id3.org/id3v2.4.0-frames */
174     static const int pi_cover_score[] = {
175         0,  /* Other */
176         5,  /* 32x32 PNG image that should be used as the file icon */
177         4,  /* File icon of a different size or format. */
178         20, /* Front cover image of the album. */
179         19, /* Back cover image of the album. */
180         13, /* Inside leaflet page of the album. */
181         18, /* Image from the album itself. */
182         17, /* Picture of the lead artist or soloist. */
183         16, /* Picture of the artist or performer. */
184         14, /* Picture of the conductor. */
185         15, /* Picture of the band or orchestra. */
186         9,  /* Picture of the composer. */
187         8,  /* Picture of the lyricist or text writer. */
188         7,  /* Picture of the recording location or studio. */
189         10, /* Picture of the artists during recording. */
190         11, /* Picture of the artists during performance. */
191         6,  /* Picture from a movie or video related to the track. */
192         1,  /* Picture of a large, coloured fish. */
193         12, /* Illustration related to the track. */
194         3,  /* Logo of the band or performer. */
195         2   /* Logo of the publisher (record company). */
196     };
197     int i_score = -1;
198
199     // Try now to get embedded art
200     list = tag->frameListMap()[ "APIC" ];
201     if( list.isEmpty() )
202         return;
203     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
204     for( ID3v2::FrameList::Iterator iter = list.begin();
205          iter != list.end(); iter++ )
206     {
207         ID3v2::AttachedPictureFrame* p_apic =
208             dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
209         input_attachment_t *p_attachment;
210
211         const char *psz_name, *psz_mime, *psz_description;
212         const char *p_data; int i_data;
213
214         psz_mime = p_apic->mimeType().toCString( true );
215         psz_description = psz_name = p_apic->description().toCString( true );
216
217         /* some old iTunes version not only sets incorrectly the mime type
218          * or the description of the image,
219          * but also embeds incorrectly the image.
220          * Recent versions seem to behave correctly */
221         if( !strncmp( psz_mime, "PNG", 3 ) ||
222             !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
223         {
224             msg_Warn( p_demux, "Invalid picture embedded by broken iTunes version" );
225             break;
226         }
227
228         p_data = p_apic->picture().data();
229         i_data = p_apic->picture().size();
230
231         msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
232                  psz_name, psz_mime, i_data );
233
234         p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
235                                 psz_description, p_data, i_data );
236         TAB_APPEND_CAST( (input_attachment_t**),
237                          p_demux_meta->i_attachments, p_demux_meta->attachments,
238                          p_attachment );
239
240         if( pi_cover_score[p_apic->type()] > i_score )
241         {
242             i_score = pi_cover_score[p_apic->type()];
243             char *psz_url;
244             if( asprintf( &psz_url, "attachment://%s",
245                           p_attachment->psz_name ) == -1 )
246                 break;
247             vlc_meta_SetArtURL( p_meta, psz_url );
248             free( psz_url );
249         }
250     }
251 }
252
253
254
255 /**
256  * Read the meta informations from XiphComments
257  * @param tag: the Xiph Comment
258  * @param p_demux; the demux object
259  * @param p_demux_meta: the demuxer meta
260  * @param p_meta: the meta
261  */
262 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
263 {
264 #define SET( keyName, metaName )                                               \
265     StringList list = tag->fieldListMap()[keyName];                            \
266     if( !list.isEmpty() )                                                      \
267         vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
268
269     SET( "COPYRIGHT", Copyright );
270 #undef SET
271
272     // Try now to get embedded art
273     StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
274     StringList art_list = tag->fieldListMap()[ "COVERART" ];
275
276     // We get only the first covert art
277     if( mime_list.size() > 1 || art_list.size() > 1 )
278         msg_Warn( p_demux, "Found %i embedded arts, so using only the first one",
279                   art_list.size() );
280     else if( mime_list.size() == 0 || art_list.size() == 0 )
281         return;
282
283     input_attachment_t *p_attachment;
284
285     const char* psz_name = "cover";
286     const char* psz_mime = mime_list[0].toCString(true);
287     const char* psz_description = "cover";
288
289     uint8_t *p_data;
290     int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
291
292     msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
293              psz_name, psz_mime, i_data );
294
295     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
296               p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
297               psz_description, p_data, i_data );
298     free( p_data );
299
300     TAB_APPEND_CAST( (input_attachment_t**),
301                      p_demux_meta->i_attachments, p_demux_meta->attachments,
302                      p_attachment );
303
304     vlc_meta_SetArtURL( p_meta, "attachment://cover" );
305 }
306
307
308
309 /**
310  * Get the tags from the file using TagLib
311  * @param p_this: the demux object
312  * @return VLC_SUCCESS if the operation success
313  */
314 static int ReadMeta( vlc_object_t* p_this)
315 {
316     demux_t*        p_demux = (demux_t*)p_this;
317     demux_meta_t*   p_demux_meta = (demux_meta_t*)p_demux->p_private;
318     vlc_meta_t*     p_meta;
319     TagLib::FileRef f;
320
321     p_demux_meta->p_meta = NULL;
322     const char* local_name = ToLocale( p_demux->psz_path );
323     if( !local_name )
324         return VLC_EGENERIC;
325     f = FileRef( local_name );
326     LocaleFree( local_name );
327
328     if( f.isNull() )
329         return VLC_EGENERIC;
330     if( !f.tag() || f.tag()->isEmpty() )
331         return VLC_EGENERIC;
332
333     p_demux_meta->p_meta = p_meta = vlc_meta_New();
334     if( !p_meta )
335         return VLC_ENOMEM;
336
337
338     // Read the tags from the file
339     Tag* p_tag = f.tag();
340
341 #define SET( tag, meta )                                                       \
342     if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() )                    \
343         vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
344 #define SETINT( tag, meta )                                                    \
345     if( p_tag->tag() )                                                         \
346     {                                                                          \
347         char psz_tmp[10];                                                      \
348         snprintf( psz_tmp, 10, "%d", p_tag->tag() );                           \
349         vlc_meta_Set##meta( p_meta, psz_tmp );                                 \
350     }
351
352     SET( title, Title );
353     SET( artist, Artist );
354     SET( album, Album );
355     SET( comment, Description );
356     SET( genre, Genre );
357     SETINT( year, Date );
358     SETINT( track, Tracknum );
359
360 #undef SETINT
361 #undef SET
362
363
364     // Try now to read special tags
365     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
366     {
367         if( flac->ID3v2Tag() )
368             ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
369         else if( flac->xiphComment() )
370             ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
371     }
372     else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
373     {
374         if( mpc->APETag() )
375             ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
376     }
377     else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
378     {
379         if( mpeg->ID3v2Tag() )
380             ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
381         else if( mpeg->APETag() )
382             ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
383     }
384     else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
385     {
386         if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
387             ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
388         else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
389             ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
390         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
391             ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
392     }
393     else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
394     {
395         if( trueaudio->ID3v2Tag() )
396             ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
397     }
398     else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
399     {
400         if( wavpack->APETag() )
401             ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
402     }
403
404     return VLC_SUCCESS;
405 }
406
407
408
409 /**
410  * Write meta informations to APE tags
411  * @param tag: the APE tag
412  * @param p_item: the input item
413  */
414 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
415 {
416     char* psz_meta;
417 #define WRITE( metaName, keyName )                      \
418     psz_meta = input_item_Get##metaName( p_item );      \
419     if( psz_meta )                                      \
420     {                                                   \
421         String key( keyName, String::UTF8 );            \
422         String value( psz_meta, String::UTF8 );         \
423         tag->addValue( key, value, true );              \
424     }                                                   \
425     free( psz_meta );
426
427     WRITE( Copyright, "COPYRIGHT" );
428     WRITE( Language, "LANGUAGE" );
429     WRITE( Publisher, "PUBLISHER" );
430
431 #undef WRITE
432 }
433
434
435
436 /**
437  * Write meta information to id3v2 tags
438  * @param tag: the id3v2 tag
439  * @param p_input: the input item
440  */
441 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
442 {
443     char* psz_meta;
444 #define WRITE( metaName, tagName )                                            \
445     psz_meta = input_item_Get##metaName( p_item );                            \
446     if( psz_meta )                                                            \
447     {                                                                         \
448         ByteVector p_byte( tagName, 4 );                                      \
449         tag->removeFrames( p_byte );                                         \
450         ID3v2::TextIdentificationFrame* p_frame =                             \
451             new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 );       \
452         p_frame->setText( psz_meta );                                         \
453         tag->addFrame( p_frame );                                             \
454     }                                                                         \
455     free( psz_meta );
456
457     WRITE( Copyright, "TCOP" );
458     WRITE( EncodedBy, "TENC" );
459     WRITE( Language,  "TLAN" );
460     WRITE( Publisher, "TPUB" );
461
462 #undef WRITE
463 }
464
465
466
467 /**
468  * Write the meta informations to XiphComments
469  * @param tag: the Xiph Comment
470  * @param p_input: the input item
471  */
472 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
473 {
474     char* psz_meta;
475 #define WRITE( metaName, keyName )                      \
476     psz_meta = input_item_Get##metaName( p_item );      \
477     if( psz_meta )                                      \
478     {                                                   \
479         String key( keyName, String::UTF8 );            \
480         String value( psz_meta, String::UTF8 );         \
481         tag->addField( key, value, true );              \
482     }                                                   \
483     free( psz_meta );
484
485     WRITE( Copyright, "COPYRIGHT" );
486
487 #undef WRITE
488 }
489
490
491
492 /**
493  * Set the tags to the file using TagLib
494  * @param p_this: the demux object
495  * @return VLC_SUCCESS if the operation success
496  */
497
498 static int WriteMeta( vlc_object_t *p_this )
499 {
500     playlist_t *p_playlist = (playlist_t *)p_this;
501     meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
502     input_item_t *p_item = p_export->p_item;
503
504     if( !p_item )
505     {
506         msg_Err( p_this, "Can't save meta data of an empty input" );
507         return VLC_EGENERIC;
508     }
509
510     FileRef f( p_export->psz_file );
511     if( f.isNull() || !f.tag() || f.file()->readOnly() )
512     {
513         msg_Err( p_this, "File %s can't be opened for tag writing\n",
514             p_export->psz_file );
515         return VLC_EGENERIC;
516     }
517
518     msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
519
520     Tag *p_tag = f.tag();
521
522     char *psz_meta;
523
524 #define SET( a, b )                                         \
525     if( b )                                                 \
526     {                                                       \
527         String* psz_tmp = new String( b, String::UTF8 );    \
528         p_tag->set##a( *psz_tmp );                          \
529         delete psz_tmp;                                     \
530     }
531
532     // Saving all common fields
533     // If the title is empty, use the name
534     psz_meta = input_item_GetTitle( p_item );
535     if( !psz_meta ) psz_meta = input_item_GetName( p_item );
536     SET( Title, psz_meta );
537     free( psz_meta );
538
539     psz_meta = input_item_GetArtist( p_item );
540     SET( Artist, psz_meta );
541     free( psz_meta );
542
543     psz_meta = input_item_GetAlbum( p_item );
544     SET( Album, psz_meta );
545     free( psz_meta );
546
547     psz_meta = input_item_GetDescription( p_item );
548     SET( Comment, psz_meta );
549     free( psz_meta );
550
551     psz_meta = input_item_GetGenre( p_item );
552     SET( Genre, psz_meta );
553     free( psz_meta );
554
555 #undef SET
556
557     psz_meta = input_item_GetDate( p_item );
558     if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
559     free( psz_meta );
560
561     psz_meta = input_item_GetTrackNum( p_item );
562     if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
563     free( psz_meta );
564
565
566     // Try now to write special tags
567     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
568     {
569         if( flac->ID3v2Tag() )
570             WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
571         else if( flac->xiphComment() )
572             WriteMetaToXiph( flac->xiphComment(), p_item );
573     }
574     else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
575     {
576         if( mpc->APETag() )
577             WriteMetaToAPE( mpc->APETag(), p_item );
578     }
579     else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
580     {
581         if( mpeg->ID3v2Tag() )
582             WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
583         else if( mpeg->APETag() )
584             WriteMetaToAPE( mpeg->APETag(), p_item );
585     }
586     else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
587     {
588         if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
589             WriteMetaToXiph( ogg_flac->tag(), p_item );
590         else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
591             WriteMetaToXiph( ogg_speex->tag(), p_item );
592         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
593             WriteMetaToXiph( ogg_vorbis->tag(), p_item );
594     }
595     else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
596     {
597         if( trueaudio->ID3v2Tag() )
598             WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
599     }
600     else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
601     {
602         if( wavpack->APETag() )
603             WriteMetaToAPE( wavpack->APETag(), p_item );
604     }
605
606     // Save the meta data
607     f.save();
608
609     return VLC_SUCCESS;
610 }
611
612
613
614 static int DownloadArt( vlc_object_t *p_this )
615 {
616     /* We need to be passed the file name
617      * Fetch the thing from the file, save it to the cache folder
618      */
619     return VLC_EGENERIC;
620 }
621