1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Rafaël Carré <funman@videolanorg>
9 * Rémi Duraffort <ivoire@videolan.org>
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.
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.
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_playlist.h>
34 #include <vlc_demux.h>
35 #include <vlc_strings.h>
36 #include <vlc_charset.h>
48 #include <tbytevector.h>
52 #include <xiphcomment.h>
58 #include <oggflacfile.h>
59 #include <speexfile.h>
60 #include <trueaudiofile.h>
61 #include <vorbisfile.h>
62 #include <wavpackfile.h>
64 #include <attachedpictureframe.h>
65 #include <textidentificationframe.h>
66 #include <uniquefileidentifierframe.h>
70 static int ReadMeta ( vlc_object_t * );
71 static int DownloadArt ( vlc_object_t * );
72 static int WriteMeta ( vlc_object_t * );
75 set_capability( "meta reader", 1000 )
76 set_callbacks( ReadMeta, NULL )
78 set_capability( "art downloader", 50 )
79 set_callbacks( DownloadArt, NULL )
81 set_capability( "meta writer", 50 )
82 set_callbacks( WriteMeta, NULL )
85 using namespace TagLib;
88 /* Try detecting embedded art */
89 static void DetectImage( FileRef f, demux_t *p_demux )
91 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
92 vlc_meta_t *p_meta = p_demux_meta->p_meta;
95 /* Preferred type of image
96 * The 21 types are defined in id3v2 standard:
97 * http://www.id3.org/id3v2.4.0-frames */
98 static const int pi_cover_score[] = {
100 5, /* 32x32 PNG image that should be used as the file icon */
101 4, /* File icon of a different size or format. */
102 20, /* Front cover image of the album. */
103 19, /* Back cover image of the album. */
104 13, /* Inside leaflet page of the album. */
105 18, /* Image from the album itself. */
106 17, /* Picture of the lead artist or soloist. */
107 16, /* Picture of the artist or performer. */
108 14, /* Picture of the conductor. */
109 15, /* Picture of the band or orchestra. */
110 9, /* Picture of the composer. */
111 8, /* Picture of the lyricist or text writer. */
112 7, /* Picture of the recording location or studio. */
113 10, /* Picture of the artists during recording. */
114 11, /* Picture of the artists during performance. */
115 6, /* Picture from a movie or video related to the track. */
116 1, /* Picture of a large, coloured fish. */
117 12, /* Illustration related to the track. */
118 3, /* Logo of the band or performer. */
119 2 /* Logo of the publisher (record company). */
122 if( MPEG::File *mpeg = dynamic_cast<MPEG::File *>(f.file() ) )
124 ID3v2::Tag *p_tag = mpeg->ID3v2Tag();
127 ID3v2::FrameList list = p_tag->frameListMap()[ "APIC" ];
130 ID3v2::AttachedPictureFrame *p_apic;
132 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
133 for( ID3v2::FrameList::Iterator iter = list.begin();
134 iter != list.end(); iter++ )
136 p_apic = dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
137 input_attachment_t *p_attachment;
139 const char *psz_name, *psz_mime, *psz_description;
140 ByteVector p_data_taglib; const char *p_data; int i_data;
142 psz_mime = p_apic->mimeType().toCString(true);
143 psz_description = psz_name = p_apic->description().toCString(true);
145 /* some old iTunes version not only sets incorrectly the mime type
146 * or the description of the image,
147 * but also embeds incorrectly the image.
148 * Recent versions seem to behave correctly */
149 if( !strncmp( psz_mime, "PNG", 3 ) ||
150 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
153 "%s: Invalid picture embedded by broken iTunes version, "
154 "you really shouldn't use this crappy software.",
155 (const char *)f.file()->name() );
159 p_data_taglib = p_apic->picture();
160 p_data = p_data_taglib.data();
161 i_data = p_data_taglib.size();
163 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
164 psz_name, psz_mime, i_data );
166 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
167 psz_description, p_data, i_data );
168 TAB_APPEND_CAST( (input_attachment_t**),
169 p_demux_meta->i_attachments, p_demux_meta->attachments,
172 if( pi_cover_score[p_apic->type()] > i_score )
174 i_score = pi_cover_score[p_apic->type()];
176 if( asprintf( &psz_url, "attachment://%s",
177 p_attachment->psz_name ) == -1 )
179 vlc_meta_SetArtURL( p_meta, psz_url );
185 if( Ogg::Vorbis::File *oggv = dynamic_cast<Ogg::Vorbis::File *>(f.file() ) )
187 Ogg::XiphComment *p_tag = oggv->tag();
191 StringList mime_list = p_tag->fieldListMap()[ "COVERARTMIME" ];
192 StringList art_list = p_tag->fieldListMap()[ "COVERART" ];
194 /* we support only one cover in ogg/vorbis */
195 if( mime_list.size() != 1 || art_list.size() != 1 )
198 input_attachment_t *p_attachment;
200 const char *psz_name, *psz_mime, *psz_description;
205 psz_mime = mime_list[0].toCString(true);
206 psz_description = "cover";
208 i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
210 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
211 psz_name, psz_mime, i_data );
213 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
214 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
215 psz_description, p_data, i_data );
218 TAB_APPEND_CAST( (input_attachment_t**),
219 p_demux_meta->i_attachments, p_demux_meta->attachments,
222 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
226 //flac embedded images are extracted in the flac demuxer
227 else if( FLAC::File *flac =
228 dynamic_cast<FLAC::File *>(f.file() ) )
230 p_tag = flac->ID3v2Tag();
233 ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
236 vlc_meta_SetArtURL( p_meta, "APIC" );
240 /* TagLib doesn't support MP4 file yet */
241 else if( MP4::File *mp4 =
242 dynamic_cast<MP4::File *>( f.file() ) )
245 dynamic_cast<MP4::Tag *>( mp4->tag() );
246 if( mp4tag && mp4tag->cover().size() )
247 vlc_meta_SetArtURL( p_meta, "MP4C" );
255 * Read meta informations from APE tags
256 * @param tag: the APE tag
257 * @param p_meta: the meta
258 * @return VLC_SUCCESS if everything goes ok
260 static int ReadMetaFromAPE( APE::Tag* tag, vlc_meta_t* p_meta )
263 #define SET( keyName, metaName ) \
264 item = tag->itemListMap()[keyName]; \
265 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
267 SET( "COPYRIGHT", Copyright );
268 SET( "LANGUAGE", Language );
269 SET( "PUBLISHER", Publisher );
278 * Read meta information from id3v2 tags
279 * @param tag: the id3v2 tag
280 * @param p_meta: the meta
281 * @return VLC_SUCCESS if everything goes ok
283 static int ReadMetaFromId2v2( ID3v2::Tag* tag, vlc_meta_t* p_meta )
285 // Get the unique file identifier
286 ID3v2::FrameList list = tag->frameListMap()["UFID"];
287 ID3v2::FrameList::Iterator iter;
288 for( iter = list.begin(); iter != list.end(); iter++ )
290 ID3v2::UniqueFileIdentifierFrame* p_ufid =
291 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
292 const char *owner = p_ufid->owner().toCString();
293 if (!strcmp( owner, "http://musicbrainz.org" ))
295 /* ID3v2 UFID contains up to 64 bytes binary data
296 * but in our case it will be a '\0'
297 * terminated string */
300 int max_size = p_ufid->identifier().size() < 63 ?
301 p_ufid->identifier().size() : 63;
302 while( j < max_size )
303 psz_ufid[j] = p_ufid->identifier()[j++];
305 vlc_meta_SetTrackID( p_meta, psz_ufid );
310 list = tag->frameListMap()["TXXX"];
311 for( iter = list.begin(); iter != list.end(); iter++ )
313 ID3v2::UserTextIdentificationFrame* p_txxx =
314 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
315 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
316 p_txxx->fieldList().toString().toCString( true ) );
319 // Get some more informations
320 #define SET( tagName, metaName ) \
321 list = tag->frameListMap()[tagName]; \
322 if( !list.isEmpty() ) \
323 vlc_meta_Set##metaName( p_meta, \
324 (*list.begin())->toString().toCString( true ) );
326 SET( "TCOP", Copyright );
327 SET( "TENC", EncodedBy );
328 SET( "TLAN", Language );
329 SET( "TPUB", Publisher );
338 * Read the meta informations from XiphComments
339 * @param tag: the Xiph Comment
340 * @param p_meta: the meta
341 * @return VLC_SUCCESS if everything goes ok
343 static int ReadMetaFromXiph( Ogg::XiphComment* tag, vlc_meta_t* p_meta )
345 #define SET( metaName, keyName ) \
346 StringList list = tag->fieldListMap()[keyName]; \
347 if( !list.isEmpty() ) \
348 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
350 SET( Copyright, "COPYRIGHT" );
358 * Get the tags from the file using TagLib
359 * @param p_this: the demux object
360 * @return VLC_SUCCESS if the operation success
362 static int ReadMeta( vlc_object_t* p_this)
364 demux_t* p_demux = (demux_t*)p_this;
365 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
369 p_demux_meta->p_meta = NULL;
370 const char* local_name = ToLocale( p_demux->psz_path );
373 f = FileRef( local_name );
374 LocaleFree( local_name );
378 if( !f.tag() || f.tag()->isEmpty() )
381 p_demux_meta->p_meta = p_meta = vlc_meta_New();
386 // Read the tags from the file
387 Tag* p_tag = f.tag();
389 #define SET( meta, tag ) \
390 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
391 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
392 #define SETINT( meta, tag ) \
396 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
397 vlc_meta_Set##meta( p_meta, psz_tmp ); \
401 SET( Artist, artist );
403 SET( Description, comment );
405 SETINT( Date, year );
406 SETINT( Tracknum, track );
412 // Try now to read special tags
413 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
415 if( flac->ID3v2Tag() )
416 ReadMetaFromId2v2( flac->ID3v2Tag(), p_meta );
417 else if( flac->xiphComment() )
418 ReadMetaFromXiph( flac->xiphComment(), p_meta );
420 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
423 ReadMetaFromAPE( mpc->APETag(), p_meta );
425 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
427 if( mpeg->ID3v2Tag() )
428 ReadMetaFromId2v2( mpeg->ID3v2Tag(), p_meta );
429 else if( mpeg->APETag() )
430 ReadMetaFromAPE( mpeg->APETag(), p_meta );
432 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
434 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
435 ReadMetaFromXiph( ogg_flac->tag(), p_meta );
436 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
437 ReadMetaFromXiph( ogg_speex->tag(), p_meta );
438 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
439 ReadMetaFromXiph( ogg_vorbis->tag(), p_meta );
441 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
443 if( trueaudio->ID3v2Tag() )
444 ReadMetaFromId2v2( trueaudio->ID3v2Tag(), p_meta );
446 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
448 if( wavpack->APETag() )
449 ReadMetaFromAPE( wavpack->APETag(), p_meta );
452 // Try now to find a image
453 DetectImage( f, p_demux );
461 * Write meta informations to APE tags
462 * @param tag: the APE tag
463 * @param p_item: the input item
464 * @return VLC_SUCCESS if everything goes ok
466 static int WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
469 #define WRITE( metaName, keyName ) \
470 psz_meta = input_item_Get##metaName( p_item ); \
473 String key( keyName, String::UTF8 ); \
474 String value( psz_meta, String::UTF8 ); \
475 tag->addValue( key, value, true ); \
479 WRITE( Copyright, "COPYRIGHT" );
480 WRITE( Language, "LANGUAGE" );
481 WRITE( Publisher, "PUBLISHER" );
491 * Write meta information to id3v2 tags
492 * @param tag: the id3v2 tag
493 * @param p_input: the input item
494 * @return VLC_SUCCESS if everything goes ok
496 static int WriteMetaToId2v2( ID3v2::Tag* tag, input_item_t* p_item )
499 #define WRITE( metaName, tagName ) \
500 psz_meta = input_item_Get##metaName( p_item ); \
503 ByteVector p_byte( tagName, 4 ); \
504 tag->removeFrames( p_byte ); \
505 ID3v2::TextIdentificationFrame* p_frame = \
506 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
507 p_frame->setText( psz_meta ); \
508 tag->addFrame( p_frame ); \
512 WRITE( Copyright, "TCOP" );
513 WRITE( EncodedBy, "TENC" );
514 WRITE( Language, "TLAN" );
515 WRITE( Publisher, "TPUB" );
524 * Write the meta informations to XiphComments
525 * @param tag: the Xiph Comment
526 * @param p_input: the input item
527 * @return VLC_SUCCESS if everything goes ok
529 static int WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
532 #define WRITE( metaName, keyName ) \
533 psz_meta = input_item_Get##metaName( p_item ); \
536 String key( keyName, String::UTF8 ); \
537 String value( psz_meta, String::UTF8 ); \
538 tag->addField( key, value, true ); \
542 WRITE( Copyright, "COPYRIGHT" );
551 * Set the tags to the file using TagLib
552 * @param p_this: the demux object
553 * @return VLC_SUCCESS if the operation success
556 static int WriteMeta( vlc_object_t *p_this )
558 playlist_t *p_playlist = (playlist_t *)p_this;
559 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
560 input_item_t *p_item = p_export->p_item;
564 msg_Err( p_this, "Can't save meta data of an empty input" );
568 FileRef f( p_export->psz_file );
569 if( f.isNull() || !f.tag() || f.file()->readOnly() )
571 msg_Err( p_this, "File %s can't be opened for tag writing\n",
572 p_export->psz_file );
576 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
578 Tag *p_tag = f.tag();
582 #define SET( a, b ) \
585 String* psz_tmp = new String( b, String::UTF8 ); \
586 p_tag->set##a( *psz_tmp ); \
590 // Saving all common fields
591 // If the title is empty, use the name
592 psz_meta = input_item_GetTitle( p_item );
593 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
594 SET( Title, psz_meta );
597 psz_meta = input_item_GetArtist( p_item );
598 SET( Artist, psz_meta );
601 psz_meta = input_item_GetAlbum( p_item );
602 SET( Album, psz_meta );
605 psz_meta = input_item_GetDescription( p_item );
606 SET( Comment, psz_meta );
609 psz_meta = input_item_GetGenre( p_item );
610 SET( Genre, psz_meta );
615 psz_meta = input_item_GetDate( p_item );
616 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
619 psz_meta = input_item_GetTrackNum( p_item );
620 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
624 // Try now to write special tags
625 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
627 if( flac->ID3v2Tag() )
628 WriteMetaToId2v2( flac->ID3v2Tag(), p_item );
629 else if( flac->xiphComment() )
630 WriteMetaToXiph( flac->xiphComment(), p_item );
632 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
635 WriteMetaToAPE( mpc->APETag(), p_item );
637 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
639 if( mpeg->ID3v2Tag() )
640 WriteMetaToId2v2( mpeg->ID3v2Tag(), p_item );
641 else if( mpeg->APETag() )
642 WriteMetaToAPE( mpeg->APETag(), p_item );
644 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
646 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
647 WriteMetaToXiph( ogg_flac->tag(), p_item );
648 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
649 WriteMetaToXiph( ogg_speex->tag(), p_item );
650 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
651 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
653 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
655 if( trueaudio->ID3v2Tag() )
656 WriteMetaToId2v2( trueaudio->ID3v2Tag(), p_item );
658 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
660 if( wavpack->APETag() )
661 WriteMetaToAPE( wavpack->APETag(), p_item );
664 // Save the meta data
672 static int DownloadArt( vlc_object_t *p_this )
674 /* We need to be passed the file name
675 * Fetch the thing from the file, save it to the cache folder