Coverage Report

Created: 2024-09-08 07:14

/src/libjxl/lib/jxl/encode.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include <brotli/encode.h>
7
#include <jxl/cms.h>
8
#include <jxl/codestream_header.h>
9
#include <jxl/encode.h>
10
#include <jxl/memory_manager.h>
11
#include <jxl/types.h>
12
#include <jxl/version.h>
13
14
#include <algorithm>
15
#include <atomic>
16
#include <cstddef>
17
#include <cstdint>
18
#include <cstring>
19
#include <utility>
20
21
#include "lib/jxl/base/byte_order.h"
22
#include "lib/jxl/base/common.h"
23
#include "lib/jxl/base/compiler_specific.h"
24
#include "lib/jxl/base/data_parallel.h"
25
#include "lib/jxl/base/exif.h"
26
#include "lib/jxl/base/printf_macros.h"
27
#include "lib/jxl/base/sanitizers.h"
28
#include "lib/jxl/base/span.h"
29
#include "lib/jxl/base/status.h"
30
#include "lib/jxl/codec_in_out.h"
31
#include "lib/jxl/enc_aux_out.h"
32
#include "lib/jxl/enc_bit_writer.h"
33
#include "lib/jxl/enc_cache.h"
34
#include "lib/jxl/enc_fast_lossless.h"
35
#include "lib/jxl/enc_fields.h"
36
#include "lib/jxl/enc_frame.h"
37
#include "lib/jxl/enc_icc_codec.h"
38
#include "lib/jxl/enc_params.h"
39
#include "lib/jxl/encode_internal.h"
40
#include "lib/jxl/jpeg/enc_jpeg_data.h"
41
#include "lib/jxl/luminance.h"
42
#include "lib/jxl/memory_manager_internal.h"
43
#include "lib/jxl/padded_bytes.h"
44
45
struct JxlErrorOrStatus {
46
  // NOLINTNEXTLINE(google-explicit-constructor)
47
0
  operator jxl::Status() const {
48
0
    switch (error_) {
49
0
      case JXL_ENC_SUCCESS:
50
0
        return jxl::OkStatus();
51
0
      case JXL_ENC_NEED_MORE_OUTPUT:
52
0
        return jxl::StatusCode::kNotEnoughBytes;
53
0
      default:
54
0
        return jxl::StatusCode::kGenericError;
55
0
    }
56
0
  }
57
  // NOLINTNEXTLINE(google-explicit-constructor)
58
2.61k
  operator JxlEncoderStatus() const { return error_; }
59
60
2.38k
  static JxlErrorOrStatus Success() {
61
2.38k
    return JxlErrorOrStatus(JXL_ENC_SUCCESS);
62
2.38k
  }
63
64
238
  static JxlErrorOrStatus MoreOutput() {
65
238
    return JxlErrorOrStatus(JXL_ENC_NEED_MORE_OUTPUT);
66
238
  }
67
68
0
  static JxlErrorOrStatus Error() { return JxlErrorOrStatus(JXL_ENC_ERROR); }
69
70
 private:
71
2.61k
  explicit JxlErrorOrStatus(JxlEncoderStatus error) : error_(error) {}
72
  JxlEncoderStatus error_;
73
};
74
75
// Debug-printing failure macro similar to JXL_FAILURE, but for the status code
76
// JXL_ENC_ERROR
77
#if (JXL_CRASH_ON_ERROR)
78
#define JXL_API_ERROR(enc, error_code, format, ...)                          \
79
  (enc->error = error_code,                                                  \
80
   ::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__), \
81
   ::jxl::Abort(), JxlErrorOrStatus::Error())
82
#define JXL_API_ERROR_NOSET(format, ...)                                     \
83
  (::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__), \
84
   ::jxl::Abort(), JxlErrorOrStatus::Error())
85
#else  // JXL_CRASH_ON_ERROR
86
#define JXL_API_ERROR(enc, error_code, format, ...)                            \
87
0
  (enc->error = error_code,                                                    \
88
0
   ((JXL_IS_DEBUG_BUILD) &&                                                    \
89
0
    ::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__)), \
90
0
   JxlErrorOrStatus::Error())
91
#define JXL_API_ERROR_NOSET(format, ...)                                     \
92
0
  (::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__), \
93
0
   JxlErrorOrStatus::Error())
94
#endif  // JXL_CRASH_ON_ERROR
95
96
jxl::StatusOr<JxlOutputProcessorBuffer>
97
JxlEncoderOutputProcessorWrapper::GetBuffer(size_t min_size,
98
714
                                            size_t requested_size) {
99
714
  JXL_ENSURE(min_size > 0);
100
714
  JXL_ENSURE(!has_buffer_);
101
714
  if (stop_requested_) return jxl::StatusCode::kNotEnoughBytes;
102
714
  requested_size = std::max(min_size, requested_size);
103
104
  // If we support seeking, output_position_ == position_.
105
714
  if (external_output_processor_ && external_output_processor_->seek) {
106
0
    JXL_ENSURE(output_position_ == position_);
107
0
  }
108
  // Otherwise, output_position_ <= position_.
109
714
  JXL_ENSURE(output_position_ <= position_);
110
714
  size_t additional_size = position_ - output_position_;
111
714
  JXL_ENSURE(memory_manager_ != nullptr);
112
113
714
  if (external_output_processor_) {
114
    // TODO(veluca): here, we cannot just ask for a larger buffer, as it will be
115
    // released with a prefix of the buffer that has not been written yet.
116
    // Figure out if there is a good way to do this more efficiently.
117
0
    if (additional_size == 0) {
118
0
      size_t size = requested_size;
119
0
      uint8_t* user_buffer =
120
0
          static_cast<uint8_t*>(external_output_processor_->get_buffer(
121
0
              external_output_processor_->opaque, &size));
122
0
      if (size == 0 || user_buffer == nullptr) {
123
0
        stop_requested_ = true;
124
0
        return jxl::StatusCode::kNotEnoughBytes;
125
0
      }
126
0
      if (size < min_size) {
127
0
        external_output_processor_->release_buffer(
128
0
            external_output_processor_->opaque, 0);
129
0
      } else {
130
0
        internal_buffers_.emplace(position_, InternalBuffer(memory_manager_));
131
0
        has_buffer_ = true;
132
0
        return JxlOutputProcessorBuffer(user_buffer, size, 0, this);
133
0
      }
134
0
    }
135
714
  } else {
136
714
    if (min_size + additional_size < *avail_out_) {
137
476
      internal_buffers_.emplace(position_, InternalBuffer(memory_manager_));
138
476
      has_buffer_ = true;
139
476
      return JxlOutputProcessorBuffer(*next_out_ + additional_size,
140
476
                                      *avail_out_ - additional_size, 0, this);
141
476
    }
142
714
  }
143
144
  // Otherwise, we need to allocate our own buffer.
145
238
  auto it =
146
238
      internal_buffers_.emplace(position_, InternalBuffer(memory_manager_))
147
238
          .first;
148
238
  InternalBuffer& buffer = it->second;
149
238
  size_t alloc_size = requested_size;
150
238
  it++;
151
238
  if (it != internal_buffers_.end()) {
152
0
    alloc_size = std::min(alloc_size, it->first - position_);
153
0
    JXL_ENSURE(alloc_size >= min_size);
154
0
  }
155
238
  JXL_ENSURE(buffer.owned_data.resize(alloc_size));
156
238
  has_buffer_ = true;
157
238
  return JxlOutputProcessorBuffer(buffer.owned_data.data(), alloc_size, 0,
158
238
                                  this);
159
238
}
160
161
0
jxl::Status JxlEncoderOutputProcessorWrapper::Seek(size_t pos) {
162
0
  JXL_ENSURE(!has_buffer_);
163
0
  if (external_output_processor_ && external_output_processor_->seek) {
164
0
    external_output_processor_->seek(external_output_processor_->opaque, pos);
165
0
    output_position_ = pos;
166
0
  }
167
0
  JXL_ENSURE(pos >= finalized_position_);
168
0
  position_ = pos;
169
0
  return true;
170
0
}
171
172
476
jxl::Status JxlEncoderOutputProcessorWrapper::SetFinalizedPosition() {
173
476
  JXL_ENSURE(!has_buffer_);
174
476
  if (external_output_processor_ && external_output_processor_->seek) {
175
0
    external_output_processor_->set_finalized_position(
176
0
        external_output_processor_->opaque, position_);
177
0
  }
178
476
  finalized_position_ = position_;
179
476
  JXL_RETURN_IF_ERROR(FlushOutput());
180
476
  return true;
181
476
}
182
183
jxl::Status JxlEncoderOutputProcessorWrapper::SetAvailOut(uint8_t** next_out,
184
476
                                                          size_t* avail_out) {
185
476
  JXL_ENSURE(!external_output_processor_);
186
476
  avail_out_ = avail_out;
187
476
  next_out_ = next_out;
188
476
  JXL_RETURN_IF_ERROR(FlushOutput());
189
476
  return true;
190
476
}
191
192
jxl::Status JxlEncoderOutputProcessorWrapper::CopyOutput(
193
0
    std::vector<uint8_t>& output, uint8_t* next_out, size_t& avail_out) {
194
0
  while (HasOutputToWrite()) {
195
0
    JXL_RETURN_IF_ERROR(SetAvailOut(&next_out, &avail_out));
196
0
    if (avail_out == 0) {
197
0
      size_t offset = next_out - output.data();
198
0
      output.resize(output.size() * 2);
199
0
      next_out = output.data() + offset;
200
0
      avail_out = output.size() - offset;
201
0
    }
202
0
  }
203
0
  output.resize(output.size() - avail_out);
204
0
  return true;
205
0
}
206
207
714
jxl::Status JxlEncoderOutputProcessorWrapper::ReleaseBuffer(size_t bytes_used) {
208
714
  JXL_ENSURE(has_buffer_);
209
714
  has_buffer_ = false;
210
714
  auto it = internal_buffers_.find(position_);
211
714
  JXL_ENSURE(it != internal_buffers_.end());
212
714
  if (bytes_used == 0) {
213
0
    if (external_output_processor_) {
214
0
      external_output_processor_->release_buffer(
215
0
          external_output_processor_->opaque, bytes_used);
216
0
    }
217
0
    internal_buffers_.erase(it);
218
0
    return true;
219
0
  }
220
714
  it->second.written_bytes = bytes_used;
221
714
  position_ += bytes_used;
222
223
714
  auto it_to_next = it;
224
714
  it_to_next++;
225
714
  if (it_to_next != internal_buffers_.end()) {
226
0
    JXL_ENSURE(it_to_next->first >= position_);
227
0
  }
228
229
714
  if (external_output_processor_) {
230
    // If the buffer was given by the user, tell the user it is not needed
231
    // anymore.
232
0
    if (it->second.owned_data.empty()) {
233
0
      external_output_processor_->release_buffer(
234
0
          external_output_processor_->opaque, bytes_used);
235
      // If we don't support seeking, this implies we will never modify again
236
      // the bytes that were written so far. Advance the finalized position and
237
      // flush the output to clean up the internal buffers.
238
0
      if (!external_output_processor_->seek) {
239
0
        JXL_RETURN_IF_ERROR(SetFinalizedPosition());
240
0
        JXL_ENSURE(output_position_ == finalized_position_);
241
0
        JXL_ENSURE(output_position_ == position_);
242
0
      } else {
243
        // Otherwise, advance the output position accordingly.
244
0
        output_position_ += bytes_used;
245
0
        JXL_ENSURE(output_position_ >= finalized_position_);
246
0
        JXL_ENSURE(output_position_ == position_);
247
0
      }
248
0
    } else if (external_output_processor_->seek) {
249
      // If we had buffered the data internally, flush it out to the external
250
      // processor if we can.
251
0
      external_output_processor_->seek(external_output_processor_->opaque,
252
0
                                       position_ - bytes_used);
253
0
      output_position_ = position_ - bytes_used;
254
0
      while (output_position_ < position_) {
255
0
        size_t num_to_write = position_ - output_position_;
256
0
        if (!AppendBufferToExternalProcessor(it->second.owned_data.data() +
257
0
                                                 output_position_ - position_ +
258
0
                                                 bytes_used,
259
0
                                             num_to_write)) {
260
0
          return true;
261
0
        }
262
0
      }
263
0
      it->second.owned_data.clear();
264
0
    }
265
0
  }
266
714
  return true;
267
714
}
268
269
// Tries to write all the bytes up to the finalized position.
270
952
jxl::Status JxlEncoderOutputProcessorWrapper::FlushOutput() {
271
952
  JXL_ENSURE(!has_buffer_);
272
1.66k
  while (output_position_ < finalized_position_ &&
273
1.66k
         (avail_out_ == nullptr || *avail_out_ > 0)) {
274
714
    JXL_ENSURE(!internal_buffers_.empty());
275
714
    auto it = internal_buffers_.begin();
276
    // If this fails, we are trying to move the finalized position past data
277
    // that was not written yet. This is a library programming error.
278
714
    JXL_ENSURE(output_position_ >= it->first);
279
714
    JXL_ENSURE(it->second.written_bytes != 0);
280
714
    size_t buffer_last_byte = it->first + it->second.written_bytes;
281
714
    if (!it->second.owned_data.empty()) {
282
238
      size_t start_in_buffer = output_position_ - it->first;
283
      // Guaranteed by the invariant on `internal_buffers_`.
284
238
      JXL_ENSURE(buffer_last_byte > output_position_);
285
238
      size_t num_to_write =
286
238
          std::min(buffer_last_byte, finalized_position_) - output_position_;
287
238
      if (avail_out_ != nullptr) {
288
238
        size_t n = std::min(num_to_write, *avail_out_);
289
238
        memcpy(*next_out_, it->second.owned_data.data() + start_in_buffer, n);
290
238
        *avail_out_ -= n;
291
238
        *next_out_ += n;
292
238
        output_position_ += n;
293
238
      } else {
294
0
        JXL_ENSURE(external_output_processor_);
295
0
        if (!AppendBufferToExternalProcessor(
296
0
                it->second.owned_data.data() + start_in_buffer, num_to_write)) {
297
0
          return true;
298
0
        }
299
0
      }
300
476
    } else {
301
476
      size_t advance =
302
476
          std::min(buffer_last_byte, finalized_position_) - output_position_;
303
476
      output_position_ += advance;
304
476
      if (avail_out_ != nullptr) {
305
476
        *next_out_ += advance;
306
476
        *avail_out_ -= advance;
307
476
      }
308
476
    }
309
714
    if (buffer_last_byte == output_position_) {
310
714
      internal_buffers_.erase(it);
311
714
    }
312
714
    if (external_output_processor_ && !external_output_processor_->seek) {
313
0
      external_output_processor_->set_finalized_position(
314
0
          external_output_processor_->opaque, output_position_);
315
0
    }
316
714
  }
317
952
  return true;
318
952
}
319
320
bool JxlEncoderOutputProcessorWrapper::AppendBufferToExternalProcessor(
321
0
    void* data, size_t count) {
322
0
  JXL_DASSERT(external_output_processor_);
323
0
  size_t n = count;
324
0
  void* user_buffer = external_output_processor_->get_buffer(
325
0
      external_output_processor_->opaque, &n);
326
0
  if (user_buffer == nullptr || n == 0) {
327
0
    stop_requested_ = true;
328
0
    return false;
329
0
  }
330
0
  n = std::min(n, count);
331
0
  memcpy(user_buffer, data, n);
332
0
  external_output_processor_->release_buffer(external_output_processor_->opaque,
333
0
                                             n);
334
0
  output_position_ += n;
335
0
  return true;
336
0
}
337
338
namespace jxl {
339
340
size_t WriteBoxHeader(const jxl::BoxType& type, size_t size, bool unbounded,
341
0
                      bool force_large_box, uint8_t* output) {
342
0
  uint64_t box_size = 0;
343
0
  bool large_size = false;
344
0
  if (!unbounded) {
345
0
    if (box_size >= kLargeBoxContentSizeThreshold || force_large_box) {
346
0
      large_size = true;
347
      // TODO(firsching): send a separate CL for this (+ test),
348
      // quick fix in the old code: box_size += 8
349
0
      box_size = size + kLargeBoxHeaderSize;
350
0
    } else {
351
0
      box_size = size + kSmallBoxHeaderSize;
352
0
    }
353
0
  }
354
355
0
  size_t idx = 0;
356
0
  {
357
0
    const uint64_t store = large_size ? 1 : box_size;
358
0
    for (size_t i = 0; i < 4; i++) {
359
0
      output[idx++] = store >> (8 * (3 - i)) & 0xff;
360
0
    }
361
0
  }
362
0
  for (size_t i = 0; i < 4; i++) {
363
0
    output[idx++] = type[i];
364
0
  }
365
366
0
  if (large_size) {
367
0
    for (size_t i = 0; i < 8; i++) {
368
0
      output[idx++] = box_size >> (8 * (7 - i)) & 0xff;
369
0
    }
370
0
  }
371
0
  return idx;
372
0
}
373
}  // namespace jxl
374
375
template <typename WriteBox>
376
jxl::Status JxlEncoderStruct::AppendBox(const jxl::BoxType& type,
377
                                        bool unbounded, size_t box_max_size,
378
0
                                        const WriteBox& write_box) {
379
0
  size_t current_position = output_processor.CurrentPosition();
380
0
  bool large_box = false;
381
0
  size_t box_header_size = 0;
382
0
  if (box_max_size >= jxl::kLargeBoxContentSizeThreshold && !unbounded) {
383
0
    box_header_size = jxl::kLargeBoxHeaderSize;
384
0
    large_box = true;
385
0
  } else {
386
0
    box_header_size = jxl::kSmallBoxHeaderSize;
387
0
  }
388
0
  JXL_RETURN_IF_ERROR(
389
0
      output_processor.Seek(current_position + box_header_size));
390
0
  size_t box_contents_start = output_processor.CurrentPosition();
391
0
  JXL_RETURN_IF_ERROR(write_box());
392
0
  size_t box_contents_end = output_processor.CurrentPosition();
393
0
  JXL_RETURN_IF_ERROR(output_processor.Seek(current_position));
394
0
  JXL_ENSURE(box_contents_end >= box_contents_start);
395
0
  if (box_contents_end - box_contents_start > box_max_size) {
396
0
    return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
397
0
                         "Internal error: upper bound on box size was "
398
0
                         "violated, upper bound: %" PRIuS ", actual: %" PRIuS,
399
0
                         box_max_size, box_contents_end - box_contents_start);
400
0
  }
401
  // We need to release the buffer before Seek.
402
0
  {
403
0
    JXL_ASSIGN_OR_RETURN(
404
0
        auto buffer,
405
0
        output_processor.GetBuffer(box_contents_start - current_position));
406
0
    const size_t n =
407
0
        jxl::WriteBoxHeader(type, box_contents_end - box_contents_start,
408
0
                            unbounded, large_box, buffer.data());
409
0
    JXL_ENSURE(n == box_header_size);
410
0
    JXL_RETURN_IF_ERROR(buffer.advance(n));
411
0
  }
412
0
  JXL_RETURN_IF_ERROR(output_processor.Seek(box_contents_end));
413
0
  JXL_RETURN_IF_ERROR(output_processor.SetFinalizedPosition());
414
0
  return jxl::OkStatus();
415
0
}
Unexecuted instantiation: encode.cc:jxl::Status JxlEncoderStruct::AppendBox<JxlEncoderStruct::ProcessOneEnqueuedInput()::$_1>(std::__1::array<unsigned char, 4ul> const&, bool, unsigned long, JxlEncoderStruct::ProcessOneEnqueuedInput()::$_1 const&)
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBox<JxlEncoderStruct::AppendBoxWithContents<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(std::__1::array<unsigned char, 4ul> const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)::{lambda()#1}>(std::__1::array<unsigned char, 4ul> const&, bool, unsigned long, JxlEncoderStruct::AppendBoxWithContents<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(std::__1::array<unsigned char, 4ul> const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)::{lambda()#1} const&)
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBox<JxlEncoderStruct::AppendBoxWithContents<jxl::Span<unsigned char const> >(std::__1::array<unsigned char, 4ul> const&, jxl::Span<unsigned char const> const&)::{lambda()#1}>(std::__1::array<unsigned char, 4ul> const&, bool, unsigned long, JxlEncoderStruct::AppendBoxWithContents<jxl::Span<unsigned char const> >(std::__1::array<unsigned char, 4ul> const&, jxl::Span<unsigned char const> const&)::{lambda()#1} const&)
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBox<JxlEncoderStruct::AppendBoxWithContents<jxl::PaddedBytes>(std::__1::array<unsigned char, 4ul> const&, jxl::PaddedBytes const&)::{lambda()#1}>(std::__1::array<unsigned char, 4ul> const&, bool, unsigned long, JxlEncoderStruct::AppendBoxWithContents<jxl::PaddedBytes>(std::__1::array<unsigned char, 4ul> const&, jxl::PaddedBytes const&)::{lambda()#1} const&)
416
417
template <typename BoxContents>
418
jxl::Status JxlEncoderStruct::AppendBoxWithContents(
419
0
    const jxl::BoxType& type, const BoxContents& contents) {
420
0
  size_t size = std::end(contents) - std::begin(contents);
421
0
  return AppendBox(type, /*unbounded=*/false, size,
422
0
                   [&]() { return AppendData(output_processor, contents); });
Unexecuted instantiation: JxlEncoderStruct::AppendBoxWithContents<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(std::__1::array<unsigned char, 4ul> const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: JxlEncoderStruct::AppendBoxWithContents<jxl::Span<unsigned char const> >(std::__1::array<unsigned char, 4ul> const&, jxl::Span<unsigned char const> const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: JxlEncoderStruct::AppendBoxWithContents<jxl::PaddedBytes>(std::__1::array<unsigned char, 4ul> const&, jxl::PaddedBytes const&)::{lambda()#1}::operator()() const
423
0
}
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBoxWithContents<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(std::__1::array<unsigned char, 4ul> const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBoxWithContents<jxl::Span<unsigned char const> >(std::__1::array<unsigned char, 4ul> const&, jxl::Span<unsigned char const> const&)
Unexecuted instantiation: jxl::Status JxlEncoderStruct::AppendBoxWithContents<jxl::PaddedBytes>(std::__1::array<unsigned char, 4ul> const&, jxl::PaddedBytes const&)
424
425
0
uint32_t JxlEncoderVersion(void) {
426
0
  return JPEGXL_MAJOR_VERSION * 1000000 + JPEGXL_MINOR_VERSION * 1000 +
427
0
         JPEGXL_PATCH_VERSION;
428
0
}
429
430
namespace {
431
432
0
void WriteJxlpBoxCounter(uint32_t counter, bool last, uint8_t* buffer) {
433
0
  if (last) counter |= 0x80000000;
434
0
  for (size_t i = 0; i < 4; i++) {
435
0
    buffer[i] = counter >> (8 * (3 - i)) & 0xff;
436
0
  }
437
0
}
438
439
jxl::Status WriteJxlpBoxCounter(uint32_t counter, bool last,
440
0
                                JxlOutputProcessorBuffer& buffer) {
441
0
  uint8_t buf[4];
442
0
  WriteJxlpBoxCounter(counter, last, buf);
443
0
  JXL_RETURN_IF_ERROR(buffer.append(buf, 4));
444
0
  return true;
445
0
}
446
447
void QueueFrame(
448
    const JxlEncoderFrameSettings* frame_settings,
449
238
    jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedFrame>& frame) {
450
238
  if (frame_settings->values.lossless) {
451
0
    frame->option_values.cparams.SetLossless();
452
0
  }
453
454
238
  jxl::JxlEncoderQueuedInput queued_input(frame_settings->enc->memory_manager);
455
238
  queued_input.frame = std::move(frame);
456
238
  frame_settings->enc->input_queue.emplace_back(std::move(queued_input));
457
238
  frame_settings->enc->num_queued_frames++;
458
238
}
459
460
void QueueFastLosslessFrame(const JxlEncoderFrameSettings* frame_settings,
461
0
                            JxlFastLosslessFrameState* fast_lossless_frame) {
462
0
  jxl::JxlEncoderQueuedInput queued_input(frame_settings->enc->memory_manager);
463
0
  queued_input.fast_lossless_frame.reset(fast_lossless_frame);
464
0
  frame_settings->enc->input_queue.emplace_back(std::move(queued_input));
465
0
  frame_settings->enc->num_queued_frames++;
466
0
}
467
468
void QueueBox(JxlEncoder* enc,
469
0
              jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedBox>& box) {
470
0
  jxl::JxlEncoderQueuedInput queued_input(enc->memory_manager);
471
0
  queued_input.box = std::move(box);
472
0
  enc->input_queue.emplace_back(std::move(queued_input));
473
0
  enc->num_queued_boxes++;
474
0
}
475
476
// TODO(lode): share this code and the Brotli compression code in enc_jpeg_data
477
JxlEncoderStatus BrotliCompress(int quality, const uint8_t* in, size_t in_size,
478
0
                                jxl::PaddedBytes* out) {
479
0
  JxlMemoryManager* memory_manager = out->memory_manager();
480
0
  std::unique_ptr<BrotliEncoderState, decltype(BrotliEncoderDestroyInstance)*>
481
0
      enc(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr),
482
0
          BrotliEncoderDestroyInstance);
483
0
  if (!enc) return JXL_API_ERROR_NOSET("BrotliEncoderCreateInstance failed");
484
485
0
  BrotliEncoderSetParameter(enc.get(), BROTLI_PARAM_QUALITY, quality);
486
0
  BrotliEncoderSetParameter(enc.get(), BROTLI_PARAM_SIZE_HINT, in_size);
487
488
0
  constexpr size_t kBufferSize = 128 * 1024;
489
0
#define QUIT(message) return JXL_API_ERROR_NOSET(message)
490
0
  JXL_ASSIGN_OR_QUIT(
491
0
      jxl::PaddedBytes temp_buffer,
492
0
      jxl::PaddedBytes::WithInitialSpace(memory_manager, kBufferSize),
493
0
      "Initialization of PaddedBytes failed");
494
0
#undef QUIT
495
496
0
  size_t avail_in = in_size;
497
0
  const uint8_t* next_in = in;
498
499
0
  size_t total_out = 0;
500
501
0
  for (;;) {
502
0
    size_t avail_out = kBufferSize;
503
0
    uint8_t* next_out = temp_buffer.data();
504
0
    jxl::msan::MemoryIsInitialized(next_in, avail_in);
505
0
    if (!BrotliEncoderCompressStream(enc.get(), BROTLI_OPERATION_FINISH,
506
0
                                     &avail_in, &next_in, &avail_out, &next_out,
507
0
                                     &total_out)) {
508
0
      return JXL_API_ERROR_NOSET("Brotli compression failed");
509
0
    }
510
0
    size_t out_size = next_out - temp_buffer.data();
511
0
    jxl::msan::UnpoisonMemory(next_out - out_size, out_size);
512
0
    if (!out->resize(out->size() + out_size)) {
513
0
      return JXL_API_ERROR_NOSET("resizing of PaddedBytes failed");
514
0
    }
515
516
0
    memcpy(out->data() + out->size() - out_size, temp_buffer.data(), out_size);
517
0
    if (BrotliEncoderIsFinished(enc.get())) break;
518
0
  }
519
520
0
  return JxlErrorOrStatus::Success();
521
0
}
522
523
// The JXL codestream can have level 5 or level 10. Levels have certain
524
// restrictions such as max allowed image dimensions. This function checks the
525
// level required to support the current encoder settings. The debug_string is
526
// intended to be used for developer API error messages, and may be set to
527
// nullptr.
528
476
int VerifyLevelSettings(const JxlEncoder* enc, std::string* debug_string) {
529
476
  const auto& m = enc->metadata.m;
530
531
476
  uint64_t xsize = enc->metadata.size.xsize();
532
476
  uint64_t ysize = enc->metadata.size.ysize();
533
  // The uncompressed ICC size, if it is used.
534
476
  size_t icc_size = 0;
535
476
  if (m.color_encoding.WantICC()) {
536
0
    icc_size = m.color_encoding.ICC().size();
537
0
  }
538
539
  // Level 10 checks
540
541
476
  if (xsize > (1ull << 30ull) || ysize > (1ull << 30ull) ||
542
476
      xsize * ysize > (1ull << 40ull)) {
543
0
    if (debug_string) *debug_string = "Too large image dimensions";
544
0
    return -1;
545
0
  }
546
476
  if (icc_size > (1ull << 28)) {
547
0
    if (debug_string) *debug_string = "Too large ICC profile size";
548
0
    return -1;
549
0
  }
550
476
  if (m.num_extra_channels > 256) {
551
0
    if (debug_string) *debug_string = "Too many extra channels";
552
0
    return -1;
553
0
  }
554
555
  // Level 5 checks
556
557
476
  if (!m.modular_16_bit_buffer_sufficient) {
558
0
    if (debug_string) *debug_string = "Too high modular bit depth";
559
0
    return 10;
560
0
  }
561
476
  if (xsize > (1ull << 18ull) || ysize > (1ull << 18ull) ||
562
476
      xsize * ysize > (1ull << 28ull)) {
563
0
    if (debug_string) *debug_string = "Too large image dimensions";
564
0
    return 10;
565
0
  }
566
476
  if (icc_size > (1ull << 22)) {
567
0
    if (debug_string) *debug_string = "Too large ICC profile";
568
0
    return 10;
569
0
  }
570
476
  if (m.num_extra_channels > 4) {
571
0
    if (debug_string) *debug_string = "Too many extra channels";
572
0
    return 10;
573
0
  }
574
476
  for (const auto& eci : m.extra_channel_info) {
575
0
    if (eci.type == jxl::ExtraChannel::kBlack) {
576
0
      if (debug_string) *debug_string = "CMYK channel not allowed";
577
0
      return 10;
578
0
    }
579
0
  }
580
581
  // TODO(lode): also need to check if consecutive composite-still frames total
582
  // pixel amount doesn't exceed 2**28 in the case of level 5. This should be
583
  // done when adding frame and requires ability to add composite still frames
584
  // to be added first.
585
586
  // TODO(lode): also need to check animation duration of a frame. This should
587
  // be done when adding frame, but first requires implementing setting the
588
  // JxlFrameHeader for a frame.
589
590
  // TODO(lode): also need to check properties such as num_splines, num_patches,
591
  // modular_16bit_buffers and multiple properties of modular trees. However
592
  // these are not user-set properties so cannot be checked here, but decisions
593
  // the C++ encoder should be able to make based on the level.
594
595
  // All level 5 checks passes, so can return the more compatible level 5
596
476
  return 5;
597
476
}
598
599
JxlEncoderStatus CheckValidBitdepth(uint32_t bits_per_sample,
600
238
                                    uint32_t exponent_bits_per_sample) {
601
238
  if (!exponent_bits_per_sample) {
602
    // The spec allows up to 31 for bits_per_sample here, but
603
    // the code does not (yet) support it.
604
238
    if (!(bits_per_sample > 0 && bits_per_sample <= 24)) {
605
0
      return JXL_API_ERROR_NOSET("Invalid value for bits_per_sample");
606
0
    }
607
238
  } else if ((exponent_bits_per_sample > 8) ||
608
0
             (bits_per_sample > 24 + exponent_bits_per_sample) ||
609
0
             (bits_per_sample < 3 + exponent_bits_per_sample)) {
610
0
    return JXL_API_ERROR_NOSET(
611
0
        "Invalid float description: bits per sample = %u, exp bits = %u",
612
0
        bits_per_sample, exponent_bits_per_sample);
613
0
  }
614
238
  return JxlErrorOrStatus::Success();
615
238
}
616
617
JxlEncoderStatus VerifyInputBitDepth(JxlBitDepth bit_depth,
618
238
                                     JxlPixelFormat format) {
619
238
  return JxlErrorOrStatus::Success();
620
238
}
621
622
inline bool EncodeVarInt(uint64_t value, size_t output_size, size_t* output_pos,
623
0
                         uint8_t* output) {
624
  // While more than 7 bits of data are left,
625
  // store 7 bits and set the next byte flag
626
0
  while (value > 127) {
627
    // TODO(eustas): should it be `>=` ?
628
0
    if (*output_pos > output_size) return false;
629
    // |128: Set the next byte flag
630
0
    output[(*output_pos)++] = (static_cast<uint8_t>(value & 127)) | 128;
631
    // Remove the seven bits we just wrote
632
0
    value >>= 7;
633
0
  }
634
  // TODO(eustas): should it be `>=` ?
635
0
  if (*output_pos > output_size) return false;
636
0
  output[(*output_pos)++] = static_cast<uint8_t>(value & 127);
637
0
  return true;
638
0
}
639
640
bool EncodeFrameIndexBox(const jxl::JxlEncoderFrameIndexBox& frame_index_box,
641
0
                         std::vector<uint8_t>& buffer_vec) {
642
0
  bool ok = true;
643
0
  int NF = 0;
644
0
  for (size_t i = 0; i < frame_index_box.entries.size(); ++i) {
645
0
    if (i == 0 || frame_index_box.entries[i].to_be_indexed) {
646
0
      ++NF;
647
0
    }
648
0
  }
649
  // Frame index box contents varint + 8 bytes
650
  // continue with NF * 3 * varint
651
  // varint max length is 10 for 64 bit numbers, and these numbers
652
  // are limited to 63 bits.
653
0
  static const int kVarintMaxLength = 10;
654
0
  static const int kFrameIndexBoxHeaderLength = kVarintMaxLength + 8;
655
0
  static const int kFrameIndexBoxElementLength = 3 * kVarintMaxLength;
656
0
  const int buffer_size =
657
0
      kFrameIndexBoxHeaderLength + NF * kFrameIndexBoxElementLength;
658
0
  buffer_vec.resize(buffer_size);
659
0
  uint8_t* buffer = buffer_vec.data();
660
0
  size_t output_pos = 0;
661
0
  ok &= EncodeVarInt(NF, buffer_vec.size(), &output_pos, buffer);
662
0
  StoreBE32(frame_index_box.TNUM, &buffer[output_pos]);
663
0
  output_pos += 4;
664
0
  StoreBE32(frame_index_box.TDEN, &buffer[output_pos]);
665
0
  output_pos += 4;
666
  // When we record a frame in the index, the record needs to know
667
  // how many frames until the next indexed frame. That is why
668
  // we store the 'prev' record. That 'prev' record needs to store
669
  // the offset byte position to previously recorded indexed frame,
670
  // that's why we also trace previous to the previous frame.
671
0
  int prev_prev_ix = -1;  // For position offset (OFFi) delta coding.
672
0
  int prev_ix = 0;
673
0
  int T_prev = 0;
674
0
  int T = 0;
675
0
  for (size_t i = 1; i < frame_index_box.entries.size(); ++i) {
676
0
    if (frame_index_box.entries[i].to_be_indexed) {
677
      // Now we can record the previous entry, since we need to store
678
      // there how many frames until the next one.
679
0
      int64_t OFFi = frame_index_box.entries[prev_ix].OFFi;
680
0
      if (prev_prev_ix != -1) {
681
        // Offi needs to be offset of start byte of this frame compared to start
682
        // byte of previous frame from this index in the JPEG XL codestream. For
683
        // the first frame, this is the offset from the first byte of the JPEG
684
        // XL codestream.
685
0
        OFFi -= frame_index_box.entries[prev_prev_ix].OFFi;
686
0
      }
687
0
      int32_t Ti = T_prev;
688
0
      int32_t Fi = i - prev_ix;
689
0
      ok &= EncodeVarInt(OFFi, buffer_vec.size(), &output_pos, buffer);
690
0
      ok &= EncodeVarInt(Ti, buffer_vec.size(), &output_pos, buffer);
691
0
      ok &= EncodeVarInt(Fi, buffer_vec.size(), &output_pos, buffer);
692
0
      prev_prev_ix = prev_ix;
693
0
      prev_ix = i;
694
0
      T_prev = T;
695
0
      T += frame_index_box.entries[i].duration;
696
0
    }
697
0
  }
698
0
  {
699
    // Last frame.
700
0
    size_t i = frame_index_box.entries.size();
701
0
    int64_t OFFi = frame_index_box.entries[prev_ix].OFFi;
702
0
    if (prev_prev_ix != -1) {
703
0
      OFFi -= frame_index_box.entries[prev_prev_ix].OFFi;
704
0
    }
705
0
    int32_t Ti = T_prev;
706
0
    int32_t Fi = i - prev_ix;
707
0
    ok &= EncodeVarInt(OFFi, buffer_vec.size(), &output_pos, buffer);
708
0
    ok &= EncodeVarInt(Ti, buffer_vec.size(), &output_pos, buffer);
709
0
    ok &= EncodeVarInt(Fi, buffer_vec.size(), &output_pos, buffer);
710
0
  }
711
0
  if (ok) buffer_vec.resize(output_pos);
712
0
  return ok;
713
0
}
714
715
struct RunnerTicket {
716
0
  explicit RunnerTicket(jxl::ThreadPool* pool) : pool(pool) {}
717
  jxl::ThreadPool* pool;
718
  std::atomic<bool> has_error{false};
719
};
720
721
void FastLosslessRunnerAdapter(void* void_ticket, void* opaque,
722
0
                               void fun(void*, size_t), size_t count) {
723
0
  auto* ticket = reinterpret_cast<RunnerTicket*>(void_ticket);
724
0
  if (!jxl::RunOnPool(
725
0
          ticket->pool, 0, count, jxl::ThreadPool::NoInit,
726
0
          [&](size_t i, size_t) -> jxl::Status {
727
0
            fun(opaque, i);
728
0
            return true;
729
0
          },
730
0
          "Encode fast lossless")) {
731
0
    ticket->has_error = true;
732
0
  }
733
0
}
734
735
}  // namespace
736
737
238
jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() {
738
238
  jxl::PaddedBytes header_bytes{&memory_manager};
739
740
238
  jxl::JxlEncoderQueuedInput& input = input_queue[0];
741
742
  // TODO(lode): split this into 3 functions: for adding the signature and other
743
  // initial headers (jbrd, ...), one for adding frame, and one for adding user
744
  // box.
745
746
238
  if (!wrote_bytes) {
747
    // First time encoding any data, verify the level 5 vs level 10 settings
748
238
    std::string level_message;
749
238
    int required_level = VerifyLevelSettings(this, &level_message);
750
    // Only level 5 and 10 are defined, and the function can return -1 to
751
    // indicate full incompatibility.
752
238
    JXL_ENSURE(required_level == -1 || required_level == 5 ||
753
238
               required_level == 10);
754
    // codestream_level == -1 means auto-set to the required level
755
238
    if (codestream_level == -1) codestream_level = required_level;
756
238
    if (codestream_level == 5 && required_level != 5) {
757
      // If the required level is 10, return error rather than automatically
758
      // setting the level to 10, to avoid inadvertently creating a level 10
759
      // JXL file while intending to target a level 5 decoder.
760
0
      return JXL_API_ERROR(
761
0
          this, JXL_ENC_ERR_API_USAGE, "%s",
762
0
          ("Codestream level verification for level 5 failed: " + level_message)
763
0
              .c_str());
764
0
    }
765
238
    if (required_level == -1) {
766
0
      return JXL_API_ERROR(
767
0
          this, JXL_ENC_ERR_API_USAGE, "%s",
768
0
          ("Codestream level verification for level 10 failed: " +
769
0
           level_message)
770
0
              .c_str());
771
0
    }
772
238
    jxl::AuxOut* aux_out =
773
238
        input.frame ? input.frame->option_values.aux_out : nullptr;
774
238
    jxl::BitWriter writer{&memory_manager};
775
238
    if (!WriteCodestreamHeaders(&metadata, &writer, aux_out)) {
776
0
      return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
777
0
                           "Failed to write codestream header");
778
0
    }
779
    // Only send ICC (at least several hundred bytes) if fields aren't enough.
780
238
    if (metadata.m.color_encoding.WantICC()) {
781
0
      if (!jxl::WriteICC(
782
0
              jxl::Span<const uint8_t>(metadata.m.color_encoding.ICC()),
783
0
              &writer, jxl::LayerType::Header, aux_out)) {
784
0
        return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
785
0
                             "Failed to write ICC profile");
786
0
      }
787
0
    }
788
    // TODO(lode): preview should be added here if a preview image is added
789
790
238
    JXL_RETURN_IF_ERROR(
791
238
        writer.WithMaxBits(8, jxl::LayerType::Header, aux_out, [&] {
792
238
          writer.ZeroPadToByte();
793
238
          return true;
794
238
        }));
795
796
238
    header_bytes = std::move(writer).TakeBytes();
797
798
    // Not actually the end of frame, but the end of metadata/ICC, but helps
799
    // the next frame to start here for indexing purposes.
800
238
    codestream_bytes_written_end_of_frame += header_bytes.size();
801
802
238
    if (MustUseContainer()) {
803
      // Add "JXL " and ftyp box.
804
0
      {
805
0
        JXL_ASSIGN_OR_RETURN(auto buffer, output_processor.GetBuffer(
806
0
                                              jxl::kContainerHeader.size()));
807
0
        JXL_RETURN_IF_ERROR(buffer.append(jxl::kContainerHeader));
808
0
      }
809
0
      if (codestream_level != 5) {
810
        // Add jxll box directly after the ftyp box to indicate the codestream
811
        // level.
812
0
        JXL_ASSIGN_OR_RETURN(auto buffer, output_processor.GetBuffer(
813
0
                                              jxl::kLevelBoxHeader.size() + 1));
814
0
        JXL_RETURN_IF_ERROR(buffer.append(jxl::kLevelBoxHeader));
815
0
        uint8_t cl = codestream_level;
816
0
        JXL_RETURN_IF_ERROR(buffer.append(&cl, 1));
817
0
      }
818
819
      // Whether to write the basic info and color profile header of the
820
      // codestream into an early separate jxlp box, so that it comes before
821
      // metadata or jpeg reconstruction boxes. In theory this could simply
822
      // always be done, but there's no reason to add an extra box with box
823
      // header overhead if the codestream will already come immediately after
824
      // the signature and level boxes.
825
0
      bool partial_header =
826
0
          store_jpeg_metadata ||
827
0
          (use_boxes && (!input.frame && !input.fast_lossless_frame));
828
829
0
      if (partial_header) {
830
0
        auto write_box = [&]() -> jxl::Status {
831
0
          JXL_ASSIGN_OR_RETURN(
832
0
              auto buffer, output_processor.GetBuffer(header_bytes.size() + 4));
833
0
          JXL_RETURN_IF_ERROR(
834
0
              WriteJxlpBoxCounter(jxlp_counter++, /*last=*/false, buffer));
835
0
          JXL_RETURN_IF_ERROR(buffer.append(header_bytes));
836
0
          return jxl::OkStatus();
837
0
        };
838
0
        JXL_RETURN_IF_ERROR(AppendBox(jxl::MakeBoxType("jxlp"),
839
0
                                      /*unbounded=*/false,
840
0
                                      header_bytes.size() + 4, write_box));
841
0
        header_bytes.clear();
842
0
      }
843
844
0
      if (store_jpeg_metadata && !jpeg_metadata.empty()) {
845
0
        JXL_RETURN_IF_ERROR(
846
0
            AppendBoxWithContents(jxl::MakeBoxType("jbrd"), jpeg_metadata));
847
0
      }
848
0
    }
849
238
    wrote_bytes = true;
850
238
  }
851
852
238
  JXL_RETURN_IF_ERROR(output_processor.SetFinalizedPosition());
853
854
  // Choose frame or box processing: exactly one of the two unique pointers (box
855
  // or frame) in the input queue item is non-null.
856
238
  if (input.frame || input.fast_lossless_frame) {
857
238
    jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedFrame> input_frame =
858
238
        std::move(input.frame);
859
238
    jxl::FJXLFrameUniquePtr fast_lossless_frame =
860
238
        std::move(input.fast_lossless_frame);
861
238
    input_queue.erase(input_queue.begin());
862
238
    num_queued_frames--;
863
238
    if (input_frame) {
864
238
      for (unsigned idx = 0; idx < input_frame->ec_initialized.size(); idx++) {
865
0
        if (!input_frame->ec_initialized[idx]) {
866
0
          return JXL_API_ERROR(this, JXL_ENC_ERR_API_USAGE,
867
0
                               "Extra channel %u is not initialized", idx);
868
0
        }
869
0
      }
870
871
      // TODO(zond): If the input queue is empty and the frames_closed is true,
872
      // then mark this frame as the last.
873
874
      // TODO(zond): Handle progressive mode like EncodeFile does it.
875
      // TODO(zond): Handle animation like EncodeFile does it, by checking if
876
      //             JxlEncoderCloseFrames has been called and if the frame
877
      //             queue is empty (to see if it's the last animation frame).
878
879
238
      if (metadata.m.xyb_encoded) {
880
238
        input_frame->option_values.cparams.color_transform =
881
238
            jxl::ColorTransform::kXYB;
882
238
      } else {
883
        // TODO(zond): Figure out when to use kYCbCr instead.
884
0
        input_frame->option_values.cparams.color_transform =
885
0
            jxl::ColorTransform::kNone;
886
0
      }
887
238
    }
888
889
238
    uint32_t duration;
890
238
    uint32_t timecode;
891
238
    if (input_frame && metadata.m.have_animation) {
892
0
      duration = input_frame->option_values.header.duration;
893
0
      timecode = input_frame->option_values.header.timecode;
894
238
    } else {
895
      // If have_animation is false, the encoder should ignore the duration and
896
      // timecode values. However, assigning them to ib will cause the encoder
897
      // to write an invalid frame header that can't be decoded so ensure
898
      // they're the default value of 0 here.
899
238
      duration = 0;
900
238
      timecode = 0;
901
238
    }
902
903
238
    const bool last_frame = frames_closed && (num_queued_frames == 0);
904
905
238
    uint32_t max_bits_per_sample = metadata.m.bit_depth.bits_per_sample;
906
238
    for (const auto& info : metadata.m.extra_channel_info) {
907
0
      max_bits_per_sample =
908
0
          std::max(max_bits_per_sample, info.bit_depth.bits_per_sample);
909
0
    }
910
    // Heuristic upper bound on how many bits a single pixel in a single channel
911
    // can use.
912
238
    uint32_t bits_per_channels_estimate =
913
238
        std::max(24u, max_bits_per_sample + 3);
914
238
    size_t upper_bound_on_compressed_size_bits =
915
238
        metadata.xsize() * metadata.ysize() *
916
238
        (metadata.m.color_encoding.Channels() + metadata.m.num_extra_channels) *
917
238
        bits_per_channels_estimate;
918
    // Add a 1MB = 0x100000 for an heuristic upper bound on small sizes.
919
238
    size_t upper_bound_on_compressed_size_bytes =
920
238
        0x100000 + (upper_bound_on_compressed_size_bits >> 3);
921
238
    bool use_large_box = upper_bound_on_compressed_size_bytes >=
922
238
                         jxl::kLargeBoxContentSizeThreshold;
923
238
    size_t box_header_size =
924
238
        use_large_box ? jxl::kLargeBoxHeaderSize : jxl::kSmallBoxHeaderSize;
925
926
238
    const size_t frame_start_pos = output_processor.CurrentPosition();
927
238
    if (MustUseContainer()) {
928
0
      if (!last_frame || jxlp_counter > 0) {
929
        // If this is the last frame and no jxlp boxes were used yet, it's
930
        // slightly more efficient to write a jxlc box since it has 4 bytes
931
        // less overhead.
932
0
        box_header_size += 4;  // jxlp_counter field
933
0
      }
934
0
      JXL_RETURN_IF_ERROR(
935
0
          output_processor.Seek(frame_start_pos + box_header_size));
936
0
    }
937
238
    const size_t frame_codestream_start = output_processor.CurrentPosition();
938
939
238
    JXL_RETURN_IF_ERROR(AppendData(output_processor, header_bytes));
940
941
238
    if (input_frame) {
942
238
      frame_index_box.AddFrame(codestream_bytes_written_end_of_frame, duration,
943
238
                               input_frame->option_values.frame_index_box);
944
945
238
      size_t save_as_reference =
946
238
          input_frame->option_values.header.layer_info.save_as_reference;
947
238
      if (save_as_reference >= 3) {
948
0
        return JXL_API_ERROR(
949
0
            this, JXL_ENC_ERR_API_USAGE,
950
0
            "Cannot use save_as_reference values >=3 (found: %d)",
951
0
            static_cast<int>(save_as_reference));
952
0
      }
953
954
238
      jxl::FrameInfo frame_info;
955
238
      frame_info.is_last = last_frame;
956
238
      frame_info.save_as_reference = save_as_reference;
957
238
      frame_info.source =
958
238
          input_frame->option_values.header.layer_info.blend_info.source;
959
238
      frame_info.clamp = FROM_JXL_BOOL(
960
238
          input_frame->option_values.header.layer_info.blend_info.clamp);
961
238
      frame_info.alpha_channel =
962
238
          input_frame->option_values.header.layer_info.blend_info.alpha;
963
238
      frame_info.extra_channel_blending_info.resize(
964
238
          metadata.m.num_extra_channels);
965
      // If extra channel blend info has not been set, use the blend mode from
966
      // the layer_info.
967
238
      JxlBlendInfo default_blend_info =
968
238
          input_frame->option_values.header.layer_info.blend_info;
969
238
      for (size_t i = 0; i < metadata.m.num_extra_channels; ++i) {
970
0
        auto& to = frame_info.extra_channel_blending_info[i];
971
0
        const auto& from =
972
0
            i < input_frame->option_values.extra_channel_blend_info.size()
973
0
                ? input_frame->option_values.extra_channel_blend_info[i]
974
0
                : default_blend_info;
975
0
        to.mode = static_cast<jxl::BlendMode>(from.blendmode);
976
0
        to.source = from.source;
977
0
        to.alpha_channel = from.alpha;
978
0
        to.clamp = (from.clamp != 0);
979
0
      }
980
238
      frame_info.origin.x0 =
981
238
          input_frame->option_values.header.layer_info.crop_x0;
982
238
      frame_info.origin.y0 =
983
238
          input_frame->option_values.header.layer_info.crop_y0;
984
238
      frame_info.blendmode = static_cast<jxl::BlendMode>(
985
238
          input_frame->option_values.header.layer_info.blend_info.blendmode);
986
238
      frame_info.blend =
987
238
          input_frame->option_values.header.layer_info.blend_info.blendmode !=
988
238
          JXL_BLEND_REPLACE;
989
238
      frame_info.image_bit_depth = input_frame->option_values.image_bit_depth;
990
238
      frame_info.duration = duration;
991
238
      frame_info.timecode = timecode;
992
238
      frame_info.name = input_frame->option_values.frame_name;
993
994
238
      if (!jxl::EncodeFrame(&memory_manager, input_frame->option_values.cparams,
995
238
                            frame_info, &metadata, input_frame->frame_data, cms,
996
238
                            thread_pool.get(), &output_processor,
997
238
                            input_frame->option_values.aux_out)) {
998
0
        return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
999
0
                             "Failed to encode frame");
1000
0
      }
1001
238
    } else {
1002
0
      JXL_ENSURE(fast_lossless_frame);
1003
0
      RunnerTicket ticket{thread_pool.get()};
1004
0
      bool ok = JxlFastLosslessProcessFrame(
1005
0
          fast_lossless_frame.get(), last_frame, &ticket,
1006
0
          &FastLosslessRunnerAdapter, &output_processor);
1007
0
      if (!ok || ticket.has_error) {
1008
0
        return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
1009
0
                             "Internal: JxlFastLosslessProcessFrame failed");
1010
0
      }
1011
0
    }
1012
1013
238
    const size_t frame_codestream_end = output_processor.CurrentPosition();
1014
238
    const size_t frame_codestream_size =
1015
238
        frame_codestream_end - frame_codestream_start;
1016
1017
238
    codestream_bytes_written_end_of_frame +=
1018
238
        frame_codestream_size - header_bytes.size();
1019
1020
238
    if (MustUseContainer()) {
1021
0
      JXL_RETURN_IF_ERROR(output_processor.Seek(frame_start_pos));
1022
0
      std::vector<uint8_t> box_header(box_header_size);
1023
0
      if (!use_large_box &&
1024
0
          frame_codestream_size >= jxl::kLargeBoxContentSizeThreshold) {
1025
        // Assuming our upper bound estimate is correct, this should never
1026
        // happen.
1027
0
        return JXL_API_ERROR(
1028
0
            this, JXL_ENC_ERR_GENERIC,
1029
0
            "Box size was estimated to be small, but turned out to be large. "
1030
0
            "Please file this error in size estimation as a bug.");
1031
0
      }
1032
0
      if (last_frame && jxlp_counter == 0) {
1033
0
        size_t n = jxl::WriteBoxHeader(
1034
0
            jxl::MakeBoxType("jxlc"), frame_codestream_size,
1035
0
            /*unbounded=*/false, use_large_box, box_header.data());
1036
0
        JXL_ENSURE(n == box_header_size);
1037
0
      } else {
1038
0
        size_t n = jxl::WriteBoxHeader(
1039
0
            jxl::MakeBoxType("jxlp"), frame_codestream_size + 4,
1040
0
            /*unbounded=*/false, use_large_box, box_header.data());
1041
0
        JXL_ENSURE(n == box_header_size - 4);
1042
0
        WriteJxlpBoxCounter(jxlp_counter++, last_frame,
1043
0
                            &box_header[box_header_size - 4]);
1044
0
      }
1045
0
      JXL_RETURN_IF_ERROR(AppendData(output_processor, box_header));
1046
0
      JXL_ENSURE(output_processor.CurrentPosition() == frame_codestream_start);
1047
0
      JXL_RETURN_IF_ERROR(output_processor.Seek(frame_codestream_end));
1048
0
    }
1049
238
    JXL_RETURN_IF_ERROR(output_processor.SetFinalizedPosition());
1050
238
    if (input_frame) {
1051
238
      last_used_cparams = input_frame->option_values.cparams;
1052
238
    }
1053
238
    if (last_frame && frame_index_box.StoreFrameIndexBox()) {
1054
0
      std::vector<uint8_t> index_box_content;
1055
      // Enough buffer has been allocated, this function should never fail in
1056
      // writing.
1057
0
      JXL_ENSURE(EncodeFrameIndexBox(frame_index_box, index_box_content));
1058
0
      JXL_RETURN_IF_ERROR(AppendBoxWithContents(jxl::MakeBoxType("jxli"),
1059
0
                                                jxl::Bytes(index_box_content)));
1060
0
    }
1061
238
  } else {
1062
    // Not a frame, so is a box instead
1063
0
    jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedBox> box =
1064
0
        std::move(input.box);
1065
0
    input_queue.erase(input_queue.begin());
1066
0
    num_queued_boxes--;
1067
1068
0
    if (box->compress_box) {
1069
0
      jxl::PaddedBytes compressed(&memory_manager);
1070
      // Prepend the original box type in the brob box contents
1071
0
      JXL_RETURN_IF_ERROR(compressed.append(box->type));
1072
0
      if (JXL_ENC_SUCCESS !=
1073
0
          BrotliCompress((brotli_effort >= 0 ? brotli_effort : 4),
1074
0
                         box->contents.data(), box->contents.size(),
1075
0
                         &compressed)) {
1076
0
        return JXL_API_ERROR(this, JXL_ENC_ERR_GENERIC,
1077
0
                             "Brotli compression for brob box failed");
1078
0
      }
1079
1080
0
      JXL_RETURN_IF_ERROR(
1081
0
          AppendBoxWithContents(jxl::MakeBoxType("brob"), compressed));
1082
0
    } else {
1083
0
      JXL_RETURN_IF_ERROR(AppendBoxWithContents(box->type, box->contents));
1084
0
    }
1085
0
  }
1086
1087
238
  return jxl::OkStatus();
1088
238
}
1089
1090
JxlEncoderStatus JxlEncoderSetColorEncoding(JxlEncoder* enc,
1091
238
                                            const JxlColorEncoding* color) {
1092
238
  if (!enc->basic_info_set) {
1093
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Basic info not yet set");
1094
0
  }
1095
238
  if (enc->color_encoding_set) {
1096
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1097
0
                         "Color encoding is already set");
1098
0
  }
1099
238
  if (!enc->metadata.m.color_encoding.FromExternal(*color)) {
1100
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_GENERIC, "Error in color conversion");
1101
0
  }
1102
238
  if (enc->metadata.m.color_encoding.GetColorSpace() ==
1103
238
      jxl::ColorSpace::kGray) {
1104
0
    if (enc->basic_info.num_color_channels != 1) {
1105
0
      return JXL_API_ERROR(
1106
0
          enc, JXL_ENC_ERR_API_USAGE,
1107
0
          "Cannot use grayscale color encoding with num_color_channels != 1");
1108
0
    }
1109
238
  } else {
1110
238
    if (enc->basic_info.num_color_channels != 3) {
1111
0
      return JXL_API_ERROR(
1112
0
          enc, JXL_ENC_ERR_API_USAGE,
1113
0
          "Cannot use RGB color encoding with num_color_channels != 3");
1114
0
    }
1115
238
  }
1116
238
  enc->color_encoding_set = true;
1117
238
  if (!enc->intensity_target_set) {
1118
238
    jxl::SetIntensityTarget(&enc->metadata.m);
1119
238
  }
1120
238
  return JxlErrorOrStatus::Success();
1121
238
}
1122
1123
JxlEncoderStatus JxlEncoderSetICCProfile(JxlEncoder* enc,
1124
                                         const uint8_t* icc_profile,
1125
0
                                         size_t size) {
1126
0
  if (!enc->basic_info_set) {
1127
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Basic info not yet set");
1128
0
  }
1129
0
  if (enc->color_encoding_set) {
1130
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1131
0
                         "ICC profile is already set");
1132
0
  }
1133
0
  if (size == 0) {
1134
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_BAD_INPUT, "Empty ICC profile");
1135
0
  }
1136
0
  jxl::IccBytes icc;
1137
0
  icc.assign(icc_profile, icc_profile + size);
1138
0
  if (enc->cms_set) {
1139
0
    if (!enc->metadata.m.color_encoding.SetICC(std::move(icc), &enc->cms)) {
1140
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_BAD_INPUT,
1141
0
                           "ICC profile could not be set");
1142
0
    }
1143
0
  } else {
1144
0
    enc->metadata.m.color_encoding.SetICCRaw(std::move(icc));
1145
0
  }
1146
0
  if (enc->metadata.m.color_encoding.GetColorSpace() ==
1147
0
      jxl::ColorSpace::kGray) {
1148
0
    if (enc->basic_info.num_color_channels != 1) {
1149
0
      return JXL_API_ERROR(
1150
0
          enc, JXL_ENC_ERR_BAD_INPUT,
1151
0
          "Cannot use grayscale ICC profile with num_color_channels != 1");
1152
0
    }
1153
0
  } else {
1154
0
    if (enc->basic_info.num_color_channels != 3) {
1155
0
      return JXL_API_ERROR(
1156
0
          enc, JXL_ENC_ERR_BAD_INPUT,
1157
0
          "Cannot use RGB ICC profile with num_color_channels != 3");
1158
0
    }
1159
    // TODO(jon): also check that a kBlack extra channel is provided in the CMYK
1160
    // case
1161
0
  }
1162
0
  enc->color_encoding_set = true;
1163
0
  if (!enc->intensity_target_set) {
1164
0
    jxl::SetIntensityTarget(&enc->metadata.m);
1165
0
  }
1166
1167
0
  if (!enc->basic_info.uses_original_profile && enc->cms_set) {
1168
0
    enc->metadata.m.color_encoding.DecideIfWantICC(enc->cms);
1169
0
  }
1170
1171
0
  return JxlErrorOrStatus::Success();
1172
0
}
1173
1174
21.5k
void JxlEncoderInitBasicInfo(JxlBasicInfo* info) {
1175
21.5k
  info->have_container = JXL_FALSE;
1176
21.5k
  info->xsize = 0;
1177
21.5k
  info->ysize = 0;
1178
21.5k
  info->bits_per_sample = 8;
1179
21.5k
  info->exponent_bits_per_sample = 0;
1180
21.5k
  info->intensity_target = 0.f;
1181
21.5k
  info->min_nits = 0.f;
1182
21.5k
  info->relative_to_max_display = JXL_FALSE;
1183
21.5k
  info->linear_below = 0.f;
1184
21.5k
  info->uses_original_profile = JXL_FALSE;
1185
21.5k
  info->have_preview = JXL_FALSE;
1186
21.5k
  info->have_animation = JXL_FALSE;
1187
21.5k
  info->orientation = JXL_ORIENT_IDENTITY;
1188
21.5k
  info->num_color_channels = 3;
1189
21.5k
  info->num_extra_channels = 0;
1190
21.5k
  info->alpha_bits = 0;
1191
21.5k
  info->alpha_exponent_bits = 0;
1192
21.5k
  info->alpha_premultiplied = JXL_FALSE;
1193
21.5k
  info->preview.xsize = 0;
1194
21.5k
  info->preview.ysize = 0;
1195
21.5k
  info->intrinsic_xsize = 0;
1196
21.5k
  info->intrinsic_ysize = 0;
1197
21.5k
  info->animation.tps_numerator = 10;
1198
21.5k
  info->animation.tps_denominator = 1;
1199
21.5k
  info->animation.num_loops = 0;
1200
21.5k
  info->animation.have_timecodes = JXL_FALSE;
1201
21.5k
}
1202
1203
238
void JxlEncoderInitFrameHeader(JxlFrameHeader* frame_header) {
1204
  // For each field, the default value of the specification is used. Depending
1205
  // on whether an animation frame, or a composite still blending frame,
1206
  // is used, different fields have to be set up by the user after initing
1207
  // the frame header.
1208
238
  frame_header->duration = 0;
1209
238
  frame_header->timecode = 0;
1210
238
  frame_header->name_length = 0;
1211
  // In the specification, the default value of is_last is !frame_type, and the
1212
  // default frame_type is kRegularFrame which has value 0, so is_last is true
1213
  // by default. However, the encoder does not use this value (the field exists
1214
  // for the decoder to set) since last frame is determined by usage of
1215
  // JxlEncoderCloseFrames instead.
1216
238
  frame_header->is_last = JXL_TRUE;
1217
238
  frame_header->layer_info.have_crop = JXL_FALSE;
1218
238
  frame_header->layer_info.crop_x0 = 0;
1219
238
  frame_header->layer_info.crop_y0 = 0;
1220
  // These must be set if have_crop is enabled, but the default value has
1221
  // have_crop false, and these dimensions 0. The user must set these to the
1222
  // desired size after enabling have_crop (which is not yet implemented).
1223
238
  frame_header->layer_info.xsize = 0;
1224
238
  frame_header->layer_info.ysize = 0;
1225
238
  JxlEncoderInitBlendInfo(&frame_header->layer_info.blend_info);
1226
238
  frame_header->layer_info.save_as_reference = 0;
1227
238
}
1228
1229
238
void JxlEncoderInitBlendInfo(JxlBlendInfo* blend_info) {
1230
  // Default blend mode in the specification is 0. Note that combining
1231
  // blend mode of replace with a duration is not useful, but the user has to
1232
  // manually set duration in case of animation, or manually change the blend
1233
  // mode in case of composite stills, so initing to a combination that is not
1234
  // useful on its own is not an issue.
1235
238
  blend_info->blendmode = JXL_BLEND_REPLACE;
1236
238
  blend_info->source = 0;
1237
238
  blend_info->alpha = 0;
1238
238
  blend_info->clamp = 0;
1239
238
}
1240
1241
JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc,
1242
238
                                        const JxlBasicInfo* info) {
1243
238
  if (!enc->metadata.size.Set(info->xsize, info->ysize)) {
1244
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid dimensions");
1245
0
  }
1246
238
  if (JXL_ENC_SUCCESS != CheckValidBitdepth(info->bits_per_sample,
1247
238
                                            info->exponent_bits_per_sample)) {
1248
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid bit depth");
1249
0
  }
1250
1251
238
  enc->metadata.m.bit_depth.bits_per_sample = info->bits_per_sample;
1252
238
  enc->metadata.m.bit_depth.exponent_bits_per_sample =
1253
238
      info->exponent_bits_per_sample;
1254
238
  enc->metadata.m.bit_depth.floating_point_sample =
1255
238
      (info->exponent_bits_per_sample != 0u);
1256
238
  enc->metadata.m.modular_16_bit_buffer_sufficient =
1257
238
      (!FROM_JXL_BOOL(info->uses_original_profile) ||
1258
238
       info->bits_per_sample <= 12) &&
1259
238
      info->alpha_bits <= 12;
1260
238
  if ((info->intrinsic_xsize > 0 || info->intrinsic_ysize > 0) &&
1261
238
      (info->intrinsic_xsize != info->xsize ||
1262
0
       info->intrinsic_ysize != info->ysize)) {
1263
0
    if (info->intrinsic_xsize > (1ull << 30ull) ||
1264
0
        info->intrinsic_ysize > (1ull << 30ull) ||
1265
0
        !enc->metadata.m.intrinsic_size.Set(info->intrinsic_xsize,
1266
0
                                            info->intrinsic_ysize)) {
1267
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1268
0
                           "Invalid intrinsic dimensions");
1269
0
    }
1270
0
    enc->metadata.m.have_intrinsic_size = true;
1271
0
  }
1272
1273
  // The number of extra channels includes the alpha channel, so for example and
1274
  // RGBA with no other extra channels, has exactly num_extra_channels == 1
1275
238
  enc->metadata.m.num_extra_channels = info->num_extra_channels;
1276
238
  enc->metadata.m.extra_channel_info.resize(enc->metadata.m.num_extra_channels);
1277
238
  if (info->num_extra_channels == 0 && info->alpha_bits) {
1278
0
    return JXL_API_ERROR(
1279
0
        enc, JXL_ENC_ERR_API_USAGE,
1280
0
        "when alpha_bits is non-zero, the number of channels must be at least "
1281
0
        "1");
1282
0
  }
1283
  // If the user provides non-zero alpha_bits, we make the channel info at index
1284
  // zero the appropriate alpha channel.
1285
238
  if (info->alpha_bits) {
1286
0
    JxlExtraChannelInfo channel_info;
1287
0
    JxlEncoderInitExtraChannelInfo(JXL_CHANNEL_ALPHA, &channel_info);
1288
0
    channel_info.bits_per_sample = info->alpha_bits;
1289
0
    channel_info.exponent_bits_per_sample = info->alpha_exponent_bits;
1290
0
    if (JxlEncoderSetExtraChannelInfo(enc, 0, &channel_info)) {
1291
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1292
0
                           "Problem setting extra channel info for alpha");
1293
0
    }
1294
0
  }
1295
1296
238
  enc->metadata.m.xyb_encoded = !FROM_JXL_BOOL(info->uses_original_profile);
1297
238
  if (info->orientation > 0 && info->orientation <= 8) {
1298
238
    enc->metadata.m.orientation = info->orientation;
1299
238
  } else {
1300
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1301
0
                         "Invalid value for orientation field");
1302
0
  }
1303
238
  if (info->num_color_channels != 1 && info->num_color_channels != 3) {
1304
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1305
0
                         "Invalid number of color channels");
1306
0
  }
1307
238
  if (info->intensity_target != 0) {
1308
0
    enc->metadata.m.SetIntensityTarget(info->intensity_target);
1309
0
    enc->intensity_target_set = true;
1310
238
  } else if (enc->color_encoding_set) {
1311
    // If this is false, JxlEncoderSetColorEncoding will be called later and we
1312
    // will get one more chance to call jxl::SetIntensityTarget, after the color
1313
    // encoding is indeed set.
1314
0
    jxl::SetIntensityTarget(&enc->metadata.m);
1315
0
    enc->intensity_target_set = true;
1316
0
  }
1317
238
  enc->metadata.m.tone_mapping.min_nits = info->min_nits;
1318
238
  enc->metadata.m.tone_mapping.relative_to_max_display =
1319
238
      FROM_JXL_BOOL(info->relative_to_max_display);
1320
238
  enc->metadata.m.tone_mapping.linear_below = info->linear_below;
1321
238
  enc->basic_info = *info;
1322
238
  enc->basic_info_set = true;
1323
1324
238
  enc->metadata.m.have_animation = FROM_JXL_BOOL(info->have_animation);
1325
238
  if (info->have_animation) {
1326
0
    if (info->animation.tps_denominator < 1) {
1327
0
      return JXL_API_ERROR(
1328
0
          enc, JXL_ENC_ERR_API_USAGE,
1329
0
          "If animation is used, tps_denominator must be >= 1");
1330
0
    }
1331
0
    if (info->animation.tps_numerator < 1) {
1332
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1333
0
                           "If animation is used, tps_numerator must be >= 1");
1334
0
    }
1335
0
    enc->metadata.m.animation.tps_numerator = info->animation.tps_numerator;
1336
0
    enc->metadata.m.animation.tps_denominator = info->animation.tps_denominator;
1337
0
    enc->metadata.m.animation.num_loops = info->animation.num_loops;
1338
0
    enc->metadata.m.animation.have_timecodes =
1339
0
        FROM_JXL_BOOL(info->animation.have_timecodes);
1340
0
  }
1341
238
  std::string level_message;
1342
238
  int required_level = VerifyLevelSettings(enc, &level_message);
1343
238
  if (required_level == -1 ||
1344
238
      (static_cast<int>(enc->codestream_level) < required_level &&
1345
238
       enc->codestream_level != -1)) {
1346
0
    return JXL_API_ERROR(
1347
0
        enc, JXL_ENC_ERR_API_USAGE, "%s",
1348
0
        ("Codestream level verification for level " +
1349
0
         std::to_string(enc->codestream_level) + " failed: " + level_message)
1350
0
            .c_str());
1351
0
  }
1352
238
  return JxlErrorOrStatus::Success();
1353
238
}
1354
1355
void JxlEncoderInitExtraChannelInfo(JxlExtraChannelType type,
1356
0
                                    JxlExtraChannelInfo* info) {
1357
0
  info->type = type;
1358
0
  info->bits_per_sample = 8;
1359
0
  info->exponent_bits_per_sample = 0;
1360
0
  info->dim_shift = 0;
1361
0
  info->name_length = 0;
1362
0
  info->alpha_premultiplied = JXL_FALSE;
1363
0
  info->spot_color[0] = 0;
1364
0
  info->spot_color[1] = 0;
1365
0
  info->spot_color[2] = 0;
1366
0
  info->spot_color[3] = 0;
1367
0
  info->cfa_channel = 0;
1368
0
}
1369
1370
JXL_EXPORT JxlEncoderStatus JxlEncoderSetUpsamplingMode(JxlEncoder* enc,
1371
                                                        const int64_t factor,
1372
0
                                                        const int64_t mode) {
1373
  // for convenience, allow calling this with factor 1 and just make it a no-op
1374
0
  if (factor == 1) return JxlErrorOrStatus::Success();
1375
0
  if (factor != 2 && factor != 4 && factor != 8) {
1376
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1377
0
                         "Invalid upsampling factor");
1378
0
  }
1379
0
  if (mode < -1)
1380
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid upsampling mode");
1381
0
  if (mode > 1) {
1382
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_NOT_SUPPORTED,
1383
0
                         "Unsupported upsampling mode");
1384
0
  }
1385
1386
0
  const size_t count = (factor == 2 ? 15 : (factor == 4 ? 55 : 210));
1387
0
  auto& td = enc->metadata.transform_data;
1388
0
  float* weights = (factor == 2 ? td.upsampling2_weights
1389
0
                                : (factor == 4 ? td.upsampling4_weights
1390
0
                                               : td.upsampling8_weights));
1391
0
  if (mode == -1) {
1392
    // Default fancy upsampling: don't signal custom weights
1393
0
    enc->metadata.transform_data.custom_weights_mask &= ~(factor >> 1);
1394
0
  } else if (mode == 0) {
1395
    // Nearest neighbor upsampling
1396
0
    enc->metadata.transform_data.custom_weights_mask |= (factor >> 1);
1397
0
    memset(weights, 0, sizeof(float) * count);
1398
0
    if (factor == 2) {
1399
0
      weights[9] = 1.f;
1400
0
    } else if (factor == 4) {
1401
0
      for (int i : {19, 24, 49}) weights[i] = 1.f;
1402
0
    } else if (factor == 8) {
1403
0
      for (int i : {39, 44, 49, 54, 119, 124, 129, 174, 179, 204}) {
1404
0
        weights[i] = 1.f;
1405
0
      }
1406
0
    }
1407
0
  } else if (mode == 1) {
1408
    // 'Pixel dots' upsampling (nearest-neighbor with cut corners)
1409
0
    JxlEncoderSetUpsamplingMode(enc, factor, 0);
1410
0
    if (factor == 4) {
1411
0
      weights[19] = 0.f;
1412
0
      weights[24] = 0.5f;
1413
0
    } else if (factor == 8) {
1414
0
      for (int i : {39, 44, 49, 119}) weights[i] = 0.f;
1415
0
      for (int i : {54, 124}) weights[i] = 0.5f;
1416
0
    }
1417
0
  }
1418
0
  return JxlErrorOrStatus::Success();
1419
0
}
1420
1421
JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelInfo(
1422
0
    JxlEncoder* enc, size_t index, const JxlExtraChannelInfo* info) {
1423
0
  if (index >= enc->metadata.m.num_extra_channels) {
1424
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1425
0
                         "Invalid value for the index of extra channel");
1426
0
  }
1427
0
  if (JXL_ENC_SUCCESS != CheckValidBitdepth(info->bits_per_sample,
1428
0
                                            info->exponent_bits_per_sample)) {
1429
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid bit depth");
1430
0
  }
1431
1432
0
  jxl::ExtraChannelInfo& channel = enc->metadata.m.extra_channel_info[index];
1433
0
  channel.type = static_cast<jxl::ExtraChannel>(info->type);
1434
0
  channel.bit_depth.bits_per_sample = info->bits_per_sample;
1435
0
  enc->metadata.m.modular_16_bit_buffer_sufficient &=
1436
0
      info->bits_per_sample <= 12;
1437
0
  channel.bit_depth.exponent_bits_per_sample = info->exponent_bits_per_sample;
1438
0
  channel.bit_depth.floating_point_sample = info->exponent_bits_per_sample != 0;
1439
0
  channel.dim_shift = info->dim_shift;
1440
0
  channel.name = "";
1441
0
  channel.alpha_associated = (info->alpha_premultiplied != 0);
1442
0
  channel.cfa_channel = info->cfa_channel;
1443
0
  channel.spot_color[0] = info->spot_color[0];
1444
0
  channel.spot_color[1] = info->spot_color[1];
1445
0
  channel.spot_color[2] = info->spot_color[2];
1446
0
  channel.spot_color[3] = info->spot_color[3];
1447
0
  std::string level_message;
1448
0
  int required_level = VerifyLevelSettings(enc, &level_message);
1449
0
  if (required_level == -1 ||
1450
0
      (static_cast<int>(enc->codestream_level) < required_level &&
1451
0
       enc->codestream_level != -1)) {
1452
0
    return JXL_API_ERROR(
1453
0
        enc, JXL_ENC_ERR_API_USAGE, "%s",
1454
0
        ("Codestream level verification for level " +
1455
0
         std::to_string(enc->codestream_level) + " failed: " + level_message)
1456
0
            .c_str());
1457
0
  }
1458
0
  return JxlErrorOrStatus::Success();
1459
0
}
1460
1461
JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelName(JxlEncoder* enc,
1462
                                                          size_t index,
1463
                                                          const char* name,
1464
0
                                                          size_t size) {
1465
0
  if (index >= enc->metadata.m.num_extra_channels) {
1466
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1467
0
                         "Invalid value for the index of extra channel");
1468
0
  }
1469
0
  enc->metadata.m.extra_channel_info[index].name =
1470
0
      std::string(name, name + size);
1471
0
  return JxlErrorOrStatus::Success();
1472
0
}
1473
1474
JxlEncoderFrameSettings* JxlEncoderFrameSettingsCreate(
1475
238
    JxlEncoder* enc, const JxlEncoderFrameSettings* source) {
1476
238
  auto opts = jxl::MemoryManagerMakeUnique<JxlEncoderFrameSettings>(
1477
238
      &enc->memory_manager);
1478
238
  if (!opts) return nullptr;
1479
238
  opts->enc = enc;
1480
238
  if (source != nullptr) {
1481
0
    opts->values = source->values;
1482
238
  } else {
1483
238
    opts->values.lossless = false;
1484
238
  }
1485
238
  opts->values.cparams.level = enc->codestream_level;
1486
238
  opts->values.cparams.ec_distance.resize(enc->metadata.m.num_extra_channels,
1487
238
                                          0);
1488
1489
238
  JxlEncoderFrameSettings* ret = opts.get();
1490
238
  enc->encoder_options.emplace_back(std::move(opts));
1491
238
  return ret;
1492
238
}
1493
1494
JxlEncoderStatus JxlEncoderSetFrameLossless(
1495
0
    JxlEncoderFrameSettings* frame_settings, const JXL_BOOL lossless) {
1496
0
  if (lossless && frame_settings->enc->basic_info_set &&
1497
0
      frame_settings->enc->metadata.m.xyb_encoded) {
1498
0
    return JXL_API_ERROR(
1499
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1500
0
        "Set uses_original_profile=true for lossless encoding");
1501
0
  }
1502
0
  frame_settings->values.lossless = FROM_JXL_BOOL(lossless);
1503
0
  return JxlErrorOrStatus::Success();
1504
0
}
1505
1506
JxlEncoderStatus JxlEncoderSetFrameDistance(
1507
0
    JxlEncoderFrameSettings* frame_settings, float distance) {
1508
0
  if (distance < 0.f || distance > 25.f) {
1509
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1510
0
                         "Distance has to be in [0.0..25.0] (corresponding to "
1511
0
                         "quality in [0.0..100.0])");
1512
0
  }
1513
0
  if (distance > 0.f && distance < 0.01f) {
1514
0
    distance = 0.01f;
1515
0
  }
1516
0
  frame_settings->values.cparams.butteraugli_distance = distance;
1517
0
  return JxlErrorOrStatus::Success();
1518
0
}
1519
1520
JxlEncoderStatus JxlEncoderSetExtraChannelDistance(
1521
0
    JxlEncoderFrameSettings* frame_settings, size_t index, float distance) {
1522
0
  if (index >= frame_settings->enc->metadata.m.num_extra_channels) {
1523
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1524
0
                         "Invalid value for the index of extra channel");
1525
0
  }
1526
0
  if (distance != -1.f && (distance < 0.f || distance > 25.f)) {
1527
0
    return JXL_API_ERROR(
1528
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1529
0
        "Distance has to be -1 or in [0.0..25.0] (corresponding to "
1530
0
        "quality in [0.0..100.0])");
1531
0
  }
1532
0
  if (distance > 0.f && distance < 0.01f) {
1533
0
    distance = 0.01f;
1534
0
  }
1535
1536
0
  if (index >= frame_settings->values.cparams.ec_distance.size()) {
1537
    // This can only happen if JxlEncoderFrameSettingsCreate() was called before
1538
    // JxlEncoderSetBasicInfo().
1539
0
    frame_settings->values.cparams.ec_distance.resize(
1540
0
        frame_settings->enc->metadata.m.num_extra_channels, 0);
1541
0
  }
1542
1543
0
  frame_settings->values.cparams.ec_distance[index] = distance;
1544
0
  return JxlErrorOrStatus::Success();
1545
0
}
1546
1547
0
float JxlEncoderDistanceFromQuality(float quality) {
1548
0
  return quality >= 100.0 ? 0.0
1549
0
         : quality >= 30
1550
0
             ? 0.1 + (100 - quality) * 0.09
1551
0
             : 53.0 / 3000.0 * quality * quality - 23.0 / 20.0 * quality + 25.0;
1552
0
}
1553
1554
JxlEncoderStatus JxlEncoderFrameSettingsSetOption(
1555
    JxlEncoderFrameSettings* frame_settings, JxlEncoderFrameSettingId option,
1556
0
    int64_t value) {
1557
  // Tri-state to bool convertors.
1558
0
  const auto default_to_true = [](int64_t v) { return v != 0; };
1559
0
  const auto default_to_false = [](int64_t v) { return v == 1; };
1560
1561
  // check if value is -1, 0 or 1 for Override-type options
1562
0
  switch (option) {
1563
0
    case JXL_ENC_FRAME_SETTING_NOISE:
1564
0
    case JXL_ENC_FRAME_SETTING_DOTS:
1565
0
    case JXL_ENC_FRAME_SETTING_PATCHES:
1566
0
    case JXL_ENC_FRAME_SETTING_GABORISH:
1567
0
    case JXL_ENC_FRAME_SETTING_MODULAR:
1568
0
    case JXL_ENC_FRAME_SETTING_KEEP_INVISIBLE:
1569
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER:
1570
0
    case JXL_ENC_FRAME_SETTING_RESPONSIVE:
1571
0
    case JXL_ENC_FRAME_SETTING_PROGRESSIVE_AC:
1572
0
    case JXL_ENC_FRAME_SETTING_QPROGRESSIVE_AC:
1573
0
    case JXL_ENC_FRAME_SETTING_LOSSY_PALETTE:
1574
0
    case JXL_ENC_FRAME_SETTING_JPEG_RECON_CFL:
1575
0
    case JXL_ENC_FRAME_SETTING_JPEG_COMPRESS_BOXES:
1576
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_EXIF:
1577
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_XMP:
1578
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_JUMBF:
1579
0
      if (value < -1 || value > 1) {
1580
0
        return JXL_API_ERROR(
1581
0
            frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1582
0
            "Option value has to be -1 (default), 0 (off) or 1 (on)");
1583
0
      }
1584
0
      break;
1585
0
    default:
1586
0
      break;
1587
0
  }
1588
1589
0
  switch (option) {
1590
0
    case JXL_ENC_FRAME_SETTING_EFFORT:
1591
0
      if (frame_settings->enc->allow_expert_options) {
1592
0
        if (value < 1 || value > 11) {
1593
0
          return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1594
0
                               "Encode effort has to be in [1..11]");
1595
0
        }
1596
0
      } else {
1597
0
        if (value < 1 || value > 10) {
1598
0
          return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1599
0
                               "Encode effort has to be in [1..10]");
1600
0
        }
1601
0
      }
1602
0
      frame_settings->values.cparams.speed_tier =
1603
0
          static_cast<jxl::SpeedTier>(10 - value);
1604
0
      break;
1605
0
    case JXL_ENC_FRAME_SETTING_BROTLI_EFFORT:
1606
0
      if (value < -1 || value > 11) {
1607
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1608
0
                             "Brotli effort has to be in [-1..11]");
1609
0
      }
1610
      // set cparams for brotli use in JPEG frames
1611
0
      frame_settings->values.cparams.brotli_effort = value;
1612
      // set enc option for brotli use in brob boxes
1613
0
      frame_settings->enc->brotli_effort = value;
1614
0
      break;
1615
0
    case JXL_ENC_FRAME_SETTING_DECODING_SPEED:
1616
0
      if (value < 0 || value > 4) {
1617
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1618
0
                             "Decoding speed has to be in [0..4]");
1619
0
      }
1620
0
      frame_settings->values.cparams.decoding_speed_tier = value;
1621
0
      break;
1622
0
    case JXL_ENC_FRAME_SETTING_RESAMPLING:
1623
0
      if (value != -1 && value != 1 && value != 2 && value != 4 && value != 8) {
1624
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1625
0
                             "Resampling factor has to be 1, 2, 4 or 8");
1626
0
      }
1627
0
      frame_settings->values.cparams.resampling = value;
1628
0
      break;
1629
0
    case JXL_ENC_FRAME_SETTING_EXTRA_CHANNEL_RESAMPLING:
1630
      // TODO(lode): the jxl codestream allows choosing a different resampling
1631
      // factor for each extra channel, independently per frame. Move this
1632
      // option to a JxlEncoderFrameSettings-option that can be set per extra
1633
      // channel, so needs its own function rather than
1634
      // JxlEncoderFrameSettingsSetOption due to the extra channel index
1635
      // argument required.
1636
0
      if (value != -1 && value != 1 && value != 2 && value != 4 && value != 8) {
1637
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1638
0
                             "Resampling factor has to be 1, 2, 4 or 8");
1639
0
      }
1640
0
      frame_settings->values.cparams.ec_resampling = value;
1641
0
      break;
1642
0
    case JXL_ENC_FRAME_SETTING_ALREADY_DOWNSAMPLED:
1643
0
      if (value < 0 || value > 1) {
1644
0
        return JxlErrorOrStatus::Error();
1645
0
      }
1646
0
      frame_settings->values.cparams.already_downsampled = (value == 1);
1647
0
      break;
1648
0
    case JXL_ENC_FRAME_SETTING_NOISE:
1649
0
      frame_settings->values.cparams.noise = static_cast<jxl::Override>(value);
1650
0
      break;
1651
0
    case JXL_ENC_FRAME_SETTING_DOTS:
1652
0
      frame_settings->values.cparams.dots = static_cast<jxl::Override>(value);
1653
0
      break;
1654
0
    case JXL_ENC_FRAME_SETTING_PATCHES:
1655
0
      frame_settings->values.cparams.patches =
1656
0
          static_cast<jxl::Override>(value);
1657
0
      break;
1658
0
    case JXL_ENC_FRAME_SETTING_EPF:
1659
0
      if (value < -1 || value > 3) {
1660
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1661
0
                             "EPF value has to be in [-1..3]");
1662
0
      }
1663
0
      frame_settings->values.cparams.epf = static_cast<int>(value);
1664
0
      break;
1665
0
    case JXL_ENC_FRAME_SETTING_GABORISH:
1666
0
      frame_settings->values.cparams.gaborish =
1667
0
          static_cast<jxl::Override>(value);
1668
0
      break;
1669
0
    case JXL_ENC_FRAME_SETTING_MODULAR:
1670
0
      frame_settings->values.cparams.modular_mode = (value == 1);
1671
0
      break;
1672
0
    case JXL_ENC_FRAME_SETTING_KEEP_INVISIBLE:
1673
0
      frame_settings->values.cparams.keep_invisible =
1674
0
          static_cast<jxl::Override>(value);
1675
0
      break;
1676
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER:
1677
0
      frame_settings->values.cparams.centerfirst = (value == 1);
1678
0
      break;
1679
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_X:
1680
0
      if (value < -1) {
1681
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1682
0
                             "Center x coordinate has to be -1 or positive");
1683
0
      }
1684
0
      frame_settings->values.cparams.center_x = static_cast<size_t>(value);
1685
0
      break;
1686
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_Y:
1687
0
      if (value < -1) {
1688
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1689
0
                             "Center y coordinate has to be -1 or positive");
1690
0
      }
1691
0
      frame_settings->values.cparams.center_y = static_cast<size_t>(value);
1692
0
      break;
1693
0
    case JXL_ENC_FRAME_SETTING_RESPONSIVE:
1694
0
      frame_settings->values.cparams.responsive = value;
1695
0
      break;
1696
0
    case JXL_ENC_FRAME_SETTING_PROGRESSIVE_AC:
1697
0
      frame_settings->values.cparams.progressive_mode =
1698
0
          static_cast<jxl::Override>(value);
1699
0
      break;
1700
0
    case JXL_ENC_FRAME_SETTING_QPROGRESSIVE_AC:
1701
0
      frame_settings->values.cparams.qprogressive_mode =
1702
0
          static_cast<jxl::Override>(value);
1703
0
      break;
1704
0
    case JXL_ENC_FRAME_SETTING_PROGRESSIVE_DC:
1705
0
      if (value < -1 || value > 2) {
1706
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1707
0
                             "Progressive DC has to be in [-1..2]");
1708
0
      }
1709
0
      frame_settings->values.cparams.progressive_dc = value;
1710
0
      break;
1711
0
    case JXL_ENC_FRAME_SETTING_PALETTE_COLORS:
1712
0
      if (value < -1 || value > 70913) {
1713
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1714
0
                             "Option value has to be in [-1..70913]");
1715
0
      }
1716
0
      if (value == -1) {
1717
0
        frame_settings->values.cparams.palette_colors = 1 << 10;
1718
0
      } else {
1719
0
        frame_settings->values.cparams.palette_colors = value;
1720
0
      }
1721
0
      break;
1722
0
    case JXL_ENC_FRAME_SETTING_LOSSY_PALETTE:
1723
      // TODO(lode): the defaults of some palette settings depend on others.
1724
      // See the logic in cjxl. Similar for other settings. This should be
1725
      // handled in the encoder during JxlEncoderProcessOutput (or,
1726
      // alternatively, in the cjxl binary like now)
1727
0
      frame_settings->values.cparams.lossy_palette = default_to_false(value);
1728
0
      break;
1729
0
    case JXL_ENC_FRAME_SETTING_COLOR_TRANSFORM:
1730
0
      if (value < -1 || value > 2) {
1731
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1732
0
                             "Option value has to be in [-1..2]");
1733
0
      }
1734
0
      if (value == -1) {
1735
0
        frame_settings->values.cparams.color_transform =
1736
0
            jxl::ColorTransform::kXYB;
1737
0
      } else {
1738
0
        frame_settings->values.cparams.color_transform =
1739
0
            static_cast<jxl::ColorTransform>(value);
1740
0
      }
1741
0
      break;
1742
0
    case JXL_ENC_FRAME_SETTING_MODULAR_COLOR_SPACE:
1743
0
      if (value < -1 || value > 41) {
1744
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1745
0
                             "Option value has to be in [-1..41]");
1746
0
      }
1747
0
      frame_settings->values.cparams.colorspace = value;
1748
0
      break;
1749
0
    case JXL_ENC_FRAME_SETTING_MODULAR_GROUP_SIZE:
1750
0
      if (value < -1 || value > 3) {
1751
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1752
0
                             "Option value has to be in [-1..3]");
1753
0
      }
1754
0
      frame_settings->values.cparams.modular_group_size_shift = value;
1755
0
      break;
1756
0
    case JXL_ENC_FRAME_SETTING_MODULAR_PREDICTOR:
1757
0
      if (value < -1 || value > 15) {
1758
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1759
0
                             "Option value has to be in [-1..15]");
1760
0
      }
1761
0
      frame_settings->values.cparams.options.predictor =
1762
0
          static_cast<jxl::Predictor>(value);
1763
0
      break;
1764
0
    case JXL_ENC_FRAME_SETTING_MODULAR_NB_PREV_CHANNELS:
1765
      // The max allowed value can in theory be higher. However, it depends on
1766
      // the effort setting. 11 is the highest safe value that doesn't cause
1767
      // tree_samples to be >= 64 in the encoder. The specification may allow
1768
      // more than this. With more fine tuning higher values could be allowed.
1769
      // For N-channel images, the largest useful value is N-1.
1770
0
      if (value < -1 || value > 11) {
1771
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1772
0
                             "Option value has to be in [-1..11]");
1773
0
      }
1774
0
      if (value == -1) {
1775
0
        frame_settings->values.cparams.options.max_properties = 0;
1776
0
      } else {
1777
0
        frame_settings->values.cparams.options.max_properties = value;
1778
0
      }
1779
0
      break;
1780
0
    case JXL_ENC_FRAME_SETTING_JPEG_RECON_CFL:
1781
0
      frame_settings->values.cparams.force_cfl_jpeg_recompression =
1782
0
          default_to_true(value);
1783
0
      break;
1784
0
    case JXL_ENC_FRAME_INDEX_BOX:
1785
0
      if (value < 0 || value > 1) {
1786
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1787
0
                             "Option value has to be 0 or 1");
1788
0
      }
1789
0
      frame_settings->values.frame_index_box = true;
1790
0
      break;
1791
0
    case JXL_ENC_FRAME_SETTING_PHOTON_NOISE:
1792
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1793
0
                           "Float option, try setting it with "
1794
0
                           "JxlEncoderFrameSettingsSetFloatOption");
1795
0
    case JXL_ENC_FRAME_SETTING_JPEG_COMPRESS_BOXES:
1796
0
      frame_settings->values.cparams.jpeg_compress_boxes =
1797
0
          default_to_true(value);
1798
0
      break;
1799
0
    case JXL_ENC_FRAME_SETTING_BUFFERING:
1800
0
      if (value < -1 || value > 3) {
1801
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1802
0
                             "Buffering has to be in [-1..3]");
1803
0
      }
1804
0
      frame_settings->values.cparams.buffering = value;
1805
0
      break;
1806
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_EXIF:
1807
0
      frame_settings->values.cparams.jpeg_keep_exif = default_to_true(value);
1808
0
      break;
1809
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_XMP:
1810
0
      frame_settings->values.cparams.jpeg_keep_xmp = default_to_true(value);
1811
0
      break;
1812
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_JUMBF:
1813
0
      frame_settings->values.cparams.jpeg_keep_jumbf = default_to_true(value);
1814
0
      break;
1815
0
    case JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS:
1816
0
      if (value < 0 || value > 1) {
1817
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1818
0
                             "Option value has to be 0 or 1");
1819
0
      }
1820
0
      frame_settings->values.cparams.use_full_image_heuristics =
1821
0
          default_to_false(value);
1822
0
      break;
1823
0
    case JXL_ENC_FRAME_SETTING_DISABLE_PERCEPTUAL_HEURISTICS:
1824
0
      if (value < 0 || value > 1) {
1825
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1826
0
                             "Option value has to be 0 or 1");
1827
0
      }
1828
0
      frame_settings->values.cparams.disable_perceptual_optimizations =
1829
0
          default_to_false(value);
1830
0
      if (frame_settings->values.cparams.disable_perceptual_optimizations &&
1831
0
          frame_settings->enc->basic_info_set &&
1832
0
          frame_settings->enc->metadata.m.xyb_encoded) {
1833
0
        return JXL_API_ERROR(
1834
0
            frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1835
0
            "Set uses_original_profile=true for non-perceptual encoding");
1836
0
      }
1837
0
      break;
1838
1839
0
    default:
1840
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1841
0
                           "Unknown option");
1842
0
  }
1843
0
  return JxlErrorOrStatus::Success();
1844
0
}
1845
1846
JxlEncoderStatus JxlEncoderFrameSettingsSetFloatOption(
1847
    JxlEncoderFrameSettings* frame_settings, JxlEncoderFrameSettingId option,
1848
0
    float value) {
1849
0
  switch (option) {
1850
0
    case JXL_ENC_FRAME_SETTING_PHOTON_NOISE:
1851
0
      if (value < 0) return JXL_ENC_ERROR;
1852
      // TODO(lode): add encoder setting to set the 8 floating point values of
1853
      // the noise synthesis parameters per frame for more fine grained control.
1854
0
      frame_settings->values.cparams.photon_noise_iso = value;
1855
0
      return JxlErrorOrStatus::Success();
1856
0
    case JXL_ENC_FRAME_SETTING_MODULAR_MA_TREE_LEARNING_PERCENT:
1857
0
      if (value < -1.f || value > 100.f) {
1858
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1859
0
                             "Option value has to be smaller than 100");
1860
0
      }
1861
      // This value is called "iterations" or "nb_repeats" in cjxl, but is in
1862
      // fact a fraction in range 0.0-1.0, with the default value 0.5.
1863
      // Convert from floating point percentage to floating point fraction here.
1864
0
      if (value < -.5f) {
1865
        // TODO(lode): for this and many other settings (also in
1866
        // JxlEncoderFrameSettingsSetOption), avoid duplicating the default
1867
        // values here and in enc_params.h and options.h, have one location
1868
        // where the defaults are specified.
1869
0
        frame_settings->values.cparams.options.nb_repeats = 0.5f;
1870
0
      } else {
1871
0
        frame_settings->values.cparams.options.nb_repeats = value * 0.01f;
1872
0
      }
1873
0
      return JxlErrorOrStatus::Success();
1874
0
    case JXL_ENC_FRAME_SETTING_CHANNEL_COLORS_GLOBAL_PERCENT:
1875
0
      if (value < -1.f || value > 100.f) {
1876
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1877
0
                             "Option value has to be in [-1..100]");
1878
0
      }
1879
0
      if (value < -.5f) {
1880
0
        frame_settings->values.cparams.channel_colors_pre_transform_percent =
1881
0
            95.0f;
1882
0
      } else {
1883
0
        frame_settings->values.cparams.channel_colors_pre_transform_percent =
1884
0
            value;
1885
0
      }
1886
0
      return JxlErrorOrStatus::Success();
1887
0
    case JXL_ENC_FRAME_SETTING_CHANNEL_COLORS_GROUP_PERCENT:
1888
0
      if (value < -1.f || value > 100.f) {
1889
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
1890
0
                             "Option value has to be in [-1..100]");
1891
0
      }
1892
0
      if (value < -.5f) {
1893
0
        frame_settings->values.cparams.channel_colors_percent = 80.0f;
1894
0
      } else {
1895
0
        frame_settings->values.cparams.channel_colors_percent = value;
1896
0
      }
1897
0
      return JxlErrorOrStatus::Success();
1898
0
    case JXL_ENC_FRAME_SETTING_EFFORT:
1899
0
    case JXL_ENC_FRAME_SETTING_DECODING_SPEED:
1900
0
    case JXL_ENC_FRAME_SETTING_RESAMPLING:
1901
0
    case JXL_ENC_FRAME_SETTING_EXTRA_CHANNEL_RESAMPLING:
1902
0
    case JXL_ENC_FRAME_SETTING_ALREADY_DOWNSAMPLED:
1903
0
    case JXL_ENC_FRAME_SETTING_NOISE:
1904
0
    case JXL_ENC_FRAME_SETTING_DOTS:
1905
0
    case JXL_ENC_FRAME_SETTING_PATCHES:
1906
0
    case JXL_ENC_FRAME_SETTING_EPF:
1907
0
    case JXL_ENC_FRAME_SETTING_GABORISH:
1908
0
    case JXL_ENC_FRAME_SETTING_MODULAR:
1909
0
    case JXL_ENC_FRAME_SETTING_KEEP_INVISIBLE:
1910
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER:
1911
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_X:
1912
0
    case JXL_ENC_FRAME_SETTING_GROUP_ORDER_CENTER_Y:
1913
0
    case JXL_ENC_FRAME_SETTING_RESPONSIVE:
1914
0
    case JXL_ENC_FRAME_SETTING_PROGRESSIVE_AC:
1915
0
    case JXL_ENC_FRAME_SETTING_QPROGRESSIVE_AC:
1916
0
    case JXL_ENC_FRAME_SETTING_PROGRESSIVE_DC:
1917
0
    case JXL_ENC_FRAME_SETTING_PALETTE_COLORS:
1918
0
    case JXL_ENC_FRAME_SETTING_LOSSY_PALETTE:
1919
0
    case JXL_ENC_FRAME_SETTING_COLOR_TRANSFORM:
1920
0
    case JXL_ENC_FRAME_SETTING_MODULAR_COLOR_SPACE:
1921
0
    case JXL_ENC_FRAME_SETTING_MODULAR_GROUP_SIZE:
1922
0
    case JXL_ENC_FRAME_SETTING_MODULAR_PREDICTOR:
1923
0
    case JXL_ENC_FRAME_SETTING_MODULAR_NB_PREV_CHANNELS:
1924
0
    case JXL_ENC_FRAME_SETTING_JPEG_RECON_CFL:
1925
0
    case JXL_ENC_FRAME_INDEX_BOX:
1926
0
    case JXL_ENC_FRAME_SETTING_BROTLI_EFFORT:
1927
0
    case JXL_ENC_FRAME_SETTING_FILL_ENUM:
1928
0
    case JXL_ENC_FRAME_SETTING_JPEG_COMPRESS_BOXES:
1929
0
    case JXL_ENC_FRAME_SETTING_BUFFERING:
1930
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_EXIF:
1931
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_XMP:
1932
0
    case JXL_ENC_FRAME_SETTING_JPEG_KEEP_JUMBF:
1933
0
    case JXL_ENC_FRAME_SETTING_USE_FULL_IMAGE_HEURISTICS:
1934
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1935
0
                           "Int option, try setting it with "
1936
0
                           "JxlEncoderFrameSettingsSetOption");
1937
0
    default:
1938
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED,
1939
0
                           "Unknown option");
1940
0
  }
1941
0
}
1942
119
JxlEncoder* JxlEncoderCreate(const JxlMemoryManager* memory_manager) {
1943
119
  JxlMemoryManager local_memory_manager;
1944
119
  if (!jxl::MemoryManagerInit(&local_memory_manager, memory_manager)) {
1945
0
    return nullptr;
1946
0
  }
1947
1948
119
  void* alloc =
1949
119
      jxl::MemoryManagerAlloc(&local_memory_manager, sizeof(JxlEncoder));
1950
119
  if (!alloc) return nullptr;
1951
119
  JxlEncoder* enc = new (alloc) JxlEncoder();
1952
119
  enc->memory_manager = local_memory_manager;
1953
  // TODO(sboukortt): add an API function to set this.
1954
119
  enc->cms = *JxlGetDefaultCms();
1955
119
  enc->cms_set = true;
1956
1957
  // Initialize all the field values.
1958
119
  JxlEncoderReset(enc);
1959
1960
119
  return enc;
1961
119
}
1962
1963
357
void JxlEncoderReset(JxlEncoder* enc) {
1964
357
  enc->thread_pool.reset();
1965
357
  enc->input_queue.clear();
1966
357
  enc->num_queued_frames = 0;
1967
357
  enc->num_queued_boxes = 0;
1968
357
  enc->encoder_options.clear();
1969
357
  enc->codestream_bytes_written_end_of_frame = 0;
1970
357
  enc->wrote_bytes = false;
1971
357
  enc->jxlp_counter = 0;
1972
357
  enc->metadata = jxl::CodecMetadata();
1973
357
  enc->last_used_cparams = jxl::CompressParams();
1974
357
  enc->frames_closed = false;
1975
357
  enc->boxes_closed = false;
1976
357
  enc->basic_info_set = false;
1977
357
  enc->color_encoding_set = false;
1978
357
  enc->intensity_target_set = false;
1979
357
  enc->use_container = false;
1980
357
  enc->use_boxes = false;
1981
357
  enc->store_jpeg_metadata = false;
1982
357
  enc->codestream_level = -1;
1983
357
  enc->output_processor =
1984
357
      JxlEncoderOutputProcessorWrapper(&enc->memory_manager);
1985
357
  JxlEncoderInitBasicInfo(&enc->basic_info);
1986
1987
  // jxl::JxlEncoderFrameIndexBox frame_index_box;
1988
  // JxlEncoderError error = JxlEncoderError::JXL_ENC_ERR_OK;
1989
  // bool allow_expert_options = false;
1990
  // int brotli_effort = -1;
1991
357
}
1992
1993
119
void JxlEncoderDestroy(JxlEncoder* enc) {
1994
119
  if (enc) {
1995
119
    JxlMemoryManager local_memory_manager = enc->memory_manager;
1996
    // Call destructor directly since custom free function is used.
1997
119
    enc->~JxlEncoder();
1998
119
    jxl::MemoryManagerFree(&local_memory_manager, enc);
1999
119
  }
2000
119
}
2001
2002
0
JxlEncoderError JxlEncoderGetError(JxlEncoder* enc) { return enc->error; }
2003
2004
JxlEncoderStatus JxlEncoderUseContainer(JxlEncoder* enc,
2005
0
                                        JXL_BOOL use_container) {
2006
0
  if (enc->wrote_bytes) {
2007
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2008
0
                         "this setting can only be set at the beginning");
2009
0
  }
2010
0
  enc->use_container = FROM_JXL_BOOL(use_container);
2011
0
  return JxlErrorOrStatus::Success();
2012
0
}
2013
2014
JxlEncoderStatus JxlEncoderStoreJPEGMetadata(JxlEncoder* enc,
2015
0
                                             JXL_BOOL store_jpeg_metadata) {
2016
0
  if (enc->wrote_bytes) {
2017
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2018
0
                         "this setting can only be set at the beginning");
2019
0
  }
2020
0
  enc->store_jpeg_metadata = FROM_JXL_BOOL(store_jpeg_metadata);
2021
0
  return JxlErrorOrStatus::Success();
2022
0
}
2023
2024
238
JxlEncoderStatus JxlEncoderSetCodestreamLevel(JxlEncoder* enc, int level) {
2025
238
  if (level != -1 && level != 5 && level != 10) {
2026
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_NOT_SUPPORTED, "invalid level");
2027
0
  }
2028
238
  if (enc->wrote_bytes) {
2029
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2030
0
                         "this setting can only be set at the beginning");
2031
0
  }
2032
238
  enc->codestream_level = level;
2033
238
  return JxlErrorOrStatus::Success();
2034
238
}
2035
2036
0
int JxlEncoderGetRequiredCodestreamLevel(const JxlEncoder* enc) {
2037
0
  return VerifyLevelSettings(enc, nullptr);
2038
0
}
2039
2040
0
void JxlEncoderSetCms(JxlEncoder* enc, JxlCmsInterface cms) {
2041
0
  jxl::msan::MemoryIsInitialized(&cms, sizeof(cms));
2042
0
  enc->cms = cms;
2043
0
  enc->cms_set = true;
2044
0
}
2045
2046
JxlEncoderStatus JxlEncoderSetParallelRunner(JxlEncoder* enc,
2047
                                             JxlParallelRunner parallel_runner,
2048
238
                                             void* parallel_runner_opaque) {
2049
238
  if (enc->thread_pool) {
2050
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2051
0
                         "parallel runner already set");
2052
0
  }
2053
238
  enc->thread_pool = jxl::MemoryManagerMakeUnique<jxl::ThreadPool>(
2054
238
      &enc->memory_manager, parallel_runner, parallel_runner_opaque);
2055
238
  if (!enc->thread_pool) {
2056
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_GENERIC,
2057
0
                         "error setting parallel runner");
2058
0
  }
2059
238
  return JxlErrorOrStatus::Success();
2060
238
}
2061
2062
namespace {
2063
JxlEncoderStatus GetCurrentDimensions(
2064
    const JxlEncoderFrameSettings* frame_settings, size_t& xsize,
2065
238
    size_t& ysize) {
2066
238
  xsize = frame_settings->enc->metadata.xsize();
2067
238
  ysize = frame_settings->enc->metadata.ysize();
2068
238
  if (frame_settings->values.header.layer_info.have_crop) {
2069
0
    xsize = frame_settings->values.header.layer_info.xsize;
2070
0
    ysize = frame_settings->values.header.layer_info.ysize;
2071
0
  }
2072
238
  if (frame_settings->values.cparams.already_downsampled) {
2073
0
    size_t factor = frame_settings->values.cparams.resampling;
2074
0
    xsize = jxl::DivCeil(xsize, factor);
2075
0
    ysize = jxl::DivCeil(ysize, factor);
2076
0
  }
2077
238
  if (xsize == 0 || ysize == 0) {
2078
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2079
0
                         "zero-sized frame is not allowed");
2080
0
  }
2081
238
  return JxlErrorOrStatus::Success();
2082
238
}
2083
}  // namespace
2084
2085
JxlEncoderStatus JxlEncoderAddJPEGFrame(
2086
    const JxlEncoderFrameSettings* frame_settings, const uint8_t* buffer,
2087
0
    size_t size) {
2088
0
  if (frame_settings->enc->frames_closed) {
2089
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2090
0
                         "Frame input is already closed");
2091
0
  }
2092
2093
0
  jxl::CodecInOut io{&frame_settings->enc->memory_manager};
2094
0
  if (!jxl::jpeg::DecodeImageJPG(jxl::Bytes(buffer, size), &io)) {
2095
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_BAD_INPUT,
2096
0
                         "Error during decode of input JPEG");
2097
0
  }
2098
2099
0
  if (!frame_settings->enc->color_encoding_set) {
2100
0
    SetColorEncodingFromJpegData(
2101
0
        *io.Main().jpeg_data, &frame_settings->enc->metadata.m.color_encoding);
2102
0
    frame_settings->enc->color_encoding_set = true;
2103
0
  }
2104
2105
0
  if (!frame_settings->enc->basic_info_set) {
2106
0
    JxlBasicInfo basic_info;
2107
0
    JxlEncoderInitBasicInfo(&basic_info);
2108
0
    basic_info.xsize = io.Main().jpeg_data->width;
2109
0
    basic_info.ysize = io.Main().jpeg_data->height;
2110
0
    basic_info.uses_original_profile = JXL_TRUE;
2111
0
    if (JxlEncoderSetBasicInfo(frame_settings->enc, &basic_info) !=
2112
0
        JXL_ENC_SUCCESS) {
2113
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2114
0
                           "Error setting basic info");
2115
0
    }
2116
0
  }
2117
2118
0
  size_t xsize;
2119
0
  size_t ysize;
2120
0
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2121
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2122
0
                         "bad dimensions");
2123
0
  }
2124
0
  if (xsize != static_cast<size_t>(io.Main().jpeg_data->width) ||
2125
0
      ysize != static_cast<size_t>(io.Main().jpeg_data->height)) {
2126
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2127
0
                         "JPEG dimensions don't match frame dimensions");
2128
0
  }
2129
2130
0
  if (frame_settings->enc->metadata.m.xyb_encoded) {
2131
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2132
0
                         "Can't XYB encode a lossless JPEG");
2133
0
  }
2134
0
  if (!io.blobs.exif.empty()) {
2135
0
    JxlOrientation orientation = static_cast<JxlOrientation>(
2136
0
        frame_settings->enc->metadata.m.orientation);
2137
0
    jxl::InterpretExif(io.blobs.exif, &orientation);
2138
0
    frame_settings->enc->metadata.m.orientation = orientation;
2139
0
  }
2140
0
  if (!io.blobs.exif.empty() && frame_settings->values.cparams.jpeg_keep_exif) {
2141
0
    size_t exif_size = io.blobs.exif.size();
2142
    // Exif data in JPEG is limited to 64k
2143
0
    if (exif_size > 0xFFFF) {
2144
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2145
0
                           "Exif larger than possible in JPEG?");
2146
0
    }
2147
0
    exif_size += 4;  // prefix 4 zero bytes for tiff offset
2148
0
    std::vector<uint8_t> exif(exif_size);
2149
0
    memcpy(exif.data() + 4, io.blobs.exif.data(), io.blobs.exif.size());
2150
0
    JxlEncoderUseBoxes(frame_settings->enc);
2151
0
    JxlEncoderAddBox(
2152
0
        frame_settings->enc, "Exif", exif.data(), exif_size,
2153
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2154
0
  }
2155
0
  if (!io.blobs.xmp.empty() && frame_settings->values.cparams.jpeg_keep_xmp) {
2156
0
    JxlEncoderUseBoxes(frame_settings->enc);
2157
0
    JxlEncoderAddBox(
2158
0
        frame_settings->enc, "xml ", io.blobs.xmp.data(), io.blobs.xmp.size(),
2159
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2160
0
  }
2161
0
  if (!io.blobs.jumbf.empty() &&
2162
0
      frame_settings->values.cparams.jpeg_keep_jumbf) {
2163
0
    JxlEncoderUseBoxes(frame_settings->enc);
2164
0
    JxlEncoderAddBox(
2165
0
        frame_settings->enc, "jumb", io.blobs.jumbf.data(),
2166
0
        io.blobs.jumbf.size(),
2167
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2168
0
  }
2169
0
  if (frame_settings->enc->store_jpeg_metadata) {
2170
0
    if (!frame_settings->values.cparams.jpeg_keep_exif ||
2171
0
        !frame_settings->values.cparams.jpeg_keep_xmp) {
2172
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2173
0
                           "Need to preserve EXIF and XMP to allow JPEG "
2174
0
                           "bitstream reconstruction");
2175
0
    }
2176
0
    jxl::jpeg::JPEGData data_in = *io.Main().jpeg_data;
2177
0
    std::vector<uint8_t> jpeg_data;
2178
0
    if (!jxl::jpeg::EncodeJPEGData(&frame_settings->enc->memory_manager,
2179
0
                                   data_in, &jpeg_data,
2180
0
                                   frame_settings->values.cparams)) {
2181
0
      return JXL_API_ERROR(
2182
0
          frame_settings->enc, JXL_ENC_ERR_JBRD,
2183
0
          "JPEG bitstream reconstruction data cannot be encoded");
2184
0
    }
2185
0
    frame_settings->enc->jpeg_metadata = jpeg_data;
2186
0
  }
2187
2188
0
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2189
0
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2190
0
  frame_data.SetJPEGData(std::move(io.Main().jpeg_data));
2191
2192
0
  auto queued_frame = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedFrame>(
2193
0
      &frame_settings->enc->memory_manager,
2194
      // JxlEncoderQueuedFrame is a struct with no constructors, so we use the
2195
      // default move constructor there.
2196
0
      jxl::JxlEncoderQueuedFrame{
2197
0
          frame_settings->values, std::move(frame_data), {}});
2198
0
  if (!queued_frame) {
2199
    // TODO(jon): when can this happen? is this an API usage error?
2200
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2201
0
                         "No frame queued?");
2202
0
  }
2203
0
  queued_frame->ec_initialized.resize(
2204
0
      frame_settings->enc->metadata.m.num_extra_channels);
2205
2206
0
  QueueFrame(frame_settings, queued_frame);
2207
0
  return JxlErrorOrStatus::Success();
2208
0
}
2209
2210
static bool CanDoFastLossless(const JxlEncoderFrameSettings* frame_settings,
2211
                              const JxlPixelFormat* pixel_format,
2212
238
                              bool has_alpha) {
2213
238
  if (!frame_settings->values.lossless) {
2214
238
    return false;
2215
238
  }
2216
  // TODO(veluca): many of the following options could be made to work, but are
2217
  // just not implemented in FJXL's frame header handling yet.
2218
0
  if (frame_settings->values.frame_index_box) {
2219
0
    return false;
2220
0
  }
2221
0
  if (frame_settings->values.header.layer_info.have_crop) {
2222
0
    return false;
2223
0
  }
2224
0
  if (frame_settings->enc->metadata.m.have_animation) {
2225
0
    return false;
2226
0
  }
2227
0
  if (frame_settings->values.cparams.speed_tier != jxl::SpeedTier::kLightning) {
2228
0
    return false;
2229
0
  }
2230
0
  if (frame_settings->values.image_bit_depth.type ==
2231
0
          JxlBitDepthType::JXL_BIT_DEPTH_CUSTOM &&
2232
0
      frame_settings->values.image_bit_depth.bits_per_sample !=
2233
0
          frame_settings->enc->metadata.m.bit_depth.bits_per_sample) {
2234
0
    return false;
2235
0
  }
2236
  // TODO(veluca): implement support for LSB-padded input in fast_lossless.
2237
0
  if (frame_settings->values.image_bit_depth.type ==
2238
0
          JxlBitDepthType::JXL_BIT_DEPTH_FROM_PIXEL_FORMAT &&
2239
0
      frame_settings->enc->metadata.m.bit_depth.bits_per_sample % 8 != 0) {
2240
0
    return false;
2241
0
  }
2242
0
  if (!frame_settings->values.frame_name.empty()) {
2243
0
    return false;
2244
0
  }
2245
  // No extra channels other than alpha.
2246
0
  if (!(has_alpha && frame_settings->enc->metadata.m.num_extra_channels == 1) &&
2247
0
      frame_settings->enc->metadata.m.num_extra_channels != 0) {
2248
0
    return false;
2249
0
  }
2250
0
  if (frame_settings->enc->metadata.m.bit_depth.bits_per_sample > 16) {
2251
0
    return false;
2252
0
  }
2253
0
  if (pixel_format->data_type != JxlDataType::JXL_TYPE_FLOAT16 &&
2254
0
      pixel_format->data_type != JxlDataType::JXL_TYPE_UINT16 &&
2255
0
      pixel_format->data_type != JxlDataType::JXL_TYPE_UINT8) {
2256
0
    return false;
2257
0
  }
2258
0
  if ((frame_settings->enc->metadata.m.bit_depth.bits_per_sample > 8) !=
2259
0
      (pixel_format->data_type == JxlDataType::JXL_TYPE_UINT16 ||
2260
0
       pixel_format->data_type == JxlDataType::JXL_TYPE_FLOAT16)) {
2261
0
    return false;
2262
0
  }
2263
0
  if (!((pixel_format->num_channels == 1 || pixel_format->num_channels == 3) &&
2264
0
        !has_alpha) &&
2265
0
      !((pixel_format->num_channels == 2 || pixel_format->num_channels == 4) &&
2266
0
        has_alpha)) {
2267
0
    return false;
2268
0
  }
2269
2270
0
  return true;
2271
0
}
2272
2273
namespace {
2274
JxlEncoderStatus JxlEncoderAddImageFrameInternal(
2275
    const JxlEncoderFrameSettings* frame_settings, size_t xsize, size_t ysize,
2276
238
    bool streaming, jxl::JxlEncoderChunkedFrameAdapter&& frame_data) {
2277
238
  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
2278
238
  {
2279
238
    JxlChunkedFrameInputSource input = frame_data.GetInputSource();
2280
238
    input.get_color_channels_pixel_format(input.opaque, &pixel_format);
2281
238
  }
2282
238
  uint32_t num_channels = pixel_format.num_channels;
2283
238
  size_t has_interleaved_alpha =
2284
238
      static_cast<size_t>(num_channels == 2 || num_channels == 4);
2285
2286
238
  if (!frame_settings->enc->basic_info_set) {
2287
    // Basic Info must be set. Otherwise, this is an API misuse.
2288
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2289
0
                         "Basic info or color encoding not set yet");
2290
0
  }
2291
238
  if (frame_settings->enc->frames_closed) {
2292
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2293
0
                         "Frame input already closed");
2294
0
  }
2295
238
  if (num_channels < 3) {
2296
0
    if (frame_settings->enc->basic_info.num_color_channels != 1) {
2297
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2298
0
                           "Grayscale pixel format input for an RGB image");
2299
0
    }
2300
238
  } else {
2301
238
    if (frame_settings->enc->basic_info.num_color_channels != 3) {
2302
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2303
0
                           "RGB pixel format input for a grayscale image");
2304
0
    }
2305
238
  }
2306
238
  if (frame_settings->values.lossless &&
2307
238
      frame_settings->enc->metadata.m.xyb_encoded) {
2308
0
    return JXL_API_ERROR(
2309
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2310
0
        "Set uses_original_profile=true for lossless encoding");
2311
0
  }
2312
238
  if (frame_settings->values.cparams.disable_perceptual_optimizations &&
2313
238
      frame_settings->enc->metadata.m.xyb_encoded) {
2314
0
    return JXL_API_ERROR(
2315
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2316
0
        "Set uses_original_profile=true for non-perceptual encoding");
2317
0
  }
2318
238
  if (JXL_ENC_SUCCESS !=
2319
238
      VerifyInputBitDepth(frame_settings->values.image_bit_depth,
2320
238
                          pixel_format)) {
2321
0
    return JXL_API_ERROR_NOSET("Invalid input bit depth");
2322
0
  }
2323
238
  if (has_interleaved_alpha >
2324
238
      frame_settings->enc->metadata.m.num_extra_channels) {
2325
0
    return JXL_API_ERROR(
2326
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2327
0
        "number of extra channels mismatch (need 1 extra channel for alpha)");
2328
0
  }
2329
2330
238
  bool has_alpha = frame_settings->enc->metadata.m.HasAlpha();
2331
2332
  // All required conditions to do fast-lossless.
2333
238
  if (CanDoFastLossless(frame_settings, &pixel_format, has_alpha)) {
2334
0
    const bool big_endian =
2335
0
        pixel_format.endianness == JXL_BIG_ENDIAN ||
2336
0
        (pixel_format.endianness == JXL_NATIVE_ENDIAN && !IsLittleEndian());
2337
2338
0
    RunnerTicket ticket{frame_settings->enc->thread_pool.get()};
2339
0
    JXL_BOOL oneshot = TO_JXL_BOOL(!frame_data.StreamingInput());
2340
0
    auto* frame_state = JxlFastLosslessPrepareFrame(
2341
0
        frame_data.GetInputSource(), xsize, ysize, num_channels,
2342
0
        frame_settings->enc->metadata.m.bit_depth.bits_per_sample, big_endian,
2343
0
        /*effort=*/2, oneshot);
2344
0
    if (!streaming) {
2345
0
      bool ok =
2346
0
          JxlFastLosslessProcessFrame(frame_state, /*is_last=*/false, &ticket,
2347
0
                                      &FastLosslessRunnerAdapter, nullptr);
2348
0
      if (!ok || ticket.has_error) {
2349
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2350
0
                             "Internal: JxlFastLosslessProcessFrame failed");
2351
0
      }
2352
0
    }
2353
0
    QueueFastLosslessFrame(frame_settings, frame_state);
2354
0
    return JxlErrorOrStatus::Success();
2355
0
  }
2356
2357
238
  if (!streaming) {
2358
    // The input callbacks are only guaranteed to be available during frame
2359
    // encoding when both the input and the output is streaming. In all other
2360
    // cases we need to create an internal copy of the frame data.
2361
238
    if (!frame_data.CopyBuffers()) {
2362
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2363
0
                           "Invalid chunked frame input source");
2364
0
    }
2365
238
  }
2366
2367
238
  if (!frame_settings->enc->color_encoding_set) {
2368
0
    jxl::ColorEncoding c_current;
2369
0
    if ((pixel_format.data_type == JXL_TYPE_FLOAT) ||
2370
0
        (pixel_format.data_type == JXL_TYPE_FLOAT16)) {
2371
0
      c_current = jxl::ColorEncoding::LinearSRGB(num_channels < 3);
2372
0
    } else {
2373
0
      c_current = jxl::ColorEncoding::SRGB(num_channels < 3);
2374
0
    }
2375
0
    frame_settings->enc->metadata.m.color_encoding = c_current;
2376
0
  }
2377
2378
238
  auto queued_frame = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedFrame>(
2379
238
      &frame_settings->enc->memory_manager,
2380
      // JxlEncoderQueuedFrame is a struct with no constructors, so we use the
2381
      // default move constructor there.
2382
238
      jxl::JxlEncoderQueuedFrame{
2383
238
          frame_settings->values, std::move(frame_data), {}});
2384
2385
238
  if (!queued_frame) {
2386
    // TODO(jon): when can this happen? is this an API usage error?
2387
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2388
0
                         "No frame queued?");
2389
0
  }
2390
2391
238
  for (auto& ec_info : frame_settings->enc->metadata.m.extra_channel_info) {
2392
0
    if (has_interleaved_alpha && ec_info.type == jxl::ExtraChannel::kAlpha) {
2393
0
      queued_frame->ec_initialized.push_back(1);
2394
0
      has_interleaved_alpha = 0;  // only first Alpha is initialized
2395
0
    } else {
2396
0
      queued_frame->ec_initialized.push_back(0);
2397
0
    }
2398
0
  }
2399
238
  queued_frame->option_values.cparams.level =
2400
238
      frame_settings->enc->codestream_level;
2401
2402
238
  QueueFrame(frame_settings, queued_frame);
2403
238
  return JxlErrorOrStatus::Success();
2404
238
}
2405
}  // namespace
2406
2407
JxlEncoderStatus JxlEncoderAddImageFrame(
2408
    const JxlEncoderFrameSettings* frame_settings,
2409
238
    const JxlPixelFormat* pixel_format, const void* buffer, size_t size) {
2410
238
  size_t xsize;
2411
238
  size_t ysize;
2412
238
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2413
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2414
0
                         "bad dimensions");
2415
0
  }
2416
238
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2417
238
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2418
238
  if (!frame_data.SetFromBuffer(0, reinterpret_cast<const uint8_t*>(buffer),
2419
238
                                size, *pixel_format)) {
2420
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2421
0
                         "provided image buffer too small");
2422
0
  }
2423
238
  return JxlEncoderAddImageFrameInternal(frame_settings, xsize, ysize,
2424
238
                                         /*streaming=*/false,
2425
238
                                         std::move(frame_data));
2426
238
}
2427
2428
JxlEncoderStatus JxlEncoderAddChunkedFrame(
2429
    const JxlEncoderFrameSettings* frame_settings, JXL_BOOL is_last_frame,
2430
0
    JxlChunkedFrameInputSource chunked_frame_input) {
2431
0
  size_t xsize;
2432
0
  size_t ysize;
2433
0
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2434
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2435
0
                         "bad dimensions");
2436
0
  }
2437
0
  bool streaming = frame_settings->enc->output_processor.OutputProcessorSet();
2438
0
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2439
0
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2440
0
  frame_data.SetInputSource(chunked_frame_input);
2441
0
  auto status = JxlEncoderAddImageFrameInternal(
2442
0
      frame_settings, xsize, ysize, streaming, std::move(frame_data));
2443
0
  if (status != JXL_ENC_SUCCESS) return status;
2444
2445
0
  auto& queued_frame = frame_settings->enc->input_queue.back();
2446
0
  if (queued_frame.frame) {
2447
0
    for (auto& val : queued_frame.frame->ec_initialized) val = 1;
2448
0
  }
2449
2450
0
  if (is_last_frame) {
2451
0
    JxlEncoderCloseInput(frame_settings->enc);
2452
0
  }
2453
0
  if (streaming) {
2454
0
    return JxlEncoderFlushInput(frame_settings->enc);
2455
0
  }
2456
0
  return JxlErrorOrStatus::Success();
2457
0
}
2458
2459
0
JxlEncoderStatus JxlEncoderUseBoxes(JxlEncoder* enc) {
2460
0
  if (enc->wrote_bytes) {
2461
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2462
0
                         "this setting can only be set at the beginning");
2463
0
  }
2464
0
  enc->use_boxes = true;
2465
0
  return JxlErrorOrStatus::Success();
2466
0
}
2467
2468
JxlEncoderStatus JxlEncoderAddBox(JxlEncoder* enc, const JxlBoxType type,
2469
                                  const uint8_t* contents, size_t size,
2470
0
                                  JXL_BOOL compress_box) {
2471
0
  if (!enc->use_boxes) {
2472
0
    return JXL_API_ERROR(
2473
0
        enc, JXL_ENC_ERR_API_USAGE,
2474
0
        "must set JxlEncoderUseBoxes at the beginning to add boxes");
2475
0
  }
2476
0
  if (enc->boxes_closed) {
2477
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2478
0
                         "Box input already closed");
2479
0
  }
2480
0
  if (compress_box) {
2481
0
    if (memcmp("jxl", type, 3) == 0) {
2482
0
      return JXL_API_ERROR(
2483
0
          enc, JXL_ENC_ERR_API_USAGE,
2484
0
          "brob box may not contain a type starting with \"jxl\"");
2485
0
    }
2486
0
    if (memcmp("jbrd", type, 4) == 0) {
2487
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2488
0
                           "jbrd box may not be brob compressed");
2489
0
    }
2490
0
    if (memcmp("brob", type, 4) == 0) {
2491
      // The compress_box will compress an existing non-brob box into a brob
2492
      // box. If already giving a valid brotli-compressed brob box, set
2493
      // compress_box to false since it is already compressed.
2494
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2495
0
                           "a brob box cannot contain another brob box");
2496
0
    }
2497
0
  }
2498
2499
0
  auto box = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedBox>(
2500
0
      &enc->memory_manager);
2501
2502
0
  box->type = jxl::MakeBoxType(type);
2503
0
  box->contents.assign(contents, contents + size);
2504
0
  box->compress_box = FROM_JXL_BOOL(compress_box);
2505
0
  QueueBox(enc, box);
2506
0
  return JxlErrorOrStatus::Success();
2507
0
}
2508
2509
JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelBuffer(
2510
    const JxlEncoderFrameSettings* frame_settings,
2511
    const JxlPixelFormat* pixel_format, const void* buffer, size_t size,
2512
0
    uint32_t index) {
2513
0
  if (index >= frame_settings->enc->metadata.m.num_extra_channels) {
2514
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2515
0
                         "Invalid value for the index of extra channel");
2516
0
  }
2517
0
  if (!frame_settings->enc->basic_info_set ||
2518
0
      !frame_settings->enc->color_encoding_set) {
2519
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2520
0
                         "Basic info has to be set first");
2521
0
  }
2522
0
  if (frame_settings->enc->input_queue.empty()) {
2523
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2524
0
                         "First add image frame, then extra channels");
2525
0
  }
2526
0
  if (frame_settings->enc->frames_closed) {
2527
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2528
0
                         "Frame input already closed");
2529
0
  }
2530
0
  JxlPixelFormat ec_format = *pixel_format;
2531
0
  ec_format.num_channels = 1;
2532
0
  if (JXL_ENC_SUCCESS !=
2533
0
      VerifyInputBitDepth(frame_settings->values.image_bit_depth, ec_format)) {
2534
0
    return JXL_API_ERROR_NOSET("Invalid input bit depth");
2535
0
  }
2536
0
  const uint8_t* uint8_buffer = reinterpret_cast<const uint8_t*>(buffer);
2537
0
  auto* queued_frame = frame_settings->enc->input_queue.back().frame.get();
2538
0
  if (!queued_frame->frame_data.SetFromBuffer(1 + index, uint8_buffer, size,
2539
0
                                              ec_format)) {
2540
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2541
0
                         "provided image buffer too small");
2542
0
  }
2543
0
  queued_frame->ec_initialized[index] = 1;
2544
2545
0
  return JxlErrorOrStatus::Success();
2546
0
}
2547
2548
0
void JxlEncoderCloseFrames(JxlEncoder* enc) { enc->frames_closed = true; }
2549
2550
0
void JxlEncoderCloseBoxes(JxlEncoder* enc) { enc->boxes_closed = true; }
2551
2552
0
void JxlEncoderCloseInput(JxlEncoder* enc) {
2553
0
  JxlEncoderCloseFrames(enc);
2554
0
  JxlEncoderCloseBoxes(enc);
2555
0
}
2556
2557
0
JXL_EXPORT JxlEncoderStatus JxlEncoderFlushInput(JxlEncoder* enc) {
2558
0
  if (!enc->output_processor.OutputProcessorSet()) {
2559
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2560
0
                         "Cannot flush input without setting output "
2561
0
                         "processor with JxlEncoderSetOutputProcessor");
2562
0
  }
2563
0
  while (!enc->input_queue.empty()) {
2564
0
    if (!enc->ProcessOneEnqueuedInput()) {
2565
0
      return JxlErrorOrStatus::Error();
2566
0
    }
2567
0
  }
2568
0
  return JxlErrorOrStatus::Success();
2569
0
}
2570
2571
JXL_EXPORT JxlEncoderStatus JxlEncoderSetOutputProcessor(
2572
0
    JxlEncoder* enc, JxlEncoderOutputProcessor output_processor) {
2573
0
  if (enc->output_processor.HasAvailOut()) {
2574
0
    return JXL_API_ERROR(
2575
0
        enc, JXL_ENC_ERR_API_USAGE,
2576
0
        "Cannot set an output processor when some output was already produced");
2577
0
  }
2578
0
  if (!output_processor.set_finalized_position ||
2579
0
      !output_processor.get_buffer || !output_processor.release_buffer) {
2580
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2581
0
                         "Missing output processor functions");
2582
0
  }
2583
0
  enc->output_processor =
2584
0
      JxlEncoderOutputProcessorWrapper(&enc->memory_manager, output_processor);
2585
0
  return JxlErrorOrStatus::Success();
2586
0
}
2587
2588
JxlEncoderStatus JxlEncoderProcessOutput(JxlEncoder* enc, uint8_t** next_out,
2589
476
                                         size_t* avail_out) {
2590
476
  if (enc->output_processor.OutputProcessorSet()) {
2591
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2592
0
                         "Cannot call JxlEncoderProcessOutput after calling "
2593
0
                         "JxlEncoderSetOutputProcessor");
2594
0
  }
2595
476
  if (!enc->output_processor.SetAvailOut(next_out, avail_out)) {
2596
0
    return JxlErrorOrStatus::Error();
2597
0
  }
2598
714
  while (*avail_out != 0 && !enc->input_queue.empty()) {
2599
238
    if (!enc->ProcessOneEnqueuedInput()) {
2600
0
      return JxlErrorOrStatus::Error();
2601
0
    }
2602
238
  }
2603
2604
476
  if (!enc->input_queue.empty() || enc->output_processor.HasOutputToWrite()) {
2605
238
    return JxlErrorOrStatus::MoreOutput();
2606
238
  }
2607
238
  return JxlErrorOrStatus::Success();
2608
476
}
2609
2610
JxlEncoderStatus JxlEncoderSetFrameHeader(
2611
    JxlEncoderFrameSettings* frame_settings,
2612
238
    const JxlFrameHeader* frame_header) {
2613
238
  if (frame_header->layer_info.blend_info.source > 3) {
2614
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2615
0
                         "invalid blending source index");
2616
0
  }
2617
  // If there are no extra channels, it's ok for the value to be 0.
2618
238
  if (frame_header->layer_info.blend_info.alpha != 0 &&
2619
238
      frame_header->layer_info.blend_info.alpha >=
2620
0
          frame_settings->enc->metadata.m.extra_channel_info.size()) {
2621
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2622
0
                         "alpha blend channel index out of bounds");
2623
0
  }
2624
2625
238
  frame_settings->values.header = *frame_header;
2626
  // Setting the frame header resets the frame name, it must be set again with
2627
  // JxlEncoderSetFrameName if desired.
2628
238
  frame_settings->values.frame_name = "";
2629
2630
238
  return JxlErrorOrStatus::Success();
2631
238
}
2632
2633
JxlEncoderStatus JxlEncoderSetExtraChannelBlendInfo(
2634
    JxlEncoderFrameSettings* frame_settings, size_t index,
2635
0
    const JxlBlendInfo* blend_info) {
2636
0
  if (index >= frame_settings->enc->metadata.m.num_extra_channels) {
2637
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2638
0
                         "Invalid value for the index of extra channel");
2639
0
  }
2640
2641
0
  if (frame_settings->values.extra_channel_blend_info.size() !=
2642
0
      frame_settings->enc->metadata.m.num_extra_channels) {
2643
0
    JxlBlendInfo default_blend_info;
2644
0
    JxlEncoderInitBlendInfo(&default_blend_info);
2645
0
    frame_settings->values.extra_channel_blend_info.resize(
2646
0
        frame_settings->enc->metadata.m.num_extra_channels, default_blend_info);
2647
0
  }
2648
0
  frame_settings->values.extra_channel_blend_info[index] = *blend_info;
2649
0
  return JxlErrorOrStatus::Success();
2650
0
}
2651
2652
JxlEncoderStatus JxlEncoderSetFrameName(JxlEncoderFrameSettings* frame_settings,
2653
0
                                        const char* frame_name) {
2654
0
  std::string str = frame_name ? frame_name : "";
2655
0
  if (str.size() > 1071) {
2656
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2657
0
                         "frame name can be max 1071 bytes long");
2658
0
  }
2659
0
  frame_settings->values.frame_name = str;
2660
0
  frame_settings->values.header.name_length = str.size();
2661
0
  return JxlErrorOrStatus::Success();
2662
0
}
2663
2664
JxlEncoderStatus JxlEncoderSetFrameBitDepth(
2665
0
    JxlEncoderFrameSettings* frame_settings, const JxlBitDepth* bit_depth) {
2666
0
  if (bit_depth->type != JXL_BIT_DEPTH_FROM_PIXEL_FORMAT &&
2667
0
      bit_depth->type != JXL_BIT_DEPTH_FROM_CODESTREAM) {
2668
0
    return JXL_API_ERROR_NOSET(
2669
0
        "Only JXL_BIT_DEPTH_FROM_PIXEL_FORMAT and "
2670
0
        "JXL_BIT_DEPTH_FROM_CODESTREAM is implemented "
2671
0
        "for input buffers.");
2672
0
  }
2673
0
  frame_settings->values.image_bit_depth = *bit_depth;
2674
0
  return JxlErrorOrStatus::Success();
2675
0
}
2676
2677
void JxlColorEncodingSetToSRGB(JxlColorEncoding* color_encoding,
2678
0
                               JXL_BOOL is_gray) {
2679
0
  *color_encoding =
2680
0
      jxl::ColorEncoding::SRGB(FROM_JXL_BOOL(is_gray)).ToExternal();
2681
0
}
2682
2683
void JxlColorEncodingSetToLinearSRGB(JxlColorEncoding* color_encoding,
2684
0
                                     JXL_BOOL is_gray) {
2685
0
  *color_encoding =
2686
0
      jxl::ColorEncoding::LinearSRGB(FROM_JXL_BOOL(is_gray)).ToExternal();
2687
0
}
2688
2689
0
void JxlEncoderAllowExpertOptions(JxlEncoder* enc) {
2690
0
  enc->allow_expert_options = true;
2691
0
}
2692
2693
JXL_EXPORT void JxlEncoderSetDebugImageCallback(
2694
    JxlEncoderFrameSettings* frame_settings, JxlDebugImageCallback callback,
2695
0
    void* opaque) {
2696
0
  frame_settings->values.cparams.debug_image = callback;
2697
0
  frame_settings->values.cparams.debug_image_opaque = opaque;
2698
0
}
2699
2700
0
JXL_EXPORT JxlEncoderStats* JxlEncoderStatsCreate() {
2701
0
  JxlEncoderStats* result = new JxlEncoderStats();
2702
0
  result->aux_out = jxl::make_unique<jxl::AuxOut>();
2703
0
  return result;
2704
0
}
2705
2706
0
JXL_EXPORT void JxlEncoderStatsDestroy(JxlEncoderStats* stats) { delete stats; }
2707
2708
JXL_EXPORT void JxlEncoderCollectStats(JxlEncoderFrameSettings* frame_settings,
2709
0
                                       JxlEncoderStats* stats) {
2710
0
  if (!stats) return;
2711
0
  frame_settings->values.aux_out = stats->aux_out.get();
2712
0
}
2713
2714
JXL_EXPORT size_t JxlEncoderStatsGet(const JxlEncoderStats* stats,
2715
0
                                     JxlEncoderStatsKey key) {
2716
0
  if (!stats) return 0;
2717
0
  const jxl::AuxOut& aux_out = *stats->aux_out;
2718
0
  switch (key) {
2719
0
    case JXL_ENC_STAT_HEADER_BITS:
2720
0
      return aux_out.layer(jxl::LayerType::Header).total_bits;
2721
0
    case JXL_ENC_STAT_TOC_BITS:
2722
0
      return aux_out.layer(jxl::LayerType::Toc).total_bits;
2723
0
    case JXL_ENC_STAT_DICTIONARY_BITS:
2724
0
      return aux_out.layer(jxl::LayerType::Dictionary).total_bits;
2725
0
    case JXL_ENC_STAT_SPLINES_BITS:
2726
0
      return aux_out.layer(jxl::LayerType::Splines).total_bits;
2727
0
    case JXL_ENC_STAT_NOISE_BITS:
2728
0
      return aux_out.layer(jxl::LayerType::Noise).total_bits;
2729
0
    case JXL_ENC_STAT_QUANT_BITS:
2730
0
      return aux_out.layer(jxl::LayerType::Quant).total_bits;
2731
0
    case JXL_ENC_STAT_MODULAR_TREE_BITS:
2732
0
      return aux_out.layer(jxl::LayerType::ModularTree).total_bits;
2733
0
    case JXL_ENC_STAT_MODULAR_GLOBAL_BITS:
2734
0
      return aux_out.layer(jxl::LayerType::ModularGlobal).total_bits;
2735
0
    case JXL_ENC_STAT_DC_BITS:
2736
0
      return aux_out.layer(jxl::LayerType::Dc).total_bits;
2737
0
    case JXL_ENC_STAT_MODULAR_DC_GROUP_BITS:
2738
0
      return aux_out.layer(jxl::LayerType::ModularDcGroup).total_bits;
2739
0
    case JXL_ENC_STAT_CONTROL_FIELDS_BITS:
2740
0
      return aux_out.layer(jxl::LayerType::ControlFields).total_bits;
2741
0
    case JXL_ENC_STAT_COEF_ORDER_BITS:
2742
0
      return aux_out.layer(jxl::LayerType::Order).total_bits;
2743
0
    case JXL_ENC_STAT_AC_HISTOGRAM_BITS:
2744
0
      return aux_out.layer(jxl::LayerType::Ac).total_bits;
2745
0
    case JXL_ENC_STAT_AC_BITS:
2746
0
      return aux_out.layer(jxl::LayerType::AcTokens).total_bits;
2747
0
    case JXL_ENC_STAT_MODULAR_AC_GROUP_BITS:
2748
0
      return aux_out.layer(jxl::LayerType::ModularAcGroup).total_bits;
2749
0
    case JXL_ENC_STAT_NUM_SMALL_BLOCKS:
2750
0
      return aux_out.num_small_blocks;
2751
0
    case JXL_ENC_STAT_NUM_DCT4X8_BLOCKS:
2752
0
      return aux_out.num_dct4x8_blocks;
2753
0
    case JXL_ENC_STAT_NUM_AFV_BLOCKS:
2754
0
      return aux_out.num_afv_blocks;
2755
0
    case JXL_ENC_STAT_NUM_DCT8_BLOCKS:
2756
0
      return aux_out.num_dct8_blocks;
2757
0
    case JXL_ENC_STAT_NUM_DCT8X32_BLOCKS:
2758
0
      return aux_out.num_dct16_blocks;
2759
0
    case JXL_ENC_STAT_NUM_DCT16_BLOCKS:
2760
0
      return aux_out.num_dct16x32_blocks;
2761
0
    case JXL_ENC_STAT_NUM_DCT16X32_BLOCKS:
2762
0
      return aux_out.num_dct32_blocks;
2763
0
    case JXL_ENC_STAT_NUM_DCT32_BLOCKS:
2764
0
      return aux_out.num_dct32x64_blocks;
2765
0
    case JXL_ENC_STAT_NUM_DCT32X64_BLOCKS:
2766
0
      return aux_out.num_dct32x64_blocks;
2767
0
    case JXL_ENC_STAT_NUM_DCT64_BLOCKS:
2768
0
      return aux_out.num_dct64_blocks;
2769
0
    case JXL_ENC_STAT_NUM_BUTTERAUGLI_ITERS:
2770
0
      return aux_out.num_butteraugli_iters;
2771
0
    default:
2772
0
      return 0;
2773
0
  }
2774
0
}
2775
2776
JXL_EXPORT void JxlEncoderStatsMerge(JxlEncoderStats* stats,
2777
0
                                     const JxlEncoderStats* other) {
2778
0
  if (!stats || !other) return;
2779
0
  stats->aux_out->Assimilate(*other->aux_out);
2780
0
}