intrax8dsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file libavcodec/intrax8dsp.c
  20. *@brief IntraX8 frame subdecoder image manipulation routines
  21. */
  22. #include "dsputil.h"
  23. /*
  24. area positions, #3 is 1 pixel only, other are 8 pixels
  25. |66666666|
  26. 3|44444444|55555555|
  27. - -+--------+--------+
  28. 1 2|XXXXXXXX|
  29. 1 2|XXXXXXXX|
  30. 1 2|XXXXXXXX|
  31. 1 2|XXXXXXXX|
  32. 1 2|XXXXXXXX|
  33. 1 2|XXXXXXXX|
  34. 1 2|XXXXXXXX|
  35. 1 2|XXXXXXXX|
  36. ^-start
  37. */
  38. #define area1 (0)
  39. #define area2 (8)
  40. #define area3 (8+8)
  41. #define area4 (8+8+1)
  42. #define area5 (8+8+1+8)
  43. #define area6 (8+8+1+16)
  44. /**
  45. Collect statistics and prepare the edge pixels required by the other spatial compensation functions.
  46. * @param src pointer to the beginning of the processed block
  47. * @param dst pointer to emu_edge, edge pixels are stored the way other compensation routines do.
  48. * @param linesize byte offset between 2 vertical pixels in the source image
  49. * @param range pointer to the variable where the edge pixel range is to be stored (max-min values)
  50. * @param psum pointer to the variable where the edge pixel sum is to be stored
  51. * @param edges Informs this routine that the block is on an image border, so it has to interpolate the missing edge pixels.
  52. and some of the edge pixels should be interpolated, the flag has the following meaning:
  53. 1 - mb_x==0 - first block in the row, interpolate area #1,#2,#3;
  54. 2 - mb_y==0 - first row, interpolate area #3,#4,#5,#6;
  55. note: 1|2 - mb_x==mb_y==0 - first block, use 0x80 value for all areas;
  56. 4 - mb_x>= (mb_width-1) last block in the row, interpolate area #5;
  57. */
  58. static void x8_setup_spatial_compensation(uint8_t *src, uint8_t *dst, int linesize,
  59. int * range, int * psum, int edges){
  60. uint8_t * ptr;
  61. int sum;
  62. int i;
  63. int min_pix,max_pix;
  64. uint8_t c;
  65. if((edges&3)==3){
  66. *psum=0x80*(8+1+8+2);
  67. *range=0;
  68. memset(dst,0x80,16+1+16+8);
  69. //this triggers flat_dc for sure.
  70. //flat_dc avoids all (other) prediction modes, but requires dc_level decoding.
  71. return;
  72. }
  73. min_pix=256;
  74. max_pix=-1;
  75. sum=0;
  76. if(!(edges&1)){//(mb_x!=0)//there is previous block on this row
  77. ptr=src-1;//left column, area 2
  78. for(i=7;i>=0;i--){
  79. c=*(ptr-1);//area1, same mb as area2, no need to check
  80. dst[area1+i]=c;
  81. c=*(ptr);
  82. sum+=c;
  83. min_pix=FFMIN(min_pix,c);
  84. max_pix=FFMAX(max_pix,c);
  85. dst[area2+i]=c;
  86. ptr+=linesize;
  87. }
  88. }
  89. if(!(edges&2)){ //(mb_y!=0)//there is row above
  90. ptr=src-linesize;//top line
  91. for(i=0;i<8;i++){
  92. c=*(ptr+i);
  93. sum+=c;
  94. min_pix=FFMIN(min_pix, c);
  95. max_pix=FFMAX(max_pix, c);
  96. }
  97. if(edges&4){//last block on the row?
  98. memset(dst+area5,c,8);//set with last pixel fr
  99. memcpy(dst+area4, ptr, 8);
  100. }else{
  101. memcpy(dst+area4, ptr, 16);//both area4 and 5
  102. }
  103. memcpy(dst+area6, ptr-linesize, 8);//area6 always present in the above block
  104. }
  105. //now calculate the stuff we need
  106. if(edges&3){//mb_x==0 || mb_y==0){
  107. int avg=(sum+4)>>3;
  108. if(edges&1){ //(mb_x==0) {//implies mb_y!=0
  109. memset(dst+area1,avg,8+8+1);//areas 1,2 and 3 are averaged
  110. }else{//implies y==0 x!=0
  111. memset(dst+area3,avg, 1+16+8);//areas 3, 4,5,6
  112. }
  113. sum+=avg*9;
  114. }else{
  115. uint8_t c=*(src-1-linesize);//the edge pixel, in the top line and left column
  116. dst[area3]=c;
  117. sum+=c;
  118. //edge pixel is not part of min/max
  119. }
  120. (*range) = max_pix - min_pix;
  121. sum += *(dst+area5) + *(dst+area5+1);
  122. *psum = sum;
  123. }
  124. static const uint16_t zero_prediction_weights[64*2] = {
  125. 640, 640, 669, 480, 708, 354, 748, 257, 792, 198, 760, 143, 808, 101, 772, 72,
  126. 480, 669, 537, 537, 598, 416, 661, 316, 719, 250, 707, 185, 768, 134, 745, 97,
  127. 354, 708, 416, 598, 488, 488, 564, 388, 634, 317, 642, 241, 716, 179, 706, 132,
  128. 257, 748, 316, 661, 388, 564, 469, 469, 543, 395, 571, 311, 655, 238, 660, 180,
  129. 198, 792, 250, 719, 317, 634, 395, 543, 469, 469, 507, 380, 597, 299, 616, 231,
  130. 161, 855, 206, 788, 266, 710, 340, 623, 411, 548, 455, 455, 548, 366, 576, 288,
  131. 122, 972, 159, 914, 211, 842, 276, 758, 341, 682, 389, 584, 483, 483, 520, 390,
  132. 110, 1172, 144, 1107, 193, 1028, 254, 932, 317, 846, 366, 731, 458, 611, 499, 499
  133. };
  134. static void spatial_compensation_0(uint8_t *src , uint8_t *dst, int linesize){
  135. int i,j;
  136. int x,y;
  137. unsigned int p;//power divided by 2
  138. int a;
  139. uint16_t left_sum[2][8];
  140. uint16_t top_sum[2][8];
  141. memset(left_sum,0,2*8*sizeof(uint16_t));
  142. memset( top_sum,0,2*8*sizeof(uint16_t));
  143. for(i=0;i<8;i++){
  144. a=src[area2+7-i]<<4;
  145. for(j=0;j<8;j++){
  146. p=abs(i-j);
  147. left_sum[p&1][j]+= a>>(p>>1);
  148. }
  149. }
  150. for(i=0;i<8;i++){
  151. a=src[area4+i]<<4;
  152. for(j=0;j<8;j++){
  153. p=abs(i-j);
  154. top_sum[p&1][j]+= a>>(p>>1);
  155. }
  156. }
  157. for(;i<10;i++){
  158. a=src[area4+i]<<4;
  159. for(j=5;j<8;j++){
  160. p=abs(i-j);
  161. top_sum[p&1][j]+= a>>(p>>1);
  162. }
  163. }
  164. for(;i<12;i++){
  165. a=src[area4+i]<<4;
  166. for(j=7;j<8;j++){
  167. p=abs(i-j);
  168. top_sum[p&1][j]+= a>>(p>>1);
  169. }
  170. }
  171. for(i=0;i<8;i++){
  172. top_sum [0][i]+=(top_sum [1][i]*181 + 128 )>>8;//181 is sqrt(2)/2
  173. left_sum[0][i]+=(left_sum[1][i]*181 + 128 )>>8;
  174. }
  175. for(y=0;y<8;y++){
  176. for(x=0;x<8;x++){
  177. dst[x] = (
  178. (uint32_t)top_sum [0][x]*zero_prediction_weights[y*16+x*2+0] +
  179. (uint32_t)left_sum[0][y]*zero_prediction_weights[y*16+x*2+1] +
  180. 0x8000
  181. )>>16;
  182. }
  183. dst+=linesize;
  184. }
  185. }
  186. static void spatial_compensation_1(uint8_t *src , uint8_t *dst, int linesize){
  187. int x,y;
  188. for(y=0;y<8;y++){
  189. for(x=0;x<8;x++){
  190. dst[x]=src[area4 + FFMIN(2*y+x+2, 15) ];
  191. }
  192. dst+=linesize;
  193. }
  194. }
  195. static void spatial_compensation_2(uint8_t *src , uint8_t *dst, int linesize){
  196. int x,y;
  197. for(y=0;y<8;y++){
  198. for(x=0;x<8;x++){
  199. dst[x]=src[area4 +1+y+x];
  200. }
  201. dst+=linesize;
  202. }
  203. }
  204. static void spatial_compensation_3(uint8_t *src , uint8_t *dst, int linesize){
  205. int x,y;
  206. for(y=0;y<8;y++){
  207. for(x=0;x<8;x++){
  208. dst[x]=src[area4 +((y+1)>>1)+x];
  209. }
  210. dst+=linesize;
  211. }
  212. }
  213. static void spatial_compensation_4(uint8_t *src , uint8_t *dst, int linesize){
  214. int x,y;
  215. for(y=0;y<8;y++){
  216. for(x=0;x<8;x++){
  217. dst[x]=( src[area4+x] + src[area6+x] + 1 )>>1;
  218. }
  219. dst+=linesize;
  220. }
  221. }
  222. static void spatial_compensation_5(uint8_t *src , uint8_t *dst, int linesize){
  223. int x,y;
  224. for(y=0;y<8;y++){
  225. for(x=0;x<8;x++){
  226. if(2*x-y<0){
  227. dst[x]=src[area2+9+2*x-y];
  228. }else{
  229. dst[x]=src[area4 +x-((y+1)>>1)];
  230. }
  231. }
  232. dst+=linesize;
  233. }
  234. }
  235. static void spatial_compensation_6(uint8_t *src , uint8_t *dst, int linesize){
  236. int x,y;
  237. for(y=0;y<8;y++){
  238. for(x=0;x<8;x++){
  239. dst[x]=src[area3+x-y];
  240. }
  241. dst+=linesize;
  242. }
  243. }
  244. static void spatial_compensation_7(uint8_t *src , uint8_t *dst, int linesize){
  245. int x,y;
  246. for(y=0;y<8;y++){
  247. for(x=0;x<8;x++){
  248. if(x-2*y>0){
  249. dst[x]=( src[area3-1+x-2*y] + src[area3+x-2*y] + 1)>>1;
  250. }else{
  251. dst[x]=src[area2+8-y +(x>>1)];
  252. }
  253. }
  254. dst+=linesize;
  255. }
  256. }
  257. static void spatial_compensation_8(uint8_t *src , uint8_t *dst, int linesize){
  258. int x,y;
  259. for(y=0;y<8;y++){
  260. for(x=0;x<8;x++){
  261. dst[x]=( src[area1+7-y] + src[area2+7-y] + 1 )>>1;
  262. }
  263. dst+=linesize;
  264. }
  265. }
  266. static void spatial_compensation_9(uint8_t *src , uint8_t *dst, int linesize){
  267. int x,y;
  268. for(y=0;y<8;y++){
  269. for(x=0;x<8;x++){
  270. dst[x]=src[area2+6-FFMIN(x+y,6)];
  271. }
  272. dst+=linesize;
  273. }
  274. }
  275. static void spatial_compensation_10(uint8_t *src , uint8_t *dst, int linesize){
  276. int x,y;
  277. for(y=0;y<8;y++){
  278. for(x=0;x<8;x++){
  279. dst[x]=(src[area2+7-y]*(8-x)+src[area4+x]*x+4)>>3;
  280. }
  281. dst+=linesize;
  282. }
  283. }
  284. static void spatial_compensation_11(uint8_t *src , uint8_t *dst, int linesize){
  285. int x,y;
  286. for(y=0;y<8;y++){
  287. for(x=0;x<8;x++){
  288. dst[x]=(src[area2+7-y]*y+src[area4+x]*(8-y)+4)>>3;
  289. }
  290. dst+=linesize;
  291. }
  292. }
  293. static void x8_loop_filter(uint8_t * ptr, const int a_stride, const int b_stride, int quant){
  294. int i,t;
  295. int p0,p1,p2,p3,p4,p5,p6,p7,p8,p9;
  296. int ql=(quant+10)>>3;
  297. for(i=0; i<8; i++,ptr+=b_stride){
  298. p0=ptr[-5*a_stride];
  299. p1=ptr[-4*a_stride];
  300. p2=ptr[-3*a_stride];
  301. p3=ptr[-2*a_stride];
  302. p4=ptr[-1*a_stride];
  303. p5=ptr[ 0 ];
  304. p6=ptr[ 1*a_stride];
  305. p7=ptr[ 2*a_stride];
  306. p8=ptr[ 3*a_stride];
  307. p9=ptr[ 4*a_stride];
  308. t=
  309. (FFABS(p1-p2) <= ql) +
  310. (FFABS(p2-p3) <= ql) +
  311. (FFABS(p3-p4) <= ql) +
  312. (FFABS(p4-p5) <= ql);
  313. if(t>0){//You need at least 1 to be able to reach a total score of 6.
  314. t+=
  315. (FFABS(p5-p6) <= ql) +
  316. (FFABS(p6-p7) <= ql) +
  317. (FFABS(p7-p8) <= ql) +
  318. (FFABS(p8-p9) <= ql) +
  319. (FFABS(p0-p1) <= ql);
  320. if(t>=6){
  321. int min,max;
  322. min=max=p1;
  323. min=FFMIN(min,p3); max=FFMAX(max,p3);
  324. min=FFMIN(min,p5); max=FFMAX(max,p5);
  325. min=FFMIN(min,p8); max=FFMAX(max,p8);
  326. if(max-min<2*quant){//early stop
  327. min=FFMIN(min,p2); max=FFMAX(max,p2);
  328. min=FFMIN(min,p4); max=FFMAX(max,p4);
  329. min=FFMIN(min,p6); max=FFMAX(max,p6);
  330. min=FFMIN(min,p7); max=FFMAX(max,p7);
  331. if(max-min<2*quant){
  332. ptr[-2*a_stride]=(4*p2 + 3*p3 + 1*p7 + 4)>>3;
  333. ptr[-1*a_stride]=(3*p2 + 3*p4 + 2*p7 + 4)>>3;
  334. ptr[ 0 ]=(2*p2 + 3*p5 + 3*p7 + 4)>>3;
  335. ptr[ 1*a_stride]=(1*p2 + 3*p6 + 4*p7 + 4)>>3;
  336. continue;
  337. };
  338. }
  339. }
  340. }
  341. {
  342. int x,x0,x1,x2;
  343. int m;
  344. x0 = (2*p3 - 5*p4 + 5*p5 - 2*p6 + 4)>>3;
  345. if(FFABS(x0) < quant){
  346. x1=(2*p1 - 5*p2 + 5*p3 - 2*p4 + 4)>>3;
  347. x2=(2*p5 - 5*p6 + 5*p7 - 2*p8 + 4)>>3;
  348. x=FFABS(x0) - FFMIN( FFABS(x1), FFABS(x2) );
  349. m=p4-p5;
  350. if( x > 0 && (m^x0) <0){
  351. int32_t sign;
  352. sign=m>>31;
  353. m=(m^sign)-sign;//abs(m)
  354. m>>=1;
  355. x=(5*x)>>3;
  356. if(x>m) x=m;
  357. x=(x^sign)-sign;
  358. ptr[-1*a_stride] -= x;
  359. ptr[ 0] += x;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. static void x8_h_loop_filter(uint8_t *src, int stride, int qscale){
  366. x8_loop_filter(src, stride, 1, qscale);
  367. }
  368. static void x8_v_loop_filter(uint8_t *src, int stride, int qscale){
  369. x8_loop_filter(src, 1, stride, qscale);
  370. }
  371. void ff_intrax8dsp_init(DSPContext* dsp, AVCodecContext *avctx) {
  372. dsp->x8_h_loop_filter=x8_h_loop_filter;
  373. dsp->x8_v_loop_filter=x8_v_loop_filter;
  374. dsp->x8_setup_spatial_compensation=x8_setup_spatial_compensation;
  375. dsp->x8_spatial_compensation[0]=spatial_compensation_0;
  376. dsp->x8_spatial_compensation[1]=spatial_compensation_1;
  377. dsp->x8_spatial_compensation[2]=spatial_compensation_2;
  378. dsp->x8_spatial_compensation[3]=spatial_compensation_3;
  379. dsp->x8_spatial_compensation[4]=spatial_compensation_4;
  380. dsp->x8_spatial_compensation[5]=spatial_compensation_5;
  381. dsp->x8_spatial_compensation[6]=spatial_compensation_6;
  382. dsp->x8_spatial_compensation[7]=spatial_compensation_7;
  383. dsp->x8_spatial_compensation[8]=spatial_compensation_8;
  384. dsp->x8_spatial_compensation[9]=spatial_compensation_9;
  385. dsp->x8_spatial_compensation[10]=spatial_compensation_10;
  386. dsp->x8_spatial_compensation[11]=spatial_compensation_11;
  387. }