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