encfrag.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "encint.h"
  18. void oc_enc_frag_sub_c(ogg_int16_t _diff[64],const unsigned char *_src,
  19. const unsigned char *_ref,int _ystride){
  20. int i;
  21. for(i=0;i<8;i++){
  22. int j;
  23. for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-_ref[j]);
  24. _src+=_ystride;
  25. _ref+=_ystride;
  26. }
  27. }
  28. void oc_enc_frag_sub_128_c(ogg_int16_t *_diff,
  29. const unsigned char *_src,int _ystride){
  30. int i;
  31. for(i=0;i<8;i++){
  32. int j;
  33. for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-128);
  34. _src+=_ystride;
  35. }
  36. }
  37. unsigned oc_enc_frag_sad_c(const unsigned char *_src,
  38. const unsigned char *_ref,int _ystride){
  39. unsigned sad;
  40. int i;
  41. sad=0;
  42. for(i=8;i-->0;){
  43. int j;
  44. for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
  45. _src+=_ystride;
  46. _ref+=_ystride;
  47. }
  48. return sad;
  49. }
  50. unsigned oc_enc_frag_sad_thresh_c(const unsigned char *_src,
  51. const unsigned char *_ref,int _ystride,unsigned _thresh){
  52. unsigned sad;
  53. int i;
  54. sad=0;
  55. for(i=8;i-->0;){
  56. int j;
  57. for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
  58. if(sad>_thresh)break;
  59. _src+=_ystride;
  60. _ref+=_ystride;
  61. }
  62. return sad;
  63. }
  64. unsigned oc_enc_frag_sad2_thresh_c(const unsigned char *_src,
  65. const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
  66. unsigned _thresh){
  67. unsigned sad;
  68. int i;
  69. sad=0;
  70. for(i=8;i-->0;){
  71. int j;
  72. for(j=0;j<8;j++)sad+=abs(_src[j]-(_ref1[j]+_ref2[j]>>1));
  73. if(sad>_thresh)break;
  74. _src+=_ystride;
  75. _ref1+=_ystride;
  76. _ref2+=_ystride;
  77. }
  78. return sad;
  79. }
  80. unsigned oc_enc_frag_intra_sad_c(const unsigned char *_src, int _ystride){
  81. const unsigned char *src = _src;
  82. unsigned dc;
  83. unsigned sad;
  84. int i;
  85. dc=0;
  86. for(i=8;i-->0;){
  87. int j;
  88. for(j=0;j<8;j++)dc+=src[j];
  89. src+=_ystride;
  90. }
  91. dc=dc+32>>6;
  92. sad=0;
  93. for(i=8;i-->0;){
  94. int j;
  95. for(j=0;j<8;j++)sad+=abs(_src[j]-dc);
  96. _src+=_ystride;
  97. }
  98. return sad;
  99. }
  100. static void oc_diff_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
  101. const unsigned char *_ref,int _ystride){
  102. int i;
  103. for(i=0;i<8;i++){
  104. int t0;
  105. int t1;
  106. int t2;
  107. int t3;
  108. int t4;
  109. int t5;
  110. int t6;
  111. int t7;
  112. int r;
  113. /*Hadamard stage 1:*/
  114. t0=_src[0]-_ref[0]+_src[4]-_ref[4];
  115. t4=_src[0]-_ref[0]-_src[4]+_ref[4];
  116. t1=_src[1]-_ref[1]+_src[5]-_ref[5];
  117. t5=_src[1]-_ref[1]-_src[5]+_ref[5];
  118. t2=_src[2]-_ref[2]+_src[6]-_ref[6];
  119. t6=_src[2]-_ref[2]-_src[6]+_ref[6];
  120. t3=_src[3]-_ref[3]+_src[7]-_ref[7];
  121. t7=_src[3]-_ref[3]-_src[7]+_ref[7];
  122. /*Hadamard stage 2:*/
  123. r=t0;
  124. t0+=t2;
  125. t2=r-t2;
  126. r=t1;
  127. t1+=t3;
  128. t3=r-t3;
  129. r=t4;
  130. t4+=t6;
  131. t6=r-t6;
  132. r=t5;
  133. t5+=t7;
  134. t7=r-t7;
  135. /*Hadamard stage 3:*/
  136. _buf[0*8+i]=(ogg_int16_t)(t0+t1);
  137. _buf[1*8+i]=(ogg_int16_t)(t0-t1);
  138. _buf[2*8+i]=(ogg_int16_t)(t2+t3);
  139. _buf[3*8+i]=(ogg_int16_t)(t2-t3);
  140. _buf[4*8+i]=(ogg_int16_t)(t4+t5);
  141. _buf[5*8+i]=(ogg_int16_t)(t4-t5);
  142. _buf[6*8+i]=(ogg_int16_t)(t6+t7);
  143. _buf[7*8+i]=(ogg_int16_t)(t6-t7);
  144. _src+=_ystride;
  145. _ref+=_ystride;
  146. }
  147. }
  148. static void oc_diff_hadamard2(ogg_int16_t _buf[64],const unsigned char *_src,
  149. const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
  150. int i;
  151. for(i=0;i<8;i++){
  152. int t0;
  153. int t1;
  154. int t2;
  155. int t3;
  156. int t4;
  157. int t5;
  158. int t6;
  159. int t7;
  160. int r;
  161. /*Hadamard stage 1:*/
  162. r=_ref1[0]+_ref2[0]>>1;
  163. t4=_ref1[4]+_ref2[4]>>1;
  164. t0=_src[0]-r+_src[4]-t4;
  165. t4=_src[0]-r-_src[4]+t4;
  166. r=_ref1[1]+_ref2[1]>>1;
  167. t5=_ref1[5]+_ref2[5]>>1;
  168. t1=_src[1]-r+_src[5]-t5;
  169. t5=_src[1]-r-_src[5]+t5;
  170. r=_ref1[2]+_ref2[2]>>1;
  171. t6=_ref1[6]+_ref2[6]>>1;
  172. t2=_src[2]-r+_src[6]-t6;
  173. t6=_src[2]-r-_src[6]+t6;
  174. r=_ref1[3]+_ref2[3]>>1;
  175. t7=_ref1[7]+_ref2[7]>>1;
  176. t3=_src[3]-r+_src[7]-t7;
  177. t7=_src[3]-r-_src[7]+t7;
  178. /*Hadamard stage 2:*/
  179. r=t0;
  180. t0+=t2;
  181. t2=r-t2;
  182. r=t1;
  183. t1+=t3;
  184. t3=r-t3;
  185. r=t4;
  186. t4+=t6;
  187. t6=r-t6;
  188. r=t5;
  189. t5+=t7;
  190. t7=r-t7;
  191. /*Hadamard stage 3:*/
  192. _buf[0*8+i]=(ogg_int16_t)(t0+t1);
  193. _buf[1*8+i]=(ogg_int16_t)(t0-t1);
  194. _buf[2*8+i]=(ogg_int16_t)(t2+t3);
  195. _buf[3*8+i]=(ogg_int16_t)(t2-t3);
  196. _buf[4*8+i]=(ogg_int16_t)(t4+t5);
  197. _buf[5*8+i]=(ogg_int16_t)(t4-t5);
  198. _buf[6*8+i]=(ogg_int16_t)(t6+t7);
  199. _buf[7*8+i]=(ogg_int16_t)(t6-t7);
  200. _src+=_ystride;
  201. _ref1+=_ystride;
  202. _ref2+=_ystride;
  203. }
  204. }
  205. static void oc_intra_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
  206. int _ystride){
  207. int i;
  208. for(i=0;i<8;i++){
  209. int t0;
  210. int t1;
  211. int t2;
  212. int t3;
  213. int t4;
  214. int t5;
  215. int t6;
  216. int t7;
  217. int r;
  218. /*Hadamard stage 1:*/
  219. t0=_src[0]+_src[4];
  220. t4=_src[0]-_src[4];
  221. t1=_src[1]+_src[5];
  222. t5=_src[1]-_src[5];
  223. t2=_src[2]+_src[6];
  224. t6=_src[2]-_src[6];
  225. t3=_src[3]+_src[7];
  226. t7=_src[3]-_src[7];
  227. /*Hadamard stage 2:*/
  228. r=t0;
  229. t0+=t2;
  230. t2=r-t2;
  231. r=t1;
  232. t1+=t3;
  233. t3=r-t3;
  234. r=t4;
  235. t4+=t6;
  236. t6=r-t6;
  237. r=t5;
  238. t5+=t7;
  239. t7=r-t7;
  240. /*Hadamard stage 3:*/
  241. _buf[0*8+i]=(ogg_int16_t)(t0+t1);
  242. _buf[1*8+i]=(ogg_int16_t)(t0-t1);
  243. _buf[2*8+i]=(ogg_int16_t)(t2+t3);
  244. _buf[3*8+i]=(ogg_int16_t)(t2-t3);
  245. _buf[4*8+i]=(ogg_int16_t)(t4+t5);
  246. _buf[5*8+i]=(ogg_int16_t)(t4-t5);
  247. _buf[6*8+i]=(ogg_int16_t)(t6+t7);
  248. _buf[7*8+i]=(ogg_int16_t)(t6-t7);
  249. _src+=_ystride;
  250. }
  251. }
  252. unsigned oc_hadamard_sad(int *_dc,const ogg_int16_t _buf[64]){
  253. unsigned sad;
  254. int dc;
  255. int t0;
  256. int t1;
  257. int t2;
  258. int t3;
  259. int t4;
  260. int t5;
  261. int t6;
  262. int t7;
  263. int r;
  264. int i;
  265. sad=dc=0;
  266. for(i=0;i<8;i++){
  267. /*Hadamard stage 1:*/
  268. t0=_buf[i*8+0]+_buf[i*8+4];
  269. t4=_buf[i*8+0]-_buf[i*8+4];
  270. t1=_buf[i*8+1]+_buf[i*8+5];
  271. t5=_buf[i*8+1]-_buf[i*8+5];
  272. t2=_buf[i*8+2]+_buf[i*8+6];
  273. t6=_buf[i*8+2]-_buf[i*8+6];
  274. t3=_buf[i*8+3]+_buf[i*8+7];
  275. t7=_buf[i*8+3]-_buf[i*8+7];
  276. /*Hadamard stage 2:*/
  277. r=t0;
  278. t0+=t2;
  279. t2=r-t2;
  280. r=t1;
  281. t1+=t3;
  282. t3=r-t3;
  283. r=t4;
  284. t4+=t6;
  285. t6=r-t6;
  286. r=t5;
  287. t5+=t7;
  288. t7=r-t7;
  289. /*Hadamard stage 3:*/
  290. r=abs(t0+t1)&-(i>0);
  291. r+=abs(t0-t1);
  292. r+=abs(t2+t3);
  293. r+=abs(t2-t3);
  294. r+=abs(t4+t5);
  295. r+=abs(t4-t5);
  296. r+=abs(t6+t7);
  297. r+=abs(t6-t7);
  298. sad+=r;
  299. }
  300. dc=_buf[0]+_buf[1]+_buf[2]+_buf[3]+_buf[4]+_buf[5]+_buf[6]+_buf[7];
  301. *_dc=dc;
  302. return sad;
  303. }
  304. unsigned oc_enc_frag_satd_c(int *_dc,const unsigned char *_src,
  305. const unsigned char *_ref,int _ystride){
  306. ogg_int16_t buf[64];
  307. oc_diff_hadamard(buf,_src,_ref,_ystride);
  308. return oc_hadamard_sad(_dc,buf);
  309. }
  310. unsigned oc_enc_frag_satd2_c(int *_dc,const unsigned char *_src,
  311. const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
  312. ogg_int16_t buf[64];
  313. oc_diff_hadamard2(buf,_src,_ref1,_ref2,_ystride);
  314. return oc_hadamard_sad(_dc,buf);
  315. }
  316. unsigned oc_enc_frag_intra_satd_c(int *_dc,
  317. const unsigned char *_src,int _ystride){
  318. ogg_int16_t buf[64];
  319. oc_intra_hadamard(buf,_src,_ystride);
  320. return oc_hadamard_sad(_dc,buf);
  321. }
  322. unsigned oc_enc_frag_ssd_c(const unsigned char *_src,
  323. const unsigned char *_ref,int _ystride){
  324. unsigned ret;
  325. int y;
  326. int x;
  327. ret=0;
  328. for(y=0;y<8;y++){
  329. for(x=0;x<8;x++)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
  330. _src+=_ystride;
  331. _ref+=_ystride;
  332. }
  333. return ret;
  334. }
  335. unsigned oc_enc_frag_border_ssd_c(const unsigned char *_src,
  336. const unsigned char *_ref,int _ystride,ogg_int64_t _mask){
  337. unsigned ret;
  338. int y;
  339. int x;
  340. ret=0;
  341. for(y=0;y<8;y++){
  342. for(x=0;x<8;x++,_mask>>=1){
  343. if(_mask&1)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
  344. }
  345. _src+=_ystride;
  346. _ref+=_ystride;
  347. }
  348. return ret;
  349. }
  350. void oc_enc_frag_copy2_c(unsigned char *_dst,
  351. const unsigned char *_src1,const unsigned char *_src2,int _ystride){
  352. int i;
  353. int j;
  354. for(i=8;i-->0;){
  355. for(j=0;j<8;j++)_dst[j]=_src1[j]+_src2[j]>>1;
  356. _dst+=_ystride;
  357. _src1+=_ystride;
  358. _src2+=_ystride;
  359. }
  360. }