encfrag.c 8.6 KB

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