Add encoder support for Dirac using the Schroedinger library.
[vlc/asuraparaju-public.git] / src / input / meta.c
bloba285d7077a43b27b6e6cfa0c3916e298e82f844d
1 /*****************************************************************************
2 * meta.c : Metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org
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.
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.
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
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>
37 #include "input_internal.h"
38 #include "../playlist/art.h"
40 struct vlc_meta_t
42 char * ppsz_meta[VLC_META_TYPE_COUNT];
44 vlc_dictionary_t extra_tags;
46 int i_status;
49 /* FIXME bad name convention */
50 const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type )
52 static const char posix_names[][16] =
54 [vlc_meta_Title] = N_("Title"),
55 [vlc_meta_Artist] = N_("Artist"),
56 [vlc_meta_Genre] = N_("Genre"),
57 [vlc_meta_Copyright] = N_("Copyright"),
58 [vlc_meta_Album] = N_("Album"),
59 [vlc_meta_TrackNumber] = N_("Track number"),
60 [vlc_meta_Description] = N_("Description"),
61 [vlc_meta_Rating] = N_("Rating"),
62 [vlc_meta_Date] = N_("Date"),
63 [vlc_meta_Setting] = N_("Setting"),
64 [vlc_meta_URL] = N_("URL"),
65 [vlc_meta_Language] = N_("Language"),
66 [vlc_meta_NowPlaying] = N_("Now Playing"),
67 [vlc_meta_Publisher] = N_("Publisher"),
68 [vlc_meta_EncodedBy] = N_("Encoded by"),
69 [vlc_meta_ArtworkURL] = N_("Artwork URL"),
70 [vlc_meta_TrackID] = N_("Track ID"),
73 assert (meta_type < (sizeof(posix_names) / sizeof(posix_names[0])));
74 return vlc_gettext (posix_names[meta_type]);
78 /**
79 * vlc_meta contructor.
80 * vlc_meta_Delete() will free the returned pointer.
81 */
82 vlc_meta_t *vlc_meta_New( void )
84 vlc_meta_t *m = (vlc_meta_t*)malloc( sizeof(*m) );
85 if( !m )
86 return NULL;
87 memset( m->ppsz_meta, 0, sizeof(m->ppsz_meta) );
88 m->i_status = 0;
89 vlc_dictionary_init( &m->extra_tags, 0 );
90 return m;
93 /* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
94 static void vlc_meta_FreeExtraKey( void *p_data, void *p_obj )
96 VLC_UNUSED( p_obj );
97 free( p_data );
100 void vlc_meta_Delete( vlc_meta_t *m )
102 int i;
103 for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
104 free( m->ppsz_meta[i] );
105 vlc_dictionary_clear( &m->extra_tags, vlc_meta_FreeExtraKey, NULL );
106 free( m );
110 * vlc_meta has two kinds of meta, the one in a table, and the one in a
111 * dictionary.
112 * FIXME - Why don't we merge those two?
115 void vlc_meta_Set( vlc_meta_t *p_meta, vlc_meta_type_t meta_type, const char *psz_val )
117 free( p_meta->ppsz_meta[meta_type] );
118 p_meta->ppsz_meta[meta_type] = psz_val ? strdup( psz_val ) : NULL;
121 const char *vlc_meta_Get( const vlc_meta_t *p_meta, vlc_meta_type_t meta_type )
123 return p_meta->ppsz_meta[meta_type];
126 void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const char *psz_value )
128 char *psz_oldvalue = (char *)vlc_dictionary_value_for_key( &m->extra_tags, psz_name );
129 if( psz_oldvalue != kVLCDictionaryNotFound )
130 vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name,
131 vlc_meta_FreeExtraKey, NULL );
132 vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) );
135 const char * vlc_meta_GetExtra( const vlc_meta_t *m, const char *psz_name )
137 return (char *)vlc_dictionary_value_for_key(&m->extra_tags, psz_name);
140 unsigned vlc_meta_GetExtraCount( const vlc_meta_t *m )
142 return vlc_dictionary_keys_count(&m->extra_tags);
145 char** vlc_meta_CopyExtraNames( const vlc_meta_t *m )
147 return vlc_dictionary_all_keys(&m->extra_tags);
151 * vlc_meta status (see vlc_meta_status_e)
153 int vlc_meta_GetStatus( vlc_meta_t *m )
155 return m->i_status;
158 void vlc_meta_SetStatus( vlc_meta_t *m, int status )
160 m->i_status = status;
165 * Merging meta
167 void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src )
169 char **ppsz_all_keys;
170 int i;
172 if( !dst || !src )
173 return;
175 for( i = 0; i < VLC_META_TYPE_COUNT; i++ )
177 if( src->ppsz_meta[i] )
179 free( dst->ppsz_meta[i] );
180 dst->ppsz_meta[i] = strdup( src->ppsz_meta[i] );
184 /* XXX: If speed up are needed, it is possible */
185 ppsz_all_keys = vlc_dictionary_all_keys( &src->extra_tags );
186 for( i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
188 /* Always try to remove the previous value */
189 vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], vlc_meta_FreeExtraKey, NULL );
191 void *p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] );
192 vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], strdup( (const char*)p_value ) );
193 free( ppsz_all_keys[i] );
195 free( ppsz_all_keys );
199 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
201 input_item_t *p_item = p_input->p->p_item;
203 /* */
204 char *psz_arturl = input_item_GetArtURL( p_item );
205 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
207 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
208 free( psz_arturl );
209 return;
212 playlist_t *p_playlist = pl_Get( p_input );
214 if( input_item_IsArtFetched( p_item ) )
216 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
217 * condition */
218 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
219 playlist_FindArtInCache( p_item );
220 goto exit;
223 /* */
224 input_attachment_t *p_attachment = NULL;
226 vlc_mutex_lock( &p_item->lock );
227 for( int i_idx = 0; i_idx < p_input->p->i_attachment; i_idx++ )
229 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
230 &psz_arturl[strlen("attachment://")] ) )
232 p_attachment = vlc_input_attachment_Duplicate( p_input->p->attachment[i_idx] );
233 break;
236 vlc_mutex_unlock( &p_item->lock );
238 if( !p_attachment || p_attachment->i_data <= 0 )
240 if( p_attachment )
241 vlc_input_attachment_Delete( p_attachment );
242 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
243 goto exit;
246 /* */
247 const char *psz_type = NULL;
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";
253 /* */
254 playlist_SaveArt( p_playlist, p_item,
255 p_attachment->p_data, p_attachment->i_data, psz_type );
257 vlc_input_attachment_Delete( p_attachment );
259 exit:
260 free( psz_arturl );
263 int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
265 meta_export_t *p_export =
266 vlc_custom_create( obj, sizeof( *p_export ), VLC_OBJECT_GENERIC,
267 "meta writer" );
268 if( p_export == NULL )
269 return VLC_ENOMEM;
270 vlc_object_attach( p_export, obj );
271 p_export->p_item = p_item;
273 int type;
274 vlc_mutex_lock( &p_item->lock );
275 type = p_item->i_type;
276 vlc_mutex_unlock( &p_item->lock );
277 if( type != ITEM_TYPE_FILE )
278 goto error;
280 char *psz_uri = input_item_GetURI( p_item );
281 p_export->psz_file = make_path( psz_uri );
282 if( p_export->psz_file == NULL )
283 msg_Err( p_export, "cannot write meta to remote media %s", psz_uri );
284 free( psz_uri );
285 if( p_export->psz_file == NULL )
286 goto error;
288 module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
289 if( p_mod )
290 module_unneed( p_export, p_mod );
291 vlc_object_release( p_export );
292 return VLC_SUCCESS;
294 error:
295 vlc_object_release( p_export );
296 return VLC_EGENERIC;