Skip to content

Commit e06b279

Browse files
committed
v0.7.8d
1 parent b7faec1 commit e06b279

File tree

10 files changed

+88
-55
lines changed

10 files changed

+88
-55
lines changed

src/cmdline.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ void CmdLine::PrintMode()
3636
if (opt.sparse_pcm) std::cout << " sparse-pcm";
3737
if (opt.optimize) {
3838
std::cout << " opt (" << std::format("{:.1f}%", opt.optimize_fraction*100.0);
39-
std::cout << ",n=" << opt.optimize_maxnfunc << ")";
39+
std::cout << ",n=" << opt.optimize_maxnfunc << ",";
40+
41+
std::string cost_str;
42+
switch (opt.optimize_cost) {
43+
case opt.SearchCost::L1:cost_str="L1";break;
44+
case opt.SearchCost::RMS:cost_str="rms";break;
45+
case opt.SearchCost::Golomb:cost_str="glb";break;
46+
case opt.SearchCost::Entropy:cost_str="ent";break;
47+
case opt.SearchCost::Bitplane:cost_str="bpn";break;
48+
default:break;
49+
}
50+
std::cout << cost_str << ")\n";
4051
}
4152
std::cout << std::endl;
4253
}
@@ -66,8 +77,8 @@ int CmdLine::Parse(int argc,char *argv[])
6677
std::string param,uparam;
6778
int k=1;
6879
while (k<argc) {
69-
param=uparam=argv[k];
70-
StrUtils::StrUpper(uparam);
80+
param=argv[k];
81+
uparam=StrUtils::str_up(param);
7182
std::string key,val;
7283
Split(uparam,key,val);
7384

@@ -107,7 +118,17 @@ int CmdLine::Parse(int argc,char *argv[])
107118
if (vs.size()>=2) {
108119
opt.optimize_fraction=std::clamp(std::stod(vs[0]),0.,1.);
109120
opt.optimize_maxnfunc=std::clamp(std::stoi(vs[1]),0,10000);
121+
if (vs.size()>=3) {
122+
std::string cf=StrUtils::str_up(vs[2]);
123+
if (cf=="L1") opt.optimize_cost = opt.SearchCost::L1;
124+
else if (cf=="RMS") opt.optimize_cost = opt.SearchCost::RMS;
125+
else if (cf=="GLB") opt.optimize_cost = opt.SearchCost::Golomb;
126+
else if (cf=="ENT") opt.optimize_cost = opt.SearchCost::Entropy; //default
127+
else if (cf=="BPN") opt.optimize_cost = opt.SearchCost::Bitplane;
128+
else std::cerr << "warning: unknown cost function '" << vs[2] << "'\n";
129+
}
110130
if (opt.optimize_fraction>0. && opt.optimize_maxnfunc>0) opt.optimize=1;
131+
else opt.optimize=0;
111132
} else std::cerr << "unknown option: " << val << '\n';
112133
}
113134
}
@@ -122,7 +143,7 @@ int CmdLine::Parse(int argc,char *argv[])
122143
} else if (key=="--ZERO-MEAN") {
123144
if (val=="NO" || val=="0") opt.zero_mean=0;
124145
else opt.zero_mean=1;
125-
} else std::cout << "warning: unknown option '" << param << "'\n";
146+
} else std::cerr << "warning: unknown option '" << param << "'\n";
126147
} else {
127148
if (first) {sinputfile=param;first=false;}
128149
else soutputfile=param;

src/cmdline.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
const char SACHelp[] =
99
"usage: sac [--options] input output\n\n"
10-
" --encode encode input.wav to output.sac (default)\n"
11-
" --normal normal compression (default)\n"
10+
" --encode encode input.wav to output.sac (def)\n"
11+
" --normal normal compression (def)\n"
1212
" --high high compression, slow\n"
1313
" --veryhigh very high compression, really slow\n"
1414
" --best you asked for it\n"
@@ -19,9 +19,10 @@ const char SACHelp[] =
1919
" advanced options (automatically set per profile)\n"
2020
" --reset-opt reset opt params at frame boundaries\n"
2121
" --optimize=# frame-based optimization\n"
22-
" no|s,n s=[0,1.0],n=[0,10000]\n"
22+
" no|s,n(,c) s=[0,1.0],n=[0,10000]\n"
23+
" c=[l1,rms,glb,ent,bpn]\n"
2324
" --zero-mean zero-mean input\n"
24-
" --framelen=n default=8 (seconds)\n"
25+
" --framelen=n def=8 (seconds)\n"
2526
" --sparse-pcm enable pcm modelling\n";
2627

2728
class CmdLine {

src/common/utils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class RunExp {
1515
public:
1616
RunExp(double alpha):sum(0.0),alpha(alpha){};
1717
RunExp(double alpha,double sum):sum(sum),alpha(alpha){};
18-
inline double Get(){return sum;};
1918
inline void Update(double val) {
2019
sum=alpha*sum+(1.-alpha)*val;
2120
}
@@ -41,6 +40,12 @@ namespace StrUtils {
4140
{
4241
std::transform(str.begin(), str.end(),str.begin(), ::toupper);
4342
}
43+
inline std::string str_up(const std::string &str)
44+
{
45+
std::string ts=str;
46+
for (auto &c:ts) c=toupper(c);
47+
return ts;
48+
}
4449
inline void SplitToken(const std::string& str,std::vector<std::string>& tokens,const std::string& delimiters)
4550
{
4651
auto lastPos = str.find_first_not_of(delimiters, 0); // Skip delimiters at beginning.

src/libsac/cost.h

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,61 @@ class CostFunction {
1212
virtual ~CostFunction(){};
1313
};
1414

15-
class CostMeanRMS : public CostFunction {
15+
class CostL1 : public CostFunction {
1616
public:
1717
double Calc(const int32_t *buf,int numsamples)
1818
{
1919
if (numsamples) {
20-
double pow=0.0;
21-
for (int i=0;i<numsamples;i++) pow+=std::fabs(buf[i]);
22-
return pow/static_cast<double>(numsamples);
20+
int64_t sum=0;
21+
for (int i=0;i<numsamples;i++) sum+=std::fabs(buf[i]);
22+
return sum/static_cast<double>(numsamples);
2323
} else return 0.;
2424
}
2525
};
2626

27-
// estimate number of needed bits with a simple golomb model
27+
class CostRMS : public CostFunction {
28+
public:
29+
double Calc(const int32_t *buf,int numsamples)
30+
{
31+
if (numsamples) {
32+
int64_t sum=0.0;
33+
for (int i=0;i<numsamples;i++) sum+=buf[i]*buf[i];
34+
return sqrt(sum/static_cast<double>(numsamples));
35+
} else return 0.;
36+
}
37+
};
38+
39+
40+
// estimate number of needed bytes with a simple golomb model
41+
// alpha paramater is critical
2842
class CostGolomb : public CostFunction {
43+
const double alpha=0.97;
44+
const double log2=log(2.0);
2945
public:
30-
CostGolomb():mean_err(0.98){};
46+
CostGolomb()
47+
:rm(alpha) {};
3148
double Calc(const int32_t *buf,int numsamples)
3249
{
33-
const double log2=log(2.0);
34-
double nbits=0;
50+
int64_t nbits=0;
3551
if (numsamples) {
3652
for (int i=0;i<numsamples;i++) {
3753
int32_t val=MathUtils::S2U(buf[i]);
38-
int m=(std::max)(static_cast<int>(mean_err.Get()),1);
54+
int m=std::max(static_cast<int>(rm.sum),1);
55+
3956
int q=val/m;
4057
//int r=val-q*m;
4158
nbits+=(q+1);
4259
if (m>1) {
43-
int b=ceil(log(m)/log2);
60+
int b=std::ceil(log(m)/log2);
4461
nbits+=b;
45-
}
46-
mean_err.Update(val);
62+
};
63+
rm.Update(val);
4764
}
48-
return nbits/(double)numsamples;
65+
return nbits/static_cast<double>(8*numsamples);
4966
} else return 0;
5067
}
5168
private:
52-
RunExp mean_err;
69+
RunWeight rm;
5370
};
5471

5572
// entropy using order-0 markov model
@@ -135,4 +152,4 @@ class CostBitplane : public CostFunction {
135152
}
136153
};
137154

138-
#endif // COST_H
155+
#endif

src/libsac/libsac.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ void FrameCoder::Optimize(SacProfile &profile,const std::vector<int>&params_to_o
352352

353353
CostFunction *CostFunc=nullptr;
354354
switch (opt.optimize_cost) {
355-
case opt.SearchCost::L1:CostFunc=new CostMeanRMS();break;
355+
case opt.SearchCost::L1:CostFunc=new CostL1();break;
356+
case opt.SearchCost::RMS:CostFunc=new CostRMS();break;
356357
case opt.SearchCost::Golomb:CostFunc=new CostGolomb();break;
357358
case opt.SearchCost::Entropy:CostFunc=new CostEntropyO0b();break;
358359
case opt.SearchCost::Bitplane:CostFunc=new CostBitplane();break;

src/libsac/libsac.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class FrameCoder {
1010
public:
1111
struct coder_ctx {
12-
enum SearchCost {L1,Entropy,Golomb,Bitplane};
12+
enum SearchCost {L1,RMS,Entropy,Golomb,Bitplane};
1313
enum SearchMethod {CMA,DDS};
1414
int optimize=0;
1515
int sparse_pcm=0;

src/libsac/pred.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Predictor {
3232
tparam p;
3333
int nA,nB,nM0,nS0,nS1;
3434

35-
OLS<double>ols[2];
35+
OLS ols[2];
3636
LMSCascade lms[2];
3737
BiasEstimator be[2];
3838
double p_lpc[2],p_lms[2];

src/libsac/vle.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Golomb {
9494
if (val<0) val=2*(-val);
9595
else if (val>0) val=(2*val)-1;
9696

97-
int m=(std::max)(static_cast<int>(msum.Get()),1);
97+
int m=(std::max)(static_cast<int>(msum.sum),1);
9898
int q=val/m;
9999
int r=val-q*m;
100100

@@ -142,7 +142,7 @@ class Golomb {
142142
int q=0;
143143
while (rc.DecodeBitOne(PSCALEh)!=0) q++;
144144

145-
int m=(std::max)(static_cast<int>(msum.Get()),1);
145+
int m=(std::max)(static_cast<int>(msum.sum),1);
146146
int r=0;
147147

148148
if (m>1)
@@ -180,7 +180,7 @@ class GolombRC {
180180
if (val<0) val=2*(-val);
181181
else if (val>0) val=(2*val)-1;
182182

183-
int m=(std::max)(static_cast<int>(msum.Get()),1);
183+
int m=(std::max)(static_cast<int>(msum.sum),1);
184184
int q=val/m;
185185
int r=val-q*m;
186186

@@ -195,7 +195,7 @@ class GolombRC {
195195
int q=0;
196196
while (rc.DecodeBitOne(PSCALEh)!=0) q++;
197197

198-
int m=(std::max)(static_cast<int>(msum.Get()),1);
198+
int m=(std::max)(static_cast<int>(msum.sum),1);
199199

200200
int r=rc.DecProb(m);
201201
rc.DecodeSymbol(r,1);

src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cmdline.h"
22
#include <variant>
33

4-
#define SAC_VERSION "0.7.8"
4+
#define SAC_VERSION "0.7.8d"
55

66
int main(int argc,char *argv[])
77
{
@@ -18,7 +18,6 @@ int main(int argc,char *argv[])
1818
std::cout << " gcc " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << "\n";
1919
#endif
2020
std::cout << "\n";
21-
2221
CmdLine cmdline;
2322
int error=cmdline.Parse(argc,argv);
2423
if (error==0) error=cmdline.Process();

src/pred/lpc.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,25 @@
33

44
#include "../common/utils.h"
55

6-
template <class T>
76
class OLS {
8-
typedef std::vector<T> vec1D;
9-
typedef std::vector<vec1D> vec2D;
10-
const T ftol=1E-8;
7+
const double ftol=1E-8;
118
public:
12-
OLS(int n,int kmax=1,T lambda=0.998,T nu=0.001,T beta_sum=0.6,T beta_pow=0.75,T beta_add=2)
9+
OLS(int n,int kmax=1,double lambda=0.998,double nu=0.001,double beta_sum=0.6,double beta_pow=0.75,double beta_add=2)
1310
:x(n),n(n),kmax(kmax),lambda(lambda),nu(nu),
1411
w(n),b(n),mcov(n,vec1D(n)),mchol(n,vec1D(n)),
1512
beta_pow(beta_pow),beta_add(beta_add),esum(beta_sum)
1613
{
1714
km=0;
1815
pred=0.0;
1916
}
20-
/*T Predict(const vec1D &p) {
21-
x=p;
22-
pred=0.;
23-
for (int i=0;i<n;i++) pred+=w[i]*x[i];
24-
return pred;
25-
}*/
26-
T Predict() {
17+
double Predict() {
2718
pred=0.;
2819
for (int i=0;i<n;i++) pred+=w[i]*x[i];
2920
return pred;
3021
}
31-
void Update(T val)
22+
void Update(double val)
3223
{
33-
T err=val-pred;
24+
double err=val-pred;
3425
esum.Update(fabs(err));
3526
double c0=pow(esum.sum+beta_add,-beta_pow);
3627

@@ -46,17 +37,15 @@ class OLS {
4637
}
4738
}
4839
vec1D x;
49-
//private:
5040
int Factor(const vec2D &mcov)
5141
{
52-
//std::cout << t << ' ' << nu << '\n';
53-
mchol=mcov; // copy the matrix
42+
mchol=mcov; // copy cov and add regularization
5443
for (int i=0;i<n;i++) mchol[i][i]+=nu;
5544

5645
#if 1
5746
for (int i=0;i<n;i++) {
5847
for (int j=0;j<=i;j++) {
59-
T sum=mchol[i][j];
48+
double sum=mchol[i][j];
6049
for (int k=0;k<j;k++) sum-=(mchol[i][k]*mchol[j][k]);
6150
if (i==j) {
6251
if (sum>ftol) mchol[i][i]=sqrt(sum);
@@ -83,21 +72,21 @@ class OLS {
8372
void Solve(const vec1D &b,vec1D &x)
8473
{
8574
for (int i=0;i<n;i++) {
86-
T sum=b[i];
75+
double sum=b[i];
8776
for (int j=0;j<i;j++) sum-=(mchol[i][j]*x[j]);
8877
x[i]=sum/mchol[i][i];
8978
}
9079
for (int i=n-1;i>=0;i--) {
91-
T sum=x[i];
80+
double sum=x[i];
9281
for (int j=i+1;j<n;j++) sum-=(mchol[j][i]*x[j]);
9382
x[i]=sum/mchol[i][i];
9483
}
9584
}
9685
int n,kmax,km;
97-
T lambda,nu,pred;
86+
double lambda,nu,pred;
9887
vec1D w,b;
9988
vec2D mcov,mchol;
100-
T beta_pow,beta_add;
89+
double beta_pow,beta_add;
10190
RunWeight esum;
10291
};
10392

0 commit comments

Comments
 (0)