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