Grok 20.3.2
RateControlStats.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 <atomic>
21#include <cfloat>
22#include <cstdint>
23#include <climits>
24#include <memory>
25
26namespace grk
27{
28
37{
39
40 void init(uint16_t numcomps)
41 {
42 numcomps_ = numcomps;
43 numpixByComponent_ = std::make_unique<std::atomic<uint64_t>[]>(numcomps);
44 for(uint16_t i = 0; i < numcomps; ++i)
45 numpixByComponent_[i].store(0);
46 numCodeBlocks_.store(0);
47 // feasible algorithm stats
48 minimumSlope_.store(USHRT_MAX);
49 maximumSlope_.store(0);
50 // simple algorithm stats
51 minRDSlope_.store(DBL_MAX);
52 maxRDSlope_.store(-1.0);
53 }
54
55 uint16_t numcomps_;
56
57 // Per-component pixel counts (accumulated atomically during T1)
58 std::unique_ptr<std::atomic<uint64_t>[]> numpixByComponent_;
59
60 // Total codeblock count
61 std::atomic<uint64_t> numCodeBlocks_{0};
62
63 // Feasible bisect: min/max slopes from convex hull (uint16_t log-domain)
64 std::atomic<uint16_t> minimumSlope_{USHRT_MAX};
65 std::atomic<uint16_t> maximumSlope_{0};
66
67 // Simple bisect: min/max raw RD slopes (double-domain)
68 std::atomic<double> minRDSlope_{DBL_MAX};
69 std::atomic<double> maxRDSlope_{-1.0};
70
71 void updateMinSlope(uint16_t val)
72 {
73 auto cur = minimumSlope_.load(std::memory_order_relaxed);
74 while(val < cur && !minimumSlope_.compare_exchange_weak(cur, val, std::memory_order_relaxed))
75 {
76 }
77 }
78
79 void updateMaxSlope(uint16_t val)
80 {
81 auto cur = maximumSlope_.load(std::memory_order_relaxed);
82 while(val > cur && !maximumSlope_.compare_exchange_weak(cur, val, std::memory_order_relaxed))
83 {
84 }
85 }
86
87 void updateMinRDSlope(double val)
88 {
89 auto cur = minRDSlope_.load(std::memory_order_relaxed);
90 while(val < cur && !minRDSlope_.compare_exchange_weak(cur, val, std::memory_order_relaxed))
91 {
92 }
93 }
94
95 void updateMaxRDSlope(double val)
96 {
97 auto cur = maxRDSlope_.load(std::memory_order_relaxed);
98 while(val > cur && !maxRDSlope_.compare_exchange_weak(cur, val, std::memory_order_relaxed))
99 {
100 }
101 }
102};
103
104} // namespace grk
ResWindow.
Definition CompressedChunkCache.h:36
uint16_t numcomps_
Definition RateControlStats.h:55
std::unique_ptr< std::atomic< uint64_t >[]> numpixByComponent_
Definition RateControlStats.h:58
std::atomic< uint64_t > numCodeBlocks_
Definition RateControlStats.h:61
std::atomic< double > maxRDSlope_
Definition RateControlStats.h:69
void updateMinSlope(uint16_t val)
Definition RateControlStats.h:71
std::atomic< double > minRDSlope_
Definition RateControlStats.h:68
void updateMinRDSlope(double val)
Definition RateControlStats.h:87
void init(uint16_t numcomps)
Definition RateControlStats.h:40
std::atomic< uint16_t > maximumSlope_
Definition RateControlStats.h:65
void updateMaxSlope(uint16_t val)
Definition RateControlStats.h:79
void updateMaxRDSlope(double val)
Definition RateControlStats.h:95
RateControlStats()
Definition RateControlStats.h:38
std::atomic< uint16_t > minimumSlope_
Definition RateControlStats.h:64