File: markad-standalone.cpp

package info (click to toggle)
vdr-plugin-markad 4.2.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: cpp: 22,441; python: 613; makefile: 270; sh: 95
file content (7725 lines) | stat: -rw-r--r-- 457,046 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
/*
 * markad-standalone.cpp: A program for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "global.h"
#ifdef POSIX
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <ctype.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/sysinfo.h>
#include <locale.h>
#include <libintl.h>
#include <execinfo.h>
#include <mntent.h>
#include <utime.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#else
#include "win32/mingw64.h"
#endif

#include "markad-standalone.h"
#include "debug.h"
#include "audio.h"
#include "test.h"


bool SYSLOG                    = false;
bool LOG2REC                   = false;
bool restartLogoDetectionDone  = false;
int SysLogLevel                = 2;
bool abortNow                  = false;
int logoSearchTime_ms          = 0;
double decodeTime_ms           = 0;

struct timeval startAll, endAll = {};


#ifdef POSIX
static inline int ioprio_set(int which, int who, int ioprio) {
#if defined(__i386__)
#define __NR_ioprio_set         289
#elif defined(__ppc__)
#define __NR_ioprio_set         273
#elif defined(__x86_64__)
#define __NR_ioprio_set         251
#elif defined(__arm__)
#define __NR_ioprio_set         314
#elif defined(__aarch64__)
#define __NR_ioprio_set          30
#elif defined(__ia64__)
#define __NR_ioprio_set        1274
#else
#define __NR_ioprio_set           0
#endif
    if (__NR_ioprio_set) {
        return syscall(__NR_ioprio_set, which, who, ioprio);
    }
    else {
        fprintf(stderr,"set io prio not supported on this system\n");
        return 0; // just do nothing
    }
}


static inline int ioprio_get(int which, int who) {
#if defined(__i386__)
#define __NR_ioprio_get         290
#elif defined(__ppc__)
#define __NR_ioprio_get         274
#elif defined(__x86_64__)
#define __NR_ioprio_get         252
#elif defined(__arm__)
#define __NR_ioprio_get         315
#elif defined(__aarch64__)
#define __NR_ioprio_get          31
#elif defined(__ia64__)
#define __NR_ioprio_get        1275
#else
#define __NR_ioprio_get           0
#endif
    if (__NR_ioprio_get) {
        return syscall(__NR_ioprio_get, which, who);
    }
    else {
        fprintf(stderr,"get io prio not supported on this system\n");
        return 0; // just do nothing
    }

}


void syslog_with_tid(int priority, const char *format, ...) {
    va_list ap;
    if ((SYSLOG) && (!LOG2REC)) {
        priority = LOG_ERR;
        char fmt[255];
        snprintf(fmt, sizeof(fmt), "[%d] %s", getpid(), format);
        va_start(ap, format);
        vsyslog(priority, fmt, ap);
        va_end(ap);
    }
    else {
        char buf[27] = {0};
        const time_t now = time(nullptr);
        if (ctime_r(&now, buf)) {
            buf[strlen(buf) - 6] = 0;
        }
        else dsyslog("ctime_r failed");
        char fmt[255];
        char prioText[10];
        switch (priority) {
        case LOG_ERR:
            strcpy(prioText,"ERROR:");
            break;
        case LOG_INFO :
            strcpy(prioText,"INFO: ");
            break;
        case LOG_DEBUG:
            strcpy(prioText,"DEBUG:");
            break;
        case LOG_TRACE:
            strcpy(prioText,"TRACE:");
            break;
        default:
            strcpy(prioText,"?????:");
            break;
        }
        snprintf(fmt, sizeof(fmt), "%s%s [%d] %s %s", LOG2REC ? "":"markad: ", buf, getpid(), prioText, format);
        va_start(ap, format);
        vprintf(fmt, ap);
        va_end(ap);
        printf("\n");
        fflush(stdout);
    }
}

int get_number_of_cpus()
{
    return get_nprocs();
}

#endif /* #ifdef POSIX */

// startFrame:
// - before CheckStart: timer or VPS start time, negativ if recoring start after timer start
// - after  CheckStart: real start frame
void cMarkAdStandalone::CalculateCheckPositions(int startFrame) {
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): timer or VPS start:    (%6d)  %3d:%02dmin", startFrame, static_cast<int>(startFrame / decoder->GetVideoFrameRate() / 60), startFrame /  decoder->GetVideoFrameRate() % 60);

    if (!length) {
        dsyslog("CalculateCheckPositions(): length of recording not found, set to 100h");
        length = 100 * 60 * 60; // try anyway, set to 100h
        startFrame = decoder->GetVideoFrameRate() * 2 * 60;  // assume default pretimer of 2min
    }

    if (startFrame < 0) {   // recodring start is too late
        isyslog("recording started too late, set start mark to start of recording");
        sMarkAdMark mark = {};
        mark.position    = 1;  // do not use position 0 because this will later be deleted
        mark.framePTS    = index->GetStartPTS();
        mark.type        = MT_RECORDINGSTART;
        AddMark(&mark);
        startFrame = decoder->GetVideoFrameRate() * 6 * 60;  // give 6 minutes to get best mark type for this recording
    }

    startA = startFrame;
    stopA  = startA + decoder->GetVideoFrameRate() * length;
    packetCheckStart = startA + decoder->GetVideoFrameRate() * (1.6 * MAX_ASSUMED) ; //  adjust for later broadcast start, changed from 1.5 to 1.6
    packetCheckStop  = startFrame + decoder->GetVideoFrameRate() * (length + (1.5 * MAX_ASSUMED));
    packetEndPart    = stopA - (decoder->GetVideoFrameRate() * MAX_ASSUMED);

    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): length of recording:      %4ds  %3d:%02dmin", length, length / 60, length % 60);
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): assumed start frame:   (%6d)  %3d:%02dmin", startA, static_cast<int>(startA / decoder->GetVideoFrameRate() / 60), static_cast<int>(startA / decoder->GetVideoFrameRate()) % 60);
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): assumed stop frame:    (%6d)  %3d:%02dmin", stopA, static_cast<int>(stopA / decoder->GetVideoFrameRate() / 60), static_cast<int>(stopA / decoder->GetVideoFrameRate()) % 60);
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): check start set to:    (%6d)  %3d:%02dmin", packetCheckStart, static_cast<int>(packetCheckStart / decoder->GetVideoFrameRate() / 60), static_cast<int>(packetCheckStart / decoder->GetVideoFrameRate()) % 60);
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): check stop set to:     (%6d)  %3d:%02dmin", packetCheckStop, static_cast<int>(packetCheckStop / decoder->GetVideoFrameRate() / 60), static_cast<int>(packetCheckStop / decoder->GetVideoFrameRate()) % 60);
    dsyslog("cMarkAdStandalone::CalculateCheckPositions(): start end part set to: (%6d)  %3d:%02dmin", packetEndPart, static_cast<int>(packetEndPart / decoder->GetVideoFrameRate() / 60), static_cast<int>(packetEndPart / decoder->GetVideoFrameRate()) % 60);
}


// try MT_CHANNELSTOP
cMark *cMarkAdStandalone::Check_CHANNELSTOP() {
    // cleanup short channel stop/start pairs, they are stream errors
    cMark *channelStop = marks.GetNext(-1, MT_CHANNELSTOP);
    while (true) {
        if (!channelStop) break;
        cMark *channelStart = marks.GetNext(channelStop->position, MT_CHANNELSTART);
        if (!channelStart) break;
        int lengthChannel = 1000 * (channelStart->position - channelStop->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): channel stop (%6d) start (%6d): length %6dms", channelStop->position, channelStart->position, lengthChannel);
        if (lengthChannel <= 280) {
            dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): channel stop (%6d) start (%6d): length too short, delete marks", channelStop->position, channelStart->position);
            int tmp = channelStop->position;
            marks.Del(channelStop->position);
            marks.Del(channelStart->position);
            channelStop = marks.GetNext(tmp, MT_CHANNELSTOP);
        }
        else channelStop = marks.GetNext(channelStop->position, MT_CHANNELSTOP);
    }
    // search for channel stop mark
#define MAX_BEFORE_CHANNEL 336   // do not increase, will miss end in double episodes with 6 channels
    cMark *end = marks.GetAround(MAX_BEFORE_CHANNEL * decoder->GetVideoFrameRate(), stopA, MT_CHANNELSTOP);
    if (end) {
        int diffAssumed = (stopA - end->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): MT_CHANNELSTOP (%d) found %ds before assumed stop (%d)", end->position, diffAssumed, stopA);
    }
    else {
        // try last channel stop
        end = marks.GetPrev(INT_MAX, MT_CHANNELSTOP);
        if (end) {
            int diffAssumed = (stopA - end->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): last MT_CHANNELSTOP (%d) found %ds before assumed stop (%d)", end->position, diffAssumed, stopA);
            cMark *channelStartAfter = marks.GetNext(end->position, MT_CHANNELSTART);
            if (channelStartAfter) {
                int diffChannelStartstopA = (stopA - channelStartAfter->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): MT_CHANNELSTART (%d) %ds before assumed stop (%d)", channelStartAfter->position, diffChannelStartstopA, stopA);
                if ((diffAssumed > MAX_BEFORE_CHANNEL) && (diffChannelStartstopA > 7)) {
                    dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): last MT_CHANNELSTOP (%d) too far before assumed stop and MT_CHANNELSTART (%d) far before assumed stop", end->position, channelStartAfter->position);
                    // we have valid channel stop/start marks, but no channel end mark
                    // delete all logo marks between, they contains no valid logo stop mark
                    // prevent to select later false logo stop mark
                    marks.DelFromTo(end->position + 1, channelStartAfter->position - 1, MT_LOGOCHANGE, 0xF0);
                    end = nullptr;
                }
            }
            if (diffAssumed <= -547) { // changed from -561 to -547
                dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): last MT_CHANNELSTOP too far after assumed stop");
                end = nullptr;
            }
        }
    }
    // check if channel stop mark is valid end mark
    if (end) {
        cMark *cStart = marks.GetPrev(end->position, MT_CHANNELSTART);      // if there is short before a channel start, this stop mark belongs to next recording
        if (cStart) {
            if ((end->position - cStart->position) < (decoder->GetVideoFrameRate() * 120)) {
                dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): MT_CHANNELSTART short before MT_CHANNELSTOP found at frame %d with delta %ds, MT_CHANNELSTOP is not valid, try to find stop mark short before", cStart->position, static_cast<int> ((end->position - cStart->position) / decoder->GetVideoFrameRate()));
                end = marks.GetAround(decoder->GetVideoFrameRate() * 120, stopA - (decoder->GetVideoFrameRate() * 120), MT_CHANNELSTOP);
                if (end) dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): new MT_CHANNELSTOP found short before at frame (%d)", end->position);
            }
            else {
                cMark *cStartFirst = marks.GetNext(0, MT_CHANNELSTART);  // get first channel start mark
                cMark *movedFirst  = marks.First();                      // maybe first mark is a moved channel mark
                if (movedFirst && (movedFirst->type == MT_MOVEDSTART) && (movedFirst->oldType == MT_CHANNELSTART)) cStartFirst = movedFirst;
                if (cStartFirst) {
                    int deltaC = (end->position - cStartFirst->position) / decoder->GetVideoFrameRate();
                    if (deltaC < 183) {  // changed from 244 to 183, found shortest last part, do not reduce
                        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): first channel start mark (%d) and possible channel end mark (%d) to near %ds, this belongs to the next recording", cStartFirst->position, end->position, deltaC);
                        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): delete channel marks at (%d) and (%d)", cStartFirst->position, end->position);
                        marks.Del(cStartFirst->position);
                        marks.Del(end->position);
                        end = nullptr;
                    }
                }
            }
        }
    }
    if (criteria->GetMarkTypeState(MT_CHANNELCHANGE) >= CRITERIA_USED) {
        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): cleanup logo start/stop marks near by channel start marks, they are useless info logo");
        cMark *channelStart = marks.GetNext(-1, MT_CHANNELSTART);
        while (channelStart) {
#define CHANNEL_LOGO_MARK 60
            cMark *logoMark = marks.GetAround(CHANNEL_LOGO_MARK * decoder->GetVideoFrameRate(), channelStart->position, MT_LOGOCHANGE, 0xF0);
            while (logoMark) {
                int diff = abs((channelStart->position - logoMark->position) / decoder->GetVideoFrameRate());
                dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): delete logo mark (%d), %ds around channel start (%d)", logoMark->position, diff, channelStart->position);
                marks.Del(logoMark->position);
                logoMark = marks.GetAround(CHANNEL_LOGO_MARK * decoder->GetVideoFrameRate(), channelStart->position, MT_LOGOCHANGE, 0xF0);
            }
            channelStart = marks.GetNext(channelStart->position, MT_CHANNELSTART);
        }
    }

    if (end) {  // we found a channel end mark
        const cMark *startMark = marks.GetFirst();
        int startChannelPos = startMark->position;
        // take care of first 2 marks,
        // can be hborder start and hborder stop if we have two broadcasts with 6 channels and hborder only in second bronadcast in the recording
        // can be black screen start and hborder stop if we have two broadcasts with both 6 channels and hborder in the recording
        if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_AVAILABLE) {
            const cMark *nextMark  = marks.GetNext(startMark->position, MT_ALL);
            if (nextMark && (nextMark->type == MT_HBORDERSTOP)) startChannelPos = nextMark->position;
        }
        marks.DelWeakFromTo(startChannelPos + 1, end->position, MT_CHANNELCHANGE); // delete all weak marks, except start mark
        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): MT_CHANNELSTOP end mark (%d) found", end->position);
        return end;
    }
    else {
        dsyslog("cMarkAdStandalone::Check_CHANNELSTOP(): no MT_CHANNELSTOP mark found");
        return nullptr;
    }
}


// try MT_HBORDERSTOP
cMark *cMarkAdStandalone::Check_HBORDERSTOP() {
    cMark *end = nullptr;
    // cleanup very short hborder start/stop pair after detection restart, this can be a very long dark scene
    cMark *hStart = marks.GetNext(stopA - (decoder->GetVideoFrameRate() * 240), MT_HBORDERSTART);
    if (hStart) {
        cMark *hStop = marks.GetNext(hStart->position, MT_HBORDERSTOP);
        if (hStop && (hStop->position < stopA)) {
            int broadcastLength = (hStop->position - hStart->position) / decoder->GetVideoFrameRate();
            if (broadcastLength <= 92) {
                dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): found short hborder start (%d) stop (%d) pair, length %ds after detection restart, this can be a very long dark scene, delete marks", hStart->position, hStop->position, broadcastLength);
                marks.Del(hStart->position);
                marks.Del(hStop->position);
            }
        }
    }
    // search hborder stop mark around stopA
    end = marks.GetAround(2 * MAX_ASSUMED * decoder->GetVideoFrameRate(), stopA, MT_HBORDERSTOP);  // 10 minutes, more trust for hborder marks
    if (end) {
        dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): MT_HBORDERSTOP found at frame %i", end->position);
        cMark *prevHStart = marks.GetPrev(end->position, MT_HBORDERSTART);
        if (prevHStart && (prevHStart->position > stopA)) {
            dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): previous hborder start mark (%d) is after assumed stop (%d), hborder stop mark (%d) is invalid", prevHStart->position, stopA, end->position);
            // check if we got first hborder stop of next broadcast
            cMark *hBorderStopPrev = marks.GetPrev(end->position, MT_HBORDERSTOP);
            if (hBorderStopPrev) {
                int diff = (stopA - hBorderStopPrev->position) / decoder->GetVideoFrameRate();
                if (diff <= 476) { // maybe recording length is wrong
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): previous hborder stop mark (%d) is %ds before assumed stop, take this as stop mark", hBorderStopPrev->position, diff);
                    end = hBorderStopPrev;
                }
                else {
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): previous hborder stop mark (%d) is %ds before assumed stop, not valid", hBorderStopPrev->position, diff);
                    end = nullptr;
                }
            }
            else {
                end = nullptr;
            }
        }
    }
    else {
        if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED) {
            cMark *hBorderLast = marks.GetPrev(INT_MAX, MT_HBORDERCHANGE, 0xF0);
            if (hBorderLast && (hBorderLast->type == MT_HBORDERSTOP)) {
                int diffAssumed = (stopA - hBorderLast->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): last hboder mark (%d) is stop mark, %ds before assumed stop (%d)", hBorderLast->position, diffAssumed, stopA);
                // if we use MT_HBORDERCHANGE and last mark is MT_HBORDERSTOP, this is broadcast end
                // even it is far before assumed stop, maybe recording length is wrong
                if (diffAssumed <= (6 * MAX_ASSUMED)) {
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): last hboder mark stop (%d) selected as end mark", hBorderLast->position);
                    end = hBorderLast;
                }
            }
        }
    }
    // we found a hborder end mark
    if (end) {
        dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): found MT_HBORDERSTOP end mark (%d)", end->position);
        cMark *channelStart = marks.GetPrev(end->position, MT_CHANNELSTART);
        // cleanup channel start mark short before hborder stop, this is start mark of next broadcast
        if (channelStart) {
            int diff = 1000 * (end->position - channelStart->position) / decoder->GetVideoFrameRate();
            if (diff <= 1000) {
                marks.Del(channelStart->position);
                dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): channel start (%d) from next braodcast %dms before hborder stop (%d) found, delete channel mark", channelStart->position, diff, end->position);
            }
        }
        // optimize hborder end mark with logo stop mark in case of next broadcast is also with hborder (black closing credits or black opening credits)
        // check sequence MT_LOGOSTOP ->  MT_HBORDERSTOP (end) -> MT_HBORDERSTART
        cMark *logoStop     = marks.GetPrev(end->position, MT_LOGOSTOP);       // end of this boradcast
        cMark *hborderStart = marks.GetNext(end->position, MT_HBORDERSTART);   // start of next hborder broadcast
        if (logoStop && hborderStart) {
            int deltaLogoStop = (end->position - logoStop->position) / decoder->GetVideoFrameRate();
            int deltaAssumed  = (stopA        - logoStop->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): MT_LOGOSTOP at (%d) %ds before hborder stop, %ds before assumed stop found", logoStop->position, deltaLogoStop, deltaAssumed);
            if ((logoStop->position > hborderStart->position) && (deltaLogoStop <= 415) && (deltaAssumed <= 12)) {
                dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): use logo stop mark at (%d) before hborder stop (%d)", logoStop->position, end->position);
                end = logoStop;
                evaluateLogoStopStartPair->SetIsAdInFrameAroundStop(end->position, STATUS_DISABLED);  // prevent to false detect hborder as adinframe
            }
        }
        // optimize hborder end mark with logo stop mark in case of next broadcast is also with hborder and too early hborder stop from closing credits overlays hborder
        // check sequence MT_HBORDERSTOP (end) -> MT_LOGOSTOP -> MT_HBORDERSTART (start of next broadcast)
        const cMark *logoStart = marks.GetNext(end->position, MT_LOGOSTART);
        logoStop         = marks.GetNext(end->position, MT_LOGOSTOP);
        hborderStart     = marks.GetNext(end->position, MT_HBORDERSTART);
        if (logoStop && hborderStart &&
                (!logoStart || (logoStart->position > logoStop->position))) {  // no logo start between hborder stop (end mark) and logo stop
            int hBorderStopLogoStop  = 1000 * (logoStop->position     - end->position)      / decoder->GetVideoFrameRate();
            int logoStophBorderStart = 1000 * (hborderStart->position - logoStop->position) /  decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): MT_HBORDERSTOP (%d) -> %dms -> MT_LOGOSTOP (%d) -> %dms -> MT_HBORDERSTART (%d)", end->position, hBorderStopLogoStop, logoStop->position, logoStophBorderStart, hborderStart->position);
            // valid example
            // MT_HBORDERSTOP (196569) -> 28000ms -> MT_LOGOSTOP (197269) -> 6040ms -> MT_HBORDERSTART (197420) RTLZWEI
            if ((hBorderStopLogoStop <= 28000) && (logoStophBorderStart >= 0) && (logoStophBorderStart <= 6040)) {
                dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): hborder end mark (%d) from closing credits overlays hborder, use logo stop after (%d)", end->position, logoStop->position);
                end = logoStop;
                evaluateLogoStopStartPair->SetIsAdInFrameAroundStop(end->position, STATUS_DISABLED);  // closing credits overlay hborder, prevent to false detect this as adinframe
            }
        }
    }
    else dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): no MT_HBORDERSTOP end mark found");

    // cleanup false vborder stop/start from dark scene
    dsyslog("cMarkAdStandalone::Check_HBORDERSTOP(): cleanup false vborder stop/start from dark scene");
    if (end && criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED) {
        const cMark *stopMark  = end;  // can be logo stop mark selected above
        const cMark *startMark = marks.GetPrev(stopMark->position, MT_HBORDERSTART);
        while (stopMark && startMark) {
            marks.DelFromTo(startMark->position, stopMark->position, MT_VBORDERCHANGE, 0xF0);
            stopMark                = marks.GetPrev(startMark->position, MT_HBORDERSTOP);
            if (stopMark) startMark = marks.GetPrev(stopMark->position, MT_HBORDERSTART);
        }
    }
    return end;
}


// try MT_VBORDERSTOP
cMark *cMarkAdStandalone::Check_VBORDERSTOP() {
    cMark *end = marks.GetAround(360 * decoder->GetVideoFrameRate(), stopA, MT_VBORDERSTOP); // 3 minutes
    if (end) {
        int deltaStopA = (end->position - stopA) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): MT_VBORDERSTOP found at frame (%d), %ds after assumed stop", end->position, deltaStopA);
        if (deltaStopA >= 326) {  // we found start of first ad from next broadcast, changed from 353 to 326
            dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): MT_VBORDERSTOP too far after assumed stop, ignoring");
            return nullptr;
        }
        if (criteria->LogoInBorder()) { // not with random logo interruption
            cMark *logoStop = marks.GetPrev(end->position, MT_LOGOSTOP);
            if (logoStop) {
                int deltaLogoStop = 1000 * (end->position - logoStop->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): MT_LOGOSTOP at (%d) %d before assumed stop found", logoStop->position, deltaLogoStop);
                if (deltaLogoStop <= 2000) {
                    dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): use logo stop mark at (%d) short before vborder stop (%d)", logoStop->position, end->position);
                    end = logoStop;
                    CleanupUndetectedInfoLogo(end); // we are sure this is correct end, cleanup invalid logo marks
                }

            }
        }
        if (end->type == MT_VBORDERSTOP) { // we have not replaced vborder top with logo stop
            cMark *prevVStart = marks.GetPrev(end->position, MT_VBORDERSTART);
            if (prevVStart) {
                if (prevVStart->position > stopA) {
                    dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): previous vertial border start (%d) is after assumed stop (%d), delete this marks, they are form next brodcast", prevVStart->position, stopA);
                    marks.Del(prevVStart->position);
                    marks.Del(end->position);
                    end = nullptr;
                }
            }
            // we use vborder and we found final vborder end mark
            if (end && (criteria->GetMarkTypeState(MT_VBORDERCHANGE) == CRITERIA_USED)) {
                dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): vertial border end mark found, delete weak marks except start mark");
                marks.DelWeakFromTo(marks.GetFirst()->position + 1, INT_MAX, MT_VBORDERCHANGE);
            }
        }
    }
    else dsyslog("cMarkAdStandalone::Check_VBORDERSTOP(): no MT_VBORDERSTOP mark found");
    return end;
}


// try MT_LOGOSTOP
cMark *cMarkAdStandalone::Check_LOGOSTOP() {
    cMark *end         = nullptr;
    cMark *lEndAssumed = marks.GetAround(MAX_ASSUMED * decoder->GetVideoFrameRate(), stopA, MT_LOGOSTOP); // do not allow more than 5 minutes away from assumed stop
    if (!lEndAssumed) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): no logo stop mark found");
        return nullptr;  // no logo stop mark around assumed stop
    }

    // if not used until now, init object
    if (!evaluateLogoStopStartPair) {
        evaluateLogoStopStartPair = new cEvaluateLogoStopStartPair(decoder, criteria);
        ALLOC(sizeof(*evaluateLogoStopStartPair), "evaluateLogoStopStartPair");
    }
    else evaluateLogoStopStartPair->SetDecoder(decoder);

    cMark *lEnd = nullptr;
    // try to select best logo end mark based on closing credits follow
    if (criteria->IsClosingCreditsChannel()) {
        LogSeparator(false);
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): search for best logo end mark based on closing credits after logo stop");
        // search from nearest logo stop mark to end
        lEnd = lEndAssumed;
        while (!end && lEnd) {
            int diffAssumed = (lEnd->position - stopA) / decoder->GetVideoFrameRate();
            if (diffAssumed >= 0) {
                int status = evaluateLogoStopStartPair->GetIsClosingCreditsAfter(lEnd->position);
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): stop mark (%d): %ds after assumed stop (%d), closing credits status %d", lEnd->position, diffAssumed, stopA, status);
                if (diffAssumed > MAX_ASSUMED) break;
                if (status == STATUS_YES) {
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): stop mark (%d) closing credits follow, valid end mark found", lEnd->position);
                    end = lEnd;
                }
            }
            lEnd = marks.GetNext(lEnd->position, MT_LOGOSTOP);
        }
        // search before nearest logo stop mark
        lEnd = lEndAssumed;
        while (!end) {
            if (!lEnd) break;
            int diffAssumed = (stopA - lEnd->position) / decoder->GetVideoFrameRate();
            if (diffAssumed > 0) {
                int status = evaluateLogoStopStartPair->GetIsClosingCreditsAfter(lEnd->position);
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): stop mark (%d): %ds before assumed stop (%d), closing credits status %d", lEnd->position, diffAssumed, stopA, status);
                if (diffAssumed > MAX_ASSUMED) break;
                if (status == STATUS_YES) {
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): stop mark (%d) closing credits follow, valid end mark found", lEnd->position);
                    end = lEnd;
                }
            }
            lEnd = marks.GetPrev(lEnd->position, MT_LOGOSTOP);
        }
    }

    // try to select best logo end mark based on long black screen or silence
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): search for logo end mark based black screen, silence or closing logo sequence separator");
    // search from nearest logo stop mark to end
    lEnd = lEndAssumed;
    while (!end && lEnd) {
        int diffAssumed = (lEnd->position - stopA) / decoder->GetVideoFrameRate();
        LogSeparator(false);
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): check for separator for logo stop (%d), %ds after assumed end (%d)", lEnd->position, diffAssumed, stopA);
        if (diffAssumed > MAX_ASSUMED || diffAssumed < -MAX_ASSUMED) break;
        if (HaveBlackSeparator(lEnd) || HaveSilenceSeparator(lEnd) || HaveInfoLogoSequence(lEnd) || HaveLogoMissingSequence(lEnd)) {
            end = lEnd;
            break;
        }
        lEnd = marks.GetNext(lEnd->position, MT_LOGOSTOP);   // try next logo stop mark as end mark
    }
    // search before nearest logo stop mark
    lEnd = lEndAssumed;
    while (!end) {
        lEnd = marks.GetPrev(lEnd->position, MT_LOGOSTOP);   // try previous logo stop mark as end mark
        if (!lEnd) break;
        int diffAssumed = (stopA - lEnd->position) / decoder->GetVideoFrameRate();
        LogSeparator(false);
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): check for separator for logo stop (%d), %ds before assumed end (%d)", lEnd->position, diffAssumed, stopA);
        // examples:
        // valid: 330 before startA
        if (diffAssumed > MAX_ASSUMED) break;
        if (HaveBlackSeparator(lEnd) || HaveSilenceSeparator(lEnd) || HaveInfoLogoSequence(lEnd)) {
            end = lEnd;
        }
    }

    // logo end mark found based on separator or closing credits, cleanup undetected info logo stop/start marks
    if (end) {
        CleanupUndetectedInfoLogo(end);
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): found logo end mark (%d)", end->position);
        evaluateLogoStopStartPair->SetIsAdInFrameAroundStop(end->position, STATUS_DISABLED);  // before closing credits oder separator there is no ad in frame
        cMark *logoStart = marks.GetNext(end->position, MT_LOGOSTART);
        if (logoStart) {
            int diffStart = 1000 * (logoStart->position - end->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): next logo start mark (%d) %dms after end mark (%d)", logoStart->position, diffStart, end->position);
            if (diffStart <= 880) criteria->SetClosingCreditsState(end->position, CRITERIA_UNAVAILABLE);  // early logo start after, there are no closing credits without logo
        }
        return end;
    }

    // cleanup very short start/stop pairs around possible end marks, these are logo detection failures
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): search for nearest logo end mark to assumed stop");
    while (true) {
        end = marks.GetAround(400 * decoder->GetVideoFrameRate(), stopA, MT_LOGOSTOP);
        if (end) {
            int iStopDelta = (stopA - end->position) / decoder->GetVideoFrameRate();
// valid examples: 105, 117, 240, 292
#define MAX_LOGO_BEFORE_ASSUMED 292
            dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): MT_LOGOSTOP found at frame (%d), %ds (expect <= %ds) before assumed stop (%d)", end->position, iStopDelta, MAX_LOGO_BEFORE_ASSUMED, stopA);
            if (iStopDelta > MAX_LOGO_BEFORE_ASSUMED) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo stop mark too far before assumed stop");
                end = nullptr;
                break;
            }
            else {
                cMark *prevLogoStart = marks.GetPrev(end->position, MT_LOGOSTART);
                if (prevLogoStart) {
                    int deltaLogoStart = 1000 * (end->position - prevLogoStart->position) / decoder->GetVideoFrameRate();
#define MIN_LOGO_START_STOP 2480   // very short logo start/stop can be false positiv logo detection or preview in ad before, changed from 1480 to 2480
                    if (deltaLogoStart <= MIN_LOGO_START_STOP ) {
                        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): very short logo start (%d) stop (%d) pair is invalid, length %dms (expect >%ds), delete marks", prevLogoStart->position, end->position, deltaLogoStart, MIN_LOGO_START_STOP);
                        marks.Del(end);
                        marks.Del(prevLogoStart);
                    }
                    else {
                        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo start mark (%d) is %dms (expect >%dms) before logo stop mark (%d), logo stop mark is valid end mark", prevLogoStart->position, deltaLogoStart, MIN_LOGO_START_STOP, end->position);
                        break;
                    }
                }
                else {
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): no previous logo start mark found");
                    break;
                }
            }
        }
        else {
            dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): no more logo stop mark found");
            break;
        }
    }
    // for broadcast without hborder check border start mark from next bradcast before logo stop
    // in this case logo stop mark is from next recording, use border start mark as end mark
    bool typeChange = false;
    if (end && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) <= CRITERIA_UNKNOWN)) {
        cMark *hBorderStart = marks.GetPrev(end->position, MT_HBORDERSTART);
        if (hBorderStart) {
            const cMark *hBorderStartPrev = marks.GetPrev(hBorderStart->position, MT_HBORDERSTART);
            if (!hBorderStartPrev) {
                int deltahBorder = (hBorderStart->position - stopA) / decoder->GetVideoFrameRate();
                int deltaLogo    = (end->position          - stopA) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): found MT_HBORDERSTART at (%d) %ds after assumed end (and no other MT_HBORDERSTART before), logo stop mark at (%d) %ds after assumed end", hBorderStart->position, deltahBorder, end->position, deltaLogo);
                if ((deltaLogo >= 0) && (deltahBorder >= -1)) {
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo stop mark at (%d) %ds after assumed end is invalid, use MT_HBORDERSTART (%d) as end mark", end->position, deltaLogo, hBorderStart->position);
                    marks.ChangeType(hBorderStart, MT_STOP);
                    end = hBorderStart;
                    typeChange = true;
                }
            }
        }
    }

    // check if very eary logo end mark is end of preview
    if (end && !typeChange) {
        int beforeAssumed = (stopA - end->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): end mark (%d) %ds before assumed stop (%d)", end->position, beforeAssumed, stopA);
        if (beforeAssumed >= 218) {
            cMark *prevLogoStart = marks.GetPrev(end->position, MT_LOGOSTART);
            // ad before
            cMark *prevLogoStop = nullptr;
            if (prevLogoStart) prevLogoStop = marks.GetPrev(prevLogoStart->position, MT_LOGOSTOP);
            // broadcast after
            cMark *nextLogoStart = marks.GetNext(end->position, MT_LOGOSTART);
            const cMark *nextLogoStop = nullptr;
            if (nextLogoStart) nextLogoStop = marks.GetNext(end->position, MT_LOGOSTOP);

            if (prevLogoStart && prevLogoStop && nextLogoStart && !nextLogoStop) {  // debug log for future use
                int adBefore = (prevLogoStart->position - prevLogoStop->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): advertising before from (%d) to (%d) %3ds", prevLogoStart->position, prevLogoStop->position, adBefore);
                int adAfter = (nextLogoStart->position - end->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): advertising after  from (%d) to (%d) %3ds", end->position, nextLogoStart->position, adAfter);
                int broadcastBefore = (end->position - prevLogoStart->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): broadcast   before from (%d) to (%d) %3ds", prevLogoStart->position, end->position, broadcastBefore);

                if (broadcastBefore <= 115) {  // end mark invalid there is only a very short broadcast after end mark, changed from 34 to 115
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): broadcast before only %ds, end mark (%d) is invalid", broadcastBefore, end->position);
                    end = nullptr;
                }
            }
        }
    }

    // now we have a logo end mark
    if (end && criteria->GoodVPS()) { // check previous logo stop mark against VPS stop event, if any
        int vpsOffset = vps->GetStop(); // get VPS stop mark
        if (vpsOffset >= 0) {
            int vpsStopFrame = index->GetFrameFromOffset(vpsOffset * 1000);
            if (vpsStopFrame >= 0) {
                cMark *prevLogoStop = marks.GetPrev(end->position, MT_LOGOSTOP); // maybe different if deleted above
                if (prevLogoStop) {
                    int diffAfterVPS = (prevLogoStop->position - vpsStopFrame) / decoder->GetVideoFrameRate();
                    if (diffAfterVPS >= 0) {
                        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): VPS stop event at (%d) is %ds after previous logo stop (%d), use this as end mark", vpsStopFrame, diffAfterVPS, prevLogoStop->position);
                        end = prevLogoStop;
                    }
                }
                // final check against good VPS stop event
                if (macontext.Info.lengthFromVPS) {
                    int diffAfterVPS = (end->position - vpsStopFrame) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo end mark (%d) %ds after VPS stop event (%d)", end->position, diffAfterVPS, vpsStopFrame);
                    if (diffAfterVPS >= 87) {
                        dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo end mark too far after VPS stop event, assume preview before logo stop");
                        end = nullptr;
                    }
                }
            }
            else esyslog("cMarkAdStandalone::Check_LOGOSTOP(): get frame number to VPS stop offset at %ds failed", vpsOffset);
        }
        else dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): no VPS atop event found");
    }

    if (end) {
        // check if there could follow closing credits, prevent false detection of closing credits from opening creditis of next broadcast
        cMark *nextLogoStart = marks.GetNext(end->position);
        if (nextLogoStart) {
            int closingCreditsLength = (nextLogoStart->position - end->position) / decoder->GetVideoFrameRate();
            if (closingCreditsLength <= 2) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): logo start (%d) %ds after end mark (%d), no closing credits without logo can follow", nextLogoStart->position, closingCreditsLength, end->position);
                criteria->SetClosingCreditsState(end->position, CRITERIA_UNAVAILABLE);
            }
        }
        // if we use logo stop in recording with a channel mark double episode, no ad in frame can be before end mark because we found no channel stop mark
        if (criteria->GetMarkTypeState(MT_CHANNELCHANGE) == CRITERIA_USED) {
            dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): channel mark double episode, no ad in frame before end mark (%d)", end->position);
            evaluateLogoStopStartPair->SetIsAdInFrameAroundStop(end->position, STATUS_DISABLED);  // prevent to false detect ad in frame
        }
    }
    else dsyslog("cMarkAdStandalone::Check_LOGOSTOP(): no MT_LOGOSTOP mark found");
    return end;
}



// detect short logo stop/start short after final start mark or short before final end mark
// they can be logo detection failure, undetected info logos, introduction logos or text previews over the logo (e.g. SAT.1)
// short after a valid start mark/short before a valid stop mark can not be a valid logo stop/start pair
// only called if we are sure this is the correct logo start/end mark by closing credit detected or separator detected
// prevent to later move end mark/start mark to previous/after logo mark from invalid logo stop/start pairs
void cMarkAdStandalone::CleanupUndetectedInfoLogo(const cMark *mark) {
    if (!mark) return;
    if (mark->type == MT_LOGOSTART) { // cleanup logo
        while (true) {
            cMark *nextLogoStop = marks.GetNext(mark->position, MT_LOGOSTOP);
            if (!nextLogoStop) return;
            cMark *nextLogoStart = marks.GetNext(nextLogoStop->position, MT_LOGOSTART);
            if (!nextLogoStart) return;
            int deltaStop = 1000 * (nextLogoStop->position  - mark->position)         / decoder->GetVideoFrameRate();
            int adLength  = 1000 * (nextLogoStart->position - nextLogoStop->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::CleanupUndetectedInfoLogo(): MT_LOGOSTART (%5d) -> %6dms -> MT_LOGOSTOP (%5d) -> %6dms -> MT_LOGOSTART (%5d)", mark->position, deltaStop, nextLogoStop->position, adLength, nextLogoStart->position);
            // example of valid logo stop/start pair in start part
            // MT_LOGOSTART (12669) -> 146520ms -> MT_LOGOSTOP (16332) ->   1320ms -> MT_LOGOSTART (16365)  -> Comedy Central: short first broadcast part
            //
            // example of invald logo stop/start pairs in start part
            // MT_LOGOSTART ( 5343) ->  33280ms -> MT_LOGOSTOP ( 6175) ->   1240ms -> MT_LOGOSTART ( 6206)
            // MT_LOGOSTART ( 5439) ->  33320ms -> MT_LOGOSTOP ( 6272) ->   1120ms -> MT_LOGOSTART ( 6300)
            // MT_LOGOSTART ( 5439) ->  41040ms -> MT_LOGOSTOP ( 6465) ->    400ms -> MT_LOGOSTART ( 6475)
            // MT_LOGOSTART (13421) ->  55220ms -> MT_LOGOSTOP (16182) ->    900ms -> MT_LOGOSTART (16227)  -> arte HD: logo detection failure
            // MT_LOGOSTART ( 9442) -> 157000ms -> MT_LOGOSTOP (13367) ->   6160ms -> MT_LOGOSTART (13521)  -> Nickelodeon: logo detection failure  (conflict)
            if ((deltaStop <= 55220) && (adLength <= 1240)) {
                dsyslog("cMarkAdStandalone::CleanupUndetectedInfoLogo(): logo detection failure or undetected info logo from (%d) to (%d), delete marks", nextLogoStop->position, nextLogoStart->position);
                marks.Del(nextLogoStop->position);
                marks.Del(nextLogoStart->position);
            }
            else return;
        }
    }
    if (mark->type == MT_LOGOSTOP) {  // cleanup logo stop/start marks short before end mark
        while (true) {
            cMark *prevLogoStart = marks.GetPrev(mark->position, MT_LOGOSTART);
            if (!prevLogoStart) return;
            cMark *prevLogoStop = marks.GetPrev(prevLogoStart->position, MT_LOGOSTOP);
            if (!prevLogoStop) return;
            int stopStart = 1000 * (prevLogoStart->position - prevLogoStop->position)  / decoder->GetVideoFrameRate();
            int startEnd  = 1000 * (mark->position          - prevLogoStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::CleanupUndetectedInfoLogo(): logo stop (%d) -> %dms -> start (%d) -> %dms -> end mark (%d)", prevLogoStop->position, stopStart, prevLogoStart->position, startEnd, mark->position);
            // no info logo sequence, do not delete
            // logo stop (99273) -> 14760ms -> start (99642) -> 9400ms -> end mark (99877)
            //
            // info logo sequence before end mark, delete marks
            // logo stop (77361) -> 12840ms -> start (77682) -> 71680ms -> end mark (79474)
            // logo stop (78852) -> 12720ms -> start (79170) -> 75400ms -> end mark (81055)
            if ((stopStart <= 30000) &&
                    (startEnd > 9400) && (startEnd <= 75400)) {
                dsyslog("cMarkAdStandalone::CleanupUndetectedInfoLogo(): logo start (%5d) stop (%5d): undetected info logo or text preview over the logo, delete marks", prevLogoStart->position, prevLogoStop->position);
                marks.Del(prevLogoStart);
                marks.Del(prevLogoStop);
            }
            else return;
        }
    }
}


bool cMarkAdStandalone::HaveLowerBorder(const cMark *mark) {
    if (!mark) return false;
    if ((mark->type & 0x0F) == MT_START) {
        // check sequence MT_NOLOWERBORDERSTART -> MT_NOLOWERBORDERSTOP -> MT_LOGOSTOP -> MT_LOGOSTART (mark) [-> MT_LOGOSTOP]
        cMark *prevLogoStop = marks.GetPrev(mark->position, MT_LOGOSTOP);
        if (prevLogoStop) {
            cMark *lowerStop = blackMarks.GetPrev(prevLogoStop->position, MT_NOLOWERBORDERSTART);
            if (lowerStop) {
                cMark *lowerStart = blackMarks.GetPrev(lowerStop->position, MT_NOLOWERBORDERSTOP);
                if (lowerStart) {
                    int diffLogoStartLogoStop = INT_MAX;
                    int nextLogoStopPosition  = INT_MAX;
                    const cMark *nextLogoStop = marks.GetNext(mark->position, MT_LOGOSTOP);
                    if (nextLogoStop) {
                        nextLogoStopPosition  = nextLogoStop->position;
                        diffLogoStartLogoStop = 1000 * (nextLogoStop->position - mark->position) / decoder->GetVideoFrameRate();
                    }
                    int diffLowerStartLowerStop = 1000 * (lowerStop->position    - lowerStart->position)   / decoder->GetVideoFrameRate();
                    int diffLowerStopLogoStop   = 1000 * (prevLogoStop->position - lowerStop->position)    / decoder->GetVideoFrameRate();
                    int diffLogoStopLogoStart   = 1000 * (mark->position         - prevLogoStop->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveLowerBorder(): MT_NOLOWERBORDERSTART (%d) -> %dms -> MT_NOLOWERBORDERSTOP (%d) -> %dms -> MT_LOGOSTOP (%d) -> %dms -> MT_LOGOSTART (%d) -> %dms -> MT_LOGOSTOP (%d)", lowerStart->position, diffLowerStartLowerStop, lowerStop->position, diffLowerStopLogoStop, prevLogoStop->position, diffLogoStopLogoStart, mark->position, diffLogoStartLogoStop, nextLogoStopPosition);
// valid example
// MT_NOLOWERBORDERSTART (3723) -> 4520ms -> MT_NOLOWERBORDERSTOP (3836) -> 7360ms -> MT_LOGOSTOP (4020) -> 120ms -> MT_LOGOSTART (4023) -> 2147483647ms -> MT_LOGOSTOP (2147483647)
//
// invalid example
// MT_NOLOWERBORDERSTART (8231) -> 4440ms -> MT_NOLOWERBORDERSTOP (8342) -> 240ms -> MT_LOGOSTOP (8348) -> 160ms -> MT_LOGOSTART (8352) -> 6960ms -> MT_LOGOSTOP (8526)
                    if ((diffLowerStartLowerStop >= MIN_LOWER_BORDER) && (diffLowerStartLowerStop <= MAX_LOWER_BORDER) &&
                            (diffLowerStopLogoStop <= 7360) && (diffLogoStopLogoStart <= 120) && (diffLogoStartLogoStop > 6960)) {
                        dsyslog("cMarkAdStandalone::HaveLowerBorder(): logo start mark (%d): lower border closing credits before are valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveLowerBorder(): logo start mark (%d): lower border closing credits are invalid", mark->position);
                }
            }
        }
    }
    return false;
}


bool cMarkAdStandalone::HaveSilenceSeparator(const cMark *mark) {
    if (!mark) return false;
    // check start mark
    if (mark->type == MT_LOGOSTART) {
        // check sequence MT_LOGOSTOP -> MT_SOUNDSTOP -> MT_SOUNDSTART -> MT_LOGOSTART (mark) -> MT_LOGOSTOP
        // long broadcast part after valid logo start mark required to prevent false positiv
        cMark *silenceStop  = silenceMarks.GetPrev(mark->position, MT_SOUNDSTART);
        const cMark *nextLogoStop = marks.GetNext(mark->position, MT_LOGOSTOP);
        int nextLogoStopPosition = decoder->GetPacketNumber();  // start mark can be last mark from detected start part, use current read position
        if (nextLogoStop) nextLogoStopPosition = nextLogoStop->position;
        if (silenceStop) {
            cMark *silenceStart = silenceMarks.GetPrev(silenceStop->position, MT_SOUNDSTOP);
            if (silenceStart) {
                cMark *logoStop = marks.GetPrev(silenceStart->position, MT_LOGOSTOP);
                if (logoStop) {
                    int logoStopSilenceStart    = 1000 * (silenceStart->position - logoStop->position)     / decoder->GetVideoFrameRate();
                    int silenceStartSilenceStop = 1000 * (silenceStop->position  - silenceStart->position) / decoder->GetVideoFrameRate();
                    int silenceStopLogoStart    = 1000 * (mark->position         - silenceStop->position)  / decoder->GetVideoFrameRate();
                    int logoStartLogoStop       = 1000 * (nextLogoStopPosition   - mark->position)         / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_LOGOSTOP (%5d) -> %6dms -> MT_SOUNDSTOP (%5d) -> %5dms -> MT_SOUNDSTART (%5d) -> %5dms -> MT_LOGOSTART (%5d) -> %dms -> MT_LOGOSTOP (%d) -> %s", logoStop->position, logoStopSilenceStart, silenceStart->position, silenceStartSilenceStop, silenceStop->position, silenceStopLogoStart, mark->position, logoStartLogoStop, nextLogoStopPosition, macontext.Info.ChannelName);
// valid logo start example
// MT_LOGOSTOP ( 4496) ->    560ms -> MT_SOUNDSTOP ( 4510) ->   160ms -> MT_SOUNDSTART ( 4514) ->  1520ms -> MT_LOGOSTART ( 4548)
// MT_LOGOSTOP ( 3536) ->   1520ms -> MT_SOUNDSTOP ( 3574) ->   440ms -> MT_SOUNDSTART ( 3585) ->  1880ms -> MT_LOGOSTART ( 3632)
// MT_LOGOSTOP ( 6960) ->   6120ms -> MT_SOUNDSTOP ( 7113) ->   160ms -> MT_SOUNDSTART ( 7117) ->    40ms -> MT_LOGOSTART ( 7118)                                    -> VOX
// MT_LOGOSTOP (11369) ->   5160ms -> MT_SOUNDSTOP (11498) ->   760ms -> MT_SOUNDSTART (11517) ->    80ms -> MT_LOGOSTART (11519)                                    -> Comedy Central
// MT_LOGOSTOP (12724) ->  13520ms -> MT_SOUNDSTOP (13062) ->   360ms -> MT_SOUNDSTART (13071) ->   120ms -> MT_LOGOSTART (13074)                                    -> Comedy Central
// MT_LOGOSTOP ( 5923) ->  10200ms -> MT_SOUNDSTOP ( 6178) ->   840ms -> MT_SOUNDSTART ( 6199) ->    80ms -> MT_LOGOSTART ( 6201)                                    -> Comedy Central
// MT_LOGOSTOP (10171) ->  12280ms -> MT_SOUNDSTOP (10478) ->   880ms -> MT_SOUNDSTART (10500) ->  1240ms -> MT_LOGOSTART (10531)                                    -> DMAX
// MT_LOGOSTOP (10074) -> 125760ms -> MT_SOUNDSTOP (13218) ->   120ms -> MT_SOUNDSTART (13221) ->   680ms -> MT_LOGOSTART (13238) -> 251520ms -> MT_LOGOSTOP (19526) -> Comedy_Central
//
// invalid logo start example
// MT_LOGOSTOP (  204) ->  28460ms -> MT_SOUNDSTOP ( 1627) ->   240ms -> MT_SOUNDSTART ( 1639) ->  1840ms -> MT_LOGOSTART ( 1731)
// MT_LOGOSTOP (10728) ->     80ms -> MT_SOUNDSTOP (10730) ->   480ms -> MT_SOUNDSTART (10742) ->   600ms -> MT_LOGOSTART (10757)
// MT_LOGOSTOP ( 1984) ->   6840ms -> MT_SOUNDSTOP ( 2155) ->   280ms -> MT_SOUNDSTART ( 2162) ->  1840ms -> MT_LOGOSTART ( 2208) -> 21520ms -> MT_LOGOSTOP (2746)
// MT_LOGOSTOP ( 1393) ->    480ms -> MT_SOUNDSTOP ( 1405) ->   280ms -> MT_SOUNDSTART ( 1412) ->   360ms -> MT_LOGOSTART ( 1421) -> 22520ms -> MT_LOGOSTOP (1984)
// MT_LOGOSTOP ( 4160) ->    760ms -> MT_SOUNDSTOP ( 4179) ->   400ms -> MT_SOUNDSTART ( 4189) ->  1440ms -> MT_LOGOSTART ( 4225) -> 28360ms -> MT_LOGOSTOP (4934) -> RTL Television
                    if (    (logoStopSilenceStart    >= 560) && (logoStopSilenceStart    <= 125760) &&
                            (silenceStartSilenceStop >= 120) && (silenceStartSilenceStop <=    880) &&
                            (silenceStopLogoStart    >=  40) && (silenceStopLogoStart    <=   1880) &&
                            (logoStartLogoStop > 28360)) {
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence before logo start is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence before logo start is invalid", mark->position);
                }
            }
        }
        // check sequence MT_LOGOSTOP -> MT_SOUNDSTOP -> MT_LOGOSTART -> MT_SOUNDSTART
        // silence around logo start
        silenceStop = silenceMarks.GetNext(mark->position - 1, MT_SOUNDSTART);   // end of silence can be on same frame as logo start
        if (silenceStop) {
            cMark *silenceStart = silenceMarks.GetPrev(mark->position, MT_SOUNDSTOP);
            if (silenceStart) {
                cMark *logoStop = marks.GetPrev(silenceStart->position, MT_LOGOSTOP);
                if (logoStop) {
                    int logoStopSilenceStart  = 1000 * (silenceStart->position - logoStop->position)     / decoder->GetVideoFrameRate();
                    int silenceStartLogoStart = 1000 * (mark->position         - silenceStart->position) / decoder->GetVideoFrameRate();
                    int logoStartSilenceStop  = 1000 * (silenceStop->position  - mark->position)         / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_LOGOSTOP (%6d) -> %5dms -> MT_SOUNDSTOP (%6d) -> %4dms -> MT_LOGOSTART (%6d) -> %4dms -> MT_SOUNDSTART (%6d) -> %s", logoStop->position, logoStopSilenceStart, silenceStart->position, silenceStartLogoStart, mark->position, logoStartSilenceStop, silenceStop->position, macontext.Info.ChannelName);
// valid example
// MT_LOGOSTOP (  6556) -> 42040ms -> MT_SOUNDSTOP (  7607) ->  360ms -> MT_LOGOSTART (  7616) ->   40ms -> MT_SOUNDSTART (  7617) -> RTL Television
// MT_LOGOSTOP (  6774) -> 36560ms -> MT_SOUNDSTOP (  7688) ->  360ms -> MT_LOGOSTART (  7697) ->    0ms -> MT_SOUNDSTART (  7697) -> RTL Television
// MT_LOGOSTOP ( 13138) -> 13520ms -> MT_SOUNDSTOP ( 13476) ->  480ms -> MT_LOGOSTART ( 13488) ->   80ms -> MT_SOUNDSTART ( 13490) -> Comedy Central
// MT_LOGOSTOP (  9990) -> 13800ms -> MT_SOUNDSTOP ( 10335) ->  200ms -> MT_LOGOSTART ( 10340) ->   80ms -> MT_SOUNDSTART ( 10342) -> Comedy Central
// MT_LOGOSTOP ( 13676) ->  5760ms -> MT_SOUNDSTOP ( 13820) ->   40ms -> MT_LOGOSTART ( 13821) ->   40ms -> MT_SOUNDSTART ( 13822) -> Comedy_Central
//
// invalid example
// MT_LOGOSTOP (  1358) ->   920ms -> MT_SOUNDSTOP (  1381) ->  280ms -> MT_LOGOSTART (  1388) ->    0ms -> MT_SOUNDSTART (  1388) -> RTL Television preview start
// MT_LOGOSTOP (   181) ->  4760ms -> MT_SOUNDSTOP (   300) ->  200ms -> MT_LOGOSTART (   305) ->    0ms -> MT_SOUNDSTART (   305) -> RTL Television preview start
// MT_LOGOSTOP (   277) -> 26920ms -> MT_SOUNDSTOP (   950) ->  160ms -> MT_LOGOSTART (   954) ->   40ms -> MT_SOUNDSTART (   955) -> RTL Television preview start (conflict)

                    if (    (logoStopSilenceStart  >= 5760) && (logoStopSilenceStart  <= 42040) &&
                            (silenceStartLogoStart >=   40) && (silenceStartLogoStart <=   480) &&
                            (logoStartSilenceStop  <=   80)) {
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence around logo start mark is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence around logo start mark is invalid", mark->position);
                }
            }
        }
        // check sequence MT_SOUNDSTOP -> MT_SOUNDSTART -> MT_LOGOSTOP -> MT_LOGOSTART (mark)
        // in this case logo stop mark is a valid end mark from previous broadcast, far away next logo start mark is valid start mark
        cMark *logoStop = marks.GetPrev(mark->position, MT_LOGOSTOP);
        if (logoStop) {
            silenceStop = silenceMarks.GetPrev(logoStop->position, MT_SOUNDSTART);
            if (silenceStop) {
                cMark *silenceStart = silenceMarks.GetPrev(silenceStop->position, MT_SOUNDSTOP);
                if (silenceStart) {
                    int silenceStarSilenceStop = 1000 * (silenceStop->position  - silenceStart->position) / decoder->GetVideoFrameRate();
                    int silenceStopLogoStop    = 1000 * (logoStop->position     - silenceStop->position)  / decoder->GetVideoFrameRate();
                    int logoStopLogoStart      = 1000 * (mark->position         - logoStop->position)     / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_SOUNDSTOP (%6d) -> %4dms -> MT_SOUNDSTART (%6d) -> %4dms -> MT_LOGOSTOP (%6d) -> %4dms -> MT_LOGOSTART (%6d)", silenceStart->position, silenceStarSilenceStop, silenceStop->position, silenceStopLogoStop, logoStop->position, logoStopLogoStart, mark->position);
                    // valid example
                    // MT_SOUNDSTOP ( 17992) ->  680ms -> MT_SOUNDSTART ( 18026) ->  400ms -> MT_LOGOSTOP ( 18046) -> 29980ms -> MT_LOGOSTART ( 19545) -> phoenix HD
                    if ((silenceStarSilenceStop >= 680) && (silenceStopLogoStop <= 400) && (logoStopLogoStart >= 29980)) {
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence sequence is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence sequence is invalid", mark->position);
                }
            }
        }

        // check sequence MT_LOGOSTART (mark) -> MT_SOUNDSTOP -> MT_SOUNDSTART
        // broadcast start with long silence in opening credits
        cMark *silenceStart = silenceMarks.GetNext(mark->position, MT_SOUNDSTOP);
        if (silenceStart) {
            silenceStop = silenceMarks.GetNext(silenceStart->position, MT_SOUNDSTART);
            if (silenceStop) {
                int diffLogoStartSilenceStart   = 1000 * (silenceStart->position  - mark->position)         / decoder->GetVideoFrameRate();
                int diffSilenceStartSilenceStop = 1000 * (silenceStop->position   - silenceStart->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_LOGOSTART (%6d) -> %4dms -> MT_SOUNDSTOP (%6d) -> %4dms -> MT_SOUNDSTART (%6d)", mark->position, diffLogoStartSilenceStart, silenceStart->position, diffSilenceStartSilenceStop, silenceStop->position);
                // valid example
                // MT_LOGOSTART (  8632) ->  400ms -> MT_SOUNDSTOP (  8642) -> 5720ms -> MT_SOUNDSTART (  8785)

                if ((diffLogoStartSilenceStart <= 400) && (diffSilenceStartSilenceStop >= 5720)) {
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence sequence is valid", mark->position);
                    return true;
                }
                dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo start mark (%d): silence sequence is invalid", mark->position);
            }
        }
    }

    // check stop mark
    if (mark->type == MT_LOGOSTOP) {
        // sequence MT_LOGOSTART -> MT_LOGOSTOP -> MT_SOUNDSTOP -> MT_SOUNDSTART -> MT_LOGOSTART
        // silence between logo stop and logo start of next broadcast
        cMark *logoStartBefore = marks.GetPrev(mark->position, MT_LOGOSTART);
        if (logoStartBefore) {
            cMark *silenceStart = silenceMarks.GetNext(mark->position - 1, MT_SOUNDSTOP); // silence can start at the same position as logo stop
            if (silenceStart) {
                cMark *silenceStop = silenceMarks.GetNext(silenceStart->position, MT_SOUNDSTART);
                if (silenceStop) {
                    cMark *startAfter = marks.GetNext(silenceStop->position, MT_START, 0x0F);
                    if (startAfter) {
                        int logoStartLogoStop       = 1000 * (mark->position         - logoStartBefore->position) / decoder->GetVideoFrameRate();
                        int logoStopSilenceStart    = 1000 * (silenceStart->position - mark->position)            / decoder->GetVideoFrameRate();
                        int silenceStartSilenceStop = 1000 * (silenceStop->position  - silenceStart->position)    / decoder->GetVideoFrameRate();
                        int silenceStopLogoStart    = 1000 * (startAfter->position   - silenceStop->position)     / decoder->GetVideoFrameRate();
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_LOGOSTART (%6d) -> %7ds -> MT_LOGOSTOP (%6d) -> %4dms -> MT_SOUNDSTOP (%6d) -> %4dms -> MT_SOUNDSTART (%6d) -> %5dms -> MT_START (%6d) -> %s", logoStartBefore->position, logoStartLogoStop, mark->position, logoStopSilenceStart, silenceStart->position, silenceStartSilenceStop, silenceStop->position, silenceStopLogoStart, startAfter->position, macontext.Info.ChannelName);
// valid sequence
// MT_LOGOSTART ( 16056) -> 1505140s -> MT_LOGOSTOP ( 91313) ->  140ms -> MT_SOUNDSTOP ( 91320) ->  180ms -> MT_SOUNDSTART ( 91329) ->  1920ms -> MT_START ( 91425) -> RTL Television
// MT_LOGOSTART ( 84526) ->   10400s -> MT_LOGOSTOP ( 84786) ->  160ms -> MT_SOUNDSTOP ( 84790) ->   80ms -> MT_SOUNDSTART ( 84792) ->  1920ms -> MT_START ( 84840) -> Pro7 MAXX
// MT_LOGOSTART ( 81870) ->   10400s -> MT_LOGOSTOP ( 82130) ->    0ms -> MT_SOUNDSTOP ( 82130) ->  320ms -> MT_SOUNDSTART ( 82138) ->  1840ms -> MT_START ( 82184) -> Pro7 MAXX
// MT_LOGOSTART ( 56844) ->   31760s -> MT_LOGOSTOP ( 57638) ->  840ms -> MT_SOUNDSTOP ( 57659) ->  440ms -> MT_SOUNDSTART ( 57670) ->  2360ms -> MT_START ( 57729) -> Comedy Central
// MT_LOGOSTART ( 56372) ->   26000s -> MT_LOGOSTOP ( 57022) ->   80ms -> MT_SOUNDSTOP ( 57024) -> 1160ms -> MT_SOUNDSTART ( 57053) ->  2440ms -> MT_START ( 57114) -> Comedy Central
// MT_LOGOSTART ( 79759) ->  224920s -> MT_LOGOSTOP ( 85382) ->  200ms -> MT_SOUNDSTOP ( 85387) ->   40ms -> MT_SOUNDSTART ( 85388) -> 27560ms -> MT_START ( 86077) -> VOX
// MT_LOGOSTART ( 79759) ->  224880s -> MT_LOGOSTOP ( 85381) ->  240ms -> MT_SOUNDSTOP ( 85387) ->   40ms -> MT_SOUNDSTART ( 85388) ->   240ms -> MT_START ( 85394) -> VOX
// MT_LOGOSTART ( 96625) ->   11840s -> MT_LOGOSTOP ( 96921) -> 1640ms -> MT_SOUNDSTOP ( 96962) ->   80ms -> MT_SOUNDSTART ( 96964) ->  1320ms -> MT_START ( 96997) -> TLC
// MT_LOGOSTART ( 37067) ->   26000s -> MT_LOGOSTOP ( 37717) ->  880ms -> MT_SOUNDSTOP ( 37739) ->  160ms -> MT_SOUNDSTART ( 37743) -> 14960ms -> MT_LOGOT ( 38117) -> Comedy_Central
// MT_LOGOSTART ( 55133) ->   37680s -> MT_LOGOSTOP ( 56075) ->  840ms -> MT_SOUNDSTOP ( 56096) ->   40ms -> MT_SOUNDSTART ( 56097) ->  2120ms -> MT_START ( 56150) -> Comedy_Central
//
// invalid sequence
// MT_LOGOSTART ( 87985) ->    1400s -> MT_LOGOSTOP ( 88020) -> 4840ms -> MT_SOUNDSTOP ( 88141) ->  160ms -> MT_SOUNDSTART ( 88145) ->  8400ms -> MT_START ( 88355) -> DMAX
// MT_LOGOSTART ( 48324) ->   95520s -> MT_LOGOSTOP ( 50712) ->  240ms -> MT_SOUNDSTOP ( 50718) ->  320ms -> MT_SOUNDSTART ( 50726) -> 12720ms -> MT_START ( 51044) -> Comedy_Central
                        if (  (((logoStartLogoStop >= 10400)  && (logoStartLogoStop <= 37680)) ||  // end of undetected info logo or short logo interuption from TLC/Comedy_Central
                                (logoStartLogoStop >  95520)) &&                                   // no info logo before
                                (logoStopSilenceStart    <= 1640) &&   // silence start short after end mark
                                (silenceStartSilenceStop >=   40) && (silenceStartSilenceStop <=  1160) &&
                                (silenceStopLogoStart    >=  240) && (silenceStopLogoStart    <= 27560)) {
                            dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence between end mark and next start mark  is valid", mark->position);
                            return true;
                        }
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence between end mark and next start mark is invalid", mark->position);
                    }
                }
            }
        }

        // sequence MT_SOUNDSTOP -> MT_LOGOSTOP (mark) -> MT_SOUNDSTART -> MT_LOGOSTART
        // silence around end mark
        cMark *silenceStart = silenceMarks.GetPrev(mark->position, MT_SOUNDSTOP);
        if (silenceStart) {
            cMark *silenceStop = silenceMarks.GetNext(mark->position, MT_SOUNDSTART);
            if (silenceStop) {
                cMark *logoStart = marks.GetNext(silenceStop->position, MT_LOGOSTART);
                if (logoStart) {
                    int silenceStartLogoStop = 1000 * (mark->position         - silenceStart->position) / decoder->GetVideoFrameRate();
                    int logoStopSilenceStop  = 1000 * (silenceStop->position  - mark->position)         / decoder->GetVideoFrameRate();
                    int silenceStopLogoStart = 1000 * (logoStart->position    - silenceStop->position)  / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_SOUNDSTOP (%6d) -> %4dms MT_LOGOSTOP (%6d) -> %4dms -> MT_SOUNDSTART (%6d) -> %6dms -> MT_LOGOSTART (%6d)", silenceStart->position, silenceStartLogoStop, mark->position, logoStopSilenceStop, silenceStop->position, silenceStopLogoStart, logoStart->position);
// valid sequence
// MT_SOUNDSTOP ( 72667) ->  560ms MT_LOGOSTOP ( 72681) ->  240ms -> MT_SOUNDSTART ( 72687) ->   1920ms -> MT_LOGOSTART ( 72735)
// MT_SOUNDSTOP ( 44964) -> 3400ms MT_LOGOSTOP ( 45049) -> 1200ms -> MT_SOUNDSTART ( 45079) ->   2480ms -> MT_LOGOSTART ( 45141)
// MT_SOUNDSTOP (158458) ->   40ms MT_LOGOSTOP (158460) ->  160ms -> MT_SOUNDSTART (158468) ->  27540ms -> MT_LOGOSTART (159845)
//
// invalid example
// MT_SOUNDSTOP ( 88721) ->  120ms MT_LOGOSTOP ( 88724) ->  240ms -> MT_SOUNDSTART ( 88730) ->  65560ms -> MT_LOGOSTART ( 90369)  -> stop mark before last ad
// MT_SOUNDSTOP ( 40563) ->  240ms MT_LOGOSTOP ( 40569) ->   80ms -> MT_SOUNDSTART ( 40571) ->   4040ms -> MT_LOGOSTART ( 40672)  -> stop mark between preview and last valid start mark
                    if ((silenceStartLogoStop <= 3400) &&
                            (logoStopSilenceStop  > 80) && (logoStopSilenceStop <= 1200) &&
                            (silenceStopLogoStart <= 27540)) {
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence is invalid", mark->position);
                }
            }
        }
        // sequence MT_SOUNDSTOP -> MT_SOUNDSTART -> MT_LOGOSTOP -> MT_LOGOSTART
        silenceStart = silenceMarks.GetPrev(mark->position, MT_SOUNDSTOP);
        if (silenceStart) {
            cMark *silenceStop = silenceMarks.GetNext(silenceStart->position, MT_SOUNDSTART);
            if (silenceStop) {
                cMark *logoStart = marks.GetNext(mark->position, MT_LOGOSTART);
                if (logoStart) {
                    int silenceStartSilenceStop = 1000 * (silenceStop->position - silenceStart->position) / decoder->GetVideoFrameRate();
                    int silenceStopLogoStop     = 1000 * (mark->position        - silenceStop->position)  / decoder->GetVideoFrameRate();
                    int logoStopLogoStart       = 1000 * (logoStart->position   - mark->position)         / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): MT_SOUNDSTOP (%6d) -> %5dms -> MT_SOUNDSTART (%6d) -> %5dms -> MT_LOGOSTOP (%6d) -> %5dms -> MT_LOGOSTART (%6d)", silenceStart->position, silenceStartSilenceStop, silenceStop->position, silenceStopLogoStop, mark->position, logoStopLogoStart, logoStart->position);
                    // valid sequence
                    // MT_SOUNDSTOP ( 96076) ->   320ms -> MT_SOUNDSTART ( 96092) ->   360ms -> MT_LOGOSTOP ( 96110) -> 13620ms -> MT_LOGOSTART ( 96791)
                    // MT_SOUNDSTOP ( 96076) ->   320ms -> MT_SOUNDSTART ( 96092) ->   380ms -> MT_LOGOSTOP ( 96111) -> 13600ms -> MT_LOGOSTART ( 96791)
                    //
                    // invalid sequence
                    // MT_SOUNDSTOP ( 40563) ->   320ms -> MT_SOUNDSTART ( 40571) ->     0ms -> MT_LOGOSTOP ( 40571) ->  4040ms -> MT_LOGOSTART ( 40672)
                    // MT_SOUNDSTOP ( 38187) ->   320ms -> MT_SOUNDSTART ( 38195) ->     0ms -> MT_LOGOSTOP ( 38195) ->  4040ms -> MT_LOGOSTART ( 38296)
                    if ((silenceStartSilenceStop >= 320) && (silenceStopLogoStop >= 360) && (silenceStopLogoStop <= 380) && (logoStopLogoStart <= 13620)) {
                        dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveSilenceSeparator(): logo stop mark (%d): silence sequence is invalid", mark->position);
                }
            }
        }
    }
    return false;
}


bool cMarkAdStandalone::HaveBlackSeparator(const cMark *mark) {
    if (!mark) return false;
    // check log start mark
    if (mark->type == MT_LOGOSTART) {

        // check squence MT_LOGOSTOP -> MT_NOBLACKSTOP -> MT_NOBLACKSTART -> MT_LOGOSTART (mark) -> MT_LOGOSTOP
        // black screen between logo stop and logo start
        cMark *blackStop = blackMarks.GetPrev(mark->position + 1, MT_NOBLACKSTART);
        if (blackStop) {  // from above
            cMark *blackStart = blackMarks.GetPrev(blackStop->position, MT_NOBLACKSTOP);
            if (blackStart) {
                cMark *stopBefore = marks.GetPrev(blackStart->position, MT_LOGOSTOP);
                if (stopBefore) {
                    const cMark *stopAfter = marks.GetNext(mark->position, MT_LOGOSTOP);
                    int diffLogoStartLogoStop  = INT_MAX;
                    int stopAfterPosition      = INT_MAX;
                    if (stopAfter) {
                        stopAfterPosition           = stopAfter->position;
                        diffLogoStartLogoStop       = 1000 * (stopAfter->position  - mark->position)       / decoder->GetVideoFrameRate();
                    }
                    int diffLogoStopBlackStart  = 1000 * (blackStart->position - stopBefore->position) / decoder->GetVideoFrameRate();
                    int diffBlackStartBlackStop = 1000 * (blackStop->position  - blackStart->position) / decoder->GetVideoFrameRate();
                    int diffBlackStopLogoStart  = 1000 * (mark->position       - blackStop->position)  / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTOP(%5d)->%6dms->MT_NOBLACKSTOP(%5d)->%4dms->MT_NOBLACKSTART(%5d)->%5dms->MT_LOGOSTART(%5d)->%10dms->MT_LOGOSTOP(%10d) -> %s", stopBefore->position, diffLogoStopBlackStart, blackStart->position, diffBlackStartBlackStop, blackStop->position, diffBlackStopLogoStart, mark->position, diffLogoStartLogoStop, stopAfterPosition, macontext.Info.ChannelName);
// black screen short after end mark of previous broadcast
// valid example
//
// invalid example
// MT_LOGOSTOP(11443)->    40ms->MT_NOBLACKSTOP(11444)-> 120ms->MT_NOBLACKSTART(11447)-> 8440ms->MT_LOGOSTART(11658)->      9760ms->MT_LOGOSTOP(     11902)
// MT_LOGOSTOP( 1908)->   560ms->MT_NOBLACKSTOP( 1922)-> 640sm->MT_NOBLACKSTART( 1938)-> 5000ms->MT_LOGOSTART( 2063)->    192200ms->MT_LOGOSTOP(      6868)
// MT_LOGOSTOP(  445)->   520ms->MT_NOBLACKSTOP(  458)-> 640ms->MT_NOBLACKSTART(  474)->24160ms->MT_LOGOSTART( 1078)->    231720ms->MT_LOGOSTOP(      6871)
// MT_LOGOSTOP( 8166)->  2160ms->MT_NOBLACKSTOP( 8220)-> 160ms->MT_NOBLACKSTART( 8224)-> 5800ms->MT_LOGOSTART( 8369)->2147483647ms->MT_LOGOSTOP(2147483647)
                    if ((diffLogoStopBlackStart <= 2159) && (diffBlackStartBlackStop >= 40) && (diffBlackStopLogoStart <= 38720) && (diffLogoStartLogoStop > 231720)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence short after previous stop is valid");
                        return true;
                    }

// black screen short before start mark of broadcast
// valid logo start mark example
// MT_LOGOSTOP( 8177)->31200ms->MT_NOBLACKSTOP( 8957)->120ms->MT_NOBLACKSTART(   8960)-> 680ms->MT_LOGOSTART( 8977)->2147483647ms->MT_LOGOSTOP(2147483647) Disney Channel
// MT_LOGOSTOP( 6035)-> 8240ms->MT_NOBLACKSTOP( 6241)->160ms->MT_NOBLACKSTART(   6245)->1400ms->MT_LOGOSTART( 6280)->2147483647ms->MT_LOGOSTOP(2147483647) DMAX
// MT_LOGOSTOP( 9047)->47380ms->MT_NOBLACKSTOP(11416)->180ms->MT_NOBLACKSTART(  11425)->1660ms->MT_LOGOSTART(11508)->2147483647ms->MT_LOGOSTOP(2147483647) KiKA
// MT_LOGOSTOP( 8313)->10800ms->MT_NOBLACKSTOP( 8583)->200ms->MT_NOBLACKSTART(   8588)->5840ms->MT_LOGOSTART( 8734)->2147483647ms->MT_LOGOSTOP(2147483647) sixx
// MT_LOGOSTOP( 6931)->21280ms->MT_NOBLACKSTOP( 7463)->160ms->MT_NOBLACKSTART(   7467)->4760ms->MT_LOGOSTART( 7586)->2147483647ms->MT_LOGOSTOP(2147483647) RTL Television
//
// invalid logo start mark example
// logo start with black screen before preview
// MT_LOGOSTOP( 6578)->165640ms->MT_NOBLACKSTOP(10719)-> 840ms->MT_NOBLACKSTART(10740)->  680ms->MT_LOGOSTART(10757)->     27440ms->MT_LOGOSTOP(     11443) Comedy Central
// MT_LOGOSTOP(    3)-> 23880ms->MT_NOBLACKSTOP(  600)->1080ms->MT_NOBLACKSTART(  627)-> 1800ms->MT_LOGOSTART(  672)->     28840ms->MT_LOGOSTOP(      1393)
// MT_LOGOSTOP(    1)-> 22920ms->MT_NOBLACKSTOP(  574)-> 160ms->MT_NOBLACKSTART(  578)->    0ms->MT_LOGOSTART(  578)->     34840ms->MT_LOGOSTOP(      1449) RTL_Television
// MT_LOGOSTOP(  534)->  9920ms->MT_NOBLACKSTOP(  782)-> 120ms->MT_NOBLACKSTART(  785)-> 5040ms->MT_LOGOSTART(  911)->     36920ms->MT_LOGOSTOP(      1834)
// MT_LOGOSTOP( 1694)-> 39400ms->MT_NOBLACKSTOP( 2679)-> 200ms->MT_NOBLACKSTART( 2684)->  640ms->MT_LOGOSTART( 2700)->    202440ms->MT_LOGOSTOP(      7761) -> SAT_1
                    if (    (diffLogoStopBlackStart  >=  8240) &&
                            (diffBlackStartBlackStop >=   120) && (diffBlackStopLogoStart <= 5840) &&
                            (diffLogoStartLogoStop   >  202440)) {  // long broadcast part must follow
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen short before logo start is valid");
                        return true;
                    }
                    else dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen short before logo start is invalid");
                }
            }
        }

        // check sequence MT_NOBLACKSTOP -> MT_LOGOSTOP -> MT_NOBLACKSTART -> MT_LOGOSTART (mark)
        // black screen around logo stop before logo start
        blackStop = blackMarks.GetPrev(mark->position + 1, MT_NOBLACKSTART);  // black screen end can be on same position as logo start
        if (blackStop) {
            cMark *stopBefore = marks.GetPrev(blackStop->position, MT_LOGOSTOP);
            if (stopBefore) {
                cMark *blackStart = blackMarks.GetPrev(stopBefore->position, MT_NOBLACKSTOP);
                if (blackStart) {
                    int diffBlackStartLogoStop = 1000 * (stopBefore->position - blackStart->position) / decoder->GetVideoFrameRate();
                    int diffLogoStopBlackStop  = 1000 * (blackStop->position - stopBefore->position)  / decoder->GetVideoFrameRate();
                    int diffBlackStopLogoStart = 1000 * (mark->position - blackStop->position)        / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_NOBLACKSTOP (%d) -> %4dms -> MT_LOGOSTOP (%d) -> %4dms -> MT_NOBLACKSTART (%d) -> %4dms -> MT_LOGOSTART (%d)", blackStart->position, diffBlackStartLogoStop, stopBefore->position, diffLogoStopBlackStop, blackStop->position, diffBlackStopLogoStart, mark->position);
// valid sequence
// MT_NOBLACKSTOP (8245) -> 4760ms -> MT_LOGOSTOP (8364) -> 5000ms -> MT_NOBLACKSTART (8489) -> 2360ms -> MT_LOGOSTART (8548) -> TELE 5
// MT_NOBLACKSTOP (7254) ->  280ms -> MT_LOGOSTOP (7261) ->   40ms -> MT_NOBLACKSTART (7262) -> 4000ms -> MT_LOGOSTART (7362) -> RTL_Television
// invalid sequence
// MT_NOBLACKSTOP (2818) -> 1480ms -> MT_LOGOSTOP (2855) ->   40ms -> MT_NOBLACKSTART (2856) -> 2040ms -> MT_LOGOSTART (2907) -> RTL2 (black screen before preview)
// MT_NOBLACKSTOP (2965) -> 1840ms -> MT_LOGOSTOP (3011) ->   40ms -> MT_NOBLACKSTART (3012) -> 1600ms -> MT_LOGOSTART (3052) -> RTL2 (black screen before preview)
// MT_NOBLACKSTOP (3480) -> 1240ms -> MT_LOGOSTOP (3511) ->   40ms -> MT_NOBLACKSTART (3512) -> 1800ms -> MT_LOGOSTART (3557) -> RTL2 (black screen before preview)
                    if ((diffBlackStartLogoStop <= 4760) && (diffLogoStopBlackStop <= 5000) &&
                            (diffBlackStopLogoStart >= 2360) && (diffBlackStopLogoStart <= 4000)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is valid");
                        return true;
                    }
                    else dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is invalid");
                }
            }
        }

        // check squence MT_LOGOSTOP ->  MT_LOGOSTART (mark) -> MT_NOBLACKSTOP -> MT_NOBLACKSTART
        // black screen after logo start
        cMark *stopBefore = marks.GetPrev(mark->position, MT_LOGOSTOP);
        if (stopBefore) {
            cMark *blackStart = blackMarks.GetNext(mark->position, MT_NOBLACKSTOP);
            if (blackStart) {
                blackStop = blackMarks.GetNext(blackStart->position, MT_NOBLACKSTART);
                if (blackStop) {
                    int diffLogoStopLogoStart   = 1000 * (mark->position       - stopBefore->position) / decoder->GetVideoFrameRate();
                    int diffLogoStartBlackStart = 1000 * (blackStart->position - mark->position)       / decoder->GetVideoFrameRate();
                    int diffBlackStartBlackStop = 1000 * (blackStop->position  - blackStart->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTOP (%4d) -> %4dms -> MT_LOGOSTART (%4d) -> %3dms -> MT_NOBLACKSTOP (%4d) -> %3dms -> MT_NOBLACKSTART (%4d) -> %s", stopBefore->position, diffLogoStopLogoStart, mark->position, diffLogoStartBlackStart, blackStart->position, diffBlackStartBlackStop, blackStop->position, macontext.Info.ChannelName);
// valid example
// MT_LOGOSTOP (3996) -> 2040ms -> MT_LOGOSTART (4047) -> 680ms -> MT_NOBLACKSTOP (4064) ->  200ms -> MT_NOBLACKSTART (4069)
//
// invalid example
// MT_LOGOSTOP (8702) ->  360ms -> MT_LOGOSTART (8711) ->  80ms -> MT_NOBLACKSTOP (8713) -> 1800ms -> MT_NOBLACKSTART (8758) -> Comedy Central, black screen after logo interuption
// MT_LOGOSTOP (8702) ->  360ms -> MT_LOGOSTART (8711) ->  80ms -> MT_NOBLACKSTOP (8713) -> 1760ms -> MT_NOBLACKSTART (8757) -> Comedy Central, black screen after logo interuption
                    if ((diffLogoStopLogoStart > 360) && (diffLogoStopLogoStart <= 2040) &&
                            (diffLogoStartBlackStart <= 680) &&
                            (diffBlackStartBlackStop >= 200) && (diffBlackStartBlackStop < 1760)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is valid");
                        return true;
                    }
                    else dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is invalid");
                }
            }
        }

        // check squence MT_LOGOSTOP ->  MT_NOBLACKSTOP -> MT_LOGOSTART (mark) -> MT_NOBLACKSTART
        // black screen around logo start mark
        blackStop = blackMarks.GetNext(mark->position, MT_NOBLACKSTART);
        if (blackStop) {
            cMark *blackStart = blackMarks.GetPrev(blackStop->position, MT_NOBLACKSTOP);
            if (blackStart) {
                stopBefore = marks.GetPrev(mark->position, MT_LOGOSTOP);
                if (stopBefore) {
                    int diffLogoStopBlackStart  = 1000 * (blackStart->position - stopBefore->position) / decoder->GetVideoFrameRate();
                    int diffBlackStartLogoStart = 1000 * (mark->position       - blackStart->position) / decoder->GetVideoFrameRate();
                    int diffLogoStartBlackStop  = 1000 * (blackStop->position  - mark->position)       / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTOP (%5d)-> %5dms -> MT_NOBLACKSTOP (%5d) -> %4dms -> MT_LOGOSTART (%5d) -> %4dms -> MT_NOBLACKSTART (%5d)", stopBefore->position, diffLogoStopBlackStart, blackStart->position, diffBlackStartLogoStart, mark->position, diffLogoStartBlackStop, blackStop->position);
// invalid example
// MT_LOGOSTOP (11912)->     0ms -> MT_NOBLACKSTOP (11912) ->  480ms -> MT_LOGOSTART (11924) ->   40ms -> MT_NOBLACKSTART (11925) -> Comedy Central: black screen after info logo
// MT_LOGOSTOP ( 4540)->  7240ms -> MT_NOBLACKSTOP ( 4721) ->    0ms -> MT_LOGOSTART ( 4721) ->   80ms -> MT_NOBLACKSTART ( 4723) -> Comedy Central: last part of broadcast before
//
// valid example
// MT_LOGOSTOP (16022)-> 79080ms -> MT_NOBLACKSTOP (19976) ->   40ms -> MT_LOGOSTART (19978) ->  440ms -> MT_NOBLACKSTART (20000)
// MT_LOGOSTOP ( 7582)->   720ms -> MT_NOBLACKSTOP ( 7600) -> 2800ms -> MT_LOGOSTART ( 7670) -> 1880ms -> MT_NOBLACKSTART ( 7717) long black opening credits, fade in logo  (TELE 5)
// MT_LOGOSTOP ( 8017)-> 21000ms -> MT_NOBLACKSTOP ( 8542) ->    0ms -> MT_LOGOSTART ( 8542) -> 2920ms -> MT_NOBLACKSTART ( 8615) -> Comedy Central
                    if ((diffLogoStopBlackStart      >= 720) && (diffLogoStopBlackStart  <= 79080) &&
                            (diffBlackStartLogoStart >=   0) && (diffBlackStartLogoStart <= 2800) &&
                            (diffLogoStartBlackStop  >= 440) && (diffLogoStartBlackStop  <= 2920)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is valid");
                        return true;
                    }
                    else dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is invalid");
                }
            }
        }

        // check sequence MT_LOGOSTART (mark) -> MT_NOBLACKSTOP -> MT_NOBLACKSTRT
        // black screen short after logo start
        cMark *blackStart = blackMarks.GetNext(mark->position, MT_NOBLACKSTOP);
        if (blackStart) {
            blackStop = blackMarks.GetNext(blackStart->position, MT_NOBLACKSTART);
            if (blackStop) {
                int diffLogoStartBlackStart  = 1000 * (blackStart->position - mark->position) / decoder->GetVideoFrameRate();
                int diffBlackStartBlackStop  = 1000 * (blackStop->position - blackStart->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTART (%5d)-> %5dms -> MT_NOBLACKSTOP (%5d) -> %4dms -> MT_NOBLACKSTART (%5d)", mark->position, diffLogoStartBlackStart, blackStart->position, diffBlackStartBlackStop, blackStop->position);
                // valid example
                // MT_LOGOSTART (14148)->    40ms -> MT_NOBLACKSTOP (14150) ->   40ms -> MT_NOBLACKSTART (14152)  arte HD
                if ((diffLogoStartBlackStart <= 40) && (diffBlackStartBlackStop <= 40)) {
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is valid");
                    return true;
                }
                else dsyslog("cMarkAdStandalone::HaveBlackSeparator(): black screen sequence is invalid");
            }
        }
    }

    // check logo stop mark
    if (mark->type == MT_LOGOSTOP) {
        // check sequence MT_NOBLACKSTOP -> MT_LOGOSTOP (mark) -> MT_NOBLACKSTART -> MT_LOGOSTART
        // black screen around end mark
        cMark *startAfter = marks.GetNext(mark->position, MT_LOGOSTART);
        if (startAfter) {
            cMark *blackStart = blackMarks.GetPrev(mark->position + 1, MT_NOBLACKSTOP);     // black screen can start at the same position as logo stop
            if (blackStart) {
                cMark *blackStop = blackMarks.GetNext(mark->position - 1, MT_NOBLACKSTART); // black screen can start at the same position as logo stop
                if (blackStop) {
                    int diffBlackStartLogoStop = 1000 * (mark->position       - blackStart->position) / decoder->GetVideoFrameRate();
                    int diffLogoStopBlackStop  = 1000 * (blackStop->position  - mark->position)       / decoder->GetVideoFrameRate();
                    int diffBlackStopLogoStart = 1000 * (startAfter->position - blackStop->position)  / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_NOBLACKSTOP (%6d) -> %4dms -> MT_LOGOSTOP (%6d) -> %4dms -> MT_NOBLACKSTART (%6d) -> %5dms -> MT_LOGOSTART (%6d) -> %s", blackStart->position, diffBlackStartLogoStop, mark->position, diffLogoStopBlackStop, blackStop->position, diffBlackStopLogoStart, startAfter->position, macontext.Info.ChannelName);
// valid sequence
// MT_NOBLACKSTOP ( 90520) ->  360ms -> MT_LOGOSTOP ( 90529) ->  200ms -> MT_NOBLACKSTART ( 90534) ->  3960ms -> MT_LOGOSTART ( 90633)
// MT_NOBLACKSTOP ( 47364) -> 1320ms -> MT_LOGOSTOP ( 47397) ->  280ms -> MT_NOBLACKSTART ( 47404) -> 18080ms -> MT_LOGOSTART ( 47856)
// MT_NOBLACKSTOP ( 84098) -> 1400ms -> MT_LOGOSTOP ( 84133) ->   40ms -> MT_NOBLACKSTART ( 84134) ->  1560ms -> MT_LOGOSTART ( 84173) -> RTL2
// MT_NOBLACKSTOP ( 42629) ->  760ms -> MT_LOGOSTOP ( 42648) ->   40ms -> MT_NOBLACKSTART ( 42649) ->   840ms -> MT_LOGOSTART ( 42670) -> SIXX
// MT_NOBLACKSTOP ( 44025) ->  840ms -> MT_LOGOSTOP ( 44046) ->  360ms -> MT_NOBLACKSTART ( 44055) ->   520ms -> MT_LOGOSTART ( 44068) -> SIXX
// MT_NOBLACKSTOP (260383) ->  400ms -> MT_LOGOSTOP (260403) ->   80ms -> MT_NOBLACKSTART (260407) -> 24320ms -> MT_LOGOSTART (261623) -> zdf neo HD
// MT_NOBLACKSTOP ( 42686) ->  880ms -> MT_LOGOSTOP ( 42708) ->    0ms -> MT_NOBLACKSTART ( 42708) ->   880ms -> MT_LOGOSTART ( 42730) -> SIXX
// MT_NOBLACKSTOP ( 50967) ->    0ms -> MT_LOGOSTOP ( 50967) ->   80ms -> MT_NOBLACKSTART ( 50969) ->   760ms -> MT_LOGOSTART ( 50988) -> sixx
                    if (    (diffBlackStartLogoStop >=   0) && (diffBlackStartLogoStop <=  1400) &&
                            (diffLogoStopBlackStop  >=   0) && (diffLogoStopBlackStop  <=   360) &&
                            (diffBlackStopLogoStart >= 520) && (diffBlackStopLogoStart <= 24320)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence around end mark is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence around end mark is invalid", mark->position);
                }
            }
        }

        // check sequence MT_NOBLACKSTOP -> MT_NOBLACKSTART -> MT_LOGOSTOP
        // blackscreen short before logo stop mark
        cMark *blackStop = blackMarks.GetPrev(mark->position, MT_NOBLACKSTART);
        if (blackStop) {
            cMark *blackStart = blackMarks.GetPrev(blackStop->position, MT_NOBLACKSTOP);   // this can be different from above
            if (blackStart) {
                int lengthBlack  = 1000 * (blackStop->position - blackStart->position)  / decoder->GetVideoFrameRate();
                int distLogoStop = 1000 * (mark->position      - blackStop->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_NOBLACKSTOP (%5d) -> %4dms -> MT_NOBLACKSTART (%5d) -> %4dms -> MT_LOGOSTOP (%5d)", blackStart->position, lengthBlack, blackStop->position, distLogoStop, mark->position);
                // valid example
                // MT_NOBLACKSTOP (136013) ->   40ms -> MT_NOBLACKSTART (136015) -> 1340ms -> MT_LOGOSTOP (136082)   -> ANIXE HD
                // MT_NOBLACKSTOP (151167) -> 2920ms -> MT_NOBLACKSTART (151313) ->  120ms -> MT_LOGOSTOP (151319)   -> SRF zwei
                //
                // invalid example
                // MT_NOBLACKSTOP  (84646) ->  160ms -> MT_NOBLACKSTART  (84650) ->  480ms -> MT_LOGOSTOP (84662)    -> pattern in blackground from preview (RTLZWEI) (conflict)
                if ((lengthBlack >= 40) && (distLogoStop <= 1340)) {
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is valid", mark->position);
                    return true;
                }
                dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is invalid", mark->position);
            }
        }

        // check sequence: MT_LOGOSTART -> MT_LOGOSTOP (mark) -> MT_NOBLACKSTOP -> MT_NOBLACKSTART -> MT_START
        // black screen between logo end mark and start of next broadcast, near (depends on fade out logo) by logo end mark
        cMark *logoStartBefore = marks.GetPrev(mark->position, MT_LOGOSTART);
        cMark *blackStart      = blackMarks.GetNext(mark->position - 1, MT_NOBLACKSTOP);
        if (logoStartBefore && blackStart) {
            blackStop = blackMarks.GetNext(blackStart->position, MT_NOBLACKSTART);
            if (blackStop) {
                cMark *nextStart = marks.GetNext(mark->position, MT_START, 0x0F);  // next broadcast can have border start mark and no logo start mark
                if (nextStart) {
                    int diffLogoStartLogoStop   = 1000 * (mark->position       - logoStartBefore->position) /  decoder->GetVideoFrameRate();
                    int diffLogoStopBlackStart  = 1000 * (blackStart->position - mark->position)            /  decoder->GetVideoFrameRate();
                    int diffBlackStartBlackStop = 1000 * (blackStop->position  - blackStart->position)      /  decoder->GetVideoFrameRate();
                    int diffBlackStopLogoStart  = 1000 * (nextStart->position  - blackStop->position)       /  decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTART (%5d) -> %4dms -> MT_LOGOSTOP (%5d) -> %5dms -> MT_NOBLACKSTOP (%5d) -> %5dms ->  MT_NOBLACKSTART (%5d) -> %6dms -> MT_START (%5d) -> %s", logoStartBefore->position, diffLogoStartLogoStop, mark->position, diffLogoStopBlackStart, blackStart->position, diffBlackStartBlackStop, blackStop->position, diffBlackStopLogoStart, nextStart->position, macontext.Info.ChannelName);
// channel with fade out logo
// valid sequence:
//                                     MT_LOGOSTOP (72210) ->  1400ms -> MT_NOBLACKSTOP (72245) ->   200ms ->  MT_NOBLACKSTART (72250) -> 104320ms -> MT_START (74858) -> TLC
//                                     MT_LOGOSTOP (86133) ->  1320ms -> MT_NOBLACKSTOP (86166) ->  2120ms ->  MT_NOBLACKSTART (86219) ->  16000ms -> MT_START (86619) -> Disney Channel
//                                     MT_LOGOSTOP (72310) ->  4240ms -> MT_NOBLACKSTOP (72416) ->   440ms ->  MT_NOBLACKSTART (72427) ->    800ms -> MT_START (72447) -> Disney Channel
//                                     MT_LOGOSTOP (47508) ->   840ms -> MT_NOBLACKSTOP (47529) ->   120ms ->  MT_NOBLACKSTART (47532) ->  13040ms -> MT_START (47858) -> Comedy Central
//                                     MT_LOGOSTOP (43346) ->  6560ms -> MT_NOBLACKSTOP (43510) ->   120ms ->  MT_NOBLACKSTART (43513) ->  20040ms -> MT_START (44014) -> Comedy Central
//
// invalid sequence:
// MT_LOGOSTART (56190) ->    360ms -> MT_LOGOSTOP (56199) ->   520ms -> MT_NOBLACKSTOP (56212) ->   240ms ->  MT_NOBLACKSTART (56218) ->  16880ms -> MT_START (56640) -> Comedy_Central
                    if ((criteria->LogoFadeInOut() & FADE_OUT) &&
                            (diffLogoStartLogoStop > 360) &&
                            (diffLogoStopBlackStart <= 6560) && (diffBlackStartBlackStop >=    120) &&
                            (diffBlackStopLogoStart >=  800) && (diffBlackStopLogoStart  <= 104320)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence between logo end mark and start of next broadcast is valid (fade out logo)", mark->position);
                        return true;
                    }
// channel without fade out logo
// valid sequence:
//                                     MT_LOGOSTOP (81055) ->     0ms -> MT_NOBLACKSTOP (81055) ->    40ms ->  MT_NOBLACKSTART (81056) ->   1680ms -> MT_START (81098) -> RTL2
//                                     MT_LOGOSTOP (84786) ->   120ms -> MT_NOBLACKSTOP (84789) ->   240ms ->  MT_NOBLACKSTART (84795) ->   1800ms -> MT_START (84840) -> Pro7 MAXX
//                                     MT_LOGOSTOP (82130) ->    80ms -> MT_NOBLACKSTOP (82132) ->   240ms ->  MT_NOBLACKSTART (82138) ->   1840ms -> MT_START (82184) -> Pro7 MAXX
//                                     MT_LOGOSTOP (78161) ->    80ms -> MT_NOBLACKSTOP (78163) ->   240ms ->  MT_NOBLACKSTART (78169) ->   2800ms -> MT_START (78239) -> Pro7 MAXX
//                                     MT_LOGOSTOP (46818) ->   840ms -> MT_NOBLACKSTOP (46839) ->   160ms ->  MT_NOBLACKSTART (46843) ->   2680ms -> MT_START (46910) -> Comedy Central
//                                     MT_LOGOSTOP (51734) ->    40ms -> MT_NOBLACKSTOP (51735) ->   320ms ->  MT_NOBLACKSTART (51743) ->    520ms -> MT_START (51756) -> sixx
// MT_LOGOSTART (36696) -> 710920ms -> MT_LOGOSTOP (54469) ->    40ms -> MT_NOBLACKSTOP (54470) ->   120ms ->  MT_NOBLACKSTART (54473) ->    440ms -> MT_START (54484) -> ProSieben
//
// invalid sequence:
//                                     MT_LOGOSTOP (81485) ->  4040ms -> MT_NOBLACKSTOP (81586) ->   160ms ->  MT_NOBLACKSTART (81590) ->  95920ms -> MT_LOGOSTART (83988) -> RTLZWEI
                    if (!(criteria->LogoFadeInOut() & FADE_OUT) &&
                            (diffLogoStopBlackStart <= 840) && (diffBlackStartBlackStop >=   40) &&
                            (diffBlackStopLogoStart >= 440) && (diffBlackStopLogoStart  <= 2800)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence between logo end mark and start of next broadcast is valid (no fade out logo)", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence between logo end mark and start of next broadcast is invalid", mark->position);
                }
            }

        }

        // check sequence: MT_LOGOSTOP -> MT_NOBLACKSTOP -> MT_LOGOSTART -> MT_NOBLACKSTART  (black screen from short after logo end mark to and short after start of next broadcast)
        blackStart = blackMarks.GetNext(mark->position - 1, MT_NOBLACKSTOP);
        if (blackStart) {
            cMark *logoStart = marks.GetNext(blackStart->position, MT_LOGOSTART);
            if (logoStart) {
                blackStop = blackMarks.GetNext(logoStart->position, MT_NOBLACKSTART);
                if (blackStop) {
                    int diffLogoStopBlackStart  = 1000 * (blackStart->position - mark->position)       /  decoder->GetVideoFrameRate();
                    int diffBlackStartLogoStart = 1000 * (logoStart->position  - blackStart->position) /  decoder->GetVideoFrameRate();
                    int diffLogoStartBlackStop  = 1000 * (blackStop->position  - logoStart->position)  /  decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTOP (%5d) -> %4dms -> MT_NOBLACKSTOP (%5d) -> %4dms ->  MT_LOGOSTART (%5d) -> %4dms -> MT_NOBLACKSTART (%5d)", mark->position, diffLogoStopBlackStart, blackStart->position, diffBlackStartLogoStart, logoStart->position, diffLogoStartBlackStop, blackStop->position);
                    // valid sequence
                    // MT_LOGOSTOP (82667) ->   80ms -> MT_NOBLACKSTOP (82669) -> 2080ms ->  MT_LOGOSTART (82721) ->  600ms -> MT_NOBLACKSTART (82736)
                    // MT_LOGOSTOP (81688) ->  120ms -> MT_NOBLACKSTOP (81691) -> 2040ms ->  MT_LOGOSTART (81742) ->  920ms -> MT_NOBLACKSTART (81765)
                    // MT_LOGOSTOP (83499) ->   80ms -> MT_NOBLACKSTOP (83501) -> 2040ms ->  MT_LOGOSTART (83552) -> 1640ms -> MT_NOBLACKSTART (83593)
                    if ((diffLogoStopBlackStart <= 120) && (diffBlackStartLogoStart <= 2080) && (diffLogoStartBlackStop <= 1640)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is invalid", mark->position);
                }
            }

        }
        // check sequence: MT_LOGOSTOP -> MT_LOGOSTART -> MT_NOBLACKSTOP -> MT_NOBLACKSTART  (long black screen short after near logo start, opening credids of next braadcast)
        cMark *logoStart = marks.GetNext(mark->position, MT_LOGOSTART);
        if (logoStart) {
            blackStart = blackMarks.GetNext(logoStart->position, MT_NOBLACKSTOP);
            if (blackStart) {
                blackStop = blackMarks.GetNext(logoStart->position, MT_NOBLACKSTART);
                if (blackStop) {
                    int diffLogoStopLogoStart  = 1000 * (logoStart->position - mark->position)       /  decoder->GetVideoFrameRate();
                    int diffLogoStartBlackStart  = 1000 * (blackStart->position - logoStart->position)       /  decoder->GetVideoFrameRate();
                    int diffBlackStartBlackStop  = 1000 * (blackStop->position - blackStart->position)       /  decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): MT_LOGOSTOP (%5d) -> %4dms -> MT_LOGOSTART (%5d) -> %4dms ->  MT_NOBLACKSTOP (%5d) -> %4dms -> MT_NOBLACKSTART (%5d)", mark->position, diffLogoStopLogoStart, logoStart->position, diffLogoStartBlackStart, blackStart->position, diffBlackStartBlackStop, blackStop->position);
                    // valid sequence
                    // MT_LOGOSTOP (178106) -> 7080ms -> MT_LOGOSTART (178283) ->   40ms ->  MT_NOBLACKSTOP (178284) -> 6680ms -> MT_NOBLACKSTART (178451)
                    if ((diffLogoStopLogoStart <= 7080) && (diffLogoStartBlackStart <= 40) && (diffBlackStartBlackStop >= 6680)) {
                        dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is valid", mark->position);
                        return true;
                    }
                    dsyslog("cMarkAdStandalone::HaveBlackSeparator(): logo stop mark (%d): black screen sequence is invalid", mark->position);
                }
            }
        }
    }
    return false;
}


// special case TLC, short sequence without logo at the end of the broadcast
bool cMarkAdStandalone::HaveLogoMissingSequence(const cMark *mark) {
    if (!mark) return false;
    if (!criteria->LogoMissingAtEndChannel()) return false;
    if (mark->type == MT_LOGOSTART) return false;

    cMark *start1Before = marks.GetPrev(mark->position, MT_LOGOSTART);
    if (!start1Before) return false;
    cMark *stop1Before = marks.GetPrev(start1Before->position, MT_LOGOSTOP);
    if (!stop1Before) return false;
    cMark *start2Before = marks.GetPrev(stop1Before->position, MT_LOGOSTART);
    if (!start2Before) return false;
    int diffStart1Mark  = 1000 * (mark->position         - start1Before->position) / decoder->GetVideoFrameRate();
    int diffStop1Start1 = 1000 * (start1Before->position - stop1Before->position)  / decoder->GetVideoFrameRate();
    int diffStart2Stop1 = 1000 * (stop1Before->position  - start2Before->position) / decoder->GetVideoFrameRate();
    dsyslog("cMarkAdStandalone::HaveLogoMissingSequence(): MT_LOGOSTART (%6d) -> %4dms -> MT_LOGOSTOP (%6d) -> %4dms -> MT_LOGOSTART (%6d) -> %5dms -> MT_LOGOSTOP (%6d) -> %s", start2Before->position, diffStart2Stop1, stop1Before->position, diffStop1Start1, start1Before->position, diffStart1Mark, mark->position, macontext.Info.ChannelName);
// valid examples
// MT_LOGOSTART ( 91326) -> 225800ms -> MT_LOGOSTOP ( 96971) -> 17960ms -> MT_LOGOSTART ( 97420) -> 11560ms -> MT_LOGOSTOP ( 97709) -> TLC
// MT_LOGOSTART ( 71075) -> 987960ms -> MT_LOGOSTOP ( 95774) -> 17920ms -> MT_LOGOSTART ( 96222) -> 11360ms -> MT_LOGOSTOP ( 96506) -> TLC
    if (    (diffStart2Stop1 >= 225800) && // broadcast
            (diffStop1Start1 <= 17960) &&  // logo interuption
            (diffStart1Mark  >= 11360)) {   // last broadcast part
        dsyslog("cMarkAdStandalone::HaveLogoMissingSequence(): logo stop mark (%d): missing logo sequence is valid", mark->position);
        return true;
    }
    else dsyslog("cMarkAdStandalone::HaveLogoMissingSequence(): logo stop mark (%d): missing logo sequence is invalid", mark->position);
    return false;
}


// special case opening logo sequence from kabel eins, unable to detect info logo change from this channel, too much and too short
bool cMarkAdStandalone::HaveInfoLogoSequence(const cMark *mark) {
    if (!mark) return false;
    if (!criteria->IsInfoLogoChannel()) return false;
    // check logo start mark
    if (mark->type == MT_LOGOSTART) {
        // MT_LOGOSTART  (mark, info logo detected as logo) -> MT_LOGOSTOP (change from info logo to logo) -> MT_LOGOSTART (logo) -> MT_LOGOSTOP (long broadcast to end of first part)
        cMark *stop1After = marks.GetNext(mark->position, MT_LOGOSTOP);
        if (!stop1After) return false;
        cMark *start2After = marks.GetNext(stop1After->position, MT_LOGOSTART);
        if (!start2After) return false;
        const cMark *stop2After = marks.GetNext(start2After->position, MT_LOGOSTOP);
        int endPart1Pos = stopA;
        if (stop2After) endPart1Pos = stop2After->position;
        int diffMarkStop1After          = 1000 * (stop1After->position  - mark->position)        / decoder->GetVideoFrameRate();
        int diffStop1AfterStart2After   = 1000 * (start2After->position - stop1After->position)  / decoder->GetVideoFrameRate();
        int diffStStart2AfterStop2After = 1000 * (endPart1Pos            - start2After->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): MT_LOGOSTART (%5d) -> %5dms -> MT_LOGOSTOP (%5d) -> %4dms -> MT_LOGOSTART (%5d) -> %7ds -> MT_LOGOSTOP (%6d) -> %s", mark->position, diffMarkStop1After, stop1After->position, diffStop1AfterStart2After, start2After->position, diffStStart2AfterStop2After, endPart1Pos, macontext.Info.ChannelName);
        // valid logo start mark example
        // MT_LOGOSTART ( 8374) ->  6240ms -> MT_LOGOSTOP ( 8530) ->  840ms -> MT_LOGOSTART ( 8551) -> 1992840s -> MT_LOGOSTOP ( 58372) -> kabel_eins
        // MT_LOGOSTART ( 8610) ->  6320ms -> MT_LOGOSTOP ( 8768) ->  760ms -> MT_LOGOSTART ( 8787) -> 1149640s -> MT_LOGOSTOP ( 37528) -> kabel_eins
        // MT_LOGOSTART ( 8441) -> 33640ms -> MT_LOGOSTOP ( 9282) ->  680ms -> MT_LOGOSTART ( 9299) -> 7709040s -> MT_LOGOSTOP (202025) -> kabel_eins
        // MT_LOGOSTART ( 8348) ->  6040ms -> MT_LOGOSTOP ( 8499) -> 1040ms -> MT_LOGOSTART ( 8525) ->   26280s -> MT_LOGOSTOP (  9182) -> kabel_eins
        // MT_LOGOSTART ( 8973) ->  6040ms -> MT_LOGOSTOP ( 9124) -> 1080ms -> MT_LOGOSTART ( 9151) ->   26240s -> MT_LOGOSTOP (  9807) -> kabel_eins
        // MT_LOGOSTART ( 9043) ->  6000ms -> MT_LOGOSTOP ( 9193) -> 1080ms -> MT_LOGOSTART ( 9220) ->   26200s -> MT_LOGOSTOP (  9875) -> kabel_eins
        // MT_LOGOSTART ( 8462) ->  6240ms -> MT_LOGOSTOP ( 8618) ->  720ms -> MT_LOGOSTART ( 8636) -> 7418560s -> MT_LOGOSTOP (194100) -> kabel_eins
        //
        // invald logo start mark example
        // MT_LOGOSTART ( 3459) ->  5880ms -> MT_LOGOSTOP ( 3606) -> 1160ms -> MT_LOGOSTART ( 3635) ->   20800s -> MT_LOGOSTOP ( 4155) -> kabel eins, end sequence of broadcast before
        // MT_LOGOSTART ( 3744) ->  6040ms -> MT_LOGOSTOP ( 3895) -> 1160ms -> MT_LOGOSTART ( 3924) ->   20960s -> MT_LOGOSTOP ( 4448) -> kabel eins, end sequence of broadcast before
        // MT_LOGOSTART ( 3704) ->  8360ms -> MT_LOGOSTOP ( 3913) ->  760ms -> MT_LOGOSTART ( 3932) ->   18960s -> MT_LOGOSTOP ( 4406) -> kabel_eins, end sequence of broadcast before
        if (    (diffMarkStop1After          >=  6000) && (diffMarkStop1After        <= 33640) &&
                (diffStop1AfterStart2After   >=   720) && (diffStop1AfterStart2After <=  1080) &&
                (diffStStart2AfterStop2After >  20960)) {
            dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): opening info logo sequence is valid");
            return true;
        }
        else dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): logo start mark (%d): opening info logo sequence is invalid", mark->position);
    }
    // check logo stop mark
    if (mark->type == MT_LOGOSTOP) {
        cMark *start1Before = marks.GetPrev(mark->position, MT_LOGOSTART);
        if (!start1Before) return false;
        cMark *stop1Before = marks.GetPrev(start1Before->position, MT_LOGOSTOP);
        if (!stop1Before) return false;
        cMark *start2Before = marks.GetPrev(stop1Before->position, MT_LOGOSTART);
        if (!start2Before) return false;
        cMark *stop2Before = marks.GetPrev(start2Before->position, MT_LOGOSTOP);
        if (!stop2Before) return false;
        int diffStart1Mark  = 1000 * (mark->position         - start1Before->position) / decoder->GetVideoFrameRate();
        int diffStop1Start1 = 1000 * (start1Before->position - stop1Before->position)  / decoder->GetVideoFrameRate();
        int diffStart2Stop1 = 1000 * (stop1Before->position  - start2Before->position) / decoder->GetVideoFrameRate();
        int diffStop2Start2 = 1000 * (start2Before->position - stop2Before->position)  / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): MT_LOGOSTOP (%6d) -> %4dms -> MT_LOGOSTART (%6d) -> %4dms -> MT_LOGOSTOP (%6d) -> %4dms -> MT_LOGOSTART (%6d) -> %5dms -> MT_LOGOSTOP (%6d) -> %s", stop2Before->position, diffStop2Start2, start2Before->position, diffStart2Stop1, stop1Before->position, diffStop1Start1, start1Before->position, diffStart1Mark, mark->position, macontext.Info.ChannelName);
// valid examples
// MT_LOGOSTOP (185315) -> 1080ms -> MT_LOGOSTART (185342) -> 8160ms -> MT_LOGOSTOP (185546) ->  840ms -> MT_LOGOSTART (185567) -> 18880ms -> MT_LOGOSTOP (186039)
// MT_LOGOSTOP (128417) -> 1120ms -> MT_LOGOSTART (128445) -> 7840ms -> MT_LOGOSTOP (128641) -> 1160ms -> MT_LOGOSTART (128670) ->  7840ms -> MT_LOGOSTOP (128866)
// MT_LOGOSTOP (170880) ->  720ms -> MT_LOGOSTART (170898) -> 8360ms -> MT_LOGOSTOP (171107) ->  840ms -> MT_LOGOSTART (171128) ->  8080ms -> MT_LOGOSTOP (171330)
// MT_LOGOSTOP (68131) ->   920ms -> MT_LOGOSTART (68154) ->  6400ms -> MT_LOGOSTOP ( 68314) ->  720ms -> MT_LOGOSTART ( 68332) -> 10000ms -> MT_LOGOSTOP ( 68582)
// MT_LOGOSTOP (162049) -> 1040ms -> MT_LOGOSTART (162075) -> 8880ms -> MT_LOGOSTOP (162297) ->  840ms -> MT_LOGOSTART (162318) ->  6360ms -> MT_LOGOSTOP (162477)
// MT_LOGOSTOP (201203) ->  880ms -> MT_LOGOSTART (201225) -> 6360ms -> MT_LOGOSTOP (201384) ->  840ms -> MT_LOGOSTART (201405) -> 14360ms -> MT_LOGOSTOP (201764) -> kabel_eins
// MT_LOGOSTOP (201503) ->  600ms -> MT_LOGOSTART (201518) -> 6360ms -> MT_LOGOSTOP (201677) ->  840ms -> MT_LOGOSTART (201698) -> 14360ms -> MT_LOGOSTOP (202057) -> kabel_eins
//
// invalid example
// MT_LOGOSTART ( 3833) -> 480ms -> MT_LOGOSTOP ( 3845) -> 480ms -> MT_LOGOSTART ( 3857)
        if (    (diffStop2Start2 >=  600) && (diffStop2Start2 <=  1120) &&  // change from logo to closing logo
                (diffStart2Stop1 >= 6360) && (diffStart2Stop1 <=  8880) &&  // closing logo deteted as logo
                (diffStop1Start1 >=  720) && (diffStop1Start1 <=  1160) &&  // change from closing logo to logo
                (diffStart1Mark  >= 6360) && (diffStart1Mark  <= 18880)) { // end part between closing logo and broadcast end
            dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): found closing info logo sequence");
            // cleanup invalid logo marks
            marks.Del(stop2Before->position);
            marks.Del(start2Before->position);
            marks.Del(stop1Before->position);
            marks.Del(start1Before->position);
            return true;
        }
        else dsyslog("cMarkAdStandalone::HaveInfoLogoSequence(): logo stop mark (%d): closing logo info sequence is invalid", mark->position);
    }
    return false;
}


cMark *cMarkAdStandalone::Check_ASPECTSTOP() {
    // if we have 16:9 broadcast, aspect ratio stop mark without aspect start before is start of next broadcast
    if ((criteria->GetMarkTypeState(MT_HBORDERCHANGE) < CRITERIA_USED) && (macontext.Info.AspectRatio.num == 16) && (macontext.Info.AspectRatio.den == 9)) {
        cMark *aspectStop = marks.GetNext(0, MT_ASPECTSTOP);
        if (aspectStop) {
            const cMark *aspectStart = marks.GetPrev(aspectStop->position, MT_ASPECTSTOP);
            if (!aspectStart) {
                // do not use this aspect stop mark, broadcast stop can be before aspect stop if there is an ad between
                dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): we have 16:9 bradcast and MT_ASPECTSTOP at frame (%d) without MT_ASPECTSTART before, this is start of next broadcast", aspectStop->position);
                marks.DelAfterFromToEnd(aspectStop->position);  // keep aspect stop mark, can be a valid end mark if there is no ad
                return nullptr;
            }
        }
    }
    cMark *end = marks.GetAround(400 * (decoder->GetVideoFrameRate()), stopA, MT_ASPECTSTOP);      // changed from 360 to 400
    if (end) {
        dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): MT_ASPECTSTOP found at frame (%d)", end->position);
        if ((macontext.Info.AspectRatio.num == 4) && (macontext.Info.AspectRatio.den == 3)) {
            dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): delete all weak marks");
            marks.DelWeakFromTo(marks.GetFirst()->position + 1, end->position, MT_ASPECTCHANGE); // delete all weak marks, except start mark
        }
        else { // 16:9 broadcast with 4:3 broadcast after, maybe ad between and we have a better hborder or logo stop mark
            cMark *stopBefore  = marks.GetPrev(end->position, MT_HBORDERSTOP);         // try hborder
            if (!stopBefore) {
                stopBefore  = marks.GetPrev(end->position, MT_LOGOSTOP);  // try logo stop
                // check position of logo start mark before aspect ratio mark, if it is after logo stop mark, logo stop mark end can be end of preview in advertising
                if (stopBefore) {
                    int diffAspectStop = (end->position - stopBefore->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): logo stop mark (%d), %ds before aspect ratio stop mark", stopBefore->position, diffAspectStop);
                    if (diffAspectStop > 234) {  // check only for early logo stop marks, do not increase, there can be a late advertising and aspect stop on same frame as logo stop
                        // changed from 153 to 234
                        cMark *startLogoBefore = marks.GetPrev(end->position, MT_LOGOSTART);
                        if (startLogoBefore && (startLogoBefore->position > stopBefore->position)) {
                            dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): logo start mark (%d) between logo stop mark (%d) and aspect ratio mark (%d), this logo stop mark is end of advertising", startLogoBefore->position, stopBefore->position, end->position);
                            stopBefore = nullptr;
                        }
                    }
                }
            }
            // now we may have a hborder or a logo stop mark before aspect stop mark, check if valid
            if (stopBefore) { // maybe real stop mark was deleted because on same frame as logo/hborder stop mark
                int diffStopA      = (stopA - stopBefore->position)        /  decoder->GetVideoFrameRate();
                int diffAspectStop = (end->position - stopBefore->position) / decoder->GetVideoFrameRate();
                char *markType = marks.TypeToText(stopBefore->type);
                dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): found %s stop mark (%d) %ds before aspect ratio end mark (%d), %ds before assumed stop", markType, stopBefore->position, diffAspectStop, end->position, diffStopA);
                FREE(strlen(markType)+1, "text");
                free(markType);
                if ((diffStopA <= 867) && (diffAspectStop <= 66)) { // changed from 760 to 867, for broadcast length from info file too long
                    // changed from 39 to 40 to 66, for longer ad found between broadcasts
                    dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): there is an advertising before aspect ratio change, use stop mark (%d) before as end mark", stopBefore->position);
                    end = stopBefore;
                    // cleanup possible info logo or logo detection failure short before end mark
                    if (end->type == MT_LOGOSTOP) marks.DelFromTo(end->position - (60 * decoder->GetVideoFrameRate()), end->position - 1, MT_LOGOCHANGE, 0xF0);
                }
            }
        }
    }
    else dsyslog("cMarkAdStandalone::Check_ASPECTSTOP(): no MT_ASPECTSTOP mark found");
    return end;
}


void cMarkAdStandalone::CheckStop() {
    LogSeparator(true);
    dsyslog("cMarkAdStandalone::CheckStop(): start check stop (%d)", decoder->GetPacketNumber());

    char *indexToHMSF = marks.IndexToHMSF(stopA, AV_NOPTS_VALUE, false);
    if (indexToHMSF) {
        ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
        dsyslog("assumed stop position (%d) at %s", stopA, indexToHMSF);
        FREE(strlen(indexToHMSF)+1, "indexToHMSF");
        free(indexToHMSF);
    }
    DebugMarks();     //  only for debugging
    // check start mark, re-calculate assumed stop if later start mark was selected
    if (CheckStartMark()) {
        stopA = marks.GetFirst()->position + decoder->GetVideoFrameRate() * length;
        indexToHMSF = marks.IndexToHMSF(stopA, AV_NOPTS_VALUE, false);
        if (indexToHMSF) {
            ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
            dsyslog("new assumed stop position after final start mark selection (%d) at %s", stopA, indexToHMSF);
            FREE(strlen(indexToHMSF)+1, "indexToHMSF");
            free(indexToHMSF);
            DebugMarks();     //  only for debugging
        }
    }

    // cleanup invalid marks
    //
    // cleanup near logo stop and start marks around aspect ratio marks, they are from a fading in/out logo
    cMark *aspectMark = marks.GetNext(0, MT_ASPECTCHANGE, 0xF0);
    while (aspectMark) {
        cMark *logoMark = marks.GetAround(5 * decoder->GetVideoFrameRate(), aspectMark->position, MT_LOGOCHANGE, 0xF0);
        while (logoMark) {
            dsyslog("cMarkAdStandalone::CheckStop(): logo mark (%d) near aspect ratio mark (%d) found, this is a fading in/out logo, delete", logoMark->position, aspectMark->position);
            marks.Del(logoMark->position);
            logoMark = marks.GetAround(5 * decoder->GetVideoFrameRate(), aspectMark->position, MT_LOGOCHANGE, 0xF0);
        }
        aspectMark = marks.GetNext(aspectMark->position, MT_ASPECTCHANGE, 0xF0);
    }

    // remove logo change marks
    RemoveLogoChangeMarks(false);

    LogSeparator(true);
    dsyslog("cMarkAdStandalone::CheckStop(): marks after first cleanup:");
    DebugMarks();     //  only for debugging
    dsyslog("cMarkAdStandalone::CheckStop(): start end mark selection");

// try MT_CHANNELSTOP
    cMark *end = nullptr;
    if (criteria->GetMarkTypeState(MT_CHANNELCHANGE) >= CRITERIA_UNKNOWN) end = Check_CHANNELSTOP();

// try MT_ASPECTSTOP
    if (!end) end = Check_ASPECTSTOP();

// try MT_HBORDERSTOP
    // try hborder end if hborder used even if we got another end mark, maybe we found a better one, but not if we have a hborder end mark
    if ((!end && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) >= CRITERIA_UNKNOWN)) ||
            (end && (end->type != MT_HBORDERSTOP) && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED))) {
        cMark *hBorder = Check_HBORDERSTOP();
        if (hBorder) end = hBorder;  // do not override an existing end mark with nullptr
    }
    // cleanup all marks after hborder start from next broadcast
    if (!end && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) <= CRITERIA_UNKNOWN)) {
        cMark *hBorderStart = marks.GetNext(stopA - (60 * decoder->GetVideoFrameRate()), MT_HBORDERSTART);
        if (hBorderStart) {
            // use logo stop mark short after hborder as end mark
            cMark *logoStop = marks.GetNext(hBorderStart->position, MT_LOGOSTOP);
            if (logoStop) {
                int diff = (logoStop->position - hBorderStart->position) / decoder->GetVideoFrameRate();
                if (diff <= 2) {
                    dsyslog("cMarkAdStandalone::CheckStop(): logo stop mark (%d) %ds after hborder start mark (%d), use it as end mark", logoStop->position, diff, hBorderStart->position);
                    marks.Del(hBorderStart->position);
                    end = logoStop;
                }
            }
            else {
                dsyslog("cMarkAdStandalone::CheckStop(): delete all marks after hborder start (%d) from next broadcast", hBorderStart->position);
                marks.DelTill(hBorderStart->position, false);
            }
        }
    }

// try MT_VBORDERSTOP
    if (!end && (criteria->GetMarkTypeState(MT_VBORDERCHANGE) >= CRITERIA_UNKNOWN)) end = Check_VBORDERSTOP();

    // cleanup all marks after vborder start from next broadcast
    if (!end && (criteria->GetMarkTypeState(MT_VBORDERCHANGE) <= CRITERIA_UNKNOWN)) {
        cMark *vBorderStart = marks.GetNext(stopA - (MAX_ASSUMED * decoder->GetVideoFrameRate()), MT_VBORDERSTART); // changed from 60 to MAX_ASSUMED
        if (vBorderStart) {
            // use logo stop mark short after vborder as end mark
            cMark *logoStop = marks.GetNext(vBorderStart->position, MT_LOGOSTOP);
            if (logoStop) {
                int diff = (logoStop->position - vBorderStart->position) / decoder->GetVideoFrameRate();
                if (diff <= 2) {
                    dsyslog("cMarkAdStandalone::CheckStop(): logo stop mark (%d) %ds after vborder start mark (%d), use it as end mark", logoStop->position, diff, vBorderStart->position);
                    marks.Del(vBorderStart->position);
                    end = logoStop;
                }
            }
            if (!end) {
                dsyslog("cMarkAdStandalone::CheckStop(): delete all marks after vborder start (%d) from next broadcast", vBorderStart->position);
                marks.DelTill(vBorderStart->position, false);
            }
        }
    }

// try MT_LOGOSTOP
    if (!end) end = Check_LOGOSTOP();   // logo detection can be disabled in end part after aspect ratio change
    // detect very short channel start before, this is start from next broadcast
    if (end && (criteria->GetMarkTypeState(MT_CHANNELCHANGE) < CRITERIA_USED)) {
        cMark *prevChannelStart = marks.GetPrev(end->position, MT_CHANNELSTART);
        if (prevChannelStart) {
            int deltaChannelStart = 1000 * (end->position - prevChannelStart->position) / decoder->GetVideoFrameRate();
            if (deltaChannelStart <= 1000) {
                dsyslog("cMarkAdStandalone::CheckStop(): channel start mark (%d) %dms before logo end mark (%d) is start of next broadcast, delete mark", prevChannelStart->position, deltaChannelStart, end->position);
                marks.Del(prevChannelStart->position);
            }
        }
    }

// no end mark found, try if we can use a start mark of next bradcast as end mark, but not for channel with good VPS and length is from VPS event
    if (!macontext.Info.lengthFromVPS || !criteria->GoodVPS()) {
        // no valid stop mark found, use MT_ASPECTSTOP (broadcast must be 16:9, start of next broadcast with 4:3)
        if (!end && (criteria->GetMarkTypeState(MT_ASPECTCHANGE) != CRITERIA_USED)) { // not possible is we use aspect marks in this broadcast
            cMark *aspectStop = marks.GetNext(stopA, MT_ASPECTSTOP);
            if (aspectStop) {
                dsyslog("cMarkAdStandalone::CheckStop(): use aspect stop mark (%d) from start of next 4:3 broadcast as end mark", aspectStop->position);
                end = aspectStop;
            }
        }
        // no valid stop mark found, try if there is a MT_CHANNELSTART from next broadcast
        if (!end && (criteria->GetMarkTypeState(MT_CHANNELCHANGE) != CRITERIA_USED)) { // not possible is we use channel mark in this broadcast
            cMark *channelStart = marks.GetNext(stopA, MT_CHANNELSTART);
            if (channelStart) {
                dsyslog("cMarkAdStandalone::CheckStop(): use channel start mark (%d) from next broadcast as end mark", channelStart->position);
                marks.ChangeType(channelStart, MT_STOP);
                end = channelStart;
            }
        }
        // try to get hborder start mark from next broadcast as stop mark
        if (!end && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) != CRITERIA_USED)) {
            cMark *hBorderStart = marks.GetNext((stopA - (240 *  decoder->GetVideoFrameRate())), MT_HBORDERSTART);  // accept max 4 min before stopA
            if (hBorderStart) {
                cMark *hBorderStop = marks.GetNext(hBorderStart->position, MT_HBORDERSTOP);
                if (hBorderStop) { // check length of hborder
                    int diff = (hBorderStop->position - hBorderStart->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::CheckStop(): MT_HBORDERSTART (%ds) -> %ds -> MT_HBORDERSTOP (%d)", hBorderStart->position, diff, hBorderStop->position);
                    // example of hborder from closing credits
                    //  MT_HBORDERSTART (284293s) -> 81s -> MT_HBORDERSTOP (288356)
                    if (diff <= 81) {
                        dsyslog("cMarkAdStandalone::CheckStop(): hborder too short, maybe from closing credists, delete marks");
                        marks.Del(hBorderStart);
                        marks.Del(hBorderStop);
                        hBorderStart = nullptr;
                    }
                }
                if (hBorderStart) {  // can be deleted above
                    dsyslog("cMarkAdStandalone::CheckStop(): found hborder start mark (%d) from next broadcast at end of recording", hBorderStart->position);
                    cMark *prevMark = marks.GetPrev(hBorderStart->position, MT_ALL);
                    if ((prevMark->type & 0x0F) == MT_START) {
                        dsyslog("cMarkAdStandalone::CheckStop(): start mark (%d) before found, use hborder start mark (%d) from next broadcast as end mark", prevMark->position, hBorderStart->position);
                        if (criteria->GetMarkTypeState(MT_LOGOCHANGE) == CRITERIA_USED) {  // if we use logo marks, there must be a valid logo stop mark before hborder start
                            cMark *lStop = marks.GetPrev(hBorderStart->position, MT_LOGOSTOP);  // try to find a early logo stop, maybe too long broadcast from info fileA
                            if (lStop) {
                                int diffAssumed = (stopA - lStop->position) / decoder->GetVideoFrameRate();
                                dsyslog("cMarkAdStandalone::CheckStop(): logo stop (%d) %ds before hborder start (%d)", lStop->position, diffAssumed, hBorderStart->position);
                                if (diffAssumed <= 251) end = lStop;
                            }
                        }
                        if (!end) {
                            end = marks.ChangeType(hBorderStart, MT_STOP);
                            if (end) end = marks.Move(end, end->position, end->pts, MT_TYPECHANGESTOP);  // one frame before hborder start is end mark
                        }
                    }
                    else {
                        dsyslog("cMarkAdStandalone::CheckStop(): use stop mark (%d) before hborder start mark (%d) ", prevMark->position, hBorderStart->position);
                        end = prevMark;
                    }
                }
            }
        }
    }

    if (end) {
        indexToHMSF = marks.GetTime(end);
        char *markType = marks.TypeToText(end->type);
        if (indexToHMSF && markType) {
            isyslog("using %s stop mark on position (%d) at %s as end mark", markType, end->position, indexToHMSF);
            FREE(strlen(markType)+1, "text");
            free(markType);
        }
    }
    else {  // no end mark found at all, set end mark to assumed end
        dsyslog("cMarkAdStandalone::CheckStop(): no stop mark found, add end mark at assumed end (%d)", stopA);
        cMark *markBefore = marks.GetPrev(stopA, MT_ALL);
        if (markBefore && ((markBefore->type & 0x0F) == MT_STOP)) {
            dsyslog("cMarkAdStandalone::CheckStop(): mark before (%d) assumed stop (%d) is a stop mark, use this as end mark", markBefore->position, stopA);
            end = markBefore;
        }
        else {
            int lastPacketNumber = index->GetLastPacket()->packetNumber;
            int stopPos          = stopA;
            if (stopPos > lastPacketNumber) {
                dsyslog("cMarkAdStandalone::CheckStop(): assumed stop (%d) after recording end (%d), use recording end", stopA, lastPacketNumber);
                stopPos = lastPacketNumber;
            }
            sMarkAdMark mark  = {};
            mark.position = index->GetKeyPacketNumberBefore(stopPos, &mark.framePTS);  // adjust to i-frame if no full decoding
            mark.type     = MT_ASSUMEDSTOP;
            AddMark(&mark);
            end = marks.Get(mark.position);
        }
    }


    // delete all marks after end mark
    if (end) { // be save, if something went wrong end = nullptr
        dsyslog("cMarkAdStandalone::CheckStop(): delete all marks after final stop mark at (%d)", end->position);
        const cMark *startBefore = marks.GetPrev(end->position, MT_START, 0x0F);
        if (!startBefore) {
            esyslog("cMarkAdStandalone::CheckStop(): invalid marks, no start mark before end mark");
            sMarkAdMark mark = {};
            mark.position    = 0;
            mark.type        = MT_RECORDINGSTART;
            AddMark(&mark);
        }
        marks.DelTill(end->position, false);
    }
    else esyslog("could not find a end mark");

    // cleanup detection failures (e.g. very long dark scenes), keep start end end mark, they can be from different type
    if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_UNAVAILABLE) marks.DelFromTo(marks.First()->position + 1, end->position - 1, MT_HBORDERCHANGE, 0xF0);
    if (criteria->GetMarkTypeState(MT_VBORDERCHANGE) == CRITERIA_UNAVAILABLE) marks.DelFromTo(marks.First()->position + 1, end->position - 1, MT_VBORDERCHANGE, 0xF0);

    DebugMarks();     //  only for debugging
    dsyslog("cMarkAdStandalone::CheckStop(): end check stop");
    doneCheckStop = true;
    LogSeparator();
    return;
}


// check if last stop mark is start of closing credits without logo or hborder
// move stop mark to end of closing credit
// <stopMark> last logo or hborder stop mark
// return: true if closing credits was found and last logo stop mark position was changed
//
bool cMarkAdStandalone::MoveLastStopAfterClosingCredits(cMark *stopMark) {
    if (!stopMark) return false;
    // check if channel uses closing credits without logo
    int closingCreditsState = criteria->GetClosingCreditsState(stopMark->position);
    if (closingCreditsState < CRITERIA_UNKNOWN) {
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): no check necessary, closing credits state: %d", closingCreditsState);
        return false;
    }
    dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): detect closing credits in frame without logo after position (%d)", stopMark->position);
    // check if we already know if there is a closing credits
    sMarkPos endClosingCredits = {-1};
    enum eEvaluateStatus isClosingStatus = STATUS_UNKNOWN;
    if (evaluateLogoStopStartPair) isClosingStatus = evaluateLogoStopStartPair->GetIsClosingCredits(stopMark->position, stopMark->position + (MAX_CLOSING_CREDITS_SEARCH * decoder->GetVideoFrameRate()), &endClosingCredits);
    switch (isClosingStatus) {
    case STATUS_DISABLED:
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): IsClosingCredits state DISABLED");
        break;
    case STATUS_NO:
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): IsClosingCredits state NO");
        break;
    case STATUS_ERROR:
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): IsClosingCredits state ERROR"); // pair not found, try detection now
    case STATUS_UNKNOWN:  // we have to detect now
    {
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): IsClosingCredits state UNKNOWN or ERROR, detect now");
        if (!detectLogoStopStart) {  // init in RemoveLogoChangeMarks(), but maybe not used
            detectLogoStopStart = new cDetectLogoStopStart(decoder, index, criteria, evaluateLogoStopStartPair, video->GetLogoCorner());
            ALLOC(sizeof(*detectLogoStopStart), "detectLogoStopStart");
        }
        // check current read position of decoder
        if (stopMark->position < decoder->GetPacketNumber()) decoder->Restart();
        int endPos = stopMark->position + (MAX_CLOSING_CREDITS_SEARCH * decoder->GetVideoFrameRate());  // try till MAX_CLOSING_CREDITS_SEARCH after stopMarkPosition
        endClosingCredits = {-1};
        if (detectLogoStopStart->Detect(stopMark->position, endPos)) detectLogoStopStart->ClosingCredit(stopMark->position, endPos, &endClosingCredits);
    }
    break;
    case STATUS_YES:
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): use known closing credits from (%d) to (%d)", stopMark->position, endClosingCredits.position);
        break;
    default:
        esyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): IsClosingCredits state invalid %d", isClosingStatus);
        break;
    }

    // move mark if closing credits found
    if (endClosingCredits.position > stopMark->position) {
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): closing credits found, move stop mark to position (%d) PTS %" PRId64, endClosingCredits.position, endClosingCredits.pts);
        marks.Move(stopMark, endClosingCredits.position, endClosingCredits.pts, MT_CLOSINGCREDITSSTOP);
        return true;
    }
    else {
        dsyslog("cMarkAdStandalone::MoveLastStopAfterClosingCredits(): no closing credits found");
        return false;
    }
}


// remove logo stop/start pairs from logo changes / info logo / introduction logo
// have to be done before end mark selection to prevent to select wrong end mark
//
void cMarkAdStandalone::RemoveLogoChangeMarks(const bool checkStart) {
    if (abortNow) return;
    LogSeparator(true);
    dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): start detect and remove logo stop/start marks from special logo");

    // only if there are at last one corect logo stop/start and one logo/stop/start from special logo
    if (marks.Count(MT_LOGOCHANGE, 0xF0) < 4) {
        dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): to less logo marks");
        return;
    }
    // check if this channel has special logos, for performance reason only known and tested channels
    if (!criteria->IsInfoLogoChannel() && !criteria->IsLogoChangeChannel() && !criteria->IsClosingCreditsChannel() && !criteria->IsAdInFrameWithLogoChannel()) {
        dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): channel not in list for special logo");
        return;
    }

    // do not messup decoder read position if called by CheckStart(), use new instance for detection
    // use local variables with same name as global
    cDecoder *decoder_local = decoder;
    if (checkStart) {
        decoder_local = new cDecoder(macontext.Config->recDir, macontext.Config->threads, macontext.Config->fullDecode, macontext.Config->hwaccel, macontext.Config->forceHW,  macontext.Config->forceInterlaced, nullptr);  // no index
        ALLOC(sizeof(*decoder_local), "decoder_local");
        if (!decoder_local->ReadNextFile()) { // force init decoder to get infos about video (frame rate is used by cEvaluateLogoStopStartPair)
            esyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): failed to open first video file");
            return;
        }
        if (detectLogoStopStart) {
            esyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): object detectLogoStopStart should not exists");
            return;
        }
        if (evaluateLogoStopStartPair) {
            esyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): object evaluateLogoStopStartPair should not exists");
            return;
        }
    }
    else decoder_local->Restart();  // we are called from CheckStop(), decoder read position is at end of recording

    // check if objects exists, otherwise create new with global variables
    if (!detectLogoStopStart) {
        detectLogoStopStart = new cDetectLogoStopStart(decoder_local, index, criteria, evaluateLogoStopStartPair, video->GetLogoCorner());
        ALLOC(sizeof(*detectLogoStopStart), "detectLogoStopStart");
    }

    if (!evaluateLogoStopStartPair) {
        evaluateLogoStopStartPair = new cEvaluateLogoStopStartPair(decoder_local, criteria);
        ALLOC(sizeof(*evaluateLogoStopStartPair), "evaluateLogoStopStartPair");
    }
    else evaluateLogoStopStartPair->SetDecoder(decoder_local);

    evaluateLogoStopStartPair->CheckLogoStopStartPairs(&marks, &blackMarks, startA, packetCheckStart, packetEndPart, stopA);

    char *indexToHMSFStop      = nullptr;
    char *indexToHMSFStart     = nullptr;
    sLogoStopStartPair logoStopStartPair;

    // loop through all logo stop/start pairs
    while (evaluateLogoStopStartPair->GetNextPair(&logoStopStartPair)) {
        if (abortNow) return;
        if (logoStopStartPair.stopPosition <= IGNORE_AT_START) {
            dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): ignore initial mark (%d)", logoStopStartPair.stopPosition);
            continue;
        }
        if (!marks.Get(logoStopStartPair.startPosition) || !marks.Get(logoStopStartPair.stopPosition)) continue;  // at least one of the mark from pair was deleted, nothing to do
        if ((logoStopStartPair.isLogoChange <= STATUS_NO) && (logoStopStartPair.isInfoLogo <= STATUS_NO) && (logoStopStartPair.isClosingCredits <= STATUS_NO)) continue;

        if (decoder_local->GetPacketNumber() >= logoStopStartPair.stopPosition) {
            dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): packet (%d): overlapping pairs from info logo merge, skip pair logo stop (%d) start (%d)", decoder_local->GetPacketNumber(), logoStopStartPair.stopPosition, logoStopStartPair.startPosition);
            continue;
        }

        LogSeparator();
        // free from loop before
        if (indexToHMSFStop) {
            FREE(strlen(indexToHMSFStop)+1, "indexToHMSF");
            free(indexToHMSFStop);
        }
        if (indexToHMSFStart) {
            FREE(strlen(indexToHMSFStart)+1, "indexToHMSF");
            free(indexToHMSFStart);
        }
        // get time of marks and log marks
        indexToHMSFStop = marks.IndexToHMSF(logoStopStartPair.stopPosition, AV_NOPTS_VALUE, false);
        if (indexToHMSFStop) {
            ALLOC(strlen(indexToHMSFStop)+1, "indexToHMSF");
        }
        indexToHMSFStart = marks.IndexToHMSF(logoStopStartPair.startPosition, AV_NOPTS_VALUE, false);
        if (indexToHMSFStart) {
            ALLOC(strlen(indexToHMSFStart)+1, "indexToHMSF");
        }
        if (indexToHMSFStop && indexToHMSFStart) {
            dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): check logo stop (%d) at %s and logo start (%d) at %s, isInfoLogo %d", logoStopStartPair.stopPosition, indexToHMSFStop, logoStopStartPair.startPosition, indexToHMSFStart, logoStopStartPair.isInfoLogo);
        }
        // only closing credits check have to be done, limit search range
        if ((logoStopStartPair.isInfoLogo <= STATUS_NO) && (logoStopStartPair.isLogoChange <= STATUS_NO)) {
            if ((logoStopStartPair.startPosition - logoStopStartPair.stopPosition) / decoder->GetVideoFrameRate() > MAX_CLOSING_CREDITS_SEARCH) {
                // set new search range
                logoStopStartPair.startPosition = logoStopStartPair.stopPosition + (MAX_CLOSING_CREDITS_SEARCH * decoder->GetVideoFrameRate());
                dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): search range too big for only closing credits search, reduce search range from stop mark (%d) to (%d)", logoStopStartPair.stopPosition,  logoStopStartPair.startPosition);
            }
        }
        // start detection
        if (detectLogoStopStart->Detect(logoStopStartPair.stopPosition, logoStopStartPair.startPosition)) {
            bool doInfoCheck = true;
            // check for closing credits if no other checks will be done, only part of the loop elements in recording end range
            sMarkPos endClosingCredits = {-1};
            if ((logoStopStartPair.isInfoLogo <= STATUS_NO) && (logoStopStartPair.isLogoChange <= STATUS_NO)) detectLogoStopStart->ClosingCredit(logoStopStartPair.stopPosition, logoStopStartPair.startPosition, &endClosingCredits);

            // check for info logo if  we are called by CheckStart and we are in broadcast
            if ((startA > 0) && criteria->IsIntroductionLogoChannel() && (logoStopStartPair.isStartMarkInBroadcast == STATUS_YES)) {
                // do not delete info logo, it can be introduction logo, it looks the same
                // expect we have another start very short before
                cMark *lStartBefore = marks.GetPrev(logoStopStartPair.stopPosition, MT_LOGOSTART);
                if (lStartBefore) {
                    int diffStart = 1000 * (logoStopStartPair.stopPosition - lStartBefore->position) / decoder_local->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): logo start (%d) %dms before stop mark (%d)", lStartBefore->position, diffStart, logoStopStartPair.stopPosition);
                    if (diffStart > 1240) {  // do info logo check if we have a logo start mark short before, some channel send a early info log after broadcast start
                        // changed from 1160 to 1240
                        dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): do not check for info logo, we are in start range, it can be introducion logo");
                        doInfoCheck = false;
                    }
                }
            }
            if (doInfoCheck && (logoStopStartPair.isInfoLogo >= STATUS_UNKNOWN) && detectLogoStopStart->IsInfoLogo(logoStopStartPair.stopPosition, logoStopStartPair.startPosition, logoStopStartPair.hasBorderAroundStart)) {
                // found info logo part
                if (indexToHMSFStop && indexToHMSFStart) {
                    dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): info logo found between frame (%i) at %s and (%i) at %s, deleting marks between this positions", logoStopStartPair.stopPosition, indexToHMSFStop, logoStopStartPair.startPosition, indexToHMSFStart);
                }
                evaluateLogoStopStartPair->SetIsInfoLogo(logoStopStartPair.stopPosition, logoStopStartPair.startPosition);
                marks.DelFromTo(logoStopStartPair.stopPosition, logoStopStartPair.startPosition, MT_LOGOCHANGE, 0xF0);  // maybe there a false start/stop inbetween
            }
            // check logo change
            if ((logoStopStartPair.isLogoChange >= STATUS_UNKNOWN) && detectLogoStopStart->IsLogoChange(logoStopStartPair.stopPosition, logoStopStartPair.startPosition)) {
                if (indexToHMSFStop && indexToHMSFStart) {
                    isyslog("logo change between frame (%6d) at %s and (%6d) at %s, deleting marks between this positions", logoStopStartPair.stopPosition, indexToHMSFStop, logoStopStartPair.startPosition, indexToHMSFStart);
                }
                marks.DelFromTo(logoStopStartPair.stopPosition, logoStopStartPair.startPosition, MT_LOGOCHANGE, 0xF0);  // maybe there a false start/stop inbetween
            }
        }
    }

    // delete buffer and objects
    if (indexToHMSFStop) {
        FREE(strlen(indexToHMSFStop)+1, "indexToHMSF");
        free(indexToHMSFStop);
    }
    if (indexToHMSFStart) {
        FREE(strlen(indexToHMSFStart)+1, "indexToHMSF");
        free(indexToHMSFStart);
    }

    // delete only one time used object from CheckStart() call, keep evaluateLogoStopStartPair for later use
    if (checkStart) {
        FREE(sizeof(*decoder_local), "decoder_local");
        delete decoder_local;
        decoder_local = nullptr;
        FREE(sizeof(*detectLogoStopStart), "detectLogoStopStart");
        delete detectLogoStopStart;
        detectLogoStopStart = nullptr;
    }

    dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): marks after detect and remove logo stop/start mark pairs with special logo");
    DebugMarks();     //  only for debugging
    dsyslog("cMarkAdStandalone::RemoveLogoChangeMarks(): end detect and remove logo stop/start mark pairs with special logo");
    LogSeparator();
}


// fix aspect info, invert MT_ASPECTSTART and MT_ASPECTSTOP and fix position
void cMarkAdStandalone::SwapAspectRatio() {
    dsyslog("cMarkAdStandalone::SwapAspectRatio(); aspect ratio from VDR info file was wrong, swap aspect ratio marks");
    if ((macontext.Info.AspectRatio.num == 4) && (macontext.Info.AspectRatio.den == 3)) {
        macontext.Info.AspectRatio.num = 16;
        macontext.Info.AspectRatio.den =  9;
        marks.DelType(MT_ASPECTCHANGE, 0xF0); // aspect marks with 16:9 videos are invalid
    }
    else {
        macontext.Info.AspectRatio.num = 4;
        macontext.Info.AspectRatio.den = 3;
    }
    // we will be one packet before/after start/stop, correct later with scene change
    cMark *aMark = marks.GetFirst();
    while (aMark) {
        if      (aMark->type == MT_ASPECTSTART) aMark->type = MT_ASPECTSTOP;
        else if (aMark->type == MT_ASPECTSTOP)  aMark->type = MT_ASPECTSTART;
        aMark = aMark->Next();
    }
    video->SetAspectRatioBroadcast(macontext.Info.AspectRatio);
    dsyslog("cMarkAdStandalone::SwapAspectRatio(): new aspect ratio %d:%d, fixed marks are:", macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den);
    DebugMarks();     //  only for debugging
}


cMark *cMarkAdStandalone::Check_CHANNELSTART() {
    dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): search for channel start mark");

    // cleanup very short channel start/stop pairs, they are from stream error
    cMark *channelStart = marks.GetNext(-1, MT_CHANNELSTART);
    while (channelStart) {
        cMark *channelStop = marks.GetNext(channelStart->position, MT_CHANNELSTOP);
        if (channelStop) {
            int diff = 1000 * (channelStop->position - channelStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): channel start (%d), channel stop (%d), length broadcast %dms", channelStart->position, channelStop->position, diff);
            if (diff < 200) {
                dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): broadcast too short, delete invalid marks");
                cMark *channelStartNext = marks.GetNext(channelStart->position, MT_CHANNELSTART);
                marks.Del(channelStart->position);
                marks.Del(channelStop->position);
                channelStart = channelStartNext;
                continue;
            }
        }
        channelStart = marks.GetNext(channelStart->position, MT_CHANNELSTART);
    }

    // delete very early first mark, if channels send ad with 6 channels, this can be wrong
    channelStart = marks.GetNext(-1, MT_CHANNELSTART);
    if (channelStart) {
        criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);  // there is a 6 channel audio in broadcast, may we can use it later
        if (channelStart->position < IGNORE_AT_START) marks.Del(channelStart->position);
    }
    // 6 channel double episode, there is no channel start mark
    if (decoder->GetAC3ChannelCount() >= 5) criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);  // there is a 6 channel audio in broadcast, may we can use it later

    // search channel start mark
    channelStart = marks.GetAround(MAX_ASSUMED * decoder->GetVideoFrameRate(), startA, MT_CHANNELSTART);
    // check audio streams
    if (channelStart) {
        dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): channels start at (%d)", channelStart->position);
        // we have a channel change, cleanup border and aspect ratio
        video->ClearBorder();
        marks.DelType(MT_ASPECTCHANGE, 0xF0);

        int diffAssumed = (channelStart->position - startA) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): audio channel start mark found at (%d) %ds after assumed start", channelStart->position, diffAssumed);
        if (channelStart->position > stopA) {  // this could be a very short recording, 6 channel is in post recording
            dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): audio channel start mark after assumed stop mark not valid");
            return nullptr;
        }

        // for early channel start mark, check if there is a logo start mark stop/start pair near assumed start
        // this can happen if previous broadcast has also 6 channel
        if (diffAssumed <= -121) {
            const cMark *logoStop = marks.GetNext(channelStart->position, MT_LOGOSTOP);
            if (logoStop) {  // if channel start is from previous recording, we should have a logo stop mark near assumed start
                cMark *logoStart = marks.GetNext(logoStop->position, MT_LOGOSTART);
                if (logoStart) {
                    int diffLogoStart = (logoStart->position - startA) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): found logo start mark (%d) %ds after assumed start", logoStart->position, diffLogoStart);
                    if ((diffLogoStart >= -4) && (diffLogoStart <= 56)) {  // changed from -1 to -4, changed from 17 to 56
                        dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): use logo start mark (%d) as start mark", logoStart->position);
                        criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
                        return logoStart;
                    }
                }
            }
        }
        // now we have a final channel start mark
        marks.DelType(MT_LOGOCHANGE,    0xF0);
        marks.DelType(MT_HBORDERCHANGE, 0xF0);
        marks.DelType(MT_VBORDERCHANGE, 0xF0);
        marks.DelWeakFromTo(0, INT_MAX, MT_CHANNELCHANGE); // we have a channel start mark, delete all weak marks
        criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
        return channelStart;
    }
    dsyslog("cMarkAdStandalone::Check_CHANNELSTART(): no audio channel start mark found");
    return nullptr;
}



cMark *cMarkAdStandalone::Check_LOGOSTART() {
    if (!index) {
        esyslog("cMarkAdStandalone::Check_LOGOSTART(): index not valid");
        return nullptr;
    }
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): search for logo start mark");
    cMark *begin = nullptr;
    if (!evaluateLogoStopStartPair) {
        evaluateLogoStopStartPair = new cEvaluateLogoStopStartPair(decoder, criteria);
        ALLOC(sizeof(*evaluateLogoStopStartPair), "evaluateLogoStopStartPair");
    }
    else evaluateLogoStopStartPair->SetDecoder(decoder);

    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): cleanup invalid logo marks");
    // remove very early logo start marks, this can be delayed logo start detection
    cMark *lStart = marks.GetNext(-1, MT_LOGOSTART);  // get first logo start mark
    while (lStart) {
        int startOffset = index->GetTimeOffsetFromPTS(lStart->pts) / 1000;  // in seconds
        if (startOffset <= 14) {  // changed from 11 to 14
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start (%5d) %ds after recording start too early", lStart->position, startOffset);
            cMark *tmpMark = marks.GetNext(lStart->position, MT_LOGOSTART);  // there can be more than one early logo start
            marks.Del(lStart->position);
            lStart = tmpMark;
            continue;
        }
        else break;
        lStart = marks.GetNext(lStart->position, MT_LOGOSTART);
    }
    // remove very short logo start/stop pairs, this, is a false positive logo detection
    lStart = marks.GetNext(-1, MT_LOGOSTART);  // get first logo start mark
    while (lStart) {
        cMark *lStop = marks.GetNext(lStart->position, MT_LOGOSTOP);
        if (lStop) {
            int diff = 1000 * (lStop->position - lStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start (%5d) logo stop  (%5d), distance %5dms", lStart->position, lStop->position, diff);
            if (diff <= 60) {  // in ms
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): false positiv logo detection: distance too short, deleting marks");
                cMark *tmpMark = marks.GetNext(lStart->position, MT_LOGOSTART);  // there can be more than one early logo start
                marks.Del(lStop->position);
                marks.Del(lStart->position);
                lStart = tmpMark;
                continue;
            }
        }
        lStart = marks.GetNext(lStart->position, MT_LOGOSTART);
    }
    // remove very short logo stop/start pairs for channel with logo interuption, these are no valid start marks
    if (criteria->IsLogoInterruptionChannel()) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART():remove very short logo stop/start pairs for channel with logo interuption");
        cMark *lStop = marks.GetNext(-1, MT_LOGOSTOP);  // get first logo stop mark
        while (lStop) {
            lStart = marks.GetNext(lStop->position, MT_LOGOSTART);
            if (lStart) {
                int diff = 1000 * (lStart->position - lStop->position) / decoder->GetVideoFrameRate();
                int startAdiff = (lStop->position - startA) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo stop  (%5d) logo start (%5d), distance %6dms, %ds after assumed start (%d)", lStop->position, lStart->position, diff, startAdiff, startA);
                // valid logo stop/start pair
                // do not delete late logo stop, this can be a valid stop mark of first part
                // logo stop  (18837) logo start (18854), distance    680ms, 452s after assumed start (7525)
                if ((diff <= 1000) && (startAdiff < 452)) {  // changed from 480 to 1000, do not delete late logo stop, this can be a valid stop mark of first part
                    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo interuption channel: distance too short, deleting marks");
                    cMark *tmpMark = marks.GetNext(lStop->position, MT_LOGOSTOP);  // there can be more than one early logo start
                    marks.Del(lStart->position);
                    marks.Del(lStop->position);
                    lStop = tmpMark;
                    continue;
                }
            }
            lStop = marks.GetNext(lStop->position, MT_LOGOSTOP);
        }
    }

// search for logo start mark around assumed start
    int maxAssumed = MAX_ASSUMED;
    if (macontext.Info.startFromVPS && criteria->GoodVPS()) {
        maxAssumed = 30;  // if we use a valid VPS event based start time do only near search, found preview with logo 31s before broadcast, changed from 56 to 30
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): channel with good VPS, max distance from VPS start event %ds", maxAssumed);
    }
    cMark *lStartAssumed = marks.GetAround(maxAssumed * decoder->GetVideoFrameRate(), startA, MT_LOGOSTART);
    if (!lStartAssumed) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): no logo start mark found in %ds around assumed start (%d)", maxAssumed, startA);
        return nullptr;
    }
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): nearest logo start mark (%d) to assumed start (%d)", lStartAssumed->position, startA);


// try to select best logo start mark based on closing credits follow
    LogSeparator(true);
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check for logo start mark based on closing credits from previous broadcast");
// prevent to detect ad in frame from previous broadcast as closing credits
    if (!criteria->IsAdInFrameWithLogoChannel() &&
            criteria->IsClosingCreditsChannel()) {
        // search from nearest logo start mark to end, first mark can be before startA
        lStart = lStartAssumed;
        while (!begin && lStart) {
            LogSeparator(false);
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check logo start mark (%d) based on closing credits after assumed start", lStart->position);
            int status = evaluateLogoStopStartPair->GetIsClosingCreditsBefore(lStart->position);
            int diffAssumed = (lStart->position - startA) / decoder->GetVideoFrameRate();
            if ((diffAssumed < -maxAssumed) || (diffAssumed > maxAssumed)) break;
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d): %ds after assumed start (%d), closing credits status %d", lStart->position, diffAssumed, startA, status);
            if (status == STATUS_YES) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d) has closing credits in frame before, valid start mark found", lStart->position);
                begin = lStart;
                break;
            }
            lStart = marks.GetNext(lStart->position, MT_LOGOSTART);
        }
        // search from nearest logo start mark to recording start
        lStart = lStartAssumed;
        while (!begin) {
            lStart = marks.GetPrev(lStart->position, MT_LOGOSTART);
            if (!lStart) break;
            LogSeparator(false);
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check logo start mark (%d) based on closing credits before assumed start", lStart->position);
            int status = evaluateLogoStopStartPair->GetIsClosingCreditsBefore(lStart->position);
            int diffAssumed = (startA - lStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d): %ds before assumed start (%d), closing credits status %d", lStart->position, diffAssumed, startA, status);
            if (diffAssumed > maxAssumed) break;
            if (status == STATUS_YES) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d) has closing credits in frame before, valid start mark found", lStart->position);
                begin = lStart;
            }
        }
    }

// try to select best logo start mark based on lower border
    lStart = lStartAssumed;
    while (!begin) { // search from nearest logo start mark to end, first mark can be before startA
        if (HaveLowerBorder(lStart)) {
            begin = lStart;
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d) has lower border before, valid start mark found", lStart->position);
            break;
        }
        lStart = marks.GetNext(lStart->position, MT_LOGOSTART);
        if (!lStart) break;
        int diffAssumed = (lStart->position - startA) / decoder->GetVideoFrameRate();
        if ((diffAssumed < -maxAssumed) || (diffAssumed > maxAssumed)) break;
    }
    lStart = lStartAssumed;
    while (!begin) {  // search from nearest logo start mark to recording start
        lStart = marks.GetPrev(lStart->position, MT_LOGOSTART);
        if (!lStart) break;
        int diffAssumed = (lStart->position - startA) / decoder->GetVideoFrameRate();
        if ((diffAssumed < -maxAssumed) || (diffAssumed > maxAssumed)) break;
        if (HaveLowerBorder(lStart)) {
            begin = lStart;
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): start mark (%d) has lower border before, valid start mark found", lStart->position);
        }
    }

// try to select best logo start mark based on black screen, silence or info logo sequence
    LogSeparator(true);
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check for logo start mark with black screen or silence separator");
    lStart = lStartAssumed;
    while (!begin && lStart) {
        int diffAssumed = (lStart->position - startA) / decoder->GetVideoFrameRate();
        LogSeparator();
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check logo start mark (%d), %ds after assumed start", lStart->position, diffAssumed);
        if (diffAssumed > maxAssumed) {
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) too late for valid broadcast start", lStart->position);
            break;
        }
        if (HaveBlackSeparator(lStart) || HaveSilenceSeparator(lStart) || HaveInfoLogoSequence(lStart)) {
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark has separator, start mark (%d) is valid", lStart->position);
            begin = lStart;
            break;
        }
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) has no separator", lStart->position);
        lStart = marks.GetNext(lStart->position, MT_LOGOSTART);
    }
// search from nearest logo start mark to recording start
    lStart = lStartAssumed;
    while (!begin) {
        lStart = marks.GetPrev(lStart->position, MT_LOGOSTART);
        if (!lStart) break;
        int diffAssumed = (startA - lStart->position) / decoder->GetVideoFrameRate();
        if (diffAssumed > maxAssumed) break;
        LogSeparator(false);
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): check logo start mark (%d) %ds before assumed start", lStart->position, diffAssumed);
        if (HaveBlackSeparator(lStart) || HaveSilenceSeparator(lStart) || HaveInfoLogoSequence(lStart)) {
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark has separator, start mark (%d) is valid", lStart->position);
            begin = lStart;
            break;
        }
    }

    if (begin) CleanupUndetectedInfoLogo(begin);  // strong broadcast start found, cleanup undetected info logos, introduction logos short after final start mark
    LogSeparator();


    lStart = lStartAssumed;
    while (!begin && lStart) {
        // check for too early, can be start of last part from previous broadcast
        int diffAssumed = (startA - lStart->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) near to assumed start, %ds before assumed start (%d), max %ds", lStart->position, diffAssumed, startA, maxAssumed);
        if (diffAssumed >= maxAssumed) { // do not accept start mark if it is more than maxAssumed min before assumed start
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) %ds before assumed start too early", lStart->position, diffAssumed);
            cMark *lNext = marks.GetNext(lStart->position, MT_LOGOSTART);  // get next logo start mark
            marks.Del(lStart);
            lStart = lNext;
            continue;
        }
        // check for too late logo start, can be of first ad
        if (diffAssumed < -maxAssumed) {  // do not accept start mark if it is more than maxAssumed min after assumed start
            // if logo start mark is after a long part without logo, it should be valid, maybe too late broadcast start
            cMark *logoStopBefore = marks.GetPrev(lStart->position, MT_LOGOSTOP);
            if (!logoStopBefore) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) %ds after assumed start is valid, first logo start mark", lStart->position, -diffAssumed);
                begin = lStart;  // start with nearest start mark to assumed start
            }
            else {
                int adLength = (lStart->position - logoStopBefore->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) %ds after assumed start, length ad before %d ", lStart->position, -diffAssumed, adLength);
                if (adLength >= 114) { // changed from 137 to 114
                    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) after long ad should be valid", lStart->position);
                    begin = lStart;  // start with nearest start mark to assumed start
                }
            }
            if (!begin) {
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo start mark (%d) %ds after assumed start too late", lStart->position, -diffAssumed);
                break;
            }
        }
        else {
            begin = lStart;  // start with nearest start mark to assumed start
        }

        // check next logo stop/start pair
        cMark *lStop = marks.GetNext(lStart->position, MT_LOGOSTOP);  // get next logo stop mark
        if (lStop) {  // there is a next stop mark in the start range
            int distanceStartStop = (lStop->position - lStart->position) / decoder->GetVideoFrameRate();
            if (distanceStartStop < 6) {  // change from 8 to 6 because of very short stop/start from logo change 6s after start mark
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): next logo stop mark found very short after start mark on position (%d), distance %ds", lStop->position, distanceStartStop);
                cMark *lNextStart = marks.GetNext(lStop->position, MT_LOGOSTART); // get next logo start mark
                if (lNextStart) {  // now we have logo start/stop/start, this can be a preview before broadcast start
                    int distanceStopNextStart = (lNextStart->position - lStop->position) / decoder->GetVideoFrameRate();
                    if (distanceStopNextStart <= 136) { // found start mark short after start/stop, use this as start mark, changed from 68 to 76 to 136
                        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): found start mark (%d) %ds after logo start/stop marks, use this start mark", lNextStart->position, distanceStopNextStart);
                        begin = lNextStart;
                        break;
                    }
                    else {
                        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): found start mark (%d) %ds after logo start/stop marks, length too big", lNextStart->position, distanceStopNextStart);
                        break;
                    }
                }
                else break;
            }
            else {  // there is a next stop mark but too far away
                dsyslog("cMarkAdStandalone::Check_LOGOSTART(): next logo stop mark (%d) but too far away %ds", lStop->position, distanceStartStop);
                break;
            }
        }
        else break; // there is no next stop mark
        lStart = marks.GetNext(lStart->position, MT_LOGOSTART);  // try next start mark
    }

    if (!begin) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): no logo start mark found");
        return nullptr;
    }

// valid logo start mark found
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): found logo start mark (%d)", begin->position);
    marks.DelWeakFromTo(0, INT_MAX, MT_LOGOCHANGE);   // maybe the is a assumed start from converted channel stop
// invalid hborder/vborder stop marks can left over from border stop as fallback start mark
    dsyslog("cMarkAdStandalone::Check_LOGOSTART(): delete invalid hborder and vborder marks from previous broadcast or detection error");
    if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_DISABLED) marks.DelType(MT_HBORDERCHANGE, 0xF0);
    if (criteria->GetMarkTypeState(MT_VBORDERCHANGE) == CRITERIA_DISABLED) marks.DelType(MT_VBORDERCHANGE, 0xF0);

    if ((criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED) ||
            (criteria->GetMarkTypeState(MT_VBORDERCHANGE) == CRITERIA_USED) ||
            (criteria->GetMarkTypeState(MT_ASPECTCHANGE)  == CRITERIA_USED) ||
            (criteria->GetMarkTypeState(MT_CHANNELCHANGE) == CRITERIA_USED)) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): stronger marks are set for detection, use logo mark only for start mark, delete logo marks after (%d)", begin->position);
        marks.DelFromTo(begin->position + 1, INT_MAX, MT_LOGOCHANGE, 0xF0);
    }
    else {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): logo marks set for detection, cleanup late hborder and vborder stop marks from previous broadcast");
        const cMark *delMark = marks.GetAround(10 * decoder->GetVideoFrameRate(), begin->position, MT_VBORDERSTOP, 0xFF);
        if (delMark) marks.Del(delMark->position);
        delMark = marks.GetAround(10 * decoder->GetVideoFrameRate(), begin->position, MT_HBORDERSTOP, 0xFF);
        if (delMark) marks.Del(delMark->position);
        criteria->SetMarkTypeState(MT_LOGOCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
    }
    if (!criteria->LogoInBorder()) {
        dsyslog("cMarkAdStandalone::Check_LOGOSTART(): disable border detection and delete border marks");  // avoid false detection of border
        marks.DelType(MT_HBORDERCHANGE, 0xF0);  // there could be hborder from an advertising in the recording
        marks.DelType(MT_VBORDERCHANGE, 0xF0);  // there could be hborder from an advertising in the recording
        criteria->SetDetectionState(MT_HBORDERCHANGE, false);
        criteria->SetDetectionState(MT_VBORDERCHANGE, false);
    }
    else {  // delete delayed vborder stop from previous broadcast and black opening credits in this boaddcast
        cMark *vborderStop = marks.GetAround(30 * decoder->GetVideoFrameRate(), begin->position, MT_VBORDERSTOP);  // trust late hborder start mark
        if (vborderStop) {
            dsyslog("cMarkAdStandalone::Check_LOGOSTART(): delete vborder stop (%d) from previous broadcast", vborderStop->position);  // avoid false detection of border
            marks.Del(vborderStop->position);
        }
    }
    return begin;
}


cMark *cMarkAdStandalone::Check_HBORDERSTART() {
    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): search for hborder start mark");
    cMark *hStart = marks.GetAround(1.4 * MAX_ASSUMED * decoder->GetVideoFrameRate(), startA, MT_HBORDERSTART);  // trust late hborder start mark, changed from 1.3 to 1.4
    if (hStart) { // we found a hborder start mark
        dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): horizontal border start found at (%d)", hStart->position);
        if (hStart->position <= IGNORE_AT_START) {  // hborder start at recording start is from previous recording
            dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): ignore too early hborder start (%d), try next", hStart->position);
            hStart = marks.GetNext(hStart->position, MT_HBORDERSTART);
            if (hStart) dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): next horizontal border start found at (%d)", hStart->position);
        }
    }
    if (hStart) { // we found a hborder start mark
        cMark *hStop = marks.GetNext(hStart->position, MT_HBORDERSTOP);
        if (hStop) { // we have a hborder stop mark in start area, check if hborder marks are valid
            // check if hbrder start/stop is end part of previous broadcast
            if (hStop->position < startA) {
                dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): horizontal border start (%d) and stop (%d) mark before assumed start (%d), hborder marks are from previous broadcast", hStart->position, hStop->position, startA);
                marks.Del(hStart->position); // keep hborder stop as end of previous broadcast, may we can use it as fallback
                return nullptr;
            }

            int lengthBroadcast   = (hStop->position - hStart->position) / decoder->GetVideoFrameRate();
            int hBorderStopStartA = (hStop->position - startA)           / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): horizontal border start (%d) and stop (%d) mark, length of first broadcast %ds, ends %ds after assumed start (%d)", hStart->position, hStop->position, lengthBroadcast, hBorderStopStartA, startA);
            // very short broadcast without next hborder start is invalid
            const cMark *hStartNext = marks.GetNext(hStop->position, MT_HBORDERSTART);
            if (!hStartNext &&
                    ((lengthBroadcast <= 142) ||  // very short broadcast can be from preview or hborder part in documentation, changed from 94 to 142
                     ((lengthBroadcast <= 350) && (hBorderStopStartA <= 194)))) {  // very early hborder part can be last part of previous broadcast
                dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): first broadcast too short, no next hborder start, delete hborder marks");
                marks.Del(hStart->position);
                marks.Del(hStop->position);
                return nullptr;
            }
            // check if hborder marks are from long black opening credits
            // false hborder from opening credits or dokus always have logo in border
            cMark *logoStartBefore = marks.GetAround(20 * decoder->GetVideoFrameRate(), hStart->position, MT_LOGOSTART);  // logo start can be short after hborder start (fade in logo)
            if (!logoStartBefore) logoStartBefore = marks.GetPrev(hStart->position, MT_LOGOSTART); // false hborder start/stop in dokus can be far after logo start
            if (logoStartBefore) {
                cMark *logoStopAfter  = marks.GetNext(hStop->position, MT_LOGOSTOP);
                if (logoStopAfter) {
                    int diffLogoStarthBorderStart  = (hStart->position        - logoStartBefore->position) / decoder->GetVideoFrameRate();
                    int diffBorderStarthBorderStop = (hStop->position         - hStart->position)          / decoder->GetVideoFrameRate();
                    int diffhBorderStopLogoStop    = (logoStopAfter->position - hStop->position)           / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): check for false detected hborder from opening credits or documentation");
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): MT_LOGOSTART (%6d) -> %3ds -> MT_HBORDERSTART (%6d) -> %3ds -> MT_HBORDERSTOP (%6d) -> %4ds -> MT_LOGOSTOP (%6d) -> %s", logoStartBefore->position, diffLogoStarthBorderStart, hStart->position, diffBorderStarthBorderStop, hStop->position, diffhBorderStopLogoStop, logoStopAfter->position, macontext.Info.ChannelName);
                    // exampe false detected hborder from opening credits
                    // MT_LOGOSTART ( 15994) ->   3s -> MT_HBORDERSTART ( 16074) ->  82s -> MT_HBORDERSTOP ( 18131) -> 1460s -> MT_LOGOSTOP ( 52588)
                    // MT_LOGOSTART ( 39468) -> -19s -> MT_HBORDERSTART ( 38502) -> 119s -> MT_HBORDERSTOP ( 44474) -> 1593s -> MT_LOGOSTOP (118182)
                    // MT_LOGOSTART (  7421) ->   0s -> MT_HBORDERSTART (  7422) -> 223s -> MT_HBORDERSTOP ( 13016) ->  960s -> MT_LOGOSTOP ( 31422)
                    // MT_LOGOSTART ( 39468) -> -19s -> MT_HBORDERSTART ( 38502) -> 118s -> MT_HBORDERSTOP ( 44428) -> 1474s -> MT_LOGOSTOP (118130) -> TMC
                    if ((diffLogoStarthBorderStart >= -19) && (diffLogoStarthBorderStart <= 3) &&
                            (diffBorderStarthBorderStop <= 223) && (diffhBorderStopLogoStop >= 960)) {
                        dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): false detected hborder from opening credits found, delete hborder marks");
                        // there can also be a false vborder start from opening credits around logo start
                        const cMark *vStart = marks.GetAround(decoder->GetVideoFrameRate(), logoStartBefore->position, MT_VBORDERSTART);
                        if (vStart) marks.Del(vStart->position);
                        marks.Del(hStart->position);
                        marks.Del(hStop->position);
                        return nullptr;
                    }
                    // some dokus have one hborder parts at the beginning of the broadcast
                    // example of one hborder part in broadcast
                    // MT_LOGOSTART ( 21731) -> 189s -> MT_HBORDERSTART ( 31188) -> 141s -> MT_HBORDERSTOP ( 38279) -> 2210s -> MT_LOGOSTOP (148817)
                    // MT_LOGOSTART ( 23761) -> 159s -> MT_HBORDERSTART ( 31756) -> 178s -> MT_HBORDERSTOP ( 40669) -> 2242s -> MT_LOGOSTOP (152787) -> zdf_neo_HD
                    if ((diffLogoStarthBorderStart <= 189) && (diffBorderStarthBorderStop <= 178) && (diffhBorderStopLogoStop >= 2210)) {
                        dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): invalid hborder marks from hborder scene in broadcast, delete hborder marks");
                        marks.Del(hStart->position);
                        marks.Del(hStop->position);
                        return nullptr;
                    }
                }
            }
            // some dokus have more than one hborder parts
            // check hborder sequence MT_HBORDERSTART -> MT_HBORDERSTOP -> MT_HBORDERSTART
            cMark *hNextStart = marks.GetNext(hStop->position, MT_HBORDERSTART);
            if (hNextStart) {
                int lengthAd = (hNextStart->position - hStop->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): MT_HBORDERSTART (%d) -> %ds -> MT_HBORDERSTOP (%d) -> %ds -> MT_HBORDERSTART (%d) -> %s", hStart->position, lengthBroadcast, hStop->position, lengthAd, hNextStart->position, macontext.Info.ChannelName);
                // example of more than one hborder part in broadcast
                // MT_HBORDERSTART (36238) -> 87s -> MT_HBORDERSTOP (40601) -> 1602s -> MT_HBORDERSTART (120704)
                if ((lengthBroadcast <= 87) && (lengthAd >= 1602)) {
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): hborder sequence invalid, assume hborder are in broadcast");
                    marks.DelType(MT_HBORDERCHANGE, 0xF0);
                    return nullptr;
                }
            }

        }

        // found valid horizontal border start mark
        criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
        // we found a hborder, check logo stop/start after to prevent to get closing credit from previous recording as start
        // only works for channel with logo in border
        if (criteria->LogoInBorder()) {
            cMark *logoStop  = marks.GetNext(hStart->position, MT_LOGOSTOP);        // logo stop mark can be after hborder start
            if (!logoStop) logoStop = marks.GetPrev(hStart->position, MT_LOGOSTOP); //                   or before hborder start
            cMark *logoStart = marks.GetNext(hStart->position, MT_LOGOSTART);
            if (logoStop && logoStart && (logoStart->position > logoStop->position)) {
                int diffStop  = (logoStop->position  - hStart->position) / decoder->GetVideoFrameRate();
                int diffStart = (logoStart->position - hStart->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): found logo stop (%d) %ds and logo start (%d) %ds after hborder start (%d)", logoStop->position, diffStop, logoStart->position, diffStart, hStart->position);
                // example of hborder start in dark closing credits from previous recording
                // found logo stop (7569) 17s and logo start (7649) 21s after hborder start (7122)
                if ((diffStop >= -1) && (diffStop <= 17) && (diffStart <= 21)) {
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): hborder start mark position (%d) includes previous closing credits, use logo start (%d) instead", hStart->position, logoStart->position);
                    marks.Del(hStart->position);
                    hStart = logoStart;
                }
            }
        }
        // cleanup all logo marks if we have a hborder start mark
        if (hStart->type != MT_LOGOSTART) {
            dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): delete logo marks if any");
            marks.DelType(MT_LOGOCHANGE, 0xF0);
        }
        // cleanup vborder marks
        dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): delete vborder marks if any");
        marks.DelType(MT_VBORDERCHANGE, 0xF0);
    }
    else { // we found no valid hborder start mark
        // check if we have a hborder double episode from recording start
        const cMark *firstBorderStart = marks.GetNext(-1, MT_HBORDERSTART);
        cMark *lastBorderStop = marks.GetPrev(INT_MAX, MT_HBORDERSTOP);
        int diffBorderStopStartA = 0;
        if (lastBorderStop)  diffBorderStopStartA = (lastBorderStop->position - startA) /  decoder->GetVideoFrameRate();
        if (firstBorderStart && (firstBorderStart->position <= IGNORE_AT_START) &&
                ((marks.Count(MT_HBORDERSTART) > marks.Count(MT_HBORDERSTOP) || // we end start part with hborder start
                  (diffBorderStopStartA >= MAX_ASSUMED)))) {                    // we have a hborder stop, but not in start part
            dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): horizontal border start mark at recording start found, we have a double episode");
            criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
        }
        else { // broadcast has no valid hborder
            dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): no horizontal border start mark found, disable horizontal border detection and cleanup marks");
            criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_DISABLED, macontext.Config->fullDecode);
            // keep last hborder stop, maybe can use it as start mark
            if (lastBorderStop) { // delete all marks before hborder stop, they can not be a valid start mark
                // delete all invalid hborder marks before last MT_HBORDERSTOP, they are invalid (detection error or from previous broadcast)
                marks.DelFromTo(0, lastBorderStop->position - 1, MT_HBORDERCHANGE, 0xF0);
                cMark *logoStart = marks.GetPrev(lastBorderStop->position);
                // if there is a logo start short before hborder stop, we have a delayed hborder stop from dark opening credits, keep logo start mark
                if (logoStart) {
                    int diff = (lastBorderStop->position - logoStart->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::Check_HBORDERSTART(): logo start (%d) %ds before hborder stop (%d) found", logoStart->position, diff, lastBorderStop->position);
                    // valid logo start mark can be some seconds before hborder stop if there are dark opening credits
                    if (diff <= 18) marks.Del(lastBorderStop->position);  // we do not need hborder stop as fallback, we have a near logo start mark
                }
            }
            else marks.DelType(MT_HBORDERCHANGE, 0xF0);  // maybe the is a late invalid hborder start mark, exists sometimes together with old vborder recordings
        }
        return nullptr;
    }
    return hStart;
}

cMark *cMarkAdStandalone::Check_VBORDERSTART(const int maxStart) {
    dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): search for vborder start mark");
    // check if we have short vbroder start/stop marks from an unreliable small vborder
    dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): check if vborder marks are valid");
    cMark *vStart = marks.GetNext(-1, MT_VBORDERSTART);
    while (vStart) {
        cMark *vStop = marks.GetNext(vStart->position, MT_VBORDERSTOP);
        if (vStop) {
            int diff = (vStop->position - vStart->position) /  decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): MT_VBORDERSTART (%5d) -> %3ds -> MT_VBORDERSTOP (%5d)", vStart->position, diff, vStop->position);
            if (diff < 82) { // changed from 90 to 82, short first valid part found
                dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): too short vborder start/stop, delete marks");
                cMark *tmpMark = marks.GetNext(vStart->position, MT_VBORDERSTART);
                marks.Del(vStart->position);
                marks.Del(vStop->position);
                vStart = tmpMark;
                continue;
            }
        }
        vStart = marks.GetNext(vStart->position, MT_VBORDERSTART);
    }

    // search vborder start mark
    vStart = marks.GetAround(240 * decoder->GetVideoFrameRate() + startA, startA, MT_VBORDERSTART);
    if (!vStart) {
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): no vertical border at start found, ignore vertical border detection");
        criteria->SetDetectionState(MT_VBORDERCHANGE, false);
        marks.DelType(MT_VBORDERSTART, 0xFF);  // maybe we have a vborder start from a preview or in a doku, delete it
        const cMark *vStop = marks.GetAround(240 * decoder->GetVideoFrameRate() + startA, startA, MT_VBORDERSTOP);
        if (vStop) {
            int pos         = vStop->position;
            int64_t pts     = vStop->pts;
            char *comment   = nullptr;
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vertical border stop without start mark found (%i), possible start mark of the following recording", pos);
            marks.Del(pos);
            if (asprintf(&comment,"assumed start from vertical border stop (%d)", pos) == -1) comment = nullptr;
            if (comment) {
                ALLOC(strlen(comment)+1, "comment");
            }
            marks.Add(MT_ASSUMEDSTART, MT_UNDEFINED, MT_UNDEFINED, pos, pts, comment);
            if (comment) {
                FREE(strlen(comment)+1, "comment");
                free(comment);
            }
        }
        return nullptr;
    }

    // found vborder start, check if it is valid
    dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vertical border start found at (%d)", vStart->position);
    cMark *vStopAfter = marks.GetNext(vStart->position, MT_VBORDERSTOP);  // if there is a MT_VBORDERSTOP short after the MT_VBORDERSTART, MT_VBORDERSTART is invalid
    if (vStopAfter) {
        int vBorderStartvBorderStop = (vStopAfter->position - vStart->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vertical border stop found at (%d), %ds after vertical border start", vStopAfter->position, vBorderStartvBorderStop);
        // prevent to get start of last part of previous broadcast as start mark
        const cMark *vNextStart = marks.GetNext(vStopAfter->position, MT_VBORDERSTART);
        if (!vNextStart) {
            // we have only start/stop vborder sequence in start part, this can be from broadcast before or false vborder detection from dark scene in vborder
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): no next vertical border start found after start (%d) and stop (%d)", vStart->position, vStopAfter->position);
            // check if it is false vborder detection from dark scene in vborder
            int startAvBorderStart          = (vStart->position           - startA)               / decoder->GetVideoFrameRate();
            int vBorderStoppacketCheckStart = (decoder->GetPacketNumber() - vStopAfter->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): startA (%5d) -> %4ds -> MT_VBORDERSTART (%5d) -> %3ds -> MT_VBORDERSTOP (%5d) -> %4ds -> packetCheckStart (%5d)", startA, startAvBorderStart,  vStart->position, vBorderStartvBorderStop, vStopAfter->position, vBorderStoppacketCheckStart, packetCheckStart);
            // example of valid vborder marks
            // startA ( 7475) ->  -31s -> MT_VBORDERSTART ( 6685) -> 149s -> MT_VBORDERSTOP (10432) -> 331s -> packetCheckStart (18725)
            //
            // example of invalid vborder from dark scene or from broadcast before
            // startA ( 4075) ->    9s -> MT_VBORDERSTART ( 4310) -> 115s -> MT_VBORDERSTOP ( 7188) ->  355s -> packetCheckStart (16075)
            // startA (16350) -> -288s -> MT_VBORDERSTART ( 1933) -> 281s -> MT_VBORDERSTOP (16019) ->  456s -> packetCheckStart (38850) -> vborder from previous recording
            // startA ( 7450) -> -298s -> MT_VBORDERSTART (    0) -> 329s -> MT_VBORDERSTOP ( 8238) ->  448s -> PacketCheckStart (19450)
            // startA (18500) -> -172s -> MT_VBORDERSTART ( 9897) -> 122s -> MT_VBORDERSTOP (16016) -> 3015s -> packetCheckStart (42500)
            if ((startAvBorderStart <= 9) && (vBorderStartvBorderStop <= 329) && (vBorderStoppacketCheckStart > 331)) {
                dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vertical border start (%d) and stop (%d) from closing credits or dark scene, delete marks", vStart->position, vStopAfter->position);
                marks.Del(vStart->position);
                marks.DelFromTo(0, vStopAfter->position, MT_VBORDERSTOP, 0xFF);  // prevent to use invalid vborder stop marks in start part as type change marks
                criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_UNAVAILABLE, macontext.Config->fullDecode);
                return nullptr;
            }
        }
    }

    // prevent to get start of next broadcast as start of this very short broadcast
    if (vStart->position > maxStart) {
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vborder start mark (%d) after max start mark (%d) is invalid", vStart->position, maxStart);
        return nullptr;
    }

    if (vStart->position < IGNORE_AT_START) { // early position is a vborder from previous recording
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): delete too early vertical border start found at (%d)", vStart->position);
        const cMark *vBorderStop = marks.GetNext(vStart->position, MT_VBORDERSTOP);
        marks.Del(vStart->position);
        if (!vBorderStop || (vBorderStop->position > startA + 420 * decoder->GetVideoFrameRate())) {
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vertical border since start, use it for mark detection");
            criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
            if (!criteria->LogoInBorder()) {
                dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): logo marks can not be valid, delete it");
                marks.DelType(MT_LOGOCHANGE, 0xF0);
            }
        }
        return nullptr;
    }

    // check if we have a logo start direct before vborder start, prevent a false vborder start/stop from dark scene as start mark
    if (!criteria->LogoInBorder()) {  // not possible for logo in border channel because vborder start and logo start can be on same position or logo start after vborder start
        cMark *logoStart  = marks.GetPrev(vStart->position, MT_ALL);
        if (logoStart && (logoStart->type == MT_LOGOSTART) && (logoStart->position >= IGNORE_AT_START)) {
            int diff = (vStart->position - logoStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): MT_LOGOSTART (%4d) -> %3ds -> MT_VBORDERSTART (%4d)", logoStart->position, diff, vStart->position);
            // valid vborder example:
            // MT_LOGOSTART (7599) -> 371s -> MT_VBORDERSTART (26172) -> rbb HB, no logo stop before vborder broadcast, maybe logo in border
            // MT_LOGOSTART (  13) -> 352s -> MT_VBORDERSTART (17660) -> ARD alpha HD, start of recoring, logo start from previous broadcast, no logo stop
            // MT_LOGOSTART ( 186) -> 323s -> MT_VBORDERSTART (16358) -> ARD alpha HD, start of recoring, logo start from previous broadcast, no logo stop
            // MT_LOGOSTART (  13) -> 316s -> MT_VBORDERSTART (15846) -> ARD alpha HD, start of recoring, logo start from previous broadcast, no logo stop
            if ((diff > 50) && (diff < 316)) {
                dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): logo start mark before vborder start found, delete invalid vborder marks from dark scene");
                marks.DelType(MT_VBORDERCHANGE, 0xF0);
                return nullptr;
            }

        }
    }

    // found valid vertical border start mark
    if (criteria->GetMarkTypeState(MT_ASPECTCHANGE) == CRITERIA_USED) {
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): use vertical border only as start mark, keep using aspect ratio detection");
        criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);
    }
    else criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_USED, macontext.Config->fullDecode);

    // check logo start after vborder start to prevent to get closing credit from previous recording as start mark
    if (criteria->LogoInBorder()) {  // prevent to get logo interruption as false start mark
        cMark *logoStart = marks.GetNext(vStart->position, MT_LOGOSTART);
        if (logoStart) {
            int diffStart = (logoStart->position - vStart->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): found logo start (%d) %ds after vborder start (%d)", logoStart->position, diffStart, vStart->position);
            // near logo start is fade in logo, undetected info logo start mark 12s after valid vborder start
            // changed from 10 to 5
            if ((diffStart >= 5) && (diffStart < 12)) {
                dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vborder start mark position (%d) includes previous closing credits, use logo start (%d) instead", vStart->position, logoStart->position);
                marks.Del(vStart->position);
                return logoStart;
            }
        }
    }

    // check logo stop after vborder start to prevent to get closing credit from previous recording as start mark
    cMark *logoStop  = marks.GetNext(vStart->position, MT_LOGOSTOP);
    if (logoStop) {
        int diffStop  = (logoStop->position  - vStart->position) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): found logo stop (%d) %ds after vborder start (%d)", logoStop->position, diffStop, vStart->position);
        if (diffStop < 11) {  // undetected info logo stop mark 11s after valid vborder start
            dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): vborder start mark position (%d) includes previous closing credits, use logo stop (%d) instead", vStart->position, logoStop->position);
            marks.Del(vStart->position);
            logoStop = marks.ChangeType(logoStop, MT_START);
            return logoStop;
        }
    }
    dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): delete logo marks if any");
    marks.DelType(MT_LOGOCHANGE, 0xF0); // delete logo marks, vborder is stronger

    dsyslog("cMarkAdStandalone::Check_VBORDERSTART(): delete HBORDER marks if any");
    marks.DelType(MT_HBORDERCHANGE, 0xF0); // delete wrong hborder marks
    return vStart;
}


void cMarkAdStandalone::CheckStart() {
    LogSeparator(true);
    dsyslog("cMarkAdStandalone::CheckStart(): checking start at frame (%d) check start planed at (%d)", decoder->GetPacketNumber(), packetCheckStart);
    int maxStart = startA + (length * decoder->GetVideoFrameRate() / 2);  // half of recording
    char *indexToHMSFStart = marks.IndexToHMSF(startA, AV_NOPTS_VALUE, false);
    if (indexToHMSFStart) {
        ALLOC(strlen(indexToHMSFStart) + 1, "indexToHMSFStart");
        dsyslog("cMarkAdStandalone::CheckStart(): assumed start frame %d at %s, max allowed start frame (%d)", startA, indexToHMSFStart, maxStart);
        FREE(strlen(indexToHMSFStart) + 1, "indexToHMSFStart");
        free(indexToHMSFStart);
    }
    DebugMarks();     //  only for debugging
    const sAspectRatio *aspectRatioFrame = decoder->GetFrameAspectRatio();  // aspect ratio of last read packet in start part

    // set initial mark criteria
    // if we have no hborder start, broadcast can not have hborder
    if (marks.Count(MT_HBORDERSTART) == 0) criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_UNAVAILABLE, macontext.Config->fullDecode);
    else if ((marks.Count(MT_HBORDERSTART) == 1) && (marks.Count(MT_HBORDERSTOP) == 0)) criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);

    // if we have no vborder start, broadcast can not have vborder
    if (marks.Count(MT_VBORDERSTART) == 0) criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_UNAVAILABLE, macontext.Config->fullDecode);
    else if ((marks.Count(MT_VBORDERSTART) == 1) && (marks.Count(MT_VBORDERSTOP) == 0)) criteria->SetMarkTypeState(MT_VBORDERCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);

    // if we have no aspect change in 16:9 video, we have no aspect ratio marks
    if ((marks.Count(MT_ASPECTCHANGE, 0xF0) == 0) && aspectRatioFrame && (aspectRatioFrame->num == 16) && (aspectRatioFrame->den == 9))criteria->SetMarkTypeState(MT_ASPECTCHANGE, CRITERIA_UNAVAILABLE, macontext.Config->fullDecode);

    // if we have no channel change and 2 channel audio, we have no channel marks (GetAC3ChannelCount will return 0 if there is no AC3 stream)
    if ((marks.Count(MT_CHANNELCHANGE, 0xF0) == 0) && (decoder->GetAC3ChannelCount() <= 2)) criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_UNAVAILABLE, macontext.Config->fullDecode);

// check recording start mark
    cMark *begin = marks.GetAround(startA, 1, MT_RECORDINGSTART);  // do we have an incomplete recording ?
    if (begin) {
        dsyslog("cMarkAdStandalone::CheckStart(): found MT_RECORDINGSTART (%i), use this as start mark for the incomplete recording", begin->position);
        // delete short stop marks without start mark
        cMark *stopMark = marks.GetNext(0, MT_CHANNELSTOP);
        if (stopMark) {
            int diff = stopMark->position / decoder->GetVideoFrameRate();
            if ((diff < 30) && (marks.Count(MT_CHANNELSTART, 0xFF) == 0)) {
                dsyslog("cMarkAdStandalone::CheckStart(): delete stop mark (%d) without start mark", stopMark->position);
                marks.Del(stopMark->position);
            }
        }
    }

// audio channel start
    if (!begin && (criteria->GetMarkTypeState(MT_CHANNELCHANGE) > CRITERIA_UNAVAILABLE)) begin = Check_CHANNELSTART();

// check if aspect ratio from VDR info file is valid
    bool checkedAspectRatio = false;
    dsyslog("cMarkAdStandalone::CheckStart(): aspect ratio from VDR info: %d:%d", macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den);
    // end of start part can not be 4:3 if broadcast is 16:9
    if (aspectRatioFrame && (macontext.Info.AspectRatio.num == 16) && (macontext.Info.AspectRatio.den == 9) && (aspectRatioFrame->num == 4) && (aspectRatioFrame->den == 3)) {
        dsyslog("cMarkAdStandalone::CheckStart(): broadcast at end of start part is 4:3, VDR info tells 16:9, info file is wrong");
        SwapAspectRatio();
        checkedAspectRatio = true;  // now we are sure, aspect ratio is correct
    }
    // very short start/stop pairs (broadcast) are impossible, these must be stop/start (ad) pairs
    if (!checkedAspectRatio) {
        cMark *aspectStart = marks.GetNext(-1, MT_ASPECTSTART); // first start can be on position 0
        if (aspectStart) {
            cMark *aspectStop = marks.GetNext(aspectStart->position, MT_ASPECTSTOP);
            if (aspectStop) {
                int diff    = (aspectStop->position - aspectStart->position) / decoder->GetVideoFrameRate();
                int startAS = startA / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::CheckStart(): aspect ratio start (%d) stop (%d): length %ds", aspectStart->position, aspectStop->position, diff);
                if (diff <= 60) {
                    dsyslog("cMarkAdStandalone::CheckStart(): length %ds for first broadcast too short, pre recording time is %ds, VDR info file must be wrong", diff, startAS);
                    SwapAspectRatio();
                    checkedAspectRatio = true;  // now we are sure, aspect ratio is correct
                }
            }
        }
    }
    // if aspect ratio from VDR info file is 16:9 and we have aspect ratio marks, check if sequence is valid
    // invalid sequence (all aspect change marks):
    // MT_ASPECTSTOP (start of broadcast, near startA) -> long broadcast -> MT_ASPECTSTOP (start of ad)
    // startA (7600) -> 318s -> MT_ASPECTSTOP (8808) -> 270s -> MT_ASPECTSTART (15565)
    if (!checkedAspectRatio && (macontext.Info.AspectRatio.num == 16) && (macontext.Info.AspectRatio.den == 9)) {
        cMark *aspectStop = marks.GetNext(-1, MT_ASPECTCHANGE, 0xF0);
        if (aspectStop && aspectStop->type == MT_ASPECTSTOP) {  // with aspect mark must be MT_ASPECTSTOP
            cMark *aspectStart = marks.GetNext(aspectStop->position, MT_ASPECTSTART);
            if (aspectStart) {
                int aspectStopAfterStartA      = (aspectStart->position - startA)               / decoder->GetVideoFrameRate();
                int aspectStartAfterAspectStop = (aspectStart->position - aspectStop->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::CheckStart(): startA (%d) -> %ds -> MT_ASPECTSTOP (%d) -> %ds -> MT_ASPECTSTART (%d)", startA, aspectStopAfterStartA, aspectStop->position, aspectStartAfterAspectStop, aspectStart->position);
                if ((aspectStopAfterStartA >= 318) && (aspectStartAfterAspectStop >= 270)) {
                    dsyslog("cMarkAdStandalone::CheckStart(): sequence is invalid for 16:9 broadcast, fix to 4:3");
                    SwapAspectRatio();
                }
            }
        }
    }
    video->SetAspectRatioBroadcast(macontext.Info.AspectRatio);  // now aspect ratio is correct, tell it video based detection
    // for 4:3 broadcast cleanup all vborder marks, these are false detected from dark scene or, if realy exists, they are too small for reliable detection
    if ((macontext.Info.AspectRatio.num == 4) && (macontext.Info.AspectRatio.den == 3)) marks.DelType(MT_VBORDERCHANGE, 0xF0); // delete wrong vborder marks


// aspect ratio start
    if (!begin && (criteria->GetMarkTypeState(MT_ASPECTCHANGE) > CRITERIA_UNAVAILABLE)) {
        dsyslog("cMarkAdStandalone::CheckStart(): search for aspect ratio start mark");
        // search for aspect ratio start mark
        cMark *aStart = marks.GetAround(480 * decoder->GetVideoFrameRate(), startA, MT_ASPECTSTART);
        if (aStart) {
            dsyslog("cMarkAdStandalone::CheckStart(): found aspect ratio start mark at (%d), video info is %d:%d", aStart->position, macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den);
            if ((macontext.Info.AspectRatio.num == 4) && (macontext.Info.AspectRatio.den == 3)) { // we have a aspect ratio start mark, check if valid
                criteria->SetMarkTypeState(MT_ASPECTCHANGE, CRITERIA_USED, macontext.Config->fullDecode);  // use aspect ratio marks for detection, even if we have to use another start mark
                while (aStart && (aStart->position <= (16 * decoder->GetVideoFrameRate()))) {    // to near at start of recording is from broadcast before
                    dsyslog("cMarkAdStandalone::CheckStart(): aspect ratio start mark (%d) too early, try next", aStart->position);
                    aStart = marks.GetNext(aStart->position, MT_ASPECTSTART);
                    if (aStart && aStart->position > (startA +  (MAX_ASSUMED * decoder->GetVideoFrameRate()))) aStart = nullptr; // too late, this can be start of second part
                }
                // check if we have a 4:3 double episode
                if (aStart) {
                    int diffAssumed = (startA - aStart->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::CheckStart(): aspect ratio start mark (%d) %ds before assumed start (%d)", aStart->position, diffAssumed, startA);
                    if (diffAssumed < 263) {
                        begin = aStart;
                        dsyslog("cMarkAdStandalone::CheckStart(): valid aspect ratio start mark (%d) found", aStart->position);
                    }
                    else dsyslog("cMarkAdStandalone::CheckStart(): ignore too early aspect ratio start mark (%d)", aStart->position);
                }
                // we have valid a aspect ratio start mark from a 4:3 recording, advertisement are 16:9, delete all other marks and disable all other detection
                if (begin) {
                    marks.DelWeakFromTo(0, INT_MAX, MT_ASPECTCHANGE); // delete all weak marks
                    marks.Del(MT_CHANNELSTART);  // delete channel marks from previous recording
                    marks.Del(MT_CHANNELSTOP);
                    video->ClearBorder();
                }
            }
            else { // video is 16:9 but we have a aspect start mark, must be end previous 4:3 broadcast, broadcast start must be after that
                dsyslog("cMarkAdStandalone::CheckStart(): 16:9 video, aspect ratio start (%d) from end of previous 4:3 broadcast, delete marks before", aStart->position);
                marks.DelTill(aStart->position, true);  // keep aspect ratio start mark, maybe we use it if we have no logo start mark
            }
        }
    }

    // log video infos
    if (decoder->GetVideoType() == MARKAD_PIDTYPE_VIDEO_H264) isyslog("HD video with aspect ratio of %d:%d detected", macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den);
    else isyslog("SD video with aspect ratio of %d:%d detected", macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den);

    // before we can check border marks, we have to check with is valid

    // cleanup invalid logo marks, done before border check because we use logo marks to correct border marks, call without decoder, do not messup read position
    RemoveLogoChangeMarks(true);

// horizontal border start
    if (!begin && (criteria->GetMarkTypeState(MT_HBORDERCHANGE) > CRITERIA_UNAVAILABLE)) begin = Check_HBORDERSTART();

// vertical border start
    if (!begin && (criteria->GetMarkTypeState(MT_VBORDERCHANGE) > CRITERIA_UNAVAILABLE)) begin = Check_VBORDERSTART(maxStart);

// try logo start mark
    if (!begin) begin = Check_LOGOSTART();

    // drop too early marks of all types
    if (begin && (begin->type != MT_RECORDINGSTART) && (begin->position <= IGNORE_AT_START)) {  // first frames are from previous recording
        dsyslog("cMarkAdStandalone::CheckStart(): start mark (%d) type 0x%X dropped because it is too early", begin->position, begin->type);
        begin = nullptr;
    }

// no mark found, try anything
    if (!begin) {
        dsyslog("cMarkAdStandalone::CheckStart(): search for any start mark");
        marks.DelTill(IGNORE_AT_START);    // we do not want to have a initial mark from previous recording as a start mark
        int maxAssumed = 160; // not too big search range, changed from 240 to 160
        if (macontext.Info.startFromVPS && criteria->GoodVPS()) maxAssumed = 70;  // if we use a valid VPS event based start time do only near search
        begin = marks.GetAround(maxAssumed * decoder->GetVideoFrameRate(), startA, MT_START, 0x0F);
        if (begin) {
            dsyslog("cMarkAdStandalone::CheckStart(): found start mark (%d) type 0x%X after search for any type", begin->position, begin->type);
            if ((begin->type == MT_ASSUMEDSTART) || (begin->inBroadCast) || !criteria->GetDetectionState(MT_LOGOCHANGE)) { // test on inBroadCast because we have to take care of black screen marks in an ad, MT_ASSUMEDSTART is from converted channel stop of previous broadcast
                const char *indexToHMSF = marks.GetTime(begin);
                if (indexToHMSF) dsyslog("cMarkAdStandalone::CheckStart(): found start mark (%i) type 0x%X at %s inBroadCast %i", begin->position, begin->type, indexToHMSF, begin->inBroadCast);
            }
            else { // mark in ad
                const char *indexToHMSF = marks.GetTime(begin);
                if (indexToHMSF) dsyslog("cMarkAdStandalone::CheckStart(): start mark found but not inBroadCast (%i) type 0x%X at %s inBroadCast %i, ignoring", begin->position, begin->type, indexToHMSF, begin->inBroadCast);
                begin = nullptr;
            }
        }
    }

    if (begin && ((begin->position  / decoder->GetVideoFrameRate()) < 1) && (begin->type != MT_RECORDINGSTART)) { // do not accept marks in the first second, the are from previous recording, expect manual set MT_RECORDINGSTART for missed recording start
        dsyslog("cMarkAdStandalone::CheckStart(): start mark (%d) type 0x%X dropped because it is too early", begin->position, begin->type);
        begin = nullptr;
    }

    // still no start mark found, try channel stop marks from previous broadcast
    // for channel with good VPS events better use this, there maybe an ad after end of previous broadcast
    if (!begin && !criteria->GoodVPS()) {
        cMark *channelStop  = marks.GetNext(0, MT_CHANNELSTOP);
        if (channelStop) {
            const cMark *channelStart = marks.GetNext(channelStop->position, MT_CHANNELSTART);
            if (!channelStart) { // if there is a channel start mark after, channel stop is not an end mark of previous broadcast
                dsyslog("cMarkAdStandalone::CheckStart(): no valid start mark found, use MT_CHANNELSTOP (%d) from previous recoring as start mark", channelStop->position);
                begin = marks.ChangeType(channelStop, MT_START);
                marks.DelTill(begin->position);
            }
        }
    }

    // still no start mark found, try hborder stop marks from previous broadcast
    if (!begin) { // try hborder stop mark as start mark
        cMark *hborderStop  = marks.GetNext(0, MT_HBORDERSTOP);
        if (hborderStop) {
            cMark *vborderStop = marks.GetAround(decoder->GetVideoFrameRate(), hborderStop->position, MT_VBORDERSTOP);  // closing credit or documentation in frame can end with both stop types, use later
            if (vborderStop && (vborderStop->position > hborderStop->position)) {
                dsyslog("cMarkAdStandalone::CheckStart(): vborder stop (%d) short after hborder stop (%d), use vborder stop", vborderStop->position, hborderStop->position);
            }
            else {
                int diffStartA = (hborderStop->position - startA) /  decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::CheckStart(): MT_HBORDERSTOP (%d) found, %ds after assumed start", hborderStop->position, diffStartA);
                int maxAssumed = MAX_ASSUMED;
                if (macontext.Info.startFromVPS && criteria->GoodVPS()) maxAssumed = 22;  // if we use a valid VPS event based start time do only near search, changed from 58 to 22
                if (abs(diffStartA) <= maxAssumed) {
                    const cMark *hborderStart = marks.GetNext(hborderStop->position, MT_HBORDERSTART);
                    if (!hborderStart) { // if there is a hborder start mark after, hborder stop is not an end mark of previous broadcast
                        dsyslog("cMarkAdStandalone::CheckStart(): no valid start mark found, use MT_HBORDERSTOP (%d) from previous recoring as start mark", hborderStop->position);
                        begin = marks.ChangeType(hborderStop, MT_START);
                        marks.DelTill(begin->position);
                    }
                }
                else dsyslog("cMarkAdStandalone::CheckStart(): MT_HBORDERSTOP (%d) invalid, %ds after assumed start", hborderStop->position, diffStartA);
            }
        }
    }

    // still no start mark found, try vborder stop marks from previous broadcast
    if (!begin) { // try hborder stop mark as start mark
        cMark *vborderStop = marks.GetNext(0, MT_VBORDERSTOP);
        if (vborderStop) {
            dsyslog("cMarkAdStandalone::CheckStart(): no valid start mark found, use MT_VBORDERSTOP (%d) from previous recoring as start mark", vborderStop->position);
            begin = marks.ChangeType(vborderStop, MT_START);
            marks.DelTill(begin->position);
        }
    }

    // no start mark found at all, set start after pre timer
    if (!begin) {
        dsyslog("cMarkAdStandalone::CheckStart(): no valid start mark found, assume start time at pre recording time");
        cMark *nextMark = marks.GetNext(startA, MT_ALL);
        if (!nextMark || ((nextMark->type & 0x0F) == MT_STOP) || (nextMark->position > (startA + MAX_ASSUMED))) {
            marks.DelTill(startA);
            sMarkAdMark mark = {};
            mark.position    = index->GetKeyPacketNumberAfter(startA, &mark.framePTS);  // set to next key packet
            mark.type        = MT_ASSUMEDSTART;
            AddMark(&mark);
            begin = marks.GetFirst();
        }
        else {
            dsyslog("cMarkAdStandalone::CheckStart(): use start mark (%d) after assumed start (%d)", nextMark->position, startA);
            begin = nextMark;
        }
    }

    // now we have the final start mark, do fine tuning
    if (!begin) {  // can only be happen after abort
        esyslog("cMarkAdStandalone::CheckStart(): no start mark found");
        return;
    }
    marks.DelTill(begin->position, true);    // delete all marks till start mark
    const char *indexToHMSF = marks.GetTime(begin);
    char *typeName = marks.TypeToText(begin->type);
    if (indexToHMSF && typeName) isyslog("using %s start mark on position (%d) at %s as broadcast start", typeName, begin->position, indexToHMSF);
    if (typeName) {
        FREE(strlen(typeName)+1, "text");
        free(typeName);
    }

    // if we have border marks, delete logo marks
    if ((begin->type == MT_VBORDERSTART) || (begin->type == MT_HBORDERSTART)) {
        marks.Del(MT_LOGOSTART);
        marks.Del(MT_LOGOSTOP);
    }

// count logo STOP/START pairs
    int countStopStart = 0;
    cMark *mark = marks.GetFirst();
    while (mark) {
        if ((mark->type == MT_LOGOSTOP) && mark->Next() && (mark->Next()->type == MT_LOGOSTART)) {
            countStopStart++;
        }
        mark = mark->Next();
    }
    if (countStopStart > 6) {  // changed from 5 to 6, sometimes there are a lot of previews in start area
        video->ReducePlanes();
        isyslog("%d logo STOP/START pairs found after start mark, something is wrong with your logo", countStopStart);
        dsyslog("cMarkAdStandalone::CheckStart(): reduce logo processing to first plane and delete all marks after start mark (%d)", begin->position);
        marks.DelAfterFromToEnd(begin->position);
    }

    criteria->SetDetectionState(MT_LOWERBORDERCHANGE, false);  // we only use lower border for start and end mark optimization
    CheckStartMark();
    LogSeparator();
    CalculateCheckPositions(marks.GetFirst()->position);
    marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
    doneCheckStart = true;
    restartLogoDetectionDone = false;  // in case of we call CheckStart() after end part in very short recordings

    // debugging infos
    DebugMarks();
    LogSeparator();
    criteria->ListMarkTypeState();
    criteria->ListDetection();
    LogSeparator();
    return;
}


// check if start mark is valid
// check for short start/stop pairs at the start and select final start mark
bool cMarkAdStandalone::CheckStartMark() {
    LogSeparator();
    bool deleted = false;
    cMark *startMark = marks.GetFirst(); // this is the start mark
    while (startMark) {
        if ((startMark->type & 0x0F) != MT_START) {
            dsyslog("cMarkAdStandalone::CheckStartMark(): invalid type, no start mark (%d)", startMark->position);
            return false;
        }
        // check logo start mark
        int startTimer = macontext.Info.tStart * decoder->GetVideoFrameRate();  // startA is changed to selected start mark
        if (startMark->type == MT_LOGOSTART) {
            dsyslog("cMarkAdStandalone::CheckStartMark(): check for final logo start mark (%d)", startMark->position);
            cMark *logoStop1  = marks.GetNext(startMark->position, MT_LOGOSTOP);
            if (logoStop1) {
                cMark *logoStart2 = marks.GetNext(logoStop1->position, MT_LOGOSTART);
                if (logoStart2) {
                    cMark *stop2 = marks.GetNext(logoStart2->position, MT_STOP, 0x0F);  // can be assumed stop
                    if (stop2) {
                        int lengthBroadcast1    = (logoStop1->position  - startMark->position)  / decoder->GetVideoFrameRate();
                        int lengthAd            = (logoStart2->position - logoStop1->position)  / decoder->GetVideoFrameRate();
                        int lengthBroadcast2    = (stop2->position      - logoStart2->position) / decoder->GetVideoFrameRate();
                        int logoStartAssumed1   = (startMark->position  - startTimer)           / decoder->GetVideoFrameRate();  // delta after timer start event
                        int logoStartAssumed2   = (logoStart2->position - startTimer)           / decoder->GetVideoFrameRate();
                        dsyslog("cMarkAdStandalone::CheckStartMark(): MT_LOGOSTART (%5d) |%4ds| -> %3ds -> MT_LOGOSTOP (%5d) -> %4ds ->  MT_LOGOSTART (%5d) |%4ds| -> %4ds -> MT_LOGOSTOP (%6d) -> %s",  startMark->position, logoStartAssumed1, lengthBroadcast1,  logoStop1->position, lengthAd, logoStart2->position, logoStartAssumed2, lengthBroadcast2, stop2->position, macontext.Info.ChannelName);
// check for short broadcast before start mark (preview) and long broadcast after (first part of broadcast)
// example of invalid logo start mark
// MT_LOGOSTART ( 4675)         ->  11s -> MT_LOGOSTOP ( 4962) ->  150s ->  MT_LOGOSTART ( 8715)        -> 1312s -> MT_LOGOSTOP ( 41526) -> Nickelodeon, ad with false detected logo
// MT_LOGOSTART ( 2944)         ->  25s -> MT_LOGOSTOP ( 3584) ->   50s ->  MT_LOGOSTART ( 4848)        -> 1213s -> MT_LOGOSTOP ( 35173) -> sixx, start of preview before broadcast
// MT_LOGOSTART ( 3828)         ->  28s -> MT_LOGOSTOP ( 4549) ->   37s ->  MT_LOGOSTART ( 5487)        -> 1237s -> MT_LOGOSTOP ( 36427) -> SIXX, start of preview before broadcast
// MT_LOGOSTART ( 3785)         ->  28s -> MT_LOGOSTOP ( 4507) ->   31s ->  MT_LOGOSTART ( 5285)        -> 1202s -> MT_LOGOSTOP ( 35355) -> SIXX, start of preview before broadcast
// MT_LOGOSTART ( 2979)         ->  33s -> MT_LOGOSTOP ( 3824) ->   54s ->  MT_LOGOSTART ( 5183)        ->  955s -> MT_LOGOSTOP ( 29081) -> SIXX, start of preview before broadcast
// MT_LOGOSTART ( 2870)         ->  56s -> MT_LOGOSTOP ( 4276) ->   50s ->  MT_LOGOSTART ( 5532)        -> 1218s -> MT_LOGOSTOP ( 35991) -> SIXX, start of preview before broadcast
// MT_LOGOSTART ( 3867)         ->  34s -> MT_LOGOSTOP ( 4718) ->   29s ->  MT_LOGOSTART ( 5458)        -> 1229s -> MT_LOGOSTOP ( 36205) -> SIXX, start of preview before broadcast
// MT_LOGOSTART ( 1015)         ->  63s -> MT_LOGOSTOP ( 2611) ->   87s ->  MT_LOGOSTART ( 4804)        -> 1236s -> MT_LOGOSTOP ( 35723) -> SIXX, start of preview before broadcast
//
// example of valid logo start mark
// MT_LOGOSTART ( 8542) |  40s| -> 113s -> MT_LOGOSTOP (11377) ->  197s ->  MT_LOGOSTART (16305) | 351s| -> 1149s -> MT_LOGOSTOP ( 45049) -> Comedy_Central, vera short first broadcast
                        if ((lengthBroadcast1 < 113) && (lengthBroadcast2 >= 955)) {
                            dsyslog("cMarkAdStandalone::CheckStartMark(): too short first broadcast part, delete start (%d) and stop (%d) mark", startMark->position, logoStop1->position);
                            marks.Del(startMark->position);
                            marks.Del(logoStop1->position);
                            deleted = true;
                            startMark = marks.GetFirst();
                        }
// check for very short first ad, which is usually between broadcasts, but take care of undetected info logo
// example of valid logo start mark
// MT_LOGOSTART (13699) | 246s| -> 172s -> MT_LOGOSTOP (18002) ->    0s ->  MT_LOGOSTART (18011) | 419s| ->  397s -> MT_LOGOSTOP ( 27952) -> Comedy Central: info logo
// MT_LOGOSTART (11703) | 167s| -> 175s -> MT_LOGOSTOP (16094) ->    0s ->  MT_LOGOSTART (16105) | 343s| ->  425s -> MT_LOGOSTOP ( 26745) -> Comedy Central: info logo
// MT_LOGOSTART (11919) | 176s| -> 191s -> MT_LOGOSTOP (16717) ->    0s ->  MT_LOGOSTART (16728) | 369s| ->  574s -> MT_LOGOSTOP ( 31092) -> Comedy Central: info logo
// MT_LOGOSTART ( 4725) |   9s| -> 216s -> MT_LOGOSTOP (10145) ->    6s ->  MT_LOGOSTART (10303) | 232s| ->  501s -> MT_LOGOSTOP ( 22838) -> Comedy Central: info logo
// MT_LOGOSTART ( 7733) |   8s| -> 130s -> MT_LOGOSTOP (10984) ->    0s ->  MT_LOGOSTART (10994) | 138s| ->  501s -> MT_LOGOSTOP ( 23533) -> Comedy Central: info logo
// MT_LOGOSTART ( 7486) |   0s| ->  94s -> MT_LOGOSTOP ( 9858) ->    0s ->  MT_LOGOSTART ( 9868) |  94s| ->  472s -> MT_LOGOSTOP ( 21678) -> Comedy Central: info logo
// MT_LOGOSTART ( 5831) | -65s| -> 179s -> MT_LOGOSTOP (10322) ->    9s ->  MT_LOGOSTART (10559) | 123s| -> 1249s -> MT_LOGOSTOP ( 41785) -> Disney_Channel, preview in frame
//
// example of invalid logo start mark, delete first start/stop pair
// MT_LOGOSTART ( 4194) |-216s| -> 600s -> MT_LOGOSTOP (34217) ->   64s ->  MT_LOGOSTART (37442) | 448s| -> 2034s -> MT_LOGOSTOP (139176) -> Das_Erste_HD
// MT_LOGOSTART ( 2945) |-183s| -> 374s -> MT_LOGOSTOP (12312) ->    6s ->  MT_LOGOSTART (12462) | 197s| ->  470s -> MT_LOGOSTOP ( 24223) -> Comedy_Central
// MT_LOGOSTART ( 6975) | -21s| -> 389s -> MT_LOGOSTOP (16707) ->    3s ->  MT_LOGOSTART (16782) | 371s| ->  703s -> MT_LOGOSTOP ( 34377) -> Comedy_Central
// MT_LOGOSTART (  596) |-279s| -> 614s -> MT_LOGOSTOP (15954) ->   25s ->  MT_LOGOSTART (16584) | 360s| -> 2360s -> MT_LOGOSTOP ( 75585) -> RTLZWEI
                        else if ((logoStartAssumed1 <= 0) && (logoStartAssumed2 <= 448) && // should be near event start
                                 (lengthBroadcast1 >= 181) && (lengthBroadcast1 <= 614) &&
                                 (lengthAd >= 3) && (lengthAd <= 64) &&                    // very short ad can be undeteced info logo
                                 (lengthBroadcast2 >= 143)) {
                            dsyslog("cMarkAdStandalone::CheckStartMark(): too short first ad, delete start (%d) and stop (%d) mark", startMark->position, logoStop1->position);
                            marks.Del(startMark->position);
                            marks.Del(logoStop1->position);
                            deleted = true;
                            startMark = marks.GetFirst();
                        }
                        else break;
                    }
                    else break;
                }
                else break;
            }
            else break;
        }
        else {
            // check non logo start mark
            dsyslog("cMarkAdStandalone::CheckStartMark(): check for final start mark (%d)", startMark->position);
            cMark *markStop = marks.GetNext(startMark->position, MT_STOP, 0x0F);
            if (markStop) {
                int minFirstBroadcast = 60;                                        // more trust strong marks
                if (startMark->type == MT_CHANNELSTART)  minFirstBroadcast =  29;  // found very short first broadcast with channel marks (ProSieben)
                else if (startMark->type < MT_LOGOSTART) minFirstBroadcast = 106;  // changed from 77 to 80 to 106
                int lengthFirstBroadcast = (markStop->position - startMark->position) / decoder->GetVideoFrameRate(); // length of the first broadcast part
                dsyslog("cMarkAdStandalone::CheckStartMark(): first broadcast length %ds from (%d) to (%d) (expect <=%ds)", lengthFirstBroadcast, startMark->position, markStop->position, minFirstBroadcast);
                cMark *markStart = marks.GetNext(markStop->position, MT_START, 0x0F);
                if (markStart) {
                    int lengthFirstAd = 1000 * (markStart->position - markStop->position) / decoder->GetVideoFrameRate(); // length of the first broadcast part
                    dsyslog("cMarkAdStandalone::CheckStartMark(): first advertising length %dms from (%d) to (%d)", lengthFirstAd, markStop->position, markStart->position);
                    if (lengthFirstAd <= 1000) {
                        dsyslog("cMarkAdStandalone::CheckStartMark(): very short first advertising, this can be a logo detection failure");
                        break;
                    }
                    else {
                        if (lengthFirstBroadcast < minFirstBroadcast) {
                            dsyslog("cMarkAdStandalone::CheckStartMark(): too short STOP/START/STOP sequence at start, delete first pair");
                            marks.Del(startMark->position);
                            marks.Del(markStop->position);
                            deleted = true;
                            startMark = marks.GetFirst();
                        }
                        else break;
                    }
                }
                else break;
            }
            else break;
        }
    }
    return deleted;
}


void cMarkAdStandalone::DebugMarks() {           // write all marks to log file
    marks.Debug();
#ifdef DEBUG_WEAK_MARKS
    // weak marks
    dsyslog("cMarkAdStandalone::DebugMarks(): current black marks:");
    blackMarks.Debug();
    dsyslog("cMarkAdStandalone::DebugMarks(): current silence marks:");
    silenceMarks.Debug();
    dsyslog("cMarkAdStandalone::DebugMarks(): current scene change marks:");
    sceneMarks.Debug();
#endif
}


void cMarkAdStandalone::CheckMarks() {           // cleanup marks that make no sense
    LogSeparator(true);

    const cMark *firstMark = marks.GetFirst();
    if (!firstMark) {
        esyslog("no marks at all detected, something went very wrong");
        return;
    }
    int newStopA = firstMark->position + decoder->GetVideoFrameRate() * length;  // we have to recalculate stopA with final start mark
    // remove invalid marks
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): remove invalid marks");
    DebugMarks();     //  only for debugging
    marks.DelInvalidSequence();
    cMark *mark = marks.GetFirst();
    while (mark) {
        // if stop/start distance is too big, remove pair
        if (((mark->type & 0x0F) == MT_STOP) && (mark->Next()) && ((mark->Next()->type & 0x0F) == MT_START)) {
            int diff = (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::CheckMarks(): check advertising from stop (%6d) to start (%6d), length %4ds", mark->position, mark->Next()->position, diff);
            if (diff > 3600) {  // assumed possible longest advertising
                dsyslog("cMarkAdStandalone::CheckMarks(): delete invalid pair");
                cMark *tmp = mark->Next()->Next();
                marks.Del(mark->Next());
                marks.Del(mark);
                mark = tmp;
                continue;
            }
        }

        // if no stop mark at the end, add one
        if (((mark->type & 0x0F) == MT_START) && (!mark->Next())) {      // delete start mark at the end
            if (marks.GetFirst()->position != mark->position) {        // do not delete start mark
                esyslog("cMarkAdStandalone::CheckMarks(): START mark at the end, deleting %d", mark->position);
                marks.Del(mark);
                break;
            }
        }
        mark = mark->Next();
    }

// delete logo and border marks if we have channel marks
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): delete logo marks if we have channel or border marks");
    DebugMarks();     //  only for debugging
    const cMark *channelStart = marks.GetNext(0, MT_CHANNELSTART);
    const cMark *channelStop  = marks.GetNext(0, MT_CHANNELSTOP);
    cMark *hborderStart       = marks.GetNext(0, MT_HBORDERSTART);
    cMark *hborderStop        = marks.GetNext(0, MT_HBORDERSTOP);
    const cMark *vborderStart = marks.GetNext(0, MT_VBORDERSTART);
    const cMark *vborderStop  = marks.GetNext(0, MT_VBORDERSTOP);
    if (hborderStart && hborderStop) {
        int hDelta = (hborderStop->position - hborderStart->position) / decoder->GetVideoFrameRate();
        if (hDelta <= 230) {  // changed from 120 to 230
            dsyslog("cMarkAdStandalone::CheckMarks(): found hborder start (%d) and stop (%d), but distance %d too short, try if there is a next pair", hborderStart->position, hborderStop->position, hDelta);
            hborderStart = marks.GetNext(hborderStart->position, MT_HBORDERSTART);
            hborderStop = marks.GetNext(hborderStop->position, MT_HBORDERSTOP);
        }
    }
    if (vborderStart && vborderStop) {
        int vDelta = (vborderStop->position - vborderStart->position) / decoder->GetVideoFrameRate();
        if (vDelta < 120) {
            dsyslog("cMarkAdStandalone::CheckMarks(): found vborder stop/start, but distance %d too short, try if there is a next pair", vDelta);
            vborderStart = marks.GetNext(vborderStart->position, MT_VBORDERSTART);
            vborderStop = marks.GetNext(vborderStop->position, MT_VBORDERSTOP);
        }
    }
    if (channelStart && channelStop) {
        mark = marks.GetFirst();
        while (mark) {
            if (mark != marks.GetFirst()) {
                if (mark == marks.GetLast()) break;
                if ((mark->type == MT_LOGOSTART) || (mark->type == MT_LOGOSTOP)) {
                    cMark *tmp = mark;
                    mark = mark->Next();
                    dsyslog("cMarkAdStandalone::CheckMarks(): we have channel marks, delete logo mark (%i)", tmp->position);
                    marks.Del(tmp);
                    continue;
                }
            }
            mark = mark->Next();
        }
    }

    if ((hborderStart && hborderStop) || (vborderStart && vborderStop)) {
        mark = marks.GetFirst();
        while (mark) {
            if (mark != marks.GetFirst()) {
                if (((mark->type == MT_LOGOSTART) || (mark->type == MT_LOGOSTOP)) && (mark->position != marks.GetLast()->position)) {  // do not delete mark on end position, can be a logo mark even if we have border
                    cMark *tmp = mark;
                    mark = mark->Next();
                    dsyslog("cMarkAdStandalone::CheckMarks(): we have border marks, delete logo mark (%i)", tmp->position);
                    marks.Del(tmp);
                    continue;
                }
            }
            mark = mark->Next();
        }
    }

// delete very short logo stop/start pairs from detection error or undetected info logo
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): delete very short logo stop/start pairs from detection error or undetected info logo");
    DebugMarks();     //  only for debugging
    mark = marks.GetFirst();
    while (mark) {
        if (mark->type == MT_LOGOSTOP) {
            cMark *prevLogoStart = marks.GetPrev(mark->position, MT_LOGOSTART);
            cMark *nextLogoStart = marks.GetNext(mark->position, MT_LOGOSTART);
            if (prevLogoStart && nextLogoStart) {
                cMark *nextStop = marks.GetNext(mark->position, MT_STOP, 0x0F);  // last stop mark can be a different type
                if (nextStop) {
                    int prevLogoStart_Stop      = 1000 * (mark->position          - prevLogoStart->position) /  decoder->GetVideoFrameRate();
                    long int stop_nextLogoStart = 1000 * (nextLogoStart->position - mark->position)          /  decoder->GetVideoFrameRate();
                    int nextLogoStart_nextStop  = 1000 * (nextStop->position      - nextLogoStart->position) /  decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::CheckMarks(): MT_LOGOSTART (%6d) -> %7dms -> [MT_LOGOSTOP (%6d) -> %7ldms -> MT_LOGOSTART (%6d)] -> %7dms -> MT_STOP (%6d) -> %s", prevLogoStart->position, prevLogoStart_Stop, mark->position, stop_nextLogoStart, nextLogoStart->position, nextLogoStart_nextStop, nextStop->position, macontext.Info.ChannelName);

// cleanup logo detection failure
// delete sequence long broadcast -> very short stop/start -> long broadcast
// MT_LOGOSTART ( 15766) -> 1103180ms -> MT_LOGOSTOP ( 70925) ->    1040ms -> MT_LOGOSTART ( 70977) ->   83260ms -> MT_STOP ( 75140)
// MT_LOGOSTART ( 70977) ->   83260ms -> MT_LOGOSTOP ( 75140) ->    1020ms -> MT_LOGOSTART ( 75191) -> 1536500ms -> MT_STOP (152016)
// MT_LOGOSTART ( 13993) -> 1092500ms -> MT_LOGOSTOP ( 68618) ->    1700ms -> MT_LOGOSTART ( 68703) ->   82620ms -> MT_STOP ( 72834)
// MT_LOGOSTART ( 14595) ->  752000ms -> MT_LOGOSTOP ( 52195) ->    2500ms -> MT_LOGOSTART ( 52320) ->  718960ms -> MT_STOP ( 88268) -> ZDFinfo_HD
                    if (    (prevLogoStart_Stop     >= 83260) &&
                            (stop_nextLogoStart     <=  2500) &&
                            (nextLogoStart_nextStop >= 82620)) {
                        dsyslog("cMarkAdStandalone::CheckMarks(): logo stop (%5d) and logo start (%5d) pair from logo detection failure, deleting", mark->position, nextLogoStart->position);
                        cMark *tmp = nextStop;
                        marks.Del(nextLogoStart);
                        marks.Del(mark);
                        mark = tmp;
                        continue;
                    }

// delete very short stop/start pair from undetected introduction logo
// short after valid logo start -> short logo interruption -> long broadcast after
// valid short stop/start, do not delete
// MT_LOGOSTART ( 48867) ->    4880ms -> MT_LOGOSTOP ( 48989) ->     760ms -> MT_LOGOSTART ( 49008) ->  795000ms -> MT_STOP (68883)
// MT_LOGOSTART ( 51224) ->   29800ms -> MT_LOGOSTOP ( 51969) ->     920ms -> MT_LOGOSTART ( 51992) ->  622840ms -> MT_STOP (67563)
// MT_LOGOSTART ( 49708) ->  593600ms -> MT_LOGOSTOP ( 64548) ->     120ms -> MT_LOGOSTART ( 64551) ->   14880ms -> MT_STOP (64923)
// MT_LOGOSTART ( 37720) ->   26280ms -> MT_LOGOSTOP ( 38377) ->     840ms -> MT_LOGOSTART ( 38398) ->  254120ms -> MT_STOP (44751)
// MT_LOGOSTART ( 74875) ->    1400ms -> MT_LOGOSTOP ( 74910) ->     760ms -> MT_LOGOSTART ( 74929) ->  566480ms -> MT_STOP ( 89091)
//
// invalid stop/start pair from introduction logo change (detected as logo) to normal logo (kabel eins)
// MT_LOGOSTART ( 99981) ->    7720ms -> MT_LOGOSTOP (100174) ->    1120ms -> MT_LOGOSTART (100202) -> 1098840ms -> MT_STOP (127673) -> introdution logo change (kabel eins)
// MT_LOGOSTART ( 41139) ->    7760ms -> MT_LOGOSTOP ( 41333) ->    1080ms -> MT_LOGOSTART ( 41360) ->  872280ms -> MT_STOP ( 63167) -> introdution logo change (kabel eins)
// MT_LOGOSTART ( 74781) ->    7800ms -> MT_LOGOSTOP ( 74976) ->    1040ms -> MT_LOGOSTART ( 75002) ->  416280ms -> MT_STOP ( 85409) -> introdution logo change (kabel eins)
// MT_LOGOSTART ( 63508) ->    7920ms -> MT_LOGOSTOP ( 63706) ->     880ms -> MT_LOGOSTART ( 63728) -> 1204960ms -> MT_STOP ( 93852) -> introdution logo change (kabel eins)
// MT_LOGOSTART (172892) ->    6240ms -> MT_LOGOSTOP (173048) ->     840ms -> MT_LOGOSTART (173069) ->  156480ms -> MT_STOP (176981) -> kabel_eins
                    if (criteria->IsIntroductionLogoChannel() &&
                            (prevLogoStart_Stop     >=   6240) && (prevLogoStart_Stop <= 7920) &&
                            (stop_nextLogoStart     >=    840) && (stop_nextLogoStart <= 1120) &&
                            (nextLogoStart_nextStop >= 156480)) {
                        dsyslog("cMarkAdStandalone::CheckMarks(): logo stop (%5d) and logo start (%5d) pair from introduction logo, deleting", mark->position, nextLogoStart->position);
                        cMark *tmp = nextStop;
                        marks.Del(nextLogoStart);
                        marks.Del(mark);
                        mark = tmp;
                        continue;
                    }

// invalid stop/start pair from undetected logo change short after logo start mark, delete pair
// MT_LOGOSTART ( 82313) ->    1120ms -> MT_LOGOSTOP ( 82341) ->     640ms -> MT_LOGOSTART ( 82357) ->    1400ms -> MT_STOP ( 82392) -> first of double logo change (TELE 5)
// MT_LOGOSTART ( 87621) ->    1560ms -> MT_LOGOSTOP ( 87660) ->     640ms -> MT_LOGOSTART ( 87676) ->  193000ms -> MT_STOP ( 92501) -> logo change (TELE 5)
// MT_LOGOSTART ( 82313) ->    3160ms -> MT_LOGOSTOP ( 82392) ->     280ms -> MT_LOGOSTART ( 82399) ->  381160ms -> MT_STOP ( 91928) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 38739) ->    3240ms -> MT_LOGOSTOP ( 38820) ->     640ms -> MT_LOGOSTART ( 38836) -> 1222360ms -> MT_STOP ( 69395) -> logo change (TELE 5)
// MT_LOGOSTART ( 83792) ->    3240ms -> MT_LOGOSTOP ( 83873) ->     640ms -> MT_LOGOSTART ( 83889) ->  349560ms -> MT_STOP ( 92628) -> logo change (TELE 5)
// MT_LOGOSTART (126382) ->    3240ms -> MT_LOGOSTOP (126463) ->     640ms -> MT_LOGOSTART (126479) -> 1303560ms -> MT_STOP (159068) -> logo change (TELE 5)
// MT_LOGOSTART ( 41268) ->    3280ms -> MT_LOGOSTOP ( 41350) ->     640ms -> MT_LOGOSTART ( 41366) -> 1185880ms -> MT_STOP ( 71013) -> logo change (TELE 5)
// MT_LOGOSTART ( 41268) ->    3280ms -> MT_LOGOSTOP ( 41350) ->     640ms -> MT_LOGOSTART ( 41366) -> 1186840ms -> MT_STOP ( 71037) -> logo change (TELE 5)
// MT_LOGOSTART ( 82897) ->    3360ms -> MT_LOGOSTOP ( 82981) ->     640ms -> MT_LOGOSTART ( 82997) ->  283880ms -> MT_STOP ( 90094) -> logo change (TELE 5)
// MT_LOGOSTART ( 40561) ->    3400ms -> MT_LOGOSTOP ( 40646) ->     440ms -> MT_LOGOSTART ( 40657) ->     560ms -> MT_STOP ( 40671) -> first of double logo change (TELE 5)
// MT_LOGOSTART ( 31246) ->    3560ms -> MT_LOGOSTOP ( 31335) ->     280ms -> MT_LOGOSTART ( 31342) ->  936960ms -> MT_STOP ( 54766) -> logo change (TELE 5)
// MT_LOGOSTART ( 43027) ->    3560ms -> MT_LOGOSTOP ( 43116) ->     360ms -> MT_LOGOSTART ( 43125) -> 1180320ms -> MT_STOP ( 72633) -> logo change (TELE 5)
// MT_LOGOSTART ( 38402) ->    4440ms -> MT_LOGOSTOP ( 38513) ->    1120ms -> MT_LOGOSTART ( 38541) -> 1242400ms -> MT_STOP ( 69601) -> logo change (TELE 5)
// MT_LOGOSTART ( 35193) ->    4840ms -> MT_LOGOSTOP ( 35314) ->     760ms -> MT_LOGOSTART ( 35333) ->  919360ms -> MT_STOP ( 58317) -> logo change (TELE 5)
// MT_LOGOSTART ( 43980) ->    6760ms -> MT_LOGOSTOP ( 44149) ->     320ms -> MT_LOGOSTART ( 44157) -> 1129600ms -> MT_STOP ( 72397) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 43745) ->    7800ms -> MT_LOGOSTOP ( 43940) ->     520ms -> MT_LOGOSTART ( 43953) -> 1165200ms -> MT_STOP ( 73083) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 40306) ->    8160ms -> MT_LOGOSTOP ( 40510) ->     760ms -> MT_LOGOSTART ( 40529) -> 1216040ms -> MT_STOP ( 70930) -> logo change (TELE 5)
// MT_LOGOSTART ( 43750) ->    8160ms -> MT_LOGOSTOP ( 43954) ->     640ms -> MT_LOGOSTART ( 43970) -> 1187040ms -> MT_STOP ( 73646) -> logo change (TELE 5)
// MT_LOGOSTART ( 49262) ->    8160ms -> MT_LOGOSTOP ( 49466) ->     640ms -> MT_LOGOSTART ( 49482) -> 1196440ms -> MT_STOP ( 79393) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 49474) ->    8200ms -> MT_LOGOSTOP ( 49679) ->     640ms -> MT_LOGOSTART ( 49695) -> 1163720ms -> MT_STOP ( 78788) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 82486) ->    8440ms -> MT_LOGOSTOP ( 82697) ->     400ms -> MT_LOGOSTART ( 82707) ->  462880ms -> MT_STOP ( 94279) -> second of double logo change (TELE 5)
// MT_LOGOSTART ( 85792) ->    4320ms -> MT_LOGOSTOP ( 85900) ->     280ms -> MT_LOGOSTART ( 85907) -> 1428200ms -> MT_STOP (121612) -> TELE_5
                    if (criteria->IsLogoChangeChannel() &&
                            (prevLogoStart_Stop     >= 1120) && (prevLogoStart_Stop     <=    8440) &&
                            (stop_nextLogoStart     >=  280) && (stop_nextLogoStart     <=    1120) &&
                            (nextLogoStart_nextStop >=  560) && (nextLogoStart_nextStop <= 1428200)) {
                        dsyslog("cMarkAdStandalone::CheckMarks(): logo stop (%5d) and logo start (%5d) pair from undetected logo change, deleting", mark->position, nextLogoStart->position);
                        cMark *tmp = nextStop;
                        marks.Del(nextLogoStart);
                        marks.Del(mark);
                        mark = tmp;
                        continue;
                    }
// invalid stop/start pair from short logo interruption channel, delete pair
// delete more aggressiv
// detect from long broadcast before (logo interuption at end of part) or long broadcast after (logo interuption at start of part
// example of invalid logo stop/start pair from logo interruption
// near after valid logo start
// MT_LOGOSTART ( 33854) ->   17800ms -> [MT_LOGOSTOP ( 34299) ->     800ms -> MT_LOGOSTART ( 34319)] ->  272280ms -> MT_STOP ( 41126) -> Comedy_Central
// MT_LOGOSTART ( 42010) ->   17800ms -> [MT_LOGOSTOP ( 42455) ->     840ms -> MT_LOGOSTART ( 42476)] ->  543960ms -> MT_STOP ( 56075) -> Comedy_Central
// MT_LOGOSTART ( 40019) ->   35080ms -> [MT_LOGOSTOP ( 40896) ->     960ms -> MT_LOGOSTART ( 40920)] ->  668720ms -> MT_STOP ( 57638) -> Comedy_Central
// MT_LOGOSTART ( 20580) ->   43920ms -> [MT_LOGOSTOP ( 21678) ->    1120ms -> MT_LOGOSTART ( 21706)] ->  499320ms -> MT_STOP ( 34189) -> Comedy_Central
// MT_LOGOSTART ( 27011) ->   11960ms -> [MT_LOGOSTOP ( 27310) ->     720ms -> MT_LOGOSTART ( 27328)] ->  551920ms -> MT_STOP ( 41126) -> Comedy_Central
// MT_LOGOSTART ( 24262) ->   11720ms -> [MT_LOGOSTOP ( 24555) ->     960ms -> MT_LOGOSTART ( 24579)] ->  314000ms -> MT_STOP ( 32429) -> Comedy_Central
// MT_LOGOSTART ( 41408) ->   17960ms -> [MT_LOGOSTOP ( 41857) ->     640ms -> MT_LOGOSTART ( 41873)] ->  393360ms -> MT_STOP ( 51707) -> Comedy_Central
//
// double logo interruption near after valid logo start
// MT_LOGOSTART ( 32649) ->   11640ms -> [MT_LOGOSTOP ( 32940) ->    1040ms -> MT_LOGOSTART ( 32966)] ->    5120ms -> MT_STOP ( 33094) -> Comedy_Central
// MT_LOGOSTART ( 30526) ->   11640ms -> [MT_LOGOSTOP ( 30817) ->    1080ms -> MT_LOGOSTART ( 30844)] ->    5080ms -> MT_STOP ( 30971) -> Comedy_Central
// MT_LOGOSTART ( 21647) ->   11720ms -> [MT_LOGOSTOP ( 21940) ->    1000ms -> MT_LOGOSTART ( 21965)] ->    5040ms -> MT_STOP ( 22091) -> Comedy_Central
//
// double logo interruption near before valid logo stop
// MT_LOGOSTART ( 32162) ->  556560ms -> [MT_LOGOSTOP ( 46076) ->     880ms -> MT_LOGOSTART ( 46098)] ->    5160ms -> MT_STOP ( 46227) -> Comedy_Central
//
// near before valid logo stop
// MT_LOGOSTART ( 10503) ->  158960ms -> [MT_LOGOSTOP ( 14477) ->     520ms -> MT_LOGOSTART ( 14490)] ->   11600ms -> MT_STOP ( 14780) -> Comedy_Central
// MT_LOGOSTART (  9887) ->  441480ms -> [MT_LOGOSTOP ( 20924) ->     520ms -> MT_LOGOSTART ( 20937)] ->   23840ms -> MT_STOP ( 21533) -> Comedy_Central
// MT_LOGOSTART ( 10503) ->  575520ms -> [MT_LOGOSTOP ( 24891) ->     520ms -> MT_LOGOSTART ( 24904)] ->   11760ms -> MT_STOP ( 25198) -> Comedy_Central
// MT_LOGOSTART (  8634) ->  500760ms -> [MT_LOGOSTOP ( 21153) ->     520ms -> MT_LOGOSTART ( 21166)] ->   23840ms -> MT_STOP ( 21762) -> Comedy_Central
// MT_LOGOSTART (  9887) ->  700920ms -> [MT_LOGOSTOP ( 27410) ->     520ms -> MT_LOGOSTART ( 27423)] ->   17760ms -> MT_STOP ( 27867) -> Comedy_Central
// MT_LOGOSTART ( 32162) ->  562600ms -> [MT_LOGOSTOP ( 46227) ->     720ms -> MT_LOGOSTART ( 46245)] ->   31800ms -> MT_STOP ( 47040) -> Comedy_Central (conflict)
//
// example of valid logo stop/start pair
// MT_LOGOSTART ( 25628) ->  507960ms -> [MT_LOGOSTOP ( 38327) ->     720ms -> MT_LOGOSTART ( 38345)] ->   25600ms -> MT_STOP ( 38985) -> Comedy_Central
// MT_LOGOSTART (  8004) ->  634320ms -> [MT_LOGOSTOP ( 23862) ->     680ms -> MT_LOGOSTART ( 23879)] ->   18160ms -> MT_STOP ( 24333) -> Comedy_Central
// MT_LOGOSTART ( 32071) ->  307720ms -> [MT_LOGOSTOP ( 39764) ->     600ms -> MT_LOGOSTART ( 39779)] ->   25120ms -> MT_STOP ( 40407) -> Comedy_Central
                    if (criteria->IsLogoInterruptionChannel() &&
                            ((prevLogoStart_Stop     >=  11720) && (prevLogoStart_Stop     <=  43920) &&    // short broadcast before, long after
                             (stop_nextLogoStart     >=    640) && (stop_nextLogoStart     <=   1120) &&
                             (nextLogoStart_nextStop >= 272280) && (nextLogoStart_nextStop <= 668720)) ||

                            ((prevLogoStart_Stop     >=  11640) && (prevLogoStart_Stop     <=  11720) &&    // short broadcast before, very short after
                             (stop_nextLogoStart     >=   1000) && (stop_nextLogoStart     <=   1080) &&
                             (nextLogoStart_nextStop >=   5040) && (nextLogoStart_nextStop <=   5120)) ||

                            ((prevLogoStart_Stop     >= 556560) && (prevLogoStart_Stop     <=  556561) &&    // long broadcast before, very short after
                             (stop_nextLogoStart     >=    880) && (stop_nextLogoStart     <=     881) &&
                             (nextLogoStart_nextStop >=   5160) && (nextLogoStart_nextStop <=    5161)) ||

                            ((prevLogoStart_Stop     >= 158960) && (prevLogoStart_Stop     <= 700920) &&    // long broadcast before, short after
                             (stop_nextLogoStart     >=    520) && (stop_nextLogoStart     <     680) &&
                             (nextLogoStart_nextStop >=  11600) && (nextLogoStart_nextStop <   25120))) {
                        dsyslog("cMarkAdStandalone::CheckMarks(): logo stop (%5d) and logo start (%5d) pair from logo change channel, deleting", mark->position, nextLogoStart->position);
                        cMark *tmp = nextStop;
                        marks.Del(nextLogoStart);
                        marks.Del(mark);
                        mark = tmp;
                        continue;
                    }
                }
            }
        }
        mark = mark->Next();
    }

// delete short START STOP logo marks because they are previews in the advertisement
// preview detection chain:
// logo start -> logo stop:  long broadcast is before advertisement or short broadcast is previous preview
// logo stop  -> logo start: advertisement
// logo start -> logo stop:  preview  (this start mark is the current mark in the loop)
//                           first start mark and last stop mark could not be part of a preview
// logo stop  -> logo start: advertisement
// logo start -> logo stop:  long broadcast is after advertisement or short broadcast is next preview
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): detect previews in advertisement");
    DebugMarks();     //  only for debugging
    mark = marks.GetFirst();
    while (true) {
        mark = mark->Next();   // ignore first mark, this is start of broadcast and can not be start of preview
        if (!mark) break;
// check logo marks
        if (mark->type != MT_LOGOSTART) continue;
        cMark *stopMark = marks.GetNext(mark->position, MT_LOGOSTOP);
        if (!stopMark) continue;
        if (stopMark->position == marks.GetLast()->position) continue; // do not check end mark
        LogSeparator();
        dsyslog("cMarkAdStandalone::CheckMarks(): check logo start (%d) and logo stop (%d) for preview", mark->position, stopMark->position);
        // get marks before and after
        const cMark *stopBefore = marks.GetPrev(mark->position, MT_LOGOSTOP);
        if (!stopBefore) {
            dsyslog("cMarkAdStandalone::CheckMarks(): no preview because no MT_LOGOSTOP before found");
            continue;
        }
        cMark *startAfter = marks.GetNext(stopMark->position, MT_LOGOSTART);
        if (!startAfter) {
            dsyslog("cMarkAdStandalone::CheckMarks(): no preview because no MT_LOGOSTART after found");
            continue;
        }
        cMark *startBefore = marks.GetPrev(stopBefore->position, MT_START, 0x0F);   // start mark can be MT_ASSUMEDSTART
        if (!startBefore) {
            dsyslog("cMarkAdStandalone::CheckMarks(): no preview because no MT_START before found");
            continue;
        }
        const cMark *stopAfter   = marks.GetNext(startAfter->position, MT_STOP, 0x0F);   // end mark can be MT_ASSUMEDSTOP
        if (!stopAfter) {
            dsyslog("cMarkAdStandalone::CheckMarks(): no preview because no MT_STOP after found");
            continue;
        }
        int lengthAdBefore        = 1000 * (mark->position       - stopBefore->position)  / decoder->GetVideoFrameRate();
        int lengthAdAfter         = 1000 * (startAfter->position - stopMark->position)    / decoder->GetVideoFrameRate();
        int lengthPreview         = 1000 * (stopMark->position   - mark->position)        / decoder->GetVideoFrameRate();
        int lengthBroadcastBefore = 1000 * (stopBefore->position - startBefore->position) / decoder->GetVideoFrameRate();
        int lengthBroadcastAfter  = 1000 * (stopAfter->position  - startAfter->position)  / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::CheckMarks(): MT_LOGOSTART(%6d)->%7dms->MT_LOGOSTOP(%6d)->%dms->[MT_LOGOSTART(%6d)->%dms->MT_LOGOSTOP(%6d)]->%dms->MT_LOGOSTART(%6d)->%dms->MT_STOP(%6d)", startBefore->position, lengthBroadcastBefore, stopBefore->position, lengthAdBefore, mark->position, lengthPreview, stopMark->position, lengthAdAfter, startAfter->position, lengthBroadcastAfter, stopAfter->position);
// preview example
// tbd
//
// no preview example
//            |- broadcast before --|         |---- ad before ----|           |---- broadcast ---|         |----- short ad ----|          |-- broadcast after -|
// MT_LOGOSTART( 40485)-> 940800ms->MT_LOGOSTOP( 64005)->612600ms->[MT_LOGOSTART(79320)->79800ms->MT_LOGOSTOP(81315)]->12000ms->MT_LOGOSTART(81615)->1365600ms->MT_STOP(115755)
//
//            |---- broadcast -----|          |----- short ad ------|         |----- broadcast ----|      |- short logo interruption -|    |- ad in frame with logo -|
// MT_LOGOSTART(149051)->1899920ms->MT_LOGOSTOP(196549)->11440ms->[MT_LOGOSTART(196835)->132360ms->MT_LOGOSTOP(200144)]->200ms->MT_LOGOSTART(200149)->19960ms->MT_LOGOSTOP(200648)
//
//            |---- broadcast ----|         |- logo detection error -|   |--- broadcast --|          |- logo detection error -|    |---- broadcast -----|
// MT_LOGOSTART(13928)->785120ms->MT_LOGOSTOP(53184)->500ms->[MT_LOGOSTART(53209)->9860ms->MT_LOGOSTOP(53702)]->2840ms->MT_LOGOSTART(53844)->1835720ms->MT_STOP(145630)

        // check if we have long broadcast before, a long broadcast after and only very short ads in sequence,
        // in this case it could not be a preview, it is a double logo detection error
        if ((lengthBroadcastBefore >= 785120) && (lengthBroadcastAfter >= 1835720) && (lengthAdBefore <= 500) && (lengthAdAfter <= 2840)) { // ignore double logo detection error
            dsyslog("cMarkAdStandalone::CheckMarks(): double logo detection failure");
            continue;
        }

        if ((lengthAdBefore >= 800) || (lengthAdAfter >= 360)) {  // check if we have ad before or after preview. if not it is a logo detection failure
            if (((lengthAdBefore >= 120) && (lengthAdBefore <= 585000) && (lengthAdAfter >= 200))) { // if advertising before is long this is the next start mark
                if (lengthPreview < 132360) {
                    // check if this logo stop and next logo start are closing credits, in this case stop mark is valid
                    bool isNextClosingCredits = false;
                    if (criteria->IsClosingCreditsChannel()) isNextClosingCredits = evaluateLogoStopStartPair && (evaluateLogoStopStartPair->GetIsClosingCreditsAfter(startAfter->position) == STATUS_YES);
                    if (!isNextClosingCredits || (stopMark->position != marks.GetLast()->position)) { // check valid only for last mark
                        // check if this logo start mark and previuos logo stop mark are closing credits with logo, in this case logo start mark is valid
                        // this only work on the first logo stark mark because there are closing credits in previews
                        bool isThisClosingCredits = false;
                        if (criteria->IsClosingCreditsChannel()) isThisClosingCredits = evaluateLogoStopStartPair && (evaluateLogoStopStartPair->GetIsClosingCreditsAfter(mark->position) == STATUS_YES);
                        if (!isThisClosingCredits || (stopMark->position != marks.GetFirst()->position)) {
                            isyslog("found preview between logo start mark (%d) and logo stop mark (%d) in advertisement, deleting marks", mark->position, stopMark->position);
                            cMark *tmp = startBefore;  // continue with mark before
                            marks.Del(mark);
                            marks.Del(stopMark);
                            mark = tmp;
                        }
                        else dsyslog("cMarkAdStandalone::CheckMarks(): long advertisement before and logo stop before and this logo start mark are closing credits, this pair contains a start mark");
                    }
                    else dsyslog("cMarkAdStandalone::CheckMarks(): next stop/start pair are closing credits, this pair contains a stop mark");
                }
                else dsyslog("cMarkAdStandalone::CheckMarks(): no preview between (%d) and (%d), length %dms not valid", mark->position, mark->Next()->position, lengthPreview);
            }
            else dsyslog("cMarkAdStandalone::CheckMarks(): no preview between (%d) and (%d), length advertising before %ds or after %dms is not valid", mark->position, mark->Next()->position, lengthAdBefore, lengthAdAfter);
        }
        else dsyslog("cMarkAdStandalone::CheckMarks(): not long enough ad before and after preview, maybe logo detection failure");
    }

// delete invalid short START STOP hborder marks
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): check border marks");
    DebugMarks();     //  only for debugging

// delete late first vborder start mark, they are start of next recording
    cMark *borderStart = marks.GetNext(0, MT_VBORDERSTART);
    if ((borderStart) && (marks.Count(MT_VBORDERCHANGE, 0xF0) == 1)) {
        int diffEnd = (borderStart->position - newStopA) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::CheckMarks(): late single vborder start mark (%d) %ds after assumed stop (%d), this is start of next broadcast", borderStart->position, diffEnd, newStopA);
        if (diffEnd >= -156) { // maybe reported recording length is too big
            dsyslog("cMarkAdStandalone::CheckMarks(): delete all marks from (%d) to end", borderStart->position);
            marks.DelAfterFromToEnd(borderStart->position - 1);
        }
    }


// delete start/stop hborder pairs before packetCheckStart if there are no other hborder marks, they are a preview with hborder before recording start
    mark = marks.GetFirst();
    if (mark && (mark->type == MT_HBORDERSTART) && (marks.Count(MT_HBORDERSTART) == 1)) {
        const cMark *markNext = mark->Next();
        if (markNext && (markNext->type == MT_HBORDERSTOP) && (markNext->position < packetCheckStart) && (markNext->position != marks.GetLast()->position) && (marks.Count(MT_HBORDERSTOP) == 1)) {
            dsyslog("cMarkAdStandalone::CheckMarks(): preview with hborder before recording start found, delete start (%d) stop (%d)", mark->position, mark->Next()->position);
            marks.Del(markNext->position);
            marks.Del(mark->position);
        }
    }
// delete short START STOP hborder marks with logo start mark between, because they are advertisement with border in the advertisement
    mark = marks.GetFirst();
    while (mark) {
        if ((mark->type == MT_HBORDERSTART) && (mark->position != marks.GetFirst()->position) && mark->Next()) {  // not start or end mark
            cMark *bStop = marks.GetNext(mark->position, MT_HBORDERSTOP);
            if (bStop && (bStop->position != marks.GetLast()->position)) { // next mark not end mark
                int lengthAd = static_cast<int> ((bStop->position - mark->position) / decoder->GetVideoFrameRate());
                if (lengthAd < 130) { // increased from 70 to 130
                    isyslog("found advertisement of length %is between hborder mark (%i) and hborder mark (%i), deleting marks", lengthAd, mark->position, bStop->position);
                    cMark *logoStart = marks.GetNext(mark->position, MT_LOGOSTART);
                    if (logoStart && (logoStart->position <= bStop->position)) { // if there is a logo start between hborder start and bhorder end, it is the logo start of the preview, this is invalid
                        dsyslog("cMarkAdStandalone::CheckMarks(): invalid logo start mark between hborder start/stop, delete (%d)", logoStart->position);
                        marks.Del(logoStart);
                    }
                    cMark *tmp = mark;
                    mark = mark->Next();  // this can be the border stop mark or any other mark in between
                    if (mark->position == bStop->position) mark = mark->Next();  // there can be other marks in between
                    marks.Del(tmp);
                    marks.Del(bStop);
                    continue;
                }
            }
        }
        mark = mark->Next();
    }

// check for short start/stop pairs at the start part
    CheckStartMark();


// if we have a VPS events, move start and end mark of weak marks to VPS event
// do not use pause events, detected marks are usually better
    LogSeparator();
    if (macontext.Config->useVPS) {
        LogSeparator();
        dsyslog("cMarkAdStandalone::CheckMarks(): apply VPS events");
        DebugMarks();     //  only for debugging
        int vpsOffset = vps->GetStart(); // VPS start mark
        if (vpsOffset >= 0) {
            isyslog("VPS start event at %d:%02d:%02d", vpsOffset / 60 / 60,  (vpsOffset / 60 ) % 60, vpsOffset % 60);
            AddMarkVPS(vpsOffset, MT_START);
        }
        else dsyslog("cMarkAdStandalone::CheckMarks(): no VPS start event found");

        vpsOffset = vps->GetStop();     // VPS stop mark
        if (vpsOffset >= 0) {
            isyslog("VPS stop  event at %d:%02d:%02d", vpsOffset / 60 / 60,  (vpsOffset / 60 ) % 60, vpsOffset % 60);
            AddMarkVPS(vpsOffset, MT_STOP);
        }
        else dsyslog("cMarkAdStandalone::CheckMarks(): no VPS stop event found");

// once again check marks
        mark = marks.GetFirst();
        while (mark) {
            if (((mark->type & 0x0F)==MT_START) && (mark->Next()) && ((mark->Next()->type & 0x0F)==MT_START)) {  // two start marks, delete second
                dsyslog("cMarkAdStandalone::CheckMarks(): start mark (%i) followed by start mark (%i) delete non VPS mark", mark->position, mark->Next()->position);
                if (mark->type == MT_VPSSTART) {
                    marks.Del(mark->Next());
                    continue;
                }
                if (mark->Next()->type == MT_VPSSTART) {
                    cMark *tmp=mark;
                    mark = mark->Next();
                    marks.Del(tmp);
                    continue;
                }
            }
            if (((mark->type & 0x0F)==MT_STOP) && (mark->Next()) && ((mark->Next()->type & 0x0F)==MT_STOP)) {  // two stop marks, delete second
                dsyslog("cMarkAdStandalone::CheckMarks(): stop mark (%i) followed by stop mark (%i) delete non VPS mark", mark->position, mark->Next()->position);
                if (mark->type == MT_VPSSTOP) {
                    marks.Del(mark->Next());
                    continue;
                }
                if (mark->Next()->type == MT_VPSSTOP) {
                    cMark *tmp=mark;
                    mark = mark->Next();
                    marks.Del(tmp);
                    continue;
                }
            }
            if (((mark->type & 0x0F) == MT_START) && (!mark->Next())) {      // delete start mark at the end
                if (marks.GetFirst()->position != mark->position) {        // do not delete start mark
                    dsyslog("cMarkAdStandalone::CheckMarks(): START mark at the end, deleting %i", mark->position);
                    marks.Del(mark);
                    break;
                }
            }
            mark = mark->Next();
        }
    }

// check for better end mark not very far away from assuemd end
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): check for final end mark in case of recording length is too big");
    DebugMarks();     //  only for debugging

    bool moreMarks = true;
    while (moreMarks) {
        if (abortNow) return;
        moreMarks = false;
        // get last 3 marks
        cMark *lastStopMark = marks.GetLast();
        if (!lastStopMark) break;
        if ((lastStopMark->type & 0x0F) != MT_STOP) break;
        if (((lastStopMark->type & 0xF0) >= MT_CHANNELCHANGE) && ((lastStopMark->type & 0xF0) != MT_MOVED)) break;  // trust channel marks and better
        cMark *lastStartMark = marks.GetPrev(lastStopMark->position);
        if (!lastStartMark) break;
        if ((lastStartMark->type & 0x0F) != MT_START) break;
        cMark *prevStopMark = marks.GetPrev(lastStartMark->position);
        if (!prevStopMark) break;
        if ((prevStopMark->type & 0x0F) != MT_STOP) break;
        cMark *prevStartMark = marks.GetPrev(prevStopMark->position);
        if (!prevStartMark) break;
        if ((prevStartMark->type & 0x0F) != MT_START) break;
        int lastBroadcast        = (lastStopMark->position  - lastStartMark->position) / decoder->GetVideoFrameRate();
        int prevBroadcast        = (prevStopMark->position  - prevStartMark->position) / decoder->GetVideoFrameRate();
        int diffPrevStopAssumed  = (prevStopMark->position  - newStopA)                / decoder->GetVideoFrameRate();
        int diffLastStopAssumed  = (lastStopMark->position  - newStopA)                / decoder->GetVideoFrameRate();
        int lastAd               = (lastStartMark->position - prevStopMark->position)  / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::CheckMarks(): MT_START (%6d) -> %4ds -> MT_STOP (%6d) |%4ds| -> %3ds -> MT_START (%6d) -> %3ds -> MT_STOP (%6d) |%3ds| -> %3s", prevStartMark->position, prevBroadcast, prevStopMark->position, diffPrevStopAssumed, lastAd, lastStartMark->position, lastBroadcast, lastStopMark->position, diffLastStopAssumed, macontext.Info.ChannelName);
        switch(lastStopMark->type) {
        case MT_ASSUMEDSTOP:
            // example of invalid assumed stop mark sequence (short last ad is between two broadcasts)
            // MT_START (103247) -> 1096s -> MT_STOP (158068) |-348s| -> 140s -> MT_START (165090) -> 207s -> MT_STOP (175483) |  0s|
            // MT_START (109086) -> 1061s -> MT_STOP (162165) |-298s| ->  71s -> MT_START (165764) -> 226s -> MT_STOP (177074) |  0s|
            // MT_START ( 80858) ->  333s -> MT_STOP ( 89202) |-324s| ->  36s -> MT_START ( 90116) -> 287s -> MT_STOP ( 97308) |  0s| -> VOXup
            // MT_START ( 90363) ->  264s -> MT_STOP ( 96964) |-317s| ->  25s -> MT_START ( 97603) -> 291s -> MT_STOP (104891) |  0s| -> VOXup
            if (    (prevBroadcast >= 264) &&         // last broadcast
                    (diffPrevStopAssumed >= -359) &&  // logo stop before distance to assumed stop
                    (lastAd <= 170) && (lastBroadcast >= 150)) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, last ad too short", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            // example of invalid assumed stop mark sequence (last broadcast too short)
            // MT_START (  4200) -> 2400s -> MT_STOP ( 64215) |-418s| -> 401s -> MT_START ( 74254) ->  17s -> MT_STOP ( 74685) |  0s|
            // MT_START (  4184) -> 2338s -> MT_STOP ( 62638) |-264s| -> 260s -> MT_START ( 69142) ->   4s -> MT_STOP ( 69259) |  0s|
            // MT_START ( 79040) ->   21s -> MT_STOP ( 79579) | -62s| ->  58s -> MT_START ( 81040) ->   4s -> MT_STOP ( 81151) |  0s|
            // MT_START (  7428) -> 1262s -> MT_STOP ( 38982) |-537s| -> 397s -> MT_START ( 48924) -> 140s -> MT_STOP ( 52427) |  0s|
            // MT_START (  7642) -> 1263s -> MT_STOP ( 39236) |-536s| -> 362s -> MT_START ( 48297) -> 173s -> MT_STOP ( 52641) |  0s|
            // MT_START (  5836) -> 1311s -> MT_STOP ( 38629) |-488s| -> 486s -> MT_START ( 50798) ->   1s -> MT_STOP ( 50835) |  0s|
            // MT_START (  8450) -> 1296s -> MT_STOP ( 40853) |-503s| -> 307s -> MT_START ( 48532) -> 196s -> MT_STOP ( 53449) |  0s|
            // MT_START ( 10748) -> 1311s -> MT_STOP ( 43547) |-488s| -> 265s -> MT_START ( 50185) -> 211s -> MT_STOP ( 55461) |-11s|   // assumed stop after recording end
            // MT_START (  7443) -> 1303s -> MT_STOP ( 40021) |-496s| -> 278s -> MT_START ( 46979) -> 218s -> MT_STOP ( 52442) |  0s|
            // MT_START (  7389) -> 1250s -> MT_STOP ( 38659) |-549s| -> 498s -> MT_START ( 51123) ->  50s -> MT_STOP ( 52380) |  0s|
            // MT_START (167649) -> 1163s -> MT_STOP (196735) |-621s| -> 515s -> MT_START (209631) -> 105s -> MT_STOP (212268) |  0s|
            else if ((diffPrevStopAssumed >= -621) && (lastBroadcast <= 218)) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, last broadcast too short", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            break;
        case MT_LOGOSTOP:
            // example of invalid logo stop mark sequence (short last ad is between two long broadcasts)
            // MT_START ( 73336) ->  960s -> MT_STOP ( 97358)         ->  18s -> MT_START ( 97817) ->  83s -> MT_STOP ( 99916)
            // MT_START ( 97756) ->    0s -> MT_STOP ( 97761)         ->   0s -> MT_START ( 97766) ->  86s -> MT_STOP ( 99916)        -> more than one false logo stop
            // MT_START ( 97460) ->    0s -> MT_STOP ( 97465)         ->   0s -> MT_START ( 97469) ->  86s -> MT_STOP ( 99619)        -> more than one false logo stop
            // MT_START ( 66619) ->  694s -> MT_STOP ( 83973)         ->  10s -> MT_START ( 84240) -> 610s -> MT_STOP ( 99507)
            // MT_START ( 39172) ->  931s -> MT_STOP ( 62463) |-280s| ->   5s -> MT_START ( 62601) -> 408s -> MT_STOP ( 72811) |133s|
            // MT_START ( 42975) ->  423s -> MT_STOP ( 53574) |-166s| ->  39s -> MT_START ( 54564) -> 232s -> MT_STOP ( 60368) |105s|
            // MT_START ( 12594) -> 1226s -> MT_STOP ( 43264) |-273s| ->  21s -> MT_START ( 43789) -> 177s -> MT_STOP ( 48232) |-74s| -> Comedy_Central: length too big
            // MT_START ( 36120) ->  675s -> MT_STOP ( 53011) |-140s| ->  21s -> MT_START ( 53536) -> 113s -> MT_STOP ( 56371) | -5s| -> Comedy_Central: length too big
            // MT_START ( 30964) ->  635s -> MT_STOP ( 46844) |-124s| ->  59s -> MT_START ( 48324) ->  95s -> MT_STOP ( 50712) | 30s| -> Comedy_Central: length too big
            if ((diffPrevStopAssumed >= -280) && (lastAd <= 59) && (diffLastStopAssumed >= -74)) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, last ad too short", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            // example of invalid log stop mark sequence (too short last broadcast is preview)
            // MT_START ( 45380) ->  290s -> MT_STOP ( 52642) |  57s| -> 141s -> MT_START ( 56190) ->   0s -> MT_STOP ( 56199) | 200s|
            // MT_START (  4106) -> 2700s -> MT_STOP ( 71623) |-691s| -> 595s -> MT_START ( 86503) ->  77s -> MT_STOP ( 88450) | -18s|
            // MT_START (  8469) -> 2395s -> MT_STOP ( 68361) |-401s| -> 222s -> MT_START ( 73925) ->  82s -> MT_STOP ( 75995) | -95s|
            // MT_START ( 26642) -> 1702s -> MT_STOP (111783) |-301s| ->  17s -> MT_START (112636) -> 129s -> MT_STOP (119135) |-154s| -> arte_HD  (conflict)
            //
            // example of valid logo end mark
            // MT_START ( 48141) -> 1050s -> MT_STOP ( 74406) |-644s| -> 468s -> MT_START ( 86116) -> 129s -> MT_STOP ( 89351) | -46s| -> VOXup
            // MT_START ( 51714) ->  955s -> MT_STOP ( 75611) |-638s| -> 436s -> MT_START ( 86513) -> 117s -> MT_STOP ( 89462) |-84s| -> VOXup
            else if (lastBroadcast < 117) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, last broadcast too short", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            // example of invalid log stop mark sequence (short last broadcast, late logo stop)
            // MT_START ( 15111) -> 2550s -> MT_STOP (142613) | -31s| ->  71s -> MT_START (146196) -> 147s -> MT_STOP (153589) |187s| -> ZDF_H
            else if ((diffPrevStopAssumed >= -31) && (lastBroadcast <= 147)) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, last broadcast short and previous logo stop near assumed end", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            break;
        case MT_MOVEDSTOP:
            // example of invalid logo stop mark sequence (too short last broadcase)
            // MT_START ( 19164) -> 1565s -> MT_STOP ( 97440) |-247s| ->  57s -> MT_START (100294) ->  26s -> MT_STOP (101638) |-163s|  (VPS stop)
            // MT_START ( 18475) -> 1568s -> MT_STOP ( 96899) |-244s| ->  47s -> MT_START ( 99287) ->  47s -> MT_STOP (101638) |-149s|  (VPS stop)
            if (lastBroadcast <= 47) {
                dsyslog("cMarkAdStandalone::CheckMarks(): use stop mark (%d) before as end mark, too short last broadcast part", prevStopMark->position);
                marks.Del(lastStopMark->position);
                marks.Del(lastStartMark->position);
                moreMarks = true;
            }
            break;
        default:
            moreMarks = false;
            break;
        }
    }

// delete short START STOP logo marks because they are previes not detected above or due to next broadcast
// delete short STOP START logo marks because they are logo detection failure
// delete short STOP START hborder marks because some channels display information in the border
// delete short STOP START vborder marks because they are from malfunction recording
    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): remove logo and hborder detection failure marks");
    DebugMarks();     //  only for debugging

// first pass, delete all very short STOP START logo marks because they are logo detection failure
    mark = marks.GetFirst();
    while (mark) {
        if ((mark->type == MT_LOGOSTOP) && (mark->Next()) && (mark->Next()->type == MT_LOGOSTART)) {
            int diff = 1000 * (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
            if (diff <= 360) {
                dsyslog("cMarkAdStandalone::CheckMarks(): very short logo stop (%d) start (%d) length %dms, deleting", mark->position, mark->Next()->position, diff);
                cMark *tmp = mark;
                mark = mark->Next()->Next();
                marks.Del(tmp->Next());
                marks.Del(tmp);
                continue;
            }
        }
        mark = mark->Next();
    }

// second pass, delete rest
    mark = marks.GetFirst();
    while (mark) {
        if ((mark->position > marks.GetFirst()->position) && (mark->type == MT_LOGOSTART) && mark->Next() && mark->Next()->type == MT_LOGOSTOP) {  // do not delete selected start mark
            // next logo stop/start pair could be "Teletext ..." info
            int MARKDIFF = static_cast<int> (decoder->GetVideoFrameRate() * 38); // changed from 8 to 18 to 35 to 38
            double distance = (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
            if ((mark->Next()->position - mark->position) <= MARKDIFF) {
                isyslog("mark distance between logo START and STOP too short %.1fs, deleting (%i,%i)", distance, mark->position, mark->Next()->position);
                cMark *tmp = mark;
                mark = mark->Next()->Next();
                marks.Del(tmp->Next());
                marks.Del(tmp);
                continue;
            }
            else dsyslog("cMarkAdStandalone::CheckMarks(): mark distance between logo START and STOP %.1fs, keep (%i,%i)", distance, mark->position, mark->Next()->position);
        }

        // check for logo detection failure, delete if logo stop/start pair is too short for an ad
        if ((mark->type == MT_LOGOSTOP) && mark->Next() && mark->Next()->type == MT_LOGOSTART) {
            int lengthAd = 1000 * (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
            dsyslog("cMarkAdStandalone::CheckMarks(): mark distance between logo stop (%d) and logo start (%d), length advertising %dms", mark->position, mark->Next()->position, lengthAd);
            if (lengthAd < 13040) {  // found shortest ad in broadcast with 13040ms, between two broadcast the can be very short
                isyslog("mark distance %ds too short, deleting logo stop mark (%d) and logo start mark (%d)", lengthAd / 1000, mark->position, mark->Next()->position);
                cMark *tmp = mark;
                mark = mark->Next()->Next();
                marks.Del(tmp->Next());
                marks.Del(tmp);
                continue;
            }
        }
        if ((mark->type == MT_HBORDERSTOP) && mark->Next() && mark->Next()->type == MT_HBORDERSTART) {
            int MARKDIFF = static_cast<int> (decoder->GetVideoFrameRate() * 20);  // increased from 15 to 20
            if ((mark->Next()->position - mark->position) <= MARKDIFF) {
                double distance = (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
                isyslog("mark distance between horizontal STOP and START too short (%.1fs), deleting %i,%i", distance, mark->position, mark->Next()->position);
                cMark *tmp = mark;
                mark = mark->Next()->Next();
                marks.Del(tmp->Next());
                marks.Del(tmp);
                continue;
            }
        }
        if ((mark->type == MT_VBORDERSTOP) && mark->Next() && mark->Next()->type == MT_VBORDERSTART) {
            int MARKDIFF = static_cast<int> (decoder->GetVideoFrameRate() * 2);
            if ((mark->Next()->position - mark->position) <= MARKDIFF) {
                double distance = (mark->Next()->position - mark->position) / decoder->GetVideoFrameRate();
                isyslog("mark distance between vertical STOP and START too short (%.1fs), deleting %i,%i", distance, mark->position, mark->Next()->position);
                cMark *tmp = mark;
                mark = mark->Next()->Next();
                marks.Del(tmp->Next());
                marks.Del(tmp);
                continue;
            }
        }
        mark = mark->Next();
    }

    marks.DelInvalidSequence();

    LogSeparator();
    dsyslog("cMarkAdStandalone::CheckMarks(): final marks:");
    DebugMarks();     //  only for debugging
    LogSeparator();
}


void cMarkAdStandalone::AddMarkVPS(const int offset, const int type) {
    if (!decoder) return;
    if (!index)   return;
    int vpsFrame = index->GetFrameFromOffset(offset * 1000);
    if (vpsFrame < 0) {
        esyslog("cMarkAdStandalone::AddMarkVPS(): failed to get frame from offset %d", offset);
        return;
    }
    // get current start / end mark and nearest mark with same type
    cMark *mark     = nullptr;
    cMark *nearMark = nullptr;  // nearest mark with same type, must be same as start / end mark
    switch (type) {
    case MT_START: {
        mark = marks.GetFirst();
        if (!mark) {
            esyslog("cMarkAdStandalone::AddMarkVPS(): no marks found");
            return;
        }
        nearMark = marks.GetAround(INT_MAX, vpsFrame, MT_START, 0x0F);
        if (!nearMark) {
            esyslog("cMarkAdStandalone::AddMarkVPS(): no near mark with same type found");
            return;
        }
        char *nearMarkType = marks.TypeToText(nearMark->type);
        dsyslog("cMarkAdStandalone::AddMarkVPS(): start mark (%d), VPS start event (%d), nearest start mark: (%d) %s start", mark->position, vpsFrame, nearMark->position, nearMarkType);
        FREE(strlen(nearMarkType) + 1, "text");
        free(nearMarkType);
        // only use VPS start event for weak marks
        if (mark->type != MT_ASSUMEDSTART) {
            char *markType = marks.TypeToText(mark->type);
            dsyslog("cMarkAdStandalone::AddMarkVPS(): keep strong %s start (%d) as start mark", markType, mark->position);
            FREE(strlen(markType) + 1, "text");
            free(markType);
            return;
        }
        break;
    }
    case MT_STOP: {
        mark = marks.GetLast();
        if (!mark) {
            esyslog("cMarkAdStandalone::AddMarkVPS(): no marks found");
            return;
        }
        nearMark = marks.GetAround(INT_MAX, vpsFrame, MT_STOP, 0x0F);
        if (!nearMark) {
            esyslog("cMarkAdStandalone::AddMarkVPS(): no near mark with same type found");
            return;
        }
        cMark *nextStartMark = marks.GetNext(vpsFrame, MT_START, 0x0F);
        if (nextStartMark) {
            dsyslog("cMarkAdStandalone::AddMarkVPS(): start mark (%d) after VPS stop event found, VPS stop event seems to be invalid", nextStartMark->position);
            return;
        }
        int diff = abs(vpsFrame - nearMark->position) / decoder->GetVideoFrameRate();
        char *nearMarkType = marks.TypeToText(nearMark->type);
        dsyslog("cMarkAdStandalone::AddMarkVPS(): current end mark (%d), VPS stop event (%d), nearest stop mark (%d) stop %s is %ds before VPS event", mark->position, vpsFrame, nearMark->position, nearMarkType, diff);
        FREE(strlen(nearMarkType) + 1, "text");
        free(nearMarkType);
        // keep strong end marks, they are better than VPS marks
        if ((mark->type != MT_ASSUMEDSTOP) &&
                ((mark->type != MT_TYPECHANGESTOP) || !criteria->GoodVPS())) {
            char *markType = marks.TypeToText(mark->type);
            dsyslog("cMarkAdStandalone::AddMarkVPS(): keep strong stop %s mark (%d) as end mark", markType, mark->position);
            FREE(strlen(markType) + 1, "text");
            free(markType);
            return;
        }
        // use near strong and reliable stop mark as end mark
        if ((diff <= 49) && (nearMark->type >= MT_CHANNELSTOP)) {
            dsyslog("cMarkAdStandalone::AddMarkVPS(): assumed end mayby wrong, use reliable stop mark (%d) near VPS event (%d) as end mark", nearMark->position, vpsFrame);
            marks.DelFromTo(nearMark->position + 1, INT_MAX, MT_ALL, 0xFF);
            return;
        }
        break;
    }
    default:
        esyslog("cMarkAdStandalone::AddMarkVPS(): invalid mark type %d", type);
        return;
    }

    char *timeText = nullptr;
    char *indexToHMSF = marks.IndexToHMSF(vpsFrame, AV_NOPTS_VALUE, false);
    if (indexToHMSF) {
        ALLOC(strlen(indexToHMSF) + 1, "indexToHMSF");
    }
    dsyslog("cMarkAdStandalone::AddMarkVPS(): apply VPS %s at frame (%d) at %s", (type == MT_START) ? "start" : "stop", vpsFrame, (indexToHMSF) ? indexToHMSF : "unknown");
    if (indexToHMSF) {
        FREE(strlen(indexToHMSF) + 1, "indexToHMSF");
        free(indexToHMSF);
    }
    mark = ((type == MT_START)) ? marks.GetNext(0, MT_START, 0x0F) : marks.GetPrev(INT_MAX, MT_STOP, 0x0F);
    if (!mark) {
        dsyslog("cMarkAdStandalone::AddMarkVPS(): found no mark found to replace");
        return;
    }
    if ( (type & 0x0F) != (mark->type & 0x0F)) return;

    timeText = marks.GetTime(mark);
    if (timeText) {
        int diff = (mark->position - vpsFrame) / decoder->GetVideoFrameRate();
        dsyslog("cMarkAdStandalone::AddMarkVPS(): mark to replace at frame (%d) type 0x%X at %s, %ds after mark", mark->position, mark->type, timeText, diff);
        if (abs(diff) < 1225) {  // do not replace very far marks, there can be an invalid VPS events
            dsyslog("cMarkAdStandalone::AddMarkVPS(): move mark on position (%d) to VPS event position (%d)", mark->position, vpsFrame);
            // remove marks witch will become invalid after applying VPS event
            switch (type) {
            case MT_START:
                marks.DelFromTo(0, mark->position - 1, MT_ALL, 0xFF);
                break;
            case MT_STOP: {  // delete all marks between stop mark before VPS stop event (included) and current end mark (not included)
                int delStart = vpsFrame;
                const cMark *prevMark = marks.GetPrev(vpsFrame);
                if (prevMark && ((prevMark->type & 0x0F) == MT_STOP)) delStart = prevMark->position;
                marks.DelFromTo(delStart, mark->position - 1, MT_ALL, 0xFF);
            }
            break;
            default:
                esyslog("cMarkAdStandalone::AddMarkVPS(): invalid type 0x%X", type);
            }
            dsyslog("cMarkAdStandalone::AddMarkVPS(): marks after cleanup:");
            DebugMarks();     //  only for debugging
            marks.Move(mark, vpsFrame, index->GetPTSAfterKeyPacketNumber(vpsFrame), (type == MT_START) ? MT_VPSSTART : MT_VPSSTOP);
        }
        else dsyslog("cMarkAdStandalone::AddMarkVPS(): VPS event too far from mark, ignoring");
    }
}


void cMarkAdStandalone::AddMark(sMarkAdMark *mark) {
    if (!mark) return;
    if (mark->type <= MT_UNDEFINED) {
        esyslog("cMarkAdStandalone::AddMark(): mark type 0x%X invalid", mark->type);
        return;
    }
    if (mark->position < 0) {
        char *markType = marks.TypeToText(mark->type);
        esyslog("cMarkAdStandalone::AddMark(): mark packet number (%d) invalid, type 0x%X <%s>", mark->position, mark->type, markType);
        FREE(strlen(markType)+1, "text");
        free(markType);
        return;
    }
    if (mark->framePTS == AV_NOPTS_VALUE) {
        char *markType = marks.TypeToText(mark->type);
        esyslog("cMarkAdStandalone::AddMark(): mark packet number (%d), type 0x%X <%s %s>: no frame frame PTS %" PRId64, mark->position, mark->type, markType, ((mark->type & 0x0F) == MT_START)? "start" : "stop", mark->framePTS);
        FREE(strlen(markType)+1, "text");
        free(markType);
    }

    // set comment of the new mark
    char *comment = nullptr;
    switch (mark->type) {
    case MT_ASSUMEDSTART:
        if (asprintf(&comment, "start assumed (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_ASSUMEDSTOP:
        if (asprintf(&comment, "end   assumed (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_SCENESTART:
        if (asprintf(&comment, "start scene (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_SOUNDSTART:
        if (asprintf(&comment, "end   silence (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_SOUNDSTOP:
        if (asprintf(&comment, "start silence (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_SCENESTOP:
        if (asprintf(&comment, "end   scene (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_NOLOWERBORDERSTART:
        if (asprintf(&comment, "end   lower border (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_NOLOWERBORDERSTOP:
        if (asprintf(&comment, "start lower border (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_NOBLACKSTART:
        if (asprintf(&comment, "end   black screen (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_NOBLACKSTOP:
        if (asprintf(&comment, "start black screen (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_LOGOSTART:
        if (asprintf(&comment, "start logo (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_LOGOSTOP:
        if (asprintf(&comment, "stop  logo (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_HBORDERSTART:
        if (asprintf(&comment, "start horiz. borders (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_HBORDERSTOP:
        if (asprintf(&comment, "stop  horiz. borders (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_VBORDERSTART:
        if (asprintf(&comment, "start vert. borders (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_VBORDERSTOP:
        if (asprintf(&comment, "stop  vert. borders (%d) ", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_ASPECTSTART:
        if ((mark->AspectRatioBefore.num == 0) || (mark->AspectRatioBefore.den == 0)) {
            if (asprintf(&comment, "start recording with aspect ratio %2d:%d (%d)*", mark->AspectRatioAfter.num, mark->AspectRatioAfter.den, mark->position) == -1) comment = nullptr;
            if (comment) {
                ALLOC(strlen(comment)+1, "comment");
            }
        }
        else {
            if (asprintf(&comment, "aspect ratio change from %2d:%d to %2d:%d (%d)*", mark->AspectRatioBefore.num, mark->AspectRatioBefore.den, mark->AspectRatioAfter.num, mark->AspectRatioAfter.den, mark->position) == -1) comment = nullptr;
            if (comment) {
                ALLOC(strlen(comment)+1, "comment");
            }
            if ((macontext.Config->autoLogo > 0) && (mark->position > 0) && (criteria->GetMarkTypeState(MT_LOGOCHANGE) == CRITERIA_DISABLED)) {
                isyslog("AddMark(): frame (%d): aspect ratio change from %2d:%d to %2d:%d, logo detection reenabled", mark->position, mark->AspectRatioBefore.num, mark->AspectRatioBefore.den, mark->AspectRatioAfter.num, mark->AspectRatioAfter.den);
                criteria->SetMarkTypeState(MT_LOGOCHANGE, CRITERIA_UNKNOWN, macontext.Config->fullDecode);
            }
        }
        break;
    case MT_ASPECTSTOP:
        if (asprintf(&comment, "aspect ratio change from %2d:%d to %2d:%d (%d) ", mark->AspectRatioBefore.num, mark->AspectRatioBefore.den, mark->AspectRatioAfter.num, mark->AspectRatioAfter.den, mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        if ((macontext.Config->autoLogo > 0) && (mark->position > 0) && (criteria->GetMarkTypeState(MT_LOGOCHANGE) == CRITERIA_DISABLED)) {
            isyslog("aspect ratio change from %2d:%d to %2d:%d at frame (%d), logo detection reenabled", mark->AspectRatioBefore.num, mark->AspectRatioBefore.den, mark->AspectRatioAfter.num, mark->AspectRatioAfter.den, mark->position);
            criteria->SetMarkTypeState(MT_LOGOCHANGE, CRITERIA_UNKNOWN, macontext.Config->fullDecode);
        }
        break;
    case MT_CHANNELSTART:
        if (asprintf(&comment, "audio channel change from %d to %d (%d)*", mark->channelsBefore, mark->channelsAfter, mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        if ((marks.Count(MT_CHANNELSTART, 0xFF) == 0) && (mark->position > stopA / 2)) {
            dsyslog("AddMark(): first channel start at frame (%d) after half of assumed recording length at frame (%d), this is start mark of next braoscast", mark->position, stopA / 2);
        }
        break;
    case MT_CHANNELSTOP:
        if ((mark->position > packetCheckStart) && (mark->position < stopA * 2 / 3) && (marks.Count(MT_CHANNELSTART, 0xFF) == 0)) {
            dsyslog("cMarkAdStandalone::AddMark(): first audio channel change is after packetCheckStart, disable video decoding detection now");
            // disable all video detection
            video->ClearBorder();
            // use now channel change for detection
            criteria->SetMarkTypeState(MT_CHANNELCHANGE, CRITERIA_USED, macontext.Config->fullDecode);
            if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED) {
                criteria->SetMarkTypeState(MT_HBORDERCHANGE, CRITERIA_AVAILABLE, macontext.Config->fullDecode);
                cMark *hborderStop = marks.GetAround(1 * decoder->GetVideoFrameRate(), mark->position, MT_HBORDERSTOP);
                if (hborderStop) {  // use hborder stop, we have no scene change or black screen to optimize channel stop mark, will result false optimization
                    dsyslog("cMarkAdStandalone::AddMark(): keep hborder stop (%d), ignore channel stop (%d)", hborderStop->position, mark->position);
                    return;
                }
            }
            if (doneCheckStart) marks.DelWeakFromTo(marks.GetFirst()->position, INT_MAX, MT_CHANNELCHANGE); // only if we have selected a start mark
        }
        if (asprintf(&comment, "audio channel change from %d to %d (%d) ", mark->channelsBefore, mark->channelsAfter, mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_RECORDINGSTART:
        if (asprintf(&comment, "start of recording (%d)*", mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    case MT_RECORDINGSTOP:
        if (asprintf(&comment, "stop of recording (%d) ",mark->position) == -1) comment = nullptr;
        if (comment) {
            ALLOC(strlen(comment)+1, "comment");
        }
        break;
    default:
        esyslog("cMarkAdStandalone::AddMark(): unknown mark type 0x%X", mark->type);
        return;
    }

    // add weak marks only to separate marks object
    switch (mark->type & 0xF0) {
    case MT_SCENECHANGE:
        sceneMarks.Add(mark->type, MT_UNDEFINED, MT_UNDEFINED, mark->position, mark->framePTS, nullptr, inBroadCast);
        if (comment) {
#ifdef DEBUG_WEAK_MARKS
            char *indexToHMSF = marks.IndexToHMSF(mark->position, mark->framePTS, false);
            if (indexToHMSF) {
                ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
                if (indexToHMSF) {
                    dsyslog("cMarkAdStandalone::AddMark(): %s PTS %" PRId64 " at %s", comment, mark->framePTS, indexToHMSF);
                    FREE(strlen(indexToHMSF)+1, "indexToHMSF");
                    free(indexToHMSF);
                }
            }
#endif
            FREE(strlen(comment)+1, "comment");
            free(comment);
        }
        break;
    case MT_SOUNDCHANGE:
        silenceMarks.Add(mark->type, MT_UNDEFINED, MT_UNDEFINED, mark->position, mark->framePTS, nullptr, inBroadCast);
        if (comment) {
            char *indexToHMSF = marks.IndexToHMSF(mark->position, mark->framePTS, false);
            if (indexToHMSF) {
                ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
                dsyslog("cMarkAdStandalone::AddMark(): %s PTS %" PRId64 " at %s", comment, mark->framePTS, indexToHMSF);
                FREE(strlen(indexToHMSF)+1, "indexToHMSF");
                free(indexToHMSF);
            }
            FREE(strlen(comment)+1, "comment");
            free(comment);
        }
        break;
    case MT_LOWERBORDERCHANGE:
    case MT_BLACKCHANGE:
        blackMarks.Add(mark->type, MT_UNDEFINED, MT_UNDEFINED, mark->position, mark->framePTS, nullptr, inBroadCast);
        if (comment) {
            char *indexToHMSF = marks.IndexToHMSF(mark->position, mark->framePTS, false);
            if (indexToHMSF) {
                ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
                dsyslog("cMarkAdStandalone::AddMark(): %s PTS %" PRId64 " at %s", comment, mark->framePTS, indexToHMSF);
                FREE(strlen(indexToHMSF)+1, "indexToHMSF");
                free(indexToHMSF);
            }
            FREE(strlen(comment)+1, "comment");
            free(comment);
        }
        break;
    default:
        // set inBroadCast status
        if (!((mark->type <= MT_ASPECTSTART) && (marks.GetPrev(mark->position, MT_CHANNELSTOP) && marks.GetPrev(mark->position, MT_CHANNELSTART)))) { // if there are MT_CHANNELSTOP and MT_CHANNELSTART marks, wait for MT_CHANNELSTART
            if ((mark->type & 0x0F) == MT_START) inBroadCast = true;
            else                                 inBroadCast = false;
        }
        // add mark
        char *indexToHMSF = nullptr;
        if (mark->type == MT_RECORDINGSTART) {
            if (asprintf(&indexToHMSF, "00:00:00.00") == -1) esyslog("cMarkAdStandalone::AddMark(): asprintf failed");  // we have no index to get time for packet number 0
        }
        else indexToHMSF = marks.IndexToHMSF(mark->position, mark->framePTS, false);
        if (indexToHMSF) {
            ALLOC(strlen(indexToHMSF)+1, "indexToHMSF");
            if (comment) isyslog("%s PTS %" PRId64 " at %s", comment, mark->framePTS, indexToHMSF);
            FREE(strlen(indexToHMSF)+1, "indexToHMSF");
            free(indexToHMSF);
        }
        marks.Add(mark->type, MT_UNDEFINED, MT_UNDEFINED, mark->position, mark->framePTS, comment, inBroadCast);
        if (comment) {
            FREE(strlen(comment)+1, "comment");
            free(comment);
        }
        if (doneCheckStart) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);  // save after start mark is valid
    }
}


// save currect content of the frame buffer to /tmp
// if path and suffix is set, this will set as target path and file name suffix
//
#if defined(DEBUG_OVERLAP_FRAME_RANGE)
void cMarkAdStandalone::SaveFrame(const int frame, const char *path, const char *suffix) {
    if (!macontext.Video.Info.height) {
        dsyslog("cMarkAdStandalone::SaveFrame(): macontext.Video.Info.height not set");
        return;
    }
    if (!macontext.Video.Info.width) {
        dsyslog("cMarkAdStandalone::SaveFrame(): macontext.Video.Info.width not set");
        return;
    }
    if (!macontext.Video.Data.valid) {
        dsyslog("cMarkAdStandalone::SaveFrame():  macontext.Video.Data.valid not set");
        return;
    }
    char szFilename[1024];

    for (int plane = 0; plane < PLANES; plane++) {
        int height;
        int width;
        if (plane == 0) {
            height = macontext.Video.Info.height;
            width  = macontext.Video.Info.width;
        }
        else {
            height = macontext.Video.Info.height / 2;
            width  = macontext.Video.Info.width  / 2;
        }
        // set path and file name
        if (path && suffix) sprintf(szFilename, "%s/frame%06d_P%d_%s.pgm", path, frame, plane, suffix);
        else sprintf(szFilename, "/tmp/frame%06dfull_P%d.pgm", frame, plane);
        // Open file
        FILE *pFile = fopen(szFilename, "wb");
        if (pFile == nullptr) {
            dsyslog("cMarkAdStandalone::SaveFrame(): open file %s failed", szFilename);
            return;
        }
        // Write header
        fprintf(pFile, "P5\n%d %d\n255\n", width, height);
        // Write pixel data
        for (int line = 0; line < height; line++) {
            if (fwrite(&macontext.Video.Data.Plane[plane][line * macontext.Video.Data.PlaneLinesize[plane]], 1, width, pFile)) {};
        }
        // Close file
        fclose(pFile);
    }
}
#endif


void cMarkAdStandalone::CheckIndexGrowing()
{
    // Here we check if the index is more
    // advanced than our framecounter.
    // If not we wait. If we wait too much,
    // we discard this check...

#define WAITTIME 15

    if (!indexFile) {
        dsyslog("cMarkAdStandalone::CheckIndexGrowing(): no index file found");
        return;
    }
    if (sleepcnt >= 2) {
        dsyslog("slept too much");
        return; // we already slept too much
    }
    if (decoder) framecnt = decoder->GetPacketNumber();
    bool notenough = true;
    do {
        struct stat statbuf;
        if (stat(indexFile,&statbuf) == -1) {
            return;
        }

        int maxframes = statbuf.st_size / 8;
        if (maxframes < (framecnt + 200)) {
            if ((difftime(time(nullptr), statbuf.st_mtime)) >= WAITTIME) {
                if (length && startTime) {
                    time_t endRecording = startTime + (time_t) length;
                    if (time(nullptr) > endRecording) {
                        // no markad during recording
//                        dsyslog("cMarkAdStandalone::CheckIndexGrowing(): assuming old recording, now > startTime + length");
                        return;
                    }
                    else {
                        sleepcnt = 0;
                        if (!iwaittime) {
                            dsyslog("cMarkAdStandalone::CheckIndexGrowing(): startTime %s length %d", strtok(ctime(&startTime), "\n"), length);
                            dsyslog("cMarkAdStandalone::CheckIndexGrowing(): expected end: %s", strtok(ctime(&endRecording), "\n"));
                            esyslog("recording interrupted, waiting for continuation...");
                        }
                        iwaittime += WAITTIME;
                    }
                }
                else {
                    dsyslog("cMarkAdStandalone::CheckIndexGrowing(): no length and startTime");
                    return;
                }
            }
            unsigned int sleeptime = WAITTIME;
            time_t sleepstart = time(nullptr);
            double slepttime = 0;
            while ((unsigned int)slepttime < sleeptime) {
                while (sleeptime > 0) {
                    macontext.Info.isRunningRecording = true;
                    unsigned int ret = sleep(sleeptime); // now we sleep and hopefully the index will grow
                    if ((errno) && (ret)) {
                        if (abortNow) return;
                        esyslog("got errno %i while waiting for new data", errno);
                        if (errno != EINTR) return;
                    }
                    sleeptime = ret;
                }
                slepttime = difftime(time(nullptr), sleepstart);
                if (slepttime < WAITTIME) {
                    esyslog("what's wrong with your system? we just slept %.0fs", slepttime);
                }
            }
            waittime += static_cast<int> (slepttime);
            sleepcnt++;
            if (sleepcnt >= 2) {
                esyslog("no new data after %is, skipping wait!", waittime);
                notenough = false; // something went wrong?
            }
        }
        else {
            if (iwaittime) {
                esyslog("resuming after %is of interrupted recording, marks can be wrong now!", iwaittime);
            }
            iwaittime = 0;
            sleepcnt = 0;
            notenough = false;
        }
    }
    while (notenough);
    return;
}


#ifdef DEBUG_MARK_FRAMES
void cMarkAdStandalone::DebugMarkFrames() {
    if (!decoder) return;

    StartSection("debug mark pictures");
    dsyslog("cMarkAdStandalone::DebugMarkFrames(): final marks:");
    marks.Debug();

    decoder->Restart();   // restart decoder with first frame
    cMark *mark = marks.GetFirst();
    if (!mark) return;

    // if no fullDecode, check if all marks are on key packet position
    if (!macontext.Config->fullDecode) {
        while (mark) {
            if (abortNow) return;
            if (mark->position != index->GetKeyPacketNumberBefore(mark->position)) esyslog("cMarkAdStandalone::DebugMarkFrames(): mark at (%d) type 0x%X is not a key packet position", mark->position, mark->type);
            mark=mark->Next();
        }
    }

    // read and decode mark video frames
    mark = marks.GetFirst();
    int keyPacketBefore = index->GetKeyPacketNumberBeforePTS(mark->pts - 1);
    if (!macontext.Config->fullDecode) {
        for (int i = 1; i < DEBUG_MARK_FRAMES; i++) keyPacketBefore = index->GetKeyPacketNumberBefore(keyPacketBefore - 1);  // go back DEBUG_MARK_FRAMES key packets
    }
    if (!decoder->SeekToPacket(keyPacketBefore)) {
        esyslog("cMarkAdStandalone::DebugMarkFrames(): seek to packet (%d) failed", keyPacketBefore);
        return;
    }
    int64_t startPTS = mark->pts - (decoder->GetPacketDuration() * DEBUG_MARK_FRAMES);
    if (!macontext.Config->fullDecode) startPTS = decoder->GetPacketPTS();  //  without full decode use PTS of DEBUG_MARK_FRAMES back
    int countAfterStop = 0;

    while (decoder->DecodeNextFrame(false)) {  // no audio
        if (abortNow) return;
        int packetNumber = decoder->GetPacketNumber();
        int64_t framePTS = decoder->GetFramePTS();
        if (framePTS >= startPTS) {
            dsyslog("cMarkAdStandalone::DebugMarkFrames(): mark (%5d) type 0x%X, PTS %" PRId64 ": read packet (%5d), got frame PTS %" PRId64, mark->position, mark->type, mark->pts, packetNumber, framePTS);
            char suffix1[10] = "";
            char suffix2[10] = "";
            if ((mark->type & 0x0F) == MT_START) strcpy(suffix1, "START");
            if ((mark->type & 0x0F) == MT_STOP)  strcpy(suffix1, "STOP");

            if (framePTS < mark->pts) strcpy(suffix2, "BEFORE");
            if ((macontext.Config->fullDecode)  && (framePTS > mark->pts)) strcpy(suffix2, "AFTER");
            if ((!macontext.Config->fullDecode) && (framePTS > mark->pts + 1)) strcpy(suffix2, "AFTER");  // for interlaced stream we will get the picture after the iFrame
            const sVideoPicture *picture = decoder->GetVideoPicture();
            if (picture) {
                char *fileName = nullptr;
                if (asprintf(&fileName,"%s/F__%07d_%s_%s.pgm", macontext.Config->recDir, packetNumber, suffix1, suffix2) >= 1) {
                    ALLOC(strlen(fileName)+1, "fileName");
                    SaveVideoPlane0(fileName, picture);
                    FREE(strlen(fileName)+1, "fileName");
                    free(fileName);
                }
            }
            else dsyslog("cMarkAdStandalone::DebugMarkFrames(): packet (%d): picture not valid", packetNumber);
            if (framePTS > mark->pts) countAfterStop++;
            if (countAfterStop >= DEBUG_MARK_FRAMES) {
                countAfterStop = 0;
                mark = mark->Next();
                if (!mark) break;
                // go to next start mark
                keyPacketBefore = index->GetKeyPacketNumberBeforePTS(mark->pts - 1);
                if (!macontext.Config->fullDecode) {
                    for (int i = 1; i < DEBUG_MARK_FRAMES; i++) keyPacketBefore = index->GetKeyPacketNumberBefore(keyPacketBefore - 1);  // go back DEBUG_MARK_FRAMES key packets
                }
                if (!decoder->SeekToPacket(keyPacketBefore)) {
                    esyslog("cMarkAdStandalone::DebugMarkFrames(): seek to packet (%d) failed", keyPacketBefore);
                    return;
                }
                startPTS = mark->pts - (decoder->GetPacketDuration() * DEBUG_MARK_FRAMES);
                if (!macontext.Config->fullDecode) startPTS = decoder->GetPacketPTS();  //  without full decode use PTS of DEBUG_MARK_FRAMES back
            }
        }
        else decoder->DropFrame();   // clenup frame buffer
    }
    elapsedTime.markPictures = EndSection("debug mark pictures");
}
#endif


void cMarkAdStandalone::MarkadCut() {
    if (!decoder) {
        esyslog("cMarkAdStandalone::MarkadCut(): decoder not set");
        return;
    }
    StartSection("cut");
    dsyslog("cMarkAdStandalone::MarkadCut(): cut video based on marks: fullDecode = %d, fullEncode = %d, ac3ReEncode = %d", macontext.Config->fullDecode, macontext.Config->fullEncode,  macontext.Config->ac3ReEncode);

    if (macontext.Config->fullEncode && !macontext.Config->fullDecode) {
        esyslog("full encode needs full decode, disable full encode");
        macontext.Config->fullEncode = false;
    }

    if (marks.Count() < 2) {
        esyslog("need at least one start mark and one stop mark to cut Video");
        return; // we cannot do much without marks
    }
    dsyslog("cMarkAdStandalone::MarkadCut(): final marks are:");
    DebugMarks();     //  only for debugging

    // init encoder
    int cutMode = CUT_MODE_KEY;
    if (macontext.Config->smartEncode)     cutMode = CUT_MODE_SMART;
    else if (macontext.Config->fullEncode) cutMode = CUT_MODE_FULL;
    cEncoder *encoder = new cEncoder(decoder, index, macontext.Config->recDir, cutMode, macontext.Config->bestEncode, macontext.Config->ac3ReEncode);
    ALLOC(sizeof(*encoder), "encoder");

    int passMin = 0;
    int passMax = 0;
    if (macontext.Config->fullEncode) {  // to full endcode we need 2 pass full encoding
        passMin = 1;
        passMax = 2;
    }

    for (int pass = passMin; pass <= passMax; pass ++) {
        dsyslog("cMarkAdStandalone::MarkadCut(): reset decoder for pass: %d", pass);
        decoder->Restart();
        encoder->Reset(pass);

        // set start and end mark of first part
        cMark *startMark = marks.GetFirst();
        if ((startMark->type & 0x0F) != MT_START) {
            esyslog("got invalid start mark at (%d) type 0x%X", startMark->position, startMark->type);
            FREE(sizeof(*encoder), "encoder");
            delete encoder;  // encoder must be valid here because it is used above
            return;
        }
        cMark *stopMark = startMark->Next();
        if ((stopMark->type & 0x0F) != MT_STOP) {
            esyslog("got invalid stop mark at (%d) type 0x%X", stopMark->position, stopMark->type);
            FREE(sizeof(*encoder), "encoder");
            delete encoder;  // encoder must be valid here because it is used above
            return;
        }
        // open output file
        if (!encoder->OpenFile()) {
            esyslog("failed to open output file");
            FREE(sizeof(*encoder), "encoder");
            delete encoder;
            encoder = nullptr;
            return;
        }

        // cut out all start/stop pairs
        while (true) {
            if (abortNow) return;
            if (!encoder->CutOut(startMark, stopMark)) break;

            // next start/stop pair
            if (stopMark->Next() && stopMark->Next()->Next()) {  // next mark pair
                startMark = stopMark->Next();
                if ((startMark->type & 0x0F) != MT_START) {
                    esyslog("got invalid start mark at (%d) type 0x%X", startMark->position, startMark->type);
                    break;
                }
                stopMark = startMark->Next();
                if ((stopMark->type & 0x0F) != MT_STOP) {
                    esyslog("got invalid stop mark at (%d) type 0x%X", stopMark->position, stopMark->type);
                    break;
                }
            }
            else break;
        }
        // close file to write index in pass 1
        if (!encoder->CloseFile()) {
            esyslog("failed to close output file");
            return;
        }
    }
    FREE(sizeof(*encoder), "encoder");
    delete encoder;  // encoder must be valid here because it is used above
    encoder = nullptr;
    elapsedTime.cut = EndSection("cut");
}


// logo mark optimization
// do it with all mark types, because even with channel marks from a double episode, logo marks can be the only valid end mark type
// - move logo marks before intrudiction logo
// - move logo marks before/after ad in frame
// - remove stop/start from info logo
//
void cMarkAdStandalone::LogoMarkOptimization() {
    if (!decoder)  return;
    if (!index)    return;
    if (!criteria) return;

    if (marks.Count(MT_LOGOCHANGE, 0xF0) == 0) {
        dsyslog("cMarkAdStandalone::LogoMarkOptimization(): no logo marks used");
        return;
    }

    StartSection("mark optimization");
    if (!evaluateLogoStopStartPair) {  // init in RemoveLogoChangeMarks(), but maybe not used
        evaluateLogoStopStartPair = new cEvaluateLogoStopStartPair(decoder, criteria);
        ALLOC(sizeof(*evaluateLogoStopStartPair), "evaluateLogoStopStartPair");
    }
    else evaluateLogoStopStartPair->SetDecoder(decoder);

    // init objects for logo mark optimization
    if (!detectLogoStopStart) {        // init in RemoveLogoChangeMarks(), but maybe not used
        detectLogoStopStart = new cDetectLogoStopStart(decoder, index, criteria, evaluateLogoStopStartPair, video->GetLogoCorner());
        ALLOC(sizeof(*detectLogoStopStart), "detectLogoStopStart");
    }

    decoder->Restart();
    bool save = false;

// check for advertising in frame with logo after logo start mark and before logo stop mark and check for introduction logo
    dsyslog("cMarkAdStandalone::LogoMarkOptimization(): check for advertising in frame with logo after logo start and before logo stop mark and check for introduction logo");
    cMark *markLogo = marks.GetFirst();
    while (markLogo) {
        if (markLogo->type == MT_LOGOSTART) {
            const char *indexToHMSFStartMark = marks.GetTime(markLogo);
            sMarkPos introductionStart = {-1};

            // check for introduction logo before logo mark position
            if (criteria->IsIntroductionLogoChannel()) {
                LogSeparator(false);
                int searchStartPosition = markLogo->position - (30 * decoder->GetVideoFrameRate()); // introduction logos are usually 10s, somettimes longer, changed from 12 to 30
                if (searchStartPosition < 0) searchStartPosition = 0;
                cMark *stopBefore = marks.GetPrev(markLogo->position, MT_STOP, 0x0F);
                if (stopBefore && (stopBefore->position > searchStartPosition)) {
                    dsyslog("cMarkAdStandalone::LogoMarkOptimization(): move start of search from (%d) to stop mark before (%d)", searchStartPosition, stopBefore->position);
                    searchStartPosition = stopBefore->position;
                }

                char *indexToHMSFSearchStart = marks.IndexToHMSF(searchStartPosition, AV_NOPTS_VALUE, false);
                if (indexToHMSFSearchStart) {
                    ALLOC(strlen(indexToHMSFSearchStart)+1, "indexToHMSFSearchStart");
                }

                if (indexToHMSFStartMark && indexToHMSFSearchStart) dsyslog("cMarkAdStandalone::LogoMarkOptimization(): search introduction logo from position (%d) at %s to logo start mark (%d) at %s", searchStartPosition, indexToHMSFSearchStart, markLogo->position, indexToHMSFStartMark);
                if (indexToHMSFSearchStart) {
                    FREE(strlen(indexToHMSFSearchStart)+1, "indexToHMSFSearchStart");
                    free(indexToHMSFSearchStart);
                }
                if (detectLogoStopStart->Detect(searchStartPosition, markLogo->position)) {
                    detectLogoStopStart->IntroductionLogo(searchStartPosition, markLogo->position, &introductionStart);
                }
                if (introductionStart.position >= 0) dsyslog("cMarkAdStandalone::LogoMarkOptimization(): found introduction logo start at (%d)", introductionStart.position);
            }

            // check for advertising in frame with logo after logo start mark position
            LogSeparator(false);
            sMarkPos adInFrameEnd = {-1};
            if (criteria->IsAdInFrameWithLogoChannel() && (evaluateLogoStopStartPair->GetIsAdInFrame(-1, markLogo->position) >= STATUS_UNKNOWN)) {
                int searchEndPosition = markLogo->position + (MAX_AD_IN_FRAME * decoder->GetVideoFrameRate());
                char *indexToHMSFSearchEnd = marks.IndexToHMSF(searchEndPosition, AV_NOPTS_VALUE, false);
                if (indexToHMSFSearchEnd) {
                    ALLOC(strlen(indexToHMSFSearchEnd)+1, "indexToHMSFSearchEnd");
                }
                if (indexToHMSFStartMark && indexToHMSFSearchEnd) dsyslog("cMarkAdStandalone::LogoMarkOptimization(): search advertising in frame with logo after logo start mark (%d) at %s to position (%d) at %s", markLogo->position, indexToHMSFStartMark, searchEndPosition, indexToHMSFSearchEnd);
                if (indexToHMSFSearchEnd) {
                    FREE(strlen(indexToHMSFSearchEnd)+1, "indexToHMSFSearchEnd");
                    free(indexToHMSFSearchEnd);
                }
                if (detectLogoStopStart->Detect(markLogo->position, searchEndPosition)) {
                    detectLogoStopStart->AdInFrameWithLogo(markLogo->position, searchEndPosition, &adInFrameEnd, true, false);
                }
                if (adInFrameEnd.position >= 0) {
                    dsyslog("cMarkAdStandalone::LogoMarkOptimization(): ad in frame between (%d) and (%d) PTS %" PRId64 " found", markLogo->position, adInFrameEnd.position, adInFrameEnd.pts);
                    if (evaluateLogoStopStartPair->IncludesInfoLogo(markLogo->position, adInFrameEnd.position)) {
                        dsyslog("cMarkAdStandalone::LogoMarkOptimization(): deleted info logo part in this range, this could not be a advertising in frame");
                        adInFrameEnd.position = -1;
                    }
                }
                if (adInFrameEnd.position != -1) {  // if we found advertising in frame, use this
                    markLogo = marks.Move(markLogo, adInFrameEnd.position, adInFrameEnd.pts, MT_NOADINFRAMESTART);
                    if (!markLogo) {
                        esyslog("cMarkAdStandalone::LogoMarkOptimization(): move mark failed");
                        break;
                    }
                    save = true;
                }
            }
            if ((adInFrameEnd.position == -1) && (introductionStart.position != -1)) {
                bool move = true;
                // check blackscreen between introduction logo start and logo start, there should be no long blackscreen, short blackscreen are from retrospect
                cMark *blackMarkStart = blackMarks.GetNext(introductionStart.position, MT_NOBLACKSTART);
                cMark *blackMarkStop  = blackMarks.GetNext(introductionStart.position, MT_NOBLACKSTOP);
                if (blackMarkStart && blackMarkStop && (blackMarkStart->position <= markLogo->position) && (blackMarkStop->position <= markLogo->position)) {
                    int innerLength = 1000 * (blackMarkStart->position - blackMarkStop->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::LogoMarkOptimization(): found black screen start (%d) and stop (%d) between introduction logo (%d) and start mark (%d), length %dms", blackMarkStop->position, blackMarkStart->position, introductionStart.position, markLogo->position, innerLength);
                    if (innerLength > 1000) move = false;  // only move if we found no long blackscreen between introduction logo and logo start
                }
                if (move) {
                    markLogo = marks.Move(markLogo, introductionStart.position, introductionStart.pts, MT_INTRODUCTIONSTART);
                }
                if (!markLogo) {
                    esyslog("cMarkAdStandalone::LogoMarkOptimization(): move mark failed");
                    break;
                }
                save = true;
            }
        }
        if ((markLogo->type == MT_LOGOSTOP) && criteria->IsAdInFrameWithLogoChannel()) {
            // check for advertising in frame with logo before logo stop mark position
            LogSeparator(false);
            int searchStartPosition = markLogo->position - (45 * decoder->GetVideoFrameRate()); // advertising in frame are usually 30s, changed from 35 to 45
            // sometimes there is a closing credit in frame with logo before
            const char *indexToHMSFStopMark = marks.GetTime(markLogo);
            char *indexToHMSFSearchPosition = marks.IndexToHMSF(searchStartPosition, AV_NOPTS_VALUE, false);
            if (indexToHMSFSearchPosition) {
                ALLOC(strlen(indexToHMSFSearchPosition)+1, "indexToHMSF");
            }

            if (indexToHMSFStopMark && indexToHMSFSearchPosition) dsyslog("cMarkAdStandalone::LogoMarkOptimization(): search advertising in frame with logo from frame (%d) at %s to logo stop mark (%d) at %s", searchStartPosition, indexToHMSFSearchPosition, markLogo->position, indexToHMSFStopMark);
            if (indexToHMSFSearchPosition) {
                FREE(strlen(indexToHMSFSearchPosition)+1, "indexToHMSF");
                free(indexToHMSFSearchPosition);
            }
            // short start/stop pair can result in overlapping checks
            if (decoder->GetPacketNumber() > searchStartPosition) {
                dsyslog("cMarkAdStandalone::LogoMarkOptimization(): current framenumber (%d) greater than framenumber to seek (%d), restart decoder", decoder->GetPacketNumber(), searchStartPosition);
                decoder->Restart();
            }
            // detect frames
            if ((evaluateLogoStopStartPair->GetIsAdInFrame(markLogo->position, -1) >= STATUS_UNKNOWN) && (detectLogoStopStart->Detect(searchStartPosition, markLogo->position))) {
                bool isEndMark = false;
                if (markLogo->position == marks.GetLast()->position) isEndMark = true;
                sMarkPos newStop = {-1};
                detectLogoStopStart->AdInFrameWithLogo(searchStartPosition, markLogo->position, &newStop, false, isEndMark);
                if (newStop.position >= 0) {
                    dsyslog("cMarkAdStandalone::LogoMarkOptimization(): ad in frame between (%d) PTS %" PRId64 " and (%d) found", newStop.position, newStop.pts, markLogo->position);
                    if (evaluateLogoStopStartPair->IncludesInfoLogo(newStop.position, markLogo->position)) {
                        dsyslog("cMarkAdStandalone::LogoMarkOptimization(): deleted info logo part in this range, this could not be a advertising in frame");
                        newStop.position = -1;
                    }
                }
                if (newStop.position != -1) {
                    evaluateLogoStopStartPair->AddAdInFrame(newStop.position, markLogo->position);  // store info that we found here adinframe
                    markLogo = marks.Move(markLogo, newStop.position, newStop.pts, MT_NOADINFRAMESTOP);
                    if (!markLogo) {
                        esyslog("cMarkAdStandalone::LogoMarkOptimization(): move mark failed");
                        break;
                    }
                    save = true;
                }
            }
        }
        markLogo = markLogo->Next();
    }

    // delete logo mark optimization objects
    FREE(sizeof(*detectLogoStopStart), "detectLogoStopStart");
    delete detectLogoStopStart;
    detectLogoStopStart = nullptr;

    // save marks
    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
    elapsedTime.markOptimization = EndSection("mark optimization");
}


void cMarkAdStandalone::BlackScreenOptimization() {
    bool save = false;
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::BlackScreenOptimization(): start mark optimization with black screen");
    DebugMarks();
    cMark *mark = marks.GetFirst();
#define START_STOP_BLACK (decoder->GetVideoFrameRate() / 2)    // black picture before start and after stop mark
    while (mark) {
        int lengthBefore   = 0;
        int lengthAfter    = 0;
        // optimize start marks
        if ((mark->type & 0x0F) == MT_START) {
            // log available start marks
            bool moved         = false;
            int diffBefore     = INT_MAX;
            int diffAfter      = INT_MAX;
            bool silenceBefore = false;
            bool silenceAfter  = false;
            // stop of black screen is start mark
            cMark *startBlackBefore  = nullptr;
            cMark *startBlackAfter   = nullptr;
            cMark *stopBlackBefore   = blackMarks.GetPrev(mark->position + 1, MT_NOBLACKSTART); // new part starts after the black screen
            cMark *stopBlackAfter    = blackMarks.GetNext(mark->position - 1, MT_NOBLACKSTART); // new part starts after the black screen
            if (stopBlackBefore) {
                startBlackBefore = blackMarks.GetPrev(stopBlackBefore->position, MT_NOBLACKSTOP);
                if (startBlackBefore) {
                    diffBefore   = 1000 * (mark->position            - stopBlackBefore->position)  / decoder->GetVideoFrameRate();
                    lengthBefore = 1000 * (stopBlackBefore->position - startBlackBefore->position) / decoder->GetVideoFrameRate();
                    // check if there is silence around black screen
                    const cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), startBlackBefore->position, MT_SOUNDCHANGE, 0xF0);         // around black screen start
                    if (!silence) silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopBlackBefore->position, MT_SOUNDCHANGE, 0xF0); // around black screen stop
                    if (silence) silenceBefore = true;
                    dsyslog("cMarkAdStandalone::BlackScreenOptimization(): start mark (%6d): found black screen from (%6d) to (%6d), %7dms before -> length %5dms, silence around %d", mark->position, startBlackBefore->position, stopBlackBefore->position, diffBefore, lengthBefore, silenceBefore);
                }
                else stopBlackBefore = nullptr; // no pair, this is invalid
            }
            if (stopBlackAfter) {
                startBlackAfter = blackMarks.GetPrev(stopBlackAfter->position, MT_NOBLACKSTOP);
                if (startBlackAfter) {
                    diffAfter   = 1000 * (stopBlackAfter->position - mark->position)      / decoder->GetVideoFrameRate();
                    lengthAfter = 1000 * (stopBlackAfter->position - startBlackAfter->position) / decoder->GetVideoFrameRate();
                    // check if there is silence around black screen
                    const cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), startBlackAfter->position, MT_SOUNDCHANGE, 0xF0);         // around black screen start
                    if (!silence) silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopBlackAfter->position, MT_SOUNDCHANGE, 0xF0); // around black screen stop
                    if (silence) silenceAfter = true;
                    dsyslog("cMarkAdStandalone::BlackScreenOptimization(): start mark (%6d): found black screen from (%6d) to (%6d), %7dms after  -> length %5dms, silence around %d", mark->position, startBlackAfter->position, stopBlackAfter->position, diffAfter, lengthAfter, silenceAfter);
                }
                else stopBlackAfter = nullptr; // no pair, this is invalid
            }
            // try black screen before start mark
            if (stopBlackBefore) {
                int maxBefore = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    // rule 1: use nearer and longer black screen
                    if ((diffAfter < diffBefore) && (lengthAfter > lengthBefore)) diffBefore = INT_MAX;

                    // rule 2: use very near black screen after
                    else if ((diffAfter <= 1440) && (diffBefore >= 26520)) diffBefore = INT_MAX;

                    maxBefore = 29960;
                    break;
                case MT_LOGOSTART:
                    // rule 1: very short blackscreen with silence after
                    if (silenceAfter && (diffAfter <= 40)) diffBefore = INT_MAX;

                    if (criteria->LogoFadeInOut() & FADE_IN) {
                        if (silenceBefore) {
                            if (lengthBefore      >= 160) maxBefore = 6639;
                            else if (lengthBefore >=  40) maxBefore = 5519;
                        }
                        else if (lengthBefore < 2960) maxBefore = 5360;  // long blackscreen is from closing credits of previous broadcast
                    }
                    else maxBefore = 2999;
                    break;
                case MT_CHANNELSTART:
                    maxBefore = 1240;
                    break;
                case MT_TYPECHANGESTART:
                    maxBefore = 1740;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_INTRODUCTIONSTART:
                        maxBefore = 4960;
                        break;
                    case MT_VPSSTART:
                        // rule 1: for channel with good VPS events use nearer and longer black screen
                        if (criteria->GoodVPS() && (diffAfter < diffBefore) && (lengthAfter > lengthBefore)) diffBefore = INT_MAX;

                        // rule 2: blackscreen with silence before VPS start (preview) and black screen with silence after VPS start (broadcast start)
                        else if (silenceBefore && silenceAfter && (diffBefore >= 26660) && (diffAfter <= 44100)) diffBefore = INT_MAX;

                        // rule 3: not so far blackscreen after is start of broadcast, far before is from closing credits, no silence around
                        else if (!silenceBefore && !silenceAfter && (diffBefore >= 49920) && (diffAfter <= 20960)) diffBefore = INT_MAX;

                        if (criteria->GoodVPS())      maxBefore =  7419;
                        else if (silenceBefore)       maxBefore = 81800;
                        else if (lengthBefore >= 600) maxBefore = 88400;
                        else if (lengthBefore >= 240) maxBefore = 50480;
                        else                          maxBefore =     0;  // do not accept short black screen, too much false positiv
                        break;
                    default:
                        maxBefore = -1;
                    }
                    break;
                default:
                    maxBefore = -1;
                }
                if (diffBefore <= maxBefore) {  // move even to same position to prevent scene change do a move
                    // start broadcast with some black picture, prevent to use closing credits before, changed from 9720 to 7100
                    if ((mark->position == marks.First()->position) && (lengthBefore < 7100))
                        mark = marks.Move(mark, startBlackBefore->position, startBlackBefore->pts, MT_NOBLACKSTART);
                    else mark = marks.Move(mark, stopBlackBefore->position,  stopBlackBefore->pts,  MT_NOBLACKSTART);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try black screen after start mark
            if (!moved && stopBlackAfter) { // move even to same position to prevent scene change do a move
                int maxAfter = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    if (lengthAfter > 40) maxAfter = 64519;
                    else maxAfter = 34159;
                    break;
                case MT_LOGOSTART:
                    if (criteria->LogoFadeInOut() & FADE_IN) maxAfter = 1440;
                    else                                     maxAfter = 2240;
                    break;
                case MT_HBORDERSTART:
                    maxAfter = 260;
                    break;
                case MT_VBORDERSTART:
                    if (lengthAfter >= maxAfter)                   maxAfter =    0;  // prevent to move to end of black screen with text from broadcast start
                    else if (silenceAfter && (lengthAfter >= 240)) maxAfter = 7440;
                    else                                           maxAfter = 6120;
                    break;
                case MT_CHANNELSTART:
                    maxAfter = 4319;   // black sceen after start of broadcast 4320ms (3840)
                    break;
                case MT_ASPECTSTART:
                    maxAfter = 1000;   // valid black screen 1000ms (650)
                    break;
                case MT_TYPECHANGESTART:
                    maxAfter = 1560;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_VPSSTART:
                        if (criteria->GoodVPS())        maxAfter =   7020;
                        else if (silenceAfter)          maxAfter = 149880;
                        else if (lengthAfter >= 3160)   maxAfter = 139240;  // black screen from separator or opening credits
                        else if (lengthAfter >    40)   maxAfter =  20960;
                        else                            maxAfter =      0;  // very short blackscreen are in broadcast
                        break;
                    case MT_INTRODUCTIONSTART:
                        if (lengthAfter >= 3800) maxAfter = 3760;
                        else                     maxAfter = 3199;
                        break;
                    default:
                        maxAfter = -1;
                    }
                    break;
                default:
                    maxAfter = -1;
                }
                if (diffAfter <= maxAfter) {
                    if (mark->position == marks.First()->position) mark = marks.Move(mark, startBlackAfter->position, startBlackAfter->pts, MT_NOBLACKSTART);  // start broadcast with some black picture
                    else                                           mark = marks.Move(mark, stopBlackAfter->position,  stopBlackAfter->pts,  MT_NOBLACKSTART);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        // optimize stop marks
        if ((mark->type & 0x0F) == MT_STOP) {
            // log available stop marks
            bool  moved             = false;
            long int diffBefore     = INT_MAX;
            int   diffAfter         = INT_MAX;
            cMark *stopBlackAfter   = nullptr;
            cMark *startBlackAfter  = blackMarks.GetNext(mark->position - 1, MT_NOBLACKSTOP);
            cMark *blackStartBefore = blackMarks.GetPrev(mark->position + 1, MT_NOBLACKSTOP);
            cMark *blackStopBefore  = nullptr;
            bool  silenceAfter      = false;
            if (blackStartBefore) {
                diffBefore = 1000 * (mark->position - blackStartBefore->position) / decoder->GetVideoFrameRate();
                blackStopBefore = blackMarks.GetNext(blackStartBefore->position, MT_NOBLACKSTART);
                if (blackStopBefore) {
                    lengthBefore = 1000 * (blackStopBefore->position - blackStartBefore->position) / decoder->GetVideoFrameRate();
                    // check if there is silence around black screen
                    bool  silenceBefore = false;
                    const cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), blackStartBefore->position, MT_SOUNDCHANGE, 0xF0);
                    if (silence) silenceBefore = true;
                    dsyslog("cMarkAdStandalone::BlackScreenOptimization(): stop  mark (%6d): found black screen from (%6d) to (%6d), %7ldms before -> length %5dms, silence around %d", mark->position, blackStartBefore->position, blackStopBefore->position, diffBefore, lengthBefore, silenceBefore);
                }
                else blackStartBefore = nullptr; // no pair, this is invalid
            }
            if (startBlackAfter) {
                diffAfter = 1000 * (startBlackAfter->position - mark->position) / decoder->GetVideoFrameRate();
                stopBlackAfter = blackMarks.GetNext(startBlackAfter->position, MT_NOBLACKSTART);
                if (stopBlackAfter) {
                    lengthAfter = 1000 * (stopBlackAfter->position - startBlackAfter->position) / decoder->GetVideoFrameRate();
                    // check if there is silence around black screen
                    const cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), startBlackAfter->position, MT_SOUNDCHANGE, 0xF0);
                    if (silence) silenceAfter = true;
                    dsyslog("cMarkAdStandalone::BlackScreenOptimization(): stop  mark (%6d): found black screen from (%6d) to (%6d), %7dms after  -> length %5dms, silence around %d", mark->position, startBlackAfter->position, stopBlackAfter->position, diffAfter, lengthAfter, silenceAfter);
                }
                else startBlackAfter = nullptr; // no pair, this is invalid
            }

            // try black screen after stop marks
            if (startBlackAfter) {  // move even to same position to prevent scene change for move again
                int maxAfter = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    // rule 1: use short before if after is far away
                    if ((diffBefore <= 7600) && (diffAfter >= 61920)) diffAfter = INT_MAX;

                    // rule 2: use longer and nearer
                    if ((lengthBefore > lengthAfter) && (diffBefore < diffAfter)) diffAfter = INT_MAX;

                    if (lengthAfter >= 120) maxAfter = 280200;
                    else                    maxAfter =  25119;
                    break;
                case MT_LOGOSTOP:
                    // rule 1: black screen before from before logo stop to after logo stop (black screen around logo stop)
                    if ((lengthBefore >= diffBefore)) diffAfter = INT_MAX;

                    // rule 2: long black screen at end of broadcast, short black screen after preview
                    else if ((!(criteria->LogoFadeInOut() & FADE_OUT)) &&
                             (diffBefore <= 4580) && (lengthBefore >= 600) && (diffAfter <= 3240) && (lengthAfter <= 520)) diffAfter = INT_MAX;

                    if (criteria->LogoFadeInOut() & FADE_OUT) {
                        if (lengthAfter > 40) maxAfter = 4960;
                        else                  maxAfter = 3039;
                    }
                    else                      maxAfter = 1399;
                    break;
                case MT_HBORDERSTOP:
                    // rule 1: black screen short before hborder stop is end of closing credits
                    if ((diffBefore <= 360) && (diffAfter >= 7000)) diffAfter = INT_MAX;

                    maxAfter = 0;
                    break;
                case MT_VBORDERSTOP:
                    maxAfter = 480;  // include black closing credits
                    break;
                case MT_CHANNELSTOP:
                    maxAfter = 320;
                    break;
                case MT_TYPECHANGESTOP:
                    maxAfter = 80;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        // rule 1: near black screen before is end mark, far after is from preview or in next broadcast
                        if ((diffBefore <= 6360) && (diffAfter >= 8500)) diffAfter = INT_MAX;

                        if (criteria->GoodVPS())    maxAfter = 12779;
                        else if (silenceAfter)      maxAfter = 82920;
                        else if (lengthAfter >= 80) maxAfter = 54600;
                        else                        maxAfter = 11560;
                        break;
                    case MT_CLOSINGCREDITSSTOP:
                        if (lengthAfter < 6240) maxAfter = 11040;  // long blackscreen after closing credits are opening credits from next broadcast
                        break;
                    case MT_NOADINFRAMESTOP:
                        maxAfter = 3439;
                        break;
                    case MT_TYPECHANGESTOP:
                        maxAfter = 560;   // length 15360ms from closing credits
                        break;
                    default:
                        maxAfter = -1;
                    }
                    break;
                default:
                    maxAfter = -1;
                }
                if (diffAfter <= maxAfter) {  // move even to same position to prevent scene change for move again
                    if (mark->position == marks.GetLast()->position) mark = marks.Move(mark, stopBlackAfter->position,  stopBlackAfter->pts,  MT_NOBLACKSTOP);  // allow some black pictures at end of broadcast
                    else                                             mark = marks.Move(mark, startBlackAfter->position, startBlackAfter->pts, MT_NOBLACKSTOP);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try black screen before stop mark
            if (!moved && blackStartBefore) {  // move even to same position to prevent scene change for move again
                int maxBefore = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    maxBefore = 29120;
                    break;
                case MT_LOGOSTOP:
                    if (criteria->LogoFadeInOut() & FADE_OUT) {
                        if (lengthBefore > diffBefore) maxBefore =   840;   // logo fade around black screen
                        else                           maxBefore =     0;   // never use black screen before fade out logo
                    }
                    else                               maxBefore =  5139;
                    break;
                case MT_HBORDERSTOP:
                    maxBefore = 360;
                    break;
                case MT_VBORDERSTOP:
                    maxBefore = 6840;
                    break;
                case MT_CHANNELSTOP:
                    maxBefore = 2040;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        maxBefore = 6360;
                        break;
                    case MT_CLOSINGCREDITSSTOP:
                        maxBefore = 9519;
                        break;
                    case MT_NOADINFRAMESTOP:
                        if ((mark->position == marks.GetLast()->position) && (lengthBefore > diffBefore)) maxBefore =    -1;  // long black closing credits before ad in frame, keep this
                        else                                                                              maxBefore = 17720;
                        break;
                    default:
                        maxBefore = -1;
                    }
                    break;
                default:
                    maxBefore = -1;
                }
                if (diffBefore <= maxBefore) {
                    if (mark->position == marks.GetLast()->position) mark = marks.Move(mark, blackStopBefore->position,  blackStopBefore->pts,  MT_NOBLACKSTOP);
                    else                                             mark = marks.Move(mark, blackStartBefore->position, blackStartBefore->pts, MT_NOBLACKSTOP);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        mark = mark->Next();
    }
    // save marks
    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
}


void cMarkAdStandalone::LowerBorderOptimization() {
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::LowerBorderOptimization(): start mark optimization with lower black or white border");
    if (criteria->GetMarkTypeState(MT_HBORDERCHANGE) == CRITERIA_USED) {
        dsyslog("cMarkAdStandalone::LowerBorderOptimization(): broadcast has hborder, no optimization with lower border possible");
        return;
    }
    DebugMarks();
    bool save = false;
    cMark *mark = marks.GetFirst();
    while (mark) {
        // only for VPS marks
        if (!((((mark->type & 0xF0) == MT_MOVED) && ((mark->newType & 0xF0) == MT_VPSCHANGE)) ||
                ((mark->type & 0xF0) == MT_ASSUMED))) { // skip mark if not VPS or ASSUMED
            mark = mark->Next();
            continue;
        }

        int lengthBefore   = 0;
        int lengthAfter    = 0;

        // optimize start marks
        if ((mark->type & 0x0F) == MT_START) {
            bool moved = false;
            // get lower border before start mark
            int diffBefore     = INT_MAX;
            cMark *stopBefore  = nullptr;
            cMark *startBefore = blackMarks.GetPrev(mark->position + 1, MT_NOLOWERBORDERSTOP);  // start of lower border before logo start mark
            while (startBefore) {
                stopBefore = blackMarks.GetNext(startBefore->position, MT_NOLOWERBORDERSTART);
                if (!stopBefore) break;
                diffBefore = 1000 * (mark->position - startBefore->position) / decoder->GetVideoFrameRate();
                lengthBefore = 1000 * (stopBefore->position - startBefore->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::LowerBorderOptimization(): start mark (%6d): lower border from (%6d) to (%6d), %7dms before -> length %5dms", mark->position, startBefore->position, stopBefore->position, diffBefore, lengthBefore);
                if ((lengthBefore >= MIN_LOWER_BORDER) && (lengthBefore <= MAX_LOWER_BORDER)) break;
                startBefore = blackMarks.GetPrev(startBefore->position, MT_NOLOWERBORDERSTOP);  // previous start of lower border
            }
            if ((lengthBefore < MIN_LOWER_BORDER) || (lengthBefore > MAX_LOWER_BORDER)) { // we got no valid result
                diffBefore  = INT_MAX;
                startBefore = nullptr;
                stopBefore  = nullptr;
            }
            if (startBefore && stopBefore) {   // we found valid lower border start/stop
                stopBefore = blackMarks.GetNext(startBefore->position, MT_NOLOWERBORDERSTART);  // end   of lower border before logo start mark
                if (stopBefore) {
                    diffBefore = 1000 * (mark->position - startBefore->position) / decoder->GetVideoFrameRate();
                    lengthBefore = 1000 * (stopBefore->position - startBefore->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::LowerBorderOptimization(): start mark (%6d): lower border from (%6d) to (%6d), %7dms before -> length %5dms", mark->position, startBefore->position, stopBefore->position, diffBefore, lengthBefore);
                }
                else startBefore = nullptr; // no pair, this is invalid
            }
            if (startBefore && stopBefore) {   // we found valid lower border start/stop
                cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopBefore->position, MT_SOUNDCHANGE, 0xF0);  // around lower border stop
                if (silence) dsyslog("cMarkAdStandalone::LowerBorderOptimization(): silence found (%d) around lower border from (%d) to (%d)", silence->position, startBefore->position, stopBefore->position);
            }

            // get lower border after start mark
            cMark *startAfter = blackMarks.GetNext(mark->position - 1, MT_NOLOWERBORDERSTOP);  // start of lower border after logo start mark
            int diffAfter     = INT_MAX;
            cMark *stopAfter  = nullptr;
            while (startAfter) { // get first long lower border start/stop after start mark
                stopAfter = blackMarks.GetNext(startAfter->position, MT_NOLOWERBORDERSTART);  // get end of lower border
                if (!stopAfter) break;
                diffAfter   = 1000 * (startAfter->position - mark->position)       / decoder->GetVideoFrameRate();
                lengthAfter = 1000 * (stopAfter->position  - startAfter->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::LowerBorderOptimization(): start mark (%6d): lower border from (%6d) to (%6d), %7dms after  -> length %5dms", mark->position, startAfter->position, stopAfter->position, diffAfter, lengthAfter);
                if ((lengthAfter >= MIN_LOWER_BORDER) && (lengthAfter <= MAX_LOWER_BORDER)) break;
                startAfter = blackMarks.GetNext(startAfter->position, MT_NOLOWERBORDERSTOP);  // next start of lower border
            }
            if ((lengthAfter < MIN_LOWER_BORDER) || (lengthAfter > MAX_LOWER_BORDER)) { // we got no valid result
                startAfter = nullptr;
                stopAfter  = nullptr;
            }
            if (startAfter && stopAfter) {   // we found valid lower border start/stop
                cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopAfter->position, MT_SOUNDCHANGE, 0xF0);  // around lower border stop
                if (silence) dsyslog("cMarkAdStandalone::LowerBorderOptimization(): silence found (%d) around lower border from (%d) to (%d)", silence->position, startAfter->position, stopAfter->position);
            }

            // try lower border before start mark
            if (startBefore) {
                int maxBefore = -1;
                switch (mark->type) {
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_VPSSTART:
                        if ((lengthBefore >= MIN_LOWER_BORDER) && (lengthBefore <= MAX_LOWER_BORDER)) {
                            if (criteria->GoodVPS()) maxBefore =   7859;
                            else                     maxBefore = 230039;
                        }
                        break;
                    default:
                        maxBefore = -1;
                    }
                    break;
                default:
                    maxBefore = -1;
                }
                if (diffBefore <= maxBefore) {
                    mark = marks.Move(mark, stopBefore->position, stopBefore->pts, MT_NOLOWERBORDERSTART);  // move to end of lower border (closing credits)
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // lower border after start mark
            if (!moved && stopAfter) {
                int maxAfter = -1;
                switch (mark->type) {
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_VPSSTART:
                        if (criteria->GoodVPS()) maxAfter =  16639;
                        else                     maxAfter = 302800;
                        break;
                    default:
                        maxAfter = -1;
                    }
                    break;
                default:
                    maxAfter = -1;
                }
                if (diffAfter <= maxAfter) {
                    mark = marks.Move(mark, stopAfter->position, stopAfter->pts, MT_NOLOWERBORDERSTART);  // use end of lower closing credits
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            if (!moved) dsyslog("cMarkAdStandalone::LowerBorderOptimization(): no matching lower border found");
        }
        // optimize stop marks
        if ((mark->type & 0x0F) == MT_STOP) {
            bool moved = false;

            // get lower border before stop mark
            long int diffBefore = LONG_MAX;
            cMark *startBefore  = blackMarks.GetPrev(mark->position + 1, MT_NOLOWERBORDERSTOP);
            cMark *stopBefore   = nullptr;
            bool silenceBefore  = false;
            while (startBefore) {
                stopBefore = blackMarks.GetNext(startBefore->position, MT_NOLOWERBORDERSTART);
                if (!stopBefore) break;
                diffBefore = 1000 * (mark->position - startBefore->position) / decoder->GetVideoFrameRate();
                lengthBefore = 1000 * (stopBefore->position - startBefore->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::LowerBorderOptimization(): stop  mark (%6d): lower border from (%6d) to (%6d), %7ldms before -> length %5dms", mark->position, startBefore->position, stopBefore->position, diffBefore, lengthBefore);
                if ((lengthBefore >= MIN_LOWER_BORDER) && (lengthBefore <= MAX_LOWER_BORDER)) break;
                startBefore = blackMarks.GetPrev(startBefore->position, MT_NOLOWERBORDERSTOP);  // previous start of lower border
            }
            if ((lengthBefore < MIN_LOWER_BORDER) || (lengthBefore > MAX_LOWER_BORDER)) { // we got no valid result
                diffBefore  = LONG_MAX;
                startBefore = nullptr;
                stopBefore  = nullptr;
            }
            if (startBefore && stopBefore) {   // we found valid lower border start/stop
                cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopBefore->position, MT_SOUNDCHANGE, 0xF0);  // around lower border stop
                if (silence) {
                    dsyslog("cMarkAdStandalone::LowerBorderOptimization(): silence found (%d) around lower border from (%d) to (%d)", silence->position, startBefore->position, stopBefore->position);
                    silenceBefore = true;
                }
            }

            // get lower border after stop mark
            int diffAfter     = INT_MAX;
            cMark *startAfter = blackMarks.GetNext(mark->position - 1, MT_NOLOWERBORDERSTOP);
            cMark *stopAfter  = nullptr;
            while (startAfter) { // get first long lower border start/stop after start mark
                stopAfter = blackMarks.GetNext(startAfter->position, MT_NOLOWERBORDERSTART);  // get end of lower border
                if (!stopAfter) break;
                diffAfter   = 1000 * (startAfter->position - mark->position)       / decoder->GetVideoFrameRate();
                lengthAfter = 1000 * (stopAfter->position  - startAfter->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::LowerBorderOptimization(): stop  mark (%6d): lower border from (%6d) to (%6d), %7dms after  -> length %5dms", mark->position, startAfter->position, stopAfter->position, diffAfter, lengthAfter);
                if ((lengthAfter >= MIN_LOWER_BORDER) && (lengthAfter <= MAX_LOWER_BORDER)) break;
                startAfter = blackMarks.GetNext(startAfter->position, MT_NOLOWERBORDERSTOP);  // next start of lower border
            }
            if ((lengthAfter < MIN_LOWER_BORDER) || (lengthAfter > MAX_LOWER_BORDER)) { // we got no valid result
                diffAfter  = INT_MAX;
                startAfter = nullptr;
                stopAfter  = nullptr;
            }
            if (startAfter && stopAfter) {   // we found valid lower border start/stop
                cMark *silence = silenceMarks.GetAround(decoder->GetVideoFrameRate(), stopAfter->position, MT_SOUNDCHANGE, 0xF0);  // around lower border stop
                if (silence) dsyslog("cMarkAdStandalone::LowerBorderOptimization(): silence found (%d) around lower border from (%d) to (%d)", silence->position, startAfter->position, stopAfter->position);
            }

            // try lower border after stop marks
            if (stopAfter) {
                int maxAfter = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    maxAfter = 68200;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        if (criteria->GoodVPS()) maxAfter =  71519;
                        else                     maxAfter = 257760;
                        break;
                    default:
                        maxAfter = -1;
                    }
                    break;
                default:
                    maxAfter = -1;
                }
                if (diffAfter <= maxAfter) {  // move even to same position to prevent scene change for move again
                    mark = marks.Move(mark, stopAfter->position, stopAfter->pts, MT_NOLOWERBORDERSTOP);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }

            // try lower border before stop mark
            if (!moved && stopBefore) {
                int maxBefore = -1;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    if (silenceBefore) maxBefore = 218520;
                    else               maxBefore =  98599;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        if (criteria->GoodVPS())                                                             maxBefore =   6579;
                        else if ((diffAfter == INT_MAX) && (lengthBefore >= 1920) && (lengthBefore <= 4920)) maxBefore = 198960;  // no lower border after, typical length
                        else                                                                                 maxBefore =  60559;
                        break;
                    default:
                        maxBefore = -1;
                    }
                    break;
                default:
                    maxBefore = -1;
                }
                if (diffBefore <= maxBefore) {
                    mark = marks.Move(mark, stopBefore->position, stopBefore->pts, MT_NOLOWERBORDERSTOP);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            if (!moved) dsyslog("cMarkAdStandalone::LowerBorderOptimization(): no matching lower border found");
        }
        mark = mark->Next();
    }
    // save marks
    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
}


void cMarkAdStandalone::SilenceOptimization() {
    LogSeparator(true);
    bool save = false;
    dsyslog("cMarkAdStandalone::SilenceOptimization(): start mark optimization with silence scenes");
    DebugMarks();
    cMark *mark = marks.GetFirst();
    while (mark) {
        // optimize start marks
        if ((mark->type & 0x0F) == MT_START) {
            // log available marks
            bool moved = false;
            int diffBefore   = INT_MAX;
            int diffAfter    = INT_MAX;
            int lengthBefore = 0;
            int lengthAfter  = 0;
            bool blackBefore = false;
            bool blackAfter  = false;
            cMark *soundStartBefore = silenceMarks.GetPrev(mark->position + 1, MT_SOUNDSTART);
            cMark *soundStopBefore  = nullptr;
            cMark *soundStartAfter  = silenceMarks.GetNext(mark->position - 1, MT_SOUNDSTART);
            cMark *soundStopAfter   = nullptr;
            if (soundStartBefore) {
                diffBefore = 1000 * (mark->position - soundStartBefore->position) / decoder->GetVideoFrameRate();
                soundStopBefore = silenceMarks.GetPrev(soundStartBefore->position, MT_SOUNDSTOP);
                if (soundStopBefore) {
                    lengthBefore = 1000 * (soundStartBefore->position - soundStopBefore->position) / decoder->GetVideoFrameRate();
                    const cMark *black = blackMarks.GetAround(decoder->GetVideoFrameRate(), soundStopBefore->position, MT_BLACKCHANGE, 0xF0);
                    if (black) blackBefore = true;
                    dsyslog("cMarkAdStandalone::SilenceOptimization(): start mark (%6d): silence from (%6d) %10" PRId64 " to (%6d) %10" PRId64 ", %8dms before, length %4dms, black %d", mark->position, soundStopBefore->position, soundStopBefore->pts, soundStartBefore->position, soundStartBefore->pts, diffBefore, lengthBefore, blackBefore);
                }
            }
            if (soundStartAfter) {
                diffAfter = 1000 * (soundStartAfter->position - mark->position) / decoder->GetVideoFrameRate();
                soundStopAfter  = silenceMarks.GetPrev(soundStartAfter->position, MT_SOUNDSTOP);
                if (soundStopAfter) {
                    lengthAfter = 1000 * (soundStartAfter->position - soundStopAfter->position) / decoder->GetVideoFrameRate();
                    const cMark *black = blackMarks.GetAround(decoder->GetVideoFrameRate(), soundStopAfter->position, MT_BLACKCHANGE, 0xF0);
                    if (black) blackAfter = true;
                    dsyslog("cMarkAdStandalone::SilenceOptimization(): start mark (%6d): silence from (%6d) %10" PRId64 " to (%6d) %10" PRId64 ", %8dms after,  length %4dms, black %d", mark->position, soundStopAfter->position, soundStopAfter->pts, soundStartAfter->position, soundStartAfter->pts, diffAfter, lengthAfter, blackAfter);
                }
            }
            // check if new position can be valid
            if (soundStartAfter) {
                cMark *nextStop = marks.GetNext(soundStartAfter->position, MT_STOP, 0x0F);
                if (nextStop) {
                    int diff = (nextStop->position - soundStartAfter->position) / decoder->GetVideoFrameRate();
                    if (diff < 60) { // min length broadcast after move to silence
                        dsyslog("cMarkAdStandalone::SilenceOptimization(): start mark (%6d): silence after (%d) is only %ds before next stop mark (%d), ignore invalid", mark->position, soundStartAfter->position, diff, nextStop->position);
                        soundStartAfter = nullptr;
                    }
                }
            }
            if (soundStartBefore && (soundStartBefore->position != mark->position)) { // do not move to same frame
                int maxBefore = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    maxBefore = 178080;
                    break;
                case MT_NOLOWERBORDERSTART:
                    maxBefore = 82980;
                    break;
                case MT_LOGOSTART:
                    // rule 1: logo start is in ad, silence before and after ad
                    if ((diffBefore >=  3140) && (diffAfter <=  3740)) diffBefore = INT_MAX;

                    if (criteria->LogoFadeInOut() & FADE_IN) {
                        if ((lengthBefore >= 880) && blackBefore) maxBefore = 10720;
                        else if (lengthBefore > 120)              maxBefore =  5399;
                        else                                      maxBefore =  3799;
                    }
                    else                                          maxBefore =  2480;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_VPSSTART:
                        if (criteria->GoodVPS()) maxBefore =  24399;
                        else                     maxBefore = 136559;
                        break;
                    case MT_INTRODUCTIONSTART:
                        maxBefore = 2040;
                        break;
                    default:
                        maxBefore = 0;
                    }
                    break;
                default:
                    maxBefore = 0;
                }
                if (diffBefore <= maxBefore) {
                    mark = marks.Move(mark, soundStartBefore->position, soundStartBefore->pts, MT_SOUNDSTART);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try silence after start position
            if (!moved && soundStartAfter) {
                int maxAfter = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    maxAfter = 227620;
                    break;
                case MT_LOGOSTART:
                    maxAfter = 720;    // silence after logo start only if logo start before broadcast start
                    break;
                case MT_VBORDERSTART:
                    if (mark->position == marks.GetFirst()->position) maxAfter = 359;
                    else                                              maxAfter =   0;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_VPSSTART:
                        if (criteria->GoodVPS())    maxAfter = 116959;
                        else if (blackAfter)        maxAfter = 233440;
                        else if (lengthAfter > 120) maxAfter = 141839;
                        else                        maxAfter =  17019;
                        break;
                    default:
                        maxAfter = 0;
                    }
                    break;
                default:
                    maxAfter = 0;
                }
                if ((diffAfter <= maxAfter) && (soundStartAfter->position != mark->position)) {
                    mark = marks.Move(mark, soundStartAfter->position, soundStartAfter->pts, MT_SOUNDSTART);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        // optimize stop marks
        if ((mark->type & 0x0F) == MT_STOP) {
            // log available marks
            bool moved = false;
            long int diffBefore     = INT_MAX;
            int diffAfter           = INT_MAX;
            cMark *soundStopBefore  = silenceMarks.GetPrev(mark->position + 1, MT_SOUNDSTOP);  // try after stop mark for fading out logo in broadcast
            cMark *soundStartBefore = nullptr;
            cMark *soundStopAfter   = silenceMarks.GetNext(mark->position - 1, MT_SOUNDSTOP);  // try after stop mark for fading out logo in broadcast
            cMark *soundStartAfter  = nullptr;
            int lengthBefore        = 0;
            int lengthAfter         = 0;
            if (soundStopBefore) {
                diffBefore = 1000 * (mark->position - soundStopBefore->position) / decoder->GetVideoFrameRate();
                soundStartBefore = silenceMarks.GetNext(soundStopBefore->position, MT_SOUNDSTART);
                if (soundStartBefore) {
                    bool blackBefore = false;
                    const cMark *black = blackMarks.GetAround(decoder->GetVideoFrameRate(), soundStopBefore->position, MT_BLACKCHANGE, 0xF0);
                    if (black) blackBefore = true;
                    lengthBefore = 1000 * (soundStartBefore->position - soundStopBefore->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::SilenceOptimization(): stop  mark (%6d): silence from (%6d) %10" PRId64 " to (%6d) %10" PRId64 ", %8ldms before, length %4dms, black %d", mark->position, soundStopBefore->position, soundStopBefore->pts, soundStartBefore->position, soundStartBefore->pts, diffBefore, lengthBefore, blackBefore);
                }
            }
            if (soundStopAfter) {
                diffAfter = 1000 * (soundStopAfter->position - mark->position) / decoder->GetVideoFrameRate();
                soundStartAfter = silenceMarks.GetNext(soundStopAfter->position, MT_SOUNDSTART);
                if (soundStartAfter) {
                    bool blackAfter = false;
                    const cMark *black = blackMarks.GetAround(decoder->GetVideoFrameRate(), soundStopAfter->position, MT_BLACKCHANGE, 0xF0);
                    if (black) blackAfter = true;
                    lengthAfter = 1000 * (soundStartAfter->position - soundStopAfter->position) / decoder->GetVideoFrameRate();
                    dsyslog("cMarkAdStandalone::SilenceOptimization(): stop  mark (%6d): silence from (%6d) %10" PRId64 " to (%6d) %10" PRId64 ", %8dms after,  length %4dms, black %d", mark->position, soundStopAfter->position, soundStopAfter->pts, soundStartAfter->position, soundStartAfter->pts, diffAfter, lengthAfter, blackAfter);
                }
            }
            // try silence after stop mark
            if (soundStopAfter && (soundStopAfter->position != mark->position)) {
                int maxAfter = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    maxAfter = 135719;  // silence in next broadcast after 135720ms
                    break;
                case MT_LOGOSTOP:
                    if (criteria->LogoFadeInOut() & FADE_OUT) maxAfter = 2360;
                    else                                      maxAfter = 1079;
                    break;
                case MT_VBORDERSTOP:
                    maxAfter = 0;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        if (criteria->GoodVPS())      maxAfter = 13479;
                        else if (lengthAfter >= 3000) maxAfter = 81920;
                        else if (lengthAfter >    40) maxAfter = 49959;   // ignore very short silence
                        break;
                    default:
                        maxAfter = 0;
                    }
                    break;
                default:
                    maxAfter = 0;
                }
                if (diffAfter <= maxAfter) {
                    mark = marks.Move(mark, soundStopAfter->position, soundStopAfter->pts, MT_SOUNDSTOP);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try silence before stop mark
            if (!moved && soundStopBefore) {
                int maxBefore = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    maxBefore = 173639;
                    break;
                case MT_LOGOSTOP:
                    if (criteria->LogoFadeInOut() & FADE_OUT) maxBefore =  520;
                    else if (lengthBefore > 40)               maxBefore = 4440;
                    else                                      maxBefore = 3439;
                    break;
                case MT_CHANNELSTOP:
                    maxBefore = 1100;
                    break;
                case MT_VBORDERSTOP:
                    maxBefore = 0;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_VPSSTOP:
                        if (criteria->GoodVPS()) maxBefore = 19980;
                        else                     maxBefore = 57280;
                        break;
                    case MT_TYPECHANGESTOP:
                        maxBefore = 6200;
                        break;
                    default:
                        maxBefore = 0;
                    }
                    break;
                default:
                    maxBefore = 0;
                }
                if ((diffBefore <= maxBefore) && (soundStopBefore->position != mark->position)) {
                    mark = marks.Move(mark, soundStopBefore->position, soundStopBefore->pts, MT_SOUNDSTOP);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        mark = mark->Next();
    }
// save marks
    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
}


void cMarkAdStandalone::SceneChangeOptimization() {
    bool save = false;
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::SceneChangeOptimization(): start mark optimization with scene changes");
    DebugMarks();
    cMark *mark = marks.GetFirst();
    while (mark) {
        // check start mark
        if ((mark->type & 0x0F) == MT_START) {
            // log available marks
            bool moved     = false;
            int diffAfter  = INT_MAX;
            int diffBefore = INT_MAX;
            cMark *sceneStartBefore = nullptr;
            cMark *sceneStartAfter  = nullptr;
            if (mark->type == MT_ASPECTSTART) {   // change of aspect ratio results in a scene change, but they are sometimes a few frames to early
                sceneStartBefore = sceneMarks.GetPrev(mark->position, MT_SCENESTART);      // do not allow one to get same position
                sceneStartAfter  = sceneMarks.GetNext(mark->position, MT_SCENESTART);      // do not allow one to get same position
            }
            else {
                sceneStartBefore = sceneMarks.GetPrev(mark->position + 1, MT_SCENESTART);  // allow one to get same position
                sceneStartAfter  = sceneMarks.GetNext(mark->position - 1, MT_SCENESTART);  // allow one to get same position
            }

            if (sceneStartBefore) {
                diffBefore = 1000 * (mark->position - sceneStartBefore->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::SceneChangeOptimization(): start mark (%6d): found scene start (%6d) %5dms before", mark->position, sceneStartBefore->position, diffBefore);
            }
            if (sceneStartAfter) {
                diffAfter = 1000 * (sceneStartAfter->position - mark->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::SceneChangeOptimization(): start mark (%6d): found scene start (%6d) %5dms after", mark->position, sceneStartAfter->position, diffAfter);
            }
            // try scene change before start mark
            if (sceneStartBefore && (sceneStartBefore->position != mark->position)) {
                int maxBefore = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    maxBefore = 7920;
                    break;
                case MT_LOGOSTART:
                    // rule 1: logo start very short before broadcast start
                    if (!(criteria->LogoFadeInOut() & FADE_IN) && (diffBefore >= 320) && (diffAfter <= 120)) diffBefore = INT_MAX;

                    // rule 2: scene start very short after logo start in old recording without fade in of fade in channel
                    else if ((criteria->LogoFadeInOut() & FADE_IN) && (diffBefore >= 1700) && (diffAfter <= 20)) diffBefore = INT_MAX;

                    if (criteria->LogoFadeInOut() & FADE_IN) maxBefore = 6059;
                    else                                     maxBefore = 1599;
                    break;
                case MT_CHANNELSTART:
                    if (diffAfter <= 460) diffBefore = INT_MAX;
                    maxBefore = 1060;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_SOUNDSTART:
                        // rule 1: prefer scene change short after silence
                        if (diffAfter <= 120) diffBefore = INT_MAX;

                        // rule 2: silence in closing scene
                        else if ((diffBefore >= 22200) && (diffAfter >= 7120)) diffBefore = INT_MAX;

                        maxBefore = 1180;
                        break;
                    case MT_VPSSTART:
                        // rule 1: use nearest scene change to VPS event
                        if (diffAfter < diffBefore) diffBefore = INT_MAX;

                        maxBefore = 3340;
                        break;
                    case MT_INTRODUCTIONSTART:
                        maxBefore = 4880;  // changed from 3799 to 4880
                        break;
                    default:
                        maxBefore = 0;
                    }
                    break;
                default:
                    maxBefore = 0;
                }
                if (diffBefore <= maxBefore) {
                    mark = marks.Move(mark, sceneStartBefore->position, sceneStartBefore->pts, MT_SCENESTART);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try scene change after start mark
            if (!moved && sceneStartAfter) {
                int maxAfter = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTART:
                    maxAfter = 9160;  // changed from 1760 to 9160
                    break;
                case MT_LOGOSTART:
                    if (criteria->LogoFadeInOut() & FADE_IN) maxAfter =   0; // with fade in logo, scene after is always false
                    else if (diffBefore >= 45400)            maxAfter = 460; // logo start before broadcast start, long scene before
                    else                                     maxAfter =  80; // some channels starts logo short before broadcast
                    break;
                case MT_ASPECTSTART:
                    maxAfter = 400;
                    break;
                case MT_CHANNELSTART:
                    maxAfter = 1800;
                    break;
                case MT_TYPECHANGESTART:
                    maxAfter =   40;
                    break;
                case MT_MOVEDSTART:
                    switch (mark->newType) {
                    case MT_SOUNDSTART:
                        maxAfter = 4619;
                        break;
                    case MT_NOLOWERBORDERSTART:
                        maxAfter = 5000;
                        break;
                    case MT_NOADINFRAMESTART:
                        maxAfter = 40;
                        break;
                    case MT_VPSSTART:
                        maxAfter = 13440;
                        break;
                    case MT_INTRODUCTIONSTART:
                        maxAfter = 1200;
                        break;
                    default:
                        maxAfter = 0;
                    }
                    break;
                default:
                    maxAfter = 0;
                }
                if ((diffAfter <= maxAfter) && (sceneStartAfter->position != mark->position)) {
                    mark = marks.Move(mark, sceneStartAfter->position, sceneStartAfter->pts, MT_SCENESTART);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        // check stop mark
        if ((mark->type & 0x0F) == MT_STOP) {
            // log available marks
            bool moved          = false;
            int diffAfter       = INT_MAX;
            long int diffBefore = INT_MAX;
            cMark *sceneStopBefore = sceneMarks.GetPrev(mark->position + 1, MT_SCENESTOP);
            cMark *sceneStopAfter  = sceneMarks.GetNext(mark->position - 1, MT_SCENESTOP);  // allow one to get same position
            if (sceneStopBefore) {
                diffBefore = 1000 * (mark->position - sceneStopBefore->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::SceneChangeOptimization(): stop  mark (%6d): found scene stop  (%6d) %5ldms before", mark->position, sceneStopBefore->position, diffBefore);
            }
            if (sceneStopAfter) {
                diffAfter = 1000 * (sceneStopAfter->position - mark->position) / decoder->GetVideoFrameRate();
                dsyslog("cMarkAdStandalone::SceneChangeOptimization(): stop  mark (%6d): found scene stop  (%6d) %5dms after", mark->position, sceneStopAfter->position, diffAfter);
            }
            // try scene change after stop mark
            if ((sceneStopAfter) && (sceneStopAfter->position != mark->position)) {
                int maxAfter = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    if ((diffBefore <= 760) && (diffAfter >= 1120)) diffAfter = INT_MAX;
                    maxAfter = 8520;  // changed from 6480 to 8520
                    break;
                case MT_LOGOSTOP:
                    // rule 1: if no fade out logo, usually we have delayed logo stop from detection fault (bright picture or patten in background)
                    // very near scene change can be valid from very short fade out logo
                    if (!(criteria->LogoFadeInOut() & FADE_OUT) && ((diffBefore <= 1720) || (diffAfter >= 760))) diffAfter = INT_MAX;

                    // rule 2: very short scene change before logo stop is valid
                    if (diffBefore <= 40) diffAfter = INT_MAX;

                    maxAfter = 4800;
                    break;
                case MT_HBORDERSTOP:
                    if ((diffBefore <= 440) && (diffAfter >= 1720)) diffAfter = INT_MAX;
                    maxAfter = 799;
                    break;
                case MT_VBORDERSTOP:
                    if (diffBefore > 20) maxAfter = 20720;
                    break;
                case MT_CHANNELSTOP:
                    // rule 1:
                    if ((diffBefore >= 80) && (diffBefore <= 640) && (diffAfter >= 160) && (diffAfter <= 1460)) diffAfter = INT_MAX;

                    // rule 2:
                    else if (diffBefore <= 40) diffAfter = INT_MAX;

                    maxAfter = 919;
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_NOLOWERBORDERSTOP:   // move after closing credits
                        // rule 1: scene change very short before and long after
                        if ((diffBefore <= 80) && (diffAfter >= 880)) diffAfter = INT_MAX;
                        maxAfter = 2520;
                        break;
                    case MT_SOUNDSTOP:
                        // rule1: use scene change short before silence (silence is at start of ad / separator / closing credits)
                        if (diffBefore <= 160) diffAfter = INT_MAX;

                        maxAfter = 360;
                        break;
                    case MT_VPSSTOP:
                        // rule 1: use nearest scene change for channel with good VPS
                        if (criteria->GoodVPS() && (diffBefore < diffAfter))  diffAfter = INT_MAX;

                        // rule 2: scene change short before
                        if ((diffBefore >= 160) && (diffBefore <= 480) && (diffAfter >= 680) && (diffAfter <= 3840)) diffAfter = INT_MAX;

                        // rule 3: long opening scene from next broadcast
                        else if ((diffBefore <= 80) && (diffAfter >= 6720)) diffAfter = INT_MAX;   // long opening scene from next broadcast

                        maxAfter = 17480;  // changed from 11200 to 17480
                        break;
                    case MT_CLOSINGCREDITSSTOP:
                        maxAfter = 1080;
                        break;
                    case MT_NOADINFRAMESTOP:
                        maxAfter = 0;
                        break;
                    default:
                        maxAfter = 0;
                    }
                    break;
                default:
                    maxAfter = 0;
                }
                if (diffAfter <= maxAfter) {  // logo is fading out before end of broadcast scene, move forward
                    mark = marks.Move(mark, sceneStopAfter->position, sceneStopAfter->pts, MT_SCENESTOP);
                    if (mark) {
                        moved = true;
                        save  = true;
                    }
                    else break;
                }
            }
            // try scene change before stop mark
            if (!moved && (sceneStopBefore) && (sceneStopBefore->position != mark->position)) { // logo stop detected too late, move backwards
                int maxBefore = 0;
                switch (mark->type) {
                case MT_ASSUMEDSTOP:
                    maxBefore = 12840;
                    break;
                case MT_LOGOSTOP:
                    if (!(criteria->LogoFadeInOut() & FADE_OUT)) maxBefore = 3000;
                    break;
                case MT_HBORDERSTOP:
                    if (mark->position == marks.GetLast()->position) maxBefore = 440;
                    break;
                case MT_ASPECTSTOP:
                    maxBefore = 200;
                    break;
                case MT_CHANNELSTOP:
                    maxBefore = 640;  // changed from 600 to 640
                    break;
                case MT_MOVEDSTOP:
                    switch (mark->newType) {
                    case MT_NOLOWERBORDERSTOP:
                        maxBefore = 160;
                        break;
                    case MT_SOUNDSTOP:
                        maxBefore = 579;
                        break;
                    case MT_VPSSTOP:
                        maxBefore = 8440;   // chaned from 1320 to 1440 to 8440
                        break;
                    case MT_NOADINFRAMESTOP:  // correct the missed start of ad in frame before stop mark
                        maxBefore = 440;
                        break;
                    default:
                        maxBefore = 0;
                    }
                    break;
                default:
                    maxBefore = 0;
                }
                if (diffBefore <= maxBefore) {
                    mark = marks.Move(mark, sceneStopBefore->position, sceneStopBefore->pts, MT_SCENESTOP);
                    if (mark) save = true;
                    else break;
                }
            }
        }
        mark = mark->Next();
    }
    // save marks
    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
}


void cMarkAdStandalone::ProcessOverlap() {
    if (abortNow)      return;
    if (duplicate)     return;
    if (!decoder)      return;
    if ((length == 0) || (startTime == 0)) {  // no recording length or start time from info file
        return;
    }
    StartSection("overlap detection");
    bool save = false;

    // overlap detection
    DebugMarks();     //  only for debugging
    cOverlap *overlap = new cOverlap(decoder, index);
    ALLOC(sizeof(*overlap), "overlap");
    save = overlap->DetectOverlap(&marks);
    FREE(sizeof(*overlap), "overlap");
    delete overlap;
    elapsedTime.overlap = EndSection("overlap detection");

    // check last stop mark if closing credits follows
    LogSeparator(false);
    dsyslog("cMarkAdStandalone::ProcessOverlap(): check last stop mark for advertisement in frame with logo or closing credits in frame without logo");
    cMark *lastStop = marks.GetLast();
    // check end mark
    if (lastStop && ((lastStop->type == MT_LOGOSTOP) ||  // prevent double detection of ad in frame and closing credits
                     ((lastStop->type == MT_MOVEDSTOP) && (lastStop->newType != MT_NOADINFRAMESTOP) && (lastStop->newType != MT_TYPECHANGESTOP)))) {
        if (MoveLastStopAfterClosingCredits(lastStop)) {
            lastStop = nullptr;  // pointer lastStop after move invalid
            save = true;
            dsyslog("cMarkAdStandalone::ProcessOverlap(): moved logo end mark after closing credit");
        }
    }
    // check border end mark
    if (lastStop && (lastStop->type == MT_HBORDERSTOP)) {
        dsyslog("cMarkAdStandalone::ProcessOverlap(): search for closing credits after border or moved end mark");
        if (MoveLastStopAfterClosingCredits(lastStop)) {
            save = true;
            dsyslog("cMarkAdStandalone::ProcessOverlap(): moved border end mark after closing credit");
        }
    }

    if (save) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
    return;
}


bool cMarkAdStandalone::ProcessFrame() {
    int frameNumber = decoder->GetPacketNumber();
    if (decoder->IsVideoFrame()) {

#ifdef DEBUG_LOGO_DETECT_FRAME_CORNER
        if ((frameNumber > (DEBUG_LOGO_DETECT_FRAME_CORNER - DEBUG_LOGO_DETECT_FRAME_CORNER_RANGE)) && (frameNumber < (DEBUG_LOGO_DETECT_FRAME_CORNER + DEBUG_LOGO_DETECT_FRAME_CORNER_RANGE))) {

            char *fileName = nullptr;
            if (asprintf(&fileName,"%s/F__%07d.pgm", macontext.Config->recDir, frameNumber) >= 1) {
                ALLOC(strlen(fileName)+1, "fileName");
                SaveVideoPlane0(fileName, decoder->GetVideoPicture());
                FREE(strlen(fileName) + 1, "fileName");
                free(fileName);
            }
        }
#endif
#ifdef DEBUG_PICTURE
        if ((frameNumber > (DEBUG_PICTURE - DEBUG_PICTURE_RANGE)) && (frameNumber < (DEBUG_PICTURE + DEBUG_PICTURE_RANGE))) {
            char *baseName = nullptr;
            if (asprintf(&baseName,"%s/F__%07d", macontext.Config->recDir, frameNumber) >= 1) {
                ALLOC(strlen(baseName) + 1, "baseName");
                SaveVideoPicture(baseName, decoder->GetVideoPicture());
                FREE(strlen(baseName) + 1, "baseName");
                free(baseName);
            }
        }
#endif

        // detect video based marks
        if (criteria->GetDetectionState(MT_VIDEO)) {
            // for performance reason only analyse i-frames if markad was called without full decoding and forced to full decoding because of codec
            if (!macontext.Config->forcedFullDecode || decoder->IsVideoIFrame()) {
                sMarkAdMarks *vmarks = video->Process();
                if (vmarks) {
                    for (int i = 0; i < vmarks->Count; i++) {
                        AddMark(&vmarks->Number[i]);
                    }
                }
            }
            else decoder->DropFrame();
        }

        // check start
        if (!doneCheckStart && inBroadCast && (frameNumber > packetCheckStart)) CheckStart();

        // check stop
        if (!doneCheckStop && (frameNumber > packetCheckStop)) {
            if (!doneCheckStart) {
                dsyslog("cMarkAdStandalone::ProcessFrame(): assumed end reached but still no CheckStart() called, do it now");
                CheckStart();
            }
            CheckStop();
            return false;   // return false to signal end of file processing
        }
    }

    // detect audio channel based marks
    if ((!macontext.Config->fullDecode ||                     // if we decode only i-frames, we will have no audio frames
            decoder->IsAudioPacket() ||
            !criteria->GetDetectionState(MT_SOUNDCHANGE)) &&  // without silence detection, we do not decode audio and will get no audio frames
            criteria->GetDetectionState(MT_AUDIO)) {
        sMarkAdMarks *amarks = audio->Detect();               // detect channel change and silence
        if (amarks) {
            for (int i = 0; i < amarks->Count; i++) AddMark(&amarks->Number[i]);
        }
    }

    // turn on all detection for end part even if we use stronger marks, just in case we will get no strong end mark
    if (!restartLogoDetectionDone && (frameNumber >= packetEndPart)) {
        dsyslog("cMarkAdStandalone::ProcessFrame(): enter end part at frame (%d), reset detector status", frameNumber);
        video->Clear(true);
        criteria->SetDetectionState(MT_ALL, true);
        if (!macontext.Config->fullDecode) criteria->SetDetectionState(MT_SCENECHANGE, false);  // does not work without full decode
        restartLogoDetectionDone = true;
    }
    return true;
}


void cMarkAdStandalone::Recording() {
    if (abortNow) return;

    StartSection("mark detection");
    if (!macontext.Config->fullDecode) criteria->SetDetectionState(MT_SCENECHANGE, false);  // does not work without full decode
    criteria->ListDetection();
    if (macontext.Config->backupMarks) marks.Backup(directory);

    // force init decoder to get infos about video
    if (!decoder->ReadNextFile()) {
        esyslog("cMarkAdStandalone::Recording(): failed to open first video file");
        abortNow = true;
        return;
    }

    // create object to analyse video picture
    video = new cVideo(decoder, index, criteria, macontext.Config->recDir, macontext.Config->autoLogo, macontext.Config->logoCacheDirectory);
    ALLOC(sizeof(*video), "video");

    // check aspect ratio of broadcast
    // if we don't know video aspect ratio, assume 16:9 and correct this in CheckStart()
    if ((macontext.Info.AspectRatio.num == 0) || (macontext.Info.AspectRatio.den == 0)) {
        dsyslog("cMarkAdStandalone::Recording(): unknown broadcast aspect ratio, assume 16:9");
        macontext.Info.AspectRatio.num = 16;
        macontext.Info.AspectRatio.den =  9;
    }
    video->SetAspectRatioBroadcast(macontext.Info.AspectRatio);

    // create object to analyse audio picture
    audio = new cAudio(decoder, index, criteria);
    ALLOC(sizeof(*audio), "audio");

    // video type
    if (decoder->GetVideoType() == 0) {
        dsyslog("cMarkAdStandalone::Recording(): video type not set");
        abortNow = true;
        return;
    }
    // video width
    dsyslog("cMarkAdStandalone::Recording(): video width: %4d", decoder->GetVideoWidth());
    // video height
    dsyslog("cMarkAdStandalone::Recording(): video hight: %4d", decoder->GetVideoHeight());
    // frame rate
    dsyslog("cMarkAdStandalone::Recording(): frame rate:         %d frames per second", decoder->GetVideoFrameRate());
    if (decoder->GetVideoFrameRate() <= 0) {
        esyslog("average frame rate of %d frames per second is invalid, recording is damaged", decoder->GetVideoFrameRate());
        abortNow = true;
        return;
    }

    // calculate assumed start and end position
    CalculateCheckPositions(macontext.Info.tStart * decoder->GetVideoFrameRate());

    CheckIndexGrowing();   // check if we have a running recording and have to wait to get new frames

    while (decoder->DecodeNextFrame(criteria->GetDetectionState(MT_SOUNDCHANGE))) {  // only decode audio if we detect silence, channel change detection needs no decoding
        if (abortNow) return;

        if (!ProcessFrame()) {   // no error, false if stopA reached
            if (abortNow) return;  // false from abort request
            break;
        }
        CheckIndexGrowing();  // check if we have a running recording and have to wait to get new frame
    }

    // we reached end of recording without CheckStart() or CheckStop() called
    if (!doneCheckStop && (decoder->GetPacketNumber() <= stopA)) {
        dsyslog("cMarkAdStandalone::Recording(): frame (%d): stopA (%d)", decoder->GetPacketNumber(), stopA);
        esyslog("end of recording before recording length from VDR info file reached");
    }
    if (!doneCheckStart) {
        dsyslog("cMarkAdStandalone::Recording(): frame (%d): recording ends before CheckStart() done, call it now", decoder->GetPacketNumber());
        CheckStart();
    }
    if (!doneCheckStop) {
        dsyslog("cMarkAdStandalone::Recording(): frame (%d): recording ends before CheckStop() done, call it now", decoder->GetPacketNumber());
        CheckStop();
    }

// cleanup marks that make no sense
    CheckMarks();

    if (!abortNow) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, false);
    elapsedTime.markDetection = EndSection("mark detection");
}


bool cMarkAdStandalone::SetFileUID(char *file) {
    if (!file) return false;
    struct stat statbuf;
    if (!stat(directory, &statbuf)) {
        if (chown(file, statbuf.st_uid, statbuf.st_gid) == -1) return false;
    }
    return true;
}


time_t cMarkAdStandalone::GetRecordingStart(time_t start, int fd) {
    // get recording start from directory name (time part)
    const char *timestr = strrchr(directory, '/');
    if (timestr) {
        timestr++;
        if (isdigit(*timestr)) {
            time_t now = time(nullptr);
            struct tm tm_r;
            struct tm t = *localtime_r(&now, &tm_r); // init timezone
            if (sscanf(timestr, "%4d-%02d-%02d.%02d%*c%02d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, & t.tm_min)==5) {
                t.tm_year -= 1900;
                t.tm_mon--;
                t.tm_sec = 0;
                t.tm_isdst = -1;
                time_t dirNameTime = mktime(&t);
                dsyslog("cMarkAdStandalone::GetRecordingStart(): recording start from directory name: %s", strtok(ctime(&dirNameTime), "\n"));
                return dirNameTime;
            }
        }
    }

    // fallback get recording start from atime of directory (if the volume is mounted with noatime)
    struct mntent *ent;
    struct stat   statbuf;
    FILE *mounts = setmntent(_PATH_MOUNTED, "r");
    int  mlen;
    int  oldmlen  = 0;
    bool useatime = false;
    while ((ent = getmntent(mounts)) != nullptr) {
        if (strstr(directory, ent->mnt_dir)) {
            mlen = strlen(ent->mnt_dir);
            if (mlen > oldmlen) {
                if (strstr(ent->mnt_opts, "noatime")) {
                    useatime = true;
                }
                else {
                    useatime = false;
                }
            }
            oldmlen = mlen;
        }
    }
    endmntent(mounts);

    if (useatime) dsyslog("cMarkAdStandalone::GetRecordingStart(): mount option noatime is set, use atime from directory %s to get creation time", directory);
    else esyslog("cMarkAdStandalone::GetRecordingStart(): mount option noatime is not set");

    if ((useatime) && (stat(directory, &statbuf) != -1)) {
        if (fabs(difftime(start,statbuf.st_atime)) < 60 * 60 * 12) {  // do not believe recordings > 12h
            dsyslog("cMarkAdStandalone::GetRecordingStart(): got recording start from directory creation time");
            return statbuf.st_atime;
        }
        dsyslog("cMarkAdStandalone::GetRecordingStart(): got no valid directory creation time, maybe recording was copied %s", strtok(ctime(&statbuf.st_atime), "\n"));
        dsyslog("cMarkAdStandalone::GetRecordingStart(): broadcast start time from vdr info file                          %s", strtok(ctime(&start), "\n"));
    }

    return (time_t) 0;
}


bool cMarkAdStandalone::CheckLogo(const int frameRate) {
    if (!macontext.Config)                      return false;
    if (!*macontext.Config->logoCacheDirectory) return false;
    if (!macontext.Info.ChannelName)            return false;
    if (macontext.Config->perftest)             return false;    // nothing to do in perftest

    StartSection("initial logo search");
    bool logoFound = false;
    int len = strlen(macontext.Info.ChannelName);
    if (!len) return false;

    dsyslog("cMarkAdStandalone::CheckLogo(): using logo cache directory %s, searching logo for %s", macontext.Config->logoCacheDirectory, macontext.Info.ChannelName);
    DIR *dir = opendir(macontext.Config->logoCacheDirectory);
    if (!dir) {
        esyslog("logo cache directory %s does not exist, use /tmp", macontext.Config->logoCacheDirectory);
        strcpy( macontext.Config->logoCacheDirectory, "/tmp");
        dsyslog("cMarkAdStandalone::CheckLogo(): using logo directory %s", macontext.Config->logoCacheDirectory);
        dir = opendir(macontext.Config->logoCacheDirectory);
        if (!dir) exit(1);
    }

    struct dirent *dirent = nullptr;
    while ((dirent = readdir(dir))) {
        if (strncmp(dirent->d_name, macontext.Info.ChannelName, len) == 0) {
            dsyslog("cMarkAdStandalone::CheckLogo(): logo found: %s", dirent->d_name);
            if ((macontext.Config->autoLogo == 0) || (macontext.Config->autoLogo == 2)) {
                closedir(dir);
                elapsedTime.logoSearch = EndSection("initial logo search");
                return true; // use only logos from cache or prefer logo from cache
            }
            logoFound = true;
        }
    }
    closedir(dir);

    if (macontext.Config->autoLogo > 0) {  // we use logo from recording directory or self extracted logo
        isyslog("search for %s %d:%d logo in recording directory %s", macontext.Info.ChannelName, macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den, macontext.Config->logoCacheDirectory);
        DIR *recDIR = opendir(macontext.Config->recDir);
        if (recDIR) {
            const struct dirent *direntRec = nullptr;
            while ((direntRec = readdir(recDIR))) {
                if (!strncmp(direntRec->d_name, macontext.Info.ChannelName, len)) {
                    closedir(recDIR);
                    isyslog("logo found in recording directory");
                    elapsedTime.logoSearch = EndSection("initial logo search");
                    return true;
                }
            }
            closedir(recDIR);
        }
        isyslog("no logo for %s %d:%d found in recording directory %s, trying to extract logo from recording", macontext.Info.ChannelName, macontext.Info.AspectRatio.num, macontext.Info.AspectRatio.den, macontext.Config->recDir);

        // only full decode if we have to because og H.264 interlaced
        extractLogo = new cExtractLogo(macontext.Config->recDir, macontext.Info.ChannelName, macontext.Config->threads, macontext.Config->forcedFullDecode, macontext.Config->hwaccel, macontext.Config->forceHW, macontext.Info.AspectRatio);
        ALLOC(sizeof(*extractLogo), "extractLogo");

        int startPos =  (macontext.Info.tStart + 2 *60) * frameRate;  // search logo from assumed start + 2 min to prevent to get logos from ad
        if (startPos < 0) startPos = 0;  // consider late start of recording
        int endpos = extractLogo->SearchLogo(startPos, false);
        for (int retry = 2; retry <= 8; retry++) {  // do not reduce, we will not get some logos
            startPos = endpos + 10 * frameRate;     // next try 10s after end of try before, maybe false border detection in dark scene
            if (endpos > LOGO_SEARCH_FOUND) {       // no logo found, endpos is last frame of search
                dsyslog("cMarkAdStandalone::CheckLogo(): no logo found in recording, retry in %ind part of the recording at frame (%d)", retry, startPos);
                endpos = extractLogo->SearchLogo(startPos, false);
            }
            else break;
        }
        FREE(sizeof(*extractLogo), "extractLogo");
        delete extractLogo;
        extractLogo = nullptr;

        if (endpos == 0) {
            dsyslog("cMarkAdStandalone::CheckLogo(): found extracted logo in recording recording directory");
            logoFound = true;
        }
        else dsyslog("cMarkAdStandalone::CheckLogo(): logo search failed");
    }
    elapsedTime.logoSearch = EndSection("initial logo search");
    return logoFound;
}


void cMarkAdStandalone::LoadInfo() {
    char *buf;
    if (asprintf(&buf, "%s/info", directory) == -1) return;
    ALLOC(strlen(buf)+1, "buf");

    if (macontext.Config->before) {
        macontext.Info.isRunningRecording = true;
        dsyslog("parameter before is set, markad is called with a running recording");
    }

    FILE *f;
    f = fopen(buf, "r");
    FREE(strlen(buf)+1, "buf");
    free(buf);
    buf = nullptr;
    if (!f) {
        // second try for reel vdr
        if (asprintf(&buf, "%s/info.txt", directory) == -1) return;
        ALLOC(strlen(buf)+1, "buf");
        f = fopen(buf,"r");
        FREE(strlen(buf)+1, "buf");
        free(buf);
    }
    if (!f) {  // no vdr info file found
        esyslog("vdr info file: not found");
        return;
    }

    char *line = nullptr;
    size_t linelen = 0;
    while (getline(&line, &linelen, f) != -1) {
        if (line[0] == 'C') {
            char channelname[256];
            memset(channelname, 0, sizeof(channelname));
            int result = sscanf(line, "%*c %*80s %250c", reinterpret_cast<char *>(&channelname));
            if (result == 1) {
                macontext.Info.ChannelName = strdup(channelname);
                ALLOC(strlen(macontext.Info.ChannelName)+1, "macontext.Info.ChannelName");
                char *lf = strchr(macontext.Info.ChannelName, 10);
                if (lf) {
                    *lf = 0;
                    char *tmpName = strdup(macontext.Info.ChannelName);
                    ALLOC(strlen(tmpName)+1, "macontext.Info.ChannelName");
                    *lf = 10;
                    FREE(strlen(macontext.Info.ChannelName)+1, "macontext.Info.ChannelName");
                    free(macontext.Info.ChannelName);
                    macontext.Info.ChannelName = tmpName;
                }
                char *cr = strchr(macontext.Info.ChannelName, 13);
                if (cr) {
                    *cr = 0;
                    char *tmpName = strdup(macontext.Info.ChannelName);
                    ALLOC(strlen(tmpName)+1, "macontext.Info.ChannelName");
                    *lf = 13;
                    FREE(strlen(macontext.Info.ChannelName)+1, "macontext.Info.ChannelName");
                    free(macontext.Info.ChannelName);
                    macontext.Info.ChannelName = tmpName;
                }
                for (int i = 0; i < static_cast<int> (strlen(macontext.Info.ChannelName)); i++) {
                    if (macontext.Info.ChannelName[i] == ' ') macontext.Info.ChannelName[i] = '_';
                    if (macontext.Info.ChannelName[i] == '.') macontext.Info.ChannelName[i] = '_';
                    if (macontext.Info.ChannelName[i] == '/') macontext.Info.ChannelName[i] = '_';
                }
            }
            else {
                esyslog("vdr info file: no channel name found");
            }
        }
        if ((line[0] == 'E') && (!bLiveRecording)) {
            long st;
            int result = sscanf(line,"%*c %*10i %20li %6i %*2x %*2x", &st, &length);
            startTime=(time_t)st;
            if (result != 2) {
                esyslog("vdr info file: could not read start time and length");
                startTime = 0;
                length = 0;
            }
        }
        if (line[0] == 'T') {
            memset(title, 0, sizeof(title));
            int result = sscanf(line, "%*c %79c", title);
            if ((result == 0) || (result == EOF)) {
                title[0] = 0;
            }
            else {
                char *lf = strchr(title, 10);
                if (lf) *lf = 0;
                char *cr = strchr(title, 13);
                if (cr) *cr = 0;
            }
        }
        if (line[0] == 'F') {
            int fps;
            int result = sscanf(line, "%*c %3i", &fps);
            if ((result == 0) || (result == EOF)) {
            }
            else dsyslog("frame rate %d (from vdr info)", fps);
        }
        if ((line[0] == 'X') && (!bLiveRecording)) {
            int stream = 0, type = 0;
            char descr[256];
            memset(descr, 0, sizeof(descr));
            int result=sscanf(line, "%*c %3i %3i %250c", &stream, &type, reinterpret_cast<char *>(&descr));
            if ((result != 0) && (result != EOF)) {
                if ((stream == 1) || (stream == 5)) {
                    if ((type != 1) && (type != 5) && (type != 9) && (type != 13)) {
                        dsyslog("aspect ratio 16:9 (from vdr info)");
                        macontext.Info.AspectRatio.num = 16;
                        macontext.Info.AspectRatio.den = 9;
                    }
                    else {
                        dsyslog("aspect ratio 4:3 (from vdr info)");
                        macontext.Info.AspectRatio.num = 4;
                        macontext.Info.AspectRatio.den = 3;
                    }
                }

                if (stream == 2) {
                    if (type == 5) {
                        // if we have DolbyDigital 2.0 disable AC3
                        if (strchr(descr, '2')) {
                            dsyslog("broadcast with DolbyDigital2.0 (from vdr info)");
                        }
                        // if we have DolbyDigital 5.1 disable video decoding
                        if (strchr(descr, '5')) {
                            dsyslog("broadcast with DolbyDigital5.1 (from vdr info)");
                        }
                    }
                }
            }
        }
    }
    // create criteria object
    criteria = new cCriteria(macontext.Info.ChannelName);
    ALLOC(sizeof(*criteria), "criteria");

    if ((macontext.Info.AspectRatio.num == 0) && (macontext.Info.AspectRatio.den == 0)) isyslog("no aspect ratio found in vdr info");
    if (line) free(line);

    if ((length) && (startTime)) {
        time_t rStart = GetRecordingStart(startTime, fileno(f));
        if (rStart) {
            dsyslog("cMarkAdStandalone::LoadInfo(): recording start at %s", strtok(ctime(&rStart), "\n"));
            dsyslog("cMarkAdStandalone::LoadInfo(): timer     start at %s", strtok(ctime(&startTime), "\n"));

            //  start offset of broadcast from timer event to recording start
            int startEvent = static_cast<int> (startTime - rStart);
            dsyslog("cMarkAdStandalone::LoadInfo(): event start at offset:               %5ds -> %d:%02d:%02dh", startEvent, startEvent / 3600, (startEvent % 3600) / 60, startEvent % 60);
            if (startEvent > 60 * 60) {  // assume max 1h pre-timer
                isyslog("cMarkAdStandalone::LoadInfo(): offset invald, maybe recording was copied, set to default 2min");
                startEvent = 2 * 60;
            }
            // start offset of broadcast from VPS event
            int startVPS = vps->GetStart();
            if (startVPS >= 0) {
                dsyslog("cMarkAdStandalone::LoadInfo(): VPS   start at offset:               %5ds -> %d:%02d:%02dh", startVPS, startVPS / 3600, (startVPS % 3600) / 60, startVPS % 60);
                if ((macontext.Info.ChannelName && criteria->GoodVPS()) || (abs(startVPS - startEvent) <= 10 * 60)) {
                    dsyslog("cMarkAdStandalone::LoadInfo(): VPS start event seems to be valid");
                    macontext.Info.tStart   = startVPS;
                    macontext.Info.startFromVPS = true;
                }
                else dsyslog("cMarkAdStandalone::LoadInfo(): VPS start event seems to be invalid, %ds away from boadcast start event", abs(startVPS - startEvent));
            }
            else dsyslog("cMarkAdStandalone::LoadInfo(): no VPS start event found");


            // try to get broadcast length from VPS start/stop events
            if (macontext.Info.tStart >= 0) {
                int vpsStop = vps->GetStop();
                if (vpsStop > macontext.Info.tStart) {
                    dsyslog("cMarkAdStandalone::LoadInfo(): VPS stop  event at offset:           %5ds -> %d:%02d:%02dh", vpsStop, vpsStop / 3600, (vpsStop % 3600) / 60, vpsStop % 60);
                    dsyslog("cMarkAdStandalone::LoadInfo(): broadcast length from vdr info file: %5ds -> %d:%02d:%02dh", length, length / 3600, (length % 3600) / 60, length % 60);
                    int lengthVPS = vpsStop - macontext.Info.tStart;
                    int diff      = lengthVPS - length;
                    dsyslog("cMarkAdStandalone::LoadInfo(): broadcast length from VPS events:    %5ds -> %d:%02d:%02dh, %ds longer than length from vdr info file", lengthVPS, lengthVPS / 3600, (lengthVPS % 3600) / 60, lengthVPS % 60, diff);
                    // invalid examples:
                    // -615 (accepted invalid VPS sequence, false stop, running after)
                    // changed from  506 to  298
                    // changed from -615 to -577  // found invalid length with -577
                    if ((diff >= 285) || (diff <= -577)) {
                        dsyslog("cMarkAdStandalone::LoadInfo(): VPS stop event seems to be invalid, use length from vdr info file");
                        vps->SetStop(-1);  // set VPS stop event to invalid
                    }
                    else {
                        dsyslog("cMarkAdStandalone::LoadInfo(): VPS events seems to be valid, use length from VPS events");
                        length = lengthVPS;
                        macontext.Info.lengthFromVPS = true;
                    }
                }
            }
            if (vps->IsVPSTimer()) { //  VPS controlled recording start, we guess assume broascast start 45s after recording start
                isyslog("VPS controlled recording start");
                if (macontext.Info.tStart < 0) {
                    dsyslog("cMarkAdStandalone::LoadInfo(): no VPS start event found");
                    macontext.Info.tStart = 45;
                }
            }

            // no valid VPS start event, try to get broadcast start offset and broadcast length from info file
            if (macontext.Info.tStart < 0) {
                macontext.Info.tStart = startEvent;
                if (macontext.Info.tStart > 60 * 60) {   // more than 1h pre-timer make no sense, there must be a wrong directory time
                    isyslog("pre-time %is not valid, possible wrong directory time, set pre-timer to vdr default (2min)", macontext.Info.tStart);
                    macontext.Info.tStart = 120;
                }
                if (macontext.Info.tStart < 0) {
                    if (length + macontext.Info.tStart > 0) {
                        startTime = rStart;
                        esyslog("recording start %02d:%02d min after timer start, recording incomplete, marks will be wrong", -macontext.Info.tStart / 60, -macontext.Info.tStart % 60);
                        dsyslog("event length from timer: %5ds", length);
                        length += macontext.Info.tStart;
                        dsyslog("event length corrected %5ds", length);
                    }
                    else {
                        isyslog("cannot determine broadcast start, assume VDR default pre timer of 120s");
                        macontext.Info.tStart = 120;
                    }
                }
            }
        }
        else macontext.Info.tStart = 0;
    }
    else {
        dsyslog("cMarkAdStandalone::LoadInfo(): start time and length from vdr info file not valid");
        macontext.Info.tStart = 0;
    }
    dsyslog("cMarkAdStandalone::LoadInfo(): broadcast start %is after recording start", macontext.Info.tStart);

    if ((!length) && (!bLiveRecording)) {
        esyslog("cannot read broadcast length from info, marks can be wrong!");
        macontext.Info.AspectRatio.num = 0;
        macontext.Info.AspectRatio.den = 0;
    }
    fclose(f);
    dsyslog("cMarkAdStandalone::LoadInfo(): length of broadcast  %5ds -> %d:%02d:%02dh", length, length / 3600, (length % 3600) / 60, length % 60);
    return;
}


bool cMarkAdStandalone::CheckTS() {
    if (!directory) return false;
    char *buf;
    if (asprintf(&buf, "%s/00001.ts", directory) == -1) return false;
    ALLOC(strlen(buf)+1, "buf");
    struct stat statbuf;
    if (stat(buf, &statbuf) == -1) {
        if (errno != ENOENT) {
            FREE(strlen(buf)+1, "buf");
            free(buf);
            return false;
        }
        buf = nullptr;
    }
    if (buf) {
        FREE(strlen(buf)+1, "buf");
        free(buf);
    }
    return true;
}


bool cMarkAdStandalone::CreatePidfile() {
    char *buf = nullptr;
    if (asprintf(&buf, "%s/markad.pid", directory) == -1) return false;
    ALLOC(strlen(buf)+1, "buf");

    // check for other running markad process
    FILE *oldpid=fopen(buf, "r");
    if (oldpid) {
        // found old pidfile, check if it's still running
        int pid;
        if (fscanf(oldpid, "%10i\n", &pid) == 1) {
            char procname[256] = "";
            snprintf(procname, sizeof(procname), "/proc/%i",pid);
            struct stat statbuf;
            if (stat(procname,&statbuf) != -1) {
                // found another, running markad
                fprintf(stderr, "another instance is running on %s", directory);
                abortNow = duplicate = true;
            }
        }
        fclose(oldpid);
    }
    else { // fopen above sets the error to 2, reset it here!
        errno = 0;
    }
    if (duplicate) {
        FREE(strlen(buf)+1, "buf");
        free(buf);
        return false;
    }

    FILE *pidfile = fopen(buf, "w+");

    SetFileUID(buf);

    FREE(strlen(buf)+1, "buf");
    free(buf);
    if (!pidfile) return false;
    fprintf(pidfile, "%i\n", static_cast<int> (getpid()));
    fflush(pidfile);
    fclose(pidfile);
    return true;
}


void cMarkAdStandalone::RemovePidfile() {
    if (!directory) return;
    if (duplicate) return;

    char *buf;
    if (asprintf(&buf, "%s/markad.pid", directory) != -1) {
        ALLOC(strlen(buf)+1, "buf");
        unlink(buf);
        FREE(strlen(buf)+1, "buf");
        free(buf);
    }
}


cMarkAdStandalone::cMarkAdStandalone(const char *directoryParam, sMarkAdConfig *config) {
    setlocale(LC_MESSAGES, "");
    directory        = directoryParam;
    inBroadCast      = false;
    iStopinBroadCast = false;
    video            = nullptr;
    audio            = nullptr;
    osd              = nullptr;
    length           = 0;
    sleepcnt         = 0;
    waittime         = 0;
    iwaittime        = 0;
    duplicate        = false;
    title[0]         = 0;

    macontext = {};
    macontext.Config = config;

    if (!config->noPid) {
        CreatePidfile();
        if (abortNow) return;
    }

    if (LOG2REC) {
        char *fbuf;
        if (asprintf(&fbuf, "%s/%s", directory, config->logFile) != -1) {
            ALLOC(strlen(fbuf)+1, "fbuf");
            if (freopen(fbuf, "w+", stdout)) {};
            SetFileUID(fbuf);
            FREE(strlen(fbuf)+1, "fbuf");
            free(fbuf);
        }
    }

    // log version and hostname
    char hostname[64];
    gethostname(hostname, 64);
    long lb;
    errno = 0;
    lb=sysconf(_SC_LONG_BIT);
    if (errno == 0) isyslog("starting markad v%s (%libit) on %s", VERSION, lb, hostname);
    else            isyslog("starting markad v%s on %s", VERSION, hostname);
    if (config->cmd) dsyslog("called with parameter cmd = %s", config->cmd);

    //  give vdr markad plugin time to pause this process until recording end
    if (strcmp(config->cmd, "after") == 0) {
        isyslog("started from markad plugin for processing after recording");
        sleep(10);
    }

    // ignore --vps if markad runs during recording
    if ((strcmp(config->cmd, "before") == 0) && config->useVPS) {
        esyslog("markad runs during recording, ignore invalid --vps parameter");
        config->useVPS = false;
    }

    // check avcodec library version
#if LIBAVCODEC_VERSION_INT < LIBAVCODEC_VERSION_DEPRECATED
#error "libavcodec not installed or version not supported, please install or update libavcodec"
#endif
#if LIBAVCODEC_VERSION_INT < LIBAVCODEC_VERSION_VALID
#warning "libavcodec version is deprecated for markad, please update"
#endif

    unsigned int ver = avcodec_version();
    char *libver = nullptr;
    if (asprintf(&libver, "%i.%i.%i", ver >> 16 & 0xFF, ver >> 8 & 0xFF, ver & 0xFF) != -1) {
        ALLOC(strlen(libver)+1, "libver");
        isyslog("using libavcodec.so.%s (%d) with %i threads", libver, ver, config->threads);
        if (ver != LIBAVCODEC_VERSION_INT) {
            esyslog("markad build with libavcodec header version %s, but runs with libavcodec lib version %s", AV_STRINGIFY(LIBAVCODEC_VERSION), libver);
            esyslog("libav header and library mismatch, fix your system");
            exit(EXIT_FAILURE);
        }
        if (ver < LIBAVCODEC_VERSION_VALID) isyslog("your libavcodec is deprecated, please update");
        FREE(strlen(libver)+1, "libver");
        free(libver);
    }
    dsyslog("libavcodec config: %s",avcodec_configuration());
    isyslog("on %s", directory);

    if (config->before) sleep(10);

    char *tmpDir = strdup(directory);
    if (!tmpDir) {
        esyslog("cMarkAdStandalone::cMarkAdStandalone(): memory allocation for tmpDir failed");
        exit(EXIT_FAILURE);
    }
#ifdef DEBUG_MEM
    ALLOC(strlen(tmpDir)+1, "tmpDir");
    int memsize_tmpDir = strlen(directory) + 1;
#endif
    char *datePart = strrchr(tmpDir, '/');
    if (!datePart) {
        dsyslog("cMarkAdStandalone::cMarkAdStandalone(): failed to find last '/'");
        FREE(strlen(tmpDir+1), "tmpDir");
        free(tmpDir);
        return;
    }
    *datePart = 0;    // cut off date part

    char *recName = strrchr(tmpDir, '/');  // "/" exists, testet with variable datePart
    if (strstr(recName, "/@")) {
        isyslog("live-recording, disabling pre-/post timer");
        bLiveRecording = true;
    }
    else {
        bLiveRecording = false;
    }
#ifdef DEBUG_MEM
    FREE(memsize_tmpDir, "tmpDir");
#endif
    free(tmpDir);

    if (!CheckTS()) {
        esyslog("no files found");
        abortNow = true;
        return;
    }

    if (asprintf(&indexFile, "%s/index", directory) == -1) indexFile = nullptr;
    if (indexFile) ALLOC(strlen(indexFile) + 1, "indexFile");

    // load VPS events
    vps = new cVPS(directory);
    ALLOC(sizeof(*vps), "vps");

    // load data from vdr info file
    LoadInfo();
    if (!macontext.Info.ChannelName) {
        if (asprintf(&macontext.Info.ChannelName, "unknown") == -1) {};
        ALLOC(strlen(macontext.Info.ChannelName) + 1, "macontext.Info.ChannelName");
    }

    // manually extract logo from recording
    if (config->logoExtraction >= 0) {
        extractLogo = new cExtractLogo(macontext.Config->recDir, macontext.Info.ChannelName, macontext.Config->threads, macontext.Config->forcedFullDecode, macontext.Config->hwaccel, macontext.Config->forceHW, macontext.Info.AspectRatio);
        ALLOC(sizeof(*extractLogo), "extractLogo");
        extractLogo->ManuallyExtractLogo(config->logoExtraction, config->logoWidth, config->logoHeight);
        ALLOC(sizeof(*extractLogo), "extractLogo");
        delete extractLogo;
        return;
    }

    // check if requested decoding parameter are valid for this video codec and used FFmpeg version
    dsyslog("cMarkAdStandalone::cMarkAdStandalone(): check codec");
    char hwaccel[1] = {0};      //!< no hardware acceleration
    cDecoder *decoderTest = new cDecoder(macontext.Config->recDir, 1, true, hwaccel, false, false, nullptr); // one thread, full decocode, no hwaccel, no force interlaced, no index
    ALLOC(sizeof(*decoderTest), "decoderTest");
    if (!decoderTest->DecodeNextFrame(false)) {  // decode one video frame to get video info
        esyslog("cMarkAdStandalone::cMarkAdStandalone(): decode of first video packet failed");
        exit(EXIT_FAILURE);
    }
    int frameRate = decoderTest->GetVideoFrameRate();   // store frameRate for logo extraction and start mark if markad runs during recording
    marks.SetFrameRate(frameRate);  // register framerate to calculate VDR timestamps
    sceneMarks.SetFrameRate(frameRate);
    silenceMarks.SetFrameRate(frameRate);
    blackMarks.SetFrameRate(frameRate);
    dsyslog("cMarkAdStandalone::cMarkAdStandalone(): video characteristics: %s, frame rate %d, type %d, pixel format %d", (decoderTest->IsInterlacedFrame()) ? "interlaced" : "progressive", frameRate, decoderTest->GetVideoType(), decoderTest->GetVideoPixelFormat());

    // FFmpeg version dependent restriction
#if LIBAVCODEC_VERSION_INT <= ((58<<16)+( 54<<8)+100)   // FFmpeg 4.2.7  (Ubuntu 20.04)
    if ((decoderTest->GetVideoType() == MARKAD_PIDTYPE_VIDEO_H264) && (macontext.Config->fullDecode == false)) {
        isyslog("FFmpeg <= 4.2.7 does not support hwaccel without full decoding for H.264 video, disable hwaccel");
        macontext.Config->hwaccel[0] = 0;
        macontext.Config->forceHW    = false;
    }
    if ((decoderTest->GetVideoType() == MARKAD_PIDTYPE_VIDEO_H264) && decoderTest->IsInterlacedFrame()) {
        isyslog("FFmpeg <= 4.2.7 does not support only i-frame decoding for interlaced H.264 video, enable full decode");
        macontext.Config->fullDecode = true;
    }
#endif
#if LIBAVCODEC_VERSION_INT <= ((58<<16)+( 91<<8)+100)   // FFmpeg 4.3.7
    // AVlog(): Assertion !p->parent->stash_hwaccel failed at libavcodec/pthread_frame.c:649
    if ((macontext.Config->hwaccel[0] != 0) && (macontext.Config->threads != 1)) {
        isyslog("FFmpeg <= 4.3.7 does not support multithreading hwaccel, reduce to one thread");
        macontext.Config->threads = 1;
    }
#endif

    // inform decoder who use hwaccel, the video is interlaced. In this case this is not possible to detect from decoder because hwaccel deinterlaces frames
    if ((macontext.Config->hwaccel[0] != 0) && decoderTest->IsInterlacedFrame() && (decoderTest->GetVideoType() == MARKAD_PIDTYPE_VIDEO_H264)) {
        dsyslog("cMarkAdStandalone::cMarkAdStandalone(): inform decoder with hwaccel about H.264 interlaced video and force full decode");
        macontext.Config->forceInterlaced  = true;
        macontext.Config->fullDecode       = true;
        macontext.Config->forcedFullDecode = true;
    }
    FREE(sizeof(*decoderTest), "decoderTest");
    delete decoderTest;

    // write an early start mark for running recordings
    if (macontext.Info.isRunningRecording) {
        dsyslog("cMarkAdStandalone::cMarkAdStandalone(): recording is running, save dummy start mark at pre timer position %ds (%d)", macontext.Info.tStart, macontext.Info.tStart * frameRate);
        // use new marks object because we do not want to have this mark in final file
        cMarks *marksTMP = new cMarks();
        ALLOC(sizeof(*marksTMP), "marksTMP");
        marksTMP->SetFrameRate(frameRate);  // register framerate to write a guessed start position, will be overridden later
        marksTMP->Add(MT_ASSUMEDSTART, MT_UNDEFINED, MT_UNDEFINED, macontext.Info.tStart * frameRate, -1, "timer start", true);  // frame number unknows
        marksTMP->Save(macontext.Config->recDir, macontext.Info.isRunningRecording, macontext.Config->pts, true);
        FREE(sizeof(*marksTMP), "marksTMP");
        delete marksTMP;
    }

    // check if we have a logo or we can extract it from recording
    if (!CheckLogo(frameRate) && (config->autoLogo == 0)) {
        isyslog("no logo found, logo detection disabled");
        criteria->SetDetectionState(MT_LOGOCHANGE, false);
    }

    if (macontext.Info.tStart > 1) {
        if ((macontext.Info.tStart < 60) && (!vps->IsVPSTimer())) macontext.Info.tStart = 60;
    }
    isyslog("pre-timer:        %2d:%02d:%02dh", macontext.Info.tStart / 60 / 60, abs((macontext.Info.tStart / 60) % 60), abs(macontext.Info.tStart % 60));

    if (length) isyslog("broadcast length: %2d:%02d:%02dh", length / 60 / 60, ( length / 60) % 60,  length % 60 );

    if (title[0]) ptitle = title;
    else ptitle = const_cast<char *>(directory);

    if (config->osd) {
        osd = new cOSDMessage(config->svdrphost, config->svdrpport);
        if (osd) {
            ALLOC(sizeof(*osd), "osd");
            osd->Send("%s '%s'", tr("starting markad for"), ptitle);
        }
    }

    if (config->markFileName[0]) marks.SetFileName(config->markFileName);

    if (macontext.Info.ChannelName) isyslog("channel: %s", macontext.Info.ChannelName);

    // create index object
    index = new cIndex(macontext.Config->fullDecode);
    ALLOC(sizeof(*index), "index");
    marks.SetIndex(index);
    sceneMarks.SetIndex(index);
    silenceMarks.SetIndex(index);
    blackMarks.SetIndex(index);

    // create decoder object
    decoder = new cDecoder(macontext.Config->recDir, macontext.Config->threads, macontext.Config->fullDecode, macontext.Config->hwaccel, macontext.Config->forceHW, macontext.Config->forceInterlaced, index);
    ALLOC(sizeof(*decoder), "decoder");
}


cMarkAdStandalone::~cMarkAdStandalone() {
    dsyslog("cMarkAdStandalone::~cMarkAdStandalone(): delete object");
    if (!abortNow) marks.Save(directory, macontext.Info.isRunningRecording, macontext.Config->pts, true);

    // cleanup all objects
    if (detectLogoStopStart) {
        FREE(sizeof(*detectLogoStopStart), "detectLogoStopStart");
        delete detectLogoStopStart;
    }
    if (indexFile) {
        FREE(strlen(indexFile) + 1, "indexFile");
        free(indexFile);
    }
    if (video) {
        FREE(sizeof(*video), "video");
        delete video;
        video = nullptr;
    }
    if (audio) {
        FREE(sizeof(*audio), "audio");
        delete audio;
        audio = nullptr;
    }

    if (osd) {
        if (!duplicate) {
            if (abortNow) osd->Send("%s '%s'", tr("markad aborted for"), ptitle);
            else osd->Send("%s '%s'", tr("markad finished for"), ptitle);
        }
        FREE(sizeof(*osd), "osd");
        delete osd;
        osd = nullptr;
    }

    if (decoder && decoder->GetErrorCount() > 0) isyslog("decoding errors: %d", decoder->GetErrorCount());

    if (evaluateLogoStopStartPair) {
        FREE(sizeof(*evaluateLogoStopStartPair), "evaluateLogoStopStartPair");
        delete evaluateLogoStopStartPair;
    }

    FREE(sizeof(*index), "index");
    delete index;
    FREE(sizeof(*criteria), "criteria");
    delete criteria;

// log statistics
    if ((!abortNow) && (!duplicate) && (decoder->GetVideoFrameRate() > 0)) {
        LogSeparator();

        // broadcast length without advertisement
        dsyslog("recording statistics: -----------------------------------------------------------------------");
        int lengthFrames = marks.Length();
        int lengthSec    = lengthFrames / decoder->GetVideoFrameRate();
        dsyslog("broadcast length without advertisement: %6d frames, %6ds -> %d:%02d:%02dh", marks.Length(), lengthSec, lengthSec / 3600, (lengthSec % 3600) / 60,  lengthSec % 60);

        // recording length from VPS events
        int vpsLength = vps->Length();
        if (vpsLength > 0) {
            dsyslog("recording length from VPS events:                      %6ds -> %d:%02d:%02dh", vpsLength, vpsLength / 3600, (vpsLength % 3600) / 60,  vpsLength % 60);
            int adQuote = 100 * (vpsLength - lengthSec) / vpsLength;
            if (adQuote > 41) esyslog("advertisement quote: %d%% very high, marks can be wrong", adQuote);  // changed from 40 to 41
            else dsyslog("advertisement quote: %d%%", adQuote);
        }
        // log match of VPS events
        if (macontext.Info.ChannelName) vps->LogMatch(macontext.Info.ChannelName, &marks);

        dsyslog("processing statistics: ----------------------------------------------------------------------");
        // initial logo search
        time_t sec = round(static_cast<double>(elapsedTime.logoSearch) / 1000);
        dsyslog("pass 1 (initial logosearch): time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);
        // mark detection
        sec = round(static_cast<double>(elapsedTime.markDetection) / 1000);
        dsyslog("pass 2 (mark detection):     time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);
        // mark optimization
        sec = round(static_cast<double>(elapsedTime.markOptimization) / 1000);
        dsyslog("pass 3 (mark optimization):  time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);
        // overlap detection
        sec = round(static_cast<double>(elapsedTime.overlap) / 1000);
        dsyslog("pass 4 (overlap detection):  time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);
        // video cut, only cut is done
        sec = round(static_cast<double>(elapsedTime.cut) / 1000);
        if (sec > 0) dsyslog("pass 5 (cut recording):      time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);
        // mark pictures
        sec = round(static_cast<double>(elapsedTime.markPictures) / 1000);
        if (sec > 0) dsyslog("pass 6 (mark pictures):      time %5lds -> %ld:%02ld:%02ldh", sec, sec / 3600, (sec % 3600) / 60,  sec % 60);

        dsyslog("global statistics: --------------------------------------------------------------------------");
        int decodeTime_s = round(decodeTime_ms / 1000);
        dsyslog("decoding:                    time %5ds -> %d:%02d:%02dh", decodeTime_s, decodeTime_s / 3600, (decodeTime_s % 3600) / 60,  decodeTime_s % 60);

        gettimeofday(&endAll, nullptr);
        sec              = endAll.tv_sec  - startAll.tv_sec;
        suseconds_t usec = endAll.tv_usec - startAll.tv_usec;
        if (usec < 0) {
            usec += 1000000;
            sec--;
        }
        double etime = 0;
        etime = sec + ((double) usec / 1000000) - waittime;
        isyslog("duration:                    time %5ds -> %d:%02d:%02dh", static_cast<int> (etime), static_cast<int> (etime / 3600),  (static_cast<int> (etime) % 3600) / 60, static_cast<int> (etime) % 60);
        dsyslog("----------------------------------------------------------------------------------------------");
    }


// cleanup objects used in statistics
    FREE(sizeof(*decoder), "decoder");
    delete decoder;
    if (vps) {
        FREE(sizeof(*vps), "vps");
        delete vps;
        vps = nullptr;
    }
    if (macontext.Info.ChannelName) {
        FREE(strlen(macontext.Info.ChannelName) + 1, "macontext.Info.ChannelName");
        free(macontext.Info.ChannelName);
        macontext.Info.ChannelName = nullptr;
    }

    RemovePidfile();
}


bool isnumber(const char *s) {
    while (*s) {
        if (!isdigit(*s))
            return false;
        s++;
    }
    return true;
}


int usage(int svdrpport) {
    // nothing done, give the user some help
    printf("Usage: markad [options] cmd <record>\n"
           "options:\n"
           "-b              --background\n"
           "                  markad runs as a background-process\n"
           "                  this will be automatically set if called with \"after\"\n"
           "-d              --disable=<option>\n"
           "                  <option>   1 = disable video decoding, 2 = disable audio\n"
           "                             decoding, 3 = disable video and audio decoding\n"
           "-l              --logocachedir\n"
           "                  directory where logos stored, default /var/lib/markad\n"
           "-p              --priority=<priority>\n"
           "                  software priority of markad when running in background\n"
           "                  <priority> from -20...19, default 19\n"
           "-r              --ioprio=<class>[,<level>]\n"
           "                  io priority of markad when running in background\n"
           "                  <class> 1 = realtime, <level> from 0..7, default 4\n"
           "                          2 = besteffort, <level> from 0..7, default 4\n"
           "                          3 = idle (default)\n"
           "                  make sure your I/O scheduler supports scheduling priorities and classes (e.g. BFQ or CFQ)\n"
           "-v              --verbose\n"
           "                  increments loglevel by one, can be given multiple times\n"
           "-B              --backupmarks\n"
           "                  make a backup of existing marks\n"
           "-L              --extractlogo=<direction>[,width[,height]]\n"
           "                  extracts logo to recording directory as pgm files (must be renamed)\n"
           "                  <direction>  0 = top left,    1 = top right\n"
           "                               2 = bottom left, 3 = bottom right\n"
           "-O              --OSD\n"
           "                  markad sends an OSD-Message for start and end\n"
           "-R              --log2rec\n"
           "                  write logfiles into recording directory\n"
           "                --logfile=<filename>\n"
           "                  logfile name (default: markad.log)\n"
           "-T              --threads=<number>\n"
           "                  number of threads used for decoding, max. 16\n"
           "                  (default is the number of cpus)\n"
           "-V              --version\n"
           "                  print version-info and exit\n"
           "                --loglevel=<level>\n"
           "                  sets loglevel to the specified value\n"
           "                  <level> 1=error 2=info 3=debug 4=trace\n"
           "                --markfile=<markfilename>\n"
           "                  set a different markfile-name\n"
           "                --nopid\n"
           "                  disables creation of markad.pid file in recdir\n"
           "                --online[=1|2] (default is 1)\n"
           "                  only valid together with cmd \"before\"\n"
           "                  start markad immediately when called together with \"before\" as cmd\n"
           "                  if online=1, markad starts online for live-recordings only\n"
           "                     online=2, markad starts online for every recording\n"
           "                  live-recordings are identified by having a '@' in the filename\n"
           "                --svdrphost=<ip/hostname> (default is 127.0.0.1)\n"
           "                  ip/hostname of a remote VDR for OSD messages\n"
           "                --svdrpport=<port> (default is %i)\n"
           "                  port of a remote VDR for OSD messages\n"
           "                --vps\n"
           "                  use markad.vps from recording directory to optimize start, stop and break marks\n"
           "                --cut\n"
           "                  cut video based on marks and write it in the recording directory\n"
           "                --ac3reencode\n"
           "                  re-encode AC3 stream to fix low audio level of cutted video on same devices\n"
           "                  requires --cut\n"
           "                --autologo=<option>\n"
           "                  <option>   0 = use logo only from logo cache directory\n"
           "                             1 = extract logo from recording, if fails use logo from logo cache\n"
           "                             2 = use logo from logo cache directory, if missing extract logo from recording (default)\n"
           "                --fulldecode\n"
           "                  decode all video frame types and set mark position to all frame types\n"
           "                --smartencode\n"
           "                  re-encode only short before and short after cut position\n"
           "                --fullencode=<streams>\n"
           "                  full re-encode video generated by --cut\n"
           "                  use it only on powerful CPUs, it will double overall run time\n"
           "                  <streams>  all  = keep all video and audio streams of the recording\n"
           "                             best = only encode best video and best audio stream, drop rest\n"
           "                --hwaccel=<hardware acceleration method>\n"
           "                  use hardware acceleration for decoding\n"
           "                  <hardware acceleration method> all methods supported by FFmpeg (ffmpeg -hide_banner -hwaccels)\n"
           "                                                 e.g.: vdpau, cuda, vaapi, vulkan, ...\n"
           "                --perftest>\n"
           "                  run decoder performance test and compare software and hardware decoder\n"
           "\ncmd: one of\n"
           "-                            dummy-parameter if called directly\n"
           "nice                         runs markad directly and with nice(19)\n"
           "after                        markad started by vdr after the recording is complete\n"
           "before                       markad started by vdr before the recording is complete, only valid together with --online\n"
           "edited                       markad started by vdr in edit function and exits immediately\n"
           "\n<record>                     is the name of the directory where the recording\n"
           "                             is stored\n\n",
           svdrpport
          );
    return EXIT_FAILURE;
}


static void signal_handler(int sig) {
    switch (sig) {
#ifdef POSIX
    case SIGTSTP:
        isyslog("paused by signal");
        kill(getpid(), SIGSTOP);
        break;
    case SIGCONT:
        isyslog("continued by signal");
        break;
#endif /* ifdef POSIX */

    case SIGABRT:
        esyslog("aborted by signal");
        abortNow = true;;
        break;
    case SIGSEGV: {
        esyslog("segmentation fault");

#ifdef POSIX
        void *trace[32];
        int i, trace_size = backtrace(trace, 32);
        char **messages = backtrace_symbols(trace, trace_size);

        esyslog("[bt] Execution path:");
        for (i=0; i < trace_size; ++i) {
            esyslog("[bt] %s", messages[i]);
        }
#endif /* #ifdef POSIX */

        _exit(1);
        break;
    }
    case SIGTERM:
    case SIGINT:
        esyslog("aborted by user");
        abortNow = true;
        break;
    default:
        break;
    }
}


char *recDir = nullptr;


void freedir(void) {
    if (recDir) free(recDir);
}


int main(int argc, char *argv[]) {
    bool bAfter         = false;
    bool bEdited        = false;
    bool bFork          = false;
    bool bNice          = false;
    bool bImmediateCall = false;
    int niceLevel       = 19;
    int ioprio_class    = 3;
    int ioprio          = 7;
    const char *tok     = nullptr;
    char *str           = nullptr;
    int ntok            = 0;
    struct sMarkAdConfig config = {};

    gettimeofday(&startAll, nullptr);

    // set defaults
    config.logoExtraction = -1;
    config.logoWidth      =  0;
    config.logoHeight     =  0;
    config.threads        = get_number_of_cpus();
    strcpy(config.svdrphost, "127.0.0.1");
    strcpy(config.logoCacheDirectory, "/var/lib/markad");

#ifdef WINDOWS
    /* winsock2 needs initialization, if we later support it. */
    w32init RuntimeInit;
#endif

    struct servent *serv=getservbyname("svdrp", "tcp");
    if (serv) {
        config.svdrpport = htons(serv->s_port);
    }
    else {
        config.svdrpport = 6419;
    }

    atexit(freedir);

    while (1) {
        int option_index = 0;
        static struct option long_options[] =
        {
            {"background",   0, 0, 'b'},
            {"disable",      1, 0, 'd'},
            {"logocachedir", 1, 0, 'l'},
            {"priority",     1, 0, 'p'},
            {"ioprio",       1, 0, 'r'},
            {"verbose",      0, 0, 'v'},
            {"backupmarks",  0, 0, 'B'},
            {"extractlogo",  1, 0, 'L'},
            {"OSD",          0, 0, 'O' },
            {"log2rec",      0, 0, 'R'},
            {"threads",      1, 0, 'T'},
            {"version",      0, 0, 'V'},
            {"markfile",     1, 0,  1},
            {"loglevel",     1, 0,  2},
            {"online",       2, 0,  3},
            {"nopid",        0, 0,  4},
            {"svdrphost",    1, 0,  5},
            {"svdrpport",    1, 0,  6},
            {"cut",          0, 0,  7},
            {"ac3reencode",  0, 0,  8},
            {"vps",          0, 0,  9},
            {"logfile",      1, 0, 10},
            {"autologo",     1, 0, 11},
            {"fulldecode",   0, 0, 12},
            {"smartencode",  0, 0, 13},
            {"fullencode",   1, 0, 14},
            {"pts",          0, 0, 15},     // undocumented, only for development use
            {"hwaccel",      1, 0, 16},
            {"perftest",     0, 0, 17},     // undocumented, only for development use

            {0, 0, 0, 0}
        };

        int option = getopt_long(argc, argv, "bd:i:l:p:r:vBGIL:ORT:V", long_options, &option_index);
        if (option == -1) break;

        switch (option) {
        case 'b':
            // --background
            bFork = SYSLOG = true;
            break;
        case 'l':
            if ((strlen(optarg) + 1) > sizeof(config.logoCacheDirectory)) {
                fprintf(stderr, "markad: logo path too long: %s\n", optarg);
                return EXIT_FAILURE;
            }
            strncpy(config.logoCacheDirectory, optarg, sizeof(config.logoCacheDirectory) - 1);
            break;
        case 'p':
            // --priority
            if (isnumber(optarg) || *optarg == '-') niceLevel = atoi(optarg);
            else {
                fprintf(stderr, "markad: invalid priority level: %s\n", optarg);
                return EXIT_FAILURE;
            }
            bNice = true;
            break;
        case 'r':
            // --ioprio
            str=strchr(optarg, ',');
            if (str) {
                *str = 0;
                ioprio = atoi(str + 1);
                *str = ',';
            }
            ioprio_class = atoi(optarg);
            if ((ioprio_class < 1) || (ioprio_class > 3)) {
                fprintf(stderr, "markad: invalid io-priority: %s\n", optarg);
                return EXIT_FAILURE;
            }
            if ((ioprio < 0) || (ioprio > 7)) {
                fprintf(stderr, "markad: invalid io-priority: %s\n", optarg);
                return EXIT_FAILURE;
            }
            if (ioprio_class == 3) ioprio = 7;
            bNice = true;
            break;
        case 'v':
            // --verbose
            SysLogLevel++;
            if (SysLogLevel > 10) SysLogLevel = 10;
            break;
        case 'B':
            // --backupmarks
            config.backupMarks = true;
            break;
        case 'L':
            // --extractlogo
            str=optarg;
            ntok=0;
            while (true) {
                tok=strtok(str, ",");
                if (!tok) break;
                switch (ntok) {
                case 0:
                    config.logoExtraction = atoi(tok);
                    if ((config.logoExtraction < 0) || (config.logoExtraction > 3)) {
                        fprintf(stderr, "markad: invalid extractlogo value: %s\n", tok);
                        return EXIT_FAILURE;
                    }
                    break;
                case 1:
                    config.logoWidth = atoi(tok);
                    break;
                case 2:
                    config.logoHeight = atoi(tok);
                    break;
                default:
                    break;
                }
                str = nullptr;
                ntok++;
            }
            break;
        case 'O':
            // --OSD
            config.osd = true;
            break;
        case 'R':
            // --log2rec
            LOG2REC = true;
            break;
        case 'T':
            // --threads
            config.threads = atoi(optarg);
            if (config.threads < 1) config.threads = 1;
            if (config.threads > 16) config.threads = 16;
            break;
        case 'V':
            printf("markad %s - marks advertisements in VDR recordings\n", VERSION);
            return EXIT_SUCCESS;
        case '?':
            printf("unknown option ?\n");
            break;
        case 0:
            printf ("option %s", long_options[option_index].name);
            if (optarg) printf (" with arg %s", optarg);
            printf ("\n");
            break;
        case 1: // --markfile
            if ((strlen(optarg) + 1) > sizeof(config.markFileName)) {
                fprintf(stderr, "markad: mark file name too long: %s\n", optarg);
                return EXIT_FAILURE;
            }
            strncpy(config.markFileName, optarg, sizeof(config.markFileName) - 1);
            break;
        case 2: // --loglevel
            SysLogLevel = atoi(optarg);
            if (SysLogLevel > 10) SysLogLevel = 10;
            if (SysLogLevel <  0) SysLogLevel =  2;
            break;
        case 3: // --online
            if (optarg) {
                config.online = atoi(optarg);
            }
            else {
                config.online = 1;
            }
            if ((config.online != 1) && (config.online != 2)) {
                fprintf(stderr, "markad: invalid online value: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 4: // --nopid
            config.noPid = true;
            break;
        case 5: // --svdrphost
            if ((strlen(optarg) + 1) > sizeof(config.svdrphost)) {
                fprintf(stderr, "markad: svdrphost too long: %s\n", optarg);
                return EXIT_FAILURE;
            }
            strncpy(config.svdrphost, optarg, sizeof(config.svdrphost) - 1);
            break;
        case 6: // --svdrpport
            if (isnumber(optarg) && atoi(optarg) > 0 && atoi(optarg) < 65536) {
                config.svdrpport = atoi(optarg);
            }
            else {
                fprintf(stderr, "markad: invalid svdrpport value: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 7: // --cut
            config.MarkadCut = true;
            break;
        case 8: // --ac3reencode
            config.ac3ReEncode = true;
            break;
        case 9: // --vps
            config.useVPS = true;
            break;
        case 10: // --logfile
            strncpy(config.logFile, optarg, sizeof(config.logFile) - 1);
            config.logFile[sizeof(config.logFile) - 1] = 0;
            break;
        case 11: // --autologo
            if (isnumber(optarg) && atoi(optarg) >= 0 && atoi(optarg) <= 2) config.autoLogo = atoi(optarg);
            else {
                fprintf(stderr, "markad: invalid autologo value: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 12: // --fulldecode
            config.fullDecode = true;
            break;
        case 13: // --smartencode
            config.smartEncode = true;
            break;
        case 14: // --fullencode
            if (config.smartEncode) fprintf(stderr, "markad: ----smartencode is set, ignore invalid --fullencode\n");
            else {
                config.fullEncode = true;
                str = optarg;
                ntok = 0;
                while (str) {
                    tok = strtok(str, ",");
                    if (!tok) break;
                    switch (ntok) {
                    case 0:
                        if (strcmp(tok, "all") == 0) config.bestEncode = false;
                        else if (strcmp(tok, "best") == 0) config.bestEncode = true;
                        else {
                            fprintf(stderr, "markad: invalid --fullencode value: %s\n", tok);
                            return EXIT_FAILURE;
                        }
                        break;
                    default:
                        break;
                    }
                    str = nullptr;
                    ntok++;
                }
            }
            break;
        case 15: // --pts
            config.pts = true;
            break;
        case 16: // --hwaccel
            str = optarg;
            tok = strtok(str, ",");
            if (tok) {
                if ((strlen(tok) + 1) > sizeof(config.hwaccel)) {
                    fprintf(stderr, "markad: hwaccel type too long: %s\n", tok);
                    return EXIT_FAILURE;
                }
                strncpy(config.hwaccel, tok, sizeof(config.hwaccel) - 1);
                tok = strtok(nullptr, ",");
                if ((tok) && (strcmp(tok, "force") == 0)) config.forceHW = true;
            }
            else {
                if ((strlen(optarg) + 1) > sizeof(config.hwaccel)) {
                    fprintf(stderr, "markad: hwaccel type too long: %s\n", optarg);
                    return EXIT_FAILURE;
                }
                strncpy(config.hwaccel, optarg, sizeof(config.hwaccel) - 1);
            }
            break;
        case 17: // --perftest
            config.perftest = true;
            break;
        default:
            printf ("markad: invalid option -%c\n", option);
        }
    }

    if (optind < argc) {
        while (optind < argc) {
            if (strcmp(argv[optind], "after" ) == 0 ) {
                config.cmd = argv[optind];
                bAfter = bFork = bNice = SYSLOG = true;
            }
            else if (strcmp(argv[optind], "before" ) == 0 ) {
                config.cmd = argv[optind];
                if (!config.online) config.online = 1;
                config.before = bFork = bNice = SYSLOG = true;
            }
            else if (strcmp(argv[optind], "edited" ) == 0 ) {
                config.cmd = argv[optind];
                bEdited = true;
            }
            else if (strcmp(argv[optind], "nice" ) == 0 ) {
                config.cmd = argv[optind];
                bNice = true;
            }
            else if (strcmp(argv[optind], "-" ) == 0 ) {
                config.cmd = argv[optind];
                bImmediateCall = true;
            }
            else {
                if (strstr(argv[optind], ".rec") != nullptr ) {
                    recDir = realpath(argv[optind], nullptr);
                    if (!recDir) {
                        fprintf(stderr, "invalid recording directory: %s\n", argv[optind]);
                        return EXIT_FAILURE;
                    }
                    config.recDir = recDir;
                }
            }
            optind++;
        }
    }

    // set defaults
    if (config.logFile[0] == 0) {
        strncpy(config.logFile, "markad.log", sizeof(config.logFile));
        config.logFile[sizeof("markad.log") - 1] = 0;
    }

    if (bEdited) return EXIT_SUCCESS; // do nothing if called from vdr before/after the video is cutted
    if ((bAfter) && (config.online > 0)) { // online not valid together with after
        fprintf(stderr, "parameter --online is invalid for start markad after recording end\n");
        return EXIT_FAILURE;
    }
    if ((config.before) && (config.online == 1) && recDir && (!strchr(recDir, '@'))) return EXIT_SUCCESS;

    // we can run, if one of bImmediateCall, bAfter, bBefore or bNice is true
    // and recDir is given
    if ( (bImmediateCall || config.before || bAfter || bNice) && recDir ) {
        // if bFork is given go in background
        if ( bFork ) {
#ifdef POSIX
            //close_files();
            pid_t pid = fork();
            if (pid < 0) {
                const char *err = strerror(errno);
                fprintf(stderr, "%s\n", err);
                return EXIT_FAILURE;
            }
            if (pid != 0) {
                return EXIT_SUCCESS; // initial program immediately returns
            }
            if (chdir("/") == -1) {
                perror("chdir");
                exit(EXIT_FAILURE);
            }
            if (setsid() == (pid_t)(-1)) {
                perror("setsid");
                exit(EXIT_FAILURE);
            }
            if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
                perror("signal(SIGHUP, SIG_IGN)");
                errno = 0;
            }
            int f;

            f = open("/dev/null", O_RDONLY);
            if (f == -1) {
                perror("/dev/null");
                errno = 0;
            }
            else {
                if (dup2(f, fileno(stdin)) == -1) {
                    perror("dup2");
                    errno = 0;
                }
                (void)close(f);
            }

            f = open("/dev/null", O_WRONLY);
            if (f == -1) {
                perror("/dev/null");
                errno = 0;
            }
            else {
                if (dup2(f, fileno(stdout)) == -1) {
                    perror("dup2");
                    errno = 0;
                }
                if (dup2(f, fileno(stderr)) == -1) {
                    perror("dup2");
                    errno = 0;
                }
                (void)close(f);
            }
#else
            std::cerr << "fork is unsupported on WIN32." << std::endl;
#endif /* ifdef POSIX */
        }

        (void)umask((mode_t)0022);

        int MaxPossibleFileDescriptors = getdtablesize();
        for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
            close(i); //close all dup'ed filedescriptors

        // should we renice ?
        if (bNice) {
            if (setpriority(PRIO_PROCESS, 0, niceLevel) == -1) {
                fprintf(stderr, "failed to set nice to %d\n", niceLevel);
            }
            if (ioprio_set(1,getpid(), ioprio | ioprio_class << 13) == -1) {
                fprintf(stderr, "failed to set ioprio to %i,%i\n", ioprio_class, ioprio);
            }
        }
        // store the real values, maybe set by calling nice
        errno = 0;
        int PrioProcess = getpriority(PRIO_PROCESS, 0);
        if ( errno ) {  // use errno because -1 is a valid return value
            fprintf(stderr,"failed to get nice value\n");
        }
        int IOPrio = ioprio_get(1, getpid());
        if (IOPrio < 0) {
            fprintf(stderr,"failed to get ioprio, rc = %d\n", IOPrio);
        }
        else IOPrio = IOPrio >> 13;

        // now do the work...
        struct stat statbuf;
        if (stat(recDir, &statbuf) == -1) {
            fprintf(stderr,"%s not found\n", recDir);
            return EXIT_FAILURE;
        }

        if (!S_ISDIR(statbuf.st_mode)) {
            fprintf(stderr, "%s is not a directory\n", recDir);
            return EXIT_FAILURE;
        }

        if (access(recDir, W_OK|R_OK) == -1) {
            fprintf(stderr,"cannot access %s\n", recDir);
            return EXIT_FAILURE;
        }

        // ignore some signals
        signal(SIGHUP, SIG_IGN);

        // catch some signals
        signal(SIGINT, signal_handler);
        signal(SIGTERM, signal_handler);
        signal(SIGSEGV, signal_handler);
        signal(SIGABRT, signal_handler);
#ifdef POSIX
        signal(SIGUSR1, signal_handler);
        signal(SIGTSTP, signal_handler);
        signal(SIGCONT, signal_handler);
#endif /* ifdef POSIX */

        // init cMarkAdStandalone here, we need now log to recording
        cMarkAdStandalone *cmasta = new cMarkAdStandalone(recDir, &config);
        if (!cmasta) return EXIT_FAILURE;
        ALLOC(sizeof(*cmasta), "cmasta");

        dsyslog("parameter --loglevel is set to %i", SysLogLevel);

        if (niceLevel != 19) {
            isyslog("parameter --priority %i", niceLevel);
            isyslog("warning: increasing priority may affect other applications");
        }
        if (ioprio_class != 3) {
            isyslog("parameter --ioprio %i", ioprio_class);
            isyslog("warning: increasing priority may affect other applications");
        }
        dsyslog("markad process nice level %i", PrioProcess);
        dsyslog("markad IO priority class  %i",IOPrio);

        dsyslog("parameter --logocachedir is set to %s", config.logoCacheDirectory);
        dsyslog("parameter --threads is set to %i", config.threads);
        if (LOG2REC) dsyslog("parameter --log2rec is set");

        if (config.useVPS) {
            dsyslog("parameter --vps is set");
        }
        if (config.MarkadCut) {
            dsyslog("parameter --cut is set");
        }
        if (config.ac3ReEncode) {
            dsyslog("parameter --ac3reencode is set");
            if (!config.MarkadCut) {
                esyslog("--cut is not set, ignoring --ac3reencode");
                config.ac3ReEncode = false;
            }
        }
        dsyslog("parameter --autologo is set to %i",config.autoLogo);
        if (config.fullDecode) {
            dsyslog("parameter --fulldecode is set");
        }
        if (config.smartEncode) {
            dsyslog("parameter --smartencode is set");
        }
        if (config.fullEncode) {
            dsyslog("parameter --fullencode is set");
            if (config.bestEncode) dsyslog("encode best streams");
            else dsyslog("encode all streams");
        }
        if (config.hwaccel[0] != 0) dsyslog("parameter --hwaccel=%s is set", config.hwaccel);
        else dsyslog("use software decoder/encoder");

        if (config.logoExtraction == -1) {
            // performance test
            if (!abortNow && config.perftest) {
                cTest *test = new cTest(config.recDir, config.fullDecode, config.hwaccel);
                test->Perf();
                delete test;
            }
            else {

                // detect marks
                if (!abortNow) cmasta->Recording();

                // logo mark optimization
                if (!abortNow) cmasta->LogoMarkOptimization();      // logo mark optimization

                // overlap detection
                if (!abortNow) cmasta->ProcessOverlap();            // overlap and closing credits detection

                // minor mark position optimization
                if (!abortNow) cmasta->BlackScreenOptimization();   // mark optimization with black scene
                if (!abortNow) cmasta->SilenceOptimization();       // mark optimization with mute scene
                if (!abortNow) cmasta->LowerBorderOptimization();   // mark optimization with lower border
                if (!abortNow) cmasta->SceneChangeOptimization();   // final optimization with scene changes (if we habe nothing else, try this as last resort)

                // video cut
                if (!abortNow) if (config.MarkadCut) cmasta->MarkadCut();

                // write debug mark pictures
#ifdef DEBUG_MARK_FRAMES
                if (!abortNow) cmasta->DebugMarkFrames(); // write frames picture of marks to recording directory
#endif

            }
        }
        FREE(sizeof(*cmasta), "cmasta");
        delete cmasta;
        cmasta = nullptr;

#ifdef DEBUG_MEM
        memList();
#endif
        return EXIT_SUCCESS;
    }
    return usage(config.svdrpport);
}