Coverage Report

Created: 2025-07-16 07:53

/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
0
  operator JxlEncoderStatus() const { return error_; }
59
60
0
  static JxlErrorOrStatus Success() {
61
0
    return JxlErrorOrStatus(JXL_ENC_SUCCESS);
62
0
  }
63
64
0
  static JxlErrorOrStatus MoreOutput() {
65
0
    return JxlErrorOrStatus(JXL_ENC_NEED_MORE_OUTPUT);
66
0
  }
67
68
0
  static JxlErrorOrStatus Error() { return JxlErrorOrStatus(JXL_ENC_ERROR); }
69
70
 private:
71
0
  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
0
                                            size_t requested_size) {
99
0
  JXL_ENSURE(min_size > 0);
100
0
  JXL_ENSURE(!has_buffer_);
101
0
  if (stop_requested_) return jxl::StatusCode::kNotEnoughBytes;
102
0
  requested_size = std::max(min_size, requested_size);
103
104
  // If we support seeking, output_position_ == position_.
105
0
  if (external_output_processor_ && external_output_processor_->seek) {
106
0
    JXL_ENSURE(output_position_ == position_);
107
0
  }
108
  // Otherwise, output_position_ <= position_.
109
0
  JXL_ENSURE(output_position_ <= position_);
110
0
  size_t additional_size = position_ - output_position_;
111
0
  JXL_ENSURE(memory_manager_ != nullptr);
112
113
0
  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
0
  } else {
136
0
    if (min_size + additional_size < *avail_out_) {
137
0
      internal_buffers_.emplace(position_, InternalBuffer(memory_manager_));
138
0
      has_buffer_ = true;
139
0
      return JxlOutputProcessorBuffer(*next_out_ + additional_size,
140
0
                                      *avail_out_ - additional_size, 0, this);
141
0
    }
142
0
  }
143
144
  // Otherwise, we need to allocate our own buffer.
145
0
  auto it =
146
0
      internal_buffers_.emplace(position_, InternalBuffer(memory_manager_))
147
0
          .first;
148
0
  InternalBuffer& buffer = it->second;
149
0
  size_t alloc_size = requested_size;
150
0
  it++;
151
0
  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
0
  JXL_RETURN_IF_ERROR(buffer.owned_data.resize(alloc_size));
156
0
  has_buffer_ = true;
157
0
  return JxlOutputProcessorBuffer(buffer.owned_data.data(), alloc_size, 0,
158
0
                                  this);
159
0
}
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
0
jxl::Status JxlEncoderOutputProcessorWrapper::SetFinalizedPosition() {
173
0
  JXL_ENSURE(!has_buffer_);
174
0
  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
0
  finalized_position_ = position_;
179
0
  JXL_RETURN_IF_ERROR(FlushOutput());
180
0
  return true;
181
0
}
182
183
jxl::Status JxlEncoderOutputProcessorWrapper::SetAvailOut(uint8_t** next_out,
184
0
                                                          size_t* avail_out) {
185
0
  JXL_ENSURE(!external_output_processor_);
186
0
  avail_out_ = avail_out;
187
0
  next_out_ = next_out;
188
0
  JXL_RETURN_IF_ERROR(FlushOutput());
189
0
  return true;
190
0
}
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
0
jxl::Status JxlEncoderOutputProcessorWrapper::ReleaseBuffer(size_t bytes_used) {
208
0
  JXL_ENSURE(has_buffer_);
209
0
  has_buffer_ = false;
210
0
  auto it = internal_buffers_.find(position_);
211
0
  JXL_ENSURE(it != internal_buffers_.end());
212
0
  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
0
  it->second.written_bytes = bytes_used;
221
0
  position_ += bytes_used;
222
223
0
  auto it_to_next = it;
224
0
  it_to_next++;
225
0
  if (it_to_next != internal_buffers_.end()) {
226
0
    JXL_ENSURE(it_to_next->first >= position_);
227
0
  }
228
229
0
  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
0
  return true;
267
0
}
268
269
// Tries to write all the bytes up to the finalized position.
270
0
jxl::Status JxlEncoderOutputProcessorWrapper::FlushOutput() {
271
0
  JXL_ENSURE(!has_buffer_);
272
0
  while (output_position_ < finalized_position_ &&
273
0
         (avail_out_ == nullptr || *avail_out_ > 0)) {
274
0
    JXL_ENSURE(!internal_buffers_.empty());
275
0
    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
0
    JXL_ENSURE(output_position_ >= it->first);
279
0
    JXL_ENSURE(it->second.written_bytes != 0);
280
0
    size_t buffer_last_byte = it->first + it->second.written_bytes;
281
0
    if (!it->second.owned_data.empty()) {
282
0
      size_t start_in_buffer = output_position_ - it->first;
283
      // Guaranteed by the invariant on `internal_buffers_`.
284
0
      JXL_ENSURE(buffer_last_byte > output_position_);
285
0
      size_t num_to_write =
286
0
          std::min(buffer_last_byte, finalized_position_) - output_position_;
287
0
      if (avail_out_ != nullptr) {
288
0
        size_t n = std::min(num_to_write, *avail_out_);
289
0
        memcpy(*next_out_, it->second.owned_data.data() + start_in_buffer, n);
290
0
        *avail_out_ -= n;
291
0
        *next_out_ += n;
292
0
        output_position_ += n;
293
0
      } 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
0
    } else {
301
0
      size_t advance =
302
0
          std::min(buffer_last_byte, finalized_position_) - output_position_;
303
0
      output_position_ += advance;
304
0
      if (avail_out_ != nullptr) {
305
0
        *next_out_ += advance;
306
0
        *avail_out_ -= advance;
307
0
      }
308
0
    }
309
0
    if (buffer_last_byte == output_position_) {
310
0
      internal_buffers_.erase(it);
311
0
    }
312
0
    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
0
  }
317
0
  return true;
318
0
}
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
0
    jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedFrame>& frame) {
450
0
  if (frame_settings->values.lossless) {
451
0
    frame->option_values.cparams.SetLossless();
452
0
  }
453
454
0
  jxl::JxlEncoderQueuedInput queued_input(frame_settings->enc->memory_manager);
455
0
  queued_input.frame = std::move(frame);
456
0
  frame_settings->enc->input_queue.emplace_back(std::move(queued_input));
457
0
  frame_settings->enc->num_queued_frames++;
458
0
}
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
0
int VerifyLevelSettings(const JxlEncoder* enc, std::string* debug_string) {
529
0
  const auto& m = enc->metadata.m;
530
531
0
  uint64_t xsize = enc->metadata.size.xsize();
532
0
  uint64_t ysize = enc->metadata.size.ysize();
533
  // The uncompressed ICC size, if it is used.
534
0
  size_t icc_size = 0;
535
0
  if (m.color_encoding.WantICC()) {
536
0
    icc_size = m.color_encoding.ICC().size();
537
0
  }
538
539
  // Level 10 checks
540
541
0
  if (xsize > (1ull << 30ull) || ysize > (1ull << 30ull) ||
542
0
      xsize * ysize > (1ull << 40ull)) {
543
0
    if (debug_string) *debug_string = "Too large image dimensions";
544
0
    return -1;
545
0
  }
546
0
  if (icc_size > (1ull << 28)) {
547
0
    if (debug_string) *debug_string = "Too large ICC profile size";
548
0
    return -1;
549
0
  }
550
0
  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
0
  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
0
  if (xsize > (1ull << 18ull) || ysize > (1ull << 18ull) ||
562
0
      xsize * ysize > (1ull << 28ull)) {
563
0
    if (debug_string) *debug_string = "Too large image dimensions";
564
0
    return 10;
565
0
  }
566
0
  if (icc_size > (1ull << 22)) {
567
0
    if (debug_string) *debug_string = "Too large ICC profile";
568
0
    return 10;
569
0
  }
570
0
  if (m.num_extra_channels > 4) {
571
0
    if (debug_string) *debug_string = "Too many extra channels";
572
0
    return 10;
573
0
  }
574
0
  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
0
  return 5;
597
0
}
598
599
JxlEncoderStatus CheckValidBitdepth(uint32_t bits_per_sample,
600
0
                                    uint32_t exponent_bits_per_sample) {
601
0
  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
0
    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
0
  } 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
0
  return JxlErrorOrStatus::Success();
615
0
}
616
617
JxlEncoderStatus VerifyInputBitDepth(JxlBitDepth bit_depth,
618
0
                                     JxlPixelFormat format) {
619
0
  return JxlErrorOrStatus::Success();
620
0
}
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
0
jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() {
738
0
  jxl::PaddedBytes header_bytes{&memory_manager};
739
740
0
  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
0
  if (!wrote_bytes) {
747
    // First time encoding any data, verify the level 5 vs level 10 settings
748
0
    std::string level_message;
749
0
    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
0
    JXL_ENSURE(required_level == -1 || required_level == 5 ||
753
0
               required_level == 10);
754
    // codestream_level == -1 means auto-set to the required level
755
0
    if (codestream_level == -1) codestream_level = required_level;
756
0
    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
0
    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
0
    jxl::AuxOut* aux_out =
773
0
        input.frame ? input.frame->option_values.aux_out : nullptr;
774
0
    jxl::BitWriter writer{&memory_manager};
775
0
    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
0
    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
0
    JXL_RETURN_IF_ERROR(
791
0
        writer.WithMaxBits(8, jxl::LayerType::Header, aux_out, [&] {
792
0
          writer.ZeroPadToByte();
793
0
          return true;
794
0
        }));
795
796
0
    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
0
    codestream_bytes_written_end_of_frame += header_bytes.size();
801
802
0
    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
0
    wrote_bytes = true;
850
0
  }
851
852
0
  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
0
  if (input.frame || input.fast_lossless_frame) {
857
0
    jxl::MemoryManagerUniquePtr<jxl::JxlEncoderQueuedFrame> input_frame =
858
0
        std::move(input.frame);
859
0
    jxl::FJXLFrameUniquePtr fast_lossless_frame =
860
0
        std::move(input.fast_lossless_frame);
861
0
    input_queue.erase(input_queue.begin());
862
0
    num_queued_frames--;
863
0
    if (input_frame) {
864
0
      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
0
      if (metadata.m.xyb_encoded) {
880
0
        input_frame->option_values.cparams.color_transform =
881
0
            jxl::ColorTransform::kXYB;
882
0
      } 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
0
    }
888
889
0
    uint32_t duration;
890
0
    uint32_t timecode;
891
0
    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
0
    } 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
0
      duration = 0;
900
0
      timecode = 0;
901
0
    }
902
903
0
    const bool last_frame = frames_closed && (num_queued_frames == 0);
904
905
0
    uint32_t max_bits_per_sample = metadata.m.bit_depth.bits_per_sample;
906
0
    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
0
    uint32_t bits_per_channels_estimate =
913
0
        std::max(24u, max_bits_per_sample + 3);
914
0
    size_t upper_bound_on_compressed_size_bits =
915
0
        metadata.xsize() * metadata.ysize() *
916
0
        (metadata.m.color_encoding.Channels() + metadata.m.num_extra_channels) *
917
0
        bits_per_channels_estimate;
918
    // Add a 1MB = 0x100000 for an heuristic upper bound on small sizes.
919
0
    size_t upper_bound_on_compressed_size_bytes =
920
0
        0x100000 + (upper_bound_on_compressed_size_bits >> 3);
921
0
    bool use_large_box = upper_bound_on_compressed_size_bytes >=
922
0
                         jxl::kLargeBoxContentSizeThreshold;
923
0
    size_t box_header_size =
924
0
        use_large_box ? jxl::kLargeBoxHeaderSize : jxl::kSmallBoxHeaderSize;
925
926
0
    const size_t frame_start_pos = output_processor.CurrentPosition();
927
0
    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
0
    const size_t frame_codestream_start = output_processor.CurrentPosition();
938
939
0
    JXL_RETURN_IF_ERROR(AppendData(output_processor, header_bytes));
940
941
0
    if (input_frame) {
942
0
      frame_index_box.AddFrame(codestream_bytes_written_end_of_frame, duration,
943
0
                               input_frame->option_values.frame_index_box);
944
945
0
      size_t save_as_reference =
946
0
          input_frame->option_values.header.layer_info.save_as_reference;
947
0
      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
0
      jxl::FrameInfo frame_info;
955
0
      frame_info.is_last = last_frame;
956
0
      frame_info.save_as_reference = save_as_reference;
957
0
      frame_info.source =
958
0
          input_frame->option_values.header.layer_info.blend_info.source;
959
0
      frame_info.clamp = FROM_JXL_BOOL(
960
0
          input_frame->option_values.header.layer_info.blend_info.clamp);
961
0
      frame_info.alpha_channel =
962
0
          input_frame->option_values.header.layer_info.blend_info.alpha;
963
0
      frame_info.extra_channel_blending_info.resize(
964
0
          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
0
      JxlBlendInfo default_blend_info =
968
0
          input_frame->option_values.header.layer_info.blend_info;
969
0
      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
0
      frame_info.origin.x0 =
981
0
          input_frame->option_values.header.layer_info.crop_x0;
982
0
      frame_info.origin.y0 =
983
0
          input_frame->option_values.header.layer_info.crop_y0;
984
0
      frame_info.blendmode = static_cast<jxl::BlendMode>(
985
0
          input_frame->option_values.header.layer_info.blend_info.blendmode);
986
0
      frame_info.blend =
987
0
          input_frame->option_values.header.layer_info.blend_info.blendmode !=
988
0
          JXL_BLEND_REPLACE;
989
0
      frame_info.image_bit_depth = input_frame->option_values.image_bit_depth;
990
0
      frame_info.duration = duration;
991
0
      frame_info.timecode = timecode;
992
0
      frame_info.name = input_frame->option_values.frame_name;
993
994
0
      if (!jxl::EncodeFrame(&memory_manager, input_frame->option_values.cparams,
995
0
                            frame_info, &metadata, input_frame->frame_data, cms,
996
0
                            thread_pool.get(), &output_processor,
997
0
                            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
0
    } 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
0
    const size_t frame_codestream_end = output_processor.CurrentPosition();
1014
0
    const size_t frame_codestream_size =
1015
0
        frame_codestream_end - frame_codestream_start;
1016
1017
0
    codestream_bytes_written_end_of_frame +=
1018
0
        frame_codestream_size - header_bytes.size();
1019
1020
0
    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
0
    JXL_RETURN_IF_ERROR(output_processor.SetFinalizedPosition());
1050
0
    if (input_frame) {
1051
0
      last_used_cparams = input_frame->option_values.cparams;
1052
0
    }
1053
0
    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
0
  } 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
0
  return jxl::OkStatus();
1088
0
}
1089
1090
JxlEncoderStatus JxlEncoderSetColorEncoding(JxlEncoder* enc,
1091
0
                                            const JxlColorEncoding* color) {
1092
0
  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
0
  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
0
  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
0
  if (enc->metadata.m.color_encoding.GetColorSpace() ==
1103
0
      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
0
  } else {
1110
0
    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
0
  }
1116
0
  enc->color_encoding_set = true;
1117
0
  if (!enc->intensity_target_set) {
1118
0
    jxl::SetIntensityTarget(&enc->metadata.m);
1119
0
  }
1120
0
  return JxlErrorOrStatus::Success();
1121
0
}
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
0
void JxlEncoderInitBasicInfo(JxlBasicInfo* info) {
1175
0
  info->have_container = JXL_FALSE;
1176
0
  info->xsize = 0;
1177
0
  info->ysize = 0;
1178
0
  info->bits_per_sample = 8;
1179
0
  info->exponent_bits_per_sample = 0;
1180
0
  info->intensity_target = 0.f;
1181
0
  info->min_nits = 0.f;
1182
0
  info->relative_to_max_display = JXL_FALSE;
1183
0
  info->linear_below = 0.f;
1184
0
  info->uses_original_profile = JXL_FALSE;
1185
0
  info->have_preview = JXL_FALSE;
1186
0
  info->have_animation = JXL_FALSE;
1187
0
  info->orientation = JXL_ORIENT_IDENTITY;
1188
0
  info->num_color_channels = 3;
1189
0
  info->num_extra_channels = 0;
1190
0
  info->alpha_bits = 0;
1191
0
  info->alpha_exponent_bits = 0;
1192
0
  info->alpha_premultiplied = JXL_FALSE;
1193
0
  info->preview.xsize = 0;
1194
0
  info->preview.ysize = 0;
1195
0
  info->intrinsic_xsize = 0;
1196
0
  info->intrinsic_ysize = 0;
1197
0
  info->animation.tps_numerator = 10;
1198
0
  info->animation.tps_denominator = 1;
1199
0
  info->animation.num_loops = 0;
1200
0
  info->animation.have_timecodes = JXL_FALSE;
1201
0
}
1202
1203
0
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
0
  frame_header->duration = 0;
1209
0
  frame_header->timecode = 0;
1210
0
  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
0
  frame_header->is_last = JXL_TRUE;
1217
0
  frame_header->layer_info.have_crop = JXL_FALSE;
1218
0
  frame_header->layer_info.crop_x0 = 0;
1219
0
  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
0
  frame_header->layer_info.xsize = 0;
1224
0
  frame_header->layer_info.ysize = 0;
1225
0
  JxlEncoderInitBlendInfo(&frame_header->layer_info.blend_info);
1226
0
  frame_header->layer_info.save_as_reference = 0;
1227
0
}
1228
1229
0
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
0
  blend_info->blendmode = JXL_BLEND_REPLACE;
1236
0
  blend_info->source = 0;
1237
0
  blend_info->alpha = 0;
1238
0
  blend_info->clamp = 0;
1239
0
}
1240
1241
JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc,
1242
0
                                        const JxlBasicInfo* info) {
1243
0
  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
0
  if (JXL_ENC_SUCCESS != CheckValidBitdepth(info->bits_per_sample,
1247
0
                                            info->exponent_bits_per_sample)) {
1248
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid bit depth");
1249
0
  }
1250
1251
0
  enc->metadata.m.bit_depth.bits_per_sample = info->bits_per_sample;
1252
0
  enc->metadata.m.bit_depth.exponent_bits_per_sample =
1253
0
      info->exponent_bits_per_sample;
1254
0
  enc->metadata.m.bit_depth.floating_point_sample =
1255
0
      (info->exponent_bits_per_sample != 0u);
1256
0
  enc->metadata.m.modular_16_bit_buffer_sufficient =
1257
0
      (!FROM_JXL_BOOL(info->uses_original_profile) ||
1258
0
       info->bits_per_sample <= 12) &&
1259
0
      info->alpha_bits <= 12;
1260
0
  if ((info->intrinsic_xsize > 0 || info->intrinsic_ysize > 0) &&
1261
0
      (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
0
  enc->metadata.m.num_extra_channels = info->num_extra_channels;
1276
0
  enc->metadata.m.extra_channel_info.resize(enc->metadata.m.num_extra_channels);
1277
0
  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
0
  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
0
  enc->metadata.m.xyb_encoded = !FROM_JXL_BOOL(info->uses_original_profile);
1297
0
  if (info->orientation > 0 && info->orientation <= 8) {
1298
0
    enc->metadata.m.orientation = info->orientation;
1299
0
  } else {
1300
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
1301
0
                         "Invalid value for orientation field");
1302
0
  }
1303
0
  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
0
  if (info->intensity_target != 0) {
1308
0
    enc->metadata.m.SetIntensityTarget(info->intensity_target);
1309
0
    enc->intensity_target_set = true;
1310
0
  } 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
0
  enc->metadata.m.tone_mapping.min_nits = info->min_nits;
1318
0
  enc->metadata.m.tone_mapping.relative_to_max_display =
1319
0
      FROM_JXL_BOOL(info->relative_to_max_display);
1320
0
  enc->metadata.m.tone_mapping.linear_below = info->linear_below;
1321
0
  enc->basic_info = *info;
1322
0
  enc->basic_info_set = true;
1323
1324
0
  enc->metadata.m.have_animation = FROM_JXL_BOOL(info->have_animation);
1325
0
  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
0
  std::string level_message;
1342
0
  int required_level = VerifyLevelSettings(enc, &level_message);
1343
0
  if (required_level == -1 ||
1344
0
      (static_cast<int>(enc->codestream_level) < required_level &&
1345
0
       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
0
  return JxlErrorOrStatus::Success();
1353
0
}
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
0
    JxlEncoder* enc, const JxlEncoderFrameSettings* source) {
1476
0
  auto opts = jxl::MemoryManagerMakeUnique<JxlEncoderFrameSettings>(
1477
0
      &enc->memory_manager);
1478
0
  if (!opts) return nullptr;
1479
0
  opts->enc = enc;
1480
0
  if (source != nullptr) {
1481
0
    opts->values = source->values;
1482
0
  } else {
1483
0
    opts->values.lossless = false;
1484
0
  }
1485
0
  opts->values.cparams.level = enc->codestream_level;
1486
0
  opts->values.cparams.ec_distance.resize(enc->metadata.m.num_extra_channels,
1487
0
                                          0);
1488
1489
0
  JxlEncoderFrameSettings* ret = opts.get();
1490
0
  enc->encoder_options.emplace_back(std::move(opts));
1491
0
  return ret;
1492
0
}
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
0
JxlEncoder* JxlEncoderCreate(const JxlMemoryManager* memory_manager) {
1943
0
  JxlMemoryManager local_memory_manager;
1944
0
  if (!jxl::MemoryManagerInit(&local_memory_manager, memory_manager)) {
1945
0
    return nullptr;
1946
0
  }
1947
1948
0
  void* alloc =
1949
0
      jxl::MemoryManagerAlloc(&local_memory_manager, sizeof(JxlEncoder));
1950
0
  if (!alloc) return nullptr;
1951
0
  JxlEncoder* enc = new (alloc) JxlEncoder();
1952
0
  enc->memory_manager = local_memory_manager;
1953
  // TODO(sboukortt): add an API function to set this.
1954
0
  enc->cms = *JxlGetDefaultCms();
1955
0
  enc->cms_set = true;
1956
1957
  // Initialize all the field values.
1958
0
  JxlEncoderReset(enc);
1959
1960
0
  return enc;
1961
0
}
1962
1963
0
void JxlEncoderReset(JxlEncoder* enc) {
1964
0
  enc->thread_pool.reset();
1965
0
  enc->input_queue.clear();
1966
0
  enc->num_queued_frames = 0;
1967
0
  enc->num_queued_boxes = 0;
1968
0
  enc->encoder_options.clear();
1969
0
  enc->codestream_bytes_written_end_of_frame = 0;
1970
0
  enc->wrote_bytes = false;
1971
0
  enc->jxlp_counter = 0;
1972
0
  enc->metadata = jxl::CodecMetadata();
1973
0
  enc->last_used_cparams = jxl::CompressParams();
1974
0
  enc->frames_closed = false;
1975
0
  enc->boxes_closed = false;
1976
0
  enc->basic_info_set = false;
1977
0
  enc->color_encoding_set = false;
1978
0
  enc->intensity_target_set = false;
1979
0
  enc->use_container = false;
1980
0
  enc->use_boxes = false;
1981
0
  enc->store_jpeg_metadata = false;
1982
0
  enc->codestream_level = -1;
1983
0
  enc->output_processor =
1984
0
      JxlEncoderOutputProcessorWrapper(&enc->memory_manager);
1985
0
  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
0
}
1992
1993
0
void JxlEncoderDestroy(JxlEncoder* enc) {
1994
0
  if (enc) {
1995
0
    JxlMemoryManager local_memory_manager = enc->memory_manager;
1996
    // Call destructor directly since custom free function is used.
1997
0
    enc->~JxlEncoder();
1998
0
    jxl::MemoryManagerFree(&local_memory_manager, enc);
1999
0
  }
2000
0
}
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
0
JxlEncoderStatus JxlEncoderSetCodestreamLevel(JxlEncoder* enc, int level) {
2025
0
  if (level != -1 && level != 5 && level != 10) {
2026
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_NOT_SUPPORTED, "invalid level");
2027
0
  }
2028
0
  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
0
  enc->codestream_level = level;
2033
0
  return JxlErrorOrStatus::Success();
2034
0
}
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
0
                                             void* parallel_runner_opaque) {
2049
0
  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
0
  enc->thread_pool = jxl::MemoryManagerMakeUnique<jxl::ThreadPool>(
2054
0
      &enc->memory_manager, parallel_runner, parallel_runner_opaque);
2055
0
  if (!enc->thread_pool) {
2056
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_GENERIC,
2057
0
                         "error setting parallel runner");
2058
0
  }
2059
0
  return JxlErrorOrStatus::Success();
2060
0
}
2061
2062
namespace {
2063
JxlEncoderStatus GetCurrentDimensions(
2064
    const JxlEncoderFrameSettings* frame_settings, size_t& xsize,
2065
0
    size_t& ysize) {
2066
0
  xsize = frame_settings->enc->metadata.xsize();
2067
0
  ysize = frame_settings->enc->metadata.ysize();
2068
0
  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
0
  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
0
  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
0
  return JxlErrorOrStatus::Success();
2082
0
}
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
    if (!SetColorEncodingFromJpegData(
2101
0
            *io.Main().jpeg_data,
2102
0
            &frame_settings->enc->metadata.m.color_encoding)) {
2103
0
      return JXL_API_ERROR(
2104
0
          frame_settings->enc, JXL_ENC_ERR_BAD_INPUT,
2105
0
          "Error decoding the ICC profile embedded in the input JPEG");
2106
0
    }
2107
0
    frame_settings->enc->color_encoding_set = true;
2108
0
  }
2109
2110
0
  if (!frame_settings->enc->basic_info_set) {
2111
0
    JxlBasicInfo basic_info;
2112
0
    JxlEncoderInitBasicInfo(&basic_info);
2113
0
    basic_info.xsize = io.Main().jpeg_data->width;
2114
0
    basic_info.ysize = io.Main().jpeg_data->height;
2115
0
    basic_info.uses_original_profile = JXL_TRUE;
2116
0
    if (JxlEncoderSetBasicInfo(frame_settings->enc, &basic_info) !=
2117
0
        JXL_ENC_SUCCESS) {
2118
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2119
0
                           "Error setting basic info");
2120
0
    }
2121
0
  }
2122
2123
0
  size_t xsize;
2124
0
  size_t ysize;
2125
0
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2126
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2127
0
                         "bad dimensions");
2128
0
  }
2129
0
  if (xsize != static_cast<size_t>(io.Main().jpeg_data->width) ||
2130
0
      ysize != static_cast<size_t>(io.Main().jpeg_data->height)) {
2131
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2132
0
                         "JPEG dimensions don't match frame dimensions");
2133
0
  }
2134
2135
0
  if (frame_settings->enc->metadata.m.xyb_encoded) {
2136
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2137
0
                         "Can't XYB encode a lossless JPEG");
2138
0
  }
2139
0
  if (!io.blobs.exif.empty()) {
2140
0
    JxlOrientation orientation = static_cast<JxlOrientation>(
2141
0
        frame_settings->enc->metadata.m.orientation);
2142
0
    jxl::InterpretExif(io.blobs.exif, &orientation);
2143
0
    frame_settings->enc->metadata.m.orientation = orientation;
2144
0
  }
2145
0
  if (!io.blobs.exif.empty() && frame_settings->values.cparams.jpeg_keep_exif) {
2146
0
    size_t exif_size = io.blobs.exif.size();
2147
    // Exif data in JPEG is limited to 64k
2148
0
    if (exif_size > 0xFFFF) {
2149
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2150
0
                           "Exif larger than possible in JPEG?");
2151
0
    }
2152
0
    exif_size += 4;  // prefix 4 zero bytes for tiff offset
2153
0
    std::vector<uint8_t> exif(exif_size);
2154
0
    memcpy(exif.data() + 4, io.blobs.exif.data(), io.blobs.exif.size());
2155
0
    JxlEncoderUseBoxes(frame_settings->enc);
2156
0
    JxlEncoderAddBox(
2157
0
        frame_settings->enc, "Exif", exif.data(), exif_size,
2158
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2159
0
  }
2160
0
  if (!io.blobs.xmp.empty() && frame_settings->values.cparams.jpeg_keep_xmp) {
2161
0
    JxlEncoderUseBoxes(frame_settings->enc);
2162
0
    JxlEncoderAddBox(
2163
0
        frame_settings->enc, "xml ", io.blobs.xmp.data(), io.blobs.xmp.size(),
2164
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2165
0
  }
2166
0
  if (!io.blobs.jumbf.empty() &&
2167
0
      frame_settings->values.cparams.jpeg_keep_jumbf) {
2168
0
    JxlEncoderUseBoxes(frame_settings->enc);
2169
0
    JxlEncoderAddBox(
2170
0
        frame_settings->enc, "jumb", io.blobs.jumbf.data(),
2171
0
        io.blobs.jumbf.size(),
2172
0
        TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes));
2173
0
  }
2174
0
  if (frame_settings->enc->store_jpeg_metadata) {
2175
0
    if (!frame_settings->values.cparams.jpeg_keep_exif ||
2176
0
        !frame_settings->values.cparams.jpeg_keep_xmp) {
2177
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2178
0
                           "Need to preserve EXIF and XMP to allow JPEG "
2179
0
                           "bitstream reconstruction");
2180
0
    }
2181
0
    jxl::jpeg::JPEGData data_in = *io.Main().jpeg_data;
2182
0
    std::vector<uint8_t> jpeg_data;
2183
0
    if (!jxl::jpeg::EncodeJPEGData(&frame_settings->enc->memory_manager,
2184
0
                                   data_in, &jpeg_data,
2185
0
                                   frame_settings->values.cparams)) {
2186
0
      return JXL_API_ERROR(
2187
0
          frame_settings->enc, JXL_ENC_ERR_JBRD,
2188
0
          "JPEG bitstream reconstruction data cannot be encoded");
2189
0
    }
2190
0
    frame_settings->enc->jpeg_metadata = jpeg_data;
2191
0
  }
2192
2193
0
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2194
0
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2195
0
  frame_data.SetJPEGData(std::move(io.Main().jpeg_data));
2196
2197
0
  auto queued_frame = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedFrame>(
2198
0
      &frame_settings->enc->memory_manager,
2199
      // JxlEncoderQueuedFrame is a struct with no constructors, so we use the
2200
      // default move constructor there.
2201
0
      jxl::JxlEncoderQueuedFrame{
2202
0
          frame_settings->values, std::move(frame_data), {}});
2203
0
  if (!queued_frame) {
2204
    // TODO(jon): when can this happen? is this an API usage error?
2205
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2206
0
                         "No frame queued?");
2207
0
  }
2208
0
  queued_frame->ec_initialized.resize(
2209
0
      frame_settings->enc->metadata.m.num_extra_channels);
2210
2211
0
  QueueFrame(frame_settings, queued_frame);
2212
0
  return JxlErrorOrStatus::Success();
2213
0
}
2214
2215
static bool CanDoFastLossless(const JxlEncoderFrameSettings* frame_settings,
2216
                              const JxlPixelFormat* pixel_format,
2217
0
                              bool has_alpha) {
2218
0
  if (!frame_settings->values.lossless) {
2219
0
    return false;
2220
0
  }
2221
  // TODO(veluca): many of the following options could be made to work, but are
2222
  // just not implemented in FJXL's frame header handling yet.
2223
0
  if (frame_settings->values.frame_index_box) {
2224
0
    return false;
2225
0
  }
2226
0
  if (frame_settings->values.header.layer_info.have_crop) {
2227
0
    return false;
2228
0
  }
2229
0
  if (frame_settings->enc->metadata.m.have_animation) {
2230
0
    return false;
2231
0
  }
2232
0
  if (frame_settings->values.cparams.speed_tier != jxl::SpeedTier::kLightning) {
2233
0
    return false;
2234
0
  }
2235
0
  if (frame_settings->values.image_bit_depth.type ==
2236
0
          JxlBitDepthType::JXL_BIT_DEPTH_CUSTOM &&
2237
0
      frame_settings->values.image_bit_depth.bits_per_sample !=
2238
0
          frame_settings->enc->metadata.m.bit_depth.bits_per_sample) {
2239
0
    return false;
2240
0
  }
2241
  // TODO(veluca): implement support for LSB-padded input in fast_lossless.
2242
0
  if (frame_settings->values.image_bit_depth.type ==
2243
0
          JxlBitDepthType::JXL_BIT_DEPTH_FROM_PIXEL_FORMAT &&
2244
0
      frame_settings->enc->metadata.m.bit_depth.bits_per_sample % 8 != 0) {
2245
0
    return false;
2246
0
  }
2247
0
  if (!frame_settings->values.frame_name.empty()) {
2248
0
    return false;
2249
0
  }
2250
  // No extra channels other than alpha.
2251
0
  if (!(has_alpha && frame_settings->enc->metadata.m.num_extra_channels == 1) &&
2252
0
      frame_settings->enc->metadata.m.num_extra_channels != 0) {
2253
0
    return false;
2254
0
  }
2255
0
  if (frame_settings->enc->metadata.m.bit_depth.bits_per_sample > 16) {
2256
0
    return false;
2257
0
  }
2258
0
  if (pixel_format->data_type != JxlDataType::JXL_TYPE_FLOAT16 &&
2259
0
      pixel_format->data_type != JxlDataType::JXL_TYPE_UINT16 &&
2260
0
      pixel_format->data_type != JxlDataType::JXL_TYPE_UINT8) {
2261
0
    return false;
2262
0
  }
2263
0
  if ((frame_settings->enc->metadata.m.bit_depth.bits_per_sample > 8) !=
2264
0
      (pixel_format->data_type == JxlDataType::JXL_TYPE_UINT16 ||
2265
0
       pixel_format->data_type == JxlDataType::JXL_TYPE_FLOAT16)) {
2266
0
    return false;
2267
0
  }
2268
0
  if (!((pixel_format->num_channels == 1 || pixel_format->num_channels == 3) &&
2269
0
        !has_alpha) &&
2270
0
      !((pixel_format->num_channels == 2 || pixel_format->num_channels == 4) &&
2271
0
        has_alpha)) {
2272
0
    return false;
2273
0
  }
2274
2275
0
  return true;
2276
0
}
2277
2278
namespace {
2279
JxlEncoderStatus JxlEncoderAddImageFrameInternal(
2280
    const JxlEncoderFrameSettings* frame_settings, size_t xsize, size_t ysize,
2281
0
    bool streaming, jxl::JxlEncoderChunkedFrameAdapter&& frame_data) {
2282
0
  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
2283
0
  {
2284
0
    JxlChunkedFrameInputSource input = frame_data.GetInputSource();
2285
0
    input.get_color_channels_pixel_format(input.opaque, &pixel_format);
2286
0
  }
2287
0
  uint32_t num_channels = pixel_format.num_channels;
2288
0
  size_t has_interleaved_alpha =
2289
0
      static_cast<size_t>(num_channels == 2 || num_channels == 4);
2290
2291
0
  if (!frame_settings->enc->basic_info_set) {
2292
    // Basic Info must be set. Otherwise, this is an API misuse.
2293
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2294
0
                         "Basic info or color encoding not set yet");
2295
0
  }
2296
0
  if (frame_settings->enc->frames_closed) {
2297
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2298
0
                         "Frame input already closed");
2299
0
  }
2300
0
  if (num_channels < 3) {
2301
0
    if (frame_settings->enc->basic_info.num_color_channels != 1) {
2302
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2303
0
                           "Grayscale pixel format input for an RGB image");
2304
0
    }
2305
0
  } else {
2306
0
    if (frame_settings->enc->basic_info.num_color_channels != 3) {
2307
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2308
0
                           "RGB pixel format input for a grayscale image");
2309
0
    }
2310
0
  }
2311
0
  if (frame_settings->values.lossless &&
2312
0
      frame_settings->enc->metadata.m.xyb_encoded) {
2313
0
    return JXL_API_ERROR(
2314
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2315
0
        "Set uses_original_profile=true for lossless encoding");
2316
0
  }
2317
0
  if (frame_settings->values.cparams.disable_perceptual_optimizations &&
2318
0
      frame_settings->enc->metadata.m.xyb_encoded) {
2319
0
    return JXL_API_ERROR(
2320
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2321
0
        "Set uses_original_profile=true for non-perceptual encoding");
2322
0
  }
2323
0
  if (JXL_ENC_SUCCESS !=
2324
0
      VerifyInputBitDepth(frame_settings->values.image_bit_depth,
2325
0
                          pixel_format)) {
2326
0
    return JXL_API_ERROR_NOSET("Invalid input bit depth");
2327
0
  }
2328
0
  if (has_interleaved_alpha >
2329
0
      frame_settings->enc->metadata.m.num_extra_channels) {
2330
0
    return JXL_API_ERROR(
2331
0
        frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2332
0
        "number of extra channels mismatch (need 1 extra channel for alpha)");
2333
0
  }
2334
2335
0
  bool has_alpha = frame_settings->enc->metadata.m.HasAlpha();
2336
2337
  // All required conditions to do fast-lossless.
2338
0
  if (CanDoFastLossless(frame_settings, &pixel_format, has_alpha)) {
2339
0
    const bool big_endian =
2340
0
        pixel_format.endianness == JXL_BIG_ENDIAN ||
2341
0
        (pixel_format.endianness == JXL_NATIVE_ENDIAN && !IsLittleEndian());
2342
2343
0
    RunnerTicket ticket{frame_settings->enc->thread_pool.get()};
2344
0
    JXL_BOOL oneshot = TO_JXL_BOOL(!frame_data.StreamingInput());
2345
0
    auto* frame_state = JxlFastLosslessPrepareFrame(
2346
0
        frame_data.GetInputSource(), xsize, ysize, num_channels,
2347
0
        frame_settings->enc->metadata.m.bit_depth.bits_per_sample, big_endian,
2348
0
        /*effort=*/2, oneshot);
2349
0
    if (!streaming) {
2350
0
      bool ok =
2351
0
          JxlFastLosslessProcessFrame(frame_state, /*is_last=*/false, &ticket,
2352
0
                                      &FastLosslessRunnerAdapter, nullptr);
2353
0
      if (!ok || ticket.has_error) {
2354
0
        return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2355
0
                             "Internal: JxlFastLosslessProcessFrame failed");
2356
0
      }
2357
0
    }
2358
0
    QueueFastLosslessFrame(frame_settings, frame_state);
2359
0
    return JxlErrorOrStatus::Success();
2360
0
  }
2361
2362
0
  if (!streaming) {
2363
    // The input callbacks are only guaranteed to be available during frame
2364
    // encoding when both the input and the output is streaming. In all other
2365
    // cases we need to create an internal copy of the frame data.
2366
0
    if (!frame_data.CopyBuffers()) {
2367
0
      return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2368
0
                           "Invalid chunked frame input source");
2369
0
    }
2370
0
  }
2371
2372
0
  if (!frame_settings->enc->color_encoding_set) {
2373
0
    jxl::ColorEncoding c_current;
2374
0
    if ((pixel_format.data_type == JXL_TYPE_FLOAT) ||
2375
0
        (pixel_format.data_type == JXL_TYPE_FLOAT16)) {
2376
0
      c_current = jxl::ColorEncoding::LinearSRGB(num_channels < 3);
2377
0
    } else {
2378
0
      c_current = jxl::ColorEncoding::SRGB(num_channels < 3);
2379
0
    }
2380
0
    frame_settings->enc->metadata.m.color_encoding = c_current;
2381
0
  }
2382
2383
0
  auto queued_frame = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedFrame>(
2384
0
      &frame_settings->enc->memory_manager,
2385
      // JxlEncoderQueuedFrame is a struct with no constructors, so we use the
2386
      // default move constructor there.
2387
0
      jxl::JxlEncoderQueuedFrame{
2388
0
          frame_settings->values, std::move(frame_data), {}});
2389
2390
0
  if (!queued_frame) {
2391
    // TODO(jon): when can this happen? is this an API usage error?
2392
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2393
0
                         "No frame queued?");
2394
0
  }
2395
2396
0
  for (auto& ec_info : frame_settings->enc->metadata.m.extra_channel_info) {
2397
0
    if (has_interleaved_alpha && ec_info.type == jxl::ExtraChannel::kAlpha) {
2398
0
      queued_frame->ec_initialized.push_back(1);
2399
0
      has_interleaved_alpha = 0;  // only first Alpha is initialized
2400
0
    } else {
2401
0
      queued_frame->ec_initialized.push_back(0);
2402
0
    }
2403
0
  }
2404
0
  queued_frame->option_values.cparams.level =
2405
0
      frame_settings->enc->codestream_level;
2406
2407
0
  QueueFrame(frame_settings, queued_frame);
2408
0
  return JxlErrorOrStatus::Success();
2409
0
}
2410
}  // namespace
2411
2412
JxlEncoderStatus JxlEncoderAddImageFrame(
2413
    const JxlEncoderFrameSettings* frame_settings,
2414
0
    const JxlPixelFormat* pixel_format, const void* buffer, size_t size) {
2415
0
  size_t xsize;
2416
0
  size_t ysize;
2417
0
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2418
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2419
0
                         "bad dimensions");
2420
0
  }
2421
0
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2422
0
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2423
0
  if (!frame_data.SetFromBuffer(0, reinterpret_cast<const uint8_t*>(buffer),
2424
0
                                size, *pixel_format)) {
2425
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2426
0
                         "provided image buffer too small");
2427
0
  }
2428
0
  return JxlEncoderAddImageFrameInternal(frame_settings, xsize, ysize,
2429
0
                                         /*streaming=*/false,
2430
0
                                         std::move(frame_data));
2431
0
}
2432
2433
JxlEncoderStatus JxlEncoderAddChunkedFrame(
2434
    const JxlEncoderFrameSettings* frame_settings, JXL_BOOL is_last_frame,
2435
0
    JxlChunkedFrameInputSource chunked_frame_input) {
2436
0
  size_t xsize;
2437
0
  size_t ysize;
2438
0
  if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) {
2439
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC,
2440
0
                         "bad dimensions");
2441
0
  }
2442
0
  bool streaming = frame_settings->enc->output_processor.OutputProcessorSet();
2443
0
  jxl::JxlEncoderChunkedFrameAdapter frame_data(
2444
0
      xsize, ysize, frame_settings->enc->metadata.m.num_extra_channels);
2445
0
  frame_data.SetInputSource(chunked_frame_input);
2446
0
  auto status = JxlEncoderAddImageFrameInternal(
2447
0
      frame_settings, xsize, ysize, streaming, std::move(frame_data));
2448
0
  if (status != JXL_ENC_SUCCESS) return status;
2449
2450
0
  auto& queued_frame = frame_settings->enc->input_queue.back();
2451
0
  if (queued_frame.frame) {
2452
0
    for (auto& val : queued_frame.frame->ec_initialized) val = 1;
2453
0
  }
2454
2455
0
  if (is_last_frame) {
2456
0
    JxlEncoderCloseInput(frame_settings->enc);
2457
0
  }
2458
0
  if (streaming) {
2459
0
    return JxlEncoderFlushInput(frame_settings->enc);
2460
0
  }
2461
0
  return JxlErrorOrStatus::Success();
2462
0
}
2463
2464
0
JxlEncoderStatus JxlEncoderUseBoxes(JxlEncoder* enc) {
2465
0
  if (enc->wrote_bytes) {
2466
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2467
0
                         "this setting can only be set at the beginning");
2468
0
  }
2469
0
  enc->use_boxes = true;
2470
0
  return JxlErrorOrStatus::Success();
2471
0
}
2472
2473
JxlEncoderStatus JxlEncoderAddBox(JxlEncoder* enc, const JxlBoxType type,
2474
                                  const uint8_t* contents, size_t size,
2475
0
                                  JXL_BOOL compress_box) {
2476
0
  if (!enc->use_boxes) {
2477
0
    return JXL_API_ERROR(
2478
0
        enc, JXL_ENC_ERR_API_USAGE,
2479
0
        "must set JxlEncoderUseBoxes at the beginning to add boxes");
2480
0
  }
2481
0
  if (enc->boxes_closed) {
2482
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2483
0
                         "Box input already closed");
2484
0
  }
2485
0
  if (compress_box) {
2486
0
    if (memcmp("jxl", type, 3) == 0) {
2487
0
      return JXL_API_ERROR(
2488
0
          enc, JXL_ENC_ERR_API_USAGE,
2489
0
          "brob box may not contain a type starting with \"jxl\"");
2490
0
    }
2491
0
    if (memcmp("jbrd", type, 4) == 0) {
2492
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2493
0
                           "jbrd box may not be brob compressed");
2494
0
    }
2495
0
    if (memcmp("brob", type, 4) == 0) {
2496
      // The compress_box will compress an existing non-brob box into a brob
2497
      // box. If already giving a valid brotli-compressed brob box, set
2498
      // compress_box to false since it is already compressed.
2499
0
      return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2500
0
                           "a brob box cannot contain another brob box");
2501
0
    }
2502
0
  }
2503
2504
0
  auto box = jxl::MemoryManagerMakeUnique<jxl::JxlEncoderQueuedBox>(
2505
0
      &enc->memory_manager);
2506
2507
0
  box->type = jxl::MakeBoxType(type);
2508
0
  box->contents.assign(contents, contents + size);
2509
0
  box->compress_box = FROM_JXL_BOOL(compress_box);
2510
0
  QueueBox(enc, box);
2511
0
  return JxlErrorOrStatus::Success();
2512
0
}
2513
2514
JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelBuffer(
2515
    const JxlEncoderFrameSettings* frame_settings,
2516
    const JxlPixelFormat* pixel_format, const void* buffer, size_t size,
2517
0
    uint32_t index) {
2518
0
  if (index >= frame_settings->enc->metadata.m.num_extra_channels) {
2519
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2520
0
                         "Invalid value for the index of extra channel");
2521
0
  }
2522
0
  if (!frame_settings->enc->basic_info_set ||
2523
0
      !frame_settings->enc->color_encoding_set) {
2524
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2525
0
                         "Basic info has to be set first");
2526
0
  }
2527
0
  if (frame_settings->enc->input_queue.empty()) {
2528
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2529
0
                         "First add image frame, then extra channels");
2530
0
  }
2531
0
  if (frame_settings->enc->frames_closed) {
2532
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2533
0
                         "Frame input already closed");
2534
0
  }
2535
0
  JxlPixelFormat ec_format = *pixel_format;
2536
0
  ec_format.num_channels = 1;
2537
0
  if (JXL_ENC_SUCCESS !=
2538
0
      VerifyInputBitDepth(frame_settings->values.image_bit_depth, ec_format)) {
2539
0
    return JXL_API_ERROR_NOSET("Invalid input bit depth");
2540
0
  }
2541
0
  const uint8_t* uint8_buffer = reinterpret_cast<const uint8_t*>(buffer);
2542
0
  auto* queued_frame = frame_settings->enc->input_queue.back().frame.get();
2543
0
  if (!queued_frame->frame_data.SetFromBuffer(1 + index, uint8_buffer, size,
2544
0
                                              ec_format)) {
2545
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2546
0
                         "provided image buffer too small");
2547
0
  }
2548
0
  queued_frame->ec_initialized[index] = 1;
2549
2550
0
  return JxlErrorOrStatus::Success();
2551
0
}
2552
2553
0
void JxlEncoderCloseFrames(JxlEncoder* enc) { enc->frames_closed = true; }
2554
2555
0
void JxlEncoderCloseBoxes(JxlEncoder* enc) { enc->boxes_closed = true; }
2556
2557
0
void JxlEncoderCloseInput(JxlEncoder* enc) {
2558
0
  JxlEncoderCloseFrames(enc);
2559
0
  JxlEncoderCloseBoxes(enc);
2560
0
}
2561
2562
0
JXL_EXPORT JxlEncoderStatus JxlEncoderFlushInput(JxlEncoder* enc) {
2563
0
  if (!enc->output_processor.OutputProcessorSet()) {
2564
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2565
0
                         "Cannot flush input without setting output "
2566
0
                         "processor with JxlEncoderSetOutputProcessor");
2567
0
  }
2568
0
  while (!enc->input_queue.empty()) {
2569
0
    if (!enc->ProcessOneEnqueuedInput()) {
2570
0
      return JxlErrorOrStatus::Error();
2571
0
    }
2572
0
  }
2573
0
  return JxlErrorOrStatus::Success();
2574
0
}
2575
2576
JXL_EXPORT JxlEncoderStatus JxlEncoderSetOutputProcessor(
2577
0
    JxlEncoder* enc, JxlEncoderOutputProcessor output_processor) {
2578
0
  if (enc->output_processor.HasAvailOut()) {
2579
0
    return JXL_API_ERROR(
2580
0
        enc, JXL_ENC_ERR_API_USAGE,
2581
0
        "Cannot set an output processor when some output was already produced");
2582
0
  }
2583
0
  if (!output_processor.set_finalized_position ||
2584
0
      !output_processor.get_buffer || !output_processor.release_buffer) {
2585
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2586
0
                         "Missing output processor functions");
2587
0
  }
2588
0
  enc->output_processor =
2589
0
      JxlEncoderOutputProcessorWrapper(&enc->memory_manager, output_processor);
2590
0
  return JxlErrorOrStatus::Success();
2591
0
}
2592
2593
JxlEncoderStatus JxlEncoderProcessOutput(JxlEncoder* enc, uint8_t** next_out,
2594
0
                                         size_t* avail_out) {
2595
0
  if (enc->output_processor.OutputProcessorSet()) {
2596
0
    return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE,
2597
0
                         "Cannot call JxlEncoderProcessOutput after calling "
2598
0
                         "JxlEncoderSetOutputProcessor");
2599
0
  }
2600
0
  if (!enc->output_processor.SetAvailOut(next_out, avail_out)) {
2601
0
    return JxlErrorOrStatus::Error();
2602
0
  }
2603
0
  while (*avail_out != 0 && !enc->input_queue.empty()) {
2604
0
    if (!enc->ProcessOneEnqueuedInput()) {
2605
0
      return JxlErrorOrStatus::Error();
2606
0
    }
2607
0
  }
2608
2609
0
  if (!enc->input_queue.empty() || enc->output_processor.HasOutputToWrite()) {
2610
0
    return JxlErrorOrStatus::MoreOutput();
2611
0
  }
2612
0
  return JxlErrorOrStatus::Success();
2613
0
}
2614
2615
JxlEncoderStatus JxlEncoderSetFrameHeader(
2616
    JxlEncoderFrameSettings* frame_settings,
2617
0
    const JxlFrameHeader* frame_header) {
2618
0
  if (frame_header->layer_info.blend_info.source > 3) {
2619
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2620
0
                         "invalid blending source index");
2621
0
  }
2622
  // If there are no extra channels, it's ok for the value to be 0.
2623
0
  if (frame_header->layer_info.blend_info.alpha != 0 &&
2624
0
      frame_header->layer_info.blend_info.alpha >=
2625
0
          frame_settings->enc->metadata.m.extra_channel_info.size()) {
2626
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2627
0
                         "alpha blend channel index out of bounds");
2628
0
  }
2629
2630
0
  frame_settings->values.header = *frame_header;
2631
  // Setting the frame header resets the frame name, it must be set again with
2632
  // JxlEncoderSetFrameName if desired.
2633
0
  frame_settings->values.frame_name = "";
2634
2635
0
  return JxlErrorOrStatus::Success();
2636
0
}
2637
2638
JxlEncoderStatus JxlEncoderSetExtraChannelBlendInfo(
2639
    JxlEncoderFrameSettings* frame_settings, size_t index,
2640
0
    const JxlBlendInfo* blend_info) {
2641
0
  if (index >= frame_settings->enc->metadata.m.num_extra_channels) {
2642
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2643
0
                         "Invalid value for the index of extra channel");
2644
0
  }
2645
2646
0
  if (frame_settings->values.extra_channel_blend_info.size() !=
2647
0
      frame_settings->enc->metadata.m.num_extra_channels) {
2648
0
    JxlBlendInfo default_blend_info;
2649
0
    JxlEncoderInitBlendInfo(&default_blend_info);
2650
0
    frame_settings->values.extra_channel_blend_info.resize(
2651
0
        frame_settings->enc->metadata.m.num_extra_channels, default_blend_info);
2652
0
  }
2653
0
  frame_settings->values.extra_channel_blend_info[index] = *blend_info;
2654
0
  return JxlErrorOrStatus::Success();
2655
0
}
2656
2657
JxlEncoderStatus JxlEncoderSetFrameName(JxlEncoderFrameSettings* frame_settings,
2658
0
                                        const char* frame_name) {
2659
0
  std::string str = frame_name ? frame_name : "";
2660
0
  if (str.size() > 1071) {
2661
0
    return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE,
2662
0
                         "frame name can be max 1071 bytes long");
2663
0
  }
2664
0
  frame_settings->values.frame_name = str;
2665
0
  frame_settings->values.header.name_length = str.size();
2666
0
  return JxlErrorOrStatus::Success();
2667
0
}
2668
2669
JxlEncoderStatus JxlEncoderSetFrameBitDepth(
2670
0
    JxlEncoderFrameSettings* frame_settings, const JxlBitDepth* bit_depth) {
2671
0
  if (bit_depth->type != JXL_BIT_DEPTH_FROM_PIXEL_FORMAT &&
2672
0
      bit_depth->type != JXL_BIT_DEPTH_FROM_CODESTREAM) {
2673
0
    return JXL_API_ERROR_NOSET(
2674
0
        "Only JXL_BIT_DEPTH_FROM_PIXEL_FORMAT and "
2675
0
        "JXL_BIT_DEPTH_FROM_CODESTREAM is implemented "
2676
0
        "for input buffers.");
2677
0
  }
2678
0
  frame_settings->values.image_bit_depth = *bit_depth;
2679
0
  return JxlErrorOrStatus::Success();
2680
0
}
2681
2682
void JxlColorEncodingSetToSRGB(JxlColorEncoding* color_encoding,
2683
147
                               JXL_BOOL is_gray) {
2684
147
  *color_encoding =
2685
147
      jxl::ColorEncoding::SRGB(FROM_JXL_BOOL(is_gray)).ToExternal();
2686
147
}
2687
2688
void JxlColorEncodingSetToLinearSRGB(JxlColorEncoding* color_encoding,
2689
0
                                     JXL_BOOL is_gray) {
2690
0
  *color_encoding =
2691
0
      jxl::ColorEncoding::LinearSRGB(FROM_JXL_BOOL(is_gray)).ToExternal();
2692
0
}
2693
2694
0
void JxlEncoderAllowExpertOptions(JxlEncoder* enc) {
2695
0
  enc->allow_expert_options = true;
2696
0
}
2697
2698
JXL_EXPORT void JxlEncoderSetDebugImageCallback(
2699
    JxlEncoderFrameSettings* frame_settings, JxlDebugImageCallback callback,
2700
0
    void* opaque) {
2701
0
  frame_settings->values.cparams.debug_image = callback;
2702
0
  frame_settings->values.cparams.debug_image_opaque = opaque;
2703
0
}
2704
2705
0
JXL_EXPORT JxlEncoderStats* JxlEncoderStatsCreate() {
2706
0
  JxlEncoderStats* result = new JxlEncoderStats();
2707
0
  result->aux_out = jxl::make_unique<jxl::AuxOut>();
2708
0
  return result;
2709
0
}
2710
2711
0
JXL_EXPORT void JxlEncoderStatsDestroy(JxlEncoderStats* stats) { delete stats; }
2712
2713
JXL_EXPORT void JxlEncoderCollectStats(JxlEncoderFrameSettings* frame_settings,
2714
0
                                       JxlEncoderStats* stats) {
2715
0
  if (!stats) return;
2716
0
  frame_settings->values.aux_out = stats->aux_out.get();
2717
0
}
2718
2719
JXL_EXPORT size_t JxlEncoderStatsGet(const JxlEncoderStats* stats,
2720
0
                                     JxlEncoderStatsKey key) {
2721
0
  if (!stats) return 0;
2722
0
  const jxl::AuxOut& aux_out = *stats->aux_out;
2723
0
  switch (key) {
2724
0
    case JXL_ENC_STAT_HEADER_BITS:
2725
0
      return aux_out.layer(jxl::LayerType::Header).total_bits;
2726
0
    case JXL_ENC_STAT_TOC_BITS:
2727
0
      return aux_out.layer(jxl::LayerType::Toc).total_bits;
2728
0
    case JXL_ENC_STAT_DICTIONARY_BITS:
2729
0
      return aux_out.layer(jxl::LayerType::Dictionary).total_bits;
2730
0
    case JXL_ENC_STAT_SPLINES_BITS:
2731
0
      return aux_out.layer(jxl::LayerType::Splines).total_bits;
2732
0
    case JXL_ENC_STAT_NOISE_BITS:
2733
0
      return aux_out.layer(jxl::LayerType::Noise).total_bits;
2734
0
    case JXL_ENC_STAT_QUANT_BITS:
2735
0
      return aux_out.layer(jxl::LayerType::Quant).total_bits;
2736
0
    case JXL_ENC_STAT_MODULAR_TREE_BITS:
2737
0
      return aux_out.layer(jxl::LayerType::ModularTree).total_bits;
2738
0
    case JXL_ENC_STAT_MODULAR_GLOBAL_BITS:
2739
0
      return aux_out.layer(jxl::LayerType::ModularGlobal).total_bits;
2740
0
    case JXL_ENC_STAT_DC_BITS:
2741
0
      return aux_out.layer(jxl::LayerType::Dc).total_bits;
2742
0
    case JXL_ENC_STAT_MODULAR_DC_GROUP_BITS:
2743
0
      return aux_out.layer(jxl::LayerType::ModularDcGroup).total_bits;
2744
0
    case JXL_ENC_STAT_CONTROL_FIELDS_BITS:
2745
0
      return aux_out.layer(jxl::LayerType::ControlFields).total_bits;
2746
0
    case JXL_ENC_STAT_COEF_ORDER_BITS:
2747
0
      return aux_out.layer(jxl::LayerType::Order).total_bits;
2748
0
    case JXL_ENC_STAT_AC_HISTOGRAM_BITS:
2749
0
      return aux_out.layer(jxl::LayerType::Ac).total_bits;
2750
0
    case JXL_ENC_STAT_AC_BITS:
2751
0
      return aux_out.layer(jxl::LayerType::AcTokens).total_bits;
2752
0
    case JXL_ENC_STAT_MODULAR_AC_GROUP_BITS:
2753
0
      return aux_out.layer(jxl::LayerType::ModularAcGroup).total_bits;
2754
0
    case JXL_ENC_STAT_NUM_SMALL_BLOCKS:
2755
0
      return aux_out.num_small_blocks;
2756
0
    case JXL_ENC_STAT_NUM_DCT4X8_BLOCKS:
2757
0
      return aux_out.num_dct4x8_blocks;
2758
0
    case JXL_ENC_STAT_NUM_AFV_BLOCKS:
2759
0
      return aux_out.num_afv_blocks;
2760
0
    case JXL_ENC_STAT_NUM_DCT8_BLOCKS:
2761
0
      return aux_out.num_dct8_blocks;
2762
0
    case JXL_ENC_STAT_NUM_DCT8X32_BLOCKS:
2763
0
      return aux_out.num_dct16_blocks;
2764
0
    case JXL_ENC_STAT_NUM_DCT16_BLOCKS:
2765
0
      return aux_out.num_dct16x32_blocks;
2766
0
    case JXL_ENC_STAT_NUM_DCT16X32_BLOCKS:
2767
0
      return aux_out.num_dct32_blocks;
2768
0
    case JXL_ENC_STAT_NUM_DCT32_BLOCKS:
2769
0
      return aux_out.num_dct32x64_blocks;
2770
0
    case JXL_ENC_STAT_NUM_DCT32X64_BLOCKS:
2771
0
      return aux_out.num_dct32x64_blocks;
2772
0
    case JXL_ENC_STAT_NUM_DCT64_BLOCKS:
2773
0
      return aux_out.num_dct64_blocks;
2774
0
    case JXL_ENC_STAT_NUM_BUTTERAUGLI_ITERS:
2775
0
      return aux_out.num_butteraugli_iters;
2776
0
    default:
2777
0
      return 0;
2778
0
  }
2779
0
}
2780
2781
JXL_EXPORT void JxlEncoderStatsMerge(JxlEncoderStats* stats,
2782
0
                                     const JxlEncoderStats* other) {
2783
0
  if (!stats || !other) return;
2784
0
  stats->aux_out->Assimilate(*other->aux_out);
2785
0
}