aboutsummaryrefslogtreecommitdiffstats
path: root/src/etisnoop.cpp
blob: 4c0645f45c81864d362830cf4ff86534d7ca00a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)
    Copyright (C) 2017 Matthias P. Braendli (http://www.opendigitalradio.org)
    Copyright (C) 2015 Data Path

    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>
                   / |  |-  ')|)  |-|_ _ (|,_   .|  _  ,_ \
         Data Path \(|(||_(|/_| (||_||(a)_||||(|||.(_()|||/

*/


#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#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 <cinttypes>
#include <string>
#include <boost/regex.hpp>
#include <sstream>
#include <time.h>
#include <signal.h>

#include "etianalyse.hpp"
#include "dabplussnoop.hpp"
#include "utils.hpp"
#include "etiinput.hpp"
#include "figs.hpp"
#include "watermarkdecoder.hpp"
#include "repetitionrate.hpp"
#include "figalyser.hpp"

using namespace std;

const char *get_programme_type_str(size_t int_table_Id, size_t pty);
int sprintfMJD(char *dst, int mjd);

static void handle_signal(int)
{
    quit.store(true);
}

#define no_argument 0
#define required_argument 1
#define optional_argument 2
const struct option longopts[] = {
    {"analyse-figs",       no_argument,        0, 'f'},
    {"help",               no_argument,        0, 'h'},
    {"ignore-error",       no_argument,        0, 'e'},
    {"input-file",         no_argument,        0, 'i'},
    {"statistics",         no_argument,        0, 's'},
    {"verbose",            no_argument,        0, 'v'},
    {"decode-stream",      required_argument,  0, 'd'},
    {"filter-fig",         required_argument,  0, 'F'},
    {"input",              required_argument,  0, 'i'}
};

void usage(void)
{
    fprintf(stderr,
            "Opendigitalradio ETISnoop analyser %s compiled at %s, %s\n\n"
            "The ETISnoop analyser decodes and prints out a RAW ETI file in a\n"
            "form that makes analysis easier.\n"
            "\n"
            "  http://www.opendigitalradio.org\n"
            "\n"
            "Usage: etisnoop [options] [-i filename]\n"
            "\n"
            "   -v      increase verbosity (can be given more than once)\n"
            "   -d N    decode subchannel N into .msc file and if DAB+, decode to .wav file\n"
            "   -s      statistic mode: decode all subchannels and measure audio level\n"
            "   -f      analyse FIC carousel\n"
            "   -r      analyse FIG rates in FIGs per second\n"
            "   -R      analyse FIG rates in frames per FIG\n"
            "   -w      decode CRC-DABMUX and ODR-DabMux watermark.\n"
            "   -F <type>/<ext>\n"
            "           add FIG type/ext to list of FIGs to display.\n"
            "           if the option is not given, all FIGs are displayed.\n"
            "\n",
#if defined(GITVERSION)
            GITVERSION,
#else
            VERSION,
#endif
            __DATE__, __TIME__);
}

int main(int argc, char *argv[])
{
    struct sigaction sa;
    memset( &sa, 0, sizeof(sa) );
    sa.sa_handler = handle_signal;
    sigfillset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);

    int index;
    int ch = 0;
    string file_name("-");

    eti_analyse_config_t config;

    while(ch != -1) {
        ch = getopt_long(argc, argv, "d:efF:hrRsvwi:", longopts, &index);
        switch (ch) {
            case 'd':
                {
                int subchix = atoi(optarg);
                config.streams_to_decode.emplace(std::piecewise_construct,
                        std::make_tuple(subchix),
                        std::make_tuple(true)); // dump to file
                }
                break;
            case 'e':
                config.ignore_error = true;
                break;
            case 'i':
                file_name = optarg;
                break;
            case 'f':
                config.analyse_fic_carousel = true;
                break;
            case 'F':
                {
                const string type_ext(optarg);
                const boost::regex regex("^([0-9]+)/([0-9]+)$");
                boost::smatch match;
                bool is_match = boost::regex_search(type_ext, match, regex);
                if (not is_match) {
                    fprintf(stderr, "Incorrect -F format\n");
                    return 1;
                }

                const string type_str = match[1];
                const int type = std::atoi(type_str.c_str());
                const string extension_str = match[2];
                const int extension = std::atoi(extension_str.c_str());

                fprintf(stderr, "Adding FIG %d/%d to filter\n", type, extension);
                config.figs_to_display.emplace_back(type, extension);
                }
                break;
            case 'r':
                config.analyse_fig_rates = true;
                config.analyse_fig_rates_per_second = true;
                break;
            case 'R':
                config.analyse_fig_rates = true;
                config.analyse_fig_rates_per_second = false;
                break;
            case 's':
                config.statistics = true;
                break;
            case 'v':
                set_verbosity(get_verbosity() + 1);
                break;
            case 'w':
                config.decode_watermark = true;
                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;
        }
    }
    config.etifd = etifd;

    ETI_Analyser eti_analyser(config);
    eti_analyser.eti_analyse();
    fclose(etifd);
}