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