1 /*****************************************************************************
2 * vorbis.h: Vorbis Comment parser
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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 *****************************************************************************/
24 #include <vlc_charset.h>
25 #include <vlc_strings.h>
26 #include <vlc_input.h>
28 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data,
29 int i_attachments, int *i_cover_score, int *i_cover_idx )
31 static const char pi_cover_score[] = {
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 6, /* movie/video screen capture */
40 8, /* Band/Artist logotype */
41 0, /* Publisher/Studio */
46 char *psz_mime = NULL;
48 char *psz_description = NULL;
49 input_attachment_t *p_attachment = NULL;
51 if( i_data < 4 + 3*4 )
53 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
55 i_type = GetDWBE( p_data ); RM(4);
56 i_len = GetDWBE( p_data ); RM(4);
58 if( i_len < 0 || i_data < i_len + 4 )
60 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
61 i_len = GetDWBE( p_data ); RM(4);
62 if( i_len < 0 || i_data < i_len + 4*4 + 4)
64 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
65 EnsureUTF8( psz_description );
67 i_len = GetDWBE( p_data ); RM(4);
68 if( i_len < 0 || i_len > i_data )
71 /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
72 i_type, psz_mime, psz_description, i_len ); */
74 snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
75 if( !strcasecmp( psz_mime, "image/jpeg" ) )
76 strcat( psz_name, ".jpg" );
77 else if( !strcasecmp( psz_mime, "image/png" ) )
78 strcat( psz_name, ".png" );
80 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
81 psz_description, p_data, i_data );
83 if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
84 *i_cover_score < pi_cover_score[i_type] )
86 *i_cover_idx = i_attachments;
87 *i_cover_score = pi_cover_score[i_type];
92 free( psz_description );
96 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta,
97 const uint8_t *p_data, int i_data,
98 int *i_attachments, input_attachment_t ***attachments,
99 int *i_cover_score, int *i_cover_idx,
100 int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
104 seekpoint_t *sk = NULL;
109 n = GetDWLE(p_data); RM(4);
110 if( n < 0 || n > i_data )
115 /* TODO report vendor string ? */
116 char *psz_vendor = psz_vendor = strndup( p_data, n );
125 i_comment = GetDWLE(p_data); RM(4);
130 vlc_meta_t *p_meta = *pp_meta;
132 *pp_meta = p_meta = vlc_meta_New();
137 bool hasTitle = false;
138 bool hasAlbum = false;
139 bool hasTrackNumber = false;
140 bool hasTrackTotal = false;
141 bool hasArtist = false;
142 bool hasCopyright = false;
143 bool hasDescription = false;
144 bool hasGenre = false;
145 bool hasDate = false;
146 bool hasPublisher = false;
148 for( ; i_comment > 0; i_comment-- )
153 n = GetDWLE(p_data); RM(4);
159 psz_comment = strndup( (const char*)p_data, n );
162 EnsureUTF8( psz_comment );
164 #define IF_EXTRACT(txt,var) \
165 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
167 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
168 if( oldval && has##var) \
171 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
173 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
177 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
180 IF_EXTRACT("TITLE=", Title )
181 else IF_EXTRACT("ALBUM=", Album )
182 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
183 else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
184 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
185 else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
186 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
187 else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
188 else IF_EXTRACT("ARTIST=", Artist )
189 else IF_EXTRACT("COPYRIGHT=", Copyright )
190 else IF_EXTRACT("ORGANIZATION=", Publisher )
191 else IF_EXTRACT("DESCRIPTION=", Description )
192 else IF_EXTRACT("COMMENTS=", Description )
193 else IF_EXTRACT("GENRE=", Genre )
194 else IF_EXTRACT("DATE=", Date )
195 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
197 if( attachments == NULL )
201 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
202 input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
203 i_size, *i_attachments, i_cover_score, i_cover_idx );
207 TAB_APPEND_CAST( (input_attachment_t**),
208 *i_attachments, *attachments, p_attachment );
211 else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
213 if( ppp_seekpoint == NULL )
217 if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
219 char *p = strchr( psz_comment, '=' );
221 sk->psz_name = strdup( p );
223 else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
226 char *p = strchr( psz_comment, '=' );
229 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
231 sk = vlc_seekpoint_New();
232 sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
233 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
237 else if( strchr( psz_comment, '=' ) )
239 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
240 * undocumented tags and replay gain ) */
241 char *p = strchr( psz_comment, '=' );
244 for( int i = 0; psz_comment[i]; i++ )
245 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
246 psz_comment[i] -= 'a' - 'A';
248 vlc_meta_AddExtra( p_meta, psz_comment, p );