]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Use FLAC's picture selection for Vorbis/Opus.
[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,
29     int i_attachments, int *i_cover_score, int *i_cover_idx )
30 {
31     static const char pi_cover_score[] = {
32         0,      /* other */
33         2, 1,   /* icons */
34         10,     /* front cover */
35         9,      /* back cover */
36         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37         6,      /* movie/video screen capture */
38         0,
39         7,      /* Illustration */
40         8,      /* Band/Artist logotype */
41         0,      /* Publisher/Studio */
42     };
43
44     int i_len;
45     int i_type;
46     char *psz_mime = NULL;
47     char psz_name[128];
48     char *psz_description = NULL;
49     input_attachment_t *p_attachment = NULL;
50
51     if( i_data < 4 + 3*4 )
52         return NULL;
53 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
54
55     i_type = GetDWBE( p_data ); RM(4);
56     i_len = GetDWBE( p_data ); RM(4);
57
58     if( i_len < 0 || i_data < i_len + 4 )
59         goto error;
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)
63         goto error;
64     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
65     EnsureUTF8( psz_description );
66     RM(4*4);
67     i_len = GetDWBE( p_data ); RM(4);
68     if( i_len < 0 || i_len > i_data )
69         goto error;
70
71     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
72              i_type, psz_mime, psz_description, i_len ); */
73
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" );
79
80     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
81             psz_description, p_data, i_data );
82
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] )
85     {
86         *i_cover_idx = i_attachments;
87         *i_cover_score = pi_cover_score[i_type];
88     }
89
90 error:
91     free( psz_mime );
92     free( psz_description );
93     return p_attachment;
94 }
95
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 )
101 {
102     int n;
103     int i_comment;
104     seekpoint_t *sk = NULL;
105
106     if( i_data < 8 )
107         return;
108
109     n = GetDWLE(p_data); RM(4);
110     if( n < 0 || n > i_data )
111         return;
112 #if 0
113     if( n > 0 )
114     {
115         /* TODO report vendor string ? */
116         char *psz_vendor = psz_vendor = strndup( p_data, n );
117         free( psz_vendor );
118     }
119 #endif
120     RM(n);
121
122     if( i_data < 4 )
123         return;
124
125     i_comment = GetDWLE(p_data); RM(4);
126     if( i_comment <= 0 )
127         return;
128
129     /* */
130     vlc_meta_t *p_meta = *pp_meta;
131     if( !p_meta )
132         *pp_meta = p_meta = vlc_meta_New();
133     if( !p_meta )
134         return;
135
136     /* */
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;
147
148     for( ; i_comment > 0; i_comment-- )
149     {
150         char *psz_comment;
151         if( i_data < 4 )
152             break;
153         n = GetDWLE(p_data); RM(4);
154         if( n > i_data )
155             break;
156         if( n <= 0 )
157             continue;
158
159         psz_comment = strndup( (const char*)p_data, n );
160         RM(n);
161
162         EnsureUTF8( psz_comment );
163
164 #define IF_EXTRACT(txt,var) \
165     if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
166     { \
167         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
168         if( oldval && has##var) \
169         { \
170             char * newval; \
171             if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
172                 newval = NULL; \
173             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
174             free( newval ); \
175         } \
176         else \
177             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
178         has##var = true; \
179     }
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=")))
196         {
197             if( attachments == NULL )
198                 continue;
199
200             uint8_t *p_picture;
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 );
204             free( p_picture );
205             if( p_attachment )
206             {
207                 TAB_APPEND_CAST( (input_attachment_t**),
208                     *i_attachments, *attachments, p_attachment );
209             }
210         }
211         else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
212         {
213             if( ppp_seekpoint == NULL )
214                 continue;
215
216             int i_chapt;
217             if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
218             {
219                 char *p = strchr( psz_comment, '=' );
220                 *p++ = '\0';
221                 sk->psz_name = strdup( p );
222             }
223             else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
224             {
225                 int h, m, s, ms;
226                 char *p = strchr( psz_comment, '=' );
227                 *p++ = '\0';
228
229                 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
230                 {
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 );
234                 }
235             }
236         }
237         else if( strchr( psz_comment, '=' ) )
238         {
239             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
240              * undocumented tags and replay gain ) */
241             char *p = strchr( psz_comment, '=' );
242             *p++ = '\0';
243
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';
247
248             vlc_meta_AddExtra( p_meta, psz_comment, p );
249         }
250 #undef IF_EXTRACT
251         free( psz_comment );
252     }
253 #undef RM
254 }
255