]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Allow vorbis.h to be included in C++
[vlc] / modules / demux / vorbis.h
1 /*****************************************************************************
2  * vorbis.h: Vorbis Comment parser
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc_charset.h>
25 #include <vlc_strings.h>
26
27 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data, int i_attachments, int *i_type )
28 {
29     int i_len;
30     char *psz_mime = NULL;
31     char psz_name[128];
32     char *psz_description = NULL;
33     input_attachment_t *p_attachment = NULL;
34
35     if( i_data < 4 + 3*4 )
36         return NULL;
37 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
38
39     *i_type = GetDWBE( p_data ); RM(4);
40     i_len = GetDWBE( p_data ); RM(4);
41
42     if( i_len < 0 || i_data < i_len + 4 )
43         goto error;
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)
47         goto error;
48     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
49     EnsureUTF8( psz_description );
50     RM(4*4);
51     i_len = GetDWBE( p_data ); RM(4);
52     if( i_len < 0 || i_len > i_data )
53         goto error;
54
55     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
56              *i_type, psz_mime, psz_description, i_len ); */
57
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" );
63
64     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
65             psz_description, p_data, i_data );
66
67 error:
68     free( psz_mime );
69     free( psz_description );
70     return p_attachment;
71 }
72
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)
75 {
76     int n;
77     int i_comment;
78     int i_attach = 0;
79     if( i_data < 8 )
80         return;
81
82     n = GetDWLE(p_data); RM(4);
83     if( n < 0 || n > i_data )
84         return;
85 #if 0
86     if( n > 0 )
87     {
88         /* TODO report vendor string ? */
89         char *psz_vendor = psz_vendor = strndup( p_data, n );
90         free( psz_vendor );
91     }
92 #endif
93     RM(n);
94
95     if( i_data < 4 )
96         return;
97
98     i_comment = GetDWLE(p_data); RM(4);
99     if( i_comment <= 0 )
100         return;
101
102     /* */
103     vlc_meta_t *p_meta = *pp_meta;
104     if( !p_meta )
105         *pp_meta = p_meta = vlc_meta_New();
106     if( !p_meta )
107         return;
108
109     bool hasTitle = false;
110     bool hasAlbum = false;
111     bool hasTrackNumber = false;
112     bool hasArtist = false;
113     bool hasCopyright = false;
114     bool hasDescription = false;
115     bool hasGenre = false;
116     bool hasDate = false;
117     bool hasPublisher = false;
118
119     for( ; i_comment > 0; i_comment-- )
120     {
121         char *psz;
122         if( i_data < 4 )
123             break;
124         n = GetDWLE(p_data); RM(4);
125         if( n > i_data )
126             break;
127         if( n <= 0 )
128             continue;
129
130         psz = strndup( (const char*)p_data, n );
131         RM(n);
132
133         EnsureUTF8( psz );
134
135 #define IF_EXTRACT(txt,var) \
136     if( !strncasecmp(psz, txt, strlen(txt)) ) \
137     { \
138         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
139         if( oldval && has##var) \
140         { \
141             char * newval; \
142             if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \
143                 newval = NULL; \
144             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
145             free( newval ); \
146         } \
147         else \
148             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \
149         has##var = true; \
150     }
151         IF_EXTRACT("TITLE=", Title )
152         else IF_EXTRACT("ALBUM=", Album )
153         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
154         else IF_EXTRACT("ARTIST=", Artist )
155         else IF_EXTRACT("COPYRIGHT=", Copyright )
156         else IF_EXTRACT("ORGANIZATION=", Publisher )
157         else IF_EXTRACT("DESCRIPTION=", Description )
158         else IF_EXTRACT("GENRE=", Genre )
159         else IF_EXTRACT("DATE=", Date )
160         else if( !strncasecmp( psz, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
161         {
162             if( attachments == NULL )
163                 continue;
164
165             int i;
166             uint8_t *p_picture;
167             size_t i_size = vlc_b64_decode_binary( &p_picture, &psz[strlen("METADATA_BLOCK_PICTURE=")]);
168             input_attachment_t *p_attachment = ParseFlacPicture( p_picture, i_size, i_attach, &i );
169             if( p_attachment )
170             {
171                 char psz_url[128];
172                 snprintf( psz_url, sizeof(psz_url), "attachment://%s", p_attachment->psz_name );
173                 vlc_meta_Set( p_meta, vlc_meta_ArtworkURL, psz_url );
174                 i_attach++;
175                 TAB_APPEND_CAST( (input_attachment_t**),
176                     *i_attachments, *attachments, p_attachment );
177             }
178         }
179         else if( strchr( psz, '=' ) )
180         {
181             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
182              * undocumented tags and replay gain ) */
183             char *p = strchr( psz, '=' );
184             *p++ = '\0';
185             vlc_meta_AddExtra( p_meta, psz, p );
186         }
187 #undef IF_EXTRACT
188         free( psz );
189     }
190 #undef RM
191 }
192