]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Qt: EPGView: includes cleanup
[vlc] / modules / demux / vorbis.h
1 /*****************************************************************************
2  * vorbis.h: Vorbis Comment parser
3  *****************************************************************************
4  * Copyright (C) 2008 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
23
24 #include <vlc_charset.h>
25 #include <vlc_strings.h>
26 #include <vlc_input.h>
27
28 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data, int i_attachments, int *i_type )
29 {
30     int i_len;
31     char *psz_mime = NULL;
32     char psz_name[128];
33     char *psz_description = NULL;
34     input_attachment_t *p_attachment = NULL;
35
36     if( i_data < 4 + 3*4 )
37         return NULL;
38 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
39
40     *i_type = GetDWBE( p_data ); RM(4);
41     i_len = GetDWBE( p_data ); RM(4);
42
43     if( i_len < 0 || i_data < i_len + 4 )
44         goto error;
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)
48         goto error;
49     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
50     EnsureUTF8( psz_description );
51     RM(4*4);
52     i_len = GetDWBE( p_data ); RM(4);
53     if( i_len < 0 || i_len > i_data )
54         goto error;
55
56     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
57              *i_type, psz_mime, psz_description, i_len ); */
58
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" );
64
65     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
66             psz_description, p_data, i_data );
67
68 error:
69     free( psz_mime );
70     free( psz_description );
71     return p_attachment;
72 }
73
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 )
78 {
79     int n;
80     int i_comment;
81     int i_attach = 0;
82     seekpoint_t *sk = NULL;
83
84     if( i_data < 8 )
85         return;
86
87     n = GetDWLE(p_data); RM(4);
88     if( n < 0 || n > i_data )
89         return;
90 #if 0
91     if( n > 0 )
92     {
93         /* TODO report vendor string ? */
94         char *psz_vendor = psz_vendor = strndup( p_data, n );
95         free( psz_vendor );
96     }
97 #endif
98     RM(n);
99
100     if( i_data < 4 )
101         return;
102
103     i_comment = GetDWLE(p_data); RM(4);
104     if( i_comment <= 0 )
105         return;
106
107     /* */
108     vlc_meta_t *p_meta = *pp_meta;
109     if( !p_meta )
110         *pp_meta = p_meta = vlc_meta_New();
111     if( !p_meta )
112         return;
113
114     /* */
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;
125
126     for( ; i_comment > 0; i_comment-- )
127     {
128         char *psz_comment;
129         if( i_data < 4 )
130             break;
131         n = GetDWLE(p_data); RM(4);
132         if( n > i_data )
133             break;
134         if( n <= 0 )
135             continue;
136
137         psz_comment = strndup( (const char*)p_data, n );
138         RM(n);
139
140         EnsureUTF8( psz_comment );
141
142 #define IF_EXTRACT(txt,var) \
143     if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
144     { \
145         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
146         if( oldval && has##var) \
147         { \
148             char * newval; \
149             if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
150                 newval = NULL; \
151             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
152             free( newval ); \
153         } \
154         else \
155             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
156         has##var = true; \
157     }
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=")))
174         {
175             if( attachments == NULL )
176                 continue;
177
178             int i;
179             uint8_t *p_picture;
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 );
182             if( p_attachment )
183             {
184                 char psz_url[128];
185                 snprintf( psz_url, sizeof(psz_url), "attachment://%s", p_attachment->psz_name );
186                 vlc_meta_Set( p_meta, vlc_meta_ArtworkURL, psz_url );
187                 i_attach++;
188                 TAB_APPEND_CAST( (input_attachment_t**),
189                     *i_attachments, *attachments, p_attachment );
190             }
191         }
192         else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
193         {
194             if( ppp_seekpoint == NULL )
195                 continue;
196
197             int i_chapt;
198             if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
199             {
200                 char *p = strchr( psz_comment, '=' );
201                 *p++ = '\0';
202                 sk->psz_name = strdup( p );
203             }
204             else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
205             {
206                 int h, m, s, ms;
207                 char *p = strchr( psz_comment, '=' );
208                 *p++ = '\0';
209
210                 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
211                 {
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 );
215                 }
216             }
217         }
218         else if( strchr( psz_comment, '=' ) )
219         {
220             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
221              * undocumented tags and replay gain ) */
222             char *p = strchr( psz_comment, '=' );
223             *p++ = '\0';
224
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';
228
229             vlc_meta_AddExtra( p_meta, psz_comment, p );
230         }
231 #undef IF_EXTRACT
232         free( psz_comment );
233     }
234 #undef RM
235 }
236