demux: mp4: parse 3GPP ID3
[vlc.git] / modules / demux / mp4 / meta.c
blob6a8942ec443f02706bad33c8b219da9de0f8c98f
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 "../meta_engine/ID3Tag.h"
32 #include "../meta_engine/ID3Meta.h"
34 #include <assert.h>
36 static const struct
38 const uint32_t xa9_type;
39 const vlc_meta_type_t meta_type;
40 } xa9typetometa[] = {
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 },
62 static const struct
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") },
112 static const struct
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 },
131 static const struct
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 )
144 return NULL;
146 switch( p_data->e_wellknowntype )
148 case DATA_WKT_UTF8:
149 case DATA_WKT_UTF8_SORT:
150 return FromCharset( "UTF-8", p_data->p_blob, p_data->i_blob );
151 case DATA_WKT_UTF16:
152 case DATA_WKT_UTF16_SORT:
153 return FromCharset( "UTF-16BE", p_data->p_blob, p_data->i_blob );
154 case DATA_WKT_SJIS:
155 return FromCharset( "SHIFT-JIS", p_data->p_blob, p_data->i_blob );
156 default:
157 return NULL;
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" );
167 if ( p_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 );
175 return psz_utf;
177 else
178 return NULL;
181 static bool AppleNameToMeta( char const* name,
182 vlc_meta_type_t const** meta_type, char const** meta_key )
184 *meta_type = NULL;
185 *meta_key = NULL;
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 )
207 *meta_type = NULL;
208 *meta_key = NULL;
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;
224 char const* key;
226 if( ( name != NULL && !AppleNameToMeta( name, &type, &key ) ) ||
227 ( name == NULL && !AtomXA9ToMeta( i_type, &type, &key ) ) )
229 return false;
232 char* psz_utf = ExtractString( p_box );
234 if( psz_utf )
236 if( type ) vlc_meta_Set( p_meta, *type, psz_utf );
237 else vlc_meta_AddExtra( p_meta, key, psz_utf );
239 free( psz_utf );
242 return true;
245 static int ExtractIntlStrings( vlc_meta_t *p_meta, MP4_Box_t *p_box )
247 if( MP4_BoxGet( p_box, "data" ) )
248 return false;
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 )
254 return 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 )
261 return false;
264 vlc_meta_t* p_meta_intl = vlc_meta_New();
266 if( unlikely( !p_meta_intl ) )
267 return false;
269 char const* p_peek = p_box->data.p_binary->p_blob;
270 uint64_t i_read = p_box->data.p_binary->i_blob;
272 while( i_read >= 4 )
274 uint16_t i_len, i_lang;
276 MP4_GET2BYTES( i_len );
277 MP4_GET2BYTES( i_lang );
279 if( i_len > i_read )
280 break;
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 );
293 if( data )
295 if( meta_type )
297 vlc_meta_Set( p_meta_intl, *meta_type, data );
299 meta_key = vlc_meta_TypeToLocalizedString( *meta_type );
300 meta_type = NULL;
302 else
304 char* key;
305 if( asprintf( &key, "%s (%s)", meta_key, charset+11 ) != -1 )
307 vlc_meta_AddExtra( p_meta_intl, key, data );
308 free( key );
311 free( data );
314 p_peek += i_len;
315 i_read -= i_len;
318 if( i_read == 0 )
319 vlc_meta_Merge( p_meta, p_meta_intl );
321 vlc_meta_Delete( p_meta_intl );
322 return i_read == 0;
325 static void ExtractItunesInfoTriplets( vlc_meta_t *p_meta, MP4_Box_t *p_box )
327 if( p_box->i_type != ATOM_ITUN )
328 return;
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) )
335 return;
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 );
345 free( psz_name );
346 free( 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 ) )
357 return;
359 SetMeta( p_meta, p_box->i_type, NULL, p_box );
360 return;
363 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
364 switch( p_box->i_type )
366 case ATOM_atID:
368 if ( BOXDATA(p_data)->i_blob >= 4 &&
369 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_BE_SIGNED )
371 char psz_utf[11];
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 );
376 break;
378 case ATOM_cnID:
380 if ( BOXDATA(p_data)->i_blob >= 4 &&
381 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_BE_SIGNED )
383 char psz_utf[11];
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 );
388 break;
390 case ATOM_disk:
392 if ( BOXDATA(p_data)->i_blob >= 6 &&
393 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_RESERVED )
395 char psz_number[5];
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 );
401 break;
403 case ATOM_gnre:
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] );
412 break;
414 case ATOM_rtng:
416 if ( BOXDATA(p_data)->i_blob >= 1 )
418 const char *psz_rating;
419 switch( *BOXDATA(p_data)->p_blob )
421 case 0x4:
422 psz_rating = N_("Explicit");
423 break;
424 case 0x2:
425 psz_rating = N_("Clean");
426 break;
427 default:
428 case 0x0:
429 psz_rating = N_("None");
430 break;
432 vlc_meta_AddExtra( p_meta, N_("Rating"), psz_rating );
434 break;
436 case ATOM_trkn:
438 if ( BOXDATA(p_data)->i_blob >= 4 &&
439 BOXDATA(p_data)->e_wellknowntype == DATA_WKT_RESERVED )
441 char psz_trck[6];
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 );
450 break;
452 case ATOM_ITUN:
453 ExtractItunesInfoTriplets( p_meta, p_box );
454 break;
455 default:
456 SetMeta( p_meta, p_box->i_type, NULL, p_box );
457 break;
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 )
464 return;
465 if ( !p_box->i_index || p_box->i_index > BOXDATA(p_keys)->i_entry_count )
466 return;
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 );
480 if ( psz_utf )
482 if ( strlen(psz_utf) == 4 )
484 SetMeta( p_meta,
485 VLC_FOURCC(psz_utf[0],psz_utf[1],psz_utf[2],psz_utf[3]),
486 NULL, p_box );
488 free( psz_utf );
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 );
500 return VLC_SUCCESS;
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 )
507 return;
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 )
522 switch( i_handler )
524 case HANDLER_mdta:
526 MP4_Box_t *p_keys = MP4_BoxGet( p_udta->p_father, "keys" );
527 SetupmdtaMeta( p_meta, p_box, p_keys );
528 break;
531 case HANDLER_ID32:
532 SetupID3v2Meta( p_meta, p_box );
533 break;
535 case HANDLER_mdir:
536 default:
537 SetupmdirMeta( p_meta, p_box );
538 break;