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