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>
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 WriteMeta ( vlc_object_t * );
74 set_capability( "meta reader", 1000 )
75 set_callbacks( ReadMeta, NULL )
77 set_capability( "meta writer", 50 )
78 set_callbacks( WriteMeta, NULL )
81 using namespace TagLib;
85 * Read meta informations from APE tags
86 * @param tag: the APE tag
87 * @param p_demux; the demux object
88 * @param p_demux_meta: the demuxer meta
89 * @param p_meta: the meta
91 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
94 #define SET( keyName, metaName ) \
95 item = tag->itemListMap()[keyName]; \
96 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
98 SET( "COPYRIGHT", Copyright );
99 SET( "LANGUAGE", Language );
100 SET( "PUBLISHER", Publisher );
108 * Read meta information from id3v2 tags
109 * @param tag: the id3v2 tag
110 * @param p_demux; the demux object
111 * @param p_demux_meta: the demuxer meta
112 * @param p_meta: the meta
114 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
116 // Get the unique file identifier
117 ID3v2::FrameList list = tag->frameListMap()["UFID"];
118 ID3v2::FrameList::Iterator iter;
119 for( iter = list.begin(); iter != list.end(); iter++ )
121 ID3v2::UniqueFileIdentifierFrame* p_ufid =
122 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
123 const char *owner = p_ufid->owner().toCString();
124 if (!strcmp( owner, "http://musicbrainz.org" ))
126 /* ID3v2 UFID contains up to 64 bytes binary data
127 * but in our case it will be a '\0'
128 * terminated string */
130 int max_size = __MIN( p_ufid->identifier().size(), 63);
131 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
132 psz_ufid[max_size] = '\0';
133 vlc_meta_SetTrackID( p_meta, psz_ufid );
138 list = tag->frameListMap()["TXXX"];
139 for( iter = list.begin(); iter != list.end(); iter++ )
141 ID3v2::UserTextIdentificationFrame* p_txxx =
142 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
143 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
144 p_txxx->fieldList().toString().toCString( true ) );
147 // Get some more informations
148 #define SET( tagName, metaName ) \
149 list = tag->frameListMap()[tagName]; \
150 if( !list.isEmpty() ) \
151 vlc_meta_Set##metaName( p_meta, \
152 (*list.begin())->toString().toCString( true ) );
154 SET( "TCOP", Copyright );
155 SET( "TENC", EncodedBy );
156 SET( "TLAN", Language );
157 SET( "TPUB", Publisher );
161 /* Preferred type of image
162 * The 21 types are defined in id3v2 standard:
163 * http://www.id3.org/id3v2.4.0-frames */
164 static const int pi_cover_score[] = {
166 5, /* 32x32 PNG image that should be used as the file icon */
167 4, /* File icon of a different size or format. */
168 20, /* Front cover image of the album. */
169 19, /* Back cover image of the album. */
170 13, /* Inside leaflet page of the album. */
171 18, /* Image from the album itself. */
172 17, /* Picture of the lead artist or soloist. */
173 16, /* Picture of the artist or performer. */
174 14, /* Picture of the conductor. */
175 15, /* Picture of the band or orchestra. */
176 9, /* Picture of the composer. */
177 8, /* Picture of the lyricist or text writer. */
178 7, /* Picture of the recording location or studio. */
179 10, /* Picture of the artists during recording. */
180 11, /* Picture of the artists during performance. */
181 6, /* Picture from a movie or video related to the track. */
182 1, /* Picture of a large, coloured fish. */
183 12, /* Illustration related to the track. */
184 3, /* Logo of the band or performer. */
185 2 /* Logo of the publisher (record company). */
189 // Try now to get embedded art
190 list = tag->frameListMap()[ "APIC" ];
194 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
195 for( iter = list.begin(); iter != list.end(); iter++ )
197 ID3v2::AttachedPictureFrame* p_apic =
198 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
199 input_attachment_t *p_attachment;
201 const char *psz_mime;
202 char *psz_name, *psz_description;
204 // Get the mime and description of the image.
205 // If the description is empty, take the type as a description
206 psz_mime = p_apic->mimeType().toCString( true );
207 if( p_apic->description().size() > 0 )
208 psz_description = strdup( p_apic->description().toCString( true ) );
211 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
212 psz_description = NULL;
215 if( !psz_description )
217 psz_name = psz_description;
219 /* some old iTunes version not only sets incorrectly the mime type
220 * or the description of the image,
221 * but also embeds incorrectly the image.
222 * Recent versions seem to behave correctly */
223 if( !strncmp( psz_mime, "PNG", 3 ) ||
224 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
226 msg_Warn( p_demux, "Invalid picture embedded by broken iTunes version" );
227 free( psz_description );
231 const ByteVector picture = p_apic->picture();
232 const char *p_data = picture.data();
233 const unsigned i_data = picture.size();
235 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %u bytes",
236 psz_name, psz_mime, i_data );
238 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
239 psz_description, p_data, i_data );
241 TAB_APPEND_CAST( (input_attachment_t**),
242 p_demux_meta->i_attachments, p_demux_meta->attachments,
244 free( psz_description );
246 if( pi_cover_score[p_apic->type()] > i_score )
248 i_score = pi_cover_score[p_apic->type()];
250 if( asprintf( &psz_url, "attachment://%s",
251 p_attachment->psz_name ) == -1 )
253 vlc_meta_SetArtURL( p_meta, psz_url );
262 * Read the meta informations from XiphComments
263 * @param tag: the Xiph Comment
264 * @param p_demux; the demux object
265 * @param p_demux_meta: the demuxer meta
266 * @param p_meta: the meta
268 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
270 #define SET( keyName, metaName ) \
271 StringList list = tag->fieldListMap()[keyName]; \
272 if( !list.isEmpty() ) \
273 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
275 SET( "COPYRIGHT", Copyright );
278 // Try now to get embedded art
279 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
280 StringList art_list = tag->fieldListMap()[ "COVERART" ];
282 // We get only the first covert art
283 if( mime_list.size() > 1 || art_list.size() > 1 )
284 msg_Warn( p_demux, "Found %i embedded arts, so using only the first one",
286 else if( mime_list.size() == 0 || art_list.size() == 0 )
289 input_attachment_t *p_attachment;
291 const char* psz_name = "cover";
292 const char* psz_mime = mime_list[0].toCString(true);
293 const char* psz_description = "cover";
296 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
298 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
299 psz_name, psz_mime, i_data );
301 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
302 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
303 psz_description, p_data, i_data );
306 TAB_APPEND_CAST( (input_attachment_t**),
307 p_demux_meta->i_attachments, p_demux_meta->attachments,
310 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
316 * Get the tags from the file using TagLib
317 * @param p_this: the demux object
318 * @return VLC_SUCCESS if the operation success
320 static int ReadMeta( vlc_object_t* p_this)
322 demux_t* p_demux = (demux_t*)p_this;
323 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
327 p_demux_meta->p_meta = NULL;
330 #if defined(WIN32) || defined (UNDER_CE)
331 if(GetVersion() < 0x80000000)
333 wchar_t wpath[MAX_PATH + 1];
334 if( !MultiByteToWideChar( CP_UTF8, 0, p_demux->psz_path, -1, wpath, MAX_PATH) )
336 wpath[MAX_PATH] = L'\0';
337 f = FileRef( wpath );
342 const char* local_name = ToLocale( p_demux->psz_path );
345 f = FileRef( local_name );
346 LocaleFree( local_name );
351 if( !f.tag() || f.tag()->isEmpty() )
354 p_demux_meta->p_meta = p_meta = vlc_meta_New();
359 // Read the tags from the file
360 Tag* p_tag = f.tag();
362 #define SET( tag, meta ) \
363 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
364 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
365 #define SETINT( tag, meta ) \
369 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
370 vlc_meta_Set##meta( p_meta, psz_tmp ); \
374 SET( artist, Artist );
376 SET( comment, Description );
378 SETINT( year, Date );
379 SETINT( track, TrackNum );
385 // Try now to read special tags
386 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
388 if( flac->ID3v2Tag() )
389 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
390 else if( flac->xiphComment() )
391 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
393 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
396 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
398 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
400 if( mpeg->ID3v2Tag() )
401 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
402 else if( mpeg->APETag() )
403 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
405 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
407 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
408 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
409 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
410 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
411 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
412 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
414 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
416 if( trueaudio->ID3v2Tag() )
417 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
419 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
421 if( wavpack->APETag() )
422 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
431 * Write meta informations to APE tags
432 * @param tag: the APE tag
433 * @param p_item: the input item
435 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
438 #define WRITE( metaName, keyName ) \
439 psz_meta = input_item_Get##metaName( p_item ); \
442 String key( keyName, String::UTF8 ); \
443 String value( psz_meta, String::UTF8 ); \
444 tag->addValue( key, value, true ); \
448 WRITE( Copyright, "COPYRIGHT" );
449 WRITE( Language, "LANGUAGE" );
450 WRITE( Publisher, "PUBLISHER" );
458 * Write meta information to id3v2 tags
459 * @param tag: the id3v2 tag
460 * @param p_input: the input item
462 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
465 #define WRITE( metaName, tagName ) \
466 psz_meta = input_item_Get##metaName( p_item ); \
469 ByteVector p_byte( tagName, 4 ); \
470 tag->removeFrames( p_byte ); \
471 ID3v2::TextIdentificationFrame* p_frame = \
472 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
473 p_frame->setText( psz_meta ); \
474 tag->addFrame( p_frame ); \
478 WRITE( Copyright, "TCOP" );
479 WRITE( EncodedBy, "TENC" );
480 WRITE( Language, "TLAN" );
481 WRITE( Publisher, "TPUB" );
489 * Write the meta informations to XiphComments
490 * @param tag: the Xiph Comment
491 * @param p_input: the input item
493 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
496 #define WRITE( metaName, keyName ) \
497 psz_meta = input_item_Get##metaName( p_item ); \
500 String key( keyName, String::UTF8 ); \
501 String value( psz_meta, String::UTF8 ); \
502 tag->addField( key, value, true ); \
506 WRITE( Copyright, "COPYRIGHT" );
514 * Set the tags to the file using TagLib
515 * @param p_this: the demux object
516 * @return VLC_SUCCESS if the operation success
519 static int WriteMeta( vlc_object_t *p_this )
521 playlist_t *p_playlist = (playlist_t *)p_this;
522 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
523 input_item_t *p_item = p_export->p_item;
528 msg_Err( p_this, "Can't save meta data of an empty input" );
532 #if defined(WIN32) || defined (UNDER_CE)
533 if(GetVersion() < 0x80000000)
535 wchar_t wpath[MAX_PATH + 1];
536 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
538 wpath[MAX_PATH] = L'\0';
539 f = FileRef( wpath );
544 const char* local_name = ToLocale( p_export->psz_file );
547 f = FileRef( local_name );
548 LocaleFree( local_name );
551 if( f.isNull() || !f.tag() || f.file()->readOnly() )
553 msg_Err( p_this, "File %s can't be opened for tag writing",
554 p_export->psz_file );
558 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
560 Tag *p_tag = f.tag();
564 #define SET( a, b ) \
567 String* psz_tmp = new String( b, String::UTF8 ); \
568 p_tag->set##a( *psz_tmp ); \
572 // Saving all common fields
573 // If the title is empty, use the name
574 psz_meta = input_item_GetTitleFbName( p_item );
575 SET( Title, psz_meta );
578 psz_meta = input_item_GetArtist( p_item );
579 SET( Artist, psz_meta );
582 psz_meta = input_item_GetAlbum( p_item );
583 SET( Album, psz_meta );
586 psz_meta = input_item_GetDescription( p_item );
587 SET( Comment, psz_meta );
590 psz_meta = input_item_GetGenre( p_item );
591 SET( Genre, psz_meta );
596 psz_meta = input_item_GetDate( p_item );
597 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
600 psz_meta = input_item_GetTrackNum( p_item );
601 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
605 // Try now to write special tags
606 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
608 if( flac->ID3v2Tag() )
609 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
610 else if( flac->xiphComment() )
611 WriteMetaToXiph( flac->xiphComment(), p_item );
613 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
616 WriteMetaToAPE( mpc->APETag(), p_item );
618 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
620 if( mpeg->ID3v2Tag() )
621 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
622 else if( mpeg->APETag() )
623 WriteMetaToAPE( mpeg->APETag(), p_item );
625 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
627 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
628 WriteMetaToXiph( ogg_flac->tag(), p_item );
629 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
630 WriteMetaToXiph( ogg_speex->tag(), p_item );
631 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
632 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
634 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
636 if( trueaudio->ID3v2Tag() )
637 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
639 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
641 if( wavpack->APETag() )
642 WriteMetaToAPE( wavpack->APETag(), p_item );
645 // Save the meta data