Coverage Report

Created: 2024-05-21 06:41

/src/libjxl/lib/jxl/frame_header.h
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
#ifndef LIB_JXL_FRAME_HEADER_H_
7
#define LIB_JXL_FRAME_HEADER_H_
8
9
// Frame header with backward and forward-compatible extension capability and
10
// compressed integer fields.
11
12
#include <algorithm>
13
#include <array>
14
#include <cstddef>
15
#include <cstdint>
16
#include <string>
17
#include <vector>
18
19
#include "lib/jxl/base/common.h"
20
#include "lib/jxl/base/compiler_specific.h"
21
#include "lib/jxl/base/status.h"
22
#include "lib/jxl/coeff_order_fwd.h"
23
#include "lib/jxl/common.h"  // kMaxNumPasses
24
#include "lib/jxl/dec_bit_reader.h"
25
#include "lib/jxl/field_encodings.h"
26
#include "lib/jxl/fields.h"
27
#include "lib/jxl/frame_dimensions.h"
28
#include "lib/jxl/image_metadata.h"
29
#include "lib/jxl/loop_filter.h"
30
31
namespace jxl {
32
33
// TODO(eustas): move to proper place?
34
// Also used by extra channel names.
35
static inline Status VisitNameString(Visitor* JXL_RESTRICT visitor,
36
1.14M
                                     std::string* name) {
37
1.14M
  uint32_t name_length = static_cast<uint32_t>(name->length());
38
  // Allows layer name lengths up to 1071 bytes
39
1.14M
  JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Val(0), Bits(4), BitsOffset(5, 16),
40
1.14M
                                         BitsOffset(10, 48), 0, &name_length));
41
1.14M
  if (visitor->IsReading()) {
42
159k
    name->resize(name_length);
43
159k
  }
44
2.42M
  for (size_t i = 0; i < name_length; i++) {
45
1.28M
    uint32_t c = static_cast<uint8_t>((*name)[i]);
46
1.28M
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(8, 0, &c));
47
1.27M
    (*name)[i] = static_cast<char>(c);
48
1.27M
  }
49
1.13M
  return true;
50
1.14M
}
Unexecuted instantiation: djxl_fuzzer.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: decode.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
frame_header.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Line
Count
Source
36
387k
                                     std::string* name) {
37
387k
  uint32_t name_length = static_cast<uint32_t>(name->length());
38
  // Allows layer name lengths up to 1071 bytes
39
387k
  JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Val(0), Bits(4), BitsOffset(5, 16),
40
387k
                                         BitsOffset(10, 48), 0, &name_length));
41
386k
  if (visitor->IsReading()) {
42
80.8k
    name->resize(name_length);
43
80.8k
  }
44
1.20M
  for (size_t i = 0; i < name_length; i++) {
45
824k
    uint32_t c = static_cast<uint8_t>((*name)[i]);
46
824k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(8, 0, &c));
47
819k
    (*name)[i] = static_cast<char>(c);
48
819k
  }
49
381k
  return true;
50
386k
}
image_metadata.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Line
Count
Source
36
761k
                                     std::string* name) {
37
761k
  uint32_t name_length = static_cast<uint32_t>(name->length());
38
  // Allows layer name lengths up to 1071 bytes
39
761k
  JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Val(0), Bits(4), BitsOffset(5, 16),
40
761k
                                         BitsOffset(10, 48), 0, &name_length));
41
761k
  if (visitor->IsReading()) {
42
78.4k
    name->resize(name_length);
43
78.4k
  }
44
1.21M
  for (size_t i = 0; i < name_length; i++) {
45
458k
    uint32_t c = static_cast<uint8_t>((*name)[i]);
46
458k
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(8, 0, &c));
47
454k
    (*name)[i] = static_cast<char>(c);
48
454k
  }
49
757k
  return true;
50
761k
}
Unexecuted instantiation: quant_weights.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: decode_to_jpeg.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_jpeg_data_writer.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_frame.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_group.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_modular.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_patch_dictionary.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: epf.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: passes_state.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: render_pipeline.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: simple_render_pipeline.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_upsampling.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: blending.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: compressed_dc.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: dec_cache.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: low_memory_render_pipeline.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_blending.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_chroma_upsampling.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_cms.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_epf.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_from_linear.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_gaborish.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_noise.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_patches.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_splines.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_spot.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_to_linear.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_tone_mapping.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_write.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_xyb.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: stage_ycbcr.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Unexecuted instantiation: image_bundle.cc:jxl::VisitNameString(jxl::Visitor*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
51
52
enum class FrameEncoding : uint32_t {
53
  kVarDCT,
54
  kModular,
55
};
56
57
enum class ColorTransform : uint32_t {
58
  kXYB,    // Values are encoded with XYB. May only be used if
59
           // ImageBundle::xyb_encoded.
60
  kNone,   // Values are encoded according to the attached color profile. May
61
           // only be used if !ImageBundle::xyb_encoded.
62
  kYCbCr,  // Values are encoded according to the attached color profile, but
63
           // transformed to YCbCr. May only be used if
64
           // !ImageBundle::xyb_encoded.
65
};
66
67
943
inline std::array<int, 3> JpegOrder(ColorTransform ct, bool is_gray) {
68
943
  if (is_gray) {
69
943
    return {{0, 0, 0}};
70
943
  }
71
0
  JXL_ASSERT(ct != ColorTransform::kXYB);
72
0
  if (ct == ColorTransform::kYCbCr) {
73
0
    return {{1, 0, 2}};
74
0
  } else {
75
0
    return {{0, 1, 2}};
76
0
  }
77
0
}
78
79
struct YCbCrChromaSubsampling : public Fields {
80
  YCbCrChromaSubsampling();
81
  JXL_FIELDS_NAME(YCbCrChromaSubsampling)
82
979k
  size_t HShift(size_t c) const { return maxhs_ - kHShift[channel_mode_[c]]; }
83
343k
  size_t VShift(size_t c) const { return maxvs_ - kVShift[channel_mode_[c]]; }
84
85
457k
  Status VisitFields(Visitor* JXL_RESTRICT visitor) override {
86
    // TODO(veluca): consider allowing 4x downsamples
87
1.37M
    for (uint32_t& ch : channel_mode_) {
88
1.37M
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(2, 0, &ch));
89
1.37M
    }
90
456k
    Recompute();
91
456k
    return true;
92
457k
  }
93
94
269k
  uint8_t MaxHShift() const { return maxhs_; }
95
269k
  uint8_t MaxVShift() const { return maxvs_; }
96
97
336
  uint8_t RawHShift(size_t c) const { return kHShift[channel_mode_[c]]; }
98
336
  uint8_t RawVShift(size_t c) const { return kVShift[channel_mode_[c]]; }
99
100
  // Uses JPEG channel order (Y, Cb, Cr).
101
0
  Status Set(const uint8_t* hsample, const uint8_t* vsample) {
102
0
    for (size_t c = 0; c < 3; c++) {
103
0
      size_t cjpeg = c < 2 ? c ^ 1 : c;
104
0
      size_t i = 0;
105
0
      for (; i < 4; i++) {
106
0
        if (1 << kHShift[i] == hsample[cjpeg] &&
107
0
            1 << kVShift[i] == vsample[cjpeg]) {
108
0
          channel_mode_[c] = i;
109
0
          break;
110
0
        }
111
0
      }
112
0
      if (i == 4) {
113
0
        return JXL_FAILURE("Invalid subsample mode");
114
0
      }
115
0
    }
116
0
    Recompute();
117
0
    return true;
118
0
  }
119
120
79.8k
  bool Is444() const {
121
79.8k
    return HShift(0) == 0 && VShift(0) == 0 &&  // Cb
122
79.8k
           HShift(2) == 0 && VShift(2) == 0 &&  // Cr
123
79.8k
           HShift(1) == 0 && VShift(1) == 0;    // Y
124
79.8k
  }
125
126
0
  bool Is420() const {
127
0
    return HShift(0) == 1 && VShift(0) == 1 &&  // Cb
128
0
           HShift(2) == 1 && VShift(2) == 1 &&  // Cr
129
0
           HShift(1) == 0 && VShift(1) == 0;    // Y
130
0
  }
131
132
0
  bool Is422() const {
133
0
    return HShift(0) == 1 && VShift(0) == 0 &&  // Cb
134
0
           HShift(2) == 1 && VShift(2) == 0 &&  // Cr
135
0
           HShift(1) == 0 && VShift(1) == 0;    // Y
136
0
  }
137
138
0
  bool Is440() const {
139
0
    return HShift(0) == 0 && VShift(0) == 1 &&  // Cb
140
0
           HShift(2) == 0 && VShift(2) == 1 &&  // Cr
141
0
           HShift(1) == 0 && VShift(1) == 0;    // Y
142
0
  }
143
144
0
  std::string DebugString() const {
145
0
    if (Is444()) return "444";
146
0
    if (Is420()) return "420";
147
0
    if (Is422()) return "422";
148
0
    if (Is440()) return "440";
149
0
    return "cs" + std::to_string(channel_mode_[0]) +
150
0
           std::to_string(channel_mode_[1]) + std::to_string(channel_mode_[2]);
151
0
  }
152
153
 private:
154
456k
  void Recompute() {
155
456k
    maxhs_ = 0;
156
456k
    maxvs_ = 0;
157
1.36M
    for (uint32_t ch : channel_mode_) {
158
1.36M
      maxhs_ = std::max(maxhs_, kHShift[ch]);
159
1.36M
      maxvs_ = std::max(maxvs_, kVShift[ch]);
160
1.36M
    }
161
456k
  }
162
  static const uint8_t kHShift[4];
163
  static const uint8_t kVShift[4];
164
  uint32_t channel_mode_[3];
165
  uint8_t maxhs_;
166
  uint8_t maxvs_;
167
};
168
169
// Indicates how to combine the current frame with a previously-saved one. Can
170
// be independently controlled for color and extra channels. Formulas are
171
// indicative and treat alpha as if it is in range 0.0-1.0. In descriptions
172
// below, alpha channel is the extra channel of type alpha used for blending
173
// according to the blend_channel, or fully opaque if there is no alpha channel.
174
// The blending specified here is used for performing blending *after* color
175
// transforms - in linear sRGB if blending a XYB-encoded frame on another
176
// XYB-encoded frame, in sRGB if blending a frame with kColorSpace == kSRGB, or
177
// in the original colorspace otherwise. Blending in XYB or YCbCr is done by
178
// using patches.
179
enum class BlendMode {
180
  // The new values (in the crop) replace the old ones: sample = new
181
  kReplace = 0,
182
  // The new values (in the crop) get added to the old ones: sample = old + new
183
  kAdd = 1,
184
  // The new values (in the crop) replace the old ones if alpha>0:
185
  // For the alpha channel that is used as source:
186
  // alpha = old + new * (1 - old)
187
  // For other channels if !alpha_associated:
188
  // sample = ((1 - new_alpha) * old * old_alpha + new_alpha * new) / alpha
189
  // For other channels if alpha_associated:
190
  // sample = (1 - new_alpha) * old + new
191
  // The alpha formula applies to the alpha used for the division in the other
192
  // channels formula, and applies to the alpha channel itself if its
193
  // blend_channel value matches itself.
194
  kBlend = 2,
195
  // The new values (in the crop) are added to the old ones if alpha>0:
196
  // For the alpha channel that is used as source:
197
  // sample = sample = old + new * (1 - old)
198
  // For other channels: sample = old + alpha * new
199
  kAlphaWeightedAdd = 3,
200
  // The new values (in the crop) get multiplied by the old ones:
201
  // sample = old * new
202
  // The range of the new value matters for multiplication purposes, and its
203
  // nominal range of 0..1 is computed the same way as this is done for the
204
  // alpha values in kBlend and kAlphaWeightedAdd.
205
  // If using kMul as a blend mode for color channels, no color transform is
206
  // performed on the current frame.
207
  kMul = 4,
208
};
209
210
struct BlendingInfo : public Fields {
211
  BlendingInfo();
212
  JXL_FIELDS_NAME(BlendingInfo)
213
  Status VisitFields(Visitor* JXL_RESTRICT visitor) override;
214
  BlendMode mode;
215
  // Which extra channel to use as alpha channel for blending, only encoded
216
  // for blend modes that involve alpha and if there are more than 1 extra
217
  // channels.
218
  uint32_t alpha_channel;
219
  // Clamp alpha or channel values to 0-1 range.
220
  bool clamp;
221
  // Frame ID to copy from (0-3). Only encoded if blend_mode is not kReplace.
222
  uint32_t source;
223
224
  std::string DebugString() const;
225
226
  size_t nonserialized_num_extra_channels = 0;
227
  bool nonserialized_is_partial_frame = false;
228
};
229
230
// Origin of the current frame. Not present for frames of type
231
// kOnlyPatches.
232
struct FrameOrigin {
233
  int32_t x0, y0;  // can be negative.
234
};
235
236
// Size of the current frame.
237
struct FrameSize {
238
  uint32_t xsize, ysize;
239
};
240
241
// AnimationFrame defines duration of animation frames.
242
struct AnimationFrame : public Fields {
243
  explicit AnimationFrame(const CodecMetadata* metadata);
244
  JXL_FIELDS_NAME(AnimationFrame)
245
246
  Status VisitFields(Visitor* JXL_RESTRICT visitor) override;
247
248
  // How long to wait [in ticks, see Animation{}] after rendering.
249
  // May be 0 if the current frame serves as a foundation for another frame.
250
  uint32_t duration;
251
252
  uint32_t timecode;  // 0xHHMMSSFF
253
254
  // Must be set to the one ImageMetadata acting as the full codestream header,
255
  // with correct xyb_encoded, list of extra channels, etc...
256
  const CodecMetadata* nonserialized_metadata = nullptr;
257
};
258
259
// For decoding to lower resolutions. Only used for kRegular frames.
260
struct Passes : public Fields {
261
  Passes();
262
  JXL_FIELDS_NAME(Passes)
263
264
  Status VisitFields(Visitor* JXL_RESTRICT visitor) override;
265
266
69.9k
  void GetDownsamplingBracket(size_t pass, int& minShift, int& maxShift) const {
267
69.9k
    maxShift = 2;
268
69.9k
    minShift = 3;
269
76.5k
    for (size_t i = 0;; i++) {
270
88.8k
      for (uint32_t j = 0; j < num_downsample; ++j) {
271
12.3k
        if (i == last_pass[j]) {
272
3.51k
          if (downsample[j] == 8) minShift = 3;
273
3.51k
          if (downsample[j] == 4) minShift = 2;
274
3.51k
          if (downsample[j] == 2) minShift = 1;
275
3.51k
          if (downsample[j] == 1) minShift = 0;
276
3.51k
        }
277
12.3k
      }
278
76.5k
      if (i == num_passes - 1) minShift = 0;
279
76.5k
      if (i == pass) return;
280
6.61k
      maxShift = minShift - 1;
281
6.61k
    }
282
69.9k
  }
283
284
0
  uint32_t GetDownsamplingTargetForCompletedPasses(uint32_t num_p) const {
285
0
    if (num_p >= num_passes) return 1;
286
0
    uint32_t retval = 8;
287
0
    for (uint32_t i = 0; i < num_downsample; ++i) {
288
0
      if (num_p > last_pass[i]) {
289
0
        retval = std::min(retval, downsample[i]);
290
0
      }
291
0
    }
292
0
    return retval;
293
0
  }
294
295
  std::string DebugString() const;
296
297
  uint32_t num_passes;      // <= kMaxNumPasses
298
  uint32_t num_downsample;  // <= num_passes
299
300
  // Array of num_downsample pairs. downsample=1/last_pass=num_passes-1 and
301
  // downsample=8/last_pass=0 need not be specified; they are implicit.
302
  uint32_t downsample[kMaxNumPasses];
303
  uint32_t last_pass[kMaxNumPasses];
304
  // Array of shift values for each pass. It is implicitly assumed to be 0 for
305
  // the last pass.
306
  uint32_t shift[kMaxNumPasses];
307
};
308
309
enum FrameType {
310
  // A "regular" frame: might be a crop, and will be blended on a previous
311
  // frame, if any, and displayed or blended in future frames.
312
  kRegularFrame = 0,
313
  // A DC frame: this frame is downsampled and will be *only* used as the DC of
314
  // a future frame and, possibly, for previews. Cannot be cropped, blended, or
315
  // referenced by patches or blending modes. Frames that *use* a DC frame
316
  // cannot have non-default sizes either.
317
  kDCFrame = 1,
318
  // A PatchesSource frame: this frame will be only used as a source frame for
319
  // taking patches. Can be cropped, but cannot have non-(0, 0) x0 and y0.
320
  kReferenceOnly = 2,
321
  // Same as kRegularFrame, but not used for progressive rendering. This also
322
  // implies no early display of DC.
323
  kSkipProgressive = 3,
324
};
325
326
// Image/frame := one of more of these, where the last has is_last = true.
327
// Starts at a byte-aligned address "a"; the next pass starts at "a + size".
328
struct FrameHeader : public Fields {
329
  // Optional postprocessing steps. These flags are the source of truth;
330
  // Override must set/clear them rather than change their meaning. Values
331
  // chosen such that typical flags == 0 (encoded in only two bits).
332
  enum Flags {
333
    // Often but not always off => low bit value:
334
335
    // Inject noise into decoded output.
336
    kNoise = 1,
337
338
    // Overlay patches.
339
    kPatches = 2,
340
341
    // 4, 8 = reserved for future sometimes-off
342
343
    // Overlay splines.
344
    kSplines = 16,
345
346
    kUseDcFrame = 32,  // Implies kSkipAdaptiveDCSmoothing.
347
348
    // 64 = reserved for future often-off
349
350
    // Almost always on => negated:
351
352
    kSkipAdaptiveDCSmoothing = 128,
353
  };
354
355
  explicit FrameHeader(const CodecMetadata* metadata);
356
  JXL_FIELDS_NAME(FrameHeader)
357
358
  Status VisitFields(Visitor* JXL_RESTRICT visitor) override;
359
360
  // Sets/clears `flag` based upon `condition`.
361
0
  void UpdateFlag(const bool condition, const uint64_t flag) {
362
0
    if (condition) {
363
0
      flags |= flag;
364
0
    } else {
365
0
      flags &= ~flag;
366
0
    }
367
0
  }
368
369
  // Returns true if this frame is supposed to be saved for future usage by
370
  // other frames.
371
518k
  bool CanBeReferenced() const {
372
    // DC frames cannot be referenced. The last frame cannot be referenced. A
373
    // duration 0 frame makes little sense if it is not referenced. A
374
    // non-duration 0 frame may or may not be referenced.
375
518k
    return !is_last && frame_type != FrameType::kDCFrame &&
376
518k
           (animation_frame.duration == 0 || save_as_reference != 0);
377
518k
  }
378
379
  mutable bool all_default;
380
381
  // Always present
382
  FrameEncoding encoding;
383
  // Some versions of UBSAN complain in VisitFrameType if not initialized.
384
  FrameType frame_type = FrameType::kRegularFrame;
385
386
  uint64_t flags;
387
388
  ColorTransform color_transform;
389
  YCbCrChromaSubsampling chroma_subsampling;
390
391
  uint32_t group_size_shift;  // only if encoding == kModular;
392
393
  uint32_t x_qm_scale;  // only if VarDCT and color_transform == kXYB
394
  uint32_t b_qm_scale;  // only if VarDCT and color_transform == kXYB
395
396
  std::string name;
397
398
  // Skipped for kReferenceOnly.
399
  Passes passes;
400
401
  // Skipped for kDCFrame
402
  bool custom_size_or_origin;
403
  FrameSize frame_size;
404
405
  // upsampling factors for color and extra channels.
406
  // Upsampling is always performed before applying any inverse color transform.
407
  // Skipped (1) if kUseDCFrame
408
  uint32_t upsampling;
409
  std::vector<uint32_t> extra_channel_upsampling;
410
411
  // Only for kRegular frames.
412
  FrameOrigin frame_origin;
413
414
  BlendingInfo blending_info;
415
  std::vector<BlendingInfo> extra_channel_blending_info;
416
417
  // Animation info for this frame.
418
  AnimationFrame animation_frame;
419
420
  // This is the last frame.
421
  bool is_last;
422
423
  // ID to refer to this frame with. 0-3, not present if kDCFrame.
424
  // 0 has a special meaning for kRegular frames of nonzero duration: it defines
425
  // a frame that will not be referenced in the future.
426
  uint32_t save_as_reference;
427
428
  // Whether to save this frame before or after the color transform. A frame
429
  // that is saved before the color tansform can only be used for blending
430
  // through patches. On the contrary, a frame that is saved after the color
431
  // transform can only be used for blending through blending modes.
432
  // Irrelevant for extra channel blending. Can only be true if
433
  // blending_info.mode == kReplace and this is not a partial kRegularFrame; if
434
  // this is a DC frame, it is always true.
435
  bool save_before_color_transform;
436
437
  uint32_t dc_level;  // 1-4 if kDCFrame (0 otherwise).
438
439
  // Must be set to the one ImageMetadata acting as the full codestream header,
440
  // with correct xyb_encoded, list of extra channels, etc...
441
  const CodecMetadata* nonserialized_metadata = nullptr;
442
443
  // NOTE: This is ignored by AllDefault.
444
  LoopFilter loop_filter;
445
446
  bool nonserialized_is_preview = false;
447
448
592k
  size_t default_xsize() const {
449
592k
    if (!nonserialized_metadata) return 0;
450
592k
    if (nonserialized_is_preview) {
451
7.90k
      return nonserialized_metadata->m.preview_size.xsize();
452
7.90k
    }
453
584k
    return nonserialized_metadata->xsize();
454
592k
  }
455
456
592k
  size_t default_ysize() const {
457
592k
    if (!nonserialized_metadata) return 0;
458
592k
    if (nonserialized_is_preview) {
459
7.90k
      return nonserialized_metadata->m.preview_size.ysize();
460
7.90k
    }
461
584k
    return nonserialized_metadata->ysize();
462
592k
  }
463
464
269k
  FrameDimensions ToFrameDimensions() const {
465
269k
    size_t xsize = default_xsize();
466
269k
    size_t ysize = default_ysize();
467
468
269k
    xsize = frame_size.xsize ? frame_size.xsize : xsize;
469
269k
    ysize = frame_size.ysize ? frame_size.ysize : ysize;
470
471
269k
    if (dc_level != 0) {
472
19.9k
      xsize = DivCeil(xsize, 1 << (3 * dc_level));
473
19.9k
      ysize = DivCeil(ysize, 1 << (3 * dc_level));
474
19.9k
    }
475
476
269k
    FrameDimensions frame_dim;
477
269k
    frame_dim.Set(xsize, ysize, group_size_shift,
478
269k
                  chroma_subsampling.MaxHShift(),
479
269k
                  chroma_subsampling.MaxVShift(),
480
269k
                  encoding == FrameEncoding::kModular, upsampling);
481
269k
    return frame_dim;
482
269k
  }
483
484
  // True if a color transform should be applied to this frame.
485
0
  bool needs_color_transform() const {
486
0
    return !save_before_color_transform ||
487
0
           frame_type == FrameType::kRegularFrame ||
488
0
           frame_type == FrameType::kSkipProgressive;
489
0
  }
490
491
  std::string DebugString() const;
492
493
  uint64_t extensions;
494
};
495
496
Status ReadFrameHeader(BitReader* JXL_RESTRICT reader,
497
                       FrameHeader* JXL_RESTRICT frame);
498
499
// Shared by enc/dec. 5F and 13 are by far the most common for d1/2/4/8, 0
500
// ensures low overhead for small images.
501
static constexpr U32Enc kOrderEnc =
502
    U32Enc(Val(0x5F), Val(0x13), Val(0), Bits(kNumOrders));
503
504
}  // namespace jxl
505
506
#endif  // LIB_JXL_FRAME_HEADER_H_