/work/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 | 3.54M | { |
34 | 3.54M | const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb); |
35 | | |
36 | 3.54M | int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3; |
37 | | |
38 | 3.54M | logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH); |
39 | | |
40 | 3.54M | if (SaoTypeIdx==0) { |
41 | 2.09M | return; |
42 | 2.09M | } |
43 | | |
44 | 1.44M | const seq_parameter_set* sps = &img->get_sps(); |
45 | 1.44M | const pic_parameter_set* pps = &img->get_pps(); |
46 | 1.44M | const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C); |
47 | 1.44M | const int maxPixelValue = (1<<bitDepth)-1; |
48 | | |
49 | | // top left position of CTB in pixels |
50 | 1.44M | const int xC = xCtb*nSW; |
51 | 1.44M | const int yC = yCtb*nSH; |
52 | | |
53 | 1.44M | const int width = img->get_width(cIdx); |
54 | 1.44M | const int height = img->get_height(cIdx); |
55 | | |
56 | 1.44M | const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS; |
57 | | |
58 | 1.44M | const int picWidthInCtbs = sps->PicWidthInCtbsY; |
59 | 1.44M | const int chromashiftW = sps->get_chroma_shift_W(cIdx); |
60 | 1.44M | const int chromashiftH = sps->get_chroma_shift_H(cIdx); |
61 | 1.44M | const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW; |
62 | 1.44M | const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH; |
63 | | |
64 | | |
65 | 8.67M | for (int i=0;i<5;i++) |
66 | 7.23M | { |
67 | 7.23M | logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]); |
68 | 7.23M | } |
69 | | |
70 | | |
71 | | // actual size of CTB to be processed (can be smaller when partially outside of image) |
72 | 1.44M | const int ctbW = (xC+nSW>width) ? width -xC : nSW; |
73 | 1.44M | const int ctbH = (yC+nSH>height) ? height-yC : nSH; |
74 | | |
75 | | |
76 | 1.44M | const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb); |
77 | | |
78 | 1.44M | if (SaoTypeIdx==2) { |
79 | 178k | int hPos[2], vPos[2]; |
80 | 178k | ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride |
81 | 178k | int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3; |
82 | | |
83 | 178k | switch (SaoEoClass) { |
84 | 26.1k | case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break; |
85 | 38.6k | case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break; |
86 | 80.1k | case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break; |
87 | 33.8k | case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break; |
88 | 178k | } |
89 | | |
90 | 178k | vPosStride[0] = vPos[0] * in_stride; |
91 | 178k | 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 | 178k | int8_t saoOffsetVal[5]; // [2] unused |
96 | 178k | saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1]; |
97 | 178k | saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1]; |
98 | 178k | saoOffsetVal[2] = 0; |
99 | 178k | saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1]; |
100 | 178k | saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1]; |
101 | | |
102 | | |
103 | 2.46M | for (int j=0;j<ctbH;j++) { |
104 | 2.28M | const pixel_t* in_ptr = &in_img [xC+(yC+j)*in_stride]; |
105 | 2.28M | /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride]; |
106 | | |
107 | 41.7M | for (int i=0;i<ctbW;i++) { |
108 | 39.5M | int edgeIdx = -1; |
109 | | |
110 | 39.5M | logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j); |
111 | | |
112 | 39.5M | if ((extendedTests && |
113 | 17.9M | (sps->pcm_loop_filter_disable_flag && |
114 | 6.38M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) || |
115 | 39.4M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { |
116 | 13.0M | continue; |
117 | 13.0M | } |
118 | | |
119 | | // do the expensive test for boundaries only at the boundaries |
120 | 26.4M | bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1); |
121 | | |
122 | 26.4M | if (testBoundary) |
123 | 15.6M | for (int k=0;k<2;k++) { |
124 | 10.7M | int xS = xC+i+hPos[k]; |
125 | 10.7M | int yS = yC+j+vPos[k]; |
126 | | |
127 | 10.7M | if (xS<0 || yS<0 || xS>=width || yS>=height) { |
128 | 627k | edgeIdx=0; |
129 | 627k | break; |
130 | 627k | } |
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 | 10.0M | slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW, |
139 | 10.0M | yS<<chromashiftH); |
140 | 10.0M | if (sliceHeader==nullptr) { return; } |
141 | | |
142 | 10.0M | int sliceAddrRS = sliceHeader->SliceAddrRS; |
143 | 10.0M | if (sliceAddrRS < ctbSliceAddrRS && |
144 | 6.55k | img->get_SliceHeader((xC+i)<<chromashiftW, |
145 | 6.55k | (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { |
146 | 3.86k | edgeIdx=0; |
147 | 3.86k | break; |
148 | 3.86k | } |
149 | | |
150 | 10.0M | if (sliceAddrRS > ctbSliceAddrRS && |
151 | 6.34k | img->get_SliceHeader(xS<<chromashiftW, |
152 | 6.34k | yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { |
153 | 2.91k | edgeIdx=0; |
154 | 2.91k | break; |
155 | 2.91k | } |
156 | | |
157 | | |
158 | 10.0M | if (pps->loop_filter_across_tiles_enabled_flag==0 && |
159 | 9.71M | pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] != |
160 | 9.71M | pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) { |
161 | 7.97k | edgeIdx=0; |
162 | 7.97k | break; |
163 | 7.97k | } |
164 | 10.0M | } |
165 | | |
166 | 26.4M | if (edgeIdx != 0) { |
167 | | |
168 | 25.8M | edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) + |
169 | 25.8M | Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]]) ); |
170 | | |
171 | 25.8M | if (1) { // edgeIdx != 0) { // seems to be faster without this check (zero in offset table) |
172 | 25.8M | int offset = saoOffsetVal[edgeIdx+2]; |
173 | | |
174 | 25.8M | out_ptr[i] = Clip3(0,maxPixelValue, |
175 | 25.8M | in_ptr[i] + offset); |
176 | 25.8M | } |
177 | 25.8M | } |
178 | 26.4M | } |
179 | 2.28M | } |
180 | 178k | } |
181 | 1.26M | else { |
182 | 1.26M | int bandShift = bitDepth-5; |
183 | 1.26M | int saoLeftClass = saoinfo->sao_band_position[cIdx]; |
184 | 1.26M | logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass); |
185 | | |
186 | 1.26M | int bandTable[32]; |
187 | 1.26M | memset(bandTable, 0, sizeof(int)*32); |
188 | | |
189 | 6.33M | for (int k=0;k<4;k++) { |
190 | 5.06M | bandTable[ (k+saoLeftClass)&31 ] = k+1; |
191 | 5.06M | } |
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 | 1.26M | if (extendedTests) { |
202 | | |
203 | | // (A) full version with all checks |
204 | | |
205 | 4.23M | for (int j=0;j<ctbH;j++) |
206 | 75.5M | for (int i=0;i<ctbW;i++) { |
207 | | |
208 | 71.6M | if ((sps->pcm_loop_filter_disable_flag && |
209 | 38.9M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) || |
210 | 71.4M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { |
211 | 65.6M | continue; |
212 | 65.6M | } |
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 | 5.93M | 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 | 5.93M | pixel = Clip3(0, maxPixelValue, pixel); |
222 | | |
223 | 5.93M | int bandIdx = bandTable[pixel >> bandShift]; |
224 | | |
225 | 5.93M | if (bandIdx>0) { |
226 | 749k | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; |
227 | | |
228 | 749k | logtrace(LogSAO,"%d %d (%d) offset %d %x -> %x\n",xC+i,yC+j,bandIdx, |
229 | 749k | offset, |
230 | 749k | in_img[xC+i+(yC+j)*in_stride], |
231 | 749k | in_img[xC+i+(yC+j)*in_stride]+offset); |
232 | | |
233 | 749k | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, |
234 | 749k | in_img[xC+i+(yC+j)*in_stride] + offset); |
235 | 749k | } |
236 | 5.93M | } |
237 | 336k | } |
238 | 930k | else |
239 | 930k | { |
240 | | // (B) simplified version (only works if no PCM and transquant_bypass is active) |
241 | | |
242 | 14.0M | for (int j=0;j<ctbH;j++) |
243 | 259M | for (int i=0;i<ctbW;i++) { |
244 | | |
245 | 246M | 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 | 246M | pixel = Clip3(0, maxPixelValue, pixel); |
251 | | |
252 | 246M | int bandIdx = bandTable[pixel >> bandShift]; |
253 | | |
254 | 246M | if (bandIdx>0) { |
255 | 14.8M | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; |
256 | | |
257 | 14.8M | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, |
258 | 14.8M | in_img[xC+i+(yC+j)*in_stride] + offset); |
259 | 14.8M | } |
260 | 246M | } |
261 | 930k | } |
262 | 1.26M | } |
263 | 1.44M | } 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 | 762k | { | 34 | 762k | const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb); | 35 | | | 36 | 762k | int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3; | 37 | | | 38 | 762k | logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH); | 39 | | | 40 | 762k | if (SaoTypeIdx==0) { | 41 | 280k | return; | 42 | 280k | } | 43 | | | 44 | 481k | const seq_parameter_set* sps = &img->get_sps(); | 45 | 481k | const pic_parameter_set* pps = &img->get_pps(); | 46 | 481k | const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C); | 47 | 481k | const int maxPixelValue = (1<<bitDepth)-1; | 48 | | | 49 | | // top left position of CTB in pixels | 50 | 481k | const int xC = xCtb*nSW; | 51 | 481k | const int yC = yCtb*nSH; | 52 | | | 53 | 481k | const int width = img->get_width(cIdx); | 54 | 481k | const int height = img->get_height(cIdx); | 55 | | | 56 | 481k | const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS; | 57 | | | 58 | 481k | const int picWidthInCtbs = sps->PicWidthInCtbsY; | 59 | 481k | const int chromashiftW = sps->get_chroma_shift_W(cIdx); | 60 | 481k | const int chromashiftH = sps->get_chroma_shift_H(cIdx); | 61 | 481k | const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW; | 62 | 481k | const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH; | 63 | | | 64 | | | 65 | 2.89M | for (int i=0;i<5;i++) | 66 | 2.40M | { | 67 | 2.40M | logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]); | 68 | 2.40M | } | 69 | | | 70 | | | 71 | | // actual size of CTB to be processed (can be smaller when partially outside of image) | 72 | 481k | const int ctbW = (xC+nSW>width) ? width -xC : nSW; | 73 | 481k | const int ctbH = (yC+nSH>height) ? height-yC : nSH; | 74 | | | 75 | | | 76 | 481k | const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb); | 77 | | | 78 | 481k | if (SaoTypeIdx==2) { | 79 | 80.1k | int hPos[2], vPos[2]; | 80 | 80.1k | ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride | 81 | 80.1k | int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3; | 82 | | | 83 | 80.1k | switch (SaoEoClass) { | 84 | 9.34k | case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break; | 85 | 9.24k | case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break; | 86 | 48.0k | case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break; | 87 | 13.5k | case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break; | 88 | 80.1k | } | 89 | | | 90 | 80.1k | vPosStride[0] = vPos[0] * in_stride; | 91 | 80.1k | 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 | 80.1k | int8_t saoOffsetVal[5]; // [2] unused | 96 | 80.1k | saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1]; | 97 | 80.1k | saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1]; | 98 | 80.1k | saoOffsetVal[2] = 0; | 99 | 80.1k | saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1]; | 100 | 80.1k | saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1]; | 101 | | | 102 | | | 103 | 1.06M | for (int j=0;j<ctbH;j++) { | 104 | 984k | const pixel_t* in_ptr = &in_img [xC+(yC+j)*in_stride]; | 105 | 984k | /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride]; | 106 | | | 107 | 19.0M | for (int i=0;i<ctbW;i++) { | 108 | 18.0M | int edgeIdx = -1; | 109 | | | 110 | 18.0M | logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j); | 111 | | | 112 | 18.0M | if ((extendedTests && | 113 | 11.9M | (sps->pcm_loop_filter_disable_flag && | 114 | 3.58M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) || | 115 | 18.0M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { | 116 | 8.27M | continue; | 117 | 8.27M | } | 118 | | | 119 | | // do the expensive test for boundaries only at the boundaries | 120 | 9.76M | bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1); | 121 | | | 122 | 9.76M | if (testBoundary) | 123 | 5.63M | for (int k=0;k<2;k++) { | 124 | 3.86M | int xS = xC+i+hPos[k]; | 125 | 3.86M | int yS = yC+j+vPos[k]; | 126 | | | 127 | 3.86M | if (xS<0 || yS<0 || xS>=width || yS>=height) { | 128 | 235k | edgeIdx=0; | 129 | 235k | break; | 130 | 235k | } | 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 | 3.63M | slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW, | 139 | 3.63M | yS<<chromashiftH); | 140 | 3.63M | if (sliceHeader==nullptr) { return; } | 141 | | | 142 | 3.63M | int sliceAddrRS = sliceHeader->SliceAddrRS; | 143 | 3.63M | if (sliceAddrRS < ctbSliceAddrRS && | 144 | 4.04k | img->get_SliceHeader((xC+i)<<chromashiftW, | 145 | 4.04k | (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { | 146 | 1.72k | edgeIdx=0; | 147 | 1.72k | break; | 148 | 1.72k | } | 149 | | | 150 | 3.63M | if (sliceAddrRS > ctbSliceAddrRS && | 151 | 3.30k | img->get_SliceHeader(xS<<chromashiftW, | 152 | 3.30k | yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { | 153 | 1.40k | edgeIdx=0; | 154 | 1.40k | break; | 155 | 1.40k | } | 156 | | | 157 | | | 158 | 3.63M | if (pps->loop_filter_across_tiles_enabled_flag==0 && | 159 | 3.29M | pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] != | 160 | 3.29M | pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) { | 161 | 3.52k | edgeIdx=0; | 162 | 3.52k | break; | 163 | 3.52k | } | 164 | 3.63M | } | 165 | | | 166 | 9.76M | if (edgeIdx != 0) { | 167 | | | 168 | 9.52M | edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) + | 169 | 9.52M | Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]]) ); | 170 | | | 171 | 9.52M | if (1) { // edgeIdx != 0) { // seems to be faster without this check (zero in offset table) | 172 | 9.52M | int offset = saoOffsetVal[edgeIdx+2]; | 173 | | | 174 | 9.52M | out_ptr[i] = Clip3(0,maxPixelValue, | 175 | 9.52M | in_ptr[i] + offset); | 176 | 9.52M | } | 177 | 9.52M | } | 178 | 9.76M | } | 179 | 984k | } | 180 | 80.1k | } | 181 | 401k | else { | 182 | 401k | int bandShift = bitDepth-5; | 183 | 401k | int saoLeftClass = saoinfo->sao_band_position[cIdx]; | 184 | 401k | logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass); | 185 | | | 186 | 401k | int bandTable[32]; | 187 | 401k | memset(bandTable, 0, sizeof(int)*32); | 188 | | | 189 | 2.00M | for (int k=0;k<4;k++) { | 190 | 1.60M | bandTable[ (k+saoLeftClass)&31 ] = k+1; | 191 | 1.60M | } | 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 | 401k | if (extendedTests) { | 202 | | | 203 | | // (A) full version with all checks | 204 | | | 205 | 2.55M | for (int j=0;j<ctbH;j++) | 206 | 53.2M | for (int i=0;i<ctbW;i++) { | 207 | | | 208 | 50.8M | if ((sps->pcm_loop_filter_disable_flag && | 209 | 30.6M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) || | 210 | 50.7M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { | 211 | 46.8M | continue; | 212 | 46.8M | } | 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 | 4.01M | 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 | 4.01M | pixel = Clip3(0, maxPixelValue, pixel); | 222 | | | 223 | 4.01M | int bandIdx = bandTable[pixel >> bandShift]; | 224 | | | 225 | 4.01M | if (bandIdx>0) { | 226 | 547k | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; | 227 | | | 228 | 547k | logtrace(LogSAO,"%d %d (%d) offset %d %x -> %x\n",xC+i,yC+j,bandIdx, | 229 | 547k | offset, | 230 | 547k | in_img[xC+i+(yC+j)*in_stride], | 231 | 547k | in_img[xC+i+(yC+j)*in_stride]+offset); | 232 | | | 233 | 547k | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, | 234 | 547k | in_img[xC+i+(yC+j)*in_stride] + offset); | 235 | 547k | } | 236 | 4.01M | } | 237 | 162k | } | 238 | 238k | else | 239 | 238k | { | 240 | | // (B) simplified version (only works if no PCM and transquant_bypass is active) | 241 | | | 242 | 3.37M | for (int j=0;j<ctbH;j++) | 243 | 66.5M | for (int i=0;i<ctbW;i++) { | 244 | | | 245 | 63.3M | 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 | 63.3M | pixel = Clip3(0, maxPixelValue, pixel); | 251 | | | 252 | 63.3M | int bandIdx = bandTable[pixel >> bandShift]; | 253 | | | 254 | 63.3M | if (bandIdx>0) { | 255 | 7.40M | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; | 256 | | | 257 | 7.40M | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, | 258 | 7.40M | in_img[xC+i+(yC+j)*in_stride] + offset); | 259 | 7.40M | } | 260 | 63.3M | } | 261 | 238k | } | 262 | 401k | } | 263 | 481k | } |
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.78M | { | 34 | 2.78M | const sao_info* saoinfo = img->get_sao_info(xCtb,yCtb); | 35 | | | 36 | 2.78M | int SaoTypeIdx = (saoinfo->SaoTypeIdx >> (2*cIdx)) & 0x3; | 37 | | | 38 | 2.78M | logtrace(LogSAO,"apply_sao CTB %d;%d cIdx:%d type=%d (%dx%d)\n",xCtb,yCtb,cIdx, SaoTypeIdx, nSW,nSH); | 39 | | | 40 | 2.78M | if (SaoTypeIdx==0) { | 41 | 1.81M | return; | 42 | 1.81M | } | 43 | | | 44 | 964k | const seq_parameter_set* sps = &img->get_sps(); | 45 | 964k | const pic_parameter_set* pps = &img->get_pps(); | 46 | 964k | const int bitDepth = (cIdx==0 ? sps->BitDepth_Y : sps->BitDepth_C); | 47 | 964k | const int maxPixelValue = (1<<bitDepth)-1; | 48 | | | 49 | | // top left position of CTB in pixels | 50 | 964k | const int xC = xCtb*nSW; | 51 | 964k | const int yC = yCtb*nSH; | 52 | | | 53 | 964k | const int width = img->get_width(cIdx); | 54 | 964k | const int height = img->get_height(cIdx); | 55 | | | 56 | 964k | const int ctbSliceAddrRS = img->get_SliceHeader(xC,yC)->SliceAddrRS; | 57 | | | 58 | 964k | const int picWidthInCtbs = sps->PicWidthInCtbsY; | 59 | 964k | const int chromashiftW = sps->get_chroma_shift_W(cIdx); | 60 | 964k | const int chromashiftH = sps->get_chroma_shift_H(cIdx); | 61 | 964k | const int ctbshiftW = sps->Log2CtbSizeY - chromashiftW; | 62 | 964k | const int ctbshiftH = sps->Log2CtbSizeY - chromashiftH; | 63 | | | 64 | | | 65 | 5.78M | for (int i=0;i<5;i++) | 66 | 4.82M | { | 67 | 4.82M | logtrace(LogSAO,"offset[%d] = %d\n", i, i==0 ? 0 : saoinfo->saoOffsetVal[cIdx][i-1]); | 68 | 4.82M | } | 69 | | | 70 | | | 71 | | // actual size of CTB to be processed (can be smaller when partially outside of image) | 72 | 964k | const int ctbW = (xC+nSW>width) ? width -xC : nSW; | 73 | 964k | const int ctbH = (yC+nSH>height) ? height-yC : nSH; | 74 | | | 75 | | | 76 | 964k | const bool extendedTests = img->get_CTB_has_pcm_or_cu_transquant_bypass(xCtb,yCtb); | 77 | | | 78 | 964k | if (SaoTypeIdx==2) { | 79 | 98.5k | int hPos[2], vPos[2]; | 80 | 98.5k | ptrdiff_t vPosStride[2]; // vPos[] multiplied by image stride | 81 | 98.5k | int SaoEoClass = (saoinfo->SaoEoClass >> (2*cIdx)) & 0x3; | 82 | | | 83 | 98.5k | switch (SaoEoClass) { | 84 | 16.7k | case 0: hPos[0]=-1; hPos[1]= 1; vPos[0]= 0; vPos[1]=0; break; | 85 | 29.3k | case 1: hPos[0]= 0; hPos[1]= 0; vPos[0]=-1; vPos[1]=1; break; | 86 | 32.1k | case 2: hPos[0]=-1; hPos[1]= 1; vPos[0]=-1; vPos[1]=1; break; | 87 | 20.3k | case 3: hPos[0]= 1; hPos[1]=-1; vPos[0]=-1; vPos[1]=1; break; | 88 | 98.5k | } | 89 | | | 90 | 98.5k | vPosStride[0] = vPos[0] * in_stride; | 91 | 98.5k | 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 | 98.5k | int8_t saoOffsetVal[5]; // [2] unused | 96 | 98.5k | saoOffsetVal[0] = saoinfo->saoOffsetVal[cIdx][1-1]; | 97 | 98.5k | saoOffsetVal[1] = saoinfo->saoOffsetVal[cIdx][2-1]; | 98 | 98.5k | saoOffsetVal[2] = 0; | 99 | 98.5k | saoOffsetVal[3] = saoinfo->saoOffsetVal[cIdx][3-1]; | 100 | 98.5k | saoOffsetVal[4] = saoinfo->saoOffsetVal[cIdx][4-1]; | 101 | | | 102 | | | 103 | 1.40M | for (int j=0;j<ctbH;j++) { | 104 | 1.30M | const pixel_t* in_ptr = &in_img [xC+(yC+j)*in_stride]; | 105 | 1.30M | /* */ pixel_t* out_ptr = &out_img[xC+(yC+j)*out_stride]; | 106 | | | 107 | 22.7M | for (int i=0;i<ctbW;i++) { | 108 | 21.4M | int edgeIdx = -1; | 109 | | | 110 | 21.4M | logtrace(LogSAO, "pos %d,%d\n",xC+i,yC+j); | 111 | | | 112 | 21.4M | if ((extendedTests && | 113 | 5.95M | (sps->pcm_loop_filter_disable_flag && | 114 | 2.80M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH))) || | 115 | 21.4M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { | 116 | 4.74M | continue; | 117 | 4.74M | } | 118 | | | 119 | | // do the expensive test for boundaries only at the boundaries | 120 | 16.7M | bool testBoundary = (i==0 || j==0 || i==ctbW-1 || j==ctbH-1); | 121 | | | 122 | 16.7M | if (testBoundary) | 123 | 10.0M | for (int k=0;k<2;k++) { | 124 | 6.84M | int xS = xC+i+hPos[k]; | 125 | 6.84M | int yS = yC+j+vPos[k]; | 126 | | | 127 | 6.84M | if (xS<0 || yS<0 || xS>=width || yS>=height) { | 128 | 392k | edgeIdx=0; | 129 | 392k | break; | 130 | 392k | } | 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 | 6.45M | slice_segment_header* sliceHeader = img->get_SliceHeader(xS<<chromashiftW, | 139 | 6.45M | yS<<chromashiftH); | 140 | 6.45M | if (sliceHeader==nullptr) { return; } | 141 | | | 142 | 6.45M | int sliceAddrRS = sliceHeader->SliceAddrRS; | 143 | 6.45M | if (sliceAddrRS < ctbSliceAddrRS && | 144 | 2.50k | img->get_SliceHeader((xC+i)<<chromashiftW, | 145 | 2.50k | (yC+j)<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { | 146 | 2.14k | edgeIdx=0; | 147 | 2.14k | break; | 148 | 2.14k | } | 149 | | | 150 | 6.44M | if (sliceAddrRS > ctbSliceAddrRS && | 151 | 3.04k | img->get_SliceHeader(xS<<chromashiftW, | 152 | 3.04k | yS<<chromashiftH)->slice_loop_filter_across_slices_enabled_flag==0) { | 153 | 1.51k | edgeIdx=0; | 154 | 1.51k | break; | 155 | 1.51k | } | 156 | | | 157 | | | 158 | 6.44M | if (pps->loop_filter_across_tiles_enabled_flag==0 && | 159 | 6.41M | pps->scan->TileIdRS[(xS>>ctbshiftW) + (yS>>ctbshiftH)*picWidthInCtbs] != | 160 | 6.41M | pps->scan->TileIdRS[(xC>>ctbshiftW) + (yC>>ctbshiftH)*picWidthInCtbs]) { | 161 | 4.45k | edgeIdx=0; | 162 | 4.45k | break; | 163 | 4.45k | } | 164 | 6.44M | } | 165 | | | 166 | 16.7M | if (edgeIdx != 0) { | 167 | | | 168 | 16.3M | edgeIdx = ( Sign(in_ptr[i] - in_ptr[i+hPos[0]+vPosStride[0]]) + | 169 | 16.3M | Sign(in_ptr[i] - in_ptr[i+hPos[1]+vPosStride[1]]) ); | 170 | | | 171 | 16.3M | if (1) { // edgeIdx != 0) { // seems to be faster without this check (zero in offset table) | 172 | 16.3M | int offset = saoOffsetVal[edgeIdx+2]; | 173 | | | 174 | 16.3M | out_ptr[i] = Clip3(0,maxPixelValue, | 175 | 16.3M | in_ptr[i] + offset); | 176 | 16.3M | } | 177 | 16.3M | } | 178 | 16.7M | } | 179 | 1.30M | } | 180 | 98.5k | } | 181 | 865k | else { | 182 | 865k | int bandShift = bitDepth-5; | 183 | 865k | int saoLeftClass = saoinfo->sao_band_position[cIdx]; | 184 | 865k | logtrace(LogSAO,"saoLeftClass: %d\n",saoLeftClass); | 185 | | | 186 | 865k | int bandTable[32]; | 187 | 865k | memset(bandTable, 0, sizeof(int)*32); | 188 | | | 189 | 4.32M | for (int k=0;k<4;k++) { | 190 | 3.45M | bandTable[ (k+saoLeftClass)&31 ] = k+1; | 191 | 3.45M | } | 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 | 865k | if (extendedTests) { | 202 | | | 203 | | // (A) full version with all checks | 204 | | | 205 | 1.68M | for (int j=0;j<ctbH;j++) | 206 | 22.2M | for (int i=0;i<ctbW;i++) { | 207 | | | 208 | 20.7M | if ((sps->pcm_loop_filter_disable_flag && | 209 | 8.27M | img->get_pcm_flag((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) || | 210 | 20.6M | img->get_cu_transquant_bypass((xC+i)<<chromashiftW,(yC+j)<<chromashiftH)) { | 211 | 18.7M | continue; | 212 | 18.7M | } | 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.92M | 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.92M | pixel = Clip3(0, maxPixelValue, pixel); | 222 | | | 223 | 1.92M | int bandIdx = bandTable[pixel >> bandShift]; | 224 | | | 225 | 1.92M | if (bandIdx>0) { | 226 | 202k | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; | 227 | | | 228 | 202k | logtrace(LogSAO,"%d %d (%d) offset %d %x -> %x\n",xC+i,yC+j,bandIdx, | 229 | 202k | offset, | 230 | 202k | in_img[xC+i+(yC+j)*in_stride], | 231 | 202k | in_img[xC+i+(yC+j)*in_stride]+offset); | 232 | | | 233 | 202k | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, | 234 | 202k | in_img[xC+i+(yC+j)*in_stride] + offset); | 235 | 202k | } | 236 | 1.92M | } | 237 | 173k | } | 238 | 691k | else | 239 | 691k | { | 240 | | // (B) simplified version (only works if no PCM and transquant_bypass is active) | 241 | | | 242 | 10.6M | for (int j=0;j<ctbH;j++) | 243 | 193M | for (int i=0;i<ctbW;i++) { | 244 | | | 245 | 183M | 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 | 183M | pixel = Clip3(0, maxPixelValue, pixel); | 251 | | | 252 | 183M | int bandIdx = bandTable[pixel >> bandShift]; | 253 | | | 254 | 183M | if (bandIdx>0) { | 255 | 7.43M | int offset = saoinfo->saoOffsetVal[cIdx][bandIdx-1]; | 256 | | | 257 | 7.43M | out_img[xC+i+(yC+j)*out_stride] = Clip3(0,maxPixelValue, | 258 | 7.43M | in_img[xC+i+(yC+j)*in_stride] + offset); | 259 | 7.43M | } | 260 | 183M | } | 261 | 691k | } | 262 | 865k | } | 263 | 964k | } |
|
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 | 3.54M | { |
272 | 3.54M | if (img->high_bit_depth(cIdx)) { |
273 | 762k | apply_sao_internal<uint16_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH, |
274 | 762k | reinterpret_cast<const uint16_t*>(in_img), in_stride, |
275 | 762k | reinterpret_cast<uint16_t*>(out_img),out_stride); |
276 | 762k | } |
277 | 2.78M | else { |
278 | 2.78M | apply_sao_internal<uint8_t>(img,xCtb,yCtb, shdr,cIdx,nSW,nSH, |
279 | 2.78M | in_img, in_stride, |
280 | 2.78M | out_img,out_stride); |
281 | 2.78M | } |
282 | 3.54M | } |
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 | 43.6k | { |
410 | 43.6k | state = Running; |
411 | 43.6k | img->thread_run(this); |
412 | | |
413 | 43.6k | const seq_parameter_set& sps = img->get_sps(); |
414 | | |
415 | 43.6k | const int rightCtb = sps.PicWidthInCtbsY-1; |
416 | 43.6k | const int ctbSize = (1<<sps.Log2CtbSizeY); |
417 | | |
418 | | |
419 | | // wait until also the CTB-rows below and above are ready |
420 | | |
421 | 43.6k | img->wait_for_progress(this, rightCtb,ctb_y, inputProgress); |
422 | | |
423 | 43.6k | if (ctb_y>0) { |
424 | 35.9k | img->wait_for_progress(this, rightCtb,ctb_y-1, inputProgress); |
425 | 35.9k | } |
426 | | |
427 | 43.6k | if (ctb_y+1<sps.PicHeightInCtbsY) { |
428 | 35.9k | img->wait_for_progress(this, rightCtb,ctb_y+1, inputProgress); |
429 | 35.9k | } |
430 | | |
431 | | |
432 | | // copy input image to output for this CTB-row |
433 | | |
434 | 43.6k | outputImg->copy_lines_from(inputImg, ctb_y * ctbSize, (ctb_y+1) * ctbSize); |
435 | | |
436 | | |
437 | | // process SAO in the CTB-row |
438 | | |
439 | 1.91M | for (int xCtb=0; xCtb<sps.PicWidthInCtbsY; xCtb++) |
440 | 1.86M | { |
441 | 1.86M | const slice_segment_header* shdr = img->get_SliceHeaderCtb(xCtb,ctb_y); |
442 | 1.86M | if (shdr==nullptr) { |
443 | 0 | break; |
444 | 0 | } |
445 | | |
446 | 1.86M | if (shdr->slice_sao_luma_flag) { |
447 | 1.19M | apply_sao(img, xCtb,ctb_y, shdr, 0, ctbSize, ctbSize, |
448 | 1.19M | inputImg ->get_image_plane(0), inputImg ->get_image_stride(0), |
449 | 1.19M | outputImg->get_image_plane(0), outputImg->get_image_stride(0)); |
450 | 1.19M | } |
451 | | |
452 | 1.86M | if (shdr->slice_sao_chroma_flag) { |
453 | 1.17M | int nSW = ctbSize / sps.SubWidthC; |
454 | 1.17M | int nSH = ctbSize / sps.SubHeightC; |
455 | | |
456 | 1.17M | apply_sao(img, xCtb,ctb_y, shdr, 1, nSW,nSH, |
457 | 1.17M | inputImg ->get_image_plane(1), inputImg ->get_image_stride(1), |
458 | 1.17M | outputImg->get_image_plane(1), outputImg->get_image_stride(1)); |
459 | | |
460 | 1.17M | apply_sao(img, xCtb,ctb_y, shdr, 2, nSW,nSH, |
461 | 1.17M | inputImg ->get_image_plane(2), inputImg ->get_image_stride(2), |
462 | 1.17M | outputImg->get_image_plane(2), outputImg->get_image_stride(2)); |
463 | 1.17M | } |
464 | 1.86M | } |
465 | | |
466 | | |
467 | | // mark SAO progress |
468 | | |
469 | 1.91M | for (int x=0;x<=rightCtb;x++) { |
470 | 1.86M | const int CtbWidth = sps.PicWidthInCtbsY; |
471 | 1.86M | img->ctb_progress[x+ctb_y*CtbWidth].set_progress(CTB_PROGRESS_SAO); |
472 | 1.86M | } |
473 | | |
474 | | |
475 | 43.6k | state = Finished; |
476 | 43.6k | img->thread_finishes(this); |
477 | 43.6k | } |
478 | | |
479 | | |
480 | | bool add_sao_tasks(image_unit* imgunit, int saoInputProgress) |
481 | 9.65k | { |
482 | 9.65k | de265_image* img = imgunit->img; |
483 | 9.65k | const seq_parameter_set& sps = img->get_sps(); |
484 | | |
485 | 9.65k | if (sps.sample_adaptive_offset_enabled_flag==0) { |
486 | 1.95k | return false; |
487 | 1.95k | } |
488 | | |
489 | | |
490 | 7.69k | decoder_context* ctx = img->decctx; |
491 | | |
492 | 7.69k | de265_error err = imgunit->sao_output.alloc_image(img->get_width(), img->get_height(), |
493 | 7.69k | img->get_chroma_format(), |
494 | 7.69k | img->get_shared_sps(), |
495 | 7.69k | false, |
496 | 7.69k | img->decctx, //img->encctx, |
497 | 7.69k | img->pts, img->user_data, true); |
498 | 7.69k | 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 | 7.69k | int nRows = sps.PicHeightInCtbsY; |
504 | | |
505 | 7.69k | img->thread_start(nRows); |
506 | | |
507 | 51.3k | for (int y=0;y<nRows;y++) |
508 | 43.6k | { |
509 | 43.6k | thread_task_sao* task = new thread_task_sao; |
510 | | |
511 | 43.6k | task->inputImg = img; |
512 | 43.6k | task->outputImg = &imgunit->sao_output; |
513 | 43.6k | task->img = img; |
514 | 43.6k | task->ctb_y = y; |
515 | 43.6k | task->inputProgress = saoInputProgress; |
516 | | |
517 | 43.6k | imgunit->tasks.push_back(task); |
518 | 43.6k | ctx->thread_pool_.add_task(task); |
519 | 43.6k | } |
520 | | |
521 | | /* Currently need barrier here because when are finished, we have to swap the pixel |
522 | | data back into the main image. */ |
523 | 7.69k | img->wait_for_completion(); |
524 | | |
525 | 7.69k | img->exchange_pixel_data_with(imgunit->sao_output); |
526 | | |
527 | 7.69k | return true; |
528 | 7.69k | } |