1 /*****************************************************************************
2 * xiph_metadata.h: Vorbis Comment parser
3 *****************************************************************************
4 * Copyright © 2008-2013 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
31 #include <vlc_strings.h>
32 #include <vlc_input.h>
33 #include "xiph_metadata.h"
35 input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data,
36 int i_attachments, int *i_cover_score, int *i_cover_idx )
38 /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
39 static const char pi_cover_score[] = {
41 5, /* 32x32 PNG image that should be used as the file icon */
42 4, /* File icon of a different size or format. */
43 20, /* Front cover image of the album. */
44 19, /* Back cover image of the album. */
45 13, /* Inside leaflet page of the album. */
46 18, /* Image from the album itself. */
47 17, /* Picture of the lead artist or soloist. */
48 16, /* Picture of the artist or performer. */
49 14, /* Picture of the conductor. */
50 15, /* Picture of the band or orchestra. */
51 9, /* Picture of the composer. */
52 8, /* Picture of the lyricist or text writer. */
53 7, /* Picture of the recording location or studio. */
54 10, /* Picture of the artists during recording. */
55 11, /* Picture of the artists during performance. */
56 6, /* Picture from a movie or video related to the track. */
57 1, /* Picture of a large, coloured fish. */
58 12, /* Illustration related to the track. */
59 3, /* Logo of the band or performer. */
60 2 /* Logo of the publisher (record company). */
65 char *psz_mime = NULL;
67 char *psz_description = NULL;
68 input_attachment_t *p_attachment = NULL;
70 if( i_data < 4 + 3*4 )
72 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
74 i_type = GetDWBE( p_data ); RM(4);
75 i_len = GetDWBE( p_data ); RM(4);
77 if( i_len < 0 || i_data < i_len + 4 )
79 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
80 i_len = GetDWBE( p_data ); RM(4);
81 if( i_len < 0 || i_data < i_len + 4*4 + 4)
83 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
84 EnsureUTF8( psz_description );
86 i_len = GetDWBE( p_data ); RM(4);
87 if( i_len < 0 || i_len > i_data )
90 /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
91 i_type, psz_mime, psz_description, i_len ); */
93 snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
94 if( !strcasecmp( psz_mime, "image/jpeg" ) )
95 strcat( psz_name, ".jpg" );
96 else if( !strcasecmp( psz_mime, "image/png" ) )
97 strcat( psz_name, ".png" );
99 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
100 psz_description, p_data, i_data );
102 if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
103 *i_cover_score < pi_cover_score[i_type] )
105 *i_cover_idx = i_attachments;
106 *i_cover_score = pi_cover_score[i_type];
111 free( psz_description );
115 typedef struct chapters_array_t
118 seekpoint_t ** pp_chapters;
121 static seekpoint_t * getChapterEntry( unsigned int i_index, chapters_array_t *p_array )
123 if ( i_index > 4096 ) return NULL;
124 if ( i_index >= p_array->i_size )
126 unsigned int i_newsize = p_array->i_size;
127 while( i_index >= i_newsize ) i_newsize += 50;
129 if ( !p_array->pp_chapters )
131 p_array->pp_chapters = calloc( i_newsize, sizeof( seekpoint_t * ) );
132 if ( !p_array->pp_chapters ) return NULL;
133 p_array->i_size = i_newsize;
135 seekpoint_t **tmp = calloc( i_newsize, sizeof( seekpoint_t * ) );
136 if ( !tmp ) return NULL;
137 memcpy( tmp, p_array->pp_chapters, p_array->i_size * sizeof( seekpoint_t * ) );
138 free( p_array->pp_chapters );
139 p_array->pp_chapters = tmp;
140 p_array->i_size = i_newsize;
143 if ( !p_array->pp_chapters[i_index] )
144 p_array->pp_chapters[i_index] = vlc_seekpoint_New();
145 return p_array->pp_chapters[i_index];
148 void vorbis_ParseComment( es_format_t *p_fmt, vlc_meta_t **pp_meta,
149 const uint8_t *p_data, int i_data,
150 int *i_attachments, input_attachment_t ***attachments,
151 int *i_cover_score, int *i_cover_idx,
152 int *i_seekpoint, seekpoint_t ***ppp_seekpoint,
153 float (* ppf_replay_gain)[AUDIO_REPLAY_GAIN_MAX],
154 float (* ppf_replay_peak)[AUDIO_REPLAY_GAIN_MAX] )
162 n = GetDWLE(p_data); RM(4);
163 if( n < 0 || n > i_data )
168 /* TODO report vendor string ? */
169 char *psz_vendor = psz_vendor = strndup( p_data, n );
178 i_comment = GetDWLE(p_data); RM(4);
183 vlc_meta_t *p_meta = *pp_meta;
185 *pp_meta = p_meta = vlc_meta_New();
190 bool hasTitle = false;
191 bool hasArtist = false;
192 bool hasGenre = false;
193 bool hasCopyright = false;
194 bool hasAlbum = false;
195 bool hasTrackNum = false;
196 bool hasDescription = false;
197 bool hasRating = false;
198 bool hasDate = false;
199 bool hasLanguage = false;
200 bool hasPublisher = false;
201 bool hasEncodedBy = false;
202 bool hasTrackTotal = false;
204 chapters_array_t chapters_array = { 0, NULL };
206 for( ; i_comment > 0; i_comment-- )
211 n = GetDWLE(p_data); RM(4);
217 psz_comment = strndup( (const char*)p_data, n );
220 EnsureUTF8( psz_comment );
222 #define IF_EXTRACT(txt,var) \
223 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
225 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
226 if( oldval && has##var) \
229 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
231 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
235 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
239 #define IF_EXTRACT_ONCE(txt,var) \
240 if( !strncasecmp(psz_comment, txt, strlen(txt)) && !has##var ) \
242 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
246 #define IF_EXTRACT_FMT(txt,var,fmt,target) \
248 if( fmt && !strncasecmp(psz_comment, txt, strlen(txt)) )\
250 if ( fmt->target ) free( fmt->target );\
251 fmt->target = strdup(&psz_comment[strlen(txt)]);\
254 IF_EXTRACT("TITLE=", Title )
255 else IF_EXTRACT("ARTIST=", Artist )
256 else IF_EXTRACT("GENRE=", Genre )
257 else IF_EXTRACT("COPYRIGHT=", Copyright )
258 else IF_EXTRACT("ALBUM=", Album )
259 else if( !hasTrackNum && !strncasecmp(psz_comment, "TRACKNUMBER=", strlen("TRACKNUMBER=" ) ) )
261 /* Yeah yeah, such a clever idea, let's put xx/xx inside TRACKNUMBER
262 * Oh, and let's not use TRACKTOTAL or TOTALTRACKS... */
263 short unsigned u_track, u_total;
264 if( sscanf( &psz_comment[strlen("TRACKNUMBER=")], "%hu/%hu", &u_track, &u_total ) == 2 )
267 snprintf(str, 6, "%u", u_track);
268 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, str );
270 snprintf(str, 6, "%u", u_total);
271 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, str );
272 hasTrackTotal = true;
276 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, &psz_comment[strlen("TRACKNUMBER=")] );
280 else IF_EXTRACT_ONCE("TRACKTOTAL=", TrackTotal )
281 else IF_EXTRACT_ONCE("TOTALTRACKS=", TrackTotal )
282 else IF_EXTRACT("DESCRIPTION=", Description )
283 else IF_EXTRACT("COMMENT=", Description )
284 else IF_EXTRACT("COMMENTS=", Description )
285 else IF_EXTRACT("RATING=", Rating )
286 else IF_EXTRACT("DATE=", Date )
287 else IF_EXTRACT_FMT("LANGUAGE=", Language, p_fmt, psz_language )
288 else IF_EXTRACT("ORGANIZATION=", Publisher )
289 else IF_EXTRACT("ENCODER=", EncodedBy )
290 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
292 if( attachments == NULL )
296 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
297 input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
298 i_size, *i_attachments, i_cover_score, i_cover_idx );
302 TAB_APPEND_CAST( (input_attachment_t**),
303 *i_attachments, *attachments, p_attachment );
306 else if ( ppf_replay_gain && ppf_replay_peak && !strncmp(psz_comment, "REPLAYGAIN_", 11) )
308 char *p = strchr( psz_comment, '=' );
311 if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_GAIN=", 22) )
313 psz_val = malloc( strlen(p+1) + 1 );
314 if (!psz_val) continue;
315 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
317 (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_val );
321 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_GAIN=", 22) )
323 psz_val = malloc( strlen(p+1) + 1 );
324 if (!psz_val) continue;
325 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
327 (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_val );
331 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_PEAK=", 22) )
333 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
335 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_PEAK=", 22) )
337 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
340 else if( !strncasecmp(psz_comment, "CHAPTER", 7) )
342 unsigned int i_chapt;
343 seekpoint_t *p_seekpoint = NULL;
345 for( int i = 0; psz_comment[i] && psz_comment[i] != '='; i++ )
346 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
347 psz_comment[i] -= 'a' - 'A';
349 if( strstr( psz_comment, "NAME=" ) &&
350 sscanf( psz_comment, "CHAPTER%uNAME=", &i_chapt ) == 1 )
352 char *p = strchr( psz_comment, '=' );
353 p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
354 if ( !p || ! p_seekpoint ) continue;
355 if ( ! p_seekpoint->psz_name )
356 p_seekpoint->psz_name = strdup( ++p );
358 else if( sscanf( psz_comment, "CHAPTER%u=", &i_chapt ) == 1 )
360 unsigned int h, m, s, ms;
361 char *p = strchr( psz_comment, '=' );
362 if( p && sscanf( ++p, "%u:%u:%u.%u", &h, &m, &s, &ms ) == 4 )
364 p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
365 if ( ! p_seekpoint ) continue;
366 p_seekpoint->i_time_offset =
367 (((int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s) * 1000 + ms) * 1000;
371 else if( strchr( psz_comment, '=' ) )
373 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
374 * undocumented tags and replay gain ) */
375 char *p = strchr( psz_comment, '=' );
378 for( int i = 0; psz_comment[i]; i++ )
379 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
380 psz_comment[i] -= 'a' - 'A';
382 vlc_meta_AddExtra( p_meta, psz_comment, p );
389 for ( unsigned int i=0; i<chapters_array.i_size; i++ )
391 if ( !chapters_array.pp_chapters[i] ) continue;
392 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint,
393 chapters_array.pp_chapters[i] );
395 free( chapters_array.pp_chapters );
398 const char *FindKateCategoryName( const char *psz_tag )
400 for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
402 if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
403 return Katei18nCategories[i].psz_i18n;
405 return N_("Unknown category");