Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libde265/libde265/sao.cc
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "sao.h"
22
#include "util.h"
23
24
#include <stdlib.h>
25
#include <string.h>
26
27
28
template <class pixel_t>
29
void apply_sao_internal(de265_image* img, int xCtb,int yCtb,
30
                        const slice_segment_header* shdr, int cIdx, int nSW,int nSH,
31
                        const pixel_t* in_img,  ptrdiff_t in_stride,
32
                        /* */ pixel_t* out_img, ptrdiff_t out_stride)
33
2.72M
{
34
2.72M
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
2.72M
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
2.72M
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
2.72M
  if (SaoTypeIdx==0) {
41
1.70M
    return;
42
1.70M
  }
43
44
1.01M
  const seq_parameter_set* sps = &img->get_sps();
45
1.01M
  const pic_parameter_set* pps = &img->get_pps();
46
1.01M
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
1.01M
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
1.01M
  const int xC = xCtb*nSW;
51
1.01M
  const int yC = yCtb*nSH;
52
53
1.01M
  const int width  = img->get_width(cIdx);
54
1.01M
  const int height = img->get_height(cIdx);
55
56
1.01M
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
1.01M
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
1.01M
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
1.01M
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
1.01M
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
1.01M
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
6.08M
  for (int i=0;i<5;i++)
66
5.06M
    {
67
5.06M
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
5.06M
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
1.01M
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
1.01M
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
1.01M
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
1.01M
  if (SaoTypeIdx==2) {
79
850k
    int hPos[2], vPos[2];
80
850k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
850k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
850k
    switch (SaoEoClass) {
84
198k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
244k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
172k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
235k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
850k
    }
89
90
850k
    vPosStride[0] = vPos[0] * in_stride;
91
850k
    vPosStride[1] = vPos[1] * in_stride;
92
93
    /* Reorder sao_info.saoOffsetVal[] array, so that we can index it
94
       directly with the sum of the two pixel-difference signs. */
95
850k
    int8_t  saoOffsetVal[5]; // [2] unused
96
850k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
850k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
850k
    saoOffsetVal[2] = 0;
99
850k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
850k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
19.6M
    for (int j=0;j<ctbH;j++) {
104
18.8M
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
18.8M
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
465M
      for (int i=0;i<ctbW;i++) {
108
446M
        int edgeIdx = -1;
109
110
446M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
446M
        if ((extendedTests &&
113
10.6M
             (sps->pcm_loop_filter_disable_flag &&
114
940k
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
447M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
7.41M
          continue;
117
7.41M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
439M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
439M
        if (testBoundary)
123
207M
          for (int k=0;k<2;k++) {
124
139M
            int xS = xC+i+hPos[k];
125
139M
            int yS = yC+j+vPos[k];
126
127
139M
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
3.15M
              edgeIdx=0;
129
3.15M
              break;
130
3.15M
            }
131
132
133
            // This part seems inefficient with all the get_SliceHeaderIndex() calls,
134
            // but removing this part (because the input was known to have only a single
135
            // slice anyway) reduced computation time only by 1.3%.
136
            // TODO: however, this may still be a big part of SAO itself.
137
138
136M
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
136M
                                                                     yS<<chromashiftH);
140
136M
            if (sliceHeader==nullptr) { return; }
141
142
136M
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
136M
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
10.2k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
10.2k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
9.35k
              edgeIdx=0;
147
9.35k
              break;
148
9.35k
            }
149
150
136M
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
8.15k
                img->get_SliceHeader(xS<<chromashiftW,
152
8.15k
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
4.86k
              edgeIdx=0;
154
4.86k
              break;
155
4.86k
            }
156
157
158
136M
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
136M
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
136M
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
26.7k
              edgeIdx=0;
162
26.7k
              break;
163
26.7k
            }
164
136M
          }
165
166
439M
        if (edgeIdx != 0) {
167
168
438M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
438M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
438M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
435M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
435M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
435M
                               in_ptr[i] + offset);
176
435M
          }
177
438M
        }
178
439M
      }
179
18.8M
    }
180
850k
  }
181
162k
  else {
182
162k
    int bandShift = bitDepth-5;
183
162k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
162k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
162k
    int bandTable[32];
187
162k
    memset(bandTable, 0, sizeof(int)*32);
188
189
816k
    for (int k=0;k<4;k++) {
190
653k
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
653k
    }
192
193
194
    /* If PCM or transquant_bypass is used in this CTB, we have to
195
       run all checks (A).
196
       Otherwise, we run a simplified version of the code (B).
197
198
       NOTE: this whole part of SAO does not seem to be a significant part of the time spent
199
    */
200
201
162k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
330k
      for (int j=0;j<ctbH;j++)
206
14.2M
        for (int i=0;i<ctbW;i++) {
207
208
13.8M
          if ((sps->pcm_loop_filter_disable_flag &&
209
1.84M
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
13.7M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
10.2M
            continue;
212
10.2M
          }
213
214
          // Shifts are a strange thing. On x86, >>x actually computes >>(x%64).
215
          // But this should never happen, because the maximum bit-depth is 16.
216
3.67M
          int pixel = in_img[xC + i + (yC + j) * in_stride];
217
218
          // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
219
          // maybe when there was a decoding error and the pixels have not been filled in correctly.
220
          // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
221
3.67M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
3.67M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
3.67M
          if (bandIdx>0) {
226
356k
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
356k
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
356k
                     offset,
230
356k
                     in_img[xC+i+(yC+j)*in_stride],
231
356k
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
356k
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
356k
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
356k
          }
236
3.67M
        }
237
12.8k
    }
238
149k
    else
239
149k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
3.51M
        for (int j=0;j<ctbH;j++)
243
92.9M
          for (int i=0;i<ctbW;i++) {
244
245
89.6M
            int pixel = in_img[xC + i + (yC + j) * in_stride];
246
247
            // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
248
            // maybe when there was a decoding error and the pixels have not been filled in correctly.
249
            // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
250
89.6M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
89.6M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
89.6M
            if (bandIdx>0) {
255
33.9M
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
33.9M
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
33.9M
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
33.9M
            }
260
89.6M
          }
261
149k
      }
262
162k
  }
263
1.01M
}
void apply_sao_internal<unsigned short>(de265_image*, int, int, slice_segment_header const*, int, int, int, unsigned short const*, long, unsigned short*, long)
Line
Count
Source
33
48.6k
{
34
48.6k
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
48.6k
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
48.6k
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
48.6k
  if (SaoTypeIdx==0) {
41
28.2k
    return;
42
28.2k
  }
43
44
20.4k
  const seq_parameter_set* sps = &img->get_sps();
45
20.4k
  const pic_parameter_set* pps = &img->get_pps();
46
20.4k
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
20.4k
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
20.4k
  const int xC = xCtb*nSW;
51
20.4k
  const int yC = yCtb*nSH;
52
53
20.4k
  const int width  = img->get_width(cIdx);
54
20.4k
  const int height = img->get_height(cIdx);
55
56
20.4k
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
20.4k
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
20.4k
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
20.4k
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
20.4k
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
20.4k
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
122k
  for (int i=0;i<5;i++)
66
102k
    {
67
102k
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
102k
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
20.4k
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
20.4k
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
20.4k
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
20.4k
  if (SaoTypeIdx==2) {
79
8.09k
    int hPos[2], vPos[2];
80
8.09k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
8.09k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
8.09k
    switch (SaoEoClass) {
84
1.86k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
1.68k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
2.16k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
2.38k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
8.09k
    }
89
90
8.09k
    vPosStride[0] = vPos[0] * in_stride;
91
8.09k
    vPosStride[1] = vPos[1] * in_stride;
92
93
    /* Reorder sao_info.saoOffsetVal[] array, so that we can index it
94
       directly with the sum of the two pixel-difference signs. */
95
8.09k
    int8_t  saoOffsetVal[5]; // [2] unused
96
8.09k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
8.09k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
8.09k
    saoOffsetVal[2] = 0;
99
8.09k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
8.09k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
154k
    for (int j=0;j<ctbH;j++) {
104
146k
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
146k
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
5.47M
      for (int i=0;i<ctbW;i++) {
108
5.32M
        int edgeIdx = -1;
109
110
5.32M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
5.32M
        if ((extendedTests &&
113
3.09M
             (sps->pcm_loop_filter_disable_flag &&
114
374k
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
5.30M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
1.83M
          continue;
117
1.83M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
3.49M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
3.49M
        if (testBoundary)
123
905k
          for (int k=0;k<2;k++) {
124
675k
            int xS = xC+i+hPos[k];
125
675k
            int yS = yC+j+vPos[k];
126
127
675k
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
124k
              edgeIdx=0;
129
124k
              break;
130
124k
            }
131
132
133
            // This part seems inefficient with all the get_SliceHeaderIndex() calls,
134
            // but removing this part (because the input was known to have only a single
135
            // slice anyway) reduced computation time only by 1.3%.
136
            // TODO: however, this may still be a big part of SAO itself.
137
138
550k
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
550k
                                                                     yS<<chromashiftH);
140
550k
            if (sliceHeader==nullptr) { return; }
141
142
550k
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
550k
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
5.07k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
5.07k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
4.25k
              edgeIdx=0;
147
4.25k
              break;
148
4.25k
            }
149
150
546k
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
4.83k
                img->get_SliceHeader(xS<<chromashiftW,
152
4.83k
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
1.65k
              edgeIdx=0;
154
1.65k
              break;
155
1.65k
            }
156
157
158
544k
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
542k
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
542k
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
22.0k
              edgeIdx=0;
162
22.0k
              break;
163
22.0k
            }
164
544k
          }
165
166
3.49M
        if (edgeIdx != 0) {
167
168
3.33M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
3.33M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
3.33M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
3.33M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
3.33M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
3.33M
                               in_ptr[i] + offset);
176
3.33M
          }
177
3.33M
        }
178
3.49M
      }
179
146k
    }
180
8.09k
  }
181
12.3k
  else {
182
12.3k
    int bandShift = bitDepth-5;
183
12.3k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
12.3k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
12.3k
    int bandTable[32];
187
12.3k
    memset(bandTable, 0, sizeof(int)*32);
188
189
61.6k
    for (int k=0;k<4;k++) {
190
49.3k
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
49.3k
    }
192
193
194
    /* If PCM or transquant_bypass is used in this CTB, we have to
195
       run all checks (A).
196
       Otherwise, we run a simplified version of the code (B).
197
198
       NOTE: this whole part of SAO does not seem to be a significant part of the time spent
199
    */
200
201
12.3k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
91.6k
      for (int j=0;j<ctbH;j++)
206
3.61M
        for (int i=0;i<ctbW;i++) {
207
208
3.52M
          if ((sps->pcm_loop_filter_disable_flag &&
209
513k
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
3.48M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
2.43M
            continue;
212
2.43M
          }
213
214
          // Shifts are a strange thing. On x86, >>x actually computes >>(x%64).
215
          // But this should never happen, because the maximum bit-depth is 16.
216
1.08M
          int pixel = in_img[xC + i + (yC + j) * in_stride];
217
218
          // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
219
          // maybe when there was a decoding error and the pixels have not been filled in correctly.
220
          // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
221
1.08M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
1.08M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
1.08M
          if (bandIdx>0) {
226
95.0k
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
95.0k
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
95.0k
                     offset,
230
95.0k
                     in_img[xC+i+(yC+j)*in_stride],
231
95.0k
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
95.0k
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
95.0k
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
95.0k
          }
236
1.08M
        }
237
4.81k
    }
238
7.51k
    else
239
7.51k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
197k
        for (int j=0;j<ctbH;j++)
243
6.87M
          for (int i=0;i<ctbW;i++) {
244
245
6.68M
            int pixel = in_img[xC + i + (yC + j) * in_stride];
246
247
            // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
248
            // maybe when there was a decoding error and the pixels have not been filled in correctly.
249
            // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
250
6.68M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
6.68M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
6.68M
            if (bandIdx>0) {
255
769k
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
769k
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
769k
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
769k
            }
260
6.68M
          }
261
7.51k
      }
262
12.3k
  }
263
20.4k
}
void apply_sao_internal<unsigned char>(de265_image*, int, int, slice_segment_header const*, int, int, int, unsigned char const*, long, unsigned char*, long)
Line
Count
Source
33
2.67M
{
34
2.67M
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
2.67M
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
2.67M
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
2.67M
  if (SaoTypeIdx==0) {
41
1.67M
    return;
42
1.67M
  }
43
44
992k
  const seq_parameter_set* sps = &img->get_sps();
45
992k
  const pic_parameter_set* pps = &img->get_pps();
46
992k
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
992k
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
992k
  const int xC = xCtb*nSW;
51
992k
  const int yC = yCtb*nSH;
52
53
992k
  const int width  = img->get_width(cIdx);
54
992k
  const int height = img->get_height(cIdx);
55
56
992k
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
992k
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
992k
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
992k
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
992k
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
992k
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
5.96M
  for (int i=0;i<5;i++)
66
4.96M
    {
67
4.96M
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
4.96M
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
992k
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
992k
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
992k
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
992k
  if (SaoTypeIdx==2) {
79
842k
    int hPos[2], vPos[2];
80
842k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
842k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
842k
    switch (SaoEoClass) {
84
196k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
242k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
169k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
232k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
842k
    }
89
90
842k
    vPosStride[0] = vPos[0] * in_stride;
91
842k
    vPosStride[1] = vPos[1] * in_stride;
92
93
    /* Reorder sao_info.saoOffsetVal[] array, so that we can index it
94
       directly with the sum of the two pixel-difference signs. */
95
842k
    int8_t  saoOffsetVal[5]; // [2] unused
96
842k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
842k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
842k
    saoOffsetVal[2] = 0;
99
842k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
842k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
19.5M
    for (int j=0;j<ctbH;j++) {
104
18.6M
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
18.6M
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
460M
      for (int i=0;i<ctbW;i++) {
108
441M
        int edgeIdx = -1;
109
110
441M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
441M
        if ((extendedTests &&
113
7.55M
             (sps->pcm_loop_filter_disable_flag &&
114
566k
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
441M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
5.57M
          continue;
117
5.57M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
435M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
435M
        if (testBoundary)
123
206M
          for (int k=0;k<2;k++) {
124
138M
            int xS = xC+i+hPos[k];
125
138M
            int yS = yC+j+vPos[k];
126
127
138M
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
3.03M
              edgeIdx=0;
129
3.03M
              break;
130
3.03M
            }
131
132
133
            // This part seems inefficient with all the get_SliceHeaderIndex() calls,
134
            // but removing this part (because the input was known to have only a single
135
            // slice anyway) reduced computation time only by 1.3%.
136
            // TODO: however, this may still be a big part of SAO itself.
137
138
135M
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
135M
                                                                     yS<<chromashiftH);
140
135M
            if (sliceHeader==nullptr) { return; }
141
142
135M
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
135M
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
5.17k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
5.17k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
5.10k
              edgeIdx=0;
147
5.10k
              break;
148
5.10k
            }
149
150
135M
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
3.31k
                img->get_SliceHeader(xS<<chromashiftW,
152
3.31k
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
3.21k
              edgeIdx=0;
154
3.21k
              break;
155
3.21k
            }
156
157
158
135M
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
136M
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
136M
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
4.68k
              edgeIdx=0;
162
4.68k
              break;
163
4.68k
            }
164
135M
          }
165
166
435M
        if (edgeIdx != 0) {
167
168
435M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
435M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
435M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
432M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
432M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
432M
                               in_ptr[i] + offset);
176
432M
          }
177
435M
        }
178
435M
      }
179
18.6M
    }
180
842k
  }
181
150k
  else {
182
150k
    int bandShift = bitDepth-5;
183
150k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
150k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
150k
    int bandTable[32];
187
150k
    memset(bandTable, 0, sizeof(int)*32);
188
189
754k
    for (int k=0;k<4;k++) {
190
604k
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
604k
    }
192
193
194
    /* If PCM or transquant_bypass is used in this CTB, we have to
195
       run all checks (A).
196
       Otherwise, we run a simplified version of the code (B).
197
198
       NOTE: this whole part of SAO does not seem to be a significant part of the time spent
199
    */
200
201
150k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
238k
      for (int j=0;j<ctbH;j++)
206
10.5M
        for (int i=0;i<ctbW;i++) {
207
208
10.3M
          if ((sps->pcm_loop_filter_disable_flag &&
209
1.32M
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
10.2M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
7.77M
            continue;
212
7.77M
          }
213
214
          // Shifts are a strange thing. On x86, >>x actually computes >>(x%64).
215
          // But this should never happen, because the maximum bit-depth is 16.
216
2.58M
          int pixel = in_img[xC + i + (yC + j) * in_stride];
217
218
          // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
219
          // maybe when there was a decoding error and the pixels have not been filled in correctly.
220
          // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
221
2.58M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
2.58M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
2.58M
          if (bandIdx>0) {
226
261k
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
261k
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
261k
                     offset,
230
261k
                     in_img[xC+i+(yC+j)*in_stride],
231
261k
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
261k
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
261k
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
261k
          }
236
2.58M
        }
237
8.06k
    }
238
142k
    else
239
142k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
3.31M
        for (int j=0;j<ctbH;j++)
243
86.1M
          for (int i=0;i<ctbW;i++) {
244
245
82.9M
            int pixel = in_img[xC + i + (yC + j) * in_stride];
246
247
            // Note: the input pixel value should never exceed the valid range, but it seems that it still does,
248
            // maybe when there was a decoding error and the pixels have not been filled in correctly.
249
            // Thus, we have to limit the pixel range to ensure that we have no illegal table access.
250
82.9M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
82.9M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
82.9M
            if (bandIdx>0) {
255
33.1M
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
33.1M
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
33.1M
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
33.1M
            }
260
82.9M
          }
261
142k
      }
262
150k
  }
263
992k
}
264
265
266
template <class pixel_t>
267
void apply_sao(de265_image* img, int xCtb,int yCtb,
268
               const slice_segment_header* shdr, int cIdx, int nSW,int nSH,
269
               const pixel_t* in_img,  ptrdiff_t in_stride,
270
               /* */ pixel_t* out_img, ptrdiff_t out_stride)
271
2.72M
{
272
2.72M
  if (img->high_bit_depth(cIdx)) {
273
48.6k
    apply_sao_internal<uint16_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH,
274
48.6k
                                 reinterpret_cast<const uint16_t*>(in_img), in_stride,
275
48.6k
                                 reinterpret_cast<uint16_t*>(out_img),out_stride);
276
48.6k
  }
277
2.67M
  else {
278
2.67M
    apply_sao_internal<uint8_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH,
279
2.67M
                                in_img, in_stride,
280
2.67M
                                out_img,out_stride);
281
2.67M
  }
282
2.72M
}
283
284
285
void apply_sample_adaptive_offset(de265_image* img)
286
0
{
287
0
  const seq_parameter_set& sps = img->get_sps();
288
289
0
  if (sps.sample_adaptive_offset_enabled_flag==0) {
290
0
    return;
291
0
  }
292
293
0
  de265_image inputCopy;
294
0
  de265_error err = inputCopy.copy_image(img);
295
0
  if (err != DE265_OK) {
296
0
    img->decctx->add_warning(DE265_WARNING_CANNOT_APPLY_SAO_OUT_OF_MEMORY,false);
297
0
    return;
298
0
  }
299
300
0
  for (int yCtb=0; yCtb<sps.PicHeightInCtbsY; yCtb++)
301
0
    for (int xCtb=0; xCtb<sps.PicWidthInCtbsY; xCtb++)
302
0
      {
303
0
        const slice_segment_header* shdr = img->get_SliceHeaderCtb(xCtb,yCtb);
304
305
0
        if (shdr->slice_sao_luma_flag) {
306
0
          apply_sao(img, xCtb,yCtb, shdr, 0, 1<<sps.Log2CtbSizeY, 1<<sps.Log2CtbSizeY,
307
0
                    inputCopy.get_image_plane(0), inputCopy.get_image_stride(0),
308
0
                    img->get_image_plane(0), img->get_image_stride(0));
309
0
        }
310
311
0
        if (shdr->slice_sao_chroma_flag) {
312
0
          int nSW = (1<<sps.Log2CtbSizeY) / sps.SubWidthC;
313
0
          int nSH = (1<<sps.Log2CtbSizeY) / sps.SubHeightC;
314
315
0
          apply_sao(img, xCtb,yCtb, shdr, 1, nSW,nSH,
316
0
                    inputCopy.get_image_plane(1), inputCopy.get_image_stride(1),
317
0
                    img->get_image_plane(1), img->get_image_stride(1));
318
319
0
          apply_sao(img, xCtb,yCtb, shdr, 2, nSW,nSH,
320
0
                    inputCopy.get_image_plane(2), inputCopy.get_image_stride(2),
321
0
                    img->get_image_plane(2), img->get_image_stride(2));
322
0
        }
323
0
      }
324
0
}
325
326
327
void apply_sample_adaptive_offset_sequential(de265_image* img)
328
0
{
329
0
  const seq_parameter_set& sps = img->get_sps();
330
331
0
  if (sps.sample_adaptive_offset_enabled_flag==0) {
332
0
    return;
333
0
  }
334
335
0
  size_t lumaImageSize   = static_cast<size_t>(img->get_image_stride(0)) * img->get_height(0) * img->get_bytes_per_pixel(0);
336
0
  size_t chromaImageSize = static_cast<size_t>(img->get_image_stride(1)) * img->get_height(1) * img->get_bytes_per_pixel(1);
337
338
0
  uint8_t* inputCopy = new uint8_t[ std::max(lumaImageSize, chromaImageSize) ];
339
0
  if (inputCopy == nullptr) {
340
0
    img->decctx->add_warning(DE265_WARNING_CANNOT_APPLY_SAO_OUT_OF_MEMORY,false);
341
0
    return;
342
0
  }
343
344
345
0
  int nChannels = 3;
346
0
  if (sps.ChromaArrayType == CHROMA_MONO) { nChannels=1; }
347
348
0
  for (int cIdx=0;cIdx<nChannels;cIdx++) {
349
350
0
    ptrdiff_t stride = img->get_image_stride(cIdx);
351
0
    int height = img->get_height(cIdx);
352
353
0
    memcpy(inputCopy, img->get_image_plane(cIdx), static_cast<size_t>(stride) * height * img->get_bytes_per_pixel(cIdx));
354
355
0
    for (int yCtb=0; yCtb<sps.PicHeightInCtbsY; yCtb++)
356
0
      for (int xCtb=0; xCtb<sps.PicWidthInCtbsY; xCtb++)
357
0
        {
358
0
          const slice_segment_header* shdr = img->get_SliceHeaderCtb(xCtb,yCtb);
359
0
          if (shdr==nullptr) {
360
0
      delete[] inputCopy;
361
0
      return;
362
0
    }
363
364
0
          if (cIdx==0 && shdr->slice_sao_luma_flag) {
365
0
            apply_sao(img, xCtb,yCtb, shdr, 0, 1<<sps.Log2CtbSizeY, 1<<sps.Log2CtbSizeY,
366
0
                      inputCopy, stride,
367
0
                      img->get_image_plane(0), img->get_image_stride(0));
368
0
          }
369
370
0
          if (cIdx!=0 && shdr->slice_sao_chroma_flag) {
371
0
            int nSW = (1<<sps.Log2CtbSizeY) / sps.SubWidthC;
372
0
            int nSH = (1<<sps.Log2CtbSizeY) / sps.SubHeightC;
373
374
0
            apply_sao(img, xCtb,yCtb, shdr, cIdx, nSW,nSH,
375
0
                      inputCopy, stride,
376
0
                      img->get_image_plane(cIdx), img->get_image_stride(cIdx));
377
0
          }
378
0
        }
379
0
  }
380
381
0
  delete[] inputCopy;
382
0
}
383
384
385
386
387
class thread_task_sao : public thread_task
388
{
389
public:
390
  int  ctb_y;
391
  de265_image* img; /* this is where we get the SPS from
392
                       (either inputImg or outputImg can be a dummy image)
393
                    */
394
395
  de265_image* inputImg;
396
  de265_image* outputImg;
397
  int inputProgress;
398
399
  virtual void work();
400
0
  virtual std::string name() const {
401
0
    char buf[100];
402
0
    sprintf(buf,"sao-%d",ctb_y);
403
0
    return buf;
404
0
  }
405
};
406
407
408
void thread_task_sao::work()
409
66.5k
{
410
66.5k
  state = Running;
411
66.5k
  img->thread_run(this);
412
413
66.5k
  const seq_parameter_set& sps = img->get_sps();
414
415
66.5k
  const int rightCtb = sps.PicWidthInCtbsY-1;
416
66.5k
  const int ctbSize  = (1<<sps.Log2CtbSizeY);
417
418
419
  // wait until also the CTB-rows below and above are ready
420
421
66.5k
  img->wait_for_progress(this, rightCtb,ctb_y,  inputProgress);
422
423
66.5k
  if (ctb_y>0) {
424
60.1k
    img->wait_for_progress(this, rightCtb,ctb_y-1, inputProgress);
425
60.1k
  }
426
427
66.5k
  if (ctb_y+1<sps.PicHeightInCtbsY) {
428
60.1k
    img->wait_for_progress(this, rightCtb,ctb_y+1, inputProgress);
429
60.1k
  }
430
431
432
  // copy input image to output for this CTB-row
433
434
66.5k
  outputImg->copy_lines_from(inputImg, ctb_y * ctbSize, (ctb_y+1) * ctbSize);
435
436
437
  // process SAO in the CTB-row
438
439
1.05M
  for (int xCtb=0; xCtb<sps.PicWidthInCtbsY; xCtb++)
440
989k
    {
441
989k
      const slice_segment_header* shdr = img->get_SliceHeaderCtb(xCtb,ctb_y);
442
989k
      if (shdr==nullptr) {
443
43
        break;
444
43
      }
445
446
989k
      if (shdr->slice_sao_luma_flag) {
447
924k
        apply_sao(img, xCtb,ctb_y, shdr, 0, ctbSize, ctbSize,
448
924k
                  inputImg ->get_image_plane(0), inputImg ->get_image_stride(0),
449
924k
                  outputImg->get_image_plane(0), outputImg->get_image_stride(0));
450
924k
      }
451
452
989k
      if (shdr->slice_sao_chroma_flag) {
453
898k
        int nSW = ctbSize / sps.SubWidthC;
454
898k
        int nSH = ctbSize / sps.SubHeightC;
455
456
898k
        apply_sao(img, xCtb,ctb_y, shdr, 1, nSW,nSH,
457
898k
                  inputImg ->get_image_plane(1), inputImg ->get_image_stride(1),
458
898k
                  outputImg->get_image_plane(1), outputImg->get_image_stride(1));
459
460
898k
        apply_sao(img, xCtb,ctb_y, shdr, 2, nSW,nSH,
461
898k
                  inputImg ->get_image_plane(2), inputImg ->get_image_stride(2),
462
898k
                  outputImg->get_image_plane(2), outputImg->get_image_stride(2));
463
898k
      }
464
989k
    }
465
466
467
  // mark SAO progress
468
469
1.05M
  for (int x=0;x<=rightCtb;x++) {
470
989k
    const int CtbWidth = sps.PicWidthInCtbsY;
471
989k
    img->ctb_progress[x+ctb_y*CtbWidth].set_progress(CTB_PROGRESS_SAO);
472
989k
  }
473
474
475
66.5k
  state = Finished;
476
66.5k
  img->thread_finishes(this);
477
66.5k
}
478
479
480
bool add_sao_tasks(image_unit* imgunit, int saoInputProgress)
481
7.34k
{
482
7.34k
  de265_image* img = imgunit->img;
483
7.34k
  const seq_parameter_set& sps = img->get_sps();
484
485
7.34k
  if (sps.sample_adaptive_offset_enabled_flag==0) {
486
913
    return false;
487
913
  }
488
489
490
6.43k
  decoder_context* ctx = img->decctx;
491
492
6.43k
  de265_error err = imgunit->sao_output.alloc_image(img->get_width(), img->get_height(),
493
6.43k
                                                    img->get_chroma_format(),
494
6.43k
                                                    img->get_shared_sps(),
495
6.43k
                                                    false,
496
6.43k
                                                    img->decctx, //img->encctx,
497
6.43k
                                                    img->pts, img->user_data, true);
498
6.43k
  if (err != DE265_OK) {
499
0
    img->decctx->add_warning(DE265_WARNING_CANNOT_APPLY_SAO_OUT_OF_MEMORY,false);
500
0
    return false;
501
0
  }
502
503
6.43k
  int nRows = sps.PicHeightInCtbsY;
504
505
6.43k
  img->thread_start(nRows);
506
507
73.0k
  for (int y=0;y<nRows;y++)
508
66.5k
    {
509
66.5k
      thread_task_sao* task = new thread_task_sao;
510
511
66.5k
      task->inputImg  = img;
512
66.5k
      task->outputImg = &imgunit->sao_output;
513
66.5k
      task->img = img;
514
66.5k
      task->ctb_y = y;
515
66.5k
      task->inputProgress = saoInputProgress;
516
517
66.5k
      imgunit->tasks.push_back(task);
518
66.5k
      ctx->thread_pool_.add_task(task);
519
66.5k
    }
520
521
  /* Currently need barrier here because when are finished, we have to swap the pixel
522
     data back into the main image. */
523
6.43k
  img->wait_for_completion();
524
525
6.43k
  img->exchange_pixel_data_with(imgunit->sao_output);
526
527
6.43k
  return true;
528
6.43k
}