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 */
50 #include <tbytevector.h>
54 #include <xiphcomment.h>
60 #include <oggflacfile.h>
62 #ifdef TAGLIB_WITH_ASF
63 # include <aifffile.h>
67 #ifdef TAGLIB_WITH_MP4
71 #include <speexfile.h>
72 #include <trueaudiofile.h>
73 #include <vorbisfile.h>
74 #include <wavpackfile.h>
76 #include <attachedpictureframe.h>
77 #include <textidentificationframe.h>
78 #include <uniquefileidentifierframe.h>
82 static int ReadMeta ( vlc_object_t * );
83 static int WriteMeta ( vlc_object_t * );
86 set_capability( "meta reader", 1000 )
87 set_callbacks( ReadMeta, NULL )
89 set_capability( "meta writer", 50 )
90 set_callbacks( WriteMeta, NULL )
93 using namespace TagLib;
97 * Read meta information from APE tags
98 * @param tag: the APE tag
99 * @param p_demux; the demux object
100 * @param p_demux_meta: the demuxer meta
101 * @param p_meta: the meta
103 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
106 #define SET( keyName, metaName ) \
107 item = tag->itemListMap()[keyName]; \
108 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
110 SET( "COPYRIGHT", Copyright );
111 SET( "LANGUAGE", Language );
112 SET( "PUBLISHER", Publisher );
120 * Read meta information from id3v2 tags
121 * @param tag: the id3v2 tag
122 * @param p_demux; the demux object
123 * @param p_demux_meta: the demuxer meta
124 * @param p_meta: the meta
126 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
128 // Get the unique file identifier
129 ID3v2::FrameList list = tag->frameListMap()["UFID"];
130 ID3v2::FrameList::Iterator iter;
131 for( iter = list.begin(); iter != list.end(); iter++ )
133 ID3v2::UniqueFileIdentifierFrame* p_ufid =
134 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
135 const char *owner = p_ufid->owner().toCString();
136 if (!strcmp( owner, "http://musicbrainz.org" ))
138 /* ID3v2 UFID contains up to 64 bytes binary data
139 * but in our case it will be a '\0'
140 * terminated string */
142 int max_size = __MIN( p_ufid->identifier().size(), 63);
143 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
144 psz_ufid[max_size] = '\0';
145 vlc_meta_SetTrackID( p_meta, psz_ufid );
150 list = tag->frameListMap()["TXXX"];
151 for( iter = list.begin(); iter != list.end(); iter++ )
153 ID3v2::UserTextIdentificationFrame* p_txxx =
154 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
155 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
156 p_txxx->fieldList().toString().toCString( true ) );
159 // Get some more information
160 #define SET( tagName, metaName ) \
161 list = tag->frameListMap()[tagName]; \
162 if( !list.isEmpty() ) \
163 vlc_meta_Set##metaName( p_meta, \
164 (*list.begin())->toString().toCString( true ) );
166 SET( "TCOP", Copyright );
167 SET( "TENC", EncodedBy );
168 SET( "TLAN", Language );
169 SET( "TPUB", Publisher );
173 /* Preferred type of image
174 * The 21 types are defined in id3v2 standard:
175 * http://www.id3.org/id3v2.4.0-frames */
176 static const int pi_cover_score[] = {
178 5, /* 32x32 PNG image that should be used as the file icon */
179 4, /* File icon of a different size or format. */
180 20, /* Front cover image of the album. */
181 19, /* Back cover image of the album. */
182 13, /* Inside leaflet page of the album. */
183 18, /* Image from the album itself. */
184 17, /* Picture of the lead artist or soloist. */
185 16, /* Picture of the artist or performer. */
186 14, /* Picture of the conductor. */
187 15, /* Picture of the band or orchestra. */
188 9, /* Picture of the composer. */
189 8, /* Picture of the lyricist or text writer. */
190 7, /* Picture of the recording location or studio. */
191 10, /* Picture of the artists during recording. */
192 11, /* Picture of the artists during performance. */
193 6, /* Picture from a movie or video related to the track. */
194 1, /* Picture of a large, coloured fish. */
195 12, /* Illustration related to the track. */
196 3, /* Logo of the band or performer. */
197 2 /* Logo of the publisher (record company). */
201 // Try now to get embedded art
202 list = tag->frameListMap()[ "APIC" ];
206 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
207 for( iter = list.begin(); iter != list.end(); iter++ )
209 ID3v2::AttachedPictureFrame* p_apic =
210 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
211 input_attachment_t *p_attachment;
213 const char *psz_mime;
214 char *psz_name, *psz_description;
216 // Get the mime and description of the image.
217 // If the description is empty, take the type as a description
218 psz_mime = p_apic->mimeType().toCString( true );
219 if( p_apic->description().size() > 0 )
220 psz_description = strdup( p_apic->description().toCString( true ) );
223 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
224 psz_description = NULL;
227 if( !psz_description )
229 psz_name = psz_description;
231 /* some old iTunes version not only sets incorrectly the mime type
232 * or the description of the image,
233 * but also embeds incorrectly the image.
234 * Recent versions seem to behave correctly */
235 if( !strncmp( psz_mime, "PNG", 3 ) ||
236 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
238 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
239 free( psz_description );
243 const ByteVector picture = p_apic->picture();
244 const char *p_data = picture.data();
245 const unsigned i_data = picture.size();
247 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
248 psz_name, psz_mime, i_data );
250 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
251 psz_description, p_data, i_data );
253 TAB_APPEND_CAST( (input_attachment_t**),
254 p_demux_meta->i_attachments, p_demux_meta->attachments,
256 free( psz_description );
258 if( pi_cover_score[p_apic->type()] > i_score )
260 i_score = pi_cover_score[p_apic->type()];
262 if( asprintf( &psz_url, "attachment://%s",
263 p_attachment->psz_name ) == -1 )
265 vlc_meta_SetArtURL( p_meta, psz_url );
274 * Read the meta information from XiphComments
275 * @param tag: the Xiph Comment
276 * @param p_demux; the demux object
277 * @param p_demux_meta: the demuxer meta
278 * @param p_meta: the meta
280 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
282 #define SET( keyName, metaName ) \
283 StringList list = tag->fieldListMap()[keyName]; \
284 if( !list.isEmpty() ) \
285 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
287 SET( "COPYRIGHT", Copyright );
290 // Try now to get embedded art
291 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
292 StringList art_list = tag->fieldListMap()[ "COVERART" ];
294 // We get only the first covert art
295 if( mime_list.size() > 1 || art_list.size() > 1 )
296 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
298 else if( mime_list.size() == 0 || art_list.size() == 0 )
301 input_attachment_t *p_attachment;
303 const char* psz_name = "cover";
304 const char* psz_mime = mime_list[0].toCString(true);
305 const char* psz_description = "cover";
308 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
310 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
311 psz_name, psz_mime, i_data );
313 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
314 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
315 psz_description, p_data, i_data );
318 TAB_APPEND_CAST( (input_attachment_t**),
319 p_demux_meta->i_attachments, p_demux_meta->attachments,
322 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
325 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
326 static void ReadMetaFromMP4( MP4::Tag* tag, demux_t *p_demux, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
328 if( tag->itemListMap().contains("covr") )
330 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
331 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
333 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
334 psz_format, list[0].data().size() );
336 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
337 input_attachment_t *p_attachment =
338 vlc_input_attachment_New( "cover", psz_format, "cover",
339 list[0].data().data(), list[0].data().size() );
340 TAB_APPEND_CAST( (input_attachment_t**),
341 p_demux_meta->i_attachments, p_demux_meta->attachments,
343 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
349 * Get the tags from the file using TagLib
350 * @param p_this: the demux object
351 * @return VLC_SUCCESS if the operation success
353 static int ReadMeta( vlc_object_t* p_this)
355 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
356 demux_t* p_demux = p_demux_meta->p_demux;
360 p_demux_meta->p_meta = NULL;
361 if( strcmp( p_demux->psz_access, "file" ) )
364 char *psz_path = strdup( p_demux->psz_file );
368 #if defined(WIN32) || defined (UNDER_CE)
369 wchar_t wpath[MAX_PATH + 1];
370 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
375 wpath[MAX_PATH] = L'\0';
376 f = FileRef( wpath );
378 const char* local_name = ToLocale( psz_path );
384 f = FileRef( local_name );
385 LocaleFree( local_name );
391 if( !f.tag() || f.tag()->isEmpty() )
394 p_demux_meta->p_meta = p_meta = vlc_meta_New();
399 // Read the tags from the file
400 Tag* p_tag = f.tag();
402 #define SET( tag, meta ) \
403 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
404 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
405 #define SETINT( tag, meta ) \
409 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
410 vlc_meta_Set##meta( p_meta, psz_tmp ); \
414 SET( artist, Artist );
416 SET( comment, Description );
418 SETINT( year, Date );
419 SETINT( track, TrackNum );
425 // Try now to read special tags
426 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
428 if( flac->ID3v2Tag() )
429 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
430 else if( flac->xiphComment() )
431 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
433 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
434 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
437 ReadMetaFromMP4( mp4->tag(), p_demux, p_demux_meta, p_meta );
440 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
443 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
445 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
447 if( mpeg->ID3v2Tag() )
448 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
449 else if( mpeg->APETag() )
450 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
452 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
454 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
455 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
456 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
457 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
458 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
459 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
461 #ifdef TAGLIB_WITH_ASF
462 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
464 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
465 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
466 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
467 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
470 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
472 if( trueaudio->ID3v2Tag() )
473 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
475 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
477 if( wavpack->APETag() )
478 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
487 * Write meta information to APE tags
488 * @param tag: the APE tag
489 * @param p_item: the input item
491 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
494 #define WRITE( metaName, keyName ) \
495 psz_meta = input_item_Get##metaName( p_item ); \
498 String key( keyName, String::UTF8 ); \
499 String value( psz_meta, String::UTF8 ); \
500 tag->addValue( key, value, true ); \
504 WRITE( Copyright, "COPYRIGHT" );
505 WRITE( Language, "LANGUAGE" );
506 WRITE( Publisher, "PUBLISHER" );
514 * Write meta information to id3v2 tags
515 * @param tag: the id3v2 tag
516 * @param p_input: the input item
518 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
521 #define WRITE( metaName, tagName ) \
522 psz_meta = input_item_Get##metaName( p_item ); \
525 ByteVector p_byte( tagName, 4 ); \
526 tag->removeFrames( p_byte ); \
527 ID3v2::TextIdentificationFrame* p_frame = \
528 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
529 p_frame->setText( psz_meta ); \
530 tag->addFrame( p_frame ); \
534 WRITE( Copyright, "TCOP" );
535 WRITE( EncodedBy, "TENC" );
536 WRITE( Language, "TLAN" );
537 WRITE( Publisher, "TPUB" );
545 * Write the meta information to XiphComments
546 * @param tag: the Xiph Comment
547 * @param p_input: the input item
549 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
552 #define WRITE( metaName, keyName ) \
553 psz_meta = input_item_Get##metaName( p_item ); \
556 String key( keyName, String::UTF8 ); \
557 String value( psz_meta, String::UTF8 ); \
558 tag->addField( key, value, true ); \
562 WRITE( Copyright, "COPYRIGHT" );
570 * Set the tags to the file using TagLib
571 * @param p_this: the demux object
572 * @return VLC_SUCCESS if the operation success
575 static int WriteMeta( vlc_object_t *p_this )
577 meta_export_t *p_export = (meta_export_t *)p_this;
578 input_item_t *p_item = p_export->p_item;
583 msg_Err( p_this, "Can't save meta data of an empty input" );
587 #if defined(WIN32) || defined (UNDER_CE)
588 wchar_t wpath[MAX_PATH + 1];
589 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
591 wpath[MAX_PATH] = L'\0';
592 f = FileRef( wpath );
594 const char* local_name = ToLocale( p_export->psz_file );
597 f = FileRef( local_name );
598 LocaleFree( local_name );
601 if( f.isNull() || !f.tag() || f.file()->readOnly() )
603 msg_Err( p_this, "File %s can't be opened for tag writing",
604 p_export->psz_file );
608 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
610 Tag *p_tag = f.tag();
614 #define SET( a, b ) \
615 psz_meta = input_item_Get ## a( p_item ); \
618 String tmp( psz_meta, String::UTF8 ); \
619 p_tag->set##b( tmp ); \
623 // Saving all common fields
624 // If the title is empty, use the name
625 SET( TitleFbName, Title );
626 SET( Artist, Artist );
628 SET( Description, Comment );
633 psz_meta = input_item_GetDate( p_item );
634 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
637 psz_meta = input_item_GetTrackNum( p_item );
638 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
642 // Try now to write special tags
643 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
645 if( flac->ID3v2Tag() )
646 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
647 else if( flac->xiphComment() )
648 WriteMetaToXiph( flac->xiphComment(), p_item );
650 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
653 WriteMetaToAPE( mpc->APETag(), p_item );
655 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
657 if( mpeg->ID3v2Tag() )
658 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
659 else if( mpeg->APETag() )
660 WriteMetaToAPE( mpeg->APETag(), p_item );
662 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
664 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
665 WriteMetaToXiph( ogg_flac->tag(), p_item );
666 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
667 WriteMetaToXiph( ogg_speex->tag(), p_item );
668 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
669 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
671 #ifdef TAGLIB_WITH_ASF
672 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
674 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
675 WriteMetaToId3v2( riff_aiff->tag(), p_item );
676 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
677 WriteMetaToId3v2( riff_wav->tag(), p_item );
680 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
682 if( trueaudio->ID3v2Tag() )
683 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
685 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
687 if( wavpack->APETag() )
688 WriteMetaToAPE( wavpack->APETag(), p_item );
691 // Save the meta data