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