-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cpp
More file actions
654 lines (578 loc) · 18 KB
/
index.cpp
File metadata and controls
654 lines (578 loc) · 18 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* dvbcut
Copyright (c) 2005 Sven Over <svenover@svenover.de>
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id$ */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <list>
#include <utility>
#include <set>
#include <vector>
#include <string>
#include "port.h"
#include "index.h"
#include "mpgfile.h"
#include "streamhandle.h"
#include "types.h"
#include "logoutput.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */
#define WRITE_FAKE_PICTURES // #undef to disable writing fake pictures
static inline ssize_t writer(int fd, const void *buf, size_t count)
{
int written=0;
while (count>0) {
int wr=write(fd,buf,count);
if (wr<0)
return wr;
written+=wr;
count-=wr;
buf=(const void*)((const char*)buf+wr);
}
return written;
}
index::~index()
{
if (p) {
free(p);
// delete read resolutions
WIDTH.clear();
HEIGHT.clear();
}
}
int index::generate(const char *savefilename, std::string *errorstring, logoutput *log)
{
fprintf(stderr, "index::generate\n");
int fd=-1;
bool usestdout=false;
int pictureswritten=0;
dvbcut_off_t filesize=0;
if (savefilename && savefilename[0]) {
if (savefilename[0]=='-' && savefilename[1]==0) // use stdout
{
fd=STDOUT_FILENO;
usestdout=true;
} else
if ((fd=::open(savefilename,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0666))<0) {
if (errorstring)
*errorstring+=std::string("Open (")+savefilename+"): "+strerror(errno)+"\n";
return fd;
}
}
int size=90000;
fprintf(stderr, "index::generate/start with %d picture size.\n",size);
if (p) {
if (pictures>0)
size=pictures;
else {
free(p);
p=0;
}
}
if (!p)
p=(picture*)malloc(size*sizeof(picture));
pictures=0;
if (log)
filesize=mpg.getfilesize();
streamhandle s(mpg.getinitialoffset());
streamdata *sd=s.newstream(VIDEOSTREAM,streamtype::mpeg2video,mpg.istransportstream());
bool foundseqheader=false;
bool waitforfirstsequenceheader=true;
int aspectratio=0,framerate=0;
double maxbitrate=0., bitrate;
long int bits=0;
pts_t maxbitratepts=0, dt;
int nres = 0;
filepos_t seqheaderpos=0, lastseqheaderpos=0;
int seqheaderpic=0;
pts_t referencepts=0; // pts of picture #0 in current sequence
int maxseqnr=-1; // maximum sequence number in current sequence
pts_t lastpts=1ll<<31, lastseqheaderpts=0, firstseqheaderpts=0;
int last_non_b_pic = -1;
int last_non_b_seqnr = -1;
int last_seqnr = -1;
int ptsmod = -1;
int errcnt = 0;
int err1cnt = 0;
int lasterr = 0;
int lastiframe = -1;
while (mpg.streamreader(s)>0) {
while (sd->getbuffer().inbytes()< (sd->getbuffer().getsize()/2))
if (mpg.streamreader(s)<=0)
break;
if (log) {
log->setprogress( (filesize>0)?(mpg.getfilepos()*1000/filesize):((mpg.getfilepos()/104753)%1001) );
if (log->cancelled()) {
if (p)
free(p);
p=0;
pictures=realpictures=0;
return 0;
}
}
// flush data to file
if (fd>=0 && pictureswritten<seqheaderpic) {
int len=(seqheaderpic-pictureswritten)*sizeof(picture);
if (::writer(fd,(void*)&p[pictureswritten],len)<0) {
if (errorstring)
*errorstring+=std::string("Write (")+savefilename+"): "+strerror(errno)+"\n";
if (!usestdout)
::close(fd);
fd=-1;
} else
pictureswritten=seqheaderpic;
}
const uint8_t *data=(const uint8_t*) sd->getdata();
unsigned int inbytes=sd->inbytes();
unsigned int skip=0;
while(skip+11<inbytes) {
if (data[skip+2]&0xfe) {
skip+=3;
continue;
}
if (data[skip+1]!=0) {
skip+=2;
continue;
}
if (*(uint32_t*)(data+skip)==mbo32(0x000001b3)) // sequence header
{
if (last_non_b_pic >= 0) {
p[last_non_b_pic].setsequencenumber(++maxseqnr);
last_non_b_pic = -1;
}
last_seqnr = -1;
waitforfirstsequenceheader=false;
foundseqheader=true;
sd->discard(skip);
data=(const uint8_t*) sd->getdata();
// store found resolutions in lookup table
std::pair<int,int> res;
res.first=(data[4]<<4)|((data[5]>>4)&0xf); // width
res.second=((data[5]&0xf)<<8)|data[6]; // height
if (resolutions.find(res)==resolutions.end() && nres<7) {
nres++;
resolutions.insert(res); // a set helps checking already read resolutions
WIDTH.push_back(res.first);
HEIGHT.push_back(res.second);
//fprintf(stderr, "RESOLUTION[%d]: %d x %d\n", nres, res.first, res.second);
}
// that's always the same preset value and thus not very usefull (in 400 bps)!!!
//bitrate = ((data[8]<<10)|(data[9]<<2)|((data[10]>>6)&0x3))*400;
//if (bitrate>maxbitrate) maxbitrate=bitrate;
aspectratio=(data[7]>>4)&0xf;
framerate=data[7]&0xf;
referencepts+=((maxseqnr+1)*mpgfile::frameratescr[framerate])/300;
seqheaderpos=sd->itemlist().front().fileposition;
seqheaderpic=pictures;
maxseqnr=-1;
sd->discard(12);
data=(const uint8_t*) sd->getdata();
inbytes=sd->inbytes();
skip=0;
} else if ((*(uint32_t*)(data+skip)==mbo32(0x00000100))&&!waitforfirstsequenceheader) // picture header
{
sd->discard(skip);
data=(const uint8_t*) sd->getdata();
filepos_t picpos=sd->itemlist().front().fileposition;
int seqnr=(data[4]<<2)|((data[5]>>6)&3);
int frametype=(data[5]>>3)&7;
if (frametype>3)
frametype=0;
pts_t pts=sd->itemlist().front().headerpts();
if (pts>=0)
{
pts=ptsreference(pts,lastpts);
lastpts = pts;
int ptsdelta = mpgfile::frameratescr[framerate] / 300;
int epsilon = ptsdelta / 100; /* allow at most 1% deviation */
int mod = pts % ptsdelta;
if (ptsmod == -1)
ptsmod = mod;
else if (mod != ptsmod) {
int error = (mod - ptsmod + ptsdelta) % ptsdelta;
if (error > ptsdelta / 2)
error -= ptsdelta;
bool complain = false;
if (lasterr != error) {
if (err1cnt > 0) {
fprintf(stderr, "last video PTS error repeated %d times\n", err1cnt);
err1cnt = 0;
}
complain = true;
lasterr = error;
}
else
++err1cnt;
++errcnt;
if (-epsilon <= error && error <= epsilon) {
if (complain)
fprintf(stderr, "inconsistent video PTS (%+d), correcting\n", error);
pts -= error;
} else {
if (complain)
fprintf(stderr, "inconsistent video PTS (%+d) in %c frame %d, NOT correcting\n",
error, frametype["?IPB"], pictures);
}
}
referencepts=pts-(seqnr*mpgfile::frameratescr[framerate])/300;
sd->discardheader();
} else
pts=referencepts+(seqnr*mpgfile::frameratescr[framerate])/300;
if (pictures>=size)
{
size+=90000;
p=(picture*)realloc((void*)p,size*sizeof(picture));
}
#ifdef WRITE_FAKE_PICTURES
p[pictures]=picture(foundseqheader?seqheaderpos:picpos,pts,framerate,
aspectratio,seqnr,frametype,foundseqheader,nres);
#else
p[pictures]=picture(foundseqheader?seqheaderpos:picpos,pts,framerate,
aspectratio,seqnr,frametype,foundseqheader,0);
#endif
// try to determine bitrate per GOP manually since the read one is not very usefull
if (foundseqheader) {
if (firstseqheaderpts && lastseqheaderpos<seqheaderpos && lastseqheaderpts<pts) {
dt = (pts-lastseqheaderpts)/90;
bits = (seqheaderpos-lastseqheaderpos)*8;
// avergage (input) bitrate per GOP
bitrate=1000*double(bits)/double(dt);
if (maxbitrate<bitrate) {
maxbitrate=bitrate;
maxbitratepts=lastseqheaderpts;
}
//fprintf(stderr, "%s: BITRATE = %d kbps over next %d ms\n",
// ptsstring(lastseqheaderpts-firstseqheaderpts).c_str(), int(bitrate/1024.), int(dt));
} else {
firstseqheaderpts=pts;
}
lastseqheaderpos=seqheaderpos;
lastseqheaderpts=pts;
}
if (frametype == IDX_PICTYPE_I) {
if (lastiframe >= 0) {
int framepts = mpgfile::frameratescr[framerate] / 300;
pts_t ptsdelta = pts - p[lastiframe].getpts();
int pdelta = pictures - lastiframe + seqnr - p[lastiframe].getsequencenumber();
if (pdelta * framepts < ptsdelta)
fprintf(stderr, "missing frames in GOP (%d, %d): %lld\n",
lastiframe, pictures, ptsdelta / framepts - pdelta);
}
lastiframe = pictures;
}
if (frametype == IDX_PICTYPE_B) {
/* check sequence number */
if (seqnr != last_seqnr + 1) {
fprintf(stderr,
"missing frame(s) before B frame %d (%d != %d)\n",
pictures, seqnr, last_seqnr + 1);
if (seqnr <= last_seqnr) {
fprintf(stderr, "=> sequence number reset (%d => %d)\n", last_seqnr + 1, seqnr);
if (last_non_b_pic >= 0 && last_non_b_seqnr > last_seqnr) {
fprintf(stderr, "=> inserting delayed picture (%d)\n", last_non_b_seqnr);
p[last_non_b_pic].setsequencenumber(++maxseqnr);
last_non_b_pic = -1;
}
}
else if (last_non_b_pic >= 0 && last_non_b_seqnr < seqnr) {
fprintf(stderr, "=> inserting delayed picture (%d)\n", last_non_b_seqnr);
p[last_non_b_pic].setsequencenumber(++maxseqnr);
last_non_b_pic = -1;
}
}
p[pictures].setsequencenumber(++maxseqnr);
last_seqnr = seqnr;
} else {
/* I and P frames are delayed */
if (last_non_b_pic >= 0) {
/* check sequence number */
if (last_non_b_seqnr != last_seqnr + 1) {
fprintf(stderr,
"missing frame(s) before %c frame %d (%d != %d)\n",
p[last_non_b_pic].isiframe() ? 'I' : 'P',
pictures, last_non_b_seqnr, last_seqnr + 1);
}
p[last_non_b_pic].setsequencenumber(++maxseqnr);
last_seqnr = last_non_b_seqnr;
}
last_non_b_pic = pictures;
last_non_b_seqnr = seqnr;
if (frametype == IDX_PICTYPE_I)
last_seqnr = -1;
}
++pictures;
foundseqheader=false;
sd->discard(8);
data=(const uint8_t*) sd->getdata();
inbytes=sd->inbytes();
skip=0;
} else
++skip;
}
sd->discard(skip);
}
if (err1cnt > 0)
fprintf(stderr, "last video PTS error repeated %d times\n", err1cnt);
if (errcnt > 0)
fprintf(stderr, "found %d video PTS errors\n", errcnt);
if (last_non_b_pic >= 0) {
p[last_non_b_pic].setsequencenumber(++maxseqnr);
last_non_b_pic = -1;
}
if (pictures==0) {
free(p);
p=0;
} else {
#ifdef WRITE_FAKE_PICTURES
if (size < pictures + 7) {
size = pictures + 7;
p=(picture*)realloc((void*)p,size*sizeof(picture));
}
// create fake pictures containing read resolutions (pos: width, pts: height)
for (int i=0; i<7; i++) {
int w = i < nres ? WIDTH[i] : 0;
int h = i < nres ? HEIGHT[i] : 0;
p[pictures]=picture(filepos_t(w),pts_t(h),0,0,0,0,false,i+1);
++pictures;
}
#endif
if (size != pictures) {
p=(picture*)realloc((void*)p,pictures*sizeof(picture));
}
}
if (fd>=0 && pictureswritten<pictures) {
int len=(pictures-pictureswritten)*sizeof(picture);
if (::writer(fd,(void*)&p[pictureswritten],len)<0) {
if (errorstring)
*errorstring+=std::string("Write (")+savefilename+"): "+strerror(errno)+"\n";
if (!usestdout)
::close(fd);
fd=-1;
} else
pictureswritten=pictures;
}
if (!usestdout && fd>=0)
::close(fd);
#ifdef WRITE_FAKE_PICTURES
if (pictures != 0)
pictures-=7; // subtract fake pictures
#endif
fprintf(stderr, "Max. input bitrate of %d kbps detected at %s\n", int(maxbitrate/1024), ptsstring(maxbitratepts-firstseqheaderpts).c_str());
fprintf(stderr, "index::generate/%d pictures in video file.\n",pictures);
return check();
}//index::generate
int
index::save(int fd, std::string *errorstring, bool closeme) {
#ifdef WRITE_FAKE_PICTURES
int len = (pictures + 7) * sizeof(picture);
#else
int len = pictures * sizeof(picture);
#endif
int res = 0;
int save = 0;
if (isatty(fd)) {
if (errorstring)
*errorstring += std::string("refusing to write index to a tty\n");
errno = EINVAL;
// Note: do NOT close it even if the caller said so
return -1;
}
if (::writer(fd, (void*)p, len) < 0) {
save = errno;
if (errorstring)
*errorstring += std::string("write: ") + strerror(errno) + "\n";
res = -1;
}
if (closeme)
::close(fd);
errno = save;
return res;
}
int
index::save(const char *filename, std::string *errorstring) {
int fd;
fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (fd == -1) {
if (errorstring)
*errorstring += std::string(filename) + ": open: " + strerror(errno) + "\n";
return -1;
}
std::string tmp;
if (save(fd, &tmp, true) == -1) {
if (errorstring)
*errorstring += std::string(filename) + ": " + tmp;
return -1;
}
return 0;
}
int index::load(const char *filename, std::string *errorstring)
{
int fd=::open(filename,O_RDONLY|O_BINARY,0666);
if (fd<0) {
if (errorstring) {
int serrno=errno;
*errorstring+=std::string("Open (")+filename+"): "+strerror(errno)+"\n";
errno=serrno;
}
return fd;
}
int size=0;
int len=0;
uint8_t *data=0;
for(;;) {
if (len>=size) {
size+=90000*sizeof(picture);
data=(uint8_t*)realloc((void*)data,size);
}
int rd=::read(fd,data+len,size-len);
if (rd<0) {
int save_errno=errno;
if (errorstring)
*errorstring+=std::string("Read (")+filename+"): "+strerror(errno)+"\n";
if (data)
free(data);
::close(fd);
errno=save_errno;
return -1;
}
if (rd==0)
break;
len+=rd;
}
::close(fd);
pictures=len/sizeof(picture);
if (pictures==0) {
free(p);
p=0;
realpictures=0;
return 0;
}
if (!((picture*)data)->getseqheader()) {
free(p);
p=0;
pictures=0;
realpictures=0;
if (errorstring)
*errorstring+=std::string("Invalid index file '")+filename+"'\n";
fprintf(stderr,"Invalid index file: first frame no sequence header\n");
return -2;
}
p=(picture*)realloc((void*)data,pictures*sizeof(picture));
// 7 fake pictures at end contain resolution lookup table
// (if new type of index file, after svn-revision 131)
if (p[0].getresolution()>0) {
pictures-=7;
int w, h, nres=0;
for (int i=pictures; nres<7; i++) {
w=int(p[i].getpos());
h=int(p[i].getpts());
if (w==0 || h==0)
break;
nres++;
WIDTH.push_back(w);
HEIGHT.push_back(h);
//fprintf(stderr, "RESOLUTION[%d]: %d x %d\n", nres, w, h);
}
}
int seqnr[1<<10]={0};
int seqpics=0;
for(int i=0;;++i) {
if (i==pictures || p[i].getseqheader()) {
for(int j=0;j<seqpics;++j) {
if (seqnr[j]!=1) // this sequence-number did not appear exactly once
{
if (errorstring)
*errorstring+=std::string("Invalid index file (")+filename+")\n";
fprintf(stderr,"Invalid index file: sequence number %u appears %u times\n",
j, seqnr[j]);
fprintf(stderr, "Picture %u/%u, %u seqpics\n", i, pictures, seqpics);
free(p);
p=0;
pictures=0;
realpictures=0;
return -2;
}
seqnr[j]=0;
}
if (i==pictures)
break;
seqpics=0;
}
++seqnr[p[i].getsequencenumber()];
++seqpics;
}
for(int i=1;i<pictures;i<<=1) {
while(i<pictures && !p[i].getseqheader())
++i;
if (i==pictures)
break;
streamhandle s(p[i].getpos().packetposition());
streamdata *sd=s.newstream(VIDEOSTREAM,streamtype::mpeg2video,mpg.istransportstream());
unsigned int po=p[i].getpos().packetoffset();
while (sd->inbytes()<po+4)
if (mpg.streamreader(s)<=0)
break;
if ( (sd->inbytes()<po+4) || (*(const uint32_t*)((const uint8_t*)sd->getdata()+po) != mbo32(0x000001b3)) ) {
fprintf(stderr,"index does not match (%08x)\n",(*(const uint32_t*)((const uint8_t*)sd->getdata()+po)));
free(p);
p=0;
pictures=0;
realpictures=0;
if (errorstring)
*errorstring+=std::string("Index file (")+filename+") does not correspond to MPEG file\n";
return -3;
}
}
return check();
}
int index::check()
{
int firstiframe;
for(firstiframe=0;firstiframe<pictures;++firstiframe)
if (p[firstiframe].isiframe())
break;
if (firstiframe>=pictures) {
realpictures=0;
skipfirst=0;
return 0;
}
int sequencebegin=0;
for (int i=firstiframe;i>0;--i)
if (p[i].getseqheader()) {
sequencebegin=i;
break;
}
skipfirst=sequencebegin;
if (p[firstiframe].getsequencenumber()>0) {
int fifseqnr=p[firstiframe].getsequencenumber();
for(int i=sequencebegin;(i<pictures)&&(!p[i].getseqheader()||i==sequencebegin);++i)
if (p[i].getsequencenumber()<fifseqnr)
++skipfirst;
}
realpictures=pictures-skipfirst;
if (realpictures<1)
return 0;
while (realpictures>0)
if (p[indexnr(realpictures-1)].isbframe())
--realpictures;
else
break;
return realpictures;
}