diff options
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | etisnoop/Makefile | 20 | ||||
-rw-r--r-- | etisnoop/README | 3 | ||||
-rw-r--r-- | etisnoop/dabplussnoop.cpp | 308 | ||||
-rw-r--r-- | etisnoop/dabplussnoop.h | 132 | ||||
-rw-r--r-- | etisnoop/etiinput.cpp | 197 | ||||
-rw-r--r-- | etisnoop/etiinput.h | 49 | ||||
-rw-r--r-- | etisnoop/etisnoop.cpp | 1133 | ||||
-rw-r--r-- | etisnoop/faad_decoder.cpp | 231 | ||||
-rw-r--r-- | etisnoop/faad_decoder.h | 122 | ||||
-rw-r--r-- | etisnoop/firecode.c | 50 | ||||
-rw-r--r-- | etisnoop/firecode.h | 33 | ||||
-rw-r--r-- | etisnoop/lib_crc.c | 459 | ||||
-rw-r--r-- | etisnoop/lib_crc.h | 66 | ||||
-rw-r--r-- | etisnoop/utils.h | 32 | ||||
-rw-r--r-- | etisnoop/wavfile.c | 82 | ||||
-rw-r--r-- | etisnoop/wavfile.h | 18 |
17 files changed, 11 insertions, 2938 deletions
@@ -3,18 +3,20 @@ Auxiliary tools and scripts for mmbTools This folder contains a weakly structured collection of scripts and tools. +If you start with the mmbTools, don't look here. Have a look a the guide, the +wiki and the opendigitalradio website first. Many things here are old and broken. + Additional information might be found in README-SFN, that was written mid-2012. There is: - * a few .ini files: example configuration files for CRC-DABMOD - * test.config for CRC-DabMux - * scripts to encode to mp2 + * a few .ini files: example configuration files for ODR-DabMod + * test.config for ODR-DabMux + * scripts to encode to mp2 and DAB+ * moduhd\*.sh illustrate how to start dabmod before configuration files were created * mux-throttled.sh same for dabmux - * filter/ utility to generate taps for FIRFilter * qrg.txt list of channels/frequencies * eti\_tcp.py used to send ETI over TCP to dabmod - * eti-udp/ experimental UDP transport with FEC - * zmq-rx.py python tool to receive data from a ZeroMQ socket + * eti-udp/ experimental UDP transport with FEC, hasn't been pursued. + * some ZMQ experiments mpb diff --git a/etisnoop/Makefile b/etisnoop/Makefile deleted file mode 100644 index 334077b..0000000 --- a/etisnoop/Makefile +++ /dev/null @@ -1,20 +0,0 @@ - -CC=g++ - -SOURCES=etisnoop.cpp dabplussnoop.cpp lib_crc.c firecode.c faad_decoder.cpp wavfile.c etiinput.cpp -HEADERS=dabplussnoop.h lib_crc.h firecode.h faad_decoder.h wavfile.h etiinput.h - -all: etisnoop - -etisnoop: $(SOURCES) $(HEADERS) - $(CC) -Wall -ggdb $(SOURCES) $(HEADERS) -lfaad -o etisnoop - -etisnoop-static: libfaad $(SOURCES) $(HEADERS) - $(CC) -Wall -ggdb $(SOURCES) $(HEADERS) -Ifaad2-2.7/include faad2-2.7/libfaad/.libs/libfaad.a -o etisnoop - -libfaad: - make -C ./faad2-2.7 - - -clean: - rm -f etisnoop *.o diff --git a/etisnoop/README b/etisnoop/README new file mode 100644 index 0000000..5acad02 --- /dev/null +++ b/etisnoop/README @@ -0,0 +1,3 @@ +etisnoop has been moved to its own repository. + +http://github.com/Opendigitalradio/etisnoop diff --git a/etisnoop/dabplussnoop.cpp b/etisnoop/dabplussnoop.cpp deleted file mode 100644 index aed155a..0000000 --- a/etisnoop/dabplussnoop.cpp +++ /dev/null @@ -1,308 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - dabplussnoop.cpp - Parse DAB+ frames from a ETI file - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <string> -#include <cassert> -#include <sstream> -#include <algorithm> -#include <vector> -#include "dabplussnoop.h" -#include "firecode.h" -#include "lib_crc.h" -#include "faad_decoder.h" - -#define DPS_INDENT "\t\t" -#define DPS_PREFIX "DAB+ decode:" - -#define DPS_DEBUG 0 - -using namespace std; - -void DabPlusSnoop::push(uint8_t* streamdata, size_t streamsize) -{ - size_t original_size = m_data.size(); - m_data.resize(original_size + streamsize); - - memcpy(&m_data[original_size], streamdata, streamsize); - - if (seek_valid_firecode()) { - // m_data now points to a valid header - if (decode()) { - // We have been able to decode the AUs - - // First dump to file - if (m_raw_data_stream_fd == NULL) { - stringstream dump_filename; - dump_filename << "stream-" << m_index << ".dabp"; - - m_raw_data_stream_fd = fopen(dump_filename.str().c_str(), "w"); - - if (m_raw_data_stream_fd == NULL) { - perror("File open failed"); - } - } - - fwrite(&m_data[0], m_subchannel_index, 120, m_raw_data_stream_fd); - - m_data.erase(m_data.begin(), m_data.begin() + m_subchannel_index * 120); - } - } -} - -// Idea and some code taken from Xpadxpert -bool DabPlusSnoop::seek_valid_firecode() -{ - if (m_data.size() < 10) { - // Not enough data - return -1; - } - - bool crc_ok = false; - size_t i; - - for (i = 0; i < m_data.size() - 10; i++) { - uint8_t* b = &m_data[i]; - - // the three bytes after the firecode must not be zero - // (simple plausibility check to avoid sync in zero byte region) - if (b[3] != 0x00 || (b[4] & 0xF0) != 0x00) { - uint16_t header_firecode = (b[0] << 8) | b[1]; - uint16_t calculated_firecode = firecode_crc(b+2, 9); - - if (header_firecode == calculated_firecode) { - crc_ok = true; - break; - } - } - } - - if (crc_ok) { -#if DPS_DEBUG - printf(DPS_PREFIX " Found valid FireCode at %zu\n", i); -#endif - - m_data.erase(m_data.begin(), m_data.begin() + i); - return true; - } - else { -#if DPS_DEBUG - printf(DPS_PREFIX " No valid FireCode found\n"); -#endif - - m_data.clear(); - return false; - } -} - -bool DabPlusSnoop::decode() -{ -#if DPS_DEBUG - printf(DPS_PREFIX " We have %zu bytes of data\n", m_data.size()); -#endif - - if (m_subchannel_index && m_data.size() >= m_subchannel_index * 120) { - - uint8_t* b = &m_data[0]; - - // -- Parse he_aac_super_frame - // ---- Parse he_aac_super_frame_header - // ------ Parse firecode and audio params - uint16_t header_firecode = (b[0] << 8) | b[1]; - uint8_t audio_params = b[2]; - int rfa = (audio_params & 0x80) ? true : false; - m_dac_rate = (audio_params & 0x40) ? true : false; - m_sbr_flag = (audio_params & 0x20) ? true : false; - m_aac_channel_mode = (audio_params & 0x10) ? true : false; - m_ps_flag = (audio_params & 0x08) ? true : false; - m_mpeg_surround_config = (audio_params & 0x07); - - int num_aus; - if (!m_dac_rate && m_sbr_flag) num_aus = 2; - // AAC core sampling rate 16 kHz - else if (m_dac_rate && m_sbr_flag) num_aus = 3; - // AAC core sampling rate 24 kHz - else if (!m_dac_rate && !m_sbr_flag) num_aus = 4; - // AAC core sampling rate 32 kHz - else if (m_dac_rate && !m_sbr_flag) num_aus = 6; - // AAC core sampling rate 48 kHz - -#if DPS_DEBUG - printf( DPS_INDENT DPS_PREFIX "\n" - DPS_INDENT "\tfirecode 0x%x\n" - DPS_INDENT "\trfa %d\n" - DPS_INDENT "\tdac_rate %d\n" - DPS_INDENT "\tsbr_flag %d\n" - DPS_INDENT "\taac_channel_mode %d\n" - DPS_INDENT "\tps_flag %d\n" - DPS_INDENT "\tmpeg_surround_config %d\n" - DPS_INDENT "\tnum_aus %d\n", - header_firecode, rfa, m_dac_rate, m_sbr_flag, - m_aac_channel_mode, m_ps_flag, m_mpeg_surround_config, - num_aus); -#else - // Avoid "unused variable" warning - (void)header_firecode; - (void)rfa; -#endif - - - // ------ Parse au_start - b += 3; - - vector<uint8_t> au_start_nibbles(0); - - /* Each AU_START is encoded in three nibbles. - * When we have n AUs, we have n-1 au_start values. */ - for (int i = 0; i < (num_aus-1)*3; i++) { - if (i % 2 == 0) { - char nibble = b[i/2] >> 4; - - au_start_nibbles.push_back( nibble ); - } - else { - char nibble = b[i/2] & 0x0F; - - au_start_nibbles.push_back( nibble ); - } - - } - - - vector<int> au_start(num_aus); - - if (num_aus == 2) - au_start[0] = 5; - else if (num_aus == 3) - au_start[0] = 6; - else if (num_aus == 4) - au_start[0] = 8; - else if (num_aus == 6) - au_start[0] = 11; - - - int nib = 0; - for (int au = 1; au < num_aus; au++) { - au_start[au] = au_start_nibbles[nib] << 8 | \ - au_start_nibbles[nib+1] << 4 | \ - au_start_nibbles[nib+2]; - - nib += 3; - } - -#if DPS_DEBUG - printf(DPS_INDENT DPS_PREFIX " AU start\n"); - for (int au = 0; au < num_aus; au++) { - printf(DPS_INDENT "\tAU[%d] %d 0x%x\n", au, - au_start[au], - au_start[au]); - } -#endif - - return extract_au(au_start); - } - else { - return false; - } -} - -bool DabPlusSnoop::extract_au(vector<int> au_start) -{ - vector<vector<uint8_t> > aus(au_start.size()); - - // The last entry of au_start must the end of valid - // AU data. We stop at m_subchannel_index * 110 because - // what comes after is RS parity - au_start.push_back(m_subchannel_index * 110); - - bool all_crc_ok = true; - - for (size_t au = 0; au < aus.size(); au++) - { -#if DPS_DEBUG - printf(DPS_PREFIX DPS_INDENT - "Copy au %zu of size %zu\n", - au, - au_start[au+1] - au_start[au]-2 ); -#endif - - aus[au].resize(au_start[au+1] - au_start[au]-2); - std::copy( - m_data.begin() + au_start[au], - m_data.begin() + au_start[au+1]-2, - aus[au].begin() ); - - /* Check CRC */ - uint16_t au_crc = m_data[au_start[au+1]-2] << 8 | \ - m_data[au_start[au+1]-1]; - - uint16_t calc_crc = 0xFFFF; - for (vector<uint8_t>::iterator au_data = aus[au].begin(); - au_data != aus[au].end(); - ++au_data) { - calc_crc = update_crc_ccitt(calc_crc, *au_data); - } - calc_crc =~ calc_crc; - - if (calc_crc != au_crc) { - printf(DPS_INDENT DPS_PREFIX - "Erroneous CRC for au %zu\n", au); - - all_crc_ok = false; - } - } - - if (all_crc_ok) { - return analyse_au(aus); - } - else { - return false; - } -} - -bool DabPlusSnoop::analyse_au(vector<vector<uint8_t> >& aus) -{ - stringstream ss_filename; - - ss_filename << "stream-" << m_index; - - if (!m_faad_decoder.is_initialised()) { - m_faad_decoder.open(ss_filename.str(), m_ps_flag, - m_aac_channel_mode, m_dac_rate, m_sbr_flag, - m_mpeg_surround_config); - } - - return m_faad_decoder.decode(aus); -} - -void DabPlusSnoop::close() -{ - m_faad_decoder.close(); - - if (m_raw_data_stream_fd) { - fclose(m_raw_data_stream_fd); - } -} diff --git a/etisnoop/dabplussnoop.h b/etisnoop/dabplussnoop.h deleted file mode 100644 index 7d7ee04..0000000 --- a/etisnoop/dabplussnoop.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - dabplussnoop.cpp - Parse DAB+ frames from a ETI file - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -/* From the spec: - -subchannel_index = MSC sub-channel size (kbps) / 8 -audio_super_frame_size (bytes) = subchannel_index * 110 - -// Derived from -// au_start[n] = au_start[n - 1] + au_size[n - 1] + 2; -// 2 bytes for CRC -au_size[n] = au_start[n+1] - au_start[n] - 2; - -he_aac_super_frame(subchannel_index) -{ - he_aac_super_frame_header() - { - header_firecode 16 - rfa 1 - dac_rate 1 - sbr_flag 1 - aac_channel_mode 1 - ps_flag 1 - mpeg_surround_config 3 - - // end of audio parameters - if ((dac_rate == 0) && (sbr_flag == 1)) num_aus = 2; - // AAC core sampling rate 16 kHz - if ((dac_rate == 1) && (sbr_flag == 1)) num_aus = 3; - // AAC core sampling rate 24 kHz - if ((dac_rate == 0) && (sbr_flag == 0)) num_aus = 4; - // AAC core sampling rate 32 kHz - if ((dac_rate == 1) && (sbr_flag == 0)) num_aus = 6; - // AAC core sampling rate 48 kHz - - for (n = 1; n < num_aus; n++) { - au_start[n]; 12 - } - - if !((dac_rate == 1) && (sbr_flag == 1)) - alignment 4 - } - - for (n = 0; n < num_aus; n++) { - au[n] 8 * au_size[n] - au_crc[n] 16 - } - -} -*/ - -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <string> -#include <sstream> -#include <vector> -#include "faad_decoder.h" - -#ifndef __DABPLUSSNOOP_H_ -#define __DABPLUSSNOOP_H_ - - -class DabPlusSnoop -{ - public: - DabPlusSnoop() : - m_index(0), - m_subchannel_index(0), - m_data(0), - m_raw_data_stream_fd(NULL) {} - - void set_subchannel_index(unsigned subchannel_index) - { - m_subchannel_index = subchannel_index; - } - - void set_index(int index) - { - m_index = index; - } - - void push(uint8_t* streamdata, size_t streamsize); - - void close(void); - - private: - /* Data needed for FAAD */ - FaadDecoder m_faad_decoder; - int m_index; - - bool m_ps_flag; - bool m_aac_channel_mode; - bool m_dac_rate; - bool m_sbr_flag; - int m_mpeg_surround_config; - - /* Functions */ - bool seek_valid_firecode(void); - bool decode(void); - bool extract_au(std::vector<int> au_start); - bool analyse_au(std::vector<std::vector<uint8_t> >& aus); - - unsigned m_subchannel_index; - std::vector<uint8_t> m_data; - - FILE* m_raw_data_stream_fd; -}; - -#endif - diff --git a/etisnoop/etiinput.cpp b/etisnoop/etiinput.cpp deleted file mode 100644 index ea3241f..0000000 --- a/etisnoop/etiinput.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 - Her Majesty the Queen in Right of Canada (Communications Research - Center Canada) - - Copyrigth (C) 2014 - Matthias P. Braendli, matthias.braendli@mpb.li - - Taken from ODR-DabMod - - Supported file formats: RAW, FRAMED, STREAMED - Supports re-sync to RAW ETI file - */ -/* - This file is part of ODR-DabMod. - - ODR-DabMod 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-DabMod 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-DabMod. If not, see <http://www.gnu.org/licenses/>. - */ -#include "etiinput.h" -#include <stdlib.h> -#include <stdint.h> -#include <string.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> -#include <fcntl.h> /* Definition of AT_* constants */ -#include <sys/stat.h> - -int identify_eti_format(FILE* inputFile, int *streamType) -{ - *streamType = ETI_STREAM_TYPE_NONE; - - struct stat inputFileStat; - fstat(fileno(inputFile), &inputFileStat); - size_t inputfilelength_ = inputFileStat.st_size; - - int nbframes_; - - uint32_t sync; - uint32_t nbFrames; - uint16_t frameSize; - - char discard_buffer[6144]; - - if (fread(&sync, sizeof(sync), 1, inputFile) != 1) { - fprintf(stderr, "Unable to read sync in input file!\n"); - perror(""); - return -1; - } - if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - *streamType = ETI_STREAM_TYPE_RAW; - if (inputfilelength_ > 0) { - nbframes_ = inputfilelength_ / 6144; - } - else { - nbframes_ = ~0; - } - if (fseek(inputFile, -sizeof(sync), SEEK_CUR) != 0) { - // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputFile) - != 1) { - fprintf(stderr, "Unable to read from input file!\n"); - perror(""); - return -1; - } - } - return 0; - } - - nbFrames = sync; - if (fread(&frameSize, sizeof(frameSize), 1, inputFile) != 1) { - fprintf(stderr, "Unable to read frame size in input file!\n"); - perror(""); - return -1; - } - sync >>= 16; - sync &= 0xffff; - sync |= ((uint32_t)frameSize) << 16; - - if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - *streamType = ETI_STREAM_TYPE_STREAMED; - frameSize = nbFrames & 0xffff; - if (inputfilelength_ > 0) { - nbframes_ = inputfilelength_ / (frameSize + 2); - } - else { - nbframes_ = ~0; - } - if (fseek(inputFile, -6, SEEK_CUR) != 0) { - // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, frameSize - 4, 1, inputFile) - != 1) { - fprintf(stderr, "Unable to read from input file!\n"); - perror(""); - return -1; - } - } - return 0; - } - - if (fread(&sync, sizeof(sync), 1, inputFile) != 1) { - fprintf(stderr, "Unable to read nb frame in input file!\n"); - perror(""); - return -1; - } - if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - *streamType = ETI_STREAM_TYPE_FRAMED; - if (fseek(inputFile, -6, SEEK_CUR) != 0) { - // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, frameSize - 4, 1, inputFile) - != 1) { - fprintf(stderr, "Unable to read from input file!\n"); - perror(""); - return -1; - } - } - nbframes_ = ~0; - return 0; - } - - // Search for the sync marker byte by byte - for (size_t i = 10; i < 6144 + 10; ++i) { - sync >>= 8; - sync &= 0xffffff; - if (fread((uint8_t*)&sync + 3, 1, 1, inputFile) != 1) { - fprintf(stderr, "Unable to read from input file!\n"); - perror(""); - return -1; - } - if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - *streamType = ETI_STREAM_TYPE_RAW; - if (inputfilelength_ > 0) { - nbframes_ = (inputfilelength_ - i) / 6144; - } - else { - nbframes_ = ~0; - } - if (fseek(inputFile, -sizeof(sync), SEEK_CUR) != 0) { - if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputFile) - != 1) { - fprintf(stderr, "Unable to read from input file!\n"); - perror(""); - return -1; - } - } - return 0; - } - } - - fprintf(stderr, "Bad input file format!\n"); - return -1; -} - -int get_eti_frame(FILE* inputfile, int stream_type, void* buf) -{ - // Initialise buffer - memset(buf, 0x55, 6144); - - uint16_t frameSize; - if (stream_type == ETI_STREAM_TYPE_RAW) { - frameSize = 6144; - } - else { - if (fread(&frameSize, sizeof(frameSize), 1, inputfile) != 1) { - // EOF - return 0; - } - } - - if (frameSize > 6144) { // there might be a better limit - printf("Wrong frame size %u in ETI file!\n", frameSize); - return -1; - } - - int read_bytes = fread(buf, 1, frameSize, inputfile); - if (read_bytes != frameSize) { - // A short read of a frame (i.e. reading an incomplete frame) - // is not tolerated. Input files must not contain incomplete frames - printf("Incomplete frame in ETI file!\n"); - return -1; - } - - // We have added padding, so we always return 6144 bytes - return 6144; -} - diff --git a/etisnoop/etiinput.h b/etisnoop/etiinput.h deleted file mode 100644 index d8b7691..0000000 --- a/etisnoop/etiinput.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 - Her Majesty the Queen in Right of Canada (Communications Research - Center Canada) - - Copyrigth (C) 2014 - Matthias P. Braendli, matthias.braendli@mpb.li - - Taken from ODR-DabMod - - Supported file formats: RAW, FRAMED, STREAMED - Supports re-sync to RAW ETI file - */ -/* - This file is part of ODR-DabMod. - - ODR-DabMod 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-DabMod 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-DabMod. If not, see <http://www.gnu.org/licenses/>. - */ -#include <stdio.h> - -#ifndef _ETIINPUT_H_ -#define _ETIINPUT_H_ - - -#define ETI_STREAM_TYPE_NONE 0 -#define ETI_STREAM_TYPE_RAW 1 -#define ETI_STREAM_TYPE_STREAMED 2 -#define ETI_STREAM_TYPE_FRAMED 3 - -/* Identify the stream type, and return 0 on success, -1 on failure */ -int identify_eti_format(FILE* inputfile, int *stream_type); - -/* Read the next ETI frame into buf, which must be at least 6144 bytes big - * Return number of bytes read, or zero if EOF */ -int get_eti_frame(FILE* inputfile, int stream_type, void* buf); - -#endif - diff --git a/etisnoop/etisnoop.cpp b/etisnoop/etisnoop.cpp deleted file mode 100644 index a048a1c..0000000 --- a/etisnoop/etisnoop.cpp +++ /dev/null @@ -1,1133 +0,0 @@ -/* - Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/) - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - etisnoop.cpp - Parse ETI NI G.703 file - - Authors: - Sergio Sagliocco <sergio.sagliocco@csp.it> - Matthias P. Braendli <matthias@mpb.li> -*/ - - - -#include <stdio.h> -#include <unistd.h> -#include <getopt.h> -#include <stdlib.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <string.h> -#include <string> -#include <vector> -#include <map> -#include <sstream> -#include "lib_crc.h" - -#include "dabplussnoop.h" -#include "etiinput.h" - -struct FIG -{ - int type; - int ext; - int len; -}; - -class FIGalyser -{ - public: - FIGalyser() - { - clear(); - } - - void set_fib(int fib) - { - m_fib = fib; - } - - void push_back(int type, int ext, int len) - { - struct FIG fig = { - .type = type, - .ext = ext, - .len = len }; - - m_figs[m_fib].push_back(fig); - } - - void analyse() - { - printf("FIC "); - - for (size_t fib = 0; fib < m_figs.size(); fib++) { - int consumed = 7; - int fic_size = 0; - printf(" [ FIB%1d ", fib); - - for (size_t i = 0; i < m_figs[fib].size(); i++) { - FIG &f = m_figs[fib][i]; - printf("%01d/%02d (%2d) ", f.type, f.ext, f.len); - - consumed += 10; - - fic_size += f.len; - } - - printf(" "); - - int align = 30 - consumed; - if (align > 0) { - while (align--) { - printf(" "); - } - } - - printf("|"); - - for (int i = 0; i < 15; i++) { - if (2*i < fic_size) { - printf("#"); - } - else { - printf("-"); - } - } - - printf("| ] "); - - } - - printf("\n"); - } - - void clear() - { - m_figs.clear(); - m_figs.resize(3); - } - - private: - int m_fib; - std::vector<std::vector<FIG> > m_figs; -}; - - -struct FIG0_13_shortAppInfo -{ - uint16_t SId; - uint8_t No:4; - uint8_t SCIdS:4; -} PACKED; - - -#define ETINIPACKETSIZE 6144 - -using namespace std; - -struct eti_analyse_config_t { - FILE* etifd; - bool ignore_error; - std::map<int, DabPlusSnoop> streams_to_decode; - bool analyse_fic_carousel; -}; - -// Globals -static int verbosity; - -// Function prototypes -void printinfo(string header, - int indent_level, - int min_verb=0); - -void printbuf(string header, - int indent_level, - unsigned char* buffer, - size_t size, - string desc=""); - -void decodeFIG(FIGalyser &figs, - unsigned char* figdata, - unsigned char figlen, - unsigned short int figtype, - unsigned short int indent); - -int eti_analyse(eti_analyse_config_t& config); - -std::string get_fig_0_13_userapp(int user_app_type) -{ - switch (user_app_type) { - case 0x000: return "Reserved for future definition"; - case 0x001: return "Not used"; - case 0x002: return "MOT Slideshow"; - case 0x003: return "MOT Broadacst Web Site"; - case 0x004: return "TPEG"; - case 0x005: return "DGPS"; - case 0x006: return "TMC"; - case 0x007: return "EPG"; - case 0x008: return "DAB Java"; - case 0x44a: return "Journaline"; - default: return "Reserved for future applications"; - } -} - -#define no_argument 0 -#define required_argument 1 -#define optional_argument 2 -const struct option longopts[] = { - {"help", no_argument, 0, 'h'}, - {"verbose", no_argument, 0, 'v'}, - {"ignore-error", no_argument, 0, 'e'}, - {"decode-stream", required_argument, 0, 'd'}, - {"input", required_argument, 0, 'i'} -}; - -void usage(void) -{ - fprintf(stderr, - "ETISnoop analyser\n\n" - "The ETSnoop analyser decodes and prints out a RAW ETI file in a\n" - "form that makes analysis easier.\n" - "Usage: etisnoop [-v] [-f] [-i filename] [-d stream_index]\n" - "\n" - " -v increase verbosity (can be given more than once)\n" - " -d N decode subchannel N into .dabp, .aac and .wav files\n" - " -f analyse FIC carousel\n"); -} - -int main(int argc, char *argv[]) -{ - int index; - int ch = 0; - string file_name("-"); - map<int, DabPlusSnoop> streams_to_decode; - - verbosity = 0; - bool ignore_error = false; - bool analyse_fic_carousel = false; - - while(ch != -1) { - ch = getopt_long(argc, argv, "d:efhvi:", longopts, &index); - switch (ch) { - case 'd': - { - int subchix = atoi(optarg); - DabPlusSnoop dps; - streams_to_decode[subchix] = dps; - } - break; - case 'e': - ignore_error = true; - break; - case 'i': - file_name = optarg; - break; - case 'f': - analyse_fic_carousel = true; - break; - case 'v': - verbosity++; - break; - case 'h': - usage(); - return 1; - break; - } - } - - FILE* etifd; - - if (file_name == "-") { - printf("Analysing stdin\n"); - etifd = stdin; - } - else { - etifd = fopen(file_name.c_str(), "r"); - if (etifd == NULL) { - perror("File open failed"); - return 1; - } - } - - eti_analyse_config_t config = { - .etifd = etifd, - .ignore_error = ignore_error, - .streams_to_decode = streams_to_decode, - .analyse_fic_carousel = analyse_fic_carousel - }; - eti_analyse(config); - fclose(etifd); -} - -int eti_analyse(eti_analyse_config_t& config) -{ - unsigned char p[ETINIPACKETSIZE]; - string desc; - char prevsync[3]={0x00,0x00,0x00}; - unsigned char ficf,nst,fp,mid,ficl; - unsigned short int fl,crch; - unsigned short int crc; - unsigned char scid,tpl,l1; - unsigned short int sad[64],stl[64]; - char sdesc[256]; - - bool running = true; - - int stream_type = ETI_STREAM_TYPE_NONE; - if (identify_eti_format(config.etifd, &stream_type) == -1) { - printf("Could not identify stream type\n"); - - running = false; - } - else { - printf("Identified ETI type "); - if (stream_type == ETI_STREAM_TYPE_RAW) - printf("RAW\n"); - else if (stream_type == ETI_STREAM_TYPE_STREAMED) - printf("STREAMED\n"); - else if (stream_type == ETI_STREAM_TYPE_FRAMED) - printf("FRAMED\n"); - else - printf("?\n"); - } - - while (running) { - - int ret = get_eti_frame(config.etifd, stream_type, p); - if (ret == -1) { - fprintf(stderr, "ETI file read error\n"); - break; - } - else if (ret == 0) { - fprintf(stderr, "End of ETI\n"); - break; - } - - // SYNC - printbuf("SYNC", 0, p, 4); - - // SYNC - ERR - if (p[0] == 0xFF) { - desc = "No error"; - printbuf("ERR", 1, p, 1, desc); - } - else { - desc = "Error"; - printbuf("ERR", 1, p, 1, desc); - if (!config.ignore_error) { - printf("Aborting because of SYNC error\n"); - break; - } - } - - // SYNC - FSYNC - - if (memcmp(prevsync, "\x00\x00\x00", 3) == 0) { - if ( (memcmp(p + 1, "\x07\x3a\xb6", 3) == 0) || - (memcmp(p + 1, "\xf8\xc5\x49", 3) == 0) ) { - desc = "OK"; - memcpy(prevsync, p+1, 3); - } - else { - desc ="Wrong FSYNC"; - memcpy(prevsync, "\x00\x00\x00", 3); - } - } else if (memcmp(prevsync, "\x07\x3a\xb6", 3) == 0) { - if (memcmp(p + 1, "\xf8\xc5\x49", 3) != 0) { - desc = "Wrong FSYNC"; - memcpy(prevsync, "\x00\x00\x00", 3); - } else { - desc = "OK"; - memcpy(prevsync, p + 1, 3); - } - } else if (memcmp(prevsync, "\xf8\xc5\x49", 3) == 0) { - if (memcmp(p + 1, "\x07\x3a\xb6", 3) != 0) { - desc = "Wrong FSYNC"; - memcpy(prevsync, "\x00\x00\x00", 3); - } else { - desc = "OK"; - memcpy(prevsync, p + 1, 3); - } - } - printbuf("Sync FSYNC", 1, p + 1, 3, desc); - - // LIDATA - printbuf("LDATA", 0, NULL, 0); - // LIDATA - FC - printbuf("FC - Frame Characterization field", 1, p+4, 4); - // LIDATA - FC - FCT - char fct[25]; - sprintf(fct, "%d", p[4]); - printbuf("FCT - Frame Count", 2, p+4, 1, fct); - // LIDATA - FC - FICF - ficf = (p[5] & 0x80) >> 7; - - { - stringstream ss; - ss << (int)ficf; - if (ficf == 1) { - ss << "- FIC Information are present"; - } - else { - ss << "- FIC Information are not present"; - } - - printbuf("FICF - Fast Information Channel Flag", 2, NULL, 0, ss.str()); - } - - // LIDATA - FC - NST - nst = p[5] & 0x7F; - { - stringstream ss; - ss << (int)nst; - printbuf("NST - Number of streams", 2, NULL, 0, ss.str()); - } - - // LIDATA - FC - FP - fp = (p[6] & 0xE0) >> 5; - { - stringstream ss; - ss << (int)fp; - printbuf("FP - Frame Phase", 2, &fp, 1, ss.str()); - } - - // LIDATA - FC - MID - mid = (p[6] & 0x18) >> 3; - { - stringstream ss; - ss << "Mode "; - if (mid != 0) { - ss << (int)mid; - } - else { - ss << "4"; - } - printbuf("MID - Mode Identity", 2, &mid, 1, ss.str()); - } - - // LIDATA - FC - FL - fl = (p[6] & 0x07) * 256 + p[7]; - { - stringstream ss; - ss << fl << " words"; - printbuf("FL - Frame Length", 2, NULL, 0, ss.str()); - } - - if (ficf == 0) { - ficl = 0; - } - else if (mid == 3) { - ficl = 32; - } - else { - ficl = 24; - } - - // STC - printbuf("STC - Stream Characterisation", 1, NULL, 0); - - for (int i=0; i < nst; i++) { - sprintf(sdesc, "Stream number %d", i); - printbuf("STC - Stream Characterisation", 2, p + 8 + 4*i, 4, sdesc); - scid = (p[8 + 4*i] & 0xFC) >> 2; - sprintf(sdesc, "%d", scid); - printbuf("SCID - Sub-channel Identifier", 3, NULL, 0, sdesc); - sad[i] = (p[8+4*i] & 0x03) * 256 + p[9+4*i]; - sprintf(sdesc, "%d", sad[i]); - printbuf("SAD - Sub-channel Start Address", 3, NULL, 0, sdesc); - tpl = (p[10+4*i] & 0xFC) >> 2; - - if ((tpl & 0x20) >> 5 == 1) { - unsigned char opt, plevel; - string plevelstr; - opt = (tpl & 0x16) >> 2; - plevel = (tpl & 0x03); - if (opt == 0x00) { - if (plevel == 0) - plevelstr = "1-A, 1/4, 16 CUs"; - else if (plevel == 1) - plevelstr = "2-A, 3/8, 8 CUs"; - else if (plevel == 2) - plevelstr = "3-A, 1/2, 6 CUs"; - else if (plevel == 3) - plevelstr = "4-A, 3/4, 4 CUs"; - } - else if (opt == 0x01) { - if (plevel == 0) - plevelstr = "1-B, 4/9, 27 CUs"; - else if (plevel == 1) - plevelstr = "2-B, 4/7, 21 CUs"; - else if (plevel == 2) - plevelstr = "3-B, 4/6, 18 CUs"; - else if (plevel == 3) - plevelstr = "4-B, 4/5, 15 CUs"; - } - else { - stringstream ss; - ss << "Unknown option " << opt; - plevelstr = ss.str(); - } - sprintf(sdesc, "0x%02x - Equal Error Protection. %s", tpl, plevelstr.c_str()); - } - else { - unsigned char tsw, uepidx; - tsw = (tpl & 0x08); - uepidx = tpl & 0x07; - sprintf(sdesc, "0x%02x - Unequal Error Protection. Table switch %d, UEP index %d", tpl, tsw, uepidx); - } - printbuf("TPL - Sub-channel Type and Protection Level", 3, NULL, 0, sdesc); - stl[i] = (p[10+4*i] & 0x03) * 256 + \ - p[11+4*i]; - sprintf(sdesc, "%d => %d kbit/s", stl[i], stl[i]*8/3); - printbuf("STL - Sub-channel Stream Length", 3, NULL, 0, sdesc); - - if (config.streams_to_decode.count(i) > 0) { - config.streams_to_decode[i].set_subchannel_index(stl[i]/3); - config.streams_to_decode[i].set_index(i); - } - } - - // EOH - printbuf("EOH - End Of Header", 1, p + 8 + 4*nst, 4); - unsigned short int mnsc = p[8 + 4*nst] * 256 + \ - p[8 + 4*nst + 1]; - { - stringstream ss; - ss << mnsc; - printbuf("MNSC - Multiplex Network Signalling Channel", 2, p+8+4*nst, 2, ss.str()); - } - - crch = p[8 + 4*nst + 2]*256 + \ - p[8 + 4*nst + 3]; - crc = 0xffff; - - for (int i=4; i < 8 + 4*nst + 2; i++) - crc = update_crc_ccitt(crc, p[i]); - crc =~ crc; - - if (crc == crch) { - sprintf(sdesc,"CRC OK"); - } - else { - sprintf(sdesc,"CRC Mismatch: %02x",crc); - } - - printbuf("Header CRC", 2, p + 8 + 4*nst + 2, 2, sdesc); - - // MST - FIC - if (ficf == 1) { - int endmarker = 0; - int figcount = 0; - unsigned char *fib, *fig; - unsigned short int figcrc; - - FIGalyser figs; - - unsigned char ficdata[32*4]; - memcpy(ficdata, p + 12 + 4*nst, ficl*4); - sprintf(sdesc, "FIC Data (%d bytes)", ficl*4); - //printbuf(sdesc, 1, ficdata, ficl*4); - printbuf(sdesc, 1, NULL, 0); - fib = p + 12 + 4*nst; - for(int i = 0; i < ficl*4/32; i++) { - fig=fib; - figs.set_fib(i); - endmarker=0; - figcount=0; - while (!endmarker) { - unsigned char figtype, figlen; - figtype = (fig[0] & 0xE0) >> 5; - if (figtype != 7) { - figlen = fig[0] & 0x1F; - sprintf(sdesc, "FIG %d [%d bytes]", figtype, figlen); - printbuf(sdesc, 3, fig+1, figlen); - decodeFIG(figs, fig+1, figlen, figtype, 4); - fig += figlen + 1; - figcount += figlen + 1; - if (figcount >= 29) - endmarker = 1; - } - else { - endmarker = 1; - } - } - figcrc = fib[30]*256 + fib[31]; - crc = 0xffff; - for (int j = 0; j < 30; j++) { - crc = update_crc_ccitt(crc, fib[j]); - } - crc =~ crc; - if (crc == figcrc) - sprintf(sdesc,"FIB CRC OK"); - else - sprintf(sdesc,"FIB CRC Mismatch: %02x",crc); - - printbuf("FIB CRC",3,fib+30,2,sdesc); - fib += 32; - } - - if (config.analyse_fic_carousel) { - figs.analyse(); - } - } - - int offset = 0; - for (int i=0; i < nst; i++) { - unsigned char streamdata[684*8]; - memcpy(streamdata, p + 12 + 4*nst + ficf*ficl*4 + offset, stl[i]*8); - offset += stl[i] * 8; - if (config.streams_to_decode.count(i) > 0) { - sprintf(sdesc, "id %d, len %d, selected for decoding", i, stl[i]*8); - } - else { - sprintf(sdesc, "id %d, len %d, not selected for decoding", i, stl[i]*8); - } - if (verbosity > 1) { - printbuf("Stream Data", 1, streamdata, stl[i]*8, sdesc); - } - else { - printbuf("Stream Data", 1, streamdata, 0, sdesc); - } - - if (config.streams_to_decode.count(i) > 0) { - config.streams_to_decode[i].push(streamdata, stl[i]*8); - } - - } - - // EOF - crch = p[12 + 4*nst + ficf*ficl*4 + offset] * 256 + \ - p[12 + 4*nst + ficf*ficl*4 + offset + 1]; - - crc = 0xffff; - - for (int i = 12 + 4*nst; i < 12 + 4*nst + ficf*ficl*4 + offset; i++) - crc = update_crc_ccitt(crc, p[i]); - crc =~ crc; - if (crc == crch) - sprintf(sdesc, "CRC OK"); - else - sprintf(sdesc, "CRC Mismatch: %02x", crc); - - printbuf("EOF", 1, p + 12 + 4*nst + ficf*ficl*4 + offset, 4); - printbuf("CRC", 2, p + 12 + 4*nst + ficf*ficl*4 + offset, 2, sdesc); - - //RFU - printbuf("RFU", 2, p + 12 + 4*nst + ficf*ficl*4 + offset + 2, 2); - - //TIST - l1 = (p[12 + 4*nst + ficf*ficl*4 + offset + 5] & 0xfe) >> 1; - sprintf(sdesc, "%d ms", l1*8); - printbuf("TIST - Time Stamp", 1, p+12+4*nst+ficf*ficl*4+offset+4, 4, sdesc); - - - if (verbosity) { - printf("-------------------------------------------------------------------------------------------------------------\n"); - } - } - - - std::map<int, DabPlusSnoop>::iterator it; - for (it = config.streams_to_decode.begin(); - it != config.streams_to_decode.end(); - ++it) { - it->second.close(); - } - - return 0; -} - -void decodeFIG(FIGalyser &figs, - unsigned char* f, - unsigned char figlen, - unsigned short int figtype, - unsigned short int indent) -{ - char desc[256]; - - switch (figtype) { - case 0: - { - unsigned short int ext,cn,oe,pd; - - cn = (f[0] & 0x80) >> 7; - oe = (f[0] & 0x40) >> 6; - pd = (f[0] & 0x20) >> 5; - ext = f[0] & 0x1F; - sprintf(desc, "FIG %d/%d: C/N=%d OE=%d P/D=%d", - figtype, ext, cn, oe, pd); - printbuf(desc, indent, f+1, figlen-1); - - figs.push_back(figtype, ext, figlen); - - switch (ext) { - - case 0: // FIG 0/0 - { - unsigned char cid, al, ch, hic, lowc, occ; - unsigned short int eid, eref; - - eid = f[1]*256+f[2]; - cid = (f[1] & 0xF0) >> 4; - eref = (f[1] & 0x0F)*256 + \ - f[2]; - ch = (f[3] & 0xC0) >> 6; - al = (f[3] & 0x20) >> 5; - hic = f[3] & 0x1F; - lowc = f[4]; - if (ch != 0) { - occ = f[5]; - sprintf(desc, - "Ensemble ID=0x%02x (Country id=%d, Ensemble reference=%d), Change flag=%d, Alarm flag=%d, CIF Count=%d/%d, Occurance change=%d", - eid, cid, eref, ch, al, hic, lowc, occ); - } - else { - sprintf(desc, - "Ensemble ID=0x%02x (Country id=%d, Ensemble reference=%d), Change flag=%d, Alarm flag=%d, CIF Count=%d/%d", - eid, cid, eref, ch, al, hic, lowc); - } - printbuf(desc, indent+1, NULL, 0); - - } - break; - case 1: // FIG 0/1 basic subchannel organisation - { - int i = 1; - - while (i < figlen-3) { - // iterate over subchannels - int subch_id = f[i] >> 2; - int start_addr = (f[i] << 6) | - (f[i+1]); - int long_flag = (f[i+2] >> 7); - - if (long_flag) { - int option = (f[i+2] >> 4) & 0x07; - int protection_level = (f[i+2] >> 2) & 0x03; - int subchannel_size = ((f[i+2] & 0x03) << 6 ) | - f[i+3]; - - i += 4; - - if (option == 0x00) { - sprintf(desc, - "Subch 0x%x, start_addr %d, long, EEP %d-A, subch size %d", - subch_id, start_addr, protection_level, subchannel_size); - } - else if (option == 0x01) { - sprintf(desc, - "Subch 0x%x, start_addr %d, long, EEP %d-B, subch size %d", - subch_id, start_addr, protection_level, subchannel_size); - } - else { - sprintf(desc, - "Subch 0x%x, start_addr %d, long, invalid option %d, protection %d, subch size %d", - subch_id, start_addr, option, protection_level, subchannel_size); - } - } - else { - int table_switch = (f[i+2] >> 6) & 0x01; - unsigned int table_index = (f[i+2] & 0x3F); - - - if (table_switch == 0) { - sprintf(desc, - "Subch 0x%x, start_addr %d, short, table index %d", - subch_id, start_addr, table_index); - } - else { - sprintf(desc, - "Subch 0x%x, start_addr %d, short, invalid table_switch(=1), table index %d", - subch_id, start_addr, table_index); - } - - i += 3; - } - printbuf(desc, indent+1, NULL, 0); - } - - } - case 2: // FIG 0/2 - { - unsigned short int sref, sid; - unsigned char cid, ecc, local, caid, ncomp, timd, ps, ca, subchid, scty; - int k=1; - string psdesc; - char sctydesc[32]; - - while (k<figlen) { - if (pd == 0) { - sid = f[k] * 256 + f[k+1]; - cid = (f[k] & 0xF0) >> 4; - sref = (f[k] & 0x0F) * 256 + f[k+1]; - k += 2; - } - else { - sid = f[k] * 256 * 256 * 256 + \ - f[k+1] * 256 * 256 + \ - f[k+2] * 256 + \ - f[k+3]; - - ecc = f[k]; - cid = (f[k+1] & 0xF0) >> 4; - sref = (f[k+1] & 0x0F) * 256 * 256 + \ - f[k+2] * 256 + \ - f[k+3]; - - k += 4; - } - - local = (f[k] & 0x80) >> 7; - caid = (f[k] & 0x70) >> 4; - ncomp = f[k] & 0x0F; - - if (pd == 0) - sprintf(desc, - "Service ID=0x%02X (Country id=%d, Service reference=%d), Number of components=%d, Local flag=%d, CAID=%d", - sid, cid, sref, ncomp, local, caid); - else - sprintf(desc, - "Service ID=0x%02X (ECC=%d, Country id=%d, Service reference=%d), Number of components=%d, Local flag=%d, CAID=%d", - sid, ecc, cid, sref, ncomp, local, caid); - printbuf(desc, indent+1, NULL, 0); - - k++; - for (int i=0; i<ncomp; i++) { - unsigned char scomp[2]; - - memcpy(scomp, f+k, 2); - sprintf(desc, "Component[%d]", i); - printbuf(desc, indent+2, scomp, 2, ""); - timd = (scomp[0] & 0xC0) >> 6; - ps = (scomp[1] & 0x02) >> 1; - ca = scomp[1] & 0x01; - scty = scomp[0] & 0x3F; - subchid = (scomp[1] & 0xFC) >> 2; - - /* useless, kept as reference - if (timd == 3) { - unsigned short int scid; - scid = scty*64 + subchid; - } - */ - - if (ps == 0) { - psdesc = "Secondary service"; - } - else { - psdesc = "Primary service"; - } - - - if (timd == 0) { - //MSC stream audio - if (scty == 0) - sprintf(sctydesc, "MPEG Foreground sound (%d)", scty); - else if (scty == 1) - sprintf(sctydesc, "MPEG Background sound (%d)", scty); - else if (scty == 2) - sprintf(sctydesc, "Multi Channel sound (%d)", scty); - else if (scty == 63) - sprintf(sctydesc, "AAC sound (%d)", scty); - else - sprintf(sctydesc, "Unknown ASCTy (%d)", scty); - - sprintf(desc, "Stream audio mode, %s, %s, SubChannel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca); - printbuf(desc, indent+3, NULL, 0); - } - else if (timd == 1) { - // MSC stream data - sprintf(sctydesc, "DSCTy=%d", scty); - sprintf(desc, "Stream data mode, %s, %s, SubChannel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca); - printbuf(desc, indent+3, NULL, 0); - } - else if (timd == 2) { - // FIDC - sprintf(sctydesc, "DSCTy=%d", scty); - sprintf(desc, "FIDC mode, %s, %s, Fast Information Data Channel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca); - printbuf(desc, indent+3, NULL, 0); - } - else if (timd == 3) { - // MSC Packet mode - sprintf(desc, "MSC Packet Mode, %s, Service Component ID=%02X, CA=%d", psdesc.c_str(), subchid, ca); - printbuf(desc, indent+3, NULL, 0); - } - k += 2; - } - } - } - break; - case 13: // FIG 0/13 - { - uint32_t SId; - uint8_t SCIdS; - uint8_t No; - - int k = 1; - - if (pd == 0) { // Programme services, 16 bit SId - SId = (f[k] << 8) | - f[k+1]; - k+=2; - - SCIdS = f[k] >> 4; - No = f[k] & 0x0F; - k++; - } - else { // Data services, 32 bit SId - SId = (f[k] << 24) | - (f[k+1] << 16) | - (f[k+2] << 8) | - f[k+3]; - k+=4; - - SCIdS = f[k] >> 4; - No = f[k] & 0x0F; - k++; - - } - - sprintf(desc, "FIG %d/%d: SId=%u SCIdS=%u No=%u", - figtype, ext, SId, SCIdS, No); - printbuf(desc, indent+1, NULL, 0); - - for (int numapp = 0; numapp < No; numapp++) { - uint16_t user_app_type = ((f[k] << 8) | - (f[k+1] & 0xE0)) >> 5; - uint8_t user_app_len = f[k+1] & 0x1F; - k+=2; - - sprintf(desc, "User Application %d '%s'; length %u", - user_app_type, - get_fig_0_13_userapp(user_app_type).c_str(), - user_app_len); - printbuf(desc, indent+2, NULL, 0); - } - } - break; - } - } - break; - - case 1: - {// SHORT LABELS - unsigned short int ext,oe,charset; - unsigned short int flag; - char label[17]; - - charset = (f[0] & 0xF0) >> 4; - oe = (f[0] & 0x08) >> 3; - ext = f[0] & 0x07; - sprintf(desc, - "FIG %d/%d: OE=%d, Charset=%d", - figtype, ext, oe, charset); - - printbuf(desc, indent, f+1, figlen-1); - memcpy(label, f+figlen-18, 16); - label[16] = 0x00; - flag = f[figlen-2] * 256 + \ - f[figlen-1]; - - figs.push_back(figtype, ext, figlen); - - switch (ext) { - case 0: - { // ENSEMBLE LABEL - unsigned short int eid; - eid = f[1] * 256 + f[2]; - sprintf(desc, "Ensemble ID 0x%04X label: \"%s\", Short label mask: 0x%04X", eid, label, flag); - printinfo(desc, indent+1); - } - break; - - case 1: - { // Programme LABEL - unsigned short int sid; - sid = f[1] * 256 + f[2]; - sprintf(desc, "Service ID 0x%04X label: \"%s\", Short label mask: 0x%04X", sid, label, flag); - printinfo(desc, indent+1); - } - break; - - case 4: - { // Service Component LABEL - unsigned int sid; - unsigned char pd, SCIdS; - pd = (f[1] & 0x80) >> 7; - SCIdS = f[1] & 0x0F; - if (pd == 0) { - sid = f[2] * 256 + \ - f[3]; - } - else { - sid = f[2] * 256 * 256 * 256 + \ - f[3] * 256 * 256 + \ - f[4] * 256 + \ - f[5]; - } - sprintf(desc, - "Service ID 0x%08X , Service Component ID 0x%04X Short, label: \"%s\", label mask: 0x%04X", - sid, SCIdS, label, flag); - printinfo(desc, indent+1); - } - break; - - case 5: - { // Data Service LABEL - unsigned int sid; - sid = f[1] * 256 * 256 * 256 + \ - f[2] * 256 * 256 + \ - f[3] * 256 + \ - f[4]; - - sprintf(desc, - "Service ID 0x%08X label: \"%s\", Short label mask: 0x%04X", - sid, label, flag); - printinfo(desc, indent+1); - } - break; - - - case 6: - { // X-PAD User Application label - unsigned int sid; - unsigned char pd, SCIdS, xpadapp; - string xpadappdesc; - - pd = (f[1] & 0x80) >> 7; - SCIdS = f[1] & 0x0F; - if (pd == 0) { - sid = f[2] * 256 + \ - f[3]; - xpadapp = f[4] & 0x1F; - } - else { - sid = f[2] * 256 * 256 * 256 + \ - f[3] * 256 * 256 + \ - f[4] * 256 + \ - f[5]; - xpadapp = f[6] & 0x1F; - } - - if (xpadapp == 2) { - xpadappdesc = "DLS"; - } - else if (xpadapp == 12) { - xpadappdesc = "MOT"; - } - else { - xpadappdesc = "?"; - } - - - sprintf(desc,"Service ID 0x%08X , Service Component ID 0x%04X Short, X-PAD App %02X (%s), label: \"%s\", label mask: 0x%04X", - sid, SCIdS, xpadapp, xpadappdesc.c_str(), label, flag); - printbuf(desc,indent+1,NULL,0,""); - } - break; - } - } - break; - case 2: - {// LONG LABELS - unsigned short int ext,oe; - - uint8_t toggle_flag = (f[0] & 0x80) >> 7; - uint8_t segment_index = (f[0] & 0x70) >> 4; - oe = (f[0] & 0x08) >> 3; - ext = f[0] & 0x07; - sprintf(desc, - "FIG %d/%d: OE=%d, Segment_index=%d", - figtype, ext, oe, segment_index); - - printbuf(desc, indent, f+1, figlen-1); - - figs.push_back(figtype, ext, figlen); - } - break; - case 5: - {// FIDC - unsigned short int ext; - - uint8_t d1 = (f[0] & 0x80) >> 7; - uint8_t d2 = (f[0] & 0x40) >> 6; - uint8_t tcid = (f[0] & 0x38) >> 5; - ext = f[0] & 0x07; - sprintf(desc, - "FIG %d/%d: D1=%d, D2=%d, TCId=%d", - figtype, ext, d1, d2, tcid); - - printbuf(desc, indent, f+1, figlen-1); - - figs.push_back(figtype, ext, figlen); - } - break; - case 6: - {// Conditional access - fprintf(stderr, "ERROR: ETI contains unsupported FIG 6"); - } - break; - } -} - - -void printinfo(string header, - int indent_level, - int min_verb) -{ - if (verbosity >= min_verb) { - for (int i = 0; i < indent_level; i++) { - printf("\t"); - } - printf("%s\n", header.c_str()); - } -} - -void printbuf(string header, - int indent_level, - unsigned char* buffer, - size_t size, - string desc) -{ - if (verbosity > 0) { - for (int i = 0; i < indent_level; i++) { - printf("\t"); - } - - printf("%s", header.c_str()); - - if (verbosity > 1) { - if (size != 0) { - printf(": "); - } - - for (size_t i = 0; i < size; i++) { - printf("%02x ", buffer[i]); - } - } - - if (desc != "") { - printf(" [%s] ", desc.c_str()); - } - - printf("\n"); - } -} - - diff --git a/etisnoop/faad_decoder.cpp b/etisnoop/faad_decoder.cpp deleted file mode 100644 index f489c1d..0000000 --- a/etisnoop/faad_decoder.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - faad_decoder.cpp - Use libfaad to decode the AAC content of the DAB+ subchannel - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -#include "faad_decoder.h" -#include "wavfile.h" -#include "utils.h" -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <cassert> -#include <string> -#include <sstream> -#include <vector> - -using namespace std; - -FaadDecoder::FaadDecoder() : - m_data_len(0), - m_fd(NULL), - m_aac(NULL), - m_initialised(false) -{ -} - -void FaadDecoder::open(string filename, bool ps_flag, bool aac_channel_mode, - bool dac_rate, bool sbr_flag, int mpeg_surround_config) -{ - m_filename = filename; - m_ps_flag = ps_flag; - m_aac_channel_mode = aac_channel_mode; - m_dac_rate = dac_rate; - m_sbr_flag = sbr_flag; - m_mpeg_surround_config = mpeg_surround_config; - - stringstream ss; - ss << filename << ".aac"; - - m_aac = fopen(ss.str().c_str(), "w"); -} - -bool FaadDecoder::decode(vector<vector<uint8_t> > aus) -{ - /* ADTS header creation taken from SDR-J */ - adts_fixed_header fh; - adts_variable_header vh; - - fh.syncword = 0xfff; - fh.id = 0; - fh.layer = 0; - fh.protection_absent = 1; - fh.profile_objecttype = 0; // aac main - 1 - fh.private_bit = 0; // ignored when decoding - fh.original_copy = 0; - fh.home = 0; - vh.copyright_id_bit = 0; - vh.copyright_id_start = 0; - vh.adts_buffer_fullness = 1999; // ? according to OpenDab - vh.no_raw_data_blocks = 0; - - uint8_t d_header[10]; - d_header[0] = fh.syncword >> 4; - d_header[1] = (fh.syncword & 0xf) << 4; - d_header[1] |= fh.id << 3; - d_header[1] |= fh.layer << 1; - d_header[1] |= fh.protection_absent; - d_header[2] = fh.profile_objecttype << 6; - // sampling frequency index filled in dynamically - d_header[2] |= fh.private_bit << 1; - // channel configuration filled in dynamically - d_header[3] = fh.original_copy << 5; - d_header[3] |= fh.home << 4; - d_header[3] |= vh.copyright_id_bit << 3; - d_header[3] |= vh.copyright_id_start << 2; - // framelength filled in dynamically - d_header[4] = 0; - d_header[5] = vh.adts_buffer_fullness >> 6; - d_header[6] = (vh.adts_buffer_fullness & 0x3f) << 2; - d_header[6] |= vh.no_raw_data_blocks; - - if (!m_dac_rate && m_sbr_flag) fh.sampling_freq_idx = 8; - // AAC core sampling rate 16 kHz - else if (m_dac_rate && m_sbr_flag) fh.sampling_freq_idx = 6; - // AAC core sampling rate 24 kHz - else if (!m_dac_rate && !m_sbr_flag) fh.sampling_freq_idx = 5; - // AAC core sampling rate 32 kHz - else if (m_dac_rate && !m_sbr_flag) fh.sampling_freq_idx = 3; - // AAC core sampling rate 48 kHz - - setBits (&d_header[2], fh.sampling_freq_idx, 2, 4); - - if (m_mpeg_surround_config == 0) { - if (m_sbr_flag && !m_aac_channel_mode && m_ps_flag) - fh.channel_conf = 2; - else - fh.channel_conf = 1 << (m_aac_channel_mode ? 1 : 0); - } - else if (m_mpeg_surround_config == 1) { - fh.channel_conf = 6; - } - else { - printf("Unrecognized mpeg surround config (ignored)\n"); - return false; - } - - setBits (&d_header[2], fh.channel_conf, 7, 3); - - for (size_t au_ix = 0; au_ix < aus.size(); au_ix++) { - - vector<uint8_t>& au = aus[au_ix]; - - uint8_t helpBuffer[960]; - memset(helpBuffer, 0, sizeof(helpBuffer)); - - // Set length in header (header + au) - vh.aac_frame_length = 7 + au.size(); - setBits(&d_header[3], vh.aac_frame_length, 6, 13); - - memcpy(helpBuffer, d_header, 7 * sizeof(uint8_t)); - memcpy(&helpBuffer[7], - &au[0], au.size() * sizeof (uint8_t)); - - fwrite(helpBuffer, 1, vh.aac_frame_length, m_aac); - - NeAACDecFrameInfo hInfo; - int16_t* outBuffer; - - if (!m_initialised) { - long unsigned samplerate; - unsigned char channels; - - int len; - - if ((len = NeAACDecInit(m_faad_handle.decoder, helpBuffer, - vh.aac_frame_length, &samplerate, &channels)) < 0) - { - /* If some error initializing occured, skip the file */ - printf("Error initializing decoder library (%d).\n", - len); - NeAACDecClose(m_faad_handle.decoder); - return false; - } - - m_initialised = true; - - outBuffer = (int16_t *)NeAACDecDecode( - m_faad_handle.decoder, &hInfo, - helpBuffer + len, vh.aac_frame_length - len ); - } - else { - outBuffer = (int16_t *)NeAACDecDecode( - m_faad_handle.decoder, &hInfo, - helpBuffer, vh.aac_frame_length ); - } - - assert(outBuffer != NULL); - - m_sample_rate = hInfo.samplerate; - m_channels = hInfo.channels; - size_t samples = hInfo.samples; - -#if 0 - printf("bytes consumed %d\n", (int)(hInfo.bytesconsumed)); - printf("samplerate = %d, samples = %zu, channels = %d," - " error = %d, sbr = %d\n", m_sample_rate, samples, - m_channels, hInfo.error, hInfo.sbr); - printf("header = %d\n", hInfo.header_type); -#endif - - if (hInfo.error != 0) { - printf("FAAD Warning: %s\n", - faacDecGetErrorMessage(hInfo.error)); - return false; - } - - if (m_fd == NULL) { - stringstream ss; - ss << m_filename << ".wav"; - m_fd = wavfile_open(ss.str().c_str(), m_sample_rate); - } - - if (samples) { - if (m_channels == 1) { - int16_t *buffer = (int16_t *)alloca (2 * samples); - size_t i; - for (i = 0; i < samples; i ++) { - buffer [2 * i] = ((int16_t *)outBuffer) [i]; - buffer [2 * i + 1] = buffer [2 * i]; - } - wavfile_write(m_fd, buffer, 2*samples); - } - else if (m_channels == 2) { - wavfile_write(m_fd, outBuffer, samples); - } - else { - printf("Cannot handle %d channels\n", m_channels); - } - } - - } - return true; -} - -void FaadDecoder::close() -{ - if (m_initialised) { - wavfile_close(m_fd); - } -} - - diff --git a/etisnoop/faad_decoder.h b/etisnoop/faad_decoder.h deleted file mode 100644 index 36e7594..0000000 --- a/etisnoop/faad_decoder.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - faad_decoder.h - Use libfaad to decode the AAC content of the DAB+ subchannel - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <string> -#include <sstream> -#include <vector> -#include <neaacdec.h> - -#ifndef __FAAD_DECODER_H_ -#define __FAAD_DECODER_H_ - -struct adts_fixed_header { - unsigned int syncword :12; - unsigned int id :1; - unsigned int layer :2; - unsigned int protection_absent :1; - unsigned int profile_objecttype :2; - unsigned int sampling_freq_idx :4; - unsigned int private_bit :1; - unsigned int channel_conf :3; - unsigned int original_copy :1; - unsigned int home :1; -}; - -struct adts_variable_header { - unsigned int copyright_id_bit :1; - unsigned int copyright_id_start :1; - unsigned int aac_frame_length :13; - unsigned int adts_buffer_fullness :11; - unsigned int no_raw_data_blocks :2; -}; - -class FaadHandle -{ - public: - FaadHandle() - { - decoder = NeAACDecOpen(); - } - - FaadHandle(const FaadHandle& other) - { - this->decoder = NeAACDecOpen(); - } - - FaadHandle& operator=(const FaadHandle& other) - { - this->decoder = NeAACDecOpen(); - return *this; - } - - ~FaadHandle() - { - NeAACDecClose(decoder); - decoder = NULL; - } - - NeAACDecHandle decoder; -}; - -class FaadDecoder -{ - public: - FaadDecoder(); - - void open(std::string filename, bool ps_flag, bool aac_channel_mode, - bool dac_rate, bool sbr_flag, int mpeg_surround_config); - - void close(void); - - bool decode(std::vector<std::vector<uint8_t> > aus); - - bool is_initialised(void) { return m_initialised; } - - private: - void update_header(void); - size_t m_data_len; - - std::string m_filename; - FILE* m_fd; - FILE* m_aac; - - /* Data needed for FAAD */ - bool m_ps_flag; - bool m_aac_channel_mode; - bool m_dac_rate; - bool m_sbr_flag; - int m_mpeg_surround_config; - - int m_channels; - int m_sample_rate; - - bool m_initialised; - FaadHandle m_faad_handle; -}; - -#endif - diff --git a/etisnoop/firecode.c b/etisnoop/firecode.c deleted file mode 100644 index 47ee976..0000000 --- a/etisnoop/firecode.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - firecode.c - Implement a FireCode CRC calculator - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -#include "firecode.h" - -uint16_t firecode_crc(uint8_t* buf, size_t size) -{ - int crc; - int gen_poly; - - crc = 0x0000; - gen_poly = 0x782F; // 0111 1000 0010 1111 (16, 14, 13, 12, 11, 5, 3, 2, 1, 0) - - for (size_t len = 0; len < size; len++) { - for (int i = 0x80; i != 0; i >>= 1) { - if ((crc & 0x8000) != 0) { - crc = (crc << 1) ^ gen_poly; - } - else { - crc = crc << 1; - } - if ((buf[len] & i) != 0) { - crc ^= gen_poly; - } - } - } - - return crc & 0xFFFF; -} - diff --git a/etisnoop/firecode.h b/etisnoop/firecode.h deleted file mode 100644 index 735a302..0000000 --- a/etisnoop/firecode.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org) - - This program 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. - - This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - - firecode.c - Implement a FireCode CRC calculator - - Authors: - Matthias P. Braendli <matthias@mpb.li> -*/ - -#ifndef _FIRECODE_H_ -#define _FIRECODE_H_ - -#include <stdint.h> -#include <stdlib.h> - -uint16_t firecode_crc(uint8_t* buf, size_t size); - -#endif - diff --git a/etisnoop/lib_crc.c b/etisnoop/lib_crc.c deleted file mode 100644 index 26b6b99..0000000 --- a/etisnoop/lib_crc.c +++ /dev/null @@ -1,459 +0,0 @@ -#include "lib_crc.h" - - - - /*******************************************************************\ - * * - * Library : lib_crc * - * File : lib_crc.c * - * Author : Lammert Bies 1999-2008 * - * E-mail : info@lammertbies.nl * - * Language : ANSI C * - * * - * * - * Description * - * =========== * - * * - * The file lib_crc.c contains the private and public func- * - * tions used for the calculation of CRC-16, CRC-CCITT and * - * CRC-32 cyclic redundancy values. * - * * - * * - * Dependencies * - * ============ * - * * - * lib_crc.h CRC definitions and prototypes * - * * - * * - * Modification history * - * ==================== * - * * - * Date Version Comment * - * * - * 2008-04-20 1.16 Added CRC-CCITT calculation for Kermit * - * * - * 2007-04-01 1.15 Added CRC16 calculation for Modbus * - * * - * 2007-03-28 1.14 Added CRC16 routine for Sick devices * - * * - * 2005-12-17 1.13 Added CRC-CCITT with initial 0x1D0F * - * * - * 2005-05-14 1.12 Added CRC-CCITT with start value 0 * - * * - * 2005-02-05 1.11 Fixed bug in CRC-DNP routine * - * * - * 2005-02-04 1.10 Added CRC-DNP routines * - * * - * 1999-02-21 1.01 Added FALSE and TRUE mnemonics * - * * - * 1999-01-22 1.00 Initial source * - * * - \*******************************************************************/ - - - - /*******************************************************************\ - * * - * #define P_xxxx * - * * - * The CRC's are computed using polynomials. The coefficients * - * for the algorithms are defined by the following constants. * - * * - \*******************************************************************/ - -#define P_16 0xA001 -#define P_32 0xEDB88320L -#define P_CCITT 0x1021 -#define P_DNP 0xA6BC -#define P_KERMIT 0x8408 -#define P_SICK 0x8005 - - - - /*******************************************************************\ - * * - * static int crc_tab...init * - * static unsigned ... crc_tab...[] * - * * - * The algorithms use tables with precalculated values. This * - * speeds up the calculation dramaticaly. The first time the * - * CRC function is called, the table for that specific calcu- * - * lation is set up. The ...init variables are used to deter- * - * mine if the initialization has taken place. The calculated * - * values are stored in the crc_tab... arrays. * - * * - * The variables are declared static. This makes them invisi- * - * ble for other modules of the program. * - * * - \*******************************************************************/ - -static int crc_tab16_init = FALSE; -static int crc_tab32_init = FALSE; -static int crc_tabccitt_init = FALSE; -static int crc_tabdnp_init = FALSE; -static int crc_tabkermit_init = FALSE; - -static unsigned short crc_tab16[256]; -static unsigned long crc_tab32[256]; -static unsigned short crc_tabccitt[256]; -static unsigned short crc_tabdnp[256]; -static unsigned short crc_tabkermit[256]; - - - - /*******************************************************************\ - * * - * static void init_crc...tab(); * - * * - * Three local functions are used to initialize the tables * - * with values for the algorithm. * - * * - \*******************************************************************/ - -static void init_crc16_tab( void ); -static void init_crc32_tab( void ); -static void init_crcccitt_tab( void ); -static void init_crcdnp_tab( void ); -static void init_crckermit_tab( void ); - - - - /*******************************************************************\ - * * - * unsigned short update_crc_ccitt( unsigned long crc, char c ); * - * * - * The function update_crc_ccitt calculates a new CRC-CCITT * - * value based on the previous value of the CRC and the next * - * byte of the data to be checked. * - * * - \*******************************************************************/ - -unsigned short update_crc_ccitt( unsigned short crc, char c ) { - - unsigned short tmp, short_c; - - short_c = 0x00ff & (unsigned short) c; - - if ( ! crc_tabccitt_init ) init_crcccitt_tab(); - - tmp = (crc >> 8) ^ short_c; - crc = (crc << 8) ^ crc_tabccitt[tmp]; - - return crc; - -} /* update_crc_ccitt */ - - - - /*******************************************************************\ - * * - * unsigned short update_crc_sick( * - * unsigned long crc, char c, char prev_byte ); * - * * - * The function update_crc_sick calculates a new CRC-SICK * - * value based on the previous value of the CRC and the next * - * byte of the data to be checked. * - * * - \*******************************************************************/ - -unsigned short update_crc_sick( unsigned short crc, char c, char prev_byte ) { - - unsigned short short_c, short_p; - - short_c = 0x00ff & (unsigned short) c; - short_p = ( 0x00ff & (unsigned short) prev_byte ) << 8; - - if ( crc & 0x8000 ) crc = ( crc << 1 ) ^ P_SICK; - else crc = crc << 1; - - crc &= 0xffff; - crc ^= ( short_c | short_p ); - - return crc; - -} /* update_crc_sick */ - - - - /*******************************************************************\ - * * - * unsigned short update_crc_16( unsigned short crc, char c ); * - * * - * The function update_crc_16 calculates a new CRC-16 value * - * based on the previous value of the CRC and the next byte * - * of the data to be checked. * - * * - \*******************************************************************/ - -unsigned short update_crc_16( unsigned short crc, char c ) { - - unsigned short tmp, short_c; - - short_c = 0x00ff & (unsigned short) c; - - if ( ! crc_tab16_init ) init_crc16_tab(); - - tmp = crc ^ short_c; - crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ]; - - return crc; - -} /* update_crc_16 */ - - - - /*******************************************************************\ - * * - * unsigned short update_crc_kermit( unsigned short crc, char c ); * - * * - * The function update_crc_kermit calculates a new CRC value * - * based on the previous value of the CRC and the next byte * - * of the data to be checked. * - * * - \*******************************************************************/ - -unsigned short update_crc_kermit( unsigned short crc, char c ) { - - unsigned short tmp, short_c; - - short_c = 0x00ff & (unsigned short) c; - - if ( ! crc_tabkermit_init ) init_crckermit_tab(); - - tmp = crc ^ short_c; - crc = (crc >> 8) ^ crc_tabkermit[ tmp & 0xff ]; - - return crc; - -} /* update_crc_kermit */ - - - - /*******************************************************************\ - * * - * unsigned short update_crc_dnp( unsigned short crc, char c ); * - * * - * The function update_crc_dnp calculates a new CRC-DNP value * - * based on the previous value of the CRC and the next byte * - * of the data to be checked. * - * * - \*******************************************************************/ - -unsigned short update_crc_dnp( unsigned short crc, char c ) { - - unsigned short tmp, short_c; - - short_c = 0x00ff & (unsigned short) c; - - if ( ! crc_tabdnp_init ) init_crcdnp_tab(); - - tmp = crc ^ short_c; - crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ]; - - return crc; - -} /* update_crc_dnp */ - - - - /*******************************************************************\ - * * - * unsigned long update_crc_32( unsigned long crc, char c ); * - * * - * The function update_crc_32 calculates a new CRC-32 value * - * based on the previous value of the CRC and the next byte * - * of the data to be checked. * - * * - \*******************************************************************/ - -unsigned long update_crc_32( unsigned long crc, char c ) { - - unsigned long tmp, long_c; - - long_c = 0x000000ffL & (unsigned long) c; - - if ( ! crc_tab32_init ) init_crc32_tab(); - - tmp = crc ^ long_c; - crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ]; - - return crc; - -} /* update_crc_32 */ - - - - /*******************************************************************\ - * * - * static void init_crc16_tab( void ); * - * * - * The function init_crc16_tab() is used to fill the array * - * for calculation of the CRC-16 with values. * - * * - \*******************************************************************/ - -static void init_crc16_tab( void ) { - - int i, j; - unsigned short crc, c; - - for (i=0; i<256; i++) { - - crc = 0; - c = (unsigned short) i; - - for (j=0; j<8; j++) { - - if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_16; - else crc = crc >> 1; - - c = c >> 1; - } - - crc_tab16[i] = crc; - } - - crc_tab16_init = TRUE; - -} /* init_crc16_tab */ - - - - /*******************************************************************\ - * * - * static void init_crckermit_tab( void ); * - * * - * The function init_crckermit_tab() is used to fill the array * - * for calculation of the CRC Kermit with values. * - * * - \*******************************************************************/ - -static void init_crckermit_tab( void ) { - - int i, j; - unsigned short crc, c; - - for (i=0; i<256; i++) { - - crc = 0; - c = (unsigned short) i; - - for (j=0; j<8; j++) { - - if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_KERMIT; - else crc = crc >> 1; - - c = c >> 1; - } - - crc_tabkermit[i] = crc; - } - - crc_tabkermit_init = TRUE; - -} /* init_crckermit_tab */ - - - - /*******************************************************************\ - * * - * static void init_crcdnp_tab( void ); * - * * - * The function init_crcdnp_tab() is used to fill the array * - * for calculation of the CRC-DNP with values. * - * * - \*******************************************************************/ - -static void init_crcdnp_tab( void ) { - - int i, j; - unsigned short crc, c; - - for (i=0; i<256; i++) { - - crc = 0; - c = (unsigned short) i; - - for (j=0; j<8; j++) { - - if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_DNP; - else crc = crc >> 1; - - c = c >> 1; - } - - crc_tabdnp[i] = crc; - } - - crc_tabdnp_init = TRUE; - -} /* init_crcdnp_tab */ - - - - /*******************************************************************\ - * * - * static void init_crc32_tab( void ); * - * * - * The function init_crc32_tab() is used to fill the array * - * for calculation of the CRC-32 with values. * - * * - \*******************************************************************/ - -static void init_crc32_tab( void ) { - - int i, j; - unsigned long crc; - - for (i=0; i<256; i++) { - - crc = (unsigned long) i; - - for (j=0; j<8; j++) { - - if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ P_32; - else crc = crc >> 1; - } - - crc_tab32[i] = crc; - } - - crc_tab32_init = TRUE; - -} /* init_crc32_tab */ - - - - /*******************************************************************\ - * * - * static void init_crcccitt_tab( void ); * - * * - * The function init_crcccitt_tab() is used to fill the array * - * for calculation of the CRC-CCITT with values. * - * * - \*******************************************************************/ - -static void init_crcccitt_tab( void ) { - - int i, j; - unsigned short crc, c; - - for (i=0; i<256; i++) { - - crc = 0; - c = ((unsigned short) i) << 8; - - for (j=0; j<8; j++) { - - if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT; - else crc = crc << 1; - - c = c << 1; - } - - crc_tabccitt[i] = crc; - } - - crc_tabccitt_init = TRUE; - -} /* init_crcccitt_tab */ diff --git a/etisnoop/lib_crc.h b/etisnoop/lib_crc.h deleted file mode 100644 index ea5ca76..0000000 --- a/etisnoop/lib_crc.h +++ /dev/null @@ -1,66 +0,0 @@ - /*******************************************************************\ - * * - * Library : lib_crc * - * File : lib_crc.h * - * Author : Lammert Bies 1999-2008 * - * E-mail : info@lammertbies.nl * - * Language : ANSI C * - * * - * * - * Description * - * =========== * - * * - * The file lib_crc.h contains public definitions and proto- * - * types for the CRC functions present in lib_crc.c. * - * * - * * - * Dependencies * - * ============ * - * * - * none * - * * - * * - * Modification history * - * ==================== * - * * - * Date Version Comment * - * * - * 2008-04-20 1.16 Added CRC-CCITT routine for Kermit * - * * - * 2007-04-01 1.15 Added CRC16 calculation for Modbus * - * * - * 2007-03-28 1.14 Added CRC16 routine for Sick devices * - * * - * 2005-12-17 1.13 Added CRC-CCITT with initial 0x1D0F * - * * - * 2005-02-14 1.12 Added CRC-CCITT with initial 0x0000 * - * * - * 2005-02-05 1.11 Fixed bug in CRC-DNP routine * - * * - * 2005-02-04 1.10 Added CRC-DNP routines * - * * - * 2005-01-07 1.02 Changes in tst_crc.c * - * * - * 1999-02-21 1.01 Added FALSE and TRUE mnemonics * - * * - * 1999-01-22 1.00 Initial source * - * * - \*******************************************************************/ - - - -#define CRC_VERSION "1.16" - - - -#define FALSE 0 -#define TRUE 1 - - - -unsigned short update_crc_16( unsigned short crc, char c ); -unsigned long update_crc_32( unsigned long crc, char c ); -unsigned short update_crc_ccitt( unsigned short crc, char c ); -unsigned short update_crc_dnp( unsigned short crc, char c ); -unsigned short update_crc_kermit( unsigned short crc, char c ); -unsigned short update_crc_sick( unsigned short crc, char c, char prev_byte ); diff --git a/etisnoop/utils.h b/etisnoop/utils.h deleted file mode 100644 index 9c43c88..0000000 --- a/etisnoop/utils.h +++ /dev/null @@ -1,32 +0,0 @@ -#include <stdlib.h> -#include <stdint.h> - -#ifndef __UTILS_H_ -#define __UTILS_H_ -static inline -void setBit(uint8_t x [], uint8_t bit, int32_t pos) -{ - int16_t iByte; - int16_t iBit; - - iByte = pos / 8; - iBit = pos % 8; - x[iByte] = (x[iByte] & (~(1 << (7 - iBit)))) | - (bit << (7 - iBit)); -} - -static inline -void setBits(uint8_t x[], uint32_t bits, - int32_t startPosition, int32_t numBits) -{ - int32_t i; - uint8_t bit; - - for (i = 0; i < numBits; i ++) { - bit = bits & (1 << (numBits - i - 1)) ? 1 : 0; - setBit(x, bit, startPosition + i); - } -} - -#endif - diff --git a/etisnoop/wavfile.c b/etisnoop/wavfile.c deleted file mode 100644 index 9ea231b..0000000 --- a/etisnoop/wavfile.c +++ /dev/null @@ -1,82 +0,0 @@ -/* -A simple sound library for CSE 20211 by Douglas Thain -For course assignments, you should not change this file. -For complete documentation, see: -http://www.nd.edu/~dthain/courses/cse20211/fall2013/wavfile -*/ - -#include "wavfile.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -struct wavfile_header { - char riff_tag[4]; - int riff_length; - char wave_tag[4]; - char fmt_tag[4]; - int fmt_length; - short audio_format; - short num_channels; - int sample_rate; - int byte_rate; - short block_align; - short bits_per_sample; - char data_tag[4]; - int data_length; -}; - -FILE * wavfile_open( const char *filename, int rate ) -{ - struct wavfile_header header; - - int samples_per_second = rate; - int bits_per_sample = 16; - - strncpy(header.riff_tag,"RIFF",4); - strncpy(header.wave_tag,"WAVE",4); - strncpy(header.fmt_tag,"fmt ",4); - strncpy(header.data_tag,"data",4); - - header.riff_length = 0; - header.fmt_length = 16; - header.audio_format = 1; - header.num_channels = 2; - header.sample_rate = samples_per_second; - header.byte_rate = samples_per_second*(bits_per_sample/8); - header.block_align = bits_per_sample/8; - header.bits_per_sample = bits_per_sample; - header.data_length = 0; - - FILE * file = fopen(filename,"w+"); - if(!file) return 0; - - fwrite(&header,sizeof(header),1,file); - - fflush(file); - - return file; - -} - -void wavfile_write( FILE *file, short data[], int length ) -{ - fwrite(data,sizeof(short),length,file); -} - -void wavfile_close( FILE *file ) -{ - int file_length = ftell(file); - - int data_length = file_length - sizeof(struct wavfile_header); - fseek(file,sizeof(struct wavfile_header) - sizeof(int),SEEK_SET); - fwrite(&data_length,sizeof(data_length),1,file); - - int riff_length = file_length - 8; - fseek(file,4,SEEK_SET); - fwrite(&riff_length,sizeof(riff_length),1,file); - - fclose(file); -} - diff --git a/etisnoop/wavfile.h b/etisnoop/wavfile.h deleted file mode 100644 index f7ba379..0000000 --- a/etisnoop/wavfile.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -A simple sound library for CSE 20211 by Douglas Thain -For course assignments, you should not change this file. -For complete documentation, see: -http://www.nd.edu/~dthain/courses/cse20211/fall2013/wavfile -*/ - -#ifndef WAVFILE_H -#define WAVFILE_H - -#include <stdio.h> -#include <inttypes.h> - -FILE * wavfile_open( const char *filename, int rate ); -void wavfile_write( FILE *file, short data[], int length ); -void wavfile_close( FILE * file ); - -#endif |