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>
48 #include <tbytevector.h>
52 #include <xiphcomment.h>
58 #include <oggflacfile.h>
60 #ifdef TAGLIB_WITH_ASF
61 # include <aifffile.h>
65 #ifdef TAGLIB_WITH_MP4
69 #include <speexfile.h>
70 #include <trueaudiofile.h>
71 #include <vorbisfile.h>
72 #include <wavpackfile.h>
74 #include <attachedpictureframe.h>
75 #include <textidentificationframe.h>
76 #include <uniquefileidentifierframe.h>
80 static int ReadMeta ( vlc_object_t * );
81 static int WriteMeta ( vlc_object_t * );
84 set_capability( "meta reader", 1000 )
85 set_callbacks( ReadMeta, NULL )
87 set_capability( "meta writer", 50 )
88 set_callbacks( WriteMeta, NULL )
91 using namespace TagLib;
95 * Read meta informations from APE tags
96 * @param tag: the APE tag
97 * @param p_demux; the demux object
98 * @param p_demux_meta: the demuxer meta
99 * @param p_meta: the meta
101 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
104 #define SET( keyName, metaName ) \
105 item = tag->itemListMap()[keyName]; \
106 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
108 SET( "COPYRIGHT", Copyright );
109 SET( "LANGUAGE", Language );
110 SET( "PUBLISHER", Publisher );
118 * Read meta information from id3v2 tags
119 * @param tag: the id3v2 tag
120 * @param p_demux; the demux object
121 * @param p_demux_meta: the demuxer meta
122 * @param p_meta: the meta
124 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
126 // Get the unique file identifier
127 ID3v2::FrameList list = tag->frameListMap()["UFID"];
128 ID3v2::FrameList::Iterator iter;
129 for( iter = list.begin(); iter != list.end(); iter++ )
131 ID3v2::UniqueFileIdentifierFrame* p_ufid =
132 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
133 const char *owner = p_ufid->owner().toCString();
134 if (!strcmp( owner, "http://musicbrainz.org" ))
136 /* ID3v2 UFID contains up to 64 bytes binary data
137 * but in our case it will be a '\0'
138 * terminated string */
140 int max_size = __MIN( p_ufid->identifier().size(), 63);
141 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
142 psz_ufid[max_size] = '\0';
143 vlc_meta_SetTrackID( p_meta, psz_ufid );
148 list = tag->frameListMap()["TXXX"];
149 for( iter = list.begin(); iter != list.end(); iter++ )
151 ID3v2::UserTextIdentificationFrame* p_txxx =
152 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
153 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
154 p_txxx->fieldList().toString().toCString( true ) );
157 // Get some more informations
158 #define SET( tagName, metaName ) \
159 list = tag->frameListMap()[tagName]; \
160 if( !list.isEmpty() ) \
161 vlc_meta_Set##metaName( p_meta, \
162 (*list.begin())->toString().toCString( true ) );
164 SET( "TCOP", Copyright );
165 SET( "TENC", EncodedBy );
166 SET( "TLAN", Language );
167 SET( "TPUB", Publisher );
171 /* Preferred type of image
172 * The 21 types are defined in id3v2 standard:
173 * http://www.id3.org/id3v2.4.0-frames */
174 static const int pi_cover_score[] = {
176 5, /* 32x32 PNG image that should be used as the file icon */
177 4, /* File icon of a different size or format. */
178 20, /* Front cover image of the album. */
179 19, /* Back cover image of the album. */
180 13, /* Inside leaflet page of the album. */
181 18, /* Image from the album itself. */
182 17, /* Picture of the lead artist or soloist. */
183 16, /* Picture of the artist or performer. */
184 14, /* Picture of the conductor. */
185 15, /* Picture of the band or orchestra. */
186 9, /* Picture of the composer. */
187 8, /* Picture of the lyricist or text writer. */
188 7, /* Picture of the recording location or studio. */
189 10, /* Picture of the artists during recording. */
190 11, /* Picture of the artists during performance. */
191 6, /* Picture from a movie or video related to the track. */
192 1, /* Picture of a large, coloured fish. */
193 12, /* Illustration related to the track. */
194 3, /* Logo of the band or performer. */
195 2 /* Logo of the publisher (record company). */
199 // Try now to get embedded art
200 list = tag->frameListMap()[ "APIC" ];
204 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
205 for( iter = list.begin(); iter != list.end(); iter++ )
207 ID3v2::AttachedPictureFrame* p_apic =
208 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
209 input_attachment_t *p_attachment;
211 const char *psz_mime;
212 char *psz_name, *psz_description;
214 // Get the mime and description of the image.
215 // If the description is empty, take the type as a description
216 psz_mime = p_apic->mimeType().toCString( true );
217 if( p_apic->description().size() > 0 )
218 psz_description = strdup( p_apic->description().toCString( true ) );
221 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
222 psz_description = NULL;
225 if( !psz_description )
227 psz_name = psz_description;
229 /* some old iTunes version not only sets incorrectly the mime type
230 * or the description of the image,
231 * but also embeds incorrectly the image.
232 * Recent versions seem to behave correctly */
233 if( !strncmp( psz_mime, "PNG", 3 ) ||
234 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
236 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
237 free( psz_description );
241 const ByteVector picture = p_apic->picture();
242 const char *p_data = picture.data();
243 const unsigned i_data = picture.size();
245 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
246 psz_name, psz_mime, i_data );
248 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
249 psz_description, p_data, i_data );
251 TAB_APPEND_CAST( (input_attachment_t**),
252 p_demux_meta->i_attachments, p_demux_meta->attachments,
254 free( psz_description );
256 if( pi_cover_score[p_apic->type()] > i_score )
258 i_score = pi_cover_score[p_apic->type()];
260 if( asprintf( &psz_url, "attachment://%s",
261 p_attachment->psz_name ) == -1 )
263 vlc_meta_SetArtURL( p_meta, psz_url );
272 * Read the meta informations from XiphComments
273 * @param tag: the Xiph Comment
274 * @param p_demux; the demux object
275 * @param p_demux_meta: the demuxer meta
276 * @param p_meta: the meta
278 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
280 #define SET( keyName, metaName ) \
281 StringList list = tag->fieldListMap()[keyName]; \
282 if( !list.isEmpty() ) \
283 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
285 SET( "COPYRIGHT", Copyright );
288 // Try now to get embedded art
289 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
290 StringList art_list = tag->fieldListMap()[ "COVERART" ];
292 // We get only the first covert art
293 if( mime_list.size() > 1 || art_list.size() > 1 )
294 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
296 else if( mime_list.size() == 0 || art_list.size() == 0 )
299 input_attachment_t *p_attachment;
301 const char* psz_name = "cover";
302 const char* psz_mime = mime_list[0].toCString(true);
303 const char* psz_description = "cover";
306 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
308 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
309 psz_name, psz_mime, i_data );
311 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
312 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
313 psz_description, p_data, i_data );
316 TAB_APPEND_CAST( (input_attachment_t**),
317 p_demux_meta->i_attachments, p_demux_meta->attachments,
320 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
323 #ifdef TAGLIB_WITH_MP4
324 static void ReadMetaFromMP4( MP4::Tag* tag, demux_t *p_demux, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
326 if( tag->itemListMap().contains("covr") )
328 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
329 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
331 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
332 psz_format, list[0].data().size() );
334 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
335 input_attachment_t *p_attachment =
336 vlc_input_attachment_New( "cover", psz_format, "cover",
337 list[0].data().data(), list[0].data().size() );
338 TAB_APPEND_CAST( (input_attachment_t**),
339 p_demux_meta->i_attachments, p_demux_meta->attachments,
341 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
347 * Get the tags from the file using TagLib
348 * @param p_this: the demux object
349 * @return VLC_SUCCESS if the operation success
351 static int ReadMeta( vlc_object_t* p_this)
353 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
354 demux_t* p_demux = p_demux_meta->p_demux;
357 char *psz_path = decode_URI_duplicate( p_demux->psz_path );
359 p_demux_meta->p_meta = NULL;
362 if( strncmp( p_demux->psz_access, "file", strlen("file") ) )
366 #if defined(WIN32) || defined (UNDER_CE)
367 wchar_t wpath[MAX_PATH + 1];
368 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
370 wpath[MAX_PATH] = L'\0';
371 f = FileRef( wpath );
373 const char* local_name = ToLocale( psz_path );
376 f = FileRef( local_name );
377 LocaleFree( local_name );
383 if( !f.tag() || f.tag()->isEmpty() )
386 p_demux_meta->p_meta = p_meta = vlc_meta_New();
391 // Read the tags from the file
392 Tag* p_tag = f.tag();
394 #define SET( tag, meta ) \
395 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
396 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
397 #define SETINT( tag, meta ) \
401 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
402 vlc_meta_Set##meta( p_meta, psz_tmp ); \
406 SET( artist, Artist );
408 SET( comment, Description );
410 SETINT( year, Date );
411 SETINT( track, TrackNum );
417 // Try now to read special tags
418 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
420 if( flac->ID3v2Tag() )
421 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
422 else if( flac->xiphComment() )
423 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
425 #ifdef TAGLIB_WITH_MP4
426 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
429 ReadMetaFromMP4( mp4->tag(), p_demux, p_demux_meta, p_meta );
432 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
435 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
437 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
439 if( mpeg->ID3v2Tag() )
440 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
441 else if( mpeg->APETag() )
442 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
444 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
446 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
447 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
448 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
449 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
450 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
451 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
453 #ifdef TAGLIB_WITH_ASF
454 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
456 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
457 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
458 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
459 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
462 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
464 if( trueaudio->ID3v2Tag() )
465 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
467 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
469 if( wavpack->APETag() )
470 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
479 * Write meta informations to APE tags
480 * @param tag: the APE tag
481 * @param p_item: the input item
483 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
486 #define WRITE( metaName, keyName ) \
487 psz_meta = input_item_Get##metaName( p_item ); \
490 String key( keyName, String::UTF8 ); \
491 String value( psz_meta, String::UTF8 ); \
492 tag->addValue( key, value, true ); \
496 WRITE( Copyright, "COPYRIGHT" );
497 WRITE( Language, "LANGUAGE" );
498 WRITE( Publisher, "PUBLISHER" );
506 * Write meta information to id3v2 tags
507 * @param tag: the id3v2 tag
508 * @param p_input: the input item
510 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
513 #define WRITE( metaName, tagName ) \
514 psz_meta = input_item_Get##metaName( p_item ); \
517 ByteVector p_byte( tagName, 4 ); \
518 tag->removeFrames( p_byte ); \
519 ID3v2::TextIdentificationFrame* p_frame = \
520 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
521 p_frame->setText( psz_meta ); \
522 tag->addFrame( p_frame ); \
526 WRITE( Copyright, "TCOP" );
527 WRITE( EncodedBy, "TENC" );
528 WRITE( Language, "TLAN" );
529 WRITE( Publisher, "TPUB" );
537 * Write the meta informations to XiphComments
538 * @param tag: the Xiph Comment
539 * @param p_input: the input item
541 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
544 #define WRITE( metaName, keyName ) \
545 psz_meta = input_item_Get##metaName( p_item ); \
548 String key( keyName, String::UTF8 ); \
549 String value( psz_meta, String::UTF8 ); \
550 tag->addField( key, value, true ); \
554 WRITE( Copyright, "COPYRIGHT" );
562 * Set the tags to the file using TagLib
563 * @param p_this: the demux object
564 * @return VLC_SUCCESS if the operation success
567 static int WriteMeta( vlc_object_t *p_this )
569 meta_export_t *p_export = (meta_export_t *)p_this;
570 input_item_t *p_item = p_export->p_item;
575 msg_Err( p_this, "Can't save meta data of an empty input" );
579 #if defined(WIN32) || defined (UNDER_CE)
580 wchar_t wpath[MAX_PATH + 1];
581 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
583 wpath[MAX_PATH] = L'\0';
584 f = FileRef( wpath );
586 const char* local_name = ToLocale( p_export->psz_file );
589 f = FileRef( local_name );
590 LocaleFree( local_name );
593 if( f.isNull() || !f.tag() || f.file()->readOnly() )
595 msg_Err( p_this, "File %s can't be opened for tag writing",
596 p_export->psz_file );
600 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
602 Tag *p_tag = f.tag();
606 #define SET( a, b ) \
607 psz_meta = input_item_Get ## a( p_item ); \
610 String tmp( psz_meta, String::UTF8 ); \
611 p_tag->set##b( tmp ); \
615 // Saving all common fields
616 // If the title is empty, use the name
617 SET( TitleFbName, Title );
618 SET( Artist, Artist );
620 SET( Description, Comment );
625 psz_meta = input_item_GetDate( p_item );
626 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
629 psz_meta = input_item_GetTrackNum( p_item );
630 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
634 // Try now to write special tags
635 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
637 if( flac->ID3v2Tag() )
638 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
639 else if( flac->xiphComment() )
640 WriteMetaToXiph( flac->xiphComment(), p_item );
642 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
645 WriteMetaToAPE( mpc->APETag(), p_item );
647 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
649 if( mpeg->ID3v2Tag() )
650 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
651 else if( mpeg->APETag() )
652 WriteMetaToAPE( mpeg->APETag(), p_item );
654 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
656 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
657 WriteMetaToXiph( ogg_flac->tag(), p_item );
658 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
659 WriteMetaToXiph( ogg_speex->tag(), p_item );
660 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
661 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
663 #ifdef TAGLIB_WITH_ASF
664 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
666 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
667 WriteMetaToId3v2( riff_aiff->tag(), p_item );
668 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
669 WriteMetaToId3v2( riff_wav->tag(), p_item );
672 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
674 if( trueaudio->ID3v2Tag() )
675 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
677 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
679 if( wavpack->APETag() )
680 WriteMetaToAPE( wavpack->APETag(), p_item );
683 // Save the meta data