1 /* 2 * Copyright (c) 2023, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 3-Clause Clear License 5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear 6 * License was not distributed with this source code in the LICENSE file, you 7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the 8 * Alliance for Open Media Patent License 1.0 was not distributed with this 9 * source code in the PATENTS file, you can obtain it at 10 * www.aomedia.org/license/patent. 11 */ 12 13 #ifndef CLI_LEB_GENERATOR_H_ 14 #define CLI_LEB_GENERATOR_H_ 15 16 #include <cstdint> 17 #include <memory> 18 #include <vector> 19 20 #include "absl/status/status.h" 21 #include "iamf/obu/types.h" 22 23 namespace iamf_tools { 24 25 class LebGenerator { 26 public: 27 enum class GenerationMode { 28 kMinimum, 29 kFixedSize, 30 }; 31 32 /*!\brief Factory function to create a `LebGenerator`. 33 * 34 * \param generation_mode Generation mode. 35 * \param fixed_size Fixed size. When using `kGenerateLebFixedSize` mode it 36 * MUST be in the range [1, 8]. When using other modes it is ignored. 37 * \return Unique pointer to `LebGenerator` on success. `nullptr` if the mode 38 * is unknown or `fixed_size` is invalid. 39 */ 40 static std::unique_ptr<LebGenerator> Create( 41 GenerationMode generation_mode = GenerationMode::kMinimum, 42 int8_t fixed_size = 0); 43 44 friend bool operator==(const LebGenerator& lhs, 45 const LebGenerator& rhs) = default; 46 47 /*!\brief Encodes a `DecodedUleb128` to a vector representing a ULEB128. 48 * 49 * The behavior of the generator is controlled by `generation_mode_`. When 50 * configured using `GenerationMode::kMinimum` values are generated using the 51 * representation with the minimum number of bytes. When configured using 52 * `GenerationMode::kFixedSize` values are generated using `fixed_size_` bytes 53 * and generation may fail if this is not sufficient to encode the value. 54 * 55 * \param input Input value. 56 * \param buffer Buffer to serialize to. 57 * \return `absl::OkStatus()` on success. `absl::InvalidArgumentError()` if 58 * the generation fails. 59 */ 60 absl::Status Uleb128ToUint8Vector(DecodedUleb128 input, 61 std::vector<uint8_t>& buffer) const; 62 63 /*!\brief Encodes a `DecodedSleb128` to a vector representing a SLEB128. 64 * 65 * The behavior of the generator is controlled by `generation_mode_`. When 66 * configured using `GenerationMode::kMinimum` values are generated using the 67 * representation with the minimum number of bytes. When configured using 68 * `GenerationMode::kFixedSize` values are generated using `fixed_size_` bytes 69 * and generation may fail if this is not sufficient to encode the value. 70 * 71 * \param input Input value. 72 * \param buffer Buffer to serialize to. 73 * \return `absl::OkStatus()` on success. `absl::InvalidArgumentError()` if 74 * the generation fails. 75 */ 76 absl::Status Sleb128ToUint8Vector(DecodedSleb128 input, 77 std::vector<uint8_t>& buffer) const; 78 79 private: 80 /*!\brief Constructor. 81 * 82 * \param generation_mode Generation mode. 83 * \param fixed_size Fixed size. When using `kGenerateLebFixedSize` mode it 84 * MUST be in the range [1, 8]. When using other modes it is ignored. 85 */ 86 LebGenerator(GenerationMode generation_mode, int8_t fixed_size); 87 88 const GenerationMode generation_mode_; 89 const int8_t fixed_size_; 90 }; 91 92 } // namespace iamf_tools 93 94 #endif // CLI_LEB_GENERATOR_H_ 95