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>
39 #include <oggflacfile.h>
42 #include <flacproperties.h>
43 #include <vorbisfile.h>
44 #include <vorbisproperties.h>
45 #include <uniquefileidentifierframe.h>
46 #include <textidentificationframe.h>
47 #if 0 /* parse the tags without taglib helpers? */
48 #include <relativevolumeframe.h>
51 static int ReadMeta ( vlc_object_t * );
52 static int DownloadArt ( vlc_object_t * );
53 static int WriteMeta ( vlc_object_t * );
56 set_capability( "meta reader", 1000 );
57 set_callbacks( ReadMeta, NULL );
59 set_capability( "art downloader", 50 );
60 set_callbacks( DownloadArt, NULL );
62 set_capability( "meta writer", 50 );
63 set_callbacks( WriteMeta, NULL );
66 static bool checkID3Image( const TagLib::ID3v2::Tag *p_tag )
68 TagLib::ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
72 /* Try detecting embedded art */
73 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
75 if( TagLib::MPEG::File *mpeg =
76 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
78 if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
79 vlc_meta_SetArtURL( p_meta, "APIC" );
81 else if( TagLib::FLAC::File *flac =
82 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
84 if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
85 vlc_meta_SetArtURL( p_meta, "APIC" );
88 /* This needs special additions to taglib */
89 * else if( TagLib::MP4::File *mp4 =
90 dynamic_cast<TagLib::MP4::File *>( f.file() ) )
92 TagLib::MP4::Tag *mp4tag =
93 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
94 if( mp4tag && mp4tag->cover().size() )
95 vlc_meta_SetArtURL( p_meta, "MP4C" );
100 static int ReadMeta( vlc_object_t *p_this )
102 demux_t *p_demux = (demux_t *)p_this;
104 if( strncmp( p_demux->psz_access, "file", 4 ) )
107 TagLib::FileRef f( p_demux->psz_path );
111 if ( !f.tag() || f.tag()->isEmpty() )
114 if( !p_demux->p_private )
115 p_demux->p_private = (void*)vlc_meta_New();
116 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
117 TagLib::Tag *p_tag = f.tag();
119 if( TagLib::MPEG::File *p_mpeg =
120 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
122 if( p_mpeg->ID3v2Tag() )
124 TagLib::ID3v2::Tag *p_tag = p_mpeg->ID3v2Tag();
125 TagLib::ID3v2::FrameList list = p_tag->frameListMap()["UFID"];
126 TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
127 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
128 iter != list.end(); iter++ )
130 p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
131 const char *owner = p_ufid->owner().toCString();
132 if (!strcmp( owner, "http://musicbrainz.org" ))
134 /* ID3v2 UFID contains up to 64 bytes binary data
135 * but in our case it will be a '\0'
136 * terminated string */
137 char *psz_ufid = (char*) malloc( 64 );
140 ( j < p_ufid->identifier().size() ) )
141 psz_ufid[j] = p_ufid->identifier()[j++];
143 vlc_meta_SetTrackID( p_meta, psz_ufid );
148 list = p_tag->frameListMap()["TXXX"];
149 TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
150 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
151 iter != list.end(); iter++ )
153 p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
154 const char *psz_desc= p_txxx->description().toCString();
155 vlc_meta_AddExtra( p_meta, psz_desc,
156 p_txxx->fieldList().toString().toCString());
159 list = p_tag->frameListMap()["RVA2"];
160 TagLib::ID3v2::RelativeVolumeFrame* p_rva2;
161 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
162 iter != list.end(); iter++ )
164 p_rva2 = dynamic_cast<TagLib::ID3v2::RelativeVolumeFrame*>(*iter);
165 /* TODO: process rva2 frames */
168 list = p_tag->frameList();
169 TagLib::ID3v2::Frame* p_t;
171 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
172 iter != list.end(); iter++ )
174 p_t = dynamic_cast<TagLib::ID3v2::Frame*> (*iter);
175 memcpy( psz_tag, p_t->frameID().data(), 4);
177 #define SET( foo, bar ) if( !strncmp( psz_tag, foo, 4 ) ) \
178 vlc_meta_Set##bar( p_meta, p_t->toString().toCString(true))
179 SET( "TPUB", Publisher );
180 SET( "TCOP", Copyright );
181 SET( "TENC", EncodedBy );
182 SET( "TLAN", Language );
183 //SET( "POPM", Rating ); /* rating needs special handling in id3v2 */
184 //if( !strncmp( psz_tag, "RVA2", 4 ) )
191 else if( TagLib::Ogg::Vorbis::File *p_ogg_v =
192 dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file() ) )
194 int i_ogg_v_length = p_ogg_v->audioProperties()->length();
196 input_thread_t *p_input = (input_thread_t *)
197 vlc_object_find( p_demux,VLC_OBJECT_INPUT, FIND_PARENT );
200 input_item_t *p_item = input_GetItem( p_input );
202 input_item_SetDuration( p_item,
203 (mtime_t) i_ogg_v_length * 1000000 );
204 vlc_object_release( p_input );
208 #if 0 /* at this moment, taglib is unable to detect ogg/flac files
209 * becauses type detection is based on file extension:
214 else if( TagLib::Ogg::FLAC::File *p_ogg_f =
215 dynamic_cast<TagLib::Ogg::FLAC::File *>(f.file() ) )
217 long i_ogg_f_length = p_ogg_f->streamLength();
218 input_thread_t *p_input = (input_thread_t *)
219 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
222 input_item_t *p_item = input_GetItem( p_input );
224 input_item_SetDuration( p_item,
225 (mtime_t) i_ogg_f_length * 1000000 );
226 vlc_object_release( p_input );
230 else if( TagLib::FLAC::File *p_flac =
231 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
233 long i_flac_length = p_flac->audioProperties()->length();
234 input_thread_t *p_input = (input_thread_t *)
235 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
238 input_item_t *p_item = input_GetItem( p_input );
240 input_item_SetDuration( p_item,
241 (mtime_t) i_flac_length * 1000000 );
242 vlc_object_release( p_input );
246 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, p_tag->bar ().toCString(true))
247 #define SETINT( foo, bar ) { \
249 snprintf( (char*)psz_tmp, 10, "%d", p_tag->bar() ); \
250 vlc_meta_Set##foo( p_meta, (char*)psz_tmp ); \
254 SET( Artist, artist );
256 SET( Description, comment );
258 SETINT( Date, year );
259 SETINT( Tracknum , track );
263 DetectImage( f, p_meta );
268 static int WriteMeta( vlc_object_t *p_this )
270 playlist_t *p_playlist = (playlist_t *)p_this;
271 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
272 input_item_t *p_item = p_export->p_item;
276 msg_Err( p_this, "Can't save meta data of an empty input" );
280 TagLib::FileRef f( p_export->psz_file );
281 if( f.isNull() || !f.tag() || f.file()->readOnly() )
283 msg_Err( p_this, "File %s can't be opened for tag writing\n",
284 p_export->psz_file );
288 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
290 TagLib::Tag *p_tag = f.tag();
296 TagLib::String *psz_##a = new TagLib::String( b, \
297 TagLib::String::UTF8 ); \
298 p_tag->set##a( *psz_##a ); \
303 psz_meta = input_item_GetArtist( p_item );
304 SET( Artist, psz_meta );
307 psz_meta = input_item_GetTitle( p_item );
308 if( !psz_meta ) psz_meta = input_item_GetName( p_item );
309 TagLib::String *psz_title = new TagLib::String( psz_meta,
310 TagLib::String::UTF8 );
311 p_tag->setTitle( *psz_title );
315 psz_meta = input_item_GetAlbum( p_item );
316 SET( Album, psz_meta );
319 psz_meta = input_item_GetGenre( p_item );
320 SET( Genre, psz_meta );
325 psz_meta = input_item_GetDate( p_item );
326 if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
329 psz_meta = input_item_GetTrackNum( p_item );
330 if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
333 if( TagLib::ID3v2::Tag *p_id3tag =
334 dynamic_cast<TagLib::ID3v2::Tag *>(p_tag) )
336 #define WRITE( foo, bar ) \
337 psz_meta = input_item_Get##foo( p_item ); \
340 TagLib::ByteVector p_byte( bar, 4 ); \
341 TagLib::ID3v2::TextIdentificationFrame p_frame( p_byte ); \
342 p_frame.setText( psz_meta ); \
343 p_id3tag->addFrame( &p_frame ); \
347 WRITE( Publisher, "TPUB" );
348 WRITE( Copyright, "TCOP" );
349 WRITE( EncodedBy, "TENC" );
350 WRITE( Language, "TLAN" );
359 static int DownloadArt( vlc_object_t *p_this )
361 /* We need to be passed the file name
362 * Fetch the thing from the file, save it to the cache folder