1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Rafaël Carré <funman@videolanorg>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_playlist.h>
33 #include <vlc_demux.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
47 #include <xiphcomment.h>
54 #include <oggflacfile.h>
55 #include <speexfile.h>
56 #include <vorbisfile.h>
57 #include <trueaudiofile.h>
58 #include <wavpackfile.h>
61 #include <textidentificationframe.h>
62 #include <tbytevector.h>
63 #include <attachedpictureframe.h>
64 //#include <oggflacfile.h> /* ogg flac files aren't auto-casted by TagLib */
65 #include <flacproperties.h>
66 #include <vorbisfile.h>
67 #include <vorbisproperties.h>
68 #include <uniquefileidentifierframe.h>
69 #include <textidentificationframe.h>
70 //#include <relativevolumeframe.h> /* parse the tags without TagLib helpers? */
72 static int ReadMeta ( vlc_object_t * );
73 static int DownloadArt ( vlc_object_t * );
74 static int WriteMeta ( vlc_object_t * );
77 set_capability( "meta reader", 1000 )
78 set_callbacks( ReadMeta, NULL )
80 set_capability( "art downloader", 50 )
81 set_callbacks( DownloadArt, NULL )
83 set_capability( "meta writer", 50 )
84 set_callbacks( WriteMeta, NULL )
87 using namespace TagLib;
91 /* Try detecting embedded art */
92 static void DetectImage( FileRef f, demux_t *p_demux )
94 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
95 vlc_meta_t *p_meta = p_demux_meta->p_meta;
98 /* Preferred type of image
99 * The 21 types are defined in id3v2 standard:
100 * http://www.id3.org/id3v2.4.0-frames */
101 static const int pi_cover_score[] = {
103 5, /* 32x32 PNG image that should be used as the file icon */
104 4, /* File icon of a different size or format. */
105 20, /* Front cover image of the album. */
106 19, /* Back cover image of the album. */
107 13, /* Inside leaflet page of the album. */
108 18, /* Image from the album itself. */
109 17, /* Picture of the lead artist or soloist. */
110 16, /* Picture of the artist or performer. */
111 14, /* Picture of the conductor. */
112 15, /* Picture of the band or orchestra. */
113 9, /* Picture of the composer. */
114 8, /* Picture of the lyricist or text writer. */
115 7, /* Picture of the recording location or studio. */
116 10, /* Picture of the artists during recording. */
117 11, /* Picture of the artists during performance. */
118 6, /* Picture from a movie or video related to the track. */
119 1, /* Picture of a large, coloured fish. */
120 12, /* Illustration related to the track. */
121 3, /* Logo of the band or performer. */
122 2 /* Logo of the publisher (record company). */
125 if( MPEG::File *mpeg = dynamic_cast<MPEG::File *>(f.file() ) )
127 ID3v2::Tag *p_tag = mpeg->ID3v2Tag();
130 ID3v2::FrameList list = p_tag->frameListMap()[ "APIC" ];
133 ID3v2::AttachedPictureFrame *p_apic;
135 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
136 for( ID3v2::FrameList::Iterator iter = list.begin();
137 iter != list.end(); iter++ )
139 p_apic = dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
140 input_attachment_t *p_attachment;
142 const char *psz_name, *psz_mime, *psz_description;
143 ByteVector p_data_taglib; const char *p_data; int i_data;
145 psz_mime = p_apic->mimeType().toCString(true);
146 psz_description = psz_name = p_apic->description().toCString(true);
148 /* some old iTunes version not only sets incorrectly the mime type
149 * or the description of the image,
150 * but also embeds incorrectly the image.
151 * Recent versions seem to behave correctly */
152 if( !strncmp( psz_mime, "PNG", 3 ) ||
153 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
156 "%s: Invalid picture embedded by broken iTunes version, "
157 "you really shouldn't use this crappy software.",
158 (const char *)f.file()->name() );
162 p_data_taglib = p_apic->picture();
163 p_data = p_data_taglib.data();
164 i_data = p_data_taglib.size();
166 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
167 psz_name, psz_mime, i_data );
169 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
170 psz_description, p_data, i_data );
171 TAB_APPEND_CAST( (input_attachment_t**),
172 p_demux_meta->i_attachments, p_demux_meta->attachments,
175 if( pi_cover_score[p_apic->type()] > i_score )
177 i_score = pi_cover_score[p_apic->type()];
179 if( asprintf( &psz_url, "attachment://%s",
180 p_attachment->psz_name ) == -1 )
182 vlc_meta_SetArtURL( p_meta, psz_url );
188 if( Ogg::Vorbis::File *oggv = dynamic_cast<Ogg::Vorbis::File *>(f.file() ) )
190 Ogg::XiphComment *p_tag = oggv->tag();
194 StringList mime_list = p_tag->fieldListMap()[ "COVERARTMIME" ];
195 StringList art_list = p_tag->fieldListMap()[ "COVERART" ];
197 /* we support only one cover in ogg/vorbis */
198 if( mime_list.size() != 1 || art_list.size() != 1 )
201 input_attachment_t *p_attachment;
203 const char *psz_name, *psz_mime, *psz_description;
208 psz_mime = mime_list[0].toCString(true);
209 psz_description = "cover";
211 i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
213 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
214 psz_name, psz_mime, i_data );
216 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
217 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
218 psz_description, p_data, i_data );
221 TAB_APPEND_CAST( (input_attachment_t**),
222 p_demux_meta->i_attachments, p_demux_meta->attachments,
225 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
229 //flac embedded images are extracted in the flac demuxer
230 else if( FLAC::File *flac =
231 dynamic_cast<FLAC::File *>(f.file() ) )
233 p_tag = flac->ID3v2Tag();
236 ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
239 vlc_meta_SetArtURL( p_meta, "APIC" );
243 /* TagLib doesn't support MP4 file yet */
244 else if( MP4::File *mp4 =
245 dynamic_cast<MP4::File *>( f.file() ) )
248 dynamic_cast<MP4::Tag *>( mp4->tag() );
249 if( mp4tag && mp4tag->cover().size() )
250 vlc_meta_SetArtURL( p_meta, "MP4C" );
258 * Read meta informations from APE tags
259 * @param tag: the APE tag
260 * @param p_meta: the meta
261 * @return VLC_SUCCESS if everything goes ok
263 static int ReadMetaFromAPE( APE::Tag* tag, vlc_meta_t* p_meta )
271 * read meta information from id3v2 tags
272 * @param tag: the id3v2 tag
273 * @param p_meta: the meta
274 * @return VLC_SUCCESS if everything goes ok
276 static int ReadMetaFromId2v2( ID3v2::Tag* tag, vlc_meta_t* p_meta )
278 // Get the unique file identifier
279 ID3v2::FrameList list = tag->frameListMap()["UFID"];
280 ID3v2::FrameList::Iterator iter;
281 for( iter = list.begin(); iter != list.end(); iter++ )
283 ID3v2::UniqueFileIdentifierFrame* p_ufid =
284 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
285 const char *owner = p_ufid->owner().toCString();
286 if (!strcmp( owner, "http://musicbrainz.org" ))
288 /* ID3v2 UFID contains up to 64 bytes binary data
289 * but in our case it will be a '\0'
290 * terminated string */
294 ( j < p_ufid->identifier().size() ) )
295 psz_ufid[j] = p_ufid->identifier()[j++];
297 vlc_meta_SetTrackID( p_meta, psz_ufid );
302 list = tag->frameListMap()["TXXX"];
303 for( iter = list.begin(); iter != list.end(); iter++ )
305 ID3v2::UserTextIdentificationFrame* p_txxx =
306 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
307 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
308 p_txxx->fieldList().toString().toCString( true ) );
311 // Get some more informations
312 #define SET( tagName, metaName ) \
313 list = tag->frameListMap()[tagName]; \
314 if( !list.isEmpty() ) \
315 vlc_meta_Set##metaName( p_meta, \
316 (*list.begin())->toString().toCString( true ) );
318 SET( "TCOP", Copyright );
319 SET( "TENC", EncodedBy );
320 SET( "TLAN", Language );
321 SET( "TPUB", Publisher );
330 * Read the meta informations from XiphComments
331 * @param tag: the Xiph Comment
332 * @param p_meta: the meta
333 * @return VLC_SUCCESS if everything goes ok
335 static int ReadMetaFromXiph( Ogg::XiphComment* tag, vlc_meta_t* p_meta )
343 * Get the tags from the file using TagLib
344 * @param p_this: the demux object
345 * @return VLC_SUCCESS if the operation success
347 static int ReadMeta( vlc_object_t* p_this)
349 demux_t* p_demux = (demux_t*)p_this;
350 demux_meta_t* p_demux_meta = (demux_meta_t*)p_demux->p_private;
354 p_demux_meta->p_meta = NULL;
355 const char* local_name = ToLocale( p_demux->psz_path );
358 f = FileRef( local_name );
359 LocaleFree( local_name );
363 if( !f.tag() || f.tag()->isEmpty() )
366 p_demux_meta->p_meta = p_meta = vlc_meta_New();
371 // Read the tags from the file
372 Tag* p_tag = f.tag();
374 #define SET( meta, tag ) \
375 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
376 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
377 #define SETINT( meta, tag ) \
381 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
382 vlc_meta_Set##meta( p_meta, psz_tmp ); \
386 SET( Artist, artist );
388 SET( Description, comment );
390 SETINT( Date, year );
391 SETINT( Tracknum, track );
397 // Try now to read special tags
398 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
400 if( flac->ID3v2Tag() )
401 ReadMetaFromId2v2( flac->ID3v2Tag(), p_meta );
402 else if( flac->xiphComment() )
403 ReadMetaFromXiph( flac->xiphComment(), p_meta );
405 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
408 ReadMetaFromAPE( mpc->APETag(), p_meta );
410 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
412 if( mpeg->ID3v2Tag() )
413 ReadMetaFromId2v2( mpeg->ID3v2Tag(), p_meta );
414 else if( mpeg->APETag() )
415 ReadMetaFromAPE( mpeg->APETag(), p_meta );
417 else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
419 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
420 ReadMetaFromXiph( ogg_flac->tag(), p_meta );
421 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
422 ReadMetaFromXiph( ogg_speex->tag(), p_meta );
423 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
424 ReadMetaFromXiph( ogg_vorbis->tag(), p_meta );
426 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
428 if( trueaudio->ID3v2Tag() )
429 ReadMetaFromId2v2( trueaudio->ID3v2Tag(), p_meta );
431 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
433 if( wavpack->APETag() )
434 ReadMetaFromAPE( wavpack->APETag(), p_meta );
437 // Try now to find a image
438 DetectImage( f, p_demux );
444 static int WriteMeta( vlc_object_t *p_this )
446 playlist_t *p_playlist = (playlist_t *)p_this;
447 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
448 input_item_t *p_item = p_export->p_item;
452 msg_Err( p_this, "Can't save meta data of an empty input" );
456 FileRef f( p_export->psz_file );
457 if( f.isNull() || !f.tag() || f.file()->readOnly() )
459 msg_Err( p_this, "File %s can't be opened for tag writing\n",
460 p_export->psz_file );
464 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
466 Tag *p_tag = f.tag();
472 String *psz_##a = new String( b, \
474 p_tag->set##a( *psz_##a ); \
479 psz_meta = input_item_GetArtist( p_item );
480 SET( Artist, psz_meta );
483 psz_meta = input_item_GetTitle( p_item );
484 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
485 String *psz_title = new String( psz_meta,
487 p_tag->setTitle( *psz_title );
491 psz_meta = input_item_GetAlbum( p_item );
492 SET( Album, psz_meta );
495 psz_meta = input_item_GetGenre( p_item );
496 SET( Genre, psz_meta );
501 psz_meta = input_item_GetDate( p_item );
502 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
505 psz_meta = input_item_GetTrackNum( p_item );
506 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
509 if( ID3v2::Tag *p_id3tag =
510 dynamic_cast<ID3v2::Tag *>(p_tag) )
512 #define WRITE( foo, bar ) \
513 psz_meta = input_item_Get##foo( p_item ); \
516 ByteVector p_byte( bar, 4 ); \
517 ID3v2::TextIdentificationFrame p_frame( p_byte ); \
518 p_frame.setText( psz_meta ); \
519 p_id3tag->addFrame( &p_frame ); \
523 WRITE( Publisher, "TPUB" );
524 WRITE( Copyright, "TCOP" );
525 WRITE( EncodedBy, "TENC" );
526 WRITE( Language, "TLAN" );
535 static int DownloadArt( vlc_object_t *p_this )
537 /* We need to be passed the file name
538 * Fetch the thing from the file, save it to the cache folder