1 /*****************************************************************************
2 * meta.c : Metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VLC authors and VideoLAN
6 * Authors: Antoine Cellerier <dionoea@videolan.org>
7 * Clément Stenac <zorglub@videolan.org
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_playlist_legacy.h>
33 #include <vlc_arrays.h>
34 #include <vlc_modules.h>
35 #include <vlc_charset.h>
37 #include "input_internal.h"
38 #include "../preparser/art.h"
42 char * ppsz_meta
[VLC_META_TYPE_COUNT
];
44 vlc_dictionary_t extra_tags
;
49 /* FIXME bad name convention */
50 const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type
)
52 static const char posix_names
[][18] =
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_ESNowPlaying
]= N_("Now Playing"),
67 [vlc_meta_NowPlaying
] = N_("Now Playing"),
68 [vlc_meta_Publisher
] = N_("Publisher"),
69 [vlc_meta_EncodedBy
] = N_("Encoded by"),
70 [vlc_meta_ArtworkURL
] = N_("Artwork URL"),
71 [vlc_meta_TrackID
] = N_("Track ID"),
72 [vlc_meta_TrackTotal
] = N_("Number of Tracks"),
73 [vlc_meta_Director
] = N_("Director"),
74 [vlc_meta_Season
] = N_("Season"),
75 [vlc_meta_Episode
] = N_("Episode"),
76 [vlc_meta_ShowName
] = N_("Show Name"),
77 [vlc_meta_Actors
] = N_("Actors"),
78 [vlc_meta_AlbumArtist
] = N_("Album Artist"),
79 [vlc_meta_DiscNumber
] = N_("Disc number")
82 assert (meta_type
< (sizeof(posix_names
) / sizeof(posix_names
[0])));
83 return vlc_gettext (posix_names
[meta_type
]);
88 * vlc_meta contructor.
89 * vlc_meta_Delete() will free the returned pointer.
91 vlc_meta_t
*vlc_meta_New( void )
93 vlc_meta_t
*m
= (vlc_meta_t
*)malloc( sizeof(*m
) );
96 memset( m
->ppsz_meta
, 0, sizeof(m
->ppsz_meta
) );
98 vlc_dictionary_init( &m
->extra_tags
, 0 );
102 /* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
103 static void vlc_meta_FreeExtraKey( void *p_data
, void *p_obj
)
109 void vlc_meta_Delete( vlc_meta_t
*m
)
111 for( int i
= 0; i
< VLC_META_TYPE_COUNT
; i
++ )
112 free( m
->ppsz_meta
[i
] );
113 vlc_dictionary_clear( &m
->extra_tags
, vlc_meta_FreeExtraKey
, NULL
);
118 * vlc_meta has two kinds of meta, the one in a table, and the one in a
120 * FIXME - Why don't we merge those two?
123 void vlc_meta_Set( vlc_meta_t
*p_meta
, vlc_meta_type_t meta_type
, const char *psz_val
)
125 free( p_meta
->ppsz_meta
[meta_type
] );
126 assert( psz_val
== NULL
|| IsUTF8( psz_val
) );
127 p_meta
->ppsz_meta
[meta_type
] = psz_val
? strdup( psz_val
) : NULL
;
130 const char *vlc_meta_Get( const vlc_meta_t
*p_meta
, vlc_meta_type_t meta_type
)
132 return p_meta
->ppsz_meta
[meta_type
];
135 void vlc_meta_AddExtra( vlc_meta_t
*m
, const char *psz_name
, const char *psz_value
)
137 char *psz_oldvalue
= (char *)vlc_dictionary_value_for_key( &m
->extra_tags
, psz_name
);
138 if( psz_oldvalue
!= kVLCDictionaryNotFound
)
139 vlc_dictionary_remove_value_for_key( &m
->extra_tags
, psz_name
,
140 vlc_meta_FreeExtraKey
, NULL
);
141 vlc_dictionary_insert( &m
->extra_tags
, psz_name
, strdup(psz_value
) );
144 const char * vlc_meta_GetExtra( const vlc_meta_t
*m
, const char *psz_name
)
146 return (char *)vlc_dictionary_value_for_key(&m
->extra_tags
, psz_name
);
149 unsigned vlc_meta_GetExtraCount( const vlc_meta_t
*m
)
151 return vlc_dictionary_keys_count(&m
->extra_tags
);
154 char** vlc_meta_CopyExtraNames( const vlc_meta_t
*m
)
156 return vlc_dictionary_all_keys(&m
->extra_tags
);
160 * vlc_meta status (see vlc_meta_status_e)
162 int vlc_meta_GetStatus( vlc_meta_t
*m
)
167 void vlc_meta_SetStatus( vlc_meta_t
*m
, int status
)
169 m
->i_status
= status
;
176 void vlc_meta_Merge( vlc_meta_t
*dst
, const vlc_meta_t
*src
)
181 for( int i
= 0; i
< VLC_META_TYPE_COUNT
; i
++ )
183 if( src
->ppsz_meta
[i
] )
185 free( dst
->ppsz_meta
[i
] );
186 dst
->ppsz_meta
[i
] = strdup( src
->ppsz_meta
[i
] );
190 /* XXX: If speed up are needed, it is possible */
191 char **ppsz_all_keys
= vlc_dictionary_all_keys( &src
->extra_tags
);
192 for( int i
= 0; ppsz_all_keys
&& ppsz_all_keys
[i
]; i
++ )
194 /* Always try to remove the previous value */
195 vlc_dictionary_remove_value_for_key( &dst
->extra_tags
, ppsz_all_keys
[i
], vlc_meta_FreeExtraKey
, NULL
);
197 void *p_value
= vlc_dictionary_value_for_key( &src
->extra_tags
, ppsz_all_keys
[i
] );
198 vlc_dictionary_insert( &dst
->extra_tags
, ppsz_all_keys
[i
], strdup( (const char*)p_value
) );
199 free( ppsz_all_keys
[i
] );
201 free( ppsz_all_keys
);
205 void input_ExtractAttachmentAndCacheArt( input_thread_t
*p_input
,
208 input_item_t
*p_item
= input_priv(p_input
)->p_item
;
210 if( input_item_IsArtFetched( p_item
) )
211 { /* XXX Weird, we should not end up with attachment:// art URL
212 * unless there is a race condition */
213 msg_Warn( p_input
, "art already fetched" );
214 if( likely(input_FindArtInCache( p_item
) == VLC_SUCCESS
) )
219 input_attachment_t
*p_attachment
= input_GetAttachment( p_input
, name
);
222 msg_Warn( p_input
, "art attachment %s not found", name
);
227 const char *psz_type
= NULL
;
229 if( !strcmp( p_attachment
->psz_mime
, "image/jpeg" ) )
231 else if( !strcmp( p_attachment
->psz_mime
, "image/png" ) )
233 else if( !strcmp( p_attachment
->psz_mime
, "image/x-pict" ) )
236 input_SaveArt( VLC_OBJECT(p_input
), p_item
,
237 p_attachment
->p_data
, p_attachment
->i_data
, psz_type
);
238 vlc_input_attachment_Delete( p_attachment
);
241 int input_item_WriteMeta( vlc_object_t
*obj
, input_item_t
*p_item
)
243 meta_export_t
*p_export
=
244 vlc_custom_create( obj
, sizeof( *p_export
), "meta writer" );
245 if( p_export
== NULL
)
247 p_export
->p_item
= p_item
;
249 enum input_item_type_e type
;
250 vlc_mutex_lock( &p_item
->lock
);
251 type
= p_item
->i_type
;
252 vlc_mutex_unlock( &p_item
->lock
);
253 if( type
!= ITEM_TYPE_FILE
)
256 char *psz_uri
= input_item_GetURI( p_item
);
257 p_export
->psz_file
= vlc_uri2path( psz_uri
);
258 if( p_export
->psz_file
== NULL
)
259 msg_Err( p_export
, "cannot write meta to remote media %s", psz_uri
);
261 if( p_export
->psz_file
== NULL
)
264 module_t
*p_mod
= module_need( p_export
, "meta writer", NULL
, false );
266 module_unneed( p_export
, p_mod
);
267 vlc_object_release( p_export
);
271 vlc_object_release( p_export
);
275 void vlc_audio_replay_gain_MergeFromMeta( audio_replay_gain_t
*p_dst
,
276 const vlc_meta_t
*p_meta
)
278 const char * psz_value
;
283 if( (psz_value
= vlc_meta_GetExtra(p_meta
, "REPLAYGAIN_TRACK_GAIN")) ||
284 (psz_value
= vlc_meta_GetExtra(p_meta
, "RG_RADIO")) )
286 p_dst
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = true;
287 p_dst
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( psz_value
);
290 if( (psz_value
= vlc_meta_GetExtra(p_meta
, "REPLAYGAIN_TRACK_PEAK" )) ||
291 (psz_value
= vlc_meta_GetExtra(p_meta
, "RG_PEAK" )) )
293 p_dst
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = true;
294 p_dst
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( psz_value
);
297 if( (psz_value
= vlc_meta_GetExtra(p_meta
, "REPLAYGAIN_ALBUM_GAIN" )) ||
298 (psz_value
= vlc_meta_GetExtra(p_meta
, "RG_AUDIOPHILE" )) )
300 p_dst
->pb_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
301 p_dst
->pf_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( psz_value
);
304 if( (psz_value
= vlc_meta_GetExtra(p_meta
, "REPLAYGAIN_ALBUM_PEAK" )) )
306 p_dst
->pb_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = true;
307 p_dst
->pf_peak
[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( psz_value
);