]> git.sesse.net Git - vlc/blob - modules/demux/xiph_metadata.c
vdpau: add some sanity checking assertion
[vlc] / modules / demux / xiph_metadata.c
1 /*****************************************************************************
2  * xiph_metadata.h: Vorbis Comment parser
3  *****************************************************************************
4  * Copyright © 2008-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
31 #include <vlc_strings.h>
32 #include <vlc_input.h>
33 #include "xiph_metadata.h"
34
35 input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data,
36     int i_attachments, int *i_cover_score, int *i_cover_idx )
37 {
38     /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
39     static const char pi_cover_score[] = {
40         0,  /* Other */
41         5,  /* 32x32 PNG image that should be used as the file icon */
42         4,  /* File icon of a different size or format. */
43         20, /* Front cover image of the album. */
44         19, /* Back cover image of the album. */
45         13, /* Inside leaflet page of the album. */
46         18, /* Image from the album itself. */
47         17, /* Picture of the lead artist or soloist. */
48         16, /* Picture of the artist or performer. */
49         14, /* Picture of the conductor. */
50         15, /* Picture of the band or orchestra. */
51         9,  /* Picture of the composer. */
52         8,  /* Picture of the lyricist or text writer. */
53         7,  /* Picture of the recording location or studio. */
54         10, /* Picture of the artists during recording. */
55         11, /* Picture of the artists during performance. */
56         6,  /* Picture from a movie or video related to the track. */
57         1,  /* Picture of a large, coloured fish. */
58         12, /* Illustration related to the track. */
59         3,  /* Logo of the band or performer. */
60         2   /* Logo of the publisher (record company). */
61     };
62
63     int i_len;
64     int i_type;
65     char *psz_mime = NULL;
66     char psz_name[128];
67     char *psz_description = NULL;
68     input_attachment_t *p_attachment = NULL;
69
70     if( i_data < 4 + 3*4 )
71         return NULL;
72 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
73
74     i_type = GetDWBE( p_data ); RM(4);
75     i_len = GetDWBE( p_data ); RM(4);
76
77     if( i_len < 0 || i_data < i_len + 4 )
78         goto error;
79     psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
80     i_len = GetDWBE( p_data ); RM(4);
81     if( i_len < 0 || i_data < i_len + 4*4 + 4)
82         goto error;
83     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
84     EnsureUTF8( psz_description );
85     RM(4*4);
86     i_len = GetDWBE( p_data ); RM(4);
87     if( i_len < 0 || i_len > i_data )
88         goto error;
89
90     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
91              i_type, psz_mime, psz_description, i_len ); */
92
93     snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
94     if( !strcasecmp( psz_mime, "image/jpeg" ) )
95         strcat( psz_name, ".jpg" );
96     else if( !strcasecmp( psz_mime, "image/png" ) )
97         strcat( psz_name, ".png" );
98
99     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
100             psz_description, p_data, i_data );
101
102     if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
103         *i_cover_score < pi_cover_score[i_type] )
104     {
105         *i_cover_idx = i_attachments;
106         *i_cover_score = pi_cover_score[i_type];
107     }
108
109 error:
110     free( psz_mime );
111     free( psz_description );
112     return p_attachment;
113 }
114
115 void vorbis_ParseComment( vlc_meta_t **pp_meta,
116         const uint8_t *p_data, int i_data,
117         int *i_attachments, input_attachment_t ***attachments,
118         int *i_cover_score, int *i_cover_idx,
119         int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
120 {
121     int n;
122     int i_comment;
123     seekpoint_t *sk = NULL;
124
125     if( i_data < 8 )
126         return;
127
128     n = GetDWLE(p_data); RM(4);
129     if( n < 0 || n > i_data )
130         return;
131 #if 0
132     if( n > 0 )
133     {
134         /* TODO report vendor string ? */
135         char *psz_vendor = psz_vendor = strndup( p_data, n );
136         free( psz_vendor );
137     }
138 #endif
139     RM(n);
140
141     if( i_data < 4 )
142         return;
143
144     i_comment = GetDWLE(p_data); RM(4);
145     if( i_comment <= 0 )
146         return;
147
148     /* */
149     vlc_meta_t *p_meta = *pp_meta;
150     if( !p_meta )
151         *pp_meta = p_meta = vlc_meta_New();
152     if( !p_meta )
153         return;
154
155     /* */
156     bool hasTitle        = false;
157     bool hasAlbum        = false;
158     bool hasTrackNumber  = false;
159     bool hasTrackTotal   = false;
160     bool hasArtist       = false;
161     bool hasCopyright    = false;
162     bool hasDescription  = false;
163     bool hasGenre        = false;
164     bool hasDate         = false;
165     bool hasPublisher    = false;
166
167     for( ; i_comment > 0; i_comment-- )
168     {
169         char *psz_comment;
170         if( i_data < 4 )
171             break;
172         n = GetDWLE(p_data); RM(4);
173         if( n > i_data )
174             break;
175         if( n <= 0 )
176             continue;
177
178         psz_comment = strndup( (const char*)p_data, n );
179         RM(n);
180
181         EnsureUTF8( psz_comment );
182
183 #define IF_EXTRACT(txt,var) \
184     if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
185     { \
186         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
187         if( oldval && has##var) \
188         { \
189             char * newval; \
190             if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
191                 newval = NULL; \
192             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
193             free( newval ); \
194         } \
195         else \
196             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
197         has##var = true; \
198     }
199         IF_EXTRACT("TITLE=", Title )
200         else IF_EXTRACT("ALBUM=", Album )
201         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
202         else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
203             vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
204         else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
205             vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
206         else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
207         else IF_EXTRACT("ARTIST=", Artist )
208         else IF_EXTRACT("COPYRIGHT=", Copyright )
209         else IF_EXTRACT("ORGANIZATION=", Publisher )
210         else IF_EXTRACT("DESCRIPTION=", Description )
211         else IF_EXTRACT("COMMENTS=", Description )
212         else IF_EXTRACT("GENRE=", Genre )
213         else IF_EXTRACT("DATE=", Date )
214         else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
215         {
216             if( attachments == NULL )
217                 continue;
218
219             uint8_t *p_picture;
220             size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
221             input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
222                 i_size, *i_attachments, i_cover_score, i_cover_idx );
223             free( p_picture );
224             if( p_attachment )
225             {
226                 TAB_APPEND_CAST( (input_attachment_t**),
227                     *i_attachments, *attachments, p_attachment );
228             }
229         }
230         else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
231         {
232             if( ppp_seekpoint == NULL )
233                 continue;
234
235             int i_chapt;
236             if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
237             {
238                 char *p = strchr( psz_comment, '=' );
239                 *p++ = '\0';
240                 sk->psz_name = strdup( p );
241             }
242             else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
243             {
244                 int h, m, s, ms;
245                 char *p = strchr( psz_comment, '=' );
246                 *p++ = '\0';
247
248                 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
249                 {
250                     sk = vlc_seekpoint_New();
251                     sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
252                     TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
253                 }
254             }
255         }
256         else if( strchr( psz_comment, '=' ) )
257         {
258             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
259              * undocumented tags and replay gain ) */
260             char *p = strchr( psz_comment, '=' );
261             *p++ = '\0';
262
263             for( int i = 0; psz_comment[i]; i++ )
264                 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
265                     psz_comment[i] -= 'a' - 'A';
266
267             vlc_meta_AddExtra( p_meta, psz_comment, p );
268         }
269 #undef IF_EXTRACT
270         free( psz_comment );
271     }
272 #undef RM
273 }
274
275 const char *FindKateCategoryName( const char *psz_tag )
276 {
277     for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
278     {
279         if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
280             return Katei18nCategories[i].psz_i18n;
281     }
282     return N_("Unknown category");
283 }
284