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);
137 const char *owner = p_ufid->owner().toCString();
138 if (!strcmp( owner, "http://musicbrainz.org" ))
140 /* ID3v2 UFID contains up to 64 bytes binary data
141 * but in our case it will be a '\0'
142 * terminated string */
144 int max_size = __MIN( p_ufid->identifier().size(), 63);
145 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
146 psz_ufid[max_size] = '\0';
147 vlc_meta_SetTrackID( p_meta, psz_ufid );
152 list = tag->frameListMap()["TXXX"];
153 for( iter = list.begin(); iter != list.end(); iter++ )
155 ID3v2::UserTextIdentificationFrame* p_txxx =
156 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
159 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
160 p_txxx->fieldList().toString().toCString( true ) );
163 // Get some more information
164 #define SET( tagName, metaName ) \
165 list = tag->frameListMap()[tagName]; \
166 if( !list.isEmpty() ) \
167 vlc_meta_Set##metaName( p_meta, \
168 (*list.begin())->toString().toCString( true ) );
170 SET( "TCOP", Copyright );
171 SET( "TENC", EncodedBy );
172 SET( "TLAN", Language );
173 SET( "TPUB", Publisher );
177 /* Preferred type of image
178 * The 21 types are defined in id3v2 standard:
179 * http://www.id3.org/id3v2.4.0-frames */
180 static const int pi_cover_score[] = {
182 5, /* 32x32 PNG image that should be used as the file icon */
183 4, /* File icon of a different size or format. */
184 20, /* Front cover image of the album. */
185 19, /* Back cover image of the album. */
186 13, /* Inside leaflet page of the album. */
187 18, /* Image from the album itself. */
188 17, /* Picture of the lead artist or soloist. */
189 16, /* Picture of the artist or performer. */
190 14, /* Picture of the conductor. */
191 15, /* Picture of the band or orchestra. */
192 9, /* Picture of the composer. */
193 8, /* Picture of the lyricist or text writer. */
194 7, /* Picture of the recording location or studio. */
195 10, /* Picture of the artists during recording. */
196 11, /* Picture of the artists during performance. */
197 6, /* Picture from a movie or video related to the track. */
198 1, /* Picture of a large, coloured fish. */
199 12, /* Illustration related to the track. */
200 3, /* Logo of the band or performer. */
201 2 /* Logo of the publisher (record company). */
205 // Try now to get embedded art
206 list = tag->frameListMap()[ "APIC" ];
210 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
211 for( iter = list.begin(); iter != list.end(); iter++ )
213 ID3v2::AttachedPictureFrame* p_apic =
214 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
217 input_attachment_t *p_attachment;
219 const char *psz_mime;
220 char *psz_name, *psz_description;
222 // Get the mime and description of the image.
223 // If the description is empty, take the type as a description
224 psz_mime = p_apic->mimeType().toCString( true );
225 if( p_apic->description().size() > 0 )
226 psz_description = strdup( p_apic->description().toCString( true ) );
229 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
230 psz_description = NULL;
233 if( !psz_description )
235 psz_name = psz_description;
237 /* some old iTunes version not only sets incorrectly the mime type
238 * or the description of the image,
239 * but also embeds incorrectly the image.
240 * Recent versions seem to behave correctly */
241 if( !strncmp( psz_mime, "PNG", 3 ) ||
242 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
244 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
245 free( psz_description );
249 const ByteVector picture = p_apic->picture();
250 const char *p_data = picture.data();
251 const unsigned i_data = picture.size();
253 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
254 psz_name, psz_mime, i_data );
256 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
257 psz_description, p_data, i_data );
259 TAB_APPEND_CAST( (input_attachment_t**),
260 p_demux_meta->i_attachments, p_demux_meta->attachments,
262 free( psz_description );
264 if( pi_cover_score[p_apic->type()] > i_score )
266 i_score = pi_cover_score[p_apic->type()];
268 if( asprintf( &psz_url, "attachment://%s",
269 p_attachment->psz_name ) == -1 )
271 vlc_meta_SetArtURL( p_meta, psz_url );
280 * Read the meta information from XiphComments
281 * @param tag: the Xiph Comment
282 * @param p_demux; the demux object
283 * @param p_demux_meta: the demuxer meta
284 * @param p_meta: the meta
286 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
288 #define SET( keyName, metaName ) \
289 StringList list = tag->fieldListMap()[keyName]; \
290 if( !list.isEmpty() ) \
291 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
293 SET( "COPYRIGHT", Copyright );
296 // Try now to get embedded art
297 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
298 StringList art_list = tag->fieldListMap()[ "COVERART" ];
300 // We get only the first covert art
301 if( mime_list.size() > 1 || art_list.size() > 1 )
302 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
304 else if( mime_list.size() == 0 || art_list.size() == 0 )
307 input_attachment_t *p_attachment;
309 const char* psz_name = "cover";
310 const char* psz_mime = mime_list[0].toCString(true);
311 const char* psz_description = "cover";
314 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
316 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
317 psz_name, psz_mime, i_data );
319 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
320 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
321 psz_description, p_data, i_data );
324 TAB_APPEND_CAST( (input_attachment_t**),
325 p_demux_meta->i_attachments, p_demux_meta->attachments,
328 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
331 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
332 static void ReadMetaFromMP4( MP4::Tag* tag, demux_t *p_demux, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
334 if( tag->itemListMap().contains("covr") )
336 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
337 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
339 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
340 psz_format, list[0].data().size() );
342 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
343 input_attachment_t *p_attachment =
344 vlc_input_attachment_New( "cover", psz_format, "cover",
345 list[0].data().data(), list[0].data().size() );
346 TAB_APPEND_CAST( (input_attachment_t**),
347 p_demux_meta->i_attachments, p_demux_meta->attachments,
349 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
355 * Get the tags from the file using TagLib
356 * @param p_this: the demux object
357 * @return VLC_SUCCESS if the operation success
359 static int ReadMeta( vlc_object_t* p_this)
361 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
362 demux_t* p_demux = p_demux_meta->p_demux;
366 p_demux_meta->p_meta = NULL;
367 if( strcmp( p_demux->psz_access, "file" ) )
370 char *psz_path = strdup( p_demux->psz_file );
374 #if defined(WIN32) || defined (UNDER_CE)
375 wchar_t wpath[MAX_PATH + 1];
376 if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
381 wpath[MAX_PATH] = L'\0';
382 f = FileRef( wpath );
384 const char* local_name = ToLocale( psz_path );
390 f = FileRef( local_name );
391 LocaleFree( local_name );
397 if( !f.tag() || f.tag()->isEmpty() )
400 p_demux_meta->p_meta = p_meta = vlc_meta_New();
405 // Read the tags from the file
406 Tag* p_tag = f.tag();
408 #define SET( tag, meta ) \
409 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
410 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
411 #define SETINT( tag, meta ) \
415 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
416 vlc_meta_Set##meta( p_meta, psz_tmp ); \
420 SET( artist, Artist );
422 SET( comment, Description );
424 SETINT( year, Date );
425 SETINT( track, TrackNum );
431 // Try now to read special tags
432 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
434 if( flac->ID3v2Tag() )
435 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
436 else if( flac->xiphComment() )
437 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
439 #if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
440 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
443 ReadMetaFromMP4( mp4->tag(), p_demux, p_demux_meta, p_meta );
446 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
449 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
451 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
453 if( mpeg->ID3v2Tag() )
454 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
455 else if( mpeg->APETag() )
456 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
458 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
460 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
461 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
462 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
463 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
464 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
465 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
467 #ifdef TAGLIB_WITH_ASF
468 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
470 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
471 ReadMetaFromId3v2( riff_aiff->tag(), p_demux, p_demux_meta, p_meta );
472 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
473 ReadMetaFromId3v2( riff_wav->tag(), p_demux, p_demux_meta, p_meta );
476 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
478 if( trueaudio->ID3v2Tag() )
479 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
481 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
483 if( wavpack->APETag() )
484 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
493 * Write meta information to APE tags
494 * @param tag: the APE tag
495 * @param p_item: the input item
497 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
500 #define WRITE( metaName, keyName ) \
501 psz_meta = input_item_Get##metaName( p_item ); \
504 String key( keyName, String::UTF8 ); \
505 String value( psz_meta, String::UTF8 ); \
506 tag->addValue( key, value, true ); \
510 WRITE( Copyright, "COPYRIGHT" );
511 WRITE( Language, "LANGUAGE" );
512 WRITE( Publisher, "PUBLISHER" );
520 * Write meta information to id3v2 tags
521 * @param tag: the id3v2 tag
522 * @param p_input: the input item
524 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
527 #define WRITE( metaName, tagName ) \
528 psz_meta = input_item_Get##metaName( p_item ); \
531 ByteVector p_byte( tagName, 4 ); \
532 tag->removeFrames( p_byte ); \
533 ID3v2::TextIdentificationFrame* p_frame = \
534 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
535 p_frame->setText( psz_meta ); \
536 tag->addFrame( p_frame ); \
540 WRITE( Copyright, "TCOP" );
541 WRITE( EncodedBy, "TENC" );
542 WRITE( Language, "TLAN" );
543 WRITE( Publisher, "TPUB" );
551 * Write the meta information to XiphComments
552 * @param tag: the Xiph Comment
553 * @param p_input: the input item
555 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
558 #define WRITE( metaName, keyName ) \
559 psz_meta = input_item_Get##metaName( p_item ); \
562 String key( keyName, String::UTF8 ); \
563 String value( psz_meta, String::UTF8 ); \
564 tag->addField( key, value, true ); \
568 WRITE( Copyright, "COPYRIGHT" );
576 * Set the tags to the file using TagLib
577 * @param p_this: the demux object
578 * @return VLC_SUCCESS if the operation success
581 static int WriteMeta( vlc_object_t *p_this )
583 meta_export_t *p_export = (meta_export_t *)p_this;
584 input_item_t *p_item = p_export->p_item;
589 msg_Err( p_this, "Can't save meta data of an empty input" );
593 #if defined(WIN32) || defined (UNDER_CE)
594 wchar_t wpath[MAX_PATH + 1];
595 if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
597 wpath[MAX_PATH] = L'\0';
598 f = FileRef( wpath );
600 const char* local_name = ToLocale( p_export->psz_file );
603 f = FileRef( local_name );
604 LocaleFree( local_name );
607 if( f.isNull() || !f.tag() || f.file()->readOnly() )
609 msg_Err( p_this, "File %s can't be opened for tag writing",
610 p_export->psz_file );
614 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
616 Tag *p_tag = f.tag();
620 #define SET( a, b ) \
621 psz_meta = input_item_Get ## a( p_item ); \
624 String tmp( psz_meta, String::UTF8 ); \
625 p_tag->set##b( tmp ); \
629 // Saving all common fields
630 // If the title is empty, use the name
631 SET( TitleFbName, Title );
632 SET( Artist, Artist );
634 SET( Description, Comment );
639 psz_meta = input_item_GetDate( p_item );
640 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
643 psz_meta = input_item_GetTrackNum( p_item );
644 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
648 // Try now to write special tags
649 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
651 if( flac->ID3v2Tag() )
652 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
653 else if( flac->xiphComment() )
654 WriteMetaToXiph( flac->xiphComment(), p_item );
656 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
659 WriteMetaToAPE( mpc->APETag(), p_item );
661 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
663 if( mpeg->ID3v2Tag() )
664 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
665 else if( mpeg->APETag() )
666 WriteMetaToAPE( mpeg->APETag(), p_item );
668 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
670 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
671 WriteMetaToXiph( ogg_flac->tag(), p_item );
672 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
673 WriteMetaToXiph( ogg_speex->tag(), p_item );
674 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
675 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
677 #ifdef TAGLIB_WITH_ASF
678 else if( RIFF::File* riff = dynamic_cast<RIFF::File*>(f.file()) )
680 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
681 WriteMetaToId3v2( riff_aiff->tag(), p_item );
682 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
683 WriteMetaToId3v2( riff_wav->tag(), p_item );
686 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
688 if( trueaudio->ID3v2Tag() )
689 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
691 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
693 if( wavpack->APETag() )
694 WriteMetaToAPE( wavpack->APETag(), p_item );
697 // Save the meta data