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;
333 char *psz_path = decode_URI_duplicate( p_demux->psz_path );
335 p_demux_meta->p_meta = NULL;
338 if( strncmp( p_demux->psz_access, "file", strlen("file") ) )
342 #if defined(WIN32) || defined (UNDER_CE)
343 wchar_t wpath[MAX_PATH + 1];
344 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
346 wpath[MAX_PATH] = L'\0';
347 f = FileRef( wpath );
349 const char* local_name = ToLocale( psz_path );
352 f = FileRef( local_name );
353 LocaleFree( local_name );
359 if( !f.tag() || f.tag()->isEmpty() )
362 p_demux_meta->p_meta = p_meta = vlc_meta_New();
367 // Read the tags from the file
368 Tag* p_tag = f.tag();
370 #define SET( tag, meta ) \
371 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
372 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
373 #define SETINT( tag, meta ) \
377 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
378 vlc_meta_Set##meta( p_meta, psz_tmp ); \
382 SET( artist, Artist );
384 SET( comment, Description );
386 SETINT( year, Date );
387 SETINT( track, TrackNum );
393 // Try now to read special tags
394 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
396 if( flac->ID3v2Tag() )
397 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
398 else if( flac->xiphComment() )
399 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
401 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
404 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
406 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
408 if( mpeg->ID3v2Tag() )
409 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
410 else if( mpeg->APETag() )
411 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
413 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
415 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
416 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
417 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
418 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
419 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
420 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
422 #ifdef TAGLIB_WITH_ASF
423 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
425 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
426 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
427 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
428 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
431 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
433 if( trueaudio->ID3v2Tag() )
434 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
436 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
438 if( wavpack->APETag() )
439 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
448 * Write meta informations to APE tags
449 * @param tag: the APE tag
450 * @param p_item: the input item
452 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
455 #define WRITE( metaName, keyName ) \
456 psz_meta = input_item_Get##metaName( p_item ); \
459 String key( keyName, String::UTF8 ); \
460 String value( psz_meta, String::UTF8 ); \
461 tag->addValue( key, value, true ); \
465 WRITE( Copyright, "COPYRIGHT" );
466 WRITE( Language, "LANGUAGE" );
467 WRITE( Publisher, "PUBLISHER" );
475 * Write meta information to id3v2 tags
476 * @param tag: the id3v2 tag
477 * @param p_input: the input item
479 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
482 #define WRITE( metaName, tagName ) \
483 psz_meta = input_item_Get##metaName( p_item ); \
486 ByteVector p_byte( tagName, 4 ); \
487 tag->removeFrames( p_byte ); \
488 ID3v2::TextIdentificationFrame* p_frame = \
489 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
490 p_frame->setText( psz_meta ); \
491 tag->addFrame( p_frame ); \
495 WRITE( Copyright, "TCOP" );
496 WRITE( EncodedBy, "TENC" );
497 WRITE( Language, "TLAN" );
498 WRITE( Publisher, "TPUB" );
506 * Write the meta informations to XiphComments
507 * @param tag: the Xiph Comment
508 * @param p_input: the input item
510 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
513 #define WRITE( metaName, keyName ) \
514 psz_meta = input_item_Get##metaName( p_item ); \
517 String key( keyName, String::UTF8 ); \
518 String value( psz_meta, String::UTF8 ); \
519 tag->addField( key, value, true ); \
523 WRITE( Copyright, "COPYRIGHT" );
531 * Set the tags to the file using TagLib
532 * @param p_this: the demux object
533 * @return VLC_SUCCESS if the operation success
536 static int WriteMeta( vlc_object_t *p_this )
538 meta_export_t *p_export = (meta_export_t *)p_this;
539 input_item_t *p_item = p_export->p_item;
544 msg_Err( p_this, "Can't save meta data of an empty input" );
548 #if defined(WIN32) || defined (UNDER_CE)
549 wchar_t wpath[MAX_PATH + 1];
550 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
552 wpath[MAX_PATH] = L'\0';
553 f = FileRef( wpath );
555 const char* local_name = ToLocale( p_export->psz_file );
558 f = FileRef( local_name );
559 LocaleFree( local_name );
562 if( f.isNull() || !f.tag() || f.file()->readOnly() )
564 msg_Err( p_this, "File %s can't be opened for tag writing",
565 p_export->psz_file );
569 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
571 Tag *p_tag = f.tag();
575 #define SET( a, b ) \
576 psz_meta = input_item_Get ## a( p_item ); \
579 String tmp( psz_meta, String::UTF8 ); \
580 p_tag->set##b( tmp ); \
584 // Saving all common fields
585 // If the title is empty, use the name
586 SET( TitleFbName, Title );
587 SET( Artist, Artist );
589 SET( Description, Comment );
594 psz_meta = input_item_GetDate( p_item );
595 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
598 psz_meta = input_item_GetTrackNum( p_item );
599 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
603 // Try now to write special tags
604 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
606 if( flac->ID3v2Tag() )
607 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
608 else if( flac->xiphComment() )
609 WriteMetaToXiph( flac->xiphComment(), p_item );
611 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
614 WriteMetaToAPE( mpc->APETag(), p_item );
616 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
618 if( mpeg->ID3v2Tag() )
619 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
620 else if( mpeg->APETag() )
621 WriteMetaToAPE( mpeg->APETag(), p_item );
623 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
625 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
626 WriteMetaToXiph( ogg_flac->tag(), p_item );
627 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
628 WriteMetaToXiph( ogg_speex->tag(), p_item );
629 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
630 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
632 #ifdef TAGLIB_WITH_ASF
633 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
635 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
636 WriteMetaToId3v2( riff_aiff->tag(), p_item );
637 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
638 WriteMetaToId3v2( riff_wav->tag(), p_item );
641 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
643 if( trueaudio->ID3v2Tag() )
644 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
646 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
648 if( wavpack->APETag() )
649 WriteMetaToAPE( wavpack->APETag(), p_item );
652 // Save the meta data