1 /*****************************************************************************
2 * meta.c: mp4 meta handling
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010, 2014 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include "../meta_engine/ID3Genres.h" /* for ATOM_gnre */
26 #include "languages.h"
29 #include <vlc_charset.h>
31 #include "../meta_engine/ID3Tag.h"
32 #include "../meta_engine/ID3Meta.h"
38 const uint32_t xa9_type
;
39 const vlc_meta_type_t meta_type
;
41 { ATOM_0x40PRM
, vlc_meta_EncodedBy
}, /* Adobe Premiere */
42 { ATOM_0x40PRQ
, vlc_meta_EncodedBy
}, /* Adobe Qt */
43 { ATOM_0xa9nam
, vlc_meta_Title
}, /* Full name */
44 { ATOM_0xa9aut
, vlc_meta_Artist
},
45 { ATOM_0xa9ART
, vlc_meta_Artist
},
46 { ATOM_0xa9cpy
, vlc_meta_Copyright
},
47 { ATOM_0xa9day
, vlc_meta_Date
}, /* Creation Date */
48 { ATOM_0xa9des
, vlc_meta_Description
}, /* Description */
49 { ATOM_0xa9gen
, vlc_meta_Genre
}, /* Genre */
50 { ATOM_0xa9alb
, vlc_meta_Album
}, /* Album */
51 { ATOM_0xa9trk
, vlc_meta_TrackNumber
}, /* Track */
52 { ATOM_0xa9cmt
, vlc_meta_Description
}, /* Comment */
53 { ATOM_0xa9url
, vlc_meta_URL
}, /* URL */
54 { ATOM_0xa9too
, vlc_meta_EncodedBy
}, /* Encoder Tool */
55 { ATOM_0xa9enc
, vlc_meta_EncodedBy
}, /* Encoded By */
56 { ATOM_0xa9pub
, vlc_meta_Publisher
},
57 { ATOM_0xa9dir
, vlc_meta_Director
},
58 { ATOM_MCPS
, vlc_meta_EncodedBy
}, /* Cleaner Pro */
59 { ATOM_aART
, vlc_meta_AlbumArtist
},
64 const uint32_t xa9_type
;
65 const char metadata
[25];
66 } xa9typetoextrameta
[] = {
67 { ATOM_0xa9wrt
, N_("Writer") },
68 { ATOM_0xa9com
, N_("Composer") },
69 { ATOM_0xa9prd
, N_("Producer") },
70 { ATOM_0xa9inf
, N_("Information") },
71 { ATOM_0xa9dis
, N_("Disclaimer") },
72 { ATOM_0xa9req
, N_("Requirements") },
73 { ATOM_0xa9fmt
, N_("Original Format") },
74 { ATOM_0xa9dsa
, N_("Display Source As") },
75 { ATOM_0xa9hst
, N_("Host Computer") },
76 { ATOM_0xa9prf
, N_("Performers") },
77 { ATOM_0xa9ope
, N_("Original Performer") },
78 { ATOM_0xa9src
, N_("Providers Source Content") },
79 { ATOM_0xa9wrn
, N_("Warning") },
80 { ATOM_0xa9swr
, N_("Software") },
81 { ATOM_0xa9lyr
, N_("Lyrics") },
82 { ATOM_0xa9mak
, N_("Record Company") },
83 { ATOM_0xa9mod
, N_("Model") },
84 { ATOM_0xa9PRD
, N_("Product") },
85 { ATOM_0xa9grp
, N_("Grouping") },
86 { ATOM_0xa9gen
, N_("Genre") },
87 { ATOM_0xa9st3
, N_("Sub-Title") },
88 { ATOM_0xa9arg
, N_("Arranger") },
89 { ATOM_0xa9ard
, N_("Art Director") },
90 { ATOM_0xa9cak
, N_("Copyright Acknowledgement") },
91 { ATOM_0xa9con
, N_("Conductor") },
92 { ATOM_0xa9des
, N_("Song Description") },
93 { ATOM_0xa9lnt
, N_("Liner Notes") },
94 { ATOM_0xa9phg
, N_("Phonogram Rights") },
95 { ATOM_0xa9pub
, N_("Publisher") },
96 { ATOM_0xa9sne
, N_("Sound Engineer") },
97 { ATOM_0xa9sol
, N_("Soloist") },
98 { ATOM_0xa9thx
, N_("Thanks") },
99 { ATOM_0xa9xpd
, N_("Executive Producer") },
100 { ATOM_aART
, N_("Album Artist") },
101 { ATOM_flvr
, N_("Encoding Params") },
102 { ATOM_vndr
, N_("Vendor") },
103 { ATOM_xid_
, N_("Catalog Number") },
104 { ATOM_gshh
, N_("YouTube Host") },
105 { ATOM_gspm
, N_("YouTube Ping Message") },
106 { ATOM_gspu
, N_("YouTube Ping Url") },
107 { ATOM_gssd
, N_("YouTube Source Data") },
108 { ATOM_gsst
, N_("YouTube Start Time") },
109 { ATOM_gstd
, N_("YouTube Track Duration") },
114 const char *psz_naming
;
115 const vlc_meta_type_t meta_type
;
116 } com_apple_quicktime_tometa
[] = {
117 { "displayname", vlc_meta_NowPlaying
},
118 { "software", vlc_meta_EncodedBy
},
119 { "Encoded_With", vlc_meta_EncodedBy
},
120 { "album", vlc_meta_Album
},
121 { "artist", vlc_meta_Artist
},
122 { "comment", vlc_meta_Description
},
123 { "description", vlc_meta_Description
},
124 { "copyright", vlc_meta_Copyright
},
125 { "creationdate", vlc_meta_Date
},
126 { "director", vlc_meta_Director
},
127 { "genre", vlc_meta_Genre
},
128 { "publisher", vlc_meta_Publisher
},
133 const char *psz_naming
;
134 const char *psz_metadata
;
135 } com_apple_quicktime_toextrameta
[] = {
136 { "information", N_("Information") },
137 { "keywords", N_("Keywords") },
138 { "make", N_("Vendor") },
141 inline static char * StringConvert( const MP4_Box_data_data_t
*p_data
)
143 if ( !p_data
|| !p_data
->i_blob
)
146 switch( p_data
->e_wellknowntype
)
149 case DATA_WKT_UTF8_SORT
:
150 return FromCharset( "UTF-8", p_data
->p_blob
, p_data
->i_blob
);
152 case DATA_WKT_UTF16_SORT
:
153 return FromCharset( "UTF-16BE", p_data
->p_blob
, p_data
->i_blob
);
155 return FromCharset( "SHIFT-JIS", p_data
->p_blob
, p_data
->i_blob
);
161 static char * ExtractString( MP4_Box_t
*p_box
)
163 if ( p_box
->i_type
== ATOM_data
)
164 return StringConvert( p_box
->data
.p_data
);
166 MP4_Box_t
*p_data
= MP4_BoxGet( p_box
, "data" );
168 return StringConvert( BOXDATA(p_data
) );
169 else if ( p_box
->data
.p_binary
&& p_box
->data
.p_binary
->p_blob
)
171 char *psz_utf
= strndup( p_box
->data
.p_binary
->p_blob
,
172 p_box
->data
.p_binary
->i_blob
);
173 if (likely( psz_utf
))
174 EnsureUTF8( psz_utf
);
181 static bool AppleNameToMeta( char const* name
,
182 vlc_meta_type_t
const** meta_type
, char const** meta_key
)
187 for( size_t i
= 0; *meta_type
== NULL
&&
188 i
< ARRAY_SIZE( com_apple_quicktime_tometa
); ++i
)
190 if( !strcmp( name
, com_apple_quicktime_tometa
[i
].psz_naming
) )
191 *meta_type
= &com_apple_quicktime_tometa
[i
].meta_type
;
194 for( size_t i
= 0; *meta_key
== NULL
&&
195 i
< ARRAY_SIZE( com_apple_quicktime_toextrameta
); ++i
)
197 if( !strcmp( name
, com_apple_quicktime_toextrameta
[i
].psz_naming
) )
198 *meta_key
= com_apple_quicktime_toextrameta
[i
].psz_metadata
;
201 return *meta_type
|| *meta_key
;
204 static bool AtomXA9ToMeta( uint32_t i_type
,
205 vlc_meta_type_t
const** meta_type
, char const** meta_key
)
210 for( size_t i
= 0; !*meta_type
&& i
< ARRAY_SIZE( xa9typetometa
); ++i
)
211 if( xa9typetometa
[i
].xa9_type
== i_type
)
212 *meta_type
= &xa9typetometa
[i
].meta_type
;
214 for( size_t i
= 0; !*meta_key
&& i
< ARRAY_SIZE( xa9typetoextrameta
); ++i
)
215 if( xa9typetoextrameta
[i
].xa9_type
== i_type
)
216 *meta_key
= xa9typetoextrameta
[i
].metadata
;
218 return *meta_type
|| *meta_key
;
221 static bool SetMeta( vlc_meta_t
* p_meta
, int i_type
, char const* name
, MP4_Box_t
* p_box
)
223 vlc_meta_type_t
const* type
;
226 if( ( name
!= NULL
&& !AppleNameToMeta( name
, &type
, &key
) ) ||
227 ( name
== NULL
&& !AtomXA9ToMeta( i_type
, &type
, &key
) ) )
232 char* psz_utf
= ExtractString( p_box
);
236 if( type
) vlc_meta_Set( p_meta
, *type
, psz_utf
);
237 else vlc_meta_AddExtra( p_meta
, key
, psz_utf
);
245 static int ExtractIntlStrings( vlc_meta_t
*p_meta
, MP4_Box_t
*p_box
)
247 if( MP4_BoxGet( p_box
, "data" ) )
250 vlc_meta_type_t
const* meta_type
;
251 char const* meta_key
;
253 if( AtomXA9ToMeta( p_box
->i_type
, &meta_type
, &meta_key
) == false )
256 if( p_box
->p_father
== NULL
||
257 p_box
->p_father
->i_type
!= ATOM_udta
||
258 p_box
->data
.p_binary
== NULL
||
259 p_box
->data
.p_binary
->p_blob
== NULL
)
264 vlc_meta_t
* p_meta_intl
= vlc_meta_New();
266 if( unlikely( !p_meta_intl
) )
269 char const* p_peek
= p_box
->data
.p_binary
->p_blob
;
270 uint64_t i_read
= p_box
->data
.p_binary
->i_blob
;
274 uint16_t i_len
, i_lang
;
276 MP4_GET2BYTES( i_len
);
277 MP4_GET2BYTES( i_lang
);
282 char charset
[15] = "MACINTOSH//";
284 decodeQtLanguageCode( i_lang
, charset
+11, &(bool){0} );
286 if( i_lang
>= 0x400 && i_lang
!= 0x7fff )
288 strcpy( charset
, i_len
< 2 || memcmp( p_peek
, "\xFE\xFF", 2 )
289 ? "UTF-8" : "UTF-16BE" );
292 char* data
= FromCharset( charset
, p_peek
, i_len
);
297 vlc_meta_Set( p_meta_intl
, *meta_type
, data
);
299 meta_key
= vlc_meta_TypeToLocalizedString( *meta_type
);
305 if( asprintf( &key
, "%s (%s)", meta_key
, charset
+11 ) != -1 )
307 vlc_meta_AddExtra( p_meta_intl
, key
, data
);
319 vlc_meta_Merge( p_meta
, p_meta_intl
);
321 vlc_meta_Delete( p_meta_intl
);
325 static void ExtractItunesInfoTriplets( vlc_meta_t
*p_meta
, MP4_Box_t
*p_box
)
327 if( p_box
->i_type
!= ATOM_ITUN
)
329 MP4_Box_t
*p_mean
= MP4_BoxGet( p_box
, "mean" );
330 MP4_Box_t
*p_name
= MP4_BoxGet( p_box
, "name" );
331 MP4_Box_t
*p_data
= MP4_BoxGet( p_box
, "data" );
332 if( !p_mean
|| p_mean
->data
.p_binary
->i_blob
< 4 + 16 ||
333 !p_name
|| p_name
->data
.p_binary
->i_blob
< 5 ||
334 !p_data
|| !BOXDATA(p_data
) )
337 if( !strncmp( &((char*)p_mean
->data
.p_binary
->p_blob
)[4], "com.apple.iTunes",
338 p_mean
->data
.p_binary
->i_blob
- 4 ) )
340 char *psz_name
= strndup( &((char*)p_name
->data
.p_binary
->p_blob
)[4],
341 p_name
->data
.p_binary
->i_blob
- 4 );
342 char *psz_value
= ExtractString( p_data
);
343 if( psz_name
&& psz_value
)
344 vlc_meta_AddExtra( p_meta
, psz_name
, psz_value
);
350 static void SetupmdirMeta( vlc_meta_t
*p_meta
, MP4_Box_t
*p_box
)
352 const MP4_Box_t
*p_data
= MP4_BoxGet( p_box
, "data" );
354 if( p_data
== NULL
|| !BOXDATA(p_data
) )
356 if( ExtractIntlStrings( p_meta
, p_box
) )
359 SetMeta( p_meta
, p_box
->i_type
, NULL
, p_box
);
363 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
364 switch( p_box
->i_type
)
368 if ( BOXDATA(p_data
)->i_blob
>= 4 &&
369 BOXDATA(p_data
)->e_wellknowntype
== DATA_WKT_BE_SIGNED
)
372 snprintf( psz_utf
, sizeof( psz_utf
), "%"PRId32
,
373 GetDWBE(BOXDATA(p_data
)->p_blob
) );
374 vlc_meta_AddExtra( p_meta
, N_("iTunes Account ID"), psz_utf
);
380 if ( BOXDATA(p_data
)->i_blob
>= 4 &&
381 BOXDATA(p_data
)->e_wellknowntype
== DATA_WKT_BE_SIGNED
)
384 snprintf( psz_utf
, sizeof( psz_utf
), "%"PRId32
,
385 GetDWBE(BOXDATA(p_data
)->p_blob
) );
386 vlc_meta_AddExtra( p_meta
, N_("iTunes Catalog ID"), psz_utf
);
392 if ( BOXDATA(p_data
)->i_blob
>= 6 &&
393 BOXDATA(p_data
)->e_wellknowntype
== DATA_WKT_RESERVED
)
396 snprintf( psz_number
, sizeof( psz_number
), "%"PRIu16
, GetWBE(&BOXDATA(p_data
)->p_blob
[2]) );
397 vlc_meta_Set( p_meta
, vlc_meta_DiscNumber
, psz_number
);
398 snprintf( psz_number
, sizeof( psz_number
), "%"PRIu16
, GetWBE(&BOXDATA(p_data
)->p_blob
[4]) );
399 vlc_meta_Set( p_meta
, vlc_meta_DiscTotal
, psz_number
);
405 if ( BOXDATA(p_data
)->i_blob
>= 2 &&
406 BOXDATA(p_data
)->e_wellknowntype
== DATA_WKT_RESERVED
)
408 const uint16_t i_genre
= GetWBE(BOXDATA(p_data
)->p_blob
);
409 if( i_genre
&& i_genre
<= ID3_GENRES_COUNT
)
410 vlc_meta_SetGenre( p_meta
, ID3_ppsz_genres
[i_genre
- 1] );
416 if ( BOXDATA(p_data
)->i_blob
>= 1 )
418 const char *psz_rating
;
419 switch( *BOXDATA(p_data
)->p_blob
)
422 psz_rating
= N_("Explicit");
425 psz_rating
= N_("Clean");
429 psz_rating
= N_("None");
432 vlc_meta_AddExtra( p_meta
, N_("Rating"), psz_rating
);
438 if ( BOXDATA(p_data
)->i_blob
>= 4 &&
439 BOXDATA(p_data
)->e_wellknowntype
== DATA_WKT_RESERVED
)
442 snprintf( psz_trck
, sizeof( psz_trck
), "%"PRIu16
, GetWBE(&BOXDATA(p_data
)->p_blob
[2]) );
443 vlc_meta_SetTrackNum( p_meta
, psz_trck
);
444 if( BOXDATA(p_data
)->i_blob
>= 8 && GetWBE(&BOXDATA(p_data
)->p_blob
[4]) )
446 snprintf( psz_trck
, sizeof( psz_trck
), "%"PRIu16
, GetWBE(&BOXDATA(p_data
)->p_blob
[4]) );
447 vlc_meta_Set( p_meta
, vlc_meta_TrackTotal
, psz_trck
);
453 ExtractItunesInfoTriplets( p_meta
, p_box
);
456 SetMeta( p_meta
, p_box
->i_type
, NULL
, p_box
);
461 static void SetupmdtaMeta( vlc_meta_t
*p_meta
, MP4_Box_t
*p_box
, MP4_Box_t
*p_keys
)
463 if ( !p_keys
|| !BOXDATA(p_keys
) || BOXDATA(p_keys
)->i_entry_count
== 0 )
465 if ( !p_box
->i_index
|| p_box
->i_index
> BOXDATA(p_keys
)->i_entry_count
)
468 const char *psz_naming
= BOXDATA(p_keys
)->p_entries
[p_box
->i_index
- 1].psz_value
;
469 const uint32_t i_namespace
= BOXDATA(p_keys
)->p_entries
[p_box
->i_index
- 1].i_namespace
;
471 if( i_namespace
== HANDLER_mdta
)
473 if ( !strncmp( "com.apple.quicktime.", psz_naming
, 20 ) )
474 SetMeta( p_meta
, 0, psz_naming
+ 20, p_box
);
476 else if ( i_namespace
== ATOM_udta
)
478 /* Regular atom inside... could that be even more complex ??? */
479 char *psz_utf
= ExtractString( p_box
);
482 if ( strlen(psz_utf
) == 4 )
485 VLC_FOURCC(psz_utf
[0],psz_utf
[1],psz_utf
[2],psz_utf
[3]),
493 static int ID3TAG_Parse_Handler( uint32_t i_tag
, const uint8_t *p_payload
,
494 size_t i_payload
, void *p_priv
)
496 vlc_meta_t
*p_meta
= (vlc_meta_t
*) p_priv
;
498 (void) ID3HandleTag( p_payload
, i_payload
, i_tag
, p_meta
, NULL
);
503 static void SetupID3v2Meta( vlc_meta_t
*p_meta
, MP4_Box_t
*p_box
)
505 const MP4_Box_t
*p_binary
= MP4_BoxGet( p_box
, "ID32" );
506 if( p_binary
== NULL
|| !BOXDATA(p_binary
) || BOXDATA(p_binary
)->i_blob
< 6 + 20 + 1 )
509 /* ID3v2 in 3GPP / ETSI TS 126 244 8.3, Header size 4 + 2 */
510 ID3TAG_Parse( &((uint8_t *)BOXDATA(p_binary
)->p_blob
)[6], BOXDATA(p_binary
)->i_blob
- 6,
511 ID3TAG_Parse_Handler
, p_meta
);
514 void SetupMeta( vlc_meta_t
*p_meta
, MP4_Box_t
*p_udta
)
516 uint32_t i_handler
= 0;
517 if ( p_udta
->p_father
)
518 i_handler
= p_udta
->i_handler
;
520 for( MP4_Box_t
*p_box
= p_udta
->p_first
; p_box
; p_box
= p_box
->p_next
)
526 MP4_Box_t
*p_keys
= MP4_BoxGet( p_udta
->p_father
, "keys" );
527 SetupmdtaMeta( p_meta
, p_box
, p_keys
);
532 SetupID3v2Meta( p_meta
, p_box
);
537 SetupmdirMeta( p_meta
, p_box
);