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 void vorbis_ParseComment( vlc_meta_t **pp_meta,
116 const uint8_t *p_data, int i_data,
117 int *i_attachments, input_attachment_t ***attachments,
118 int *i_cover_score, int *i_cover_idx,
119 int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
123 seekpoint_t *sk = NULL;
128 n = GetDWLE(p_data); RM(4);
129 if( n < 0 || n > i_data )
134 /* TODO report vendor string ? */
135 char *psz_vendor = psz_vendor = strndup( p_data, n );
144 i_comment = GetDWLE(p_data); RM(4);
149 vlc_meta_t *p_meta = *pp_meta;
151 *pp_meta = p_meta = vlc_meta_New();
156 bool hasTitle = false;
157 bool hasAlbum = false;
158 bool hasTrackNumber = false;
159 bool hasTrackTotal = false;
160 bool hasArtist = false;
161 bool hasCopyright = false;
162 bool hasDescription = false;
163 bool hasGenre = false;
164 bool hasDate = false;
165 bool hasPublisher = false;
167 for( ; i_comment > 0; i_comment-- )
172 n = GetDWLE(p_data); RM(4);
178 psz_comment = strndup( (const char*)p_data, n );
181 EnsureUTF8( psz_comment );
183 #define IF_EXTRACT(txt,var) \
184 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
186 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
187 if( oldval && has##var) \
190 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
192 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
196 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
199 IF_EXTRACT("TITLE=", Title )
200 else IF_EXTRACT("ALBUM=", Album )
201 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
202 else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
203 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
204 else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
205 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
206 else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
207 else IF_EXTRACT("ARTIST=", Artist )
208 else IF_EXTRACT("COPYRIGHT=", Copyright )
209 else IF_EXTRACT("ORGANIZATION=", Publisher )
210 else IF_EXTRACT("DESCRIPTION=", Description )
211 else IF_EXTRACT("COMMENTS=", Description )
212 else IF_EXTRACT("GENRE=", Genre )
213 else IF_EXTRACT("DATE=", Date )
214 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
216 if( attachments == NULL )
220 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
221 input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
222 i_size, *i_attachments, i_cover_score, i_cover_idx );
226 TAB_APPEND_CAST( (input_attachment_t**),
227 *i_attachments, *attachments, p_attachment );
230 else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
232 if( ppp_seekpoint == NULL )
236 if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
238 char *p = strchr( psz_comment, '=' );
240 sk->psz_name = strdup( p );
242 else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
245 char *p = strchr( psz_comment, '=' );
248 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
250 sk = vlc_seekpoint_New();
251 sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
252 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
256 else if( strchr( psz_comment, '=' ) )
258 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
259 * undocumented tags and replay gain ) */
260 char *p = strchr( psz_comment, '=' );
263 for( int i = 0; psz_comment[i]; i++ )
264 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
265 psz_comment[i] -= 'a' - 'A';
267 vlc_meta_AddExtra( p_meta, psz_comment, p );
275 const char *FindKateCategoryName( const char *psz_tag )
277 for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
279 if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
280 return Katei18nCategories[i].psz_i18n;
282 return N_("Unknown category");