1 /*****************************************************************************
2 * vorbis.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 *****************************************************************************/
25 #include <vlc_charset.h>
26 #include <vlc_strings.h>
27 #include <vlc_input.h>
29 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data,
30 int i_attachments, int *i_cover_score, int *i_cover_idx )
32 /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
33 static const char pi_cover_score[] = {
35 5, /* 32x32 PNG image that should be used as the file icon */
36 4, /* File icon of a different size or format. */
37 20, /* Front cover image of the album. */
38 19, /* Back cover image of the album. */
39 13, /* Inside leaflet page of the album. */
40 18, /* Image from the album itself. */
41 17, /* Picture of the lead artist or soloist. */
42 16, /* Picture of the artist or performer. */
43 14, /* Picture of the conductor. */
44 15, /* Picture of the band or orchestra. */
45 9, /* Picture of the composer. */
46 8, /* Picture of the lyricist or text writer. */
47 7, /* Picture of the recording location or studio. */
48 10, /* Picture of the artists during recording. */
49 11, /* Picture of the artists during performance. */
50 6, /* Picture from a movie or video related to the track. */
51 1, /* Picture of a large, coloured fish. */
52 12, /* Illustration related to the track. */
53 3, /* Logo of the band or performer. */
54 2 /* Logo of the publisher (record company). */
59 char *psz_mime = NULL;
61 char *psz_description = NULL;
62 input_attachment_t *p_attachment = NULL;
64 if( i_data < 4 + 3*4 )
66 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
68 i_type = GetDWBE( p_data ); RM(4);
69 i_len = GetDWBE( p_data ); RM(4);
71 if( i_len < 0 || i_data < i_len + 4 )
73 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
74 i_len = GetDWBE( p_data ); RM(4);
75 if( i_len < 0 || i_data < i_len + 4*4 + 4)
77 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
78 EnsureUTF8( psz_description );
80 i_len = GetDWBE( p_data ); RM(4);
81 if( i_len < 0 || i_len > i_data )
84 /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
85 i_type, psz_mime, psz_description, i_len ); */
87 snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
88 if( !strcasecmp( psz_mime, "image/jpeg" ) )
89 strcat( psz_name, ".jpg" );
90 else if( !strcasecmp( psz_mime, "image/png" ) )
91 strcat( psz_name, ".png" );
93 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
94 psz_description, p_data, i_data );
96 if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
97 *i_cover_score < pi_cover_score[i_type] )
99 *i_cover_idx = i_attachments;
100 *i_cover_score = pi_cover_score[i_type];
105 free( psz_description );
109 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta,
110 const uint8_t *p_data, int i_data,
111 int *i_attachments, input_attachment_t ***attachments,
112 int *i_cover_score, int *i_cover_idx,
113 int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
117 seekpoint_t *sk = NULL;
122 n = GetDWLE(p_data); RM(4);
123 if( n < 0 || n > i_data )
128 /* TODO report vendor string ? */
129 char *psz_vendor = psz_vendor = strndup( p_data, n );
138 i_comment = GetDWLE(p_data); RM(4);
143 vlc_meta_t *p_meta = *pp_meta;
145 *pp_meta = p_meta = vlc_meta_New();
150 bool hasTitle = false;
151 bool hasAlbum = false;
152 bool hasTrackNumber = false;
153 bool hasTrackTotal = false;
154 bool hasArtist = false;
155 bool hasCopyright = false;
156 bool hasDescription = false;
157 bool hasGenre = false;
158 bool hasDate = false;
159 bool hasPublisher = false;
161 for( ; i_comment > 0; i_comment-- )
166 n = GetDWLE(p_data); RM(4);
172 psz_comment = strndup( (const char*)p_data, n );
175 EnsureUTF8( psz_comment );
177 #define IF_EXTRACT(txt,var) \
178 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
180 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
181 if( oldval && has##var) \
184 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
186 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
190 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
193 IF_EXTRACT("TITLE=", Title )
194 else IF_EXTRACT("ALBUM=", Album )
195 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
196 else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
197 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
198 else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
199 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
200 else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
201 else IF_EXTRACT("ARTIST=", Artist )
202 else IF_EXTRACT("COPYRIGHT=", Copyright )
203 else IF_EXTRACT("ORGANIZATION=", Publisher )
204 else IF_EXTRACT("DESCRIPTION=", Description )
205 else IF_EXTRACT("COMMENTS=", Description )
206 else IF_EXTRACT("GENRE=", Genre )
207 else IF_EXTRACT("DATE=", Date )
208 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
210 if( attachments == NULL )
214 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
215 input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
216 i_size, *i_attachments, i_cover_score, i_cover_idx );
220 TAB_APPEND_CAST( (input_attachment_t**),
221 *i_attachments, *attachments, p_attachment );
224 else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
226 if( ppp_seekpoint == NULL )
230 if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
232 char *p = strchr( psz_comment, '=' );
234 sk->psz_name = strdup( p );
236 else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
239 char *p = strchr( psz_comment, '=' );
242 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
244 sk = vlc_seekpoint_New();
245 sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
246 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
250 else if( strchr( psz_comment, '=' ) )
252 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
253 * undocumented tags and replay gain ) */
254 char *p = strchr( psz_comment, '=' );
257 for( int i = 0; psz_comment[i]; i++ )
258 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
259 psz_comment[i] -= 'a' - 'A';
261 vlc_meta_AddExtra( p_meta, psz_comment, p );
269 static const struct {
271 const char *psz_i18n;
272 } Katei18nCategories[] = {
273 /* From Silvia's Mozilla list */
274 { "CC", N_("Closed captions") },
275 { "SUB", N_("Subtitles") },
276 { "TAD", N_("Textual audio descriptions") },
277 { "KTV", N_("Karaoke") },
278 { "TIK", N_("Ticker text") },
279 { "AR", N_("Active regions") },
280 { "NB", N_("Semantic annotations") },
281 { "META", N_("Metadata") },
282 { "TRX", N_("Transcript") },
283 { "LRC", N_("Lyrics") },
284 { "LIN", N_("Linguistic markup") },
285 { "CUE", N_("Cue points") },
288 { "subtitles", N_("Subtitles") },
289 { "spu-subtitles", N_("Subtitles (images)") },
290 { "lyrics", N_("Lyrics") },
293 { "K-SPU", N_("Subtitles (images)") },
294 { "K-SLD-T", N_("Slides (text)") },
295 { "K-SLD-I", N_("Slides (images)") },
298 const char *FindKateCategoryName( const char *psz_tag )
300 for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
302 if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
303 return Katei18nCategories[i].psz_i18n;
305 return N_("Unknown category");