Coverage Report

Created: 2026-06-16 07:20

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