]> git.sesse.net Git - vlc/blob - modules/demux/xiph_metadata.c
mediacodec: don't loop in GetOutput
[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 typedef struct chapters_array_t
116 {
117     unsigned int i_size;
118     seekpoint_t ** pp_chapters;
119 } chapters_array_t;
120
121 static seekpoint_t * getChapterEntry( unsigned int i_index, chapters_array_t *p_array )
122 {
123     if ( i_index > 4096 ) return NULL;
124     if ( i_index >= p_array->i_size )
125     {
126         unsigned int i_newsize = p_array->i_size;
127         while( i_index >= i_newsize ) i_newsize += 50;
128
129         if ( !p_array->pp_chapters )
130         {
131             p_array->pp_chapters = calloc( i_newsize, sizeof( seekpoint_t * ) );
132             if ( !p_array->pp_chapters ) return NULL;
133             p_array->i_size = i_newsize;
134         } else {
135             seekpoint_t **tmp = calloc( i_newsize, sizeof( seekpoint_t * ) );
136             if ( !tmp ) return NULL;
137             memcpy( tmp, p_array->pp_chapters, p_array->i_size * sizeof( seekpoint_t * ) );
138             free( p_array->pp_chapters );
139             p_array->pp_chapters = tmp;
140             p_array->i_size = i_newsize;
141         }
142     }
143     if ( !p_array->pp_chapters[i_index] )
144         p_array->pp_chapters[i_index] = vlc_seekpoint_New();
145     return p_array->pp_chapters[i_index];
146 }
147
148 void vorbis_ParseComment( es_format_t *p_fmt, vlc_meta_t **pp_meta,
149         const uint8_t *p_data, int i_data,
150         int *i_attachments, input_attachment_t ***attachments,
151         int *i_cover_score, int *i_cover_idx,
152         int *i_seekpoint, seekpoint_t ***ppp_seekpoint,
153         float (* ppf_replay_gain)[AUDIO_REPLAY_GAIN_MAX],
154         float (* ppf_replay_peak)[AUDIO_REPLAY_GAIN_MAX] )
155 {
156     int n;
157     int i_comment;
158
159     if( i_data < 8 )
160         return;
161
162     n = GetDWLE(p_data); RM(4);
163     if( n < 0 || n > i_data )
164         return;
165 #if 0
166     if( n > 0 )
167     {
168         /* TODO report vendor string ? */
169         char *psz_vendor = psz_vendor = strndup( p_data, n );
170         free( psz_vendor );
171     }
172 #endif
173     RM(n);
174
175     if( i_data < 4 )
176         return;
177
178     i_comment = GetDWLE(p_data); RM(4);
179     if( i_comment <= 0 )
180         return;
181
182     /* */
183     vlc_meta_t *p_meta = *pp_meta;
184     if( !p_meta )
185         *pp_meta = p_meta = vlc_meta_New();
186     if( !p_meta )
187         return;
188
189     /* */
190     bool hasTitle        = false;
191     bool hasArtist       = false;
192     bool hasGenre        = false;
193     bool hasCopyright    = false;
194     bool hasAlbum        = false;
195     bool hasTrackNum     = false;
196     bool hasDescription  = false;
197     bool hasRating       = false;
198     bool hasDate         = false;
199     bool hasLanguage     = false;
200     bool hasPublisher    = false;
201     bool hasEncodedBy    = false;
202     bool hasTrackTotal   = false;
203
204     chapters_array_t chapters_array = { 0, NULL };
205
206     for( ; i_comment > 0; i_comment-- )
207     {
208         char *psz_comment;
209         if( i_data < 4 )
210             break;
211         n = GetDWLE(p_data); RM(4);
212         if( n > i_data )
213             break;
214         if( n <= 0 )
215             continue;
216
217         psz_comment = strndup( (const char*)p_data, n );
218         RM(n);
219
220         EnsureUTF8( psz_comment );
221
222 #define IF_EXTRACT(txt,var) \
223     if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
224     { \
225         const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
226         if( oldval && has##var) \
227         { \
228             char * newval; \
229             if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
230                 newval = NULL; \
231             vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
232             free( newval ); \
233         } \
234         else \
235             vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
236         has##var = true; \
237     }
238
239 #define IF_EXTRACT_ONCE(txt,var) \
240     if( !strncasecmp(psz_comment, txt, strlen(txt)) && !has##var ) \
241     { \
242         vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
243         has##var = true; \
244     }
245
246 #define IF_EXTRACT_FMT(txt,var,fmt,target) \
247     IF_EXTRACT(txt,var)\
248     if( fmt && !strncasecmp(psz_comment, txt, strlen(txt)) )\
249         {\
250             if ( fmt->target ) free( fmt->target );\
251             fmt->target = strdup(&psz_comment[strlen(txt)]);\
252         }
253
254         IF_EXTRACT("TITLE=", Title )
255         else IF_EXTRACT("ARTIST=", Artist )
256         else IF_EXTRACT("GENRE=", Genre )
257         else IF_EXTRACT("COPYRIGHT=", Copyright )
258         else IF_EXTRACT("ALBUM=", Album )
259         else if( !hasTrackNum && !strncasecmp(psz_comment, "TRACKNUMBER=", strlen("TRACKNUMBER=" ) ) )
260         {
261             /* Yeah yeah, such a clever idea, let's put xx/xx inside TRACKNUMBER
262              * Oh, and let's not use TRACKTOTAL or TOTALTRACKS... */
263             short unsigned u_track, u_total;
264             if( sscanf( &psz_comment[strlen("TRACKNUMBER=")], "%hu/%hu", &u_track, &u_total ) == 2 )
265             {
266                 char str[6];
267                 snprintf(str, 6, "%u", u_track);
268                 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, str );
269                 hasTrackNum = true;
270                 snprintf(str, 6, "%u", u_total);
271                 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, str );
272                 hasTrackTotal = true;
273             }
274             else
275             {
276                 vlc_meta_Set( p_meta, vlc_meta_TrackNumber, &psz_comment[strlen("TRACKNUMBER=")] );
277                 hasTrackNum = true;
278             }
279         }
280         else IF_EXTRACT_ONCE("TRACKTOTAL=", TrackTotal )
281         else IF_EXTRACT_ONCE("TOTALTRACKS=", TrackTotal )
282         else IF_EXTRACT("DESCRIPTION=", Description )
283         else IF_EXTRACT("COMMENT=", Description )
284         else IF_EXTRACT("COMMENTS=", Description )
285         else IF_EXTRACT("RATING=", Rating )
286         else IF_EXTRACT("DATE=", Date )
287         else IF_EXTRACT_FMT("LANGUAGE=", Language, p_fmt, psz_language )
288         else IF_EXTRACT("ORGANIZATION=", Publisher )
289         else IF_EXTRACT("ENCODER=", EncodedBy )
290         else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
291         {
292             if( attachments == NULL )
293                 continue;
294
295             uint8_t *p_picture;
296             size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
297             input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
298                 i_size, *i_attachments, i_cover_score, i_cover_idx );
299             free( p_picture );
300             if( p_attachment )
301             {
302                 TAB_APPEND_CAST( (input_attachment_t**),
303                     *i_attachments, *attachments, p_attachment );
304             }
305         }
306         else if ( ppf_replay_gain && ppf_replay_peak && !strncmp(psz_comment, "REPLAYGAIN_", 11) )
307         {
308             char *p = strchr( psz_comment, '=' );
309             char *psz_val;
310             if (!p) continue;
311             if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_GAIN=", 22) )
312             {
313                 psz_val = malloc( strlen(p+1) + 1 );
314                 if (!psz_val) continue;
315                 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
316                 {
317                     (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( psz_val );
318                     free( psz_val );
319                 }
320             }
321             else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_GAIN=", 22) )
322             {
323                 psz_val = malloc( strlen(p+1) + 1 );
324                 if (!psz_val) continue;
325                 if( sscanf( ++p, "%s dB", psz_val ) == 1 )
326                 {
327                     (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( psz_val );
328                     free( psz_val );
329                 }
330             }
331             else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_PEAK=", 22) )
332             {
333                 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
334             }
335             else if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_PEAK=", 22) )
336             {
337                 (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
338             }
339         }
340         else if( !strncasecmp(psz_comment, "CHAPTER", 7) )
341         {
342             unsigned int i_chapt;
343             seekpoint_t *p_seekpoint = NULL;
344
345             for( int i = 0; psz_comment[i] && psz_comment[i] != '='; i++ )
346                 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
347                     psz_comment[i] -= 'a' - 'A';
348
349             if( strstr( psz_comment, "NAME=" ) &&
350                     sscanf( psz_comment, "CHAPTER%uNAME=", &i_chapt ) == 1 )
351             {
352                 char *p = strchr( psz_comment, '=' );
353                 p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
354                 if ( !p || ! p_seekpoint ) continue;
355                 if ( ! p_seekpoint->psz_name )
356                     p_seekpoint->psz_name = strdup( ++p );
357             }
358             else if( sscanf( psz_comment, "CHAPTER%u=", &i_chapt ) == 1 )
359             {
360                 unsigned int h, m, s, ms;
361                 char *p = strchr( psz_comment, '=' );
362                 if( p && sscanf( ++p, "%u:%u:%u.%u", &h, &m, &s, &ms ) == 4 )
363                 {
364                     p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
365                     if ( ! p_seekpoint ) continue;
366                     p_seekpoint->i_time_offset =
367                       (((int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s) * 1000 + ms) * 1000;
368                 }
369             }
370         }
371         else if( strchr( psz_comment, '=' ) )
372         {
373             /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
374              * undocumented tags and replay gain ) */
375             char *p = strchr( psz_comment, '=' );
376             *p++ = '\0';
377
378             for( int i = 0; psz_comment[i]; i++ )
379                 if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
380                     psz_comment[i] -= 'a' - 'A';
381
382             vlc_meta_AddExtra( p_meta, psz_comment, p );
383         }
384 #undef IF_EXTRACT
385         free( psz_comment );
386     }
387 #undef RM
388
389     for ( unsigned int i=0; i<chapters_array.i_size; i++ )
390     {
391         if ( !chapters_array.pp_chapters[i] ) continue;
392         TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint,
393                          chapters_array.pp_chapters[i] );
394     }
395     free( chapters_array.pp_chapters );
396 }
397
398 const char *FindKateCategoryName( const char *psz_tag )
399 {
400     for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
401     {
402         if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
403             return Katei18nCategories[i].psz_i18n;
404     }
405     return N_("Unknown category");
406 }
407