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.

855 lines
28KB

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