od_filter.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "od_defs.h"
  24. #include "od_filter.h"
  25. int NE_FILTER_PARAMS4[4];
  26. int NE_FILTER_PARAMS8[10];
  27. int NE_FILTER_PARAMS16[22];
  28. void ne_filter_params_init(){
  29. int i;
  30. for(i=0;i<4;i++){
  31. NE_FILTER_PARAMS4[i]=OD_FILTER_PARAMS4[i];
  32. }
  33. for(i=0;i<10;i++){
  34. NE_FILTER_PARAMS8[i]=OD_FILTER_PARAMS8[i];
  35. }
  36. for(i=0;i<22;i++){
  37. NE_FILTER_PARAMS16[i]=OD_FILTER_PARAMS16[i];
  38. }
  39. }
  40. const od_filter_func NE_PRE_FILTER[OD_NBSIZES]={
  41. od_pre_filter4,
  42. od_pre_filter8,
  43. od_pre_filter16
  44. };
  45. const od_filter_func NE_POST_FILTER[OD_NBSIZES]={
  46. od_post_filter4,
  47. od_post_filter8,
  48. od_post_filter16
  49. };
  50. static void ne_pre_filter4_double(double _y[4],const double _x[4],
  51. const int _f[4]){
  52. double t[4];
  53. t[3]=_x[0]-_x[3];
  54. t[2]=_x[1]-_x[2];
  55. t[1]=_x[1]-(t[2]/2);
  56. t[0]=_x[0]-(t[3]/2);
  57. t[2]=t[2]*_f[0]/(1<<FILTER_BITS);
  58. t[3]=t[3]*_f[1]/(1<<FILTER_BITS);
  59. t[3]+=t[2]*_f[2]/(1<<FILTER_BITS);
  60. t[2]+=t[3]*_f[3]/(1<<FILTER_BITS);
  61. t[0]+=t[3]/2;
  62. _y[0]=t[0];
  63. t[1]+=t[2]/2;
  64. _y[1]=t[1];
  65. _y[2]=(t[1]-t[2]);
  66. _y[3]=(t[0]-t[3]);
  67. }
  68. static void ne_post_filter4_double(double _x[4],const double _y[4],
  69. const int _f[4]){
  70. double t[4];
  71. t[3]=_y[0]-_y[3];
  72. t[2]=_y[1]-_y[2];
  73. t[1]=_y[1]-(t[2]/2);
  74. t[0]=_y[0]-(t[3]/2);
  75. t[2]-=t[3]*_f[3]/(1<<FILTER_BITS);
  76. t[3]-=t[2]*_f[2]/(1<<FILTER_BITS);
  77. t[3]=t[3]*(1<<FILTER_BITS)/_f[1];
  78. t[2]=t[2]*(1<<FILTER_BITS)/_f[0];
  79. t[0]+=t[3]/2;
  80. _x[0]=t[0];
  81. t[1]+=t[2]/2;
  82. _x[1]=t[1];
  83. _x[2]=(t[1]-t[2]);
  84. _x[3]=(t[0]-t[3]);
  85. }
  86. static void ne_pre_filter8_double(double _y[8],const double _x[8],
  87. const int _f[10]){
  88. double t[8];
  89. t[7]=_x[0]-_x[7];
  90. t[6]=_x[1]-_x[6];
  91. t[5]=_x[2]-_x[5];
  92. t[4]=_x[3]-_x[4];
  93. t[3]=_x[3]-(t[4]/2);
  94. t[2]=_x[2]-(t[5]/2);
  95. t[1]=_x[1]-(t[6]/2);
  96. t[0]=_x[0]-(t[7]/2);
  97. t[4]=t[4]*_f[0]/(1<<FILTER_BITS);
  98. t[5]=t[5]*_f[1]/(1<<FILTER_BITS);
  99. t[6]=t[6]*_f[2]/(1<<FILTER_BITS);
  100. t[7]=t[7]*_f[3]/(1<<FILTER_BITS);
  101. #if USE_TYPE3
  102. t[7]+=t[6]*_f[6]/(1<<FILTER_BITS);
  103. t[6]+=t[7]*_f[9]/(1<<FILTER_BITS);
  104. t[6]+=t[5]*_f[5]/(1<<FILTER_BITS);
  105. t[5]+=t[6]*_f[8]/(1<<FILTER_BITS);
  106. t[5]+=t[4]*_f[4]/(1<<FILTER_BITS);
  107. t[4]+=t[5]*_f[7]/(1<<FILTER_BITS);
  108. #else
  109. t[5]+=t[4]*_f[4]/(1<<FILTER_BITS);
  110. t[6]+=t[5]*_f[5]/(1<<FILTER_BITS);
  111. t[7]+=t[6]*_f[6]/(1<<FILTER_BITS);
  112. t[6]+=t[7]*_f[9]/(1<<FILTER_BITS);
  113. t[5]+=t[6]*_f[8]/(1<<FILTER_BITS);
  114. t[4]+=t[5]*_f[7]/(1<<FILTER_BITS);
  115. #endif
  116. t[0]+=t[7]/2;
  117. _y[0]=t[0];
  118. t[1]+=t[6]/2;
  119. _y[1]=t[1];
  120. t[2]+=t[5]/2;
  121. _y[2]=t[2];
  122. t[3]+=t[4]/2;
  123. _y[3]=t[3];
  124. _y[4]=(t[3]-t[4]);
  125. _y[5]=(t[2]-t[5]);
  126. _y[6]=(t[1]-t[6]);
  127. _y[7]=(t[0]-t[7]);
  128. }
  129. static void ne_post_filter8_double(double _x[8],const double _y[8],
  130. const int _f[10]){
  131. double t[8];
  132. t[7]=_y[0]-_y[7];
  133. t[6]=_y[1]-_y[6];
  134. t[5]=_y[2]-_y[5];
  135. t[4]=_y[3]-_y[4];
  136. t[3]=_y[3]-(t[4]/2);
  137. t[2]=_y[2]-(t[5]/2);
  138. t[1]=_y[1]-(t[6]/2);
  139. t[0]=_y[0]-(t[7]/2);
  140. #if USE_TYPE3
  141. t[4]-=t[5]*_f[7]/(1<<FILTER_BITS);
  142. t[5]-=t[4]*_f[4]/(1<<FILTER_BITS);
  143. t[5]-=t[6]*_f[8]/(1<<FILTER_BITS);
  144. t[6]-=t[5]*_f[5]/(1<<FILTER_BITS);
  145. t[6]-=t[7]*_f[9]/(1<<FILTER_BITS);
  146. t[7]-=t[6]*_f[6]/(1<<FILTER_BITS);
  147. #else
  148. t[4]-=t[5]*_f[7]/(1<<FILTER_BITS);
  149. t[5]-=t[6]*_f[8]/(1<<FILTER_BITS);
  150. t[6]-=t[7]*_f[9]/(1<<FILTER_BITS);
  151. t[7]-=t[6]*_f[6]/(1<<FILTER_BITS);
  152. t[6]-=t[5]*_f[5]/(1<<FILTER_BITS);
  153. t[5]-=t[4]*_f[4]/(1<<FILTER_BITS);
  154. #endif
  155. t[7]=t[7]*(1<<FILTER_BITS)/_f[3];
  156. t[6]=t[6]*(1<<FILTER_BITS)/_f[2];
  157. t[5]=t[5]*(1<<FILTER_BITS)/_f[1];
  158. t[4]=t[4]*(1<<FILTER_BITS)/_f[0];
  159. t[0]+=t[7]/2;
  160. _x[0]=t[0];
  161. t[1]+=t[6]/2;
  162. _x[1]=t[1];
  163. t[2]+=t[5]/2;
  164. _x[2]=t[2];
  165. t[3]+=t[4]/2;
  166. _x[3]=t[3];
  167. _x[4]=(t[3]-t[4]);
  168. _x[5]=(t[2]-t[5]);
  169. _x[6]=(t[1]-t[6]);
  170. _x[7]=(t[0]-t[7]);
  171. }
  172. static void ne_pre_filter16_double(double _y[16],const double _x[16],
  173. const int _f[22]){
  174. double t[16];
  175. t[15]=_x[0]-_x[15];
  176. t[14]=_x[1]-_x[14];
  177. t[13]=_x[2]-_x[13];
  178. t[12]=_x[3]-_x[12];
  179. t[11]=_x[4]-_x[11];
  180. t[10]=_x[5]-_x[10];
  181. t[9]=_x[6]-_x[9];
  182. t[8]=_x[7]-_x[8];
  183. t[7]=_x[7]-(t[8]/2);
  184. t[6]=_x[6]-(t[9]/2);
  185. t[5]=_x[5]-(t[10]/2);
  186. t[4]=_x[4]-(t[11]/2);
  187. t[3]=_x[3]-(t[12]/2);
  188. t[2]=_x[2]-(t[13]/2);
  189. t[1]=_x[1]-(t[14]/2);
  190. t[0]=_x[0]-(t[15]/2);
  191. t[8]=t[8]*_f[0]/(1<<FILTER_BITS);
  192. t[9]=t[9]*_f[1]/(1<<FILTER_BITS);
  193. t[10]=t[10]*_f[2]/(1<<FILTER_BITS);
  194. t[11]=t[11]*_f[3]/(1<<FILTER_BITS);
  195. t[12]=t[12]*_f[4]/(1<<FILTER_BITS);
  196. t[13]=t[13]*_f[5]/(1<<FILTER_BITS);
  197. t[14]=t[14]*_f[6]/(1<<FILTER_BITS);
  198. t[15]=t[15]*_f[7]/(1<<FILTER_BITS);
  199. #if USE_TYPE3
  200. t[15]+=t[14]*_f[14]/(1<<FILTER_BITS);
  201. t[14]+=t[15]*_f[21]/(1<<FILTER_BITS);
  202. t[14]+=t[13]*_f[13]/(1<<FILTER_BITS);
  203. t[13]+=t[14]*_f[20]/(1<<FILTER_BITS);
  204. t[13]+=t[12]*_f[12]/(1<<FILTER_BITS);
  205. t[12]+=t[13]*_f[19]/(1<<FILTER_BITS);
  206. t[12]+=t[11]*_f[11]/(1<<FILTER_BITS);
  207. t[11]+=t[12]*_f[18]/(1<<FILTER_BITS);
  208. t[11]+=t[10]*_f[10]/(1<<FILTER_BITS);
  209. t[10]+=t[11]*_f[17]/(1<<FILTER_BITS);
  210. t[10]+=t[9]*_f[9]/(1<<FILTER_BITS);
  211. t[9]+=t[10]*_f[16]/(1<<FILTER_BITS);
  212. t[9]+=t[8]*_f[8]/(1<<FILTER_BITS);
  213. t[8]+=t[9]*_f[15]/(1<<FILTER_BITS);
  214. #else
  215. t[9]+=t[8]*_f[8]/(1<<FILTER_BITS);
  216. t[10]+=t[9]*_f[9]/(1<<FILTER_BITS);
  217. t[11]+=t[10]*_f[10]/(1<<FILTER_BITS);
  218. t[12]+=t[11]*_f[11]/(1<<FILTER_BITS);
  219. t[13]+=t[12]*_f[12]/(1<<FILTER_BITS);
  220. t[14]+=t[13]*_f[13]/(1<<FILTER_BITS);
  221. t[15]+=t[14]*_f[14]/(1<<FILTER_BITS);
  222. t[14]+=t[15]*_f[21]/(1<<FILTER_BITS);
  223. t[13]+=t[14]*_f[20]/(1<<FILTER_BITS);
  224. t[12]+=t[13]*_f[19]/(1<<FILTER_BITS);
  225. t[11]+=t[12]*_f[18]/(1<<FILTER_BITS);
  226. t[10]+=t[11]*_f[17]/(1<<FILTER_BITS);
  227. t[9]+=t[10]*_f[16]/(1<<FILTER_BITS);
  228. t[8]+=t[9]*_f[15]/(1<<FILTER_BITS);
  229. #endif
  230. t[0]+=t[15]/2;
  231. _y[0]=t[0];
  232. t[1]+=t[14]/2;
  233. _y[1]=t[1];
  234. t[2]+=t[13]/2;
  235. _y[2]=t[2];
  236. t[3]+=t[12]/2;
  237. _y[3]=t[3];
  238. t[4]+=t[11]/2;
  239. _y[4]=t[4];
  240. t[5]+=t[10]/2;
  241. _y[5]=t[5];
  242. t[6]+=t[9]/2;
  243. _y[6]=t[6];
  244. t[7]+=t[8]/2;
  245. _y[7]=t[7];
  246. _y[8]=(t[7]-t[8]);
  247. _y[9]=(t[6]-t[9]);
  248. _y[10]=(t[5]-t[10]);
  249. _y[11]=(t[4]-t[11]);
  250. _y[12]=(t[3]-t[12]);
  251. _y[13]=(t[2]-t[13]);
  252. _y[14]=(t[1]-t[14]);
  253. _y[15]=(t[0]-t[15]);
  254. }
  255. static void ne_post_filter16_double(double _x[16],const double _y[16],
  256. const int _f[22]){
  257. double t[16];
  258. t[15]=_y[0]-_y[15];
  259. t[14]=_y[1]-_y[14];
  260. t[13]=_y[2]-_y[13];
  261. t[12]=_y[3]-_y[12];
  262. t[11]=_y[4]-_y[11];
  263. t[10]=_y[5]-_y[10];
  264. t[9]=_y[6]-_y[9];
  265. t[8]=_y[7]-_y[8];
  266. t[7]=_y[7]-(t[8]/2);
  267. t[6]=_y[6]-(t[9]/2);
  268. t[5]=_y[5]-(t[10]/2);
  269. t[4]=_y[4]-(t[11]/2);
  270. t[3]=_y[3]-(t[12]/2);
  271. t[2]=_y[2]-(t[13]/2);
  272. t[1]=_y[1]-(t[14]/2);
  273. t[0]=_y[0]-(t[15]/2);
  274. #if USE_TYPE3
  275. t[8]-=t[9]*_f[15]/(1<<FILTER_BITS);
  276. t[9]-=t[8]*_f[8]/(1<<FILTER_BITS);
  277. t[9]-=t[10]*_f[16]/(1<<FILTER_BITS);
  278. t[10]-=t[9]*_f[9]/(1<<FILTER_BITS);
  279. t[10]-=t[11]*_f[17]/(1<<FILTER_BITS);
  280. t[11]-=t[10]*_f[10]/(1<<FILTER_BITS);
  281. t[11]-=t[12]*_f[18]/(1<<FILTER_BITS);
  282. t[12]-=t[11]*_f[11]/(1<<FILTER_BITS);
  283. t[12]-=t[13]*_f[19]/(1<<FILTER_BITS);
  284. t[13]-=t[12]*_f[12]/(1<<FILTER_BITS);
  285. t[13]-=t[14]*_f[20]/(1<<FILTER_BITS);
  286. t[14]-=t[13]*_f[13]/(1<<FILTER_BITS);
  287. t[14]-=t[15]*_f[21]/(1<<FILTER_BITS);
  288. t[15]-=t[14]*_f[14]/(1<<FILTER_BITS);
  289. #else
  290. t[8]-=t[9]*_f[15]/(1<<FILTER_BITS);
  291. t[9]-=t[10]*_f[16]/(1<<FILTER_BITS);
  292. t[10]-=t[11]*_f[17]/(1<<FILTER_BITS);
  293. t[11]-=t[12]*_f[18]/(1<<FILTER_BITS);
  294. t[12]-=t[13]*_f[19]/(1<<FILTER_BITS);
  295. t[13]-=t[14]*_f[20]/(1<<FILTER_BITS);
  296. t[14]-=t[15]*_f[21]/(1<<FILTER_BITS);
  297. t[15]-=t[14]*_f[14]/(1<<FILTER_BITS);
  298. t[14]-=t[13]*_f[13]/(1<<FILTER_BITS);
  299. t[13]-=t[12]*_f[12]/(1<<FILTER_BITS);
  300. t[12]-=t[11]*_f[11]/(1<<FILTER_BITS);
  301. t[11]-=t[10]*_f[10]/(1<<FILTER_BITS);
  302. t[10]-=t[9]*_f[9]/(1<<FILTER_BITS);
  303. t[9]-=t[8]*_f[8]/(1<<FILTER_BITS);
  304. #endif
  305. t[15]=t[15]*(1<<FILTER_BITS)/_f[7];
  306. t[14]=t[14]*(1<<FILTER_BITS)/_f[6];
  307. t[13]=t[13]*(1<<FILTER_BITS)/_f[5];
  308. t[12]=t[12]*(1<<FILTER_BITS)/_f[4];
  309. t[11]=t[11]*(1<<FILTER_BITS)/_f[3];
  310. t[10]=t[10]*(1<<FILTER_BITS)/_f[2];
  311. t[9]=t[9]*(1<<FILTER_BITS)/_f[1];
  312. t[8]=t[8]*(1<<FILTER_BITS)/_f[0];
  313. t[0]+=t[15]/2;
  314. _x[0]=t[0];
  315. t[1]+=t[14]/2;
  316. _x[1]=t[1];
  317. t[2]+=t[13]/2;
  318. _x[2]=t[2];
  319. t[3]+=t[12]/2;
  320. _x[3]=t[3];
  321. t[4]+=t[11]/2;
  322. _x[4]=t[4];
  323. t[5]+=t[10]/2;
  324. _x[5]=t[5];
  325. t[6]+=t[9]/2;
  326. _x[6]=t[6];
  327. t[7]+=t[8]/2;
  328. _x[7]=t[7];
  329. _x[8]=(t[7]-t[8]);
  330. _x[9]=(t[6]-t[9]);
  331. _x[10]=(t[5]-t[10]);
  332. _x[11]=(t[4]-t[11]);
  333. _x[12]=(t[3]-t[12]);
  334. _x[13]=(t[2]-t[13]);
  335. _x[14]=(t[1]-t[14]);
  336. _x[15]=(t[0]-t[15]);
  337. }
  338. const ne_filter_func_double NE_PRE_FILTER_DOUBLE[OD_NBSIZES]={
  339. ne_pre_filter4_double,
  340. ne_pre_filter8_double,
  341. ne_pre_filter16_double
  342. };
  343. const ne_filter_func_double NE_POST_FILTER_DOUBLE[OD_NBSIZES]={
  344. ne_post_filter4_double,
  345. ne_post_filter8_double,
  346. ne_post_filter16_double
  347. };