-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathghe_algorithm.c
369 lines (313 loc) · 13.7 KB
/
ghe_algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// SPDX-License-Identifier: MIT
/*
* Copyright © 2024 Intel Corporation
*/
#include "ghe_algorithm.h"
static double ghe_calculate_filter_coefficient(struct globalhist_context *ghecontext);
/*
* Get normalized SRGB tranformed value.
* sRGB can be converted to gamma-2.2 curve with 0.04045 as
* intersection point.
* To convert sRGB to CIE XYX use conversion values as
* X= 0.04045 and 12.92.
*/
static double ghe_get_srgb_decoding_value(double input)
{
double output;
if (input <= 0.04045)
output = input / 12.92;
else
output = pow(((input + 0.055) / 1.055), 2.4);
return output;
}
static void ghe_initialize_temporal_filter_params(struct globalhist_context *ghecontext)
{
ghecontext->filterparams.min_cut_off_freq_in_milli_hz =
GLOBALHIST_SMOOTHENING_MIN_SPEED_DEFAULT;
ghecontext->filterparams.max_cut_off_freq_in_milli_hz =
GLOBALHIST_SMOOTHENING_MAX_SPEED_DEFAULT;
ghecontext->filterparams.current_max_cut_off_freq_in_milli_hz =
DD_MAX(ghecontext->filterparams.max_cut_off_freq_in_milli_hz,
ghecontext->filterparams.min_cut_off_freq_in_milli_hz);
ghecontext->filterparams.minimum_step_percent = GLOBALHIST_SMOOTHENING_TOLERANCE_DEFAULT;
for (uint8_t ietbinindex = 0; ietbinindex < GLOBALHIST_IET_LUT_LENGTH; ietbinindex++) {
for (uint8_t filterorder = 0; filterorder < GLOBALHIST_IIR_FILTER_ORDER;
filterorder++)
ghecontext->filterparams.iethistory[ietbinindex][filterorder] =
GLOBALHIST_IET_SCALE_FACTOR;
}
for (uint8_t count = 0; count < GLOBALHIST_IET_LUT_LENGTH; count++)
ghecontext->imageenhancement.luttarget[count] = GLOBALHIST_IET_SCALE_FACTOR;
}
static double ghe_estimate_probability_Of_full_screen_solidcolor(double *powerhistogram,
double totalpower)
{
const double solidcolorpowerthreshold = SOLID_COLOR_POWER_THRESHOLD * totalpower;
double windowsizetoprobabilitymapping[SOLID_COLOR_SEARCH_WINDOW_SIZE] = {1, 1, 0.75, 0.375};
uint8_t n;
/*
* Find N number of consecutive bins which contain SOLID_COLOR_POWER_THRESHOLD
* amount of frame power.
* N = 1 means solid color for sure.
* Since SOLID_COLOR_POWER_THRESHOLD is not 1.0, it detects almost solid color.
* N = 2 may mean near solid color. One pixel value shift will shift energy
* to next or prev bin.
* For example, image with solid color patches 247 and 248 will look almost single solid,
* but histogram will be spread across two bins.
* N >= 3 means probability of solid color is less.
* Return value is gradullay reduced to 0 for N >= 3.
*/
for (n = 1; n <= SOLID_COLOR_SEARCH_WINDOW_SIZE; n++) {
for (uint8_t binindex = 0; binindex < (GLOBALHIST_BIN_COUNT - n + 1); binindex++) {
double sumpower = 0;
for (uint8_t i = binindex; i < (binindex + n); i++) {
sumpower += powerhistogram[i];
if (sumpower >= solidcolorpowerthreshold)
return windowsizetoprobabilitymapping[n - 1];
}
}
}
return 0;
}
/*
* Interpolate IET LUT from histogram based LUT.
* This is required because IET has more entries than histogram LUT
* Interpolation Logic => MinValue + (MaxValue - MinValue) * Interpolator.
*/
static double ghe_apply_1dlut(double inval, double *lut, double maxindex)
{
uint32_t index1, index2;
double val1, val2, interpolator;
double DIndex = inval * maxindex;
index1 = DIndex;
index2 = ceil(DIndex);
interpolator = (DIndex - (double)index1);
val1 = lut[index1];
val2 = lut[index2];
return val1 + interpolator * (val2 - val1);
}
static void ghe_set_diet_reg(struct globalhist_context *ghecontext,
struct globalhist_args *gheargs)
{
gheargs->pipeid = ghecontext->pipe;
gheargs->isprogramdiet = true;
for (uint8_t count = 0; count < GLOBALHIST_IET_LUT_LENGTH; count++)
gheargs->dietfactor[count] = ghecontext->imageenhancement.lutapplied[count];
}
static void ghe_reset_algorithm(struct globalhist_context *ghecontext)
{
for (uint8_t count = 0; count < GLOBALHIST_IET_LUT_LENGTH; count++) {
ghecontext->imageenhancement.lutapplied[count] = GLOBALHIST_IET_SCALE_FACTOR;
for (uint8_t filterorder = 0; filterorder < GLOBALHIST_IIR_FILTER_ORDER;
filterorder++)
ghecontext->filterparams.iethistory[count][filterorder] =
GLOBALHIST_IET_SCALE_FACTOR;
}
/* Reset filter params */
ghecontext->filterparams.targetboost = GLOBALHIST_DEFAULT_BOOST;
ghecontext->filterparams.smootheningiteration = 0;
}
static bool ghe_is_target_iet_reached(struct globalhist_context *ghecontext)
{
double maxacceptabledelta, ietdelta;
maxacceptabledelta = ghecontext->filterparams.minimum_step_percent / 100.0;
for (uint32_t i = 0; i < GLOBALHIST_IET_LUT_LENGTH; i++) {
ietdelta = ((double)ghecontext->imageenhancement.lutapplied[i] -
(double)ghecontext->imageenhancement.luttarget[i])
/ (double)ghecontext->imageenhancement.luttarget[i];
if (ietdelta > maxacceptabledelta)
return false;
}
return true;
}
static double ghe_pcphase_coordinator_temporal_filter_Nthorder(double filtercoefficient,
double input, double *inputhistoryPhase)
{
double adjustedvalue = input;
for (uint8_t count = 0; count < GLOBALHIST_IIR_FILTER_ORDER; count++) {
adjustedvalue = filtercoefficient * adjustedvalue;
adjustedvalue += (1 - filtercoefficient) * inputhistoryPhase[count];
inputhistoryPhase[count] = adjustedvalue;
}
return adjustedvalue;
}
static void ghe_temporal_smoothen_iet(struct globalhist_context *ghecontext,
struct globalhist_args *gheargs)
{
double temporalfiltercoefficient;
if (ghe_is_target_iet_reached(ghecontext) == false) {
temporalfiltercoefficient = ghe_calculate_filter_coefficient(ghecontext);
for (uint8_t count = 0; count < GLOBALHIST_IET_LUT_LENGTH; count++) {
double ietval = ghe_pcphase_coordinator_temporal_filter_Nthorder(
temporalfiltercoefficient,
ghecontext->imageenhancement.luttarget[count],
&ghecontext->filterparams.iethistory[count][0])
+ 0.5;
ietval = DD_MIN(ietval, GLOBALHIST_IET_MAX_VAL);
ghecontext->imageenhancement.lutapplied[count] = ietval;
}
} else {
memcpy(ghecontext->imageenhancement.lutapplied,
ghecontext->imageenhancement.luttarget,
sizeof(ghecontext->imageenhancement.luttarget));
for (uint8_t count = 0; count < GLOBALHIST_IET_LUT_LENGTH; count++) {
for (uint8_t HistoryIndex = 0; HistoryIndex < GLOBALHIST_IIR_FILTER_ORDER;
HistoryIndex++)
ghecontext->filterparams.iethistory[count][HistoryIndex] =
ghecontext->imageenhancement.luttarget[count];
}
}
memcpy(ghecontext->filterparams.prevhistogram, ghecontext->histogram,
sizeof(ghecontext->filterparams.prevhistogram));
}
static double ghe_get_relative_frame_brightness_change(struct globalhist_context *ghecontext,
uint32_t *prevhist)
{
double frameaveragepowerprev = 0;
double frameaveragepowercurr = 0;
double histchangepercentage = 0;
for (uint32_t count = 0; count < GLOBALHIST_BIN_COUNT; count++) {
frameaveragepowerprev += ghecontext->degammalut[count] * (double)prevhist[count];
frameaveragepowercurr += ghecontext->degammalut[count] *
(double)ghecontext->histogram[count];
}
if (frameaveragepowerprev > 0)
histchangepercentage = DD_ABS(frameaveragepowercurr - frameaveragepowerprev)
/ frameaveragepowerprev;
else if (frameaveragepowerprev != frameaveragepowercurr)
histchangepercentage = 1.0;
return DD_MIN(histchangepercentage, 1.0);
}
static double ghe_calculate_filter_coefficient(struct globalhist_context *ghecontext)
{
double angularfrequency, temporalfiltercoefficient;
double mincutofffreq, maxcutofffreq;
double cutofffreq = 0;
double minghesmootheningperiod, relativeframebrightnesschange;
struct globalhist_temporal_filter_params *pfilterparams;
pfilterparams = &ghecontext->filterparams;
mincutofffreq = MILLIUNIT_TO_UNIT(
(double)pfilterparams->current_min_cut_off_freq_in_milli_hz);
maxcutofffreq = MILLIUNIT_TO_UNIT(
(double)pfilterparams->current_max_cut_off_freq_in_milli_hz);
minghesmootheningperiod = (double)(332225.9136) / (double)(10 * 1000 * 1000);
relativeframebrightnesschange = ghe_get_relative_frame_brightness_change(ghecontext,
pfilterparams->prevhistogram);
cutofffreq = mincutofffreq +
(maxcutofffreq - mincutofffreq) * relativeframebrightnesschange;
angularfrequency = 6.2831853 * cutofffreq;
temporalfiltercoefficient = (minghesmootheningperiod * angularfrequency) /
(1 + (minghesmootheningperiod * angularfrequency));
return temporalfiltercoefficient;
}
static void ghe_algorithm(struct globalhist_context *ghecontext,
struct globalhist_args *gheargs)
{
uint32_t totalnumofpixel = 0;
uint32_t *multiplierlut = ghecontext->imageenhancement.luttarget;
double enhancementtable[GLOBALHIST_BIN_COUNT];
double filtered_enhancement_table[GLOBALHIST_BIN_COUNT];
double binindexnormalized, ietval;
double cdfrange, cdfnormalizingfactor;
double probability_of_full_screen_solid_color;
double sumpower = 0; /* Total Power of input frame */
/* Histogram bin wise distribution of power */
double powerdistribution[GLOBALHIST_BIN_COUNT];
const double maxhistbinindex = GLOBALHIST_MAX_BIN_INDEX;
const double ietlutstepsize = 1.0 / (double)GLOBALHIST_MAX_IET_INDEX;
const double histbinstepsize = 1.0 / maxhistbinindex;
const double maxslope = 7.0;
const double minslope = 0.3;
for (uint8_t binindex = 0; binindex < GLOBALHIST_BIN_COUNT; binindex++) {
totalnumofpixel += ghecontext->histogram[binindex];
enhancementtable[binindex] = totalnumofpixel;
}
ghecontext->algorithm.imagesize = totalnumofpixel;
/* Calculate histogram bin wise power distribution and total frame power */
for (uint8_t binindex = 0; binindex < GLOBALHIST_BIN_COUNT; binindex++) {
double binweight = ghecontext->degammalut[binindex];
powerdistribution[binindex] = binweight * (double)ghecontext->histogram[binindex];
sumpower += powerdistribution[binindex];
}
probability_of_full_screen_solid_color =
ghe_estimate_probability_Of_full_screen_solidcolor(powerdistribution, sumpower);
/* Do not modify pixel values for Solid Color */
if (probability_of_full_screen_solid_color == 1) {
for (uint8_t binindex = 0; binindex < GLOBALHIST_IET_LUT_LENGTH; binindex++)
multiplierlut[binindex] = GLOBALHIST_IET_SCALE_FACTOR;
}
cdfrange = enhancementtable[GLOBALHIST_BIN_COUNT - 1] - ghecontext->histogram[0];
cdfnormalizingfactor = 1.0 / cdfrange;
enhancementtable[0] = (double)(enhancementtable[0] - ghecontext->histogram[0])
* cdfnormalizingfactor;
for (uint8_t binindex = 1; binindex < GLOBALHIST_BIN_COUNT; binindex++) {
double outval = (enhancementtable[binindex] - ghecontext->histogram[0])
* cdfnormalizingfactor;
double prevsampleval = enhancementtable[binindex - 1];
double slope = maxhistbinindex * (outval - prevsampleval);
slope = DD_MAX(slope, minslope);
slope = DD_MIN(slope, maxslope);
outval = prevsampleval + slope * histbinstepsize;
outval = DD_MIN(outval, 1.0);
enhancementtable[binindex] = outval;
}
/* No filtering for 0th and last values of the IET. */
filtered_enhancement_table[0] = enhancementtable[0];
filtered_enhancement_table[GLOBALHIST_MAX_BIN_INDEX] =
enhancementtable[GLOBALHIST_MAX_BIN_INDEX];
/* Smoothen jerks in FilteredEnhancementTable by averaging with nearby bins. */
for (uint8_t binindex = 1; binindex < GLOBALHIST_MAX_BIN_INDEX; binindex++) {
/* Average out current and two nearby sampels.*/
filtered_enhancement_table[binindex] = 0.333333 * (enhancementtable[binindex - 1] +
enhancementtable[binindex] + enhancementtable[binindex + 1]);
}
/* Convert LUT of size GlobalHist_BIN_COUNT to IET LUT of size GLOBALHIST_IET_LUT_LENGTH */
for (uint32_t binindex = 1; binindex < GLOBALHIST_IET_LUT_LENGTH; binindex++) {
binindexnormalized = (double)binindex * ietlutstepsize;
ietval = ghe_apply_1dlut(binindexnormalized, filtered_enhancement_table,
maxhistbinindex);
/* Compute sample for multiplier LUT */
ietval = ietval / binindexnormalized;
/* Cap IET val to the value that will not cause clipping. */
ietval = DD_MIN(ietval, 1.0 / binindexnormalized);
ietval = (double)GLOBALHIST_IET_SCALE_FACTOR * ietval + 0.5;
ietval = DD_MIN(ietval, GLOBALHIST_IET_MAX_VAL);
ietval = DD_MAX(ietval, GLOBALHIST_IET_SCALE_FACTOR); /* Never dim any pixel */
multiplierlut[binindex] = ietval;
}
/* 0th multiplier sample can't be computed. Extend 1st sample to the 0th */
multiplierlut[0] = multiplierlut[1];
ghe_temporal_smoothen_iet(ghecontext, gheargs);
memcpy(ghecontext->filterparams.prevhistogram, ghecontext->histogram,
sizeof(ghecontext->histogram));
}
static void ghe_initialize_algorithm(struct globalhist_context *ghecontext,
struct globalhist_args *gheargs)
{
double hist_lut_step_size;
ghecontext->ghefunctable.ghealgorithm = (globalhistalgorithm)ghe_algorithm;
ghecontext->ghefunctable.ghesetiet =
(globalhistprogramietregisters)ghe_set_diet_reg;
ghecontext->ghefunctable.gheresetalgorithm =
(globalhistresetalgorithm)ghe_reset_algorithm;
/* Reset GlobalHist data structures */
ghecontext->ghefunctable.gheresetalgorithm(ghecontext);
ghe_initialize_temporal_filter_params(ghecontext);
hist_lut_step_size = 1.0 / (double)GLOBALHIST_MAX_BIN_INDEX;
for (uint8_t binindex = 0; binindex < GLOBALHIST_MAX_BIN_INDEX; binindex++)
ghecontext->degammalut[binindex] =
ghe_get_srgb_decoding_value((double)binindex * hist_lut_step_size);
/* This call will manage the GlobalHist algorithm and activate Phase-In. */
ghecontext->ghefunctable.ghealgorithm(ghecontext, gheargs);
/* Program calculated DIET factor */
ghecontext->ghefunctable.ghesetiet(ghecontext, gheargs);
}
void histogram_compute_generate_data_bin(struct globalhist_args *gheargs)
{
struct globalhist_context *ghecontext =
(struct globalhist_context *)malloc(sizeof(struct globalhist_context));
assert(ghecontext != NULL);
memcpy(ghecontext->histogram, gheargs->histogram, GLOBALHIST_BIN_COUNT * sizeof(uint32_t));
ghecontext->algorithm.imagesize = (gheargs->resolution_x * gheargs->resolution_y);
ghe_initialize_algorithm(ghecontext, gheargs);
}