aboutsummaryrefslogtreecommitdiffstats
path: root/etiinput.cpp
blob: 8f47ec0df1c0331feeaaa0cb9d19aece96f230b3 (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
/*
   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.hpp"
#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;
        }
    }

    (void)nbframes_; // suppress warning "nbframes_ unused"
    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;
}