]> git.sesse.net Git - vlc/blobdiff - modules/meta_engine/taglib.cpp
gnutls: replace deprecated types
[vlc] / modules / meta_engine / taglib.cpp
index f1419ecbc373de08e928cd7bfb60ccaa5034ae00..0ce4c164257b685ac460f7187a4b44d9981eafc1 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * taglib.cpp: Taglib tag parser/writer
  *****************************************************************************
- * Copyright (C) 2003-2009 the VideoLAN team
+ * Copyright (C) 2003-2011 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_meta.h>
-#include <vlc_demux.h>
-#include <vlc_strings.h>
-#include <vlc_charset.h>
-#include <vlc_url.h>
-#include <vlc_input_item.h>
-#include <vlc_input.h> /* for attachment_new */
+#include <vlc_demux.h>              /* demux_meta_t */
+#include <vlc_strings.h>            /* vlc_b64_decode_binary */
+#include <vlc_input.h>              /* for attachment_new */
 
 #ifdef WIN32
+# include <vlc_charset.h>
 # include <io.h>
 #else
 # include <unistd.h>
 
 
 // Taglib headers
+#include <taglib.h>
+#define VERSION_INT(a, b, c) ((a)<<16 | (b)<<8 | (c))
+#define TAGLIB_VERSION VERSION_INT(TAGLIB_MAJOR_VERSION, \
+                                   TAGLIB_MINOR_VERSION, \
+                                   TAGLIB_PATCH_VERSION)
+
 #include <fileref.h>
 #include <tag.h>
 #include <tbytevector.h>
 
-#include <apetag.h>
-#include <id3v2tag.h>
-#include <xiphcomment.h>
+#if TAGLIB_VERSION >= VERSION_INT(1,7,0)
+# define TAGLIB_HAVE_APEFILE_H
+# include <apefile.h>
+# ifdef TAGLIB_WITH_ASF                     // ASF pictures comes with v1.7.0
+#  define TAGLIB_HAVE_ASFPICTURE_H
+#  include <asffile.h>
+# endif
+#endif
 
+#include <apetag.h>
 #include <flacfile.h>
 #include <mpcfile.h>
 #include <mpegfile.h>
 #include <oggfile.h>
 #include <oggflacfile.h>
+#include "../demux/vorbis.h"
 
-#ifdef TAGLIB_WITH_ASF
+#if TAGLIB_VERSION >= VERSION_INT(1,6,0)
+# define TAGLIB_HAVE_AIFF_WAV_H
 # include <aifffile.h>
 # include <wavfile.h>
+#else
+# include <id3v2tag.h>
 #endif
 
-#ifdef TAGLIB_WITH_MP4
+#if TAGLIB_VERSION >= VERSION_INT(1,6,1) && defined(TAGLIB_WITH_MP4)
+# define TAGLIB_HAVE_MP4COVERTART_H
 # include <mp4file.h>
 #endif
 
@@ -94,6 +108,18 @@ vlc_module_end ()
 
 using namespace TagLib;
 
+static void ExtractTrackNumberValues( vlc_meta_t* p_meta, const char *psz_value )
+{
+    unsigned int i_trknum, i_trktot;
+    if( sscanf( psz_value, "%u/%u", &i_trknum, &i_trktot ) == 2 )
+    {
+        char psz_trck[11];
+        snprintf( psz_trck, sizeof( psz_trck ), "%u", i_trknum );
+        vlc_meta_SetTrackNum( p_meta, psz_trck );
+        snprintf( psz_trck, sizeof( psz_trck ), "%u", i_trktot );
+        vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
+    }
+}
 
 /**
  * Read meta information from APE tags
@@ -106,16 +132,74 @@ static void ReadMetaFromAPE( APE::Tag* tag, demux_meta_t*, vlc_meta_t* p_meta )
     APE::Item item;
 #define SET( keyName, metaName ) \
     item = tag->itemListMap()[keyName]; \
-    vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) );\
+    if( !item.isEmpty() ) vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) ); \
 
     SET( "COPYRIGHT", Copyright );
     SET( "LANGUAGE", Language );
     SET( "PUBLISHER", Publisher );
 
 #undef SET
+
+    /* */
+    item = tag->itemListMap()["TRACK"];
+    if( !item.isEmpty() )
+    {
+        ExtractTrackNumberValues( p_meta, item.toString().toCString( true ) );
+    }
 }
 
 
+#ifdef TAGLIB_HAVE_ASFPICTURE_H
+/**
+ * Read meta information from APE tags
+ * @param tag: the APE tag
+ * @param p_demux_meta: the demuxer meta
+ * @param p_meta: the meta
+ */
+static void ReadMetaFromASF( ASF::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
+{
+    // List the pictures
+    ASF::AttributeList list = tag->attributeListMap()["WM/Picture"];
+    ASF::AttributeList::Iterator iter;
+    for( iter = list.begin(); iter != list.end(); iter++ )
+    {
+        const ASF::Picture asfPicture = (*iter).toPicture();
+        const ByteVector picture = asfPicture.picture();
+        const char *psz_mime = asfPicture.mimeType().toCString();
+        const char *p_data = picture.data();
+        const unsigned i_data = picture.size();
+        char *psz_name;
+        input_attachment_t *p_attachment;
+
+        if( asfPicture.description().size() > 0 )
+            psz_name = strdup( asfPicture.description().toCString( true ) );
+        else
+        {
+            if( asprintf( &psz_name, "%i", asfPicture.type() ) == -1 )
+                continue;
+        }
+
+        msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
+                 psz_name, psz_mime, i_data );
+
+        p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
+                                psz_name, p_data, i_data );
+        if( p_attachment )
+            TAB_APPEND_CAST( (input_attachment_t**),
+                             p_demux_meta->i_attachments, p_demux_meta->attachments,
+                             p_attachment );
+        free( psz_name );
+
+        char *psz_url;
+        if( asprintf( &psz_url, "attachment://%s",
+                      p_attachment->psz_name ) == -1 )
+            continue;
+        vlc_meta_SetArtURL( p_meta, psz_url );
+        free( psz_url );
+    }
+}
+#endif
+
 
 /**
  * Read meta information from id3v2 tags
@@ -156,6 +240,11 @@ static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_
                 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
         if( !p_txxx )
             continue;
+        if( !strcmp( p_txxx->description().toCString( true ), "TRACKTOTAL" ) )
+        {
+            vlc_meta_Set( p_meta, vlc_meta_TrackTotal, p_txxx->fieldList().back().toCString( true ) );
+            continue;
+        }
         vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
                            p_txxx->fieldList().back().toCString( true ) );
     }
@@ -174,6 +263,13 @@ static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_
 
 #undef SET
 
+    /* */
+    list = tag->frameListMap()["TRCK"];
+    if( !list.isEmpty() )
+    {
+        ExtractTrackNumberValues( p_meta, (*list.begin())->toString().toCString( true ) );
+    }
+
     /* Preferred type of image
      * The 21 types are defined in id3v2 standard:
      * http://www.id3.org/id3v2.4.0-frames */
@@ -280,7 +376,6 @@ static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_
 }
 
 
-
 /**
  * Read the meta information from XiphComments
  * @param tag: the Xiph Comment
@@ -289,8 +384,9 @@ static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_
  */
 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
 {
+    StringList list;
 #define SET( keyName, metaName )                                               \
-    StringList list = tag->fieldListMap()[keyName];                            \
+    list = tag->fieldListMap()[keyName];                                       \
     if( !list.isEmpty() )                                                      \
         vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
 
@@ -301,38 +397,61 @@ static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta,
     StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
     StringList art_list = tag->fieldListMap()[ "COVERART" ];
 
-    // We get only the first covert art
-    if( mime_list.size() > 1 || art_list.size() > 1 )
-        msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
-                  art_list.size() );
-    else if( mime_list.size() == 0 || art_list.size() == 0 )
-        return;
-
     input_attachment_t *p_attachment;
 
-    const char* psz_name = "cover";
-    const char* psz_mime = mime_list[0].toCString(true);
-    const char* psz_description = "cover";
+    if( mime_list.size() != 0 && art_list.size() != 0 )
+    {
+        // We get only the first covert art
+        if( mime_list.size() > 1 || art_list.size() > 1 )
+            msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
+                    art_list.size() );
+
+        const char* psz_name = "cover";
+        const char* psz_mime = mime_list[0].toCString(true);
+        const char* psz_description = "cover";
 
-    uint8_t *p_data;
-    int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
+        uint8_t *p_data;
+        int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
 
-    msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
-             psz_name, psz_mime, i_data );
+        msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
+                psz_name, psz_mime, i_data );
 
-    TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
-              p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
-              psz_description, p_data, i_data );
-    free( p_data );
+        p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
+                psz_description, p_data, i_data );
+        free( p_data );
+    }
+    else
+    {
+        art_list = tag->fieldListMap()[ "METADATA_BLOCK_PICTURE" ];
+        if( art_list.size() == 0 )
+            return;
+
+        uint8_t *p_data;
+        int type;
+        int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
+        p_attachment = ParseFlacPicture( p_data, i_data, 0, &type );
+    }
 
+    TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
     TAB_APPEND_CAST( (input_attachment_t**),
                      p_demux_meta->i_attachments, p_demux_meta->attachments,
                      p_attachment );
 
-    vlc_meta_SetArtURL( p_meta, "attachment://cover" );
+    char *psz_url;
+    if( asprintf( &psz_url, "attachment://%s", p_attachment->psz_name ) != -1 ) {
+        vlc_meta_SetArtURL( p_meta, psz_url );
+        free( psz_url );
+    }
 }
 
-#if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
+
+#ifdef TAGLIB_HAVE_MP4COVERTART_H
+/**
+ * Read the meta information from mp4 specific tags
+ * @param tag: the mp4 tag
+ * @param p_demux_meta: the demuxer meta
+ * @param p_meta: the meta
+ */
 static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
 {
     if( tag->itemListMap().contains("covr") )
@@ -355,6 +474,7 @@ static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta
 }
 #endif
 
+
 /**
  * Get the tags from the file using TagLib
  * @param p_this: the demux object
@@ -377,23 +497,16 @@ static int ReadMeta( vlc_object_t* p_this)
         return VLC_ENOMEM;
 
 #if defined(WIN32) || defined (UNDER_CE)
-    wchar_t wpath[MAX_PATH + 1];
-    if( !MultiByteToWideChar( CP_UTF8, 0, psz_path, -1, wpath, MAX_PATH) )
+    wchar_t *wpath = ToWide( psz_path );
+    if( wpath == NULL )
     {
         free( psz_path );
         return VLC_EGENERIC;
     }
-    wpath[MAX_PATH] = L'\0';
     f = FileRef( wpath );
+    free( wpath );
 #else
-    const char* local_name = ToLocale( psz_path );
-    if( !local_name )
-    {
-        free( psz_path );
-        return VLC_EGENERIC;
-    }
-    f = FileRef( local_name );
-    LocaleFree( local_name );
+    f = FileRef( psz_path );
 #endif
     free( psz_path );
 
@@ -434,6 +547,22 @@ static int ReadMeta( vlc_object_t* p_this)
 
 
     // Try now to read special tags
+#ifdef TAGLIB_HAVE_APEFILE_H
+    if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
+    {
+        if( ape->APETag() )
+            ReadMetaFromAPE( ape->APETag(), p_demux_meta, p_meta );
+    }
+    else
+#endif
+#ifdef TAGLIB_HAVE_ASFPICTURE_H
+    if( ASF::File* asf = dynamic_cast<ASF::File*>(f.file()) )
+    {
+        if( asf->tag() )
+            ReadMetaFromASF( asf->tag(), p_demux_meta, p_meta );
+    }
+    else
+#endif
     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
     {
         if( flac->ID3v2Tag() )
@@ -441,7 +570,7 @@ static int ReadMeta( vlc_object_t* p_this)
         else if( flac->xiphComment() )
             ReadMetaFromXiph( flac->xiphComment(), p_demux_meta, p_meta );
     }
-#if defined(TAGLIB_WITH_MP4) && defined(HAVE_TAGLIB_MP4COVERART_H)
+#ifdef TAGLIB_HAVE_MP4COVERTART_H
     else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
     {
         if( mp4->tag() )
@@ -469,7 +598,7 @@ static int ReadMeta( vlc_object_t* p_this)
         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
             ReadMetaFromXiph( ogg_vorbis->tag(), p_demux_meta, p_meta );
     }
-#ifdef TAGLIB_WITH_ASF
+#ifdef TAGLIB_HAVE_AIFF_WAV_H
     else if( dynamic_cast<RIFF::File*>(f.file()) )
     {
         if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
@@ -493,7 +622,6 @@ static int ReadMeta( vlc_object_t* p_this)
 }
 
 
-
 /**
  * Write meta information to APE tags
  * @param tag: the APE tag
@@ -520,7 +648,6 @@ static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
 }
 
 
-
 /**
  * Write meta information to id3v2 tags
  * @param tag: the id3v2 tag
@@ -551,7 +678,6 @@ static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
 }
 
 
-
 /**
  * Write the meta information to XiphComments
  * @param tag: the Xiph Comment
@@ -576,7 +702,6 @@ static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
 }
 
 
-
 /**
  * Set the tags to the file using TagLib
  * @param p_this: the demux object
@@ -597,17 +722,13 @@ static int WriteMeta( vlc_object_t *p_this )
     }
 
 #if defined(WIN32) || defined (UNDER_CE)
-    wchar_t wpath[MAX_PATH + 1];
-    if( !MultiByteToWideChar( CP_UTF8, 0, p_export->psz_file, -1, wpath, MAX_PATH) )
+    wchar_t *wpath = ToWide( p_export->psz_file );
+    if( wpath == NULL )
         return VLC_EGENERIC;
-    wpath[MAX_PATH] = L'\0';
     f = FileRef( wpath );
+    free( wpath );
 #else
-    const char* local_name = ToLocale( p_export->psz_file );
-    if( !local_name )
-        return VLC_EGENERIC;
-    f = FileRef( local_name );
-    LocaleFree( local_name );
+    f = FileRef( p_export->psz_file );
 #endif
 
     if( f.isNull() || !f.tag() || f.file()->readOnly() )
@@ -643,15 +764,23 @@ static int WriteMeta( vlc_object_t *p_this )
 #undef SET
 
     psz_meta = input_item_GetDate( p_item );
-    if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
+    if( !EMPTY_STR(psz_meta) ) p_tag->setYear( atoi( psz_meta ) );
     free( psz_meta );
 
     psz_meta = input_item_GetTrackNum( p_item );
-    if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
+    if( !EMPTY_STR(psz_meta) ) p_tag->setTrack( atoi( psz_meta ) );
     free( psz_meta );
 
 
     // Try now to write special tags
+#ifdef TAGLIB_HAVE_APEFILE_H
+    if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
+    {
+        if( ape->APETag() )
+            WriteMetaToAPE( ape->APETag(), p_item );
+    }
+    else
+#endif
     if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
     {
         if( flac->ID3v2Tag() )
@@ -680,7 +809,7 @@ static int WriteMeta( vlc_object_t *p_this )
         else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
             WriteMetaToXiph( ogg_vorbis->tag(), p_item );
     }
-#ifdef TAGLIB_WITH_ASF
+#ifdef TAGLIB_HAVE_AIFF_WAV_H
     else if( dynamic_cast<RIFF::File*>(f.file()) )
     {
         if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )