vqgen.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: train a VQ codebook
  13. last mod: $Id$
  14. ********************************************************************/
  15. /* This code is *not* part of libvorbis. It is used to generate
  16. trained codebooks offline and then spit the results into a
  17. pregenerated codebook that is compiled into libvorbis. It is an
  18. expensive (but good) algorithm. Run it on big iron. */
  19. /* There are so many optimizations to explore in *both* stages that
  20. considering the undertaking is almost withering. For now, we brute
  21. force it all */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include "vqgen.h"
  27. #include "bookutil.h"
  28. /* Codebook generation happens in two steps:
  29. 1) Train the codebook with data collected from the encoder: We use
  30. one of a few error metrics (which represent the distance between a
  31. given data point and a candidate point in the training set) to
  32. divide the training set up into cells representing roughly equal
  33. probability of occurring.
  34. 2) Generate the codebook and auxiliary data from the trained data set
  35. */
  36. /* Codebook training ****************************************************
  37. *
  38. * The basic idea here is that a VQ codebook is like an m-dimensional
  39. * foam with n bubbles. The bubbles compete for space/volume and are
  40. * 'pressurized' [biased] according to some metric. The basic alg
  41. * iterates through allowing the bubbles to compete for space until
  42. * they converge (if the damping is dome properly) on a steady-state
  43. * solution. Individual input points, collected from libvorbis, are
  44. * used to train the algorithm monte-carlo style. */
  45. /* internal helpers *****************************************************/
  46. #define vN(data,i) (data+v->elements*i)
  47. /* default metric; squared 'distance' from desired value. */
  48. float _dist(vqgen *v,float *a, float *b){
  49. int i;
  50. int el=v->elements;
  51. float acc=0.f;
  52. for(i=0;i<el;i++){
  53. float val=(a[i]-b[i]);
  54. acc+=val*val;
  55. }
  56. return sqrt(acc);
  57. }
  58. float *_weight_null(vqgen *v,float *a){
  59. return a;
  60. }
  61. /* *must* be beefed up. */
  62. void _vqgen_seed(vqgen *v){
  63. long i;
  64. for(i=0;i<v->entries;i++)
  65. memcpy(_now(v,i),_point(v,i),sizeof(float)*v->elements);
  66. v->seeded=1;
  67. }
  68. int directdsort(const void *a, const void *b){
  69. float av=*((float *)a);
  70. float bv=*((float *)b);
  71. return (av<bv)-(av>bv);
  72. }
  73. void vqgen_cellmetric(vqgen *v){
  74. int j,k;
  75. float min=-1.f,max=-1.f,mean=0.f,acc=0.f;
  76. long dup=0,unused=0;
  77. #ifdef NOISY
  78. int i;
  79. char buff[80];
  80. float spacings[v->entries];
  81. int count=0;
  82. FILE *cells;
  83. sprintf(buff,"cellspace%d.m",v->it);
  84. cells=fopen(buff,"w");
  85. #endif
  86. /* minimum, maximum, cell spacing */
  87. for(j=0;j<v->entries;j++){
  88. float localmin=-1.;
  89. for(k=0;k<v->entries;k++){
  90. if(j!=k){
  91. float this=_dist(v,_now(v,j),_now(v,k));
  92. if(this>0){
  93. if(v->assigned[k] && (localmin==-1 || this<localmin))
  94. localmin=this;
  95. }else{
  96. if(k<j){
  97. dup++;
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. if(k<v->entries)continue;
  104. if(v->assigned[j]==0){
  105. unused++;
  106. continue;
  107. }
  108. localmin=v->max[j]+localmin/2; /* this gives us rough diameter */
  109. if(min==-1 || localmin<min)min=localmin;
  110. if(max==-1 || localmin>max)max=localmin;
  111. mean+=localmin;
  112. acc++;
  113. #ifdef NOISY
  114. spacings[count++]=localmin;
  115. #endif
  116. }
  117. fprintf(stderr,"cell diameter: %.03g::%.03g::%.03g (%ld unused/%ld dup)\n",
  118. min,mean/acc,max,unused,dup);
  119. #ifdef NOISY
  120. qsort(spacings,count,sizeof(float),directdsort);
  121. for(i=0;i<count;i++)
  122. fprintf(cells,"%g\n",spacings[i]);
  123. fclose(cells);
  124. #endif
  125. }
  126. /* External calls *******************************************************/
  127. /* We have two forms of quantization; in the first, each vector
  128. element in the codebook entry is orthogonal. Residues would use this
  129. quantization for example.
  130. In the second, we have a sequence of monotonically increasing
  131. values that we wish to quantize as deltas (to save space). We
  132. still need to quantize so that absolute values are accurate. For
  133. example, LSP quantizes all absolute values, but the book encodes
  134. distance between values because each successive value is larger
  135. than the preceeding value. Thus the desired quantibits apply to
  136. the encoded (delta) values, not abs positions. This requires minor
  137. additional encode-side trickery. */
  138. void vqgen_quantize(vqgen *v,quant_meta *q){
  139. float maxdel;
  140. float mindel;
  141. float delta;
  142. float maxquant=((1<<q->quant)-1);
  143. int j,k;
  144. mindel=maxdel=_now(v,0)[0];
  145. for(j=0;j<v->entries;j++){
  146. float last=0.f;
  147. for(k=0;k<v->elements;k++){
  148. if(mindel>_now(v,j)[k]-last)mindel=_now(v,j)[k]-last;
  149. if(maxdel<_now(v,j)[k]-last)maxdel=_now(v,j)[k]-last;
  150. if(q->sequencep)last=_now(v,j)[k];
  151. }
  152. }
  153. /* first find the basic delta amount from the maximum span to be
  154. encoded. Loosen the delta slightly to allow for additional error
  155. during sequence quantization */
  156. delta=(maxdel-mindel)/((1<<q->quant)-1.5f);
  157. q->min=_float32_pack(mindel);
  158. q->delta=_float32_pack(delta);
  159. mindel=_float32_unpack(q->min);
  160. delta=_float32_unpack(q->delta);
  161. for(j=0;j<v->entries;j++){
  162. float last=0;
  163. for(k=0;k<v->elements;k++){
  164. float val=_now(v,j)[k];
  165. float now=rint((val-last-mindel)/delta);
  166. _now(v,j)[k]=now;
  167. if(now<0){
  168. /* be paranoid; this should be impossible */
  169. fprintf(stderr,"fault; quantized value<0\n");
  170. exit(1);
  171. }
  172. if(now>maxquant){
  173. /* be paranoid; this should be impossible */
  174. fprintf(stderr,"fault; quantized value>max\n");
  175. exit(1);
  176. }
  177. if(q->sequencep)last=(now*delta)+mindel+last;
  178. }
  179. }
  180. }
  181. /* much easier :-). Unlike in the codebook, we don't un-log log
  182. scales; we just make sure they're properly offset. */
  183. void vqgen_unquantize(vqgen *v,quant_meta *q){
  184. long j,k;
  185. float mindel=_float32_unpack(q->min);
  186. float delta=_float32_unpack(q->delta);
  187. for(j=0;j<v->entries;j++){
  188. float last=0.f;
  189. for(k=0;k<v->elements;k++){
  190. float now=_now(v,j)[k];
  191. now=fabs(now)*delta+last+mindel;
  192. if(q->sequencep)last=now;
  193. _now(v,j)[k]=now;
  194. }
  195. }
  196. }
  197. void vqgen_init(vqgen *v,int elements,int aux,int entries,float mindist,
  198. float (*metric)(vqgen *,float *, float *),
  199. float *(*weight)(vqgen *,float *),int centroid){
  200. memset(v,0,sizeof(vqgen));
  201. v->centroid=centroid;
  202. v->elements=elements;
  203. v->aux=aux;
  204. v->mindist=mindist;
  205. v->allocated=32768;
  206. v->pointlist=_ogg_malloc(v->allocated*(v->elements+v->aux)*sizeof(float));
  207. v->entries=entries;
  208. v->entrylist=_ogg_malloc(v->entries*v->elements*sizeof(float));
  209. v->assigned=_ogg_malloc(v->entries*sizeof(long));
  210. v->bias=_ogg_calloc(v->entries,sizeof(float));
  211. v->max=_ogg_calloc(v->entries,sizeof(float));
  212. if(metric)
  213. v->metric_func=metric;
  214. else
  215. v->metric_func=_dist;
  216. if(weight)
  217. v->weight_func=weight;
  218. else
  219. v->weight_func=_weight_null;
  220. v->asciipoints=tmpfile();
  221. }
  222. void vqgen_addpoint(vqgen *v, float *p,float *a){
  223. int k;
  224. for(k=0;k<v->elements;k++)
  225. fprintf(v->asciipoints,"%.12g\n",p[k]);
  226. for(k=0;k<v->aux;k++)
  227. fprintf(v->asciipoints,"%.12g\n",a[k]);
  228. if(v->points>=v->allocated){
  229. v->allocated*=2;
  230. v->pointlist=_ogg_realloc(v->pointlist,v->allocated*(v->elements+v->aux)*
  231. sizeof(float));
  232. }
  233. memcpy(_point(v,v->points),p,sizeof(float)*v->elements);
  234. if(v->aux)memcpy(_point(v,v->points)+v->elements,a,sizeof(float)*v->aux);
  235. /* quantize to the density mesh if it's selected */
  236. if(v->mindist>0.f){
  237. /* quantize to the mesh */
  238. for(k=0;k<v->elements+v->aux;k++)
  239. _point(v,v->points)[k]=
  240. rint(_point(v,v->points)[k]/v->mindist)*v->mindist;
  241. }
  242. v->points++;
  243. if(!(v->points&0xff))spinnit("loading... ",v->points);
  244. }
  245. /* yes, not threadsafe. These utils aren't */
  246. static int sortit=0;
  247. static int sortsize=0;
  248. static int meshcomp(const void *a,const void *b){
  249. if(((sortit++)&0xfff)==0)spinnit("sorting mesh...",sortit);
  250. return(memcmp(a,b,sortsize));
  251. }
  252. void vqgen_sortmesh(vqgen *v){
  253. sortit=0;
  254. if(v->mindist>0.f){
  255. long i,march=1;
  256. /* sort to make uniqueness detection trivial */
  257. sortsize=(v->elements+v->aux)*sizeof(float);
  258. qsort(v->pointlist,v->points,sortsize,meshcomp);
  259. /* now march through and eliminate dupes */
  260. for(i=1;i<v->points;i++){
  261. if(memcmp(_point(v,i),_point(v,i-1),sortsize)){
  262. /* a new, unique entry. march it down */
  263. if(i>march)memcpy(_point(v,march),_point(v,i),sortsize);
  264. march++;
  265. }
  266. spinnit("eliminating density... ",v->points-i);
  267. }
  268. /* we're done */
  269. fprintf(stderr,"\r%ld training points remining out of %ld"
  270. " after density mesh (%ld%%)\n",march,v->points,march*100/v->points);
  271. v->points=march;
  272. }
  273. v->sorted=1;
  274. }
  275. float vqgen_iterate(vqgen *v,int biasp){
  276. long i,j,k;
  277. float fdesired;
  278. long desired;
  279. long desired2;
  280. float asserror=0.f;
  281. float meterror=0.f;
  282. float *new;
  283. float *new2;
  284. long *nearcount;
  285. float *nearbias;
  286. #ifdef NOISY
  287. char buff[80];
  288. FILE *assig;
  289. FILE *bias;
  290. FILE *cells;
  291. sprintf(buff,"cells%d.m",v->it);
  292. cells=fopen(buff,"w");
  293. sprintf(buff,"assig%d.m",v->it);
  294. assig=fopen(buff,"w");
  295. sprintf(buff,"bias%d.m",v->it);
  296. bias=fopen(buff,"w");
  297. #endif
  298. if(v->entries<2){
  299. fprintf(stderr,"generation requires at least two entries\n");
  300. exit(1);
  301. }
  302. if(!v->sorted)vqgen_sortmesh(v);
  303. if(!v->seeded)_vqgen_seed(v);
  304. fdesired=(float)v->points/v->entries;
  305. desired=fdesired;
  306. desired2=desired*2;
  307. new=_ogg_malloc(sizeof(float)*v->entries*v->elements);
  308. new2=_ogg_malloc(sizeof(float)*v->entries*v->elements);
  309. nearcount=_ogg_malloc(v->entries*sizeof(long));
  310. nearbias=_ogg_malloc(v->entries*desired2*sizeof(float));
  311. /* fill in nearest points for entry biasing */
  312. /*memset(v->bias,0,sizeof(float)*v->entries);*/
  313. memset(nearcount,0,sizeof(long)*v->entries);
  314. memset(v->assigned,0,sizeof(long)*v->entries);
  315. if(biasp){
  316. for(i=0;i<v->points;i++){
  317. float *ppt=v->weight_func(v,_point(v,i));
  318. float firstmetric=v->metric_func(v,_now(v,0),ppt)+v->bias[0];
  319. float secondmetric=v->metric_func(v,_now(v,1),ppt)+v->bias[1];
  320. long firstentry=0;
  321. long secondentry=1;
  322. if(!(i&0xff))spinnit("biasing... ",v->points+v->points+v->entries-i);
  323. if(firstmetric>secondmetric){
  324. float temp=firstmetric;
  325. firstmetric=secondmetric;
  326. secondmetric=temp;
  327. firstentry=1;
  328. secondentry=0;
  329. }
  330. for(j=2;j<v->entries;j++){
  331. float thismetric=v->metric_func(v,_now(v,j),ppt)+v->bias[j];
  332. if(thismetric<secondmetric){
  333. if(thismetric<firstmetric){
  334. secondmetric=firstmetric;
  335. secondentry=firstentry;
  336. firstmetric=thismetric;
  337. firstentry=j;
  338. }else{
  339. secondmetric=thismetric;
  340. secondentry=j;
  341. }
  342. }
  343. }
  344. j=firstentry;
  345. for(j=0;j<v->entries;j++){
  346. float thismetric,localmetric;
  347. float *nearbiasptr=nearbias+desired2*j;
  348. long k=nearcount[j];
  349. localmetric=v->metric_func(v,_now(v,j),ppt);
  350. /* 'thismetric' is to be the bias value necessary in the current
  351. arrangement for entry j to capture point i */
  352. if(firstentry==j){
  353. /* use the secondary entry as the threshhold */
  354. thismetric=secondmetric-localmetric;
  355. }else{
  356. /* use the primary entry as the threshhold */
  357. thismetric=firstmetric-localmetric;
  358. }
  359. /* support the idea of 'minimum distance'... if we want the
  360. cells in a codebook to be roughly some minimum size (as with
  361. the low resolution residue books) */
  362. /* a cute two-stage delayed sorting hack */
  363. if(k<desired){
  364. nearbiasptr[k]=thismetric;
  365. k++;
  366. if(k==desired){
  367. spinnit("biasing... ",v->points+v->points+v->entries-i);
  368. qsort(nearbiasptr,desired,sizeof(float),directdsort);
  369. }
  370. }else if(thismetric>nearbiasptr[desired-1]){
  371. nearbiasptr[k]=thismetric;
  372. k++;
  373. if(k==desired2){
  374. spinnit("biasing... ",v->points+v->points+v->entries-i);
  375. qsort(nearbiasptr,desired2,sizeof(float),directdsort);
  376. k=desired;
  377. }
  378. }
  379. nearcount[j]=k;
  380. }
  381. }
  382. /* inflate/deflate */
  383. for(i=0;i<v->entries;i++){
  384. float *nearbiasptr=nearbias+desired2*i;
  385. spinnit("biasing... ",v->points+v->entries-i);
  386. /* due to the delayed sorting, we likely need to finish it off....*/
  387. if(nearcount[i]>desired)
  388. qsort(nearbiasptr,nearcount[i],sizeof(float),directdsort);
  389. v->bias[i]=nearbiasptr[desired-1];
  390. }
  391. }else{
  392. memset(v->bias,0,v->entries*sizeof(float));
  393. }
  394. /* Now assign with new bias and find new midpoints */
  395. for(i=0;i<v->points;i++){
  396. float *ppt=v->weight_func(v,_point(v,i));
  397. float firstmetric=v->metric_func(v,_now(v,0),ppt)+v->bias[0];
  398. long firstentry=0;
  399. if(!(i&0xff))spinnit("centering... ",v->points-i);
  400. for(j=0;j<v->entries;j++){
  401. float thismetric=v->metric_func(v,_now(v,j),ppt)+v->bias[j];
  402. if(thismetric<firstmetric){
  403. firstmetric=thismetric;
  404. firstentry=j;
  405. }
  406. }
  407. j=firstentry;
  408. #ifdef NOISY
  409. fprintf(cells,"%g %g\n%g %g\n\n",
  410. _now(v,j)[0],_now(v,j)[1],
  411. ppt[0],ppt[1]);
  412. #endif
  413. firstmetric-=v->bias[j];
  414. meterror+=firstmetric;
  415. if(v->centroid==0){
  416. /* set up midpoints for next iter */
  417. if(v->assigned[j]++){
  418. for(k=0;k<v->elements;k++)
  419. vN(new,j)[k]+=ppt[k];
  420. if(firstmetric>v->max[j])v->max[j]=firstmetric;
  421. }else{
  422. for(k=0;k<v->elements;k++)
  423. vN(new,j)[k]=ppt[k];
  424. v->max[j]=firstmetric;
  425. }
  426. }else{
  427. /* centroid */
  428. if(v->assigned[j]++){
  429. for(k=0;k<v->elements;k++){
  430. if(vN(new,j)[k]>ppt[k])vN(new,j)[k]=ppt[k];
  431. if(vN(new2,j)[k]<ppt[k])vN(new2,j)[k]=ppt[k];
  432. }
  433. if(firstmetric>v->max[firstentry])v->max[j]=firstmetric;
  434. }else{
  435. for(k=0;k<v->elements;k++){
  436. vN(new,j)[k]=ppt[k];
  437. vN(new2,j)[k]=ppt[k];
  438. }
  439. v->max[firstentry]=firstmetric;
  440. }
  441. }
  442. }
  443. /* assign midpoints */
  444. for(j=0;j<v->entries;j++){
  445. #ifdef NOISY
  446. fprintf(assig,"%ld\n",v->assigned[j]);
  447. fprintf(bias,"%g\n",v->bias[j]);
  448. #endif
  449. asserror+=fabs(v->assigned[j]-fdesired);
  450. if(v->assigned[j]){
  451. if(v->centroid==0){
  452. for(k=0;k<v->elements;k++)
  453. _now(v,j)[k]=vN(new,j)[k]/v->assigned[j];
  454. }else{
  455. for(k=0;k<v->elements;k++)
  456. _now(v,j)[k]=(vN(new,j)[k]+vN(new2,j)[k])/2.f;
  457. }
  458. }
  459. }
  460. asserror/=(v->entries*fdesired);
  461. fprintf(stderr,"Pass #%d... ",v->it);
  462. fprintf(stderr,": dist %g(%g) metric error=%g \n",
  463. asserror,fdesired,meterror/v->points);
  464. v->it++;
  465. free(new);
  466. free(nearcount);
  467. free(nearbias);
  468. #ifdef NOISY
  469. fclose(assig);
  470. fclose(bias);
  471. fclose(cells);
  472. #endif
  473. return(asserror);
  474. }