Files
Aaru/Aaru.Compression/FLAC.cs

147 lines
7.4 KiB
C#
Raw Normal View History

2021-11-04 18:57:24 +00:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : FLAC.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Compression algorithms.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2021-11-04 18:57:24 +00:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Compression;
2021-11-04 18:57:24 +00:00
using System.IO;
using System.Runtime.InteropServices;
using CUETools.Codecs;
using CUETools.Codecs.Flake;
2022-03-15 01:37:37 +00:00
// ReSharper disable once InconsistentNaming
2022-03-18 01:32:22 +00:00
/// <summary>
/// Implements the FLAC lossless audio compression algorithm
/// </summary>
2021-11-04 18:57:24 +00:00
public class FLAC
{
2022-03-07 07:36:44 +00:00
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true;
2021-11-04 18:57:24 +00:00
[DllImport("libAaru.Compression.Native", SetLastError = true)]
2022-03-15 01:37:37 +00:00
static extern nuint AARU_flac_decode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize);
2021-11-04 18:57:24 +00:00
[DllImport("libAaru.Compression.Native", SetLastError = true)]
2022-03-15 01:37:37 +00:00
static extern nuint AARU_flac_encode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize, uint blocksize, int doMidSideStereo,
int looseMidSideStereo, string apodization,
uint maxLpcOrder, uint qlpCoeffPrecision,
int doQlpCoeffPrecSearch, int doExhaustiveModelSearch,
uint minResidualPartitionOrder,
uint maxResidualPartitionOrder, string applicationID,
uint applicationIDLen);
2021-11-04 18:57:24 +00:00
/// <summary>Decodes a buffer compressed with FLAC</summary>
/// <param name="source">Encoded buffer</param>
/// <param name="destination">Buffer where to write the decoded data</param>
/// <returns>The number of decoded bytes</returns>
public static int DecodeBuffer(byte[] source, byte[] destination)
{
if(Native.IsSupported)
return (int)AARU_flac_decode_redbook_buffer(destination, (nuint)destination.Length, source,
(nuint)source.Length);
var flacMs = new MemoryStream(source);
var flakeReader = new AudioDecoder(new DecoderSettings(), "", flacMs);
int samples = destination.Length / 4;
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, destination, samples);
flakeReader.Read(audioBuffer, samples);
flakeReader.Close();
flacMs.Close();
return samples * 4;
}
/// <summary>Compresses a buffer using FLAC</summary>
/// <param name="source">Data to compress</param>
/// <param name="destination">Buffer to store the compressed data</param>
2022-03-18 01:32:22 +00:00
/// <param name="blockSize">Block size</param>
/// <param name="doMidSideStereo">Do mid side stereo</param>
/// <param name="looseMidSideStereo">Loose mid side stereo</param>
/// <param name="apodization">Apodization algorithm</param>
/// <param name="maxLpcOrder">Maximum LPC order</param>
/// <param name="qlpCoeffPrecision">QLP coefficient precision</param>
/// <param name="doQlpCoeffPrecSearch">Do precise search for QLP coefficient</param>
/// <param name="doExhaustiveModelSearch">Do exhaustive model search</param>
/// <param name="minResidualPartitionOrder">Minimum residual partition order</param>
/// <param name="maxResidualPartitionOrder">Maximum residual partition order</param>
/// <param name="applicationID">Application ID</param>
/// <returns>The size of the compressed data</returns>
2021-11-04 18:57:24 +00:00
public static int EncodeBuffer(byte[] source, byte[] destination, uint blockSize, bool doMidSideStereo,
2022-03-15 01:37:37 +00:00
bool looseMidSideStereo, string apodization, uint maxLpcOrder,
2022-03-07 07:36:44 +00:00
uint qlpCoeffPrecision, bool doQlpCoeffPrecSearch, bool doExhaustiveModelSearch,
uint minResidualPartitionOrder, uint maxResidualPartitionOrder, string applicationID)
2021-11-04 18:57:24 +00:00
{
if(Native.IsSupported)
return (int)AARU_flac_encode_redbook_buffer(destination, (nuint)destination.Length, source,
(nuint)source.Length, blockSize, doMidSideStereo ? 1 : 0,
2022-03-15 01:37:37 +00:00
looseMidSideStereo ? 1 : 0, apodization, maxLpcOrder,
2022-03-07 07:36:44 +00:00
qlpCoeffPrecision, doQlpCoeffPrecSearch ? 1 : 0,
doExhaustiveModelSearch ? 1 : 0, minResidualPartitionOrder,
maxResidualPartitionOrder, applicationID,
(uint)applicationID.Length);
2021-11-04 18:57:24 +00:00
var flakeWriterSettings = new EncoderSettings
{
PCM = AudioPCMConfig.RedBook,
DoMD5 = false,
BlockSize = (int)blockSize,
MinFixedOrder = 0,
MaxFixedOrder = 4,
MinLPCOrder = 1,
MaxLPCOrder = 32,
MaxPartitionOrder = (int)maxResidualPartitionOrder,
StereoMethod = StereoMethod.Estimate,
PredictionType = PredictionType.Search,
WindowMethod = WindowMethod.EvaluateN,
EstimationDepth = 5,
MinPrecisionSearch = 1,
MaxPrecisionSearch = 1,
TukeyParts = 0,
TukeyOverlap = 1.0,
TukeyP = 1.0,
AllowNonSubset = true
};
// Check if FLAKE's block size is bigger than what we want
if(flakeWriterSettings.BlockSize > 4608)
flakeWriterSettings.BlockSize = 4608;
if(flakeWriterSettings.BlockSize < 256)
flakeWriterSettings.BlockSize = 256;
var flacMs = new MemoryStream(destination);
var flakeWriter = new AudioEncoder(flakeWriterSettings, "", flacMs);
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, source, source.Length / 4);
flakeWriter.Write(audioBuffer);
flakeWriter.Close();
flacMs.Close();
return (int)flacMs.Length;
}
}