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.

556 lines
15KB

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