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.

837 lines
27KB

  1. /*
  2. * DSP functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
  3. *
  4. * Copyright (c) 2009-2011 Maxim Poliakovski
  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. * DSP functions (inverse transforms, motion compensation, wavelet recompositions)
  25. * for Indeo Video Interactive codecs.
  26. */
  27. #include "avcodec.h"
  28. #include "ivi.h"
  29. #include "ivi_dsp.h"
  30. void ff_ivi_recompose53(const IVIPlaneDesc *plane, uint8_t *dst,
  31. const ptrdiff_t dst_pitch)
  32. {
  33. int x, y, indx;
  34. int32_t p0, p1, p2, p3, tmp0, tmp1, tmp2;
  35. int32_t b0_1, b0_2, b1_1, b1_2, b1_3, b2_1, b2_2, b2_3, b2_4, b2_5, b2_6;
  36. int32_t b3_1, b3_2, b3_3, b3_4, b3_5, b3_6, b3_7, b3_8, b3_9;
  37. ptrdiff_t pitch, back_pitch;
  38. const short *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
  39. const int num_bands = 4;
  40. /* all bands should have the same pitch */
  41. pitch = plane->bands[0].pitch;
  42. /* pixels at the position "y-1" will be set to pixels at the "y" for the 1st iteration */
  43. back_pitch = 0;
  44. /* get pointers to the wavelet bands */
  45. b0_ptr = plane->bands[0].buf;
  46. b1_ptr = plane->bands[1].buf;
  47. b2_ptr = plane->bands[2].buf;
  48. b3_ptr = plane->bands[3].buf;
  49. for (y = 0; y < plane->height; y += 2) {
  50. /* load storage variables with values */
  51. if (num_bands > 0) {
  52. b0_1 = b0_ptr[0];
  53. b0_2 = b0_ptr[pitch];
  54. }
  55. if (num_bands > 1) {
  56. b1_1 = b1_ptr[back_pitch];
  57. b1_2 = b1_ptr[0];
  58. b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch];
  59. }
  60. if (num_bands > 2) {
  61. b2_2 = b2_ptr[0]; // b2[x, y ]
  62. b2_3 = b2_2; // b2[x+1,y ] = b2[x,y]
  63. b2_5 = b2_ptr[pitch]; // b2[x ,y+1]
  64. b2_6 = b2_5; // b2[x+1,y+1] = b2[x,y+1]
  65. }
  66. if (num_bands > 3) {
  67. b3_2 = b3_ptr[back_pitch]; // b3[x ,y-1]
  68. b3_3 = b3_2; // b3[x+1,y-1] = b3[x ,y-1]
  69. b3_5 = b3_ptr[0]; // b3[x ,y ]
  70. b3_6 = b3_5; // b3[x+1,y ] = b3[x ,y ]
  71. b3_8 = b3_2 - b3_5*6 + b3_ptr[pitch];
  72. b3_9 = b3_8;
  73. }
  74. for (x = 0, indx = 0; x < plane->width; x+=2, indx++) {
  75. /* some values calculated in the previous iterations can */
  76. /* be reused in the next ones, so do appropriate copying */
  77. b2_1 = b2_2; // b2[x-1,y ] = b2[x, y ]
  78. b2_2 = b2_3; // b2[x ,y ] = b2[x+1,y ]
  79. b2_4 = b2_5; // b2[x-1,y+1] = b2[x ,y+1]
  80. b2_5 = b2_6; // b2[x ,y+1] = b2[x+1,y+1]
  81. b3_1 = b3_2; // b3[x-1,y-1] = b3[x ,y-1]
  82. b3_2 = b3_3; // b3[x ,y-1] = b3[x+1,y-1]
  83. b3_4 = b3_5; // b3[x-1,y ] = b3[x ,y ]
  84. b3_5 = b3_6; // b3[x ,y ] = b3[x+1,y ]
  85. b3_7 = b3_8; // vert_HPF(x-1)
  86. b3_8 = b3_9; // vert_HPF(x )
  87. p0 = p1 = p2 = p3 = 0;
  88. /* process the LL-band by applying LPF both vertically and horizontally */
  89. if (num_bands > 0) {
  90. tmp0 = b0_1;
  91. tmp2 = b0_2;
  92. b0_1 = b0_ptr[indx+1];
  93. b0_2 = b0_ptr[pitch+indx+1];
  94. tmp1 = tmp0 + b0_1;
  95. p0 = tmp0 << 4;
  96. p1 = tmp1 << 3;
  97. p2 = (tmp0 + tmp2) << 3;
  98. p3 = (tmp1 + tmp2 + b0_2) << 2;
  99. }
  100. /* process the HL-band by applying HPF vertically and LPF horizontally */
  101. if (num_bands > 1) {
  102. tmp0 = b1_2;
  103. tmp1 = b1_1;
  104. b1_2 = b1_ptr[indx+1];
  105. b1_1 = b1_ptr[back_pitch+indx+1];
  106. tmp2 = tmp1 - tmp0*6 + b1_3;
  107. b1_3 = b1_1 - b1_2*6 + b1_ptr[pitch+indx+1];
  108. p0 += (tmp0 + tmp1) << 3;
  109. p1 += (tmp0 + tmp1 + b1_1 + b1_2) << 2;
  110. p2 += tmp2 << 2;
  111. p3 += (tmp2 + b1_3) << 1;
  112. }
  113. /* process the LH-band by applying LPF vertically and HPF horizontally */
  114. if (num_bands > 2) {
  115. b2_3 = b2_ptr[indx+1];
  116. b2_6 = b2_ptr[pitch+indx+1];
  117. tmp0 = b2_1 + b2_2;
  118. tmp1 = b2_1 - b2_2*6 + b2_3;
  119. p0 += tmp0 << 3;
  120. p1 += tmp1 << 2;
  121. p2 += (tmp0 + b2_4 + b2_5) << 2;
  122. p3 += (tmp1 + b2_4 - b2_5*6 + b2_6) << 1;
  123. }
  124. /* process the HH-band by applying HPF both vertically and horizontally */
  125. if (num_bands > 3) {
  126. b3_6 = b3_ptr[indx+1]; // b3[x+1,y ]
  127. b3_3 = b3_ptr[back_pitch+indx+1]; // b3[x+1,y-1]
  128. tmp0 = b3_1 + b3_4;
  129. tmp1 = b3_2 + b3_5;
  130. tmp2 = b3_3 + b3_6;
  131. b3_9 = b3_3 - b3_6*6 + b3_ptr[pitch+indx+1];
  132. p0 += (tmp0 + tmp1) << 2;
  133. p1 += (tmp0 - tmp1*6 + tmp2) << 1;
  134. p2 += (b3_7 + b3_8) << 1;
  135. p3 += b3_7 - b3_8*6 + b3_9;
  136. }
  137. /* output four pixels */
  138. dst[x] = av_clip_uint8((p0 >> 6) + 128);
  139. dst[x+1] = av_clip_uint8((p1 >> 6) + 128);
  140. dst[dst_pitch+x] = av_clip_uint8((p2 >> 6) + 128);
  141. dst[dst_pitch+x+1] = av_clip_uint8((p3 >> 6) + 128);
  142. }// for x
  143. dst += dst_pitch << 1;
  144. back_pitch = -pitch;
  145. b0_ptr += pitch;
  146. b1_ptr += pitch;
  147. b2_ptr += pitch;
  148. b3_ptr += pitch;
  149. }
  150. }
  151. void ff_ivi_recompose_haar(const IVIPlaneDesc *plane, uint8_t *dst,
  152. const ptrdiff_t dst_pitch)
  153. {
  154. int x, y, indx, b0, b1, b2, b3, p0, p1, p2, p3;
  155. const short *b0_ptr, *b1_ptr, *b2_ptr, *b3_ptr;
  156. ptrdiff_t pitch;
  157. /* all bands should have the same pitch */
  158. pitch = plane->bands[0].pitch;
  159. /* get pointers to the wavelet bands */
  160. b0_ptr = plane->bands[0].buf;
  161. b1_ptr = plane->bands[1].buf;
  162. b2_ptr = plane->bands[2].buf;
  163. b3_ptr = plane->bands[3].buf;
  164. for (y = 0; y < plane->height; y += 2) {
  165. for (x = 0, indx = 0; x < plane->width; x += 2, indx++) {
  166. /* load coefficients */
  167. b0 = b0_ptr[indx]; //should be: b0 = (num_bands > 0) ? b0_ptr[indx] : 0;
  168. b1 = b1_ptr[indx]; //should be: b1 = (num_bands > 1) ? b1_ptr[indx] : 0;
  169. b2 = b2_ptr[indx]; //should be: b2 = (num_bands > 2) ? b2_ptr[indx] : 0;
  170. b3 = b3_ptr[indx]; //should be: b3 = (num_bands > 3) ? b3_ptr[indx] : 0;
  171. /* haar wavelet recomposition */
  172. p0 = (b0 + b1 + b2 + b3 + 2) >> 2;
  173. p1 = (b0 + b1 - b2 - b3 + 2) >> 2;
  174. p2 = (b0 - b1 + b2 - b3 + 2) >> 2;
  175. p3 = (b0 - b1 - b2 + b3 + 2) >> 2;
  176. /* bias, convert and output four pixels */
  177. dst[x] = av_clip_uint8(p0 + 128);
  178. dst[x + 1] = av_clip_uint8(p1 + 128);
  179. dst[dst_pitch + x] = av_clip_uint8(p2 + 128);
  180. dst[dst_pitch + x + 1] = av_clip_uint8(p3 + 128);
  181. }// for x
  182. dst += dst_pitch << 1;
  183. b0_ptr += pitch;
  184. b1_ptr += pitch;
  185. b2_ptr += pitch;
  186. b3_ptr += pitch;
  187. }// for y
  188. }
  189. /** butterfly operation for the inverse Haar transform */
  190. #define IVI_HAAR_BFLY(s1, s2, o1, o2, t) \
  191. t = (s1 - s2) >> 1;\
  192. o1 = (s1 + s2) >> 1;\
  193. o2 = t;\
  194. /** inverse 8-point Haar transform */
  195. #define INV_HAAR8(s1, s5, s3, s7, s2, s4, s6, s8,\
  196. d1, d2, d3, d4, d5, d6, d7, d8,\
  197. t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
  198. t1 = s1 << 1; t5 = s5 << 1;\
  199. IVI_HAAR_BFLY(t1, t5, t1, t5, t0); IVI_HAAR_BFLY(t1, s3, t1, t3, t0);\
  200. IVI_HAAR_BFLY(t5, s7, t5, t7, t0); IVI_HAAR_BFLY(t1, s2, t1, t2, t0);\
  201. IVI_HAAR_BFLY(t3, s4, t3, t4, t0); IVI_HAAR_BFLY(t5, s6, t5, t6, t0);\
  202. IVI_HAAR_BFLY(t7, s8, t7, t8, t0);\
  203. d1 = COMPENSATE(t1);\
  204. d2 = COMPENSATE(t2);\
  205. d3 = COMPENSATE(t3);\
  206. d4 = COMPENSATE(t4);\
  207. d5 = COMPENSATE(t5);\
  208. d6 = COMPENSATE(t6);\
  209. d7 = COMPENSATE(t7);\
  210. d8 = COMPENSATE(t8); }
  211. /** inverse 4-point Haar transform */
  212. #define INV_HAAR4(s1, s3, s5, s7, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
  213. IVI_HAAR_BFLY(s1, s3, t0, t1, t4);\
  214. IVI_HAAR_BFLY(t0, s5, t2, t3, t4);\
  215. d1 = COMPENSATE(t2);\
  216. d2 = COMPENSATE(t3);\
  217. IVI_HAAR_BFLY(t1, s7, t2, t3, t4);\
  218. d3 = COMPENSATE(t2);\
  219. d4 = COMPENSATE(t3); }
  220. void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  221. const uint8_t *flags)
  222. {
  223. int i, shift, sp1, sp2, sp3, sp4;
  224. const int32_t *src;
  225. int32_t *dst;
  226. int tmp[64];
  227. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  228. /* apply the InvHaar8 to all columns */
  229. #define COMPENSATE(x) (x)
  230. src = in;
  231. dst = tmp;
  232. for (i = 0; i < 8; i++) {
  233. if (flags[i]) {
  234. /* pre-scaling */
  235. shift = !(i & 4);
  236. sp1 = src[ 0] << shift;
  237. sp2 = src[ 8] << shift;
  238. sp3 = src[16] << shift;
  239. sp4 = src[24] << shift;
  240. INV_HAAR8( sp1, sp2, sp3, sp4,
  241. src[32], src[40], src[48], src[56],
  242. dst[ 0], dst[ 8], dst[16], dst[24],
  243. dst[32], dst[40], dst[48], dst[56],
  244. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  245. } else
  246. dst[ 0] = dst[ 8] = dst[16] = dst[24] =
  247. dst[32] = dst[40] = dst[48] = dst[56] = 0;
  248. src++;
  249. dst++;
  250. }
  251. #undef COMPENSATE
  252. /* apply the InvHaar8 to all rows */
  253. #define COMPENSATE(x) (x)
  254. src = tmp;
  255. for (i = 0; i < 8; i++) {
  256. if ( !src[0] && !src[1] && !src[2] && !src[3]
  257. && !src[4] && !src[5] && !src[6] && !src[7]) {
  258. memset(out, 0, 8 * sizeof(out[0]));
  259. } else {
  260. INV_HAAR8(src[0], src[1], src[2], src[3],
  261. src[4], src[5], src[6], src[7],
  262. out[0], out[1], out[2], out[3],
  263. out[4], out[5], out[6], out[7],
  264. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  265. }
  266. src += 8;
  267. out += pitch;
  268. }
  269. #undef COMPENSATE
  270. }
  271. void ff_ivi_row_haar8(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  272. const uint8_t *flags)
  273. {
  274. int i;
  275. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  276. /* apply the InvHaar8 to all rows */
  277. #define COMPENSATE(x) (x)
  278. for (i = 0; i < 8; i++) {
  279. if ( !in[0] && !in[1] && !in[2] && !in[3]
  280. && !in[4] && !in[5] && !in[6] && !in[7]) {
  281. memset(out, 0, 8 * sizeof(out[0]));
  282. } else {
  283. INV_HAAR8(in[0], in[1], in[2], in[3],
  284. in[4], in[5], in[6], in[7],
  285. out[0], out[1], out[2], out[3],
  286. out[4], out[5], out[6], out[7],
  287. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  288. }
  289. in += 8;
  290. out += pitch;
  291. }
  292. #undef COMPENSATE
  293. }
  294. void ff_ivi_col_haar8(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  295. const uint8_t *flags)
  296. {
  297. int i;
  298. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  299. /* apply the InvHaar8 to all columns */
  300. #define COMPENSATE(x) (x)
  301. for (i = 0; i < 8; i++) {
  302. if (flags[i]) {
  303. INV_HAAR8(in[ 0], in[ 8], in[16], in[24],
  304. in[32], in[40], in[48], in[56],
  305. out[0 * pitch], out[1 * pitch],
  306. out[2 * pitch], out[3 * pitch],
  307. out[4 * pitch], out[5 * pitch],
  308. out[6 * pitch], out[7 * pitch],
  309. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  310. } else
  311. out[0 * pitch] = out[1 * pitch] =
  312. out[2 * pitch] = out[3 * pitch] =
  313. out[4 * pitch] = out[5 * pitch] =
  314. out[6 * pitch] = out[7 * pitch] = 0;
  315. in++;
  316. out++;
  317. }
  318. #undef COMPENSATE
  319. }
  320. void ff_ivi_inverse_haar_4x4(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  321. const uint8_t *flags)
  322. {
  323. int i, shift, sp1, sp2;
  324. const int32_t *src;
  325. int32_t *dst;
  326. int tmp[16];
  327. int t0, t1, t2, t3, t4;
  328. /* apply the InvHaar4 to all columns */
  329. #define COMPENSATE(x) (x)
  330. src = in;
  331. dst = tmp;
  332. for (i = 0; i < 4; i++) {
  333. if (flags[i]) {
  334. /* pre-scaling */
  335. shift = !(i & 2);
  336. sp1 = src[0] << shift;
  337. sp2 = src[4] << shift;
  338. INV_HAAR4( sp1, sp2, src[8], src[12],
  339. dst[0], dst[4], dst[8], dst[12],
  340. t0, t1, t2, t3, t4);
  341. } else
  342. dst[0] = dst[4] = dst[8] = dst[12] = 0;
  343. src++;
  344. dst++;
  345. }
  346. #undef COMPENSATE
  347. /* apply the InvHaar8 to all rows */
  348. #define COMPENSATE(x) (x)
  349. src = tmp;
  350. for (i = 0; i < 4; i++) {
  351. if (!src[0] && !src[1] && !src[2] && !src[3]) {
  352. memset(out, 0, 4 * sizeof(out[0]));
  353. } else {
  354. INV_HAAR4(src[0], src[1], src[2], src[3],
  355. out[0], out[1], out[2], out[3],
  356. t0, t1, t2, t3, t4);
  357. }
  358. src += 4;
  359. out += pitch;
  360. }
  361. #undef COMPENSATE
  362. }
  363. void ff_ivi_row_haar4(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  364. const uint8_t *flags)
  365. {
  366. int i;
  367. int t0, t1, t2, t3, t4;
  368. /* apply the InvHaar4 to all rows */
  369. #define COMPENSATE(x) (x)
  370. for (i = 0; i < 4; i++) {
  371. if (!in[0] && !in[1] && !in[2] && !in[3]) {
  372. memset(out, 0, 4 * sizeof(out[0]));
  373. } else {
  374. INV_HAAR4(in[0], in[1], in[2], in[3],
  375. out[0], out[1], out[2], out[3],
  376. t0, t1, t2, t3, t4);
  377. }
  378. in += 4;
  379. out += pitch;
  380. }
  381. #undef COMPENSATE
  382. }
  383. void ff_ivi_col_haar4(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  384. const uint8_t *flags)
  385. {
  386. int i;
  387. int t0, t1, t2, t3, t4;
  388. /* apply the InvHaar8 to all columns */
  389. #define COMPENSATE(x) (x)
  390. for (i = 0; i < 4; i++) {
  391. if (flags[i]) {
  392. INV_HAAR4(in[0], in[4], in[8], in[12],
  393. out[0 * pitch], out[1 * pitch],
  394. out[2 * pitch], out[3 * pitch],
  395. t0, t1, t2, t3, t4);
  396. } else
  397. out[0 * pitch] = out[1 * pitch] =
  398. out[2 * pitch] = out[3 * pitch] = 0;
  399. in++;
  400. out++;
  401. }
  402. #undef COMPENSATE
  403. }
  404. void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  405. int blk_size)
  406. {
  407. int x, y;
  408. int16_t dc_coeff;
  409. dc_coeff = (*in + 0) >> 3;
  410. for (y = 0; y < blk_size; out += pitch, y++) {
  411. for (x = 0; x < blk_size; x++)
  412. out[x] = dc_coeff;
  413. }
  414. }
  415. /** butterfly operation for the inverse slant transform */
  416. #define IVI_SLANT_BFLY(s1, s2, o1, o2, t) \
  417. t = s1 - s2;\
  418. o1 = s1 + s2;\
  419. o2 = t;\
  420. /** This is a reflection a,b = 1/2, 5/4 for the inverse slant transform */
  421. #define IVI_IREFLECT(s1, s2, o1, o2, t) \
  422. t = ((s1 + s2*2 + 2) >> 2) + s1;\
  423. o2 = ((s1*2 - s2 + 2) >> 2) - s2;\
  424. o1 = t;\
  425. /** This is a reflection a,b = 1/2, 7/8 for the inverse slant transform */
  426. #define IVI_SLANT_PART4(s1, s2, o1, o2, t) \
  427. t = s2 + ((s1*4 - s2 + 4) >> 3);\
  428. o2 = s1 + ((-s1 - s2*4 + 4) >> 3);\
  429. o1 = t;\
  430. /** inverse slant8 transform */
  431. #define IVI_INV_SLANT8(s1, s4, s8, s5, s2, s6, s3, s7,\
  432. d1, d2, d3, d4, d5, d6, d7, d8,\
  433. t0, t1, t2, t3, t4, t5, t6, t7, t8) {\
  434. IVI_SLANT_PART4(s4, s5, t4, t5, t0);\
  435. \
  436. IVI_SLANT_BFLY(s1, t5, t1, t5, t0); IVI_SLANT_BFLY(s2, s6, t2, t6, t0);\
  437. IVI_SLANT_BFLY(s7, s3, t7, t3, t0); IVI_SLANT_BFLY(t4, s8, t4, t8, t0);\
  438. \
  439. IVI_SLANT_BFLY(t1, t2, t1, t2, t0); IVI_IREFLECT (t4, t3, t4, t3, t0);\
  440. IVI_SLANT_BFLY(t5, t6, t5, t6, t0); IVI_IREFLECT (t8, t7, t8, t7, t0);\
  441. IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
  442. IVI_SLANT_BFLY(t5, t8, t5, t8, t0); IVI_SLANT_BFLY(t6, t7, t6, t7, t0);\
  443. d1 = COMPENSATE(t1);\
  444. d2 = COMPENSATE(t2);\
  445. d3 = COMPENSATE(t3);\
  446. d4 = COMPENSATE(t4);\
  447. d5 = COMPENSATE(t5);\
  448. d6 = COMPENSATE(t6);\
  449. d7 = COMPENSATE(t7);\
  450. d8 = COMPENSATE(t8);}
  451. /** inverse slant4 transform */
  452. #define IVI_INV_SLANT4(s1, s4, s2, s3, d1, d2, d3, d4, t0, t1, t2, t3, t4) {\
  453. IVI_SLANT_BFLY(s1, s2, t1, t2, t0); IVI_IREFLECT (s4, s3, t4, t3, t0);\
  454. \
  455. IVI_SLANT_BFLY(t1, t4, t1, t4, t0); IVI_SLANT_BFLY(t2, t3, t2, t3, t0);\
  456. d1 = COMPENSATE(t1);\
  457. d2 = COMPENSATE(t2);\
  458. d3 = COMPENSATE(t3);\
  459. d4 = COMPENSATE(t4);}
  460. void ff_ivi_inverse_slant_8x8(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  461. {
  462. int i;
  463. const int32_t *src;
  464. int32_t *dst;
  465. int tmp[64];
  466. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  467. #define COMPENSATE(x) (x)
  468. src = in;
  469. dst = tmp;
  470. for (i = 0; i < 8; i++) {
  471. if (flags[i]) {
  472. IVI_INV_SLANT8(src[0], src[8], src[16], src[24], src[32], src[40], src[48], src[56],
  473. dst[0], dst[8], dst[16], dst[24], dst[32], dst[40], dst[48], dst[56],
  474. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  475. } else
  476. dst[0] = dst[8] = dst[16] = dst[24] = dst[32] = dst[40] = dst[48] = dst[56] = 0;
  477. src++;
  478. dst++;
  479. }
  480. #undef COMPENSATE
  481. #define COMPENSATE(x) ((x + 1)>>1)
  482. src = tmp;
  483. for (i = 0; i < 8; i++) {
  484. if (!src[0] && !src[1] && !src[2] && !src[3] && !src[4] && !src[5] && !src[6] && !src[7]) {
  485. memset(out, 0, 8*sizeof(out[0]));
  486. } else {
  487. IVI_INV_SLANT8(src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
  488. out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
  489. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  490. }
  491. src += 8;
  492. out += pitch;
  493. }
  494. #undef COMPENSATE
  495. }
  496. void ff_ivi_inverse_slant_4x4(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  497. {
  498. int i;
  499. const int32_t *src;
  500. int32_t *dst;
  501. int tmp[16];
  502. int t0, t1, t2, t3, t4;
  503. #define COMPENSATE(x) (x)
  504. src = in;
  505. dst = tmp;
  506. for (i = 0; i < 4; i++) {
  507. if (flags[i]) {
  508. IVI_INV_SLANT4(src[0], src[4], src[8], src[12],
  509. dst[0], dst[4], dst[8], dst[12],
  510. t0, t1, t2, t3, t4);
  511. } else
  512. dst[0] = dst[4] = dst[8] = dst[12] = 0;
  513. src++;
  514. dst++;
  515. }
  516. #undef COMPENSATE
  517. #define COMPENSATE(x) ((x + 1)>>1)
  518. src = tmp;
  519. for (i = 0; i < 4; i++) {
  520. if (!src[0] && !src[1] && !src[2] && !src[3]) {
  521. out[0] = out[1] = out[2] = out[3] = 0;
  522. } else {
  523. IVI_INV_SLANT4(src[0], src[1], src[2], src[3],
  524. out[0], out[1], out[2], out[3],
  525. t0, t1, t2, t3, t4);
  526. }
  527. src += 4;
  528. out += pitch;
  529. }
  530. #undef COMPENSATE
  531. }
  532. void ff_ivi_dc_slant_2d(const int32_t *in, int16_t *out, ptrdiff_t pitch, int blk_size)
  533. {
  534. int x, y;
  535. int16_t dc_coeff;
  536. dc_coeff = (*in + 1) >> 1;
  537. for (y = 0; y < blk_size; out += pitch, y++) {
  538. for (x = 0; x < blk_size; x++)
  539. out[x] = dc_coeff;
  540. }
  541. }
  542. void ff_ivi_row_slant8(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  543. {
  544. int i;
  545. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  546. #define COMPENSATE(x) ((x + 1)>>1)
  547. for (i = 0; i < 8; i++) {
  548. if (!in[0] && !in[1] && !in[2] && !in[3] && !in[4] && !in[5] && !in[6] && !in[7]) {
  549. memset(out, 0, 8*sizeof(out[0]));
  550. } else {
  551. IVI_INV_SLANT8( in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7],
  552. out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7],
  553. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  554. }
  555. in += 8;
  556. out += pitch;
  557. }
  558. #undef COMPENSATE
  559. }
  560. void ff_ivi_dc_row_slant(const int32_t *in, int16_t *out, ptrdiff_t pitch, int blk_size)
  561. {
  562. int x, y;
  563. int16_t dc_coeff;
  564. dc_coeff = (*in + 1) >> 1;
  565. for (x = 0; x < blk_size; x++)
  566. out[x] = dc_coeff;
  567. out += pitch;
  568. for (y = 1; y < blk_size; out += pitch, y++) {
  569. for (x = 0; x < blk_size; x++)
  570. out[x] = 0;
  571. }
  572. }
  573. void ff_ivi_col_slant8(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  574. {
  575. int i, row2, row4, row8;
  576. int t0, t1, t2, t3, t4, t5, t6, t7, t8;
  577. row2 = pitch << 1;
  578. row4 = pitch << 2;
  579. row8 = pitch << 3;
  580. #define COMPENSATE(x) ((x + 1)>>1)
  581. for (i = 0; i < 8; i++) {
  582. if (flags[i]) {
  583. IVI_INV_SLANT8(in[0], in[8], in[16], in[24], in[32], in[40], in[48], in[56],
  584. out[0], out[pitch], out[row2], out[row2 + pitch], out[row4],
  585. out[row4 + pitch], out[row4 + row2], out[row8 - pitch],
  586. t0, t1, t2, t3, t4, t5, t6, t7, t8);
  587. } else {
  588. out[0] = out[pitch] = out[row2] = out[row2 + pitch] = out[row4] =
  589. out[row4 + pitch] = out[row4 + row2] = out[row8 - pitch] = 0;
  590. }
  591. in++;
  592. out++;
  593. }
  594. #undef COMPENSATE
  595. }
  596. void ff_ivi_dc_col_slant(const int32_t *in, int16_t *out, ptrdiff_t pitch, int blk_size)
  597. {
  598. int x, y;
  599. int16_t dc_coeff;
  600. dc_coeff = (*in + 1) >> 1;
  601. for (y = 0; y < blk_size; out += pitch, y++) {
  602. out[0] = dc_coeff;
  603. for (x = 1; x < blk_size; x++)
  604. out[x] = 0;
  605. }
  606. }
  607. void ff_ivi_row_slant4(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  608. {
  609. int i;
  610. int t0, t1, t2, t3, t4;
  611. #define COMPENSATE(x) ((x + 1)>>1)
  612. for (i = 0; i < 4; i++) {
  613. if (!in[0] && !in[1] && !in[2] && !in[3]) {
  614. memset(out, 0, 4*sizeof(out[0]));
  615. } else {
  616. IVI_INV_SLANT4( in[0], in[1], in[2], in[3],
  617. out[0], out[1], out[2], out[3],
  618. t0, t1, t2, t3, t4);
  619. }
  620. in += 4;
  621. out += pitch;
  622. }
  623. #undef COMPENSATE
  624. }
  625. void ff_ivi_col_slant4(const int32_t *in, int16_t *out, ptrdiff_t pitch, const uint8_t *flags)
  626. {
  627. int i, row2;
  628. int t0, t1, t2, t3, t4;
  629. row2 = pitch << 1;
  630. #define COMPENSATE(x) ((x + 1)>>1)
  631. for (i = 0; i < 4; i++) {
  632. if (flags[i]) {
  633. IVI_INV_SLANT4(in[0], in[4], in[8], in[12],
  634. out[0], out[pitch], out[row2], out[row2 + pitch],
  635. t0, t1, t2, t3, t4);
  636. } else {
  637. out[0] = out[pitch] = out[row2] = out[row2 + pitch] = 0;
  638. }
  639. in++;
  640. out++;
  641. }
  642. #undef COMPENSATE
  643. }
  644. void ff_ivi_put_pixels_8x8(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  645. const uint8_t *flags)
  646. {
  647. int x, y;
  648. for (y = 0; y < 8; out += pitch, in += 8, y++)
  649. for (x = 0; x < 8; x++)
  650. out[x] = in[x];
  651. }
  652. void ff_ivi_put_dc_pixel_8x8(const int32_t *in, int16_t *out, ptrdiff_t pitch,
  653. int blk_size)
  654. {
  655. int y;
  656. out[0] = in[0];
  657. memset(out + 1, 0, 7*sizeof(out[0]));
  658. out += pitch;
  659. for (y = 1; y < 8; out += pitch, y++)
  660. memset(out, 0, 8*sizeof(out[0]));
  661. }
  662. #define IVI_MC_TEMPLATE(size, suffix, OP) \
  663. static void ivi_mc_ ## size ##x## size ## suffix(int16_t *buf, \
  664. ptrdiff_t dpitch, \
  665. const int16_t *ref_buf, \
  666. ptrdiff_t pitch, int mc_type) \
  667. { \
  668. int i, j; \
  669. const int16_t *wptr; \
  670. \
  671. switch (mc_type) { \
  672. case 0: /* fullpel (no interpolation) */ \
  673. for (i = 0; i < size; i++, buf += dpitch, ref_buf += pitch) { \
  674. for (j = 0; j < size; j++) {\
  675. OP(buf[j], ref_buf[j]); \
  676. } \
  677. } \
  678. break; \
  679. case 1: /* horizontal halfpel interpolation */ \
  680. for (i = 0; i < size; i++, buf += dpitch, ref_buf += pitch) \
  681. for (j = 0; j < size; j++) \
  682. OP(buf[j], (ref_buf[j] + ref_buf[j+1]) >> 1); \
  683. break; \
  684. case 2: /* vertical halfpel interpolation */ \
  685. wptr = ref_buf + pitch; \
  686. for (i = 0; i < size; i++, buf += dpitch, wptr += pitch, ref_buf += pitch) \
  687. for (j = 0; j < size; j++) \
  688. OP(buf[j], (ref_buf[j] + wptr[j]) >> 1); \
  689. break; \
  690. case 3: /* vertical and horizontal halfpel interpolation */ \
  691. wptr = ref_buf + pitch; \
  692. for (i = 0; i < size; i++, buf += dpitch, wptr += pitch, ref_buf += pitch) \
  693. for (j = 0; j < size; j++) \
  694. OP(buf[j], (ref_buf[j] + ref_buf[j+1] + wptr[j] + wptr[j+1]) >> 2); \
  695. break; \
  696. } \
  697. } \
  698. \
  699. void ff_ivi_mc_ ## size ##x## size ## suffix(int16_t *buf, const int16_t *ref_buf, \
  700. ptrdiff_t pitch, int mc_type) \
  701. { \
  702. ivi_mc_ ## size ##x## size ## suffix(buf, pitch, ref_buf, pitch, mc_type); \
  703. } \
  704. #define IVI_MC_AVG_TEMPLATE(size, suffix, OP) \
  705. void ff_ivi_mc_avg_ ## size ##x## size ## suffix(int16_t *buf, \
  706. const int16_t *ref_buf, \
  707. const int16_t *ref_buf2, \
  708. ptrdiff_t pitch, \
  709. int mc_type, int mc_type2) \
  710. { \
  711. int16_t tmp[size * size]; \
  712. int i, j; \
  713. \
  714. ivi_mc_ ## size ##x## size ## _no_delta(tmp, size, ref_buf, pitch, mc_type); \
  715. ivi_mc_ ## size ##x## size ## _delta(tmp, size, ref_buf2, pitch, mc_type2); \
  716. for (i = 0; i < size; i++, buf += pitch) { \
  717. for (j = 0; j < size; j++) {\
  718. OP(buf[j], tmp[i * size + j] >> 1); \
  719. } \
  720. } \
  721. } \
  722. #define OP_PUT(a, b) (a) = (b)
  723. #define OP_ADD(a, b) (a) += (b)
  724. IVI_MC_TEMPLATE(8, _no_delta, OP_PUT)
  725. IVI_MC_TEMPLATE(8, _delta, OP_ADD)
  726. IVI_MC_TEMPLATE(4, _no_delta, OP_PUT)
  727. IVI_MC_TEMPLATE(4, _delta, OP_ADD)
  728. IVI_MC_AVG_TEMPLATE(8, _no_delta, OP_PUT)
  729. IVI_MC_AVG_TEMPLATE(8, _delta, OP_ADD)
  730. IVI_MC_AVG_TEMPLATE(4, _no_delta, OP_PUT)
  731. IVI_MC_AVG_TEMPLATE(4, _delta, OP_ADD)