aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2016-07-17 19:26:08 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2016-07-17 19:26:08 +0200
commit01155e32842f0e8835f571e983ab7bfdf5cf21e1 (patch)
tree532184728066f305a3d4342a7c7dada4d61beb1c
parent6b92343222a2d788621c603d25855bd1a0c86252 (diff)
downloadodr-edilib-01155e32842f0e8835f571e983ab7bfdf5cf21e1.tar.gz
odr-edilib-01155e32842f0e8835f571e983ab7bfdf5cf21e1.tar.bz2
odr-edilib-01155e32842f0e8835f571e983ab7bfdf5cf21e1.zip
Improve AFPacket decoder
-rw-r--r--CMakeLists.txt8
-rw-r--r--decoder/EDIDecoder.cpp295
-rw-r--r--decoder/EDIDecoder.hpp29
-rw-r--r--encoder/AFPacket.cpp91
-rw-r--r--encoder/AFPacket.h58
-rw-r--r--encoder/PFT.cpp295
-rw-r--r--encoder/PFT.h108
-rw-r--r--encoder/TagItems.cpp213
-rw-r--r--encoder/TagItems.h156
-rw-r--r--encoder/TagPacket.cpp73
-rw-r--r--encoder/TagPacket.h55
-rw-r--r--test/main.cpp24
12 files changed, 350 insertions, 1055 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 92c6ad3..abf7880 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -40,15 +40,13 @@ include_directories(${FEC_INCLUDE_DIRS})
# Setup apps
########################################################################
-include_directories("./encoder")
+include_directories("./decoder")
include_directories(".")
list(APPEND edilib_sources
- encoder/AFPacket.cpp
- encoder/PFT.cpp
- encoder/TagItems.cpp
- encoder/TagPacket.cpp
ReedSolomon.cpp
+ crc.c
+ decoder/EDIDecoder.cpp
test/main.cpp
)
diff --git a/decoder/EDIDecoder.cpp b/decoder/EDIDecoder.cpp
new file mode 100644
index 0000000..75b420c
--- /dev/null
+++ b/decoder/EDIDecoder.cpp
@@ -0,0 +1,295 @@
+#include "EDIDecoder.hpp"
+#include "crc.h"
+#include <stdio.h>
+#include <cassert>
+
+using namespace std;
+
+#define debug(fmt, args...) fprintf (stdout, fmt "\n" , ## args)
+
+void EDIDecoder::push_byte(uint8_t byte)
+{
+ m_input_data.push_back(byte);
+
+
+ if (m_input_data.size() > 2) {
+ if (m_input_data[0] == 'A' and
+ m_input_data[1] == 'F') {
+ decode_state_t st = decode_afpacket();
+
+ if (st == decode_state_t::INVALID or
+ st == decode_state_t::VALID) {
+ m_input_data.clear();
+ }
+
+ }
+ else if (m_input_data[0] == 'P' and
+ m_input_data[1] == 'F') {
+ debug("Needs PFT!");
+ }
+ else {
+ debug("Unknown!");
+ }
+ }
+}
+
+#define AFPACKET_HEADER_LEN 10 // includes SYNC
+
+template<class T>
+static uint16_t read_16b(T buf)
+{
+ uint16_t value = 0;
+ value = (uint16_t)(buf[0]) << 8;
+ value |= (uint16_t)(buf[1]);
+ return value;
+}
+
+template<class T>
+static uint32_t read_24b(T buf)
+{
+ uint32_t value = 0;
+ value = (uint32_t)(buf[0]) << 16;
+ value |= (uint32_t)(buf[1]) << 8;
+ value |= (uint32_t)(buf[2]);
+ return value;
+}
+
+template<class T>
+static uint32_t read_32b(T buf)
+{
+ uint32_t value = 0;
+ value = (uint32_t)(buf[0]) << 24;
+ value |= (uint32_t)(buf[1]) << 16;
+ value |= (uint32_t)(buf[2]) << 8;
+ value |= (uint32_t)(buf[3]);
+ return value;
+}
+
+decode_state_t EDIDecoder::decode_afpacket()
+{
+ if (m_input_data.size() < AFPACKET_HEADER_LEN) {
+ return decode_state_t::INCOMPLETE;
+ }
+
+ // read length from packet
+ uint32_t taglength = read_32b(m_input_data.begin() + 2);
+ uint16_t seq = read_16b(m_input_data.begin() + 6);
+
+ bool has_crc = (m_input_data[8] & 0x80) ? true : false;
+ uint8_t revision = m_input_data[8] & 0x7F;
+ uint8_t pt = m_input_data[9];
+ if (pt != 'T') {
+ // only support Tag
+ return decode_state_t::INVALID;
+ }
+
+ const size_t crclength = 2;
+ if (m_input_data.size() < AFPACKET_HEADER_LEN + taglength + crclength) {
+ return decode_state_t::INCOMPLETE;
+ }
+
+ debug("AFPacket %d, len %d, rev %d", seq, taglength, revision);
+
+ if (not has_crc) {
+ debug("Not supported, has no CRC");
+ return decode_state_t::INVALID;
+ }
+
+ uint16_t crc = 0xffff;
+ debug("calc crc %d", AFPACKET_HEADER_LEN + taglength);
+ for (size_t i = 0; i < AFPACKET_HEADER_LEN + taglength; i++) {
+ crc = crc16(crc, &m_input_data[i], 1);
+ }
+ crc ^= 0xffff;
+
+ uint16_t packet_crc = read_16b(m_input_data.begin() + AFPACKET_HEADER_LEN + taglength);
+
+ if (packet_crc != crc) {
+ debug(" AFPacket crc wrong %x vs calculated %x",
+ packet_crc, crc);
+ return decode_state_t::INVALID;
+ }
+ else {
+ vector<uint8_t> payload(taglength);
+ copy(m_input_data.begin() + AFPACKET_HEADER_LEN,
+ m_input_data.begin() + AFPACKET_HEADER_LEN + taglength,
+ payload.begin());
+
+ return decode_tagpacket(payload);
+ }
+}
+
+decode_state_t EDIDecoder::decode_tagpacket(const vector<uint8_t> payload)
+{
+ size_t length = 0;
+ auto st = decode_state_t::VALID;
+
+ for (size_t i = 0; i + 8 < payload.size(); i += 8 + length) {
+ char tag_sz[5];
+ tag_sz[4] = '\0';
+ copy(payload.begin() + i, payload.begin() + i + 4, tag_sz);
+
+ string tag(tag_sz);
+
+ uint32_t taglength = read_32b(payload.begin() + i + 4);
+
+ if (taglength % 8 != 0) {
+ st = decode_state_t::INVALID;
+ break;
+ }
+ taglength /= 8;
+
+ length = taglength;
+
+ vector<uint8_t> tag_value(taglength);
+ copy( payload.begin() + i+8,
+ payload.begin() + i+8+taglength,
+ tag_value.begin());
+
+ auto tagstate = decode_state_t::INVALID;
+ if (tag == "*ptr") {
+ tagstate = decode_starptr(tag_value);
+ }
+ else if (tag == "deti") {
+ tagstate = decode_deti(tag_value);
+ }
+ else if (tag.substr(0, 3) == "est") {
+ uint8_t n = tag_sz[3];
+ tagstate = decode_estn(tag_value, n);
+ }
+ else if (tag == "*dmy") {
+ tagstate = decode_stardmy(tag_value);
+ }
+ else {
+ debug("Unknown TAG %s", tag.c_str());
+ break;
+ }
+
+ if (tagstate != decode_state_t::VALID) {
+ debug("Error decoding TAG %s", tag.c_str());
+ st = tagstate;
+ break;
+ }
+ }
+ return st;
+}
+
+decode_state_t EDIDecoder::decode_starptr(const vector<uint8_t> value)
+{
+ if (value.size() != 0x40 / 8) {
+ debug("Incorrect length %02lx for *PTR", value.size());
+ return decode_state_t::INVALID;
+ }
+
+ char protocol_sz[5];
+ protocol_sz[4] = '\0';
+ copy(value.begin(), value.begin() + 4, protocol_sz);
+ string protocol(protocol_sz);
+
+ uint16_t major = read_16b(value.begin() + 4);
+ uint16_t minor = read_16b(value.begin() + 6);
+
+ debug("*PTR '%s' maj=%d min=%d",
+ protocol.c_str(), major, minor);
+
+ return decode_state_t::VALID;
+}
+
+decode_state_t EDIDecoder::decode_deti(const vector<uint8_t> value)
+{
+ /*
+ uint16_t detiHeader = fct | (fcth << 8) | (rfudf << 13) | (ficf << 14) | (atstf << 15);
+ packet.push_back(detiHeader >> 8);
+ packet.push_back(detiHeader & 0xFF);
+ */
+
+ uint16_t detiHeader = read_16b(value.begin());
+
+ bool atstf = (detiHeader >> 15) & 0x1;
+ bool ficf = (detiHeader >> 14) & 0x1;
+ bool rfudf = (detiHeader >> 13) & 0x1;
+ uint8_t fcth = (detiHeader >> 8) & 0x1F;
+ uint8_t fct = detiHeader & 0xFF;
+
+ uint16_t dflc = fcth * 250 + fct; // modulo 5000 counter
+
+ uint32_t etiHeader = read_32b(value.begin() + 2);
+
+ uint8_t stat = (etiHeader >> 24) & 0xFF;
+ uint8_t mid = (etiHeader >> 22) & 0x03;
+ uint8_t fp = (etiHeader >> 19) & 0x07;
+ bool rfa = (etiHeader >> 17) & 0x1;
+ bool rfu = (etiHeader >> 16) & 0x1;
+ uint16_t mnsc = etiHeader & 0xFFFF;
+
+ debug("DETI DFLC=%d STAT=%d MID=%d FP=%d RFA=%d RFU=%d MNSC=%04x",
+ dflc, stat, mid, fp, rfa, rfu, mnsc);
+
+ size_t i = 2 + 4;
+
+ if (atstf) {
+ uint8_t utco = value[i];
+ i++;
+
+ uint32_t seconds = read_32b(value.begin() + i);
+ i += 4;
+
+ uint32_t tsta = read_24b(value.begin() + i);
+ i += 4;
+ debug(" with ATST utco=%d seconds=%d tsta=%d",
+ utco, seconds, tsta);
+ }
+
+ if (ficf) {
+ size_t fic_length = (mid == 3 ? 32 : 24);
+
+ assert(value.size() > i + fic_length);
+ vector<uint8_t> fic(fic_length);
+ copy( value.begin() + i,
+ value.begin() + i + fic_length,
+ fic.begin());
+ i += fic_length;
+
+ debug(" with FIC of length %zu",
+ fic_length);
+ }
+
+ if (rfudf) {
+ uint32_t rfud = read_24b(value.begin() + i);
+
+ i += 3;
+
+ debug(" with RFUD %d",
+ rfud);
+ }
+
+ return decode_state_t::VALID;
+}
+
+decode_state_t EDIDecoder::decode_estn(const vector<uint8_t> value, uint8_t n)
+{
+ uint32_t sstc = read_24b(value.begin());
+
+ uint8_t scid = (sstc >> 18) & 0x3F;
+ uint8_t sad = (sstc >> 8) & 0x3FF;
+ uint8_t tpl = (sstc >> 2) & 0x3F;
+ uint8_t rfa = sstc & 0x3;
+
+ vector<uint8_t> mst_data;
+ copy( value.begin() + 3,
+ value.end(),
+ back_inserter(mst_data));
+
+ debug("EST%d SCID=%d SAD=%d TPL=%d RFA=%d MST length=%zu",
+ n, scid, sad, tpl, rfa, mst_data.size());
+
+ return decode_state_t::VALID;
+}
+
+decode_state_t EDIDecoder::decode_stardmy(const vector<uint8_t> value)
+{
+ debug("*DMY len=%zu", value.size());
+
+ return decode_state_t::VALID;
+}
+
diff --git a/decoder/EDIDecoder.hpp b/decoder/EDIDecoder.hpp
new file mode 100644
index 0000000..71ccc51
--- /dev/null
+++ b/decoder/EDIDecoder.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <stdint.h>
+#include <deque>
+#include <string>
+#include <vector>
+
+enum class decode_state_t {
+ INCOMPLETE,
+ VALID,
+ INVALID
+};
+
+class EDIDecoder {
+ public:
+ EDIDecoder() {}
+
+ void push_byte(uint8_t byte);
+
+ private:
+ decode_state_t decode_afpacket(void);
+ decode_state_t decode_tagpacket(const std::vector<uint8_t> payload);
+ decode_state_t decode_starptr(const std::vector<uint8_t> value);
+ decode_state_t decode_deti(const std::vector<uint8_t> value);
+ decode_state_t decode_estn(const std::vector<uint8_t> value, uint8_t n);
+ decode_state_t decode_stardmy(const std::vector<uint8_t> value);
+
+ std::deque<uint8_t> m_input_data;
+};
diff --git a/encoder/AFPacket.cpp b/encoder/AFPacket.cpp
deleted file mode 100644
index a1d39b9..0000000
--- a/encoder/AFPacket.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This implements an AF Packet as defined ETSI TS 102 821.
- Also see ETSI TS 102 693
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-#include "config.h"
-#include "crc.h"
-#include "AFPacket.h"
-#include "TagItems.h"
-#include "TagPacket.h"
-#include <vector>
-#include <string>
-#include <iostream>
-#include <cstdio>
-#include <stdint.h>
-#include <arpa/inet.h>
-
-// Header PT field. AF packet contains TAG payload
-#define AFHEADER_PT_TAG 'T'
-
-// AF Packet Major (3 bits) and Minor (4 bits) version
-#define AFHEADER_VERSION 0x10 // MAJ=1, MIN=0
-
-AFPacket AFPacketiser::Assemble(TagPacket tag_packet)
-{
- std::vector<uint8_t> payload = tag_packet.Assemble();
-
- if (m_verbose)
- std::cerr << "Assemble AFPacket " << seq << std::endl;
-
- std::string pack_data("AF"); // SYNC
- std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
-
- uint32_t taglength = payload.size();
-
- if (m_verbose)
- std::cerr << " AFPacket payload size " << payload.size() << std::endl;
-
- // write length into packet
- packet.push_back((taglength >> 24) & 0xFF);
- packet.push_back((taglength >> 16) & 0xFF);
- packet.push_back((taglength >> 8) & 0xFF);
- packet.push_back(taglength & 0xFF);
-
- // fill rest of header
- packet.push_back(seq >> 8);
- packet.push_back(seq & 0xFF);
- seq++;
- packet.push_back((have_crc ? 0x80 : 0) | AFHEADER_VERSION); // ar_cf: CRC=1
- packet.push_back(AFHEADER_PT_TAG);
-
- // insert payload, must have a length multiple of 8 bytes
- packet.insert(packet.end(), payload.begin(), payload.end());
-
- // calculate CRC over AF Header and payload
- uint16_t crc = 0xffff;
- crc = crc16(crc, &(packet.front()), packet.size());
- crc ^= 0xffff;
-
- if (m_verbose)
- fprintf(stderr, " AFPacket crc %x\n", crc);
-
- packet.push_back((crc >> 8) & 0xFF);
- packet.push_back(crc & 0xFF);
-
- if (m_verbose)
- std::cerr << " AFPacket length " << packet.size() << std::endl;
-
- return packet;
-}
-
diff --git a/encoder/AFPacket.h b/encoder/AFPacket.h
deleted file mode 100644
index 9b189b8..0000000
--- a/encoder/AFPacket.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This implements an AF Packet as defined ETSI TS 102 821.
- Also see ETSI TS 102 693
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef _AFPACKET_H_
-#define _AFPACKET_H_
-
-#include "config.h"
-#include <vector>
-#include <stdint.h>
-#include "TagItems.h"
-#include "TagPacket.h"
-
-typedef std::vector<uint8_t> AFPacket;
-
-// ETSI TS 102 821, 6.1 AF packet structure
-class AFPacketiser
-{
- public:
- AFPacketiser() :
- m_verbose(false) {};
- AFPacketiser(bool verbose) :
- m_verbose(verbose) {};
-
- AFPacket Assemble(TagPacket tag_packet);
-
- private:
- static const bool have_crc = true;
-
- uint16_t seq; //counter that overflows at 0xFFFF
-
- bool m_verbose;
-};
-
-#endif
-
diff --git a/encoder/PFT.cpp b/encoder/PFT.cpp
deleted file mode 100644
index 6bed4c1..0000000
--- a/encoder/PFT.cpp
+++ /dev/null
@@ -1,295 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output,
- Protection, Fragmentation and Transport. (PFT)
-
- Are supported:
- Reed-Solomon and Fragmentation
-
- This implements part of PFT as defined ETSI TS 102 821.
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "config.h"
-#include <vector>
-#include <list>
-#include <cstdio>
-#include <cstring>
-#include <stdint.h>
-#include <arpa/inet.h>
-#include <stdexcept>
-#include <sstream>
-#include "PFT.h"
-#include "crc.h"
-#include "ReedSolomon.h"
-
-using namespace std;
-
-// An integer division that rounds up, i.e. ceil(a/b)
-#define CEIL_DIV(a, b) (a % b == 0 ? a / b : a / b + 1)
-
-RSBlock PFT::Protect(AFPacket af_packet)
-{
- RSBlock rs_block;
-
- // number of chunks is ceil(afpacketsize / m_k)
- // TS 102 821 7.2.2: c = ceil(l / k_max)
- m_num_chunks = CEIL_DIV(af_packet.size(), m_k);
-
- if (m_verbose) {
- fprintf(stderr, "Protect %zu chunks of size %zu\n",
- m_num_chunks, af_packet.size());
- }
-
- // calculate size of chunk:
- // TS 102 821 7.2.2: k = ceil(l / c)
- // chunk_len does not include the 48 bytes of protection.
- const size_t chunk_len = CEIL_DIV(af_packet.size(), m_num_chunks);
- if (chunk_len > 207) {
- std::stringstream ss;
- ss << "Chunk length " << chunk_len << " too large (>207)";
- throw std::runtime_error(ss.str());
- }
-
- // The last RS chunk is zero padded
- // TS 102 821 7.2.2: z = c*k - l
- const size_t zero_pad = m_num_chunks * chunk_len - af_packet.size();
-
- // Create the RS(k+p,k) encoder
- const int firstRoot = 1; // Discovered by analysing EDI dump
- const int gfPoly = 0x11d;
- const bool reverse = 0;
- // The encoding has to be 255, 207 always, because the chunk has to
- // be padded at the end, and not at the beginning as libfec would
- // do
- ReedSolomon rs_encoder(255, 207, reverse, gfPoly, firstRoot);
-
- // add zero padding to last chunk
- for (size_t i = 0; i < zero_pad; i++) {
- af_packet.push_back(0);
- }
-
- if (m_verbose) {
- fprintf(stderr, " add %zu zero padding\n", zero_pad);
- }
-
- // Calculate RS for each chunk and assemble RS block
- for (size_t i = 0; i < af_packet.size(); i+= chunk_len) {
- vector<uint8_t> chunk(207);
- vector<uint8_t> protection(ParityBytes);
-
- // copy chunk_len bytes into new chunk
- memcpy(&chunk.front(), &af_packet[i], chunk_len);
-
- // calculate RS for chunk with padding
- rs_encoder.encode(&chunk.front(), &protection.front(), 207);
-
- // Drop the padding
- chunk.resize(chunk_len);
-
- // append new chunk and protection to the RS Packet
- rs_block.insert(rs_block.end(), chunk.begin(), chunk.end());
- rs_block.insert(rs_block.end(), protection.begin(), protection.end());
- }
-
- return rs_block;
-}
-
-vector< vector<uint8_t> > PFT::ProtectAndFragment(AFPacket af_packet)
-{
- const bool enable_RS = (m_m > 0);
-
- if (enable_RS) {
- RSBlock rs_block = Protect(af_packet);
-
-#if 0
- fprintf(stderr, " af_packet (%zu):", af_packet.size());
- for (size_t i = 0; i < af_packet.size(); i++) {
- fprintf(stderr, "%02x ", af_packet[i]);
- }
- fprintf(stderr, "\n");
-
- fprintf(stderr, " rs_block (%zu):", rs_block.size());
- for (size_t i = 0; i < rs_block.size(); i++) {
- fprintf(stderr, "%02x ", rs_block[i]);
- }
- fprintf(stderr, "\n");
-#endif
-
- // TS 102 821 7.2.2: s_max = MIN(floor(c*p/(m+1)), MTU - h))
- const size_t max_payload_size = ( m_num_chunks * ParityBytes ) / (m_m + 1);
-
- // Calculate fragment count and size
- // TS 102 821 7.2.2: ceil((l + c*p + z) / s_max)
- // l + c*p + z = length of RS block
- const size_t num_fragments = CEIL_DIV(rs_block.size(), max_payload_size);
-
- // TS 102 821 7.2.2: ceil((l + c*p + z) / f)
- const size_t fragment_size = CEIL_DIV(rs_block.size(), num_fragments);
-
- if (m_verbose)
- fprintf(stderr, " PnF fragment_size %zu, num frag %zu\n",
- fragment_size, num_fragments);
-
- vector< vector<uint8_t> > fragments(num_fragments);
-
- for (size_t i = 0; i < num_fragments; i++) {
- fragments[i].resize(fragment_size);
- for (size_t j = 0; j < fragment_size; j++) {
- if (j*num_fragments + i > rs_block.size()) {
- fragments[i][j] = 0;
- }
- else {
- fragments[i][j] = rs_block[j*num_fragments + i];
- }
- }
- }
-
- return fragments;
- }
- else { // No RS, only fragmentation
- // TS 102 821 7.2.2: s_max = MTU - h
- const size_t max_payload_size = 1000; // TODO define properly
-
- // Calculate fragment count and size
- // TS 102 821 7.2.2: ceil((l + c*p + z) / s_max)
- // l + c*p + z = length of AF packet
- const size_t num_fragments = CEIL_DIV(af_packet.size(), max_payload_size);
-
- // TS 102 821 7.2.2: ceil((l + c*p + z) / f)
- const size_t fragment_size = CEIL_DIV(af_packet.size(), num_fragments);
- vector< vector<uint8_t> > fragments(num_fragments);
-
- for (size_t i = 0; i < num_fragments; i++) {
- //fragments[i].reserve(fragment_size);
-
- for (size_t j = 0; j < fragment_size; j++) {
- if (i*fragment_size + j > af_packet.size()) {
- break;
- }
- else {
- fragments[i].push_back(af_packet[i*fragment_size + j]);
- }
- }
- }
-
- return fragments;
- }
-}
-
-std::vector< PFTFragment > PFT::Assemble(AFPacket af_packet)
-{
- vector< vector<uint8_t> > fragments = ProtectAndFragment(af_packet);
- vector< vector<uint8_t> > pft_fragments; // These contain PF headers
-
- const bool enable_RS = (m_m > 0);
- const bool enable_transport = true;
-
- unsigned int findex = 0;
-
- unsigned fcount = fragments.size();
-
- // calculate size of chunk:
- // TS 102 821 7.2.2: k = ceil(l / c)
- // chunk_len does not include the 48 bytes of protection.
- const size_t chunk_len = enable_RS ?
- CEIL_DIV(af_packet.size(), m_num_chunks) : 0;
-
- // The last RS chunk is zero padded
- // TS 102 821 7.2.2: z = c*k - l
- const size_t zero_pad = enable_RS ?
- m_num_chunks * chunk_len - af_packet.size() : 0;
-
- for (size_t i = 0; i < fragments.size(); i++) {
- const vector<uint8_t>& fragment = fragments[i];
-
- // Psync
- std::string psync("PF");
- std::vector<uint8_t> packet(psync.begin(), psync.end());
-
- // Pseq
- packet.push_back(m_pseq >> 8);
- packet.push_back(m_pseq & 0xFF);
-
- // Findex
- packet.push_back(findex >> 16);
- packet.push_back(findex >> 8);
- packet.push_back(findex & 0xFF);
- findex++;
-
- // Fcount
- packet.push_back(fcount >> 16);
- packet.push_back(fcount >> 8);
- packet.push_back(fcount & 0xFF);
-
- // RS (1 bit), transport (1 bit) and Plen (14 bits)
- unsigned int plen = fragment.size();
- if (enable_RS) {
- plen |= 0x8000; // Set FEC bit
- }
-
- if (enable_transport) {
- plen |= 0x4000; // Set ADDR bit
- }
-
- packet.push_back(plen >> 8);
- packet.push_back(plen & 0xFF);
-
- if (enable_RS) {
- packet.push_back(chunk_len); // RSk
- packet.push_back(zero_pad); // RSz
- }
-
- if (enable_transport) {
- // Source (16 bits)
- uint16_t addr_source = 0;
- packet.push_back(addr_source >> 8);
- packet.push_back(addr_source & 0xFF);
-
- // Dest (16 bits)
- packet.push_back(m_dest_port >> 8);
- packet.push_back(m_dest_port & 0xFF);
- }
-
- // calculate CRC over AF Header and payload
- uint16_t crc = 0xffff;
- crc = crc16(crc, &(packet.front()), packet.size());
- crc ^= 0xffff;
-
- packet.push_back((crc >> 8) & 0xFF);
- packet.push_back(crc & 0xFF);
-
- // insert payload, must have a length multiple of 8 bytes
- packet.insert(packet.end(), fragment.begin(), fragment.end());
-
- pft_fragments.push_back(packet);
-
-#if 0
- fprintf(stderr, "* PFT pseq %d, findex %d, fcount %d, plen %d\n",
- m_pseq, findex, fcount, plen & ~0x8000);
-#endif
- }
-
- m_pseq++;
-
- return pft_fragments;
-}
-
diff --git a/encoder/PFT.h b/encoder/PFT.h
deleted file mode 100644
index 8fb6d94..0000000
--- a/encoder/PFT.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output,
- Protection, Fragmentation and Transport. (PFT)
-
- Are supported:
- Reed-Solomon and Fragmentation
-
- This implements part of PFT as defined ETSI TS 102 821.
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef _PFT_H_
-#define _PFT_H_
-
-#include "config.h"
-#include <vector>
-#include <list>
-#include <stdexcept>
-#include <stdint.h>
-#include "AFPacket.h"
-#include "Log.h"
-#include "ReedSolomon.h"
-#include "dabOutput/dabOutput.h"
-
-typedef std::vector<uint8_t> RSBlock;
-typedef std::vector<uint8_t> PFTFragment;
-
-class PFT
-{
- public:
- static const int ParityBytes = 48;
-
- PFT() :
- m_k(207),
- m_m(3),
- m_dest_port(12000),
- m_pseq(0),
- m_num_chunks(0),
- m_verbose(false)
- { }
-
- PFT(const edi_configuration_t &conf) :
- m_k(conf.chunk_len),
- m_m(conf.fec),
- m_dest_port(conf.dest_port),
- m_pseq(0),
- m_num_chunks(0),
- m_verbose(conf.verbose)
- {
- if (m_k > 207) {
- etiLog.level(warn) <<
- "EDI PFT: maximum chunk size is 207.";
- throw std::out_of_range("EDI PFT Chunk size too large.");
- }
-
- if (m_m > 5) {
- etiLog.level(warn) <<
- "EDI PFT: high number of recoverable fragments"
- " may lead to large overhead";
- // See TS 102 821, 7.2.1 Known values, list entry for 'm'
- }
- }
-
- // return a list of PFT fragments with the correct
- // PFT headers
- std::vector< PFTFragment > Assemble(AFPacket af_packet);
-
- // Apply Reed-Solomon FEC to the AF Packet
- RSBlock Protect(AFPacket af_packet);
-
- // Cut a RSBlock into several fragments that can be transmitted
- std::vector< std::vector<uint8_t> > ProtectAndFragment(AFPacket af_packet);
-
- private:
- unsigned int m_k; // length of RS data word
- unsigned int m_m; // number of fragments that can be recovered if lost
-
- unsigned int m_dest_port; // Destination port for transport header
-
- uint16_t m_pseq;
-
- size_t m_num_chunks;
-
- bool m_verbose;
-
-};
-
-#endif
-
diff --git a/encoder/TagItems.cpp b/encoder/TagItems.cpp
deleted file mode 100644
index 54e996b..0000000
--- a/encoder/TagItems.cpp
+++ /dev/null
@@ -1,213 +0,0 @@
-/*
- Copyright (C) 2016 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This defines a few TAG items as defined ETSI TS 102 821 and
- ETSI TS 102 693
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef _AFPACKET_H_
-#define _AFPACKET_H_
-
-#include "config.h"
-#include "TagItems.h"
-#include <vector>
-#include <iostream>
-#include <string>
-#include <stdint.h>
-#include <stdexcept>
-
-std::vector<uint8_t> TagStarPTR::Assemble()
-{
- //std::cerr << "TagItem *ptr" << std::endl;
- std::string pack_data("*ptr");
- std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
-
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0x40);
-
- std::string protocol("DETI");
- packet.insert(packet.end(), protocol.begin(), protocol.end());
-
- // Major
- packet.push_back(0);
- packet.push_back(0);
-
- // Minor
- packet.push_back(0);
- packet.push_back(0);
- return packet;
-}
-
-std::vector<uint8_t> TagDETI::Assemble()
-{
- std::string pack_data("deti");
- std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
- packet.reserve(256);
-
- // Placeholder for length
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0);
-
- uint8_t fct = dflc % 250;
- uint8_t fcth = dflc / 250;
-
-
- uint16_t detiHeader = fct | (fcth << 8) | (rfudf << 13) | (ficf << 14) | (atstf << 15);
- packet.push_back(detiHeader >> 8);
- packet.push_back(detiHeader & 0xFF);
-
- uint32_t etiHeader = mnsc | (rfu << 16) | (rfa << 17) |
- (fp << 19) | (mid << 22) | (stat << 24);
- packet.push_back((etiHeader >> 24) & 0xFF);
- packet.push_back((etiHeader >> 16) & 0xFF);
- packet.push_back((etiHeader >> 8) & 0xFF);
- packet.push_back(etiHeader & 0xFF);
-
- if (atstf) {
- packet.push_back(utco);
-
- packet.push_back((seconds >> 24) & 0xFF);
- packet.push_back((seconds >> 16) & 0xFF);
- packet.push_back((seconds >> 8) & 0xFF);
- packet.push_back(seconds & 0xFF);
-
- packet.push_back((tsta >> 16) & 0xFF);
- packet.push_back((tsta >> 8) & 0xFF);
- packet.push_back(tsta & 0xFF);
- }
-
- if (ficf) {
- for (size_t i = 0; i < fic_length; i++) {
- packet.push_back(fic_data[i]);
- }
- }
-
- if (rfudf) {
- packet.push_back((rfud >> 16) & 0xFF);
- packet.push_back((rfud >> 8) & 0xFF);
- packet.push_back(rfud & 0xFF);
- }
-
- // calculate and update size
- // remove TAG name and TAG length fields and convert to bits
- uint32_t taglength = (packet.size() - 8) * 8;
-
- // write length into packet
- packet[4] = (taglength >> 24) & 0xFF;
- packet[5] = (taglength >> 16) & 0xFF;
- packet[6] = (taglength >> 8) & 0xFF;
- packet[7] = taglength & 0xFF;
-
- dflc = (dflc+1) % 5000;
-
- /*
- std::cerr << "TagItem deti, packet.size " << packet.size() << std::endl;
- std::cerr << " fic length " << fic_length << std::endl;
- std::cerr << " length " << taglength / 8 << std::endl;
- */
- return packet;
-}
-
-void TagDETI::set_seconds(std::chrono::system_clock::time_point t)
-{
- std::time_t posix_timestamp_1_jan_2000 = 946684800;
- seconds = std::chrono::system_clock::to_time_t(t) - posix_timestamp_1_jan_2000;
-}
-
-
-std::vector<uint8_t> TagESTn::Assemble()
-{
- std::string pack_data("est");
- std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
- packet.reserve(mst_length*8 + 16);
-
- packet.push_back(id);
-
- // Placeholder for length
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0);
- packet.push_back(0);
-
- if (tpl > 0x3F) {
- throw std::runtime_error("TagESTn: invalid TPL value");
- }
-
- if (sad > 0x3FF) {
- throw std::runtime_error("TagESTn: invalid SAD value");
- }
-
- if (scid > 0x3F) {
- throw std::runtime_error("TagESTn: invalid SCID value");
- }
-
- uint32_t sstc = (scid << 18) | (sad << 8) | (tpl << 2) | rfa;
- packet.push_back((sstc >> 16) & 0xFF);
- packet.push_back((sstc >> 8) & 0xFF);
- packet.push_back(sstc & 0xFF);
-
- for (size_t i = 0; i < mst_length * 8; i++) {
- packet.push_back(mst_data[i]);
- }
-
- // calculate and update size
- // remove TAG name and TAG length fields and convert to bits
- uint32_t taglength = (packet.size() - 8) * 8;
-
- // write length into packet
- packet[4] = (taglength >> 24) & 0xFF;
- packet[5] = (taglength >> 16) & 0xFF;
- packet[6] = (taglength >> 8) & 0xFF;
- packet[7] = taglength & 0xFF;
-
- /*
- std::cerr << "TagItem ESTn, length " << packet.size() << std::endl;
- std::cerr << " mst_length " << mst_length << std::endl;
- */
- return packet;
-}
-
-std::vector<uint8_t> TagStarDMY::Assemble()
-{
- std::string pack_data("*dmy");
- std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
-
- packet.resize(4 + 4 + length_);
-
- const uint32_t length_bits = length_ * 8;
-
- packet[4] = (length_bits >> 24) & 0xFF;
- packet[5] = (length_bits >> 16) & 0xFF;
- packet[6] = (length_bits >> 8) & 0xFF;
- packet[7] = length_bits & 0xFF;
-
- // The remaining bytes in the packet are "undefined data"
-
- return packet;
-}
-
-#endif
-
diff --git a/encoder/TagItems.h b/encoder/TagItems.h
deleted file mode 100644
index 6a8470b..0000000
--- a/encoder/TagItems.h
+++ /dev/null
@@ -1,156 +0,0 @@
-/*
- Copyright (C) 2016 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This defines a few TAG items as defined ETSI TS 102 821 and
- ETSI TS 102 693
-
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef _TAGITEMS_H_
-#define _TAGITEMS_H_
-
-#include "config.h"
-#include "Eti.h"
-#include <vector>
-#include <chrono>
-#include <string>
-#include <stdint.h>
-
-class TagItem
-{
- public:
- virtual std::vector<uint8_t> Assemble() = 0;
-};
-
-// ETSI TS 102 693, 5.1.1 Protocol type and revision
-class TagStarPTR : public TagItem
-{
- public:
- std::vector<uint8_t> Assemble();
-};
-
-// ETSI TS 102 693, 5.1.3 DAB ETI(LI) Management (deti)
-class TagDETI : public TagItem
-{
- public:
- TagDETI()
- {
- // set optional fields to "not present"
- atstf = 0;
- rfudf = 0;
- ficf = 0;
-
- rfa = 0; //shall be zero
- rfu = 0; //mnsc is valid
- }
- std::vector<uint8_t> Assemble();
-
- /***** DATA in intermediary format ****/
- // For the ETI Header: must be defined !
- uint8_t stat;
- uint8_t mid;
- uint8_t fp;
- uint8_t rfa;
- uint8_t rfu;
- uint16_t mnsc;
- uint16_t dflc; // modulo 5000 frame counter
-
- // ATST (optional)
- bool atstf; // presence of atst data
-
- /* UTCO: Offset (in seconds) between UTC and the Seconds value. The
- * value is expressed as an unsigned 8-bit quantity. As of February
- * 2009, the value shall be 2 and shall change as a result of each
- * modification of the number of leap seconds, as proscribed by
- * International Earth Rotation and Reference Systems Service (IERS).
- *
- * According to Annex F
- * EDI = TAI - 32s (constant)
- * EDI = UTC + UTCO
- * we derive
- * UTCO = TAI-UTC - 32
- * where the TAI-UTC offset is given by the USNO bulletin using
- * the ClockTAI module.
- */
- uint8_t utco;
-
- void set_utco(int tai_utc_offset) { utco = tai_utc_offset - 32; }
-
- /* The number of SI seconds since 2000-01-01 T 00:00:00 UTC as an
- * unsigned 32-bit quantity
- */
- uint32_t seconds;
-
- void set_seconds(std::chrono::system_clock::time_point t);
-
- /* TSTA: Shall be the 24 least significant bits of the Time Stamp
- * (TIST) field from the STI-D(LI) Frame. The full definition for the
- * STI TIST can be found in annex B of EN 300 797 [4]. The most
- * significant 8 bits of the TIST field of the incoming STI-D(LI)
- * frame, if required, may be carried in the RFAD field.
- */
- uint32_t tsta;
-
- // the FIC (optional)
- bool ficf;
- const unsigned char* fic_data;
- size_t fic_length;
-
- // rfu
- bool rfudf;
- uint32_t rfud;
-
-
-};
-
-// ETSI TS 102 693, 5.1.5 ETI Sub-Channel Stream <n>
-class TagESTn : public TagItem
-{
- public:
- std::vector<uint8_t> Assemble();
-
- // SSTCn
- uint8_t scid;
- uint16_t sad;
- uint8_t tpl;
- uint8_t rfa;
-
- // Pointer to MSTn data
- uint8_t* mst_data;
- size_t mst_length; // STLn * 8 bytes
-
- uint8_t id;
-};
-
-// ETSI TS 102 821, 5.2.2.2 Dummy padding
-class TagStarDMY : public TagItem
-{
- public:
- /* length is the TAG value length in bytes */
- TagStarDMY(uint32_t length) : length_(length) {}
- std::vector<uint8_t> Assemble();
-
- private:
- uint32_t length_;
-};
-
-#endif
-
diff --git a/encoder/TagPacket.cpp b/encoder/TagPacket.cpp
deleted file mode 100644
index 435c071..0000000
--- a/encoder/TagPacket.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This defines a TAG Packet.
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "config.h"
-#include "Eti.h"
-#include "TagPacket.h"
-#include "TagItems.h"
-#include <vector>
-#include <iostream>
-#include <string>
-#include <list>
-#include <stdint.h>
-#include <cassert>
-
-TagPacket::TagPacket(unsigned int alignment) : m_alignment(alignment)
-{ }
-
-std::vector<uint8_t> TagPacket::Assemble()
-{
- std::list<TagItem*>::iterator tag;
-
- std::vector<uint8_t> packet;
-
- //std::cerr << "Assemble TAGPacket" << std::endl;
-
- for (tag = tag_items.begin(); tag != tag_items.end(); ++tag) {
- std::vector<uint8_t> tag_data = (*tag)->Assemble();
- packet.insert(packet.end(), tag_data.begin(), tag_data.end());
-
- //std::cerr << " Add TAGItem of length " << tag_data.size() << std::endl;
- }
-
- if (m_alignment == 0) { /* no padding */ }
- else if (m_alignment == 8) {
- // Add padding inside TAG packet
- while (packet.size() % 8 > 0) {
- packet.push_back(0); // TS 102 821, 5.1, "padding shall be undefined"
- }
- }
- else if (m_alignment > 8) {
- TagStarDMY dmy(m_alignment - 8);
- auto dmy_data = dmy.Assemble();
- packet.insert(packet.end(), dmy_data.begin(), dmy_data.end());
- }
- else {
- std::cerr << "Invalid alignment requirement " << m_alignment <<
- " defined in TagPacket" << std::endl;
- }
-
- return packet;
-}
-
diff --git a/encoder/TagPacket.h b/encoder/TagPacket.h
deleted file mode 100644
index 5ad1904..0000000
--- a/encoder/TagPacket.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- Copyright (C) 2013,2014 Matthias P. Braendli
- http://mpb.li
-
- EDI output.
- This defines a TAG Packet.
- */
-/*
- This file is part of ODR-DabMux.
-
- ODR-DabMux is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- ODR-DabMux is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef _TAGPACKET_H_
-#define _TAGPACKET_H_
-
-#include "config.h"
-#include "TagItems.h"
-#include <vector>
-#include <string>
-#include <list>
-#include <stdint.h>
-
-
-
-// A TagPacket is nothing else than a list of tag items, with an
-// Assemble function that puts the bytestream together and adds
-// padding such that the total length is a multiple of 8 Bytes.
-//
-// ETSI TS 102 821, 5.1 Tag Packet
-class TagPacket
-{
- public:
- TagPacket(unsigned int alignment);
- std::vector<uint8_t> Assemble();
-
- std::list<TagItem*> tag_items;
-
- private:
- unsigned int m_alignment;
-};
-
-#endif
-
diff --git a/test/main.cpp b/test/main.cpp
index 7ab618f..5e00439 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -6,10 +6,32 @@
#include <stdint.h>
#include <stdio.h>
-#include "PFT.h"
+#include "EDIDecoder.hpp"
int main(int argc, char **argv)
{
+ if (argc == 1) {
+ fprintf(stderr, "Please specify filename\n");
+ return 1;
+ }
+
+ const char* fname = argv[1];
+ FILE* fd = fopen(fname, "r");
+
+ EDIDecoder decoder;
+
+ while (not feof(fd)) {
+ uint8_t buf;
+ int bytes_read = fread(&buf, 1, 1, fd);
+
+ if (bytes_read) {
+ decoder.push_byte(buf);
+ }
+ else {
+ break;
+ }
+ }
+
return 0;
}