-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnoad.cpp
executable file
·2694 lines (2488 loc) · 75.1 KB
/
noad.cpp
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
noaddata.h - description
-------------------
begin : Sun Mar 10 2002
copyright : (C) 2002/2004 by theNoad #709GRW
email : [email protected]
***************************************************************************/
/*
this is the main part of noad
most of the work is done here
*/
#include "config.h"
bool bOnMarksOnly = false;
#ifdef VNOAD
bool bPlayAudio = false;
int ossfd = 0;
bool useAudioDetection = true;
#define noCHECK_ONMARKS
#define noDO_PASS2
#define DO_NEWPASS2
#define AUTO_CLEANUP
#else
bool useAudioDetection = false;
#define noDO_PASS2
#define DO_NEWPASS2
#define AUTO_CLEANUP
#endif
#include "noad.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#include <signal.h>
#endif
#include <inttypes.h>
#include "svdrpc.h"
#include "audiotools.h"
MPEGDecoder *decoder;
extern "C"
{
static const char *VERSIONSTRING = "0.8.8";
}
#ifdef VNOAD
#include <qobject.h>
#include <qwidget.h>
QWidget *widget = NULL;
#include "vnoad_f.h"
#endif
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include "vdr_cl.h"
#include "tools.h"
#include "videodir.h"
#include "noaddata.h"
#include "ccontrol.h"
#ifdef VNOAD
#include "ctoolbox.h"
frameCallbackFunc frameCallback = NULL;
infocbfunc info_callback = NULL;
positioncbfunc fPosCallBack = NULL;
#endif
fcbSizeChanged fSizeChanged = NULL;
typedef unsigned char uchar;
// Picture types:
#define NO_PICTURE 0
#define I_FRAME 1
#define P_FRAME 2
#define B_FRAME 3
extern int demux_track;
extern int SysLogLevel;
noadData *ndata = NULL;
CControl* cctrl = NULL;
char *filename = NULL;
bool bReUseLogo = true;
int default_Decoder = FFMPEG_DECODER;
cNoadIndexFile *cIF = NULL;
cFileName *cfn = NULL;
bool bMarkChanged = false;
extern int bFrameDisplayed;
//statistic data
int totalFrames = 0;
int totalDecodedFrames = 0;
int decodedFramesForLogoDetection = 0;
int decodedFramesForLogoCheck = 0;
int decodedFramesForAC3Detection = 0;
int decodedFramesForAC3Check = 0;
int secsForLogoDetection = 0;
int secsForLogoCheck = 0;
int secsForAC3Detection = 0;
int secsForAC3Scan = 0;
int secsForScan = 0;
bool hasBlackLines = false;
bool hasAC3 = false;
bool hasOverlaps = false;
int picWidth = 0;
int picHeight = 0;
// control-data
bool backupMarks = false;
bool isNelonen = false;
bool doPass2 = true;
#ifdef VNOAD
bool detectOverlaps = true;
bool saveLogo = true;
bool useSceneChangeDetection = false;
int osdMsg=1;
bool logoJumpDetection = true;
bool markComments = true;
bool doPass3 = true;
#else
bool detectOverlaps = false;
bool saveLogo = false;
bool useSceneChangeDetection = false;
int osdMsg=0;
bool logoJumpDetection = false;
bool markComments = false;
bool doPass3 = false;
#endif
bool pass3only = false;
int iLastIFrame = 0;
int iIgnoreFrames = 0;
unsigned long ulTopBlackLines;
unsigned long ulBotBlackLines;
int checkedFrames;
uchar readBuffer[MAXFRAMESIZE]; // frame-buffer
int curIndex = 1; // current index
uint16_t FileNumber; // current file-number
off_t FileOffset; // current file-offset
bool Independent; // current picture-type
int Length; // frame-lenght of current frame
uint8_t * end; // pointer to frame-end
noadYUVBuf lastYUVBuf/* = NULL*/; // last yuvbuf from StdCallBack
// Standard-Callback-Funktion
bool StdCallBack( noadYUVBuf* yuvbuf )
{
totalDecodedFrames++;
lastYUVBuf = yuvbuf;
// soll der Frame ignoriert werden ?
if( iIgnoreFrames )
{
iIgnoreFrames--;
return false;
}
if( ndata && yuvbuf)
{
// bei Änderung der Bildgrösse daten zurücksetzen
if( yuvbuf->width != ndata->m_nGrabWidth || yuvbuf->height != ndata->m_nGrabHeight )
{
delete cctrl;
cctrl = NULL;
ndata->setGrabSize( yuvbuf->width, yuvbuf->height );
if( fSizeChanged )
fSizeChanged(yuvbuf->width, yuvbuf->height);
}
ndata->setExternalMem(&lastYUVBuf);
return true;
}
return false;
}
int simpleCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf) )
{
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
}
return 0;
}
int LogoDetectCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf) )
{
if(cctrl == NULL )
{
cctrl = new CControl(ndata);
}
checkedFrames++;
ndata->setCorners();
if( ndata->m_bFound == false )
{
cctrl->newData();
}
else
{
bool bOldState = ndata->m_pCheckLogo->isLogo;
ndata->m_pCheckLogo->newData();
if( bOldState != ndata->m_pCheckLogo->isLogo )
{
dsyslog("LogoDetection: logo %s at %d", ndata->m_pCheckLogo->isLogo ? "appears" : "disappears", iLastIFrame);
}
}
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
}
return 0;
}
int BlacklineCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf ) )
{
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
ndata->detectBlackLines(yuvbuf);
ulTopBlackLines += ndata->m_nBlackLinesTop;
ulBotBlackLines += ndata->m_nBlackLinesBottom;
checkedFrames++;
#ifdef VNOAD
// ??? zweimal ???
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
}
return 0;
}
int BlackframeCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf) )
{
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
ndata->CheckFrameIsBlank(iCurrentDecodedFrame,yuvbuf);
ulTopBlackLines += ndata->m_nBlackLinesTop;
ulBotBlackLines += ndata->m_nBlackLinesBottom;
checkedFrames++;
}
return 0;
}
int checkCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf ) )
{
checkedFrames++;
ndata->setCorners();
ndata->m_pCheckLogo->newData();
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
}
return 0;
}
int nBlacklineCallback( noadYUVBuf* yuvbuf )
{
if( StdCallBack( yuvbuf ) )
{
ndata->ndetectBlackLines( yuvbuf);
ulTopBlackLines += ndata->m_nBlackLinesTop;
ulBotBlackLines += ndata->m_nBlackLinesBottom;
checkedFrames++;
#ifdef VNOAD
if( frameCallback )
frameCallback(iCurrentDecodedFrame,yuvbuf);
#endif
}
return 0;
}
// check for logo in some parts of the recording
// check CHECKLOGO_BLOCKS*CHECKLOGO_BLOCKFRAMES frames
// with a distance of CHECKLOGO_DIST between the blocks
// logo should be visible in at least #CHECKLOGO_FRAMES frames
bool checkLogo(int startpos)
{
dsyslog("checklogo for %d frames starting at frame %d", CHECKLOGO_FRAMES, startpos);
int iLogosFound = 0;
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(checkCallback);
int index = startpos;
int flags = DEMUX_RESET;
index = cIF->GetNextIFrame( index, true, &FileNumber, &FileOffset, &Length, true);
for( int i = 0; i < CHECKLOGO_BLOCKS && iLogosFound < CHECKLOGO_FRAMES; i++ )
{
for( int ii = 1; ii <= CHECKLOGO_BLOCKFRAMES && iLogosFound < CHECKLOGO_FRAMES; ii++ )
{
decoder->getNextPicture(index+ii-1, flags);
flags = 0;
iLogosFound += ndata->m_pCheckLogo->isLogo ? 1 : 0;
iCurrentDecodedFrame = index+ii;
}
index += CHECKLOGO_DIST;
index = cIF->GetNextIFrame( index, true, &FileNumber, &FileOffset, &Length, true);
flags = DEMUX_RESET;
}
decoder->setCallback(oldCallback);
dsyslog("checklogo for %d frames gives %d", CHECKLOGO_FRAMES, iLogosFound >= CHECKLOGO_FRAMES);
return iLogosFound >= CHECKLOGO_FRAMES;
}
#define CHECKLOGOSHORT_FRAMES 600
bool checkLogoShort(int startpos)
{
dsyslog("checklogo for %d frames", CHECKLOGO_FRAMES);
int iLogosFound = 0;
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(checkCallback);
int index = startpos;
int flags = DEMUX_RESET;
index = cIF->GetNextIFrame( index, true, &FileNumber, &FileOffset, &Length, true);
for( int i = 0; i < CHECKLOGO_BLOCKS && iLogosFound < CHECKLOGO_FRAMES; i++ )
{
for( int ii = 1; ii <= CHECKLOGO_BLOCKFRAMES && iLogosFound < CHECKLOGO_FRAMES; ii++ )
{
decoder->getNextPicture(index+ii-1, flags);
flags = 0;
iLogosFound += ndata->m_pCheckLogo->isLogo ? 1 : 0;
}
index += CHECKLOGO_SHORTDIST;
index = cIF->GetNextIFrame( index, true, &FileNumber, &FileOffset, &Length, true);
flags = DEMUX_RESET;
}
decoder->setCallback(oldCallback);
dsyslog("checklogo for %d frames gives %d", CHECKLOGO_FRAMES, iLogosFound >= CHECKLOGO_FRAMES);
return iLogosFound >= CHECKLOGO_FRAMES;
}
// try to detect a Logo within the next #FRAMES_TO_CHECK frames
bool doLogoDetection(int startIndex)
{
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(LogoDetectCallback);
int flags = DEMUX_RESET;
// go to the next I-Frame near curIndex
curIndex = cIF->GetNextIFrame( startIndex, true, &FileNumber, &FileOffset, &Length, true);
iLastIFrame = curIndex;
dsyslog("doLogoDetection %d %d", startIndex, curIndex);
while( curIndex >= 0 && !ndata->m_bFound && checkedFrames < FRAMES_TO_CHECK )
{
decoder->getNextPicture(curIndex, flags);
flags = 0;
curIndex++;
if( !cIF->Get( curIndex, &FileNumber, &FileOffset, &Independent, &Length) )
{
decoder->setCallback(oldCallback);
return false;
}
else
if(Independent)
iLastIFrame = curIndex;
}
decoder->setCallback(oldCallback);
return ndata->m_bFound;
}
void reInitNoad(int top, int bottom )
{
dsyslog("reInitNoad %d %d", top, bottom);
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
delete cctrl;
cctrl = NULL;
ndata->m_nTopLinesToIgnore = top;
ndata->m_nBottomLinesToIgnore = bottom;
ndata->initBuffer();
ndata->m_bFound = false;
ndata->m_nLogoCorner = UNKNOWN;
cctrl = new CControl(ndata);
}
// try to detect the Logo of the Record and verfiy it
// in some parts of the video
bool detectLogo( const char* logoname, int iStartFrame )
{
INFO("Logo-detection","");
time_t start;
time_t end;
reInitNoad( 0, 0 );
if( bReUseLogo && iStartFrame == 0 )
{
// try to reuse a saved logo
if( ndata->loadCheckData(logoname, true) )
{
start = time(NULL);
INFO(NULL,"re-use saved logo");
// we have a logo
// just check that it's ok
if( checkLogo( 0 ) )
{
INFO(NULL,"logo ok");
decodedFramesForLogoCheck = totalDecodedFrames;
end = time(NULL);
secsForLogoCheck = (int)(end - start);
return true;
}
}
}
// start a new logo-scan
start = time(NULL);
int iAStartpos[] =
{
0, int(8*FRAMESPERMIN), int(16*FRAMESPERMIN), int(32*FRAMESPERMIN),
int(5*FRAMESPERMIN), int(10*FRAMESPERMIN), int(15*FRAMESPERMIN),
int(20*FRAMESPERMIN), int(25*FRAMESPERMIN), int(30*FRAMESPERMIN),
-1
};
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
int iPart = 0;
INFO(NULL,"logo detection");
do
{
// detect from start
dsyslog("detectLogo part%d",iPart+1);
reInitNoad( 0,0 );
if( doLogoDetection(iAStartpos[iPart]+iStartFrame ) )
{
if( checkLogo( iAStartpos[iPart]+iStartFrame ) )
{
end = time(NULL);
secsForLogoDetection = (int)(end - start);
decodedFramesForLogoDetection = totalDecodedFrames;
dsyslog("logo detected %s",
ndata->m_nLogoCorner == TOP_LEFT ? "top left ":
ndata->m_nLogoCorner == TOP_RIGHT ? "top right ":
ndata->m_nLogoCorner == BOT_LEFT ? "bottom left ":
ndata->m_nLogoCorner == BOT_RIGHT ? "bottom right ": "unknown"
);
dsyslog("logo data:");
ndata->logCheckData();
dsyslog("logo data end");
INFO(NULL,"logo found");
return true;
}
}
iPart++;
} while( iAStartpos[iPart] >= 0);
end = time(NULL);
secsForLogoDetection = (int)(end - start);
decodedFramesForLogoDetection = totalDecodedFrames;
dsyslog("detectLogo: no Logo found, give up");
INFO(NULL,"no logo found!!");
return false;
}
#define FRAMESTOCHECK 10
int checkLogoState(int iState, int iCurrentFrame, int /*FramesToSkip*/, int FramesToCheck)
{
INFO(NULL,"checkLogoState");
int iLastIFrame = 0;
bool extLogoSearch = ndata->extLogoSearch;
ndata->extLogoSearch = false;
int flags = DEMUX_RESET;
dsyslog("checkLogoState for %d Frames starting at Frame %d",
FramesToCheck,iCurrentFrame);
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(checkCallback);
iCurrentFrame = cIF->GetNextIFrame( iCurrentFrame, true, &FileNumber, &FileOffset, &Length, true);
while( iCurrentFrame < cIF->Last() && iCurrentFrame >= 0 && FramesToCheck > 0 )
{
cIF->Get( iCurrentFrame, &FileNumber, &FileOffset, &Independent, &Length);
if( Independent == 1 /*I_FRAME*/ )
{
iLastIFrame = iCurrentFrame;
}
iCurrentDecodedFrame = iCurrentFrame;
checkedFrames = 0;
decoder->getNextPicture(iCurrentFrame, flags);
flags = 0;
if( iState != ndata->m_pCheckLogo->isLogo )
{
dsyslog("checkLogoState Logo lost, iLastIFrame is %d ", iLastIFrame);
ndata->extLogoSearch = extLogoSearch;
decoder->setCallback(oldCallback);
return( iLastIFrame );
}
iCurrentFrame ++;
FramesToCheck --;
}
decoder->setCallback(oldCallback);
ndata->extLogoSearch = extLogoSearch;
dsyslog("checkLogoState ends, iLastIFrame %d ", iLastIFrame);
return( 0 );
}
//#define CHANGE_SEARCH_BREAK FRAMESPERMIN*12
#define CHANGE_SEARCH_BREAK FRAMESPERMIN*15
int findLogoChange(int iState, int& iCurrentFrame,
int FramesToSkip, int repeatCheckframes)
{
INFO(NULL, "search Logo-change");
dsyslog("findLogoChange: iState %d, iCurrentFrame %d, iTotalFrames %d, FramesToSkip %d, repeatCheckframes=%d",
iState, iCurrentFrame, cIF->Last(), FramesToSkip, repeatCheckframes);
if(logoJumpDetection)
{
if( iState == 1 )
ndata->extLogoSearch = false;
else
ndata->extLogoSearch = true;
}
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(checkCallback);
int flags = DEMUX_RESET;
iCurrentFrame = getIFrameFor(cIF,iCurrentFrame);
iLastIFrame = iCurrentFrame;
int iEndFrame = iState == 0 ? iCurrentFrame+CHANGE_SEARCH_BREAK:cIF->Last();
Independent = 1; //I_FRAME
#ifdef VNOAD
bool bFirst = true;
#endif
iIgnoreFrames = 5;
while( iCurrentFrame < cIF->Last() && iCurrentFrame >= 0 && iCurrentFrame < iEndFrame )
{
checkedFrames = 0;
//iLastIFrame = iCurrentFrame;
while( checkedFrames < FRAMESTOCHECK && iCurrentFrame < cIF->Last() )
{
iCurrentDecodedFrame = iCurrentFrame;
decoder->getNextPicture(iCurrentFrame, flags);
flags = 0;
cIF->Get( iCurrentFrame, &FileNumber, &FileOffset, &Independent, &Length);
if( Independent == 1 /*I_FRAME*/ )
{
iLastIFrame = iCurrentFrame;
}
iCurrentFrame++;
}
#ifdef VNOAD
bFirst = false;
#endif
if( iState != ndata->m_pCheckLogo->isLogo )
{
dsyslog("StateChanged: iLastIFrame %d ", iLastIFrame);
if( repeatCheckframes > 0 )
{
// new found state must be stable over repeatCheckframes frames
int i = checkLogoState(ndata->m_pCheckLogo->isLogo, iCurrentFrame, framespersec*1, repeatCheckframes);
dsyslog("checkLogoState: i = %d",i);
if( i == 0 )
{
decoder->setCallback(oldCallback);
return( ndata->m_pCheckLogo->isLogo );
}
else
{
iCurrentFrame = i-FramesToSkip;
flags = DEMUX_RESET;
}
}
else
{
decoder->setCallback(oldCallback);
return( ndata->m_pCheckLogo->isLogo );
}
}
if( iCurrentFrame+FramesToSkip >= cIF->Last() )
{
decoder->setCallback(oldCallback);
return iState;
}
if( FramesToSkip > 1 )
{
iCurrentFrame += FramesToSkip;
iCurrentFrame = cIF->GetNextIFrame( iCurrentFrame, true, &FileNumber, &FileOffset, &Length, true);
iLastIFrame = iCurrentFrame;
Independent = 1;//I_FRAME;
flags = DEMUX_RESET;
iIgnoreFrames = 5;
}
}
decoder->setCallback(oldCallback);
if( iCurrentFrame >= iEndFrame )
return -2;
return( -1 );
}
void MarkToggle(cMarks *marks, int index, const char *Comment)
{
dsyslog( "ToggleMark: %d ", index);
cMark *m = marks->Get(index);
if (m)
{
dsyslog( "del Mark: %d ", m->position);
marks->Del(m);
}
else
{
dsyslog( "add Mark: %d ", index);
marks->Add(index);
if(markComments && Comment)
{
m = marks->Get(index);
if(m)
m->comment = strdup(Comment);
}
}
marks->Save();
bMarkChanged = true;
}
void moveMark( cMarks *marks, cMark *m, int iNewPos, const char *Comment)
{
char *cpOldPos, *cpNewPos, *newComment;
asprintf(&cpOldPos, "%s",(const char *)m->ToText(false,true));
asprintf(&cpNewPos, "%s",(const char *)IndexToHMSF(iNewPos, false));
asprintf(&newComment, Comment?Comment:"(%s)",(const char *)m->ToText(false,true) );
dsyslog( "moveMark from %d to %d (%s-->%s)",m->position, iNewPos, cpOldPos, cpNewPos );
marks->Del(m);
marks->Add(iNewPos);
cMark *mNew = marks->Get(iNewPos);
if(markComments && mNew && newComment)
mNew->comment = strdup(newComment);
marks->Save();
bMarkChanged = true;
free(cpOldPos);
free(cpNewPos);
free(newComment);
}
#define BACK_TIME 90
bool checkOnMark( cMarks *marks, cMark *m, bool bForward, int iCheckTime)
{
INFO(NULL, "checkOnMark");
dsyslog( "checkOnMark at %d",m->position );
int iDiff;
int iOffset = 0;
if( bForward )
{
if( m->position+iCheckTime*framespersec > cIF->Last() )
return false;
iDiff = framespersec;
iOffset = iCheckTime*framespersec;
}
else
{
if( m->position < iCheckTime*framespersec )
return false;
iDiff = -framespersec;
}
iDiff = -framespersec;
int index = cIF->GetNextIFrame( m->position+iOffset, true, &FileNumber, &FileOffset, &Length, true);
int flags = DEMUX_RESET;
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
for( int i = 0; i < iCheckTime; i++ )
{
for( int ii = 0; ii < framespersec; ii++)
{
decoder->getNextPicture(index+ii, flags );
flags = 0;
if( ndata->m_nBlackLinesTop > ndata->m_nGrabHeight/2 ||
ndata->m_nBlackLinesBottom > ndata->m_nGrabHeight/2 )
{
int newIndex = cIF->GetNextIFrame( index, true, &FileNumber, &FileOffset, &Length, true);
if( newIndex > 0 )
{
dsyslog( "do moveMark from %d to %d ",index, newIndex );
moveMark( marks, m, newIndex, "moved from [%s] by checkOnMark");
}
return true;
}
}
index = cIF->GetNextIFrame( index+iDiff, true, &FileNumber, &FileOffset, &Length, true);
flags = DEMUX_RESET;
}
return false;
}
bool checkBlacklineOnMark( cMarks *marks, cMark *m, bool bForward, int iCheckTime, int iTopLines, int iBottomLines)
{
INFO(NULL, "checkBlacklineOnMark");
#define POS_OK (ndata->m_nBlackLinesTop >= iTopMin \
&& ndata->m_nBlackLinesTop <= iTopMax \
&& ndata->m_nBlackLinesBottom >= ibotmin \
&& ndata->m_nBlackLinesBottom <= ibotmax)
#define POS_NOT_OK (ndata->m_nBlackLinesTop < iTopMin \
|| ndata->m_nBlackLinesBottom < ibotmin)
#define POS_MAYBE_OK (ndata->m_nBlackLinesTop > iTopMin \
&& ndata->m_nBlackLinesBottom > ibotmin)
#define PRE_POS_OK (ndata->m_nBlackLinesTop >= iTopLines && ndata->m_nBlackLinesBottom >= iBottomLines)
#define PRE_POS_RESET (ndata->m_nBlackLinesTop + ndata->m_nBlackLinesBottom < (iTopLines+iBottomLines)-(((iTopLines+iBottomLines)*20)/100))
// MAX_BEST_POS_RESET_COUNT - Anzahl Frames mit POS_NOT_OK == true,
// nach denen iBestPos zurückgesetzt wird
// 20.9.03: geändert auf 25 (vorher 3) wegen RTL/The 6th Day
#define MAX_BEST_POS_RESET_COUNT 90
dsyslog( "checkBlacklineOnMark at %d %s %d %d for %d frames",m->position, bForward ? "forward":"backward",iTopLines, iBottomLines, iCheckTime );
int iDiff;
int iOffset = 0;
int iBestPos = 0;
int iBestPosResetCount = 0;
int iPrePos = 0;
int iPrePosResetCount = 0;
int iEnd = m->position;
int iTopMax = iTopLines + iTopLines*30/100;
int iTopMin = iTopLines - iTopLines*20/100;
int ibotmax = iBottomLines + iBottomLines*30/100;
int ibotmin = iBottomLines - iBottomLines*30/100;
dsyslog( "check-range top %d %d, bott %d %d",iTopMin,iTopMax,ibotmin,ibotmax );
iOffset = 0;//iCheckTime*FRAMESPERSEC;
if( bForward )
{
if( m->position+iCheckTime > cIF->Last() )
return false;
iDiff = framespersec;
iEnd = m->position+iCheckTime;
iBestPos = iPrePos = m->position;
}
else
{
if( m->position < iCheckTime )
return false;
iOffset = iCheckTime;
iOffset *= -1;
iDiff = framespersec;
}
int index = getIFrameFor(cIF,m->position+iOffset-1);
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
int flags = DEMUX_RESET;
while( index < iEnd )
{
// get the next frame
iCurrentDecodedFrame = index;
ulTopBlackLines = 0;
ulBotBlackLines = 0;
decoder->getNextPicture(index,flags );
flags = 0;
if( iPrePos == 0 && PRE_POS_OK )
{
iPrePos = index;
iPrePosResetCount = 0;
dsyslog( "checkOnMark set prepos %d",iPrePos );
}
if( iPrePos != 0 && PRE_POS_RESET )
{
iPrePosResetCount++;
if(iPrePosResetCount > 3)
{
iPrePos = 0;
dsyslog( "checkOnMark preposreset at %d",index );
}
}
if( (iBestPos == 0) && (checkedFrames > 3) && POS_OK )
{
iBestPos = index;
dsyslog( "checkOnMark best pos = %d, prepos = %d",iBestPos, iPrePos );
iBestPosResetCount = 0;
}
else if( (iBestPos > 0) && (checkedFrames > 3) && POS_NOT_OK )
{
iBestPosResetCount++;
if(iBestPosResetCount > MAX_BEST_POS_RESET_COUNT)
{
dsyslog( "checkOnMark reset best pos to 0 at %d", index );
iBestPos = 0;
checkedFrames = 0;
}
}
++index;
}
dsyslog( "checkOnMark bestPos %d prepos %d",iBestPos,iPrePos );
if( (iBestPos == 0 || (iPrePos - iBestPos) < 200) && POS_MAYBE_OK && iPrePos > 0 )
{
iBestPos = iPrePos;
dsyslog( "checkOnMark use prepos to set newmark" );
}
if( iBestPos > 0 )
{
int newIndex = cIF->GetNextIFrame( iBestPos-1, true, &FileNumber, &FileOffset, &Length, true);
if( newIndex > 0 && newIndex != m->position)
{
dsyslog( "do moveMark from %d to %d, prepos = %d ",m->position, newIndex, iPrePos );
if(bForward)
moveMark( marks, m, newIndex,"moved from [%s] by checkBlacklineOnMark(forward)");
else
moveMark( marks, m, newIndex,"moved from [%s] by checkBlacklineOnMark(backward)");
return true;
}
if( newIndex == m->position)
{
dsyslog( "mark is on same position" );
if(!bForward)
return false;
}
}
return false;
#undef POS_OK
#undef POS_NOT_OK
#undef POS_MAYBE_OK
#undef PRE_POS_OK
#undef PRE_POS_RESET
}
bool checkBlacklineOffMark( cMarks *marks, cMark *m, int iCheckTime, int iTopLines, int iBottomLines)
{
INFO(NULL, "checkBlacklineOffMark");
#define POS_OK (ndata->m_nBlackLinesTop >= iTopMin \
&& ndata->m_nBlackLinesTop <= iTopMax \
&& ndata->m_nBlackLinesBottom >= ibotmin \
&& ndata->m_nBlackLinesBottom <= ibotmax)
#define POS_NOT_OK (ndata->m_nBlackLinesTop < iTopMin \
|| ndata->m_nBlackLinesBottom < ibotmin)
#define POS_MAYBE_OK (ndata->m_nBlackLinesTop > iTopMin \
&& ndata->m_nBlackLinesBottom > ibotmin)
#define PRE_POS_OK (ndata->m_nBlackLinesTop >= iTopLines && ndata->m_nBlackLinesBottom >= iBottomLines)
#define PRE_POS_RESET (ndata->m_nBlackLinesTop + ndata->m_nBlackLinesBottom < (iTopLines+iBottomLines)-(((iTopLines+iBottomLines)*20)/100))
dsyslog( "checkBlacklineOffMark at %d %d %d for %d frames",m->position, iTopLines, iBottomLines, iCheckTime );
int iDiff;
int iOffset = 0;
int iBestPos = 0;
int iPrePos = 0;
int iEnd = m->position;
int iTopMax = iTopLines + iTopLines*30/100;
int iTopMin = iTopLines - iTopLines*20/100;
int ibotmax = iBottomLines + iBottomLines*30/100;
int ibotmin = iBottomLines - iBottomLines*30/100;
int flags = DEMUX_RESET;
dsyslog( "check-range top %d %d, bott %d %d",iTopMin,iTopMax,ibotmin,ibotmax );
iOffset = 0;
if( m->position < iCheckTime )
return false;
iOffset = iCheckTime;
iOffset *= -1;
iDiff = framespersec;
int index = getIFrameFor(cIF,m->position+iOffset-1);
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
while( index < iEnd )
{
// get the next frame
iCurrentDecodedFrame = index;
ulTopBlackLines = 0;
ulBotBlackLines = 0;
decoder->getNextPicture(index, flags );
flags = 0;
if( (checkedFrames > 3) && POS_OK )
{
iBestPos = index;
}
++index;
}
dsyslog( "checkOffMark bestPos %d prepos %d",iBestPos,iPrePos );
if( (iBestPos == 0 || (iPrePos - iBestPos) < 200) && POS_MAYBE_OK && iPrePos > 0 )
{
iBestPos = iPrePos;
dsyslog( "checkOffMark use prepos to set newmark" );
}
if( iBestPos > 0 )
{
int newIndex = cIF->GetNextIFrame( iBestPos-1, true, &FileNumber, &FileOffset, &Length, true);
if( newIndex > 0 && newIndex != m->position)
{
dsyslog( "do moveMark from %d to %d, prepos = %d ",m->position, newIndex, iPrePos );
moveMark( marks, m, newIndex, "moved from [%s] by checkBlacklineOffMark");
return true;
}
if( newIndex == m->position)
{
dsyslog( "mark is on same position" );
}
}
return false;
#undef POS_OK
#undef POS_NOT_OK
#undef POS_MAYBE_OK
#undef PRE_POS_OK
#undef PRE_POS_RESET
}
#define MONOFRAME_CUTOFF 65
bool checkBlackFrameOnMark( cMarks *marks, cMark *m, bool bForward, int iCheckTime )
{
INFO(NULL, "checkBlackFrameOnMark");
int iOffset = 0;
int iBestPos = 0;
int iEnd = m->position;
bool bPosReset = true;
if( bForward )
{
if( m->position+iCheckTime*framespersec > cIF->Last() )
return false;
iEnd = m->position+iCheckTime*framespersec;
// don't walk over the next mark! (0.4.2)
cMark *mnext = marks->GetNext(m->position);
if( mnext != NULL && iEnd > mnext->position )
iEnd = mnext->position;
}
else
{
if( m->position < iCheckTime*framespersec )
return false;
iOffset = iCheckTime*framespersec;
iOffset *= -1;
}
int index = getIFrameFor(cIF, m->position+iOffset-1);
ulTopBlackLines = 0;
ulBotBlackLines = 0;
checkedFrames = 0;
noadCallback oldCallback = decoder->getCallback();
decoder->setCallback(BlackframeCallback);
int flags = DEMUX_RESET;
dsyslog( "checkBlackFrameOnMark at %d %s (frames %d --> %hhn)",m->position, bForward ? "forward":"backward",index,end );
while( index < iEnd )
{
iCurrentDecodedFrame = index;
ulTopBlackLines = 0;
ulBotBlackLines = 0;
decoder->getNextPicture(index, flags );
flags = 0;
if( bPosReset && ndata->isBlankFrame() )
{
dsyslog( "set iBestPos to %d",index);
iBestPos = index;
bPosReset = false;
}
if( !ndata->isBlankFrame() )
bPosReset = true;
index++;
}
decoder->setCallback(oldCallback);
if( iBestPos > 0 )
{
int newIndex = cIF->GetNextIFrame( iBestPos-1, true, &FileNumber, &FileOffset, &Length, true);
if( newIndex > 0 && newIndex < iEnd)
{
dsyslog( "do moveMark from %d to %d ",m->position, newIndex );
moveMark( marks, m, newIndex, "moved from[%s] by checkBlackFrameOnMark");
listMarks( marks);
}