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