floor1.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 vorbis_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. if(count>VIF_POSIT)goto err_out;
  122. for(;k<count;k++){
  123. int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
  124. if(t>=(1<<rangebits))goto err_out;
  125. }
  126. }
  127. if(oggpack_eop(opb))goto err_out;
  128. info->postlist[0]=0;
  129. info->postlist[1]=1<<rangebits;
  130. info->posts=count+2;
  131. /* also store a sorted position index */
  132. for(j=0;j<info->posts;j++)info->forward_index[j]=j;
  133. vorbis_mergesort(info->forward_index,info->postlist,info->posts);
  134. /* discover our neighbors for decode where we don't use fit flags
  135. (that would push the neighbors outward) */
  136. for(j=0;j<info->posts-2;j++){
  137. int lo=0;
  138. int hi=1;
  139. int lx=0;
  140. int hx=info->postlist[1];
  141. int currentx=info->postlist[j+2];
  142. for(k=0;k<j+2;k++){
  143. int x=info->postlist[k];
  144. if(x>lx && x<currentx){
  145. lo=k;
  146. lx=x;
  147. }
  148. if(x<hx && x>currentx){
  149. hi=k;
  150. hx=x;
  151. }
  152. }
  153. info->loneighbor[j]=lo;
  154. info->hineighbor[j]=hi;
  155. }
  156. return(info);
  157. err_out:
  158. floor1_free_info(info);
  159. return(NULL);
  160. }
  161. static int render_point(int x0,int x1,int y0,int y1,int x){
  162. y0&=0x7fff; /* mask off flag */
  163. y1&=0x7fff;
  164. {
  165. int dy=y1-y0;
  166. int adx=x1-x0;
  167. int ady=abs(dy);
  168. int err=ady*(x-x0);
  169. int off=err/adx;
  170. if(dy<0)return(y0-off);
  171. return(y0+off);
  172. }
  173. }
  174. static void render_line(int n,int x0,int x1,int y0,int y1,ogg_int32_t *d){
  175. int dy=y1-y0;
  176. int adx=x1-x0;
  177. int ady=abs(dy);
  178. int base=dy/adx;
  179. int sy=(dy<0?base-1:base+1);
  180. int x=x0;
  181. int y=y0;
  182. int err=0;
  183. if(n>x1)n=x1;
  184. ady-=abs(base*adx);
  185. if(x<n)
  186. d[x]= MULT31_SHIFT15(d[x],FLOOR_fromdB_LOOKUP[y]);
  187. while(++x<n){
  188. err=err+ady;
  189. if(err>=adx){
  190. err-=adx;
  191. y+=sy;
  192. }else{
  193. y+=base;
  194. }
  195. d[x]= MULT31_SHIFT15(d[x],FLOOR_fromdB_LOOKUP[y]);
  196. }
  197. }
  198. int floor1_memosize(vorbis_info_floor *i){
  199. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  200. return info->posts;
  201. }
  202. static int quant_look[4]={256,128,86,64};
  203. ogg_int32_t *floor1_inverse1(vorbis_dsp_state *vd,vorbis_info_floor *in,
  204. ogg_int32_t *fit_value){
  205. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  206. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  207. int i,j,k;
  208. codebook *books=ci->book_param;
  209. int quant_q=quant_look[info->mult-1];
  210. /* unpack wrapped/predicted values from stream */
  211. if(oggpack_read(&vd->opb,1)==1){
  212. fit_value[0]=oggpack_read(&vd->opb,ilog(quant_q-1));
  213. fit_value[1]=oggpack_read(&vd->opb,ilog(quant_q-1));
  214. /* partition by partition */
  215. /* partition by partition */
  216. for(i=0,j=2;i<info->partitions;i++){
  217. int classv=info->partitionclass[i];
  218. int cdim=info->class[classv].class_dim;
  219. int csubbits=info->class[classv].class_subs;
  220. int csub=1<<csubbits;
  221. int cval=0;
  222. /* decode the partition's first stage cascade value */
  223. if(csubbits){
  224. cval=vorbis_book_decode(books+info->class[classv].class_book,&vd->opb);
  225. if(cval==-1)goto eop;
  226. }
  227. for(k=0;k<cdim;k++){
  228. int book=info->class[classv].class_subbook[cval&(csub-1)];
  229. cval>>=csubbits;
  230. if(book!=0xff){
  231. if((fit_value[j+k]=vorbis_book_decode(books+book,&vd->opb))==-1)
  232. goto eop;
  233. }else{
  234. fit_value[j+k]=0;
  235. }
  236. }
  237. j+=cdim;
  238. }
  239. /* unwrap positive values and reconsitute via linear interpolation */
  240. for(i=2;i<info->posts;i++){
  241. int predicted=render_point(info->postlist[info->loneighbor[i-2]],
  242. info->postlist[info->hineighbor[i-2]],
  243. fit_value[info->loneighbor[i-2]],
  244. fit_value[info->hineighbor[i-2]],
  245. info->postlist[i]);
  246. int hiroom=quant_q-predicted;
  247. int loroom=predicted;
  248. int room=(hiroom<loroom?hiroom:loroom)<<1;
  249. int val=fit_value[i];
  250. if(val){
  251. if(val>=room){
  252. if(hiroom>loroom){
  253. val = val-loroom;
  254. }else{
  255. val = -1-(val-hiroom);
  256. }
  257. }else{
  258. if(val&1){
  259. val= -((val+1)>>1);
  260. }else{
  261. val>>=1;
  262. }
  263. }
  264. fit_value[i]=val+predicted;
  265. fit_value[info->loneighbor[i-2]]&=0x7fff;
  266. fit_value[info->hineighbor[i-2]]&=0x7fff;
  267. }else{
  268. fit_value[i]=predicted|0x8000;
  269. }
  270. }
  271. return(fit_value);
  272. }
  273. eop:
  274. return(NULL);
  275. }
  276. int floor1_inverse2(vorbis_dsp_state *vd,vorbis_info_floor *in,
  277. ogg_int32_t *fit_value,ogg_int32_t *out){
  278. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  279. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  280. int n=ci->blocksizes[vd->W]/2;
  281. int j;
  282. if(fit_value){
  283. /* render the lines */
  284. int hx=0;
  285. int lx=0;
  286. int ly=fit_value[0]*info->mult;
  287. for(j=1;j<info->posts;j++){
  288. int current=info->forward_index[j];
  289. int hy=fit_value[current]&0x7fff;
  290. if(hy==fit_value[current]){
  291. hy*=info->mult;
  292. hx=info->postlist[current];
  293. render_line(n,lx,hx,ly,hy,out);
  294. lx=hx;
  295. ly=hy;
  296. }
  297. }
  298. for(j=hx;j<n;j++)out[j]*=ly; /* be certain */
  299. return(1);
  300. }
  301. memset(out,0,sizeof(*out)*n);
  302. return(0);
  303. }