]> git.sesse.net Git - vlc/blob - modules/demux/vorbis.h
Merge Kate metadata into vorbis.h
[vlc] / modules / demux / vorbis.h
1 /*****************************************************************************
2  * vorbis.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 #include <vlc_charset.h>
26 #include <vlc_strings.h>
27 #include <vlc_input.h>
28
29 static input_attachment_t* ParseFlacPicture( const uint8_t *p_data, int i_data,
30     int i_attachments, int *i_cover_score, int *i_cover_idx )
31 {
32     /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
33     static const char pi_cover_score[] = {
34         0,  /* Other */
35         5,  /* 32x32 PNG image that should be used as the file icon */
36         4,  /* File icon of a different size or format. */
37         20, /* Front cover image of the album. */
38         19, /* Back cover image of the album. */
39         13, /* Inside leaflet page of the album. */
40         18, /* Image from the album itself. */
41         17, /* Picture of the lead artist or soloist. */
42         16, /* Picture of the artist or performer. */
43         14, /* Picture of the conductor. */
44         15, /* Picture of the band or orchestra. */
45         9,  /* Picture of the composer. */
46         8,  /* Picture of the lyricist or text writer. */
47         7,  /* Picture of the recording location or studio. */
48         10, /* Picture of the artists during recording. */
49         11, /* Picture of the artists during performance. */
50         6,  /* Picture from a movie or video related to the track. */
51         1,  /* Picture of a large, coloured fish. */
52         12, /* Illustration related to the track. */
53         3,  /* Logo of the band or performer. */
54         2   /* Logo of the publisher (record company). */
55     };
56
57     int i_len;
58     int i_type;
59     char *psz_mime = NULL;
60     char psz_name[128];
61     char *psz_description = NULL;
62     input_attachment_t *p_attachment = NULL;
63
64     if( i_data < 4 + 3*4 )
65         return NULL;
66 #define RM(x) do { i_data -= (x); p_data += (x); } while(0)
67
68     i_type = GetDWBE( p_data ); RM(4);
69     i_len = GetDWBE( p_data ); RM(4);
70
71     if( i_len < 0 || i_data < i_len + 4 )
72         goto error;
73     psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len);
74     i_len = GetDWBE( p_data ); RM(4);
75     if( i_len < 0 || i_data < i_len + 4*4 + 4)
76         goto error;
77     psz_description = strndup( (const char*)p_data, i_len ); RM(i_len);
78     EnsureUTF8( psz_description );
79     RM(4*4);
80     i_len = GetDWBE( p_data ); RM(4);
81     if( i_len < 0 || i_len > i_data )
82         goto error;
83
84     /* printf( "Picture type=%d mime=%s description='%s' file length=%d\n",
85              i_type, psz_mime, psz_description, i_len ); */
86
87     snprintf( psz_name, sizeof(psz_name), "picture%d", i_attachments );
88     if( !strcasecmp( psz_mime, "image/jpeg" ) )
89         strcat( psz_name, ".jpg" );
90     else if( !strcasecmp( psz_mime, "image/png" ) )
91         strcat( psz_name, ".png" );
92
93     p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
94             psz_description, p_data, i_data );
95
96     if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) &&
97         *i_cover_score < pi_cover_score[i_type] )
98     {
99         *i_cover_idx = i_attachments;
100         *i_cover_score = pi_cover_score[i_type];
101     }
102
103 error:
104     free( psz_mime );
105     free( psz_description );
106     return p_attachment;
107 }
108
109 static inline void vorbis_ParseComment( vlc_meta_t **pp_meta,
110         const uint8_t *p_data, int i_data,
111         int *i_attachments, input_attachment_t ***attachments,
112         int *i_cover_score, int *i_cover_idx,
113         int *i_seekpoint, seekpoint_t ***ppp_seekpoint )
114 {
115     int n;
116     int i_comment;
117     seekpoint_t *sk = NULL;
118
119     if( i_data < 8 )
120         return;
121
122     n = GetDWLE(p_data); RM(4);
123     if( n < 0 || n > i_data )
124         return;
125 #if 0
126     if( n > 0 )
127     {
128         /* TODO report vendor string ? */
129         char *psz_vendor = psz_vendor = strndup( p_data, n );
130         free( psz_vendor );
131     }
132 #endif
133     RM(n);
134
135     if( i_data < 4 )
136         return;
137
138     i_comment = GetDWLE(p_data); RM(4);
139     if( i_comment <= 0 )
140         return;
141
142     /* */
143     vlc_meta_t *p_meta = *pp_meta;
144     if( !p_meta )
145         *pp_meta = p_meta = vlc_meta_New();
146     if( !p_meta )
147         return;
148
149     /* */
150     bool hasTitle        = false;
151     bool hasAlbum        = false;
152     bool hasTrackNumber  = false;
153     bool hasTrackTotal   = false;
154     bool hasArtist       = false;
155     bool hasCopyright    = false;
156     bool hasDescription  = false;
157     bool hasGenre        = false;
158     bool hasDate         = false;
159     bool hasPublisher    = false;
160
161     for( ; i_comment > 0; i_comment-- )
162     {
163         char *psz_comment;
164         if( i_data < 4 )
165             break;
166         n = GetDWLE(p_data); RM(4);
167         if( n > i_data )
168             break;
169         if( n <= 0 )
170             continue;
171
172         psz_comment = strndup( (const char*)p_data, n );
173         RM(n);
174
175         EnsureUTF8( psz_comment );
176
177 #define IF_EXTRACT(txt,var) \
178     if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
179     { \
180         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
181         if( oldval && has##var) \
182         { \
183             char * newval; \
184             if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
185                 newval = NULL; \
186             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
187             free( newval ); \
188         } \
189         else \
190             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
191         has##var = true; \
192     }
193         IF_EXTRACT("TITLE=", Title )
194         else IF_EXTRACT("ALBUM=", Album )
195         else IF_EXTRACT("TRACKNUMBER=", TrackNumber )
196         else if( !strncasecmp(psz_comment, "TRACKTOTAL=", strlen("TRACKTOTAL=")))
197             vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TRACKTOTAL=")] );
198         else if( !strncasecmp(psz_comment, "TOTALTRACKS=", strlen("TOTALTRACKS=")))
199             vlc_meta_Set( p_meta, vlc_meta_TrackTotal, &psz_comment[strlen("TOTALTRACKS=")] );
200         else IF_EXTRACT("TOTALTRACKS=", TrackTotal )
201         else IF_EXTRACT("ARTIST=", Artist )
202         else IF_EXTRACT("COPYRIGHT=", Copyright )
203         else IF_EXTRACT("ORGANIZATION=", Publisher )
204         else IF_EXTRACT("DESCRIPTION=", Description )
205         else IF_EXTRACT("COMMENTS=", Description )
206         else IF_EXTRACT("GENRE=", Genre )
207         else IF_EXTRACT("DATE=", Date )
208         else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
209         {
210             if( attachments == NULL )
211                 continue;
212
213             uint8_t *p_picture;
214             size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
215             input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
216                 i_size, *i_attachments, i_cover_score, i_cover_idx );
217             free( p_picture );
218             if( p_attachment )
219             {
220                 TAB_APPEND_CAST( (input_attachment_t**),
221                     *i_attachments, *attachments, p_attachment );
222             }
223         }
224         else if( !strncasecmp(psz_comment, "chapter", strlen("chapter")) )
225         {
226             if( ppp_seekpoint == NULL )
227                 continue;
228
229             int i_chapt;
230             if( strstr( psz_comment, "name") && sscanf( psz_comment, "chapter%i=", &i_chapt ) == 1 )
231             {
232                 char *p = strchr( psz_comment, '=' );
233                 *p++ = '\0';
234                 sk->psz_name = strdup( p );
235             }
236             else if( sscanf( psz_comment, "chapter %i=", &i_chapt ) == 1 )
237             {
238                 int h, m, s, ms;
239                 char *p = strchr( psz_comment, '=' );
240                 *p++ = '\0';
241
242                 if( sscanf( p, "%d:%d:%d.%d", &h, &m, &s, &ms ) == 4 )
243                 {
244                     sk = vlc_seekpoint_New();
245                     sk->i_time_offset = ((h * 3600 + m * 60 + s) *1000 + ms) * 1000;
246                     TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint, sk );
247                 }
248             }
249         }
250         else if( strchr( psz_comment, '=' ) )
251         {
252             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
253              * undocumented tags and replay gain ) */
254             char *p = strchr( psz_comment, '=' );
255             *p++ = '\0';
256
257             for( int i = 0; psz_comment[i]; i++ )
258                 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
259                     psz_comment[i] -= 'a' - 'A';
260
261             vlc_meta_AddExtra( p_meta, psz_comment, p );
262         }
263 #undef IF_EXTRACT
264         free( psz_comment );
265     }
266 #undef RM
267 }
268
269 static const struct {
270   const char *psz_tag;
271   const char *psz_i18n;
272 } Katei18nCategories[] = {
273     /* From Silvia's Mozilla list */
274     { "CC",      N_("Closed captions") },
275     { "SUB",     N_("Subtitles") },
276     { "TAD",     N_("Textual audio descriptions") },
277     { "KTV",     N_("Karaoke") },
278     { "TIK",     N_("Ticker text") },
279     { "AR",      N_("Active regions") },
280     { "NB",      N_("Semantic annotations") },
281     { "META",    N_("Metadata") },
282     { "TRX",     N_("Transcript") },
283     { "LRC",     N_("Lyrics") },
284     { "LIN",     N_("Linguistic markup") },
285     { "CUE",     N_("Cue points") },
286
287     /* Grandfathered */
288     { "subtitles", N_("Subtitles") },
289     { "spu-subtitles", N_("Subtitles (images)") },
290     { "lyrics", N_("Lyrics") },
291
292     /* Kate specific */
293     { "K-SPU", N_("Subtitles (images)") },
294     { "K-SLD-T", N_("Slides (text)") },
295     { "K-SLD-I", N_("Slides (images)") },
296 };
297
298 const char *FindKateCategoryName( const char *psz_tag )
299 {
300     for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
301     {
302         if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
303             return Katei18nCategories[i].psz_i18n;
304     }
305     return N_("Unknown category");
306 }
307