]> git.sesse.net Git - vlc/blob - src/input/meta.c
decoder: reorder to avoid forward declation, no functional changes
[vlc] / src / input / meta.c
1 /*****************************************************************************
2  * meta.c : Metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_playlist.h>
33 #include <vlc_url.h>
34 #include <vlc_arrays.h>
35 #include <vlc_modules.h>
36 #include <vlc_charset.h>
37
38 #include "input_internal.h"
39 #include "../playlist/art.h"
40
41 struct vlc_meta_t
42 {
43     char * ppsz_meta[VLC_META_TYPE_COUNT];
44
45     vlc_dictionary_t extra_tags;
46
47     int i_status;
48 };
49
50 /* FIXME bad name convention */
51 const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type )
52 {
53     static const char posix_names[][18] =
54     {
55         [vlc_meta_Title]       = N_("Title"),
56         [vlc_meta_Artist]      = N_("Artist"),
57         [vlc_meta_Genre]       = N_("Genre"),
58         [vlc_meta_Copyright]   = N_("Copyright"),
59         [vlc_meta_Album]       = N_("Album"),
60         [vlc_meta_TrackNumber] = N_("Track number"),
61         [vlc_meta_Description] = N_("Description"),
62         [vlc_meta_Rating]      = N_("Rating"),
63         [vlc_meta_Date]        = N_("Date"),
64         [vlc_meta_Setting]     = N_("Setting"),
65         [vlc_meta_URL]         = N_("URL"),
66         [vlc_meta_Language]    = N_("Language"),
67         [vlc_meta_ESNowPlaying]= N_("Now Playing"),
68         [vlc_meta_NowPlaying]  = N_("Now Playing"),
69         [vlc_meta_Publisher]   = N_("Publisher"),
70         [vlc_meta_EncodedBy]   = N_("Encoded by"),
71         [vlc_meta_ArtworkURL]  = N_("Artwork URL"),
72         [vlc_meta_TrackID]     = N_("Track ID"),
73         [vlc_meta_TrackTotal]  = N_("Number of Tracks"),
74         [vlc_meta_Director]    = N_("Director"),
75         [vlc_meta_Season]      = N_("Season"),
76         [vlc_meta_Episode]     = N_("Episode"),
77         [vlc_meta_ShowName]    = N_("Show Name"),
78         [vlc_meta_Actors]      = N_("Actors"),
79         [vlc_meta_AlbumArtist] = N_("Album Artist"),
80         [vlc_meta_DiscNumber]  = N_("DiscNumber")
81     };
82
83     assert (meta_type < (sizeof(posix_names) / sizeof(posix_names[0])));
84     return vlc_gettext (posix_names[meta_type]);
85 };
86
87
88 /**
89  * vlc_meta contructor.
90  * vlc_meta_Delete() will free the returned pointer.
91  */
92 vlc_meta_t *vlc_meta_New( void )
93 {
94     vlc_meta_t *m = (vlc_meta_t*)malloc( sizeof(*m) );
95     if( !m )
96         return NULL;
97     memset( m->ppsz_meta, 0, sizeof(m->ppsz_meta) );
98     m->i_status = 0;
99     vlc_dictionary_init( &m->extra_tags, 0 );
100     return m;
101 }
102
103 /* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
104 static void vlc_meta_FreeExtraKey( void *p_data, void *p_obj )
105 {
106     VLC_UNUSED( p_obj );
107     free( p_data );
108 }
109
110 void vlc_meta_Delete( vlc_meta_t *m )
111 {
112     int i;
113     for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
114         free( m->ppsz_meta[i] );
115     vlc_dictionary_clear( &m->extra_tags, vlc_meta_FreeExtraKey, NULL );
116     free( m );
117 }
118
119 /**
120  * vlc_meta has two kinds of meta, the one in a table, and the one in a
121  * dictionary.
122  * FIXME - Why don't we merge those two?
123  */
124
125 void vlc_meta_Set( vlc_meta_t *p_meta, vlc_meta_type_t meta_type, const char *psz_val )
126 {
127     free( p_meta->ppsz_meta[meta_type] );
128     assert( psz_val == NULL || IsUTF8( psz_val ) );
129     p_meta->ppsz_meta[meta_type] = psz_val ? strdup( psz_val ) : NULL;
130 }
131
132 const char *vlc_meta_Get( const vlc_meta_t *p_meta, vlc_meta_type_t meta_type )
133 {
134     return p_meta->ppsz_meta[meta_type];
135 }
136
137 void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const char *psz_value )
138 {
139     char *psz_oldvalue = (char *)vlc_dictionary_value_for_key( &m->extra_tags, psz_name );
140     if( psz_oldvalue != kVLCDictionaryNotFound )
141         vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name,
142                                             vlc_meta_FreeExtraKey, NULL );
143     vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) );
144 }
145
146 const char * vlc_meta_GetExtra( const vlc_meta_t *m, const char *psz_name )
147 {
148     return (char *)vlc_dictionary_value_for_key(&m->extra_tags, psz_name);
149 }
150
151 unsigned vlc_meta_GetExtraCount( const vlc_meta_t *m )
152 {
153     return vlc_dictionary_keys_count(&m->extra_tags);
154 }
155
156 char** vlc_meta_CopyExtraNames( const vlc_meta_t *m )
157 {
158     return vlc_dictionary_all_keys(&m->extra_tags);
159 }
160
161 /**
162  * vlc_meta status (see vlc_meta_status_e)
163  */
164 int vlc_meta_GetStatus( vlc_meta_t *m )
165 {
166     return m->i_status;
167 }
168
169 void vlc_meta_SetStatus( vlc_meta_t *m, int status )
170 {
171     m->i_status = status;
172 }
173
174
175 /**
176  * Merging meta
177  */
178 void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src )
179 {
180     char **ppsz_all_keys;
181     int i;
182
183     if( !dst || !src )
184         return;
185
186     for( i = 0; i < VLC_META_TYPE_COUNT; i++ )
187     {
188         if( src->ppsz_meta[i] )
189         {
190             free( dst->ppsz_meta[i] );
191             dst->ppsz_meta[i] = strdup( src->ppsz_meta[i] );
192         }
193     }
194
195     /* XXX: If speed up are needed, it is possible */
196     ppsz_all_keys = vlc_dictionary_all_keys( &src->extra_tags );
197     for( i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
198     {
199         /* Always try to remove the previous value */
200         vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], vlc_meta_FreeExtraKey, NULL );
201
202         void *p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] );
203         vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], strdup( (const char*)p_value ) );
204         free( ppsz_all_keys[i] );
205     }
206     free( ppsz_all_keys );
207 }
208
209
210 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input,
211                                          const char *name )
212 {
213     input_item_t *p_item = p_input->p->p_item;
214
215     if( input_item_IsArtFetched( p_item ) )
216     {   /* XXX Weird, we should not end up with attachment:// art URL
217          * unless there is a race condition */
218         msg_Warn( p_input, "art already fetched" );
219         playlist_FindArtInCache( p_item );
220         return;
221     }
222
223     /* */
224     input_attachment_t *p_attachment = NULL;
225
226     vlc_mutex_lock( &p_item->lock );
227     for( int i_idx = 0; i_idx < p_input->p->i_attachment; i_idx++ )
228     {
229         input_attachment_t *a = p_input->p->attachment[i_idx];
230
231         if( !strcmp( a->psz_name, name ) )
232         {
233             p_attachment = vlc_input_attachment_Duplicate( a );
234             break;
235         }
236     }
237     vlc_mutex_unlock( &p_item->lock );
238
239     if( p_attachment == NULL )
240     {
241         msg_Warn( p_input, "art attachment %s not found", name );
242         return;
243     }
244
245     /* */
246     const char *psz_type = NULL;
247
248     if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
249         psz_type = ".jpg";
250     else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
251         psz_type = ".png";
252     else if( !strcmp( p_attachment->psz_mime, "image/x-pict" ) )
253         psz_type = ".pct";
254
255     playlist_SaveArt( VLC_OBJECT(p_input), p_item,
256                       p_attachment->p_data, p_attachment->i_data, psz_type );
257     vlc_input_attachment_Delete( p_attachment );
258 }
259
260 int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
261 {
262     meta_export_t *p_export =
263         vlc_custom_create( obj, sizeof( *p_export ), "meta writer" );
264     if( p_export == NULL )
265         return VLC_ENOMEM;
266     p_export->p_item = p_item;
267
268     int type;
269     vlc_mutex_lock( &p_item->lock );
270     type = p_item->i_type;
271     vlc_mutex_unlock( &p_item->lock );
272     if( type != ITEM_TYPE_FILE )
273         goto error;
274
275     char *psz_uri = input_item_GetURI( p_item );
276     p_export->psz_file = make_path( psz_uri );
277     if( p_export->psz_file == NULL )
278         msg_Err( p_export, "cannot write meta to remote media %s", psz_uri );
279     free( psz_uri );
280     if( p_export->psz_file == NULL )
281         goto error;
282
283     module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
284     if( p_mod )
285         module_unneed( p_export, p_mod );
286     vlc_object_release( p_export );
287     return VLC_SUCCESS;
288
289 error:
290     vlc_object_release( p_export );
291     return VLC_EGENERIC;
292 }
293
294 void vlc_audio_replay_gain_MergeFromMeta( audio_replay_gain_t *p_dst,
295                                           const vlc_meta_t *p_meta )
296 {
297     const char * psz_value;
298
299     if( !p_meta )
300         return;
301
302     if( (psz_value = vlc_meta_GetExtra(p_meta, "REPLAYGAIN_TRACK_GAIN")) ||
303         (psz_value = vlc_meta_GetExtra(p_meta, "RG_RADIO")) )
304     {
305         p_dst->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
306         p_dst->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
307     }
308
309     if( (psz_value = vlc_meta_GetExtra(p_meta, "REPLAYGAIN_TRACK_PEAK" )) ||
310              (psz_value = vlc_meta_GetExtra(p_meta, "RG_PEAK" )) )
311     {
312         p_dst->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
313         p_dst->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_value );
314     }
315
316     if( (psz_value = vlc_meta_GetExtra(p_meta, "REPLAYGAIN_ALBUM_GAIN" )) ||
317              (psz_value = vlc_meta_GetExtra(p_meta, "RG_AUDIOPHILE" )) )
318     {
319         p_dst->pb_gain[AUDIO_REPLAY_GAIN_ALBUM] = true;
320         p_dst->pf_gain[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
321     }
322
323     if( (psz_value = vlc_meta_GetExtra(p_meta, "REPLAYGAIN_ALBUM_PEAK" )) )
324     {
325         p_dst->pb_peak[AUDIO_REPLAY_GAIN_ALBUM] = true;
326         p_dst->pf_peak[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_value );
327     }
328 }