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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
#include <lib/dvb/tstools.h>
#include <lib/base/eerror.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
eDVBTSTools::eDVBTSTools()
{
m_pid = -1;
m_maxrange = 256*1024;
m_begin_valid = 0;
m_end_valid = 0;
m_use_streaminfo = 0;
}
eDVBTSTools::~eDVBTSTools()
{
closeFile();
}
int eDVBTSTools::openFile(const char *filename)
{
closeFile();
m_streaminfo.load((std::string(filename) + ".ap").c_str());
if (!m_streaminfo.empty())
m_use_streaminfo = 1;
else
{
eDebug("no recorded stream information available");
m_use_streaminfo = 0;
}
if (m_file.open(filename) < 0)
return -1;
return 0;
}
void eDVBTSTools::closeFile()
{
m_file.close();
}
void eDVBTSTools::setSyncPID(int pid)
{
m_pid = pid;
}
void eDVBTSTools::setSearchRange(int maxrange)
{
m_maxrange = maxrange;
}
/* getPTS extracts a pts value from any PID at a given offset. */
int eDVBTSTools::getPTS(off_t &offset, pts_t &pts, int fixed)
{
if (m_use_streaminfo)
return m_streaminfo.getPTS(offset, pts);
if (!m_file.valid() < 0)
return -1;
offset -= offset % 188;
if (m_file.lseek(offset, SEEK_SET) < 0)
return -1;
int left = m_maxrange;
while (left >= 188)
{
unsigned char block[188];
if (m_file.read(block, 188) != 188)
{
eDebug("read error");
break;
}
left -= 188;
offset += 188;
if (block[0] != 0x47)
{
int i = 0;
while (i < 188)
{
if (block[i] == 0x47)
break;
++i;
}
offset = m_file.lseek(i - 188, SEEK_CUR);
continue;
}
int pid = ((block[1] << 8) | block[2]) & 0x1FFF;
int pusi = !!(block[1] & 0x40);
// printf("PID %04x, PUSI %d\n", pid, pusi);
if (m_pid >= 0)
if (pid != m_pid)
continue;
if (!pusi)
continue;
/* ok, now we have a PES header */
unsigned char *pes;
/* check for adaption field */
if (block[3] & 0x20)
pes = block + block[4] + 4 + 1;
else
pes = block + 4;
/* somehow not a startcode. (this is invalid, since pusi was set.) ignore it. */
if (pes[0] || pes[1] || (pes[2] != 1))
continue;
if (pes[7] & 0x80) /* PTS */
{
pts = ((unsigned long long)(pes[ 9]&0xE)) << 29;
pts |= ((unsigned long long)(pes[10]&0xFF)) << 22;
pts |= ((unsigned long long)(pes[11]&0xFE)) << 14;
pts |= ((unsigned long long)(pes[12]&0xFF)) << 7;
pts |= ((unsigned long long)(pes[13]&0xFE)) >> 1;
offset -= 188;
/* convert to zero-based */
if (fixed)
fixupPTS(offset, pts);
return 0;
}
}
return -1;
}
int eDVBTSTools::fixupPTS(const off_t &offset, pts_t &now)
{
if (m_use_streaminfo)
{
return m_streaminfo.fixupPTS(offset, now);
} else
{
/* for the simple case, we assume one epoch, with up to one wrap around in the middle. */
calcBegin();
if (!m_begin_valid)
{
eDebug("begin not valid, can't fixup");
return -1;
}
pts_t pos = m_pts_begin;
if ((now < pos) && ((pos - now) < 90000 * 10))
{
pos = 0;
return 0;
}
if (now < pos) /* wrap around */
now = now + 0x200000000LL - pos;
else
now -= pos;
return 0;
}
}
int eDVBTSTools::getOffset(off_t &offset, pts_t &pts)
{
if (m_use_streaminfo)
{
offset = m_streaminfo.getAccessPoint(pts);
return 0;
} else
{
int bitrate = calcBitrate(); /* in bits/s */
if (bitrate <= 0)
return -1;
offset = (pts * (pts_t)bitrate) / 8ULL / 90000ULL;
offset -= offset % 188;
return 0;
}
}
int eDVBTSTools::getNextAccessPoint(pts_t &ts, const pts_t &start, int direction)
{
if (m_use_streaminfo)
return m_streaminfo.getNextAccessPoint(ts, start, direction);
else
{
eDebug("can't get next access point without streaminfo");
return -1;
}
}
void eDVBTSTools::calcBegin()
{
if (!m_file.valid())
return;
if (!m_begin_valid)
{
m_offset_begin = 0;
if (!getPTS(m_offset_begin, m_pts_begin))
m_begin_valid = 1;
}
}
void eDVBTSTools::calcEnd()
{
if (!m_file.valid())
return;
off_t end = m_file.lseek(0, SEEK_END);
if (abs(end - m_offset_end) > 1*1024*1024)
{
m_offset_end = end;
m_end_valid = 0;
eDebug("file size changed, recalc length");
}
int maxiter = 10;
while (!m_end_valid)
{
if (!--maxiter)
return;
m_offset_end -= m_maxrange;
if (m_offset_end < 0)
m_offset_end = 0;
if (!getPTS(m_offset_end, m_pts_end))
m_end_valid = 1;
if (!m_offset_end)
return;
}
}
int eDVBTSTools::calcLen(pts_t &len)
{
calcBegin(); calcEnd();
if (!(m_begin_valid && m_end_valid))
return -1;
len = m_pts_end - m_pts_begin;
/* wrap around? */
if (len < 0)
len += 0x200000000LL;
return 0;
}
int eDVBTSTools::calcBitrate()
{
calcBegin(); calcEnd();
if (!(m_begin_valid && m_end_valid))
return -1;
pts_t len_in_pts = m_pts_end - m_pts_begin;
/* wrap around? */
if (len_in_pts < 0)
len_in_pts += 0x200000000LL;
off_t len_in_bytes = m_offset_end - m_offset_begin;
if (!len_in_pts)
return -1;
unsigned long long bitrate = len_in_bytes * 90000 * 8 / len_in_pts;
if ((bitrate < 10000) || (bitrate > 100000000))
return -1;
return bitrate;
}
|