floor1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: floor backend 1 implementation
  34. ************************************************************************/
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <math.h>
  38. #include "ogg.h"
  39. #include "ivorbiscodec.h"
  40. #include "codec_internal.h"
  41. #include "codebook.h"
  42. #include "misc.h"
  43. extern const ogg_int32_t FLOOR_fromdB_LOOKUP[];
  44. #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */
  45. #define VIF_POSIT 63
  46. /***********************************************/
  47. void floor1_free_info(vorbis_info_floor *i){
  48. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  49. if(info){
  50. if(info->klass)_ogg_free(info->klass);
  51. if(info->partitionclass)_ogg_free(info->partitionclass);
  52. if(info->postlist)_ogg_free(info->postlist);
  53. if(info->forward_index)_ogg_free(info->forward_index);
  54. if(info->hineighbor)_ogg_free(info->hineighbor);
  55. if(info->loneighbor)_ogg_free(info->loneighbor);
  56. memset(info,0,sizeof(*info));
  57. _ogg_free(info);
  58. }
  59. }
  60. static int ilog(unsigned int v){
  61. int ret=0;
  62. while(v){
  63. ret++;
  64. v>>=1;
  65. }
  66. return(ret);
  67. }
  68. static void floor1_mergesort(ogg_uint8_t *index,ogg_uint16_t *vals,ogg_uint16_t n){
  69. ogg_uint16_t i,j;
  70. ogg_uint8_t *temp,*A=index,*B=_ogg_malloc(n*sizeof(*B));
  71. for(i=1;i<n;i<<=1){
  72. for(j=0;j+i<n;){
  73. int k1=j;
  74. int mid=j+i;
  75. int k2=mid;
  76. int end=(j+i*2<n?j+i*2:n);
  77. while(k1<mid && k2<end){
  78. if(vals[A[k1]]<vals[A[k2]])
  79. B[j++]=A[k1++];
  80. else
  81. B[j++]=A[k2++];
  82. }
  83. while(k1<mid) B[j++]=A[k1++];
  84. while(k2<end) B[j++]=A[k2++];
  85. }
  86. for(;j<n;j++)B[j]=A[j];
  87. temp=A;A=B;B=temp;
  88. }
  89. if(B==index){
  90. for(j=0;j<n;j++)B[j]=A[j];
  91. _ogg_free(A);
  92. }else
  93. _ogg_free(B);
  94. }
  95. vorbis_info_floor *floor1_info_unpack (vorbis_info *vi,oggpack_buffer *opb){
  96. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  97. int j,k,count=0,maxclass=-1,rangebits;
  98. vorbis_info_floor1 *info=(vorbis_info_floor1 *)_ogg_calloc(1,sizeof(*info));
  99. /* read partitions */
  100. info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
  101. info->partitionclass=
  102. (ogg_uint8_t *)_ogg_malloc(info->partitions*sizeof(*info->partitionclass));
  103. for(j=0;j<info->partitions;j++){
  104. info->partitionclass[j]=(char)oggpack_read(opb,4); /* only 0 to 15 legal */
  105. if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
  106. }
  107. /* read partition classes */
  108. info->klass=
  109. (floor1class *)_ogg_malloc((maxclass+1)*sizeof(*info->klass));
  110. for(j=0;j<maxclass+1;j++){
  111. info->klass[j].class_dim=(char)oggpack_read(opb,3)+1; /* 1 to 8 */
  112. info->klass[j].class_subs=(char)oggpack_read(opb,2); /* 0,1,2,3 bits */
  113. if(oggpack_eop(opb)<0) goto err_out;
  114. if(info->klass[j].class_subs)
  115. info->klass[j].class_book=(unsigned char)oggpack_read(opb,8);
  116. else
  117. info->klass[j].class_book=0;
  118. if(info->klass[j].class_book>=ci->books)goto err_out;
  119. for(k=0;k<(1<<info->klass[j].class_subs);k++){
  120. info->klass[j].class_subbook[k]=(unsigned char)(oggpack_read(opb,8)-1);
  121. if(info->klass[j].class_subbook[k]>=ci->books &&
  122. info->klass[j].class_subbook[k]!=0xff)goto err_out;
  123. }
  124. }
  125. /* read the post list */
  126. info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */
  127. rangebits=oggpack_read(opb,4);
  128. for(j=0,k=0;j<info->partitions;j++)
  129. count+=info->klass[info->partitionclass[j]].class_dim;
  130. info->postlist=
  131. (ogg_uint16_t *)_ogg_malloc((count+2)*sizeof(*info->postlist));
  132. info->forward_index=
  133. (ogg_uint8_t *)_ogg_malloc((count+2)*sizeof(*info->forward_index));
  134. info->loneighbor=
  135. (ogg_uint8_t *)_ogg_malloc(count*sizeof(*info->loneighbor));
  136. info->hineighbor=
  137. (ogg_uint8_t *)_ogg_malloc(count*sizeof(*info->hineighbor));
  138. count=0;
  139. for(j=0,k=0;j<info->partitions;j++){
  140. count+=info->klass[info->partitionclass[j]].class_dim;
  141. for(;k<count;k++){
  142. int t=info->postlist[k+2]=(ogg_uint16_t)oggpack_read(opb,rangebits);
  143. if(t>=(1<<rangebits))goto err_out;
  144. }
  145. }
  146. if(oggpack_eop(opb))goto err_out;
  147. info->postlist[0]=0;
  148. info->postlist[1]=1<<rangebits;
  149. info->posts=count+2;
  150. /* also store a sorted position index */
  151. for(j=0;j<info->posts;j++)info->forward_index[j]=j;
  152. floor1_mergesort(info->forward_index,info->postlist,info->posts);
  153. /* discover our neighbors for decode where we don't use fit flags
  154. (that would push the neighbors outward) */
  155. for(j=0;j<info->posts-2;j++){
  156. int lo=0;
  157. int hi=1;
  158. int lx=0;
  159. int hx=info->postlist[1];
  160. int currentx=info->postlist[j+2];
  161. for(k=0;k<j+2;k++){
  162. int x=info->postlist[k];
  163. if(x>lx && x<currentx){
  164. lo=k;
  165. lx=x;
  166. }
  167. if(x<hx && x>currentx){
  168. hi=k;
  169. hx=x;
  170. }
  171. }
  172. info->loneighbor[j]=lo;
  173. info->hineighbor[j]=hi;
  174. }
  175. return(info);
  176. err_out:
  177. floor1_free_info(info);
  178. return(NULL);
  179. }
  180. #ifdef ONLY_C
  181. static
  182. #endif
  183. int render_point(int x0,int x1,int y0,int y1,int x){
  184. y0&=0x7fff; /* mask off flag */
  185. y1&=0x7fff;
  186. {
  187. int dy=y1-y0;
  188. int adx=x1-x0;
  189. int ady=abs(dy);
  190. int err=ady*(x-x0);
  191. int off=err/adx;
  192. if(dy<0)return(y0-off);
  193. return(y0+off);
  194. }
  195. }
  196. #ifndef ONLY_C
  197. void render_lineARM(int n, ogg_int32_t *d,const ogg_int32_t *floor, int base, int err, int adx, int ady);
  198. #endif
  199. static void render_line(int n,int x0,int x1,int y0,int y1,ogg_int32_t *d){
  200. int dy;
  201. int adx;
  202. int ady;
  203. int base;
  204. int err;
  205. const ogg_int32_t *floor;
  206. if(n>x1)n=x1;
  207. n -= x0;
  208. if (n <= 0 || y0 < 0 || y0 > 255 || y1 < 0 || y1 > 255) {
  209. return;
  210. }
  211. dy=y1-y0;
  212. adx=x1-x0;
  213. ady=abs(dy);
  214. base=dy/adx;
  215. err=adx-1;
  216. floor=&FLOOR_fromdB_LOOKUP[y0];
  217. d += x0;
  218. ady-=abs(base*adx);
  219. /* We should add base each time, and then:
  220. * if dy >=0 we occasionally add 1
  221. * else occasionally subtract 1.
  222. * As an optimisation we say that if dy <0 we make base 1 smaller.
  223. * Then we need to add 1 occassionally, rather than subtract 1 - but we
  224. * need to add 1 in all the cases when we wouldn't have done so before.
  225. * Previously we'd have added 1 (100*ady/adx)% of the time. Now we want
  226. * to do so (100*(adx-ady)/adx)% of the time.
  227. */
  228. if (dy < 0){
  229. base--;
  230. ady = adx-ady;
  231. err = 0;
  232. }
  233. //if(x<n)
  234. // d[x]= MULT31_SHIFT15(d[x],FLOOR_fromdB_LOOKUP[y]);
  235. #if defined(ONLY_C)
  236. do{
  237. *d = MULT31_SHIFT15(*d,*floor);
  238. d++;
  239. floor+=base;
  240. err-=ady;
  241. if(err<0){
  242. err+=adx;
  243. floor+=1;
  244. }
  245. n--;
  246. } while(n>0);
  247. #else
  248. render_lineARM(n,d,floor,base,err,adx,ady);
  249. #endif
  250. }
  251. int floor1_memosize(vorbis_info_floor *i){
  252. vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
  253. return info->posts;
  254. }
  255. static int quant_look[4]={256,128,86,64};
  256. ogg_int32_t *floor1_inverse1(vorbis_dsp_state *vd,vorbis_info_floor *in,
  257. ogg_int32_t *fit_value){
  258. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  259. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  260. int i,j,k;
  261. codebook *books=ci->book_param;
  262. int quant_q=quant_look[info->mult-1];
  263. /* unpack wrapped/predicted values from stream */
  264. if(oggpack_read(&vd->opb,1)==1){
  265. fit_value[0]=oggpack_read(&vd->opb,ilog(quant_q-1));
  266. fit_value[1]=oggpack_read(&vd->opb,ilog(quant_q-1));
  267. /* partition by partition */
  268. /* partition by partition */
  269. for(i=0,j=2;i<info->partitions;i++){
  270. int classv=info->partitionclass[i];
  271. int cdim=info->klass[classv].class_dim;
  272. int csubbits=info->klass[classv].class_subs;
  273. int csub=1<<csubbits;
  274. int cval=0;
  275. /* decode the partition's first stage cascade value */
  276. if(csubbits){
  277. cval=vorbis_book_decode(books+info->klass[classv].class_book,&vd->opb);
  278. if(cval==-1)goto eop;
  279. }
  280. for(k=0;k<cdim;k++){
  281. int book=info->klass[classv].class_subbook[cval&(csub-1)];
  282. cval>>=csubbits;
  283. if(book!=0xff){
  284. if((fit_value[j+k]=vorbis_book_decode(books+book,&vd->opb))==-1)
  285. goto eop;
  286. }else{
  287. fit_value[j+k]=0;
  288. }
  289. }
  290. j+=cdim;
  291. }
  292. /* unwrap positive values and reconsitute via linear interpolation */
  293. for(i=2;i<info->posts;i++){
  294. int predicted=render_point(info->postlist[info->loneighbor[i-2]],
  295. info->postlist[info->hineighbor[i-2]],
  296. fit_value[info->loneighbor[i-2]],
  297. fit_value[info->hineighbor[i-2]],
  298. info->postlist[i]);
  299. int hiroom=quant_q-predicted;
  300. int loroom=predicted;
  301. int room=(hiroom<loroom?hiroom:loroom)<<1;
  302. int val=fit_value[i];
  303. if(val){
  304. if(val>=room){
  305. if(hiroom>loroom){
  306. val = val-loroom;
  307. }else{
  308. val = -1-(val-hiroom);
  309. }
  310. }else{
  311. if(val&1){
  312. val= -((val+1)>>1);
  313. }else{
  314. val>>=1;
  315. }
  316. }
  317. fit_value[i]=val+predicted;
  318. fit_value[info->loneighbor[i-2]]&=0x7fff;
  319. fit_value[info->hineighbor[i-2]]&=0x7fff;
  320. }else{
  321. fit_value[i]=predicted|0x8000;
  322. }
  323. }
  324. return(fit_value);
  325. }
  326. eop:
  327. return(NULL);
  328. }
  329. int floor1_inverse2(vorbis_dsp_state *vd,vorbis_info_floor *in,
  330. ogg_int32_t *fit_value,ogg_int32_t *out){
  331. vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
  332. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  333. int n=ci->blocksizes[vd->W]/2;
  334. int j;
  335. if(fit_value){
  336. /* render the lines */
  337. int hx=0;
  338. int lx=0;
  339. int ly=fit_value[0]*info->mult;
  340. for(j=1;j<info->posts;j++){
  341. int current=info->forward_index[j];
  342. int hy=fit_value[current]&0x7fff;
  343. if(hy==fit_value[current]){
  344. hy*=info->mult;
  345. hx=info->postlist[current];
  346. render_line(n,lx,hx,ly,hy,out);
  347. lx=hx;
  348. ly=hy;
  349. }
  350. }
  351. for(j=hx;j<n;j++)out[j]*=ly; /* be certain */
  352. return(1);
  353. }
  354. memset(out,0,sizeof(*out)*n);
  355. return(0);
  356. }