Fanny Linderborg f045dbd67c Modify sequence index on key frames
For key frames: increase the sequence index until the last 7 bits are
all zeroes. If this results in an overflow, wraparound to 0.

Also:
* Allow setting and getting the sequence index
* Allow getting LayerId

Bug: webrtc:358039777
Change-Id: Ibe16689a3d1eb5706d4fce5c9220770046f26896
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/362540
Reviewed-by: Erik Språng <sprang@webrtc.org>
Auto-Submit: Fanny Linderborg <linderborg@webrtc.org>
Commit-Queue: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43042}
2024-09-18 09:29:47 +00:00

74 lines
2.0 KiB
C++

/*
* Copyright 2024 The WebRTC 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 in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "video/corruption_detection/halton_sequence.h"
#include <algorithm>
#include <vector>
#include "rtc_base/checks.h"
namespace webrtc {
namespace {
static constexpr int kMaxDimensions = 5;
const int kBases[kMaxDimensions] = {2, 3, 5, 7, 11};
double GetVanDerCorputSequenceElement(int sequence_idx, int base) {
if (sequence_idx < 0 || base < 2) {
sequence_idx = 0;
base = 2;
}
double element = 0.0;
double positional_value = 1.0;
int left = sequence_idx;
while (left > 0) {
positional_value /= base;
element += positional_value * (left % base);
left /= base;
}
return element;
}
} // namespace
HaltonSequence::HaltonSequence(int num_dimensions)
: num_dimensions_(num_dimensions), current_idx_(0) {
RTC_CHECK_GE(num_dimensions_, 1)
<< "num_dimensions must be >= 1. Will be set to 1.";
RTC_CHECK_LE(num_dimensions_, kMaxDimensions)
<< "num_dimensions must be <= " << kMaxDimensions << ". Will be set to "
<< kMaxDimensions << ".";
num_dimensions_ = std::clamp(num_dimensions_, 1, kMaxDimensions);
}
std::vector<double> HaltonSequence::GetNext() {
std::vector<double> point = {};
point.reserve(num_dimensions_);
for (int i = 0; i < num_dimensions_; ++i) {
point.push_back(GetVanDerCorputSequenceElement(current_idx_, kBases[i]));
}
++current_idx_;
return point;
}
void HaltonSequence::SetCurrentIndex(int idx) {
if (idx >= 0) {
current_idx_ = idx;
}
RTC_DCHECK_GE(idx, 0) << "Index must be non-negative";
}
void HaltonSequence::Reset() {
HaltonSequence::current_idx_ = 0;
}
} // namespace webrtc