1 /* 2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ 12 #define LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ 13 14 #include "absl/types/optional.h" 15 #include "api/array_view.h" 16 #include "logging/rtc_event_log/events/rtc_event_field_extraction.h" 17 18 namespace webrtc { 19 20 // Parameters for fixed-size delta-encoding/decoding. 21 // These are tailored for the sequence which will be encoded (e.g. widths). 22 class FixedLengthEncodingParametersV3 final { 23 public: ValidParameters(uint64_t delta_bit_width,bool signed_deltas,bool values_optional,uint64_t value_bit_width)24 static bool ValidParameters(uint64_t delta_bit_width, 25 bool signed_deltas, 26 bool values_optional, 27 uint64_t value_bit_width) { 28 return (1 <= delta_bit_width && delta_bit_width <= 64 && 29 1 <= value_bit_width && value_bit_width <= 64 && 30 (delta_bit_width <= value_bit_width || 31 (signed_deltas && delta_bit_width == 64))); 32 } 33 34 static FixedLengthEncodingParametersV3 CalculateParameters( 35 uint64_t base, 36 rtc::ArrayView<const uint64_t> values, 37 uint64_t value_bit_width, 38 bool values_optional); 39 static absl::optional<FixedLengthEncodingParametersV3> ParseDeltaHeader( 40 uint64_t header, 41 uint64_t value_bit_width); 42 43 uint64_t DeltaHeaderAsInt() const; 44 45 // Number of bits necessary to hold the widest(*) of the deltas between the 46 // values in the sequence. 47 // (*) - Widest might not be the largest, if signed deltas are used. delta_bit_width()48 uint64_t delta_bit_width() const { return delta_bit_width_; } 49 50 // Whether deltas are signed. signed_deltas()51 bool signed_deltas() const { return signed_deltas_; } 52 53 // Whether the values of the sequence are optional. That is, it may be 54 // that some of them do not have a value (not even a sentinel value indicating 55 // invalidity). values_optional()56 bool values_optional() const { return values_optional_; } 57 58 // Whether all values are equal. 64-bit signed deltas are assumed to not 59 // occur, since those could equally well be represented using 64 bit unsigned 60 // deltas. values_equal()61 bool values_equal() const { 62 return delta_bit_width() == 64 && signed_deltas(); 63 } 64 65 // Number of bits necessary to hold the largest value in the sequence. value_bit_width()66 uint64_t value_bit_width() const { return value_bit_width_; } 67 68 // Masks where only the bits relevant to the deltas/values are turned on. delta_mask()69 uint64_t delta_mask() const { return delta_mask_; } value_mask()70 uint64_t value_mask() const { return value_mask_; } 71 72 private: FixedLengthEncodingParametersV3(uint64_t delta_bit_width,bool signed_deltas,bool values_optional,uint64_t value_bit_width)73 FixedLengthEncodingParametersV3(uint64_t delta_bit_width, 74 bool signed_deltas, 75 bool values_optional, 76 uint64_t value_bit_width) 77 : delta_bit_width_(delta_bit_width), 78 signed_deltas_(signed_deltas), 79 values_optional_(values_optional), 80 value_bit_width_(value_bit_width), 81 delta_mask_( 82 webrtc_event_logging::MaxUnsignedValueOfBitWidth(delta_bit_width_)), 83 value_mask_(webrtc_event_logging::MaxUnsignedValueOfBitWidth( 84 value_bit_width_)) {} 85 86 uint64_t delta_bit_width_; 87 bool signed_deltas_; 88 bool values_optional_; 89 uint64_t value_bit_width_; 90 91 uint64_t delta_mask_; 92 uint64_t value_mask_; 93 }; 94 95 } // namespace webrtc 96 #endif // LOGGING_RTC_EVENT_LOG_EVENTS_FIXED_LENGTH_ENCODING_PARAMETERS_V3_H_ 97