]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
taglib: support for id3v2 embedded album art
[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  *          Rafaël Carré <funman@videolanorg>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
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 <textidentificationframe.h>
35 #include <tbytevector.h>
36 #include <mpegfile.h>
37 #include <flacfile.h>
38 #include <attachedpictureframe.h>
39 #if 0
40 #include <oggflacfile.h>
41 #endif
42 #include <flacfile.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>
50 #endif
51
52 static int  ReadMeta    ( vlc_object_t * );
53 static int  DownloadArt ( vlc_object_t * );
54 static int  WriteMeta   ( vlc_object_t * );
55
56 vlc_module_begin();
57     set_capability( "meta reader", 1000 );
58     set_callbacks( ReadMeta, NULL );
59     add_submodule();
60         set_capability( "art downloader", 50 );
61         set_callbacks( DownloadArt, NULL );
62     add_submodule();
63         set_capability( "meta writer", 50 );
64         set_callbacks( WriteMeta, NULL );
65 vlc_module_end();
66
67 /* Try detecting embedded art */
68 static void DetectImage( TagLib::FileRef f, demux_t *p_demux )
69 {
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;
73     int                 i_score         = -1;
74
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[] = {
79         0,  /* Other */
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). */
100     };
101
102     if( TagLib::MPEG::File *mpeg =
103                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
104     {
105         p_tag = mpeg->ID3v2Tag();
106         if( !p_tag )
107             return;
108         TagLib::ID3v2::FrameList list = p_tag->frameListMap()[ "APIC" ];
109         if( list.isEmpty() )
110             return;
111         input_thread_t *p_input = (input_thread_t *)
112                 vlc_object_find( p_demux,VLC_OBJECT_INPUT, FIND_PARENT );
113         if( !p_input )
114             return;
115         input_item_t *p_item = input_GetItem( p_input );
116         TagLib::ID3v2::AttachedPictureFrame *p_apic;
117
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++ )
121         {
122             p_apic = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(*iter);
123             input_attachment_t *p_attachment;
124
125             const char *psz_name, *psz_mime, *psz_description;
126             TagLib::ByteVector p_data_taglib; const char *p_data; int i_data;
127
128             psz_mime = p_apic->mimeType().toCString(true);
129             psz_description = p_apic->description().toCString(true);
130             psz_name = psz_description;
131
132             p_data_taglib = p_apic->picture();
133             p_data = p_data_taglib.data();
134             i_data = p_data_taglib.size();
135
136             msg_Dbg( p_demux, "Found embedded art: %s (%s) is %i bytes",
137                     psz_name, psz_mime, i_data );
138
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,
143                     p_attachment );
144
145             if( pi_cover_score[p_apic->type()] > i_score )
146             {
147                 i_score = pi_cover_score[p_apic->type()];
148                 char *psz_url;
149                 if( asprintf( &psz_url, "attachment://%s",
150                         p_attachment->psz_name ) == -1 )
151                 {
152                     vlc_object_release( p_input );
153                     return;
154                 }
155                 vlc_meta_SetArtURL( p_meta, psz_url );
156                 free( psz_url );
157             }
158         }
159         vlc_object_release( p_input );
160     }
161 #if 0
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() ) )
165     {
166         p_tag = flac->ID3v2Tag();
167         if( p_tag )
168             return;
169         TagLib::ID3v2::FrameList l = p_tag->frameListMap()[ "APIC" ];
170         if( l.isEmpty() )
171             return;
172             vlc_meta_SetArtURL( p_meta, "APIC" );
173     }
174 #endif
175 #if 0
176 /* This needs special additions to taglib */
177  * else if( TagLib::MP4::File *mp4 =
178                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
179     {
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" );
184     }
185 #endif
186 }
187
188 static int ReadMeta( vlc_object_t *p_this )
189 {
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;
193
194     TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
195     p_demux_meta->p_meta = NULL;
196
197     TagLib::FileRef f( p_demux->psz_path );
198     if( f.isNull() )
199         return VLC_EGENERIC;
200
201     if ( !f.tag() || f.tag()->isEmpty() )
202         return VLC_EGENERIC;
203
204     p_demux_meta->p_meta = p_meta = vlc_meta_New();
205     TagLib::Tag *p_tag = f.tag();
206
207     if( TagLib::MPEG::File *p_mpeg =
208         dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
209     {
210         if( p_mpeg->ID3v2Tag() )
211         {
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++ )
217             {
218                 p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
219                 const char *owner = p_ufid->owner().toCString();
220                 if (!strcmp( owner, "http://musicbrainz.org" ))
221                 {
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 );
226                     int j = 0;
227                     while( ( j < 63 ) &&
228                             ( j < p_ufid->identifier().size() ) )
229                         psz_ufid[j] = p_ufid->identifier()[j++];
230                     psz_ufid[j] = '\0';
231                     vlc_meta_SetTrackID( p_meta, psz_ufid );
232                     free( psz_ufid );
233                 }
234             }
235
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++ )
240             {
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());
245             }
246 #if 0
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++ )
251             {
252                 p_rva2 = dynamic_cast<TagLib::ID3v2::RelativeVolumeFrame*>(*iter);
253                 /* TODO: process rva2 frames */
254             }
255 #endif
256             list = p_tag->frameList();
257             TagLib::ID3v2::Frame* p_t;
258             char psz_tag[4];
259             for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
260                     iter != list.end(); iter++ )
261             {
262                 p_t = dynamic_cast<TagLib::ID3v2::Frame*> (*iter);
263                 memcpy( psz_tag, p_t->frameID().data(), 4);
264
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 ) )
273                     /* TODO */
274 #undef SET
275             }
276         }
277     }
278
279     else if( TagLib::Ogg::Vorbis::File *p_ogg_v =
280         dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file() ) )
281     {
282         int i_ogg_v_length = p_ogg_v->audioProperties()->length();
283
284         input_thread_t *p_input = (input_thread_t *)
285                 vlc_object_find( p_demux,VLC_OBJECT_INPUT, FIND_PARENT );
286         if( p_input )
287         {
288             input_item_t *p_item = input_GetItem( p_input );
289             if( p_item )
290                 input_item_SetDuration( p_item,
291                         (mtime_t) i_ogg_v_length * 1000000 );
292             vlc_object_release( p_input );
293         }
294
295     }
296 #if 0 /* at this moment, taglib is unable to detect ogg/flac files
297 * becauses type detection is based on file extension:
298 * ogg = ogg/vorbis
299 * flac = flac
300 * ø = ogg/flac
301 */
302     else if( TagLib::Ogg::FLAC::File *p_ogg_f =
303         dynamic_cast<TagLib::Ogg::FLAC::File *>(f.file() ) )
304     {
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 );
308         if( p_input )
309         {
310             input_item_t *p_item = input_GetItem( p_input );
311             if( p_item )
312                 input_item_SetDuration( p_item,
313                         (mtime_t) i_ogg_f_length * 1000000 );
314             vlc_object_release( p_input );
315         }
316     }
317 #endif
318     else if( TagLib::FLAC::File *p_flac =
319         dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
320     {
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 );
324         if( p_input )
325         {
326             input_item_t *p_item = input_GetItem( p_input );
327             if( p_item )
328                 input_item_SetDuration( p_item,
329                         (mtime_t) i_flac_length * 1000000 );
330             vlc_object_release( p_input );
331         }
332     }
333
334 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, p_tag->bar ().toCString(true))
335 #define SETINT( foo, bar ) { \
336         char psz_tmp[10]; \
337         snprintf( (char*)psz_tmp, 10, "%d", p_tag->bar() ); \
338         vlc_meta_Set##foo( p_meta, (char*)psz_tmp ); \
339     }
340
341     SET( Title, title );
342     SET( Artist, artist );
343     SET( Album, album );
344     SET( Description, comment );
345     SET( Genre, genre );
346     SETINT( Date, year );
347     SETINT( Tracknum , track );
348 #undef SET
349 #undef SETINT
350
351     DetectImage( f, p_demux );
352
353     return VLC_SUCCESS;
354 }
355
356 static int WriteMeta( vlc_object_t *p_this )
357 {
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;
361
362     if( p_item == NULL )
363     {
364         msg_Err( p_this, "Can't save meta data of an empty input" );
365         return VLC_EGENERIC;
366     }
367
368     TagLib::FileRef f( p_export->psz_file );
369     if( f.isNull() || !f.tag() || f.file()->readOnly() )
370     {
371         msg_Err( p_this, "File %s can't be opened for tag writing\n",
372             p_export->psz_file );
373         return VLC_EGENERIC;
374     }
375
376     msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
377
378     TagLib::Tag *p_tag = f.tag();
379
380     char *psz_meta;
381
382 #define SET(a,b) \
383         if(b) { \
384             TagLib::String *psz_##a = new TagLib::String( b, \
385                 TagLib::String::UTF8 ); \
386             p_tag->set##a( *psz_##a ); \
387             delete psz_##a; \
388         }
389
390
391     psz_meta = input_item_GetArtist( p_item );
392     SET( Artist, psz_meta );
393     free( psz_meta );
394
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 );
400     delete psz_title;
401     free( psz_meta );
402
403     psz_meta = input_item_GetAlbum( p_item );
404     SET( Album, psz_meta );
405     free( psz_meta );
406
407     psz_meta = input_item_GetGenre( p_item );
408     SET( Genre, psz_meta );
409     free( psz_meta );
410
411 #undef SET
412
413     psz_meta = input_item_GetDate( p_item );
414     if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
415     free( psz_meta );
416
417     psz_meta = input_item_GetTrackNum( p_item );
418     if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
419     free( psz_meta );
420
421     if( TagLib::ID3v2::Tag *p_id3tag =
422         dynamic_cast<TagLib::ID3v2::Tag *>(p_tag) )
423     {
424 #define WRITE( foo, bar ) \
425         psz_meta = input_item_Get##foo( p_item ); \
426         if( psz_meta ) \
427         { \
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 ); \
432             free( psz_meta ); \
433         } \
434
435         WRITE( Publisher, "TPUB" );
436         WRITE( Copyright, "TCOP" );
437         WRITE( EncodedBy, "TENC" );
438         WRITE( Language, "TLAN" );
439
440 #undef WRITE
441     }
442
443     f.save();
444     return VLC_SUCCESS;
445 }
446
447 static int DownloadArt( vlc_object_t *p_this )
448 {
449     /* We need to be passed the file name
450      * Fetch the thing from the file, save it to the cache folder
451      */
452     return VLC_EGENERIC;
453 }
454