summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/render_pipeline/stage_tone_mapping.cc
blob: e8cd90b24418784a9d84544edd1a867e92d0ca08 (plain)
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
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/render_pipeline/stage_tone_mapping.h"

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "lib/jxl/render_pipeline/stage_tone_mapping.cc"
#include <hwy/foreach_target.h>
#include <hwy/highway.h>

#include "lib/jxl/cms/tone_mapping-inl.h"
#include "lib/jxl/dec_xyb-inl.h"
#include "lib/jxl/sanitizers.h"

HWY_BEFORE_NAMESPACE();
namespace jxl {
namespace HWY_NAMESPACE {

class ToneMappingStage : public RenderPipelineStage {
 public:
  explicit ToneMappingStage(OutputEncodingInfo output_encoding_info)
      : RenderPipelineStage(RenderPipelineStage::Settings()),
        output_encoding_info_(std::move(output_encoding_info)) {
    if (output_encoding_info_.desired_intensity_target ==
        output_encoding_info_.orig_intensity_target) {
      // No tone mapping requested.
      return;
    }
    const auto& orig_tf = output_encoding_info_.orig_color_encoding.Tf();
    const auto& dest_tf = output_encoding_info_.color_encoding.Tf();
    if (orig_tf.IsPQ() && output_encoding_info_.desired_intensity_target <
                              output_encoding_info_.orig_intensity_target) {
      tone_mapper_ = jxl::make_unique<ToneMapper>(
          /*source_range=*/std::pair<float, float>(
              0, output_encoding_info_.orig_intensity_target),
          /*target_range=*/
          std::pair<float, float>(
              0, output_encoding_info_.desired_intensity_target),
          output_encoding_info_.luminances);
    } else if (orig_tf.IsHLG() && !dest_tf.IsHLG()) {
      hlg_ootf_ = jxl::make_unique<HlgOOTF>(
          /*source_luminance=*/output_encoding_info_.orig_intensity_target,
          /*target_luminance=*/output_encoding_info_.desired_intensity_target,
          output_encoding_info_.luminances);
    }

    if (dest_tf.IsPQ() && (tone_mapper_ || hlg_ootf_)) {
      to_intensity_target_ =
          10000.f / output_encoding_info_.orig_intensity_target;
      from_desired_intensity_target_ =
          output_encoding_info_.desired_intensity_target / 10000.f;
    }
  }

  bool IsNeeded() const { return tone_mapper_ || hlg_ootf_; }

  Status ProcessRow(const RowInfo& input_rows, const RowInfo& output_rows,
                    size_t xextra, size_t xsize, size_t xpos, size_t ypos,
                    size_t thread_id) const final {
    if (!(tone_mapper_ || hlg_ootf_)) return true;

    const HWY_FULL(float) d;
    const size_t xsize_v = RoundUpTo(xsize, Lanes(d));
    float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
    float* JXL_RESTRICT row1 = GetInputRow(input_rows, 1, 0);
    float* JXL_RESTRICT row2 = GetInputRow(input_rows, 2, 0);
    // All calculations are lane-wise, still some might require
    // value-dependent behaviour (e.g. NearestInt). Temporary unpoison last
    // vector tail.
    msan::UnpoisonMemory(row0 + xsize, sizeof(float) * (xsize_v - xsize));
    msan::UnpoisonMemory(row1 + xsize, sizeof(float) * (xsize_v - xsize));
    msan::UnpoisonMemory(row2 + xsize, sizeof(float) * (xsize_v - xsize));
    for (ssize_t x = -xextra; x < static_cast<ssize_t>(xsize + xextra);
         x += Lanes(d)) {
      auto r = LoadU(d, row0 + x);
      auto g = LoadU(d, row1 + x);
      auto b = LoadU(d, row2 + x);
      if (tone_mapper_ || hlg_ootf_) {
        r = Mul(r, Set(d, to_intensity_target_));
        g = Mul(g, Set(d, to_intensity_target_));
        b = Mul(b, Set(d, to_intensity_target_));
        if (tone_mapper_) {
          tone_mapper_->ToneMap(&r, &g, &b);
        } else {
          JXL_ASSERT(hlg_ootf_);
          hlg_ootf_->Apply(&r, &g, &b);
        }
        if (tone_mapper_ || hlg_ootf_->WarrantsGamutMapping()) {
          GamutMap(&r, &g, &b, output_encoding_info_.luminances);
        }
        r = Mul(r, Set(d, from_desired_intensity_target_));
        g = Mul(g, Set(d, from_desired_intensity_target_));
        b = Mul(b, Set(d, from_desired_intensity_target_));
      }
      StoreU(r, d, row0 + x);
      StoreU(g, d, row1 + x);
      StoreU(b, d, row2 + x);
    }
    msan::PoisonMemory(row0 + xsize, sizeof(float) * (xsize_v - xsize));
    msan::PoisonMemory(row1 + xsize, sizeof(float) * (xsize_v - xsize));
    msan::PoisonMemory(row2 + xsize, sizeof(float) * (xsize_v - xsize));
    return true;
  }

  RenderPipelineChannelMode GetChannelMode(size_t c) const final {
    return c < 3 ? RenderPipelineChannelMode::kInPlace
                 : RenderPipelineChannelMode::kIgnored;
  }

  const char* GetName() const override { return "ToneMapping"; }

 private:
  using ToneMapper = Rec2408ToneMapper<HWY_FULL(float)>;
  OutputEncodingInfo output_encoding_info_;
  std::unique_ptr<ToneMapper> tone_mapper_;
  std::unique_ptr<HlgOOTF> hlg_ootf_;
  // When the target colorspace is PQ, 1 represents 10000 nits instead of
  // orig_intensity_target. This temporarily changes this if the tone mappers
  // require it.
  float to_intensity_target_ = 1.f;
  float from_desired_intensity_target_ = 1.f;
};

std::unique_ptr<RenderPipelineStage> GetToneMappingStage(
    const OutputEncodingInfo& output_encoding_info) {
  auto stage = jxl::make_unique<ToneMappingStage>(output_encoding_info);
  if (!stage->IsNeeded()) return nullptr;
  return stage;
}

// NOLINTNEXTLINE(google-readability-namespace-comments)
}  // namespace HWY_NAMESPACE
}  // namespace jxl
HWY_AFTER_NAMESPACE();

#if HWY_ONCE
namespace jxl {

HWY_EXPORT(GetToneMappingStage);

std::unique_ptr<RenderPipelineStage> GetToneMappingStage(
    const OutputEncodingInfo& output_encoding_info) {
  return HWY_DYNAMIC_DISPATCH(GetToneMappingStage)(output_encoding_info);
}

}  // namespace jxl
#endif