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
24 #include "bit_depth_template.c"
28 #define SET(dst, x) (dst) = (x)
29 #define SCALE(dst, x) (dst) = av_clip_int16(((x) + add) >> shift)
30 #define ADD_AND_SCALE(dst, x) (dst) = av_clip_pixel((dst) + av_clip_int16(((x) + add) >> shift))
32 static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t _stride, int size,
33 GetBitContext *gb, int pcm_bit_depth)
36 pixel *dst = (pixel*)_dst;
37 ptrdiff_t stride = _stride / sizeof(pixel);
39 for (y = 0; y < size; y++) {
40 for (x = 0; x < size; x++)
41 dst[x] = get_bits(gb, pcm_bit_depth) << (BIT_DEPTH - pcm_bit_depth);
46 static void FUNC(transquant_bypass4x4)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
49 pixel *dst = (pixel*)_dst;
50 ptrdiff_t stride = _stride / sizeof(pixel);
52 for (y = 0; y < 4; y++) {
53 for (x = 0; x < 4; x++) {
62 static void FUNC(transquant_bypass8x8)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
65 pixel *dst = (pixel*)_dst;
66 ptrdiff_t stride = _stride / sizeof(pixel);
68 for (y = 0; y < 8; y++) {
69 for (x = 0; x < 8; x++) {
77 static void FUNC(transquant_bypass16x16)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
80 pixel *dst = (pixel*)_dst;
81 ptrdiff_t stride = _stride / sizeof(pixel);
83 for (y = 0; y < 16; y++) {
84 for (x = 0; x < 16; x++) {
93 static void FUNC(transquant_bypass32x32)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
96 pixel *dst = (pixel*)_dst;
97 ptrdiff_t stride = _stride / sizeof(pixel);
99 for (y = 0; y < 32; y++) {
100 for (x = 0; x < 32; x++) {
108 static void FUNC(transform_skip)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
110 pixel *dst = (pixel*)_dst;
111 ptrdiff_t stride = _stride / sizeof(pixel);
113 int shift = 13 - BIT_DEPTH;
115 int offset = 1 << (shift - 1);
122 for (y = 0; y < 32*32; y+=32) {
123 for (x = 0; x < 32; x++) {
124 dst[x] = av_clip_pixel(dst[x] + ((coeffs[y + x] + offset) >> shift));
130 for (y = 0; y < 16*16; y+=16) {
131 for (x = 0; x < 16; x++) {
132 dst[x] = av_clip_pixel(dst[x] + ((coeffs[y + x] + offset) >> shift));
138 for (y = 0; y < 8*8; y+=8) {
139 for (x = 0; x < 8; x++) {
140 dst[x] = av_clip_pixel(dst[x] + ((coeffs[y + x] + offset) >> shift));
146 for (y = 0; y < 4*4; y+=4) {
147 for (x = 0; x < 4; x++) {
148 dst[x] = av_clip_pixel(dst[x] + ((coeffs[y + x] + offset) >> shift));
156 static void FUNC(transform_4x4_luma_add)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
158 #define TR_4x4_LUMA(dst, src, step, assign) \
160 int c0 = src[0*step] + src[2*step]; \
161 int c1 = src[2*step] + src[3*step]; \
162 int c2 = src[0*step] - src[3*step]; \
163 int c3 = 74 * src[1*step]; \
165 assign(dst[2*step], 74 * (src[0*step] - src[2*step] + src[3*step])); \
166 assign(dst[0*step], 29 * c0 + 55 * c1 + c3); \
167 assign(dst[1*step], 55 * c2 - 29 * c1 + c3); \
168 assign(dst[3*step], 55 * c0 + 29 * c2 - c3); \
172 pixel *dst = (pixel*)_dst;
173 ptrdiff_t stride = _stride / sizeof(pixel);
175 int add = 1 << (shift - 1);
176 int16_t *src = coeffs;
178 for (i = 0; i < 4; i++) {
179 TR_4x4_LUMA(src, src, 4, SCALE);
183 shift = 20 - BIT_DEPTH;
184 add = 1 << (shift - 1);
185 for (i = 0; i < 4; i++) {
186 TR_4x4_LUMA(dst, coeffs, 1, ADD_AND_SCALE);
194 #define TR_4(dst, src, dstep, sstep, assign) \
196 const int e0 = transform[8*0][0] * src[0*sstep] + \
197 transform[8*2][0] * src[2*sstep]; \
198 const int e1 = transform[8*0][1] * src[0*sstep] + \
199 transform[8*2][1] * src[2*sstep]; \
200 const int o0 = transform[8*1][0] * src[1*sstep] + \
201 transform[8*3][0] * src[3*sstep]; \
202 const int o1 = transform[8*1][1] * src[1*sstep] + \
203 transform[8*3][1] * src[3*sstep]; \
205 assign(dst[0*dstep], e0 + o0); \
206 assign(dst[1*dstep], e1 + o1); \
207 assign(dst[2*dstep], e1 - o1); \
208 assign(dst[3*dstep], e0 - o0); \
210 #define TR_4_1(dst, src) TR_4(dst, src, 4, 4, SCALE)
211 #define TR_4_2(dst, src) TR_4(dst, src, 1, 1, ADD_AND_SCALE)
213 static void FUNC(transform_4x4_add)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
216 pixel *dst = (pixel*)_dst;
217 ptrdiff_t stride = _stride / sizeof(pixel);
219 int add = 1 << (shift - 1);
220 int16_t *src = coeffs;
222 for (i = 0; i < 4; i++) {
227 shift = 20 - BIT_DEPTH;
228 add = 1 << (shift - 1);
229 for (i = 0; i < 4; i++) {
236 #define TR_8(dst, src, dstep, sstep, assign) \
240 int o_8[4] = { 0 }; \
241 for (i = 0; i < 4; i++) \
242 for (j = 1; j < 8; j += 2) \
243 o_8[i] += transform[4*j][i] * src[j*sstep]; \
244 TR_4(e_8, src, 1, 2*sstep, SET); \
246 for (i = 0; i < 4; i++) { \
247 assign(dst[i*dstep], e_8[i] + o_8[i]); \
248 assign(dst[(7-i)*dstep], e_8[i] - o_8[i]); \
251 #define TR_16(dst, src, dstep, sstep, assign) \
255 int o_16[8] = { 0 }; \
256 for (i = 0; i < 8; i++) \
257 for (j = 1; j < 16; j += 2) \
258 o_16[i] += transform[2*j][i] * src[j*sstep]; \
259 TR_8(e_16, src, 1, 2*sstep, SET); \
261 for (i = 0; i < 8; i++) { \
262 assign(dst[i*dstep], e_16[i] + o_16[i]); \
263 assign(dst[(15-i)*dstep], e_16[i] - o_16[i]); \
266 #define TR_32(dst, src, dstep, sstep, assign) \
270 int o_32[16] = { 0 }; \
271 for (i = 0; i < 16; i++) \
272 for (j = 1; j < 32; j += 2) \
273 o_32[i] += transform[j][i] * src[j*sstep]; \
274 TR_16(e_32, src, 1, 2*sstep, SET); \
276 for (i = 0; i < 16; i++) { \
277 assign(dst[i*dstep], e_32[i] + o_32[i]); \
278 assign(dst[(31-i)*dstep], e_32[i] - o_32[i]); \
282 #define TR_8_1(dst, src) TR_8(dst, src, 8, 8, SCALE)
283 #define TR_16_1(dst, src) TR_16(dst, src, 16, 16, SCALE)
284 #define TR_32_1(dst, src) TR_32(dst, src, 32, 32, SCALE)
286 #define TR_8_2(dst, src) TR_8(dst, src, 1, 1, ADD_AND_SCALE)
287 #define TR_16_2(dst, src) TR_16(dst, src, 1, 1, ADD_AND_SCALE)
288 #define TR_32_2(dst, src) TR_32(dst, src, 1, 1, ADD_AND_SCALE)
290 static void FUNC(transform_8x8_add)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
293 pixel *dst = (pixel*)_dst;
294 ptrdiff_t stride = _stride / sizeof(pixel);
296 int add = 1 << (shift - 1);
297 int16_t *src = coeffs;
299 for (i = 0; i < 8; i++) {
304 shift = 20 - BIT_DEPTH;
305 add = 1 << (shift - 1);
306 for (i = 0; i < 8; i++) {
313 static void FUNC(transform_16x16_add)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
316 pixel *dst = (pixel*)_dst;
317 ptrdiff_t stride = _stride / sizeof(pixel);
319 int add = 1 << (shift - 1);
320 int16_t *src = coeffs;
322 for (i = 0; i < 16; i++) {
327 shift = 20 - BIT_DEPTH;
328 add = 1 << (shift - 1);
329 for (i = 0; i < 16; i++) {
330 TR_16_2(dst, coeffs);
336 static void FUNC(transform_32x32_add)(uint8_t *_dst, int16_t *coeffs, ptrdiff_t _stride)
338 #define IT32x32_even(i,w) ( src[ 0*w] * transform[ 0][i] ) + ( src[16*w] * transform[16][i] )
339 #define IT32x32_odd(i,w) ( src[ 8*w] * transform[ 8][i] ) + ( src[24*w] * transform[24][i] )
340 #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] )
341 #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] ) + \
342 ( src[18*w] * transform[18][i] ) + ( src[22*w] * transform[22][i] ) + ( src[26*w] * transform[26][i] ) + ( src[30*w] * transform[30][i] )
343 #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] ) + \
344 ( src[ 9*w] * transform[ 9][i] ) + ( src[11*w] * transform[11][i] ) + ( src[13*w] * transform[13][i] ) + ( src[15*w] * transform[15][i] ) + \
345 ( src[17*w] * transform[17][i] ) + ( src[19*w] * transform[19][i] ) + ( src[21*w] * transform[21][i] ) + ( src[23*w] * transform[23][i] ) + \
346 ( src[25*w] * transform[25][i] ) + ( src[27*w] * transform[27][i] ) + ( src[29*w] * transform[29][i] ) + ( src[31*w] * transform[31][i] )
348 pixel *dst = (pixel*)_dst;
349 ptrdiff_t stride = _stride / sizeof(pixel);
351 int add = 1 << (shift - 1);
352 int16_t *src = coeffs;
354 for (i = 0; i < 32; i++) {
359 shift = 20 - BIT_DEPTH;
360 add = 1 << (shift - 1);
361 for (i = 0; i < 32; i++) {
362 TR_32_2(dst, coeffs);
373 static void FUNC(sao_band_filter)(uint8_t *_dst, uint8_t *_src,
374 ptrdiff_t _stride, SAOParams *sao,
375 int *borders, int width, int height,
376 int c_idx, int class)
378 pixel *dst = (pixel*)_dst;
379 pixel *src = (pixel*)_src;
380 ptrdiff_t stride = _stride / sizeof(pixel);
381 int offset_table[32] = { 0 };
383 int chroma = !!c_idx;
384 int shift = BIT_DEPTH - 5;
385 int *sao_offset_val = sao->offset_val[c_idx];
386 int sao_left_class = sao->band_position[c_idx];
387 int init_y = 0, init_x = 0;
392 width -= ((8 >> chroma) + 2);
394 height -= ((4 >> chroma) + 2);
397 init_y = -(4 >> chroma) - 2;
399 width -= ((8 >> chroma) + 2);
400 height = (4 >> chroma) + 2;
403 init_x = -(8 >> chroma) - 2;
404 width = (8 >> chroma) + 2;
406 height -= ((4 >> chroma) + 2);
409 init_y = -(4 >> chroma) - 2;
410 init_x = -(8 >> chroma) - 2;
411 width = (8 >> chroma) + 2;
412 height = (4 >> chroma) + 2;
416 dst = dst + (init_y * stride + init_x);
417 src = src + (init_y * stride + init_x);
418 for (k = 0; k < 4; k++)
419 offset_table[(k + sao_left_class) & 31] = sao_offset_val[k + 1];
420 for (y = 0; y < height; y++) {
421 for (x = 0; x < width; x++)
422 dst[x] = av_clip_pixel(src[x] + offset_table[av_clip_pixel(src[x] >> shift)]);
428 static void FUNC(sao_band_filter_0)(uint8_t *dst, uint8_t *src,
429 ptrdiff_t stride, SAOParams *sao,
430 int *borders, int width, int height,
433 FUNC(sao_band_filter)(dst, src, stride, sao, borders, width, height, c_idx, 0);
436 static void FUNC(sao_band_filter_1)(uint8_t *dst, uint8_t *src,
437 ptrdiff_t stride, SAOParams *sao,
438 int *borders, int width, int height,
441 FUNC(sao_band_filter)(dst, src, stride, sao, borders, 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, width, height, c_idx, 2);
452 static void FUNC(sao_band_filter_3)(uint8_t *_dst, uint8_t *_src,
453 ptrdiff_t _stride, SAOParams *sao,
454 int *borders, int width, int height,
457 FUNC(sao_band_filter)(_dst, _src, _stride, sao, borders, width, height, c_idx, 3);
460 static void FUNC(sao_edge_filter_0)(uint8_t *_dst, uint8_t *_src,
461 ptrdiff_t _stride, SAOParams *sao,
462 int *borders, int _width, int _height,
464 uint8_t vert_edge, uint8_t horiz_edge, uint8_t diag_edge)
467 pixel *dst = (pixel*)_dst;
468 pixel *src = (pixel*)_src;
469 ptrdiff_t stride = _stride / sizeof(pixel);
470 int chroma = !!c_idx;
471 int *sao_offset_val = sao->offset_val[c_idx];
472 int sao_eo_class = sao->eo_class[c_idx];
474 static const int8_t pos[4][2][2] = {
475 {{ -1, 0}, { 1, 0}}, // horizontal
476 {{ 0, -1}, { 0, 1}}, // vertical
477 {{ -1, -1}, { 1, 1}}, // 45 degree
478 {{ 1, -1}, {-1, 1}}, // 135 degree
480 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
482 int init_x = 0, init_y = 0, width = _width, height = _height;
484 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
487 width -= (8 >> chroma) + 2;
489 height -= (4 >> chroma) + 2;
491 dst = dst + (init_y * stride + init_x);
492 src = src + (init_y * stride + init_x);
494 if (sao_eo_class != SAO_EO_VERT) {
496 int offset_val = sao_offset_val[0];
498 for (y = 0; y < height; y++) {
499 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
505 int offset_val = sao_offset_val[0];
506 int x_stride = width - 1;
507 for (x = 0; x < height; x++) {
508 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
515 if (sao_eo_class != SAO_EO_HORIZ ) {
517 int offset_val = sao_offset_val[0];
518 for (x = init_x; x < width; x++) {
519 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);
533 int y_stride = init_y * stride;
534 int pos_0_0 = pos[sao_eo_class][0][0];
535 int pos_0_1 = pos[sao_eo_class][0][1];
536 int pos_1_0 = pos[sao_eo_class][1][0];
537 int pos_1_1 = pos[sao_eo_class][1][1];
539 int y_stride_0_1 = (init_y + pos_0_1) * stride;
540 int y_stride_1_1 = (init_y + pos_1_1) * stride;
541 for (y = init_y; y < height; y++) {
542 for (x = init_x; x < width; x++) {
543 int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
544 int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
545 int offset_val = edge_idx[2 + diff0 + diff1];
546 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
549 y_stride_0_1 += stride;
550 y_stride_1_1 += stride;
555 // Restore pixels that can't be modified
556 int save_upper_left = !diag_edge && sao_eo_class == SAO_EO_135D && !borders[0] && !borders[1];
557 if (vert_edge && sao_eo_class != SAO_EO_VERT)
558 for (y = init_y+save_upper_left; y< height; y++)
559 dst[y*stride] = src[y*stride];
560 if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
561 for(x = init_x+save_upper_left; x<width; x++)
563 if(diag_edge && sao_eo_class == SAO_EO_135D)
570 static void FUNC(sao_edge_filter_1)(uint8_t *_dst, uint8_t *_src,
571 ptrdiff_t _stride, SAOParams *sao,
572 int *borders, int _width, int _height,
574 uint8_t vert_edge, uint8_t horiz_edge, uint8_t diag_edge)
577 pixel *dst = (pixel*)_dst;
578 pixel *src = (pixel*)_src;
579 ptrdiff_t stride = _stride / sizeof(pixel);
580 int chroma = !!c_idx;
581 int *sao_offset_val = sao->offset_val[c_idx];
582 int sao_eo_class = sao->eo_class[c_idx];
584 static const int8_t pos[4][2][2] = {
585 {{ -1, 0}, { 1, 0 }}, // horizontal
586 {{ 0, -1}, { 0, 1 }}, // vertical
587 {{ -1, -1}, { 1, 1 }}, // 45 degree
588 {{ 1, -1}, {-1, 1 }}, // 135 degree
590 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
592 int init_x = 0, init_y = 0, width = _width, height = _height;
594 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
596 init_y = -(4 >> chroma) - 2;
598 width -= (8 >> chroma) + 2;
599 height = (4 >> chroma) + 2;
601 dst = dst + (init_y * stride + init_x);
602 src = src + (init_y * stride + init_x);
604 if (sao_eo_class != SAO_EO_VERT) {
606 int offset_val = sao_offset_val[0];
608 for (y = 0; y < height; y++) {
609 dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
615 int offset_val = sao_offset_val[0];
616 int x_stride = width - 1;
617 for (x = 0; x < height; x++) {
618 dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
626 int y_stride = init_y * stride;
627 int pos_0_0 = pos[sao_eo_class][0][0];
628 int pos_0_1 = pos[sao_eo_class][0][1];
629 int pos_1_0 = pos[sao_eo_class][1][0];
630 int pos_1_1 = pos[sao_eo_class][1][1];
632 int y_stride_0_1 = (init_y + pos_0_1) * stride;
633 int y_stride_1_1 = (init_y + pos_1_1) * stride;
634 for (y = init_y; y < height; y++) {
635 for (x = init_x; x < width; x++) {
636 int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
637 int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
638 int offset_val = edge_idx[2 + diff0 + diff1];
639 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
642 y_stride_0_1 += stride;
643 y_stride_1_1 += stride;
648 // Restore pixels that can't be modified
649 int save_lower_left = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[0];
650 if(vert_edge && sao_eo_class != SAO_EO_VERT)
651 for(y = init_y; y< height-save_lower_left; y++)
652 dst[y*stride] = src[y*stride];
653 if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
654 for(x = init_x+save_lower_left; x<width; x++)
655 dst[(height-1)*stride+x] = src[(height-1)*stride+x];
656 if(diag_edge && sao_eo_class == SAO_EO_45D)
657 dst[stride*(height-1)] = src[stride*(height-1)];
663 static void FUNC(sao_edge_filter_2)(uint8_t *_dst, uint8_t *_src,
664 ptrdiff_t _stride, SAOParams *sao,
665 int *borders, int _width, int _height,
667 uint8_t vert_edge, uint8_t horiz_edge, uint8_t diag_edge)
670 pixel *dst = (pixel*)_dst;
671 pixel *src = (pixel*)_src;
672 ptrdiff_t stride = _stride / sizeof(pixel);
673 int chroma = !!c_idx;
674 int *sao_offset_val = sao->offset_val[c_idx];
675 int sao_eo_class = sao->eo_class[c_idx];
677 static const int8_t pos[4][2][2] = {
678 {{ -1, 0}, { 1, 0}}, // horizontal
679 {{ 0, -1}, { 0, 1}}, // vertical
680 {{ -1, -1}, { 1, 1}}, // 45 degree
681 {{ 1, -1}, {-1, 1}}, // 135 degree
683 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
685 int init_x = 0, init_y = 0, width = _width, height = _height;
687 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
689 init_x = -(8 >> chroma) - 2;
690 width = (8 >> chroma) + 2;
692 height -= (4 >> chroma) + 2;
694 dst = dst + (init_y * stride + init_x);
695 src = src + (init_y * stride + init_x);
697 if (sao_eo_class != SAO_EO_HORIZ) {
699 int offset_val = sao_offset_val[0];
700 for (x = init_x; x < width; x++) {
701 dst[x] = av_clip_pixel(src[x] + offset_val);
706 int offset_val = sao_offset_val[0];
707 int y_stride = stride * (height - 1);
708 for (x = init_x; x < width; x++) {
709 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
715 int y_stride = init_y * stride;
716 int pos_0_0 = pos[sao_eo_class][0][0];
717 int pos_0_1 = pos[sao_eo_class][0][1];
718 int pos_1_0 = pos[sao_eo_class][1][0];
719 int pos_1_1 = pos[sao_eo_class][1][1];
721 int y_stride_0_1 = (init_y + pos_0_1) * stride;
722 int y_stride_1_1 = (init_y + pos_1_1) * stride;
723 for (y = init_y; y < height; y++) {
724 for (x = init_x; x < width; x++) {
725 int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
726 int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
727 int offset_val = edge_idx[2 + diff0 + diff1];
728 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
731 y_stride_0_1 += stride;
732 y_stride_1_1 += stride;
737 // Restore pixels that can't be modified
738 int save_upper_right = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[1];
739 if(vert_edge && sao_eo_class != SAO_EO_VERT)
740 for(y = init_y+save_upper_right; y< height; y++)
741 dst[y*stride+width-1] = src[y*stride+width-1];
742 if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
743 for(x = init_x; x<width-save_upper_right; x++)
745 if(diag_edge && sao_eo_class == SAO_EO_45D)
746 dst[width-1] = src[width-1];
751 static void FUNC(sao_edge_filter_3)(uint8_t *_dst, uint8_t *_src,
752 ptrdiff_t _stride, SAOParams *sao,
753 int *borders, int _width, int _height,
755 uint8_t vert_edge, uint8_t horiz_edge, uint8_t diag_edge)
758 pixel *dst = (pixel*)_dst;
759 pixel *src = (pixel*)_src;
760 ptrdiff_t stride = _stride / sizeof(pixel);
761 int chroma = !!c_idx;
762 int *sao_offset_val = sao->offset_val[c_idx];
763 int sao_eo_class = sao->eo_class[c_idx];
765 static const int8_t pos[4][2][2] = {
766 {{ -1, 0}, { 1, 0}}, // horizontal
767 {{ 0, -1}, { 0, 1}}, // vertical
768 {{ -1, -1}, { 1, 1}}, // 45 degree
769 {{ 1, -1}, {-1, 1}}, // 135 degree
771 static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
773 int init_x = 0, init_y = 0, width = _width, height = _height;
775 #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
777 init_y = -(4 >> chroma) - 2;
778 init_x = -(8 >> chroma) - 2;
779 width = (8 >> chroma) + 2;
780 height = (4 >> chroma) + 2;
783 dst = dst + (init_y * stride + init_x);
784 src = src + (init_y * stride + init_x);
788 int y_stride = init_y * stride;
789 int pos_0_0 = pos[sao_eo_class][0][0];
790 int pos_0_1 = pos[sao_eo_class][0][1];
791 int pos_1_0 = pos[sao_eo_class][1][0];
792 int pos_1_1 = pos[sao_eo_class][1][1];
794 int y_stride_0_1 = (init_y + pos_0_1) * stride;
795 int y_stride_1_1 = (init_y + pos_1_1) * stride;
797 for (y = init_y; y < height; y++) {
798 for (x = init_x; x < width; x++) {
799 int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
800 int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
801 int offset_val = edge_idx[2 + diff0 + diff1];
802 dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
805 y_stride_0_1 += stride;
806 y_stride_1_1 += stride;
811 // Restore pixels that can't be modified
812 int save_lower_right = !diag_edge && sao_eo_class == SAO_EO_135D;
813 if(vert_edge && sao_eo_class != SAO_EO_VERT)
814 for(y = init_y; y< height-save_lower_right; y++)
815 dst[y*stride+width-1] = src[y*stride+width-1];
816 if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
817 for(x = init_x; x<width-save_lower_right; x++)
818 dst[(height-1)*stride+x] = src[(height-1)*stride+x];
819 if(diag_edge && sao_eo_class == SAO_EO_135D)
820 dst[stride*(height-1)+width-1] = src[stride*(height-1)+width-1];
841 static void FUNC(put_hevc_qpel_pixels)(int16_t *dst, ptrdiff_t dststride,
842 uint8_t *_src, ptrdiff_t _srcstride,
843 int width, int height, int16_t* mcbuffer)
846 pixel *src = (pixel*)_src;
847 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
849 for (y = 0; y < height; y++) {
850 for (x = 0; x < width; x++)
851 dst[x] = src[x] << (14 - BIT_DEPTH);
857 #define QPEL_FILTER_1(src, stride) \
858 (-src[x-3*stride] + 4*src[x-2*stride] - 10*src[x-stride] + 58*src[x] + \
859 17*src[x+stride] - 5*src[x+2*stride] + 1*src[x+3*stride])
860 #define QPEL_FILTER_2(src, stride) \
861 (-src[x-3*stride] + 4*src[x-2*stride] - 11*src[x-stride] + 40*src[x] + \
862 40*src[x+stride] - 11*src[x+2*stride] + 4*src[x+3*stride] - src[x+4*stride])
863 #define QPEL_FILTER_3(src, stride) \
864 (src[x-2*stride] - 5*src[x-stride] + 17*src[x] + 58*src[x+stride] \
865 - 10*src[x+2*stride] + 4*src[x+3*stride] - src[x+4*stride])
868 #define PUT_HEVC_QPEL_H(H) \
869 static void FUNC(put_hevc_qpel_h ## H)(int16_t *dst, ptrdiff_t dststride, \
870 uint8_t *_src, ptrdiff_t _srcstride, \
871 int width, int height, \
875 pixel *src = (pixel*)_src; \
876 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
878 for (y = 0; y < height; y++) { \
879 for (x = 0; x < width; x++) \
880 dst[x] = QPEL_FILTER_ ## H (src, 1) >> (BIT_DEPTH - 8); \
886 #define PUT_HEVC_QPEL_V(V) \
887 static void FUNC(put_hevc_qpel_v ## V)(int16_t *dst, ptrdiff_t dststride, \
888 uint8_t *_src, ptrdiff_t _srcstride, \
889 int width, int height, \
893 pixel *src = (pixel*)_src; \
894 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
896 for (y = 0; y < height; y++) { \
897 for (x = 0; x < width; x++) \
898 dst[x] = QPEL_FILTER_ ## V (src, srcstride) >> (BIT_DEPTH - 8); \
904 #define PUT_HEVC_QPEL_HV(H, V) \
905 static void FUNC(put_hevc_qpel_h ## H ## v ## V)(int16_t *dst, ptrdiff_t dststride, \
906 uint8_t *_src, ptrdiff_t _srcstride,\
907 int width, int height, \
911 pixel *src = (pixel*)_src; \
912 ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
914 int16_t tmp_array[(MAX_PB_SIZE + 7)*MAX_PB_SIZE]; \
915 int16_t *tmp = tmp_array; \
917 src -= ff_hevc_qpel_extra_before[V] * srcstride; \
919 for (y = 0; y < height + ff_hevc_qpel_extra[V]; y++) { \
920 for (x = 0; x < width; x++) \
921 tmp[x] = QPEL_FILTER_ ## H (src, 1) >> (BIT_DEPTH - 8); \
923 tmp += MAX_PB_SIZE; \
926 tmp = tmp_array + ff_hevc_qpel_extra_before[V] * MAX_PB_SIZE; \
928 for (y = 0; y < height; y++) { \
929 for (x = 0; x < width; x++) \
930 dst[x] = QPEL_FILTER_ ## V (tmp, MAX_PB_SIZE) >> 6; \
931 tmp += MAX_PB_SIZE; \
942 PUT_HEVC_QPEL_HV(1, 1)
943 PUT_HEVC_QPEL_HV(1, 2)
944 PUT_HEVC_QPEL_HV(1, 3)
945 PUT_HEVC_QPEL_HV(2, 1)
946 PUT_HEVC_QPEL_HV(2, 2)
947 PUT_HEVC_QPEL_HV(2, 3)
948 PUT_HEVC_QPEL_HV(3, 1)
949 PUT_HEVC_QPEL_HV(3, 2)
950 PUT_HEVC_QPEL_HV(3, 3)
952 static void FUNC(put_hevc_epel_pixels)(int16_t *dst, ptrdiff_t dststride,
953 uint8_t *_src, ptrdiff_t _srcstride,
954 int width, int height, int mx, int my,
958 pixel *src = (pixel*)_src;
959 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
961 for (y = 0; y < height; y++) {
962 for (x = 0; x < width; x++) {
963 dst[x] = src[x] << (14 - BIT_DEPTH);
970 #define EPEL_FILTER(src, stride) \
971 (filter_0*src[x-stride] + filter_1*src[x] + filter_2*src[x+stride] + filter_3*src[x+2*stride])
973 static void FUNC(put_hevc_epel_h)(int16_t *dst, ptrdiff_t dststride,
974 uint8_t *_src, ptrdiff_t _srcstride,
975 int width, int height, int mx, int my,
979 pixel *src = (pixel*)_src;
980 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
981 const int8_t *filter = ff_hevc_epel_filters[mx - 1];
982 int8_t filter_0 = filter[0];
983 int8_t filter_1 = filter[1];
984 int8_t filter_2 = filter[2];
985 int8_t filter_3 = filter[3];
986 for (y = 0; y < height; y++) {
987 for (x = 0; x < width; x++) {
988 dst[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
995 static void FUNC(put_hevc_epel_v)(int16_t *dst, ptrdiff_t dststride,
996 uint8_t *_src, ptrdiff_t _srcstride,
997 int width, int height, int mx, int my,
1001 pixel *src = (pixel*)_src;
1002 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1003 const int8_t *filter = ff_hevc_epel_filters[my-1];
1004 int8_t filter_0 = filter[0];
1005 int8_t filter_1 = filter[1];
1006 int8_t filter_2 = filter[2];
1007 int8_t filter_3 = filter[3];
1009 for (y = 0; y < height; y++) {
1010 for (x = 0; x < width; x++) {
1011 dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
1018 static void FUNC(put_hevc_epel_hv)(int16_t *dst, ptrdiff_t dststride,
1019 uint8_t *_src, ptrdiff_t _srcstride,
1020 int width, int height, int mx, int my,
1024 pixel *src = (pixel*)_src;
1025 ptrdiff_t srcstride = _srcstride / sizeof(pixel);
1026 const int8_t *filter_h = ff_hevc_epel_filters[mx-1];
1027 const int8_t *filter_v = ff_hevc_epel_filters[my-1];
1028 int8_t filter_0 = filter_h[0];
1029 int8_t filter_1 = filter_h[1];
1030 int8_t filter_2 = filter_h[2];
1031 int8_t filter_3 = filter_h[3];
1032 int16_t tmp_array[(MAX_PB_SIZE + 3)*MAX_PB_SIZE];
1033 int16_t *tmp = tmp_array;
1035 src -= EPEL_EXTRA_BEFORE * srcstride;
1037 for (y = 0; y < height + EPEL_EXTRA; y++) {
1038 for (x = 0; x < width; x++) {
1039 tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
1045 tmp = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
1046 filter_0 = filter_v[0];
1047 filter_1 = filter_v[1];
1048 filter_2 = filter_v[2];
1049 filter_3 = filter_v[3];
1050 for (y = 0; y < height; y++) {
1051 for (x = 0; x < width; x++) {
1052 dst[x] = EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
1059 static void FUNC(put_unweighted_pred)(uint8_t *_dst, ptrdiff_t _dststride,
1060 int16_t *src, ptrdiff_t srcstride,
1061 int width, int height)
1064 pixel *dst = (pixel*)_dst;
1065 ptrdiff_t dststride = _dststride / sizeof(pixel);
1067 int shift = 14 - BIT_DEPTH;
1069 int offset = 1 << (shift - 1);
1073 for (y = 0; y < height; y++) {
1074 for (x = 0; x < width; x++) {
1075 dst[x] = av_clip_pixel((src[x] + offset) >> shift);
1082 static void FUNC(put_weighted_pred_avg)(uint8_t *_dst, ptrdiff_t _dststride,
1083 int16_t *src1, int16_t *src2,
1084 ptrdiff_t srcstride,
1085 int width, int height)
1088 pixel *dst = (pixel*)_dst;
1089 ptrdiff_t dststride = _dststride / sizeof(pixel);
1091 int shift = 14 + 1 - BIT_DEPTH;
1093 int offset = 1 << (shift - 1);
1098 for (y = 0; y < height; y++) {
1099 for (x = 0; x < width; x++) {
1100 dst[x] = av_clip_pixel((src1[x] + src2[x] + offset) >> shift);
1108 static void FUNC(weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
1109 uint8_t *_dst, ptrdiff_t _dststride,
1110 int16_t *src, ptrdiff_t srcstride,
1111 int width, int height)
1119 pixel *dst = (pixel*)_dst;
1120 ptrdiff_t dststride = _dststride / sizeof(pixel);
1122 shift = 14 - BIT_DEPTH;
1123 log2Wd = denom + shift;
1124 offset = 1 << (log2Wd - 1);
1126 ox = olxFlag * (1 << (BIT_DEPTH - 8));
1128 for (y = 0; y < height; y++) {
1129 for (x = 0; x < width; x++) {
1131 dst[x] = av_clip_pixel(((src[x] * wx + offset) >> log2Wd) + ox);
1133 dst[x] = av_clip_pixel(src[x] * wx + ox);
1141 static void FUNC(weighted_pred_avg)(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag,
1142 int16_t ol0Flag, int16_t ol1Flag,
1143 uint8_t *_dst, ptrdiff_t _dststride,
1144 int16_t *src1, int16_t *src2, ptrdiff_t srcstride,
1145 int width, int height)
1154 pixel *dst = (pixel*)_dst;
1155 ptrdiff_t dststride = _dststride / sizeof(pixel);
1157 shift = 14 - BIT_DEPTH;
1158 log2Wd = denom + shift;
1161 o0 = (ol0Flag) * (1 << (BIT_DEPTH - 8));
1162 o1 = (ol1Flag) * (1 << (BIT_DEPTH - 8));
1164 for (y = 0; y < height; y++) {
1165 for (x = 0; x < width; x++) {
1166 dst[x] = av_clip_pixel((src1[x] * w0 + src2[x] * w1 +
1167 ((o0 + o1 + 1) << log2Wd)) >> (log2Wd + 1));
1176 #define P3 pix[-4*xstride]
1177 #define P2 pix[-3*xstride]
1178 #define P1 pix[-2*xstride]
1179 #define P0 pix[-xstride]
1181 #define Q1 pix[xstride]
1182 #define Q2 pix[2*xstride]
1183 #define Q3 pix[3*xstride]
1185 // line three. used only for deblocking decision
1186 #define TP3 pix[-4*xstride+3*ystride]
1187 #define TP2 pix[-3*xstride+3*ystride]
1188 #define TP1 pix[-2*xstride+3*ystride]
1189 #define TP0 pix[-xstride+3*ystride]
1190 #define TQ0 pix[3*ystride]
1191 #define TQ1 pix[xstride+3*ystride]
1192 #define TQ2 pix[2*xstride+3*ystride]
1193 #define TQ3 pix[3*xstride+3*ystride]
1195 static void FUNC(hevc_loop_filter_luma)(uint8_t *_pix, ptrdiff_t _xstride,
1196 ptrdiff_t _ystride, int *_beta, int *_tc,
1197 uint8_t *_no_p, uint8_t *_no_q)
1200 pixel *pix = (pixel*)_pix;
1201 ptrdiff_t xstride = _xstride / sizeof(pixel);
1202 ptrdiff_t ystride = _ystride / sizeof(pixel);
1204 for (j = 0; j < 2; j++) {
1205 const int dp0 = abs(P2 - 2 * P1 + P0);
1206 const int dq0 = abs(Q2 - 2 * Q1 + Q0);
1207 const int dp3 = abs(TP2 - 2 * TP1 + TP0);
1208 const int dq3 = abs(TQ2 - 2 * TQ1 + TQ0);
1209 const int d0 = dp0 + dq0;
1210 const int d3 = dp3 + dq3;
1211 int beta = _beta[j] << (BIT_DEPTH - 8);
1212 const int tc = _tc[j] << (BIT_DEPTH - 8);
1213 const int no_p = _no_p[j];
1214 const int no_q = _no_q[j];
1216 if (d0 + d3 >= beta /*|| tc <= 0*/) {
1220 const int beta_3 = beta >> 3;
1221 const int beta_2 = beta >> 2;
1222 const int tc25 = ((tc * 5 + 1) >> 1);
1224 if (abs( P3 - P0) + abs( Q3 - Q0) < beta_3 && abs( P0 - Q0) < tc25 &&
1225 abs(TP3 - TP0) + abs(TQ3 - TQ0) < beta_3 && abs(TP0 - TQ0) < tc25 &&
1226 (d0 << 1) < beta_2 && (d3 << 1) < beta_2) {
1228 const int tc2 = tc << 1;
1229 for (d = 0; d < 4; d++) {
1239 P0 = p0 + av_clip((( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3) - p0, -tc2, tc2);
1240 P1 = p1 + av_clip((( p2 + p1 + p0 + q0 + 2 ) >> 2) - p1, -tc2, tc2);
1241 P2 = p2 + av_clip((( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3) - p2, -tc2, tc2);
1244 Q0 = q0 + av_clip((( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3) - q0, -tc2, tc2);
1245 Q1 = q1 + av_clip((( p0 + q0 + q1 + q2 + 2 ) >> 2) - q1, -tc2, tc2);
1246 Q2 = q2 + av_clip((( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3) - q2, -tc2, tc2);
1250 } else { // normal filtering
1253 const int tc_2 = tc >> 1;
1254 if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
1256 if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
1259 for (d = 0; d < 4; d++) {
1266 int delta0 = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
1267 if (abs(delta0) < 10 * tc) {
1268 delta0 = av_clip(delta0, -tc, tc);
1270 P0 = av_clip_pixel(p0 + delta0);
1272 Q0 = av_clip_pixel(q0 - delta0);
1273 if (!no_p && nd_p > 1) {
1274 const int deltap1 = av_clip((((p2 + p0 + 1) >> 1) - p1 + delta0) >> 1, -tc_2, tc_2);
1275 P1 = av_clip_pixel(p1 + deltap1);
1277 if (!no_q && nd_q > 1) {
1278 const int deltaq1 = av_clip((((q2 + q0 + 1) >> 1) - q1 - delta0) >> 1, -tc_2, tc_2);
1279 Q1 = av_clip_pixel(q1 + deltaq1);
1289 static void FUNC(hevc_loop_filter_chroma)(uint8_t *_pix, ptrdiff_t _xstride,
1290 ptrdiff_t _ystride, int *_tc,
1291 uint8_t *_no_p, uint8_t *_no_q)
1295 pixel *pix = (pixel*)_pix;
1296 ptrdiff_t xstride = _xstride / sizeof(pixel);
1297 ptrdiff_t ystride = _ystride / sizeof(pixel);
1299 for (j = 0; j < 2; j++) {
1300 const int tc = _tc[j] << (BIT_DEPTH - 8);
1308 for (d = 0; d < 4; d++) {
1314 delta0 = av_clip((((q0 - p0) << 2) + p1 - q1 + 4) >> 3, -tc, tc);
1316 P0 = av_clip_pixel(p0 + delta0);
1318 Q0 = av_clip_pixel(q0 - delta0);
1324 static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1325 int *tc, uint8_t *no_p, uint8_t *no_q)
1327 FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
1330 static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
1331 int *tc, uint8_t *no_p, uint8_t *no_q)
1333 FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
1336 static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1337 int *beta, int *tc, uint8_t *no_p,
1340 FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel), beta, tc, no_p, no_q);
1343 static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
1344 int *beta, int *tc, uint8_t *no_p,
1347 FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride, beta, tc, no_p, no_q);