Coverage Report

Created: 2026-07-25 07:06

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
1.72M
{
34
1.72M
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
1.72M
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
1.72M
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
1.72M
  if (SaoTypeIdx==0) {
41
989k
    return;
42
989k
  }
43
44
740k
  const seq_parameter_set* sps = &img->get_sps();
45
740k
  const pic_parameter_set* pps = &img->get_pps();
46
740k
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
740k
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
740k
  const int xC = xCtb*nSW;
51
740k
  const int yC = yCtb*nSH;
52
53
740k
  const int width  = img->get_width(cIdx);
54
740k
  const int height = img->get_height(cIdx);
55
56
740k
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
740k
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
740k
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
740k
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
740k
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
740k
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
4.44M
  for (int i=0;i<5;i++)
66
3.70M
    {
67
3.70M
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
3.70M
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
740k
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
740k
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
740k
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
740k
  if (SaoTypeIdx==2) {
79
340k
    int hPos[2], vPos[2];
80
340k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
340k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
340k
    switch (SaoEoClass) {
84
73.3k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
101k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
82.0k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
83.0k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
340k
    }
89
90
340k
    vPosStride[0] = vPos[0] * in_stride;
91
340k
    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
340k
    int8_t  saoOffsetVal[5]; // [2] unused
96
340k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
340k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
340k
    saoOffsetVal[2] = 0;
99
340k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
340k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
15.0M
    for (int j=0;j<ctbH;j++) {
104
14.7M
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
14.7M
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
726M
      for (int i=0;i<ctbW;i++) {
108
711M
        int edgeIdx = -1;
109
110
711M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
711M
        if ((extendedTests &&
113
33.1M
             (sps->pcm_loop_filter_disable_flag &&
114
1.41M
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
711M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
17.1M
          continue;
117
17.1M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
694M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
694M
        if (testBoundary)
123
165M
          for (int k=0;k<2;k++) {
124
111M
            int xS = xC+i+hPos[k];
125
111M
            int yS = yC+j+vPos[k];
126
127
111M
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
3.60M
              edgeIdx=0;
129
3.60M
              break;
130
3.60M
            }
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
108M
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
108M
                                                                     yS<<chromashiftH);
140
108M
            if (sliceHeader==nullptr) { return; }
141
142
108M
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
108M
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
8.12k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
8.12k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
6.49k
              edgeIdx=0;
147
6.49k
              break;
148
6.49k
            }
149
150
108M
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
2.56k
                img->get_SliceHeader(xS<<chromashiftW,
152
2.56k
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
1.86k
              edgeIdx=0;
154
1.86k
              break;
155
1.86k
            }
156
157
158
108M
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
108M
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
108M
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
3.67k
              edgeIdx=0;
162
3.67k
              break;
163
3.67k
            }
164
108M
          }
165
166
694M
        if (edgeIdx != 0) {
167
168
693M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
693M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
693M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
690M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
690M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
690M
                               in_ptr[i] + offset);
176
690M
          }
177
693M
        }
178
694M
      }
179
14.7M
    }
180
340k
  }
181
399k
  else {
182
399k
    int bandShift = bitDepth-5;
183
399k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
399k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
399k
    int bandTable[32];
187
399k
    memset(bandTable, 0, sizeof(int)*32);
188
189
1.99M
    for (int k=0;k<4;k++) {
190
1.59M
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
1.59M
    }
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
399k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
928k
      for (int j=0;j<ctbH;j++)
206
43.8M
        for (int i=0;i<ctbW;i++) {
207
208
42.9M
          if ((sps->pcm_loop_filter_disable_flag &&
209
2.15M
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
42.9M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
27.2M
            continue;
212
27.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
15.7M
          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
15.7M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
15.7M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
15.7M
          if (bandIdx>0) {
226
1.85M
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
1.85M
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
1.85M
                     offset,
230
1.85M
                     in_img[xC+i+(yC+j)*in_stride],
231
1.85M
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
1.85M
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
1.85M
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
1.85M
          }
236
15.7M
        }
237
31.5k
    }
238
368k
    else
239
368k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
14.3M
        for (int j=0;j<ctbH;j++)
243
640M
          for (int i=0;i<ctbW;i++) {
244
245
626M
            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
626M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
626M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
626M
            if (bandIdx>0) {
255
78.0M
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
78.0M
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
78.0M
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
78.0M
            }
260
626M
          }
261
368k
      }
262
399k
  }
263
740k
}
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
178k
{
34
178k
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
178k
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
178k
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
178k
  if (SaoTypeIdx==0) {
41
104k
    return;
42
104k
  }
43
44
73.9k
  const seq_parameter_set* sps = &img->get_sps();
45
73.9k
  const pic_parameter_set* pps = &img->get_pps();
46
73.9k
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
73.9k
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
73.9k
  const int xC = xCtb*nSW;
51
73.9k
  const int yC = yCtb*nSH;
52
53
73.9k
  const int width  = img->get_width(cIdx);
54
73.9k
  const int height = img->get_height(cIdx);
55
56
73.9k
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
73.9k
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
73.9k
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
73.9k
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
73.9k
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
73.9k
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
443k
  for (int i=0;i<5;i++)
66
369k
    {
67
369k
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
369k
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
73.9k
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
73.9k
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
73.9k
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
73.9k
  if (SaoTypeIdx==2) {
79
11.9k
    int hPos[2], vPos[2];
80
11.9k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
11.9k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
11.9k
    switch (SaoEoClass) {
84
2.52k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
2.31k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
4.23k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
2.88k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
11.9k
    }
89
90
11.9k
    vPosStride[0] = vPos[0] * in_stride;
91
11.9k
    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
11.9k
    int8_t  saoOffsetVal[5]; // [2] unused
96
11.9k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
11.9k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
11.9k
    saoOffsetVal[2] = 0;
99
11.9k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
11.9k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
348k
    for (int j=0;j<ctbH;j++) {
104
336k
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
336k
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
15.9M
      for (int i=0;i<ctbW;i++) {
108
15.6M
        int edgeIdx = -1;
109
110
15.6M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
15.6M
        if ((extendedTests &&
113
9.47M
             (sps->pcm_loop_filter_disable_flag &&
114
406k
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
15.6M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
4.87M
          continue;
117
4.87M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
10.7M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
10.7M
        if (testBoundary)
123
2.21M
          for (int k=0;k<2;k++) {
124
1.59M
            int xS = xC+i+hPos[k];
125
1.59M
            int yS = yC+j+vPos[k];
126
127
1.59M
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
246k
              edgeIdx=0;
129
246k
              break;
130
246k
            }
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
1.35M
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
1.35M
                                                                     yS<<chromashiftH);
140
1.35M
            if (sliceHeader==nullptr) { return; }
141
142
1.35M
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
1.35M
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
2.20k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
2.20k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
1.94k
              edgeIdx=0;
147
1.94k
              break;
148
1.94k
            }
149
150
1.35M
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
637
                img->get_SliceHeader(xS<<chromashiftW,
152
637
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
377
              edgeIdx=0;
154
377
              break;
155
377
            }
156
157
158
1.34M
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
1.34M
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
1.34M
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
223
              edgeIdx=0;
162
223
              break;
163
223
            }
164
1.34M
          }
165
166
10.7M
        if (edgeIdx != 0) {
167
168
10.5M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
10.5M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
10.5M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
10.5M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
10.5M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
10.5M
                               in_ptr[i] + offset);
176
10.5M
          }
177
10.5M
        }
178
10.7M
      }
179
336k
    }
180
11.9k
  }
181
62.0k
  else {
182
62.0k
    int bandShift = bitDepth-5;
183
62.0k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
62.0k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
62.0k
    int bandTable[32];
187
62.0k
    memset(bandTable, 0, sizeof(int)*32);
188
189
310k
    for (int k=0;k<4;k++) {
190
248k
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
248k
    }
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
62.0k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
352k
      for (int j=0;j<ctbH;j++)
206
15.2M
        for (int i=0;i<ctbW;i++) {
207
208
14.9M
          if ((sps->pcm_loop_filter_disable_flag &&
209
1.15M
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
14.9M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
8.71M
            continue;
212
8.71M
          }
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
6.23M
          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
6.23M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
6.23M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
6.23M
          if (bandIdx>0) {
226
625k
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
625k
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
625k
                     offset,
230
625k
                     in_img[xC+i+(yC+j)*in_stride],
231
625k
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
625k
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
625k
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
625k
          }
236
6.23M
        }
237
14.0k
    }
238
47.9k
    else
239
47.9k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
1.83M
        for (int j=0;j<ctbH;j++)
243
73.3M
          for (int i=0;i<ctbW;i++) {
244
245
71.5M
            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
71.5M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
71.5M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
71.5M
            if (bandIdx>0) {
255
10.5M
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
10.5M
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
10.5M
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
10.5M
            }
260
71.5M
          }
261
47.9k
      }
262
62.0k
  }
263
73.9k
}
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
1.55M
{
34
1.55M
  const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb);
35
36
1.55M
  int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3;
37
38
1.55M
  logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH);
39
40
1.55M
  if (SaoTypeIdx==0) {
41
885k
    return;
42
885k
  }
43
44
666k
  const seq_parameter_set* sps = &img->get_sps();
45
666k
  const pic_parameter_set* pps = &img->get_pps();
46
666k
  const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C);
47
666k
  const int maxPixelValue = (1<<bitDepth)-1;
48
49
  // top left position of CTB in pixels
50
666k
  const int xC = xCtb*nSW;
51
666k
  const int yC = yCtb*nSH;
52
53
666k
  const int width  = img->get_width(cIdx);
54
666k
  const int height = img->get_height(cIdx);
55
56
666k
  const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS;
57
58
666k
  const int picWidthInCtbs = sps->PicWidthInCtbsY;
59
666k
  const int chromashiftW = sps->get_chroma_shift_W(cIdx);
60
666k
  const int chromashiftH = sps->get_chroma_shift_H(cIdx);
61
666k
  const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW;
62
666k
  const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH;
63
64
65
3.99M
  for (int i=0;i<5;i++)
66
3.33M
    {
67
3.33M
      logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]);
68
3.33M
    }
69
70
71
  // actual size of CTB to be processed (can be smaller when partially outside of image)
72
666k
  const int ctbW = (xC+nSW>width)  ? width -xC : nSW;
73
666k
  const int ctbH = (yC+nSH>height) ? height-yC : nSH;
74
75
76
666k
  const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb);
77
78
666k
  if (SaoTypeIdx==2) {
79
328k
    int hPos[2], vPos[2];
80
328k
    ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride
81
328k
    int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3;
82
83
328k
    switch (SaoEoClass) {
84
70.7k
    case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break;
85
99.5k
    case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break;
86
77.8k
    case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break;
87
80.1k
    case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break;
88
328k
    }
89
90
328k
    vPosStride[0] = vPos[0] * in_stride;
91
328k
    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
328k
    int8_t  saoOffsetVal[5]; // [2] unused
96
328k
    saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1];
97
328k
    saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1];
98
328k
    saoOffsetVal[2] = 0;
99
328k
    saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1];
100
328k
    saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1];
101
102
103
14.7M
    for (int j=0;j<ctbH;j++) {
104
14.4M
      const pixel_t* in_ptr  = &in_img [xC+(yC+j)*in_stride];
105
14.4M
      /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride];
106
107
710M
      for (int i=0;i<ctbW;i++) {
108
695M
        int edgeIdx = -1;
109
110
695M
        logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j);
111
112
695M
        if ((extendedTests &&
113
23.6M
             (sps->pcm_loop_filter_disable_flag &&
114
1.00M
              img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) ||
115
696M
            img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
116
12.3M
          continue;
117
12.3M
        }
118
119
        // do the expensive test for boundaries only at the boundaries
120
683M
        bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1);
121
122
683M
        if (testBoundary)
123
163M
          for (int k=0;k<2;k++) {
124
110M
            int xS = xC+i+hPos[k];
125
110M
            int yS = yC+j+vPos[k];
126
127
110M
            if (xS<0 || yS<0 || xS>=width || yS>=height) {
128
3.35M
              edgeIdx=0;
129
3.35M
              break;
130
3.35M
            }
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
106M
            slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW,
139
106M
                                                                     yS<<chromashiftH);
140
106M
            if (sliceHeader==nullptr) { return; }
141
142
106M
            int sliceAddrRS = sliceHeader->SliceAddrRS;
143
106M
            if (sliceAddrRS <  ctbSliceAddrRS &&
144
5.91k
                img->get_SliceHeader((xC+i)<<chromashiftW,
145
5.91k
                                     (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
146
4.54k
              edgeIdx=0;
147
4.54k
              break;
148
4.54k
            }
149
150
106M
            if (sliceAddrRS >  ctbSliceAddrRS &&
151
1.92k
                img->get_SliceHeader(xS<<chromashiftW,
152
1.92k
                                     yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) {
153
1.48k
              edgeIdx=0;
154
1.48k
              break;
155
1.48k
            }
156
157
158
106M
            if (pps->loop_filter_across_tiles_enabled_flag==0 &&
159
106M
                pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] !=
160
106M
                pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) {
161
3.45k
              edgeIdx=0;
162
3.45k
              break;
163
3.45k
            }
164
106M
          }
165
166
683M
        if (edgeIdx != 0) {
167
168
682M
          edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) +
169
682M
                      Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]])   );
170
171
682M
          if (1) { // edgeIdx != 0) {   // seems to be faster without this check (zero in offset table)
172
679M
            int offset = saoOffsetVal[edgeIdx+2];
173
174
679M
            out_ptr[i] = Clip3(0,maxPixelValue,
175
679M
                               in_ptr[i] + offset);
176
679M
          }
177
682M
        }
178
683M
      }
179
14.4M
    }
180
328k
  }
181
337k
  else {
182
337k
    int bandShift = bitDepth-5;
183
337k
    int saoLeftClass = saoinfo->sao_band_position[cIdx];
184
337k
    logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass);
185
186
337k
    int bandTable[32];
187
337k
    memset(bandTable, 0, sizeof(int)*32);
188
189
1.68M
    for (int k=0;k<4;k++) {
190
1.35M
      bandTable[ (k+saoLeftClass)&31 ] = k+1;
191
1.35M
    }
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
337k
    if (extendedTests) {
202
203
      // (A) full version with all checks
204
205
575k
      for (int j=0;j<ctbH;j++)
206
28.6M
        for (int i=0;i<ctbW;i++) {
207
208
28.0M
          if ((sps->pcm_loop_filter_disable_flag &&
209
997k
               img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) ||
210
28.0M
              img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) {
211
18.4M
            continue;
212
18.4M
          }
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
9.54M
          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
9.54M
          pixel = Clip3(0, maxPixelValue, pixel);
222
223
9.54M
          int bandIdx = bandTable[pixel >> bandShift];
224
225
9.54M
          if (bandIdx>0) {
226
1.22M
            int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
227
228
1.22M
            logtrace(LogSAO,"%d %d (%d) offset %d  %x -> %x\n",xC+i,yC+j,bandIdx,
229
1.22M
                     offset,
230
1.22M
                     in_img[xC+i+(yC+j)*in_stride],
231
1.22M
                     in_img[xC+i+(yC+j)*in_stride]+offset);
232
233
1.22M
            out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
234
1.22M
                                                    in_img[xC+i+(yC+j)*in_stride] + offset);
235
1.22M
          }
236
9.54M
        }
237
17.4k
    }
238
320k
    else
239
320k
      {
240
        // (B) simplified version (only works if no PCM and transquant_bypass is active)
241
242
12.5M
        for (int j=0;j<ctbH;j++)
243
566M
          for (int i=0;i<ctbW;i++) {
244
245
554M
            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
554M
            pixel = Clip3(0, maxPixelValue, pixel);
251
252
554M
            int bandIdx = bandTable[pixel >> bandShift];
253
254
554M
            if (bandIdx>0) {
255
67.4M
              int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1];
256
257
67.4M
              out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue,
258
67.4M
                                                      in_img[xC+i+(yC+j)*in_stride] + offset);
259
67.4M
            }
260
554M
          }
261
320k
      }
262
337k
  }
263
666k
}
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
1.72M
{
272
1.72M
  if (img->high_bit_depth(cIdx)) {
273
178k
    apply_sao_internal<uint16_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH,
274
178k
                                 reinterpret_cast<const uint16_t*>(in_img), in_stride,
275
178k
                                 reinterpret_cast<uint16_t*>(out_img),out_stride);
276
178k
  }
277
1.55M
  else {
278
1.55M
    apply_sao_internal<uint8_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH,
279
1.55M
                                in_img, in_stride,
280
1.55M
                                out_img,out_stride);
281
1.55M
  }
282
1.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
51.2k
{
410
51.2k
  state = Running;
411
51.2k
  img->thread_run(this);
412
413
51.2k
  const seq_parameter_set& sps = img->get_sps();
414
415
51.2k
  const int rightCtb = sps.PicWidthInCtbsY-1;
416
51.2k
  const int ctbSize  = (1<<sps.Log2CtbSizeY);
417
418
419
  // wait until also the CTB-rows below and above are ready
420
421
51.2k
  img->wait_for_progress(this, rightCtb,ctb_y,  inputProgress);
422
423
51.2k
  if (ctb_y>0) {
424
40.2k
    img->wait_for_progress(this, rightCtb,ctb_y-1, inputProgress);
425
40.2k
  }
426
427
51.2k
  if (ctb_y+1<sps.PicHeightInCtbsY) {
428
40.2k
    img->wait_for_progress(this, rightCtb,ctb_y+1, inputProgress);
429
40.2k
  }
430
431
432
  // copy input image to output for this CTB-row
433
434
51.2k
  outputImg->copy_lines_from(inputImg, ctb_y * ctbSize, (ctb_y+1) * ctbSize);
435
436
437
  // process SAO in the CTB-row
438
439
685k
  for (int xCtb=0; xCtb<sps.PicWidthInCtbsY; xCtb++)
440
634k
    {
441
634k
      const slice_segment_header* shdr = img->get_SliceHeaderCtb(xCtb,ctb_y);
442
634k
      if (shdr==nullptr) {
443
4
        break;
444
4
      }
445
446
634k
      if (shdr->slice_sao_luma_flag) {
447
611k
        apply_sao(img, xCtb,ctb_y, shdr, 0, ctbSize, ctbSize,
448
611k
                  inputImg ->get_image_plane(0), inputImg ->get_image_stride(0),
449
611k
                  outputImg->get_image_plane(0), outputImg->get_image_stride(0));
450
611k
      }
451
452
634k
      if (shdr->slice_sao_chroma_flag) {
453
558k
        int nSW = ctbSize / sps.SubWidthC;
454
558k
        int nSH = ctbSize / sps.SubHeightC;
455
456
558k
        apply_sao(img, xCtb,ctb_y, shdr, 1, nSW,nSH,
457
558k
                  inputImg ->get_image_plane(1), inputImg ->get_image_stride(1),
458
558k
                  outputImg->get_image_plane(1), outputImg->get_image_stride(1));
459
460
558k
        apply_sao(img, xCtb,ctb_y, shdr, 2, nSW,nSH,
461
558k
                  inputImg ->get_image_plane(2), inputImg ->get_image_stride(2),
462
558k
                  outputImg->get_image_plane(2), outputImg->get_image_stride(2));
463
558k
      }
464
634k
    }
465
466
467
  // mark SAO progress
468
469
685k
  for (int x=0;x<=rightCtb;x++) {
470
634k
    const int CtbWidth = sps.PicWidthInCtbsY;
471
634k
    img->ctb_progress[x+ctb_y*CtbWidth].set_progress(CTB_PROGRESS_SAO);
472
634k
  }
473
474
475
51.2k
  state = Finished;
476
51.2k
  img->thread_finishes(this);
477
51.2k
}
478
479
480
bool add_sao_tasks(image_unit* imgunit, int saoInputProgress)
481
12.1k
{
482
12.1k
  de265_image* img = imgunit->img;
483
12.1k
  const seq_parameter_set& sps = img->get_sps();
484
485
12.1k
  if (sps.sample_adaptive_offset_enabled_flag==0) {
486
1.15k
    return false;
487
1.15k
  }
488
489
490
10.9k
  decoder_context* ctx = img->decctx;
491
492
10.9k
  de265_error err = imgunit->sao_output.alloc_image(img->get_width(), img->get_height(),
493
10.9k
                                                    img->get_chroma_format(),
494
10.9k
                                                    img->get_shared_sps(),
495
10.9k
                                                    false,
496
10.9k
                                                    img->decctx, //img->encctx,
497
10.9k
                                                    img->pts, img->user_data, true);
498
10.9k
  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
10.9k
  int nRows = sps.PicHeightInCtbsY;
504
505
10.9k
  img->thread_start(nRows);
506
507
62.2k
  for (int y=0;y<nRows;y++)
508
51.2k
    {
509
51.2k
      thread_task_sao* task = new thread_task_sao;
510
511
51.2k
      task->inputImg  = img;
512
51.2k
      task->outputImg = &imgunit->sao_output;
513
51.2k
      task->img = img;
514
51.2k
      task->ctb_y = y;
515
51.2k
      task->inputProgress = saoInputProgress;
516
517
51.2k
      imgunit->tasks.push_back(task);
518
51.2k
      ctx->thread_pool_.add_task(task);
519
51.2k
    }
520
521
  /* Currently need barrier here because when are finished, we have to swap the pixel
522
     data back into the main image. */
523
10.9k
  img->wait_for_completion();
524
525
10.9k
  img->exchange_pixel_data_with(imgunit->sao_output);
526
527
10.9k
  return true;
528
10.9k
}