]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
Reads more metadata from ID3v2 tags
[vlc] / modules / meta_engine / taglib.cpp
1 /*****************************************************************************
2  * taglib.cpp: Taglib tag parser/writer
3  *****************************************************************************
4  * Copyright (C) 2003-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include <stdlib.h>
24
25 #include <vlc/vlc.h>
26 #include <vlc_playlist.h>
27 #include <vlc_meta.h>
28 #include <vlc_demux.h>
29
30 #include <fileref.h>
31 #include <tag.h>
32 #include <tstring.h>
33 #include <id3v2tag.h>
34 #include <mpegfile.h>
35 #include <flacfile.h>
36 #if 0
37 #include <oggflacfile.h>
38 #endif
39 #include <flacfile.h>
40 #include <flacproperties.h>
41 #include <vorbisfile.h>
42 #include <vorbisproperties.h>
43 #include <uniquefileidentifierframe.h>
44 #include <textidentificationframe.h>
45 //#include <relativevolumeframe.h> /* parse the tags without taglib helpers? */
46
47 static int  ReadMeta    ( vlc_object_t * );
48 static int  DownloadArt ( vlc_object_t * );
49 static int  WriteMeta   ( vlc_object_t * );
50
51 vlc_module_begin();
52     set_capability( "meta reader", 1000 );
53     set_callbacks( ReadMeta, NULL );
54     add_submodule();
55         set_capability( "art downloader", 50 );
56         set_callbacks( DownloadArt, NULL );
57     add_submodule();
58         set_capability( "meta writer", 50 );
59         set_callbacks( WriteMeta, NULL );
60 vlc_module_end();
61
62 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
63 {
64     TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
65     return !l.isEmpty();
66 }
67
68 /* Try detecting embedded art */
69 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
70 {
71     if( TagLib::MPEG::File *mpeg =
72                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
73     {
74         if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
75             vlc_meta_SetArtURL( p_meta, "APIC" );
76     }
77     else if( TagLib::FLAC::File *flac =
78              dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
79     {
80         if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
81             vlc_meta_SetArtURL( p_meta, "APIC" );
82     }
83 #if 0
84 /* This needs special additions to taglib */
85  * else if( TagLib::MP4::File *mp4 =
86                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
87     {
88         TagLib::MP4::Tag *mp4tag =
89                 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
90         if( mp4tag && mp4tag->cover().size() )
91             vlc_meta_SetArtURL( p_meta, "MP4C" );
92     }
93 #endif
94 }
95
96 static int ReadMeta( vlc_object_t *p_this )
97 {
98     demux_t *p_demux = (demux_t *)p_this;
99
100     if( !strncmp( p_demux->psz_access, "file", 4 ) )
101     {
102         if( !p_demux->p_private )
103             p_demux->p_private = (void*)vlc_meta_New();
104         TagLib::FileRef f( p_demux->psz_path );
105
106         if( !f.isNull() )
107         {
108             if( TagLib::Ogg::Vorbis::File *p_ogg_v =
109                 dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file() ) )
110             {
111                 int i_ogg_v_length = p_ogg_v->audioProperties()->length();
112
113                 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
114                 if( p_input )
115                 {
116                     input_item_t *p_item = input_GetItem( p_input );
117                     if( p_item )
118                     {
119                         vlc_mutex_lock( &p_item->lock );
120                         p_item->i_duration = i_ogg_v_length * 1000000;
121                         vlc_mutex_unlock( &p_item->lock );
122                     }
123                     vlc_object_release( p_input );
124                 }
125             }
126 #if 0 /* at this moment, taglib is unable to detect ogg/flac files
127        * becauses type detection is based on file extension:
128        * ogg = ogg/vorbis
129        * flac = flac
130        * ø = ogg/flac
131        */
132             else if( TagLib::Ogg::FLAC::File *p_ogg_f =
133                 dynamic_cast<TagLib::Ogg::FLAC::File *>(f.file() ) )
134             {
135                 long i_ogg_f_length = p_ogg_f->streamLength();
136                 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
137                 if( p_input )
138                 {
139                     input_item_t *p_item = input_GetItem( p_input );
140                     if( p_item )
141                     {
142                         vlc_mutex_lock( &p_item->lock );
143                         p_item->i_duration = i_ogg_f_length * 1000000;
144                         vlc_mutex_unlock( &p_item->lock );
145                     }
146                     vlc_object_release( p_input );
147                 }
148             }
149 #endif
150             else if( TagLib::FLAC::File *p_flac =
151                 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
152             {
153                 long i_flac_length = p_flac->audioProperties()->length();
154                 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
155                 if( p_input )
156                 {
157                     input_item_t *p_item = input_GetItem( p_input );
158                     if( p_item )
159                     {
160                         vlc_mutex_lock( &p_item->lock );
161                         p_item->i_duration = i_flac_length * 1000000;
162                         vlc_mutex_unlock( &p_item->lock );
163                     }
164                     vlc_object_release( p_input );
165                 }
166             }
167         }
168
169         if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
170         {
171             TagLib::Tag *tag = f.tag();
172             vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
173
174 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
175 #define SETINT( foo, bar ) { \
176             char psz_tmp[10]; \
177             snprintf( (char*)psz_tmp, 10, "%d", tag->bar() ); \
178             vlc_meta_Set##foo( p_meta, (char*)psz_tmp ); \
179         }
180
181             SET( Title, title );
182             SET( Artist, artist );
183             SET( Album, album );
184             SET( Description, comment );
185             SET( Genre, genre );
186             SETINT( Date, year );
187             SETINT( Tracknum , track );
188 #undef SET
189 #undef SETINT
190
191             if( TagLib::MPEG::File *p_mpeg =
192                 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
193             {
194                 if( p_mpeg->ID3v2Tag() )
195                 {
196                     TagLib::ID3v2::Tag *tag = p_mpeg->ID3v2Tag();
197                     TagLib::ID3v2::FrameList list = tag->frameListMap()["UFID"];
198                     TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
199                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
200                             iter != list.end(); iter++ )
201                     {
202                         p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
203                         const char *owner = p_ufid->owner().toCString();
204                         if (!strcmp( owner, "http://musicbrainz.org" ))
205                         {
206                             /* ID3v2 UFID contains up to 64 bytes binary data
207                              * but in our case it will be a '\0' 
208                              * terminated string */
209                             char *psz_ufid = (char*) malloc( 64 );
210                             int j = 0;
211                             while( ( j < 63 ) &&
212                                     ( j < p_ufid->identifier().size() ) )
213                                 psz_ufid[j] = p_ufid->identifier()[j++];
214                             psz_ufid[j] = '\0';
215                             vlc_meta_SetTrackID( p_meta, psz_ufid );
216                             free( psz_ufid );
217                         }
218                     }
219
220                     list = tag->frameListMap()["TXXX"];
221                     TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
222                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
223                             iter != list.end(); iter++ )
224                     {
225                         p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
226                         const char *psz_desc= p_txxx->description().toCString();
227 #if 0 /* musicbrainz artist and album id: not useful (yet?) */
228                         if( !strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
229                             vlc_meta_SetArtistID( p_meta,
230                                     p_txxx->fieldList().toString().toCString());
231                         if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
232                             vlc_meta_SetAlbumID( p_meta,
233                                     p_txxx->fieldList().toString().toCString());
234 #endif
235                         vlc_meta_AddExtra( p_meta, psz_desc, 
236                                     p_txxx->fieldList().toString().toCString());
237                     }
238 #if 0
239                     list = tag->frameListMap()["RVA2"];
240                     TagLib::ID3v2::RelativeVolumeFrame* p_rva2;
241                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
242                             iter != list.end(); iter++ )
243                     {
244                         p_rva2 = dynamic_cast<TagLib::ID3v2::RelativeVolumeFrame*>(*iter);
245                         /* TODO: process rva2 frames */
246                     }
247 #endif
248                     list = tag->frameList();
249                     TagLib::ID3v2::Frame* p_t;
250                     char psz_tag[4];
251                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
252                             iter != list.end(); iter++ )
253                     {
254                         p_t = dynamic_cast<TagLib::ID3v2::Frame*> (*iter);
255                         memcpy( psz_tag, p_t->frameID().data(), 4);
256
257 #define SET( foo, bar ) if( !strncmp( psz_tag, foo, 4 ) ) \
258     vlc_meta_Set##bar( p_meta, p_t->toString().toCString(true))
259                         SET( "TPUB", Publisher );
260                         SET( "TCOP", Copyright );
261                         SET( "TENC", EncodedBy );
262                         SET( "TLAN", Language );
263                         //SET( "POPM", Rating );
264                         //if( !strncmp( psz_tag, "RVA2", 4 ) )
265                             /* TODO */
266 #undef SET
267                     }
268                 }
269             }
270
271             DetectImage( f, p_meta );
272
273             return VLC_SUCCESS;
274         }
275     }
276     return VLC_EGENERIC;
277 }
278
279 #define SET(a,b) if(b) { \
280         TagLib::String *psz_##a = new TagLib::String( b, \
281             TagLib::String::UTF8 ); \
282         tag->set##a( *psz_##a ); \
283         delete psz_##a; \
284     }
285
286 static int WriteMeta( vlc_object_t *p_this )
287 {
288     playlist_t *p_playlist = (playlist_t *)p_this;
289     meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
290     input_item_t *p_item = p_export->p_item;
291     
292     if( p_item == NULL )
293     {
294         msg_Err( p_this, "Can't save meta data of an empty input" );
295         return VLC_EGENERIC;
296     }
297
298     TagLib::FileRef f( p_export->psz_file );
299     if( !f.isNull() && f.tag() )
300     {
301         msg_Dbg( p_this, "Updating metadata for %s", p_export->psz_file );
302
303         TagLib::Tag *tag = f.tag();
304
305         char *psz_meta;
306
307         psz_meta = input_item_GetArtist( p_item );
308         SET( Artist, psz_meta );
309         free( psz_meta );
310
311         psz_meta = input_item_GetTitle( p_item );
312         if( !psz_meta ) psz_meta = input_item_GetName( p_item );
313         TagLib::String *psz_title = new TagLib::String( psz_meta,
314             TagLib::String::UTF8 );
315         tag->setTitle( *psz_title );
316         delete psz_title;
317         free( psz_meta );
318
319         psz_meta = input_item_GetAlbum( p_item );
320         SET( Album, psz_meta );
321         free( psz_meta );
322
323         psz_meta = input_item_GetGenre( p_item );
324         SET( Genre, psz_meta );
325         free( psz_meta );
326
327         psz_meta = input_item_GetDate( p_item );
328         if( psz_meta ) tag->setYear( atoi( psz_meta ) );
329         free( psz_meta );
330
331         psz_meta = input_item_GetTrackNum( p_item );
332         if( psz_meta ) tag->setTrack( atoi( psz_meta ) );
333         free( psz_meta );
334
335         f.save();
336         return VLC_SUCCESS;
337     }
338     msg_Err( p_this, "File %s can't be opened for tag writing\n",
339         p_export->psz_file );
340     return VLC_EGENERIC;
341 }
342
343 static int DownloadArt( vlc_object_t *p_this )
344 {
345     /* We need to be passed the file name
346      * Fetch the thing from the file, save it to the cache folder
347      */
348     return VLC_EGENERIC;
349 }