]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
dbus: Correctly implement the GetTracksMetadata method
[vlc] / modules / meta_engine / taglib.cpp
1 /*****************************************************************************
2  * taglib.cpp: Taglib tag parser/writer
3  *****************************************************************************
4  * Copyright (C) 2003-2009 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_meta.h>
33 #include <vlc_demux.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
36 #include <vlc_url.h>
37 #include <vlc_input_item.h>
38 #include <vlc_input.h> /* for attachment_new */
39
40 #ifdef WIN32
41 # include <io.h>
42 #else
43 # include <unistd.h>
44 #endif
45
46
47 // Taglib headers
48 #include <fileref.h>
49 #include <tag.h>
50 #include <tbytevector.h>
51
52 #include <apetag.h>
53 #include <id3v2tag.h>
54 #include <xiphcomment.h>
55
56 #ifdef HAVE_TAGLIB_APEFILE_H
57 #include <apefile.h>
58 #endif
59 #include <flacfile.h>
60 #include <mpcfile.h>
61 #include <mpegfile.h>
62 #include <oggfile.h>
63 #include <oggflacfile.h>
64
65 #ifdef TAGLIB_WITH_ASF
66 # include <aifffile.h>
67 # include <wavfile.h>
68 #endif
69
70 #ifdef TAGLIB_WITH_MP4
71 # include <mp4file.h>
72 #endif
73
74 #include <speexfile.h>
75 #include <trueaudiofile.h>
76 #include <vorbisfile.h>
77 #include <wavpackfile.h>
78
79 #include <attachedpictureframe.h>
80 #include <textidentificationframe.h>
81 #include <uniquefileidentifierframe.h>
82
83 // taglib is not thread safe
84 static vlc_mutex_t taglib_lock = VLC_STATIC_MUTEX;
85
86 // Local functions
87 static int ReadMeta    ( vlc_object_t * );
88 static int WriteMeta   ( vlc_object_t * );
89
90 vlc_module_begin ()
91     set_capability( "meta reader", 1000 )
92     set_callbacks( ReadMeta, NULL )
93     add_submodule ()
94         set_capability( "meta writer", 50 )
95         set_callbacks( WriteMeta, NULL )
96 vlc_module_end ()
97
98 using namespace TagLib;
99
100
101 /**
102  * Read meta information from APE tags
103  * @param tag: the APE tag
104  * @param p_demux_meta: the demuxer meta
105  * @param p_meta: the meta
106  */
107 static void ReadMetaFromAPE( APE::Tag* tag, demux_meta_t*, vlc_meta_t* p_meta )
108 {
109     APE::Item item;
110 #define SET( keyName, metaName ) \
111     item = tag->itemListMap()[keyName]; \
112     vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
113
114     SET( "COPYRIGHT", Copyright );
115     SET( "LANGUAGE", Language );
116     SET( "PUBLISHER", Publisher );
117
118 #undef SET
119 }
120
121
122
123 /**
124  * Read meta information from id3v2 tags
125  * @param tag: the id3v2 tag
126  * @param p_demux_meta: the demuxer meta
127  * @param p_meta: the meta
128  */
129 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
130 {
131     // Get the unique file identifier
132     ID3v2::FrameList list = tag->frameListMap()["UFID"];
133     ID3v2::FrameList::Iterator iter;
134     for( iter = list.begin(); iter != list.end(); iter++ )
135     {
136         ID3v2::UniqueFileIdentifierFrame* p_ufid =
137                 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
138         if( !p_ufid )
139             continue;
140         const char *owner = p_ufid->owner().toCString();
141         if (!strcmp( owner, "http://musicbrainz.org" ))
142         {
143             /* ID3v2 UFID contains up to 64 bytes binary data
144              * but in our case it will be a '\0'
145              * terminated string */
146             char psz_ufid[64];
147             int max_size = __MIN( p_ufid->identifier().size(), 63);
148             strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
149             psz_ufid[max_size] = '\0';
150             vlc_meta_SetTrackID( p_meta, psz_ufid );
151         }
152     }
153
154     // Get the use text
155     list = tag->frameListMap()["TXXX"];
156     for( iter = list.begin(); iter != list.end(); iter++ )
157     {
158         ID3v2::UserTextIdentificationFrame* p_txxx =
159                 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
160         if( !p_txxx )
161             continue;
162         vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
163                            p_txxx->fieldList().back().toCString( true ) );
164     }
165
166     // Get some more information
167 #define SET( tagName, metaName )                                               \
168     list = tag->frameListMap()[tagName];                                       \
169     if( !list.isEmpty() )                                                      \
170         vlc_meta_Set##metaName( p_meta,                                        \
171                                 (*list.begin())->toString().toCString( true ) );
172
173     SET( "TCOP", Copyright );
174     SET( "TENC", EncodedBy );
175     SET( "TLAN", Language );
176     SET( "TPUB", Publisher );
177
178 #undef SET
179
180     /* Preferred type of image
181      * The 21 types are defined in id3v2 standard:
182      * http://www.id3.org/id3v2.4.0-frames */
183     static const int pi_cover_score[] = {
184         0,  /* Other */
185         5,  /* 32x32 PNG image that should be used as the file icon */
186         4,  /* File icon of a different size or format. */
187         20, /* Front cover image of the album. */
188         19, /* Back cover image of the album. */
189         13, /* Inside leaflet page of the album. */
190         18, /* Image from the album itself. */
191         17, /* Picture of the lead artist or soloist. */
192         16, /* Picture of the artist or performer. */
193         14, /* Picture of the conductor. */
194         15, /* Picture of the band or orchestra. */
195         9,  /* Picture of the composer. */
196         8,  /* Picture of the lyricist or text writer. */
197         7,  /* Picture of the recording location or studio. */
198         10, /* Picture of the artists during recording. */
199         11, /* Picture of the artists during performance. */
200         6,  /* Picture from a movie or video related to the track. */
201         1,  /* Picture of a large, coloured fish. */
202         12, /* Illustration related to the track. */
203         3,  /* Logo of the band or performer. */
204         2   /* Logo of the publisher (record company). */
205     };
206     #define PI_COVER_SCORE_SIZE (sizeof (pi_cover_score) / sizeof (pi_cover_score[0]))
207     int i_score = -1;
208
209     // Try now to get embedded art
210     list = tag->frameListMap()[ "APIC" ];
211     if( list.isEmpty() )
212         return;
213
214     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
215     for( iter = list.begin(); iter != list.end(); iter++ )
216     {
217         ID3v2::AttachedPictureFrame* p_apic =
218             dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
219         if( !p_apic )
220             continue;
221         input_attachment_t *p_attachment;
222
223         const char *psz_mime;
224         char *psz_name, *psz_description;
225
226         // Get the mime and description of the image.
227         // If the description is empty, take the type as a description
228         psz_mime = p_apic->mimeType().toCString( true );
229         if( p_apic->description().size() > 0 )
230             psz_description = strdup( p_apic->description().toCString( true ) );
231         else
232         {
233             if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
234                 psz_description = NULL;
235         }
236
237         if( !psz_description )
238             continue;
239         psz_name = psz_description;
240
241         /* some old iTunes version not only sets incorrectly the mime type
242          * or the description of the image,
243          * but also embeds incorrectly the image.
244          * Recent versions seem to behave correctly */
245         if( !strncmp( psz_mime, "PNG", 3 ) ||
246             !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
247         {
248             msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
249             free( psz_description );
250             continue;
251         }
252
253         const ByteVector picture = p_apic->picture();
254         const char *p_data = picture.data();
255         const unsigned i_data = picture.size();
256
257         msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
258                  psz_name, psz_mime, i_data );
259
260         p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
261                                 psz_description, p_data, i_data );
262         if( p_attachment )
263             TAB_APPEND_CAST( (input_attachment_t**),
264                              p_demux_meta->i_attachments, p_demux_meta->attachments,
265                              p_attachment );
266         free( psz_description );
267
268         unsigned i_pic_type = p_apic->type();
269         if( i_pic_type >= PI_COVER_SCORE_SIZE )
270             i_pic_type = 0; // Defaults to "Other"
271
272         if( pi_cover_score[i_pic_type] > i_score )
273         {
274             i_score = pi_cover_score[i_pic_type];
275             char *psz_url;
276             if( asprintf( &psz_url, "attachment://%s",
277                           p_attachment->psz_name ) == -1 )
278                 continue;
279             vlc_meta_SetArtURL( p_meta, psz_url );
280             free( psz_url );
281         }
282     }
283 }
284
285
286
287 /**
288  * Read the meta information from XiphComments
289  * @param tag: the Xiph Comment
290  * @param p_demux_meta: the demuxer meta
291  * @param p_meta: the meta
292  */
293 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
294 {
295     StringList list;
296 #define SET( keyName, metaName )                                               \
297     list = tag->fieldListMap()[keyName];                                       \
298     if( !list.isEmpty() )                                                      \
299         vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
300
301     SET( "COPYRIGHT", Copyright );
302 #undef SET
303
304     // Try now to get embedded art
305     StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
306     StringList art_list = tag->fieldListMap()[ "COVERART" ];
307
308     // We get only the first covert art
309     if( mime_list.size() > 1 || art_list.size() > 1 )
310         msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
311                   art_list.size() );
312     else if( mime_list.size() == 0 || art_list.size() == 0 )
313         return;
314
315     input_attachment_t *p_attachment;
316
317     const char* psz_name = "cover";
318     const char* psz_mime = mime_list[0].toCString(true);
319     const char* psz_description = "cover";
320
321     uint8_t *p_data;
322     int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
323
324     msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
325              psz_name, psz_mime, i_data );
326
327     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
328               p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
329               psz_description, p_data, i_data );
330     free( p_data );
331
332     TAB_APPEND_CAST( (input_attachment_t**),
333                      p_demux_meta->i_attachments, p_demux_meta->attachments,
334                      p_attachment );
335
336     vlc_meta_SetArtURL( p_meta, "attachment://cover" );
337 }
338
339 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
340 static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
341 {
342     if( tag->itemListMap().contains("covr") )
343     {
344         MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
345         const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
346
347         msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
348                  psz_format, list[0].data().size() );
349
350         TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
351         input_attachment_t *p_attachment =
352                 vlc_input_attachment_New( "cover", psz_format, "cover",
353                                           list[0].data().data(), list[0].data().size() );
354         TAB_APPEND_CAST( (input_attachment_t**),
355                          p_demux_meta->i_attachments, p_demux_meta->attachments,
356                          p_attachment );
357         vlc_meta_SetArtURL( p_meta, "attachment://cover" );
358     }
359 }
360 #endif
361
362 /**
363  * Get the tags from the file using TagLib
364  * @param p_this: the demux object
365  * @return VLC_SUCCESS if the operation success
366  */
367 static int ReadMeta( vlc_object_t* p_this)
368 {
369     vlc_mutex_locker locker (&taglib_lock);
370     demux_meta_t*   p_demux_meta = (demux_meta_t *)p_this;
371     demux_t*        p_demux = p_demux_meta->p_demux;
372     vlc_meta_t*     p_meta;
373     FileRef f;
374
375     p_demux_meta->p_meta = NULL;
376     if( strcmp( p_demux->psz_access, "file" ) )
377         return VLC_EGENERIC;
378
379     char *psz_path = strdup( p_demux->psz_file );
380     if( !psz_path )
381         return VLC_ENOMEM;
382
383 #if defined(WIN32) || defined (UNDER_CE)
384     wchar_t wpath[MAX_PATH + 1];
385     if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
386     {
387         free( psz_path );
388         return VLC_EGENERIC;
389     }
390     wpath[MAX_PATH] = L'\0';
391     f = FileRef( wpath );
392 #else
393     const char* local_name = ToLocale( psz_path );
394     if( !local_name )
395     {
396         free( psz_path );
397         return VLC_EGENERIC;
398     }
399     f = FileRef( local_name );
400     LocaleFree( local_name );
401 #endif
402     free( psz_path );
403
404     if( f.isNull() )
405         return VLC_EGENERIC;
406     if( !f.tag() || f.tag()->isEmpty() )
407         return VLC_EGENERIC;
408
409     p_demux_meta->p_meta = p_meta = vlc_meta_New();
410     if( !p_meta )
411         return VLC_ENOMEM;
412
413
414     // Read the tags from the file
415     Tag* p_tag = f.tag();
416
417 #define SET( tag, meta )                                                       \
418     if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() )                    \
419         vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
420 #define SETINT( tag, meta )                                                    \
421     if( p_tag->tag() )                                                         \
422     {                                                                          \
423         char psz_tmp[10];                                                      \
424         snprintf( psz_tmp, 10, "%d", p_tag->tag() );                           \
425         vlc_meta_Set##meta( p_meta, psz_tmp );                                 \
426     }
427
428     SET( title, Title );
429     SET( artist, Artist );
430     SET( album, Album );
431     SET( comment, Description );
432     SET( genre, Genre );
433     SETINT( year, Date );
434     SETINT( track, TrackNum );
435
436 #undef SETINT
437 #undef SET
438
439
440     // Try now to read special tags
441 #ifdef HAVE_TAGLIB_APEFILE_H
442     if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
443     {
444         if( ape->APETag() )
445             ReadMetaFromAPE( ape->APETag(), p_demux_meta, p_meta );
446     }
447     else
448 #endif
449     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
450     {
451         if( flac->ID3v2Tag() )
452             ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux_meta, p_meta );
453         else if( flac->xiphComment() )
454             ReadMetaFromXiph( flac->xiphComment(), p_demux_meta, p_meta );
455     }
456 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
457     else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
458     {
459         if( mp4->tag() )
460             ReadMetaFromMP4( mp4->tag(), p_demux_meta, p_meta );
461     }
462 #endif
463     else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
464     {
465         if( mpc->APETag() )
466             ReadMetaFromAPE( mpc->APETag(), p_demux_meta, p_meta );
467     }
468     else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
469     {
470         if( mpeg->ID3v2Tag() )
471             ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux_meta, p_meta );
472         else if( mpeg->APETag() )
473             ReadMetaFromAPE( mpeg->APETag(), p_demux_meta, p_meta );
474     }
475     else if( dynamic_cast<Ogg::File*>(f.file()) )
476     {
477         if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
478             ReadMetaFromXiph( ogg_flac->tag(), p_demux_meta, p_meta );
479         else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
480             ReadMetaFromXiph( ogg_speex->tag(), p_demux_meta, p_meta );
481         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
482             ReadMetaFromXiph( ogg_vorbis->tag(), p_demux_meta, p_meta );
483     }
484 #ifdef TAGLIB_WITH_ASF
485     else if( dynamic_cast<RIFF::File*>(f.file()) )
486     {
487         if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
488             ReadMetaFromId3v2( riff_aiff->tag(), p_demux_meta, p_meta );
489         else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
490             ReadMetaFromId3v2( riff_wav->tag(), p_demux_meta, p_meta );
491     }
492 #endif
493     else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
494     {
495         if( trueaudio->ID3v2Tag() )
496             ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux_meta, p_meta );
497     }
498     else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
499     {
500         if( wavpack->APETag() )
501             ReadMetaFromAPE( wavpack->APETag(), p_demux_meta, p_meta );
502     }
503
504     return VLC_SUCCESS;
505 }
506
507
508
509 /**
510  * Write meta information to APE tags
511  * @param tag: the APE tag
512  * @param p_item: the input item
513  */
514 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
515 {
516     char* psz_meta;
517 #define WRITE( metaName, keyName )                      \
518     psz_meta = input_item_Get##metaName( p_item );      \
519     if( psz_meta )                                      \
520     {                                                   \
521         String key( keyName, String::UTF8 );            \
522         String value( psz_meta, String::UTF8 );         \
523         tag->addValue( key, value, true );              \
524     }                                                   \
525     free( psz_meta );
526
527     WRITE( Copyright, "COPYRIGHT" );
528     WRITE( Language, "LANGUAGE" );
529     WRITE( Publisher, "PUBLISHER" );
530
531 #undef WRITE
532 }
533
534
535
536 /**
537  * Write meta information to id3v2 tags
538  * @param tag: the id3v2 tag
539  * @param p_input: the input item
540  */
541 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
542 {
543     char* psz_meta;
544 #define WRITE( metaName, tagName )                                            \
545     psz_meta = input_item_Get##metaName( p_item );                            \
546     if( psz_meta )                                                            \
547     {                                                                         \
548         ByteVector p_byte( tagName, 4 );                                      \
549         tag->removeFrames( p_byte );                                         \
550         ID3v2::TextIdentificationFrame* p_frame =                             \
551             new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 );       \
552         p_frame->setText( psz_meta );                                         \
553         tag->addFrame( p_frame );                                             \
554     }                                                                         \
555     free( psz_meta );
556
557     WRITE( Copyright, "TCOP" );
558     WRITE( EncodedBy, "TENC" );
559     WRITE( Language,  "TLAN" );
560     WRITE( Publisher, "TPUB" );
561
562 #undef WRITE
563 }
564
565
566
567 /**
568  * Write the meta information to XiphComments
569  * @param tag: the Xiph Comment
570  * @param p_input: the input item
571  */
572 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
573 {
574     char* psz_meta;
575 #define WRITE( metaName, keyName )                      \
576     psz_meta = input_item_Get##metaName( p_item );      \
577     if( psz_meta )                                      \
578     {                                                   \
579         String key( keyName, String::UTF8 );            \
580         String value( psz_meta, String::UTF8 );         \
581         tag->addField( key, value, true );              \
582     }                                                   \
583     free( psz_meta );
584
585     WRITE( Copyright, "COPYRIGHT" );
586
587 #undef WRITE
588 }
589
590
591
592 /**
593  * Set the tags to the file using TagLib
594  * @param p_this: the demux object
595  * @return VLC_SUCCESS if the operation success
596  */
597
598 static int WriteMeta( vlc_object_t *p_this )
599 {
600     vlc_mutex_locker locker (&taglib_lock);
601     meta_export_t *p_export = (meta_export_t *)p_this;
602     input_item_t *p_item = p_export->p_item;
603     FileRef f;
604
605     if( !p_item )
606     {
607         msg_Err( p_this, "Can't save meta data of an empty input" );
608         return VLC_EGENERIC;
609     }
610
611 #if defined(WIN32) || defined (UNDER_CE)
612     wchar_t wpath[MAX_PATH + 1];
613     if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
614         return VLC_EGENERIC;
615     wpath[MAX_PATH] = L'\0';
616     f = FileRef( wpath );
617 #else
618     const char* local_name = ToLocale( p_export->psz_file );
619     if( !local_name )
620         return VLC_EGENERIC;
621     f = FileRef( local_name );
622     LocaleFree( local_name );
623 #endif
624
625     if( f.isNull() || !f.tag() || f.file()->readOnly() )
626     {
627         msg_Err( p_this, "File %s can't be opened for tag writing",
628                  p_export->psz_file );
629         return VLC_EGENERIC;
630     }
631
632     msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
633
634     Tag *p_tag = f.tag();
635
636     char *psz_meta;
637
638 #define SET( a, b )                                             \
639     psz_meta = input_item_Get ## a( p_item );                   \
640     if( psz_meta )                                              \
641     {                                                           \
642         String tmp( psz_meta, String::UTF8 );                   \
643         p_tag->set##b( tmp );                                   \
644     }                                                           \
645     free( psz_meta );
646
647     // Saving all common fields
648     // If the title is empty, use the name
649     SET( TitleFbName, Title );
650     SET( Artist, Artist );
651     SET( Album, Album );
652     SET( Description, Comment );
653     SET( Genre, Genre );
654
655 #undef SET
656
657     psz_meta = input_item_GetDate( p_item );
658     if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
659     free( psz_meta );
660
661     psz_meta = input_item_GetTrackNum( p_item );
662     if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
663     free( psz_meta );
664
665
666     // Try now to write special tags
667 #ifdef HAVE_TAGLIB_APEFILE_H
668     if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
669     {
670         if( ape->APETag() )
671             WriteMetaToAPE( ape->APETag(), p_item );
672     }
673     else
674 #endif
675     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
676     {
677         if( flac->ID3v2Tag() )
678             WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
679         else if( flac->xiphComment() )
680             WriteMetaToXiph( flac->xiphComment(), p_item );
681     }
682     else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
683     {
684         if( mpc->APETag() )
685             WriteMetaToAPE( mpc->APETag(), p_item );
686     }
687     else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
688     {
689         if( mpeg->ID3v2Tag() )
690             WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
691         else if( mpeg->APETag() )
692             WriteMetaToAPE( mpeg->APETag(), p_item );
693     }
694     else if( dynamic_cast<Ogg::File*>(f.file()) )
695     {
696         if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
697             WriteMetaToXiph( ogg_flac->tag(), p_item );
698         else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
699             WriteMetaToXiph( ogg_speex->tag(), p_item );
700         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
701             WriteMetaToXiph( ogg_vorbis->tag(), p_item );
702     }
703 #ifdef TAGLIB_WITH_ASF
704     else if( dynamic_cast<RIFF::File*>(f.file()) )
705     {
706         if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
707             WriteMetaToId3v2( riff_aiff->tag(), p_item );
708         else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
709             WriteMetaToId3v2( riff_wav->tag(), p_item );
710     }
711 #endif
712     else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
713     {
714         if( trueaudio->ID3v2Tag() )
715             WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
716     }
717     else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
718     {
719         if( wavpack->APETag() )
720             WriteMetaToAPE( wavpack->APETag(), p_item );
721     }
722
723     // Save the meta data
724     f.save();
725
726     return VLC_SUCCESS;
727 }
728