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