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.

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