demux: mp4: move ID3 genres to meta
[vlc.git] / modules / demux / mp4 / meta.c
blob3421e847d6d2bd399ee247f9d75b067b0d861385
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 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "mp4.h"
25 #include "../meta_engine/ID3Genres.h" /* for ATOM_gnre */
26 #include "languages.h"
28 #include <vlc_meta.h>
29 #include <vlc_charset.h>
31 #include <assert.h>
33 static const struct
35 const uint32_t xa9_type;
36 const vlc_meta_type_t meta_type;
37 } xa9typetometa[] = {
38 { ATOM_0x40PRM, vlc_meta_EncodedBy }, /* Adobe Premiere */
39 { ATOM_0x40PRQ, vlc_meta_EncodedBy }, /* Adobe Qt */
40 { ATOM_0xa9nam, vlc_meta_Title }, /* Full name */
41 { ATOM_0xa9aut, vlc_meta_Artist },
42 { ATOM_0xa9ART, vlc_meta_Artist },
43 { ATOM_0xa9cpy, vlc_meta_Copyright },
44 { ATOM_0xa9day, vlc_meta_Date }, /* Creation Date */
45 { ATOM_0xa9des, vlc_meta_Description }, /* Description */
46 { ATOM_0xa9gen, vlc_meta_Genre }, /* Genre */
47 { ATOM_0xa9alb, vlc_meta_Album }, /* Album */
48 { ATOM_0xa9trk, vlc_meta_TrackNumber }, /* Track */
49 { ATOM_0xa9cmt, vlc_meta_Description }, /* Comment */
50 { ATOM_0xa9url, vlc_meta_URL }, /* URL */
51 { ATOM_0xa9too, vlc_meta_EncodedBy }, /* Encoder Tool */
52 { ATOM_0xa9enc, vlc_meta_EncodedBy }, /* Encoded By */
53 { ATOM_0xa9pub, vlc_meta_Publisher },
54 { ATOM_0xa9dir, vlc_meta_Director },
55 { ATOM_MCPS, vlc_meta_EncodedBy }, /* Cleaner Pro */
56 { ATOM_aART, vlc_meta_AlbumArtist },
59 static const struct
61 const uint32_t xa9_type;
62 const char metadata[25];
63 } xa9typetoextrameta[] = {
64 { ATOM_0xa9wrt, N_("Writer") },
65 { ATOM_0xa9com, N_("Composer") },
66 { ATOM_0xa9prd, N_("Producer") },
67 { ATOM_0xa9inf, N_("Information") },
68 { ATOM_0xa9dis, N_("Disclaimer") },
69 { ATOM_0xa9req, N_("Requirements") },
70 { ATOM_0xa9fmt, N_("Original Format") },
71 { ATOM_0xa9dsa, N_("Display Source As") },
72 { ATOM_0xa9hst, N_("Host Computer") },
73 { ATOM_0xa9prf, N_("Performers") },
74 { ATOM_0xa9ope, N_("Original Performer") },
75 { ATOM_0xa9src, N_("Providers Source Content") },
76 { ATOM_0xa9wrn, N_("Warning") },
77 { ATOM_0xa9swr, N_("Software") },
78 { ATOM_0xa9lyr, N_("Lyrics") },
79 { ATOM_0xa9mak, N_("Record Company") },
80 { ATOM_0xa9mod, N_("Model") },
81 { ATOM_0xa9PRD, N_("Product") },
82 { ATOM_0xa9grp, N_("Grouping") },
83 { ATOM_0xa9gen, N_("Genre") },
84 { ATOM_0xa9st3, N_("Sub-Title") },
85 { ATOM_0xa9arg, N_("Arranger") },
86 { ATOM_0xa9ard, N_("Art Director") },
87 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
88 { ATOM_0xa9con, N_("Conductor") },
89 { ATOM_0xa9des, N_("Song Description") },
90 { ATOM_0xa9lnt, N_("Liner Notes") },
91 { ATOM_0xa9phg, N_("Phonogram Rights") },
92 { ATOM_0xa9pub, N_("Publisher") },
93 { ATOM_0xa9sne, N_("Sound Engineer") },
94 { ATOM_0xa9sol, N_("Soloist") },
95 { ATOM_0xa9thx, N_("Thanks") },
96 { ATOM_0xa9xpd, N_("Executive Producer") },
97 { ATOM_aART, N_("Album Artist") },
98 { ATOM_flvr, N_("Encoding Params") },
99 { ATOM_vndr, N_("Vendor") },
100 { ATOM_xid_, N_("Catalog Number") },
101 { ATOM_gshh, N_("YouTube Host") },
102 { ATOM_gspm, N_("YouTube Ping Message") },
103 { ATOM_gspu, N_("YouTube Ping Url") },
104 { ATOM_gssd, N_("YouTube Source Data") },
105 { ATOM_gsst, N_("YouTube Start Time") },
106 { ATOM_gstd, N_("YouTube Track Duration") },
109 static const struct
111 const char *psz_naming;
112 const vlc_meta_type_t meta_type;
113 } com_apple_quicktime_tometa[] = {
114 { "displayname", vlc_meta_NowPlaying },
115 { "software", vlc_meta_EncodedBy },
116 { "Encoded_With", vlc_meta_EncodedBy },
117 { "album", vlc_meta_Album },
118 { "artist", vlc_meta_Artist },
119 { "comment", vlc_meta_Description },
120 { "description", vlc_meta_Description },
121 { "copyright", vlc_meta_Copyright },
122 { "creationdate", vlc_meta_Date },
123 { "director", vlc_meta_Director },
124 { "genre", vlc_meta_Genre },
125 { "publisher", vlc_meta_Publisher },
128 static const struct
130 const char *psz_naming;
131 const char *psz_metadata;
132 } com_apple_quicktime_toextrameta[] = {
133 { "information", N_("Information") },
134 { "keywords", N_("Keywords") },
135 { "make", N_("Vendor") },
138 inline static char * StringConvert( const MP4_Box_data_data_t *p_data )
140 if ( !p_data || !p_data->i_blob )
141 return NULL;
143 switch( p_data->e_wellknowntype )
145 case DATA_WKT_UTF8:
146 case DATA_WKT_UTF8_SORT:
147 return FromCharset( "UTF-8", p_data->p_blob, p_data->i_blob );
148 case DATA_WKT_UTF16:
149 case DATA_WKT_UTF16_SORT:
150 return FromCharset( "UTF-16BE", p_data->p_blob, p_data->i_blob );
151 case DATA_WKT_SJIS:
152 return FromCharset( "SHIFT-JIS", p_data->p_blob, p_data->i_blob );
153 default:
154 return NULL;
158 static char * ExtractString( MP4_Box_t *p_box )
160 if ( p_box->i_type == ATOM_data )
161 return StringConvert( p_box->data.p_data );
163 MP4_Box_t *p_data = MP4_BoxGet( p_box, "data" );
164 if ( p_data )
165 return StringConvert( BOXDATA(p_data) );
166 else if ( p_box->data.p_binary && p_box->data.p_binary->p_blob )
168 char *psz_utf = strndup( p_box->data.p_binary->p_blob,
169 p_box->data.p_binary->i_blob );
170 if (likely( psz_utf ))
171 EnsureUTF8( psz_utf );
172 return psz_utf;
174 else
175 return NULL;
178 static bool AppleNameToMeta( char const* name,
179 vlc_meta_type_t const** meta_type, char const** meta_key )
181 *meta_type = NULL;
182 *meta_key = NULL;
184 for( size_t i = 0; *meta_type == NULL &&
185 i < ARRAY_SIZE( com_apple_quicktime_tometa ); ++i )
187 if( !strcmp( name, com_apple_quicktime_tometa[i].psz_naming ) )
188 *meta_type = &com_apple_quicktime_tometa[i].meta_type;
191 for( size_t i = 0; *meta_key == NULL &&
192 i < ARRAY_SIZE( com_apple_quicktime_toextrameta ); ++i )
194 if( !strcmp( name, com_apple_quicktime_toextrameta[i].psz_naming ) )
195 *meta_key = com_apple_quicktime_toextrameta[i].psz_metadata;
198 return *meta_type || *meta_key;
201 static bool AtomXA9ToMeta( uint32_t i_type,
202 vlc_meta_type_t const** meta_type, char const** meta_key )
204 *meta_type = NULL;
205 *meta_key = NULL;
207 for( size_t i = 0; !*meta_type && i < ARRAY_SIZE( xa9typetometa ); ++i )
208 if( xa9typetometa[i].xa9_type == i_type )
209 *meta_type = &xa9typetometa[i].meta_type;
211 for( size_t i = 0; !*meta_key && i < ARRAY_SIZE( xa9typetoextrameta ); ++i )
212 if( xa9typetoextrameta[i].xa9_type == i_type )
213 *meta_key = xa9typetoextrameta[i].metadata;
215 return *meta_type || *meta_key;
218 static bool SetMeta( vlc_meta_t* p_meta, int i_type, char const* name, MP4_Box_t* p_box )
220 vlc_meta_type_t const* type;
221 char const* key;
223 if( ( name != NULL && !AppleNameToMeta( name, &type, &key ) ) ||
224 ( name == NULL && !AtomXA9ToMeta( i_type, &type, &key ) ) )
226 return false;
229 char* psz_utf = ExtractString( p_box );
231 if( psz_utf )
233 if( type ) vlc_meta_Set( p_meta, *type, psz_utf );
234 else vlc_meta_AddExtra( p_meta, key, psz_utf );
236 free( psz_utf );
239 return true;
242 static int ExtractIntlStrings( vlc_meta_t *p_meta, MP4_Box_t *p_box )
244 if( MP4_BoxGet( p_box, "data" ) )
245 return false;
247 vlc_meta_type_t const* meta_type;
248 char const* meta_key;
250 if( AtomXA9ToMeta( p_box->i_type, &meta_type, &meta_key ) == false )
251 return false;
253 if( p_box->p_father == NULL ||
254 p_box->p_father->i_type != ATOM_udta ||
255 p_box->data.p_binary == NULL ||
256 p_box->data.p_binary->p_blob == NULL )
258 return false;
261 vlc_meta_t* p_meta_intl = vlc_meta_New();
263 if( unlikely( !p_meta_intl ) )
264 return false;
266 char const* p_peek = p_box->data.p_binary->p_blob;
267 uint64_t i_read = p_box->data.p_binary->i_blob;
269 while( i_read >= 4 )
271 uint16_t i_len, i_lang;
273 MP4_GET2BYTES( i_len );
274 MP4_GET2BYTES( i_lang );
276 if( i_len > i_read )
277 break;
279 char charset[15] = "MACINTOSH//";
281 decodeQtLanguageCode( i_lang, charset+11, &(bool){0} );
283 if( i_lang >= 0x400 && i_lang != 0x7fff )
285 strcpy( charset, i_len < 2 || memcmp( p_peek, "\xFE\xFF", 2 )
286 ? "UTF-8" : "UTF-16BE" );
289 char* data = FromCharset( charset, p_peek, i_len );
290 if( data )
292 if( meta_type )
294 vlc_meta_Set( p_meta_intl, *meta_type, data );
296 meta_key = vlc_meta_TypeToLocalizedString( *meta_type );
297 meta_type = NULL;
299 else
301 char* key;
302 if( asprintf( &key, "%s (%s)", meta_key, charset+11 ) != -1 )
304 vlc_meta_AddExtra( p_meta_intl, key, data );
305 free( key );
308 free( data );
311 p_peek += i_len;
312 i_read -= i_len;
315 if( i_read == 0 )
316 vlc_meta_Merge( p_meta, p_meta_intl );
318 vlc_meta_Delete( p_meta_intl );
319 return i_read == 0;
322 static void ExtractItunesInfoTriplets( vlc_meta_t *p_meta, MP4_Box_t *p_box )
324 if( p_box->i_type != ATOM_ITUN )
325 return;
326 MP4_Box_t *p_mean = MP4_BoxGet( p_box, "mean" );
327 MP4_Box_t *p_name = MP4_BoxGet( p_box, "name" );
328 MP4_Box_t *p_data = MP4_BoxGet( p_box, "data" );
329 if( !p_mean || p_mean->data.p_binary->i_blob < 4 + 16 ||
330 !p_name || p_name->data.p_binary->i_blob < 5 ||
331 !p_data || !BOXDATA(p_data) )
332 return;
334 if( !strncmp( &((char*)p_mean->data.p_binary->p_blob)[4], "com.apple.iTunes",
335 p_mean->data.p_binary->i_blob - 4 ) )
337 char *psz_name = strndup( &((char*)p_name->data.p_binary->p_blob)[4],
338 p_name->data.p_binary->i_blob - 4 );
339 char *psz_value = ExtractString( p_data );
340 if( psz_name && psz_value )
341 vlc_meta_AddExtra( p_meta, psz_name, psz_value );
342 free( psz_name );
343 free( psz_value );
347 static void SetupmdirMeta( vlc_meta_t *p_meta, MP4_Box_t *p_box )
349 const MP4_Box_t *p_data = MP4_BoxGet( p_box, "data" );
351 if( p_data == NULL || !BOXDATA(p_data) )
353 if( ExtractIntlStrings( p_meta, p_box ) )
354 return;
356 SetMeta( p_meta, p_box->i_type, NULL, p_box );
357 return;
360 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
361 switch( p_box->i_type )
363 case ATOM_atID:
365 if ( BOXDATA(p_data)->i_blob >= 4 &&
366 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_BE_SIGNED )
368 char psz_utf[11];
369 snprintf( psz_utf, sizeof( psz_utf ), "%"PRId32,
370 GetDWBE(BOXDATA(p_data)->p_blob) );
371 vlc_meta_AddExtra( p_meta, N_("iTunes Account ID"), psz_utf );
373 break;
375 case ATOM_cnID:
377 if ( BOXDATA(p_data)->i_blob >= 4 &&
378 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_BE_SIGNED )
380 char psz_utf[11];
381 snprintf( psz_utf, sizeof( psz_utf ), "%"PRId32,
382 GetDWBE(BOXDATA(p_data)->p_blob) );
383 vlc_meta_AddExtra( p_meta, N_("iTunes Catalog ID"), psz_utf );
385 break;
387 case ATOM_disk:
389 if ( BOXDATA(p_data)->i_blob >= 6 &&
390 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_RESERVED )
392 char psz_number[5];
393 snprintf( psz_number, sizeof( psz_number ), "%"PRIu16, GetWBE(&BOXDATA(p_data)->p_blob[2]) );
394 vlc_meta_Set( p_meta, vlc_meta_DiscNumber, psz_number );
395 snprintf( psz_number, sizeof( psz_number ), "%"PRIu16, GetWBE(&BOXDATA(p_data)->p_blob[4]) );
396 vlc_meta_Set( p_meta, vlc_meta_DiscTotal, psz_number );
398 break;
400 case ATOM_gnre:
402 if ( BOXDATA(p_data)->i_blob >= 2 &&
403 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_RESERVED )
405 const uint16_t i_genre = GetWBE(BOXDATA(p_data)->p_blob);
406 if( i_genre && i_genre <= ID3_GENRES_COUNT )
407 vlc_meta_SetGenre( p_meta, ID3_ppsz_genres[i_genre - 1] );
409 break;
411 case ATOM_rtng:
413 if ( BOXDATA(p_data)->i_blob >= 1 )
415 const char *psz_rating;
416 switch( *BOXDATA(p_data)->p_blob )
418 case 0x4:
419 psz_rating = N_("Explicit");
420 break;
421 case 0x2:
422 psz_rating = N_("Clean");
423 break;
424 default:
425 case 0x0:
426 psz_rating = N_("None");
427 break;
429 vlc_meta_AddExtra( p_meta, N_("Rating"), psz_rating );
431 break;
433 case ATOM_trkn:
435 if ( BOXDATA(p_data)->i_blob >= 4 &&
436 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_RESERVED )
438 char psz_trck[6];
439 snprintf( psz_trck, sizeof( psz_trck ), "%"PRIu16, GetWBE(&BOXDATA(p_data)->p_blob[2]) );
440 vlc_meta_SetTrackNum( p_meta, psz_trck );
441 if( BOXDATA(p_data)->i_blob >= 8 && GetWBE(&BOXDATA(p_data)->p_blob[4]) )
443 snprintf( psz_trck, sizeof( psz_trck ), "%"PRIu16, GetWBE(&BOXDATA(p_data)->p_blob[4]) );
444 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
447 break;
449 case ATOM_ITUN:
450 ExtractItunesInfoTriplets( p_meta, p_box );
451 break;
452 default:
453 SetMeta( p_meta, p_box->i_type, NULL, p_box );
454 break;
458 static void SetupmdtaMeta( vlc_meta_t *p_meta, MP4_Box_t *p_box, MP4_Box_t *p_keys )
460 if ( !p_keys || !BOXDATA(p_keys) || BOXDATA(p_keys)->i_entry_count == 0 )
461 return;
462 if ( !p_box->i_index || p_box->i_index > BOXDATA(p_keys)->i_entry_count )
463 return;
465 const char *psz_naming = BOXDATA(p_keys)->p_entries[p_box->i_index - 1].psz_value;
466 const uint32_t i_namespace = BOXDATA(p_keys)->p_entries[p_box->i_index - 1].i_namespace;
468 if( i_namespace == HANDLER_mdta )
470 if ( !strncmp( "com.apple.quicktime.", psz_naming, 20 ) )
471 SetMeta( p_meta, 0, psz_naming + 20, p_box );
473 else if ( i_namespace == ATOM_udta )
475 /* Regular atom inside... could that be even more complex ??? */
476 char *psz_utf = ExtractString( p_box );
477 if ( psz_utf )
479 if ( strlen(psz_utf) == 4 )
481 SetMeta( p_meta,
482 VLC_FOURCC(psz_utf[0],psz_utf[1],psz_utf[2],psz_utf[3]),
483 NULL, p_box );
485 free( psz_utf );
490 void SetupMeta( vlc_meta_t *p_meta, MP4_Box_t *p_udta )
492 uint32_t i_handler = 0;
493 if ( p_udta->p_father )
494 i_handler = p_udta->i_handler;
496 for( MP4_Box_t *p_box = p_udta->p_first; p_box; p_box = p_box->p_next )
498 switch( i_handler )
500 case HANDLER_mdta:
502 MP4_Box_t *p_keys = MP4_BoxGet( p_udta->p_father, "keys" );
503 SetupmdtaMeta( p_meta, p_box, p_keys );
504 break;
507 case HANDLER_mdir:
508 default:
509 SetupmdirMeta( p_meta, p_box );
510 break;