denoising.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef __DENOISING_H__
  17. #define __DENOISING_H__
  18. #include "device/device.h"
  19. #include "device/device_denoising.h"
  20. #include "render/buffers.h"
  21. #include "util/util_string.h"
  22. #include "util/util_vector.h"
  23. #include "util/util_unique_ptr.h"
  24. #include <OpenImageIO/imageio.h>
  25. OIIO_NAMESPACE_USING
  26. CCL_NAMESPACE_BEGIN
  27. /* Denoiser */
  28. class Denoiser {
  29. public:
  30. Denoiser(DeviceInfo &device_info);
  31. ~Denoiser();
  32. bool run();
  33. /* Error message after running, in case of failure. */
  34. string error;
  35. /* Sequential list of frame filepaths to denoise. */
  36. vector<string> input;
  37. /* Sequential list of frame filepaths to write result to. Empty entries
  38. * are skipped, so only a subset of the sequence can be denoised while
  39. * taking into account all input frames. */
  40. vector<string> output;
  41. /* Sample number override, takes precedence over values from input frames. */
  42. int samples_override;
  43. /* Tile size for processing on device. */
  44. int2 tile_size;
  45. /* Equivalent to the settings in the regular denoiser. */
  46. DenoiseParams params;
  47. protected:
  48. friend class DenoiseTask;
  49. Stats stats;
  50. Profiler profiler;
  51. Device *device;
  52. int num_frames;
  53. };
  54. /* Denoise Image Layer */
  55. struct DenoiseImageLayer {
  56. string name;
  57. /* All channels belonging to this DenoiseImageLayer. */
  58. vector<string> channels;
  59. /* Layer to image channel mapping. */
  60. vector<int> layer_to_image_channel;
  61. /* Sample amount that was used for rendering this layer. */
  62. int samples;
  63. /* Device input channel will be copied from image channel input_to_image_channel[i]. */
  64. vector<int> input_to_image_channel;
  65. /* input_to_image_channel of the secondary frames, if any are used. */
  66. vector<vector<int>> neighbor_input_to_image_channel;
  67. /* Write i-th channel of the processing output to output_to_image_channel[i]-th channel of the
  68. * file. */
  69. vector<int> output_to_image_channel;
  70. /* Detect whether this layer contains a full set of channels and set up the offsets accordingly.
  71. */
  72. bool detect_denoising_channels();
  73. /* Map the channels of a secondary frame to the channels that are required for processing,
  74. * fill neighbor_input_to_image_channel if all are present or return false if a channel are
  75. * missing. */
  76. bool match_channels(int neighbor,
  77. const std::vector<string> &channelnames,
  78. const std::vector<string> &neighbor_channelnames);
  79. };
  80. /* Denoise Image Data */
  81. class DenoiseImage {
  82. public:
  83. DenoiseImage();
  84. ~DenoiseImage();
  85. /* Dimensions */
  86. int width, height, num_channels;
  87. /* Samples */
  88. int samples;
  89. /* Pixel buffer with interleaved channels. */
  90. array<float> pixels;
  91. /* Image file handles */
  92. ImageSpec in_spec;
  93. vector<unique_ptr<ImageInput>> in_neighbors;
  94. /* Render layers */
  95. vector<DenoiseImageLayer> layers;
  96. void free();
  97. /* Open the input image, parse its channels, open the output image and allocate the output
  98. * buffer. */
  99. bool load(const string &in_filepath, string &error);
  100. /* Load neighboring frames. */
  101. bool load_neighbors(const vector<string> &filepaths, const vector<int> &frames, string &error);
  102. /* Load subset of pixels from file buffer into input buffer, as needed for denoising
  103. * on the device. Channels are reshuffled following the provided mapping. */
  104. void read_pixels(const DenoiseImageLayer &layer, float *input_pixels);
  105. bool read_neighbor_pixels(int neighbor, const DenoiseImageLayer &layer, float *input_pixels);
  106. bool save_output(const string &out_filepath, string &error);
  107. protected:
  108. /* Parse input file channels, separate them into DenoiseImageLayers,
  109. * detect DenoiseImageLayers with full channel sets,
  110. * fill layers and set up the output channels and passthrough map. */
  111. bool parse_channels(const ImageSpec &in_spec, string &error);
  112. void close_input();
  113. };
  114. /* Denoise Task */
  115. class DenoiseTask {
  116. public:
  117. DenoiseTask(Device *device, Denoiser *denoiser, int frame, const vector<int> &neighbor_frames);
  118. ~DenoiseTask();
  119. /* Task stages */
  120. bool load();
  121. bool exec();
  122. bool save();
  123. void free();
  124. string error;
  125. protected:
  126. /* Denoiser parameters and device */
  127. Denoiser *denoiser;
  128. Device *device;
  129. /* Frame number to be denoised */
  130. int frame;
  131. vector<int> neighbor_frames;
  132. /* Image file data */
  133. DenoiseImage image;
  134. int current_layer;
  135. /* Device input buffer */
  136. device_vector<float> input_pixels;
  137. /* Tiles */
  138. thread_mutex tiles_mutex;
  139. list<RenderTile> tiles;
  140. int num_tiles;
  141. thread_mutex output_mutex;
  142. map<int, device_vector<float> *> output_pixels;
  143. /* Task handling */
  144. bool load_input_pixels(int layer);
  145. void create_task(DeviceTask &task);
  146. /* Device task callbacks */
  147. bool acquire_tile(Device *device, Device *tile_device, RenderTile &tile);
  148. void map_neighboring_tiles(RenderTile *tiles, Device *tile_device);
  149. void unmap_neighboring_tiles(RenderTile *tiles);
  150. void release_tile();
  151. bool get_cancel();
  152. };
  153. CCL_NAMESPACE_END
  154. #endif /* __DENOISING_H__ */