floor1.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: floor backend 1 implementation
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include "ogg.h"
  19. #include "ivorbiscodec.h"
  20. #include "codec_internal.h"
  21. #include "codebook.h"
  22. #include "misc.h"
  23. extern const ogg_int32_t FLOOR_fromdB_LOOKUP[];
  24. #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */
  25. #define VIF_POSIT 63
  26. /***********************************************/
  27. void floor1_free_info(vorbis_info_floor *i){
  28. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  29. if(info){
  30. if(info->class)_ogg_free(info->class);
  31. if(info->partitionclass)_ogg_free(info->partitionclass);
  32. if(info->postlist)_ogg_free(info->postlist);
  33. if(info->forward_index)_ogg_free(info->forward_index);
  34. if(info->hineighbor)_ogg_free(info->hineighbor);
  35. if(info->loneighbor)_ogg_free(info->loneighbor);
  36. memset(info,0,sizeof(*info));
  37. _ogg_free(info);
  38. }
  39. }
  40. static int ilog(unsigned int v){
  41. int ret=0;
  42. while(v){
  43. ret++;
  44. v>>=1;
  45. }
  46. return(ret);
  47. }
  48. static void mergesort(char *index,ogg_uint16_t *vals,ogg_uint16_t n){
  49. ogg_uint16_t i,j;
  50. char *temp,*A=index,*B=_ogg_malloc(n*sizeof(*B));
  51. for(i=1;i<n;i<<=1){
  52. for(j=0;j+i<n;){
  53. int k1=j;
  54. int mid=j+i;
  55. int k2=mid;
  56. int end=(j+i*2<n?j+i*2:n);
  57. while(k1<mid && k2<end){
  58. if(vals[A[k1]]<vals[A[k2]])
  59. B[j++]=A[k1++];
  60. else
  61. B[j++]=A[k2++];
  62. }
  63. while(k1<mid) B[j++]=A[k1++];
  64. while(k2<end) B[j++]=A[k2++];
  65. }
  66. for(;j<n;j++)B[j]=A[j];
  67. temp=A;A=B;B=temp;
  68. }
  69. if(B==index){
  70. for(j=0;j<n;j++)B[j]=A[j];
  71. _ogg_free(A);
  72. }else
  73. _ogg_free(B);
  74. }
  75. vorbis_info_floor *floor1_info_unpack (vorbis_info *vi,oggpack_buffer *opb){
  76. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  77. int j,k,count=0,maxclass=-1,rangebits;
  78. vorbis_info_floor1 *info=(vorbis_info_floor1 *)_ogg_calloc(1,sizeof(*info));
  79. /* read partitions */
  80. info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
  81. info->partitionclass=
  82. (char *)_ogg_malloc(info->partitions*sizeof(*info->partitionclass));
  83. for(j=0;j<info->partitions;j++){
  84. info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */
  85. if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  86. }
  87. /* read partition classes */
  88. info->class=
  89. (floor1class *)_ogg_malloc((maxclass+1)*sizeof(*info->class));
  90. for(j=0;j<maxclass+1;j++){
  91. info->class[j].class_dim=oggpack_read(opb,3)+1; /* 1 to 8 */
  92. info->class[j].class_subs=oggpack_read(opb,2); /* 0,1,2,3 bits */
  93. if(oggpack_eop(opb)<0) goto err_out;
  94. if(info->class[j].class_subs)
  95. info->class[j].class_book=oggpack_read(opb,8);
  96. else
  97. info->class[j].class_book=0;
  98. if(info->class[j].class_book>=ci->books)goto err_out;
  99. for(k=0;k<(1<<info->class[j].class_subs);k++){
  100. info->class[j].class_subbook[k]=oggpack_read(opb,8)-1;
  101. if(info->class[j].class_subbook[k]>=ci->books &&
  102. info->class[j].class_subbook[k]!=0xff)goto err_out;
  103. }
  104. }
  105. /* read the post list */
  106. info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */
  107. rangebits=oggpack_read(opb,4);
  108. for(j=0,k=0;j<info->partitions;j++)
  109. count+=info->class[info->partitionclass[j]].class_dim;
  110. info->postlist=
  111. (ogg_uint16_t *)_ogg_malloc((count+2)*sizeof(*info->postlist));
  112. info->forward_index=
  113. (char *)_ogg_malloc((count+2)*sizeof(*info->forward_index));
  114. info->loneighbor=
  115. (char *)_ogg_malloc(count*sizeof(*info->loneighbor));
  116. info->hineighbor=
  117. (char *)_ogg_malloc(count*sizeof(*info->hineighbor));
  118. count=0;
  119. for(j=0,k=0;j<info->partitions;j++){
  120. count+=info->class[info->partitionclass[j]].class_dim;
  121. for(;k<count;k++){
  122. int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
  123. if(t>=(1<<rangebits))goto err_out;
  124. }
  125. }
  126. if(oggpack_eop(opb))goto err_out;
  127. info->postlist[0]=0;
  128. info->postlist[1]=1<<rangebits;
  129. info->posts=count+2;
  130. /* also store a sorted position index */
  131. for(j=0;j<info->posts;j++)info->forward_index[j]=j;
  132. mergesort(info->forward_index,info->postlist,info->posts);
  133. /* discover our neighbors for decode where we don't use fit flags
  134. (that would push the neighbors outward) */
  135. for(j=0;j<info->posts-2;j++){
  136. int lo=0;
  137. int hi=1;
  138. int lx=0;
  139. int hx=info->postlist[1];
  140. int currentx=info->postlist[j+2];
  141. for(k=0;k<j+2;k++){
  142. int x=info->postlist[k];
  143. if(x>lx && x<currentx){
  144. lo=k;
  145. lx=x;
  146. }
  147. if(x<hx && x>currentx){
  148. hi=k;
  149. hx=x;
  150. }
  151. }
  152. info->loneighbor[j]=lo;
  153. info->hineighbor[j]=hi;
  154. }
  155. return(info);
  156. err_out:
  157. floor1_free_info(info);
  158. return(NULL);
  159. }
  160. #ifdef ONLY_C
  161. static
  162. #endif
  163. int render_point(int x0,int x1,int y0,int y1,int x){
  164. y0&=0x7fff; /* mask off flag */
  165. y1&=0x7fff;
  166. {
  167. int dy=y1-y0;
  168. int adx=x1-x0;
  169. int ady=abs(dy);
  170. int err=ady*(x-x0);
  171. int off=err/adx;
  172. if(dy<0)return(y0-off);
  173. return(y0+off);
  174. }
  175. }
  176. static void render_line(int n,int x0,int x1,int y0,int y1,ogg_int32_t *d){
  177. int dy;
  178. int adx;
  179. int ady;
  180. int base;
  181. int err;
  182. const ogg_int32_t *floor;
  183. if(n>x1)n=x1;
  184. n -= x0;
  185. if (n <= 0)
  186. return;
  187. dy=y1-y0;
  188. adx=x1-x0;
  189. ady=abs(dy);
  190. base=dy/adx;
  191. err=adx-1;
  192. floor=&FLOOR_fromdB_LOOKUP[y0];
  193. d += x0;
  194. ady-=abs(base*adx);
  195. /* We should add base each time, and then:
  196. * if dy >=0 we occasionally add 1
  197. * else occasionally subtract 1.
  198. * As an optimisation we say that if dy <0 we make base 1 smaller.
  199. * Then we need to add 1 occassionally, rather than subtract 1 - but we
  200. * need to add 1 in all the cases when we wouldn't have done so before.
  201. * Previously we'd have added 1 (100*ady/adx)% of the time. Now we want
  202. * to do so (100*(adx-ady)/adx)% of the time.
  203. */
  204. if (dy < 0){
  205. base--;
  206. ady = adx-ady;
  207. err = 0;
  208. }
  209. //if(x<n)
  210. // d[x]= MULT31_SHIFT15(d[x],FLOOR_fromdB_LOOKUP[y]);
  211. #if defined(ONLY_C)
  212. do{
  213. *d = MULT31_SHIFT15(*d,*floor);
  214. d++;
  215. floor+=base;
  216. err-=ady;
  217. if(err<0){
  218. err+=adx;
  219. floor+=1;
  220. }
  221. n--;
  222. } while(n>0);
  223. #else
  224. render_lineARM(n,d,floor,base,err,adx,ady);
  225. #endif
  226. }
  227. int floor1_memosize(vorbis_info_floor *i){
  228. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  229. return info->posts;
  230. }
  231. static int quant_look[4]={256,128,86,64};
  232. ogg_int32_t *floor1_inverse1(vorbis_dsp_state *vd,vorbis_info_floor *in,
  233. ogg_int32_t *fit_value){
  234. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  235. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  236. int i,j,k;
  237. codebook *books=ci->book_param;
  238. int quant_q=quant_look[info->mult-1];
  239. /* unpack wrapped/predicted values from stream */
  240. if(oggpack_read(&vd->opb,1)==1){
  241. fit_value[0]=oggpack_read(&vd->opb,ilog(quant_q-1));
  242. fit_value[1]=oggpack_read(&vd->opb,ilog(quant_q-1));
  243. /* partition by partition */
  244. /* partition by partition */
  245. for(i=0,j=2;i<info->partitions;i++){
  246. int classv=info->partitionclass[i];
  247. int cdim=info->class[classv].class_dim;
  248. int csubbits=info->class[classv].class_subs;
  249. int csub=1<<csubbits;
  250. int cval=0;
  251. /* decode the partition's first stage cascade value */
  252. if(csubbits){
  253. cval=vorbis_book_decode(books+info->class[classv].class_book,&vd->opb);
  254. if(cval==-1)goto eop;
  255. }
  256. for(k=0;k<cdim;k++){
  257. int book=info->class[classv].class_subbook[cval&(csub-1)];
  258. cval>>=csubbits;
  259. if(book!=0xff){
  260. if((fit_value[j+k]=vorbis_book_decode(books+book,&vd->opb))==-1)
  261. goto eop;
  262. }else{
  263. fit_value[j+k]=0;
  264. }
  265. }
  266. j+=cdim;
  267. }
  268. /* unwrap positive values and reconsitute via linear interpolation */
  269. for(i=2;i<info->posts;i++){
  270. int predicted=render_point(info->postlist[info->loneighbor[i-2]],
  271. info->postlist[info->hineighbor[i-2]],
  272. fit_value[info->loneighbor[i-2]],
  273. fit_value[info->hineighbor[i-2]],
  274. info->postlist[i]);
  275. int hiroom=quant_q-predicted;
  276. int loroom=predicted;
  277. int room=(hiroom<loroom?hiroom:loroom)<<1;
  278. int val=fit_value[i];
  279. if(val){
  280. if(val>=room){
  281. if(hiroom>loroom){
  282. val = val-loroom;
  283. }else{
  284. val = -1-(val-hiroom);
  285. }
  286. }else{
  287. if(val&1){
  288. val= -((val+1)>>1);
  289. }else{
  290. val>>=1;
  291. }
  292. }
  293. fit_value[i]=val+predicted;
  294. fit_value[info->loneighbor[i-2]]&=0x7fff;
  295. fit_value[info->hineighbor[i-2]]&=0x7fff;
  296. }else{
  297. fit_value[i]=predicted|0x8000;
  298. }
  299. }
  300. return(fit_value);
  301. }
  302. eop:
  303. return(NULL);
  304. }
  305. int floor1_inverse2(vorbis_dsp_state *vd,vorbis_info_floor *in,
  306. ogg_int32_t *fit_value,ogg_int32_t *out){
  307. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  308. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  309. int n=ci->blocksizes[vd->W]/2;
  310. int j;
  311. if(fit_value){
  312. /* render the lines */
  313. int hx=0;
  314. int lx=0;
  315. int ly=fit_value[0]*info->mult;
  316. for(j=1;j<info->posts;j++){
  317. int current=info->forward_index[j];
  318. int hy=fit_value[current]&0x7fff;
  319. if(hy==fit_value[current]){
  320. hy*=info->mult;
  321. hx=info->postlist[current];
  322. render_line(n,lx,hx,ly,hy,out);
  323. lx=hx;
  324. ly=hy;
  325. }
  326. }
  327. for(j=hx;j<n;j++)out[j]*=ly; /* be certain */
  328. return(1);
  329. }
  330. memset(out,0,sizeof(*out)*n);
  331. return(0);
  332. }