latticehint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: utility main for building thresh/pigeonhole encode hints
  14. last mod: $Id: latticehint.c,v 1.2.2.1 2000/08/31 09:00:02 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include "vorbis/codebook.h"
  22. #include "../lib/sharedbook.h"
  23. #include "../lib/scales.h"
  24. #include "bookutil.h"
  25. #include "vqgen.h"
  26. #include "vqsplit.h"
  27. /* The purpose of this util is to build encode hints for lattice
  28. codebooks so that brute forcing each codebook entry isn't needed.
  29. Threshhold hints are for books in which each scalar in the vector
  30. is independant (eg, residue) and pigeonhole lookups provide a
  31. minimum error fit for words where the scalars are interdependant
  32. (each affecting the fit of the next in sequence) as in an LSP
  33. sequential book (or can be used along with a sparse threshhold map,
  34. like a splitting tree that need not be trained)
  35. If the input book is non-sequential, a threshhold hint is built.
  36. If the input book is sequential, a pigeonholing hist is built.
  37. If the book is sparse, a pigeonholing hint is built, possibly in addition
  38. to the threshhold hint
  39. command line:
  40. latticehint book.vqh
  41. latticehint produces book.vqh on stdout */
  42. static int longsort(const void *a, const void *b){
  43. return(**((long **)a)-**((long **)b));
  44. }
  45. static int addtosearch(int entry,long **tempstack,long *tempcount,int add){
  46. long *ptr=tempstack[entry];
  47. long i=tempcount[entry];
  48. if(ptr){
  49. while(i--)
  50. if(*ptr++==add)return(0);
  51. tempstack[entry]=realloc(tempstack[entry],
  52. (tempcount[entry]+1)*sizeof(long));
  53. }else{
  54. tempstack[entry]=malloc(sizeof(long));
  55. }
  56. tempstack[entry][tempcount[entry]++]=add;
  57. return(1);
  58. }
  59. static void setvals(int dim,encode_aux_pigeonhole *p,
  60. long *temptrack,float *tempmin,float *tempmax,
  61. int seqp){
  62. int i;
  63. float last=0.;
  64. for(i=0;i<dim;i++){
  65. tempmin[i]=(temptrack[i])*p->del+p->min+last;
  66. tempmax[i]=tempmin[i]+p->del;
  67. if(seqp)last=tempmin[i];
  68. }
  69. }
  70. /* note that things are currently set up such that input fits that
  71. quantize outside the pigeonmap are dropped and brute-forced. So we
  72. can ignore the <0 and >=n boundary cases in min/max error */
  73. static float minerror(int dim,float *a,encode_aux_pigeonhole *p,
  74. long *temptrack,float *tempmin,float *tempmax){
  75. int i;
  76. float err=0.;
  77. for(i=0;i<dim;i++){
  78. float eval=0.;
  79. if(a[i]<tempmin[i]){
  80. eval=tempmin[i]-a[i];
  81. }else if(a[i]>tempmax[i]){
  82. eval=a[i]-tempmax[i];
  83. }
  84. err+=eval*eval;
  85. }
  86. return(err);
  87. }
  88. static float maxerror(int dim,float *a,encode_aux_pigeonhole *p,
  89. long *temptrack,float *tempmin,float *tempmax){
  90. int i;
  91. float err=0.,eval;
  92. for(i=0;i<dim;i++){
  93. if(a[i]<tempmin[i]){
  94. eval=tempmax[i]-a[i];
  95. }else if(a[i]>tempmax[i]){
  96. eval=a[i]-tempmin[i];
  97. }else{
  98. float t1=a[i]-tempmin[i];
  99. eval=tempmax[i]-a[i];
  100. if(t1>eval)eval=t1;
  101. }
  102. err+=eval*eval;
  103. }
  104. return(err);
  105. }
  106. int main(int argc,char *argv[]){
  107. codebook *b;
  108. static_codebook *c;
  109. int entries=-1,dim=-1;
  110. float min,del;
  111. char *name;
  112. long i,j;
  113. long dB=0;
  114. if(argv[1]==NULL){
  115. fprintf(stderr,"Need a lattice book on the command line.\n");
  116. exit(1);
  117. }
  118. if(argv[2])dB=1;
  119. {
  120. char *ptr;
  121. char *filename=strdup(argv[1]);
  122. b=codebook_load(filename);
  123. c=(static_codebook *)(b->c);
  124. ptr=strrchr(filename,'.');
  125. if(ptr){
  126. *ptr='\0';
  127. name=strdup(filename);
  128. }else{
  129. name=strdup(filename);
  130. }
  131. }
  132. if(c->maptype!=1){
  133. fprintf(stderr,"Provided book is not a latticebook.\n");
  134. exit(1);
  135. }
  136. entries=b->entries;
  137. dim=b->dim;
  138. min=_float32_unpack(c->q_min);
  139. del=_float32_unpack(c->q_delta);
  140. /* Do we want to gen a threshold hint? */
  141. if(c->q_sequencep==0){
  142. /* yes. Discard any preexisting threshhold hint */
  143. long quantvals=_book_maptype1_quantvals(c);
  144. long **quantsort=alloca(quantvals*sizeof(long *));
  145. encode_aux_threshmatch *t=calloc(1,sizeof(encode_aux_threshmatch));
  146. c->thresh_tree=t;
  147. fprintf(stderr,"Adding threshold hint to %s...\n",name);
  148. /* simplest possible threshold hint only */
  149. t->quantthresh=calloc(quantvals-1,sizeof(float));
  150. t->quantmap=calloc(quantvals,sizeof(int));
  151. t->threshvals=quantvals;
  152. t->quantvals=quantvals;
  153. /* the quantvals may not be in order; sort em first */
  154. for(i=0;i<quantvals;i++)quantsort[i]=c->quantlist+i;
  155. qsort(quantsort,quantvals,sizeof(long *),longsort);
  156. /* ok, gen the map and thresholds */
  157. for(i=0;i<quantvals;i++)t->quantmap[i]=quantsort[i]-c->quantlist;
  158. for(i=0;i<quantvals-1;i++){
  159. float v1=*(quantsort[i])*del+min;
  160. float v2=*(quantsort[i+1])*del+min;
  161. if(dB){
  162. if(fabs(v1)<.01)v1=(v1+v2)*.5;
  163. if(fabs(v2)<.01)v2=(v1+v2)*.5;
  164. t->quantthresh[i]=fromdB((todB(v1)+todB(v2))*.5);
  165. if(v1<0 || v2<0)t->quantthresh[i]*=-1;
  166. }else{
  167. t->quantthresh[i]=(v1+v2)*.5;
  168. }
  169. }
  170. }
  171. /* Do we want to gen a pigeonhole hint? */
  172. for(i=0;i<entries;i++)if(c->lengthlist[i]==0)break;
  173. if(c->q_sequencep || i<entries){
  174. long **tempstack;
  175. long *tempcount;
  176. long *temptrack;
  177. float *tempmin;
  178. float *tempmax;
  179. long totalstack=0;
  180. long pigeons;
  181. long subpigeons;
  182. long quantvals=_book_maptype1_quantvals(c);
  183. int changep=1,factor;
  184. encode_aux_pigeonhole *p=calloc(1,sizeof(encode_aux_pigeonhole));
  185. c->pigeon_tree=p;
  186. fprintf(stderr,"Adding pigeonhole hint to %s...\n",name);
  187. /* the idea is that we quantize uniformly, even in a nonuniform
  188. lattice, so that quantization of one scalar has a predictable
  189. result on the next sequential scalar in a greedy matching
  190. algorithm. We generate a lookup based on the quantization of
  191. the vector (pigeonmap groups quantized entries together) and
  192. list the entries that could possible be the best fit for any
  193. given member of that pigeonhole. The encode process then has a
  194. much smaller list to brute force */
  195. /* find our pigeonhole-specific quantization values, fill in the
  196. quant value->pigeonhole map */
  197. factor=3;
  198. p->del=del;
  199. p->min=min;
  200. p->quantvals=quantvals;
  201. {
  202. int max=0;
  203. for(i=0;i<quantvals;i++)if(max<c->quantlist[i])max=c->quantlist[i];
  204. p->mapentries=max;
  205. }
  206. p->pigeonmap=malloc(p->mapentries*sizeof(long));
  207. p->quantvals=(quantvals+factor-1)/factor;
  208. /* pigeonhole roughly on the boundaries of the quantvals; the
  209. exact pigeonhole grouping is an optimization issue, not a
  210. correctness issue */
  211. for(i=0;i<p->mapentries;i++){
  212. float thisval=del*i+min; /* middle of the quant zone */
  213. int quant=0;
  214. float err=fabs(c->quantlist[0]*del+min-thisval);
  215. for(j=1;j<quantvals;j++){
  216. float thiserr=fabs(c->quantlist[j]*del+min-thisval);
  217. if(thiserr<err){
  218. quant=j/factor;
  219. err=thiserr;
  220. }
  221. }
  222. p->pigeonmap[i]=quant;
  223. }
  224. /* pigeonmap complete. Now do the grungy business of finding the
  225. entries that could possibly be the best fit for a value appearing
  226. in the pigeonhole. The trick that allows the below to work is the
  227. uniform quantization; even though the scalars may be 'sequential'
  228. (each a delta from the last), the uniform quantization means that
  229. the error variance is *not* dependant. Given a pigeonhole and an
  230. entry, we can find the minimum and maximum possible errors
  231. (relative to the entry) for any point that could appear in the
  232. pigeonhole */
  233. /* must iterate over both pigeonholes and entries */
  234. /* temporarily (in order to avoid thinking hard), we grow each
  235. pigeonhole seperately, the build a stack of 'em later */
  236. pigeons=1;
  237. subpigeons=1;
  238. for(i=0;i<dim;i++)subpigeons*=p->mapentries;
  239. for(i=0;i<dim;i++)pigeons*=p->quantvals;
  240. temptrack=calloc(dim,sizeof(long));
  241. tempmin=calloc(dim,sizeof(float));
  242. tempmax=calloc(dim,sizeof(float));
  243. tempstack=calloc(pigeons,sizeof(long *));
  244. tempcount=calloc(pigeons,sizeof(long));
  245. while(1){
  246. float errorpost=-1;
  247. char buffer[80];
  248. /* map our current pigeonhole to a 'big pigeonhole' so we know
  249. what list we're after */
  250. int entry=0;
  251. for(i=dim-1;i>=0;i--)entry=entry*p->quantvals+p->pigeonmap[temptrack[i]];
  252. setvals(dim,p,temptrack,tempmin,tempmax,c->q_sequencep);
  253. sprintf(buffer,"Building pigeonhole search list [%ld]...",totalstack);
  254. /* Search all entries to find the one with the minimum possible
  255. maximum error. Record that error */
  256. for(i=0;i<entries;i++){
  257. if(c->lengthlist[i]>0){
  258. float this=maxerror(dim,b->valuelist+i*dim,p,
  259. temptrack,tempmin,tempmax);
  260. if(errorpost==-1 || this<errorpost)errorpost=this;
  261. spinnit(buffer,subpigeons);
  262. }
  263. }
  264. /* Our search list will contain all entries with a minimum
  265. possible error <= our errorpost */
  266. for(i=0;i<entries;i++)
  267. if(c->lengthlist[i]>0){
  268. spinnit(buffer,subpigeons);
  269. if(minerror(dim,b->valuelist+i*dim,p,
  270. temptrack,tempmin,tempmax)<errorpost)
  271. totalstack+=addtosearch(entry,tempstack,tempcount,i);
  272. }
  273. for(i=0;i<dim;i++){
  274. temptrack[i]++;
  275. if(temptrack[i]<p->mapentries)break;
  276. temptrack[i]=0;
  277. }
  278. if(i==dim)break;
  279. subpigeons--;
  280. }
  281. fprintf(stderr,"\r "
  282. "\rTotal search list size (all entries): %ld\n",totalstack);
  283. /* pare the index of lists for improbable quantizations (where
  284. improbable is determined by c->lengthlist; we assume that
  285. pigeonholing is in sync with the codeword cells, which it is */
  286. /*for(i=0;i<entries;i++){
  287. float probability= 1./(1<<c->lengthlist[i]);
  288. if(c->lengthlist[i]==0 || probability*entries<cutoff){
  289. totalstack-=tempcount[i];
  290. tempcount[i]=0;
  291. }
  292. }*/
  293. /* pare the list of shortlists; merge contained and similar lists
  294. together */
  295. p->fitmap=malloc(pigeons*sizeof(long));
  296. for(i=0;i<pigeons;i++)p->fitmap[i]=-1;
  297. while(changep){
  298. char buffer[80];
  299. changep=0;
  300. for(i=0;i<pigeons;i++){
  301. if(p->fitmap[i]<0 && tempcount[i]){
  302. for(j=i+1;j<pigeons;j++){
  303. if(p->fitmap[j]<0 && tempcount[j]){
  304. /* is one list a superset, or are they sufficiently similar? */
  305. int amiss=0,bmiss=0,ii,jj;
  306. for(ii=0;ii<tempcount[i];ii++){
  307. for(jj=0;jj<tempcount[j];jj++)
  308. if(tempstack[i][ii]==tempstack[j][jj])break;
  309. if(jj==tempcount[j])amiss++;
  310. }
  311. for(jj=0;jj<tempcount[j];jj++){
  312. for(ii=0;ii<tempcount[i];ii++)
  313. if(tempstack[i][ii]==tempstack[j][jj])break;
  314. if(ii==tempcount[i])bmiss++;
  315. }
  316. if(amiss==0 ||
  317. bmiss==0 ||
  318. (amiss*2<tempcount[i] && bmiss*2<tempcount[j] &&
  319. tempcount[i]+bmiss<entries/30)){
  320. /*superset/similar Add all of one to the other. */
  321. for(jj=0;jj<tempcount[j];jj++)
  322. totalstack+=addtosearch(i,tempstack,tempcount,
  323. tempstack[j][jj]);
  324. totalstack-=tempcount[j];
  325. p->fitmap[j]=i;
  326. changep=1;
  327. }
  328. }
  329. }
  330. sprintf(buffer,"Consolidating [%ld total, %s]... ",totalstack,
  331. changep?"reit":"nochange");
  332. spinnit(buffer,pigeons-i);
  333. }
  334. }
  335. }
  336. /* repack the temp stack in final form */
  337. fprintf(stderr,"\r "
  338. "\rFinal total list size: %ld\n",totalstack);
  339. p->fittotal=totalstack;
  340. p->fitlist=malloc((totalstack+1)*sizeof(long));
  341. p->fitlength=malloc(pigeons*sizeof(long));
  342. {
  343. long usage=0;
  344. for(i=0;i<pigeons;i++){
  345. if(p->fitmap[i]==-1){
  346. if(tempcount[i])
  347. memcpy(p->fitlist+usage,tempstack[i],tempcount[i]*sizeof(long));
  348. p->fitmap[i]=usage;
  349. p->fitlength[i]=tempcount[i];
  350. usage+=tempcount[i];
  351. if(usage>totalstack){
  352. fprintf(stderr,"Internal error; usage>totalstack\n");
  353. exit(1);
  354. }
  355. }else{
  356. p->fitlength[i]=p->fitlength[p->fitmap[i]];
  357. p->fitmap[i]=p->fitmap[p->fitmap[i]];
  358. }
  359. }
  360. }
  361. }
  362. write_codebook(stdout,name,c);
  363. fprintf(stderr,"\r "
  364. "\nDone.\n");
  365. exit(0);
  366. }