libflute
EncodingSymbol.cpp
Go to the documentation of this file.
1 // libflute - FLUTE/ALC library
2 //
3 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
4 //
5 // Licensed under the License terms and conditions for use, reproduction, and
6 // distribution of 5G-MAG software (the “License”). You may not use this file
7 // except in compliance with the License. You may obtain a copy of the License at
8 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
9 // agreed to in writing, software distributed under the License is distributed on
10 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 // or implied.
12 //
13 // See the License for the specific language governing permissions and limitations
14 // under the License.
15 //
16 #include <cstdio>
17 #include <cstring>
18 #include <iostream>
19 #include <cmath>
20 #include <arpa/inet.h>
21 #include "EncodingSymbol.h"
22 #include "spdlog/spdlog.h"
23 
24 auto LibFlute::EncodingSymbol::from_payload(char* encoded_data, size_t data_len, const FecOti& fec_oti, ContentEncoding encoding) -> std::vector<EncodingSymbol>
25 {
26  auto source_block_number = 0;
27  auto encoding_symbol_id = 0;
28  std::vector<EncodingSymbol> symbols;
29 
30  if (encoding != ContentEncoding::NONE && encoding != ContentEncoding::GZIP) {
31  throw "Only unencoded or gzipped content is supported";
32  }
33 
34  if (fec_oti.encoding_id == FecScheme::CompactNoCode) {
35  source_block_number = ntohs(*(uint16_t*)encoded_data);
36  encoded_data += 2;
37  encoding_symbol_id = ntohs(*(uint16_t*)encoded_data);
38  encoded_data += 2;
39  data_len -= 4;
40  } else {
41  throw "Only compact no-code FEC is supported";
42  }
43 
44  int nof_symbols = std::ceil((float)data_len / (float)fec_oti.encoding_symbol_length);
45  for (int i = 0; i < nof_symbols; i++) {
46  if (fec_oti.encoding_id == FecScheme::CompactNoCode) {
47  symbols.emplace_back(encoding_symbol_id, source_block_number, encoded_data, std::min(data_len, (size_t)fec_oti.encoding_symbol_length), fec_oti.encoding_id);
48  }
49  encoded_data += fec_oti.encoding_symbol_length;
50  encoding_symbol_id++;
51  }
52 
53  return symbols;
54 }
55 
56 auto LibFlute::EncodingSymbol::to_payload(const std::vector<EncodingSymbol>& symbols, char* encoded_data, size_t data_len, const FecOti& fec_oti, ContentEncoding encoding) -> size_t
57 {
58  size_t len = 0;
59  auto ptr = encoded_data;
60  auto first_symbol = symbols.begin();
61  if (fec_oti.encoding_id == FecScheme::CompactNoCode && data_len >= 4) {
62  *((uint16_t*)ptr) = htons(first_symbol->source_block_number());
63  ptr += 2;
64  *((uint16_t*)ptr) = htons(first_symbol->id());
65  ptr += 2;
66  len += 4;
67  data_len -= 4;
68  } else {
69  throw "Only compact no-code FEC is supported";
70  }
71 
72  for (const auto& symbol : symbols) {
73  if (symbol.len() <= data_len) {
74  auto symbol_len = symbol.encode_to(ptr, data_len);
75  data_len -= symbol_len;
76  len += symbol_len;
77  ptr += symbol_len;
78  } else {
79  spdlog::warn("Not enough space in payload buffer for encoding symbol");
80  break;
81  }
82  }
83  return len;
84 }
85 
86 auto LibFlute::EncodingSymbol::decode_to(char* buffer, size_t max_length) const -> void {
87  if (_fec_scheme == FecScheme::CompactNoCode) {
88  if (_data_len <= max_length) {
89  memcpy(buffer, _encoded_data, _data_len);
90  }
91  }
92 }
93 
94 auto LibFlute::EncodingSymbol::encode_to(char* buffer, size_t max_length) const -> size_t {
95  if (_fec_scheme == FecScheme::CompactNoCode) {
96  if (_data_len <= max_length) {
97  memcpy(buffer, _encoded_data, _data_len);
98  return _data_len;
99  }
100  }
101  return 0;
102 }
static size_t to_payload(const std::vector< EncodingSymbol > &, char *encoded_data, size_t data_len, const FecOti &fec_oti, ContentEncoding encoding)
Write encoding symbols to a packet payload buffer.
size_t encode_to(char *buffer, size_t max_length) const
Encode to a buffer.
static std::vector< EncodingSymbol > from_payload(char *encoded_data, size_t data_len, const FecOti &fec_oti, ContentEncoding encoding)
Parse and construct all encoding symbols from a payload data buffer.
void decode_to(char *buffer, size_t max_length) const
Decode to a buffer.
ContentEncoding
Content Encodings.
Definition: flute_types.h:34
OTI values struct.
Definition: flute_types.h:51