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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
#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;
m_samples_taken = 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;
}
m_samples_taken = 0;
if (m_file.open(filename, 1) < 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())
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;
// eDebug("found pts %08llx at %08llx", pts, offset);
/* 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
{
// eDebug("get offset");
if (!m_samples_taken)
takeSamples();
if (!m_samples.empty())
{
// eDebug("ok, samples ok");
/* search entry before and after */
std::map<pts_t, off_t>::const_iterator l = m_samples.lower_bound(pts);
std::map<pts_t, off_t>::const_iterator u = l;
if (l != m_samples.begin())
--l;
if ((u != m_samples.end()) && (l != m_samples.end()))
{
pts_t pts_diff = u->first - l->first;
off_t offset_diff = u->second - l->second;
// eDebug("using: %llx:%llx -> %llx:%llx", l->first, u->first, l->second, u->second);
if (pts_diff)
{
int bitrate = offset_diff * 90000 * 8 / pts_diff;
if (bitrate > 0)
{
offset = l->second;
offset += ((pts - l->first) * (pts_t)bitrate) / 8ULL / 90000ULL;
offset -= offset % 188;
return 0;
}
}
}
}
eDebug("falling back");
int bitrate = calcBitrate();
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;
}
/* pts, off */
void eDVBTSTools::takeSamples()
{
m_samples_taken = 1;
m_samples.clear();
pts_t dummy;
if (calcLen(dummy) == -1)
return;
int nr_samples = 30;
off_t bytes_per_sample = (m_offset_end - m_offset_begin) / (long long)nr_samples;
if (bytes_per_sample < 40*1024*1024)
bytes_per_sample = 40*1024*1024;
bytes_per_sample -= bytes_per_sample % 188;
for (off_t offset = m_offset_begin; offset < m_offset_end; offset += bytes_per_sample)
{
off_t o = offset;
pts_t p;
if (!eDVBTSTools::getPTS(o, p, 1))
{
// eDebug("sample: %llx, %llx", o, p);
m_samples[p] = o;
}
}
m_samples[m_pts_begin] = m_offset_begin;
m_samples[m_pts_end] = m_offset_end;
// eDebug("begin, end: %llx %llx", m_offset_begin, m_offset_end);
}
int eDVBTSTools::findPMT(int &pmt_pid, int &service_id)
{
/* FIXME: this will be factored out soon! */
if (!m_file.valid())
{
eDebug(" file not valid");
return -1;
}
if (m_file.lseek(0, SEEK_SET) < 0)
{
eDebug("seek failed");
return -1;
}
int left = 5*1024*1024;
while (left >= 188)
{
unsigned char block[188];
if (m_file.read(block, 188) != 188)
{
eDebug("read error");
break;
}
left -= 188;
if (block[0] != 0x47)
{
int i = 0;
while (i < 188)
{
if (block[i] == 0x47)
break;
++i;
}
m_file.lseek(i - 188, SEEK_CUR);
continue;
}
int pid = ((block[1] << 8) | block[2]) & 0x1FFF;
int pusi = !!(block[1] & 0x40);
if (!pusi)
continue;
/* ok, now we have a PES header or section header*/
unsigned char *sec;
/* check for adaption field */
if (block[3] & 0x20)
sec = block + block[4] + 4 + 1;
else
sec = block + 4;
if (sec[0]) /* table pointer, assumed to be 0 */
continue;
if (sec[1] == 0x02) /* program map section */
{
pmt_pid = pid;
service_id = (sec[4] << 8) | sec[5];
return 0;
}
}
return -1;
}
|