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