summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/modular/encoding/encoding.cc
blob: 7e7aa019e314134934dc452f044b55e2ba52a65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/modular/encoding/encoding.h"

#include <stdint.h>
#include <stdlib.h>

#include <queue>

#include "lib/jxl/base/printf_macros.h"
#include "lib/jxl/base/scope_guard.h"
#include "lib/jxl/dec_ans.h"
#include "lib/jxl/dec_bit_reader.h"
#include "lib/jxl/frame_dimensions.h"
#include "lib/jxl/image_ops.h"
#include "lib/jxl/modular/encoding/context_predict.h"
#include "lib/jxl/modular/options.h"
#include "lib/jxl/pack_signed.h"

namespace jxl {

// Removes all nodes that use a static property (i.e. channel or group ID) from
// the tree and collapses each node on even levels with its two children to
// produce a flatter tree. Also computes whether the resulting tree requires
// using the weighted predictor.
FlatTree FilterTree(const Tree &global_tree,
                    std::array<pixel_type, kNumStaticProperties> &static_props,
                    size_t *num_props, bool *use_wp, bool *wp_only,
                    bool *gradient_only) {
  *num_props = 0;
  bool has_wp = false;
  bool has_non_wp = false;
  *gradient_only = true;
  const auto mark_property = [&](int32_t p) {
    if (p == kWPProp) {
      has_wp = true;
    } else if (p >= kNumStaticProperties) {
      has_non_wp = true;
    }
    if (p >= kNumStaticProperties && p != kGradientProp) {
      *gradient_only = false;
    }
  };
  FlatTree output;
  std::queue<size_t> nodes;
  nodes.push(0);
  // Produces a trimmed and flattened tree by doing a BFS visit of the original
  // tree, ignoring branches that are known to be false and proceeding two
  // levels at a time to collapse nodes in a flatter tree; if an inner parent
  // node has a leaf as a child, the leaf is duplicated and an implicit fake
  // node is added. This allows to reduce the number of branches when traversing
  // the resulting flat tree.
  while (!nodes.empty()) {
    size_t cur = nodes.front();
    nodes.pop();
    // Skip nodes that we can decide now, by jumping directly to their children.
    while (global_tree[cur].property < kNumStaticProperties &&
           global_tree[cur].property != -1) {
      if (static_props[global_tree[cur].property] > global_tree[cur].splitval) {
        cur = global_tree[cur].lchild;
      } else {
        cur = global_tree[cur].rchild;
      }
    }
    FlatDecisionNode flat;
    if (global_tree[cur].property == -1) {
      flat.property0 = -1;
      flat.childID = global_tree[cur].lchild;
      flat.predictor = global_tree[cur].predictor;
      flat.predictor_offset = global_tree[cur].predictor_offset;
      flat.multiplier = global_tree[cur].multiplier;
      *gradient_only &= flat.predictor == Predictor::Gradient;
      has_wp |= flat.predictor == Predictor::Weighted;
      has_non_wp |= flat.predictor != Predictor::Weighted;
      output.push_back(flat);
      continue;
    }
    flat.childID = output.size() + nodes.size() + 1;

    flat.property0 = global_tree[cur].property;
    *num_props = std::max<size_t>(flat.property0 + 1, *num_props);
    flat.splitval0 = global_tree[cur].splitval;

    for (size_t i = 0; i < 2; i++) {
      size_t cur_child =
          i == 0 ? global_tree[cur].lchild : global_tree[cur].rchild;
      // Skip nodes that we can decide now.
      while (global_tree[cur_child].property < kNumStaticProperties &&
             global_tree[cur_child].property != -1) {
        if (static_props[global_tree[cur_child].property] >
            global_tree[cur_child].splitval) {
          cur_child = global_tree[cur_child].lchild;
        } else {
          cur_child = global_tree[cur_child].rchild;
        }
      }
      // We ended up in a leaf, add a placeholder decision and two copies of the
      // leaf.
      if (global_tree[cur_child].property == -1) {
        flat.properties[i] = 0;
        flat.splitvals[i] = 0;
        nodes.push(cur_child);
        nodes.push(cur_child);
      } else {
        flat.properties[i] = global_tree[cur_child].property;
        flat.splitvals[i] = global_tree[cur_child].splitval;
        nodes.push(global_tree[cur_child].lchild);
        nodes.push(global_tree[cur_child].rchild);
        *num_props = std::max<size_t>(flat.properties[i] + 1, *num_props);
      }
    }

    for (int16_t property : flat.properties) mark_property(property);
    mark_property(flat.property0);
    output.push_back(flat);
  }
  if (*num_props > kNumNonrefProperties) {
    *num_props =
        DivCeil(*num_props - kNumNonrefProperties, kExtraPropsPerChannel) *
            kExtraPropsPerChannel +
        kNumNonrefProperties;
  } else {
    *num_props = kNumNonrefProperties;
  }
  *use_wp = has_wp;
  *wp_only = has_wp && !has_non_wp;

  return output;
}

namespace detail {
template <bool uses_lz77>
Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader,
                                 const std::vector<uint8_t> &context_map,
                                 const Tree &global_tree,
                                 const weighted::Header &wp_header,
                                 pixel_type chan, size_t group_id,
                                 TreeLut<uint8_t, true> &tree_lut,
                                 Image *image) {
  Channel &channel = image->channel[chan];

  std::array<pixel_type, kNumStaticProperties> static_props = {
      {chan, static_cast<int>(group_id)}};
  // TODO(veluca): filter the tree according to static_props.

  // zero pixel channel? could happen
  if (channel.w == 0 || channel.h == 0) return true;

  bool tree_has_wp_prop_or_pred = false;
  bool is_wp_only = false;
  bool is_gradient_only = false;
  size_t num_props;
  FlatTree tree =
      FilterTree(global_tree, static_props, &num_props,
                 &tree_has_wp_prop_or_pred, &is_wp_only, &is_gradient_only);

  // From here on, tree lookup returns a *clustered* context ID.
  // This avoids an extra memory lookup after tree traversal.
  for (auto &node : tree) {
    if (node.property0 == -1) {
      node.childID = context_map[node.childID];
    }
  }

  JXL_DEBUG_V(3, "Decoded MA tree with %" PRIuS " nodes", tree.size());

  // MAANS decode
  const auto make_pixel = [](uint64_t v, pixel_type multiplier,
                             pixel_type_w offset) -> pixel_type {
    JXL_DASSERT((v & 0xFFFFFFFF) == v);
    pixel_type_w val = UnpackSigned(v);
    // if it overflows, it overflows, and we have a problem anyway
    return val * multiplier + offset;
  };

  if (tree.size() == 1) {
    // special optimized case: no meta-adaptation, so no need
    // to compute properties.
    Predictor predictor = tree[0].predictor;
    int64_t offset = tree[0].predictor_offset;
    int32_t multiplier = tree[0].multiplier;
    size_t ctx_id = tree[0].childID;
    if (predictor == Predictor::Zero) {
      uint32_t value;
      if (reader->IsSingleValueAndAdvance(ctx_id, &value,
                                          channel.w * channel.h)) {
        // Special-case: histogram has a single symbol, with no extra bits, and
        // we use ANS mode.
        JXL_DEBUG_V(8, "Fastest track.");
        pixel_type v = make_pixel(value, multiplier, offset);
        for (size_t y = 0; y < channel.h; y++) {
          pixel_type *JXL_RESTRICT r = channel.Row(y);
          std::fill(r, r + channel.w, v);
        }
      } else {
        JXL_DEBUG_V(8, "Fast track.");
        if (multiplier == 1 && offset == 0) {
          for (size_t y = 0; y < channel.h; y++) {
            pixel_type *JXL_RESTRICT r = channel.Row(y);
            for (size_t x = 0; x < channel.w; x++) {
              uint32_t v =
                  reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br);
              r[x] = UnpackSigned(v);
            }
          }
        } else {
          for (size_t y = 0; y < channel.h; y++) {
            pixel_type *JXL_RESTRICT r = channel.Row(y);
            for (size_t x = 0; x < channel.w; x++) {
              uint32_t v =
                  reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id,
                                                                         br);
              r[x] = make_pixel(v, multiplier, offset);
            }
          }
        }
      }
      return true;
    } else if (uses_lz77 && predictor == Predictor::Gradient && offset == 0 &&
               multiplier == 1 && reader->HuffRleOnly()) {
      JXL_DEBUG_V(8, "Gradient RLE (fjxl) very fast track.");
      uint32_t run = 0;
      uint32_t v = 0;
      pixel_type_w sv = 0;
      for (size_t y = 0; y < channel.h; y++) {
        pixel_type *JXL_RESTRICT r = channel.Row(y);
        const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1);
        const pixel_type *JXL_RESTRICT rtopleft =
            (y ? channel.Row(y - 1) - 1 : r - 1);
        pixel_type_w guess = (y ? rtop[0] : 0);
        if (run == 0) {
          reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &v, &run);
          sv = UnpackSigned(v);
        } else {
          run--;
        }
        r[0] = sv + guess;
        for (size_t x = 1; x < channel.w; x++) {
          pixel_type left = r[x - 1];
          pixel_type top = rtop[x];
          pixel_type topleft = rtopleft[x];
          pixel_type_w guess = ClampedGradient(top, left, topleft);
          if (!run) {
            reader->ReadHybridUintClusteredHuffRleOnly(ctx_id, br, &v, &run);
            sv = UnpackSigned(v);
          } else {
            run--;
          }
          r[x] = sv + guess;
        }
      }
      return true;
    } else if (predictor == Predictor::Gradient && offset == 0 &&
               multiplier == 1) {
      JXL_DEBUG_V(8, "Gradient very fast track.");
      const intptr_t onerow = channel.plane.PixelsPerRow();
      for (size_t y = 0; y < channel.h; y++) {
        pixel_type *JXL_RESTRICT r = channel.Row(y);
        for (size_t x = 0; x < channel.w; x++) {
          pixel_type left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0);
          pixel_type top = (y ? *(r + x - onerow) : left);
          pixel_type topleft = (x && y ? *(r + x - 1 - onerow) : left);
          pixel_type guess = ClampedGradient(top, left, topleft);
          uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(
              ctx_id, br);
          r[x] = make_pixel(v, 1, guess);
        }
      }
      return true;
    }
  }

  // Check if this tree is a WP-only tree with a small enough property value
  // range.
  if (is_wp_only) {
    is_wp_only = TreeToLookupTable(tree, tree_lut);
  }
  if (is_gradient_only) {
    is_gradient_only = TreeToLookupTable(tree, tree_lut);
  }

  if (is_gradient_only) {
    JXL_DEBUG_V(8, "Gradient fast track.");
    const intptr_t onerow = channel.plane.PixelsPerRow();
    for (size_t y = 0; y < channel.h; y++) {
      pixel_type *JXL_RESTRICT r = channel.Row(y);
      for (size_t x = 0; x < channel.w; x++) {
        pixel_type_w left = (x ? r[x - 1] : y ? *(r + x - onerow) : 0);
        pixel_type_w top = (y ? *(r + x - onerow) : left);
        pixel_type_w topleft = (x && y ? *(r + x - 1 - onerow) : left);
        int32_t guess = ClampedGradient(top, left, topleft);
        uint32_t pos =
            kPropRangeFast +
            std::min<pixel_type_w>(
                std::max<pixel_type_w>(-kPropRangeFast, top + left - topleft),
                kPropRangeFast - 1);
        uint32_t ctx_id = tree_lut.context_lookup[pos];
        uint64_t v =
            reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(ctx_id, br);
        r[x] = make_pixel(
            v, tree_lut.multipliers[pos],
            static_cast<pixel_type_w>(tree_lut.offsets[pos]) + guess);
      }
    }
  } else if (!uses_lz77 && is_wp_only && channel.w > 8) {
    JXL_DEBUG_V(8, "WP fast track.");
    weighted::State wp_state(wp_header, channel.w, channel.h);
    Properties properties(1);
    for (size_t y = 0; y < channel.h; y++) {
      pixel_type *JXL_RESTRICT r = channel.Row(y);
      const pixel_type *JXL_RESTRICT rtop = (y ? channel.Row(y - 1) : r - 1);
      const pixel_type *JXL_RESTRICT rtoptop =
          (y > 1 ? channel.Row(y - 2) : rtop);
      const pixel_type *JXL_RESTRICT rtopleft =
          (y ? channel.Row(y - 1) - 1 : r - 1);
      const pixel_type *JXL_RESTRICT rtopright =
          (y ? channel.Row(y - 1) + 1 : r - 1);
      size_t x = 0;
      {
        size_t offset = 0;
        pixel_type_w left = y ? rtop[x] : 0;
        pixel_type_w toptop = y ? rtoptop[x] : 0;
        pixel_type_w topright = (x + 1 < channel.w && y ? rtop[x + 1] : left);
        int32_t guess = wp_state.Predict</*compute_properties=*/true>(
            x, y, channel.w, left, left, topright, left, toptop, &properties,
            offset);
        uint32_t pos =
            kPropRangeFast + std::min(std::max(-kPropRangeFast, properties[0]),
                                      kPropRangeFast - 1);
        uint32_t ctx_id = tree_lut.context_lookup[pos];
        uint64_t v =
            reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br);
        r[x] = make_pixel(
            v, tree_lut.multipliers[pos],
            static_cast<pixel_type_w>(tree_lut.offsets[pos]) + guess);
        wp_state.UpdateErrors(r[x], x, y, channel.w);
      }
      for (x = 1; x + 1 < channel.w; x++) {
        size_t offset = 0;
        int32_t guess = wp_state.Predict</*compute_properties=*/true>(
            x, y, channel.w, rtop[x], r[x - 1], rtopright[x], rtopleft[x],
            rtoptop[x], &properties, offset);
        uint32_t pos =
            kPropRangeFast + std::min(std::max(-kPropRangeFast, properties[0]),
                                      kPropRangeFast - 1);
        uint32_t ctx_id = tree_lut.context_lookup[pos];
        uint64_t v =
            reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br);
        r[x] = make_pixel(
            v, tree_lut.multipliers[pos],
            static_cast<pixel_type_w>(tree_lut.offsets[pos]) + guess);
        wp_state.UpdateErrors(r[x], x, y, channel.w);
      }
      {
        size_t offset = 0;
        int32_t guess = wp_state.Predict</*compute_properties=*/true>(
            x, y, channel.w, rtop[x], r[x - 1], rtop[x], rtopleft[x],
            rtoptop[x], &properties, offset);
        uint32_t pos =
            kPropRangeFast + std::min(std::max(-kPropRangeFast, properties[0]),
                                      kPropRangeFast - 1);
        uint32_t ctx_id = tree_lut.context_lookup[pos];
        uint64_t v =
            reader->ReadHybridUintClusteredInlined<uses_lz77>(ctx_id, br);
        r[x] = make_pixel(
            v, tree_lut.multipliers[pos],
            static_cast<pixel_type_w>(tree_lut.offsets[pos]) + guess);
        wp_state.UpdateErrors(r[x], x, y, channel.w);
      }
    }
  } else if (!tree_has_wp_prop_or_pred) {
    // special optimized case: the weighted predictor and its properties are not
    // used, so no need to compute weights and properties.
    JXL_DEBUG_V(8, "Slow track.");
    MATreeLookup tree_lookup(tree);
    Properties properties = Properties(num_props);
    const intptr_t onerow = channel.plane.PixelsPerRow();
    JXL_ASSIGN_OR_RETURN(
        Channel references,
        Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
    for (size_t y = 0; y < channel.h; y++) {
      pixel_type *JXL_RESTRICT p = channel.Row(y);
      PrecomputeReferences(channel, y, *image, chan, &references);
      InitPropsRow(&properties, static_props, y);
      if (y > 1 && channel.w > 8 && references.w == 0) {
        for (size_t x = 0; x < 2; x++) {
          PredictionResult res =
              PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y,
                              tree_lookup, references);
          uint64_t v =
              reader->ReadHybridUintClustered<uses_lz77>(res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
        }
        for (size_t x = 2; x < channel.w - 2; x++) {
          PredictionResult res =
              PredictTreeNoWPNEC(&properties, channel.w, p + x, onerow, x, y,
                                 tree_lookup, references);
          uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>(
              res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
        }
        for (size_t x = channel.w - 2; x < channel.w; x++) {
          PredictionResult res =
              PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y,
                              tree_lookup, references);
          uint64_t v =
              reader->ReadHybridUintClustered<uses_lz77>(res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
        }
      } else {
        for (size_t x = 0; x < channel.w; x++) {
          PredictionResult res =
              PredictTreeNoWP(&properties, channel.w, p + x, onerow, x, y,
                              tree_lookup, references);
          uint64_t v = reader->ReadHybridUintClusteredMaybeInlined<uses_lz77>(
              res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
        }
      }
    }
  } else {
    JXL_DEBUG_V(8, "Slowest track.");
    MATreeLookup tree_lookup(tree);
    Properties properties = Properties(num_props);
    const intptr_t onerow = channel.plane.PixelsPerRow();
    JXL_ASSIGN_OR_RETURN(
        Channel references,
        Channel::Create(properties.size() - kNumNonrefProperties, channel.w));
    weighted::State wp_state(wp_header, channel.w, channel.h);
    for (size_t y = 0; y < channel.h; y++) {
      pixel_type *JXL_RESTRICT p = channel.Row(y);
      InitPropsRow(&properties, static_props, y);
      PrecomputeReferences(channel, y, *image, chan, &references);
      if (!uses_lz77 && y > 1 && channel.w > 8 && references.w == 0) {
        for (size_t x = 0; x < 2; x++) {
          PredictionResult res =
              PredictTreeWP(&properties, channel.w, p + x, onerow, x, y,
                            tree_lookup, references, &wp_state);
          uint64_t v =
              reader->ReadHybridUintClustered<uses_lz77>(res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
          wp_state.UpdateErrors(p[x], x, y, channel.w);
        }
        for (size_t x = 2; x < channel.w - 2; x++) {
          PredictionResult res =
              PredictTreeWPNEC(&properties, channel.w, p + x, onerow, x, y,
                               tree_lookup, references, &wp_state);
          uint64_t v = reader->ReadHybridUintClusteredInlined<uses_lz77>(
              res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
          wp_state.UpdateErrors(p[x], x, y, channel.w);
        }
        for (size_t x = channel.w - 2; x < channel.w; x++) {
          PredictionResult res =
              PredictTreeWP(&properties, channel.w, p + x, onerow, x, y,
                            tree_lookup, references, &wp_state);
          uint64_t v =
              reader->ReadHybridUintClustered<uses_lz77>(res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
          wp_state.UpdateErrors(p[x], x, y, channel.w);
        }
      } else {
        for (size_t x = 0; x < channel.w; x++) {
          PredictionResult res =
              PredictTreeWP(&properties, channel.w, p + x, onerow, x, y,
                            tree_lookup, references, &wp_state);
          uint64_t v =
              reader->ReadHybridUintClustered<uses_lz77>(res.context, br);
          p[x] = make_pixel(v, res.multiplier, res.guess);
          wp_state.UpdateErrors(p[x], x, y, channel.w);
        }
      }
    }
  }
  return true;
}
}  // namespace detail

Status DecodeModularChannelMAANS(BitReader *br, ANSSymbolReader *reader,
                                 const std::vector<uint8_t> &context_map,
                                 const Tree &global_tree,
                                 const weighted::Header &wp_header,
                                 pixel_type chan, size_t group_id,
                                 TreeLut<uint8_t, true> &tree_lut,
                                 Image *image) {
  if (reader->UsesLZ77()) {
    return detail::DecodeModularChannelMAANS</*uses_lz77=*/true>(
        br, reader, context_map, global_tree, wp_header, chan, group_id,
        tree_lut, image);
  } else {
    return detail::DecodeModularChannelMAANS</*uses_lz77=*/false>(
        br, reader, context_map, global_tree, wp_header, chan, group_id,
        tree_lut, image);
  }
}

GroupHeader::GroupHeader() { Bundle::Init(this); }

Status ValidateChannelDimensions(const Image &image,
                                 const ModularOptions &options) {
  size_t nb_channels = image.channel.size();
  for (bool is_dc : {true, false}) {
    size_t group_dim = options.group_dim * (is_dc ? kBlockDim : 1);
    size_t c = image.nb_meta_channels;
    for (; c < nb_channels; c++) {
      const Channel &ch = image.channel[c];
      if (ch.w > options.group_dim || ch.h > options.group_dim) break;
    }
    for (; c < nb_channels; c++) {
      const Channel &ch = image.channel[c];
      if (ch.w == 0 || ch.h == 0) continue;  // skip empty
      bool is_dc_channel = std::min(ch.hshift, ch.vshift) >= 3;
      if (is_dc_channel != is_dc) continue;
      size_t tile_dim = group_dim >> std::max(ch.hshift, ch.vshift);
      if (tile_dim == 0) {
        return JXL_FAILURE("Inconsistent transforms");
      }
    }
  }
  return true;
}

Status ModularDecode(BitReader *br, Image &image, GroupHeader &header,
                     size_t group_id, ModularOptions *options,
                     const Tree *global_tree, const ANSCode *global_code,
                     const std::vector<uint8_t> *global_ctx_map,
                     const bool allow_truncated_group) {
  if (image.channel.empty()) return true;

  // decode transforms
  Status status = Bundle::Read(br, &header);
  if (!allow_truncated_group) JXL_RETURN_IF_ERROR(status);
  if (status.IsFatalError()) return status;
  if (!br->AllReadsWithinBounds()) {
    // Don't do/undo transforms if header is incomplete.
    header.transforms.clear();
    image.transform = header.transforms;
    for (auto &ch : image.channel) {
      ZeroFillImage(&ch.plane);
    }
    return Status(StatusCode::kNotEnoughBytes);
  }

  JXL_DEBUG_V(3, "Image data underwent %" PRIuS " transformations: ",
              header.transforms.size());
  image.transform = header.transforms;
  for (Transform &transform : image.transform) {
    JXL_RETURN_IF_ERROR(transform.MetaApply(image));
  }
  if (image.error) {
    return JXL_FAILURE("Corrupt file. Aborting.");
  }
  JXL_RETURN_IF_ERROR(ValidateChannelDimensions(image, *options));

  size_t nb_channels = image.channel.size();

  size_t num_chans = 0;
  size_t distance_multiplier = 0;
  for (size_t i = 0; i < nb_channels; i++) {
    Channel &channel = image.channel[i];
    if (!channel.w || !channel.h) {
      continue;  // skip empty channels
    }
    if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size ||
                                        channel.h > options->max_chan_size)) {
      break;
    }
    if (channel.w > distance_multiplier) {
      distance_multiplier = channel.w;
    }
    num_chans++;
  }
  if (num_chans == 0) return true;

  size_t next_channel = 0;
  auto scope_guard = MakeScopeGuard([&]() {
    for (size_t c = next_channel; c < image.channel.size(); c++) {
      ZeroFillImage(&image.channel[c].plane);
    }
  });
  // Do not do anything if truncated groups are not allowed.
  if (allow_truncated_group) scope_guard.Disarm();

  // Read tree.
  Tree tree_storage;
  std::vector<uint8_t> context_map_storage;
  ANSCode code_storage;
  const Tree *tree = &tree_storage;
  const ANSCode *code = &code_storage;
  const std::vector<uint8_t> *context_map = &context_map_storage;
  if (!header.use_global_tree) {
    uint64_t max_tree_size = 1024;
    for (size_t i = 0; i < nb_channels; i++) {
      Channel &channel = image.channel[i];
      if (i >= image.nb_meta_channels && (channel.w > options->max_chan_size ||
                                          channel.h > options->max_chan_size)) {
        break;
      }
      uint64_t pixels = channel.w * channel.h;
      max_tree_size += pixels;
    }
    max_tree_size = std::min(static_cast<uint64_t>(1 << 20), max_tree_size);
    JXL_RETURN_IF_ERROR(DecodeTree(br, &tree_storage, max_tree_size));
    JXL_RETURN_IF_ERROR(DecodeHistograms(br, (tree_storage.size() + 1) / 2,
                                         &code_storage, &context_map_storage));
  } else {
    if (!global_tree || !global_code || !global_ctx_map ||
        global_tree->empty()) {
      return JXL_FAILURE("No global tree available but one was requested");
    }
    tree = global_tree;
    code = global_code;
    context_map = global_ctx_map;
  }

  // Read channels
  ANSSymbolReader reader(code, br, distance_multiplier);
  auto tree_lut = jxl::make_unique<TreeLut<uint8_t, true>>();
  for (; next_channel < nb_channels; next_channel++) {
    Channel &channel = image.channel[next_channel];
    if (!channel.w || !channel.h) {
      continue;  // skip empty channels
    }
    if (next_channel >= image.nb_meta_channels &&
        (channel.w > options->max_chan_size ||
         channel.h > options->max_chan_size)) {
      break;
    }
    JXL_RETURN_IF_ERROR(DecodeModularChannelMAANS(
        br, &reader, *context_map, *tree, header.wp_header, next_channel,
        group_id, *tree_lut, &image));

    // Truncated group.
    if (!br->AllReadsWithinBounds()) {
      if (!allow_truncated_group) return JXL_FAILURE("Truncated input");
      return Status(StatusCode::kNotEnoughBytes);
    }
  }

  // Make sure no zero-filling happens even if next_channel < nb_channels.
  scope_guard.Disarm();

  if (!reader.CheckANSFinalState()) {
    return JXL_FAILURE("ANS decode final state failed");
  }
  return true;
}

Status ModularGenericDecompress(BitReader *br, Image &image,
                                GroupHeader *header, size_t group_id,
                                ModularOptions *options, bool undo_transforms,
                                const Tree *tree, const ANSCode *code,
                                const std::vector<uint8_t> *ctx_map,
                                bool allow_truncated_group) {
#ifdef JXL_ENABLE_ASSERT
  std::vector<std::pair<uint32_t, uint32_t>> req_sizes(image.channel.size());
  for (size_t c = 0; c < req_sizes.size(); c++) {
    req_sizes[c] = {image.channel[c].w, image.channel[c].h};
  }
#endif
  GroupHeader local_header;
  if (header == nullptr) header = &local_header;
  size_t bit_pos = br->TotalBitsConsumed();
  auto dec_status = ModularDecode(br, image, *header, group_id, options, tree,
                                  code, ctx_map, allow_truncated_group);
  if (!allow_truncated_group) JXL_RETURN_IF_ERROR(dec_status);
  if (dec_status.IsFatalError()) return dec_status;
  if (undo_transforms) image.undo_transforms(header->wp_header);
  if (image.error) return JXL_FAILURE("Corrupt file. Aborting.");
  JXL_DEBUG_V(4,
              "Modular-decoded a %" PRIuS "x%" PRIuS " nbchans=%" PRIuS
              " image from %" PRIuS " bytes",
              image.w, image.h, image.channel.size(),
              (br->TotalBitsConsumed() - bit_pos) / 8);
  JXL_DEBUG_V(5, "Modular image: %s", image.DebugString().c_str());
  (void)bit_pos;
#ifdef JXL_ENABLE_ASSERT
  // Check that after applying all transforms we are back to the requested image
  // sizes, otherwise there's a programming error with the transformations.
  if (undo_transforms) {
    JXL_ASSERT(image.channel.size() == req_sizes.size());
    for (size_t c = 0; c < req_sizes.size(); c++) {
      JXL_ASSERT(req_sizes[c].first == image.channel[c].w);
      JXL_ASSERT(req_sizes[c].second == image.channel[c].h);
    }
  }
#endif
  return dec_status;
}

}  // namespace jxl