summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/modular/encoding
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
commitfbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch)
tree4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /third_party/jpeg-xl/lib/jxl/modular/encoding
parentReleasing progress-linux version 124.0.1-1~progress7.99u1. (diff)
downloadfirefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz
firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/jpeg-xl/lib/jxl/modular/encoding')
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/context_predict.h12
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/dec_ma.cc2
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/enc_debug_tree.cc2
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/enc_encoding.cc82
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/enc_ma.cc15
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc12
6 files changed, 76 insertions, 49 deletions
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/context_predict.h b/third_party/jpeg-xl/lib/jxl/modular/encoding/context_predict.h
index 4c3a33a52a..7bec5128fc 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/context_predict.h
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/context_predict.h
@@ -10,6 +10,7 @@
#include <vector>
#include "lib/jxl/fields.h"
+#include "lib/jxl/image_ops.h"
#include "lib/jxl/modular/modular_image.h"
#include "lib/jxl/modular/options.h"
@@ -78,14 +79,14 @@ struct State {
294337, 289262, 284359, 279620, 275036, 270600, 266305, 262144};
constexpr static pixel_type_w AddBits(pixel_type_w x) {
- return uint64_t(x) << kPredExtraBits;
+ return static_cast<uint64_t>(x) << kPredExtraBits;
}
State(Header header, size_t xsize, size_t ysize) : header(header) {
// Extra margin to avoid out-of-bounds writes.
// All have space for two rows of data.
- for (size_t i = 0; i < 4; i++) {
- pred_errors[i].resize((xsize + 2) * 2);
+ for (auto &pred_error : pred_errors) {
+ pred_error.resize((xsize + 2) * 2);
}
error.resize((xsize + 2) * 2);
}
@@ -538,8 +539,9 @@ JXL_INLINE PredictionResult Predict(
}
if (mode & kAllPredictions) {
for (size_t i = 0; i < kNumModularPredictors; i++) {
- predictions[i] = PredictOne((Predictor)i, left, top, toptop, topleft,
- topright, leftleft, toprightright, wp_pred);
+ predictions[i] =
+ PredictOne(static_cast<Predictor>(i), left, top, toptop, topleft,
+ topright, leftleft, toprightright, wp_pred);
}
}
result.guess += PredictOne(predictor, left, top, toptop, topleft, topright,
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/dec_ma.cc b/third_party/jpeg-xl/lib/jxl/modular/encoding/dec_ma.cc
index ee7177bcd6..b53b9a9103 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/dec_ma.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/dec_ma.cc
@@ -5,6 +5,8 @@
#include "lib/jxl/modular/encoding/dec_ma.h"
+#include <limits>
+
#include "lib/jxl/base/printf_macros.h"
#include "lib/jxl/dec_ans.h"
#include "lib/jxl/modular/encoding/ma_common.h"
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_debug_tree.cc b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_debug_tree.cc
index bd27f28458..f863823629 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_debug_tree.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_debug_tree.cc
@@ -95,7 +95,7 @@ std::string PropertyName(size_t i) {
case 15:
return "WGH";
default:
- return "ch[" + ToString(15 - (int)i) + "]";
+ return "ch[" + ToString(15 - static_cast<int>(i)) + "]";
}
}
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_encoding.cc b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_encoding.cc
index fc2e69e4a6..84d8137d21 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_encoding.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_encoding.cc
@@ -6,35 +6,24 @@
#include <stdint.h>
#include <stdlib.h>
-#include <cinttypes>
#include <limits>
-#include <numeric>
#include <queue>
-#include <set>
-#include <unordered_map>
-#include <unordered_set>
#include "lib/jxl/base/common.h"
#include "lib/jxl/base/printf_macros.h"
#include "lib/jxl/base/status.h"
-#include "lib/jxl/dec_ans.h"
-#include "lib/jxl/dec_bit_reader.h"
#include "lib/jxl/enc_ans.h"
#include "lib/jxl/enc_aux_out.h"
#include "lib/jxl/enc_bit_writer.h"
#include "lib/jxl/enc_fields.h"
-#include "lib/jxl/entropy_coder.h"
#include "lib/jxl/fields.h"
#include "lib/jxl/image_ops.h"
#include "lib/jxl/modular/encoding/context_predict.h"
-#include "lib/jxl/modular/encoding/enc_debug_tree.h"
#include "lib/jxl/modular/encoding/enc_ma.h"
#include "lib/jxl/modular/encoding/encoding.h"
#include "lib/jxl/modular/encoding/ma_common.h"
#include "lib/jxl/modular/options.h"
-#include "lib/jxl/modular/transform/transform.h"
#include "lib/jxl/pack_signed.h"
-#include "lib/jxl/toc.h"
namespace jxl {
@@ -61,7 +50,7 @@ inline std::array<uint8_t, 3> PredictorColor(Predictor p) {
return {{255, 0, 255}};
case Predictor::Weighted:
return {{0, 255, 255}};
- // TODO
+ // TODO(jon)
default:
return {{255, 255, 255}};
};
@@ -101,17 +90,17 @@ Tree MakeFixedTree(int property, const std::vector<int32_t> &cutoffs,
} // namespace
-void GatherTreeData(const Image &image, pixel_type chan, size_t group_id,
- const weighted::Header &wp_header,
- const ModularOptions &options, TreeSamples &tree_samples,
- size_t *total_pixels) {
+Status GatherTreeData(const Image &image, pixel_type chan, size_t group_id,
+ const weighted::Header &wp_header,
+ const ModularOptions &options, TreeSamples &tree_samples,
+ size_t *total_pixels) {
const Channel &channel = image.channel[chan];
JXL_DEBUG_V(7, "Learning %" PRIuS "x%" PRIuS " channel %d", channel.w,
channel.h, chan);
std::array<pixel_type, kNumStaticProperties> static_props = {
- {chan, (int)group_id}};
+ {chan, static_cast<int>(group_id)}};
Properties properties(kNumNonrefProperties +
kExtraPropsPerChannel * options.max_properties);
double pixel_fraction = std::min(1.0f, options.nb_repeats);
@@ -137,7 +126,9 @@ void GatherTreeData(const Image &image, pixel_type chan, size_t group_id,
};
const intptr_t onerow = channel.plane.PixelsPerRow();
- Channel references(properties.size() - kNumNonrefProperties, channel.w);
+ JXL_ASSIGN_OR_RETURN(
+ Channel references,
+ Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
weighted::State wp_state(wp_header, channel.w, channel.h);
tree_samples.PrepareForSamples(pixel_fraction * channel.h * channel.w + 64);
const bool multiple_predictors = tree_samples.NumPredictors() != 1;
@@ -198,6 +189,7 @@ void GatherTreeData(const Image &image, pixel_type chan, size_t group_id,
}
}
}
+ return true;
}
Tree PredefinedTree(ModularOptions::TreeKind tree_kind, size_t total_pixels) {
@@ -316,7 +308,9 @@ Status EncodeModularChannelMAANS(const Image &image, pixel_type chan,
JXL_ASSERT(channel.w != 0 && channel.h != 0);
Image3F predictor_img;
- if (kWantDebug) predictor_img = Image3F(channel.w, channel.h);
+ if (kWantDebug) {
+ JXL_ASSIGN_OR_RETURN(predictor_img, Image3F::Create(channel.w, channel.h));
+ }
JXL_DEBUG_V(6,
"Encoding %" PRIuS "x%" PRIuS
@@ -325,8 +319,9 @@ Status EncodeModularChannelMAANS(const Image &image, pixel_type chan,
channel.w, channel.h, chan, channel.hshift, channel.vshift);
std::array<pixel_type, kNumStaticProperties> static_props = {
- {chan, (int)group_id}};
- bool use_wp, is_wp_only;
+ {chan, static_cast<int>(group_id)}};
+ bool use_wp;
+ bool is_wp_only;
bool is_gradient_only;
size_t num_props;
FlatTree tree = FilterTree(global_tree, static_props, &num_props, &use_wp,
@@ -439,7 +434,8 @@ Status EncodeModularChannelMAANS(const Image &image, pixel_type chan,
FillImage(static_cast<float>(PredictorColor(tree[0].predictor)[c]),
&predictor_img.Plane(c));
}
- uint32_t mul_shift = FloorLog2Nonzero((uint32_t)tree[0].multiplier);
+ uint32_t mul_shift =
+ FloorLog2Nonzero(static_cast<uint32_t>(tree[0].multiplier));
const intptr_t onerow = channel.plane.PixelsPerRow();
for (size_t y = 0; y < channel.h; y++) {
const pixel_type *JXL_RESTRICT r = channel.Row(y);
@@ -454,7 +450,9 @@ Status EncodeModularChannelMAANS(const Image &image, pixel_type chan,
} else if (!use_wp && !skip_encoder_fast_path) {
const intptr_t onerow = channel.plane.PixelsPerRow();
- Channel references(properties.size() - kNumNonrefProperties, channel.w);
+ JXL_ASSIGN_OR_RETURN(
+ Channel references,
+ Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
for (size_t y = 0; y < channel.h; y++) {
const pixel_type *JXL_RESTRICT p = channel.Row(y);
PrecomputeReferences(channel, y, image, chan, &references);
@@ -481,7 +479,9 @@ Status EncodeModularChannelMAANS(const Image &image, pixel_type chan,
}
} else {
const intptr_t onerow = channel.plane.PixelsPerRow();
- Channel references(properties.size() - kNumNonrefProperties, channel.w);
+ JXL_ASSIGN_OR_RETURN(
+ Channel references,
+ Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
weighted::State wp_state(wp_header, channel.w, channel.h);
for (size_t y = 0; y < channel.h; y++) {
const pixel_type *JXL_RESTRICT p = channel.Row(y);
@@ -556,8 +556,20 @@ Status ModularEncode(const Image &image, const ModularOptions &options,
TreeSamples tree_samples_storage;
size_t total_pixels_storage = 0;
if (!total_pixels) total_pixels = &total_pixels_storage;
+ if (*total_pixels == 0) {
+ for (size_t i = 0; i < nb_channels; i++) {
+ if (i >= image.nb_meta_channels &&
+ (image.channel[i].w > options.max_chan_size ||
+ image.channel[i].h > options.max_chan_size)) {
+ break;
+ }
+ *total_pixels += image.channel[i].w * image.channel[i].h;
+ }
+ *total_pixels = std::max<size_t>(*total_pixels, 1);
+ }
// If there's no tree, compute one (or gather data to).
- if (tree == nullptr) {
+ if (tree == nullptr &&
+ options.tree_kind == ModularOptions::TreeKind::kLearn) {
bool gather_data = tree_samples != nullptr;
if (tree_samples == nullptr) {
JXL_RETURN_IF_ERROR(tree_samples_storage.SetPredictor(
@@ -586,9 +598,9 @@ Status ModularEncode(const Image &image, const ModularOptions &options,
image.channel[i].h > options.max_chan_size)) {
break;
}
- GatherTreeData(image, i, group_id, header->wp_header, options,
- gather_data ? *tree_samples : tree_samples_storage,
- total_pixels);
+ JXL_RETURN_IF_ERROR(GatherTreeData(
+ image, i, group_id, header->wp_header, options,
+ gather_data ? *tree_samples : tree_samples_storage, total_pixels));
}
if (gather_data) return true;
}
@@ -609,10 +621,10 @@ Status ModularEncode(const Image &image, const ModularOptions &options,
? LearnTree(std::move(tree_samples_storage), *total_pixels, options)
: PredefinedTree(options.tree_kind, *total_pixels);
tree = &tree_storage;
- tokens = &tokens_storage[0];
+ tokens = tokens_storage.data();
Tree decoded_tree;
- TokenizeTree(*tree, &tree_tokens[0], &decoded_tree);
+ TokenizeTree(*tree, tree_tokens.data(), &decoded_tree);
JXL_ASSERT(tree->size() == decoded_tree.size());
tree_storage = std::move(decoded_tree);
@@ -622,9 +634,9 @@ Status ModularEncode(const Image &image, const ModularOptions &options,
} */
// Write tree
- BuildAndEncodeHistograms(HistogramParams(), kNumTreeContexts, tree_tokens,
- &code, &context_map, writer, kLayerModularTree,
- aux_out);
+ BuildAndEncodeHistograms(options.histogram_params, kNumTreeContexts,
+ tree_tokens, &code, &context_map, writer,
+ kLayerModularTree, aux_out);
WriteTokens(tree_tokens[0], code, context_map, 0, writer, kLayerModularTree,
aux_out);
}
@@ -669,7 +681,7 @@ Status ModularEncode(const Image &image, const ModularOptions &options,
if (!header->use_global_tree) {
EntropyEncodingData code;
std::vector<uint8_t> context_map;
- HistogramParams histo_params;
+ HistogramParams histo_params = options.histogram_params;
histo_params.image_widths.push_back(image_width);
BuildAndEncodeHistograms(histo_params, (tree->size() + 1) / 2,
tokens_storage, &code, &context_map, writer, layer,
@@ -691,7 +703,7 @@ Status ModularGenericCompress(Image &image, const ModularOptions &opts,
if (image.w == 0 || image.h == 0) return true;
ModularOptions options = opts; // Make a copy to modify it.
- if (options.predictor == static_cast<Predictor>(-1)) {
+ if (options.predictor == kUndefinedPredictor) {
options.predictor = Predictor::Gradient;
}
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_ma.cc b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_ma.cc
index ef72b2477b..de629ad038 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_ma.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/enc_ma.cc
@@ -109,8 +109,8 @@ IntersectionType BoxIntersects(StaticPropRange needle, StaticPropRange haystack,
void SplitTreeSamples(TreeSamples &tree_samples, size_t begin, size_t pos,
size_t end, size_t prop) {
auto cmp = [&](size_t a, size_t b) {
- return int32_t(tree_samples.Property(prop, a)) -
- int32_t(tree_samples.Property(prop, b));
+ return static_cast<int32_t>(tree_samples.Property(prop, a)) -
+ static_cast<int32_t>(tree_samples.Property(prop, b));
};
Rng rng(0);
while (end > begin + 1) {
@@ -243,7 +243,8 @@ void FindBestSplit(TreeSamples &tree_samples, float threshold,
// properties. We do this even if the current node is not a leaf, to
// minimize the number of nodes in the resulting tree.
for (size_t i = 0; i < mul_info.size(); i++) {
- uint32_t axis, val;
+ uint32_t axis;
+ uint32_t val;
IntersectionType t =
BoxIntersects(static_prop_range, mul_info[i].range, axis, val);
if (t == IntersectionType::kNone) continue;
@@ -696,7 +697,11 @@ void TreeSamples::Swap(size_t a, size_t b) {
}
void TreeSamples::ThreeShuffle(size_t a, size_t b, size_t c) {
- if (b == c) return Swap(a, b);
+ if (b == c) {
+ Swap(a, b);
+ return;
+ }
+
for (auto &r : residuals) {
auto tmp = r[a];
r[a] = r[c];
@@ -966,7 +971,7 @@ void CollectPixelSamples(const Image &image, const ModularOptions &options,
const pixel_type *row = image.channel[channel_ids[i]].Row(y);
pixel_samples.push_back(row[x]);
size_t xp = x == 0 ? 1 : x - 1;
- diff_samples.push_back((int64_t)row[x] - row[xp]);
+ diff_samples.push_back(static_cast<int64_t>(row[x]) - row[xp]);
}
}
diff --git a/third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc b/third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc
index a6abdcfc91..bb690b74ba 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc
@@ -14,6 +14,8 @@
#include "lib/jxl/base/scope_guard.h"
#include "lib/jxl/dec_ans.h"
#include "lib/jxl/dec_bit_reader.h"
+#include "lib/jxl/frame_dimensions.h"
+#include "lib/jxl/image_ops.h"
#include "lib/jxl/modular/encoding/context_predict.h"
#include "lib/jxl/modular/options.h"
#include "lib/jxl/pack_signed.h"
@@ -141,7 +143,7 @@ Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader,
Channel &channel = image->channel[chan];
std::array<pixel_type, kNumStaticProperties> static_props = {
- {chan, (int)group_id}};
+ {chan, static_cast<int>(group_id)}};
// TODO(veluca): filter the tree according to static_props.
// zero pixel channel? could happen
@@ -376,7 +378,9 @@ Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader,
MATreeLookup tree_lookup(tree);
Properties properties = Properties(num_props);
const intptr_t onerow = channel.plane.PixelsPerRow();
- Channel references(properties.size() - kNumNonrefProperties, channel.w);
+ JXL_ASSIGN_OR_RETURN(
+ Channel references,
+ Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
for (size_t y = 0; y < channel.h; y++) {
pixel_type *JXL_RESTRICT p = channel.Row(y);
PrecomputeReferences(channel, y, *image, chan, &references);
@@ -422,7 +426,9 @@ Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader,
MATreeLookup tree_lookup(tree);
Properties properties = Properties(num_props);
const intptr_t onerow = channel.plane.PixelsPerRow();
- Channel references(properties.size() - kNumNonrefProperties, channel.w);
+ JXL_ASSIGN_OR_RETURN(
+ Channel references,
+ Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
weighted::State wp_state(wp_header, channel.w, channel.h);
for (size_t y = 0; y < channel.h; y++) {
pixel_type *JXL_RESTRICT p = channel.Row(y);