Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libavcodec / hevcdsp_template.c
1 /*
2  * HEVC video decoder
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "get_bits.h"
24 #include "hevc.h"
25
26 #include "bit_depth_template.c"
27 #include "hevcdsp.h"
28
29
30 static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t stride, int size,
31                           GetBitContext *gb, int pcm_bit_depth)
32 {
33     int x, y;
34     pixel *dst = (pixel *)_dst;
35
36     stride /= sizeof(pixel);
37
38     for (y = 0; y < size; y++) {
39         for (x = 0; x < size; x++)
40             dst[x] = get_bits(gb, pcm_bit_depth) << (BIT_DEPTH - pcm_bit_depth);
41         dst += stride;
42     }
43 }
44
45 static void FUNC(transquant_bypass4x4)(uint8_t *_dst, int16_t *coeffs,
46                                        ptrdiff_t stride)
47 {
48     int x, y;
49     pixel *dst = (pixel *)_dst;
50
51     stride /= sizeof(pixel);
52
53     for (y = 0; y < 4; y++) {
54         for (x = 0; x < 4; x++) {
55             dst[x] += *coeffs;
56             coeffs++;
57         }
58         dst += stride;
59     }
60 }
61
62 static void FUNC(transquant_bypass8x8)(uint8_t *_dst, int16_t *coeffs,
63                                        ptrdiff_t stride)
64 {
65     int x, y;
66     pixel *dst = (pixel *)_dst;
67
68     stride /= sizeof(pixel);
69
70     for (y = 0; y < 8; y++) {
71         for (x = 0; x < 8; x++) {
72             dst[x] += *coeffs;
73             coeffs++;
74         }
75         dst += stride;
76     }
77 }
78
79 static void FUNC(transquant_bypass16x16)(uint8_t *_dst, int16_t *coeffs,
80                                          ptrdiff_t stride)
81 {
82     int x, y;
83     pixel *dst = (pixel *)_dst;
84
85     stride /= sizeof(pixel);
86
87     for (y = 0; y < 16; y++) {
88         for (x = 0; x < 16; x++) {
89             dst[x] += *coeffs;
90             coeffs++;
91         }
92         dst += stride;
93     }
94 }
95
96 static void FUNC(transquant_bypass32x32)(uint8_t *_dst, int16_t *coeffs,
97                                          ptrdiff_t stride)
98 {
99     int x, y;
100     pixel *dst = (pixel *)_dst;
101
102     stride /= sizeof(pixel);
103
104     for (y = 0; y < 32; y++) {
105         for (x = 0; x < 32; x++) {
106             dst[x] += *coeffs;
107             coeffs++;
108         }
109         dst += stride;
110     }
111 }
112
113 static void FUNC(transform_skip)(uint8_t *_dst, int16_t *coeffs,
114                                  ptrdiff_t stride)
115 {
116     pixel *dst = (pixel *)_dst;
117     int shift  = 13 - BIT_DEPTH;
118 #if BIT_DEPTH <= 13
119     int offset = 1 << (shift - 1);
120 #else
121     int offset = 0;
122 #endif
123     int x, y;
124
125     stride /= sizeof(pixel);
126
127     for (y = 0; y < 4 * 4; y += 4) {
128         for (x = 0; x < 4; x++)
129             dst[x] = av_clip_pixel(dst[x] + ((coeffs[y + x] + offset) >> shift));
130         dst += stride;
131     }
132 }
133
134 #define SET(dst, x)   (dst) = (x)
135 #define SCALE(dst, x) (dst) = av_clip_int16(((x) + add) >> shift)
136 #define ADD_AND_SCALE(dst, x)                                           \
137     (dst) = av_clip_pixel((dst) + av_clip_int16(((x) + add) >> shift))
138
139 #define TR_4x4_LUMA(dst, src, step, assign)                             \
140     do {                                                                \
141         int c0 = src[0 * step] + src[2 * step];                         \
142         int c1 = src[2 * step] + src[3 * step];                         \
143         int c2 = src[0 * step] - src[3 * step];                         \
144         int c3 = 74 * src[1 * step];                                    \
145                                                                         \
146         assign(dst[2 * step], 74 * (src[0 * step] -                     \
147                                     src[2 * step] +                     \
148                                     src[3 * step]));                    \
149         assign(dst[0 * step], 29 * c0 + 55 * c1 + c3);                  \
150         assign(dst[1 * step], 55 * c2 - 29 * c1 + c3);                  \
151         assign(dst[3 * step], 55 * c0 + 29 * c2 - c3);                  \
152     } while (0)
153
154 static void FUNC(transform_4x4_luma_add)(uint8_t *_dst, int16_t *coeffs,
155                                          ptrdiff_t stride)
156 {
157     int i;
158     pixel *dst   = (pixel *)_dst;
159     int shift    = 7;
160     int add      = 1 << (shift - 1);
161     int16_t *src = coeffs;
162
163     stride /= sizeof(pixel);
164
165     for (i = 0; i < 4; i++) {
166         TR_4x4_LUMA(src, src, 4, SCALE);
167         src++;
168     }
169
170     shift = 20 - BIT_DEPTH;
171     add   = 1 << (shift - 1);
172     for (i = 0; i < 4; i++) {
173         TR_4x4_LUMA(dst, coeffs, 1, ADD_AND_SCALE);
174         coeffs += 4;
175         dst    += stride;
176     }
177 }
178
179 #undef TR_4x4_LUMA
180
181 #define TR_4(dst, src, dstep, sstep, assign)                            \
182     do {                                                                \
183         const int e0 = transform[8 * 0][0] * src[0 * sstep] +           \
184                        transform[8 * 2][0] * src[2 * sstep];            \
185         const int e1 = transform[8 * 0][1] * src[0 * sstep] +           \
186                        transform[8 * 2][1] * src[2 * sstep];            \
187         const int o0 = transform[8 * 1][0] * src[1 * sstep] +           \
188                        transform[8 * 3][0] * src[3 * sstep];            \
189         const int o1 = transform[8 * 1][1] * src[1 * sstep] +           \
190                        transform[8 * 3][1] * src[3 * sstep];            \
191                                                                         \
192         assign(dst[0 * dstep], e0 + o0);                                \
193         assign(dst[1 * dstep], e1 + o1);                                \
194         assign(dst[2 * dstep], e1 - o1);                                \
195         assign(dst[3 * dstep], e0 - o0);                                \
196     } while (0)
197 #define TR_4_1(dst, src) TR_4(dst, src, 4, 4, SCALE)
198 #define TR_4_2(dst, src) TR_4(dst, src, 1, 1, ADD_AND_SCALE)
199
200 static void FUNC(transform_4x4_add)(uint8_t *_dst, int16_t *coeffs,
201                                     ptrdiff_t stride)
202 {
203     int i;
204     pixel *dst   = (pixel *)_dst;
205     int shift    = 7;
206     int add      = 1 << (shift - 1);
207     int16_t *src = coeffs;
208
209     stride /= sizeof(pixel);
210
211     for (i = 0; i < 4; i++) {
212         TR_4_1(src, src);
213         src++;
214     }
215
216     shift = 20 - BIT_DEPTH;
217     add   = 1 << (shift - 1);
218     for (i = 0; i < 4; i++) {
219         TR_4_2(dst, coeffs);
220         coeffs += 4;
221         dst    += stride;
222     }
223 }
224
225 #define TR_8(dst, src, dstep, sstep, assign)                      \
226     do {                                                          \
227         int i, j;                                                 \
228         int e_8[4];                                               \
229         int o_8[4] = { 0 };                                       \
230         for (i = 0; i < 4; i++)                                   \
231             for (j = 1; j < 8; j += 2)                            \
232                 o_8[i] += transform[4 * j][i] * src[j * sstep];   \
233         TR_4(e_8, src, 1, 2 * sstep, SET);                        \
234                                                                   \
235         for (i = 0; i < 4; i++) {                                 \
236             assign(dst[i * dstep], e_8[i] + o_8[i]);              \
237             assign(dst[(7 - i) * dstep], e_8[i] - o_8[i]);        \
238         }                                                         \
239     } while (0)
240
241 #define TR_16(dst, src, dstep, sstep, assign)                     \
242     do {                                                          \
243         int i, j;                                                 \
244         int e_16[8];                                              \
245         int o_16[8] = { 0 };                                      \
246         for (i = 0; i < 8; i++)                                   \
247             for (j = 1; j < 16; j += 2)                           \
248                 o_16[i] += transform[2 * j][i] * src[j * sstep];  \
249         TR_8(e_16, src, 1, 2 * sstep, SET);                       \
250                                                                   \
251         for (i = 0; i < 8; i++) {                                 \
252             assign(dst[i * dstep], e_16[i] + o_16[i]);            \
253             assign(dst[(15 - i) * dstep], e_16[i] - o_16[i]);     \
254         }                                                         \
255     } while (0)
256
257 #define TR_32(dst, src, dstep, sstep, assign)                     \
258     do {                                                          \
259         int i, j;                                                 \
260         int e_32[16];                                             \
261         int o_32[16] = { 0 };                                     \
262         for (i = 0; i < 16; i++)                                  \
263             for (j = 1; j < 32; j += 2)                           \
264                 o_32[i] += transform[j][i] * src[j * sstep];      \
265         TR_16(e_32, src, 1, 2 * sstep, SET);                      \
266                                                                   \
267         for (i = 0; i < 16; i++) {                                \
268             assign(dst[i * dstep], e_32[i] + o_32[i]);            \
269             assign(dst[(31 - i) * dstep], e_32[i] - o_32[i]);     \
270         }                                                         \
271     } while (0)
272
273 #define TR_8_1(dst, src) TR_8(dst, src, 8, 8, SCALE)
274 #define TR_16_1(dst, src) TR_16(dst, src, 16, 16, SCALE)
275 #define TR_32_1(dst, src) TR_32(dst, src, 32, 32, SCALE)
276
277 #define TR_8_2(dst, src) TR_8(dst, src, 1, 1, ADD_AND_SCALE)
278 #define TR_16_2(dst, src) TR_16(dst, src, 1, 1, ADD_AND_SCALE)
279 #define TR_32_2(dst, src) TR_32(dst, src, 1, 1, ADD_AND_SCALE)
280
281 static void FUNC(transform_8x8_add)(uint8_t *_dst, int16_t *coeffs,
282                                     ptrdiff_t stride)
283 {
284     int i;
285     pixel *dst   = (pixel *)_dst;
286     int shift    = 7;
287     int add      = 1 << (shift - 1);
288     int16_t *src = coeffs;
289
290     stride /= sizeof(pixel);
291
292     for (i = 0; i < 8; i++) {
293         TR_8_1(src, src);
294         src++;
295     }
296
297     shift = 20 - BIT_DEPTH;
298     add   = 1 << (shift - 1);
299     for (i = 0; i < 8; i++) {
300         TR_8_2(dst, coeffs);
301         coeffs += 8;
302         dst    += stride;
303     }
304 }
305
306 static void FUNC(transform_16x16_add)(uint8_t *_dst, int16_t *coeffs,
307                                       ptrdiff_t stride)
308 {
309     int i;
310     pixel *dst   = (pixel *)_dst;
311     int shift    = 7;
312     int add      = 1 << (shift - 1);
313     int16_t *src = coeffs;
314
315     stride /= sizeof(pixel);
316
317     for (i = 0; i < 16; i++) {
318         TR_16_1(src, src);
319         src++;
320     }
321
322     shift = 20 - BIT_DEPTH;
323     add   = 1 << (shift - 1);
324     for (i = 0; i < 16; i++) {
325         TR_16_2(dst, coeffs);
326         coeffs += 16;
327         dst    += stride;
328     }
329 }
330
331 static void FUNC(transform_32x32_add)(uint8_t *_dst, int16_t *coeffs,
332                                       ptrdiff_t stride)
333 {
334 #define IT32x32_even(i,w) ( src[ 0*w] * transform[ 0][i] ) + ( src[16*w] * transform[16][i] )
335 #define IT32x32_odd(i,w)  ( src[ 8*w] * transform[ 8][i] ) + ( src[24*w] * transform[24][i] )
336 #define IT16x16(i,w)      ( src[ 4*w] * transform[ 4][i] ) + ( src[12*w] * transform[12][i] ) + ( src[20*w] * transform[20][i] ) + ( src[28*w] * transform[28][i] )
337 #define IT8x8(i,w)        ( src[ 2*w] * transform[ 2][i] ) + ( src[ 6*w] * transform[ 6][i] ) + ( src[10*w] * transform[10][i] ) + ( src[14*w] * transform[14][i] ) + \
338                           ( src[18*w] * transform[18][i] ) + ( src[22*w] * transform[22][i] ) + ( src[26*w] * transform[26][i] ) + ( src[30*w] * transform[30][i] )
339 #define IT4x4(i,w)        ( src[ 1*w] * transform[ 1][i] ) + ( src[ 3*w] * transform[ 3][i] ) + ( src[ 5*w] * transform[ 5][i] ) + ( src[ 7*w] * transform[ 7][i] ) + \
340                           ( src[ 9*w] * transform[ 9][i] ) + ( src[11*w] * transform[11][i] ) + ( src[13*w] * transform[13][i] ) + ( src[15*w] * transform[15][i] ) + \
341                           ( src[17*w] * transform[17][i] ) + ( src[19*w] * transform[19][i] ) + ( src[21*w] * transform[21][i] ) + ( src[23*w] * transform[23][i] ) + \
342                           ( src[25*w] * transform[25][i] ) + ( src[27*w] * transform[27][i] ) + ( src[29*w] * transform[29][i] ) + ( src[31*w] * transform[31][i] )
343     int i;
344     pixel *dst   = (pixel *)_dst;
345     int shift    = 7;
346     int add      = 1 << (shift - 1);
347     int16_t *src = coeffs;
348
349     stride /= sizeof(pixel);
350
351     for (i = 0; i < 32; i++) {
352         TR_32_1(src, src);
353         src++;
354     }
355     src   = coeffs;
356     shift = 20 - BIT_DEPTH;
357     add   = 1 << (shift - 1);
358     for (i = 0; i < 32; i++) {
359         TR_32_2(dst, coeffs);
360         coeffs += 32;
361         dst    += stride;
362     }
363 #undef IT32x32_even
364 #undef IT32x32_odd
365 #undef IT16x16
366 #undef IT8x8
367 #undef IT4x4
368 }
369
370 static void FUNC(sao_band_filter)(uint8_t *_dst, uint8_t *_src,
371                                   ptrdiff_t stride, SAOParams *sao,
372                                   int *borders, int width, int height,
373                                   int c_idx, int class)
374 {
375     pixel *dst = (pixel *)_dst;
376     pixel *src = (pixel *)_src;
377     int offset_table[32] = { 0 };
378     int k, y, x;
379     int chroma = !!c_idx;
380     int shift  = BIT_DEPTH - 5;
381     int *sao_offset_val = sao->offset_val[c_idx];
382     int sao_left_class  = sao->band_position[c_idx];
383     int init_y = 0, init_x = 0;
384
385     stride /= sizeof(pixel);
386
387     switch (class) {
388     case 0:
389         if (!borders[2])
390             width -= (8 >> chroma) + 2;
391         if (!borders[3])
392             height -= (4 >> chroma) + 2;
393         break;
394     case 1:
395         init_y = -(4 >> chroma) - 2;
396         if (!borders[2])
397             width -= (8 >> chroma) + 2;
398         height = (4 >> chroma) + 2;
399         break;
400     case 2:
401         init_x = -(8 >> chroma) - 2;
402         width  =  (8 >> chroma) + 2;
403         if (!borders[3])
404             height -= (4 >> chroma) + 2;
405         break;
406     case 3:
407         init_y = -(4 >> chroma) - 2;
408         init_x = -(8 >> chroma) - 2;
409         width  =  (8 >> chroma) + 2;
410         height =  (4 >> chroma) + 2;
411         break;
412     }
413
414     dst = dst + (init_y * stride + init_x);
415     src = src + (init_y * stride + init_x);
416     for (k = 0; k < 4; k++)
417         offset_table[(k + sao_left_class) & 31] = sao_offset_val[k + 1];
418     for (y = 0; y < height; y++) {
419         for (x = 0; x < width; x++)
420             dst[x] = av_clip_pixel(src[x] + offset_table[av_clip_pixel(src[x] >> shift)]);
421         dst += stride;
422         src += stride;
423     }
424 }
425
426 static void FUNC(sao_band_filter_0)(uint8_t *dst, uint8_t *src,
427                                     ptrdiff_t stride, SAOParams *sao,
428                                     int *borders, int width, int height,
429                                     int c_idx)
430 {
431     FUNC(sao_band_filter)(dst, src, stride, sao, borders,
432                           width, height, c_idx, 0);
433 }
434
435 static void FUNC(sao_band_filter_1)(uint8_t *dst, uint8_t *src,
436                                     ptrdiff_t stride, SAOParams *sao,
437                                     int *borders, int width, int height,
438                                     int c_idx)
439 {
440     FUNC(sao_band_filter)(dst, src, stride, sao, borders,
441                           width, height, c_idx, 1);
442 }
443
444 static void FUNC(sao_band_filter_2)(uint8_t *dst, uint8_t *src,
445                                     ptrdiff_t stride, SAOParams *sao,
446                                     int *borders, int width, int height,
447                                     int c_idx)
448 {
449     FUNC(sao_band_filter)(dst, src, stride, sao, borders,
450                           width, height, c_idx, 2);
451 }
452
453 static void FUNC(sao_band_filter_3)(uint8_t *_dst, uint8_t *_src,
454                                     ptrdiff_t stride, SAOParams *sao,
455                                     int *borders, int width, int height,
456                                     int c_idx)
457 {
458     FUNC(sao_band_filter)(_dst, _src, stride, sao, borders,
459                           width, height, c_idx, 3);
460 }
461
462 static void FUNC(sao_edge_filter_0)(uint8_t *_dst, uint8_t *_src,
463                                     ptrdiff_t stride, SAOParams *sao,
464                                     int *borders, int _width, int _height,
465                                     int c_idx, uint8_t vert_edge,
466                                     uint8_t horiz_edge, uint8_t diag_edge)
467 {
468     int x, y;
469     pixel *dst = (pixel *)_dst;
470     pixel *src = (pixel *)_src;
471     int chroma = !!c_idx;
472     int *sao_offset_val = sao->offset_val[c_idx];
473     int sao_eo_class    = sao->eo_class[c_idx];
474     int init_x = 0, init_y = 0, width = _width, height = _height;
475
476     static const int8_t pos[4][2][2] = {
477         { { -1,  0 }, {  1, 0 } }, // horizontal
478         { {  0, -1 }, {  0, 1 } }, // vertical
479         { { -1, -1 }, {  1, 1 } }, // 45 degree
480         { {  1, -1 }, { -1, 1 } }, // 135 degree
481     };
482     static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
483
484 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
485
486     stride /= sizeof(pixel);
487
488     if (!borders[2])
489         width -= (8 >> chroma) + 2;
490     if (!borders[3])
491         height -= (4 >> chroma) + 2;
492
493     dst = dst + (init_y * stride + init_x);
494     src = src + (init_y * stride + init_x);
495     init_y = init_x = 0;
496     if (sao_eo_class != SAO_EO_VERT) {
497         if (borders[0]) {
498             int offset_val = sao_offset_val[0];
499             int y_stride   = 0;
500             for (y = 0; y < height; y++) {
501                 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
502                 y_stride     += stride;
503             }
504             init_x = 1;
505         }
506         if (borders[2]) {
507             int offset_val = sao_offset_val[0];
508             int x_stride   = width - 1;
509             for (x = 0; x < height; x++) {
510                 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
511                 x_stride     += stride;
512             }
513             width--;
514         }
515     }
516     if (sao_eo_class != SAO_EO_HORIZ) {
517         if (borders[1]) {
518             int offset_val = sao_offset_val[0];
519             for (x = init_x; x < width; x++)
520                 dst[x] = av_clip_pixel(src[x] + offset_val);
521             init_y = 1;
522         }
523         if (borders[3]) {
524             int offset_val = sao_offset_val[0];
525             int y_stride   = stride * (height - 1);
526             for (x = init_x; x < width; x++)
527                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
528             height--;
529         }
530     }
531     {
532         int y_stride = init_y * stride;
533         int pos_0_0  = pos[sao_eo_class][0][0];
534         int pos_0_1  = pos[sao_eo_class][0][1];
535         int pos_1_0  = pos[sao_eo_class][1][0];
536         int pos_1_1  = pos[sao_eo_class][1][1];
537
538         int y_stride_0_1 = (init_y + pos_0_1) * stride;
539         int y_stride_1_1 = (init_y + pos_1_1) * stride;
540         for (y = init_y; y < height; y++) {
541             for (x = init_x; x < width; x++) {
542                 int diff0         = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
543                 int diff1         = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
544                 int offset_val    = edge_idx[2 + diff0 + diff1];
545                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
546             }
547             y_stride     += stride;
548             y_stride_0_1 += stride;
549             y_stride_1_1 += stride;
550         }
551     }
552
553     {
554         // Restore pixels that can't be modified
555         int save_upper_left = !diag_edge && sao_eo_class == SAO_EO_135D && !borders[0] && !borders[1];
556         if (vert_edge && sao_eo_class != SAO_EO_VERT)
557             for (y = init_y+save_upper_left; y< height; y++)
558                 dst[y*stride] = src[y*stride];
559         if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
560             for(x = init_x+save_upper_left; x<width; x++)
561                 dst[x] = src[x];
562         if(diag_edge && sao_eo_class == SAO_EO_135D)
563             dst[0] = src[0];
564     }
565
566 #undef CMP
567 }
568
569 static void FUNC(sao_edge_filter_1)(uint8_t *_dst, uint8_t *_src,
570                                     ptrdiff_t stride, SAOParams *sao,
571                                     int *borders, int _width, int _height,
572                                     int c_idx, uint8_t vert_edge,
573                                     uint8_t horiz_edge, uint8_t diag_edge)
574 {
575     int x, y;
576     pixel *dst = (pixel *)_dst;
577     pixel *src = (pixel *)_src;
578     int chroma = !!c_idx;
579     int *sao_offset_val = sao->offset_val[c_idx];
580     int sao_eo_class    = sao->eo_class[c_idx];
581     int init_x = 0, init_y = 0, width = _width, height = _height;
582
583     static const int8_t pos[4][2][2] = {
584         { { -1, 0  }, { 1,  0 } }, // horizontal
585         { { 0,  -1 }, { 0,  1 } }, // vertical
586         { { -1, -1 }, { 1,  1 } }, // 45 degree
587         { { 1,  -1 }, { -1, 1 } }, // 135 degree
588     };
589     static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
590
591 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
592
593     stride /= sizeof(pixel);
594
595     init_y = -(4 >> chroma) - 2;
596     if (!borders[2])
597         width -= (8 >> chroma) + 2;
598     height = (4 >> chroma) + 2;
599
600     dst = dst + (init_y * stride + init_x);
601     src = src + (init_y * stride + init_x);
602     init_y = init_x = 0;
603     if (sao_eo_class != SAO_EO_VERT) {
604         if (borders[0]) {
605             int offset_val = sao_offset_val[0];
606             int y_stride   = 0;
607             for (y = 0; y < height; y++) {
608                 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
609                 y_stride     += stride;
610             }
611             init_x = 1;
612         }
613         if (borders[2]) {
614             int offset_val = sao_offset_val[0];
615             int x_stride   = width - 1;
616             for (x = 0; x < height; x++) {
617                 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
618                 x_stride     += stride;
619             }
620             width--;
621         }
622     }
623     {
624         int y_stride = init_y * stride;
625         int pos_0_0  = pos[sao_eo_class][0][0];
626         int pos_0_1  = pos[sao_eo_class][0][1];
627         int pos_1_0  = pos[sao_eo_class][1][0];
628         int pos_1_1  = pos[sao_eo_class][1][1];
629
630         int y_stride_0_1 = (init_y + pos_0_1) * stride;
631         int y_stride_1_1 = (init_y + pos_1_1) * stride;
632         for (y = init_y; y < height; y++) {
633             for (x = init_x; x < width; x++) {
634                 int diff0         = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
635                 int diff1         = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
636                 int offset_val    = edge_idx[2 + diff0 + diff1];
637                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
638             }
639             y_stride     += stride;
640             y_stride_0_1 += stride;
641             y_stride_1_1 += stride;
642         }
643     }
644
645     {
646         // Restore pixels that can't be modified
647         int save_lower_left = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[0];
648         if(vert_edge && sao_eo_class != SAO_EO_VERT)
649             for(y = init_y; y< height-save_lower_left; y++)
650                 dst[y*stride] = src[y*stride];
651         if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
652             for(x = init_x+save_lower_left; x<width; x++)
653                 dst[(height-1)*stride+x] = src[(height-1)*stride+x];
654         if(diag_edge && sao_eo_class == SAO_EO_45D)
655             dst[stride*(height-1)] = src[stride*(height-1)];
656     }
657
658 #undef CMP
659 }
660
661 static void FUNC(sao_edge_filter_2)(uint8_t *_dst, uint8_t *_src,
662                                     ptrdiff_t stride, SAOParams *sao,
663                                     int *borders, int _width, int _height,
664                                     int c_idx, uint8_t vert_edge,
665                                     uint8_t horiz_edge, uint8_t diag_edge)
666 {
667     int x, y;
668     pixel *dst = (pixel *)_dst;
669     pixel *src = (pixel *)_src;
670     int chroma = !!c_idx;
671     int *sao_offset_val = sao->offset_val[c_idx];
672     int sao_eo_class    = sao->eo_class[c_idx];
673
674     static const int8_t pos[4][2][2] = {
675         { { -1,  0 }, {  1, 0 } }, // horizontal
676         { {  0, -1 }, {  0, 1 } }, // vertical
677         { { -1, -1 }, {  1, 1 } }, // 45 degree
678         { {  1, -1 }, { -1, 1 } }, // 135 degree
679     };
680     static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
681
682     int init_x = 0, init_y = 0, width = _width, height = _height;
683
684 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
685     stride /= sizeof(pixel);
686
687     init_x = -(8 >> chroma) - 2;
688     width  =  (8 >> chroma) + 2;
689     if (!borders[3])
690         height -= (4 >> chroma) + 2;
691
692     dst = dst + (init_y * stride + init_x);
693     src = src + (init_y * stride + init_x);
694     init_y = init_x = 0;
695     if (sao_eo_class != SAO_EO_HORIZ) {
696         if (borders[1]) {
697             int offset_val = sao_offset_val[0];
698             for (x = init_x; x < width; x++)
699                 dst[x] = av_clip_pixel(src[x] + offset_val);
700             init_y = 1;
701         }
702         if (borders[3]) {
703             int offset_val = sao_offset_val[0];
704             int y_stride   = stride * (height - 1);
705             for (x = init_x; x < width; x++)
706                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
707             height--;
708         }
709     }
710     {
711         int y_stride = init_y * stride;
712         int pos_0_0  = pos[sao_eo_class][0][0];
713         int pos_0_1  = pos[sao_eo_class][0][1];
714         int pos_1_0  = pos[sao_eo_class][1][0];
715         int pos_1_1  = pos[sao_eo_class][1][1];
716
717         int y_stride_0_1 = (init_y + pos_0_1) * stride;
718         int y_stride_1_1 = (init_y + pos_1_1) * stride;
719         for (y = init_y; y < height; y++) {
720             for (x = init_x; x < width; x++) {
721                 int diff0         = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
722                 int diff1         = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
723                 int offset_val    = edge_idx[2 + diff0 + diff1];
724                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
725             }
726             y_stride     += stride;
727             y_stride_0_1 += stride;
728             y_stride_1_1 += stride;
729         }
730     }
731
732     {
733         // Restore pixels that can't be modified
734         int save_upper_right = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[1];
735         if(vert_edge && sao_eo_class != SAO_EO_VERT)
736             for(y = init_y+save_upper_right; y< height; y++)
737                 dst[y*stride+width-1] = src[y*stride+width-1];
738         if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
739             for(x = init_x; x<width-save_upper_right; x++)
740                 dst[x] = src[x];
741         if(diag_edge && sao_eo_class == SAO_EO_45D)
742             dst[width-1] = src[width-1];
743     }
744 #undef CMP
745 }
746
747 static void FUNC(sao_edge_filter_3)(uint8_t *_dst, uint8_t *_src,
748                                     ptrdiff_t stride, SAOParams *sao,
749                                     int *borders, int _width, int _height,
750                                     int c_idx, uint8_t vert_edge,
751                                     uint8_t horiz_edge, uint8_t diag_edge)
752 {
753     int x, y;
754     pixel *dst = (pixel *)_dst;
755     pixel *src = (pixel *)_src;
756     int chroma = !!c_idx;
757     int *sao_offset_val = sao->offset_val[c_idx];
758     int sao_eo_class    = sao->eo_class[c_idx];
759     int init_x = 0, init_y = 0, width = _width, height = _height;
760
761     static const int8_t pos[4][2][2] = {
762         { { -1,  0 }, {  1, 0 } }, // horizontal
763         { {  0, -1 }, {  0, 1 } }, // vertical
764         { { -1, -1 }, {  1, 1 } }, // 45 degree
765         { {  1, -1 }, { -1, 1 } }, // 135 degree
766     };
767     static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
768
769 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
770
771     stride /= sizeof(pixel);
772
773     init_y = -(4 >> chroma) - 2;
774     init_x = -(8 >> chroma) - 2;
775     width  =  (8 >> chroma) + 2;
776     height =  (4 >> chroma) + 2;
777
778
779     dst    = dst + (init_y * stride + init_x);
780     src    = src + (init_y * stride + init_x);
781     init_y = init_x = 0;
782
783     {
784         int y_stride = init_y * stride;
785         int pos_0_0  = pos[sao_eo_class][0][0];
786         int pos_0_1  = pos[sao_eo_class][0][1];
787         int pos_1_0  = pos[sao_eo_class][1][0];
788         int pos_1_1  = pos[sao_eo_class][1][1];
789
790         int y_stride_0_1 = (init_y + pos_0_1) * stride;
791         int y_stride_1_1 = (init_y + pos_1_1) * stride;
792
793         for (y = init_y; y < height; y++) {
794             for (x = init_x; x < width; x++) {
795                 int diff0         = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
796                 int diff1         = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
797                 int offset_val    = edge_idx[2 + diff0 + diff1];
798                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
799             }
800             y_stride     += stride;
801             y_stride_0_1 += stride;
802             y_stride_1_1 += stride;
803         }
804     }
805
806     {
807         // Restore pixels that can't be modified
808         int save_lower_right = !diag_edge && sao_eo_class == SAO_EO_135D;
809         if(vert_edge && sao_eo_class != SAO_EO_VERT)
810             for(y = init_y; y< height-save_lower_right; y++)
811                 dst[y*stride+width-1] = src[y*stride+width-1];
812         if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
813             for(x = init_x; x<width-save_lower_right; x++)
814                 dst[(height-1)*stride+x] = src[(height-1)*stride+x];
815         if(diag_edge && sao_eo_class == SAO_EO_135D)
816             dst[stride*(height-1)+width-1] = src[stride*(height-1)+width-1];
817     }
818 #undef CMP
819 }
820
821 #undef SET
822 #undef SCALE
823 #undef ADD_AND_SCALE
824 #undef TR_4
825 #undef TR_4_1
826 #undef TR_4_2
827 #undef TR_8
828 #undef TR_8_1
829 #undef TR_8_2
830 #undef TR_16
831 #undef TR_16_1
832 #undef TR_16_2
833 #undef TR_32
834 #undef TR_32_1
835 #undef TR_32_2
836
837 static void FUNC(put_hevc_qpel_pixels)(int16_t *dst, ptrdiff_t dststride,
838                                        uint8_t *_src, ptrdiff_t _srcstride,
839                                        int width, int height, int16_t* mcbuffer)
840 {
841     int x, y;
842     pixel *src          = (pixel *)_src;
843     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
844
845     for (y = 0; y < height; y++) {
846         for (x = 0; x < width; x++)
847             dst[x] = src[x] << (14 - BIT_DEPTH);
848         src += srcstride;
849         dst += dststride;
850     }
851 }
852
853 #define QPEL_FILTER_1(src, stride)      \
854     (1 * -src[x - 3 * stride] +         \
855      4 *  src[x - 2 * stride] -         \
856     10 *  src[x -     stride] +         \
857     58 *  src[x]              +         \
858     17 *  src[x +     stride] -         \
859      5 *  src[x + 2 * stride] +         \
860      1 *  src[x + 3 * stride])
861
862 #define QPEL_FILTER_2(src, stride)      \
863     (1  * -src[x - 3 * stride] +        \
864      4  *  src[x - 2 * stride] -        \
865     11  *  src[x -     stride] +        \
866     40  *  src[x]              +        \
867     40  *  src[x +     stride] -        \
868     11  *  src[x + 2 * stride] +        \
869      4  *  src[x + 3 * stride] -        \
870      1  *  src[x + 4 * stride])
871
872 #define QPEL_FILTER_3(src, stride)      \
873     (1  * src[x - 2 * stride] -         \
874      5  * src[x -     stride] +         \
875     17  * src[x]              +         \
876     58  * src[x + stride]     -         \
877     10  * src[x + 2 * stride] +         \
878      4  * src[x + 3 * stride] -         \
879      1  * src[x + 4 * stride])
880
881
882 #define PUT_HEVC_QPEL_H(H)                                                     \
883 static void FUNC(put_hevc_qpel_h ## H)(int16_t *dst,  ptrdiff_t dststride,     \
884                                        uint8_t *_src, ptrdiff_t _srcstride,    \
885                                        int width, int height,                  \
886                                        int16_t* mcbuffer)                      \
887 {                                                                              \
888     int x, y;                                                                  \
889     pixel *src = (pixel*)_src;                                                 \
890     ptrdiff_t srcstride = _srcstride / sizeof(pixel);                          \
891                                                                                \
892     for (y = 0; y < height; y++) {                                             \
893         for (x = 0; x < width; x++)                                            \
894             dst[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8);             \
895         src += srcstride;                                                      \
896         dst += dststride;                                                      \
897     }                                                                          \
898 }
899
900 #define PUT_HEVC_QPEL_V(V)                                                     \
901 static void FUNC(put_hevc_qpel_v ## V)(int16_t *dst,  ptrdiff_t dststride,     \
902                                        uint8_t *_src, ptrdiff_t _srcstride,    \
903                                        int width, int height,                  \
904                                        int16_t* mcbuffer)                      \
905 {                                                                              \
906     int x, y;                                                                  \
907     pixel *src = (pixel*)_src;                                                 \
908     ptrdiff_t srcstride = _srcstride / sizeof(pixel);                          \
909                                                                                \
910     for (y = 0; y < height; y++)  {                                            \
911         for (x = 0; x < width; x++)                                            \
912             dst[x] = QPEL_FILTER_ ## V(src, srcstride) >> (BIT_DEPTH - 8);     \
913         src += srcstride;                                                      \
914         dst += dststride;                                                      \
915     }                                                                          \
916 }
917
918 #define PUT_HEVC_QPEL_HV(H, V)                                                 \
919 static void FUNC(put_hevc_qpel_h ## H ## v ## V)(int16_t *dst,                 \
920                                                  ptrdiff_t dststride,          \
921                                                  uint8_t *_src,                \
922                                                  ptrdiff_t _srcstride,         \
923                                                  int width, int height,        \
924                                                  int16_t* mcbuffer)            \
925 {                                                                              \
926     int x, y;                                                                  \
927     pixel *src = (pixel*)_src;                                                 \
928     ptrdiff_t srcstride = _srcstride / sizeof(pixel);                          \
929                                                                                \
930     int16_t tmp_array[(MAX_PB_SIZE + 7) * MAX_PB_SIZE];                        \
931     int16_t *tmp = tmp_array;                                                  \
932                                                                                \
933     src -= ff_hevc_qpel_extra_before[V] * srcstride;                           \
934                                                                                \
935     for (y = 0; y < height + ff_hevc_qpel_extra[V]; y++) {                     \
936         for (x = 0; x < width; x++)                                            \
937             tmp[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8);             \
938         src += srcstride;                                                      \
939         tmp += MAX_PB_SIZE;                                                    \
940     }                                                                          \
941                                                                                \
942     tmp = tmp_array + ff_hevc_qpel_extra_before[V] * MAX_PB_SIZE;              \
943                                                                                \
944     for (y = 0; y < height; y++) {                                             \
945         for (x = 0; x < width; x++)                                            \
946             dst[x] = QPEL_FILTER_ ## V(tmp, MAX_PB_SIZE) >> 6;                 \
947         tmp += MAX_PB_SIZE;                                                    \
948         dst += dststride;                                                      \
949     }                                                                          \
950 }
951
952 PUT_HEVC_QPEL_H(1)
953 PUT_HEVC_QPEL_H(2)
954 PUT_HEVC_QPEL_H(3)
955 PUT_HEVC_QPEL_V(1)
956 PUT_HEVC_QPEL_V(2)
957 PUT_HEVC_QPEL_V(3)
958 PUT_HEVC_QPEL_HV(1, 1)
959 PUT_HEVC_QPEL_HV(1, 2)
960 PUT_HEVC_QPEL_HV(1, 3)
961 PUT_HEVC_QPEL_HV(2, 1)
962 PUT_HEVC_QPEL_HV(2, 2)
963 PUT_HEVC_QPEL_HV(2, 3)
964 PUT_HEVC_QPEL_HV(3, 1)
965 PUT_HEVC_QPEL_HV(3, 2)
966 PUT_HEVC_QPEL_HV(3, 3)
967
968 static void FUNC(put_hevc_epel_pixels)(int16_t *dst, ptrdiff_t dststride,
969                                        uint8_t *_src, ptrdiff_t _srcstride,
970                                        int width, int height, int mx, int my,
971                                        int16_t* mcbuffer)
972 {
973     int x, y;
974     pixel *src          = (pixel *)_src;
975     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
976
977     for (y = 0; y < height; y++) {
978         for (x = 0; x < width; x++)
979             dst[x] = src[x] << (14 - BIT_DEPTH);
980         src += srcstride;
981         dst += dststride;
982     }
983 }
984
985 #define EPEL_FILTER(src, stride)                \
986     (filter_0 * src[x - stride] +               \
987      filter_1 * src[x]          +               \
988      filter_2 * src[x + stride] +               \
989      filter_3 * src[x + 2 * stride])
990
991 static void FUNC(put_hevc_epel_h)(int16_t *dst, ptrdiff_t dststride,
992                                   uint8_t *_src, ptrdiff_t _srcstride,
993                                   int width, int height, int mx, int my,
994                                   int16_t* mcbuffer)
995 {
996     int x, y;
997     pixel *src = (pixel *)_src;
998     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
999     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1000     int8_t filter_0 = filter[0];
1001     int8_t filter_1 = filter[1];
1002     int8_t filter_2 = filter[2];
1003     int8_t filter_3 = filter[3];
1004     for (y = 0; y < height; y++) {
1005         for (x = 0; x < width; x++)
1006             dst[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1007         src += srcstride;
1008         dst += dststride;
1009     }
1010 }
1011
1012 static void FUNC(put_hevc_epel_v)(int16_t *dst, ptrdiff_t dststride,
1013                                   uint8_t *_src, ptrdiff_t _srcstride,
1014                                   int width, int height, int mx, int my,
1015                                   int16_t* mcbuffer)
1016 {
1017     int x, y;
1018     pixel *src = (pixel *)_src;
1019     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1020     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1021     int8_t filter_0 = filter[0];
1022     int8_t filter_1 = filter[1];
1023     int8_t filter_2 = filter[2];
1024     int8_t filter_3 = filter[3];
1025
1026     for (y = 0; y < height; y++) {
1027         for (x = 0; x < width; x++)
1028             dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
1029         src += srcstride;
1030         dst += dststride;
1031     }
1032 }
1033
1034 static void FUNC(put_hevc_epel_hv)(int16_t *dst, ptrdiff_t dststride,
1035                                    uint8_t *_src, ptrdiff_t _srcstride,
1036                                    int width, int height, int mx, int my,
1037                                    int16_t* mcbuffer)
1038 {
1039     int x, y;
1040     pixel *src = (pixel *)_src;
1041     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1042     const int8_t *filter_h = ff_hevc_epel_filters[mx - 1];
1043     const int8_t *filter_v = ff_hevc_epel_filters[my - 1];
1044     int8_t filter_0 = filter_h[0];
1045     int8_t filter_1 = filter_h[1];
1046     int8_t filter_2 = filter_h[2];
1047     int8_t filter_3 = filter_h[3];
1048     int16_t tmp_array[(MAX_PB_SIZE + 3) * MAX_PB_SIZE];
1049     int16_t *tmp = tmp_array;
1050
1051     src -= EPEL_EXTRA_BEFORE * srcstride;
1052
1053     for (y = 0; y < height + EPEL_EXTRA; y++) {
1054         for (x = 0; x < width; x++)
1055             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1056         src += srcstride;
1057         tmp += MAX_PB_SIZE;
1058     }
1059
1060     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1061     filter_0 = filter_v[0];
1062     filter_1 = filter_v[1];
1063     filter_2 = filter_v[2];
1064     filter_3 = filter_v[3];
1065     for (y = 0; y < height; y++) {
1066         for (x = 0; x < width; x++)
1067             dst[x] = EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
1068         tmp += MAX_PB_SIZE;
1069         dst += dststride;
1070     }
1071 }
1072
1073 static void FUNC(put_unweighted_pred)(uint8_t *_dst, ptrdiff_t _dststride,
1074                                       int16_t *src, ptrdiff_t srcstride,
1075                                       int width, int height)
1076 {
1077     int x, y;
1078     pixel *dst          = (pixel *)_dst;
1079     ptrdiff_t dststride = _dststride / sizeof(pixel);
1080
1081     int shift = 14 - BIT_DEPTH;
1082 #if BIT_DEPTH < 14
1083     int offset = 1 << (shift - 1);
1084 #else
1085     int offset = 0;
1086 #endif
1087     for (y = 0; y < height; y++) {
1088         for (x = 0; x < width; x++)
1089             dst[x] = av_clip_pixel((src[x] + offset) >> shift);
1090         dst += dststride;
1091         src += srcstride;
1092     }
1093 }
1094
1095 static void FUNC(put_weighted_pred_avg)(uint8_t *_dst, ptrdiff_t _dststride,
1096                                         int16_t *src1, int16_t *src2,
1097                                         ptrdiff_t srcstride,
1098                                         int width, int height)
1099 {
1100     int x, y;
1101     pixel *dst          = (pixel *)_dst;
1102     ptrdiff_t dststride = _dststride / sizeof(pixel);
1103
1104     int shift = 14 + 1 - BIT_DEPTH;
1105 #if BIT_DEPTH < 14
1106     int offset = 1 << (shift - 1);
1107 #else
1108     int offset = 0;
1109 #endif
1110
1111     for (y = 0; y < height; y++) {
1112         for (x = 0; x < width; x++)
1113             dst[x] = av_clip_pixel((src1[x] + src2[x] + offset) >> shift);
1114         dst  += dststride;
1115         src1 += srcstride;
1116         src2 += srcstride;
1117     }
1118 }
1119
1120 static void FUNC(weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
1121                                 uint8_t *_dst, ptrdiff_t _dststride,
1122                                 int16_t *src, ptrdiff_t srcstride,
1123                                 int width, int height)
1124 {
1125     int shift, log2Wd, wx, ox, x, y, offset;
1126     pixel *dst          = (pixel *)_dst;
1127     ptrdiff_t dststride = _dststride / sizeof(pixel);
1128
1129     shift  = 14 - BIT_DEPTH;
1130     log2Wd = denom + shift;
1131     offset = 1 << (log2Wd - 1);
1132     wx     = wlxFlag;
1133     ox     = olxFlag * (1 << (BIT_DEPTH - 8));
1134
1135     for (y = 0; y < height; y++) {
1136         for (x = 0; x < width; x++) {
1137             if (log2Wd >= 1) {
1138                 dst[x] = av_clip_pixel(((src[x] * wx + offset) >> log2Wd) + ox);
1139             } else {
1140                 dst[x] = av_clip_pixel(src[x] * wx + ox);
1141             }
1142         }
1143         dst += dststride;
1144         src += srcstride;
1145     }
1146 }
1147
1148 static void FUNC(weighted_pred_avg)(uint8_t denom,
1149                                     int16_t wl0Flag, int16_t wl1Flag,
1150                                     int16_t ol0Flag, int16_t ol1Flag,
1151                                     uint8_t *_dst, ptrdiff_t _dststride,
1152                                     int16_t *src1, int16_t *src2,
1153                                     ptrdiff_t srcstride,
1154                                     int width, int height)
1155 {
1156     int shift, log2Wd, w0, w1, o0, o1, x, y;
1157     pixel *dst = (pixel *)_dst;
1158     ptrdiff_t dststride = _dststride / sizeof(pixel);
1159
1160     shift  = 14 - BIT_DEPTH;
1161     log2Wd = denom + shift;
1162     w0     = wl0Flag;
1163     w1     = wl1Flag;
1164     o0     = ol0Flag * (1 << (BIT_DEPTH - 8));
1165     o1     = ol1Flag * (1 << (BIT_DEPTH - 8));
1166
1167     for (y = 0; y < height; y++) {
1168         for (x = 0; x < width; x++)
1169             dst[x] = av_clip_pixel((src1[x] * w0 + src2[x] * w1 +
1170                                     ((o0 + o1 + 1) << log2Wd)) >> (log2Wd + 1));
1171         dst  += dststride;
1172         src1 += srcstride;
1173         src2 += srcstride;
1174     }
1175 }
1176
1177 // line zero
1178 #define P3 pix[-4 * xstride]
1179 #define P2 pix[-3 * xstride]
1180 #define P1 pix[-2 * xstride]
1181 #define P0 pix[-1 * xstride]
1182 #define Q0 pix[0 * xstride]
1183 #define Q1 pix[1 * xstride]
1184 #define Q2 pix[2 * xstride]
1185 #define Q3 pix[3 * xstride]
1186
1187 // line three. used only for deblocking decision
1188 #define TP3 pix[-4 * xstride + 3 * ystride]
1189 #define TP2 pix[-3 * xstride + 3 * ystride]
1190 #define TP1 pix[-2 * xstride + 3 * ystride]
1191 #define TP0 pix[-1 * xstride + 3 * ystride]
1192 #define TQ0 pix[0  * xstride + 3 * ystride]
1193 #define TQ1 pix[1  * xstride + 3 * ystride]
1194 #define TQ2 pix[2  * xstride + 3 * ystride]
1195 #define TQ3 pix[3  * xstride + 3 * ystride]
1196
1197 static void FUNC(hevc_loop_filter_luma)(uint8_t *_pix,
1198                                         ptrdiff_t _xstride, ptrdiff_t _ystride,
1199                                         int *_beta, int *_tc,
1200                                         uint8_t *_no_p, uint8_t *_no_q)
1201 {
1202     int d, j;
1203     pixel *pix        = (pixel *)_pix;
1204     ptrdiff_t xstride = _xstride / sizeof(pixel);
1205     ptrdiff_t ystride = _ystride / sizeof(pixel);
1206
1207     for (j = 0; j < 2; j++) {
1208         const int dp0  = abs(P2  - 2 * P1  + P0);
1209         const int dq0  = abs(Q2  - 2 * Q1  + Q0);
1210         const int dp3  = abs(TP2 - 2 * TP1 + TP0);
1211         const int dq3  = abs(TQ2 - 2 * TQ1 + TQ0);
1212         const int d0   = dp0 + dq0;
1213         const int d3   = dp3 + dq3;
1214         const int beta = _beta[j] << (BIT_DEPTH - 8);
1215         const int tc   = _tc[j]   << (BIT_DEPTH - 8);
1216         const int no_p = _no_p[j];
1217         const int no_q = _no_q[j];
1218
1219         if (d0 + d3 >= beta /*|| tc <= 0*/) {
1220             pix += 4 * ystride;
1221             continue;
1222         } else {
1223             const int beta_3 = beta >> 3;
1224             const int beta_2 = beta >> 2;
1225             const int tc25   = ((tc * 5 + 1) >> 1);
1226
1227             if (abs(P3  -  P0) + abs(Q3  -  Q0) < beta_3 && abs(P0  -  Q0) < tc25 &&
1228                 abs(TP3 - TP0) + abs(TQ3 - TQ0) < beta_3 && abs(TP0 - TQ0) < tc25 &&
1229                                       (d0 << 1) < beta_2 &&      (d3 << 1) < beta_2) {
1230                 // strong filtering
1231                 const int tc2 = tc << 1;
1232                 for (d = 0; d < 4; d++) {
1233                     const int p3 = P3;
1234                     const int p2 = P2;
1235                     const int p1 = P1;
1236                     const int p0 = P0;
1237                     const int q0 = Q0;
1238                     const int q1 = Q1;
1239                     const int q2 = Q2;
1240                     const int q3 = Q3;
1241                     if (!no_p) {
1242                         P0 = p0 + av_clip(((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) - p0, -tc2, tc2);
1243                         P1 = p1 + av_clip(((p2 + p1 + p0 + q0 + 2) >> 2) - p1, -tc2, tc2);
1244                         P2 = p2 + av_clip(((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) - p2, -tc2, tc2);
1245                     }
1246                     if (!no_q) {
1247                         Q0 = q0 + av_clip(((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) - q0, -tc2, tc2);
1248                         Q1 = q1 + av_clip(((p0 + q0 + q1 + q2 + 2) >> 2) - q1, -tc2, tc2);
1249                         Q2 = q2 + av_clip(((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) - q2, -tc2, tc2);
1250                     }
1251                     pix += ystride;
1252                 }
1253             } else { // normal filtering
1254                 int nd_p = 1;
1255                 int nd_q = 1;
1256                 const int tc_2 = tc >> 1;
1257                 if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
1258                     nd_p = 2;
1259                 if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
1260                     nd_q = 2;
1261
1262                 for (d = 0; d < 4; d++) {
1263                     const int p2 = P2;
1264                     const int p1 = P1;
1265                     const int p0 = P0;
1266                     const int q0 = Q0;
1267                     const int q1 = Q1;
1268                     const int q2 = Q2;
1269                     int delta0   = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
1270                     if (abs(delta0) < 10 * tc) {
1271                         delta0 = av_clip(delta0, -tc, tc);
1272                         if (!no_p)
1273                             P0 = av_clip_pixel(p0 + delta0);
1274                         if (!no_q)
1275                             Q0 = av_clip_pixel(q0 - delta0);
1276                         if (!no_p && nd_p > 1) {
1277                             const int deltap1 = av_clip((((p2 + p0 + 1) >> 1) - p1 + delta0) >> 1, -tc_2, tc_2);
1278                             P1 = av_clip_pixel(p1 + deltap1);
1279                         }
1280                         if (!no_q && nd_q > 1) {
1281                             const int deltaq1 = av_clip((((q2 + q0 + 1) >> 1) - q1 - delta0) >> 1, -tc_2, tc_2);
1282                             Q1 = av_clip_pixel(q1 + deltaq1);
1283                         }
1284                     }
1285                     pix += ystride;
1286                 }
1287             }
1288         }
1289     }
1290 }
1291
1292 static void FUNC(hevc_loop_filter_chroma)(uint8_t *_pix, ptrdiff_t _xstride,
1293                                           ptrdiff_t _ystride, int *_tc,
1294                                           uint8_t *_no_p, uint8_t *_no_q)
1295 {
1296     int d, j, no_p, no_q;
1297     pixel *pix        = (pixel *)_pix;
1298     ptrdiff_t xstride = _xstride / sizeof(pixel);
1299     ptrdiff_t ystride = _ystride / sizeof(pixel);
1300
1301     for (j = 0; j < 2; j++) {
1302         const int tc = _tc[j] << (BIT_DEPTH - 8);
1303         if (tc <= 0) {
1304             pix += 4 * ystride;
1305             continue;
1306         }
1307         no_p = _no_p[j];
1308         no_q = _no_q[j];
1309
1310         for (d = 0; d < 4; d++) {
1311             int delta0;
1312             const int p1 = P1;
1313             const int p0 = P0;
1314             const int q0 = Q0;
1315             const int q1 = Q1;
1316             delta0 = av_clip((((q0 - p0) << 2) + p1 - q1 + 4) >> 3, -tc, tc);
1317             if (!no_p)
1318                 P0 = av_clip_pixel(p0 + delta0);
1319             if (!no_q)
1320                 Q0 = av_clip_pixel(q0 - delta0);
1321             pix += ystride;
1322         }
1323     }
1324 }
1325
1326 static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1327                                             int *tc, uint8_t *no_p,
1328                                             uint8_t *no_q)
1329 {
1330     FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
1331 }
1332
1333 static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1334                                             int *tc, uint8_t *no_p,
1335                                             uint8_t *no_q)
1336 {
1337     FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
1338 }
1339
1340 static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1341                                           int *beta, int *tc, uint8_t *no_p,
1342                                           uint8_t *no_q)
1343 {
1344     FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel),
1345                                 beta, tc, no_p, no_q);
1346 }
1347
1348 static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1349                                           int *beta, int *tc, uint8_t *no_p,
1350                                           uint8_t *no_q)
1351 {
1352     FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride,
1353                                 beta, tc, no_p, no_q);
1354 }
1355
1356 #undef P3
1357 #undef P2
1358 #undef P1
1359 #undef P0
1360 #undef Q0
1361 #undef Q1
1362 #undef Q2
1363 #undef Q3
1364
1365 #undef TP3
1366 #undef TP2
1367 #undef TP1
1368 #undef TP0
1369 #undef TQ0
1370 #undef TQ1
1371 #undef TQ2
1372 #undef TQ3