1 /*****************************************************************************
2 * xiph_metadata.h: Vorbis Comment parser
3 *****************************************************************************
4 * Copyright © 2008-2013 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
7 * Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_charset.h>
32 #include <vlc_strings.h>
33 #include <vlc_arrays.h>
34 #include <vlc_input.h>
35 #include "xiph_metadata.h"
37 input_attachment_t
* ParseFlacPicture( const uint8_t *p_data
, size_t size
,
38 int i_attachments
, int *i_cover_score
, int *i_cover_idx
)
40 /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
41 static const char pi_cover_score
[] = {
43 5, /* 32x32 PNG image that should be used as the file icon */
44 4, /* File icon of a different size or format. */
45 20, /* Front cover image of the album. */
46 19, /* Back cover image of the album. */
47 13, /* Inside leaflet page of the album. */
48 18, /* Image from the album itself. */
49 17, /* Picture of the lead artist or soloist. */
50 16, /* Picture of the artist or performer. */
51 14, /* Picture of the conductor. */
52 15, /* Picture of the band or orchestra. */
53 9, /* Picture of the composer. */
54 8, /* Picture of the lyricist or text writer. */
55 7, /* Picture of the recording location or studio. */
56 10, /* Picture of the artists during recording. */
57 11, /* Picture of the artists during performance. */
58 6, /* Picture from a movie or video related to the track. */
59 1, /* Picture of a large, coloured fish. */
60 12, /* Illustration related to the track. */
61 3, /* Logo of the band or performer. */
62 2 /* Logo of the publisher (record company). */
71 assert(size >= (x)); \
76 type
= GetDWBE( p_data
);
78 len
= GetDWBE( p_data
);
84 char *mime
= strndup( (const char *)p_data
, len
);
85 if( unlikely(mime
== NULL
) )
95 len
= GetDWBE( p_data
);
104 input_attachment_t
*p_attachment
= NULL
;
105 char *description
= strndup( (const char *)p_data
, len
);
106 if( unlikely(description
== NULL
) )
110 EnsureUTF8( description
);
115 RM(4 * 4); /* skip */
117 len
= GetDWBE( p_data
);
123 /* printf( "Picture type=%"PRIu32" mime=%s description='%s' "
124 "file length=%zu\n", type, mime, description, len ); */
126 char name
[7 + (sizeof (i_attachments
) * 3) + 4 + 1];
128 snprintf( name
, sizeof (name
), "picture%u", i_attachments
);
130 if( !strcasecmp( mime
, "image/jpeg" ) )
131 strcat( name
, ".jpg" );
132 else if( !strcasecmp( mime
, "image/png" ) )
133 strcat( name
, ".png" );
135 p_attachment
= vlc_input_attachment_New( name
, mime
, description
, p_data
,
136 size
/* XXX: len instead? */ );
138 if( type
< ARRAY_SIZE(pi_cover_score
) &&
139 *i_cover_score
< pi_cover_score
[type
] )
141 *i_cover_idx
= i_attachments
;
142 *i_cover_score
= pi_cover_score
[type
];
159 typedef struct chapters_array_t
162 seekpoint_t
** pp_chapters
;
165 static seekpoint_t
* getChapterEntry( unsigned int i_index
, chapters_array_t
*p_array
)
167 if ( i_index
> 4096 ) return NULL
;
168 if ( i_index
>= p_array
->i_size
)
170 unsigned int i_newsize
= p_array
->i_size
;
171 while( i_index
>= i_newsize
) i_newsize
+= 50;
173 if ( !p_array
->pp_chapters
)
175 p_array
->pp_chapters
= calloc( i_newsize
, sizeof( seekpoint_t
* ) );
176 if ( !p_array
->pp_chapters
) return NULL
;
177 p_array
->i_size
= i_newsize
;
179 seekpoint_t
**tmp
= calloc( i_newsize
, sizeof( seekpoint_t
* ) );
180 if ( !tmp
) return NULL
;
181 memcpy( tmp
, p_array
->pp_chapters
, p_array
->i_size
* sizeof( seekpoint_t
* ) );
182 free( p_array
->pp_chapters
);
183 p_array
->pp_chapters
= tmp
;
184 p_array
->i_size
= i_newsize
;
187 if ( !p_array
->pp_chapters
[i_index
] )
188 p_array
->pp_chapters
[i_index
] = vlc_seekpoint_New();
189 return p_array
->pp_chapters
[i_index
];
192 #define XIPHMETA_Title (1 << 0)
193 #define XIPHMETA_Artist (1 << 1)
194 #define XIPHMETA_Genre (1 << 2)
195 #define XIPHMETA_Copyright (1 << 3)
196 #define XIPHMETA_Album (1 << 4)
197 #define XIPHMETA_TrackNum (1 << 5)
198 #define XIPHMETA_Description (1 << 6)
199 #define XIPHMETA_Rating (1 << 7)
200 #define XIPHMETA_Date (1 << 8)
201 #define XIPHMETA_Language (1 << 9)
202 #define XIPHMETA_Publisher (1 << 10)
203 #define XIPHMETA_EncodedBy (1 << 11)
204 #define XIPHMETA_TrackTotal (1 << 12)
206 static char * xiph_ExtractCueSheetMeta( const char *psz_line
,
207 const char *psz_tag
, int i_tag
,
210 if( !strncasecmp( psz_line
, psz_tag
, i_tag
) )
213 return strdup( &psz_line
[i_tag
] );
215 /* Unquote string value */
216 char *psz_value
= malloc( strlen( psz_line
) - i_tag
+ 1 );
219 char *psz_out
= psz_value
;
221 bool b_escaped
= false;
230 *(psz_out
++) = *psz_line
;
241 *(psz_out
++) = *psz_line
;
245 *(psz_out
++) = *psz_line
;
257 static void xiph_ParseCueSheetMeta( unsigned *pi_flags
, vlc_meta_t
*p_meta
,
258 const char *psz_line
,
259 int *pi_seekpoint
, seekpoint_t
***ppp_seekpoint
,
260 seekpoint_t
**pp_tmppoint
, bool *pb_valid
)
262 VLC_UNUSED(pi_seekpoint
);
263 VLC_UNUSED(ppp_seekpoint
);
265 seekpoint_t
*p_seekpoint
= *pp_tmppoint
;
268 #define TRY_EXTRACT_CUEMETA(var, string, quoted) \
269 if( !(*pi_flags & XIPHMETA_##var) &&\
270 ( psz_string = xiph_ExtractCueSheetMeta( psz_line, string, sizeof(string) - 1, quoted ) ) )\
272 vlc_meta_Set( p_meta, vlc_meta_##var, psz_string );\
274 *pi_flags |= XIPHMETA_##var;\
277 TRY_EXTRACT_CUEMETA(Title
, "TITLE \"", true)
278 else TRY_EXTRACT_CUEMETA(Genre
, "REM GENRE ", false)
279 else TRY_EXTRACT_CUEMETA(Date
, "REM DATE ", false)
280 else TRY_EXTRACT_CUEMETA(Artist
, "PERFORMER \"", true)
281 else if( !strncasecmp( psz_line
, " TRACK ", 8 ) )
286 TAB_APPEND( *pi_seekpoint
, *ppp_seekpoint
, p_seekpoint
);
288 vlc_seekpoint_Delete( p_seekpoint
);
291 *pp_tmppoint
= p_seekpoint
= vlc_seekpoint_New();
293 else if( p_seekpoint
&& !strncasecmp( psz_line
, " INDEX 01 ", 13 ) )
296 if( sscanf( &psz_line
[13], "%u:%u:%u", &m
, &s
, &f
) == 3 )
298 p_seekpoint
->i_time_offset
= vlc_tick_from_sec(m
* 60 + s
) + vlc_tick_from_samples(f
, 75);
302 else if( p_seekpoint
&& !p_seekpoint
->psz_name
)
304 p_seekpoint
->psz_name
= xiph_ExtractCueSheetMeta( psz_line
, " TITLE \"", 11, true );
308 static void xiph_ParseCueSheet( unsigned *pi_flags
, vlc_meta_t
*p_meta
,
309 const char *p_data
, int i_data
,
310 int *pi_seekpoint
, seekpoint_t
***ppp_seekpoint
)
312 seekpoint_t
*p_seekpoint
= NULL
;
313 bool b_valid
= false;
315 const char *p_head
= p_data
;
316 const char *p_tail
= p_head
;
317 while( p_tail
< p_data
+ i_data
)
319 if( *p_tail
== 0x0D )
321 char *psz
= strndup( p_head
, p_tail
- p_head
);
324 xiph_ParseCueSheetMeta( pi_flags
, p_meta
, psz
,
325 pi_seekpoint
, ppp_seekpoint
,
326 &p_seekpoint
, &b_valid
);
329 if( *(++p_tail
) == 0x0A )
343 TAB_APPEND( *pi_seekpoint
, *ppp_seekpoint
, p_seekpoint
);
345 vlc_seekpoint_Delete( p_seekpoint
);
349 void vorbis_ParseComment( es_format_t
*p_fmt
, vlc_meta_t
**pp_meta
,
350 const uint8_t *p_data
, size_t i_data
,
351 int *i_attachments
, input_attachment_t
***attachments
,
352 int *i_cover_score
, int *i_cover_idx
,
353 int *i_seekpoint
, seekpoint_t
***ppp_seekpoint
,
354 float (* ppf_replay_gain
)[AUDIO_REPLAY_GAIN_MAX
],
355 float (* ppf_replay_peak
)[AUDIO_REPLAY_GAIN_MAX
] )
360 uint32_t vendor_length
= GetDWLE(p_data
); RM(4);
362 if( vendor_length
> i_data
)
363 return; /* invalid length */
365 RM(vendor_length
); /* TODO: handle vendor payload */
370 uint32_t i_comment
= GetDWLE(p_data
); RM(4);
372 if( i_comment
> i_data
|| i_comment
== 0 )
373 return; /* invalid length */
376 vlc_meta_t
*p_meta
= *pp_meta
;
378 *pp_meta
= p_meta
= vlc_meta_New();
380 if( unlikely( !p_meta
) )
384 unsigned hasMetaFlags
= 0;
386 chapters_array_t chapters_array
= { 0, NULL
};
388 for( ; i_comment
> 0 && i_data
>= 4; i_comment
-- )
390 uint32_t comment_size
= GetDWLE(p_data
); RM(4);
392 if( comment_size
> i_data
)
395 if( comment_size
== 0 )
398 char* psz_comment
= malloc( comment_size
+ 1 );
400 if( unlikely( !psz_comment
) )
403 memcpy( psz_comment
, p_data
, comment_size
);
404 psz_comment
[comment_size
] = '\0';
406 EnsureUTF8( psz_comment
);
408 #define IF_EXTRACT(txt,var) \
409 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
411 const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
412 if( oldval && (hasMetaFlags & XIPHMETA_##var)) \
415 if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
417 vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
421 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
422 hasMetaFlags |= XIPHMETA_##var; \
425 #define IF_EXTRACT_ONCE(txt,var) \
426 if( !strncasecmp(psz_comment, txt, strlen(txt)) && !(hasMetaFlags & XIPHMETA_##var) ) \
428 vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
429 hasMetaFlags |= XIPHMETA_##var; \
432 #define IF_EXTRACT_FMT(txt,var,fmt,target) \
433 if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
438 free( fmt->target );\
439 fmt->target = strdup(&psz_comment[strlen(txt)]);\
443 IF_EXTRACT("TITLE=", Title
)
444 else IF_EXTRACT("ARTIST=", Artist
)
445 else IF_EXTRACT("GENRE=", Genre
)
446 else IF_EXTRACT("COPYRIGHT=", Copyright
)
447 else IF_EXTRACT("ALBUM=", Album
)
448 else if( !(hasMetaFlags
& XIPHMETA_TrackNum
) && !strncasecmp(psz_comment
, "TRACKNUMBER=", strlen("TRACKNUMBER=" ) ) )
450 /* Yeah yeah, such a clever idea, let's put xx/xx inside TRACKNUMBER
451 * Oh, and let's not use TRACKTOTAL or TOTALTRACKS... */
452 short unsigned u_track
, u_total
;
453 int nb_values
= sscanf( &psz_comment
[strlen("TRACKNUMBER=")], "%hu/%hu", &u_track
, &u_total
);
457 snprintf(str
, 6, "%u", u_track
);
458 vlc_meta_Set( p_meta
, vlc_meta_TrackNumber
, str
);
459 hasMetaFlags
|= XIPHMETA_TrackNum
;
462 snprintf(str
, 6, "%u", u_total
);
463 vlc_meta_Set( p_meta
, vlc_meta_TrackTotal
, str
);
464 hasMetaFlags
|= XIPHMETA_TrackTotal
;
468 else IF_EXTRACT_ONCE("TRACKTOTAL=", TrackTotal
)
469 else IF_EXTRACT_ONCE("TOTALTRACKS=", TrackTotal
)
470 else IF_EXTRACT("DESCRIPTION=", Description
)
471 else IF_EXTRACT("COMMENT=", Description
)
472 else IF_EXTRACT("COMMENTS=", Description
)
473 else IF_EXTRACT("RATING=", Rating
)
474 else IF_EXTRACT("DATE=", Date
)
475 else IF_EXTRACT_FMT("LANGUAGE=", Language
, p_fmt
, psz_language
)
476 else IF_EXTRACT("ORGANIZATION=", Publisher
)
477 else IF_EXTRACT("ENCODER=", EncodedBy
)
478 else if( !strncasecmp( psz_comment
, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
480 if( attachments
== NULL
)
484 size_t i_size
= vlc_b64_decode_binary( &p_picture
, &psz_comment
[strlen("METADATA_BLOCK_PICTURE=")]);
485 input_attachment_t
*p_attachment
= ParseFlacPicture( p_picture
,
486 i_size
, *i_attachments
, i_cover_score
, i_cover_idx
);
490 TAB_APPEND_CAST( (input_attachment_t
**),
491 *i_attachments
, *attachments
, p_attachment
);
494 else if ( ppf_replay_gain
&& ppf_replay_peak
&& !strncmp(psz_comment
, "REPLAYGAIN_", 11) )
496 char *p
= strchr( psz_comment
, '=' );
497 if (!p
) goto next_comment
;
498 if ( !strncasecmp(psz_comment
, "REPLAYGAIN_TRACK_GAIN=", 22) )
500 (*ppf_replay_gain
)[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( ++p
);
502 else if ( !strncasecmp(psz_comment
, "REPLAYGAIN_ALBUM_GAIN=", 22) )
504 (*ppf_replay_gain
)[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( ++p
);
506 else if ( !strncasecmp(psz_comment
, "REPLAYGAIN_ALBUM_PEAK=", 22) )
508 (*ppf_replay_peak
)[AUDIO_REPLAY_GAIN_ALBUM
] = us_atof( ++p
);
510 else if ( !strncasecmp(psz_comment
, "REPLAYGAIN_TRACK_PEAK=", 22) )
512 (*ppf_replay_peak
)[AUDIO_REPLAY_GAIN_TRACK
] = us_atof( ++p
);
515 else if( !strncasecmp(psz_comment
, "CHAPTER", 7) )
517 unsigned int i_chapt
;
518 seekpoint_t
*p_seekpoint
= NULL
;
520 for( int i
= 0; psz_comment
[i
] && psz_comment
[i
] != '='; i
++ )
521 if( psz_comment
[i
] >= 'a' && psz_comment
[i
] <= 'z' )
522 psz_comment
[i
] -= 'a' - 'A';
524 if( strstr( psz_comment
, "NAME=" ) &&
525 sscanf( psz_comment
, "CHAPTER%uNAME=", &i_chapt
) == 1 )
527 char *p
= strchr( psz_comment
, '=' );
528 p_seekpoint
= getChapterEntry( i_chapt
, &chapters_array
);
529 if ( !p
|| ! p_seekpoint
) goto next_comment
;
530 if ( ! p_seekpoint
->psz_name
)
531 p_seekpoint
->psz_name
= strdup( ++p
);
533 else if( sscanf( psz_comment
, "CHAPTER%u=", &i_chapt
) == 1 )
535 unsigned int h
, m
, s
, ms
;
536 char *p
= strchr( psz_comment
, '=' );
537 if( p
&& sscanf( ++p
, "%u:%u:%u.%u", &h
, &m
, &s
, &ms
) == 4 )
539 p_seekpoint
= getChapterEntry( i_chapt
, &chapters_array
);
540 if ( ! p_seekpoint
) goto next_comment
;
541 p_seekpoint
->i_time_offset
= vlc_tick_from_sec(h
* 3600 + m
* 60 + s
) + VLC_TICK_FROM_MS(ms
);
545 else if( !strncasecmp(psz_comment
, "cuesheet=", 9) )
547 xiph_ParseCueSheet( &hasMetaFlags
, p_meta
, &psz_comment
[9], comment_size
- 9,
548 i_seekpoint
, ppp_seekpoint
);
550 else if( strchr( psz_comment
, '=' ) )
552 /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
553 * undocumented tags and replay gain ) */
554 char *p
= strchr( psz_comment
, '=' );
557 for( int i
= 0; psz_comment
[i
]; i
++ )
558 if( psz_comment
[i
] >= 'a' && psz_comment
[i
] <= 'z' )
559 psz_comment
[i
] -= 'a' - 'A';
561 vlc_meta_AddExtra( p_meta
, psz_comment
, p
);
570 for ( unsigned int i
=0; i
<chapters_array
.i_size
; i
++ )
572 if ( !chapters_array
.pp_chapters
[i
] ) continue;
573 TAB_APPEND_CAST( (seekpoint_t
**), *i_seekpoint
, *ppp_seekpoint
,
574 chapters_array
.pp_chapters
[i
] );
576 free( chapters_array
.pp_chapters
);
579 const char *FindKateCategoryName( const char *psz_tag
)
581 for( size_t i
= 0; i
< sizeof(Katei18nCategories
)/sizeof(Katei18nCategories
[0]); i
++ )
583 if( !strcmp( psz_tag
, Katei18nCategories
[i
].psz_tag
) )
584 return Katei18nCategories
[i
].psz_i18n
;
586 return N_("Unknown category");