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.

1039 lines
39KB

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