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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
Research Center Canada)
Copyright (C) 2016 Matthias P. Braendli
http://www.opendigitalradio.org
*/
/*
This file is part of ODR-DabMux.
ODR-DabMux is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
ODR-DabMux is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef _WIN32
# define O_BINARY 0
#endif
#include "input/File.h"
#include "mpeg.h"
namespace Inputs {
int FileBase::open(const std::string& name)
{
m_fd = ::open(name.c_str(), O_RDONLY | O_BINARY);
if (m_fd == -1) {
std::stringstream ss;
ss << "Could not open input file " << name << ": " <<
strerror(errno);
throw std::runtime_error(ss.str());
}
return 0;
}
int FileBase::close()
{
if (m_fd != -1) {
::close(m_fd);
m_fd = -1;
}
return 0;
}
int FileBase::rewind()
{
return ::lseek(m_fd, 0, SEEK_SET);
}
int MPEGFile::readFrame(void* buffer, int size)
{
int result;
bool do_rewind = false;
READ_SUBCHANNEL:
if (m_parity) {
result = readData(m_fd, buffer, size, 2);
m_parity = false;
return 0;
} else {
result = readMpegHeader(m_fd, buffer, size);
if (result > 0) {
result = readMpegFrame(m_fd, buffer, size);
if (result < 0 && getMpegFrequency(buffer) == 24000) {
m_parity = true;
result = size;
}
}
}
switch (result) {
case MPEG_BUFFER_UNDERFLOW:
etiLog.log(warn, "data underflow -> frame muted\n");
goto MUTE_SUBCHANNEL;
case MPEG_BUFFER_OVERFLOW:
etiLog.log(warn, "bitrate too high -> frame muted\n");
goto MUTE_SUBCHANNEL;
case MPEG_FILE_EMPTY:
if (do_rewind) {
etiLog.log(error, "file rewinded and still empty "
"-> frame muted\n");
goto MUTE_SUBCHANNEL;
}
else {
etiLog.log(info, "reach end of file -> rewinding\n");
rewind();
goto READ_SUBCHANNEL;
}
case MPEG_FILE_ERROR:
etiLog.log(alert, "can't read file (%i) -> frame muted\n", errno);
perror("");
goto MUTE_SUBCHANNEL;
case MPEG_SYNC_NOT_FOUND:
etiLog.log(alert, "mpeg sync not found, maybe is not a valid file "
"-> frame muted\n");
goto MUTE_SUBCHANNEL;
case MPEG_INVALID_FRAME:
etiLog.log(alert, "file is not a valid mpeg file "
"-> frame muted\n");
goto MUTE_SUBCHANNEL;
default:
if (result < 0) {
etiLog.log(alert,
"unknown error (code = %i) -> frame muted\n",
result);
MUTE_SUBCHANNEL:
memset(buffer, 0, size);
}
else {
if (result < size) {
etiLog.log(warn, "bitrate too low from file "
"-> frame padded\n");
memset((char*)buffer + result, 0, size - result);
}
result = checkDabMpegFrame(buffer);
switch (result) {
case MPEG_FREQUENCY:
etiLog.log(error, "file has a frame with an invalid "
"frequency: %i, should be 48000 or 24000\n",
getMpegFrequency(buffer));
break;
case MPEG_PADDING:
etiLog.log(warn,
"file has a frame with padding bit set\n");
break;
case MPEG_COPYRIGHT:
result = 0;
break;
case MPEG_ORIGINAL:
result = 0;
break;
case MPEG_EMPHASIS:
etiLog.log(warn,
"file has a frame with emphasis bits set\n");
break;
default:
if (result < 0) {
etiLog.log(alert, "mpeg file has an invalid DAB "
"mpeg frame (unknown reason: %i)\n", result);
}
break;
}
}
}
return result;
}
int MPEGFile::setBitrate(int bitrate)
{
if (bitrate == 0) {
char buffer[4];
if (readFrame(buffer, 4) == 0) {
bitrate = getMpegBitrate(buffer);
}
else {
bitrate = -1;
}
rewind();
}
return bitrate;
}
int DABPlusFile::readFrame(void* buffer, int size)
{
uint8_t* dataOut = reinterpret_cast<uint8_t*>(buffer);
ssize_t ret = read(m_fd, dataOut, size);
if (ret == -1) {
etiLog.log(alert, "ERROR: Can't read file\n");
perror("");
return -1;
}
if (ret < size) {
ssize_t sizeOut = ret;
etiLog.log(info, "reach end of file -> rewinding\n");
if (rewind() == -1) {
etiLog.log(alert, "ERROR: Can't rewind file\n");
return -1;
}
ret = read(m_fd, dataOut + sizeOut, size - sizeOut);
if (ret == -1) {
etiLog.log(alert, "ERROR: Can't read file\n");
perror("");
return -1;
}
if (ret < size) {
etiLog.log(alert, "ERROR: Not enough data in file\n");
return -1;
}
}
return size;
}
int DABPlusFile::setBitrate(int bitrate)
{
if (bitrate <= 0) {
etiLog.log(error, "Invalid bitrate (%i)\n", bitrate);
return -1;
}
return bitrate;
}
};
|