CUETools.Flake: better use of new window functions

This commit is contained in:
Grigory Chudov
2014-12-08 22:18:34 -05:00
parent c671f200df
commit 6201101ccf
14 changed files with 679 additions and 707 deletions

View File

@@ -1162,7 +1162,10 @@ namespace CUETools.Codecs.ALAC
_windowcount = 0;
calculate_window(window, lpc.window_welch, WindowFunction.Welch);
calculate_window(window, lpc.window_bartlett, WindowFunction.Bartlett);
calculate_window(window, lpc.window_tukey, WindowFunction.Tukey);
calculate_window(window, (w, wsz) =>
{
lpc.window_tukey(w, wsz, 0.5);
}, WindowFunction.Tukey);
calculate_window(window, lpc.window_hann, WindowFunction.Hann);
calculate_window(window, lpc.window_flattop, WindowFunction.Flattop);
int tukey_parts = 2;
@@ -1171,7 +1174,7 @@ namespace CUETools.Codecs.ALAC
for (int m = 0; m < tukey_parts; m++)
calculate_window(window, (w, wsz) =>
{
lpc.window_punchout_tukey(w, wsz, 0.1,
lpc.window_punchout_tukey(w, wsz, 0.1, 0.1,
m / (tukey_parts + overlap_units),
(m + 1 + overlap_units) / (tukey_parts + overlap_units));
}, WindowFunction.PartialTukey);
@@ -1183,7 +1186,7 @@ namespace CUETools.Codecs.ALAC
for (int m = 0; m < tukey_parts; m++)
calculate_window(window, (w, wsz) =>
{
lpc.window_punchout_tukey(w, wsz, 0.1,
lpc.window_punchout_tukey(w, wsz, 0.1, 0.1,
m / (tukey_parts + overlap_units),
(m + 1 + overlap_units) / (tukey_parts + overlap_units));
}, WindowFunction.PunchoutTukey);

View File

@@ -1118,7 +1118,10 @@ namespace CUETools.Codecs.FLACCL
{
calculate_window(task, lpc.window_welch, WindowFunction.Welch);
calculate_window(task, lpc.window_flattop, WindowFunction.Flattop);
calculate_window(task, lpc.window_tukey, WindowFunction.Tukey);
calculate_window(task, (w, wsz) =>
{
lpc.window_tukey(w, wsz, 0.5);
}, WindowFunction.Tukey);
calculate_window(task, lpc.window_hann, WindowFunction.Hann);
calculate_window(task, lpc.window_bartlett, WindowFunction.Bartlett);
if (task.nWindowFunctions == 0)

View File

@@ -8,6 +8,7 @@ namespace CUETools.Codecs.FLAKE
{
best = new FlacSubframe();
sf = new LpcSubframeInfo();
best_fixed = new ulong[5];
lpc_ctx = new LpcContext[lpc.MAX_LPC_WINDOWS];
for (int i = 0; i < lpc.MAX_LPC_WINDOWS; i++)
lpc_ctx[i] = new LpcContext();
@@ -20,6 +21,8 @@ namespace CUETools.Codecs.FLAKE
samples = s;
obits = bps - w;
wbits = w;
for (int o = 0; o <= 4; o++)
best_fixed[o] = 0;
best.residual = r;
best.type = SubframeType.Verbatim;
best.size = AudioSamples.UINT32_MAX;
@@ -35,6 +38,7 @@ namespace CUETools.Codecs.FLAKE
public int wbits;
public int* samples;
public uint done_fixed;
public ulong[] best_fixed;
public LpcContext[] lpc_ctx;
public LpcSubframeInfo sf;
};

File diff suppressed because it is too large Load Diff

View File

@@ -3,11 +3,13 @@ namespace CUETools.Codecs.FLAKE
{
public enum StereoMethod
{
Independent = 0,
Estimate = 1,
Evaluate = 2,
Search = 3,
EstimateX = 4,
EvaluateX = 5,
Invalid,
Independent,
Estimate,
Evaluate,
Search,
EstimateX,
EvaluateX,
EstimateFixed,
}
}

View File

@@ -8,11 +8,20 @@
Hann = 4,
Flattop = 8,
Bartlett = 16,
TukeyFlattop = 10,
PartialTukey = 32,
TukeyPartialTukey = 34,
TukeyFlattopPartialTukey = 42,
PunchoutTukey = 64,
TukeyFlattopPartialTukeyPunchoutTukey = 106,
Tukey2 = 32,
Tukey3 = 64,
Tukey4 = (1 << 7),
Tukey2A = (1 << 9),
Tukey2B = (1 << 10),
Tukey3A = (1 << 11),
Tukey3B = (1 << 12),
Tukey4A = (1 << 13),
Tukey4B = (1 << 14),
Tukey1A = (1 << 15),
Tukey1B = (1 << 16),
Tukey1X = (1 << 17),
Tukey2X = (1 << 18),
Tukey3X = (1 << 19),
Tukey4X = (1 << 20),
}
}

View File

@@ -2,6 +2,7 @@
{
public enum WindowMethod
{
Invalid,
Evaluate,
Search,
Estimate,

View File

@@ -81,11 +81,23 @@ namespace CUETools.Codecs
if (attribute is DefaultValueForModeAttribute)
{
var defaultValueForMode = attribute as DefaultValueForModeAttribute;
res &= (int)property.GetValue(this) == defaultValueForMode.m_values[index];
res &= property.GetValue(this).Equals(defaultValueForMode.m_values[index]);
}
return res;
}
public int GuessEncoderMode()
{
string defaultMode;
string[] modes = this.GetSupportedModes(out defaultMode).Split(' ');
if (modes == null || modes.Length < 1)
return -1;
for (int i = 0; i < modes.Length; i++)
if (HasDefaultValuesForMode(i))
return i;
return -1;
}
[Browsable(false)]
[XmlIgnore]
public AudioPCMConfig PCM

View File

@@ -11,21 +11,32 @@ namespace CUETools.Codecs
return log2i((uint)v);
}
public static readonly byte[] MultiplyDeBruijnBitPosition = new byte[32]
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
public static int log2i(ulong v)
{
int n = 0;
if (0 != (v & 0xffffffff00000000)) { v >>= 32; n += 32; }
if (0 != (v & 0xffff0000)) { v >>= 16; n += 16; }
if (0 != (v & 0xff00)) { v >>= 8; n += 8; }
return n + byte_to_log2_table[v];
v |= v >> 1; // first round down to one less than a power of 2
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
if (v >> 32 == 0)
return MultiplyDeBruijnBitPosition[(uint)((uint)v * 0x07C4ACDDU) >> 27];
return 32 + MultiplyDeBruijnBitPosition[(uint)((uint)(v >> 32) * 0x07C4ACDDU) >> 27];
}
public static int log2i(uint v)
{
int n = 0;
if (0 != (v & 0xffff0000)) { v >>= 16; n += 16; }
if (0 != (v & 0xff00)) { v >>= 8; n += 8; }
return n + byte_to_log2_table[v];
v |= v >> 1; // first round down to one less than a power of 2
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return MultiplyDeBruijnBitPosition[(uint)(v * 0x07C4ACDDU) >> 27];
}
public static readonly byte[] byte_to_unary_table = new byte[]
@@ -48,26 +59,6 @@ namespace CUETools.Codecs
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
public static readonly byte[] byte_to_log2_table = new byte[]
{
0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
};
#endregion
private byte* buffer_m;

View File

@@ -108,6 +108,12 @@ namespace CUETools.Codecs
writebits(8, (byte)s[i]);
}
public void write(byte[] s)
{
for (int i = 0; i < s.Length; i++)
writebits(8, s[i]);
}
public void writebits_signed(int bits, int val)
{
writebits(bits, val & ((1 << bits) - 1));

View File

@@ -13,13 +13,13 @@ namespace CUETools.Codecs
/// <summary>
/// Resource manager to use;
/// </summary>
public int[] m_values;
public object[] m_values;
/// <summary>
/// Construct the description attribute
/// </summary>
/// <param name="text"></param>
public DefaultValueForModeAttribute(params int[] values)
public DefaultValueForModeAttribute(params object[] values)
{
this.m_values = values;
}

View File

@@ -5,9 +5,9 @@ namespace CUETools.Codecs
public class lpc
{
public const int MAX_LPC_ORDER = 32;
public const int MAX_LPC_WINDOWS = 8;
public const int MAX_LPC_WINDOWS = 16;
public const int MAX_LPC_PRECISIONS = 4;
public const int MAX_LPC_SECTIONS = 32;
public const int MAX_LPC_SECTIONS = 128;
public unsafe static void window_welch(float* window, int L)
{
@@ -47,9 +47,8 @@ namespace CUETools.Codecs
window[n] = (float)(1.0 - 1.93 * Math.Cos(2.0 * Math.PI * n / N) + 1.29 * Math.Cos(4.0 * Math.PI * n / N) - 0.388 * Math.Cos(6.0 * Math.PI * n / N) + 0.0322 * Math.Cos(8.0 * Math.PI * n / N));
}
public unsafe static void window_tukey(float* window, int L)
public unsafe static void window_tukey(float* window, int L, double p)
{
double p = 0.5;
int z = 0;
int Np = (int)(p / 2.0 * L) - z;
if (Np > 0)
@@ -63,28 +62,29 @@ namespace CUETools.Codecs
}
}
public unsafe static void window_punchout_tukey(float* window, int L, double p, double start, double end)
public unsafe static void window_punchout_tukey(float* window, int L, double p, double p1, double start, double end)
{
int start_n = (int)(start * L);
int end_n = (int)(end * L);
int Np = (int)(p / 2.0 * L);
int Np1 = (int)(p1 / 2.0 * L);
int i, n = 0;
if (start_n != 0)
{
for (i = 1; n < Np; n++, i++)
window[n] = (float)(0.5 - 0.5 * Math.Cos(Math.PI * i / Np));
for (; n < start_n - Np; n++)
for (; n < start_n - Np1; n++)
window[n] = 1.0f;
for (i = Np; n < start_n; n++, i--)
window[n] = (float)(0.5 - 0.5 * Math.Cos(Math.PI * i / Np));
for (i = Np1; n < start_n; n++, i--)
window[n] = (float)(0.5 - 0.5 * Math.Cos(Math.PI * i / Np1));
}
for (; n < end_n; n++)
window[n] = 0.0f;
if (end_n != L)
{
for (i = 1; n < end_n + Np; n++, i++)
window[n] = (float)(0.5 - 0.5 * Math.Cos(Math.PI * i / Np));
for (i = 1; n < end_n + Np1; n++, i++)
window[n] = (float)(0.5 - 0.5 * Math.Cos(Math.PI * i / Np1));
for (; n < L - Np; n++)
window[n] = 1.0f;
for (i = Np; n < L; n++, i--)
@@ -358,11 +358,11 @@ namespace CUETools.Codecs
}
static public unsafe void
compute_autocorr_glue(/*const*/ int* data, float* window, int offs, int sz, int min, int lag, double* autoc)
compute_autocorr_glue(/*const*/ int* data, float* window, int offs, int offs1, int min, int lag, double* autoc)
{
double* data1 = stackalloc double[lag + lag];
for (int i = -lag; i < lag; i++)
data1[i + lag] = offs + i >= 0 && offs + i < sz ? data[offs + i] * window[offs + i] : 0;
data1[i + lag] = offs + i >= 0 && offs + i < offs1 ? data[offs + i] * window[offs + i] : 0;
for (int i = min; i <= lag; ++i)
{
double temp = 0;

View File

@@ -83,7 +83,7 @@ namespace CUETools.Codecs
else if (m_type == SectionType.Data)
lpc.compute_autocorr(data + m_start, window + m_start, m_end - m_start, min_order, order, autoc);
else if (m_type == SectionType.Glue)
lpc.compute_autocorr_glue(data, window, m_start, blocksize, min_order, order, autoc);
lpc.compute_autocorr_glue(data, window, m_start, m_end, min_order, order, autoc);
else if (m_type == SectionType.OneGlue)
lpc.compute_autocorr_glue(data + m_start, min_order, order, autoc);
}
@@ -109,6 +109,7 @@ namespace CUETools.Codecs
&& w == window_segment[i1 * stride + x])
alias[i1, boundaries.Count] = i;
}
if (boundaries.Count >= lpc.MAX_LPC_SECTIONS * 2) throw new IndexOutOfRangeException();
types[i, boundaries.Count] =
boundaries.Count >= lpc.MAX_LPC_SECTIONS * 2 - 2 ?
LpcWindowSection.SectionType.Data : w == 0.0 ?
@@ -143,6 +144,7 @@ namespace CUETools.Codecs
// leave room for glue
if (secs[i] >= lpc.MAX_LPC_SECTIONS - 1)
{
throw new IndexOutOfRangeException();
window_sections[secs[i] - 1].m_type = LpcWindowSection.SectionType.Data;
window_sections[secs[i] - 1].m_end = boundaries[j + 1];
continue;
@@ -165,6 +167,7 @@ namespace CUETools.Codecs
secs[i]--;
continue;
}
if (section_id >= lpc.MAX_LPC_SECTIONS) throw new IndexOutOfRangeException();
if (alias_set[i, j] != 0
&& types[i, j] != SectionType.Zero
&& section_id < lpc.MAX_LPC_SECTIONS)
@@ -174,7 +177,7 @@ namespace CUETools.Codecs
sections[i1 * lpc.MAX_LPC_SECTIONS + secs[i1] - 1].m_id = section_id;
section_id++;
}
// TODO: section_id for glue?
// TODO: section_id for glue? nontrivial, must be sure next sections are the same size
if (sec > 0
&& (window_sections[sec].m_type == SectionType.One || window_sections[sec].m_type == SectionType.OneLarge)
&& window_sections[sec].m_end - window_sections[sec].m_start >= lpc.MAX_LPC_ORDER
@@ -203,6 +206,15 @@ namespace CUETools.Codecs
}
for (int i = 0; i < _windowcount; i++)
{
for (int s = 0; s < secs[i]; s++)
{
LpcWindowSection* window_sections = sections + i * lpc.MAX_LPC_SECTIONS;
if (window_sections[s].m_type == SectionType.Glue
|| window_sections[s].m_type == SectionType.OneGlue)
{
window_sections[s].m_end = window_sections[s + 1].m_end;
}
}
while (secs[i] < lpc.MAX_LPC_SECTIONS)
{
LpcWindowSection* window_sections = sections + i * lpc.MAX_LPC_SECTIONS;

View File

@@ -94,21 +94,23 @@ namespace CUETools.FlakeExe
Console.WriteLine();
Console.WriteLine(" -0 .. -11 Compression level, default 7; 9..11 require --lax");
Console.WriteLine(" -o <file> Output filename, or \"-\" for stdout, or nul.");
Console.WriteLine(" -p # Padding bytes.");
Console.WriteLine(" -q --quiet Quiet mode.");
Console.WriteLine(" --lax Allow non-subset modes");
Console.WriteLine(" --verify Verify during encoding.");
Console.WriteLine(" -P, --padding # Padding bytes.");
Console.WriteLine(" -q, --quiet Quiet mode.");
Console.WriteLine(" -f, --force Overwrite existing files.");
Console.WriteLine(" -V, --verify Verify a correct encoding.");
Console.WriteLine(" -T, --tag FIELD=VAL Add a FLAC tag; may appear multiple times");
Console.WriteLine(" --lax Allow encoder to generate non-Subset files");
Console.WriteLine(" --no-md5 Don't compute MD5 hash.");
Console.WriteLine(" --no-seektable Don't generate a seektable.");
Console.WriteLine(" --ignore-chunk-sizes Ignore WAV length (for pipe input)");
Console.WriteLine(" --ignore-chunk-sizes Ignore data chunk sizes in WAVE files (for pipe input)");
Console.WriteLine();
Console.WriteLine("Advanced Options:");
Console.WriteLine();
Console.WriteLine(" -b # Block size.");
Console.WriteLine(" -v # Variable block size mode (0,4).");
Console.WriteLine(" -t <type> Prediction type (fixed,levinson,search).");
Console.WriteLine(" -s <method> Stereo decorrelation (independent,estimate,evaluate,search).");
Console.WriteLine(" -r #[,#] Rice partition order {max} or {min},{max} (0..8).");
Console.WriteLine(" --vbr # Variable block size mode (0,4).");
Console.WriteLine();
Console.WriteLine("LPC options:");
Console.WriteLine();
@@ -134,15 +136,9 @@ namespace CUETools.FlakeExe
DateTime start = DateTime.Now;
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
bool debug = false, quiet = false;
string prediction_type = null;
string stereo_method = null;
string window_method = null;
string order_method = null;
string window_function = null;
string input_file = null;
string output_file = null;
int min_precision = -1, max_precision = -1,
estimation_depth = -1;
int skip_a = 0, skip_b = 0;
int intarg = -1, vbr_mode = -1, magic = -1;
bool do_seektable = true;
@@ -151,6 +147,7 @@ namespace CUETools.FlakeExe
var settings = new FlakeWriterSettings() { AllowNonSubset = true };
bool allowNonSubset = false;
bool ignore_chunk_sizes = false;
bool force = false;
#if FINETUNE
int finetune_depth = -1;
#endif
@@ -158,112 +155,141 @@ namespace CUETools.FlakeExe
for (int arg = 0; arg < args.Length; arg++)
{
bool ok = true;
if (args[arg].Length == 0)
ok = false;
else if (args[arg] == "--debug")
debug = true;
else if ((args[arg] == "-q" || args[arg] == "--quiet"))
quiet = true;
else if (args[arg] == "--verify")
settings.DoVerify = true;
else if (args[arg] == "--no-seektable")
do_seektable = false;
else if (args[arg] == "--ignore-chunk-sizes")
ignore_chunk_sizes = true;
else if (args[arg] == "--no-md5")
settings.DoMD5 = false;
else if (args[arg] == "--lax")
allowNonSubset = true;
else if (args[arg] == "--buffered")
buffered = true;
else if ((args[arg] == "-o" || args[arg] == "--output") && ++arg < args.Length)
output_file = args[arg];
else if ((args[arg] == "-t" || args[arg] == "--prediction-type") && ++arg < args.Length)
prediction_type = args[arg];
else if ((args[arg] == "-s" || args[arg] == "--stereo") && ++arg < args.Length)
stereo_method = args[arg];
else if ((args[arg] == "-m" || args[arg] == "--order-method") && ++arg < args.Length)
order_method = args[arg];
else if ((args[arg] == "-w" || args[arg] == "--window") && ++arg < args.Length)
window_function = args[arg];
else if (args[arg] == "--window-method" && ++arg < args.Length)
window_method = args[arg];
else if ((args[arg] == "-r" || args[arg] == "--partition-order") && ++arg < args.Length)
try
{
int min_partition_order, max_partition_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_partition_order)
&& (settings.MinPartitionOrder = min_partition_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_partition_order)
&& (settings.MaxPartitionOrder = max_partition_order) != -1)
|| (int.TryParse(args[arg], out max_partition_order)
&& (settings.MaxPartitionOrder = max_partition_order) != -1);
}
else if ((args[arg] == "-l" || args[arg] == "--lpc-order") && ++arg < args.Length)
{
int min_lpc_order, max_lpc_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_lpc_order)
&& (settings.MinLPCOrder = min_lpc_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_lpc_order)
&& (settings.MaxLPCOrder = max_lpc_order) != -1)
|| (int.TryParse(args[arg], out max_lpc_order)
&& (settings.MaxLPCOrder = max_lpc_order) != -1);
}
else if ((args[arg] == "-f" || args[arg] == "--fixed-order") && ++arg < args.Length)
{
int min_fixed_order, max_fixed_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_fixed_order)
&& (settings.MinFixedOrder = min_fixed_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_fixed_order)
&& (settings.MaxFixedOrder = max_fixed_order) != -1)
|| (int.TryParse(args[arg], out max_fixed_order)
&& (settings.MaxFixedOrder = max_fixed_order) != -1);
}
else if (args[arg] == "--skip" && ++arg < args.Length)
{
ok = (args[arg].Split(',').Length == 2 &&
int.TryParse(args[arg].Split(',')[0], out skip_a) &&
int.TryParse(args[arg].Split(',')[1], out skip_b)) ||
int.TryParse(args[arg], out skip_a);
}
else if ((args[arg] == "-e" || args[arg] == "--estimation-depth") && ++arg < args.Length)
ok = int.TryParse(args[arg], out estimation_depth);
else if ((args[arg] == "-c" || args[arg] == "--max-precision") && ++arg < args.Length)
{
ok = (args[arg].Split(',').Length == 2 &&
int.TryParse(args[arg].Split(',')[0], out min_precision) &&
int.TryParse(args[arg].Split(',')[1], out max_precision)) ||
int.TryParse(args[arg], out max_precision);
}
else if ((args[arg] == "-v" || args[arg] == "--vbr"))
ok = (++arg < args.Length) && int.TryParse(args[arg], out vbr_mode);
else if ((args[arg] == "-b" || args[arg] == "--blocksize") && ++arg < args.Length && int.TryParse(args[arg], out intarg))
settings.BlockSize = intarg;
else if ((args[arg] == "-p" || args[arg] == "--padding") && ++arg < args.Length && int.TryParse(args[arg], out intarg))
settings.Padding = intarg;
else if (args[arg] == "--magic" && ++arg < args.Length)
ok = int.TryParse(args[arg], out magic);
if (args[arg].Length == 0)
ok = false;
else if (args[arg] == "--debug")
debug = true;
else if ((args[arg] == "-f" || args[arg] == "--force"))
force = true;
else if ((args[arg] == "-q" || args[arg] == "--quiet"))
quiet = true;
else if ((args[arg] == "-V" || args[arg] == "--verify"))
settings.DoVerify = true;
else if (args[arg] == "--no-seektable")
do_seektable = false;
else if (args[arg] == "--ignore-chunk-sizes")
ignore_chunk_sizes = true;
else if (args[arg] == "--no-md5")
settings.DoMD5 = false;
else if (args[arg] == "--lax")
allowNonSubset = true;
else if (args[arg] == "--buffered")
buffered = true;
else if ((args[arg] == "-o" || args[arg] == "--output") && ++arg < args.Length)
output_file = args[arg];
else if ((args[arg] == "-T" || args[arg] == "--tag") && ++arg < args.Length)
{
var tags = settings.Tags != null ? new List<string>(settings.Tags) : new List<string>();
tags.Add(args[arg]);
settings.Tags = tags.ToArray();
}
else if ((args[arg] == "-t" || args[arg] == "--prediction-type") && ++arg < args.Length)
settings.PredictionType = Flake.LookupPredictionType(args[arg]);
else if ((args[arg] == "-s" || args[arg] == "--stereo") && ++arg < args.Length)
settings.StereoMethod = Flake.LookupStereoMethod(args[arg]);
else if ((args[arg] == "-m" || args[arg] == "--order-method") && ++arg < args.Length)
order_method = args[arg];
else if ((args[arg] == "-w" || args[arg] == "--window") && ++arg < args.Length)
settings.WindowFunctions = Flake.LookupWindowFunction(args[arg]);
else if (args[arg] == "--window-method" && ++arg < args.Length)
settings.WindowMethod = Flake.LookupWindowMethod(args[arg]);
else if ((args[arg] == "-r" || args[arg] == "--partition-order") && ++arg < args.Length)
{
int min_partition_order, max_partition_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_partition_order)
&& (settings.MinPartitionOrder = min_partition_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_partition_order)
&& (settings.MaxPartitionOrder = max_partition_order) != -1)
|| (int.TryParse(args[arg], out max_partition_order)
&& (settings.MaxPartitionOrder = max_partition_order) != -1);
}
else if ((args[arg] == "-l" || args[arg] == "--lpc-order") && ++arg < args.Length)
{
int min_lpc_order, max_lpc_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_lpc_order)
&& (settings.MinLPCOrder = min_lpc_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_lpc_order)
&& (settings.MaxLPCOrder = max_lpc_order) != -1)
|| (int.TryParse(args[arg], out max_lpc_order)
&& (settings.MaxLPCOrder = max_lpc_order) != -1);
}
else if ((args[arg] == "--fixed-order") && ++arg < args.Length)
{
int min_fixed_order, max_fixed_order;
ok = (args[arg].Split(',').Length == 2
&& int.TryParse(args[arg].Split(',')[0], out min_fixed_order)
&& (settings.MinFixedOrder = min_fixed_order) != -1
&& int.TryParse(args[arg].Split(',')[1], out max_fixed_order)
&& (settings.MaxFixedOrder = max_fixed_order) != -1)
|| (int.TryParse(args[arg], out max_fixed_order)
&& (settings.MaxFixedOrder = max_fixed_order) != -1);
}
else if (args[arg] == "--skip" && ++arg < args.Length)
{
ok = (args[arg].Split(',').Length == 2 &&
int.TryParse(args[arg].Split(',')[0], out skip_a) &&
int.TryParse(args[arg].Split(',')[1], out skip_b)) ||
int.TryParse(args[arg], out skip_a);
}
else if ((args[arg] == "-e" || args[arg] == "--estimation-depth") && ++arg < args.Length)
{
settings.EstimationDepth = int.Parse(args[arg]);
}
else if (args[arg] == "--tukey-parts" && ++arg < args.Length)
settings.TukeyParts = int.Parse(args[arg]);
else if (args[arg] == "--tukey-overlap" && ++arg < args.Length)
settings.TukeyOverlap = double.Parse(args[arg]);
else if (args[arg] == "--tukey-p" && ++arg < args.Length)
settings.TukeyP = double.Parse(args[arg]);
else if ((args[arg] == "-c" || args[arg] == "--max-precision") && ++arg < args.Length)
{
int min_precision = -1, max_precision = -1;
ok = (args[arg].Split(',').Length == 2 &&
int.TryParse(args[arg].Split(',')[0], out min_precision) &&
int.TryParse(args[arg].Split(',')[1], out max_precision)) ||
int.TryParse(args[arg], out max_precision);
settings.MinPrecisionSearch = min_precision;
settings.MaxPrecisionSearch = max_precision;
}
else if (args[arg] == "--vbr")
ok = (++arg < args.Length) && int.TryParse(args[arg], out vbr_mode);
else if ((args[arg] == "-b" || args[arg] == "--blocksize") && ++arg < args.Length && int.TryParse(args[arg], out intarg))
settings.BlockSize = intarg;
else if ((args[arg] == "-P" || args[arg] == "--padding") && ++arg < args.Length && int.TryParse(args[arg], out intarg))
settings.Padding = intarg;
else if (args[arg] == "--magic" && ++arg < args.Length)
ok = int.TryParse(args[arg], out magic);
#if FINETUNE
else if (args[arg] == "--depth" && ++arg < args.Length)
ok = int.TryParse(args[arg], out finetune_depth);
#endif
else if (args[arg] == "--coefs" && ++arg < args.Length)
coeffs = args[arg];
else if (args[arg] != "-" && args[arg][0] == '-' && int.TryParse(args[arg].Substring(1), out intarg))
{
ok = intarg >= 0 && intarg <= 11;
settings.EncoderModeIndex = intarg;
else if (args[arg] == "--coefs" && ++arg < args.Length)
coeffs = args[arg];
else if (args[arg] != "-" && args[arg][0] == '-' && int.TryParse(args[arg].Substring(1), out intarg))
{
ok = intarg >= 0 && intarg <= 11;
settings.EncoderModeIndex = intarg;
}
else if ((args[arg][0] != '-' || args[arg] == "-") && input_file == null)
input_file = args[arg];
else
ok = false;
if (!ok)
{
Usage();
return 1;
}
}
else if ((args[arg][0] != '-' || args[arg] == "-") && input_file == null)
input_file = args[arg];
else
ok = false;
if (!ok)
catch (Exception ex)
{
Usage();
return 1;
Console.WriteLine("");
Console.WriteLine("{0}: {1}", args[arg - 1], ex.Message);
return 5;
}
}
if (input_file == null || ((input_file == "-" || Path.GetExtension(input_file) == ".flac") && output_file == null))
@@ -274,8 +300,8 @@ namespace CUETools.FlakeExe
if (!quiet)
{
Console.WriteLine("CUETools.Flake, Copyright (C) 2009-2013 Grigory Chudov.");
Console.WriteLine("Based on Flake encoder by Justin Ruggles, <http://flake-enc.sourceforge.net/>.");
Console.WriteLine("CUETools.Flake, Copyright (C) 2009-2014 Grigory Chudov.");
Console.WriteLine("Initially based on Flake encoder by Justin Ruggles.");
Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
}
@@ -342,6 +368,13 @@ namespace CUETools.FlakeExe
audioSource = new AudioPipe(audioSource, 0x10000);
if (output_file == null)
output_file = Path.ChangeExtension(input_file, "flac");
if (File.Exists(output_file) && !force)
{
Usage();
Console.WriteLine();
Console.WriteLine("File '{0}' already exists.", output_file);
return 9;
}
settings.PCM = audioSource.PCM;
settings.AllowNonSubset = allowNonSubset;
FlakeWriter flake;
@@ -354,22 +387,8 @@ namespace CUETools.FlakeExe
settings);
flake.FinalSampleCount = audioSource.Length < 0 ? -1 : audioSource.Length - skip_a - skip_b;
if (prediction_type != null)
flake.PredictionType = Flake.LookupPredictionType(prediction_type);
if (stereo_method != null)
flake.StereoMethod = Flake.LookupStereoMethod(stereo_method);
if (window_method != null)
flake.WindowMethod = Flake.LookupWindowMethod(window_method);
if (order_method != null)
flake.OrderMethod = Flake.LookupOrderMethod(order_method);
if (window_function != null)
flake.WindowFunction = Flake.LookupWindowFunction(window_function);
if (min_precision >= 0)
flake.MinPrecisionSearch = min_precision;
if (max_precision >= 0)
flake.MaxPrecisionSearch = max_precision;
if (estimation_depth >= 0)
flake.EstimationDepth = estimation_depth;
if (vbr_mode >= 0)
flake.VBRMode = vbr_mode;
if (magic >= 0)
@@ -479,20 +498,23 @@ namespace CUETools.FlakeExe
{
settings = flake.Settings as FlakeWriterSettings;
Console.SetOut(stdout);
Console.Out.WriteLine("{17}\t{0}\t{1:0.00}\t{2}\t{3}\t{4}\t{5}\t{6}..{7}\t{8}..{9}\t{10}..{11}\t{12}..{13}\t{14}\t{15}\t{16}\t{18}",
Console.Out.WriteLine("{17}\t{0}\t{1:0.000}\t{2}\t{3}\t{4}\t{5}\t{6}..{7}\t{8}..{9}\t{10}..{11}\t{12}..{13}\t{14}\t{15}\t{16}\t{18}",
flake.TotalSize,
flake.UserProcessorTime.TotalSeconds > 0 ? flake.UserProcessorTime.TotalSeconds : totalElapsed.TotalSeconds,
flake.PredictionType.ToString().PadRight(15),
flake.StereoMethod.ToString().PadRight(15),
(flake.OrderMethod.ToString() + "(" + flake.EstimationDepth.ToString() + ")").PadRight(15),
(flake.WindowMethod.ToString().PadRight(10) + "(" +
((flake.WindowFunction & WindowFunction.Tukey) != 0 ? "T" : " ") +
((flake.WindowFunction & WindowFunction.PartialTukey) != 0 ? "P" : " ") +
(((flake.WindowFunction & WindowFunction.PunchoutTukey) != 0 ? "O" : "") +
((flake.WindowFunction & WindowFunction.Welch) == 0 ? "" : "W") +
((flake.WindowFunction & WindowFunction.Hann) == 0 ? "" : "H") +
((flake.WindowFunction & WindowFunction.Flattop) == 0 ? "" : "F") +
((flake.WindowFunction & WindowFunction.Bartlett) == 0 ? "" : "B")).PadRight(1))
settings.PredictionType.ToString().PadRight(15),
settings.StereoMethod.ToString().PadRight(15),
(flake.OrderMethod.ToString() + "(" + settings.EstimationDepth.ToString() + ")").PadRight(15),
(settings.WindowMethod.ToString().PadRight(10) + "(" +
((settings.WindowFunctions & WindowFunction.Tukey1X) != 0 ? "X" : (settings.WindowFunctions & WindowFunction.Tukey) != 0 ? "T" : (settings.WindowFunctions & WindowFunction.Tukey1A) != 0 ? "A" : (settings.WindowFunctions & WindowFunction.Tukey1B) != 0 ? "B" : " ") +
((settings.WindowFunctions & WindowFunction.Tukey2X) != 0 ? "X" : (settings.WindowFunctions & WindowFunction.Tukey2) != 0 ? "2" : (settings.WindowFunctions & WindowFunction.Tukey2A) != 0 ? "A" : (settings.WindowFunctions & WindowFunction.Tukey2B) != 0 ? "B" : " ") +
((settings.WindowFunctions & WindowFunction.Tukey3X) != 0 ? "X" : (settings.WindowFunctions & WindowFunction.Tukey3) != 0 ? "3" : (settings.WindowFunctions & WindowFunction.Tukey3A) != 0 ? "A" : (settings.WindowFunctions & WindowFunction.Tukey3B) != 0 ? "B" : " ") +
((settings.WindowFunctions & WindowFunction.Tukey4X) != 0 ? "X" : ((settings.WindowFunctions & WindowFunction.Tukey4) != 0 ? "4" : (settings.WindowFunctions & WindowFunction.Tukey4A) != 0 ? "A" : (settings.WindowFunctions & WindowFunction.Tukey4B) != 0 ? "B" : " ") +
((settings.WindowFunctions & WindowFunction.Welch) == 0 ? "" : "W") +
((settings.WindowFunctions & WindowFunction.Hann) == 0 ? "" : "H") +
((settings.WindowFunctions & WindowFunction.Flattop) == 0 ? "" : "F") +
((settings.WindowFunctions & WindowFunction.Bartlett) == 0 ? "" : "B")).PadRight(1))
+ ((settings.WindowFunctions & (WindowFunction.Tukey1X|WindowFunction.Tukey2X|WindowFunction.Tukey2X|WindowFunction.Tukey3X|WindowFunction.Tukey4X)) != 0 ?
("," + settings.TukeyOverlap.ToString("F3").PadLeft(6) + "," + settings.TukeyP.ToString("F3")) : "").PadRight(13)
+")",
settings.MinPartitionOrder,
settings.MaxPartitionOrder,
@@ -500,22 +522,13 @@ namespace CUETools.FlakeExe
settings.MaxLPCOrder,
settings.MinFixedOrder,
settings.MaxFixedOrder,
flake.MinPrecisionSearch,
flake.MaxPrecisionSearch,
settings.MinPrecisionSearch,
settings.MaxPrecisionSearch,
flake.Settings.BlockSize,
flake.VBRMode,
coeffs ?? "",
audioSource.Position * audioSource.PCM.BlockAlign,
(flake.PredictionType == PredictionType.Search && flake.StereoMethod == StereoMethod.Evaluate && flake.WindowMethod == WindowMethod.EvaluateN && flake.WindowFunction == (WindowFunction.PunchoutTukey | WindowFunction.PartialTukey | WindowFunction.Tukey) && flake.EstimationDepth == 2 && flake.MinPrecisionSearch == 0 && settings.HasDefaultValuesForMode(8)) ? "8" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Evaluate && flake.WindowMethod == WindowMethod.EvaluateN && flake.WindowFunction == (WindowFunction.PunchoutTukey | WindowFunction.PartialTukey | WindowFunction.Tukey) && flake.EstimationDepth == 1 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(7)) ? "7" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Evaluate && flake.WindowMethod == WindowMethod.EstimateN && flake.WindowFunction == (WindowFunction.PunchoutTukey | WindowFunction.PartialTukey) && flake.EstimationDepth == 1 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(6)) ? "6" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Evaluate && flake.WindowMethod == WindowMethod.Estimate && flake.WindowFunction == WindowFunction.PunchoutTukey && flake.EstimationDepth == 1 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(5)) ? "5" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Estimate && flake.WindowMethod == WindowMethod.Estimate && flake.WindowFunction == WindowFunction.PunchoutTukey && flake.EstimationDepth == 2 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(4)) ? "4" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Estimate && flake.WindowMethod == WindowMethod.Estimate && flake.WindowFunction == WindowFunction.PunchoutTukey && flake.EstimationDepth == 1 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(3)) ? "3" :
(flake.PredictionType == PredictionType.Levinson && flake.StereoMethod == StereoMethod.Estimate && flake.WindowMethod == WindowMethod.Estimate && flake.WindowFunction == WindowFunction.PartialTukey && flake.EstimationDepth == 1 && flake.MinPrecisionSearch == 1 && settings.HasDefaultValuesForMode(2)) ? "2" :
(flake.PredictionType == PredictionType.Fixed && flake.StereoMethod == StereoMethod.Independent && flake.WindowMethod == WindowMethod.Estimate && flake.MinPrecisionSearch == 1 && flake.WindowFunction == WindowFunction.Tukey && settings.HasDefaultValuesForMode(1)) ? "1" :
(flake.PredictionType == PredictionType.Fixed && flake.StereoMethod == StereoMethod.Independent && flake.WindowMethod == WindowMethod.Estimate && flake.MinPrecisionSearch == 1 && flake.WindowFunction == WindowFunction.Tukey && settings.HasDefaultValuesForMode(0)) ? "0" :
"?"
settings.GuessEncoderMode().ToString().Replace("-1","?")
);
}
#endif