Grok 20.3.2
PostDecodeFiltersOJPH.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include "BlockExec.h"
21
22namespace grk::t1::ojph
23{
24template<typename T>
26{
27public:
29 : roiShift(block->roishift), shift(31U - (block->k_msbs + 1U))
30 {}
31 inline void copy(T* dest, const T* src, uint32_t len)
32 {
33 T thresh = 1 << roiShift;
34 for(uint32_t i = 0; i < len; ++i)
35 {
36 T val = src[i];
37 T mag = (val & 0x7FFFFFFF);
38 if(mag >= thresh)
39 val = (T)(((uint32_t)mag >> roiShift) & ((uint32_t)val & 0x80000000));
40 int32_t val_shifted = (val & 0x7FFFFFFF) >> shift;
41 dest[i] = (int32_t)(((uint32_t)val & 0x80000000) ? -val_shifted : val_shifted);
42 }
43 }
44
45private:
46 uint32_t roiShift;
47 uint32_t shift;
48};
49template<typename T>
51{
52public:
53 explicit ShiftOJPHFilter(DecompressBlockExec* block) : shift(31U - (block->k_msbs + 1U)) {}
54 inline void copy(T* dest, const T* src, uint32_t len)
55 {
56 for(uint32_t i = 0; i < len; ++i)
57 {
58 T val = src[i];
59 T val_shifted = (val & 0x7FFFFFFF) >> shift;
60 dest[i] = (T)(((uint32_t)val & 0x80000000) ? -val_shifted : val_shifted);
61 }
62 }
63
64private:
65 uint32_t shift;
66};
67
68template<typename T>
70{
71public:
73 : roiShift(block->roishift), scale(block->stepsize / (float)(1u << (31 - block->bandNumbps)))
74 {
75 assert(block->bandNumbps <= 31);
76 }
77 inline void copy(T* dest, const T* src, uint32_t len)
78 {
79 T thresh = 1 << roiShift;
80 for(uint32_t i = 0; i < len; ++i)
81 {
82 T val = src[i];
83 T mag = (T)(val & 0x7FFFFFFF);
84 if(mag >= thresh)
85 val = (T)(((uint32_t)mag >> roiShift) & ((uint32_t)val & 0x80000000));
86 float val_scaled = (float)(val & 0x7FFFFFFF) * scale;
87 ((float*)dest)[i] = ((uint32_t)val & 0x80000000) ? -val_scaled : val_scaled;
88 }
89 }
90
91private:
92 uint32_t roiShift;
93 float scale;
94};
95
96template<typename T>
98{
99public:
101 : scale(block->stepsize / (float)(1u << (31 - block->bandNumbps)))
102 {
103 assert(block->bandNumbps <= 31);
104 }
105 inline void copy(T* dest, const T* src, uint32_t len)
106 {
107 for(uint32_t i = 0; i < len; ++i)
108 {
109 int32_t val = src[i];
110 float val_scaled = (float)(val & 0x7FFFFFFF) * scale;
111 ((float*)dest)[i] = ((uint32_t)val & 0x80000000) ? -val_scaled : val_scaled;
112 }
113 }
114
115private:
116 float scale;
117};
118
119// Narrowing filters for 16-bit DWT path: OJPH int32_t sign-magnitude → int16_t band buffers
121{
122public:
123 explicit NarrowShiftOJPHFilter(DecompressBlockExec* block) : shift(31U - (block->k_msbs + 1U)) {}
124 inline void copy(int16_t* dest, const int32_t* src, uint32_t len)
125 {
126 for(uint32_t i = 0; i < len; ++i)
127 {
128 int32_t val = src[i];
129 int32_t val_shifted = (val & 0x7FFFFFFF) >> shift;
130 dest[i] = (int16_t)(((uint32_t)val & 0x80000000) ? -val_shifted : val_shifted);
131 }
132 }
133
134private:
135 uint32_t shift;
136};
137
139{
140public:
142 : roiShift(block->roishift), shift(31U - (block->k_msbs + 1U))
143 {}
144 inline void copy(int16_t* dest, const int32_t* src, uint32_t len)
145 {
146 int32_t thresh = 1 << roiShift;
147 for(uint32_t i = 0; i < len; ++i)
148 {
149 int32_t val = src[i];
150 int32_t mag = (val & 0x7FFFFFFF);
151 if(mag >= thresh)
152 val = (int32_t)(((uint32_t)mag >> roiShift) & ((uint32_t)val & 0x80000000));
153 int32_t val_shifted = (val & 0x7FFFFFFF) >> shift;
154 dest[i] = (int16_t)(((uint32_t)val & 0x80000000) ? -val_shifted : val_shifted);
155 }
156 }
157
158private:
159 uint32_t roiShift;
160 uint32_t shift;
161};
162
163// Narrowing dequantization filter for 16-bit fixed-point 9/7 DWT path (OJPH).
164// Converts OJPH block coder int32 sign-magnitude output to int16 by
165// applying the quantization step size (dequantization) and clamping.
167{
168public:
170 : scale_(block->stepsize / (float)(1u << (31 - block->bandNumbps)))
171 {
172 assert(block->bandNumbps <= 31);
173 }
174 inline void copy(int16_t* dest, const int32_t* src, uint32_t len)
175 {
176 for(uint32_t i = 0; i < len; ++i)
177 {
178 int32_t val = src[i];
179 float val_scaled = (float)(val & 0x7FFFFFFF) * scale_;
180 float signed_val = ((uint32_t)val & 0x80000000) ? -val_scaled : val_scaled;
181 int32_t rounded = (int32_t)(signed_val >= 0 ? signed_val + 0.5f : signed_val - 0.5f);
182 if(rounded > 32767)
183 rounded = 32767;
184 else if(rounded < -32768)
185 rounded = -32768;
186 dest[i] = (int16_t)rounded;
187 }
188 }
189
190private:
191 float scale_;
192};
193
194// Same as NarrowScaleOJPHFilter16 but with ROI shift applied first.
196{
197public:
199 : roiShift_(block->roishift),
200 scale_(block->stepsize / (float)(1u << (31 - block->bandNumbps)))
201 {
202 assert(block->bandNumbps <= 31);
203 }
204 inline void copy(int16_t* dest, const int32_t* src, uint32_t len)
205 {
206 int32_t thresh = 1 << roiShift_;
207 for(uint32_t i = 0; i < len; ++i)
208 {
209 int32_t val = src[i];
210 int32_t mag = (val & 0x7FFFFFFF);
211 if(mag >= thresh)
212 {
213 mag >>= roiShift_;
214 val = (int32_t)(((uint32_t)mag) | ((uint32_t)val & 0x80000000));
215 }
216 float val_scaled = (float)(val & 0x7FFFFFFF) * scale_;
217 float signed_val = ((uint32_t)val & 0x80000000) ? -val_scaled : val_scaled;
218 int32_t rounded = (int32_t)(signed_val >= 0 ? signed_val + 0.5f : signed_val - 0.5f);
219 if(rounded > 32767)
220 rounded = 32767;
221 else if(rounded < -32768)
222 rounded = -32768;
223 dest[i] = (int16_t)rounded;
224 }
225 }
226
227private:
228 uint32_t roiShift_;
229 float scale_;
230};
231
232} // namespace grk::t1::ojph
NarrowRoiScaleOJPHFilter16(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:198
void copy(int16_t *dest, const int32_t *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:204
float scale_
Definition PostDecodeFiltersOJPH.h:229
uint32_t roiShift_
Definition PostDecodeFiltersOJPH.h:228
uint32_t shift
Definition PostDecodeFiltersOJPH.h:160
NarrowRoiShiftOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:141
void copy(int16_t *dest, const int32_t *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:144
uint32_t roiShift
Definition PostDecodeFiltersOJPH.h:159
float scale_
Definition PostDecodeFiltersOJPH.h:191
NarrowScaleOJPHFilter16(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:169
void copy(int16_t *dest, const int32_t *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:174
void copy(int16_t *dest, const int32_t *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:124
NarrowShiftOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:123
uint32_t shift
Definition PostDecodeFiltersOJPH.h:135
uint32_t roiShift
Definition PostDecodeFiltersOJPH.h:92
void copy(T *dest, const T *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:77
RoiScaleOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:72
float scale
Definition PostDecodeFiltersOJPH.h:93
uint32_t shift
Definition PostDecodeFiltersOJPH.h:47
RoiShiftOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:28
void copy(T *dest, const T *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:31
uint32_t roiShift
Definition PostDecodeFiltersOJPH.h:46
float scale
Definition PostDecodeFiltersOJPH.h:116
ScaleOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:100
void copy(T *dest, const T *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:105
uint32_t shift
Definition PostDecodeFiltersOJPH.h:65
void copy(T *dest, const T *src, uint32_t len)
Definition PostDecodeFiltersOJPH.h:54
ShiftOJPHFilter(DecompressBlockExec *block)
Definition PostDecodeFiltersOJPH.h:53
Definition CoderOJPH.cpp:92
uint8_t bandNumbps
Definition BlockExec.h:39
Definition BlockExec.h:64