Coverage Report

Created: 2022-08-24 06:04

/src/libjxl/lib/jxl/progressive_split.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_PROGRESSIVE_SPLIT_H_
7
#define LIB_JXL_PROGRESSIVE_SPLIT_H_
8
9
#include <stddef.h>
10
#include <stdint.h>
11
12
#include <limits>
13
#include <memory>
14
#include <vector>
15
16
#include "lib/jxl/ac_strategy.h"
17
#include "lib/jxl/base/compiler_specific.h"
18
#include "lib/jxl/base/status.h"
19
#include "lib/jxl/chroma_from_luma.h"
20
#include "lib/jxl/codec_in_out.h"
21
#include "lib/jxl/common.h"
22
#include "lib/jxl/dct_util.h"
23
#include "lib/jxl/frame_header.h"
24
#include "lib/jxl/image.h"
25
#include "lib/jxl/image_ops.h"
26
#include "lib/jxl/splines.h"
27
28
// Functions to split DCT coefficients in multiple passes. All the passes of a
29
// single frame are added together.
30
31
namespace jxl {
32
33
constexpr size_t kNoDownsamplingFactor = std::numeric_limits<size_t>::max();
34
35
struct PassDefinition {
36
  // Side of the square of the coefficients that should be kept in each 8x8
37
  // block. Must be greater than 1, and at most 8. Should be in non-decreasing
38
  // order.
39
  size_t num_coefficients;
40
41
  // How much to shift the encoded values by, with rounding.
42
  size_t shift;
43
44
  // If specified, this indicates that if the requested downsampling factor is
45
  // sufficiently high, then it is fine to stop decoding after this pass.
46
  // By default, passes are not marked as being suitable for any downsampling.
47
  size_t suitable_for_downsampling_of_at_least;
48
};
49
50
struct ProgressiveMode {
51
  size_t num_passes = 1;
52
  PassDefinition passes[kMaxNumPasses] = {
53
      PassDefinition{/*num_coefficients=*/8, /*shift=*/0,
54
                     /*suitable_for_downsampling_of_at_least=*/1}};
55
56
92
  ProgressiveMode() = default;
57
58
  template <size_t nump>
59
0
  explicit ProgressiveMode(const PassDefinition (&p)[nump]) {
60
0
    JXL_ASSERT(nump <= kMaxNumPasses);
61
0
    num_passes = nump;
62
0
    PassDefinition previous_pass{
63
0
        /*num_coefficients=*/1, /*shift=*/0,
64
0
        /*suitable_for_downsampling_of_at_least=*/kNoDownsamplingFactor};
65
0
    size_t last_downsampling_factor = kNoDownsamplingFactor;
66
0
    for (size_t i = 0; i < nump; i++) {
67
0
      JXL_ASSERT(p[i].num_coefficients > previous_pass.num_coefficients ||
68
0
                 (p[i].num_coefficients == previous_pass.num_coefficients &&
69
0
                  p[i].shift < previous_pass.shift));
70
0
      JXL_ASSERT(p[i].suitable_for_downsampling_of_at_least ==
71
0
                     kNoDownsamplingFactor ||
72
0
                 p[i].suitable_for_downsampling_of_at_least <=
73
0
                     last_downsampling_factor);
74
0
      if (p[i].suitable_for_downsampling_of_at_least != kNoDownsamplingFactor) {
75
0
        last_downsampling_factor = p[i].suitable_for_downsampling_of_at_least;
76
0
      }
77
0
      previous_pass = passes[i] = p[i];
78
0
    }
79
0
  }
Unexecuted instantiation: jxl::ProgressiveMode::ProgressiveMode<2ul>(jxl::PassDefinition const (&) [2ul])
Unexecuted instantiation: jxl::ProgressiveMode::ProgressiveMode<3ul>(jxl::PassDefinition const (&) [3ul])
80
};
81
82
class ProgressiveSplitter {
83
 public:
84
0
  void SetProgressiveMode(ProgressiveMode mode) { mode_ = mode; }
85
86
1.01k
  size_t GetNumPasses() const { return mode_.num_passes; }
87
88
92
  void InitPasses(Passes* JXL_RESTRICT passes) const {
89
92
    passes->num_passes = static_cast<uint32_t>(GetNumPasses());
90
92
    passes->num_downsample = 0;
91
92
    JXL_ASSERT(passes->num_passes != 0);
92
92
    passes->shift[passes->num_passes - 1] = 0;
93
92
    if (passes->num_passes == 1) return;  // Done, arrays are empty
94
95
0
    for (uint32_t i = 0; i < mode_.num_passes - 1; ++i) {
96
0
      const size_t min_downsampling_factor =
97
0
          mode_.passes[i].suitable_for_downsampling_of_at_least;
98
0
      passes->shift[i] = mode_.passes[i].shift;
99
0
      if (1 < min_downsampling_factor &&
100
0
          min_downsampling_factor != kNoDownsamplingFactor) {
101
0
        passes->downsample[passes->num_downsample] = min_downsampling_factor;
102
0
        passes->last_pass[passes->num_downsample] = i;
103
0
        if (mode_.passes[i + 1].suitable_for_downsampling_of_at_least <
104
0
            min_downsampling_factor) {
105
0
          passes->num_downsample += 1;
106
0
        }
107
0
      }
108
0
    }
109
0
  }
110
111
  template <typename T>
112
  void SplitACCoefficients(const T* JXL_RESTRICT block, size_t size,
113
                           const AcStrategy& acs, size_t bx, size_t by,
114
                           size_t offset,
115
                           T* JXL_RESTRICT output[kMaxNumPasses][3]);
116
117
 private:
118
  ProgressiveMode mode_;
119
};
120
121
extern template void ProgressiveSplitter::SplitACCoefficients<int32_t>(
122
    const int32_t* JXL_RESTRICT, size_t, const AcStrategy&, size_t, size_t,
123
    size_t, int32_t* JXL_RESTRICT[kMaxNumPasses][3]);
124
125
extern template void ProgressiveSplitter::SplitACCoefficients<int16_t>(
126
    const int16_t* JXL_RESTRICT, size_t, const AcStrategy&, size_t, size_t,
127
    size_t, int16_t* JXL_RESTRICT[kMaxNumPasses][3]);
128
129
}  // namespace jxl
130
131
#endif  // LIB_JXL_PROGRESSIVE_SPLIT_H_