summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/render_pipeline/low_memory_render_pipeline.h
blob: b386f7c078a2ef857f475a87f9eb9991e36276b6 (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
// 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.

#ifndef LIB_JXL_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_
#define LIB_JXL_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_

#include <stdint.h>

#include "lib/jxl/dec_group_border.h"
#include "lib/jxl/render_pipeline/render_pipeline.h"

namespace jxl {

// A multithreaded, low-memory rendering pipeline that only allocates a minimal
// amount of buffers.
class LowMemoryRenderPipeline final : public RenderPipeline {
 private:
  std::vector<std::pair<ImageF*, Rect>> PrepareBuffers(
      size_t group_id, size_t thread_id) override;

  void PrepareForThreadsInternal(size_t num, bool use_group_ids) override;

  void ProcessBuffers(size_t group_id, size_t thread_id) override;

  void ClearDone(size_t i) override { group_border_assigner_.ClearDone(i); }

  void Init() override;

  void EnsureBordersStorage();
  size_t GroupInputXSize(size_t c) const;
  size_t GroupInputYSize(size_t c) const;
  void RenderRect(size_t thread_id, std::vector<ImageF>& input_data,
                  Rect data_max_color_channel_rect,
                  Rect image_max_color_channel_rect);
  void RenderPadding(size_t thread_id, Rect rect);

  void SaveBorders(size_t group_id, size_t c, const ImageF& in);
  void LoadBorders(size_t group_id, size_t c, const Rect& r, ImageF* out);

  std::pair<size_t, size_t> ColorDimensionsToChannelDimensions(
      std::pair<size_t, size_t> in, size_t c, size_t stage) const;

  std::pair<size_t, size_t> BorderToStore(size_t c) const;

  bool use_group_ids_;

  // Storage for borders between groups. Borders of adjacent groups are stacked
  // together, e.g. bottom border of current group is followed by top border
  // of next group.
  std::vector<ImageF> borders_horizontal_;
  std::vector<ImageF> borders_vertical_;

  // Manages the status of borders.
  GroupBorderAssigner group_border_assigner_;

  // Size (in color-channel-pixels) of the border around each group that might
  // be assigned to that group.
  std::pair<size_t, size_t> group_border_;
  // base_color_shift_ defines the size of groups in terms of final image
  // pixels.
  size_t base_color_shift_;

  // Buffer for decoded pixel data for a group, indexed by [thread][channel] or
  // [group][channel] depending on `use_group_ids_`.
  std::vector<std::vector<ImageF>> group_data_;

  // Borders for storing group data.
  size_t group_data_x_border_;
  size_t group_data_y_border_;

  // Buffers for intermediate rows for the various stages, indexed by
  // [thread][channel][stage].
  std::vector<std::vector<std::vector<ImageF>>> stage_data_;

  // Buffers for out-of-frame data, indexed by [thread]; every row is a
  // different channel.
  std::vector<ImageF> out_of_frame_data_;

  // For each stage, a non-kIgnored channel.
  std::vector<int32_t> anyc_;

  // Size of the image at each stage.
  std::vector<Rect> image_rect_;

  // For each stage, for each channel, keep track of the kInOut stage that
  // produced the input to that stage (which corresponds to the buffer index
  // containing the data). -1 if data comes from the original input.
  std::vector<std::vector<int32_t>> stage_input_for_channel_;

  // Number of (virtual) extra rows that must be processed at each stage
  // to produce sufficient output for future stages.
  std::vector<int> virtual_ypadding_for_output_;

  // Same thing for columns, except these are real columns and not virtual ones.
  std::vector<int> xpadding_for_output_;

  // First stage that doesn't have any kInOut channel.
  size_t first_trailing_stage_;

  // Origin and size of the frame after switching to image dimensions.
  FrameOrigin frame_origin_;
  size_t full_image_xsize_;
  size_t full_image_ysize_;
  size_t first_image_dim_stage_;
};

}  // namespace jxl

#endif  // LIB_JXL_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_