1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2006 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 *****************************************************************************/
26 #include <vlc_playlist.h>
28 #include <vlc_demux.h>
34 #include <textidentificationframe.h>
35 #include <tbytevector.h>
38 #include <attachedpictureframe.h>
40 #include <oggflacfile.h>
43 #include <flacproperties.h>
44 #include <vorbisfile.h>
45 #include <vorbisproperties.h>
46 #include <uniquefileidentifierframe.h>
47 #include <textidentificationframe.h>
48 #if 0 /* parse the tags without taglib helpers? */
49 #include <relativevolumeframe.h>
52 static int ReadMeta ( vlc_object_t * );
53 static int DownloadArt ( vlc_object_t * );
54 static int WriteMeta ( vlc_object_t * );
57 set_capability( "meta reader", 1000 );
58 set_callbacks( ReadMeta, NULL );
60 set_capability( "art downloader", 50 );
61 set_callbacks( DownloadArt, NULL );
63 set_capability( "meta writer", 50 );
64 set_callbacks( WriteMeta, NULL );
67 /* Try detecting embedded art */
68 static void DetectImage( TagLib::FileRef f, demux_t *p_demux )
70 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
71 vlc_meta_t *p_meta = p_demux_meta->p_meta;
72 TagLib::ID3v2::Tag *p_tag;
75 /* Preferred type of image
76 * The 21 types are defined in id3v2 standard:
77 * http://www.id3.org/id3v2.4.0-frames */
78 static const int pi_cover_score[] = {
80 5, /* 32x32 PNG image that should be used as the file icon */
81 4, /* File icon of a different size or format. */
82 20, /* Front cover image of the album. */
83 19, /* Back cover image of the album. */
84 13, /* Inside leaflet page of the album. */
85 18, /* Image from the album itself. */
86 17, /* Picture of the lead artist or soloist. */
87 16, /* Picture of the artist or performer. */
88 14, /* Picture of the conductor. */
89 15, /* Picture of the band or orchestra. */
90 9, /* Picture of the composer. */
91 8, /* Picture of the lyricist or text writer. */
92 7, /* Picture of the recording location or studio. */
93 10, /* Picture of the artists during recording. */
94 11, /* Picture of the artists during performance. */
95 6, /* Picture from a movie or video related to the track. */
96 1, /* Picture of a large, coloured fish. */
97 12, /* Illustration related to the track. */
98 3, /* Logo of the band or performer. */
99 2 /* Logo of the publisher (record company). */
102 if( TagLib::MPEG::File *mpeg =
103 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
105 p_tag = mpeg->ID3v2Tag();
108 TagLib::ID3v2::FrameList list = p_tag->frameListMap()[ "APIC" ];
111 input_thread_t *p_input = (input_thread_t *)
112 vlc_object_find( p_demux,VLC_OBJECT_INPUT, FIND_PARENT );
115 input_item_t *p_item = input_GetItem( p_input );
116 TagLib::ID3v2::AttachedPictureFrame *p_apic;
118 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
119 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
120 iter != list.end(); iter++ )
122 p_apic = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(*iter);
123 input_attachment_t *p_attachment;
125 const char *psz_name, *psz_mime, *psz_description;
126 TagLib::ByteVector p_data_taglib; const char *p_data; int i_data;
128 psz_mime = p_apic->mimeType().toCString(true);
129 psz_description = p_apic->description().toCString(true);
130 psz_name = psz_description;
132 p_data_taglib = p_apic->picture();
133 p_data = p_data_taglib.data();
134 i_data = p_data_taglib.size();
136 msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
137 psz_name, psz_mime, i_data );
139 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
140 psz_description, p_data, i_data );
141 TAB_APPEND_CAST( (input_attachment_t**),
142 p_demux_meta->i_attachments, p_demux_meta->attachments,
145 if( pi_cover_score[p_apic->type()] > i_score )
147 i_score = pi_cover_score[p_apic->type()];
149 if( asprintf( &psz_url, "attachment://%s",
150 p_attachment->psz_name ) == -1 )
152 vlc_object_release( p_input );
155 vlc_meta_SetArtURL( p_meta, psz_url );
159 vlc_object_release( p_input );
162 //flac embedded images are extracted in the flac demuxer
163 else if( TagLib::FLAC::File *flac =
164 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
166 p_tag = flac->ID3v2Tag();
169 TagLib::ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
172 vlc_meta_SetArtURL( p_meta, "APIC" );
176 /* This needs special additions to taglib */
177 * else if( TagLib::MP4::File *mp4 =
178 dynamic_cast<TagLib::MP4::File *>( f.file() ) )
180 TagLib::MP4::Tag *mp4tag =
181 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
182 if( mp4tag && mp4tag->cover().size() )
183 vlc_meta_SetArtURL( p_meta, "MP4C" );
188 static int ReadMeta( vlc_object_t *p_this )
190 demux_t *p_demux = (demux_t *)p_this;
191 demux_meta_t *p_demux_meta = (demux_meta_t*)p_demux->p_private;
192 vlc_meta_t *p_meta = p_demux_meta->p_meta;
194 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
195 p_demux_meta->p_meta = NULL;
197 TagLib::FileRef f( p_demux->psz_path );
201 if ( !f.tag() || f.tag()->isEmpty() )
204 p_demux_meta->p_meta = p_meta = vlc_meta_New();
205 TagLib::Tag *p_tag = f.tag();
207 if( TagLib::MPEG::File *p_mpeg =
208 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
210 if( p_mpeg->ID3v2Tag() )
212 TagLib::ID3v2::Tag *p_tag = p_mpeg->ID3v2Tag();
213 TagLib::ID3v2::FrameList list = p_tag->frameListMap()["UFID"];
214 TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
215 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
216 iter != list.end(); iter++ )
218 p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
219 const char *owner = p_ufid->owner().toCString();
220 if (!strcmp( owner, "http://musicbrainz.org" ))
222 /* ID3v2 UFID contains up to 64 bytes binary data
223 * but in our case it will be a '\0'
224 * terminated string */
225 char *psz_ufid = (char*) malloc( 64 );
228 ( j < p_ufid->identifier().size() ) )
229 psz_ufid[j] = p_ufid->identifier()[j++];
231 vlc_meta_SetTrackID( p_meta, psz_ufid );
236 list = p_tag->frameListMap()["TXXX"];
237 TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
238 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
239 iter != list.end(); iter++ )
241 p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
242 const char *psz_desc= p_txxx->description().toCString();
243 vlc_meta_AddExtra( p_meta, psz_desc,
244 p_txxx->fieldList().toString().toCString());
247 list = p_tag->frameListMap()["RVA2"];
248 TagLib::ID3v2::RelativeVolumeFrame* p_rva2;
249 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
250 iter != list.end(); iter++ )
252 p_rva2 = dynamic_cast<TagLib::ID3v2::RelativeVolumeFrame*>(*iter);
253 /* TODO: process rva2 frames */
256 list = p_tag->frameList();
257 TagLib::ID3v2::Frame* p_t;
259 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
260 iter != list.end(); iter++ )
262 p_t = dynamic_cast<TagLib::ID3v2::Frame*> (*iter);
263 memcpy( psz_tag, p_t->frameID().data(), 4);
265 #define SET( foo, bar ) if( !strncmp( psz_tag, foo, 4 ) ) \
266 vlc_meta_Set##bar( p_meta, p_t->toString().toCString(true))
267 SET( "TPUB", Publisher );
268 SET( "TCOP", Copyright );
269 SET( "TENC", EncodedBy );
270 SET( "TLAN", Language );
271 //SET( "POPM", Rating ); /* rating needs special handling in id3v2 */
272 //if( !strncmp( psz_tag, "RVA2", 4 ) )
279 else if( TagLib::Ogg::Vorbis::File *p_ogg_v =
280 dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file() ) )
282 int i_ogg_v_length = p_ogg_v->audioProperties()->length();
284 input_thread_t *p_input = (input_thread_t *)
285 vlc_object_find( p_demux,VLC_OBJECT_INPUT, FIND_PARENT );
288 input_item_t *p_item = input_GetItem( p_input );
290 input_item_SetDuration( p_item,
291 (mtime_t) i_ogg_v_length * 1000000 );
292 vlc_object_release( p_input );
296 #if 0 /* at this moment, taglib is unable to detect ogg/flac files
297 * becauses type detection is based on file extension:
302 else if( TagLib::Ogg::FLAC::File *p_ogg_f =
303 dynamic_cast<TagLib::Ogg::FLAC::File *>(f.file() ) )
305 long i_ogg_f_length = p_ogg_f->streamLength();
306 input_thread_t *p_input = (input_thread_t *)
307 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
310 input_item_t *p_item = input_GetItem( p_input );
312 input_item_SetDuration( p_item,
313 (mtime_t) i_ogg_f_length * 1000000 );
314 vlc_object_release( p_input );
318 else if( TagLib::FLAC::File *p_flac =
319 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
321 long i_flac_length = p_flac->audioProperties()->length();
322 input_thread_t *p_input = (input_thread_t *)
323 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
326 input_item_t *p_item = input_GetItem( p_input );
328 input_item_SetDuration( p_item,
329 (mtime_t) i_flac_length * 1000000 );
330 vlc_object_release( p_input );
334 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, p_tag->bar ().toCString(true))
335 #define SETINT( foo, bar ) { \
337 snprintf( (char*)psz_tmp, 10, "%d", p_tag->bar() ); \
338 vlc_meta_Set##foo( p_meta, (char*)psz_tmp ); \
342 SET( Artist, artist );
344 SET( Description, comment );
346 SETINT( Date, year );
347 SETINT( Tracknum , track );
351 DetectImage( f, p_demux );
356 static int WriteMeta( vlc_object_t *p_this )
358 playlist_t *p_playlist = (playlist_t *)p_this;
359 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
360 input_item_t *p_item = p_export->p_item;
364 msg_Err( p_this, "Can't save meta data of an empty input" );
368 TagLib::FileRef f( p_export->psz_file );
369 if( f.isNull() || !f.tag() || f.file()->readOnly() )
371 msg_Err( p_this, "File %s can't be opened for tag writing\n",
372 p_export->psz_file );
376 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
378 TagLib::Tag *p_tag = f.tag();
384 TagLib::String *psz_##a = new TagLib::String( b, \
385 TagLib::String::UTF8 ); \
386 p_tag->set##a( *psz_##a ); \
391 psz_meta = input_item_GetArtist( p_item );
392 SET( Artist, psz_meta );
395 psz_meta = input_item_GetTitle( p_item );
396 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
397 TagLib::String *psz_title = new TagLib::String( psz_meta,
398 TagLib::String::UTF8 );
399 p_tag->setTitle( *psz_title );
403 psz_meta = input_item_GetAlbum( p_item );
404 SET( Album, psz_meta );
407 psz_meta = input_item_GetGenre( p_item );
408 SET( Genre, psz_meta );
413 psz_meta = input_item_GetDate( p_item );
414 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
417 psz_meta = input_item_GetTrackNum( p_item );
418 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
421 if( TagLib::ID3v2::Tag *p_id3tag =
422 dynamic_cast<TagLib::ID3v2::Tag *>(p_tag) )
424 #define WRITE( foo, bar ) \
425 psz_meta = input_item_Get##foo( p_item ); \
428 TagLib::ByteVector p_byte( bar, 4 ); \
429 TagLib::ID3v2::TextIdentificationFrame p_frame( p_byte ); \
430 p_frame.setText( psz_meta ); \
431 p_id3tag->addFrame( &p_frame ); \
435 WRITE( Publisher, "TPUB" );
436 WRITE( Copyright, "TCOP" );
437 WRITE( EncodedBy, "TENC" );
438 WRITE( Language, "TLAN" );
447 static int DownloadArt( vlc_object_t *p_this )
449 /* We need to be passed the file name
450 * Fetch the thing from the file, save it to the cache folder