1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2009 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Rafaël Carré <funman@videolanorg>
9 * Rémi Duraffort <ivoire@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_playlist.h>
34 #include <vlc_demux.h>
35 #include <vlc_strings.h>
36 #include <vlc_charset.h>
49 #include <tbytevector.h>
53 #include <xiphcomment.h>
59 #include <oggflacfile.h>
61 #ifdef TAGLIB_WITH_ASF
62 # include <aifffile.h>
66 #ifdef TAGLIB_WITH_MP4
70 #include <speexfile.h>
71 #include <trueaudiofile.h>
72 #include <vorbisfile.h>
73 #include <wavpackfile.h>
75 #include <attachedpictureframe.h>
76 #include <textidentificationframe.h>
77 #include <uniquefileidentifierframe.h>
81 static int ReadMeta ( vlc_object_t
* );
82 static int WriteMeta ( vlc_object_t
* );
85 set_capability( "meta reader", 1000 )
86 set_callbacks( ReadMeta
, NULL
)
88 set_capability( "meta writer", 50 )
89 set_callbacks( WriteMeta
, NULL
)
92 using namespace TagLib
;
96 * Read meta informations from APE tags
97 * @param tag: the APE tag
98 * @param p_demux; the demux object
99 * @param p_demux_meta: the demuxer meta
100 * @param p_meta: the meta
102 static void ReadMetaFromAPE( APE::Tag
* tag
, demux_t
* p_demux
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
105 #define SET( keyName, metaName ) \
106 item = tag->itemListMap()[keyName]; \
107 vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
109 SET( "COPYRIGHT", Copyright );
110 SET( "LANGUAGE", Language
);
111 SET( "PUBLISHER", Publisher
);
119 * Read meta information from id3v2 tags
120 * @param tag: the id3v2 tag
121 * @param p_demux; the demux object
122 * @param p_demux_meta: the demuxer meta
123 * @param p_meta: the meta
125 static void ReadMetaFromId3v2( ID3v2::Tag
* tag
, demux_t
* p_demux
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
127 // Get the unique file identifier
128 ID3v2::FrameList list
= tag
->frameListMap()["UFID"];
129 ID3v2::FrameList::Iterator iter
;
130 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
132 ID3v2::UniqueFileIdentifierFrame
* p_ufid
=
133 dynamic_cast<ID3v2::UniqueFileIdentifierFrame
*>(*iter
);
134 const char *owner
= p_ufid
->owner().toCString();
135 if (!strcmp( owner
, "http://musicbrainz.org" ))
137 /* ID3v2 UFID contains up to 64 bytes binary data
138 * but in our case it will be a '\0'
139 * terminated string */
141 int max_size
= __MIN( p_ufid
->identifier().size(), 63);
142 strncpy( psz_ufid
, p_ufid
->identifier().data(), max_size
);
143 psz_ufid
[max_size
] = '\0';
144 vlc_meta_SetTrackID( p_meta
, psz_ufid
);
149 list
= tag
->frameListMap()["TXXX"];
150 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
152 ID3v2::UserTextIdentificationFrame
* p_txxx
=
153 dynamic_cast<ID3v2::UserTextIdentificationFrame
*>(*iter
);
154 vlc_meta_AddExtra( p_meta
, p_txxx
->description().toCString( true ),
155 p_txxx
->fieldList().toString().toCString( true ) );
158 // Get some more informations
159 #define SET( tagName, metaName ) \
160 list = tag->frameListMap()[tagName]; \
161 if( !list.isEmpty() ) \
162 vlc_meta_Set##metaName( p_meta, \
163 (*list.begin())->toString().toCString( true ) );
165 SET( "TCOP", Copyright
);
166 SET( "TENC", EncodedBy
);
167 SET( "TLAN", Language
);
168 SET( "TPUB", Publisher
);
172 /* Preferred type of image
173 * The 21 types are defined in id3v2 standard:
174 * http://www.id3.org/id3v2.4.0-frames */
175 static const int pi_cover_score
[] = {
177 5, /* 32x32 PNG image that should be used as the file icon */
178 4, /* File icon of a different size or format. */
179 20, /* Front cover image of the album. */
180 19, /* Back cover image of the album. */
181 13, /* Inside leaflet page of the album. */
182 18, /* Image from the album itself. */
183 17, /* Picture of the lead artist or soloist. */
184 16, /* Picture of the artist or performer. */
185 14, /* Picture of the conductor. */
186 15, /* Picture of the band or orchestra. */
187 9, /* Picture of the composer. */
188 8, /* Picture of the lyricist or text writer. */
189 7, /* Picture of the recording location or studio. */
190 10, /* Picture of the artists during recording. */
191 11, /* Picture of the artists during performance. */
192 6, /* Picture from a movie or video related to the track. */
193 1, /* Picture of a large, coloured fish. */
194 12, /* Illustration related to the track. */
195 3, /* Logo of the band or performer. */
196 2 /* Logo of the publisher (record company). */
200 // Try now to get embedded art
201 list
= tag
->frameListMap()[ "APIC" ];
205 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
206 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
208 ID3v2::AttachedPictureFrame
* p_apic
=
209 dynamic_cast<ID3v2::AttachedPictureFrame
*>(*iter
);
210 input_attachment_t
*p_attachment
;
212 const char *psz_mime
;
213 char *psz_name
, *psz_description
;
215 // Get the mime and description of the image.
216 // If the description is empty, take the type as a description
217 psz_mime
= p_apic
->mimeType().toCString( true );
218 if( p_apic
->description().size() > 0 )
219 psz_description
= strdup( p_apic
->description().toCString( true ) );
222 if( asprintf( &psz_description
, "%i", p_apic
->type() ) == -1 )
223 psz_description
= NULL
;
226 if( !psz_description
)
228 psz_name
= psz_description
;
230 /* some old iTunes version not only sets incorrectly the mime type
231 * or the description of the image,
232 * but also embeds incorrectly the image.
233 * Recent versions seem to behave correctly */
234 if( !strncmp( psz_mime
, "PNG", 3 ) ||
235 !strncmp( psz_name
, "\xC2\x89PNG", 5 ) )
237 msg_Warn( p_demux_meta
, "Invalid picture embedded by broken iTunes version" );
238 free( psz_description
);
242 const ByteVector picture
= p_apic
->picture();
243 const char *p_data
= picture
.data();
244 const unsigned i_data
= picture
.size();
246 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %u bytes",
247 psz_name
, psz_mime
, i_data
);
249 p_attachment
= vlc_input_attachment_New( psz_name
, psz_mime
,
250 psz_description
, p_data
, i_data
);
252 TAB_APPEND_CAST( (input_attachment_t
**),
253 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
255 free( psz_description
);
257 if( pi_cover_score
[p_apic
->type()] > i_score
)
259 i_score
= pi_cover_score
[p_apic
->type()];
261 if( asprintf( &psz_url
, "attachment://%s",
262 p_attachment
->psz_name
) == -1 )
264 vlc_meta_SetArtURL( p_meta
, psz_url
);
273 * Read the meta informations from XiphComments
274 * @param tag: the Xiph Comment
275 * @param p_demux; the demux object
276 * @param p_demux_meta: the demuxer meta
277 * @param p_meta: the meta
279 static void ReadMetaFromXiph( Ogg::XiphComment
* tag
, demux_t
* p_demux
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
281 #define SET( keyName, metaName ) \
282 StringList list = tag->fieldListMap()[keyName]; \
283 if( !list.isEmpty() ) \
284 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
286 SET( "COPYRIGHT", Copyright
);
289 // Try now to get embedded art
290 StringList mime_list
= tag
->fieldListMap()[ "COVERARTMIME" ];
291 StringList art_list
= tag
->fieldListMap()[ "COVERART" ];
293 // We get only the first covert art
294 if( mime_list
.size() > 1 || art_list
.size() > 1 )
295 msg_Warn( p_demux_meta
, "Found %i embedded arts, so using only the first one",
297 else if( mime_list
.size() == 0 || art_list
.size() == 0 )
300 input_attachment_t
*p_attachment
;
302 const char* psz_name
= "cover";
303 const char* psz_mime
= mime_list
[0].toCString(true);
304 const char* psz_description
= "cover";
307 int i_data
= vlc_b64_decode_binary( &p_data
, art_list
[0].toCString(true) );
309 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %i bytes",
310 psz_name
, psz_mime
, i_data
);
312 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
313 p_attachment
= vlc_input_attachment_New( psz_name
, psz_mime
,
314 psz_description
, p_data
, i_data
);
317 TAB_APPEND_CAST( (input_attachment_t
**),
318 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
321 vlc_meta_SetArtURL( p_meta
, "attachment://cover" );
324 #ifdef TAGLIB_WITH_MP4
325 static void ReadMetaFromMP4( MP4::Tag
* tag
, demux_t
*p_demux
, demux_meta_t
*p_demux_meta
, vlc_meta_t
* p_meta
)
327 if( tag
->itemListMap().contains("covr") )
329 MP4::CoverArtList list
= tag
->itemListMap()["covr"].toCoverArtList();
330 const char *psz_format
= list
[0].format() == MP4::CoverArt::PNG
? "image/png" : "image/jpeg";
332 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
333 input_attachment_t
*p_attachment
=
334 vlc_input_attachment_New( "cover", psz_format
, "cover",
335 list
[0].data().data(), list
[0].data().size() );
336 TAB_APPEND_CAST( (input_attachment_t
**),
337 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
339 vlc_meta_SetArtURL( p_meta
, "attachment://cover" );
345 * Get the tags from the file using TagLib
346 * @param p_this: the demux object
347 * @return VLC_SUCCESS if the operation success
349 static int ReadMeta( vlc_object_t
* p_this
)
351 demux_meta_t
* p_demux_meta
= (demux_meta_t
*)p_this
;
352 demux_t
* p_demux
= p_demux_meta
->p_demux
;
356 p_demux_meta
->p_meta
= NULL
;
359 #if defined(WIN32) || defined (UNDER_CE)
360 wchar_t wpath
[MAX_PATH
+ 1];
361 if( !MultiByteToWideChar( CP_UTF8
, 0, p_demux
->psz_path
, -1, wpath
, MAX_PATH
) )
363 wpath
[MAX_PATH
] = L
'\0';
364 f
= FileRef( wpath
);
366 const char* local_name
= ToLocale( p_demux
->psz_path
);
369 f
= FileRef( local_name
);
370 LocaleFree( local_name
);
375 if( !f
.tag() || f
.tag()->isEmpty() )
378 p_demux_meta
->p_meta
= p_meta
= vlc_meta_New();
383 // Read the tags from the file
384 Tag
* p_tag
= f
.tag();
386 #define SET( tag, meta ) \
387 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
388 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
389 #define SETINT( tag, meta ) \
393 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
394 vlc_meta_Set##meta( p_meta, psz_tmp ); \
398 SET( artist
, Artist
);
400 SET( comment
, Description
);
402 SETINT( year
, Date
);
403 SETINT( track
, TrackNum
);
409 // Try now to read special tags
410 if( FLAC::File
* flac
= dynamic_cast<FLAC::File
*>(f
.file()) )
412 if( flac
->ID3v2Tag() )
413 ReadMetaFromId3v2( flac
->ID3v2Tag(), p_demux
, p_demux_meta
, p_meta
);
414 else if( flac
->xiphComment() )
415 ReadMetaFromXiph( flac
->xiphComment(), p_demux
, p_demux_meta
, p_meta
);
417 #ifdef TAGLIB_WITH_MP4
418 else if( MP4::File
*mp4
= dynamic_cast<MP4::File
*>(f
.file()) )
421 ReadMetaFromMP4( mp4
->tag(), p_demux
, p_demux_meta
, p_meta
);
424 else if( MPC::File
* mpc
= dynamic_cast<MPC::File
*>(f
.file()) )
427 ReadMetaFromAPE( mpc
->APETag(), p_demux
, p_demux_meta
, p_meta
);
429 else if( MPEG::File
* mpeg
= dynamic_cast<MPEG::File
*>(f
.file()) )
431 if( mpeg
->ID3v2Tag() )
432 ReadMetaFromId3v2( mpeg
->ID3v2Tag(), p_demux
, p_demux_meta
, p_meta
);
433 else if( mpeg
->APETag() )
434 ReadMetaFromAPE( mpeg
->APETag(), p_demux
, p_demux_meta
, p_meta
);
436 else if( Ogg::File
* ogg
= dynamic_cast<Ogg::File
*>(f
.file()) )
438 if( Ogg::FLAC::File
* ogg_flac
= dynamic_cast<Ogg::FLAC::File
*>(f
.file()))
439 ReadMetaFromXiph( ogg_flac
->tag(), p_demux
, p_demux_meta
, p_meta
);
440 else if( Ogg::Speex::File
* ogg_speex
= dynamic_cast<Ogg::Speex::File
*>(f
.file()) )
441 ReadMetaFromXiph( ogg_speex
->tag(), p_demux
, p_demux_meta
, p_meta
);
442 else if( Ogg::Vorbis::File
* ogg_vorbis
= dynamic_cast<Ogg::Vorbis::File
*>(f
.file()) )
443 ReadMetaFromXiph( ogg_vorbis
->tag(), p_demux
, p_demux_meta
, p_meta
);
445 #ifdef TAGLIB_WITH_ASF
446 else if( RIFF::File
* riff
= dynamic_cast<RIFF::File
*>(f
.file()) )
448 if( RIFF::AIFF::File
* riff_aiff
= dynamic_cast<RIFF::AIFF::File
*>(f
.file()) )
449 ReadMetaFromId3v2( riff_aiff
->tag(), p_demux
, p_demux_meta
, p_meta
);
450 else if( RIFF::WAV::File
* riff_wav
= dynamic_cast<RIFF::WAV::File
*>(f
.file()) )
451 ReadMetaFromId3v2( riff_wav
->tag(), p_demux
, p_demux_meta
, p_meta
);
454 else if( TrueAudio::File
* trueaudio
= dynamic_cast<TrueAudio::File
*>(f
.file()) )
456 if( trueaudio
->ID3v2Tag() )
457 ReadMetaFromId3v2( trueaudio
->ID3v2Tag(), p_demux
, p_demux_meta
, p_meta
);
459 else if( WavPack::File
* wavpack
= dynamic_cast<WavPack::File
*>(f
.file()) )
461 if( wavpack
->APETag() )
462 ReadMetaFromAPE( wavpack
->APETag(), p_demux
, p_demux_meta
, p_meta
);
471 * Write meta informations to APE tags
472 * @param tag: the APE tag
473 * @param p_item: the input item
475 static void WriteMetaToAPE( APE::Tag
* tag
, input_item_t
* p_item
)
478 #define WRITE( metaName, keyName ) \
479 psz_meta = input_item_Get##metaName( p_item ); \
482 String key( keyName, String::UTF8 ); \
483 String value( psz_meta, String::UTF8 ); \
484 tag->addValue( key, value, true ); \
488 WRITE( Copyright
, "COPYRIGHT" );
489 WRITE( Language
, "LANGUAGE" );
490 WRITE( Publisher
, "PUBLISHER" );
498 * Write meta information to id3v2 tags
499 * @param tag: the id3v2 tag
500 * @param p_input: the input item
502 static void WriteMetaToId3v2( ID3v2::Tag
* tag
, input_item_t
* p_item
)
505 #define WRITE( metaName, tagName ) \
506 psz_meta = input_item_Get##metaName( p_item ); \
509 ByteVector p_byte( tagName, 4 ); \
510 tag->removeFrames( p_byte ); \
511 ID3v2::TextIdentificationFrame* p_frame = \
512 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
513 p_frame->setText( psz_meta ); \
514 tag->addFrame( p_frame ); \
518 WRITE( Copyright
, "TCOP" );
519 WRITE( EncodedBy
, "TENC" );
520 WRITE( Language
, "TLAN" );
521 WRITE( Publisher
, "TPUB" );
529 * Write the meta informations to XiphComments
530 * @param tag: the Xiph Comment
531 * @param p_input: the input item
533 static void WriteMetaToXiph( Ogg::XiphComment
* tag
, input_item_t
* p_item
)
536 #define WRITE( metaName, keyName ) \
537 psz_meta = input_item_Get##metaName( p_item ); \
540 String key( keyName, String::UTF8 ); \
541 String value( psz_meta, String::UTF8 ); \
542 tag->addField( key, value, true ); \
546 WRITE( Copyright
, "COPYRIGHT" );
554 * Set the tags to the file using TagLib
555 * @param p_this: the demux object
556 * @return VLC_SUCCESS if the operation success
559 static int WriteMeta( vlc_object_t
*p_this
)
561 meta_export_t
*p_export
= (meta_export_t
*)p_this
;
562 input_item_t
*p_item
= p_export
->p_item
;
567 msg_Err( p_this
, "Can't save meta data of an empty input" );
571 #if defined(WIN32) || defined (UNDER_CE)
572 wchar_t wpath
[MAX_PATH
+ 1];
573 if( !MultiByteToWideChar( CP_UTF8
, 0, p_export
->psz_file
, -1, wpath
, MAX_PATH
) )
575 wpath
[MAX_PATH
] = L
'\0';
576 f
= FileRef( wpath
);
578 const char* local_name
= ToLocale( p_export
->psz_file
);
581 f
= FileRef( local_name
);
582 LocaleFree( local_name
);
585 if( f
.isNull() || !f
.tag() || f
.file()->readOnly() )
587 msg_Err( p_this
, "File %s can't be opened for tag writing",
588 p_export
->psz_file
);
592 msg_Dbg( p_this
, "Writing metadata for %s", p_export
->psz_file
);
594 Tag
*p_tag
= f
.tag();
598 #define SET( a, b ) \
599 psz_meta = input_item_Get ## a( p_item ); \
602 String tmp( psz_meta, String::UTF8 ); \
603 p_tag->set##b( tmp ); \
607 // Saving all common fields
608 // If the title is empty, use the name
609 SET( TitleFbName
, Title
);
610 SET( Artist
, Artist
);
612 SET( Description
, Comment
);
617 psz_meta
= input_item_GetDate( p_item
);
618 if( psz_meta
) p_tag
->setYear( atoi( psz_meta
) );
621 psz_meta
= input_item_GetTrackNum( p_item
);
622 if( psz_meta
) p_tag
->setTrack( atoi( psz_meta
) );
626 // Try now to write special tags
627 if( FLAC::File
* flac
= dynamic_cast<FLAC::File
*>(f
.file()) )
629 if( flac
->ID3v2Tag() )
630 WriteMetaToId3v2( flac
->ID3v2Tag(), p_item
);
631 else if( flac
->xiphComment() )
632 WriteMetaToXiph( flac
->xiphComment(), p_item
);
634 else if( MPC::File
* mpc
= dynamic_cast<MPC::File
*>(f
.file()) )
637 WriteMetaToAPE( mpc
->APETag(), p_item
);
639 else if( MPEG::File
* mpeg
= dynamic_cast<MPEG::File
*>(f
.file()) )
641 if( mpeg
->ID3v2Tag() )
642 WriteMetaToId3v2( mpeg
->ID3v2Tag(), p_item
);
643 else if( mpeg
->APETag() )
644 WriteMetaToAPE( mpeg
->APETag(), p_item
);
646 else if( Ogg::File
* ogg
= dynamic_cast<Ogg::File
*>(f
.file()) )
648 if( Ogg::FLAC::File
* ogg_flac
= dynamic_cast<Ogg::FLAC::File
*>(f
.file()))
649 WriteMetaToXiph( ogg_flac
->tag(), p_item
);
650 else if( Ogg::Speex::File
* ogg_speex
= dynamic_cast<Ogg::Speex::File
*>(f
.file()) )
651 WriteMetaToXiph( ogg_speex
->tag(), p_item
);
652 else if( Ogg::Vorbis::File
* ogg_vorbis
= dynamic_cast<Ogg::Vorbis::File
*>(f
.file()) )
653 WriteMetaToXiph( ogg_vorbis
->tag(), p_item
);
655 #ifdef TAGLIB_WITH_ASF
656 else if( RIFF::File
* riff
= dynamic_cast<RIFF::File
*>(f
.file()) )
658 if( RIFF::AIFF::File
* riff_aiff
= dynamic_cast<RIFF::AIFF::File
*>(f
.file()) )
659 WriteMetaToId3v2( riff_aiff
->tag(), p_item
);
660 else if( RIFF::WAV::File
* riff_wav
= dynamic_cast<RIFF::WAV::File
*>(f
.file()) )
661 WriteMetaToId3v2( riff_wav
->tag(), p_item
);
664 else if( TrueAudio::File
* trueaudio
= dynamic_cast<TrueAudio::File
*>(f
.file()) )
666 if( trueaudio
->ID3v2Tag() )
667 WriteMetaToId3v2( trueaudio
->ID3v2Tag(), p_item
);
669 else if( WavPack::File
* wavpack
= dynamic_cast<WavPack::File
*>(f
.file()) )
671 if( wavpack
->APETag() )
672 WriteMetaToAPE( wavpack
->APETag(), p_item
);
675 // Save the meta data