4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "bit_depth_template.c"
30 static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t stride, int size,
31 GetBitContext *gb, int pcm_bit_depth)
34 pixel *dst = (pixel *)_dst;
36 stride /= sizeof(pixel);
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);
45 static void FUNC(transquant_bypass4x4)(uint8_t *_dst, int16_t *coeffs,
49 pixel *dst = (pixel *)_dst;
51 stride /= sizeof(pixel);
53 for (y = 0; y < 4; y++) {
54 for (x = 0; x < 4; x++) {
62 static void FUNC(transquant_bypass8x8)(uint8_t *_dst, int16_t *coeffs,
66 pixel *dst = (pixel *)_dst;
68 stride /= sizeof(pixel);
70 for (y = 0; y < 8; y++) {
71 for (x = 0; x < 8; x++) {
79 static void FUNC(transquant_bypass16x16)(uint8_t *_dst, int16_t *coeffs,
83 pixel *dst = (pixel *)_dst;
85 stride /= sizeof(pixel);
87 for (y = 0; y < 16; y++) {
88 for (x = 0; x < 16; x++) {
96 static void FUNC(transquant_bypass32x32)(uint8_t *_dst, int16_t *coeffs,
100 pixel *dst = (pixel *)_dst;
102 stride /= sizeof(pixel);
104 for (y = 0; y < 32; y++) {
105 for (x = 0; x < 32; x++) {
113 static void FUNC(transform_skip)(uint8_t *_dst, int16_t *coeffs,
116 pixel *dst = (pixel *)_dst;
117 int shift = 13 - BIT_DEPTH;
119 int offset = 1 << (shift - 1);
125 stride /= sizeof(pixel);
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));
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))
139 #define TR_4x4_LUMA(dst, src, step, assign) \
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]; \
146 assign(dst[2 * step], 74 * (src[0 * 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); \
154 static void FUNC(transform_4x4_luma_add)(uint8_t *_dst, int16_t *coeffs,
158 pixel *dst = (pixel *)_dst;
160 int add = 1 << (shift - 1);
161 int16_t *src = coeffs;
163 stride /= sizeof(pixel);
165 for (i = 0; i < 4; i++) {
166 TR_4x4_LUMA(src, src, 4, SCALE);
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);
181 #define TR_4(dst, src, dstep, sstep, assign) \
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]; \
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); \
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)
200 static void FUNC(transform_4x4_add)(uint8_t *_dst, int16_t *coeffs,
204 pixel *dst = (pixel *)_dst;
206 int add = 1 << (shift - 1);
207 int16_t *src = coeffs;
209 stride /= sizeof(pixel);
211 for (i = 0; i < 4; i++) {
216 shift = 20 - BIT_DEPTH;
217 add = 1 << (shift - 1);
218 for (i = 0; i < 4; i++) {
225 #define TR_8(dst, src, dstep, sstep, assign) \
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); \
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]); \
241 #define TR_16(dst, src, dstep, sstep, assign) \
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); \
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]); \
257 #define TR_32(dst, src, dstep, sstep, assign) \
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); \
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]); \
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)
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)
281 static void FUNC(transform_8x8_add)(uint8_t *_dst, int16_t *coeffs,
285 pixel *dst = (pixel *)_dst;
287 int add = 1 << (shift - 1);
288 int16_t *src = coeffs;
290 stride /= sizeof(pixel);
292 for (i = 0; i < 8; i++) {
297 shift = 20 - BIT_DEPTH;
298 add = 1 << (shift - 1);
299 for (i = 0; i < 8; i++) {
306 static void FUNC(transform_16x16_add)(uint8_t *_dst, int16_t *coeffs,
310 pixel *dst = (pixel *)_dst;
312 int add = 1 << (shift - 1);
313 int16_t *src = coeffs;
315 stride /= sizeof(pixel);
317 for (i = 0; i < 16; i++) {
322 shift = 20 - BIT_DEPTH;
323 add = 1 << (shift - 1);
324 for (i = 0; i < 16; i++) {
325 TR_16_2(dst, coeffs);
331 static void FUNC(transform_32x32_add)(uint8_t *_dst, int16_t *coeffs,
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] )
344 pixel *dst = (pixel *)_dst;
346 int add = 1 << (shift - 1);
347 int16_t *src = coeffs;
349 stride /= sizeof(pixel);
351 for (i = 0; i < 32; i++) {
356 shift = 20 - BIT_DEPTH;
357 add = 1 << (shift - 1);
358 for (i = 0; i < 32; i++) {
359 TR_32_2(dst, coeffs);
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)
375 pixel *dst = (pixel *)_dst;
376 pixel *src = (pixel *)_src;
377 int offset_table[32] = { 0 };
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;
385 stride /= sizeof(pixel);
390 width -= (8 >> chroma) + 2;
392 height -= (4 >> chroma) + 2;
395 init_y = -(4 >> chroma) - 2;
397 width -= (8 >> chroma) + 2;
398 height = (4 >> chroma) + 2;
401 init_x = -(8 >> chroma) - 2;
402 width = (8 >> chroma) + 2;
404 height -= (4 >> chroma) + 2;
407 init_y = -(4 >> chroma) - 2;
408 init_x = -(8 >> chroma) - 2;
409 width = (8 >> chroma) + 2;
410 height = (4 >> chroma) + 2;
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)]);
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,
431 FUNC(sao_band_filter)(dst, src, stride, sao, borders,
432 width, height, c_idx, 0);
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,
440 FUNC(sao_band_filter)(dst, src, stride, sao, borders,
441 width, height, c_idx, 1);
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,
449 FUNC(sao_band_filter)(dst, src, stride, sao, borders,
450 width, height, c_idx, 2);
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,
458 FUNC(sao_band_filter)(_dst, _src, stride, sao, borders,
459 width, height, c_idx, 3);
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)
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;
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
482 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
484 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
486 stride /= sizeof(pixel);
489 width -= (8 >> chroma) + 2;
491 height -= (4 >> chroma) + 2;
493 dst = dst + (init_y * stride + init_x);
494 src = src + (init_y * stride + init_x);
496 if (sao_eo_class != SAO_EO_VERT) {
498 int offset_val = sao_offset_val[0];
500 for (y = 0; y < height; y++) {
501 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
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);
516 if (sao_eo_class != SAO_EO_HORIZ) {
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);
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);
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];
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]);
548 y_stride_0_1 += stride;
549 y_stride_1_1 += stride;
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++)
562 if(diag_edge && sao_eo_class == SAO_EO_135D)
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)
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;
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
589 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
591 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
593 stride /= sizeof(pixel);
595 init_y = -(4 >> chroma) - 2;
597 width -= (8 >> chroma) + 2;
598 height = (4 >> chroma) + 2;
600 dst = dst + (init_y * stride + init_x);
601 src = src + (init_y * stride + init_x);
603 if (sao_eo_class != SAO_EO_VERT) {
605 int offset_val = sao_offset_val[0];
607 for (y = 0; y < height; y++) {
608 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
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);
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];
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]);
640 y_stride_0_1 += stride;
641 y_stride_1_1 += stride;
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)];
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)
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];
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
680 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
682 int init_x = 0, init_y = 0, width = _width, height = _height;
684 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
685 stride /= sizeof(pixel);
687 init_x = -(8 >> chroma) - 2;
688 width = (8 >> chroma) + 2;
690 height -= (4 >> chroma) + 2;
692 dst = dst + (init_y * stride + init_x);
693 src = src + (init_y * stride + init_x);
695 if (sao_eo_class != SAO_EO_HORIZ) {
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);
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);
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];
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]);
727 y_stride_0_1 += stride;
728 y_stride_1_1 += stride;
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++)
741 if(diag_edge && sao_eo_class == SAO_EO_45D)
742 dst[width-1] = src[width-1];
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)
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;
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
767 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
769 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
771 stride /= sizeof(pixel);
773 init_y = -(4 >> chroma) - 2;
774 init_x = -(8 >> chroma) - 2;
775 width = (8 >> chroma) + 2;
776 height = (4 >> chroma) + 2;
779 dst = dst + (init_y * stride + init_x);
780 src = src + (init_y * stride + init_x);
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];
790 int y_stride_0_1 = (init_y + pos_0_1) * stride;
791 int y_stride_1_1 = (init_y + pos_1_1) * stride;
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]);
801 y_stride_0_1 += stride;
802 y_stride_1_1 += stride;
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];
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)
842 pixel *src = (pixel *)_src;
843 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
845 for (y = 0; y < height; y++) {
846 for (x = 0; x < width; x++)
847 dst[x] = src[x] << (14 - BIT_DEPTH);
853 #define QPEL_FILTER_1(src, stride) \
854 (1 * -src[x - 3 * stride] + \
855 4 * src[x - 2 * stride] - \
856 10 * src[x - stride] + \
858 17 * src[x + stride] - \
859 5 * src[x + 2 * stride] + \
860 1 * src[x + 3 * stride])
862 #define QPEL_FILTER_2(src, stride) \
863 (1 * -src[x - 3 * stride] + \
864 4 * src[x - 2 * stride] - \
865 11 * src[x - stride] + \
867 40 * src[x + stride] - \
868 11 * src[x + 2 * stride] + \
869 4 * src[x + 3 * stride] - \
870 1 * src[x + 4 * stride])
872 #define QPEL_FILTER_3(src, stride) \
873 (1 * src[x - 2 * stride] - \
874 5 * src[x - stride] + \
876 58 * src[x + stride] - \
877 10 * src[x + 2 * stride] + \
878 4 * src[x + 3 * stride] - \
879 1 * src[x + 4 * stride])
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, \
889 pixel *src = (pixel*)_src; \
890 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
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); \
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, \
907 pixel *src = (pixel*)_src; \
908 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
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); \
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, \
922 ptrdiff_t _srcstride, \
923 int width, int height, \
927 pixel *src = (pixel*)_src; \
928 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
930 int16_t tmp_array[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]; \
931 int16_t *tmp = tmp_array; \
933 src -= ff_hevc_qpel_extra_before[V] * srcstride; \
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); \
939 tmp += MAX_PB_SIZE; \
942 tmp = tmp_array + ff_hevc_qpel_extra_before[V] * MAX_PB_SIZE; \
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; \
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)
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,
974 pixel *src = (pixel *)_src;
975 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
977 for (y = 0; y < height; y++) {
978 for (x = 0; x < width; x++)
979 dst[x] = src[x] << (14 - BIT_DEPTH);
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])
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,
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);
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,
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];
1026 for (y = 0; y < height; y++) {
1027 for (x = 0; x < width; x++)
1028 dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
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,
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;
1051 src -= EPEL_EXTRA_BEFORE * srcstride;
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);
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;
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)
1078 pixel *dst = (pixel *)_dst;
1079 ptrdiff_t dststride = _dststride / sizeof(pixel);
1081 int shift = 14 - BIT_DEPTH;
1083 int offset = 1 << (shift - 1);
1087 for (y = 0; y < height; y++) {
1088 for (x = 0; x < width; x++)
1089 dst[x] = av_clip_pixel((src[x] + offset) >> shift);
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)
1101 pixel *dst = (pixel *)_dst;
1102 ptrdiff_t dststride = _dststride / sizeof(pixel);
1104 int shift = 14 + 1 - BIT_DEPTH;
1106 int offset = 1 << (shift - 1);
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);
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)
1125 int shift, log2Wd, wx, ox, x, y, offset;
1126 pixel *dst = (pixel *)_dst;
1127 ptrdiff_t dststride = _dststride / sizeof(pixel);
1129 shift = 14 - BIT_DEPTH;
1130 log2Wd = denom + shift;
1131 offset = 1 << (log2Wd - 1);
1133 ox = olxFlag * (1 << (BIT_DEPTH - 8));
1135 for (y = 0; y < height; y++) {
1136 for (x = 0; x < width; x++) {
1138 dst[x] = av_clip_pixel(((src[x] * wx + offset) >> log2Wd) + ox);
1140 dst[x] = av_clip_pixel(src[x] * wx + ox);
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)
1156 int shift, log2Wd, w0, w1, o0, o1, x, y;
1157 pixel *dst = (pixel *)_dst;
1158 ptrdiff_t dststride = _dststride / sizeof(pixel);
1160 shift = 14 - BIT_DEPTH;
1161 log2Wd = denom + shift;
1164 o0 = ol0Flag * (1 << (BIT_DEPTH - 8));
1165 o1 = ol1Flag * (1 << (BIT_DEPTH - 8));
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));
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]
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]
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)
1203 pixel *pix = (pixel *)_pix;
1204 ptrdiff_t xstride = _xstride / sizeof(pixel);
1205 ptrdiff_t ystride = _ystride / sizeof(pixel);
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];
1219 if (d0 + d3 >= beta /*|| tc <= 0*/) {
1223 const int beta_3 = beta >> 3;
1224 const int beta_2 = beta >> 2;
1225 const int tc25 = ((tc * 5 + 1) >> 1);
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) {
1231 const int tc2 = tc << 1;
1232 for (d = 0; d < 4; d++) {
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);
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);
1253 } else { // normal filtering
1256 const int tc_2 = tc >> 1;
1257 if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
1259 if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
1262 for (d = 0; d < 4; d++) {
1269 int delta0 = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
1270 if (abs(delta0) < 10 * tc) {
1271 delta0 = av_clip(delta0, -tc, tc);
1273 P0 = av_clip_pixel(p0 + delta0);
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);
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);
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)
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);
1301 for (j = 0; j < 2; j++) {
1302 const int tc = _tc[j] << (BIT_DEPTH - 8);
1310 for (d = 0; d < 4; d++) {
1316 delta0 = av_clip((((q0 - p0) << 2) + p1 - q1 + 4) >> 3, -tc, tc);
1318 P0 = av_clip_pixel(p0 + delta0);
1320 Q0 = av_clip_pixel(q0 - delta0);
1326 static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1327 int *tc, uint8_t *no_p,
1330 FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
1333 static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1334 int *tc, uint8_t *no_p,
1337 FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
1340 static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1341 int *beta, int *tc, uint8_t *no_p,
1344 FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel),
1345 beta, tc, no_p, no_q);
1348 static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1349 int *beta, int *tc, uint8_t *no_p,
1352 FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride,
1353 beta, tc, no_p, no_q);