od_filter.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*Daala video codec
  2. Copyright (c) 2013 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #include "od_defs.h"
  21. #include "od_filter.h"
  22. int NE_FILTER_PARAMS4[4];
  23. int NE_FILTER_PARAMS8[10];
  24. int NE_FILTER_PARAMS16[22];
  25. void ne_filter_params_init(){
  26. int i;
  27. #if B_SZ==4
  28. for(i=0;i<4;i++){
  29. NE_FILTER_PARAMS4[i]=OD_FILTER_PARAMS4[i];
  30. }
  31. #elif B_SZ==8
  32. for(i=0;i<10;i++){
  33. NE_FILTER_PARAMS8[i]=OD_FILTER_PARAMS8[i];
  34. }
  35. #elif B_SZ==16
  36. for(i=0;i<22;i++){
  37. NE_FILTER_PARAMS16[i]=OD_FILTER_PARAMS16[i];
  38. }
  39. #else
  40. # error "Need filter params for this block size."
  41. #endif
  42. }
  43. const od_filter_func NE_PRE_FILTER[OD_NBSIZES]={
  44. od_pre_filter4,
  45. od_pre_filter8,
  46. od_pre_filter16
  47. };
  48. const od_filter_func NE_POST_FILTER[OD_NBSIZES]={
  49. od_post_filter4,
  50. od_post_filter8,
  51. od_post_filter16
  52. };
  53. static void ne_pre_filter4_double(double _y[4],const double _x[4],
  54. const int _f[4]){
  55. double t[4];
  56. t[3]=_x[0]-_x[3];
  57. t[2]=_x[1]-_x[2];
  58. t[1]=_x[1]-(t[2]/2);
  59. t[0]=_x[0]-(t[3]/2);
  60. t[2]=t[2]*_f[0]/(1<<FILTER_BITS);
  61. t[3]=t[3]*_f[1]/(1<<FILTER_BITS);
  62. t[3]+=t[2]*_f[2]/(1<<FILTER_BITS);
  63. t[2]+=t[3]*_f[3]/(1<<FILTER_BITS);
  64. t[0]+=t[3]/2;
  65. _y[0]=t[0];
  66. t[1]+=t[2]/2;
  67. _y[1]=t[1];
  68. _y[2]=(t[1]-t[2]);
  69. _y[3]=(t[0]-t[3]);
  70. }
  71. static void ne_post_filter4_double(double _x[4],const double _y[4],
  72. const int _f[4]){
  73. double t[4];
  74. t[3]=_y[0]-_y[3];
  75. t[2]=_y[1]-_y[2];
  76. t[1]=_y[1]-(t[2]/2);
  77. t[0]=_y[0]-(t[3]/2);
  78. t[2]-=t[3]*_f[3]/(1<<FILTER_BITS);
  79. t[3]-=t[2]*_f[2]/(1<<FILTER_BITS);
  80. t[3]=t[3]*(1<<FILTER_BITS)/_f[1];
  81. t[2]=t[2]*(1<<FILTER_BITS)/_f[0];
  82. t[0]+=t[3]/2;
  83. _x[0]=t[0];
  84. t[1]+=t[2]/2;
  85. _x[1]=t[1];
  86. _x[2]=(t[1]-t[2]);
  87. _x[3]=(t[0]-t[3]);
  88. }
  89. static void ne_pre_filter8_double(double _y[8],const double _x[8],
  90. const int _f[10]){
  91. double t[8];
  92. t[7]=_x[0]-_x[7];
  93. t[6]=_x[1]-_x[6];
  94. t[5]=_x[2]-_x[5];
  95. t[4]=_x[3]-_x[4];
  96. t[3]=_x[3]-(t[4]/2);
  97. t[2]=_x[2]-(t[5]/2);
  98. t[1]=_x[1]-(t[6]/2);
  99. t[0]=_x[0]-(t[7]/2);
  100. t[4]=t[4]*_f[0]/(1<<FILTER_BITS);
  101. t[5]=t[5]*_f[1]/(1<<FILTER_BITS);
  102. t[6]=t[6]*_f[2]/(1<<FILTER_BITS);
  103. t[7]=t[7]*_f[3]/(1<<FILTER_BITS);
  104. #if USE_TYPE3
  105. t[7]+=t[6]*_f[6]/(1<<FILTER_BITS);
  106. t[6]+=t[7]*_f[9]/(1<<FILTER_BITS);
  107. t[6]+=t[5]*_f[5]/(1<<FILTER_BITS);
  108. t[5]+=t[6]*_f[8]/(1<<FILTER_BITS);
  109. t[5]+=t[4]*_f[4]/(1<<FILTER_BITS);
  110. t[4]+=t[5]*_f[7]/(1<<FILTER_BITS);
  111. #else
  112. t[5]+=t[4]*_f[4]/(1<<FILTER_BITS);
  113. t[6]+=t[5]*_f[5]/(1<<FILTER_BITS);
  114. t[7]+=t[6]*_f[6]/(1<<FILTER_BITS);
  115. t[6]+=t[7]*_f[9]/(1<<FILTER_BITS);
  116. t[5]+=t[6]*_f[8]/(1<<FILTER_BITS);
  117. t[4]+=t[5]*_f[7]/(1<<FILTER_BITS);
  118. #endif
  119. t[0]+=t[7]/2;
  120. _y[0]=t[0];
  121. t[1]+=t[6]/2;
  122. _y[1]=t[1];
  123. t[2]+=t[5]/2;
  124. _y[2]=t[2];
  125. t[3]+=t[4]/2;
  126. _y[3]=t[3];
  127. _y[4]=(t[3]-t[4]);
  128. _y[5]=(t[2]-t[5]);
  129. _y[6]=(t[1]-t[6]);
  130. _y[7]=(t[0]-t[7]);
  131. }
  132. static void ne_post_filter8_double(double _x[8],const double _y[8],
  133. const int _f[10]){
  134. double t[8];
  135. t[7]=_y[0]-_y[7];
  136. t[6]=_y[1]-_y[6];
  137. t[5]=_y[2]-_y[5];
  138. t[4]=_y[3]-_y[4];
  139. t[3]=_y[3]-(t[4]/2);
  140. t[2]=_y[2]-(t[5]/2);
  141. t[1]=_y[1]-(t[6]/2);
  142. t[0]=_y[0]-(t[7]/2);
  143. #if USE_TYPE3
  144. t[4]-=t[5]*_f[7]/(1<<FILTER_BITS);
  145. t[5]-=t[4]*_f[4]/(1<<FILTER_BITS);
  146. t[5]-=t[6]*_f[8]/(1<<FILTER_BITS);
  147. t[6]-=t[5]*_f[5]/(1<<FILTER_BITS);
  148. t[6]-=t[7]*_f[9]/(1<<FILTER_BITS);
  149. t[7]-=t[6]*_f[6]/(1<<FILTER_BITS);
  150. #else
  151. t[4]-=t[5]*_f[7]/(1<<FILTER_BITS);
  152. t[5]-=t[6]*_f[8]/(1<<FILTER_BITS);
  153. t[6]-=t[7]*_f[9]/(1<<FILTER_BITS);
  154. t[7]-=t[6]*_f[6]/(1<<FILTER_BITS);
  155. t[6]-=t[5]*_f[5]/(1<<FILTER_BITS);
  156. t[5]-=t[4]*_f[4]/(1<<FILTER_BITS);
  157. #endif
  158. t[7]=t[7]*(1<<FILTER_BITS)/_f[3];
  159. t[6]=t[6]*(1<<FILTER_BITS)/_f[2];
  160. t[5]=t[5]*(1<<FILTER_BITS)/_f[1];
  161. t[4]=t[4]*(1<<FILTER_BITS)/_f[0];
  162. t[0]+=t[7]/2;
  163. _x[0]=t[0];
  164. t[1]+=t[6]/2;
  165. _x[1]=t[1];
  166. t[2]+=t[5]/2;
  167. _x[2]=t[2];
  168. t[3]+=t[4]/2;
  169. _x[3]=t[3];
  170. _x[4]=(t[3]-t[4]);
  171. _x[5]=(t[2]-t[5]);
  172. _x[6]=(t[1]-t[6]);
  173. _x[7]=(t[0]-t[7]);
  174. }
  175. static void ne_pre_filter16_double(double _y[16],const double _x[16],
  176. const int _f[22]){
  177. double t[16];
  178. t[15]=_x[0]-_x[15];
  179. t[14]=_x[1]-_x[14];
  180. t[13]=_x[2]-_x[13];
  181. t[12]=_x[3]-_x[12];
  182. t[11]=_x[4]-_x[11];
  183. t[10]=_x[5]-_x[10];
  184. t[9]=_x[6]-_x[9];
  185. t[8]=_x[7]-_x[8];
  186. t[7]=_x[7]-(t[8]/2);
  187. t[6]=_x[6]-(t[9]/2);
  188. t[5]=_x[5]-(t[10]/2);
  189. t[4]=_x[4]-(t[11]/2);
  190. t[3]=_x[3]-(t[12]/2);
  191. t[2]=_x[2]-(t[13]/2);
  192. t[1]=_x[1]-(t[14]/2);
  193. t[0]=_x[0]-(t[15]/2);
  194. t[8]=t[8]*_f[0]/(1<<FILTER_BITS);
  195. t[9]=t[9]*_f[1]/(1<<FILTER_BITS);
  196. t[10]=t[10]*_f[2]/(1<<FILTER_BITS);
  197. t[11]=t[11]*_f[3]/(1<<FILTER_BITS);
  198. t[12]=t[12]*_f[4]/(1<<FILTER_BITS);
  199. t[13]=t[13]*_f[5]/(1<<FILTER_BITS);
  200. t[14]=t[14]*_f[6]/(1<<FILTER_BITS);
  201. t[15]=t[15]*_f[7]/(1<<FILTER_BITS);
  202. #if USE_TYPE3
  203. t[15]+=t[14]*_f[14]/(1<<FILTER_BITS);
  204. t[14]+=t[15]*_f[21]/(1<<FILTER_BITS);
  205. t[14]+=t[13]*_f[13]/(1<<FILTER_BITS);
  206. t[13]+=t[14]*_f[20]/(1<<FILTER_BITS);
  207. t[13]+=t[12]*_f[12]/(1<<FILTER_BITS);
  208. t[12]+=t[13]*_f[19]/(1<<FILTER_BITS);
  209. t[12]+=t[11]*_f[11]/(1<<FILTER_BITS);
  210. t[11]+=t[12]*_f[18]/(1<<FILTER_BITS);
  211. t[11]+=t[10]*_f[10]/(1<<FILTER_BITS);
  212. t[10]+=t[11]*_f[17]/(1<<FILTER_BITS);
  213. t[10]+=t[9]*_f[9]/(1<<FILTER_BITS);
  214. t[9]+=t[10]*_f[16]/(1<<FILTER_BITS);
  215. t[9]+=t[8]*_f[8]/(1<<FILTER_BITS);
  216. t[8]+=t[9]*_f[15]/(1<<FILTER_BITS);
  217. #else
  218. t[9]+=t[8]*_f[8]/(1<<FILTER_BITS);
  219. t[10]+=t[9]*_f[9]/(1<<FILTER_BITS);
  220. t[11]+=t[10]*_f[10]/(1<<FILTER_BITS);
  221. t[12]+=t[11]*_f[11]/(1<<FILTER_BITS);
  222. t[13]+=t[12]*_f[12]/(1<<FILTER_BITS);
  223. t[14]+=t[13]*_f[13]/(1<<FILTER_BITS);
  224. t[15]+=t[14]*_f[14]/(1<<FILTER_BITS);
  225. t[14]+=t[15]*_f[21]/(1<<FILTER_BITS);
  226. t[13]+=t[14]*_f[20]/(1<<FILTER_BITS);
  227. t[12]+=t[13]*_f[19]/(1<<FILTER_BITS);
  228. t[11]+=t[12]*_f[18]/(1<<FILTER_BITS);
  229. t[10]+=t[11]*_f[17]/(1<<FILTER_BITS);
  230. t[9]+=t[10]*_f[16]/(1<<FILTER_BITS);
  231. t[8]+=t[9]*_f[15]/(1<<FILTER_BITS);
  232. #endif
  233. t[0]+=t[15]/2;
  234. _y[0]=t[0];
  235. t[1]+=t[14]/2;
  236. _y[1]=t[1];
  237. t[2]+=t[13]/2;
  238. _y[2]=t[2];
  239. t[3]+=t[12]/2;
  240. _y[3]=t[3];
  241. t[4]+=t[11]/2;
  242. _y[4]=t[4];
  243. t[5]+=t[10]/2;
  244. _y[5]=t[5];
  245. t[6]+=t[9]/2;
  246. _y[6]=t[6];
  247. t[7]+=t[8]/2;
  248. _y[7]=t[7];
  249. _y[8]=(t[7]-t[8]);
  250. _y[9]=(t[6]-t[9]);
  251. _y[10]=(t[5]-t[10]);
  252. _y[11]=(t[4]-t[11]);
  253. _y[12]=(t[3]-t[12]);
  254. _y[13]=(t[2]-t[13]);
  255. _y[14]=(t[1]-t[14]);
  256. _y[15]=(t[0]-t[15]);
  257. }
  258. static void ne_post_filter16_double(double _x[16],const double _y[16],
  259. const int _f[22]){
  260. double t[16];
  261. t[15]=_y[0]-_y[15];
  262. t[14]=_y[1]-_y[14];
  263. t[13]=_y[2]-_y[13];
  264. t[12]=_y[3]-_y[12];
  265. t[11]=_y[4]-_y[11];
  266. t[10]=_y[5]-_y[10];
  267. t[9]=_y[6]-_y[9];
  268. t[8]=_y[7]-_y[8];
  269. t[7]=_y[7]-(t[8]/2);
  270. t[6]=_y[6]-(t[9]/2);
  271. t[5]=_y[5]-(t[10]/2);
  272. t[4]=_y[4]-(t[11]/2);
  273. t[3]=_y[3]-(t[12]/2);
  274. t[2]=_y[2]-(t[13]/2);
  275. t[1]=_y[1]-(t[14]/2);
  276. t[0]=_y[0]-(t[15]/2);
  277. #if USE_TYPE3
  278. t[8]-=t[9]*_f[15]/(1<<FILTER_BITS);
  279. t[9]-=t[8]*_f[8]/(1<<FILTER_BITS);
  280. t[9]-=t[10]*_f[16]/(1<<FILTER_BITS);
  281. t[10]-=t[9]*_f[9]/(1<<FILTER_BITS);
  282. t[10]-=t[11]*_f[17]/(1<<FILTER_BITS);
  283. t[11]-=t[10]*_f[10]/(1<<FILTER_BITS);
  284. t[11]-=t[12]*_f[18]/(1<<FILTER_BITS);
  285. t[12]-=t[11]*_f[11]/(1<<FILTER_BITS);
  286. t[12]-=t[13]*_f[19]/(1<<FILTER_BITS);
  287. t[13]-=t[12]*_f[12]/(1<<FILTER_BITS);
  288. t[13]-=t[14]*_f[20]/(1<<FILTER_BITS);
  289. t[14]-=t[13]*_f[13]/(1<<FILTER_BITS);
  290. t[14]-=t[15]*_f[21]/(1<<FILTER_BITS);
  291. t[15]-=t[14]*_f[14]/(1<<FILTER_BITS);
  292. #else
  293. t[8]-=t[9]*_f[15]/(1<<FILTER_BITS);
  294. t[9]-=t[10]*_f[16]/(1<<FILTER_BITS);
  295. t[10]-=t[11]*_f[17]/(1<<FILTER_BITS);
  296. t[11]-=t[12]*_f[18]/(1<<FILTER_BITS);
  297. t[12]-=t[13]*_f[19]/(1<<FILTER_BITS);
  298. t[13]-=t[14]*_f[20]/(1<<FILTER_BITS);
  299. t[14]-=t[15]*_f[21]/(1<<FILTER_BITS);
  300. t[15]-=t[14]*_f[14]/(1<<FILTER_BITS);
  301. t[14]-=t[13]*_f[13]/(1<<FILTER_BITS);
  302. t[13]-=t[12]*_f[12]/(1<<FILTER_BITS);
  303. t[12]-=t[11]*_f[11]/(1<<FILTER_BITS);
  304. t[11]-=t[10]*_f[10]/(1<<FILTER_BITS);
  305. t[10]-=t[9]*_f[9]/(1<<FILTER_BITS);
  306. t[9]-=t[8]*_f[8]/(1<<FILTER_BITS);
  307. #endif
  308. t[15]=t[15]*(1<<FILTER_BITS)/_f[7];
  309. t[14]=t[14]*(1<<FILTER_BITS)/_f[6];
  310. t[13]=t[13]*(1<<FILTER_BITS)/_f[5];
  311. t[12]=t[12]*(1<<FILTER_BITS)/_f[4];
  312. t[11]=t[11]*(1<<FILTER_BITS)/_f[3];
  313. t[10]=t[10]*(1<<FILTER_BITS)/_f[2];
  314. t[9]=t[9]*(1<<FILTER_BITS)/_f[1];
  315. t[8]=t[8]*(1<<FILTER_BITS)/_f[0];
  316. t[0]+=t[15]/2;
  317. _x[0]=t[0];
  318. t[1]+=t[14]/2;
  319. _x[1]=t[1];
  320. t[2]+=t[13]/2;
  321. _x[2]=t[2];
  322. t[3]+=t[12]/2;
  323. _x[3]=t[3];
  324. t[4]+=t[11]/2;
  325. _x[4]=t[4];
  326. t[5]+=t[10]/2;
  327. _x[5]=t[5];
  328. t[6]+=t[9]/2;
  329. _x[6]=t[6];
  330. t[7]+=t[8]/2;
  331. _x[7]=t[7];
  332. _x[8]=(t[7]-t[8]);
  333. _x[9]=(t[6]-t[9]);
  334. _x[10]=(t[5]-t[10]);
  335. _x[11]=(t[4]-t[11]);
  336. _x[12]=(t[3]-t[12]);
  337. _x[13]=(t[2]-t[13]);
  338. _x[14]=(t[1]-t[14]);
  339. _x[15]=(t[0]-t[15]);
  340. }
  341. const ne_filter_func_double NE_PRE_FILTER_DOUBLE[OD_NBSIZES]={
  342. ne_pre_filter4_double,
  343. ne_pre_filter8_double,
  344. ne_pre_filter16_double
  345. };
  346. const ne_filter_func_double NE_POST_FILTER_DOUBLE[OD_NBSIZES]={
  347. ne_post_filter4_double,
  348. ne_post_filter8_double,
  349. ne_post_filter16_double
  350. };