latticehint.c 14 KB

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