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 #ifdef TAGLIB_WITH_MP4
70 #include <speexfile.h>
71 #include <trueaudiofile.h>
72 #include <vorbisfile.h>
73 #include <wavpackfile.h>
75 #include <attachedpictureframe.h>
76 #include <textidentificationframe.h>
77 #include <uniquefileidentifierframe.h>
81 static int ReadMeta ( vlc_object_t * );
82 static int WriteMeta ( vlc_object_t * );
85 set_capability( "meta reader", 1000 )
86 set_callbacks( ReadMeta, NULL )
88 set_capability( "meta writer", 50 )
89 set_callbacks( WriteMeta, NULL )
92 using namespace TagLib;
96 * Read meta informations from APE tags
97 * @param tag: the APE tag
98 * @param p_demux; the demux object
99 * @param p_demux_meta: the demuxer meta
100 * @param p_meta: the meta
102 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
105 #define SET( keyName, metaName ) \
106 item = tag->itemListMap()[keyName]; \
107 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
109 SET( "COPYRIGHT", Copyright );
110 SET( "LANGUAGE", Language );
111 SET( "PUBLISHER", Publisher );
119 * Read meta information from id3v2 tags
120 * @param tag: the id3v2 tag
121 * @param p_demux; the demux object
122 * @param p_demux_meta: the demuxer meta
123 * @param p_meta: the meta
125 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
127 // Get the unique file identifier
128 ID3v2::FrameList list = tag->frameListMap()["UFID"];
129 ID3v2::FrameList::Iterator iter;
130 for( iter = list.begin(); iter != list.end(); iter++ )
132 ID3v2::UniqueFileIdentifierFrame* p_ufid =
133 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
134 const char *owner = p_ufid->owner().toCString();
135 if (!strcmp( owner, "http://musicbrainz.org" ))
137 /* ID3v2 UFID contains up to 64 bytes binary data
138 * but in our case it will be a '\0'
139 * terminated string */
141 int max_size = __MIN( p_ufid->identifier().size(), 63);
142 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
143 psz_ufid[max_size] = '\0';
144 vlc_meta_SetTrackID( p_meta, psz_ufid );
149 list = tag->frameListMap()["TXXX"];
150 for( iter = list.begin(); iter != list.end(); iter++ )
152 ID3v2::UserTextIdentificationFrame* p_txxx =
153 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
154 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
155 p_txxx->fieldList().toString().toCString( true ) );
158 // Get some more informations
159 #define SET( tagName, metaName ) \
160 list = tag->frameListMap()[tagName]; \
161 if( !list.isEmpty() ) \
162 vlc_meta_Set##metaName( p_meta, \
163 (*list.begin())->toString().toCString( true ) );
165 SET( "TCOP", Copyright );
166 SET( "TENC", EncodedBy );
167 SET( "TLAN", Language );
168 SET( "TPUB", Publisher );
172 /* Preferred type of image
173 * The 21 types are defined in id3v2 standard:
174 * http://www.id3.org/id3v2.4.0-frames */
175 static const int pi_cover_score[] = {
177 5, /* 32x32 PNG image that should be used as the file icon */
178 4, /* File icon of a different size or format. */
179 20, /* Front cover image of the album. */
180 19, /* Back cover image of the album. */
181 13, /* Inside leaflet page of the album. */
182 18, /* Image from the album itself. */
183 17, /* Picture of the lead artist or soloist. */
184 16, /* Picture of the artist or performer. */
185 14, /* Picture of the conductor. */
186 15, /* Picture of the band or orchestra. */
187 9, /* Picture of the composer. */
188 8, /* Picture of the lyricist or text writer. */
189 7, /* Picture of the recording location or studio. */
190 10, /* Picture of the artists during recording. */
191 11, /* Picture of the artists during performance. */
192 6, /* Picture from a movie or video related to the track. */
193 1, /* Picture of a large, coloured fish. */
194 12, /* Illustration related to the track. */
195 3, /* Logo of the band or performer. */
196 2 /* Logo of the publisher (record company). */
200 // Try now to get embedded art
201 list = tag->frameListMap()[ "APIC" ];
205 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
206 for( iter = list.begin(); iter != list.end(); iter++ )
208 ID3v2::AttachedPictureFrame* p_apic =
209 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
210 input_attachment_t *p_attachment;
212 const char *psz_mime;
213 char *psz_name, *psz_description;
215 // Get the mime and description of the image.
216 // If the description is empty, take the type as a description
217 psz_mime = p_apic->mimeType().toCString( true );
218 if( p_apic->description().size() > 0 )
219 psz_description = strdup( p_apic->description().toCString( true ) );
222 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
223 psz_description = NULL;
226 if( !psz_description )
228 psz_name = psz_description;
230 /* some old iTunes version not only sets incorrectly the mime type
231 * or the description of the image,
232 * but also embeds incorrectly the image.
233 * Recent versions seem to behave correctly */
234 if( !strncmp( psz_mime, "PNG", 3 ) ||
235 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
237 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
238 free( psz_description );
242 const ByteVector picture = p_apic->picture();
243 const char *p_data = picture.data();
244 const unsigned i_data = picture.size();
246 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
247 psz_name, psz_mime, i_data );
249 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
250 psz_description, p_data, i_data );
252 TAB_APPEND_CAST( (input_attachment_t**),
253 p_demux_meta->i_attachments, p_demux_meta->attachments,
255 free( psz_description );
257 if( pi_cover_score[p_apic->type()] > i_score )
259 i_score = pi_cover_score[p_apic->type()];
261 if( asprintf( &psz_url, "attachment://%s",
262 p_attachment->psz_name ) == -1 )
264 vlc_meta_SetArtURL( p_meta, psz_url );
273 * Read the meta informations from XiphComments
274 * @param tag: the Xiph Comment
275 * @param p_demux; the demux object
276 * @param p_demux_meta: the demuxer meta
277 * @param p_meta: the meta
279 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
281 #define SET( keyName, metaName ) \
282 StringList list = tag->fieldListMap()[keyName]; \
283 if( !list.isEmpty() ) \
284 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
286 SET( "COPYRIGHT", Copyright );
289 // Try now to get embedded art
290 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
291 StringList art_list = tag->fieldListMap()[ "COVERART" ];
293 // We get only the first covert art
294 if( mime_list.size() > 1 || art_list.size() > 1 )
295 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
297 else if( mime_list.size() == 0 || art_list.size() == 0 )
300 input_attachment_t *p_attachment;
302 const char* psz_name = "cover";
303 const char* psz_mime = mime_list[0].toCString(true);
304 const char* psz_description = "cover";
307 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
309 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
310 psz_name, psz_mime, i_data );
312 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
313 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
314 psz_description, p_data, i_data );
317 TAB_APPEND_CAST( (input_attachment_t**),
318 p_demux_meta->i_attachments, p_demux_meta->attachments,
321 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
324 #ifdef TAGLIB_WITH_MP4
325 static void ReadMetaFromMP4( MP4::Tag* tag, demux_t *p_demux, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
327 if( tag->itemListMap().contains("covr") )
329 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
330 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
332 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
333 input_attachment_t *p_attachment =
334 vlc_input_attachment_New( "cover", psz_format, "cover",
335 list[0].data().data(), list[0].data().size() );
336 TAB_APPEND_CAST( (input_attachment_t**),
337 p_demux_meta->i_attachments, p_demux_meta->attachments,
339 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
345 * Get the tags from the file using TagLib
346 * @param p_this: the demux object
347 * @return VLC_SUCCESS if the operation success
349 static int ReadMeta( vlc_object_t* p_this)
351 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
352 demux_t* p_demux = p_demux_meta->p_demux;
355 char *psz_path = decode_URI_duplicate( p_demux->psz_path );
357 p_demux_meta->p_meta = NULL;
360 if( strncmp( p_demux->psz_access, "file", strlen("file") ) )
364 #if defined(WIN32) || defined (UNDER_CE)
365 wchar_t wpath[MAX_PATH + 1];
366 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
368 wpath[MAX_PATH] = L'\0';
369 f = FileRef( wpath );
371 const char* local_name = ToLocale( psz_path );
374 f = FileRef( local_name );
375 LocaleFree( local_name );
381 if( !f.tag() || f.tag()->isEmpty() )
384 p_demux_meta->p_meta = p_meta = vlc_meta_New();
389 // Read the tags from the file
390 Tag* p_tag = f.tag();
392 #define SET( tag, meta ) \
393 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
394 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
395 #define SETINT( tag, meta ) \
399 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
400 vlc_meta_Set##meta( p_meta, psz_tmp ); \
404 SET( artist, Artist );
406 SET( comment, Description );
408 SETINT( year, Date );
409 SETINT( track, TrackNum );
415 // Try now to read special tags
416 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
418 if( flac->ID3v2Tag() )
419 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
420 else if( flac->xiphComment() )
421 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
423 #ifdef TAGLIB_WITH_MP4
424 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
427 ReadMetaFromMP4( mp4->tag(), p_demux, p_demux_meta, p_meta );
430 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
433 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
435 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
437 if( mpeg->ID3v2Tag() )
438 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
439 else if( mpeg->APETag() )
440 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
442 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
444 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
445 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
446 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
447 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
448 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
449 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
451 #ifdef TAGLIB_WITH_ASF
452 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
454 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
455 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
456 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
457 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
460 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
462 if( trueaudio->ID3v2Tag() )
463 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
465 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
467 if( wavpack->APETag() )
468 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
477 * Write meta informations to APE tags
478 * @param tag: the APE tag
479 * @param p_item: the input item
481 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
484 #define WRITE( metaName, keyName ) \
485 psz_meta = input_item_Get##metaName( p_item ); \
488 String key( keyName, String::UTF8 ); \
489 String value( psz_meta, String::UTF8 ); \
490 tag->addValue( key, value, true ); \
494 WRITE( Copyright, "COPYRIGHT" );
495 WRITE( Language, "LANGUAGE" );
496 WRITE( Publisher, "PUBLISHER" );
504 * Write meta information to id3v2 tags
505 * @param tag: the id3v2 tag
506 * @param p_input: the input item
508 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
511 #define WRITE( metaName, tagName ) \
512 psz_meta = input_item_Get##metaName( p_item ); \
515 ByteVector p_byte( tagName, 4 ); \
516 tag->removeFrames( p_byte ); \
517 ID3v2::TextIdentificationFrame* p_frame = \
518 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
519 p_frame->setText( psz_meta ); \
520 tag->addFrame( p_frame ); \
524 WRITE( Copyright, "TCOP" );
525 WRITE( EncodedBy, "TENC" );
526 WRITE( Language, "TLAN" );
527 WRITE( Publisher, "TPUB" );
535 * Write the meta informations to XiphComments
536 * @param tag: the Xiph Comment
537 * @param p_input: the input item
539 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
542 #define WRITE( metaName, keyName ) \
543 psz_meta = input_item_Get##metaName( p_item ); \
546 String key( keyName, String::UTF8 ); \
547 String value( psz_meta, String::UTF8 ); \
548 tag->addField( key, value, true ); \
552 WRITE( Copyright, "COPYRIGHT" );
560 * Set the tags to the file using TagLib
561 * @param p_this: the demux object
562 * @return VLC_SUCCESS if the operation success
565 static int WriteMeta( vlc_object_t *p_this )
567 meta_export_t *p_export = (meta_export_t *)p_this;
568 input_item_t *p_item = p_export->p_item;
573 msg_Err( p_this, "Can't save meta data of an empty input" );
577 #if defined(WIN32) || defined (UNDER_CE)
578 wchar_t wpath[MAX_PATH + 1];
579 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
581 wpath[MAX_PATH] = L'\0';
582 f = FileRef( wpath );
584 const char* local_name = ToLocale( p_export->psz_file );
587 f = FileRef( local_name );
588 LocaleFree( local_name );
591 if( f.isNull() || !f.tag() || f.file()->readOnly() )
593 msg_Err( p_this, "File %s can't be opened for tag writing",
594 p_export->psz_file );
598 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
600 Tag *p_tag = f.tag();
604 #define SET( a, b ) \
605 psz_meta = input_item_Get ## a( p_item ); \
608 String tmp( psz_meta, String::UTF8 ); \
609 p_tag->set##b( tmp ); \
613 // Saving all common fields
614 // If the title is empty, use the name
615 SET( TitleFbName, Title );
616 SET( Artist, Artist );
618 SET( Description, Comment );
623 psz_meta = input_item_GetDate( p_item );
624 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
627 psz_meta = input_item_GetTrackNum( p_item );
628 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
632 // Try now to write special tags
633 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
635 if( flac->ID3v2Tag() )
636 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
637 else if( flac->xiphComment() )
638 WriteMetaToXiph( flac->xiphComment(), p_item );
640 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
643 WriteMetaToAPE( mpc->APETag(), p_item );
645 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
647 if( mpeg->ID3v2Tag() )
648 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
649 else if( mpeg->APETag() )
650 WriteMetaToAPE( mpeg->APETag(), p_item );
652 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
654 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
655 WriteMetaToXiph( ogg_flac->tag(), p_item );
656 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
657 WriteMetaToXiph( ogg_speex->tag(), p_item );
658 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
659 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
661 #ifdef TAGLIB_WITH_ASF
662 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
664 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
665 WriteMetaToId3v2( riff_aiff->tag(), p_item );
666 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
667 WriteMetaToId3v2( riff_wav->tag(), p_item );
670 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
672 if( trueaudio->ID3v2Tag() )
673 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
675 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
677 if( wavpack->APETag() )
678 WriteMetaToAPE( wavpack->APETag(), p_item );
681 // Save the meta data