/* 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 . */ #include "config.h" #include #include #include #include #include #include #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(), 207); 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); // 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 ReedSolomon rs_encoder(chunk_len + ParityBytes, chunk_len); // 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 chunk(chunk_len + ParityBytes); // copy chunk_len bytes into new chunk memcpy(&chunk.front(), &af_packet[i], chunk_len); // calculate RS for chunk rs_encoder.encode(&chunk.front(), chunk.size()); // append new chunk to the RS Packet rs_block.insert(rs_block.end(), chunk.begin(), chunk.end()); } return rs_block; } vector< vector > PFT::ProtectAndFragment(AFPacket af_packet) { 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 > 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; } std::vector< PFTFragment > PFT::Assemble(AFPacket af_packet) { vector< vector > fragments = ProtectAndFragment(af_packet); vector< vector > pft_fragments; // These contain PF headers 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 = CEIL_DIV(af_packet.size(), m_num_chunks); // 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(); for (size_t i = 0; i < fragments.size(); i++) { const vector& fragment = fragments[i]; std::string psync("PF"); // SYNC std::vector packet(psync.begin(), psync.end()); packet.push_back(m_pseq >> 8); packet.push_back(m_pseq & 0xFF); packet.push_back(findex >> 16); packet.push_back(findex >> 8); packet.push_back(findex & 0xFF); findex++; packet.push_back(fcount >> 16); packet.push_back(fcount >> 8); packet.push_back(fcount & 0xFF); unsigned int plen = fragment.size(); plen |= 0x8000; // Set FEC bit packet.push_back(plen >> 8); packet.push_back(plen & 0xFF); packet.push_back(chunk_len); // RSk packet.push_back(zero_pad); // RSz // 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; }