Blender  V2.93
denoising.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2018 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __DENOISING_H__
18 #define __DENOISING_H__
19 
20 #include "device/device.h"
22 
23 #include "render/buffers.h"
24 
25 #include "util/util_string.h"
26 #include "util/util_unique_ptr.h"
27 #include "util/util_vector.h"
28 
29 #include <OpenImageIO/imageio.h>
30 
31 OIIO_NAMESPACE_USING
32 
34 
35 /* Denoiser */
36 
37 class Denoiser {
38  public:
39  Denoiser(DeviceInfo &device_info);
40  ~Denoiser();
41 
42  bool run();
43 
44  /* Error message after running, in case of failure. */
45  string error;
46 
47  /* Sequential list of frame filepaths to denoise. */
49  /* Sequential list of frame filepaths to write result to. Empty entries
50  * are skipped, so only a subset of the sequence can be denoised while
51  * taking into account all input frames. */
53 
54  /* Sample number override, takes precedence over values from input frames. */
56  /* Tile size for processing on device. */
58 
59  /* Equivalent to the settings in the regular denoiser. */
61 
62  protected:
63  friend class DenoiseTask;
64 
68 
70 };
71 
72 /* Denoise Image Layer */
73 
75  string name;
76  /* All channels belonging to this DenoiseImageLayer. */
78  /* Layer to image channel mapping. */
80 
81  /* Sample amount that was used for rendering this layer. */
82  int samples;
83 
84  /* Device input channel will be copied from image channel input_to_image_channel[i]. */
86 
87  /* input_to_image_channel of the secondary frames, if any are used. */
89 
90  /* Write i-th channel of the processing output to output_to_image_channel[i]-th channel of the
91  * file. */
93 
94  /* Detect whether this layer contains a full set of channels and set up the offsets accordingly.
95  */
97 
98  /* Map the channels of a secondary frame to the channels that are required for processing,
99  * fill neighbor_input_to_image_channel if all are present or return false if a channel are
100  * missing. */
101  bool match_channels(int neighbor,
102  const std::vector<string> &channelnames,
103  const std::vector<string> &neighbor_channelnames);
104 };
105 
106 /* Denoise Image Data */
107 
109  public:
110  DenoiseImage();
111  ~DenoiseImage();
112 
113  /* Dimensions */
115 
116  /* Samples */
117  int samples;
118 
119  /* Pixel buffer with interleaved channels. */
121 
122  /* Image file handles */
123  ImageSpec in_spec;
125 
126  /* Render layers */
128 
129  void free();
130 
131  /* Open the input image, parse its channels, open the output image and allocate the output
132  * buffer. */
133  bool load(const string &in_filepath, string &error);
134 
135  /* Load neighboring frames. */
136  bool load_neighbors(const vector<string> &filepaths, const vector<int> &frames, string &error);
137 
138  /* Load subset of pixels from file buffer into input buffer, as needed for denoising
139  * on the device. Channels are reshuffled following the provided mapping. */
140  void read_pixels(const DenoiseImageLayer &layer, float *input_pixels);
141  bool read_neighbor_pixels(int neighbor, const DenoiseImageLayer &layer, float *input_pixels);
142 
143  bool save_output(const string &out_filepath, string &error);
144 
145  protected:
146  /* Parse input file channels, separate them into DenoiseImageLayers,
147  * detect DenoiseImageLayers with full channel sets,
148  * fill layers and set up the output channels and passthrough map. */
149  bool parse_channels(const ImageSpec &in_spec, string &error);
150 
151  void close_input();
152 };
153 
154 /* Denoise Task */
155 
156 class DenoiseTask {
157  public:
159  ~DenoiseTask();
160 
161  /* Task stages */
162  bool load();
163  bool exec();
164  bool save();
165  void free();
166 
167  string error;
168 
169  protected:
170  /* Denoiser parameters and device */
173 
174  /* Frame number to be denoised */
175  int frame;
177 
178  /* Image file data */
181 
182  /* Device input buffer */
184 
185  /* Tiles */
187  list<RenderTile> tiles;
189 
191  map<int, device_vector<float> *> output_pixels;
192 
193  /* Task handling */
194  bool load_input_pixels(int layer);
195  void create_task(DeviceTask &task);
196 
197  /* Device task callbacks */
198  bool acquire_tile(Device *device, Device *tile_device, RenderTile &tile);
199  void map_neighboring_tiles(RenderTileNeighbors &neighbors, Device *tile_device);
201  void release_tile();
202  bool get_cancel();
203 };
204 
206 
207 #endif /* __DENOISING_H__ */
vector< unique_ptr< ImageInput > > in_neighbors
Definition: denoising.h:124
bool parse_channels(const ImageSpec &in_spec, string &error)
Definition: denoising.cpp:618
void close_input()
Definition: denoising.cpp:607
bool read_neighbor_pixels(int neighbor, const DenoiseImageLayer &layer, float *input_pixels)
Definition: denoising.cpp:694
bool load_neighbors(const vector< string > &filepaths, const vector< int > &frames, string &error)
Definition: denoising.cpp:759
array< float > pixels
Definition: denoising.h:120
vector< DenoiseImageLayer > layers
Definition: denoising.h:127
int num_channels
Definition: denoising.h:114
bool load(const string &in_filepath, string &error)
Definition: denoising.cpp:719
bool save_output(const string &out_filepath, string &error)
Definition: denoising.cpp:802
ImageSpec in_spec
Definition: denoising.h:123
void read_pixels(const DenoiseImageLayer &layer, float *input_pixels)
Definition: denoising.cpp:679
void release_tile()
Definition: denoising.cpp:367
DenoiseTask(Device *device, Denoiser *denoiser, int frame, const vector< int > &neighbor_frames)
Definition: denoising.cpp:227
map< int, device_vector< float > * > output_pixels
Definition: denoising.h:191
bool acquire_tile(Device *device, Device *tile_device, RenderTile &tile)
Definition: denoising.cpp:249
thread_mutex tiles_mutex
Definition: denoising.h:186
bool get_cancel()
Definition: denoising.cpp:371
bool load()
Definition: denoising.cpp:526
string error
Definition: denoising.h:167
Device * device
Definition: denoising.h:172
int num_tiles
Definition: denoising.h:188
bool exec()
Definition: denoising.cpp:556
Denoiser * denoiser
Definition: denoising.h:171
list< RenderTile > tiles
Definition: denoising.h:187
bool save()
Definition: denoising.cpp:578
bool load_input_pixels(int layer)
Definition: denoising.cpp:440
device_vector< float > input_pixels
Definition: denoising.h:183
int current_layer
Definition: denoising.h:180
void map_neighboring_tiles(RenderTileNeighbors &neighbors, Device *tile_device)
Definition: denoising.cpp:274
void free()
Definition: denoising.cpp:585
vector< int > neighbor_frames
Definition: denoising.h:176
void create_task(DeviceTask &task)
Definition: denoising.cpp:376
DenoiseImage image
Definition: denoising.h:179
void unmap_neighboring_tiles(RenderTileNeighbors &neighbors)
Definition: denoising.cpp:333
thread_mutex output_mutex
Definition: denoising.h:190
Device * device
Definition: denoising.h:67
int samples_override
Definition: denoising.h:55
int num_frames
Definition: denoising.h:69
string error
Definition: denoising.h:45
vector< string > output
Definition: denoising.h:52
bool run()
Definition: denoising.cpp:890
Stats stats
Definition: denoising.h:65
DenoiseParams params
Definition: denoising.h:60
Profiler profiler
Definition: denoising.h:66
vector< string > input
Definition: denoising.h:48
Denoiser(DeviceInfo &device_info)
Definition: denoising.cpp:866
int2 tile_size
Definition: denoising.h:57
Definition: device.h:293
#define CCL_NAMESPACE_END
static void error(const char *str)
Definition: meshlaplacian.c:65
struct blender::compositor::@172::@174 task
vector< int > input_to_image_channel
Definition: denoising.h:85
bool match_channels(int neighbor, const std::vector< string > &channelnames, const std::vector< string > &neighbor_channelnames)
Definition: denoising.cpp:200
bool detect_denoising_channels()
Definition: denoising.cpp:157
vector< int > output_to_image_channel
Definition: denoising.h:92
vector< vector< int > > neighbor_input_to_image_channel
Definition: denoising.h:88
vector< string > channels
Definition: denoising.h:77
vector< int > layer_to_image_channel
Definition: denoising.h:79
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: util_thread.h:40