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.

946 lines
33KB

  1. /*
  2. * VC-1 and WMV3 decoder - DSP functions
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * VC-1 and WMV3 decoder
  24. *
  25. */
  26. #include "libavutil/common.h"
  27. #include "h264chroma.h"
  28. #include "vc1dsp.h"
  29. /* Apply overlap transform to horizontal edge */
  30. static void vc1_v_overlap_c(uint8_t *src, int stride)
  31. {
  32. int i;
  33. int a, b, c, d;
  34. int d1, d2;
  35. int rnd = 1;
  36. for (i = 0; i < 8; i++) {
  37. a = src[-2 * stride];
  38. b = src[-stride];
  39. c = src[0];
  40. d = src[stride];
  41. d1 = (a - d + 3 + rnd) >> 3;
  42. d2 = (a - d + b - c + 4 - rnd) >> 3;
  43. src[-2 * stride] = a - d1;
  44. src[-stride] = av_clip_uint8(b - d2);
  45. src[0] = av_clip_uint8(c + d2);
  46. src[stride] = d + d1;
  47. src++;
  48. rnd = !rnd;
  49. }
  50. }
  51. /* Apply overlap transform to vertical edge */
  52. static void vc1_h_overlap_c(uint8_t *src, int stride)
  53. {
  54. int i;
  55. int a, b, c, d;
  56. int d1, d2;
  57. int rnd = 1;
  58. for (i = 0; i < 8; i++) {
  59. a = src[-2];
  60. b = src[-1];
  61. c = src[0];
  62. d = src[1];
  63. d1 = (a - d + 3 + rnd) >> 3;
  64. d2 = (a - d + b - c + 4 - rnd) >> 3;
  65. src[-2] = a - d1;
  66. src[-1] = av_clip_uint8(b - d2);
  67. src[0] = av_clip_uint8(c + d2);
  68. src[1] = d + d1;
  69. src += stride;
  70. rnd = !rnd;
  71. }
  72. }
  73. static void vc1_v_s_overlap_c(int16_t *top, int16_t *bottom)
  74. {
  75. int i;
  76. int a, b, c, d;
  77. int d1, d2;
  78. int rnd1 = 4, rnd2 = 3;
  79. for (i = 0; i < 8; i++) {
  80. a = top[48];
  81. b = top[56];
  82. c = bottom[0];
  83. d = bottom[8];
  84. d1 = a - d;
  85. d2 = a - d + b - c;
  86. top[48] = ((a << 3) - d1 + rnd1) >> 3;
  87. top[56] = ((b << 3) - d2 + rnd2) >> 3;
  88. bottom[0] = ((c << 3) + d2 + rnd1) >> 3;
  89. bottom[8] = ((d << 3) + d1 + rnd2) >> 3;
  90. bottom++;
  91. top++;
  92. rnd2 = 7 - rnd2;
  93. rnd1 = 7 - rnd1;
  94. }
  95. }
  96. static void vc1_h_s_overlap_c(int16_t *left, int16_t *right)
  97. {
  98. int i;
  99. int a, b, c, d;
  100. int d1, d2;
  101. int rnd1 = 4, rnd2 = 3;
  102. for (i = 0; i < 8; i++) {
  103. a = left[6];
  104. b = left[7];
  105. c = right[0];
  106. d = right[1];
  107. d1 = a - d;
  108. d2 = a - d + b - c;
  109. left[6] = ((a << 3) - d1 + rnd1) >> 3;
  110. left[7] = ((b << 3) - d2 + rnd2) >> 3;
  111. right[0] = ((c << 3) + d2 + rnd1) >> 3;
  112. right[1] = ((d << 3) + d1 + rnd2) >> 3;
  113. right += 8;
  114. left += 8;
  115. rnd2 = 7 - rnd2;
  116. rnd1 = 7 - rnd1;
  117. }
  118. }
  119. /**
  120. * VC-1 in-loop deblocking filter for one line
  121. * @param src source block type
  122. * @param stride block stride
  123. * @param pq block quantizer
  124. * @return whether other 3 pairs should be filtered or not
  125. * @see 8.6
  126. */
  127. static av_always_inline int vc1_filter_line(uint8_t *src, int stride, int pq)
  128. {
  129. int a0 = (2 * (src[-2 * stride] - src[1 * stride]) -
  130. 5 * (src[-1 * stride] - src[0 * stride]) + 4) >> 3;
  131. int a0_sign = a0 >> 31; /* Store sign */
  132. a0 = (a0 ^ a0_sign) - a0_sign; /* a0 = FFABS(a0); */
  133. if (a0 < pq) {
  134. int a1 = FFABS((2 * (src[-4 * stride] - src[-1 * stride]) -
  135. 5 * (src[-3 * stride] - src[-2 * stride]) + 4) >> 3);
  136. int a2 = FFABS((2 * (src[ 0 * stride] - src[ 3 * stride]) -
  137. 5 * (src[ 1 * stride] - src[ 2 * stride]) + 4) >> 3);
  138. if (a1 < a0 || a2 < a0) {
  139. int clip = src[-1 * stride] - src[0 * stride];
  140. int clip_sign = clip >> 31;
  141. clip = ((clip ^ clip_sign) - clip_sign) >> 1;
  142. if (clip) {
  143. int a3 = FFMIN(a1, a2);
  144. int d = 5 * (a3 - a0);
  145. int d_sign = (d >> 31);
  146. d = ((d ^ d_sign) - d_sign) >> 3;
  147. d_sign ^= a0_sign;
  148. if (d_sign ^ clip_sign)
  149. d = 0;
  150. else {
  151. d = FFMIN(d, clip);
  152. d = (d ^ d_sign) - d_sign; /* Restore sign */
  153. src[-1 * stride] = av_clip_uint8(src[-1 * stride] - d);
  154. src[ 0 * stride] = av_clip_uint8(src[ 0 * stride] + d);
  155. }
  156. return 1;
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. /**
  163. * VC-1 in-loop deblocking filter
  164. * @param src source block type
  165. * @param step distance between horizontally adjacent elements
  166. * @param stride distance between vertically adjacent elements
  167. * @param len edge length to filter (4 or 8 pixels)
  168. * @param pq block quantizer
  169. * @see 8.6
  170. */
  171. static inline void vc1_loop_filter(uint8_t *src, int step, int stride,
  172. int len, int pq)
  173. {
  174. int i;
  175. int filt3;
  176. for (i = 0; i < len; i += 4) {
  177. filt3 = vc1_filter_line(src + 2 * step, stride, pq);
  178. if (filt3) {
  179. vc1_filter_line(src + 0 * step, stride, pq);
  180. vc1_filter_line(src + 1 * step, stride, pq);
  181. vc1_filter_line(src + 3 * step, stride, pq);
  182. }
  183. src += step * 4;
  184. }
  185. }
  186. static void vc1_v_loop_filter4_c(uint8_t *src, int stride, int pq)
  187. {
  188. vc1_loop_filter(src, 1, stride, 4, pq);
  189. }
  190. static void vc1_h_loop_filter4_c(uint8_t *src, int stride, int pq)
  191. {
  192. vc1_loop_filter(src, stride, 1, 4, pq);
  193. }
  194. static void vc1_v_loop_filter8_c(uint8_t *src, int stride, int pq)
  195. {
  196. vc1_loop_filter(src, 1, stride, 8, pq);
  197. }
  198. static void vc1_h_loop_filter8_c(uint8_t *src, int stride, int pq)
  199. {
  200. vc1_loop_filter(src, stride, 1, 8, pq);
  201. }
  202. static void vc1_v_loop_filter16_c(uint8_t *src, int stride, int pq)
  203. {
  204. vc1_loop_filter(src, 1, stride, 16, pq);
  205. }
  206. static void vc1_h_loop_filter16_c(uint8_t *src, int stride, int pq)
  207. {
  208. vc1_loop_filter(src, stride, 1, 16, pq);
  209. }
  210. /* Do inverse transform on 8x8 block */
  211. static void vc1_inv_trans_8x8_dc_c(uint8_t *dest, int linesize, int16_t *block)
  212. {
  213. int i;
  214. int dc = block[0];
  215. dc = (3 * dc + 1) >> 1;
  216. dc = (3 * dc + 16) >> 5;
  217. for (i = 0; i < 8; i++) {
  218. dest[0] = av_clip_uint8(dest[0] + dc);
  219. dest[1] = av_clip_uint8(dest[1] + dc);
  220. dest[2] = av_clip_uint8(dest[2] + dc);
  221. dest[3] = av_clip_uint8(dest[3] + dc);
  222. dest[4] = av_clip_uint8(dest[4] + dc);
  223. dest[5] = av_clip_uint8(dest[5] + dc);
  224. dest[6] = av_clip_uint8(dest[6] + dc);
  225. dest[7] = av_clip_uint8(dest[7] + dc);
  226. dest += linesize;
  227. }
  228. }
  229. static void vc1_inv_trans_8x8_c(int16_t block[64])
  230. {
  231. int i;
  232. register int t1, t2, t3, t4, t5, t6, t7, t8;
  233. int16_t *src, *dst, temp[64];
  234. src = block;
  235. dst = temp;
  236. for (i = 0; i < 8; i++) {
  237. t1 = 12 * (src[ 0] + src[32]) + 4;
  238. t2 = 12 * (src[ 0] - src[32]) + 4;
  239. t3 = 16 * src[16] + 6 * src[48];
  240. t4 = 6 * src[16] - 16 * src[48];
  241. t5 = t1 + t3;
  242. t6 = t2 + t4;
  243. t7 = t2 - t4;
  244. t8 = t1 - t3;
  245. t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56];
  246. t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56];
  247. t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56];
  248. t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56];
  249. dst[0] = (t5 + t1) >> 3;
  250. dst[1] = (t6 + t2) >> 3;
  251. dst[2] = (t7 + t3) >> 3;
  252. dst[3] = (t8 + t4) >> 3;
  253. dst[4] = (t8 - t4) >> 3;
  254. dst[5] = (t7 - t3) >> 3;
  255. dst[6] = (t6 - t2) >> 3;
  256. dst[7] = (t5 - t1) >> 3;
  257. src += 1;
  258. dst += 8;
  259. }
  260. src = temp;
  261. dst = block;
  262. for (i = 0; i < 8; i++) {
  263. t1 = 12 * (src[ 0] + src[32]) + 64;
  264. t2 = 12 * (src[ 0] - src[32]) + 64;
  265. t3 = 16 * src[16] + 6 * src[48];
  266. t4 = 6 * src[16] - 16 * src[48];
  267. t5 = t1 + t3;
  268. t6 = t2 + t4;
  269. t7 = t2 - t4;
  270. t8 = t1 - t3;
  271. t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56];
  272. t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56];
  273. t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56];
  274. t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56];
  275. dst[ 0] = (t5 + t1) >> 7;
  276. dst[ 8] = (t6 + t2) >> 7;
  277. dst[16] = (t7 + t3) >> 7;
  278. dst[24] = (t8 + t4) >> 7;
  279. dst[32] = (t8 - t4 + 1) >> 7;
  280. dst[40] = (t7 - t3 + 1) >> 7;
  281. dst[48] = (t6 - t2 + 1) >> 7;
  282. dst[56] = (t5 - t1 + 1) >> 7;
  283. src++;
  284. dst++;
  285. }
  286. }
  287. /* Do inverse transform on 8x4 part of block */
  288. static void vc1_inv_trans_8x4_dc_c(uint8_t *dest, int linesize, int16_t *block)
  289. {
  290. int i;
  291. int dc = block[0];
  292. dc = (3 * dc + 1) >> 1;
  293. dc = (17 * dc + 64) >> 7;
  294. for (i = 0; i < 4; i++) {
  295. dest[0] = av_clip_uint8(dest[0] + dc);
  296. dest[1] = av_clip_uint8(dest[1] + dc);
  297. dest[2] = av_clip_uint8(dest[2] + dc);
  298. dest[3] = av_clip_uint8(dest[3] + dc);
  299. dest[4] = av_clip_uint8(dest[4] + dc);
  300. dest[5] = av_clip_uint8(dest[5] + dc);
  301. dest[6] = av_clip_uint8(dest[6] + dc);
  302. dest[7] = av_clip_uint8(dest[7] + dc);
  303. dest += linesize;
  304. }
  305. }
  306. static void vc1_inv_trans_8x4_c(uint8_t *dest, int linesize, int16_t *block)
  307. {
  308. int i;
  309. register int t1, t2, t3, t4, t5, t6, t7, t8;
  310. int16_t *src, *dst;
  311. src = block;
  312. dst = block;
  313. for (i = 0; i < 4; i++) {
  314. t1 = 12 * (src[0] + src[4]) + 4;
  315. t2 = 12 * (src[0] - src[4]) + 4;
  316. t3 = 16 * src[2] + 6 * src[6];
  317. t4 = 6 * src[2] - 16 * src[6];
  318. t5 = t1 + t3;
  319. t6 = t2 + t4;
  320. t7 = t2 - t4;
  321. t8 = t1 - t3;
  322. t1 = 16 * src[1] + 15 * src[3] + 9 * src[5] + 4 * src[7];
  323. t2 = 15 * src[1] - 4 * src[3] - 16 * src[5] - 9 * src[7];
  324. t3 = 9 * src[1] - 16 * src[3] + 4 * src[5] + 15 * src[7];
  325. t4 = 4 * src[1] - 9 * src[3] + 15 * src[5] - 16 * src[7];
  326. dst[0] = (t5 + t1) >> 3;
  327. dst[1] = (t6 + t2) >> 3;
  328. dst[2] = (t7 + t3) >> 3;
  329. dst[3] = (t8 + t4) >> 3;
  330. dst[4] = (t8 - t4) >> 3;
  331. dst[5] = (t7 - t3) >> 3;
  332. dst[6] = (t6 - t2) >> 3;
  333. dst[7] = (t5 - t1) >> 3;
  334. src += 8;
  335. dst += 8;
  336. }
  337. src = block;
  338. for (i = 0; i < 8; i++) {
  339. t1 = 17 * (src[ 0] + src[16]) + 64;
  340. t2 = 17 * (src[ 0] - src[16]) + 64;
  341. t3 = 22 * src[ 8] + 10 * src[24];
  342. t4 = 22 * src[24] - 10 * src[ 8];
  343. dest[0 * linesize] = av_clip_uint8(dest[0 * linesize] + ((t1 + t3) >> 7));
  344. dest[1 * linesize] = av_clip_uint8(dest[1 * linesize] + ((t2 - t4) >> 7));
  345. dest[2 * linesize] = av_clip_uint8(dest[2 * linesize] + ((t2 + t4) >> 7));
  346. dest[3 * linesize] = av_clip_uint8(dest[3 * linesize] + ((t1 - t3) >> 7));
  347. src++;
  348. dest++;
  349. }
  350. }
  351. /* Do inverse transform on 4x8 parts of block */
  352. static void vc1_inv_trans_4x8_dc_c(uint8_t *dest, int linesize, int16_t *block)
  353. {
  354. int i;
  355. int dc = block[0];
  356. dc = (17 * dc + 4) >> 3;
  357. dc = (12 * dc + 64) >> 7;
  358. for (i = 0; i < 8; i++) {
  359. dest[0] = av_clip_uint8(dest[0] + dc);
  360. dest[1] = av_clip_uint8(dest[1] + dc);
  361. dest[2] = av_clip_uint8(dest[2] + dc);
  362. dest[3] = av_clip_uint8(dest[3] + dc);
  363. dest += linesize;
  364. }
  365. }
  366. static void vc1_inv_trans_4x8_c(uint8_t *dest, int linesize, int16_t *block)
  367. {
  368. int i;
  369. register int t1, t2, t3, t4, t5, t6, t7, t8;
  370. int16_t *src, *dst;
  371. src = block;
  372. dst = block;
  373. for (i = 0; i < 8; i++) {
  374. t1 = 17 * (src[0] + src[2]) + 4;
  375. t2 = 17 * (src[0] - src[2]) + 4;
  376. t3 = 22 * src[1] + 10 * src[3];
  377. t4 = 22 * src[3] - 10 * src[1];
  378. dst[0] = (t1 + t3) >> 3;
  379. dst[1] = (t2 - t4) >> 3;
  380. dst[2] = (t2 + t4) >> 3;
  381. dst[3] = (t1 - t3) >> 3;
  382. src += 8;
  383. dst += 8;
  384. }
  385. src = block;
  386. for (i = 0; i < 4; i++) {
  387. t1 = 12 * (src[ 0] + src[32]) + 64;
  388. t2 = 12 * (src[ 0] - src[32]) + 64;
  389. t3 = 16 * src[16] + 6 * src[48];
  390. t4 = 6 * src[16] - 16 * src[48];
  391. t5 = t1 + t3;
  392. t6 = t2 + t4;
  393. t7 = t2 - t4;
  394. t8 = t1 - t3;
  395. t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56];
  396. t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56];
  397. t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56];
  398. t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56];
  399. dest[0 * linesize] = av_clip_uint8(dest[0 * linesize] + ((t5 + t1) >> 7));
  400. dest[1 * linesize] = av_clip_uint8(dest[1 * linesize] + ((t6 + t2) >> 7));
  401. dest[2 * linesize] = av_clip_uint8(dest[2 * linesize] + ((t7 + t3) >> 7));
  402. dest[3 * linesize] = av_clip_uint8(dest[3 * linesize] + ((t8 + t4) >> 7));
  403. dest[4 * linesize] = av_clip_uint8(dest[4 * linesize] + ((t8 - t4 + 1) >> 7));
  404. dest[5 * linesize] = av_clip_uint8(dest[5 * linesize] + ((t7 - t3 + 1) >> 7));
  405. dest[6 * linesize] = av_clip_uint8(dest[6 * linesize] + ((t6 - t2 + 1) >> 7));
  406. dest[7 * linesize] = av_clip_uint8(dest[7 * linesize] + ((t5 - t1 + 1) >> 7));
  407. src++;
  408. dest++;
  409. }
  410. }
  411. /* Do inverse transform on 4x4 part of block */
  412. static void vc1_inv_trans_4x4_dc_c(uint8_t *dest, int linesize, int16_t *block)
  413. {
  414. int i;
  415. int dc = block[0];
  416. dc = (17 * dc + 4) >> 3;
  417. dc = (17 * dc + 64) >> 7;
  418. for (i = 0; i < 4; i++) {
  419. dest[0] = av_clip_uint8(dest[0] + dc);
  420. dest[1] = av_clip_uint8(dest[1] + dc);
  421. dest[2] = av_clip_uint8(dest[2] + dc);
  422. dest[3] = av_clip_uint8(dest[3] + dc);
  423. dest += linesize;
  424. }
  425. }
  426. static void vc1_inv_trans_4x4_c(uint8_t *dest, int linesize, int16_t *block)
  427. {
  428. int i;
  429. register int t1, t2, t3, t4;
  430. int16_t *src, *dst;
  431. src = block;
  432. dst = block;
  433. for (i = 0; i < 4; i++) {
  434. t1 = 17 * (src[0] + src[2]) + 4;
  435. t2 = 17 * (src[0] - src[2]) + 4;
  436. t3 = 22 * src[1] + 10 * src[3];
  437. t4 = 22 * src[3] - 10 * src[1];
  438. dst[0] = (t1 + t3) >> 3;
  439. dst[1] = (t2 - t4) >> 3;
  440. dst[2] = (t2 + t4) >> 3;
  441. dst[3] = (t1 - t3) >> 3;
  442. src += 8;
  443. dst += 8;
  444. }
  445. src = block;
  446. for (i = 0; i < 4; i++) {
  447. t1 = 17 * (src[0] + src[16]) + 64;
  448. t2 = 17 * (src[0] - src[16]) + 64;
  449. t3 = 22 * src[8] + 10 * src[24];
  450. t4 = 22 * src[24] - 10 * src[8];
  451. dest[0 * linesize] = av_clip_uint8(dest[0 * linesize] + ((t1 + t3) >> 7));
  452. dest[1 * linesize] = av_clip_uint8(dest[1 * linesize] + ((t2 - t4) >> 7));
  453. dest[2 * linesize] = av_clip_uint8(dest[2 * linesize] + ((t2 + t4) >> 7));
  454. dest[3 * linesize] = av_clip_uint8(dest[3 * linesize] + ((t1 - t3) >> 7));
  455. src++;
  456. dest++;
  457. }
  458. }
  459. /* motion compensation functions */
  460. /* Filter in case of 2 filters */
  461. #define VC1_MSPEL_FILTER_16B(DIR, TYPE) \
  462. static av_always_inline int vc1_mspel_ ## DIR ## _filter_16bits(const TYPE *src, \
  463. int stride, \
  464. int mode) \
  465. { \
  466. switch(mode) { \
  467. case 0: /* no shift - should not occur */ \
  468. return 0; \
  469. case 1: /* 1/4 shift */ \
  470. return -4 * src[-stride] + 53 * src[0] + \
  471. 18 * src[stride] - 3 * src[stride * 2]; \
  472. case 2: /* 1/2 shift */ \
  473. return -1 * src[-stride] + 9 * src[0] + \
  474. 9 * src[stride] - 1 * src[stride * 2]; \
  475. case 3: /* 3/4 shift */ \
  476. return -3 * src[-stride] + 18 * src[0] + \
  477. 53 * src[stride] - 4 * src[stride * 2]; \
  478. } \
  479. return 0; /* should not occur */ \
  480. }
  481. VC1_MSPEL_FILTER_16B(ver, uint8_t)
  482. VC1_MSPEL_FILTER_16B(hor, int16_t)
  483. /* Filter used to interpolate fractional pel values */
  484. static av_always_inline int vc1_mspel_filter(const uint8_t *src, int stride,
  485. int mode, int r)
  486. {
  487. switch (mode) {
  488. case 0: // no shift
  489. return src[0];
  490. case 1: // 1/4 shift
  491. return (-4 * src[-stride] + 53 * src[0] +
  492. 18 * src[stride] - 3 * src[stride * 2] + 32 - r) >> 6;
  493. case 2: // 1/2 shift
  494. return (-1 * src[-stride] + 9 * src[0] +
  495. 9 * src[stride] - 1 * src[stride * 2] + 8 - r) >> 4;
  496. case 3: // 3/4 shift
  497. return (-3 * src[-stride] + 18 * src[0] +
  498. 53 * src[stride] - 4 * src[stride * 2] + 32 - r) >> 6;
  499. }
  500. return 0; // should not occur
  501. }
  502. /* Function used to do motion compensation with bicubic interpolation */
  503. #define VC1_MSPEL_MC(OP, OPNAME) \
  504. static av_always_inline void OPNAME ## vc1_mspel_mc(uint8_t *dst, \
  505. const uint8_t *src, \
  506. int stride, \
  507. int hmode, \
  508. int vmode, \
  509. int rnd) \
  510. { \
  511. int i, j; \
  512. \
  513. if (vmode) { /* Horizontal filter to apply */ \
  514. int r; \
  515. \
  516. if (hmode) { /* Vertical filter to apply, output to tmp */ \
  517. static const int shift_value[] = { 0, 5, 1, 5 }; \
  518. int shift = (shift_value[hmode] + shift_value[vmode]) >> 1; \
  519. int16_t tmp[11 * 8], *tptr = tmp; \
  520. \
  521. r = (1 << (shift - 1)) + rnd - 1; \
  522. \
  523. src -= 1; \
  524. for (j = 0; j < 8; j++) { \
  525. for (i = 0; i < 11; i++) \
  526. tptr[i] = (vc1_mspel_ver_filter_16bits(src + i, stride, vmode) + r) >> shift; \
  527. src += stride; \
  528. tptr += 11; \
  529. } \
  530. \
  531. r = 64 - rnd; \
  532. tptr = tmp + 1; \
  533. for (j = 0; j < 8; j++) { \
  534. for (i = 0; i < 8; i++) \
  535. OP(dst[i], (vc1_mspel_hor_filter_16bits(tptr + i, 1, hmode) + r) >> 7); \
  536. dst += stride; \
  537. tptr += 11; \
  538. } \
  539. \
  540. return; \
  541. } else { /* No horizontal filter, output 8 lines to dst */ \
  542. r = 1 - rnd; \
  543. \
  544. for (j = 0; j < 8; j++) { \
  545. for (i = 0; i < 8; i++) \
  546. OP(dst[i], vc1_mspel_filter(src + i, stride, vmode, r)); \
  547. src += stride; \
  548. dst += stride; \
  549. } \
  550. return; \
  551. } \
  552. } \
  553. \
  554. /* Horizontal mode with no vertical mode */ \
  555. for (j = 0; j < 8; j++) { \
  556. for (i = 0; i < 8; i++) \
  557. OP(dst[i], vc1_mspel_filter(src + i, 1, hmode, rnd)); \
  558. dst += stride; \
  559. src += stride; \
  560. } \
  561. }
  562. #define op_put(a, b) a = av_clip_uint8(b)
  563. #define op_avg(a, b) a = (a + av_clip_uint8(b) + 1) >> 1
  564. VC1_MSPEL_MC(op_put, put_)
  565. VC1_MSPEL_MC(op_avg, avg_)
  566. /* pixel functions - really are entry points to vc1_mspel_mc */
  567. #define PUT_VC1_MSPEL(a, b) \
  568. static void put_vc1_mspel_mc ## a ## b ## _c(uint8_t *dst, \
  569. const uint8_t *src, \
  570. ptrdiff_t stride, int rnd) \
  571. { \
  572. put_vc1_mspel_mc(dst, src, stride, a, b, rnd); \
  573. } \
  574. static void avg_vc1_mspel_mc ## a ## b ## _c(uint8_t *dst, \
  575. const uint8_t *src, \
  576. ptrdiff_t stride, int rnd) \
  577. { \
  578. avg_vc1_mspel_mc(dst, src, stride, a, b, rnd); \
  579. }
  580. PUT_VC1_MSPEL(1, 0)
  581. PUT_VC1_MSPEL(2, 0)
  582. PUT_VC1_MSPEL(3, 0)
  583. PUT_VC1_MSPEL(0, 1)
  584. PUT_VC1_MSPEL(1, 1)
  585. PUT_VC1_MSPEL(2, 1)
  586. PUT_VC1_MSPEL(3, 1)
  587. PUT_VC1_MSPEL(0, 2)
  588. PUT_VC1_MSPEL(1, 2)
  589. PUT_VC1_MSPEL(2, 2)
  590. PUT_VC1_MSPEL(3, 2)
  591. PUT_VC1_MSPEL(0, 3)
  592. PUT_VC1_MSPEL(1, 3)
  593. PUT_VC1_MSPEL(2, 3)
  594. PUT_VC1_MSPEL(3, 3)
  595. #define chroma_mc(a) \
  596. ((A * src[a] + B * src[a + 1] + \
  597. C * src[stride + a] + D * src[stride + a + 1] + 32 - 4) >> 6)
  598. static void put_no_rnd_vc1_chroma_mc8_c(uint8_t *dst /* align 8 */,
  599. uint8_t *src /* align 1 */,
  600. int stride, int h, int x, int y)
  601. {
  602. const int A = (8 - x) * (8 - y);
  603. const int B = (x) * (8 - y);
  604. const int C = (8 - x) * (y);
  605. const int D = (x) * (y);
  606. int i;
  607. assert(x < 8 && y < 8 && x >= 0 && y >= 0);
  608. for (i = 0; i < h; i++) {
  609. dst[0] = chroma_mc(0);
  610. dst[1] = chroma_mc(1);
  611. dst[2] = chroma_mc(2);
  612. dst[3] = chroma_mc(3);
  613. dst[4] = chroma_mc(4);
  614. dst[5] = chroma_mc(5);
  615. dst[6] = chroma_mc(6);
  616. dst[7] = chroma_mc(7);
  617. dst += stride;
  618. src += stride;
  619. }
  620. }
  621. static void put_no_rnd_vc1_chroma_mc4_c(uint8_t *dst, uint8_t *src,
  622. int stride, int h, int x, int y)
  623. {
  624. const int A = (8 - x) * (8 - y);
  625. const int B = (x) * (8 - y);
  626. const int C = (8 - x) * (y);
  627. const int D = (x) * (y);
  628. int i;
  629. assert(x < 8 && y < 8 && x >= 0 && y >= 0);
  630. for (i = 0; i < h; i++) {
  631. dst[0] = chroma_mc(0);
  632. dst[1] = chroma_mc(1);
  633. dst[2] = chroma_mc(2);
  634. dst[3] = chroma_mc(3);
  635. dst += stride;
  636. src += stride;
  637. }
  638. }
  639. #define avg2(a, b) (((a) + (b) + 1) >> 1)
  640. static void avg_no_rnd_vc1_chroma_mc8_c(uint8_t *dst /* align 8 */,
  641. uint8_t *src /* align 1 */,
  642. int stride, int h, int x, int y)
  643. {
  644. const int A = (8 - x) * (8 - y);
  645. const int B = (x) * (8 - y);
  646. const int C = (8 - x) * (y);
  647. const int D = (x) * (y);
  648. int i;
  649. assert(x < 8 && y < 8 && x >= 0 && y >= 0);
  650. for (i = 0; i < h; i++) {
  651. dst[0] = avg2(dst[0], chroma_mc(0));
  652. dst[1] = avg2(dst[1], chroma_mc(1));
  653. dst[2] = avg2(dst[2], chroma_mc(2));
  654. dst[3] = avg2(dst[3], chroma_mc(3));
  655. dst[4] = avg2(dst[4], chroma_mc(4));
  656. dst[5] = avg2(dst[5], chroma_mc(5));
  657. dst[6] = avg2(dst[6], chroma_mc(6));
  658. dst[7] = avg2(dst[7], chroma_mc(7));
  659. dst += stride;
  660. src += stride;
  661. }
  662. }
  663. static void avg_no_rnd_vc1_chroma_mc4_c(uint8_t *dst /* align 8 */,
  664. uint8_t *src /* align 1 */,
  665. int stride, int h, int x, int y)
  666. {
  667. const int A = (8 - x) * (8 - y);
  668. const int B = ( x) * (8 - y);
  669. const int C = (8 - x) * ( y);
  670. const int D = ( x) * ( y);
  671. int i;
  672. assert(x < 8 && y < 8 && x >= 0 && y >= 0);
  673. for (i = 0; i < h; i++) {
  674. dst[0] = avg2(dst[0], chroma_mc(0));
  675. dst[1] = avg2(dst[1], chroma_mc(1));
  676. dst[2] = avg2(dst[2], chroma_mc(2));
  677. dst[3] = avg2(dst[3], chroma_mc(3));
  678. dst += stride;
  679. src += stride;
  680. }
  681. }
  682. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  683. static void sprite_h_c(uint8_t *dst, const uint8_t *src, int offset,
  684. int advance, int count)
  685. {
  686. while (count--) {
  687. int a = src[(offset >> 16)];
  688. int b = src[(offset >> 16) + 1];
  689. *dst++ = a + ((b - a) * (offset & 0xFFFF) >> 16);
  690. offset += advance;
  691. }
  692. }
  693. static av_always_inline void sprite_v_template(uint8_t *dst,
  694. const uint8_t *src1a,
  695. const uint8_t *src1b,
  696. int offset1,
  697. int two_sprites,
  698. const uint8_t *src2a,
  699. const uint8_t *src2b,
  700. int offset2,
  701. int alpha, int scaled,
  702. int width)
  703. {
  704. int a1, b1, a2, b2;
  705. while (width--) {
  706. a1 = *src1a++;
  707. if (scaled) {
  708. b1 = *src1b++;
  709. a1 = a1 + ((b1 - a1) * offset1 >> 16);
  710. }
  711. if (two_sprites) {
  712. a2 = *src2a++;
  713. if (scaled > 1) {
  714. b2 = *src2b++;
  715. a2 = a2 + ((b2 - a2) * offset2 >> 16);
  716. }
  717. a1 = a1 + ((a2 - a1) * alpha >> 16);
  718. }
  719. *dst++ = a1;
  720. }
  721. }
  722. static void sprite_v_single_c(uint8_t *dst, const uint8_t *src1a,
  723. const uint8_t *src1b,
  724. int offset, int width)
  725. {
  726. sprite_v_template(dst, src1a, src1b, offset, 0, NULL, NULL, 0, 0, 1, width);
  727. }
  728. static void sprite_v_double_noscale_c(uint8_t *dst, const uint8_t *src1a,
  729. const uint8_t *src2a,
  730. int alpha, int width)
  731. {
  732. sprite_v_template(dst, src1a, NULL, 0, 1, src2a, NULL, 0, alpha, 0, width);
  733. }
  734. static void sprite_v_double_onescale_c(uint8_t *dst,
  735. const uint8_t *src1a,
  736. const uint8_t *src1b,
  737. int offset1,
  738. const uint8_t *src2a,
  739. int alpha, int width)
  740. {
  741. sprite_v_template(dst, src1a, src1b, offset1, 1, src2a, NULL, 0, alpha, 1,
  742. width);
  743. }
  744. static void sprite_v_double_twoscale_c(uint8_t *dst,
  745. const uint8_t *src1a,
  746. const uint8_t *src1b,
  747. int offset1,
  748. const uint8_t *src2a,
  749. const uint8_t *src2b,
  750. int offset2,
  751. int alpha,
  752. int width)
  753. {
  754. sprite_v_template(dst, src1a, src1b, offset1, 1, src2a, src2b, offset2,
  755. alpha, 2, width);
  756. }
  757. #endif /* CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER */
  758. av_cold void ff_vc1dsp_init(VC1DSPContext *dsp)
  759. {
  760. dsp->vc1_inv_trans_8x8 = vc1_inv_trans_8x8_c;
  761. dsp->vc1_inv_trans_4x8 = vc1_inv_trans_4x8_c;
  762. dsp->vc1_inv_trans_8x4 = vc1_inv_trans_8x4_c;
  763. dsp->vc1_inv_trans_4x4 = vc1_inv_trans_4x4_c;
  764. dsp->vc1_inv_trans_8x8_dc = vc1_inv_trans_8x8_dc_c;
  765. dsp->vc1_inv_trans_4x8_dc = vc1_inv_trans_4x8_dc_c;
  766. dsp->vc1_inv_trans_8x4_dc = vc1_inv_trans_8x4_dc_c;
  767. dsp->vc1_inv_trans_4x4_dc = vc1_inv_trans_4x4_dc_c;
  768. dsp->vc1_h_overlap = vc1_h_overlap_c;
  769. dsp->vc1_v_overlap = vc1_v_overlap_c;
  770. dsp->vc1_h_s_overlap = vc1_h_s_overlap_c;
  771. dsp->vc1_v_s_overlap = vc1_v_s_overlap_c;
  772. dsp->vc1_v_loop_filter4 = vc1_v_loop_filter4_c;
  773. dsp->vc1_h_loop_filter4 = vc1_h_loop_filter4_c;
  774. dsp->vc1_v_loop_filter8 = vc1_v_loop_filter8_c;
  775. dsp->vc1_h_loop_filter8 = vc1_h_loop_filter8_c;
  776. dsp->vc1_v_loop_filter16 = vc1_v_loop_filter16_c;
  777. dsp->vc1_h_loop_filter16 = vc1_h_loop_filter16_c;
  778. dsp->put_vc1_mspel_pixels_tab[0] = ff_put_pixels8x8_c;
  779. dsp->put_vc1_mspel_pixels_tab[1] = put_vc1_mspel_mc10_c;
  780. dsp->put_vc1_mspel_pixels_tab[2] = put_vc1_mspel_mc20_c;
  781. dsp->put_vc1_mspel_pixels_tab[3] = put_vc1_mspel_mc30_c;
  782. dsp->put_vc1_mspel_pixels_tab[4] = put_vc1_mspel_mc01_c;
  783. dsp->put_vc1_mspel_pixels_tab[5] = put_vc1_mspel_mc11_c;
  784. dsp->put_vc1_mspel_pixels_tab[6] = put_vc1_mspel_mc21_c;
  785. dsp->put_vc1_mspel_pixels_tab[7] = put_vc1_mspel_mc31_c;
  786. dsp->put_vc1_mspel_pixels_tab[8] = put_vc1_mspel_mc02_c;
  787. dsp->put_vc1_mspel_pixels_tab[9] = put_vc1_mspel_mc12_c;
  788. dsp->put_vc1_mspel_pixels_tab[10] = put_vc1_mspel_mc22_c;
  789. dsp->put_vc1_mspel_pixels_tab[11] = put_vc1_mspel_mc32_c;
  790. dsp->put_vc1_mspel_pixels_tab[12] = put_vc1_mspel_mc03_c;
  791. dsp->put_vc1_mspel_pixels_tab[13] = put_vc1_mspel_mc13_c;
  792. dsp->put_vc1_mspel_pixels_tab[14] = put_vc1_mspel_mc23_c;
  793. dsp->put_vc1_mspel_pixels_tab[15] = put_vc1_mspel_mc33_c;
  794. dsp->avg_vc1_mspel_pixels_tab[0] = ff_avg_pixels8x8_c;
  795. dsp->avg_vc1_mspel_pixels_tab[1] = avg_vc1_mspel_mc10_c;
  796. dsp->avg_vc1_mspel_pixels_tab[2] = avg_vc1_mspel_mc20_c;
  797. dsp->avg_vc1_mspel_pixels_tab[3] = avg_vc1_mspel_mc30_c;
  798. dsp->avg_vc1_mspel_pixels_tab[4] = avg_vc1_mspel_mc01_c;
  799. dsp->avg_vc1_mspel_pixels_tab[5] = avg_vc1_mspel_mc11_c;
  800. dsp->avg_vc1_mspel_pixels_tab[6] = avg_vc1_mspel_mc21_c;
  801. dsp->avg_vc1_mspel_pixels_tab[7] = avg_vc1_mspel_mc31_c;
  802. dsp->avg_vc1_mspel_pixels_tab[8] = avg_vc1_mspel_mc02_c;
  803. dsp->avg_vc1_mspel_pixels_tab[9] = avg_vc1_mspel_mc12_c;
  804. dsp->avg_vc1_mspel_pixels_tab[10] = avg_vc1_mspel_mc22_c;
  805. dsp->avg_vc1_mspel_pixels_tab[11] = avg_vc1_mspel_mc32_c;
  806. dsp->avg_vc1_mspel_pixels_tab[12] = avg_vc1_mspel_mc03_c;
  807. dsp->avg_vc1_mspel_pixels_tab[13] = avg_vc1_mspel_mc13_c;
  808. dsp->avg_vc1_mspel_pixels_tab[14] = avg_vc1_mspel_mc23_c;
  809. dsp->avg_vc1_mspel_pixels_tab[15] = avg_vc1_mspel_mc33_c;
  810. dsp->put_no_rnd_vc1_chroma_pixels_tab[0] = put_no_rnd_vc1_chroma_mc8_c;
  811. dsp->avg_no_rnd_vc1_chroma_pixels_tab[0] = avg_no_rnd_vc1_chroma_mc8_c;
  812. dsp->put_no_rnd_vc1_chroma_pixels_tab[1] = put_no_rnd_vc1_chroma_mc4_c;
  813. dsp->avg_no_rnd_vc1_chroma_pixels_tab[1] = avg_no_rnd_vc1_chroma_mc4_c;
  814. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  815. dsp->sprite_h = sprite_h_c;
  816. dsp->sprite_v_single = sprite_v_single_c;
  817. dsp->sprite_v_double_noscale = sprite_v_double_noscale_c;
  818. dsp->sprite_v_double_onescale = sprite_v_double_onescale_c;
  819. dsp->sprite_v_double_twoscale = sprite_v_double_twoscale_c;
  820. #endif /* CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER */
  821. if (ARCH_AARCH64)
  822. ff_vc1dsp_init_aarch64(dsp);
  823. if (ARCH_ARM)
  824. ff_vc1dsp_init_arm(dsp);
  825. if (ARCH_PPC)
  826. ff_vc1dsp_init_ppc(dsp);
  827. if (ARCH_X86)
  828. ff_vc1dsp_init_x86(dsp);
  829. }