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 )
268 * Read meta information from id3v2 tags
269 * @param tag: the id3v2 tag
270 * @param p_meta: the meta
271 * @return VLC_SUCCESS if everything goes ok
273 static int ReadMetaFromId2v2( ID3v2::Tag* tag, vlc_meta_t* p_meta )
275 // Get the unique file identifier
276 ID3v2::FrameList list = tag->frameListMap()["UFID"];
277 ID3v2::FrameList::Iterator iter;
278 for( iter = list.begin(); iter != list.end(); iter++ )
280 ID3v2::UniqueFileIdentifierFrame* p_ufid =
281 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
282 const char *owner = p_ufid->owner().toCString();
283 if (!strcmp( owner, "http://musicbrainz.org" ))
285 /* ID3v2 UFID contains up to 64 bytes binary data
286 * but in our case it will be a '\0'
287 * terminated string */
290 int max_size = p_ufid->identifier().size() < 63 ?
291 p_ufid->identifier().size() : 63;
292 while( j < max_size )
293 psz_ufid[j] = p_ufid->identifier()[j++];
295 vlc_meta_SetTrackID( p_meta, psz_ufid );
300 list = tag->frameListMap()["TXXX"];
301 for( iter = list.begin(); iter != list.end(); iter++ )
303 ID3v2::UserTextIdentificationFrame* p_txxx =
304 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
305 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
306 p_txxx->fieldList().toString().toCString( true ) );
309 // Get some more informations
310 #define SET( tagName, metaName ) \
311 list = tag->frameListMap()[tagName]; \
312 if( !list.isEmpty() ) \
313 vlc_meta_Set##metaName( p_meta, \
314 (*list.begin())->toString().toCString( true ) );
316 SET( "TCOP", Copyright );
317 SET( "TENC", EncodedBy );
318 SET( "TLAN", Language );
319 SET( "TPUB", Publisher );
328 * Read the meta informations from XiphComments
329 * @param tag: the Xiph Comment
330 * @param p_meta: the meta
331 * @return VLC_SUCCESS if everything goes ok
333 static int ReadMetaFromXiph( Ogg::XiphComment* tag, vlc_meta_t* p_meta )
335 #define SET( metaName, keyName ) \
336 StringList list = tag->fieldListMap()[keyName]; \
337 if( !list.isEmpty() ) \
338 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
340 SET( Copyright, "COPYRIGHT" );
348 * Get the tags from the file using TagLib
349 * @param p_this: the demux object
350 * @return VLC_SUCCESS if the operation success
352 static int ReadMeta( vlc_object_t* p_this)
354 demux_t* p_demux = (demux_t*)p_this;
355 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
359 p_demux_meta->p_meta = NULL;
360 const char* local_name = ToLocale( p_demux->psz_path );
363 f = FileRef( local_name );
364 LocaleFree( local_name );
368 if( !f.tag() || f.tag()->isEmpty() )
371 p_demux_meta->p_meta = p_meta = vlc_meta_New();
376 // Read the tags from the file
377 Tag* p_tag = f.tag();
379 #define SET( meta, tag ) \
380 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
381 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
382 #define SETINT( meta, tag ) \
386 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
387 vlc_meta_Set##meta( p_meta, psz_tmp ); \
391 SET( Artist, artist );
393 SET( Description, comment );
395 SETINT( Date, year );
396 SETINT( Tracknum, track );
402 // Try now to read special tags
403 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
405 if( flac->ID3v2Tag() )
406 ReadMetaFromId2v2( flac->ID3v2Tag(), p_meta );
407 else if( flac->xiphComment() )
408 ReadMetaFromXiph( flac->xiphComment(), p_meta );
410 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
413 ReadMetaFromAPE( mpc->APETag(), p_meta );
415 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
417 if( mpeg->ID3v2Tag() )
418 ReadMetaFromId2v2( mpeg->ID3v2Tag(), p_meta );
419 else if( mpeg->APETag() )
420 ReadMetaFromAPE( mpeg->APETag(), p_meta );
422 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
424 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
425 ReadMetaFromXiph( ogg_flac->tag(), p_meta );
426 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
427 ReadMetaFromXiph( ogg_speex->tag(), p_meta );
428 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
429 ReadMetaFromXiph( ogg_vorbis->tag(), p_meta );
431 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
433 if( trueaudio->ID3v2Tag() )
434 ReadMetaFromId2v2( trueaudio->ID3v2Tag(), p_meta );
436 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
438 if( wavpack->APETag() )
439 ReadMetaFromAPE( wavpack->APETag(), p_meta );
442 // Try now to find a image
443 DetectImage( f, p_demux );
451 * Write meta informations to APE tags
452 * @param tag: the APE tag
453 * @param p_input: the input item
454 * @return VLC_SUCCESS if everything goes ok
456 static int WriteMetaToAPE( APE::Tag* tag, input_item_t* p_input )
464 * Write meta information to id3v2 tags
465 * @param tag: the id3v2 tag
466 * @param p_input: the input item
467 * @return VLC_SUCCESS if everything goes ok
469 static int WriteMetaToId2v2( ID3v2::Tag* tag, input_item_t* p_item )
472 #define WRITE( metaName, tagName ) \
473 psz_meta = input_item_Get##metaName( p_item ); \
476 ByteVector p_byte( tagName, 4 ); \
477 tag->removeFrames( p_byte ); \
478 ID3v2::TextIdentificationFrame* p_frame = \
479 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
480 p_frame->setText( psz_meta ); \
481 tag->addFrame( p_frame ); \
485 WRITE( Copyright, "TCOP" );
486 WRITE( EncodedBy, "TENC" );
487 WRITE( Language, "TLAN" );
488 WRITE( Publisher, "TPUB" );
497 * Write the meta informations to XiphComments
498 * @param tag: the Xiph Comment
499 * @param p_input: the input item
500 * @return VLC_SUCCESS if everything goes ok
502 static int WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
505 #define WRITE( metaName, keyName ) \
506 psz_meta = input_item_Get##metaName( p_item ); \
509 String key( keyName, String::UTF8 ); \
510 String value( psz_meta, String::UTF8 ); \
511 tag->addField( key, value, true ); \
515 WRITE( Copyright, "COPYRIGHT" );
524 * Set the tags to the file using TagLib
525 * @param p_this: the demux object
526 * @return VLC_SUCCESS if the operation success
529 static int WriteMeta( vlc_object_t *p_this )
531 playlist_t *p_playlist = (playlist_t *)p_this;
532 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
533 input_item_t *p_item = p_export->p_item;
537 msg_Err( p_this, "Can't save meta data of an empty input" );
541 FileRef f( p_export->psz_file );
542 if( f.isNull() || !f.tag() || f.file()->readOnly() )
544 msg_Err( p_this, "File %s can't be opened for tag writing\n",
545 p_export->psz_file );
549 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
551 Tag *p_tag = f.tag();
555 #define SET( a, b ) \
558 String* psz_tmp = new String( b, String::UTF8 ); \
559 p_tag->set##a( *psz_tmp ); \
563 // Saving all common fields
564 // If the title is empty, use the name
565 psz_meta = input_item_GetTitle( p_item );
566 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
567 SET( Title, psz_meta );
570 psz_meta = input_item_GetArtist( p_item );
571 SET( Artist, psz_meta );
574 psz_meta = input_item_GetAlbum( p_item );
575 SET( Album, psz_meta );
578 psz_meta = input_item_GetDescription( p_item );
579 SET( Comment, psz_meta );
582 psz_meta = input_item_GetGenre( p_item );
583 SET( Genre, psz_meta );
588 psz_meta = input_item_GetDate( p_item );
589 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
592 psz_meta = input_item_GetTrackNum( p_item );
593 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
597 // Try now to write special tags
598 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
600 if( flac->ID3v2Tag() )
601 WriteMetaToId2v2( flac->ID3v2Tag(), p_item );
602 else if( flac->xiphComment() )
603 WriteMetaToXiph( flac->xiphComment(), p_item );
605 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
608 WriteMetaToAPE( mpc->APETag(), p_item );
610 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
612 if( mpeg->ID3v2Tag() )
613 WriteMetaToId2v2( mpeg->ID3v2Tag(), p_item );
614 else if( mpeg->APETag() )
615 WriteMetaToAPE( mpeg->APETag(), p_item );
617 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
619 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
620 WriteMetaToXiph( ogg_flac->tag(), p_item );
621 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
622 WriteMetaToXiph( ogg_speex->tag(), p_item );
623 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
624 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
626 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
628 if( trueaudio->ID3v2Tag() )
629 WriteMetaToId2v2( trueaudio->ID3v2Tag(), p_item );
631 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
633 if( wavpack->APETag() )
634 WriteMetaToAPE( wavpack->APETag(), p_item );
637 // Save the meta data
645 static int DownloadArt( vlc_object_t *p_this )
647 /* We need to be passed the file name
648 * Fetch the thing from the file, save it to the cache folder