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 informations 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 informations
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 informations 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 #ifdef TAGLIB_WITH_MP4
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;
359 char *psz_path = decode_URI_duplicate( p_demux->psz_path );
361 p_demux_meta->p_meta = NULL;
364 if( strncmp( p_demux->psz_access, "file", strlen("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) )
372 wpath[MAX_PATH] = L'\0';
373 f = FileRef( wpath );
375 const char* local_name = ToLocale( psz_path );
378 f = FileRef( local_name );
379 LocaleFree( local_name );
385 if( !f.tag() || f.tag()->isEmpty() )
388 p_demux_meta->p_meta = p_meta = vlc_meta_New();
393 // Read the tags from the file
394 Tag* p_tag = f.tag();
396 #define SET( tag, meta ) \
397 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
398 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
399 #define SETINT( tag, meta ) \
403 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
404 vlc_meta_Set##meta( p_meta, psz_tmp ); \
408 SET( artist, Artist );
410 SET( comment, Description );
412 SETINT( year, Date );
413 SETINT( track, TrackNum );
419 // Try now to read special tags
420 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
422 if( flac->ID3v2Tag() )
423 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
424 else if( flac->xiphComment() )
425 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
427 #ifdef TAGLIB_WITH_MP4
428 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
431 ReadMetaFromMP4( mp4->tag(), p_demux, p_demux_meta, p_meta );
434 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
437 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
439 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
441 if( mpeg->ID3v2Tag() )
442 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
443 else if( mpeg->APETag() )
444 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
446 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
448 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
449 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
450 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
451 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
452 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
453 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
455 #ifdef TAGLIB_WITH_ASF
456 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
458 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
459 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
460 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
461 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
464 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
466 if( trueaudio->ID3v2Tag() )
467 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
469 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
471 if( wavpack->APETag() )
472 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
481 * Write meta informations to APE tags
482 * @param tag: the APE tag
483 * @param p_item: the input item
485 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
488 #define WRITE( metaName, keyName ) \
489 psz_meta = input_item_Get##metaName( p_item ); \
492 String key( keyName, String::UTF8 ); \
493 String value( psz_meta, String::UTF8 ); \
494 tag->addValue( key, value, true ); \
498 WRITE( Copyright, "COPYRIGHT" );
499 WRITE( Language, "LANGUAGE" );
500 WRITE( Publisher, "PUBLISHER" );
508 * Write meta information to id3v2 tags
509 * @param tag: the id3v2 tag
510 * @param p_input: the input item
512 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
515 #define WRITE( metaName, tagName ) \
516 psz_meta = input_item_Get##metaName( p_item ); \
519 ByteVector p_byte( tagName, 4 ); \
520 tag->removeFrames( p_byte ); \
521 ID3v2::TextIdentificationFrame* p_frame = \
522 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
523 p_frame->setText( psz_meta ); \
524 tag->addFrame( p_frame ); \
528 WRITE( Copyright, "TCOP" );
529 WRITE( EncodedBy, "TENC" );
530 WRITE( Language, "TLAN" );
531 WRITE( Publisher, "TPUB" );
539 * Write the meta informations to XiphComments
540 * @param tag: the Xiph Comment
541 * @param p_input: the input item
543 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
546 #define WRITE( metaName, keyName ) \
547 psz_meta = input_item_Get##metaName( p_item ); \
550 String key( keyName, String::UTF8 ); \
551 String value( psz_meta, String::UTF8 ); \
552 tag->addField( key, value, true ); \
556 WRITE( Copyright, "COPYRIGHT" );
564 * Set the tags to the file using TagLib
565 * @param p_this: the demux object
566 * @return VLC_SUCCESS if the operation success
569 static int WriteMeta( vlc_object_t *p_this )
571 meta_export_t *p_export = (meta_export_t *)p_this;
572 input_item_t *p_item = p_export->p_item;
577 msg_Err( p_this, "Can't save meta data of an empty input" );
581 #if defined(WIN32) || defined (UNDER_CE)
582 wchar_t wpath[MAX_PATH + 1];
583 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
585 wpath[MAX_PATH] = L'\0';
586 f = FileRef( wpath );
588 const char* local_name = ToLocale( p_export->psz_file );
591 f = FileRef( local_name );
592 LocaleFree( local_name );
595 if( f.isNull() || !f.tag() || f.file()->readOnly() )
597 msg_Err( p_this, "File %s can't be opened for tag writing",
598 p_export->psz_file );
602 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
604 Tag *p_tag = f.tag();
608 #define SET( a, b ) \
609 psz_meta = input_item_Get ## a( p_item ); \
612 String tmp( psz_meta, String::UTF8 ); \
613 p_tag->set##b( tmp ); \
617 // Saving all common fields
618 // If the title is empty, use the name
619 SET( TitleFbName, Title );
620 SET( Artist, Artist );
622 SET( Description, Comment );
627 psz_meta = input_item_GetDate( p_item );
628 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
631 psz_meta = input_item_GetTrackNum( p_item );
632 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
636 // Try now to write special tags
637 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
639 if( flac->ID3v2Tag() )
640 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
641 else if( flac->xiphComment() )
642 WriteMetaToXiph( flac->xiphComment(), p_item );
644 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
647 WriteMetaToAPE( mpc->APETag(), p_item );
649 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
651 if( mpeg->ID3v2Tag() )
652 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
653 else if( mpeg->APETag() )
654 WriteMetaToAPE( mpeg->APETag(), p_item );
656 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
658 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
659 WriteMetaToXiph( ogg_flac->tag(), p_item );
660 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
661 WriteMetaToXiph( ogg_speex->tag(), p_item );
662 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
663 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
665 #ifdef TAGLIB_WITH_ASF
666 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
668 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
669 WriteMetaToId3v2( riff_aiff->tag(), p_item );
670 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
671 WriteMetaToId3v2( riff_wav->tag(), p_item );
674 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
676 if( trueaudio->ID3v2Tag() )
677 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
679 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
681 if( wavpack->APETag() )
682 WriteMetaToAPE( wavpack->APETag(), p_item );
685 // Save the meta data