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