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