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