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>
60 #include <speexfile.h>
61 #include <trueaudiofile.h>
62 #include <vorbisfile.h>
63 #include <wavpackfile.h>
65 #include <attachedpictureframe.h>
66 #include <textidentificationframe.h>
67 #include <uniquefileidentifierframe.h>
71 static int ReadMeta ( vlc_object_t * );
72 static int WriteMeta ( vlc_object_t * );
75 set_capability( "meta reader", 1000 )
76 set_callbacks( ReadMeta, NULL )
78 set_capability( "meta writer", 50 )
79 set_callbacks( WriteMeta, NULL )
82 using namespace TagLib;
86 * Read meta informations from APE tags
87 * @param tag: the APE tag
88 * @param p_demux; the demux object
89 * @param p_demux_meta: the demuxer meta
90 * @param p_meta: the meta
92 static void ReadMetaFromAPE( APE::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
95 #define SET( keyName, metaName ) \
96 item = tag->itemListMap()[keyName]; \
97 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
99 SET( "COPYRIGHT", Copyright );
100 SET( "LANGUAGE", Language );
101 SET( "PUBLISHER", Publisher );
109 * Read meta information from id3v2 tags
110 * @param tag: the id3v2 tag
111 * @param p_demux; the demux object
112 * @param p_demux_meta: the demuxer meta
113 * @param p_meta: the meta
115 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
117 // Get the unique file identifier
118 ID3v2::FrameList list = tag->frameListMap()["UFID"];
119 ID3v2::FrameList::Iterator iter;
120 for( iter = list.begin(); iter != list.end(); iter++ )
122 ID3v2::UniqueFileIdentifierFrame* p_ufid =
123 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
124 const char *owner = p_ufid->owner().toCString();
125 if (!strcmp( owner, "http://musicbrainz.org" ))
127 /* ID3v2 UFID contains up to 64 bytes binary data
128 * but in our case it will be a '\0'
129 * terminated string */
131 int max_size = __MIN( p_ufid->identifier().size(), 63);
132 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
133 psz_ufid[max_size] = '\0';
134 vlc_meta_SetTrackID( p_meta, psz_ufid );
139 list = tag->frameListMap()["TXXX"];
140 for( iter = list.begin(); iter != list.end(); iter++ )
142 ID3v2::UserTextIdentificationFrame* p_txxx =
143 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
144 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
145 p_txxx->fieldList().toString().toCString( true ) );
148 // Get some more informations
149 #define SET( tagName, metaName ) \
150 list = tag->frameListMap()[tagName]; \
151 if( !list.isEmpty() ) \
152 vlc_meta_Set##metaName( p_meta, \
153 (*list.begin())->toString().toCString( true ) );
155 SET( "TCOP", Copyright );
156 SET( "TENC", EncodedBy );
157 SET( "TLAN", Language );
158 SET( "TPUB", Publisher );
162 /* Preferred type of image
163 * The 21 types are defined in id3v2 standard:
164 * http://www.id3.org/id3v2.4.0-frames */
165 static const int pi_cover_score[] = {
167 5, /* 32x32 PNG image that should be used as the file icon */
168 4, /* File icon of a different size or format. */
169 20, /* Front cover image of the album. */
170 19, /* Back cover image of the album. */
171 13, /* Inside leaflet page of the album. */
172 18, /* Image from the album itself. */
173 17, /* Picture of the lead artist or soloist. */
174 16, /* Picture of the artist or performer. */
175 14, /* Picture of the conductor. */
176 15, /* Picture of the band or orchestra. */
177 9, /* Picture of the composer. */
178 8, /* Picture of the lyricist or text writer. */
179 7, /* Picture of the recording location or studio. */
180 10, /* Picture of the artists during recording. */
181 11, /* Picture of the artists during performance. */
182 6, /* Picture from a movie or video related to the track. */
183 1, /* Picture of a large, coloured fish. */
184 12, /* Illustration related to the track. */
185 3, /* Logo of the band or performer. */
186 2 /* Logo of the publisher (record company). */
190 // Try now to get embedded art
191 list = tag->frameListMap()[ "APIC" ];
195 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
196 for( iter = list.begin(); iter != list.end(); iter++ )
198 ID3v2::AttachedPictureFrame* p_apic =
199 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
200 input_attachment_t *p_attachment;
202 const char *psz_mime;
203 char *psz_name, *psz_description;
205 // Get the mime and description of the image.
206 // If the description is empty, take the type as a description
207 psz_mime = p_apic->mimeType().toCString( true );
208 if( p_apic->description().size() > 0 )
209 psz_description = strdup( p_apic->description().toCString( true ) );
212 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
213 psz_description = NULL;
216 if( !psz_description )
218 psz_name = psz_description;
220 /* some old iTunes version not only sets incorrectly the mime type
221 * or the description of the image,
222 * but also embeds incorrectly the image.
223 * Recent versions seem to behave correctly */
224 if( !strncmp( psz_mime, "PNG", 3 ) ||
225 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
227 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
228 free( psz_description );
232 const ByteVector picture = p_apic->picture();
233 const char *p_data = picture.data();
234 const unsigned i_data = picture.size();
236 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
237 psz_name, psz_mime, i_data );
239 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
240 psz_description, p_data, i_data );
242 TAB_APPEND_CAST( (input_attachment_t**),
243 p_demux_meta->i_attachments, p_demux_meta->attachments,
245 free( psz_description );
247 if( pi_cover_score[p_apic->type()] > i_score )
249 i_score = pi_cover_score[p_apic->type()];
251 if( asprintf( &psz_url, "attachment://%s",
252 p_attachment->psz_name ) == -1 )
254 vlc_meta_SetArtURL( p_meta, psz_url );
263 * Read the meta informations from XiphComments
264 * @param tag: the Xiph Comment
265 * @param p_demux; the demux object
266 * @param p_demux_meta: the demuxer meta
267 * @param p_meta: the meta
269 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_t* p_demux, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
271 #define SET( keyName, metaName ) \
272 StringList list = tag->fieldListMap()[keyName]; \
273 if( !list.isEmpty() ) \
274 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
276 SET( "COPYRIGHT", Copyright );
279 // Try now to get embedded art
280 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
281 StringList art_list = tag->fieldListMap()[ "COVERART" ];
283 // We get only the first covert art
284 if( mime_list.size() > 1 || art_list.size() > 1 )
285 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
287 else if( mime_list.size() == 0 || art_list.size() == 0 )
290 input_attachment_t *p_attachment;
292 const char* psz_name = "cover";
293 const char* psz_mime = mime_list[0].toCString(true);
294 const char* psz_description = "cover";
297 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
299 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
300 psz_name, psz_mime, i_data );
302 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
303 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
304 psz_description, p_data, i_data );
307 TAB_APPEND_CAST( (input_attachment_t**),
308 p_demux_meta->i_attachments, p_demux_meta->attachments,
311 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
317 * Get the tags from the file using TagLib
318 * @param p_this: the demux object
319 * @return VLC_SUCCESS if the operation success
321 static int ReadMeta( vlc_object_t* p_this)
323 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
324 demux_t* p_demux = p_demux_meta->p_demux;
328 p_demux_meta->p_meta = NULL;
331 #if defined(WIN32) || defined (UNDER_CE)
332 wchar_t wpath[MAX_PATH + 1];
333 if( !MultiByteToWideChar( CP_UTF8, 0, p_demux->psz_path, -1, wpath, MAX_PATH) )
335 wpath[MAX_PATH] = L'\0';
336 f = FileRef( wpath );
338 const char* local_name = ToLocale( p_demux->psz_path );
341 f = FileRef( local_name );
342 LocaleFree( local_name );
347 if( !f.tag() || f.tag()->isEmpty() )
350 p_demux_meta->p_meta = p_meta = vlc_meta_New();
355 // Read the tags from the file
356 Tag* p_tag = f.tag();
358 #define SET( tag, meta ) \
359 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
360 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
361 #define SETINT( tag, meta ) \
365 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
366 vlc_meta_Set##meta( p_meta, psz_tmp ); \
370 SET( artist, Artist );
372 SET( comment, Description );
374 SETINT( year, Date );
375 SETINT( track, TrackNum );
381 // Try now to read special tags
382 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
384 if( flac->ID3v2Tag() )
385 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
386 else if( flac->xiphComment() )
387 ReadMetaFromXiph( flac->xiphComment(), p_demux, p_demux_meta, p_meta );
389 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
392 ReadMetaFromAPE( mpc->APETag(), p_demux, p_demux_meta, p_meta );
394 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
396 if( mpeg->ID3v2Tag() )
397 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
398 else if( mpeg->APETag() )
399 ReadMetaFromAPE( mpeg->APETag(), p_demux, p_demux_meta, p_meta );
401 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
403 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
404 ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
405 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
406 ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
407 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
408 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
410 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
412 if( trueaudio->ID3v2Tag() )
413 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
415 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
417 if( wavpack->APETag() )
418 ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
427 * Write meta informations to APE tags
428 * @param tag: the APE tag
429 * @param p_item: the input item
431 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
434 #define WRITE( metaName, keyName ) \
435 psz_meta = input_item_Get##metaName( p_item ); \
438 String key( keyName, String::UTF8 ); \
439 String value( psz_meta, String::UTF8 ); \
440 tag->addValue( key, value, true ); \
444 WRITE( Copyright, "COPYRIGHT" );
445 WRITE( Language, "LANGUAGE" );
446 WRITE( Publisher, "PUBLISHER" );
454 * Write meta information to id3v2 tags
455 * @param tag: the id3v2 tag
456 * @param p_input: the input item
458 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
461 #define WRITE( metaName, tagName ) \
462 psz_meta = input_item_Get##metaName( p_item ); \
465 ByteVector p_byte( tagName, 4 ); \
466 tag->removeFrames( p_byte ); \
467 ID3v2::TextIdentificationFrame* p_frame = \
468 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
469 p_frame->setText( psz_meta ); \
470 tag->addFrame( p_frame ); \
474 WRITE( Copyright, "TCOP" );
475 WRITE( EncodedBy, "TENC" );
476 WRITE( Language, "TLAN" );
477 WRITE( Publisher, "TPUB" );
485 * Write the meta informations to XiphComments
486 * @param tag: the Xiph Comment
487 * @param p_input: the input item
489 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
492 #define WRITE( metaName, keyName ) \
493 psz_meta = input_item_Get##metaName( p_item ); \
496 String key( keyName, String::UTF8 ); \
497 String value( psz_meta, String::UTF8 ); \
498 tag->addField( key, value, true ); \
502 WRITE( Copyright, "COPYRIGHT" );
510 * Set the tags to the file using TagLib
511 * @param p_this: the demux object
512 * @return VLC_SUCCESS if the operation success
515 static int WriteMeta( vlc_object_t *p_this )
517 meta_export_t *p_export = (meta_export_t *)p_this;
518 input_item_t *p_item = p_export->p_item;
523 msg_Err( p_this, "Can't save meta data of an empty input" );
527 char *export_file = strdup(p_export->psz_file);
528 if( decode_URI( export_file ) == NULL )
534 #if defined(WIN32) || defined (UNDER_CE)
535 wchar_t wpath[MAX_PATH + 1];
536 if( !MultiByteToWideChar( CP_UTF8, 0, export_file , -1, wpath, MAX_PATH) )
538 wpath[MAX_PATH] = L'\0';
539 f = FileRef( wpath );
541 const char* local_name = ToLocale( export_file );
547 f = FileRef( local_name );
548 LocaleFree( local_name );
551 if( f.isNull() || !f.tag() || f.file()->readOnly() )
553 msg_Err( p_this, "File %s can't be opened for tag writing",
559 msg_Dbg( p_this, "Writing metadata for %s", export_file );
562 Tag *p_tag = f.tag();
566 #define SET( a, b ) \
569 String* psz_tmp = new String( b, String::UTF8 ); \
570 p_tag->set##a( *psz_tmp ); \
574 // Saving all common fields
575 // If the title is empty, use the name
576 psz_meta = input_item_GetTitleFbName( p_item );
577 SET( Title, psz_meta );
580 psz_meta = input_item_GetArtist( p_item );
581 SET( Artist, psz_meta );
584 psz_meta = input_item_GetAlbum( p_item );
585 SET( Album, psz_meta );
588 psz_meta = input_item_GetDescription( p_item );
589 SET( Comment, psz_meta );
592 psz_meta = input_item_GetGenre( p_item );
593 SET( Genre, psz_meta );
598 psz_meta = input_item_GetDate( p_item );
599 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
602 psz_meta = input_item_GetTrackNum( p_item );
603 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
607 // Try now to write special tags
608 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
610 if( flac->ID3v2Tag() )
611 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
612 else if( flac->xiphComment() )
613 WriteMetaToXiph( flac->xiphComment(), p_item );
615 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
618 WriteMetaToAPE( mpc->APETag(), p_item );
620 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
622 if( mpeg->ID3v2Tag() )
623 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
624 else if( mpeg->APETag() )
625 WriteMetaToAPE( mpeg->APETag(), p_item );
627 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
629 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
630 WriteMetaToXiph( ogg_flac->tag(), p_item );
631 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
632 WriteMetaToXiph( ogg_speex->tag(), p_item );
633 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
634 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
636 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
638 if( trueaudio->ID3v2Tag() )
639 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
641 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
643 if( wavpack->APETag() )
644 WriteMetaToAPE( wavpack->APETag(), p_item );
647 // Save the meta data