1 /*****************************************************************************
2 * vorbis.h: Vorbis Comment parser
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <vlc_charset.h>
25 #include <vlc_strings.h>
27 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data, int i_attachments, int *i_type )
30 char *psz_mime = NULL;
32 char *psz_description = NULL;
33 input_attachment_t *p_attachment = NULL;
35 if( i_data < 4 + 3*4 )
37 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
39 *i_type = GetDWBE( p_data ); RM(4);
40 i_len = GetDWBE( p_data ); RM(4);
42 if( i_len < 0 || i_data < i_len + 4 )
44 psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
45 i_len = GetDWBE( p_data ); RM(4);
46 if( i_len < 0 || i_data < i_len + 4*4 + 4)
48 psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
49 EnsureUTF8( psz_description );
51 i_len = GetDWBE( p_data ); RM(4);
52 if( i_len < 0 || i_len > i_data )
55 /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
56 *i_type, psz_mime, psz_description, i_len ); */
58 snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
59 if( !strcasecmp( psz_mime, "image/jpeg" ) )
60 strcat( psz_name, ".jpg" );
61 else if( !strcasecmp( psz_mime, "image/png" ) )
62 strcat( psz_name, ".png" );
64 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
65 psz_description, p_data, i_data );
69 free( psz_description );
73 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta, const uint8_t *p_data, int i_data,
74 int *i_attachments, input_attachment_t ***attachments)
82 n = GetDWLE(p_data); RM(4);
83 if( n < 0 || n > i_data )
88 /* TODO report vendor string ? */
89 char *psz_vendor = psz_vendor = strndup( p_data, n );
98 i_comment = GetDWLE(p_data); RM(4);
103 vlc_meta_t *p_meta = *pp_meta;
105 *pp_meta = p_meta = vlc_meta_New();
109 bool hasTitle = false;
110 bool hasAlbum = false;
111 bool hasTrackNumber = false;
112 bool hasTrackTotal = false;
113 bool hasArtist = false;
114 bool hasCopyright = false;
115 bool hasDescription = false;
116 bool hasGenre = false;
117 bool hasDate = false;
118 bool hasPublisher = false;
120 for( ; i_comment > 0; i_comment-- )
125 n = GetDWLE(p_data); RM(4);
131 psz_comment = strndup( (const char*)p_data, n );
134 EnsureUTF8( psz_comment );
136 #define IF_EXTRACT(txt,var) \
137 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
139 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
140 if( oldval && has##var) \
143 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
145 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
149 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
152 IF_EXTRACT("TITLE=", Title )
153 else IF_EXTRACT("ALBUM=", Album )
154 else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
155 else IF_EXTRACT("TRACKTOTAL=", TrackTotal )
156 else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
157 else IF_EXTRACT("ARTIST=", Artist )
158 else IF_EXTRACT("COPYRIGHT=", Copyright )
159 else IF_EXTRACT("ORGANIZATION=", Publisher )
160 else IF_EXTRACT("DESCRIPTION=", Description )
161 else if( !hasDescription )
163 IF_EXTRACT("COMMENTS=", Description )
165 else IF_EXTRACT("GENRE=", Genre )
166 else IF_EXTRACT("DATE=", Date )
167 else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
169 if( attachments == NULL )
174 size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
175 input_attachment_t *p_attachment = ParseFlacPicture( p_picture, i_size, i_attach, &i );
179 snprintf( psz_url, sizeof(psz_url), "attachment://%s", p_attachment->psz_name );
180 vlc_meta_Set( p_meta, vlc_meta_ArtworkURL, psz_url );
182 TAB_APPEND_CAST( (input_attachment_t**),
183 *i_attachments, *attachments, p_attachment );
186 else if( strchr( psz_comment, '=' ) )
188 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
189 * undocumented tags and replay gain ) */
190 char *p = strchr( psz_comment, '=' );
192 vlc_meta_AddExtra( p_meta, psz_comment, p );