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 */
49 #define VERSION_INT(a, b, c) ((a)<<16 | (b)<<8 | (c))
50 #define TAGLIB_VERSION VERSION_INT(TAGLIB_MAJOR_VERSION, \
51 TAGLIB_MINOR_VERSION, \
56 #include <tbytevector.h>
60 #include <xiphcomment.h>
62 #if TAGLIB_VERSION >= VERSION_INT(1,7,0)
63 # define TAGLIB_HAVE_APEFILE_H
70 #include <oggflacfile.h>
72 #if TAGLIB_VERSION >= VERSION_INT(1,6,0)
73 # define TAGLIB_HAVE_RIFF_WAV_H
74 # include <aifffile.h>
78 #ifdef TAGLIB_WITH_MP4
82 #include <speexfile.h>
83 #include <trueaudiofile.h>
84 #include <vorbisfile.h>
85 #include <wavpackfile.h>
87 #include <attachedpictureframe.h>
88 #include <textidentificationframe.h>
89 #include <uniquefileidentifierframe.h>
91 // taglib is not thread safe
92 static vlc_mutex_t taglib_lock = VLC_STATIC_MUTEX;
95 static int ReadMeta ( vlc_object_t * );
96 static int WriteMeta ( vlc_object_t * );
99 set_capability( "meta reader", 1000 )
100 set_callbacks( ReadMeta, NULL )
102 set_capability( "meta writer", 50 )
103 set_callbacks( WriteMeta, NULL )
106 using namespace TagLib;
110 * Read meta information from APE tags
111 * @param tag: the APE tag
112 * @param p_demux_meta: the demuxer meta
113 * @param p_meta: the meta
115 static void ReadMetaFromAPE( APE::Tag* tag, demux_meta_t*, vlc_meta_t* p_meta )
118 #define SET( keyName, metaName ) \
119 item = tag->itemListMap()[keyName]; \
120 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
122 SET( "COPYRIGHT", Copyright );
123 SET( "LANGUAGE", Language );
124 SET( "PUBLISHER", Publisher );
132 * Read meta information from id3v2 tags
133 * @param tag: the id3v2 tag
134 * @param p_demux_meta: the demuxer meta
135 * @param p_meta: the meta
137 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
139 // Get the unique file identifier
140 ID3v2::FrameList list = tag->frameListMap()["UFID"];
141 ID3v2::FrameList::Iterator iter;
142 for( iter = list.begin(); iter != list.end(); iter++ )
144 ID3v2::UniqueFileIdentifierFrame* p_ufid =
145 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
148 const char *owner = p_ufid->owner().toCString();
149 if (!strcmp( owner, "http://musicbrainz.org" ))
151 /* ID3v2 UFID contains up to 64 bytes binary data
152 * but in our case it will be a '\0'
153 * terminated string */
155 int max_size = __MIN( p_ufid->identifier().size(), 63);
156 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
157 psz_ufid[max_size] = '\0';
158 vlc_meta_SetTrackID( p_meta, psz_ufid );
163 list = tag->frameListMap()["TXXX"];
164 for( iter = list.begin(); iter != list.end(); iter++ )
166 ID3v2::UserTextIdentificationFrame* p_txxx =
167 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
170 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
171 p_txxx->fieldList().back().toCString( true ) );
174 // Get some more information
175 #define SET( tagName, metaName ) \
176 list = tag->frameListMap()[tagName]; \
177 if( !list.isEmpty() ) \
178 vlc_meta_Set##metaName( p_meta, \
179 (*list.begin())->toString().toCString( true ) );
181 SET( "TCOP", Copyright );
182 SET( "TENC", EncodedBy );
183 SET( "TLAN", Language );
184 SET( "TPUB", Publisher );
188 /* Preferred type of image
189 * The 21 types are defined in id3v2 standard:
190 * http://www.id3.org/id3v2.4.0-frames */
191 static const int pi_cover_score[] = {
193 5, /* 32x32 PNG image that should be used as the file icon */
194 4, /* File icon of a different size or format. */
195 20, /* Front cover image of the album. */
196 19, /* Back cover image of the album. */
197 13, /* Inside leaflet page of the album. */
198 18, /* Image from the album itself. */
199 17, /* Picture of the lead artist or soloist. */
200 16, /* Picture of the artist or performer. */
201 14, /* Picture of the conductor. */
202 15, /* Picture of the band or orchestra. */
203 9, /* Picture of the composer. */
204 8, /* Picture of the lyricist or text writer. */
205 7, /* Picture of the recording location or studio. */
206 10, /* Picture of the artists during recording. */
207 11, /* Picture of the artists during performance. */
208 6, /* Picture from a movie or video related to the track. */
209 1, /* Picture of a large, coloured fish. */
210 12, /* Illustration related to the track. */
211 3, /* Logo of the band or performer. */
212 2 /* Logo of the publisher (record company). */
214 #define PI_COVER_SCORE_SIZE (sizeof (pi_cover_score) / sizeof (pi_cover_score[0]))
217 // Try now to get embedded art
218 list = tag->frameListMap()[ "APIC" ];
222 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
223 for( iter = list.begin(); iter != list.end(); iter++ )
225 ID3v2::AttachedPictureFrame* p_apic =
226 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
229 input_attachment_t *p_attachment;
231 const char *psz_mime;
232 char *psz_name, *psz_description;
234 // Get the mime and description of the image.
235 // If the description is empty, take the type as a description
236 psz_mime = p_apic->mimeType().toCString( true );
237 if( p_apic->description().size() > 0 )
238 psz_description = strdup( p_apic->description().toCString( true ) );
241 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
242 psz_description = NULL;
245 if( !psz_description )
247 psz_name = psz_description;
249 /* some old iTunes version not only sets incorrectly the mime type
250 * or the description of the image,
251 * but also embeds incorrectly the image.
252 * Recent versions seem to behave correctly */
253 if( !strncmp( psz_mime, "PNG", 3 ) ||
254 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
256 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
257 free( psz_description );
261 const ByteVector picture = p_apic->picture();
262 const char *p_data = picture.data();
263 const unsigned i_data = picture.size();
265 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
266 psz_name, psz_mime, i_data );
268 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
269 psz_description, p_data, i_data );
271 TAB_APPEND_CAST( (input_attachment_t**),
272 p_demux_meta->i_attachments, p_demux_meta->attachments,
274 free( psz_description );
276 unsigned i_pic_type = p_apic->type();
277 if( i_pic_type >= PI_COVER_SCORE_SIZE )
278 i_pic_type = 0; // Defaults to "Other"
280 if( pi_cover_score[i_pic_type] > i_score )
282 i_score = pi_cover_score[i_pic_type];
284 if( asprintf( &psz_url, "attachment://%s",
285 p_attachment->psz_name ) == -1 )
287 vlc_meta_SetArtURL( p_meta, psz_url );
296 * Read the meta information from XiphComments
297 * @param tag: the Xiph Comment
298 * @param p_demux_meta: the demuxer meta
299 * @param p_meta: the meta
301 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
304 #define SET( keyName, metaName ) \
305 list = tag->fieldListMap()[keyName]; \
306 if( !list.isEmpty() ) \
307 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
309 SET( "COPYRIGHT", Copyright );
312 // Try now to get embedded art
313 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
314 StringList art_list = tag->fieldListMap()[ "COVERART" ];
316 // We get only the first covert art
317 if( mime_list.size() > 1 || art_list.size() > 1 )
318 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
320 else if( mime_list.size() == 0 || art_list.size() == 0 )
323 input_attachment_t *p_attachment;
325 const char* psz_name = "cover";
326 const char* psz_mime = mime_list[0].toCString(true);
327 const char* psz_description = "cover";
330 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
332 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
333 psz_name, psz_mime, i_data );
335 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
336 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
337 psz_description, p_data, i_data );
340 TAB_APPEND_CAST( (input_attachment_t**),
341 p_demux_meta->i_attachments, p_demux_meta->attachments,
344 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
347 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
348 static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
350 if( tag->itemListMap().contains("covr") )
352 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
353 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
355 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
356 psz_format, list[0].data().size() );
358 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
359 input_attachment_t *p_attachment =
360 vlc_input_attachment_New( "cover", psz_format, "cover",
361 list[0].data().data(), list[0].data().size() );
362 TAB_APPEND_CAST( (input_attachment_t**),
363 p_demux_meta->i_attachments, p_demux_meta->attachments,
365 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
371 * Get the tags from the file using TagLib
372 * @param p_this: the demux object
373 * @return VLC_SUCCESS if the operation success
375 static int ReadMeta( vlc_object_t* p_this)
377 vlc_mutex_locker locker (&taglib_lock);
378 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
379 demux_t* p_demux = p_demux_meta->p_demux;
383 p_demux_meta->p_meta = NULL;
384 if( strcmp( p_demux->psz_access, "file" ) )
387 char *psz_path = strdup( p_demux->psz_file );
391 #if defined(WIN32) || defined (UNDER_CE)
392 wchar_t wpath[MAX_PATH + 1];
393 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
398 wpath[MAX_PATH] = L'\0';
399 f = FileRef( wpath );
401 const char* local_name = ToLocale( psz_path );
407 f = FileRef( local_name );
408 LocaleFree( local_name );
414 if( !f.tag() || f.tag()->isEmpty() )
417 p_demux_meta->p_meta = p_meta = vlc_meta_New();
422 // Read the tags from the file
423 Tag* p_tag = f.tag();
425 #define SET( tag, meta ) \
426 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
427 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
428 #define SETINT( tag, meta ) \
432 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
433 vlc_meta_Set##meta( p_meta, psz_tmp ); \
437 SET( artist, Artist );
439 SET( comment, Description );
441 SETINT( year, Date );
442 SETINT( track, TrackNum );
448 // Try now to read special tags
449 #ifdef TAGLIB_HAVE_APEFILE_H
450 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
453 ReadMetaFromAPE( ape->APETag(), p_demux_meta, p_meta );
457 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
459 if( flac->ID3v2Tag() )
460 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux_meta, p_meta );
461 else if( flac->xiphComment() )
462 ReadMetaFromXiph( flac->xiphComment(), p_demux_meta, p_meta );
464 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
465 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
468 ReadMetaFromMP4( mp4->tag(), p_demux_meta, p_meta );
471 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
474 ReadMetaFromAPE( mpc->APETag(), p_demux_meta, p_meta );
476 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
478 if( mpeg->ID3v2Tag() )
479 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux_meta, p_meta );
480 else if( mpeg->APETag() )
481 ReadMetaFromAPE( mpeg->APETag(), p_demux_meta, p_meta );
483 else if( dynamic_cast<Ogg::File*>(f.file()) )
485 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
486 ReadMetaFromXiph( ogg_flac->tag(), p_demux_meta, p_meta );
487 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
488 ReadMetaFromXiph( ogg_speex->tag(), p_demux_meta, p_meta );
489 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
490 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux_meta, p_meta );
492 #ifdef TAGLIB_HAVE_RIFF_WAV_H
493 else if( dynamic_cast<RIFF::File*>(f.file()) )
495 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
496 ReadMetaFromId3v2( riff_aiff->tag(), p_demux_meta, p_meta );
497 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
498 ReadMetaFromId3v2( riff_wav->tag(), p_demux_meta, p_meta );
501 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
503 if( trueaudio->ID3v2Tag() )
504 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux_meta, p_meta );
506 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
508 if( wavpack->APETag() )
509 ReadMetaFromAPE( wavpack->APETag(), p_demux_meta, p_meta );
518 * Write meta information to APE tags
519 * @param tag: the APE tag
520 * @param p_item: the input item
522 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
525 #define WRITE( metaName, keyName ) \
526 psz_meta = input_item_Get##metaName( p_item ); \
529 String key( keyName, String::UTF8 ); \
530 String value( psz_meta, String::UTF8 ); \
531 tag->addValue( key, value, true ); \
535 WRITE( Copyright, "COPYRIGHT" );
536 WRITE( Language, "LANGUAGE" );
537 WRITE( Publisher, "PUBLISHER" );
545 * Write meta information to id3v2 tags
546 * @param tag: the id3v2 tag
547 * @param p_input: the input item
549 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
552 #define WRITE( metaName, tagName ) \
553 psz_meta = input_item_Get##metaName( p_item ); \
556 ByteVector p_byte( tagName, 4 ); \
557 tag->removeFrames( p_byte ); \
558 ID3v2::TextIdentificationFrame* p_frame = \
559 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
560 p_frame->setText( psz_meta ); \
561 tag->addFrame( p_frame ); \
565 WRITE( Copyright, "TCOP" );
566 WRITE( EncodedBy, "TENC" );
567 WRITE( Language, "TLAN" );
568 WRITE( Publisher, "TPUB" );
576 * Write the meta information to XiphComments
577 * @param tag: the Xiph Comment
578 * @param p_input: the input item
580 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
583 #define WRITE( metaName, keyName ) \
584 psz_meta = input_item_Get##metaName( p_item ); \
587 String key( keyName, String::UTF8 ); \
588 String value( psz_meta, String::UTF8 ); \
589 tag->addField( key, value, true ); \
593 WRITE( Copyright, "COPYRIGHT" );
601 * Set the tags to the file using TagLib
602 * @param p_this: the demux object
603 * @return VLC_SUCCESS if the operation success
606 static int WriteMeta( vlc_object_t *p_this )
608 vlc_mutex_locker locker (&taglib_lock);
609 meta_export_t *p_export = (meta_export_t *)p_this;
610 input_item_t *p_item = p_export->p_item;
615 msg_Err( p_this, "Can't save meta data of an empty input" );
619 #if defined(WIN32) || defined (UNDER_CE)
620 wchar_t wpath[MAX_PATH + 1];
621 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
623 wpath[MAX_PATH] = L'\0';
624 f = FileRef( wpath );
626 const char* local_name = ToLocale( p_export->psz_file );
629 f = FileRef( local_name );
630 LocaleFree( local_name );
633 if( f.isNull() || !f.tag() || f.file()->readOnly() )
635 msg_Err( p_this, "File %s can't be opened for tag writing",
636 p_export->psz_file );
640 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
642 Tag *p_tag = f.tag();
646 #define SET( a, b ) \
647 psz_meta = input_item_Get ## a( p_item ); \
650 String tmp( psz_meta, String::UTF8 ); \
651 p_tag->set##b( tmp ); \
655 // Saving all common fields
656 // If the title is empty, use the name
657 SET( TitleFbName, Title );
658 SET( Artist, Artist );
660 SET( Description, Comment );
665 psz_meta = input_item_GetDate( p_item );
666 if( !EMPTY_STR(psz_meta) ) p_tag->setYear( atoi( psz_meta ) );
669 psz_meta = input_item_GetTrackNum( p_item );
670 if( !EMPTY_STR(psz_meta) ) p_tag->setTrack( atoi( psz_meta ) );
674 // Try now to write special tags
675 #ifdef TAGLIB_HAVE_APEFILE_H
676 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
679 WriteMetaToAPE( ape->APETag(), p_item );
683 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
685 if( flac->ID3v2Tag() )
686 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
687 else if( flac->xiphComment() )
688 WriteMetaToXiph( flac->xiphComment(), p_item );
690 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
693 WriteMetaToAPE( mpc->APETag(), p_item );
695 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
697 if( mpeg->ID3v2Tag() )
698 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
699 else if( mpeg->APETag() )
700 WriteMetaToAPE( mpeg->APETag(), p_item );
702 else if( dynamic_cast<Ogg::File*>(f.file()) )
704 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
705 WriteMetaToXiph( ogg_flac->tag(), p_item );
706 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
707 WriteMetaToXiph( ogg_speex->tag(), p_item );
708 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
709 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
711 #ifdef TAGLIB_HAVE_RIFF_WAV_H
712 else if( dynamic_cast<RIFF::File*>(f.file()) )
714 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
715 WriteMetaToId3v2( riff_aiff->tag(), p_item );
716 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
717 WriteMetaToId3v2( riff_wav->tag(), p_item );
720 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
722 if( trueaudio->ID3v2Tag() )
723 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
725 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
727 if( wavpack->APETag() )
728 WriteMetaToAPE( wavpack->APETag(), p_item );
731 // Save the meta data