]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
Corrects indentation
[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 <vlc/vlc.h>
24 #include <vlc_playlist.h>
25 #include <vlc/input.h>
26 #include <vlc_meta.h>
27
28 #include <fileref.h>
29 #include <tag.h>
30 #include <id3v2tag.h>
31 #include <mpegfile.h>
32 #include <flacfile.h>
33 #include <vorbisfile.h>
34 #include <uniquefileidentifierframe.h>
35 #if 0 //for artist and album id
36 #include <textidentificationframe.h>
37 #endif
38
39 static int  ReadMeta    ( vlc_object_t * );
40 static int  DownloadArt ( vlc_object_t * );
41 static int  WriteMeta   ( vlc_object_t * );
42
43 vlc_module_begin();
44     set_capability( "meta reader", 1000 );
45     set_callbacks( ReadMeta, NULL );
46     add_submodule();
47         set_capability( "art downloader", 50 );
48         set_callbacks( DownloadArt, NULL );
49     add_submodule();
50         set_capability( "meta writer", 50 );
51         set_callbacks( WriteMeta, NULL );
52 vlc_module_end();
53
54 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
55 {
56     TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
57     return !l.isEmpty();
58 }
59
60 /* Try detecting embedded art */
61 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
62 {
63     if( TagLib::MPEG::File *mpeg =
64                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
65     {
66         if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
67             vlc_meta_SetArtURL( p_meta, "APIC" );
68     }
69     else if( TagLib::FLAC::File *flac =
70              dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
71     {
72         if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
73             vlc_meta_SetArtURL( p_meta, "APIC" );
74     }
75 #if 0
76 /* This needs special additions to taglib */
77  * else if( TagLib::MP4::File *mp4 =
78                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
79     {
80         TagLib::MP4::Tag *mp4tag =
81                 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
82         if( mp4tag && mp4tag->cover().size() )
83             vlc_meta_SetArtURL( p_meta, "MP4C" );
84     }
85 #endif
86 }
87
88 static int ReadMeta( vlc_object_t *p_this )
89 {
90     demux_t *p_demux = (demux_t *)p_this;
91
92     if( !strncmp( p_demux->psz_access, "file", 4 ) )
93     {
94         if( !p_demux->p_private )
95             p_demux->p_private = (void*)vlc_meta_New();
96         TagLib::FileRef f( p_demux->psz_path );
97         if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
98         {
99             TagLib::Tag *tag = f.tag();
100             vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
101
102 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
103             SET( Title, title );
104             SET( Artist, artist );
105             SET( Album, album );
106 //            SET( Comment, comment );
107             SET( Genre, genre );
108 //            SET( Year, year ); Gra, this is an int, need to convert
109 //            SET( Tracknum , track ); Same
110 #undef SET
111
112             if( TagLib::MPEG::File *p_mpeg =
113                 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
114             {
115                 if( p_mpeg->ID3v2Tag() )
116                 {
117                     TagLib::ID3v2::Tag *tag = p_mpeg->ID3v2Tag();
118                     TagLib::ID3v2::FrameList list = tag->frameListMap()["UFID"];
119                     TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
120                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
121                             iter != list.end(); iter++ )
122                     {
123                         p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
124                         const char *owner = p_ufid->owner().toCString();
125                         if (!strcmp( owner, "http://musicbrainz.org" ))
126                         {
127                             /* ID3v2 UFID contains up to 64 bytes binary data
128                              * but in our case it will be a '\0' 
129                              * terminated string */
130                             char *psz_ufid = (char*) malloc( 64 );
131                             int j = 0;
132                             while( ( j < 63 ) &&
133                                     ( j < p_ufid->identifier().size() ) )
134                                 psz_ufid[j] = p_ufid->identifier()[j++];
135                             psz_ufid[j] = '\0';
136                             vlc_meta_SetTrackID( p_meta, psz_ufid );
137                             free( psz_ufid );
138                         }
139                     }
140                     /* musicbrainz artist and album id: not useful yet */
141 #if 0
142                     list = tag->frameListMap()["TXXX"];
143                     TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
144                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
145                             iter != list.end(); iter++ )
146                     {
147                         p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
148                         const char *psz_desc= p_txxx->description().toCString();
149                         if( !strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
150                             vlc_meta_SetArtistID( p_meta,
151                                     p_txxx->fieldList().toString().toCString());
152                         if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
153                             vlc_meta_SetAlbumID( p_meta,
154                                     p_txxx->fieldList().toString().toCString());
155                     }
156 #endif
157                 }
158             }
159
160             DetectImage( f, p_meta );
161
162             return VLC_SUCCESS;
163         }
164     }
165     return VLC_EGENERIC;
166 }
167
168 static int WriteMeta( vlc_object_t *p_this )
169 {
170     playlist_t *p_playlist = (playlist_t *)p_this;
171     meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
172     input_item_t *p_item = p_export->p_item;
173
174     TagLib::FileRef f( p_export->psz_file );
175     if( !f.isNull() && f.tag() )
176     {
177         TagLib::Tag *tag = f.tag();
178         tag->setArtist( p_item->p_meta->psz_artist );
179         f.save();
180         return VLC_SUCCESS;
181     }
182     return VLC_EGENERIC;
183 }
184
185 static int DownloadArt( vlc_object_t *p_this )
186 {
187     /* We need to be passed the file name
188      * Fetch the thing from the file, save it to the cache folder
189      */
190     return VLC_EGENERIC;
191 }