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>
33 #include <vlc_demux.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
37 #include <vlc_input_item.h>
38 #include <vlc_input.h> /* for attachment_new */
50 #include <tbytevector.h>
54 #include <xiphcomment.h>
56 #ifdef HAVE_TAGLIB_APEFILE_H
63 #include <oggflacfile.h>
65 #ifdef TAGLIB_WITH_ASF
66 # include <aifffile.h>
70 #ifdef TAGLIB_WITH_MP4
74 #include <speexfile.h>
75 #include <trueaudiofile.h>
76 #include <vorbisfile.h>
77 #include <wavpackfile.h>
79 #include <attachedpictureframe.h>
80 #include <textidentificationframe.h>
81 #include <uniquefileidentifierframe.h>
83 // taglib is not thread safe
84 static vlc_mutex_t taglib_lock = VLC_STATIC_MUTEX;
87 static int ReadMeta ( vlc_object_t * );
88 static int WriteMeta ( vlc_object_t * );
91 set_capability( "meta reader", 1000 )
92 set_callbacks( ReadMeta, NULL )
94 set_capability( "meta writer", 50 )
95 set_callbacks( WriteMeta, NULL )
98 using namespace TagLib;
102 * Read meta information from APE tags
103 * @param tag: the APE tag
104 * @param p_demux_meta: the demuxer meta
105 * @param p_meta: the meta
107 static void ReadMetaFromAPE( APE::Tag* tag, demux_meta_t*, vlc_meta_t* p_meta )
110 #define SET( keyName, metaName ) \
111 item = tag->itemListMap()[keyName]; \
112 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
114 SET( "COPYRIGHT", Copyright );
115 SET( "LANGUAGE", Language );
116 SET( "PUBLISHER", Publisher );
124 * Read meta information from id3v2 tags
125 * @param tag: the id3v2 tag
126 * @param p_demux_meta: the demuxer meta
127 * @param p_meta: the meta
129 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
131 // Get the unique file identifier
132 ID3v2::FrameList list = tag->frameListMap()["UFID"];
133 ID3v2::FrameList::Iterator iter;
134 for( iter = list.begin(); iter != list.end(); iter++ )
136 ID3v2::UniqueFileIdentifierFrame* p_ufid =
137 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
140 const char *owner = p_ufid->owner().toCString();
141 if (!strcmp( owner, "http://musicbrainz.org" ))
143 /* ID3v2 UFID contains up to 64 bytes binary data
144 * but in our case it will be a '\0'
145 * terminated string */
147 int max_size = __MIN( p_ufid->identifier().size(), 63);
148 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
149 psz_ufid[max_size] = '\0';
150 vlc_meta_SetTrackID( p_meta, psz_ufid );
155 list = tag->frameListMap()["TXXX"];
156 for( iter = list.begin(); iter != list.end(); iter++ )
158 ID3v2::UserTextIdentificationFrame* p_txxx =
159 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
162 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
163 p_txxx->fieldList().back().toCString( true ) );
166 // Get some more information
167 #define SET( tagName, metaName ) \
168 list = tag->frameListMap()[tagName]; \
169 if( !list.isEmpty() ) \
170 vlc_meta_Set##metaName( p_meta, \
171 (*list.begin())->toString().toCString( true ) );
173 SET( "TCOP", Copyright );
174 SET( "TENC", EncodedBy );
175 SET( "TLAN", Language );
176 SET( "TPUB", Publisher );
180 /* Preferred type of image
181 * The 21 types are defined in id3v2 standard:
182 * http://www.id3.org/id3v2.4.0-frames */
183 static const int pi_cover_score[] = {
185 5, /* 32x32 PNG image that should be used as the file icon */
186 4, /* File icon of a different size or format. */
187 20, /* Front cover image of the album. */
188 19, /* Back cover image of the album. */
189 13, /* Inside leaflet page of the album. */
190 18, /* Image from the album itself. */
191 17, /* Picture of the lead artist or soloist. */
192 16, /* Picture of the artist or performer. */
193 14, /* Picture of the conductor. */
194 15, /* Picture of the band or orchestra. */
195 9, /* Picture of the composer. */
196 8, /* Picture of the lyricist or text writer. */
197 7, /* Picture of the recording location or studio. */
198 10, /* Picture of the artists during recording. */
199 11, /* Picture of the artists during performance. */
200 6, /* Picture from a movie or video related to the track. */
201 1, /* Picture of a large, coloured fish. */
202 12, /* Illustration related to the track. */
203 3, /* Logo of the band or performer. */
204 2 /* Logo of the publisher (record company). */
206 #define PI_COVER_SCORE_SIZE (sizeof (pi_cover_score) / sizeof (pi_cover_score[0]))
209 // Try now to get embedded art
210 list = tag->frameListMap()[ "APIC" ];
214 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
215 for( iter = list.begin(); iter != list.end(); iter++ )
217 ID3v2::AttachedPictureFrame* p_apic =
218 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
221 input_attachment_t *p_attachment;
223 const char *psz_mime;
224 char *psz_name, *psz_description;
226 // Get the mime and description of the image.
227 // If the description is empty, take the type as a description
228 psz_mime = p_apic->mimeType().toCString( true );
229 if( p_apic->description().size() > 0 )
230 psz_description = strdup( p_apic->description().toCString( true ) );
233 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
234 psz_description = NULL;
237 if( !psz_description )
239 psz_name = psz_description;
241 /* some old iTunes version not only sets incorrectly the mime type
242 * or the description of the image,
243 * but also embeds incorrectly the image.
244 * Recent versions seem to behave correctly */
245 if( !strncmp( psz_mime, "PNG", 3 ) ||
246 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
248 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
249 free( psz_description );
253 const ByteVector picture = p_apic->picture();
254 const char *p_data = picture.data();
255 const unsigned i_data = picture.size();
257 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
258 psz_name, psz_mime, i_data );
260 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
261 psz_description, p_data, i_data );
263 TAB_APPEND_CAST( (input_attachment_t**),
264 p_demux_meta->i_attachments, p_demux_meta->attachments,
266 free( psz_description );
268 unsigned i_pic_type = p_apic->type();
269 if( i_pic_type >= PI_COVER_SCORE_SIZE )
270 i_pic_type = 0; // Defaults to "Other"
272 if( pi_cover_score[i_pic_type] > i_score )
274 i_score = pi_cover_score[i_pic_type];
276 if( asprintf( &psz_url, "attachment://%s",
277 p_attachment->psz_name ) == -1 )
279 vlc_meta_SetArtURL( p_meta, psz_url );
288 * Read the meta information from XiphComments
289 * @param tag: the Xiph Comment
290 * @param p_demux_meta: the demuxer meta
291 * @param p_meta: the meta
293 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
296 #define SET( keyName, metaName ) \
297 list = tag->fieldListMap()[keyName]; \
298 if( !list.isEmpty() ) \
299 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
301 SET( "COPYRIGHT", Copyright );
304 // Try now to get embedded art
305 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
306 StringList art_list = tag->fieldListMap()[ "COVERART" ];
308 // We get only the first covert art
309 if( mime_list.size() > 1 || art_list.size() > 1 )
310 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
312 else if( mime_list.size() == 0 || art_list.size() == 0 )
315 input_attachment_t *p_attachment;
317 const char* psz_name = "cover";
318 const char* psz_mime = mime_list[0].toCString(true);
319 const char* psz_description = "cover";
322 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
324 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
325 psz_name, psz_mime, i_data );
327 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
328 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
329 psz_description, p_data, i_data );
332 TAB_APPEND_CAST( (input_attachment_t**),
333 p_demux_meta->i_attachments, p_demux_meta->attachments,
336 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
339 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
340 static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
342 if( tag->itemListMap().contains("covr") )
344 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
345 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
347 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
348 psz_format, list[0].data().size() );
350 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
351 input_attachment_t *p_attachment =
352 vlc_input_attachment_New( "cover", psz_format, "cover",
353 list[0].data().data(), list[0].data().size() );
354 TAB_APPEND_CAST( (input_attachment_t**),
355 p_demux_meta->i_attachments, p_demux_meta->attachments,
357 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
363 * Get the tags from the file using TagLib
364 * @param p_this: the demux object
365 * @return VLC_SUCCESS if the operation success
367 static int ReadMeta( vlc_object_t* p_this)
369 vlc_mutex_locker locker (&taglib_lock);
370 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
371 demux_t* p_demux = p_demux_meta->p_demux;
375 p_demux_meta->p_meta = NULL;
376 if( strcmp( p_demux->psz_access, "file" ) )
379 char *psz_path = strdup( p_demux->psz_file );
383 #if defined(WIN32) || defined (UNDER_CE)
384 wchar_t wpath[MAX_PATH + 1];
385 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
390 wpath[MAX_PATH] = L'\0';
391 f = FileRef( wpath );
393 const char* local_name = ToLocale( psz_path );
399 f = FileRef( local_name );
400 LocaleFree( local_name );
406 if( !f.tag() || f.tag()->isEmpty() )
409 p_demux_meta->p_meta = p_meta = vlc_meta_New();
414 // Read the tags from the file
415 Tag* p_tag = f.tag();
417 #define SET( tag, meta ) \
418 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
419 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
420 #define SETINT( tag, meta ) \
424 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
425 vlc_meta_Set##meta( p_meta, psz_tmp ); \
429 SET( artist, Artist );
431 SET( comment, Description );
433 SETINT( year, Date );
434 SETINT( track, TrackNum );
440 // Try now to read special tags
441 #ifdef HAVE_TAGLIB_APEFILE_H
442 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
445 ReadMetaFromAPE( ape->APETag(), p_demux_meta, p_meta );
449 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
451 if( flac->ID3v2Tag() )
452 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux_meta, p_meta );
453 else if( flac->xiphComment() )
454 ReadMetaFromXiph( flac->xiphComment(), p_demux_meta, p_meta );
456 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
457 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
460 ReadMetaFromMP4( mp4->tag(), p_demux_meta, p_meta );
463 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
466 ReadMetaFromAPE( mpc->APETag(), p_demux_meta, p_meta );
468 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
470 if( mpeg->ID3v2Tag() )
471 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux_meta, p_meta );
472 else if( mpeg->APETag() )
473 ReadMetaFromAPE( mpeg->APETag(), p_demux_meta, p_meta );
475 else if( dynamic_cast<Ogg::File*>(f.file()) )
477 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
478 ReadMetaFromXiph( ogg_flac->tag(), p_demux_meta, p_meta );
479 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
480 ReadMetaFromXiph( ogg_speex->tag(), p_demux_meta, p_meta );
481 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
482 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux_meta, p_meta );
484 #ifdef TAGLIB_WITH_ASF
485 else if( dynamic_cast<RIFF::File*>(f.file()) )
487 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
488 ReadMetaFromId3v2( riff_aiff->tag(), p_demux_meta, p_meta );
489 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
490 ReadMetaFromId3v2( riff_wav->tag(), p_demux_meta, p_meta );
493 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
495 if( trueaudio->ID3v2Tag() )
496 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux_meta, p_meta );
498 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
500 if( wavpack->APETag() )
501 ReadMetaFromAPE( wavpack->APETag(), p_demux_meta, p_meta );
510 * Write meta information to APE tags
511 * @param tag: the APE tag
512 * @param p_item: the input item
514 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
517 #define WRITE( metaName, keyName ) \
518 psz_meta = input_item_Get##metaName( p_item ); \
521 String key( keyName, String::UTF8 ); \
522 String value( psz_meta, String::UTF8 ); \
523 tag->addValue( key, value, true ); \
527 WRITE( Copyright, "COPYRIGHT" );
528 WRITE( Language, "LANGUAGE" );
529 WRITE( Publisher, "PUBLISHER" );
537 * Write meta information to id3v2 tags
538 * @param tag: the id3v2 tag
539 * @param p_input: the input item
541 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
544 #define WRITE( metaName, tagName ) \
545 psz_meta = input_item_Get##metaName( p_item ); \
548 ByteVector p_byte( tagName, 4 ); \
549 tag->removeFrames( p_byte ); \
550 ID3v2::TextIdentificationFrame* p_frame = \
551 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
552 p_frame->setText( psz_meta ); \
553 tag->addFrame( p_frame ); \
557 WRITE( Copyright, "TCOP" );
558 WRITE( EncodedBy, "TENC" );
559 WRITE( Language, "TLAN" );
560 WRITE( Publisher, "TPUB" );
568 * Write the meta information to XiphComments
569 * @param tag: the Xiph Comment
570 * @param p_input: the input item
572 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
575 #define WRITE( metaName, keyName ) \
576 psz_meta = input_item_Get##metaName( p_item ); \
579 String key( keyName, String::UTF8 ); \
580 String value( psz_meta, String::UTF8 ); \
581 tag->addField( key, value, true ); \
585 WRITE( Copyright, "COPYRIGHT" );
593 * Set the tags to the file using TagLib
594 * @param p_this: the demux object
595 * @return VLC_SUCCESS if the operation success
598 static int WriteMeta( vlc_object_t *p_this )
600 vlc_mutex_locker locker (&taglib_lock);
601 meta_export_t *p_export = (meta_export_t *)p_this;
602 input_item_t *p_item = p_export->p_item;
607 msg_Err( p_this, "Can't save meta data of an empty input" );
611 #if defined(WIN32) || defined (UNDER_CE)
612 wchar_t wpath[MAX_PATH + 1];
613 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
615 wpath[MAX_PATH] = L'\0';
616 f = FileRef( wpath );
618 const char* local_name = ToLocale( p_export->psz_file );
621 f = FileRef( local_name );
622 LocaleFree( local_name );
625 if( f.isNull() || !f.tag() || f.file()->readOnly() )
627 msg_Err( p_this, "File %s can't be opened for tag writing",
628 p_export->psz_file );
632 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
634 Tag *p_tag = f.tag();
638 #define SET( a, b ) \
639 psz_meta = input_item_Get ## a( p_item ); \
642 String tmp( psz_meta, String::UTF8 ); \
643 p_tag->set##b( tmp ); \
647 // Saving all common fields
648 // If the title is empty, use the name
649 SET( TitleFbName, Title );
650 SET( Artist, Artist );
652 SET( Description, Comment );
657 psz_meta = input_item_GetDate( p_item );
658 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
661 psz_meta = input_item_GetTrackNum( p_item );
662 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
666 // Try now to write special tags
667 #ifdef HAVE_TAGLIB_APEFILE_H
668 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
671 WriteMetaToAPE( ape->APETag(), p_item );
675 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
677 if( flac->ID3v2Tag() )
678 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
679 else if( flac->xiphComment() )
680 WriteMetaToXiph( flac->xiphComment(), p_item );
682 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
685 WriteMetaToAPE( mpc->APETag(), p_item );
687 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
689 if( mpeg->ID3v2Tag() )
690 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
691 else if( mpeg->APETag() )
692 WriteMetaToAPE( mpeg->APETag(), p_item );
694 else if( dynamic_cast<Ogg::File*>(f.file()) )
696 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
697 WriteMetaToXiph( ogg_flac->tag(), p_item );
698 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
699 WriteMetaToXiph( ogg_speex->tag(), p_item );
700 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
701 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
703 #ifdef TAGLIB_WITH_ASF
704 else if( dynamic_cast<RIFF::File*>(f.file()) )
706 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
707 WriteMetaToId3v2( riff_aiff->tag(), p_item );
708 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
709 WriteMetaToId3v2( riff_wav->tag(), p_item );
712 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
714 if( trueaudio->ID3v2Tag() )
715 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
717 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
719 if( wavpack->APETag() )
720 WriteMetaToAPE( wavpack->APETag(), p_item );
723 // Save the meta data