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.

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