]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevcdsp_template.c
hevc/rext: basic infrastructure for supporting range extension
[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 width, int height,
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 < height; y++) {
39         for (x = 0; x < width; 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(transform_add4x4)(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] = av_clip_pixel(dst[x] + *coeffs);
56             coeffs++;
57         }
58         dst += stride;
59     }
60 }
61
62 static void FUNC(transform_add8x8)(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] = av_clip_pixel(dst[x] + *coeffs);
73             coeffs++;
74         }
75         dst += stride;
76     }
77 }
78
79 static void FUNC(transform_add16x16)(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] = av_clip_pixel(dst[x] + *coeffs);
90             coeffs++;
91         }
92         dst += stride;
93     }
94 }
95
96 static void FUNC(transform_add32x32)(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] = av_clip_pixel(dst[x] + *coeffs);
107             coeffs++;
108         }
109         dst += stride;
110     }
111 }
112
113 static void FUNC(transform_skip)(int16_t *_coeffs, int16_t log2_size)
114 {
115     int shift  = 15 - BIT_DEPTH - log2_size;
116     int x, y;
117     int size = 1 << log2_size;
118     int16_t *coeffs = _coeffs;
119
120
121     if (shift > 0) {
122         int offset = 1 << (shift - 1);
123         for (y = 0; y < size; y++) {
124             for (x = 0; x < size; x++) {
125                 *coeffs = (*coeffs + offset) >> shift;
126                 coeffs++;
127             }
128         }
129     } else {
130         for (y = 0; y < size; y++) {
131             for (x = 0; x < size; x++) {
132                 *coeffs = *coeffs << -shift;
133                 coeffs++;
134             }
135         }
136     }
137 }
138
139 #define SET(dst, x)   (dst) = (x)
140 #define SCALE(dst, x) (dst) = av_clip_int16(((x) + add) >> shift)
141 #define ADD_AND_SCALE(dst, x)                                           \
142     (dst) = av_clip_pixel((dst) + av_clip_int16(((x) + add) >> shift))
143
144 #define TR_4x4_LUMA(dst, src, step, assign)                             \
145     do {                                                                \
146         int c0 = src[0 * step] + src[2 * step];                         \
147         int c1 = src[2 * step] + src[3 * step];                         \
148         int c2 = src[0 * step] - src[3 * step];                         \
149         int c3 = 74 * src[1 * step];                                    \
150                                                                         \
151         assign(dst[2 * step], 74 * (src[0 * step] -                     \
152                                     src[2 * step] +                     \
153                                     src[3 * step]));                    \
154         assign(dst[0 * step], 29 * c0 + 55 * c1 + c3);                  \
155         assign(dst[1 * step], 55 * c2 - 29 * c1 + c3);                  \
156         assign(dst[3 * step], 55 * c0 + 29 * c2 - c3);                  \
157     } while (0)
158
159 static void FUNC(transform_4x4_luma)(int16_t *coeffs)
160 {
161     int i;
162     int shift    = 7;
163     int add      = 1 << (shift - 1);
164     int16_t *src = coeffs;
165
166     for (i = 0; i < 4; i++) {
167         TR_4x4_LUMA(src, src, 4, SCALE);
168         src++;
169     }
170
171     shift = 20 - BIT_DEPTH;
172     add   = 1 << (shift - 1);
173     for (i = 0; i < 4; i++) {
174         TR_4x4_LUMA(coeffs, coeffs, 1, SCALE);
175         coeffs += 4;
176     }
177 }
178
179 #undef TR_4x4_LUMA
180
181 #define TR_4(dst, src, dstep, sstep, assign, end)                              \
182     do {                                                                       \
183         const int e0 = 64 * src[0 * sstep] + 64 * src[2 * sstep];              \
184         const int e1 = 64 * src[0 * sstep] - 64 * src[2 * sstep];              \
185         const int o0 = 83 * src[1 * sstep] + 36 * src[3 * sstep];              \
186         const int o1 = 36 * src[1 * sstep] - 83 * src[3 * sstep];              \
187                                                                                \
188         assign(dst[0 * dstep], e0 + o0);                                       \
189         assign(dst[1 * dstep], e1 + o1);                                       \
190         assign(dst[2 * dstep], e1 - o1);                                       \
191         assign(dst[3 * dstep], e0 - o0);                                       \
192     } while (0)
193
194 #define TR_8(dst, src, dstep, sstep, assign, end)                              \
195     do {                                                                       \
196         int i, j;                                                              \
197         int e_8[4];                                                            \
198         int o_8[4] = { 0 };                                                    \
199         for (i = 0; i < 4; i++)                                                \
200             for (j = 1; j < end; j += 2)                                       \
201                 o_8[i] += transform[4 * j][i] * src[j * sstep];                \
202         TR_4(e_8, src, 1, 2 * sstep, SET, 4);                                  \
203                                                                                \
204         for (i = 0; i < 4; i++) {                                              \
205             assign(dst[i * dstep], e_8[i] + o_8[i]);                           \
206             assign(dst[(7 - i) * dstep], e_8[i] - o_8[i]);                     \
207         }                                                                      \
208     } while (0)
209
210 #define TR_16(dst, src, dstep, sstep, assign, end)                             \
211     do {                                                                       \
212         int i, j;                                                              \
213         int e_16[8];                                                           \
214         int o_16[8] = { 0 };                                                   \
215         for (i = 0; i < 8; i++)                                                \
216             for (j = 1; j < end; j += 2)                                       \
217                 o_16[i] += transform[2 * j][i] * src[j * sstep];               \
218         TR_8(e_16, src, 1, 2 * sstep, SET, 8);                                 \
219                                                                                \
220         for (i = 0; i < 8; i++) {                                              \
221             assign(dst[i * dstep], e_16[i] + o_16[i]);                         \
222             assign(dst[(15 - i) * dstep], e_16[i] - o_16[i]);                  \
223         }                                                                      \
224     } while (0)
225
226 #define TR_32(dst, src, dstep, sstep, assign, end)                             \
227     do {                                                                       \
228         int i, j;                                                              \
229         int e_32[16];                                                          \
230         int o_32[16] = { 0 };                                                  \
231         for (i = 0; i < 16; i++)                                               \
232             for (j = 1; j < end; j += 2)                                       \
233                 o_32[i] += transform[j][i] * src[j * sstep];                   \
234         TR_16(e_32, src, 1, 2 * sstep, SET, end/2);                            \
235                                                                                \
236         for (i = 0; i < 16; i++) {                                             \
237             assign(dst[i * dstep], e_32[i] + o_32[i]);                         \
238             assign(dst[(31 - i) * dstep], e_32[i] - o_32[i]);                  \
239         }                                                                      \
240     } while (0)
241
242 #define IDCT_VAR4(H)                                                          \
243     int      limit2   = FFMIN(col_limit + 4, H)
244 #define IDCT_VAR8(H)                                                          \
245         int      limit   = FFMIN(col_limit, H);                               \
246         int      limit2   = FFMIN(col_limit + 4, H)
247 #define IDCT_VAR16(H)   IDCT_VAR8(H)
248 #define IDCT_VAR32(H)   IDCT_VAR8(H)
249
250 #define IDCT(H)                                                              \
251 static void FUNC(idct_##H ##x ##H )(                                         \
252                    int16_t *coeffs, int col_limit) {                         \
253     int i;                                                                   \
254     int      shift   = 7;                                                    \
255     int      add     = 1 << (shift - 1);                                     \
256     int16_t *src     = coeffs;                                               \
257     IDCT_VAR ##H(H);                                                         \
258                                                                              \
259     for (i = 0; i < H; i++) {                                                \
260         TR_ ## H(src, src, H, H, SCALE, limit2);                             \
261         if (limit2 < H && i%4 == 0 && !!i)                                   \
262             limit2 -= 4;                                                     \
263         src++;                                                               \
264     }                                                                        \
265                                                                              \
266     shift   = 20 - BIT_DEPTH;                                                \
267     add     = 1 << (shift - 1);                                              \
268     for (i = 0; i < H; i++) {                                                \
269         TR_ ## H(coeffs, coeffs, 1, 1, SCALE, limit);                        \
270         coeffs += H;                                                         \
271     }                                                                        \
272 }
273
274 #define IDCT_DC(H)                                                           \
275 static void FUNC(idct_##H ##x ##H ##_dc)(                                    \
276                    int16_t *coeffs) {                                        \
277     int i, j;                                                                \
278     int      shift   = 14 - BIT_DEPTH;                                       \
279     int      add     = 1 << (shift - 1);                                     \
280     int      coeff   = (((coeffs[0] + 1) >> 1) + add) >> shift;              \
281                                                                              \
282     for (j = 0; j < H; j++) {                                                \
283         for (i = 0; i < H; i++) {                                            \
284             coeffs[i+j*H] = coeff;                                           \
285         }                                                                    \
286     }                                                                        \
287 }
288
289 IDCT( 4)
290 IDCT( 8)
291 IDCT(16)
292 IDCT(32)
293
294 IDCT_DC( 4)
295 IDCT_DC( 8)
296 IDCT_DC(16)
297 IDCT_DC(32)
298
299 #undef TR_4
300 #undef TR_8
301 #undef TR_16
302 #undef TR_32
303
304 #undef SET
305 #undef SCALE
306 #undef ADD_AND_SCALE
307
308 static void FUNC(sao_band_filter_0)(uint8_t *_dst, uint8_t *_src,
309                                   ptrdiff_t stride, SAOParams *sao,
310                                   int *borders, int width, int height,
311                                   int c_idx)
312 {
313     pixel *dst = (pixel *)_dst;
314     pixel *src = (pixel *)_src;
315     int offset_table[32] = { 0 };
316     int k, y, x;
317     int shift  = BIT_DEPTH - 5;
318     int *sao_offset_val = sao->offset_val[c_idx];
319     int sao_left_class  = sao->band_position[c_idx];
320
321     stride /= sizeof(pixel);
322
323     for (k = 0; k < 4; k++)
324         offset_table[(k + sao_left_class) & 31] = sao_offset_val[k + 1];
325     for (y = 0; y < height; y++) {
326         for (x = 0; x < width; x++)
327             dst[x] = av_clip_pixel(src[x] + offset_table[av_clip_pixel(src[x] >> shift)]);
328         dst += stride;
329         src += stride;
330     }
331 }
332
333 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
334
335 static void FUNC(sao_edge_filter)(uint8_t *_dst, uint8_t *_src,
336                                   ptrdiff_t stride, SAOParams *sao,
337                                   int width, int height,
338                                   int c_idx, int init_x, int init_y) {
339
340     static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
341     static const int8_t pos[4][2][2] = {
342         { { -1,  0 }, {  1, 0 } }, // horizontal
343         { {  0, -1 }, {  0, 1 } }, // vertical
344         { { -1, -1 }, {  1, 1 } }, // 45 degree
345         { {  1, -1 }, { -1, 1 } }, // 135 degree
346     };
347     int *sao_offset_val = sao->offset_val[c_idx];
348     int sao_eo_class    = sao->eo_class[c_idx];
349     pixel *dst = (pixel *)_dst;
350     pixel *src = (pixel *)_src;
351
352     int y_stride = init_y * stride;
353     int pos_0_0  = pos[sao_eo_class][0][0];
354     int pos_0_1  = pos[sao_eo_class][0][1];
355     int pos_1_0  = pos[sao_eo_class][1][0];
356     int pos_1_1  = pos[sao_eo_class][1][1];
357     int x, y;
358
359     int y_stride_0_1 = (init_y + pos_0_1) * stride;
360     int y_stride_1_1 = (init_y + pos_1_1) * stride;
361     for (y = init_y; y < height; y++) {
362         for (x = init_x; x < width; x++) {
363             int diff0         = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
364             int diff1         = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
365             int offset_val    = edge_idx[2 + diff0 + diff1];
366             dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
367         }
368         y_stride     += stride;
369         y_stride_0_1 += stride;
370         y_stride_1_1 += stride;
371     }
372 }
373
374 static void FUNC(sao_edge_filter_0)(uint8_t *_dst, uint8_t *_src,
375                                     ptrdiff_t stride, SAOParams *sao,
376                                     int *borders, int _width, int _height,
377                                     int c_idx, uint8_t *vert_edge,
378                                     uint8_t *horiz_edge, uint8_t *diag_edge)
379 {
380     int x, y;
381     pixel *dst = (pixel *)_dst;
382     pixel *src = (pixel *)_src;
383     int *sao_offset_val = sao->offset_val[c_idx];
384     int sao_eo_class    = sao->eo_class[c_idx];
385     int init_x = 0, init_y = 0, width = _width, height = _height;
386
387     stride /= sizeof(pixel);
388
389     if (sao_eo_class != SAO_EO_VERT) {
390         if (borders[0]) {
391             int offset_val = sao_offset_val[0];
392             int y_stride   = 0;
393             for (y = 0; y < height; y++) {
394                 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
395                 y_stride     += stride;
396             }
397             init_x = 1;
398         }
399         if (borders[2]) {
400             int offset_val = sao_offset_val[0];
401             int x_stride   = width - 1;
402             for (x = 0; x < height; x++) {
403                 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
404                 x_stride     += stride;
405             }
406             width--;
407         }
408     }
409     if (sao_eo_class != SAO_EO_HORIZ) {
410         if (borders[1]) {
411             int offset_val = sao_offset_val[0];
412             for (x = init_x; x < width; x++)
413                 dst[x] = av_clip_pixel(src[x] + offset_val);
414             init_y = 1;
415         }
416         if (borders[3]) {
417             int offset_val = sao_offset_val[0];
418             int y_stride   = stride * (height - 1);
419             for (x = init_x; x < width; x++)
420                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
421             height--;
422         }
423     }
424
425     FUNC(sao_edge_filter)((uint8_t *)dst, (uint8_t *)src, stride, sao, width, height, c_idx, init_x, init_y);
426 }
427
428 static void FUNC(sao_edge_filter_1)(uint8_t *_dst, uint8_t *_src,
429                                     ptrdiff_t stride, SAOParams *sao,
430                                     int *borders, int _width, int _height,
431                                     int c_idx, uint8_t *vert_edge,
432                                     uint8_t *horiz_edge, uint8_t *diag_edge)
433 {
434     int x, y;
435     pixel *dst = (pixel *)_dst;
436     pixel *src = (pixel *)_src;
437     int *sao_offset_val = sao->offset_val[c_idx];
438     int sao_eo_class    = sao->eo_class[c_idx];
439     int init_x = 0, init_y = 0, width = _width, height = _height;
440
441     stride /= sizeof(pixel);
442
443     if (sao_eo_class != SAO_EO_VERT) {
444         if (borders[0]) {
445             int offset_val = sao_offset_val[0];
446             int y_stride   = 0;
447             for (y = 0; y < height; y++) {
448                 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
449                 y_stride     += stride;
450             }
451             init_x = 1;
452         }
453         if (borders[2]) {
454             int offset_val = sao_offset_val[0];
455             int x_stride   = width - 1;
456             for (x = 0; x < height; x++) {
457                 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
458                 x_stride     += stride;
459             }
460             width--;
461         }
462     }
463     if (sao_eo_class != SAO_EO_HORIZ) {
464         if (borders[1]) {
465             int offset_val = sao_offset_val[0];
466             for (x = init_x; x < width; x++)
467                 dst[x] = av_clip_pixel(src[x] + offset_val);
468             init_y = 1;
469         }
470         if (borders[3]) {
471             int offset_val = sao_offset_val[0];
472             int y_stride   = stride * (height - 1);
473             for (x = init_x; x < width; x++)
474                 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
475             height--;
476         }
477     }
478
479     FUNC(sao_edge_filter)((uint8_t *)dst, (uint8_t *)src, stride, sao, width, height, c_idx, init_x, init_y);
480
481     {
482         int save_upper_left  = !diag_edge[0] && sao_eo_class == SAO_EO_135D && !borders[0] && !borders[1];
483         int save_upper_right = !diag_edge[1] && sao_eo_class == SAO_EO_45D  && !borders[1] && !borders[2];
484         int save_lower_right = !diag_edge[2] && sao_eo_class == SAO_EO_135D && !borders[2] && !borders[3];
485         int save_lower_left  = !diag_edge[3] && sao_eo_class == SAO_EO_45D  && !borders[0] && !borders[3];
486
487         // Restore pixels that can't be modified
488         if(vert_edge[0] && sao_eo_class != SAO_EO_VERT) {
489             for(y = init_y+save_upper_left; y< height-save_lower_left; y++)
490                 dst[y*stride] = src[y*stride];
491         }
492         if(vert_edge[1] && sao_eo_class != SAO_EO_VERT) {
493             for(y = init_y+save_upper_right; y< height-save_lower_right; y++)
494                 dst[y*stride+width-1] = src[y*stride+width-1];
495         }
496
497         if(horiz_edge[0] && sao_eo_class != SAO_EO_HORIZ) {
498             for(x = init_x+save_upper_left; x < width-save_upper_right; x++)
499                 dst[x] = src[x];
500         }
501         if(horiz_edge[1] && sao_eo_class != SAO_EO_HORIZ) {
502             for(x = init_x+save_lower_left; x < width-save_lower_right; x++)
503                 dst[(height-1)*stride+x] = src[(height-1)*stride+x];
504         }
505         if(diag_edge[0] && sao_eo_class == SAO_EO_135D)
506             dst[0] = src[0];
507         if(diag_edge[1] && sao_eo_class == SAO_EO_45D)
508             dst[width-1] = src[width-1];
509         if(diag_edge[2] && sao_eo_class == SAO_EO_135D)
510             dst[stride*(height-1)+width-1] = src[stride*(height-1)+width-1];
511         if(diag_edge[3] && sao_eo_class == SAO_EO_45D)
512             dst[stride*(height-1)] = src[stride*(height-1)];
513
514     }
515 }
516
517 #undef CMP
518
519 ////////////////////////////////////////////////////////////////////////////////
520 //
521 ////////////////////////////////////////////////////////////////////////////////
522 static void FUNC(put_hevc_pel_pixels)(int16_t *dst, ptrdiff_t dststride,
523                                       uint8_t *_src, ptrdiff_t _srcstride,
524                                       int height, intptr_t mx, intptr_t my, int width)
525 {
526     int x, y;
527     pixel *src          = (pixel *)_src;
528     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
529
530     for (y = 0; y < height; y++) {
531         for (x = 0; x < width; x++)
532             dst[x] = src[x] << (14 - BIT_DEPTH);
533         src += srcstride;
534         dst += dststride;
535     }
536 }
537
538 static void FUNC(put_hevc_pel_uni_pixels)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
539                                           int height, intptr_t mx, intptr_t my, int width)
540 {
541     int y;
542     pixel *src          = (pixel *)_src;
543     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
544     pixel *dst          = (pixel *)_dst;
545     ptrdiff_t dststride = _dststride / sizeof(pixel);
546
547     for (y = 0; y < height; y++) {
548         memcpy(dst, src, width * sizeof(pixel));
549         src += srcstride;
550         dst += dststride;
551     }
552 }
553
554 static void FUNC(put_hevc_pel_bi_pixels)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
555                                          int16_t *src2, ptrdiff_t src2stride,
556                                          int height, intptr_t mx, intptr_t my, int width)
557 {
558     int x, y;
559     pixel *src          = (pixel *)_src;
560     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
561     pixel *dst          = (pixel *)_dst;
562     ptrdiff_t dststride = _dststride / sizeof(pixel);
563
564     int shift = 14  + 1 - BIT_DEPTH;
565 #if BIT_DEPTH < 14
566     int offset = 1 << (shift - 1);
567 #else
568     int offset = 0;
569 #endif
570
571     for (y = 0; y < height; y++) {
572         for (x = 0; x < width; x++)
573             dst[x] = av_clip_pixel(((src[x] << (14 - BIT_DEPTH)) + src2[x] + offset) >> shift);
574         src  += srcstride;
575         dst  += dststride;
576         src2 += src2stride;
577     }
578 }
579
580 static void FUNC(put_hevc_pel_uni_w_pixels)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
581                                             int height, int denom, int wx, int ox, intptr_t mx, intptr_t my, int width)
582 {
583     int x, y;
584     pixel *src          = (pixel *)_src;
585     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
586     pixel *dst          = (pixel *)_dst;
587     ptrdiff_t dststride = _dststride / sizeof(pixel);
588     int shift = denom + 14 - BIT_DEPTH;
589 #if BIT_DEPTH < 14
590     int offset = 1 << (shift - 1);
591 #else
592     int offset = 0;
593 #endif
594
595     ox     = ox * (1 << (BIT_DEPTH - 8));
596     for (y = 0; y < height; y++) {
597         for (x = 0; x < width; x++)
598             dst[x] = av_clip_pixel((((src[x] << (14 - BIT_DEPTH)) * wx + offset) >> shift) + ox);
599         src += srcstride;
600         dst += dststride;
601     }
602 }
603
604 static void FUNC(put_hevc_pel_bi_w_pixels)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
605                                            int16_t *src2, ptrdiff_t src2stride,
606                                            int height, int denom, int wx0, int wx1,
607                                            int ox0, int ox1, intptr_t mx, intptr_t my, int width)
608 {
609     int x, y;
610     pixel *src          = (pixel *)_src;
611     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
612     pixel *dst          = (pixel *)_dst;
613     ptrdiff_t dststride = _dststride / sizeof(pixel);
614
615     int shift = 14  + 1 - BIT_DEPTH;
616     int log2Wd = denom + shift - 1;
617
618     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
619     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
620     for (y = 0; y < height; y++) {
621         for (x = 0; x < width; x++) {
622             dst[x] = av_clip_pixel(( (src[x] << (14 - BIT_DEPTH)) * wx1 + src2[x] * wx0 + ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
623         }
624         src  += srcstride;
625         dst  += dststride;
626         src2 += src2stride;
627     }
628 }
629
630 ////////////////////////////////////////////////////////////////////////////////
631 //
632 ////////////////////////////////////////////////////////////////////////////////
633 #define QPEL_FILTER(src, stride)                                               \
634     (filter[0] * src[x - 3 * stride] +                                         \
635      filter[1] * src[x - 2 * stride] +                                         \
636      filter[2] * src[x -     stride] +                                         \
637      filter[3] * src[x             ] +                                         \
638      filter[4] * src[x +     stride] +                                         \
639      filter[5] * src[x + 2 * stride] +                                         \
640      filter[6] * src[x + 3 * stride] +                                         \
641      filter[7] * src[x + 4 * stride])
642
643 static void FUNC(put_hevc_qpel_h)(int16_t *dst,  ptrdiff_t dststride,
644                                   uint8_t *_src, ptrdiff_t _srcstride,
645                                   int height, intptr_t mx, intptr_t my, int width)
646 {
647     int x, y;
648     pixel        *src       = (pixel*)_src;
649     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
650     const int8_t *filter    = ff_hevc_qpel_filters[mx - 1];
651     for (y = 0; y < height; y++) {
652         for (x = 0; x < width; x++)
653             dst[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
654         src += srcstride;
655         dst += dststride;
656     }
657 }
658
659 static void FUNC(put_hevc_qpel_v)(int16_t *dst,  ptrdiff_t dststride,
660                                   uint8_t *_src, ptrdiff_t _srcstride,
661                                   int height, intptr_t mx, intptr_t my, int width)
662 {
663     int x, y;
664     pixel        *src       = (pixel*)_src;
665     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
666     const int8_t *filter    = ff_hevc_qpel_filters[my - 1];
667     for (y = 0; y < height; y++)  {
668         for (x = 0; x < width; x++)
669             dst[x] = QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
670         src += srcstride;
671         dst += dststride;
672     }
673 }
674
675 static void FUNC(put_hevc_qpel_hv)(int16_t *dst,
676                                    ptrdiff_t dststride,
677                                    uint8_t *_src,
678                                    ptrdiff_t _srcstride,
679                                    int height, intptr_t mx,
680                                    intptr_t my, int width)
681 {
682     int x, y;
683     const int8_t *filter;
684     pixel *src = (pixel*)_src;
685     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
686     int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
687     int16_t *tmp = tmp_array;
688
689     src   -= QPEL_EXTRA_BEFORE * srcstride;
690     filter = ff_hevc_qpel_filters[mx - 1];
691     for (y = 0; y < height + QPEL_EXTRA; y++) {
692         for (x = 0; x < width; x++)
693             tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
694         src += srcstride;
695         tmp += MAX_PB_SIZE;
696     }
697
698     tmp    = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
699     filter = ff_hevc_qpel_filters[my - 1];
700     for (y = 0; y < height; y++) {
701         for (x = 0; x < width; x++)
702             dst[x] = QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
703         tmp += MAX_PB_SIZE;
704         dst += dststride;
705     }
706 }
707
708 static void FUNC(put_hevc_qpel_uni_h)(uint8_t *_dst,  ptrdiff_t _dststride,
709                                       uint8_t *_src, ptrdiff_t _srcstride,
710                                       int height, intptr_t mx, intptr_t my, int width)
711 {
712     int x, y;
713     pixel        *src       = (pixel*)_src;
714     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
715     pixel *dst          = (pixel *)_dst;
716     ptrdiff_t dststride = _dststride / sizeof(pixel);
717     const int8_t *filter    = ff_hevc_qpel_filters[mx - 1];
718     int shift = 14 - BIT_DEPTH;
719
720 #if BIT_DEPTH < 14
721     int offset = 1 << (shift - 1);
722 #else
723     int offset = 0;
724 #endif
725
726     for (y = 0; y < height; y++) {
727         for (x = 0; x < width; x++)
728             dst[x] = av_clip_pixel(((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + offset) >> shift);
729         src += srcstride;
730         dst += dststride;
731     }
732 }
733
734 static void FUNC(put_hevc_qpel_bi_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
735                                      int16_t *src2, ptrdiff_t src2stride,
736                                      int height, intptr_t mx, intptr_t my, int width)
737 {
738     int x, y;
739     pixel        *src       = (pixel*)_src;
740     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
741     pixel *dst          = (pixel *)_dst;
742     ptrdiff_t dststride = _dststride / sizeof(pixel);
743
744     const int8_t *filter    = ff_hevc_qpel_filters[mx - 1];
745
746     int shift = 14  + 1 - BIT_DEPTH;
747 #if BIT_DEPTH < 14
748     int offset = 1 << (shift - 1);
749 #else
750     int offset = 0;
751 #endif
752
753     for (y = 0; y < height; y++) {
754         for (x = 0; x < width; x++)
755             dst[x] = av_clip_pixel(((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + src2[x] + offset) >> shift);
756         src  += srcstride;
757         dst  += dststride;
758         src2 += src2stride;
759     }
760 }
761
762 static void FUNC(put_hevc_qpel_uni_v)(uint8_t *_dst,  ptrdiff_t _dststride,
763                                      uint8_t *_src, ptrdiff_t _srcstride,
764                                      int height, intptr_t mx, intptr_t my, int width)
765 {
766     int x, y;
767     pixel        *src       = (pixel*)_src;
768     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
769     pixel *dst          = (pixel *)_dst;
770     ptrdiff_t dststride = _dststride / sizeof(pixel);
771     const int8_t *filter    = ff_hevc_qpel_filters[my - 1];
772     int shift = 14 - BIT_DEPTH;
773
774 #if BIT_DEPTH < 14
775     int offset = 1 << (shift - 1);
776 #else
777     int offset = 0;
778 #endif
779
780     for (y = 0; y < height; y++) {
781         for (x = 0; x < width; x++)
782             dst[x] = av_clip_pixel(((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) + offset) >> shift);
783         src += srcstride;
784         dst += dststride;
785     }
786 }
787
788
789 static void FUNC(put_hevc_qpel_bi_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
790                                      int16_t *src2, ptrdiff_t src2stride,
791                                      int height, intptr_t mx, intptr_t my, int width)
792 {
793     int x, y;
794     pixel        *src       = (pixel*)_src;
795     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
796     pixel *dst          = (pixel *)_dst;
797     ptrdiff_t dststride = _dststride / sizeof(pixel);
798
799     const int8_t *filter    = ff_hevc_qpel_filters[my - 1];
800
801     int shift = 14 + 1 - BIT_DEPTH;
802 #if BIT_DEPTH < 14
803     int offset = 1 << (shift - 1);
804 #else
805     int offset = 0;
806 #endif
807
808     for (y = 0; y < height; y++) {
809         for (x = 0; x < width; x++)
810             dst[x] = av_clip_pixel(((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) + src2[x] + offset) >> shift);
811         src  += srcstride;
812         dst  += dststride;
813         src2 += src2stride;
814     }
815 }
816
817 static void FUNC(put_hevc_qpel_uni_hv)(uint8_t *_dst,  ptrdiff_t _dststride,
818                                        uint8_t *_src, ptrdiff_t _srcstride,
819                                        int height, intptr_t mx, intptr_t my, int width)
820 {
821     int x, y;
822     const int8_t *filter;
823     pixel *src = (pixel*)_src;
824     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
825     pixel *dst          = (pixel *)_dst;
826     ptrdiff_t dststride = _dststride / sizeof(pixel);
827     int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
828     int16_t *tmp = tmp_array;
829     int shift =  14 - BIT_DEPTH;
830
831 #if BIT_DEPTH < 14
832     int offset = 1 << (shift - 1);
833 #else
834     int offset = 0;
835 #endif
836
837     src   -= QPEL_EXTRA_BEFORE * srcstride;
838     filter = ff_hevc_qpel_filters[mx - 1];
839     for (y = 0; y < height + QPEL_EXTRA; y++) {
840         for (x = 0; x < width; x++)
841             tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
842         src += srcstride;
843         tmp += MAX_PB_SIZE;
844     }
845
846     tmp    = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
847     filter = ff_hevc_qpel_filters[my - 1];
848
849     for (y = 0; y < height; y++) {
850         for (x = 0; x < width; x++)
851             dst[x] = av_clip_pixel(((QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) + offset) >> shift);
852         tmp += MAX_PB_SIZE;
853         dst += dststride;
854     }
855 }
856
857 static void FUNC(put_hevc_qpel_bi_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
858                                       int16_t *src2, ptrdiff_t src2stride,
859                                       int height, intptr_t mx, intptr_t my, int width)
860 {
861     int x, y;
862     const int8_t *filter;
863     pixel *src = (pixel*)_src;
864     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
865     pixel *dst          = (pixel *)_dst;
866     ptrdiff_t dststride = _dststride / sizeof(pixel);
867     int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
868     int16_t *tmp = tmp_array;
869     int shift = 14 + 1 - BIT_DEPTH;
870 #if BIT_DEPTH < 14
871     int offset = 1 << (shift - 1);
872 #else
873     int offset = 0;
874 #endif
875
876     src   -= QPEL_EXTRA_BEFORE * srcstride;
877     filter = ff_hevc_qpel_filters[mx - 1];
878     for (y = 0; y < height + QPEL_EXTRA; y++) {
879         for (x = 0; x < width; x++)
880             tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
881         src += srcstride;
882         tmp += MAX_PB_SIZE;
883     }
884
885     tmp    = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
886     filter = ff_hevc_qpel_filters[my - 1];
887
888     for (y = 0; y < height; y++) {
889         for (x = 0; x < width; x++)
890             dst[x] = av_clip_pixel(((QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) + src2[x] + offset) >> shift);
891         tmp  += MAX_PB_SIZE;
892         dst  += dststride;
893         src2 += src2stride;
894     }
895 }
896
897 static void FUNC(put_hevc_qpel_uni_w_h)(uint8_t *_dst,  ptrdiff_t _dststride,
898                                         uint8_t *_src, ptrdiff_t _srcstride,
899                                         int height, int denom, int wx, int ox,
900                                         intptr_t mx, intptr_t my, int width)
901 {
902     int x, y;
903     pixel        *src       = (pixel*)_src;
904     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
905     pixel *dst          = (pixel *)_dst;
906     ptrdiff_t dststride = _dststride / sizeof(pixel);
907     const int8_t *filter    = ff_hevc_qpel_filters[mx - 1];
908     int shift = denom + 14 - BIT_DEPTH;
909 #if BIT_DEPTH < 14
910     int offset = 1 << (shift - 1);
911 #else
912     int offset = 0;
913 #endif
914
915     ox = ox * (1 << (BIT_DEPTH - 8));
916     for (y = 0; y < height; y++) {
917         for (x = 0; x < width; x++)
918             dst[x] = av_clip_pixel((((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) * wx + offset) >> shift) + ox);
919         src += srcstride;
920         dst += dststride;
921     }
922 }
923
924 static void FUNC(put_hevc_qpel_bi_w_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
925                                        int16_t *src2, ptrdiff_t src2stride,
926                                        int height, int denom, int wx0, int wx1,
927                                        int ox0, int ox1, intptr_t mx, intptr_t my, int width)
928 {
929     int x, y;
930     pixel        *src       = (pixel*)_src;
931     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
932     pixel *dst          = (pixel *)_dst;
933     ptrdiff_t dststride = _dststride / sizeof(pixel);
934
935     const int8_t *filter    = ff_hevc_qpel_filters[mx - 1];
936
937     int shift = 14  + 1 - BIT_DEPTH;
938     int log2Wd = denom + shift - 1;
939
940     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
941     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
942     for (y = 0; y < height; y++) {
943         for (x = 0; x < width; x++)
944             dst[x] = av_clip_pixel(((QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) * wx1 + src2[x] * wx0 +
945                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
946         src  += srcstride;
947         dst  += dststride;
948         src2 += src2stride;
949     }
950 }
951
952 static void FUNC(put_hevc_qpel_uni_w_v)(uint8_t *_dst,  ptrdiff_t _dststride,
953                                         uint8_t *_src, ptrdiff_t _srcstride,
954                                         int height, int denom, int wx, int ox,
955                                         intptr_t mx, intptr_t my, int width)
956 {
957     int x, y;
958     pixel        *src       = (pixel*)_src;
959     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
960     pixel *dst          = (pixel *)_dst;
961     ptrdiff_t dststride = _dststride / sizeof(pixel);
962     const int8_t *filter    = ff_hevc_qpel_filters[my - 1];
963     int shift = denom + 14 - BIT_DEPTH;
964 #if BIT_DEPTH < 14
965     int offset = 1 << (shift - 1);
966 #else
967     int offset = 0;
968 #endif
969
970     ox = ox * (1 << (BIT_DEPTH - 8));
971     for (y = 0; y < height; y++) {
972         for (x = 0; x < width; x++)
973             dst[x] = av_clip_pixel((((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) * wx + offset) >> shift) + ox);
974         src += srcstride;
975         dst += dststride;
976     }
977 }
978
979 static void FUNC(put_hevc_qpel_bi_w_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
980                                        int16_t *src2, ptrdiff_t src2stride,
981                                        int height, int denom, int wx0, int wx1,
982                                        int ox0, int ox1, intptr_t mx, intptr_t my, int width)
983 {
984     int x, y;
985     pixel        *src       = (pixel*)_src;
986     ptrdiff_t     srcstride = _srcstride / sizeof(pixel);
987     pixel *dst          = (pixel *)_dst;
988     ptrdiff_t dststride = _dststride / sizeof(pixel);
989
990     const int8_t *filter    = ff_hevc_qpel_filters[my - 1];
991
992     int shift = 14 + 1 - BIT_DEPTH;
993     int log2Wd = denom + shift - 1;
994
995     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
996     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
997     for (y = 0; y < height; y++) {
998         for (x = 0; x < width; x++)
999             dst[x] = av_clip_pixel(((QPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) * wx1 + src2[x] * wx0 +
1000                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
1001         src  += srcstride;
1002         dst  += dststride;
1003         src2 += src2stride;
1004     }
1005 }
1006
1007 static void FUNC(put_hevc_qpel_uni_w_hv)(uint8_t *_dst,  ptrdiff_t _dststride,
1008                                          uint8_t *_src, ptrdiff_t _srcstride,
1009                                          int height, int denom, int wx, int ox,
1010                                          intptr_t mx, intptr_t my, int width)
1011 {
1012     int x, y;
1013     const int8_t *filter;
1014     pixel *src = (pixel*)_src;
1015     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1016     pixel *dst          = (pixel *)_dst;
1017     ptrdiff_t dststride = _dststride / sizeof(pixel);
1018     int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
1019     int16_t *tmp = tmp_array;
1020     int shift = denom + 14 - BIT_DEPTH;
1021 #if BIT_DEPTH < 14
1022     int offset = 1 << (shift - 1);
1023 #else
1024     int offset = 0;
1025 #endif
1026
1027     src   -= QPEL_EXTRA_BEFORE * srcstride;
1028     filter = ff_hevc_qpel_filters[mx - 1];
1029     for (y = 0; y < height + QPEL_EXTRA; y++) {
1030         for (x = 0; x < width; x++)
1031             tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1032         src += srcstride;
1033         tmp += MAX_PB_SIZE;
1034     }
1035
1036     tmp    = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1037     filter = ff_hevc_qpel_filters[my - 1];
1038
1039     ox = ox * (1 << (BIT_DEPTH - 8));
1040     for (y = 0; y < height; y++) {
1041         for (x = 0; x < width; x++)
1042             dst[x] = av_clip_pixel((((QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) * wx + offset) >> shift) + ox);
1043         tmp += MAX_PB_SIZE;
1044         dst += dststride;
1045     }
1046 }
1047
1048 static void FUNC(put_hevc_qpel_bi_w_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1049                                         int16_t *src2, ptrdiff_t src2stride,
1050                                         int height, int denom, int wx0, int wx1,
1051                                         int ox0, int ox1, intptr_t mx, intptr_t my, int width)
1052 {
1053     int x, y;
1054     const int8_t *filter;
1055     pixel *src = (pixel*)_src;
1056     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1057     pixel *dst          = (pixel *)_dst;
1058     ptrdiff_t dststride = _dststride / sizeof(pixel);
1059     int16_t tmp_array[(MAX_PB_SIZE + QPEL_EXTRA) * MAX_PB_SIZE];
1060     int16_t *tmp = tmp_array;
1061     int shift = 14 + 1 - BIT_DEPTH;
1062     int log2Wd = denom + shift - 1;
1063
1064     src   -= QPEL_EXTRA_BEFORE * srcstride;
1065     filter = ff_hevc_qpel_filters[mx - 1];
1066     for (y = 0; y < height + QPEL_EXTRA; y++) {
1067         for (x = 0; x < width; x++)
1068             tmp[x] = QPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1069         src += srcstride;
1070         tmp += MAX_PB_SIZE;
1071     }
1072
1073     tmp    = tmp_array + QPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1074     filter = ff_hevc_qpel_filters[my - 1];
1075
1076     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
1077     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
1078     for (y = 0; y < height; y++) {
1079         for (x = 0; x < width; x++)
1080             dst[x] = av_clip_pixel(((QPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) * wx1 + src2[x] * wx0 +
1081                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
1082         tmp  += MAX_PB_SIZE;
1083         dst  += dststride;
1084         src2 += src2stride;
1085     }
1086 }
1087
1088 ////////////////////////////////////////////////////////////////////////////////
1089 //
1090 ////////////////////////////////////////////////////////////////////////////////
1091 #define EPEL_FILTER(src, stride)                                               \
1092     (filter[0] * src[x - stride] +                                             \
1093      filter[1] * src[x]          +                                             \
1094      filter[2] * src[x + stride] +                                             \
1095      filter[3] * src[x + 2 * stride])
1096
1097 static void FUNC(put_hevc_epel_h)(int16_t *dst, ptrdiff_t dststride,
1098                                   uint8_t *_src, ptrdiff_t _srcstride,
1099                                   int height, intptr_t mx, intptr_t my, int width)
1100 {
1101     int x, y;
1102     pixel *src = (pixel *)_src;
1103     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1104     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1105     for (y = 0; y < height; y++) {
1106         for (x = 0; x < width; x++)
1107             dst[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1108         src += srcstride;
1109         dst += dststride;
1110     }
1111 }
1112
1113 static void FUNC(put_hevc_epel_v)(int16_t *dst, ptrdiff_t dststride,
1114                                   uint8_t *_src, ptrdiff_t _srcstride,
1115                                   int height, intptr_t mx, intptr_t my, int width)
1116 {
1117     int x, y;
1118     pixel *src = (pixel *)_src;
1119     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1120     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1121
1122     for (y = 0; y < height; y++) {
1123         for (x = 0; x < width; x++)
1124             dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
1125         src += srcstride;
1126         dst += dststride;
1127     }
1128 }
1129
1130 static void FUNC(put_hevc_epel_hv)(int16_t *dst, ptrdiff_t dststride,
1131                                    uint8_t *_src, ptrdiff_t _srcstride,
1132                                    int height, intptr_t mx, intptr_t my, int width)
1133 {
1134     int x, y;
1135     pixel *src = (pixel *)_src;
1136     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1137     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1138     int16_t tmp_array[(MAX_PB_SIZE + EPEL_EXTRA) * MAX_PB_SIZE];
1139     int16_t *tmp = tmp_array;
1140
1141     src -= EPEL_EXTRA_BEFORE * srcstride;
1142
1143     for (y = 0; y < height + EPEL_EXTRA; y++) {
1144         for (x = 0; x < width; x++)
1145             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1146         src += srcstride;
1147         tmp += MAX_PB_SIZE;
1148     }
1149
1150     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1151     filter = ff_hevc_epel_filters[my - 1];
1152
1153     for (y = 0; y < height; y++) {
1154         for (x = 0; x < width; x++)
1155             dst[x] = EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
1156         tmp += MAX_PB_SIZE;
1157         dst += dststride;
1158     }
1159 }
1160
1161 static void FUNC(put_hevc_epel_uni_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1162                                       int height, intptr_t mx, intptr_t my, int width)
1163 {
1164     int x, y;
1165     pixel *src = (pixel *)_src;
1166     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1167     pixel *dst          = (pixel *)_dst;
1168     ptrdiff_t dststride = _dststride / sizeof(pixel);
1169     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1170     int shift = 14 - BIT_DEPTH;
1171 #if BIT_DEPTH < 14
1172     int offset = 1 << (shift - 1);
1173 #else
1174     int offset = 0;
1175 #endif
1176
1177     for (y = 0; y < height; y++) {
1178         for (x = 0; x < width; x++)
1179             dst[x] = av_clip_pixel(((EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + offset) >> shift);
1180         src += srcstride;
1181         dst += dststride;
1182     }
1183 }
1184
1185 static void FUNC(put_hevc_epel_bi_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1186                                      int16_t *src2, ptrdiff_t src2stride,
1187                                      int height, intptr_t mx, intptr_t my, int width)
1188 {
1189     int x, y;
1190     pixel *src = (pixel *)_src;
1191     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1192     pixel *dst          = (pixel *)_dst;
1193     ptrdiff_t dststride = _dststride / sizeof(pixel);
1194     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1195     int shift = 14 + 1 - BIT_DEPTH;
1196 #if BIT_DEPTH < 14
1197     int offset = 1 << (shift - 1);
1198 #else
1199     int offset = 0;
1200 #endif
1201
1202     for (y = 0; y < height; y++) {
1203         for (x = 0; x < width; x++) {
1204             dst[x] = av_clip_pixel(((EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) + src2[x] + offset) >> shift);
1205         }
1206         dst  += dststride;
1207         src  += srcstride;
1208         src2 += src2stride;
1209     }
1210 }
1211
1212 static void FUNC(put_hevc_epel_uni_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1213                                       int height, intptr_t mx, intptr_t my, int width)
1214 {
1215     int x, y;
1216     pixel *src = (pixel *)_src;
1217     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1218     pixel *dst          = (pixel *)_dst;
1219     ptrdiff_t dststride = _dststride / sizeof(pixel);
1220     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1221     int shift = 14 - BIT_DEPTH;
1222 #if BIT_DEPTH < 14
1223     int offset = 1 << (shift - 1);
1224 #else
1225     int offset = 0;
1226 #endif
1227
1228     for (y = 0; y < height; y++) {
1229         for (x = 0; x < width; x++)
1230             dst[x] = av_clip_pixel(((EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) + offset) >> shift);
1231         src += srcstride;
1232         dst += dststride;
1233     }
1234 }
1235
1236 static void FUNC(put_hevc_epel_bi_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1237                                      int16_t *src2, ptrdiff_t src2stride,
1238                                      int height, intptr_t mx, intptr_t my, int width)
1239 {
1240     int x, y;
1241     pixel *src = (pixel *)_src;
1242     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1243     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1244     pixel *dst          = (pixel *)_dst;
1245     ptrdiff_t dststride = _dststride / sizeof(pixel);
1246     int shift = 14 + 1 - BIT_DEPTH;
1247 #if BIT_DEPTH < 14
1248     int offset = 1 << (shift - 1);
1249 #else
1250     int offset = 0;
1251 #endif
1252
1253     for (y = 0; y < height; y++) {
1254         for (x = 0; x < width; x++)
1255             dst[x] = av_clip_pixel(((EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) + src2[x] + offset) >> shift);
1256         dst  += dststride;
1257         src  += srcstride;
1258         src2 += src2stride;
1259     }
1260 }
1261
1262 static void FUNC(put_hevc_epel_uni_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1263                                        int height, intptr_t mx, intptr_t my, int width)
1264 {
1265     int x, y;
1266     pixel *src = (pixel *)_src;
1267     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1268     pixel *dst          = (pixel *)_dst;
1269     ptrdiff_t dststride = _dststride / sizeof(pixel);
1270     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1271     int16_t tmp_array[(MAX_PB_SIZE + EPEL_EXTRA) * MAX_PB_SIZE];
1272     int16_t *tmp = tmp_array;
1273     int shift = 14 - BIT_DEPTH;
1274 #if BIT_DEPTH < 14
1275     int offset = 1 << (shift - 1);
1276 #else
1277     int offset = 0;
1278 #endif
1279
1280     src -= EPEL_EXTRA_BEFORE * srcstride;
1281
1282     for (y = 0; y < height + EPEL_EXTRA; y++) {
1283         for (x = 0; x < width; x++)
1284             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1285         src += srcstride;
1286         tmp += MAX_PB_SIZE;
1287     }
1288
1289     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1290     filter = ff_hevc_epel_filters[my - 1];
1291
1292     for (y = 0; y < height; y++) {
1293         for (x = 0; x < width; x++)
1294             dst[x] = av_clip_pixel(((EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) + offset) >> shift);
1295         tmp += MAX_PB_SIZE;
1296         dst += dststride;
1297     }
1298 }
1299
1300 static void FUNC(put_hevc_epel_bi_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1301                                       int16_t *src2, ptrdiff_t src2stride,
1302                                       int height, intptr_t mx, intptr_t my, int width)
1303 {
1304     int x, y;
1305     pixel *src = (pixel *)_src;
1306     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1307     pixel *dst          = (pixel *)_dst;
1308     ptrdiff_t dststride = _dststride / sizeof(pixel);
1309     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1310     int16_t tmp_array[(MAX_PB_SIZE + EPEL_EXTRA) * MAX_PB_SIZE];
1311     int16_t *tmp = tmp_array;
1312     int shift = 14 + 1 - BIT_DEPTH;
1313 #if BIT_DEPTH < 14
1314     int offset = 1 << (shift - 1);
1315 #else
1316     int offset = 0;
1317 #endif
1318
1319     src -= EPEL_EXTRA_BEFORE * srcstride;
1320
1321     for (y = 0; y < height + EPEL_EXTRA; y++) {
1322         for (x = 0; x < width; x++)
1323             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1324         src += srcstride;
1325         tmp += MAX_PB_SIZE;
1326     }
1327
1328     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1329     filter = ff_hevc_epel_filters[my - 1];
1330
1331     for (y = 0; y < height; y++) {
1332         for (x = 0; x < width; x++)
1333             dst[x] = av_clip_pixel(((EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) + src2[x] + offset) >> shift);
1334         tmp  += MAX_PB_SIZE;
1335         dst  += dststride;
1336         src2 += src2stride;
1337     }
1338 }
1339
1340 static void FUNC(put_hevc_epel_uni_w_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1341                                         int height, int denom, int wx, int ox, intptr_t mx, intptr_t my, int width)
1342 {
1343     int x, y;
1344     pixel *src = (pixel *)_src;
1345     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1346     pixel *dst          = (pixel *)_dst;
1347     ptrdiff_t dststride = _dststride / sizeof(pixel);
1348     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1349     int shift = denom + 14 - BIT_DEPTH;
1350 #if BIT_DEPTH < 14
1351     int offset = 1 << (shift - 1);
1352 #else
1353     int offset = 0;
1354 #endif
1355
1356     ox     = ox * (1 << (BIT_DEPTH - 8));
1357     for (y = 0; y < height; y++) {
1358         for (x = 0; x < width; x++) {
1359             dst[x] = av_clip_pixel((((EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) * wx + offset) >> shift) + ox);
1360         }
1361         dst += dststride;
1362         src += srcstride;
1363     }
1364 }
1365
1366 static void FUNC(put_hevc_epel_bi_w_h)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1367                                        int16_t *src2, ptrdiff_t src2stride,
1368                                        int height, int denom, int wx0, int wx1,
1369                                        int ox0, int ox1, intptr_t mx, intptr_t my, int width)
1370 {
1371     int x, y;
1372     pixel *src = (pixel *)_src;
1373     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1374     pixel *dst          = (pixel *)_dst;
1375     ptrdiff_t dststride = _dststride / sizeof(pixel);
1376     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1377     int shift = 14 + 1 - BIT_DEPTH;
1378     int log2Wd = denom + shift - 1;
1379
1380     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
1381     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
1382     for (y = 0; y < height; y++) {
1383         for (x = 0; x < width; x++)
1384             dst[x] = av_clip_pixel(((EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8)) * wx1 + src2[x] * wx0 +
1385                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
1386         src  += srcstride;
1387         dst  += dststride;
1388         src2 += src2stride;
1389     }
1390 }
1391
1392 static void FUNC(put_hevc_epel_uni_w_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1393                                         int height, int denom, int wx, int ox, intptr_t mx, intptr_t my, int width)
1394 {
1395     int x, y;
1396     pixel *src = (pixel *)_src;
1397     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1398     pixel *dst          = (pixel *)_dst;
1399     ptrdiff_t dststride = _dststride / sizeof(pixel);
1400     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1401     int shift = denom + 14 - BIT_DEPTH;
1402 #if BIT_DEPTH < 14
1403     int offset = 1 << (shift - 1);
1404 #else
1405     int offset = 0;
1406 #endif
1407
1408     ox     = ox * (1 << (BIT_DEPTH - 8));
1409     for (y = 0; y < height; y++) {
1410         for (x = 0; x < width; x++) {
1411             dst[x] = av_clip_pixel((((EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) * wx + offset) >> shift) + ox);
1412         }
1413         dst += dststride;
1414         src += srcstride;
1415     }
1416 }
1417
1418 static void FUNC(put_hevc_epel_bi_w_v)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1419                                        int16_t *src2, ptrdiff_t src2stride,
1420                                        int height, int denom, int wx0, int wx1,
1421                                        int ox0, int ox1, intptr_t mx, intptr_t my, int width)
1422 {
1423     int x, y;
1424     pixel *src = (pixel *)_src;
1425     ptrdiff_t srcstride  = _srcstride / sizeof(pixel);
1426     const int8_t *filter = ff_hevc_epel_filters[my - 1];
1427     pixel *dst          = (pixel *)_dst;
1428     ptrdiff_t dststride = _dststride / sizeof(pixel);
1429     int shift = 14 + 1 - BIT_DEPTH;
1430     int log2Wd = denom + shift - 1;
1431
1432     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
1433     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
1434     for (y = 0; y < height; y++) {
1435         for (x = 0; x < width; x++)
1436             dst[x] = av_clip_pixel(((EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8)) * wx1 + src2[x] * wx0 +
1437                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
1438         src  += srcstride;
1439         dst  += dststride;
1440         src2 += src2stride;
1441     }
1442 }
1443
1444 static void FUNC(put_hevc_epel_uni_w_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1445                                          int height, int denom, int wx, int ox, intptr_t mx, intptr_t my, int width)
1446 {
1447     int x, y;
1448     pixel *src = (pixel *)_src;
1449     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1450     pixel *dst          = (pixel *)_dst;
1451     ptrdiff_t dststride = _dststride / sizeof(pixel);
1452     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1453     int16_t tmp_array[(MAX_PB_SIZE + EPEL_EXTRA) * MAX_PB_SIZE];
1454     int16_t *tmp = tmp_array;
1455     int shift = denom + 14 - BIT_DEPTH;
1456 #if BIT_DEPTH < 14
1457     int offset = 1 << (shift - 1);
1458 #else
1459     int offset = 0;
1460 #endif
1461
1462     src -= EPEL_EXTRA_BEFORE * srcstride;
1463
1464     for (y = 0; y < height + EPEL_EXTRA; y++) {
1465         for (x = 0; x < width; x++)
1466             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1467         src += srcstride;
1468         tmp += MAX_PB_SIZE;
1469     }
1470
1471     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1472     filter = ff_hevc_epel_filters[my - 1];
1473
1474     ox     = ox * (1 << (BIT_DEPTH - 8));
1475     for (y = 0; y < height; y++) {
1476         for (x = 0; x < width; x++)
1477             dst[x] = av_clip_pixel((((EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) * wx + offset) >> shift) + ox);
1478         tmp += MAX_PB_SIZE;
1479         dst += dststride;
1480     }
1481 }
1482
1483 static void FUNC(put_hevc_epel_bi_w_hv)(uint8_t *_dst, ptrdiff_t _dststride, uint8_t *_src, ptrdiff_t _srcstride,
1484                                         int16_t *src2, ptrdiff_t src2stride,
1485                                         int height, int denom, int wx0, int wx1,
1486                                         int ox0, int ox1, intptr_t mx, intptr_t my, int width)
1487 {
1488     int x, y;
1489     pixel *src = (pixel *)_src;
1490     ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1491     pixel *dst          = (pixel *)_dst;
1492     ptrdiff_t dststride = _dststride / sizeof(pixel);
1493     const int8_t *filter = ff_hevc_epel_filters[mx - 1];
1494     int16_t tmp_array[(MAX_PB_SIZE + EPEL_EXTRA) * MAX_PB_SIZE];
1495     int16_t *tmp = tmp_array;
1496     int shift = 14 + 1 - BIT_DEPTH;
1497     int log2Wd = denom + shift - 1;
1498
1499     src -= EPEL_EXTRA_BEFORE * srcstride;
1500
1501     for (y = 0; y < height + EPEL_EXTRA; y++) {
1502         for (x = 0; x < width; x++)
1503             tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1504         src += srcstride;
1505         tmp += MAX_PB_SIZE;
1506     }
1507
1508     tmp      = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1509     filter = ff_hevc_epel_filters[my - 1];
1510
1511     ox0     = ox0 * (1 << (BIT_DEPTH - 8));
1512     ox1     = ox1 * (1 << (BIT_DEPTH - 8));
1513     for (y = 0; y < height; y++) {
1514         for (x = 0; x < width; x++)
1515             dst[x] = av_clip_pixel(((EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6) * wx1 + src2[x] * wx0 +
1516                                     ((ox0 + ox1 + 1) << log2Wd)) >> (log2Wd + 1));
1517         tmp  += MAX_PB_SIZE;
1518         dst  += dststride;
1519         src2 += src2stride;
1520     }
1521 }// line zero
1522 #define P3 pix[-4 * xstride]
1523 #define P2 pix[-3 * xstride]
1524 #define P1 pix[-2 * xstride]
1525 #define P0 pix[-1 * xstride]
1526 #define Q0 pix[0 * xstride]
1527 #define Q1 pix[1 * xstride]
1528 #define Q2 pix[2 * xstride]
1529 #define Q3 pix[3 * xstride]
1530
1531 // line three. used only for deblocking decision
1532 #define TP3 pix[-4 * xstride + 3 * ystride]
1533 #define TP2 pix[-3 * xstride + 3 * ystride]
1534 #define TP1 pix[-2 * xstride + 3 * ystride]
1535 #define TP0 pix[-1 * xstride + 3 * ystride]
1536 #define TQ0 pix[0  * xstride + 3 * ystride]
1537 #define TQ1 pix[1  * xstride + 3 * ystride]
1538 #define TQ2 pix[2  * xstride + 3 * ystride]
1539 #define TQ3 pix[3  * xstride + 3 * ystride]
1540
1541 static void FUNC(hevc_loop_filter_luma)(uint8_t *_pix,
1542                                         ptrdiff_t _xstride, ptrdiff_t _ystride,
1543                                         int *_beta, int *_tc,
1544                                         uint8_t *_no_p, uint8_t *_no_q)
1545 {
1546     int d, j;
1547     pixel *pix        = (pixel *)_pix;
1548     ptrdiff_t xstride = _xstride / sizeof(pixel);
1549     ptrdiff_t ystride = _ystride / sizeof(pixel);
1550
1551     for (j = 0; j < 2; j++) {
1552         const int dp0  = abs(P2  - 2 * P1  + P0);
1553         const int dq0  = abs(Q2  - 2 * Q1  + Q0);
1554         const int dp3  = abs(TP2 - 2 * TP1 + TP0);
1555         const int dq3  = abs(TQ2 - 2 * TQ1 + TQ0);
1556         const int d0   = dp0 + dq0;
1557         const int d3   = dp3 + dq3;
1558         const int beta = _beta[j] << (BIT_DEPTH - 8);
1559         const int tc   = _tc[j]   << (BIT_DEPTH - 8);
1560         const int no_p = _no_p[j];
1561         const int no_q = _no_q[j];
1562
1563         if (d0 + d3 >= beta) {
1564             pix += 4 * ystride;
1565             continue;
1566         } else {
1567             const int beta_3 = beta >> 3;
1568             const int beta_2 = beta >> 2;
1569             const int tc25   = ((tc * 5 + 1) >> 1);
1570
1571             if (abs(P3  -  P0) + abs(Q3  -  Q0) < beta_3 && abs(P0  -  Q0) < tc25 &&
1572                 abs(TP3 - TP0) + abs(TQ3 - TQ0) < beta_3 && abs(TP0 - TQ0) < tc25 &&
1573                                       (d0 << 1) < beta_2 &&      (d3 << 1) < beta_2) {
1574                 // strong filtering
1575                 const int tc2 = tc << 1;
1576                 for (d = 0; d < 4; d++) {
1577                     const int p3 = P3;
1578                     const int p2 = P2;
1579                     const int p1 = P1;
1580                     const int p0 = P0;
1581                     const int q0 = Q0;
1582                     const int q1 = Q1;
1583                     const int q2 = Q2;
1584                     const int q3 = Q3;
1585                     if (!no_p) {
1586                         P0 = p0 + av_clip(((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) - p0, -tc2, tc2);
1587                         P1 = p1 + av_clip(((p2 + p1 + p0 + q0 + 2) >> 2) - p1, -tc2, tc2);
1588                         P2 = p2 + av_clip(((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) - p2, -tc2, tc2);
1589                     }
1590                     if (!no_q) {
1591                         Q0 = q0 + av_clip(((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) - q0, -tc2, tc2);
1592                         Q1 = q1 + av_clip(((p0 + q0 + q1 + q2 + 2) >> 2) - q1, -tc2, tc2);
1593                         Q2 = q2 + av_clip(((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) - q2, -tc2, tc2);
1594                     }
1595                     pix += ystride;
1596                 }
1597             } else { // normal filtering
1598                 int nd_p = 1;
1599                 int nd_q = 1;
1600                 const int tc_2 = tc >> 1;
1601                 if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
1602                     nd_p = 2;
1603                 if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
1604                     nd_q = 2;
1605
1606                 for (d = 0; d < 4; d++) {
1607                     const int p2 = P2;
1608                     const int p1 = P1;
1609                     const int p0 = P0;
1610                     const int q0 = Q0;
1611                     const int q1 = Q1;
1612                     const int q2 = Q2;
1613                     int delta0   = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
1614                     if (abs(delta0) < 10 * tc) {
1615                         delta0 = av_clip(delta0, -tc, tc);
1616                         if (!no_p)
1617                             P0 = av_clip_pixel(p0 + delta0);
1618                         if (!no_q)
1619                             Q0 = av_clip_pixel(q0 - delta0);
1620                         if (!no_p && nd_p > 1) {
1621                             const int deltap1 = av_clip((((p2 + p0 + 1) >> 1) - p1 + delta0) >> 1, -tc_2, tc_2);
1622                             P1 = av_clip_pixel(p1 + deltap1);
1623                         }
1624                         if (!no_q && nd_q > 1) {
1625                             const int deltaq1 = av_clip((((q2 + q0 + 1) >> 1) - q1 - delta0) >> 1, -tc_2, tc_2);
1626                             Q1 = av_clip_pixel(q1 + deltaq1);
1627                         }
1628                     }
1629                     pix += ystride;
1630                 }
1631             }
1632         }
1633     }
1634 }
1635
1636 static void FUNC(hevc_loop_filter_chroma)(uint8_t *_pix, ptrdiff_t _xstride,
1637                                           ptrdiff_t _ystride, int *_tc,
1638                                           uint8_t *_no_p, uint8_t *_no_q)
1639 {
1640     int d, j, no_p, no_q;
1641     pixel *pix        = (pixel *)_pix;
1642     ptrdiff_t xstride = _xstride / sizeof(pixel);
1643     ptrdiff_t ystride = _ystride / sizeof(pixel);
1644
1645     for (j = 0; j < 2; j++) {
1646         const int tc = _tc[j] << (BIT_DEPTH - 8);
1647         if (tc <= 0) {
1648             pix += 4 * ystride;
1649             continue;
1650         }
1651         no_p = _no_p[j];
1652         no_q = _no_q[j];
1653
1654         for (d = 0; d < 4; d++) {
1655             int delta0;
1656             const int p1 = P1;
1657             const int p0 = P0;
1658             const int q0 = Q0;
1659             const int q1 = Q1;
1660             delta0 = av_clip((((q0 - p0) * 4) + p1 - q1 + 4) >> 3, -tc, tc);
1661             if (!no_p)
1662                 P0 = av_clip_pixel(p0 + delta0);
1663             if (!no_q)
1664                 Q0 = av_clip_pixel(q0 - delta0);
1665             pix += ystride;
1666         }
1667     }
1668 }
1669
1670 static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1671                                             int *tc, uint8_t *no_p,
1672                                             uint8_t *no_q)
1673 {
1674     FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
1675 }
1676
1677 static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1678                                             int *tc, uint8_t *no_p,
1679                                             uint8_t *no_q)
1680 {
1681     FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
1682 }
1683
1684 static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1685                                           int *beta, int *tc, uint8_t *no_p,
1686                                           uint8_t *no_q)
1687 {
1688     FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel),
1689                                 beta, tc, no_p, no_q);
1690 }
1691
1692 static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1693                                           int *beta, int *tc, uint8_t *no_p,
1694                                           uint8_t *no_q)
1695 {
1696     FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride,
1697                                 beta, tc, no_p, no_q);
1698 }
1699
1700 #undef P3
1701 #undef P2
1702 #undef P1
1703 #undef P0
1704 #undef Q0
1705 #undef Q1
1706 #undef Q2
1707 #undef Q3
1708
1709 #undef TP3
1710 #undef TP2
1711 #undef TP1
1712 #undef TP0
1713 #undef TQ0
1714 #undef TQ1
1715 #undef TQ2
1716 #undef TQ3