FFmpeg
h2656_sao_template.c
Go to the documentation of this file.
1 /*
2  * HEVC/VVC SAO template
3  *
4  * Copyright (C) 2024 Nuo Mi
5  * Copyright (C) 2012 - 2013 Guillaume Martres
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 static void FUNC(sao_band_filter)(uint8_t *_dst, const uint8_t *_src,
25  ptrdiff_t stride_dst, ptrdiff_t stride_src,
26  const int16_t *sao_offset_val, int sao_left_class,
27  int width, int height)
28 {
29  pixel *dst = (pixel *)_dst;
30  const pixel *src = (const pixel *)_src;
31  int offset_table[32] = { 0 };
32  int k, y, x;
33  int shift = BIT_DEPTH - 5;
34 
35  stride_dst /= sizeof(pixel);
36  stride_src /= sizeof(pixel);
37 
38  for (k = 0; k < 4; k++)
39  offset_table[(k + sao_left_class) & 31] = sao_offset_val[k + 1];
40  for (y = 0; y < height; y++) {
41  for (x = 0; x < width; x++)
42  dst[x] = av_clip_pixel(src[x] + offset_table[(src[x] >> shift) & 31]);
43  dst += stride_dst;
44  src += stride_src;
45  }
46 }
47 
48 #define CMP(a, b) (((a) > (b)) - ((a) < (b)))
49 
50 static void FUNC(sao_edge_filter)(uint8_t *_dst, const uint8_t *_src, ptrdiff_t stride_dst, const int16_t *sao_offset_val,
51  int eo, int width, int height) {
52 
53  static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
54  static const int8_t pos[4][2][2] = {
55  { { -1, 0 }, { 1, 0 } }, // horizontal
56  { { 0, -1 }, { 0, 1 } }, // vertical
57  { { -1, -1 }, { 1, 1 } }, // 45 degree
58  { { 1, -1 }, { -1, 1 } }, // 135 degree
59  };
60  pixel *dst = (pixel *)_dst;
61  const pixel *src = (const pixel *)_src;
62  int a_stride, b_stride;
63  int x, y;
64  ptrdiff_t stride_src = (2*MAX_PB_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / sizeof(pixel);
65  stride_dst /= sizeof(pixel);
66 
67  a_stride = pos[eo][0][0] + pos[eo][0][1] * stride_src;
68  b_stride = pos[eo][1][0] + pos[eo][1][1] * stride_src;
69  for (y = 0; y < height; y++) {
70  for (x = 0; x < width; x++) {
71  int diff0 = CMP(src[x], src[x + a_stride]);
72  int diff1 = CMP(src[x], src[x + b_stride]);
73  int offset_val = edge_idx[2 + diff0 + diff1];
74  dst[x] = av_clip_pixel(src[x] + sao_offset_val[offset_val]);
75  }
76  src += stride_src;
77  dst += stride_dst;
78  }
79 }
80 
81 static void FUNC(sao_edge_restore_0)(uint8_t *_dst, const uint8_t *_src,
82  ptrdiff_t stride_dst, ptrdiff_t stride_src, const SAOParams *sao,
83  const int *borders, int _width, int _height,
84  int c_idx, const uint8_t *vert_edge,
85  const uint8_t *horiz_edge, const uint8_t *diag_edge)
86 {
87  int x, y;
88  pixel *dst = (pixel *)_dst;
89  const pixel *src = (const pixel *)_src;
90  const int16_t *sao_offset_val = sao->offset_val[c_idx];
91  int sao_eo_class = sao->eo_class[c_idx];
92  int init_x = 0, width = _width, height = _height;
93 
94  stride_dst /= sizeof(pixel);
95  stride_src /= sizeof(pixel);
96 
97  if (sao_eo_class != SAO_EO_VERT) {
98  if (borders[0]) {
99  int offset_val = sao_offset_val[0];
100  for (y = 0; y < height; y++) {
101  dst[y * stride_dst] = av_clip_pixel(src[y * stride_src] + offset_val);
102  }
103  init_x = 1;
104  }
105  if (borders[2]) {
106  int offset_val = sao_offset_val[0];
107  int offset = width - 1;
108  for (x = 0; x < height; x++) {
109  dst[x * stride_dst + offset] = av_clip_pixel(src[x * stride_src + offset] + offset_val);
110  }
111  width--;
112  }
113  }
114  if (sao_eo_class != SAO_EO_HORIZ) {
115  if (borders[1]) {
116  int offset_val = sao_offset_val[0];
117  for (x = init_x; x < width; x++)
118  dst[x] = av_clip_pixel(src[x] + offset_val);
119  }
120  if (borders[3]) {
121  int offset_val = sao_offset_val[0];
122  ptrdiff_t y_stride_dst = stride_dst * (height - 1);
123  ptrdiff_t y_stride_src = stride_src * (height - 1);
124  for (x = init_x; x < width; x++)
125  dst[x + y_stride_dst] = av_clip_pixel(src[x + y_stride_src] + offset_val);
126  height--;
127  }
128  }
129 }
130 
131 static void FUNC(sao_edge_restore_1)(uint8_t *_dst, const uint8_t *_src,
132  ptrdiff_t stride_dst, ptrdiff_t stride_src, const SAOParams *sao,
133  const int *borders, int _width, int _height,
134  int c_idx, const uint8_t *vert_edge,
135  const uint8_t *horiz_edge, const uint8_t *diag_edge)
136 {
137  int x, y;
138  pixel *dst = (pixel *)_dst;
139  const pixel *src = (const pixel *)_src;
140  const int16_t *sao_offset_val = sao->offset_val[c_idx];
141  int sao_eo_class = sao->eo_class[c_idx];
142  int init_x = 0, init_y = 0, width = _width, height = _height;
143 
144  stride_dst /= sizeof(pixel);
145  stride_src /= sizeof(pixel);
146 
147  if (sao_eo_class != SAO_EO_VERT) {
148  if (borders[0]) {
149  int offset_val = sao_offset_val[0];
150  for (y = 0; y < height; y++) {
151  dst[y * stride_dst] = av_clip_pixel(src[y * stride_src] + offset_val);
152  }
153  init_x = 1;
154  }
155  if (borders[2]) {
156  int offset_val = sao_offset_val[0];
157  int offset = width - 1;
158  for (x = 0; x < height; x++) {
159  dst[x * stride_dst + offset] = av_clip_pixel(src[x * stride_src + offset] + offset_val);
160  }
161  width--;
162  }
163  }
164  if (sao_eo_class != SAO_EO_HORIZ) {
165  if (borders[1]) {
166  int offset_val = sao_offset_val[0];
167  for (x = init_x; x < width; x++)
168  dst[x] = av_clip_pixel(src[x] + offset_val);
169  init_y = 1;
170  }
171  if (borders[3]) {
172  int offset_val = sao_offset_val[0];
173  ptrdiff_t y_stride_dst = stride_dst * (height - 1);
174  ptrdiff_t y_stride_src = stride_src * (height - 1);
175  for (x = init_x; x < width; x++)
176  dst[x + y_stride_dst] = av_clip_pixel(src[x + y_stride_src] + offset_val);
177  height--;
178  }
179  }
180 
181  {
182  int save_upper_left = !diag_edge[0] && sao_eo_class == SAO_EO_135D && !borders[0] && !borders[1];
183  int save_upper_right = !diag_edge[1] && sao_eo_class == SAO_EO_45D && !borders[1] && !borders[2];
184  int save_lower_right = !diag_edge[2] && sao_eo_class == SAO_EO_135D && !borders[2] && !borders[3];
185  int save_lower_left = !diag_edge[3] && sao_eo_class == SAO_EO_45D && !borders[0] && !borders[3];
186 
187  // Restore pixels that can't be modified
188  if(vert_edge[0] && sao_eo_class != SAO_EO_VERT) {
189  for(y = init_y+save_upper_left; y< height-save_lower_left; y++)
190  dst[y*stride_dst] = src[y*stride_src];
191  }
192  if(vert_edge[1] && sao_eo_class != SAO_EO_VERT) {
193  for(y = init_y+save_upper_right; y< height-save_lower_right; y++)
194  dst[y*stride_dst+width-1] = src[y*stride_src+width-1];
195  }
196 
197  if(horiz_edge[0] && sao_eo_class != SAO_EO_HORIZ) {
198  for(x = init_x+save_upper_left; x < width-save_upper_right; x++)
199  dst[x] = src[x];
200  }
201  if(horiz_edge[1] && sao_eo_class != SAO_EO_HORIZ) {
202  for(x = init_x+save_lower_left; x < width-save_lower_right; x++)
203  dst[(height-1)*stride_dst+x] = src[(height-1)*stride_src+x];
204  }
205  if(diag_edge[0] && sao_eo_class == SAO_EO_135D)
206  dst[0] = src[0];
207  if(diag_edge[1] && sao_eo_class == SAO_EO_45D)
208  dst[width-1] = src[width-1];
209  if(diag_edge[2] && sao_eo_class == SAO_EO_135D)
210  dst[stride_dst*(height-1)+width-1] = src[stride_src*(height-1)+width-1];
211  if(diag_edge[3] && sao_eo_class == SAO_EO_45D)
212  dst[stride_dst*(height-1)] = src[stride_src*(height-1)];
213 
214  }
215 }
216 
217 #undef CMP
SAO_EO_45D
@ SAO_EO_45D
Definition: hevcdec.h:168
BIT_DEPTH
#define BIT_DEPTH
Definition: aom_film_grain.c:61
sao_edge_restore_1
static void FUNC() sao_edge_restore_1(uint8_t *_dst, const uint8_t *_src, ptrdiff_t stride_dst, ptrdiff_t stride_src, const SAOParams *sao, const int *borders, int _width, int _height, int c_idx, const uint8_t *vert_edge, const uint8_t *horiz_edge, const uint8_t *diag_edge)
Definition: h2656_sao_template.c:131
CMP
#define CMP(a, b)
Definition: h2656_sao_template.c:48
sao_edge_restore_0
static void FUNC() sao_edge_restore_0(uint8_t *_dst, const uint8_t *_src, ptrdiff_t stride_dst, ptrdiff_t stride_src, const SAOParams *sao, const int *borders, int _width, int _height, int c_idx, const uint8_t *vert_edge, const uint8_t *horiz_edge, const uint8_t *diag_edge)
Definition: h2656_sao_template.c:81
SAO_EO_135D
@ SAO_EO_135D
Definition: hevcdec.h:167
width
#define width
sao_band_filter
static void FUNC() sao_band_filter(uint8_t *_dst, const uint8_t *_src, ptrdiff_t stride_dst, ptrdiff_t stride_src, const int16_t *sao_offset_val, int sao_left_class, int width, int height)
Definition: h2656_sao_template.c:24
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
shift
static int shift(int a, int b)
Definition: bonk.c:262
height
#define height
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
offset_table
static const uint8_t offset_table[]
Definition: escape130.c:42
MAX_PB_SIZE
#define MAX_PB_SIZE
Definition: hevcdsp.h:32
SAO_EO_HORIZ
@ SAO_EO_HORIZ
Definition: hevcdec.h:165
SAOParams
Definition: hevcdsp.h:34
sao_edge_filter
static void FUNC() sao_edge_filter(uint8_t *_dst, const uint8_t *_src, ptrdiff_t stride_dst, const int16_t *sao_offset_val, int eo, int width, int height)
Definition: h2656_sao_template.c:50
av_clip_pixel
#define av_clip_pixel(a)
Definition: bit_depth_template.c:98
pos
unsigned int pos
Definition: spdifenc.c:413
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:104
SAO_EO_VERT
@ SAO_EO_VERT
Definition: hevcdec.h:166
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418