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>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_playlist.h>
33 #include <vlc_demux.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
47 #include <xiphcomment.h>
54 #include <oggflacfile.h>
55 #include <speexfile.h>
56 #include <vorbisfile.h>
57 #include <trueaudiofile.h>
58 #include <wavpackfile.h>
61 #include <textidentificationframe.h>
62 #include <tbytevector.h>
63 #include <attachedpictureframe.h>
64 //#include <oggflacfile.h> /* ogg flac files aren't auto-casted by TagLib */
65 #include <flacproperties.h>
66 #include <vorbisfile.h>
67 #include <vorbisproperties.h>
68 #include <uniquefileidentifierframe.h>
69 #include <textidentificationframe.h>
70 //#include <relativevolumeframe.h> /* parse the tags without TagLib helpers? */
72 static int ReadMeta ( vlc_object_t * );
73 static int DownloadArt ( vlc_object_t * );
74 static int WriteMeta ( vlc_object_t * );
77 set_capability( "meta reader", 1000 )
78 set_callbacks( ReadMeta, NULL )
80 set_capability( "art downloader", 50 )
81 set_callbacks( DownloadArt, NULL )
83 set_capability( "meta writer", 50 )
84 set_callbacks( WriteMeta, NULL )
87 using namespace TagLib;
90 /* Try detecting embedded art */
91 static void DetectImage( FileRef f, demux_t *p_demux )
93 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
94 vlc_meta_t *p_meta = p_demux_meta->p_meta;
97 /* Preferred type of image
98 * The 21 types are defined in id3v2 standard:
99 * http://www.id3.org/id3v2.4.0-frames */
100 static const int pi_cover_score[] = {
102 5, /* 32x32 PNG image that should be used as the file icon */
103 4, /* File icon of a different size or format. */
104 20, /* Front cover image of the album. */
105 19, /* Back cover image of the album. */
106 13, /* Inside leaflet page of the album. */
107 18, /* Image from the album itself. */
108 17, /* Picture of the lead artist or soloist. */
109 16, /* Picture of the artist or performer. */
110 14, /* Picture of the conductor. */
111 15, /* Picture of the band or orchestra. */
112 9, /* Picture of the composer. */
113 8, /* Picture of the lyricist or text writer. */
114 7, /* Picture of the recording location or studio. */
115 10, /* Picture of the artists during recording. */
116 11, /* Picture of the artists during performance. */
117 6, /* Picture from a movie or video related to the track. */
118 1, /* Picture of a large, coloured fish. */
119 12, /* Illustration related to the track. */
120 3, /* Logo of the band or performer. */
121 2 /* Logo of the publisher (record company). */
124 if( MPEG::File *mpeg = dynamic_cast<MPEG::File *>(f.file() ) )
126 ID3v2::Tag *p_tag = mpeg->ID3v2Tag();
129 ID3v2::FrameList list = p_tag->frameListMap()[ "APIC" ];
132 ID3v2::AttachedPictureFrame *p_apic;
134 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
135 for( ID3v2::FrameList::Iterator iter = list.begin();
136 iter != list.end(); iter++ )
138 p_apic = dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
139 input_attachment_t *p_attachment;
141 const char *psz_name, *psz_mime, *psz_description;
142 ByteVector p_data_taglib; const char *p_data; int i_data;
144 psz_mime = p_apic->mimeType().toCString(true);
145 psz_description = psz_name = p_apic->description().toCString(true);
147 /* some old iTunes version not only sets incorrectly the mime type
148 * or the description of the image,
149 * but also embeds incorrectly the image.
150 * Recent versions seem to behave correctly */
151 if( !strncmp( psz_mime, "PNG", 3 ) ||
152 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
155 "%s: Invalid picture embedded by broken iTunes version, "
156 "you really shouldn't use this crappy software.",
157 (const char *)f.file()->name() );
161 p_data_taglib = p_apic->picture();
162 p_data = p_data_taglib.data();
163 i_data = p_data_taglib.size();
165 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
166 psz_name, psz_mime, i_data );
168 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
169 psz_description, p_data, i_data );
170 TAB_APPEND_CAST( (input_attachment_t**),
171 p_demux_meta->i_attachments, p_demux_meta->attachments,
174 if( pi_cover_score[p_apic->type()] > i_score )
176 i_score = pi_cover_score[p_apic->type()];
178 if( asprintf( &psz_url, "attachment://%s",
179 p_attachment->psz_name ) == -1 )
181 vlc_meta_SetArtURL( p_meta, psz_url );
187 if( Ogg::Vorbis::File *oggv = dynamic_cast<Ogg::Vorbis::File *>(f.file() ) )
189 Ogg::XiphComment *p_tag = oggv->tag();
193 StringList mime_list = p_tag->fieldListMap()[ "COVERARTMIME" ];
194 StringList art_list = p_tag->fieldListMap()[ "COVERART" ];
196 /* we support only one cover in ogg/vorbis */
197 if( mime_list.size() != 1 || art_list.size() != 1 )
200 input_attachment_t *p_attachment;
202 const char *psz_name, *psz_mime, *psz_description;
207 psz_mime = mime_list[0].toCString(true);
208 psz_description = "cover";
210 i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
212 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
213 psz_name, psz_mime, i_data );
215 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
216 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
217 psz_description, p_data, i_data );
220 TAB_APPEND_CAST( (input_attachment_t**),
221 p_demux_meta->i_attachments, p_demux_meta->attachments,
224 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
228 //flac embedded images are extracted in the flac demuxer
229 else if( FLAC::File *flac =
230 dynamic_cast<FLAC::File *>(f.file() ) )
232 p_tag = flac->ID3v2Tag();
235 ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
238 vlc_meta_SetArtURL( p_meta, "APIC" );
242 /* TagLib doesn't support MP4 file yet */
243 else if( MP4::File *mp4 =
244 dynamic_cast<MP4::File *>( f.file() ) )
247 dynamic_cast<MP4::Tag *>( mp4->tag() );
248 if( mp4tag && mp4tag->cover().size() )
249 vlc_meta_SetArtURL( p_meta, "MP4C" );
257 * Read meta informations from APE tags
258 * @param tag: the APE tag
259 * @param p_meta: the meta
260 * @return VLC_SUCCESS if everything goes ok
262 static int ReadMetaFromAPE( APE::Tag* tag, vlc_meta_t* p_meta )
270 * Read meta information from id3v2 tags
271 * @param tag: the id3v2 tag
272 * @param p_meta: the meta
273 * @return VLC_SUCCESS if everything goes ok
275 static int ReadMetaFromId2v2( ID3v2::Tag* tag, vlc_meta_t* p_meta )
277 // Get the unique file identifier
278 ID3v2::FrameList list = tag->frameListMap()["UFID"];
279 ID3v2::FrameList::Iterator iter;
280 for( iter = list.begin(); iter != list.end(); iter++ )
282 ID3v2::UniqueFileIdentifierFrame* p_ufid =
283 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
284 const char *owner = p_ufid->owner().toCString();
285 if (!strcmp( owner, "http://musicbrainz.org" ))
287 /* ID3v2 UFID contains up to 64 bytes binary data
288 * but in our case it will be a '\0'
289 * terminated string */
292 int max_size = p_ufid->identifier().size() < 63 ?
293 p_ufid->identifier().size() : 63;
294 while( j < max_size )
295 psz_ufid[j] = p_ufid->identifier()[j++];
297 vlc_meta_SetTrackID( p_meta, psz_ufid );
302 list = tag->frameListMap()["TXXX"];
303 for( iter = list.begin(); iter != list.end(); iter++ )
305 ID3v2::UserTextIdentificationFrame* p_txxx =
306 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
307 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
308 p_txxx->fieldList().toString().toCString( true ) );
311 // Get some more informations
312 #define SET( tagName, metaName ) \
313 list = tag->frameListMap()[tagName]; \
314 if( !list.isEmpty() ) \
315 vlc_meta_Set##metaName( p_meta, \
316 (*list.begin())->toString().toCString( true ) );
318 SET( "TCOP", Copyright );
319 SET( "TENC", EncodedBy );
320 SET( "TLAN", Language );
321 SET( "TPUB", Publisher );
330 * Read the meta informations from XiphComments
331 * @param tag: the Xiph Comment
332 * @param p_meta: the meta
333 * @return VLC_SUCCESS if everything goes ok
335 static int ReadMetaFromXiph( Ogg::XiphComment* tag, vlc_meta_t* p_meta )
343 * Get the tags from the file using TagLib
344 * @param p_this: the demux object
345 * @return VLC_SUCCESS if the operation success
347 static int ReadMeta( vlc_object_t* p_this)
349 demux_t* p_demux = (demux_t*)p_this;
350 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
354 p_demux_meta->p_meta = NULL;
355 const char* local_name = ToLocale( p_demux->psz_path );
358 f = FileRef( local_name );
359 LocaleFree( local_name );
363 if( !f.tag() || f.tag()->isEmpty() )
366 p_demux_meta->p_meta = p_meta = vlc_meta_New();
371 // Read the tags from the file
372 Tag* p_tag = f.tag();
374 #define SET( meta, tag ) \
375 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
376 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
377 #define SETINT( meta, tag ) \
381 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
382 vlc_meta_Set##meta( p_meta, psz_tmp ); \
386 SET( Artist, artist );
388 SET( Description, comment );
390 SETINT( Date, year );
391 SETINT( Tracknum, track );
397 // Try now to read special tags
398 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
400 if( flac->ID3v2Tag() )
401 ReadMetaFromId2v2( flac->ID3v2Tag(), p_meta );
402 else if( flac->xiphComment() )
403 ReadMetaFromXiph( flac->xiphComment(), p_meta );
405 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
408 ReadMetaFromAPE( mpc->APETag(), p_meta );
410 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
412 if( mpeg->ID3v2Tag() )
413 ReadMetaFromId2v2( mpeg->ID3v2Tag(), p_meta );
414 else if( mpeg->APETag() )
415 ReadMetaFromAPE( mpeg->APETag(), p_meta );
417 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
419 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
420 ReadMetaFromXiph( ogg_flac->tag(), p_meta );
421 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
422 ReadMetaFromXiph( ogg_speex->tag(), p_meta );
423 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
424 ReadMetaFromXiph( ogg_vorbis->tag(), p_meta );
426 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
428 if( trueaudio->ID3v2Tag() )
429 ReadMetaFromId2v2( trueaudio->ID3v2Tag(), p_meta );
431 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
433 if( wavpack->APETag() )
434 ReadMetaFromAPE( wavpack->APETag(), p_meta );
437 // Try now to find a image
438 DetectImage( f, p_demux );
446 * Write meta informations to APE tags
447 * @param tag: the APE tag
448 * @param p_input: the input item
449 * @return VLC_SUCCESS if everything goes ok
451 static int WriteMetaToAPE( APE::Tag* tag, input_item_t* p_input )
459 * Write meta information to id3v2 tags
460 * @param tag: the id3v2 tag
461 * @param p_input: the input item
462 * @return VLC_SUCCESS if everything goes ok
464 static int WriteMetaToId2v2( ID3v2::Tag* tag, input_item_t* p_item )
467 #define WRITE( metaName, tagName ) \
468 psz_meta = input_item_Get##metaName( p_item ); \
471 ByteVector p_byte( tagName, 4 ); \
472 tag->removeFrames( p_byte ); \
473 ID3v2::TextIdentificationFrame* p_frame = \
474 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
475 p_frame->setText( psz_meta ); \
476 tag->addFrame( p_frame ); \
480 WRITE( Copyright, "TCOP" );
481 WRITE( EncodedBy, "TENC" );
482 WRITE( Language, "TLAN" );
483 WRITE( Publisher, "TPUB" );
492 * Write the meta informations to XiphComments
493 * @param tag: the Xiph Comment
494 * @param p_input: the input item
495 * @return VLC_SUCCESS if everything goes ok
497 static int WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_input )
505 * Set the tags to the file using TagLib
506 * @param p_this: the demux object
507 * @return VLC_SUCCESS if the operation success
510 static int WriteMeta( vlc_object_t *p_this )
512 playlist_t *p_playlist = (playlist_t *)p_this;
513 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
514 input_item_t *p_item = p_export->p_item;
518 msg_Err( p_this, "Can't save meta data of an empty input" );
522 FileRef f( p_export->psz_file );
523 if( f.isNull() || !f.tag() || f.file()->readOnly() )
525 msg_Err( p_this, "File %s can't be opened for tag writing\n",
526 p_export->psz_file );
530 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
532 Tag *p_tag = f.tag();
536 #define SET( a, b ) \
539 String* psz_tmp = new String( b, String::UTF8 ); \
540 p_tag->set##a( *psz_tmp ); \
544 // Saving all common fields
545 // If the title is empty, use the name
546 psz_meta = input_item_GetTitle( p_item );
547 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
548 SET( Title, psz_meta );
551 psz_meta = input_item_GetArtist( p_item );
552 SET( Artist, psz_meta );
555 psz_meta = input_item_GetAlbum( p_item );
556 SET( Album, psz_meta );
559 psz_meta = input_item_GetDescription( p_item );
560 SET( Comment, psz_meta );
563 psz_meta = input_item_GetGenre( p_item );
564 SET( Genre, psz_meta );
569 psz_meta = input_item_GetDate( p_item );
570 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
573 psz_meta = input_item_GetTrackNum( p_item );
574 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
578 // Try now to write special tags
579 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
581 if( flac->ID3v2Tag() )
582 WriteMetaToId2v2( flac->ID3v2Tag(), p_item );
583 else if( flac->xiphComment() )
584 WriteMetaToXiph( flac->xiphComment(), p_item );
586 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
589 WriteMetaToAPE( mpc->APETag(), p_item );
591 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
593 if( mpeg->ID3v2Tag() )
594 WriteMetaToId2v2( mpeg->ID3v2Tag(), p_item );
595 else if( mpeg->APETag() )
596 WriteMetaToAPE( mpeg->APETag(), p_item );
598 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
600 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
601 WriteMetaToXiph( ogg_flac->tag(), p_item );
602 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
603 WriteMetaToXiph( ogg_speex->tag(), p_item );
604 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
605 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
607 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
609 if( trueaudio->ID3v2Tag() )
610 WriteMetaToId2v2( trueaudio->ID3v2Tag(), p_item );
612 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
614 if( wavpack->APETag() )
615 WriteMetaToAPE( wavpack->APETag(), p_item );
618 // Save the meta data
626 static int DownloadArt( vlc_object_t *p_this )
628 /* We need to be passed the file name
629 * Fetch the thing from the file, save it to the cache folder