1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2009 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>
49 #include <tbytevector.h>
53 #include <xiphcomment.h>
59 #include <oggflacfile.h>
61 #ifdef TAGLIB_WITH_ASF
62 # include <aifffile.h>
66 #include <speexfile.h>
67 #include <trueaudiofile.h>
68 #include <vorbisfile.h>
69 #include <wavpackfile.h>
71 #include <attachedpictureframe.h>
72 #include <textidentificationframe.h>
73 #include <uniquefileidentifierframe.h>
77 static int ReadMeta ( vlc_object_t * );
78 static int WriteMeta ( vlc_object_t * );
81 set_capability( "meta reader", 1000 )
82 set_callbacks( ReadMeta, NULL )
84 set_capability( "meta writer", 50 )
85 set_callbacks( WriteMeta, NULL )
88 using namespace TagLib;
92 * Read meta informations from APE tags
93 * @param tag: the APE tag
94 * @param p_demux; the demux object
95 * @param p_demux_meta: the demuxer meta
96 * @param p_meta: the meta
98 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
101 #define SET( keyName, metaName ) \
102 item = tag->itemListMap()[keyName]; \
103 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
105 SET( "COPYRIGHT", Copyright );
106 SET( "LANGUAGE", Language );
107 SET( "PUBLISHER", Publisher );
115 * Read meta information from id3v2 tags
116 * @param tag: the id3v2 tag
117 * @param p_demux; the demux object
118 * @param p_demux_meta: the demuxer meta
119 * @param p_meta: the meta
121 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
123 // Get the unique file identifier
124 ID3v2::FrameList list = tag->frameListMap()["UFID"];
125 ID3v2::FrameList::Iterator iter;
126 for( iter = list.begin(); iter != list.end(); iter++ )
128 ID3v2::UniqueFileIdentifierFrame* p_ufid =
129 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
130 const char *owner = p_ufid->owner().toCString();
131 if (!strcmp( owner, "http://musicbrainz.org" ))
133 /* ID3v2 UFID contains up to 64 bytes binary data
134 * but in our case it will be a '\0'
135 * terminated string */
137 int max_size = __MIN( p_ufid->identifier().size(), 63);
138 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
139 psz_ufid[max_size] = '\0';
140 vlc_meta_SetTrackID( p_meta, psz_ufid );
145 list = tag->frameListMap()["TXXX"];
146 for( iter = list.begin(); iter != list.end(); iter++ )
148 ID3v2::UserTextIdentificationFrame* p_txxx =
149 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
150 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
151 p_txxx->fieldList().toString().toCString( true ) );
154 // Get some more informations
155 #define SET( tagName, metaName ) \
156 list = tag->frameListMap()[tagName]; \
157 if( !list.isEmpty() ) \
158 vlc_meta_Set##metaName( p_meta, \
159 (*list.begin())->toString().toCString( true ) );
161 SET( "TCOP", Copyright );
162 SET( "TENC", EncodedBy );
163 SET( "TLAN", Language );
164 SET( "TPUB", Publisher );
168 /* Preferred type of image
169 * The 21 types are defined in id3v2 standard:
170 * http://www.id3.org/id3v2.4.0-frames */
171 static const int pi_cover_score[] = {
173 5, /* 32x32 PNG image that should be used as the file icon */
174 4, /* File icon of a different size or format. */
175 20, /* Front cover image of the album. */
176 19, /* Back cover image of the album. */
177 13, /* Inside leaflet page of the album. */
178 18, /* Image from the album itself. */
179 17, /* Picture of the lead artist or soloist. */
180 16, /* Picture of the artist or performer. */
181 14, /* Picture of the conductor. */
182 15, /* Picture of the band or orchestra. */
183 9, /* Picture of the composer. */
184 8, /* Picture of the lyricist or text writer. */
185 7, /* Picture of the recording location or studio. */
186 10, /* Picture of the artists during recording. */
187 11, /* Picture of the artists during performance. */
188 6, /* Picture from a movie or video related to the track. */
189 1, /* Picture of a large, coloured fish. */
190 12, /* Illustration related to the track. */
191 3, /* Logo of the band or performer. */
192 2 /* Logo of the publisher (record company). */
196 // Try now to get embedded art
197 list = tag->frameListMap()[ "APIC" ];
201 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
202 for( iter = list.begin(); iter != list.end(); iter++ )
204 ID3v2::AttachedPictureFrame* p_apic =
205 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
206 input_attachment_t *p_attachment;
208 const char *psz_mime;
209 char *psz_name, *psz_description;
211 // Get the mime and description of the image.
212 // If the description is empty, take the type as a description
213 psz_mime = p_apic->mimeType().toCString( true );
214 if( p_apic->description().size() > 0 )
215 psz_description = strdup( p_apic->description().toCString( true ) );
218 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
219 psz_description = NULL;
222 if( !psz_description )
224 psz_name = psz_description;
226 /* some old iTunes version not only sets incorrectly the mime type
227 * or the description of the image,
228 * but also embeds incorrectly the image.
229 * Recent versions seem to behave correctly */
230 if( !strncmp( psz_mime, "PNG", 3 ) ||
231 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
233 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
234 free( psz_description );
238 const ByteVector picture = p_apic->picture();
239 const char *p_data = picture.data();
240 const unsigned i_data = picture.size();
242 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
243 psz_name, psz_mime, i_data );
245 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
246 psz_description, p_data, i_data );
248 TAB_APPEND_CAST( (input_attachment_t**),
249 p_demux_meta->i_attachments, p_demux_meta->attachments,
251 free( psz_description );
253 if( pi_cover_score[p_apic->type()] > i_score )
255 i_score = pi_cover_score[p_apic->type()];
257 if( asprintf( &psz_url, "attachment://%s",
258 p_attachment->psz_name ) == -1 )
260 vlc_meta_SetArtURL( p_meta, psz_url );
269 * Read the meta informations from XiphComments
270 * @param tag: the Xiph Comment
271 * @param p_demux; the demux object
272 * @param p_demux_meta: the demuxer meta
273 * @param p_meta: the meta
275 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
277 #define SET( keyName, metaName ) \
278 StringList list = tag->fieldListMap()[keyName]; \
279 if( !list.isEmpty() ) \
280 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
282 SET( "COPYRIGHT", Copyright );
285 // Try now to get embedded art
286 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
287 StringList art_list = tag->fieldListMap()[ "COVERART" ];
289 // We get only the first covert art
290 if( mime_list.size() > 1 || art_list.size() > 1 )
291 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
293 else if( mime_list.size() == 0 || art_list.size() == 0 )
296 input_attachment_t *p_attachment;
298 const char* psz_name = "cover";
299 const char* psz_mime = mime_list[0].toCString(true);
300 const char* psz_description = "cover";
303 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
305 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
306 psz_name, psz_mime, i_data );
308 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
309 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
310 psz_description, p_data, i_data );
313 TAB_APPEND_CAST( (input_attachment_t**),
314 p_demux_meta->i_attachments, p_demux_meta->attachments,
317 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
323 * Get the tags from the file using TagLib
324 * @param p_this: the demux object
325 * @return VLC_SUCCESS if the operation success
327 static int ReadMeta( vlc_object_t* p_this)
329 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
330 demux_t* p_demux = p_demux_meta->p_demux;
334 p_demux_meta->p_meta = NULL;
337 #if defined(WIN32) || defined (UNDER_CE)
338 wchar_t wpath[MAX_PATH + 1];
339 if( !MultiByteToWideChar( CP_UTF8, 0, p_demux->psz_path, -1, wpath, MAX_PATH) )
341 wpath[MAX_PATH] = L'\0';
342 f = FileRef( wpath );
344 const char* local_name = ToLocale( p_demux->psz_path );
347 f = FileRef( local_name );
348 LocaleFree( local_name );
353 if( !f.tag() || f.tag()->isEmpty() )
356 p_demux_meta->p_meta = p_meta = vlc_meta_New();
361 // Read the tags from the file
362 Tag* p_tag = f.tag();
364 #define SET( tag, meta ) \
365 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
366 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
367 #define SETINT( tag, meta ) \
371 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
372 vlc_meta_Set##meta( p_meta, psz_tmp ); \
376 SET( artist, Artist );
378 SET( comment, Description );
380 SETINT( year, Date );
381 SETINT( track, TrackNum );
387 // Try now to read special tags
388 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
390 if( flac->ID3v2Tag() )
391 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
392 else if( flac->xiphComment() )
393 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
395 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
398 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
400 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
402 if( mpeg->ID3v2Tag() )
403 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
404 else if( mpeg->APETag() )
405 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
407 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
409 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
410 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
411 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
412 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
413 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
414 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
416 #ifdef TAGLIB_WITH_ASF
417 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
419 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
420 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
421 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
422 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
425 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
427 if( trueaudio->ID3v2Tag() )
428 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
430 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
432 if( wavpack->APETag() )
433 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
442 * Write meta informations to APE tags
443 * @param tag: the APE tag
444 * @param p_item: the input item
446 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
449 #define WRITE( metaName, keyName ) \
450 psz_meta = input_item_Get##metaName( p_item ); \
453 String key( keyName, String::UTF8 ); \
454 String value( psz_meta, String::UTF8 ); \
455 tag->addValue( key, value, true ); \
459 WRITE( Copyright, "COPYRIGHT" );
460 WRITE( Language, "LANGUAGE" );
461 WRITE( Publisher, "PUBLISHER" );
469 * Write meta information to id3v2 tags
470 * @param tag: the id3v2 tag
471 * @param p_input: the input item
473 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
476 #define WRITE( metaName, tagName ) \
477 psz_meta = input_item_Get##metaName( p_item ); \
480 ByteVector p_byte( tagName, 4 ); \
481 tag->removeFrames( p_byte ); \
482 ID3v2::TextIdentificationFrame* p_frame = \
483 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
484 p_frame->setText( psz_meta ); \
485 tag->addFrame( p_frame ); \
489 WRITE( Copyright, "TCOP" );
490 WRITE( EncodedBy, "TENC" );
491 WRITE( Language, "TLAN" );
492 WRITE( Publisher, "TPUB" );
500 * Write the meta informations to XiphComments
501 * @param tag: the Xiph Comment
502 * @param p_input: the input item
504 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
507 #define WRITE( metaName, keyName ) \
508 psz_meta = input_item_Get##metaName( p_item ); \
511 String key( keyName, String::UTF8 ); \
512 String value( psz_meta, String::UTF8 ); \
513 tag->addField( key, value, true ); \
517 WRITE( Copyright, "COPYRIGHT" );
525 * Set the tags to the file using TagLib
526 * @param p_this: the demux object
527 * @return VLC_SUCCESS if the operation success
530 static int WriteMeta( vlc_object_t *p_this )
532 meta_export_t *p_export = (meta_export_t *)p_this;
533 input_item_t *p_item = p_export->p_item;
538 msg_Err( p_this, "Can't save meta data of an empty input" );
542 char *export_file = decode_URI_duplicate(p_export->psz_file);
543 if( export_file == NULL )
546 #if defined(WIN32) || defined (UNDER_CE)
547 wchar_t wpath[MAX_PATH + 1];
548 if( !MultiByteToWideChar( CP_UTF8, 0, export_file , -1, wpath, MAX_PATH) )
550 wpath[MAX_PATH] = L'\0';
551 f = FileRef( wpath );
553 const char* local_name = ToLocale( export_file );
559 f = FileRef( local_name );
560 LocaleFree( local_name );
563 if( f.isNull() || !f.tag() || f.file()->readOnly() )
565 msg_Err( p_this, "File %s can't be opened for tag writing",
571 msg_Dbg( p_this, "Writing metadata for %s", export_file );
574 Tag *p_tag = f.tag();
578 #define SET( a, b ) \
579 psz_meta = input_item_Get ## a( p_item ); \
582 String tmp( psz_meta, String::UTF8 ); \
583 p_tag->set##b( tmp ); \
587 // Saving all common fields
588 // If the title is empty, use the name
589 SET( TitleFbName, Title );
590 SET( Artist, Artist );
592 SET( Description, Comment );
597 psz_meta = input_item_GetDate( p_item );
598 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
601 psz_meta = input_item_GetTrackNum( p_item );
602 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
606 // Try now to write special tags
607 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
609 if( flac->ID3v2Tag() )
610 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
611 else if( flac->xiphComment() )
612 WriteMetaToXiph( flac->xiphComment(), p_item );
614 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
617 WriteMetaToAPE( mpc->APETag(), p_item );
619 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
621 if( mpeg->ID3v2Tag() )
622 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
623 else if( mpeg->APETag() )
624 WriteMetaToAPE( mpeg->APETag(), p_item );
626 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
628 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
629 WriteMetaToXiph( ogg_flac->tag(), p_item );
630 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
631 WriteMetaToXiph( ogg_speex->tag(), p_item );
632 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
633 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
635 #ifdef TAGLIB_WITH_ASF
636 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
638 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
639 WriteMetaToId3v2( riff_aiff->tag(), p_item );
640 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
641 WriteMetaToId3v2( riff_wav->tag(), p_item );
644 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
646 if( trueaudio->ID3v2Tag() )
647 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
649 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
651 if( wavpack->APETag() )
652 WriteMetaToAPE( wavpack->APETag(), p_item );
655 // Save the meta data