1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/****************************************************************************************
* Copyright (C) 2003-2005 Max Howell <max.howell@methylblue.com> *
* (C) 2003-2010 Mark Kretschmann <kretschmann@kde.org> *
* (C) 2005-2007 Alexandre Oliveira <aleprj@gmail.com> *
* (C) 2008 Dan Meltzer <parallelgrapefruit@gmail.com> *
* (C) 2008-2009 Jeff Mitchell <mitchell@kde.org> *
* (C) 2010 Ralf Engels <ralf-engels@gmx.de> *
* (c) 2010 Sergey Ivanov <123kash@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 2 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "VorbisCommentTagHelper.h"
#include "StringHelper.h"
#include <QBuffer>
#include <QImage>
#include <flacpicture.h>
using namespace Meta::Tag;
static const TagLib::String VORBIS_PICTURE_TAG( "METADATA_BLOCK_PICTURE" );
VorbisCommentTagHelper::VorbisCommentTagHelper( TagLib::Tag *tag, TagLib::Ogg::XiphComment *commentsTag, Amarok::FileType fileType, TagLib::FLAC::File *file )
: TagHelper( tag, fileType )
, m_tag( commentsTag )
, m_flacFile( file )
{
m_fieldMap.insert( Meta::valAlbumArtist, TagLib::String( "ALBUMARTIST" ) );
m_fieldMap.insert( Meta::valBpm, TagLib::String( "BPM" ) );
m_fieldMap.insert( Meta::valCompilation, TagLib::String( "COMPILATION" ) );
m_fieldMap.insert( Meta::valComposer, TagLib::String( "COMPOSER" ) );
m_fieldMap.insert( Meta::valDiscNr, TagLib::String( "DISCNUMBER" ) );
m_fieldMap.insert( Meta::valHasCover, TagLib::String( "COVERART" ) ); // non-standard but frequently used
m_fieldMap.insert( Meta::valPlaycount, TagLib::String( "FMPS_PLAYCOUNT" ) );
m_fieldMap.insert( Meta::valRating, TagLib::String( "FMPS_RATING" ) );
m_fieldMap.insert( Meta::valScore, TagLib::String( "FMPS_RATING_AMAROK_SCORE" ) );
m_fieldMap.insert( Meta::valLyrics, TagLib::String( "LYRICS" ) );
m_uidFieldMap.insert( UIDAFT, TagLib::String( "AMAROK 2 AFTV1 - AMAROK.KDE.ORG" ) );
}
static inline bool
isAlbumCover( const TagLib::FLAC::Picture* picture )
{
return ( picture->type() == TagLib::FLAC::Picture::FrontCover ||
picture->type() == TagLib::FLAC::Picture::Other ) &&
picture->data().size() > MIN_COVER_SIZE; // must be at least 1kb
}
/**
* Extract the given FLAC Picture object to QImage picture.
*
* If the FLAC image is a front cover, store the result in cover and return true.
* Otherwise, if otherCover is null, store the image there and return false.
*/
static inline bool
flacPictureToQImage( const TagLib::FLAC::Picture* picture, QImage& cover, QImage& otherCover )
{
QByteArray image_data( picture->data().data(), picture->data().size() );
if( picture->type() == TagLib::FLAC::Picture::FrontCover )
{
cover.loadFromData( image_data );
return true;
}
else if( otherCover.isNull() )
{
otherCover.loadFromData( image_data );
}
return false;
}
Meta::FieldHash
VorbisCommentTagHelper::tags() const
{
Meta::FieldHash data = TagHelper::tags();
TagLib::Ogg::FieldListMap map = m_tag->fieldListMap();
for( TagLib::Ogg::FieldListMap::ConstIterator it = map.begin(); it != map.end(); ++it )
{
qint64 field;
QString value = TStringToQString( it->second.toString( '\n' ) );
if( ( field = fieldName( it->first ) ) )
{
if( field == Meta::valDiscNr )
{
int disc;
if( ( disc = splitDiscNr( value ).first ) )
data.insert( field, disc );
}
else if( field == Meta::valRating )
data.insert( field, qRound( value.toFloat() * 10.0 ) );
else if( field == Meta::valScore )
data.insert( field, value.toFloat() * 100.0 );
else if( field == Meta::valHasCover )
data.insert( Meta::valHasCover, true );
else
data.insert( field, value );
}
else if( it->first == uidFieldName( UIDAFT ) && isValidUID( value, UIDAFT ) )
data.insert( Meta::valUniqueId, value );
else if( it->first == VORBIS_PICTURE_TAG )
{
if( parsePictureBlock( it->second ) )
data.insert( Meta::valHasCover, true );
}
}
if( m_flacFile )
{
const TagLib::List<TagLib::FLAC::Picture*> picturelist = m_flacFile->pictureList();
for( TagLib::List<TagLib::FLAC::Picture*>::ConstIterator it = picturelist.begin(); it != picturelist.end(); it++ )
{
TagLib::FLAC::Picture* picture = *it;
if( ( picture->type() == TagLib::FLAC::Picture::FrontCover ||
picture->type() == TagLib::FLAC::Picture::Other ) &&
picture->data().size() > MIN_COVER_SIZE ) // must be at least 1kb
{
data.insert( Meta::valHasCover, true );
break;
}
}
}
return data;
}
bool
VorbisCommentTagHelper::setTags( const Meta::FieldHash &changes )
{
bool modified = TagHelper::setTags( changes );
for( const qint64 key : changes.keys() )
{
QVariant value = changes.value( key );
TagLib::String field = fieldName( key );
if( !field.isEmpty() )
{
if( key == Meta::valHasCover )
continue;
else if( key == Meta::valRating )
m_tag->addField( field, Qt4QStringToTString( QString::number( value.toFloat() / 10.0 ) ) );
else if( key == Meta::valScore )
m_tag->addField( field, Qt4QStringToTString( QString::number( value.toFloat() / 100.0 ) ) );
else
m_tag->addField( field, Qt4QStringToTString( value.toString() ) );
modified = true;
}
else if( key == Meta::valUniqueId )
{
QPair < UIDType, QString > uidPair = splitUID( value.toString() );
if( uidPair.first == UIDInvalid )
continue;
m_tag->addField( uidFieldName( uidPair.first ), Qt4QStringToTString( uidPair.second ) );
modified = true;
}
}
return modified;
}
TagLib::ByteVector
VorbisCommentTagHelper::render() const
{
return m_tag->render();
}
bool
VorbisCommentTagHelper::hasEmbeddedCover() const
{
if( m_flacFile )
{
const TagLib::List<TagLib::FLAC::Picture*> picturelist = m_flacFile->pictureList();
for( TagLib::List<TagLib::FLAC::Picture*>::ConstIterator it = picturelist.begin(); it != picturelist.end(); it++ )
{
const TagLib::FLAC::Picture *picture = *it;
if( ( picture->type() == TagLib::FLAC::Picture::FrontCover ||
picture->type() == TagLib::FLAC::Picture::Other ) &&
picture->data().size() > MIN_COVER_SIZE ) // must be at least 1kb
{
return true;
}
}
}
else if( m_tag->fieldListMap().contains( VORBIS_PICTURE_TAG ) )
{
return parsePictureBlock( m_tag->fieldListMap()[ VORBIS_PICTURE_TAG ] );
}
else if( !fieldName( Meta::valHasCover ).isEmpty() )
{
TagLib::ByteVector field = fieldName( Meta::valHasCover ).toCString();
return m_tag->fieldListMap().contains( field );
}
return false;
}
QImage
VorbisCommentTagHelper::embeddedCover() const
{
QImage cover;
if( m_flacFile )
{
QImage otherCover;
const TagLib::List<TagLib::FLAC::Picture*> picturelist = m_flacFile->pictureList();
for( TagLib::List<TagLib::FLAC::Picture*>::ConstIterator it = picturelist.begin(); it != picturelist.end(); it++ )
{
const TagLib::FLAC::Picture *picture = *it;
if( ( picture->type() == TagLib::FLAC::Picture::FrontCover ||
picture->type() == TagLib::FLAC::Picture::Other ) &&
picture->data().size() > MIN_COVER_SIZE ) // must be at least 1kb
{
QByteArray image_data( picture->data().data(), picture->data().size() );
if( picture->type() == TagLib::FLAC::Picture::FrontCover )
{
cover.loadFromData( image_data );
break;
}
else if( otherCover.isNull() )
{
otherCover.loadFromData( image_data );
}
}
}
if( cover.isNull() && !otherCover.isNull() )
cover = otherCover;
}
else if( m_tag->fieldListMap().contains( VORBIS_PICTURE_TAG ) )
{
QImage resultCover;
parsePictureBlock( m_tag->fieldListMap()[ VORBIS_PICTURE_TAG ], &resultCover );
if( cover.isNull() && !resultCover.isNull() )
cover = resultCover;
}
else if( !fieldName( Meta::valHasCover ).isEmpty() )
{
TagLib::ByteVector field = fieldName( Meta::valHasCover ).toCString();
if( !m_tag->fieldListMap().contains( field ) )
return cover;
TagLib::StringList coverList = m_tag->fieldListMap()[field];
for( uint i=0; i<coverList.size(); i++ )
{
QByteArray image_data_b64( coverList[i].toCString() );
QByteArray image_data = QByteArray::fromBase64(image_data_b64);
if( image_data.size() <= MIN_COVER_SIZE ) // must be at least 1kb
continue;
bool success = false;
success = cover.loadFromData( image_data );
if( !success )
success = cover.loadFromData( image_data_b64 );
if( success )
break;
}
}
return cover;
}
bool
VorbisCommentTagHelper::setEmbeddedCover( const QImage &cover )
{
// NOTE since the COVERART tag is not standardized and a proper way has been defined recently [1],
// we should wait until taglib provides an implementation.
// [1] http://wiki.xiph.org/index.php/VorbisComment#Cover_art
if( m_flacFile )
{
QByteArray bytes;
QBuffer buffer( &bytes );
buffer.open( QIODevice::WriteOnly );
if( !cover.save( &buffer, "JPEG" ) )
{
buffer.close();
return false;
}
buffer.close();
// remove all covers
m_flacFile->removePictures();
// add new cover
TagLib::FLAC::Picture *newPicture = new TagLib::FLAC::Picture();
newPicture->setData( TagLib::ByteVector( bytes.data(), bytes.size() ) );
newPicture->setMimeType( "image/jpeg" );
newPicture->setType( TagLib::FLAC::Picture::FrontCover );
m_flacFile->addPicture( newPicture );
return true;
}
return false;
}
bool
VorbisCommentTagHelper::parsePictureBlock( const TagLib::StringList& block, QImage* result )
{
QImage otherCover;
// Here's what's happening: "block" may contain several FLAC picture entries.
// We need to find at least one that satisfies our needs.
for( TagLib::StringList::ConstIterator i = block.begin(); i != block.end(); ++i )
{
QByteArray data( QByteArray::fromBase64( i->to8Bit().c_str() ) );
TagLib::ByteVector tdata( data.data(), data.size() );
TagLib::FLAC::Picture p;
if(!p.parse(tdata))
continue;
if(isAlbumCover(&p))
{
if( result )
{
// Now, if the image is a front cover, we just use it and quit
// Otherwise, we store it in otherCover and wait for a better one
if( flacPictureToQImage( &p, *result, otherCover ) )
return true;
}
else
// We found some image, but we don't need best one here, so just leave
return true;
}
}
if(result)
{
// Now here we haven't found any front covers in the file
// So we just use otherCover and exit
*result = otherCover;
return !result->isNull();
}
return false;
}
|