Ejemplo n.º 1
0
void
MP4::Tag::parseCovr(const MP4::Atom *atom)
{
  MP4::CoverArtList value;
  ByteVector data = d->file->readBlock(atom->length - 8);
  unsigned int pos = 0;
  while(pos < data.size()) {
    const int length = static_cast<int>(data.toUInt(pos));
    if(length < 12) {
      debug("MP4: Too short atom");
      break;;
    }

    const ByteVector name = data.mid(pos + 4, 4);
    const int flags = static_cast<int>(data.toUInt(pos + 8));
    if(name != "data") {
      debug("MP4: Unexpected atom \"" + name + "\", expecting \"data\"");
      break;
    }
    if(flags == TypeJPEG || flags == TypePNG || flags == TypeBMP ||
       flags == TypeGIF || flags == TypeImplicit) {
      value.append(MP4::CoverArt(MP4::CoverArt::Format(flags),
                                 data.mid(pos + 16, length - 16)));
    }
    else {
      debug("MP4: Unknown covr format " + String::number(flags));
    }
    pos += length;
  }
  if(!value.isEmpty())
    addItem(atom->name, value);
}
Ejemplo n.º 2
0
bool TagEditor::open( const QString &fileName )
{
	clear();
#ifdef Q_OS_WIN
	fRef = new FileRef( ( const wchar_t * )fileName.utf16(), false );
#else
	fRef = new FileRef( fileName.toLocal8Bit(), false );
#endif
	if ( !fRef->isNull() && fRef->tag() )
	{
		File &file = *fRef->file();

#if TAGLIB19
		/* Copy ID3v2 to InfoTag */
		if ( instanceOf( file, RIFF::WAV::File ) )
		{
			const Tag &tag = *fRef->tag();
			RIFF::Info::Tag &infoTag = *( ( RIFF::WAV::File & )file ).InfoTag();
			if ( infoTag.isEmpty() && !tag.isEmpty() )
			{
				infoTag.setTitle( tag.title() );
				infoTag.setArtist( tag.artist() );
				infoTag.setAlbum( tag.album() );
				infoTag.setComment( tag.comment() );
				infoTag.setGenre( tag.genre() );
				infoTag.setYear( tag.year() );
				infoTag.setTrack( tag.track() );
			}
		}
#endif

		const Tag &tag = getTag( *fRef, file );
		bool hasTags = !tag.isEmpty();
		setChecked( true );
		if ( hasTags )
		{
			titleE->setText( tag.title().toCString( true ) );
			artistE->setText( tag.artist().toCString( true ) );
			albumE->setText( tag.album().toCString( true ) );
			commentE->setText( tag.comment().toCString( true ) );
			genreE->setText( tag.genre().toCString( true ) );
			yearB->setValue( tag.year() );
			trackB->setValue( tag.track() );
		}
		/* Covers */
		if ( instanceOf( file, MPEG::File ) || instanceOf( file, RIFF::AIFF::File ) )
		{
			pictureB->setEnabled( true );
			if ( hasTags )
			{
				ID3v2::Tag *id3v2 = NULL;
				if ( instanceOf( file, MPEG::File ) )
				{
					MPEG::File &mpegF = ( MPEG::File & )file;
#if TAGLIB19
					if ( mpegF.hasID3v2Tag() )
#endif
						id3v2 = mpegF.ID3v2Tag();
				}
				else if ( instanceOf( file, RIFF::AIFF::File ) )
					id3v2 = ( ( RIFF::AIFF::File & )file ).tag();
				if ( id3v2 )
				{
					const ID3v2::FrameList &frameList = id3v2->frameList( "APIC" );
					if ( !frameList.isEmpty() )
					{
						ID3v2::AttachedPictureFrame &pictureFrame = *( ID3v2::AttachedPictureFrame * )frameList.front();
						pictureMimeType = pictureFrame.mimeType().toCString();
						*picture = pictureFrame.picture();
						pictureB->setChecked( true );
						pictureW->update();
					}
				}
			}
		}
		else if ( instanceOf( file, FLAC::File ) )
		{
			pictureB->setEnabled( true );
			FLAC::File &flacF = ( FLAC::File & )file;
			if ( !flacF.pictureList().isEmpty() )
			{
				FLAC::Picture &flacPicture = *flacF.pictureList().front();
				pictureMimeType = flacPicture.mimeType().toCString();
				*picture = flacPicture.data();
				pictureB->setChecked( true );
				pictureW->update();
				hasTags = true;
			}
		}
		else if ( instanceOf( file, MP4::File ) )
		{
			MP4::ItemListMap &itemListMap = ( ( MP4::File & )file ).tag()->itemListMap();
			MP4::ItemListMap::ConstIterator it = itemListMap.find( "covr" );
			pictureB->setEnabled( true );
			if ( it != itemListMap.end() )
			{
				MP4::CoverArtList coverArtList = it->second.toCoverArtList();
				if ( !coverArtList.isEmpty() )
				{
					MP4::CoverArt coverArt = coverArtList.front();
					switch ( coverArt.format() )
					{
						case MP4::CoverArt::JPEG:
							pictureMimeType = "image/jpeg";
							break;
						case MP4::CoverArt::PNG:
							pictureMimeType = "image/png";
							break;
#if TAGLIB18
						case MP4::CoverArt::BMP:
							pictureMimeType = "image/bmp";
							break;
						case MP4::CoverArt::GIF:
							pictureMimeType = "image/gif";
							break;
#endif
						default:
							break;
					}
					if ( !pictureMimeType.isEmpty() )
					{
						*picture = coverArt.data();
						pictureB->setChecked( true );
						pictureW->update();
						hasTags = true;
					}
				}
			}
		}
		else if ( isOgg( file ) )
		{
			const Ogg::XiphComment *xiphComment = getXiphComment( file );
			if ( xiphComment )
			{
				const Ogg::FieldListMap &fieldListMap = xiphComment->fieldListMap();
				Ogg::FieldListMap::ConstIterator it = fieldListMap.find( "METADATA_BLOCK_PICTURE" );
				pictureB->setEnabled( true );
				if ( it != fieldListMap.end() && !it->second.isEmpty() )
				{
					/* OGG picture and FLAC picture are the same except OGG picture is encoded into Base64 */
					QByteArray pict_frame_decoded = QByteArray::fromBase64( it->second.front().toCString() );
					FLAC::Picture flacPicture;
					if ( flacPicture.parse( ByteVector( pict_frame_decoded.data(), pict_frame_decoded.size() ) ) )
					{
						pictureMimeType = flacPicture.mimeType().toCString();
						*picture = flacPicture.data();
						pictureB->setChecked( true );
						pictureW->update();
					}
				}
			}
		}
		pictureBChecked = pictureB->isChecked();
		setChecked( hasTags );
		return true;
	}
	delete fRef;
	fRef = NULL;
	return false;
}