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, int i_attachments, int *i_type )
31 char *psz_mime = NULL;
33 char *psz_description = NULL;
34 input_attachment_t *p_attachment = NULL;
36 if( i_data < 4 + 3*4 )
38 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
40 *i_type = GetDWBE( p_data ); RM(4);
41 i_len = GetDWBE( p_data ); RM(4);
43 if( i_len < 0 || i_data < i_len + 4 )
45 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
46 i_len = GetDWBE( p_data ); RM(4);
47 if( i_len < 0 || i_data < i_len + 4*4 + 4)
49 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
50 EnsureUTF8( psz_description );
52 i_len = GetDWBE( p_data ); RM(4);
53 if( i_len < 0 || i_len > i_data )
56 /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
57 *i_type, psz_mime, psz_description, i_len ); */
59 snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
60 if( !strcasecmp( psz_mime, "image/jpeg" ) )
61 strcat( psz_name, ".jpg" );
62 else if( !strcasecmp( psz_mime, "image/png" ) )
63 strcat( psz_name, ".png" );
65 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
66 psz_description, p_data, i_data );
70 free( psz_description );
74 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta,
75 const uint8_t *p_data, int i_data,
76 int *i_attachments, input_attachment_t ***attachments,
77 int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
82 seekpoint_t *sk = NULL;
87 n = GetDWLE(p_data); RM(4);
88 if( n < 0 || n > i_data )
93 /* TODO report vendor string ? */
94 char *psz_vendor = psz_vendor = strndup( p_data, n );
103 i_comment = GetDWLE(p_data); RM(4);
108 vlc_meta_t *p_meta = *pp_meta;
110 *pp_meta = p_meta = vlc_meta_New();
115 bool hasTitle = false;
116 bool hasAlbum = false;
117 bool hasTrackNumber = false;
118 bool hasTrackTotal = false;
119 bool hasArtist = false;
120 bool hasCopyright = false;
121 bool hasDescription = false;
122 bool hasGenre = false;
123 bool hasDate = false;
124 bool hasPublisher = false;
126 for( ; i_comment > 0; i_comment-- )
131 n = GetDWLE(p_data); RM(4);
137 psz_comment = strndup( (const char*)p_data, n );
140 EnsureUTF8( psz_comment );
142 #define IF_EXTRACT(txt,var) \
143 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
145 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
146 if( oldval && has##var) \
149 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
151 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
155 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
158 IF_EXTRACT("TITLE=", Title )
159 else IF_EXTRACT("ALBUM=", Album )
160 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
161 else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
162 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
163 else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
164 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
165 else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
166 else IF_EXTRACT("ARTIST=", Artist )
167 else IF_EXTRACT("COPYRIGHT=", Copyright )
168 else IF_EXTRACT("ORGANIZATION=", Publisher )
169 else IF_EXTRACT("DESCRIPTION=", Description )
170 else IF_EXTRACT("COMMENTS=", Description )
171 else IF_EXTRACT("GENRE=", Genre )
172 else IF_EXTRACT("DATE=", Date )
173 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
175 if( attachments == NULL )
180 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
181 input_attachment_t *p_attachment = ParseFlacPicture( p_picture, i_size, i_attach, &i );
185 snprintf( psz_url, sizeof(psz_url), "attachment://%s", p_attachment->psz_name );
186 vlc_meta_Set( p_meta, vlc_meta_ArtworkURL, psz_url );
188 TAB_APPEND_CAST( (input_attachment_t**),
189 *i_attachments, *attachments, p_attachment );
192 else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
194 if( ppp_seekpoint == NULL )
198 if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
200 char *p = strchr( psz_comment, '=' );
202 sk->psz_name = strdup( p );
204 else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
207 char *p = strchr( psz_comment, '=' );
210 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
212 sk = vlc_seekpoint_New();
213 sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
214 TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
218 else if( strchr( psz_comment, '=' ) )
220 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
221 * undocumented tags and replay gain ) */
222 char *p = strchr( psz_comment, '=' );
225 for( int i = 0; psz_comment[i]; i++ )
226 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
227 psz_comment[i] -= 'a' - 'A';
229 vlc_meta_AddExtra( p_meta, psz_comment, p );