320 static constexpr double slope = clipperSmallSignalSlope<C, double>();
323 std::int64_t frames_, pad_, extent_;
324 int factor_, block_, highBlock_, delay_ = 0, left_ = 0, filterSize_ = 0;
332 void operator()(std::int64_t first,
int count,
double *out)
334 owner->readPadded(first, count, out, difference);
336 } raw_{
this,
false}, difference_{
this,
true};
337 std::optional<OfflineHilbertMap> rawMap_, differenceMap_;
338 std::array<std::optional<SincPhase>, 16> phases_;
340 std::array<std::int64_t, 3> nativeLeaf_{-1, -1, -1};
341 std::array<std::int64_t, 3> upLeaf_{-1, -1, -1}, shapedLeaf_{-1, -1, -1};
343 std::uint64_t evaluations_ = 0;
344 struct ProjectedReader
347 void operator()(std::int64_t first,
int count,
double *out)
349 owner->projectedSource(first, count, out);
352 std::optional<SincProjection<ProjectedReader>> projection_;
354 static std::int64_t checkedExtent(std::int64_t frames, std::int64_t pad,
int factor,
int block)
356 if (frames < 1 || pad < 64 || factor < 1 || factor > 16 || (factor & (factor - 1)) ||
357 block < 128 || block > 16384 || block * factor > 65536 || (block & (block - 1)))
359 constexpr auto maximum = std::numeric_limits<std::int64_t>::max();
360 if (pad > (maximum / factor - frames) / 2 || frames > maximum / factor)
362 return frames + 2 * pad;
364 void readPadded(std::int64_t first,
int count,
double *out,
bool difference)
368 const auto leaf = first / block_;
369 const int offset =
static_cast<int>(first % block_),
370 length = std::min(count, block_ - offset);
371 const auto slot =
static_cast<std::size_t
>(leaf % 3);
372 auto *data = native_.get() + slot * 2 * block_;
373 if (nativeLeaf_[slot] != leaf)
375 std::fill_n(data, 2 * block_, 0.);
376 const auto origin = leaf * block_;
377 const auto begin = std::max(origin, pad_),
378 end = std::min(origin + block_, pad_ + frames_);
381 const int at =
static_cast<int>(begin - origin),
382 n =
static_cast<int>(end - begin);
383 for (
int done = 0; done < n;)
385 const int chunk = std::min(n - done, 4096);
386 source_(begin - pad_ + done, chunk, data + at + done);
389 for (
int i = at; i < at + n; ++i)
391 if (!std::isfinite(data[i]))
393 const double shaped = clipperShape<C>(data[i], ceiling_);
398 nativeLeaf_[slot] = leaf;
400 std::copy_n(data + (difference ? block_ : 0) + offset, length, out);
406 double *upsampled(std::int64_t leaf)
408 const auto slot =
static_cast<std::size_t
>(leaf % 3);
409 auto *data = up_.get() + slot * 2 * highBlock_;
410 if (upLeaf_[slot] == leaf)
412 for (
int p = 0; p < factor_; ++p)
414 phases_[p]->evaluate(*rawMap_,
static_cast<std::size_t
>(leaf), raw_, phaseWork_.get());
415 for (
int i = 0; i < block_; ++i)
416 data[i * factor_ + p] = phaseWork_[i];
417 phases_[p]->evaluate(*differenceMap_,
static_cast<std::size_t
>(leaf), difference_,
419 for (
int i = 0; i < block_; ++i)
420 data[highBlock_ + i * factor_ + p] = phaseWork_[i];
422 upLeaf_[slot] = leaf;
425 void highInput(std::int64_t first,
int count,
double *a,
double *b)
427 const auto highExtent = extent_ * factor_;
432 const int n =
static_cast<int>(std::min<std::int64_t>(-first, count));
433 std::fill_n(a, n, 0.);
434 std::fill_n(b, n, 0.);
441 if (first >= highExtent)
443 std::fill_n(a, count, 0.);
444 std::fill_n(b, count, 0.);
447 const auto leaf = first / highBlock_;
448 const int offset =
static_cast<int>(first % highBlock_);
449 const int n =
static_cast<int>(
450 std::min<std::int64_t>(std::min(highBlock_ - offset, count), highExtent - first));
451 const auto *data = upsampled(leaf);
452 std::copy_n(data + offset, n, a);
453 std::copy_n(data + highBlock_ + offset, n, b);
460 const double *stage(std::int64_t leaf)
462 const auto slot =
static_cast<std::size_t
>(leaf % 3);
463 auto *data = shaped_.get() + slot * 2 * highBlock_;
464 if (shapedLeaf_[slot] == leaf)
466 const auto first = leaf * highBlock_;
467 const int length = highBlock_ + filterSize_ - 1;
468 highInput(first - left_, length, scratchA_.get(), scratchB_.get());
469 stage_.
reset(ceiling_);
470 stage_.
process(scratchA_.get(), scratchB_.get(), length, exterior_);
471 std::copy_n(scratchA_.get() + filterSize_ - 1, highBlock_, data);
472 std::copy_n(scratchB_.get() + filterSize_ - 1, highBlock_, data + highBlock_);
473 shapedLeaf_[slot] = leaf;
477 void projectedSource(std::int64_t first,
int count,
double *out)
481 const auto leaf = first / highBlock_;
482 const int offset =
static_cast<int>(first % highBlock_);
483 const int n = std::min(highBlock_ - offset, count);
484 const auto *data = stage(leaf);
485 for (
int i = 0; i < n; ++i)
486 out[i] = data[offset + i] +
496 int factor,
double ceiling,
Exterior exterior,
int block = 512)
497 : job_(job), source_(std::move(reader)), frames_(frames), pad_(pad),
498 extent_(checkedExtent(frames, pad, factor, block)), factor_(factor), block_(block),
499 highBlock_(block * factor), ceiling_(ceiling), exterior_(exterior),
503 if (!(ceiling > 0) || !std::isfinite(ceiling))
505 native_ = job.
allocateScratch<
double>(6 *
static_cast<std::uint64_t
>(block_));
506 rawMap_.emplace(work_, raw_);
507 differenceMap_.emplace(work_, difference_);
508 for (
int p = 0; p < factor; ++p)
509 phases_[p].emplace(work_,
double(p) / factor);
511 delay_ = stage_.
delay();
512 left_ = filterSize_ - 1 - delay_;
513 up_ = job.
allocateScratch<
double>(6 *
static_cast<std::uint64_t
>(highBlock_));
514 shaped_ = job.
allocateScratch<
double>(6 *
static_cast<std::uint64_t
>(highBlock_));
515 scratchA_ = job.
allocateScratch<
double>(
static_cast<std::uint64_t
>(highBlock_) + filterSize_ - 1);
516 scratchB_ = job.
allocateScratch<
double>(
static_cast<std::uint64_t
>(highBlock_) + filterSize_ - 1);
522 [[nodiscard]]
bool hasGeometry(std::int64_t pad,
int block)
const noexcept
524 return pad_ == pad && block_ == block;
528 if (!(ceiling > 0) || !std::isfinite(ceiling))
530 if (ceiling == ceiling_ && exterior == exterior_)
533 exterior_ = exterior;
534 nativeLeaf_.fill(-1);
536 shapedLeaf_.fill(-1);
537 differenceMap_->rebuild(work_, difference_);
539 projection_->rebuild();
550 if (!projection_ && factor_ > 1)
551 projection_.emplace(job_, extent_ * factor_, factor_, ProjectedReader{
this},
556 return pad_ / block_;
560 return (pad_ + frames_ - 1) / block_ + 1;
562 [[nodiscard]] std::int64_t
leafOrigin(std::int64_t leaf)
const
564 return std::max(leaf * block_, pad_) - pad_;
566 [[nodiscard]] std::span<const double>
get(std::int64_t leaf)
571 const auto first = leaf * block_;
572 const auto values = projection_ ? projection_->get(
static_cast<std::size_t
>(leaf))
573 : std::span<const double>{};
574 const auto available =
575 projection_ ?
static_cast<std::int64_t
>(values.size())
576 : std::min<std::int64_t>(block_, extent_ - first);
577 const auto begin = std::max(first, pad_), end = std::min(first + available, pad_ + frames_);
578 const auto *filtered = stage(leaf), *raw = upsampled(leaf);
579 const int offset =
static_cast<int>(begin - first), length =
static_cast<int>(end - begin);
580 for (
int i = 0; i < length; ++i)
582 const int at = offset + i;
583 const double projected =
584 projection_ ? values[at]
585 : filtered[at] + (exterior_ ==
Exterior::Matched ? filtered[highBlock_ + at] : 0.);
586 output_[i] = clipperShape<C>(raw[at * factor_], ceiling_) + projected +
588 if (!std::isfinite(output_[i]))
591 return {output_.get(),
static_cast<std::size_t
>(length)};
593 template <
class Consumer>
void render(Consumer consume)
598 const auto data =
get(leaf);
599 consume(
leafOrigin(leaf),
static_cast<int>(data.size()), data.data());