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.

961 lines
34KB

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