1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
|
/*
* Copyright (c) 2018 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 "logging/rtc_event_log/encoder/delta_encoding.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "logging/rtc_event_log/encoder/bit_writer.h"
#include "logging/rtc_event_log/encoder/var_int.h"
#include "rtc_base/bitstream_reader.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
namespace {
// TODO(eladalon): Only build the decoder in tools and unit tests.
bool g_force_unsigned_for_testing = false;
bool g_force_signed_for_testing = false;
size_t BitsToBytes(size_t bits) {
return (bits / 8) + (bits % 8 > 0 ? 1 : 0);
}
// TODO(eladalon): Replace by something more efficient.
uint64_t UnsignedBitWidth(uint64_t input, bool zero_val_as_zero_width = false) {
if (zero_val_as_zero_width && input == 0) {
return 0;
}
uint64_t width = 0;
do { // input == 0 -> width == 1
width += 1;
input >>= 1;
} while (input != 0);
return width;
}
uint64_t SignedBitWidth(uint64_t max_pos_magnitude,
uint64_t max_neg_magnitude) {
const uint64_t bitwidth_pos = UnsignedBitWidth(max_pos_magnitude, true);
const uint64_t bitwidth_neg =
(max_neg_magnitude > 0) ? UnsignedBitWidth(max_neg_magnitude - 1, true)
: 0;
return 1 + std::max(bitwidth_pos, bitwidth_neg);
}
// Return the maximum integer of a given bit width.
// Examples:
// MaxUnsignedValueOfBitWidth(1) = 0x01
// MaxUnsignedValueOfBitWidth(6) = 0x3f
// MaxUnsignedValueOfBitWidth(8) = 0xff
// MaxUnsignedValueOfBitWidth(32) = 0xffffffff
uint64_t MaxUnsignedValueOfBitWidth(uint64_t bit_width) {
RTC_DCHECK_GE(bit_width, 1);
RTC_DCHECK_LE(bit_width, 64);
return (bit_width == 64) ? std::numeric_limits<uint64_t>::max()
: ((static_cast<uint64_t>(1) << bit_width) - 1);
}
// Computes the delta between `previous` and `current`, under the assumption
// that wrap-around occurs after `width` is exceeded.
uint64_t UnsignedDelta(uint64_t previous, uint64_t current, uint64_t bit_mask) {
return (current - previous) & bit_mask;
}
// Determines the encoding type (e.g. fixed-size encoding).
// Given an encoding type, may also distinguish between some variants of it
// (e.g. which fields of the fixed-size encoding are explicitly mentioned by
// the header, and which are implicitly assumed to hold certain default values).
enum class EncodingType {
kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt = 0,
kFixedSizeSignedDeltasEarlyWrapAndOptSupported = 1,
kReserved1 = 2,
kReserved2 = 3,
kNumberOfEncodingTypes // Keep last
};
// The width of each field in the encoding header. Note that this is the
// width in case the field exists; not all fields occur in all encoding types.
constexpr size_t kBitsInHeaderForEncodingType = 2;
constexpr size_t kBitsInHeaderForDeltaWidthBits = 6;
constexpr size_t kBitsInHeaderForSignedDeltas = 1;
constexpr size_t kBitsInHeaderForValuesOptional = 1;
constexpr size_t kBitsInHeaderForValueWidthBits = 6;
static_assert(static_cast<size_t>(EncodingType::kNumberOfEncodingTypes) <=
1 << kBitsInHeaderForEncodingType,
"Not all encoding types fit.");
// Default values for when the encoding header does not specify explicitly.
constexpr bool kDefaultSignedDeltas = false;
constexpr bool kDefaultValuesOptional = false;
constexpr uint64_t kDefaultValueWidthBits = 64;
// Parameters for fixed-size delta-encoding/decoding.
// These are tailored for the sequence which will be encoded (e.g. widths).
class FixedLengthEncodingParameters final {
public:
static bool ValidParameters(uint64_t delta_width_bits,
bool /* signed_deltas */,
uint64_t value_width_bits) {
return (1 <= delta_width_bits && delta_width_bits <= 64 &&
1 <= value_width_bits && value_width_bits <= 64 &&
delta_width_bits <= value_width_bits);
}
FixedLengthEncodingParameters(uint64_t delta_width_bits,
bool signed_deltas,
bool values_optional,
uint64_t value_width_bits)
: delta_width_bits_(delta_width_bits),
signed_deltas_(signed_deltas),
values_optional_(values_optional),
value_width_bits_(value_width_bits),
delta_mask_(MaxUnsignedValueOfBitWidth(delta_width_bits_)),
value_mask_(MaxUnsignedValueOfBitWidth(value_width_bits_)) {
RTC_DCHECK(
ValidParameters(delta_width_bits, signed_deltas, value_width_bits));
}
// Number of bits necessary to hold the widest(*) of the deltas between the
// values in the sequence.
// (*) - Widest might not be the largest, if signed deltas are used.
uint64_t delta_width_bits() const { return delta_width_bits_; }
// Whether deltas are signed.
bool signed_deltas() const { return signed_deltas_; }
// Whether the values of the sequence are optional. That is, it may be
// that some of them do not have a value (not even a sentinel value indicating
// invalidity).
bool values_optional() const { return values_optional_; }
// Number of bits necessary to hold the largest value in the sequence.
uint64_t value_width_bits() const { return value_width_bits_; }
// Masks where only the bits relevant to the deltas/values are turned on.
uint64_t delta_mask() const { return delta_mask_; }
uint64_t value_mask() const { return value_mask_; }
void SetSignedDeltas(bool signed_deltas) { signed_deltas_ = signed_deltas; }
void SetDeltaWidthBits(uint64_t delta_width_bits) {
delta_width_bits_ = delta_width_bits;
delta_mask_ = MaxUnsignedValueOfBitWidth(delta_width_bits);
}
private:
uint64_t delta_width_bits_; // Normally const, but mutable in tests.
bool signed_deltas_; // Normally const, but mutable in tests.
const bool values_optional_;
const uint64_t value_width_bits_;
uint64_t delta_mask_; // Normally const, but mutable in tests.
const uint64_t value_mask_;
};
// Performs delta-encoding of a single (non-empty) sequence of values, using
// an encoding where all deltas are encoded using the same number of bits.
// (With the exception of optional elements; those are encoded as a bit vector
// with one bit per element, plus a fixed number of bits for every element that
// has a value.)
class FixedLengthDeltaEncoder final {
public:
// See webrtc::EncodeDeltas() for general details.
// This function return a bit pattern that would allow the decoder to
// determine whether it was produced by FixedLengthDeltaEncoder, and can
// therefore be decoded by FixedLengthDeltaDecoder, or whether it was produced
// by a different encoder.
static std::string EncodeDeltas(
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values);
FixedLengthDeltaEncoder(const FixedLengthDeltaEncoder&) = delete;
FixedLengthDeltaEncoder& operator=(const FixedLengthDeltaEncoder&) = delete;
private:
// Calculate min/max values of unsigned/signed deltas, given the bit width
// of all the values in the series.
static void CalculateMinAndMaxDeltas(
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values,
uint64_t bit_width,
uint64_t* max_unsigned_delta,
uint64_t* max_pos_signed_delta,
uint64_t* min_neg_signed_delta);
// No effect outside of unit tests.
// In unit tests, may lead to forcing signed/unsigned deltas, etc.
static void ConsiderTestOverrides(FixedLengthEncodingParameters* params,
uint64_t delta_width_bits_signed,
uint64_t delta_width_bits_unsigned);
// FixedLengthDeltaEncoder objects are to be created by EncodeDeltas() and
// released by it before it returns. They're mostly a convenient way to
// avoid having to pass a lot of state between different functions.
// Therefore, it was deemed acceptable to let them have a reference to
// `values`, whose lifetime must exceed the lifetime of `this`.
FixedLengthDeltaEncoder(const FixedLengthEncodingParameters& params,
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values,
size_t existent_values_count);
// Perform delta-encoding using the parameters given to the ctor on the
// sequence of values given to the ctor.
std::string Encode();
// Exact lengths.
size_t OutputLengthBytes(size_t existent_values_count) const;
size_t HeaderLengthBits() const;
size_t EncodedDeltasLengthBits(size_t existent_values_count) const;
// Encode the compression parameters into the stream.
void EncodeHeader();
// Encode a given delta into the stream.
void EncodeDelta(uint64_t previous, uint64_t current);
void EncodeUnsignedDelta(uint64_t previous, uint64_t current);
void EncodeSignedDelta(uint64_t previous, uint64_t current);
// The parameters according to which encoding will be done (width of
// fields, whether signed deltas should be used, etc.)
const FixedLengthEncodingParameters params_;
// The encoding scheme assumes that at least one value is transmitted OOB,
// so that the first value can be encoded as a delta from that OOB value,
// which is `base_`.
const std::optional<uint64_t> base_;
// The values to be encoded.
// Note: This is a non-owning reference. See comment above ctor for details.
const std::vector<std::optional<uint64_t>>& values_;
// Buffer into which encoded values will be written.
// This is created dynmically as a way to enforce that the rest of the
// ctor has finished running when this is constructed, so that the lower
// bound on the buffer size would be guaranteed correct.
std::unique_ptr<BitWriter> writer_;
};
// TODO(eladalon): Reduce the number of passes.
std::string FixedLengthDeltaEncoder::EncodeDeltas(
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values) {
RTC_DCHECK(!values.empty());
// As a special case, if all of the elements are identical to the base,
// (including, for optional fields, about their existence/non-existence),
// the empty string is used to signal that.
if (std::all_of(
values.cbegin(), values.cend(),
[base](std::optional<uint64_t> val) { return val == base; })) {
return std::string();
}
bool non_decreasing = true;
uint64_t max_value_including_base = base.value_or(0u);
size_t existent_values_count = 0;
{
uint64_t previous = base.value_or(0u);
for (size_t i = 0; i < values.size(); ++i) {
if (!values[i].has_value()) {
continue;
}
++existent_values_count;
non_decreasing &= (previous <= values[i].value());
max_value_including_base =
std::max(max_value_including_base, values[i].value());
previous = values[i].value();
}
}
// If the sequence is non-decreasing, it may be assumed to have width = 64;
// there's no reason to encode the actual max width in the encoding header.
const uint64_t value_width_bits =
non_decreasing ? 64 : UnsignedBitWidth(max_value_including_base);
uint64_t max_unsigned_delta;
uint64_t max_pos_signed_delta;
uint64_t min_neg_signed_delta;
CalculateMinAndMaxDeltas(base, values, value_width_bits, &max_unsigned_delta,
&max_pos_signed_delta, &min_neg_signed_delta);
const uint64_t delta_width_bits_unsigned =
UnsignedBitWidth(max_unsigned_delta);
const uint64_t delta_width_bits_signed =
SignedBitWidth(max_pos_signed_delta, min_neg_signed_delta);
// Note: Preference for unsigned if the two have the same width (efficiency).
const bool signed_deltas =
delta_width_bits_signed < delta_width_bits_unsigned;
const uint64_t delta_width_bits =
signed_deltas ? delta_width_bits_signed : delta_width_bits_unsigned;
const bool values_optional =
!base.has_value() || (existent_values_count < values.size());
FixedLengthEncodingParameters params(delta_width_bits, signed_deltas,
values_optional, value_width_bits);
// No effect in production.
ConsiderTestOverrides(¶ms, delta_width_bits_signed,
delta_width_bits_unsigned);
FixedLengthDeltaEncoder encoder(params, base, values, existent_values_count);
return encoder.Encode();
}
void FixedLengthDeltaEncoder::CalculateMinAndMaxDeltas(
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values,
uint64_t bit_width,
uint64_t* max_unsigned_delta_out,
uint64_t* max_pos_signed_delta_out,
uint64_t* min_neg_signed_delta_out) {
RTC_DCHECK(!values.empty());
RTC_DCHECK(max_unsigned_delta_out);
RTC_DCHECK(max_pos_signed_delta_out);
RTC_DCHECK(min_neg_signed_delta_out);
const uint64_t bit_mask = MaxUnsignedValueOfBitWidth(bit_width);
uint64_t max_unsigned_delta = 0;
uint64_t max_pos_signed_delta = 0;
uint64_t min_neg_signed_delta = 0;
std::optional<uint64_t> prev = base;
for (size_t i = 0; i < values.size(); ++i) {
if (!values[i].has_value()) {
continue;
}
if (!prev.has_value()) {
// If the base is non-existent, the first existent value is encoded as
// a varint, rather than as a delta.
RTC_DCHECK(!base.has_value());
prev = values[i];
continue;
}
const uint64_t current = values[i].value();
const uint64_t forward_delta = UnsignedDelta(*prev, current, bit_mask);
const uint64_t backward_delta = UnsignedDelta(current, *prev, bit_mask);
max_unsigned_delta = std::max(max_unsigned_delta, forward_delta);
if (forward_delta < backward_delta) {
max_pos_signed_delta = std::max(max_pos_signed_delta, forward_delta);
} else {
min_neg_signed_delta = std::max(min_neg_signed_delta, backward_delta);
}
prev = current;
}
*max_unsigned_delta_out = max_unsigned_delta;
*max_pos_signed_delta_out = max_pos_signed_delta;
*min_neg_signed_delta_out = min_neg_signed_delta;
}
void FixedLengthDeltaEncoder::ConsiderTestOverrides(
FixedLengthEncodingParameters* params,
uint64_t delta_width_bits_signed,
uint64_t delta_width_bits_unsigned) {
if (g_force_unsigned_for_testing) {
params->SetDeltaWidthBits(delta_width_bits_unsigned);
params->SetSignedDeltas(false);
} else if (g_force_signed_for_testing) {
params->SetDeltaWidthBits(delta_width_bits_signed);
params->SetSignedDeltas(true);
} else {
// Unchanged.
}
}
FixedLengthDeltaEncoder::FixedLengthDeltaEncoder(
const FixedLengthEncodingParameters& params,
std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values,
size_t existent_values_count)
: params_(params), base_(base), values_(values) {
RTC_DCHECK(!values_.empty());
writer_ =
std::make_unique<BitWriter>(OutputLengthBytes(existent_values_count));
}
std::string FixedLengthDeltaEncoder::Encode() {
EncodeHeader();
if (params_.values_optional()) {
// Encode which values exist and which don't.
for (std::optional<uint64_t> value : values_) {
writer_->WriteBits(value.has_value() ? 1u : 0u, 1);
}
}
std::optional<uint64_t> previous = base_;
for (std::optional<uint64_t> value : values_) {
if (!value.has_value()) {
RTC_DCHECK(params_.values_optional());
continue;
}
if (!previous.has_value()) {
// If the base is non-existent, the first existent value is encoded as
// a varint, rather than as a delta.
RTC_DCHECK(!base_.has_value());
writer_->WriteBits(EncodeVarInt(value.value()));
} else {
EncodeDelta(previous.value(), value.value());
}
previous = value;
}
return writer_->GetString();
}
size_t FixedLengthDeltaEncoder::OutputLengthBytes(
size_t existent_values_count) const {
return BitsToBytes(HeaderLengthBits() +
EncodedDeltasLengthBits(existent_values_count));
}
size_t FixedLengthDeltaEncoder::HeaderLengthBits() const {
if (params_.signed_deltas() == kDefaultSignedDeltas &&
params_.values_optional() == kDefaultValuesOptional &&
params_.value_width_bits() == kDefaultValueWidthBits) {
return kBitsInHeaderForEncodingType + kBitsInHeaderForDeltaWidthBits;
} else {
return kBitsInHeaderForEncodingType + kBitsInHeaderForDeltaWidthBits +
kBitsInHeaderForSignedDeltas + kBitsInHeaderForValuesOptional +
kBitsInHeaderForValueWidthBits;
}
}
size_t FixedLengthDeltaEncoder::EncodedDeltasLengthBits(
size_t existent_values_count) const {
if (!params_.values_optional()) {
return values_.size() * params_.delta_width_bits();
} else {
RTC_DCHECK_EQ(std::count_if(values_.begin(), values_.end(),
[](std::optional<uint64_t> val) {
return val.has_value();
}),
existent_values_count);
// One bit for each delta, to indicate if the value exists, and delta_width
// for each existent value, to indicate the delta itself.
// If base_ is non-existent, the first value (if any) is encoded as a varint
// rather than as a delta.
const size_t existence_bitmap_size_bits = 1 * values_.size();
const bool first_value_is_varint =
!base_.has_value() && existent_values_count >= 1;
const size_t first_value_varint_size_bits = 8 * kMaxVarIntLengthBytes;
const size_t deltas_count = existent_values_count - first_value_is_varint;
const size_t deltas_size_bits = deltas_count * params_.delta_width_bits();
return existence_bitmap_size_bits + first_value_varint_size_bits +
deltas_size_bits;
}
}
void FixedLengthDeltaEncoder::EncodeHeader() {
RTC_DCHECK(writer_);
const EncodingType encoding_type =
(params_.value_width_bits() == kDefaultValueWidthBits &&
params_.signed_deltas() == kDefaultSignedDeltas &&
params_.values_optional() == kDefaultValuesOptional)
? EncodingType::kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt
: EncodingType::kFixedSizeSignedDeltasEarlyWrapAndOptSupported;
writer_->WriteBits(static_cast<uint64_t>(encoding_type),
kBitsInHeaderForEncodingType);
// Note: Since it's meaningless for a field to be of width 0, when it comes
// to fields that relate widths, we encode width 1 as 0, width 2 as 1,
writer_->WriteBits(params_.delta_width_bits() - 1,
kBitsInHeaderForDeltaWidthBits);
if (encoding_type == EncodingType::kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt) {
return;
}
writer_->WriteBits(static_cast<uint64_t>(params_.signed_deltas()),
kBitsInHeaderForSignedDeltas);
writer_->WriteBits(static_cast<uint64_t>(params_.values_optional()),
kBitsInHeaderForValuesOptional);
writer_->WriteBits(params_.value_width_bits() - 1,
kBitsInHeaderForValueWidthBits);
}
void FixedLengthDeltaEncoder::EncodeDelta(uint64_t previous, uint64_t current) {
if (params_.signed_deltas()) {
EncodeSignedDelta(previous, current);
} else {
EncodeUnsignedDelta(previous, current);
}
}
void FixedLengthDeltaEncoder::EncodeUnsignedDelta(uint64_t previous,
uint64_t current) {
RTC_DCHECK(writer_);
const uint64_t delta = UnsignedDelta(previous, current, params_.value_mask());
writer_->WriteBits(delta, params_.delta_width_bits());
}
void FixedLengthDeltaEncoder::EncodeSignedDelta(uint64_t previous,
uint64_t current) {
RTC_DCHECK(writer_);
const uint64_t forward_delta =
UnsignedDelta(previous, current, params_.value_mask());
const uint64_t backward_delta =
UnsignedDelta(current, previous, params_.value_mask());
uint64_t delta;
if (forward_delta <= backward_delta) {
delta = forward_delta;
} else {
// Compute the unsigned representation of a negative delta.
// This is the two's complement representation of this negative value,
// when deltas are of width params_.delta_mask().
RTC_DCHECK_GE(params_.delta_mask(), backward_delta);
RTC_DCHECK_LT(params_.delta_mask() - backward_delta, params_.delta_mask());
delta = params_.delta_mask() - backward_delta + 1;
RTC_DCHECK_LE(delta, params_.delta_mask());
}
writer_->WriteBits(delta, params_.delta_width_bits());
}
// Perform decoding of a a delta-encoded stream, extracting the original
// sequence of values.
class FixedLengthDeltaDecoder final {
public:
// Checks whether FixedLengthDeltaDecoder is a suitable decoder for this
// bitstream. Note that this does NOT imply that stream is valid, and will
// be decoded successfully. It DOES imply that all other decoder classes
// will fail to decode this input, though.
static bool IsSuitableDecoderFor(absl::string_view input);
// Assuming that `input` is the result of fixed-size delta-encoding
// that took place with the same value to `base` and over `num_of_deltas`
// original values, this will return the sequence of original values.
// If an error occurs (can happen if `input` is corrupt), an empty
// vector will be returned.
static std::vector<std::optional<uint64_t>> DecodeDeltas(
absl::string_view input,
std::optional<uint64_t> base,
size_t num_of_deltas);
FixedLengthDeltaDecoder(const FixedLengthDeltaDecoder&) = delete;
FixedLengthDeltaDecoder& operator=(const FixedLengthDeltaDecoder&) = delete;
private:
// Reads the encoding header in `input` and returns a FixedLengthDeltaDecoder
// with the corresponding configuration, that can be used to decode the
// values in `input`.
// If the encoding header is corrupt (contains an illegal configuration),
// nullptr will be returned.
// When a valid FixedLengthDeltaDecoder is returned, this does not mean that
// the entire stream is free of error. Rather, only the encoding header is
// examined and guaranteed.
static std::unique_ptr<FixedLengthDeltaDecoder> Create(
absl::string_view input,
std::optional<uint64_t> base,
size_t num_of_deltas);
// FixedLengthDeltaDecoder objects are to be created by DecodeDeltas() and
// released by it before it returns. They're mostly a convenient way to
// avoid having to pass a lot of state between different functions.
// Therefore, it was deemed acceptable that `reader` does not own the buffer
// it reads, meaning the lifetime of `this` must not exceed the lifetime
// of `reader`'s underlying buffer.
FixedLengthDeltaDecoder(BitstreamReader reader,
const FixedLengthEncodingParameters& params,
std::optional<uint64_t> base,
size_t num_of_deltas);
// Perform the decoding using the parameters given to the ctor.
std::vector<std::optional<uint64_t>> Decode();
// Add `delta` to `base` to produce the next value in a sequence.
// The delta is applied as signed/unsigned depending on the parameters
// given to the ctor. Wrap-around is taken into account according to the
// values' width, as specified by the aforementioned encoding parameters.
uint64_t ApplyDelta(uint64_t base, uint64_t delta) const;
// Helpers for ApplyDelta().
uint64_t ApplyUnsignedDelta(uint64_t base, uint64_t delta) const;
uint64_t ApplySignedDelta(uint64_t base, uint64_t delta) const;
// Reader of the input stream to be decoded. Does not own that buffer.
// See comment above ctor for details.
BitstreamReader reader_;
// The parameters according to which encoding will be done (width of
// fields, whether signed deltas should be used, etc.)
const FixedLengthEncodingParameters params_;
// The encoding scheme assumes that at least one value is transmitted OOB,
// so that the first value can be encoded as a delta from that OOB value,
// which is `base_`.
const std::optional<uint64_t> base_;
// The number of values to be known to be decoded.
const size_t num_of_deltas_;
};
bool FixedLengthDeltaDecoder::IsSuitableDecoderFor(absl::string_view input) {
BitstreamReader reader(input);
uint64_t encoding_type_bits = reader.ReadBits(kBitsInHeaderForEncodingType);
if (!reader.Ok()) {
return false;
}
const auto encoding_type = static_cast<EncodingType>(encoding_type_bits);
return encoding_type ==
EncodingType::kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt ||
encoding_type ==
EncodingType::kFixedSizeSignedDeltasEarlyWrapAndOptSupported;
}
std::vector<std::optional<uint64_t>> FixedLengthDeltaDecoder::DecodeDeltas(
absl::string_view input,
std::optional<uint64_t> base,
size_t num_of_deltas) {
auto decoder = FixedLengthDeltaDecoder::Create(input, base, num_of_deltas);
if (!decoder) {
return std::vector<std::optional<uint64_t>>();
}
return decoder->Decode();
}
std::unique_ptr<FixedLengthDeltaDecoder> FixedLengthDeltaDecoder::Create(
absl::string_view input,
std::optional<uint64_t> base,
size_t num_of_deltas) {
BitstreamReader reader(input);
// Encoding type
uint32_t encoding_type_bits = reader.ReadBits(kBitsInHeaderForEncodingType);
if (!reader.Ok()) {
return nullptr;
}
const EncodingType encoding = static_cast<EncodingType>(encoding_type_bits);
if (encoding != EncodingType::kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt &&
encoding !=
EncodingType::kFixedSizeSignedDeltasEarlyWrapAndOptSupported) {
RTC_LOG(LS_WARNING) << "Unrecognized encoding type.";
return nullptr;
}
// See encoding for +1's rationale.
const uint64_t delta_width_bits =
reader.ReadBits(kBitsInHeaderForDeltaWidthBits) + 1;
RTC_DCHECK_LE(delta_width_bits, 64);
// signed_deltas, values_optional, value_width_bits
bool signed_deltas;
bool values_optional;
uint64_t value_width_bits;
if (encoding == EncodingType::kFixedSizeUnsignedDeltasNoEarlyWrapNoOpt) {
signed_deltas = kDefaultSignedDeltas;
values_optional = kDefaultValuesOptional;
value_width_bits = kDefaultValueWidthBits;
} else {
signed_deltas = reader.Read<bool>();
values_optional = reader.Read<bool>();
// See encoding for +1's rationale.
value_width_bits = reader.ReadBits(kBitsInHeaderForValueWidthBits) + 1;
RTC_DCHECK_LE(value_width_bits, 64);
}
if (!reader.Ok()) {
return nullptr;
}
// Note: Because of the way the parameters are read, it is not possible
// for illegal values to be read. We check nevertheless, in case the code
// changes in the future in a way that breaks this promise.
if (!FixedLengthEncodingParameters::ValidParameters(
delta_width_bits, signed_deltas, value_width_bits)) {
RTC_LOG(LS_WARNING) << "Corrupt log; illegal encoding parameters.";
return nullptr;
}
FixedLengthEncodingParameters params(delta_width_bits, signed_deltas,
values_optional, value_width_bits);
return absl::WrapUnique(
new FixedLengthDeltaDecoder(reader, params, base, num_of_deltas));
}
FixedLengthDeltaDecoder::FixedLengthDeltaDecoder(
BitstreamReader reader,
const FixedLengthEncodingParameters& params,
std::optional<uint64_t> base,
size_t num_of_deltas)
: reader_(reader),
params_(params),
base_(base),
num_of_deltas_(num_of_deltas) {
RTC_DCHECK(reader_.Ok());
}
std::vector<std::optional<uint64_t>> FixedLengthDeltaDecoder::Decode() {
RTC_DCHECK(reader_.Ok());
std::vector<bool> existing_values(num_of_deltas_);
if (params_.values_optional()) {
for (size_t i = 0; i < num_of_deltas_; ++i) {
existing_values[i] = reader_.Read<bool>();
}
} else {
std::fill(existing_values.begin(), existing_values.end(), true);
}
std::optional<uint64_t> previous = base_;
std::vector<std::optional<uint64_t>> values(num_of_deltas_);
for (size_t i = 0; i < num_of_deltas_; ++i) {
if (!existing_values[i]) {
RTC_DCHECK(params_.values_optional());
continue;
}
if (!previous) {
// If the base is non-existent, the first existent value is encoded as
// a varint, rather than as a delta.
RTC_DCHECK(!base_.has_value());
values[i] = DecodeVarInt(reader_);
} else {
uint64_t delta = reader_.ReadBits(params_.delta_width_bits());
values[i] = ApplyDelta(*previous, delta);
}
previous = values[i];
}
if (!reader_.Ok()) {
values = {};
}
return values;
}
uint64_t FixedLengthDeltaDecoder::ApplyDelta(uint64_t base,
uint64_t delta) const {
RTC_DCHECK_LE(base, MaxUnsignedValueOfBitWidth(params_.value_width_bits()));
RTC_DCHECK_LE(delta, MaxUnsignedValueOfBitWidth(params_.delta_width_bits()));
return params_.signed_deltas() ? ApplySignedDelta(base, delta)
: ApplyUnsignedDelta(base, delta);
}
uint64_t FixedLengthDeltaDecoder::ApplyUnsignedDelta(uint64_t base,
uint64_t delta) const {
// Note: May still be used if signed deltas used.
RTC_DCHECK_LE(base, MaxUnsignedValueOfBitWidth(params_.value_width_bits()));
RTC_DCHECK_LE(delta, MaxUnsignedValueOfBitWidth(params_.delta_width_bits()));
return (base + delta) & params_.value_mask();
}
uint64_t FixedLengthDeltaDecoder::ApplySignedDelta(uint64_t base,
uint64_t delta) const {
RTC_DCHECK(params_.signed_deltas());
RTC_DCHECK_LE(base, MaxUnsignedValueOfBitWidth(params_.value_width_bits()));
RTC_DCHECK_LE(delta, MaxUnsignedValueOfBitWidth(params_.delta_width_bits()));
const uint64_t top_bit = static_cast<uint64_t>(1)
<< (params_.delta_width_bits() - 1);
const bool positive_delta = ((delta & top_bit) == 0);
if (positive_delta) {
return ApplyUnsignedDelta(base, delta);
}
const uint64_t delta_abs = (~delta & params_.delta_mask()) + 1;
return (base - delta_abs) & params_.value_mask();
}
} // namespace
std::string EncodeDeltas(std::optional<uint64_t> base,
const std::vector<std::optional<uint64_t>>& values) {
// TODO(eladalon): Support additional encodings.
return FixedLengthDeltaEncoder::EncodeDeltas(base, values);
}
std::vector<std::optional<uint64_t>> DecodeDeltas(absl::string_view input,
std::optional<uint64_t> base,
size_t num_of_deltas) {
RTC_DCHECK_GT(num_of_deltas, 0); // Allows empty vector to indicate error.
// The empty string is a special case indicating that all values were equal
// to the base.
if (input.empty()) {
std::vector<std::optional<uint64_t>> result(num_of_deltas);
std::fill(result.begin(), result.end(), base);
return result;
}
if (FixedLengthDeltaDecoder::IsSuitableDecoderFor(input)) {
return FixedLengthDeltaDecoder::DecodeDeltas(input, base, num_of_deltas);
}
RTC_LOG(LS_WARNING) << "Could not decode delta-encoded stream.";
return std::vector<std::optional<uint64_t>>();
}
void SetFixedLengthEncoderDeltaSignednessForTesting(bool signedness) {
g_force_unsigned_for_testing = !signedness;
g_force_signed_for_testing = signedness;
}
void UnsetFixedLengthEncoderDeltaSignednessForTesting() {
g_force_unsigned_for_testing = false;
g_force_signed_for_testing = false;
}
} // namespace webrtc
|