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( 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 IF_EXTRACT("TITLE=", Title )
247 else IF_EXTRACT("ARTIST=", Artist )
248 else IF_EXTRACT("GENRE=", Genre )
249 else IF_EXTRACT("COPYRIGHT=", Copyright )
250 else IF_EXTRACT("ALBUM=", Album )
251 else if( !hasTrackNum && !strncasecmp(psz_comment, "TRACKNUMBER=", strlen("TRACKNUMBER=" ) ) )
253 /* Yeah yeah, such a clever idea, let's put xx/xx inside TRACKNUMBER
254 * Oh, and let's not use TRACKTOTAL or TOTALTRACKS... */
255 short unsigned u_track, u_total;
256 if( sscanf( &psz_comment[strlen("TRACKNUMBER=")], "%hu/%hu", &u_track, &u_total ) == 2 )
259 snprintf(str, 6, "%u", u_track);
260 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, str );
262 snprintf(str, 6, "%u", u_total);
263 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, str );
264 hasTrackTotal = true;
268 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, &psz_comment[strlen("TRACKNUMBER=")] );
272 else IF_EXTRACT_ONCE("TRACKTOTAL=", TrackTotal )
273 else IF_EXTRACT_ONCE("TOTALTRACKS=", TrackTotal )
274 else IF_EXTRACT("DESCRIPTION=", Description )
275 else IF_EXTRACT("COMMENT=", Description )
276 else IF_EXTRACT("COMMENTS=", Description )
277 else IF_EXTRACT("RATING=", Rating )
278 else IF_EXTRACT("DATE=", Date )
279 else IF_EXTRACT("LANGUAGE=", Language )
280 else IF_EXTRACT("ORGANIZATION=", Publisher )
281 else IF_EXTRACT("ENCODER=", EncodedBy )
282 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
284 if( attachments == NULL )
288 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
289 input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
290 i_size, *i_attachments, i_cover_score, i_cover_idx );
294 TAB_APPEND_CAST( (input_attachment_t**),
295 *i_attachments, *attachments, p_attachment );
298 else if ( ppf_replay_gain && ppf_replay_peak && !strncmp(psz_comment, "REPLAYGAIN_", 11) )
300 char *p = strchr( psz_comment, '=' );
303 if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_GAIN=", 22) )
305 psz_val = malloc( strlen(p+1) + 1 );
306 if (!psz_val) continue;
307 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
309 (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_val );
313 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_GAIN=", 22) )
315 psz_val = malloc( strlen(p+1) + 1 );
316 if (!psz_val) continue;
317 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
319 (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_val );
323 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_PEAK=", 22) )
325 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
327 else if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_PEAK=", 22) )
329 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
332 else if( !strncasecmp(psz_comment, "CHAPTER", 7) )
334 unsigned int i_chapt;
335 seekpoint_t *p_seekpoint = NULL;
337 for( int i = 0; psz_comment[i] && psz_comment[i] != '='; i++ )
338 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
339 psz_comment[i] -= 'a' - 'A';
341 if( strstr( psz_comment, "NAME=" ) &&
342 sscanf( psz_comment, "CHAPTER%uNAME=", &i_chapt ) == 1 )
344 char *p = strchr( psz_comment, '=' );
345 p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
346 if ( !p || ! p_seekpoint ) continue;
347 if ( ! p_seekpoint->psz_name )
348 p_seekpoint->psz_name = strdup( ++p );
350 else if( sscanf( psz_comment, "CHAPTER%u=", &i_chapt ) == 1 )
352 unsigned int h, m, s, ms;
353 char *p = strchr( psz_comment, '=' );
354 if( p && sscanf( ++p, "%u:%u:%u.%u", &h, &m, &s, &ms ) == 4 )
356 p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
357 if ( ! p_seekpoint ) continue;
358 p_seekpoint->i_time_offset =
359 (((int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s) * 1000 + ms) * 1000;
363 else if( strchr( psz_comment, '=' ) )
365 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
366 * undocumented tags and replay gain ) */
367 char *p = strchr( psz_comment, '=' );
370 for( int i = 0; psz_comment[i]; i++ )
371 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
372 psz_comment[i] -= 'a' - 'A';
374 vlc_meta_AddExtra( p_meta, psz_comment, p );
381 for ( unsigned int i=0; i<chapters_array.i_size; i++ )
383 if ( !chapters_array.pp_chapters[i] ) continue;
384 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint,
385 chapters_array.pp_chapters[i] );
387 free( chapters_array.pp_chapters );
390 const char *FindKateCategoryName( const char *psz_tag )
392 for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
394 if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
395 return Katei18nCategories[i].psz_i18n;
397 return N_("Unknown category");