Coverage Report

Created: 2025-06-16 07:00

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