| 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_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_ |
| 7 |
#define LIB_JXL_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_ |
| 8 |
|
| 9 |
#include <jxl/memory_manager.h> |
| 10 |
|
| 11 |
#include <cstddef> |
| 12 |
#include <cstdint> |
| 13 |
#include <utility> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "lib/jxl/base/rect.h" |
| 17 |
#include "lib/jxl/base/status.h" |
| 18 |
#include "lib/jxl/dec_group_border.h" |
| 19 |
#include "lib/jxl/frame_header.h" |
| 20 |
#include "lib/jxl/image.h" |
| 21 |
#include "lib/jxl/render_pipeline/render_pipeline.h" |
| 22 |
|
| 23 |
namespace jxl { |
| 24 |
|
| 25 |
// A multithreaded, low-memory rendering pipeline that only allocates a minimal |
| 26 |
// amount of buffers. |
| 27 |
class LowMemoryRenderPipeline final : public RenderPipeline { |
| 28 |
public: |
| 29 |
explicit LowMemoryRenderPipeline(JxlMemoryManager* memory_manager) |
| 30 |
: RenderPipeline(memory_manager) {} |
| 31 |
|
| 32 |
private: |
| 33 |
std::vector<std::pair<ImageF*, Rect>> PrepareBuffers( |
| 34 |
size_t group_id, size_t thread_id) override; |
| 35 |
|
| 36 |
Status PrepareForThreadsInternal(size_t num, bool use_group_ids) override; |
| 37 |
|
| 38 |
Status ProcessBuffers(size_t group_id, size_t thread_id) override; |
| 39 |
|
| 40 |
void ClearDone(size_t i) override { group_border_assigner_.ClearDone(i); } |
| 41 |
|
| 42 |
Status Init() override; |
| 43 |
|
| 44 |
Status EnsureBordersStorage(); |
| 45 |
size_t GroupInputXSize(size_t c) const; |
| 46 |
size_t GroupInputYSize(size_t c) const; |
| 47 |
Status RenderRect(size_t thread_id, std::vector<ImageF>& input_data, |
| 48 |
Rect data_max_color_channel_rect, |
| 49 |
Rect image_max_color_channel_rect); |
| 50 |
Status RenderPadding(size_t thread_id, Rect rect); |
| 51 |
|
| 52 |
Status SaveBorders(size_t group_id, size_t c, const ImageF& in); |
| 53 |
Status LoadBorders(size_t group_id, size_t c, const Rect& r, ImageF* out); |
| 54 |
|
| 55 |
std::pair<size_t, size_t> ColorDimensionsToChannelDimensions( |
| 56 |
std::pair<size_t, size_t> in, size_t c, size_t stage) const; |
| 57 |
|
| 58 |
std::pair<size_t, size_t> BorderToStore(size_t c) const; |
| 59 |
|
| 60 |
bool use_group_ids_; |
| 61 |
|
| 62 |
// Storage for borders between groups. Borders of adjacent groups are stacked |
| 63 |
// together, e.g. bottom border of current group is followed by top border |
| 64 |
// of next group. |
| 65 |
std::vector<ImageF> borders_horizontal_; |
| 66 |
std::vector<ImageF> borders_vertical_; |
| 67 |
|
| 68 |
// Manages the status of borders. |
| 69 |
GroupBorderAssigner group_border_assigner_; |
| 70 |
|
| 71 |
// Size (in color-channel-pixels) of the border around each group that might |
| 72 |
// be assigned to that group. |
| 73 |
std::pair<size_t, size_t> group_border_; |
| 74 |
// base_color_shift_ defines the size of groups in terms of final image |
| 75 |
// pixels. |
| 76 |
size_t base_color_shift_; |
| 77 |
|
| 78 |
// Buffer for decoded pixel data for a group, indexed by [thread][channel] or |
| 79 |
// [group][channel] depending on `use_group_ids_`. |
| 80 |
std::vector<std::vector<ImageF>> group_data_; |
| 81 |
|
| 82 |
// Borders for storing group data. |
| 83 |
size_t group_data_x_border_; |
| 84 |
size_t group_data_y_border_; |
| 85 |
|
| 86 |
// Buffers for intermediate rows for the various stages, indexed by |
| 87 |
// [thread][channel][stage]. |
| 88 |
std::vector<std::vector<std::vector<ImageF>>> stage_data_; |
| 89 |
|
| 90 |
// Buffers for out-of-frame data, indexed by [thread]; every row is a |
| 91 |
// different channel. |
| 92 |
std::vector<ImageF> out_of_frame_data_; |
| 93 |
|
| 94 |
// For each stage, a non-kIgnored channel. |
| 95 |
std::vector<int32_t> anyc_; |
| 96 |
|
| 97 |
// Size of the image at each stage. |
| 98 |
std::vector<Rect> image_rect_; |
| 99 |
|
| 100 |
// For each stage, for each channel, keep track of the kInOut stage that |
| 101 |
// produced the input to that stage (which corresponds to the buffer index |
| 102 |
// containing the data). -1 if data comes from the original input. |
| 103 |
std::vector<std::vector<int32_t>> stage_input_for_channel_; |
| 104 |
|
| 105 |
// Number of (virtual) extra rows that must be processed at each stage |
| 106 |
// to produce sufficient output for future stages. |
| 107 |
std::vector<int> virtual_ypadding_for_output_; |
| 108 |
|
| 109 |
// Same thing for columns, except these are real columns and not virtual ones. |
| 110 |
std::vector<int> xpadding_for_output_; |
| 111 |
|
| 112 |
// First stage that doesn't have any kInOut channel. |
| 113 |
size_t first_trailing_stage_; |
| 114 |
|
| 115 |
// Origin and size of the frame after switching to image dimensions. |
| 116 |
FrameOrigin frame_origin_; |
| 117 |
size_t full_image_xsize_; |
| 118 |
size_t full_image_ysize_; |
| 119 |
size_t first_image_dim_stage_; |
| 120 |
}; |
| 121 |
|
| 122 |
} // namespace jxl |
| 123 |
|
| 124 |
#endif // LIB_JXL_RENDER_PIPELINE_LOW_MEMORY_RENDER_PIPELINE_H_ |