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;
89 * Read meta informations from APE tags
90 * @param tag: the APE tag
91 * @param p_demux; the demux object
92 * @param p_demux_meta: the demuxer meta
93 * @param p_meta: the meta
95 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
98 #define SET( keyName, metaName ) \
99 item = tag->itemListMap()[keyName]; \
100 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
102 SET( "COPYRIGHT", Copyright );
103 SET( "LANGUAGE", Language );
104 SET( "PUBLISHER", Publisher );
112 * Read meta information from id3v2 tags
113 * @param tag: the id3v2 tag
114 * @param p_demux; the demux object
115 * @param p_demux_meta: the demuxer meta
116 * @param p_meta: the meta
118 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
120 // Get the unique file identifier
121 ID3v2::FrameList list = tag->frameListMap()["UFID"];
122 ID3v2::FrameList::Iterator iter;
123 for( iter = list.begin(); iter != list.end(); iter++ )
125 ID3v2::UniqueFileIdentifierFrame* p_ufid =
126 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
127 const char *owner = p_ufid->owner().toCString();
128 if (!strcmp( owner, "http://musicbrainz.org" ))
130 /* ID3v2 UFID contains up to 64 bytes binary data
131 * but in our case it will be a '\0'
132 * terminated string */
134 int max_size = __MIN( p_ufid->identifier().size(), 63);
135 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
136 psz_ufid[max_size] = '\0';
137 vlc_meta_SetTrackID( p_meta, psz_ufid );
142 list = tag->frameListMap()["TXXX"];
143 for( iter = list.begin(); iter != list.end(); iter++ )
145 ID3v2::UserTextIdentificationFrame* p_txxx =
146 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
147 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
148 p_txxx->fieldList().toString().toCString( true ) );
151 // Get some more informations
152 #define SET( tagName, metaName ) \
153 list = tag->frameListMap()[tagName]; \
154 if( !list.isEmpty() ) \
155 vlc_meta_Set##metaName( p_meta, \
156 (*list.begin())->toString().toCString( true ) );
158 SET( "TCOP", Copyright );
159 SET( "TENC", EncodedBy );
160 SET( "TLAN", Language );
161 SET( "TPUB", Publisher );
165 /* Preferred type of image
166 * The 21 types are defined in id3v2 standard:
167 * http://www.id3.org/id3v2.4.0-frames */
168 static const int pi_cover_score[] = {
170 5, /* 32x32 PNG image that should be used as the file icon */
171 4, /* File icon of a different size or format. */
172 20, /* Front cover image of the album. */
173 19, /* Back cover image of the album. */
174 13, /* Inside leaflet page of the album. */
175 18, /* Image from the album itself. */
176 17, /* Picture of the lead artist or soloist. */
177 16, /* Picture of the artist or performer. */
178 14, /* Picture of the conductor. */
179 15, /* Picture of the band or orchestra. */
180 9, /* Picture of the composer. */
181 8, /* Picture of the lyricist or text writer. */
182 7, /* Picture of the recording location or studio. */
183 10, /* Picture of the artists during recording. */
184 11, /* Picture of the artists during performance. */
185 6, /* Picture from a movie or video related to the track. */
186 1, /* Picture of a large, coloured fish. */
187 12, /* Illustration related to the track. */
188 3, /* Logo of the band or performer. */
189 2 /* Logo of the publisher (record company). */
193 // Try now to get embedded art
194 list = tag->frameListMap()[ "APIC" ];
197 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
198 for( ID3v2::FrameList::Iterator iter = list.begin();
199 iter != list.end(); iter++ )
201 ID3v2::AttachedPictureFrame* p_apic =
202 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
203 input_attachment_t *p_attachment;
205 const char *psz_name, *psz_mime, *psz_description;
206 const char *p_data; int i_data;
208 psz_mime = p_apic->mimeType().toCString( true );
209 psz_description = psz_name = p_apic->description().toCString( true );
211 /* some old iTunes version not only sets incorrectly the mime type
212 * or the description of the image,
213 * but also embeds incorrectly the image.
214 * Recent versions seem to behave correctly */
215 if( !strncmp( psz_mime, "PNG", 3 ) ||
216 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
218 msg_Warn( p_demux, "Invalid picture embedded by broken iTunes version" );
222 p_data = p_apic->picture().data();
223 i_data = p_apic->picture().size();
225 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
226 psz_name, psz_mime, i_data );
228 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
229 psz_description, p_data, i_data );
230 TAB_APPEND_CAST( (input_attachment_t**),
231 p_demux_meta->i_attachments, p_demux_meta->attachments,
234 if( pi_cover_score[p_apic->type()] > i_score )
236 i_score = pi_cover_score[p_apic->type()];
238 if( asprintf( &psz_url, "attachment://%s",
239 p_attachment->psz_name ) == -1 )
241 vlc_meta_SetArtURL( p_meta, psz_url );
250 * Read the meta informations from XiphComments
251 * @param tag: the Xiph Comment
252 * @param p_demux; the demux object
253 * @param p_demux_meta: the demuxer meta
254 * @param p_meta: the meta
256 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
258 #define SET( keyName, metaName ) \
259 StringList list = tag->fieldListMap()[keyName]; \
260 if( !list.isEmpty() ) \
261 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
263 SET( "COPYRIGHT", Copyright );
266 // Try now to get embedded art
267 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
268 StringList art_list = tag->fieldListMap()[ "COVERART" ];
270 // We get only the first covert art
271 if( mime_list.size() > 1 || art_list.size() > 1 )
272 msg_Warn( p_demux, "Found %i embedded arts, so using only the first one",
274 else if( mime_list.size() == 0 || art_list.size() == 0 )
277 input_attachment_t *p_attachment;
279 const char* psz_name = "cover";
280 const char* psz_mime = mime_list[0].toCString(true);
281 const char* psz_description = "cover";
284 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
286 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
287 psz_name, psz_mime, i_data );
289 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
290 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
291 psz_description, p_data, i_data );
294 TAB_APPEND_CAST( (input_attachment_t**),
295 p_demux_meta->i_attachments, p_demux_meta->attachments,
298 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
304 * Get the tags from the file using TagLib
305 * @param p_this: the demux object
306 * @return VLC_SUCCESS if the operation success
308 static int ReadMeta( vlc_object_t* p_this)
310 demux_t* p_demux = (demux_t*)p_this;
311 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
315 p_demux_meta->p_meta = NULL;
316 const char* local_name = ToLocale( p_demux->psz_path );
319 f = FileRef( local_name );
320 LocaleFree( local_name );
324 if( !f.tag() || f.tag()->isEmpty() )
327 p_demux_meta->p_meta = p_meta = vlc_meta_New();
332 // Read the tags from the file
333 Tag* p_tag = f.tag();
335 #define SET( tag, meta ) \
336 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
337 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
338 #define SETINT( tag, meta ) \
342 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
343 vlc_meta_Set##meta( p_meta, psz_tmp ); \
347 SET( artist, Artist );
349 SET( comment, Description );
351 SETINT( year, Date );
352 SETINT( track, Tracknum );
358 // Try now to read special tags
359 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
361 if( flac->ID3v2Tag() )
362 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
363 else if( flac->xiphComment() )
364 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
366 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
369 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
371 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
373 if( mpeg->ID3v2Tag() )
374 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
375 else if( mpeg->APETag() )
376 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
378 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
380 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
381 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
382 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
383 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
384 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
385 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
387 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
389 if( trueaudio->ID3v2Tag() )
390 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
392 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
394 if( wavpack->APETag() )
395 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
404 * Write meta informations to APE tags
405 * @param tag: the APE tag
406 * @param p_item: the input item
408 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
411 #define WRITE( metaName, keyName ) \
412 psz_meta = input_item_Get##metaName( p_item ); \
415 String key( keyName, String::UTF8 ); \
416 String value( psz_meta, String::UTF8 ); \
417 tag->addValue( key, value, true ); \
421 WRITE( Copyright, "COPYRIGHT" );
422 WRITE( Language, "LANGUAGE" );
423 WRITE( Publisher, "PUBLISHER" );
431 * Write meta information to id3v2 tags
432 * @param tag: the id3v2 tag
433 * @param p_input: the input item
435 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
438 #define WRITE( metaName, tagName ) \
439 psz_meta = input_item_Get##metaName( p_item ); \
442 ByteVector p_byte( tagName, 4 ); \
443 tag->removeFrames( p_byte ); \
444 ID3v2::TextIdentificationFrame* p_frame = \
445 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
446 p_frame->setText( psz_meta ); \
447 tag->addFrame( p_frame ); \
451 WRITE( Copyright, "TCOP" );
452 WRITE( EncodedBy, "TENC" );
453 WRITE( Language, "TLAN" );
454 WRITE( Publisher, "TPUB" );
462 * Write the meta informations to XiphComments
463 * @param tag: the Xiph Comment
464 * @param p_input: the input item
466 static void WriteMetaToXiph( Ogg::XiphComment* 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->addField( key, value, true ); \
479 WRITE( Copyright, "COPYRIGHT" );
487 * Set the tags to the file using TagLib
488 * @param p_this: the demux object
489 * @return VLC_SUCCESS if the operation success
492 static int WriteMeta( vlc_object_t *p_this )
494 playlist_t *p_playlist = (playlist_t *)p_this;
495 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
496 input_item_t *p_item = p_export->p_item;
500 msg_Err( p_this, "Can't save meta data of an empty input" );
504 FileRef f( p_export->psz_file );
505 if( f.isNull() || !f.tag() || f.file()->readOnly() )
507 msg_Err( p_this, "File %s can't be opened for tag writing\n",
508 p_export->psz_file );
512 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
514 Tag *p_tag = f.tag();
518 #define SET( a, b ) \
521 String* psz_tmp = new String( b, String::UTF8 ); \
522 p_tag->set##a( *psz_tmp ); \
526 // Saving all common fields
527 // If the title is empty, use the name
528 psz_meta = input_item_GetTitle( p_item );
529 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
530 SET( Title, psz_meta );
533 psz_meta = input_item_GetArtist( p_item );
534 SET( Artist, psz_meta );
537 psz_meta = input_item_GetAlbum( p_item );
538 SET( Album, psz_meta );
541 psz_meta = input_item_GetDescription( p_item );
542 SET( Comment, psz_meta );
545 psz_meta = input_item_GetGenre( p_item );
546 SET( Genre, psz_meta );
551 psz_meta = input_item_GetDate( p_item );
552 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
555 psz_meta = input_item_GetTrackNum( p_item );
556 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
560 // Try now to write special tags
561 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
563 if( flac->ID3v2Tag() )
564 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
565 else if( flac->xiphComment() )
566 WriteMetaToXiph( flac->xiphComment(), p_item );
568 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
571 WriteMetaToAPE( mpc->APETag(), p_item );
573 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
575 if( mpeg->ID3v2Tag() )
576 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
577 else if( mpeg->APETag() )
578 WriteMetaToAPE( mpeg->APETag(), p_item );
580 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
582 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
583 WriteMetaToXiph( ogg_flac->tag(), p_item );
584 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
585 WriteMetaToXiph( ogg_speex->tag(), p_item );
586 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
587 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
589 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
591 if( trueaudio->ID3v2Tag() )
592 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
594 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
596 if( wavpack->APETag() )
597 WriteMetaToAPE( wavpack->APETag(), p_item );
600 // Save the meta data
608 static int DownloadArt( vlc_object_t *p_this )
610 /* We need to be passed the file name
611 * Fetch the thing from the file, save it to the cache folder