You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

586 lines
16KB

  1. /*
  2. * Simple IDCT
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file simple_idct.c
  22. * simpleidct in C.
  23. */
  24. /*
  25. based upon some outcommented c code from mpeg2dec (idct_mmx.c
  26. written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
  27. */
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "simple_idct.h"
  31. #if 0
  32. #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
  33. #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
  34. #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
  35. #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
  36. #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
  37. #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
  38. #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
  39. #define ROW_SHIFT 8
  40. #define COL_SHIFT 17
  41. #else
  42. #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  43. #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  44. #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  45. #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  46. #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  47. #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  48. #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  49. #define ROW_SHIFT 11
  50. #define COL_SHIFT 20 // 6
  51. #endif
  52. #if defined(ARCH_POWERPC_405)
  53. /* signed 16x16 -> 32 multiply add accumulate */
  54. #define MAC16(rt, ra, rb) \
  55. asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
  56. /* signed 16x16 -> 32 multiply */
  57. #define MUL16(rt, ra, rb) \
  58. asm ("mullhw %0, %1, %2" : "=r" (rt) : "r" (ra), "r" (rb));
  59. #else
  60. /* signed 16x16 -> 32 multiply add accumulate */
  61. #define MAC16(rt, ra, rb) rt += (ra) * (rb)
  62. /* signed 16x16 -> 32 multiply */
  63. #define MUL16(rt, ra, rb) rt = (ra) * (rb)
  64. #endif
  65. static inline void idctRowCondDC (DCTELEM * row)
  66. {
  67. int a0, a1, a2, a3, b0, b1, b2, b3;
  68. #ifdef FAST_64BIT
  69. uint64_t temp;
  70. #else
  71. uint32_t temp;
  72. #endif
  73. #ifdef FAST_64BIT
  74. #ifdef WORDS_BIGENDIAN
  75. #define ROW0_MASK 0xffff000000000000LL
  76. #else
  77. #define ROW0_MASK 0xffffLL
  78. #endif
  79. if(sizeof(DCTELEM)==2){
  80. if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |
  81. ((uint64_t *)row)[1]) == 0) {
  82. temp = (row[0] << 3) & 0xffff;
  83. temp += temp << 16;
  84. temp += temp << 32;
  85. ((uint64_t *)row)[0] = temp;
  86. ((uint64_t *)row)[1] = temp;
  87. return;
  88. }
  89. }else{
  90. if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
  91. row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
  92. return;
  93. }
  94. }
  95. #else
  96. if(sizeof(DCTELEM)==2){
  97. if (!(((uint32_t*)row)[1] |
  98. ((uint32_t*)row)[2] |
  99. ((uint32_t*)row)[3] |
  100. row[1])) {
  101. temp = (row[0] << 3) & 0xffff;
  102. temp += temp << 16;
  103. ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
  104. ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
  105. return;
  106. }
  107. }else{
  108. if (!(row[1]|row[2]|row[3]|row[4]|row[5]|row[6]|row[7])) {
  109. row[0]=row[1]=row[2]=row[3]=row[4]=row[5]=row[6]=row[7]= row[0] << 3;
  110. return;
  111. }
  112. }
  113. #endif
  114. a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
  115. a1 = a0;
  116. a2 = a0;
  117. a3 = a0;
  118. /* no need to optimize : gcc does it */
  119. a0 += W2 * row[2];
  120. a1 += W6 * row[2];
  121. a2 -= W6 * row[2];
  122. a3 -= W2 * row[2];
  123. MUL16(b0, W1, row[1]);
  124. MAC16(b0, W3, row[3]);
  125. MUL16(b1, W3, row[1]);
  126. MAC16(b1, -W7, row[3]);
  127. MUL16(b2, W5, row[1]);
  128. MAC16(b2, -W1, row[3]);
  129. MUL16(b3, W7, row[1]);
  130. MAC16(b3, -W5, row[3]);
  131. #ifdef FAST_64BIT
  132. temp = ((uint64_t*)row)[1];
  133. #else
  134. temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
  135. #endif
  136. if (temp != 0) {
  137. a0 += W4*row[4] + W6*row[6];
  138. a1 += - W4*row[4] - W2*row[6];
  139. a2 += - W4*row[4] + W2*row[6];
  140. a3 += W4*row[4] - W6*row[6];
  141. MAC16(b0, W5, row[5]);
  142. MAC16(b0, W7, row[7]);
  143. MAC16(b1, -W1, row[5]);
  144. MAC16(b1, -W5, row[7]);
  145. MAC16(b2, W7, row[5]);
  146. MAC16(b2, W3, row[7]);
  147. MAC16(b3, W3, row[5]);
  148. MAC16(b3, -W1, row[7]);
  149. }
  150. row[0] = (a0 + b0) >> ROW_SHIFT;
  151. row[7] = (a0 - b0) >> ROW_SHIFT;
  152. row[1] = (a1 + b1) >> ROW_SHIFT;
  153. row[6] = (a1 - b1) >> ROW_SHIFT;
  154. row[2] = (a2 + b2) >> ROW_SHIFT;
  155. row[5] = (a2 - b2) >> ROW_SHIFT;
  156. row[3] = (a3 + b3) >> ROW_SHIFT;
  157. row[4] = (a3 - b3) >> ROW_SHIFT;
  158. }
  159. static inline void idctSparseColPut (uint8_t *dest, int line_size,
  160. DCTELEM * col)
  161. {
  162. int a0, a1, a2, a3, b0, b1, b2, b3;
  163. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  164. /* XXX: I did that only to give same values as previous code */
  165. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
  166. a1 = a0;
  167. a2 = a0;
  168. a3 = a0;
  169. a0 += + W2*col[8*2];
  170. a1 += + W6*col[8*2];
  171. a2 += - W6*col[8*2];
  172. a3 += - W2*col[8*2];
  173. MUL16(b0, W1, col[8*1]);
  174. MUL16(b1, W3, col[8*1]);
  175. MUL16(b2, W5, col[8*1]);
  176. MUL16(b3, W7, col[8*1]);
  177. MAC16(b0, + W3, col[8*3]);
  178. MAC16(b1, - W7, col[8*3]);
  179. MAC16(b2, - W1, col[8*3]);
  180. MAC16(b3, - W5, col[8*3]);
  181. if(col[8*4]){
  182. a0 += + W4*col[8*4];
  183. a1 += - W4*col[8*4];
  184. a2 += - W4*col[8*4];
  185. a3 += + W4*col[8*4];
  186. }
  187. if (col[8*5]) {
  188. MAC16(b0, + W5, col[8*5]);
  189. MAC16(b1, - W1, col[8*5]);
  190. MAC16(b2, + W7, col[8*5]);
  191. MAC16(b3, + W3, col[8*5]);
  192. }
  193. if(col[8*6]){
  194. a0 += + W6*col[8*6];
  195. a1 += - W2*col[8*6];
  196. a2 += + W2*col[8*6];
  197. a3 += - W6*col[8*6];
  198. }
  199. if (col[8*7]) {
  200. MAC16(b0, + W7, col[8*7]);
  201. MAC16(b1, - W5, col[8*7]);
  202. MAC16(b2, + W3, col[8*7]);
  203. MAC16(b3, - W1, col[8*7]);
  204. }
  205. dest[0] = cm[(a0 + b0) >> COL_SHIFT];
  206. dest += line_size;
  207. dest[0] = cm[(a1 + b1) >> COL_SHIFT];
  208. dest += line_size;
  209. dest[0] = cm[(a2 + b2) >> COL_SHIFT];
  210. dest += line_size;
  211. dest[0] = cm[(a3 + b3) >> COL_SHIFT];
  212. dest += line_size;
  213. dest[0] = cm[(a3 - b3) >> COL_SHIFT];
  214. dest += line_size;
  215. dest[0] = cm[(a2 - b2) >> COL_SHIFT];
  216. dest += line_size;
  217. dest[0] = cm[(a1 - b1) >> COL_SHIFT];
  218. dest += line_size;
  219. dest[0] = cm[(a0 - b0) >> COL_SHIFT];
  220. }
  221. static inline void idctSparseColAdd (uint8_t *dest, int line_size,
  222. DCTELEM * col)
  223. {
  224. int a0, a1, a2, a3, b0, b1, b2, b3;
  225. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  226. /* XXX: I did that only to give same values as previous code */
  227. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
  228. a1 = a0;
  229. a2 = a0;
  230. a3 = a0;
  231. a0 += + W2*col[8*2];
  232. a1 += + W6*col[8*2];
  233. a2 += - W6*col[8*2];
  234. a3 += - W2*col[8*2];
  235. MUL16(b0, W1, col[8*1]);
  236. MUL16(b1, W3, col[8*1]);
  237. MUL16(b2, W5, col[8*1]);
  238. MUL16(b3, W7, col[8*1]);
  239. MAC16(b0, + W3, col[8*3]);
  240. MAC16(b1, - W7, col[8*3]);
  241. MAC16(b2, - W1, col[8*3]);
  242. MAC16(b3, - W5, col[8*3]);
  243. if(col[8*4]){
  244. a0 += + W4*col[8*4];
  245. a1 += - W4*col[8*4];
  246. a2 += - W4*col[8*4];
  247. a3 += + W4*col[8*4];
  248. }
  249. if (col[8*5]) {
  250. MAC16(b0, + W5, col[8*5]);
  251. MAC16(b1, - W1, col[8*5]);
  252. MAC16(b2, + W7, col[8*5]);
  253. MAC16(b3, + W3, col[8*5]);
  254. }
  255. if(col[8*6]){
  256. a0 += + W6*col[8*6];
  257. a1 += - W2*col[8*6];
  258. a2 += + W2*col[8*6];
  259. a3 += - W6*col[8*6];
  260. }
  261. if (col[8*7]) {
  262. MAC16(b0, + W7, col[8*7]);
  263. MAC16(b1, - W5, col[8*7]);
  264. MAC16(b2, + W3, col[8*7]);
  265. MAC16(b3, - W1, col[8*7]);
  266. }
  267. dest[0] = cm[dest[0] + ((a0 + b0) >> COL_SHIFT)];
  268. dest += line_size;
  269. dest[0] = cm[dest[0] + ((a1 + b1) >> COL_SHIFT)];
  270. dest += line_size;
  271. dest[0] = cm[dest[0] + ((a2 + b2) >> COL_SHIFT)];
  272. dest += line_size;
  273. dest[0] = cm[dest[0] + ((a3 + b3) >> COL_SHIFT)];
  274. dest += line_size;
  275. dest[0] = cm[dest[0] + ((a3 - b3) >> COL_SHIFT)];
  276. dest += line_size;
  277. dest[0] = cm[dest[0] + ((a2 - b2) >> COL_SHIFT)];
  278. dest += line_size;
  279. dest[0] = cm[dest[0] + ((a1 - b1) >> COL_SHIFT)];
  280. dest += line_size;
  281. dest[0] = cm[dest[0] + ((a0 - b0) >> COL_SHIFT)];
  282. }
  283. static inline void idctSparseCol (DCTELEM * col)
  284. {
  285. int a0, a1, a2, a3, b0, b1, b2, b3;
  286. /* XXX: I did that only to give same values as previous code */
  287. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
  288. a1 = a0;
  289. a2 = a0;
  290. a3 = a0;
  291. a0 += + W2*col[8*2];
  292. a1 += + W6*col[8*2];
  293. a2 += - W6*col[8*2];
  294. a3 += - W2*col[8*2];
  295. MUL16(b0, W1, col[8*1]);
  296. MUL16(b1, W3, col[8*1]);
  297. MUL16(b2, W5, col[8*1]);
  298. MUL16(b3, W7, col[8*1]);
  299. MAC16(b0, + W3, col[8*3]);
  300. MAC16(b1, - W7, col[8*3]);
  301. MAC16(b2, - W1, col[8*3]);
  302. MAC16(b3, - W5, col[8*3]);
  303. if(col[8*4]){
  304. a0 += + W4*col[8*4];
  305. a1 += - W4*col[8*4];
  306. a2 += - W4*col[8*4];
  307. a3 += + W4*col[8*4];
  308. }
  309. if (col[8*5]) {
  310. MAC16(b0, + W5, col[8*5]);
  311. MAC16(b1, - W1, col[8*5]);
  312. MAC16(b2, + W7, col[8*5]);
  313. MAC16(b3, + W3, col[8*5]);
  314. }
  315. if(col[8*6]){
  316. a0 += + W6*col[8*6];
  317. a1 += - W2*col[8*6];
  318. a2 += + W2*col[8*6];
  319. a3 += - W6*col[8*6];
  320. }
  321. if (col[8*7]) {
  322. MAC16(b0, + W7, col[8*7]);
  323. MAC16(b1, - W5, col[8*7]);
  324. MAC16(b2, + W3, col[8*7]);
  325. MAC16(b3, - W1, col[8*7]);
  326. }
  327. col[0 ] = ((a0 + b0) >> COL_SHIFT);
  328. col[8 ] = ((a1 + b1) >> COL_SHIFT);
  329. col[16] = ((a2 + b2) >> COL_SHIFT);
  330. col[24] = ((a3 + b3) >> COL_SHIFT);
  331. col[32] = ((a3 - b3) >> COL_SHIFT);
  332. col[40] = ((a2 - b2) >> COL_SHIFT);
  333. col[48] = ((a1 - b1) >> COL_SHIFT);
  334. col[56] = ((a0 - b0) >> COL_SHIFT);
  335. }
  336. void simple_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
  337. {
  338. int i;
  339. for(i=0; i<8; i++)
  340. idctRowCondDC(block + i*8);
  341. for(i=0; i<8; i++)
  342. idctSparseColPut(dest + i, line_size, block + i);
  343. }
  344. void simple_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
  345. {
  346. int i;
  347. for(i=0; i<8; i++)
  348. idctRowCondDC(block + i*8);
  349. for(i=0; i<8; i++)
  350. idctSparseColAdd(dest + i, line_size, block + i);
  351. }
  352. void simple_idct(DCTELEM *block)
  353. {
  354. int i;
  355. for(i=0; i<8; i++)
  356. idctRowCondDC(block + i*8);
  357. for(i=0; i<8; i++)
  358. idctSparseCol(block + i);
  359. }
  360. /* 2x4x8 idct */
  361. #define CN_SHIFT 12
  362. #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
  363. #define C1 C_FIX(0.6532814824)
  364. #define C2 C_FIX(0.2705980501)
  365. /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
  366. and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
  367. #define C_SHIFT (4+1+12)
  368. static inline void idct4col(uint8_t *dest, int line_size, const DCTELEM *col)
  369. {
  370. int c0, c1, c2, c3, a0, a1, a2, a3;
  371. const uint8_t *cm = cropTbl + MAX_NEG_CROP;
  372. a0 = col[8*0];
  373. a1 = col[8*2];
  374. a2 = col[8*4];
  375. a3 = col[8*6];
  376. c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  377. c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  378. c1 = a1 * C1 + a3 * C2;
  379. c3 = a1 * C2 - a3 * C1;
  380. dest[0] = cm[(c0 + c1) >> C_SHIFT];
  381. dest += line_size;
  382. dest[0] = cm[(c2 + c3) >> C_SHIFT];
  383. dest += line_size;
  384. dest[0] = cm[(c2 - c3) >> C_SHIFT];
  385. dest += line_size;
  386. dest[0] = cm[(c0 - c1) >> C_SHIFT];
  387. }
  388. #define BF(k) \
  389. {\
  390. int a0, a1;\
  391. a0 = ptr[k];\
  392. a1 = ptr[8 + k];\
  393. ptr[k] = a0 + a1;\
  394. ptr[8 + k] = a0 - a1;\
  395. }
  396. /* only used by DV codec. The input must be interlaced. 128 is added
  397. to the pixels before clamping to avoid systematic error
  398. (1024*sqrt(2)) offset would be needed otherwise. */
  399. /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
  400. compensate the extra butterfly stage - I don't have the full DV
  401. specification */
  402. void simple_idct248_put(uint8_t *dest, int line_size, DCTELEM *block)
  403. {
  404. int i;
  405. DCTELEM *ptr;
  406. /* butterfly */
  407. ptr = block;
  408. for(i=0;i<4;i++) {
  409. BF(0);
  410. BF(1);
  411. BF(2);
  412. BF(3);
  413. BF(4);
  414. BF(5);
  415. BF(6);
  416. BF(7);
  417. ptr += 2 * 8;
  418. }
  419. /* IDCT8 on each line */
  420. for(i=0; i<8; i++) {
  421. idctRowCondDC(block + i*8);
  422. }
  423. /* IDCT4 and store */
  424. for(i=0;i<8;i++) {
  425. idct4col(dest + i, 2 * line_size, block + i);
  426. idct4col(dest + line_size + i, 2 * line_size, block + 8 + i);
  427. }
  428. }
  429. /* 8x4 & 4x8 WMV2 IDCT */
  430. #undef CN_SHIFT
  431. #undef C_SHIFT
  432. #undef C_FIX
  433. #undef C1
  434. #undef C2
  435. #define CN_SHIFT 12
  436. #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
  437. #define C1 C_FIX(0.6532814824)
  438. #define C2 C_FIX(0.2705980501)
  439. #define C3 C_FIX(0.5)
  440. #define C_SHIFT (4+1+12)
  441. static inline void idct4col_add(uint8_t *dest, int line_size, const DCTELEM *col)
  442. {
  443. int c0, c1, c2, c3, a0, a1, a2, a3;
  444. const uint8_t *cm = cropTbl + MAX_NEG_CROP;
  445. a0 = col[8*0];
  446. a1 = col[8*1];
  447. a2 = col[8*2];
  448. a3 = col[8*3];
  449. c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
  450. c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
  451. c1 = a1 * C1 + a3 * C2;
  452. c3 = a1 * C2 - a3 * C1;
  453. dest[0] = cm[dest[0] + ((c0 + c1) >> C_SHIFT)];
  454. dest += line_size;
  455. dest[0] = cm[dest[0] + ((c2 + c3) >> C_SHIFT)];
  456. dest += line_size;
  457. dest[0] = cm[dest[0] + ((c2 - c3) >> C_SHIFT)];
  458. dest += line_size;
  459. dest[0] = cm[dest[0] + ((c0 - c1) >> C_SHIFT)];
  460. }
  461. #define RN_SHIFT 15
  462. #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
  463. #define R1 R_FIX(0.6532814824)
  464. #define R2 R_FIX(0.2705980501)
  465. #define R3 R_FIX(0.5)
  466. #define R_SHIFT 11
  467. static inline void idct4row(DCTELEM *row)
  468. {
  469. int c0, c1, c2, c3, a0, a1, a2, a3;
  470. //const uint8_t *cm = cropTbl + MAX_NEG_CROP;
  471. a0 = row[0];
  472. a1 = row[1];
  473. a2 = row[2];
  474. a3 = row[3];
  475. c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
  476. c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
  477. c1 = a1 * R1 + a3 * R2;
  478. c3 = a1 * R2 - a3 * R1;
  479. row[0]= (c0 + c1) >> R_SHIFT;
  480. row[1]= (c2 + c3) >> R_SHIFT;
  481. row[2]= (c2 - c3) >> R_SHIFT;
  482. row[3]= (c0 - c1) >> R_SHIFT;
  483. }
  484. void simple_idct84_add(uint8_t *dest, int line_size, DCTELEM *block)
  485. {
  486. int i;
  487. /* IDCT8 on each line */
  488. for(i=0; i<4; i++) {
  489. idctRowCondDC(block + i*8);
  490. }
  491. /* IDCT4 and store */
  492. for(i=0;i<8;i++) {
  493. idct4col_add(dest + i, line_size, block + i);
  494. }
  495. }
  496. void simple_idct48_add(uint8_t *dest, int line_size, DCTELEM *block)
  497. {
  498. int i;
  499. /* IDCT4 on each line */
  500. for(i=0; i<8; i++) {
  501. idct4row(block + i*8);
  502. }
  503. /* IDCT8 and store */
  504. for(i=0; i<4; i++){
  505. idctSparseColAdd(dest + i, line_size, block + i);
  506. }
  507. }