File: xineDevice.c

package info (click to toggle)
vdr-plugin-xine 0.9.4-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,756 kB
  • sloc: ansic: 14,014; makefile: 86; sh: 34
file content (4486 lines) | stat: -rw-r--r-- 114,717 bytes parent folder | download | duplicates (3)
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

#include "xineCommon.h"

#include <vdr/remux.h>
#include <vdr/transfer.h>

#if APIVERSNUM >= 10703
#include "vdr172remux.h"
#define VDR172 ::vdr172
#else
#define VDR172
#endif

#include "xineDevice.h"
#include "xineOsd.h"
#include "xineSettings.h"


//#define LOG_ME(x) x
#define LOG_ME(x)


namespace PluginXine
{
  void cXineSpuDecoder::ptsAdjust(uint32_t &pts)
  {
    if (0 == pts
        || -1 == (int32_t)pts)
    {
      return;
    }
    
    const int64_t ptsXine = m_xineDevice->GetSTC();

    if (-1 == ptsXine)
      return;
    
//    ::fprintf(stderr, "ptsAdjust: %ld, %lld, %lld\n", pts, ptsXine, pts - ptsXine);

    pts = (uint32_t)ptsXine;
  }
  
  int cXineSpuDecoder::setTime(uint32_t pts)
  {
    ptsAdjust(pts);
    
    return cDvbSpuDecoder::setTime(pts);
  }

  static cXineDevice *theXineDevice = 0;

  bool cXineDevice::HasDecoder(void) const
  {
    return true;
  }

  cSpuDecoder *cXineDevice::GetSpuDecoder(void)
  {
    if (!m_spuDecoder
        && IsPrimaryDevice())
    {
      m_spuDecoder = new cXineSpuDecoder(this);
    }
    
    return m_spuDecoder;
  }

  bool cXineDevice::CanReplay(void) const
  {
    return true;
  }

  static bool findVideo = false;
  static bool foundVideo = false;
  static int ts = 0;
  static bool f = false;
  static bool np = false;
  static bool muted = false;
  static int jumboPESsize = 0;
  static int jumboPEStail = 0;
  static ePlayMode pm = pmNone;
  static bool audioSeen = false;

  static int64_t ptsV = -1, ptsA = -1, ptsP = -1, ptsD = -1;
  
  static cMutex softStartMutex;
  static enum { sstNone = 0, sstNormal, sstNoMetronom } softStartTrigger = sstNone;
  static enum
  {
    sIdle
    , sInitiateSequence
    , sStartSequence
    , sContinueSequence
  }
  softStartState = sIdle;

  double tNow()
  {
    timeval tv;
    ::gettimeofday(&tv, 0);
    return tv.tv_sec + tv.tv_usec / 1.0e+6; 
  }

  static double ttt0 = tNow(), ttt1 = tNow(), ttt2 = 0, ttt3 = 0, ttt4 = 0, ttt5 = 0, ttt6 = 0;

//  static int streams[ 256 ];
  static bool doClear = false;

  bool cXineDevice::SetPlayMode(ePlayMode PlayMode)
  {
    if (pmNone == PlayMode) 
    {
      doClear = true;
      ttt0 = tNow();
    }
    else 
      ttt2 = tNow();
/*
    timeval tv0;
    ::gettimeofday(&tv0, 0);
*/
    if (0)
    {
      time_t t1 = time(0);
      static time_t t0 = t1;

      if (0 == PlayMode && (t1 - t0) > (30 * 60))
        *(char *)0 = 0;
      
      t0 = t1;
    }

    bool playModeSupported = false;

    switch (PlayMode)
    {
    case pmNone:
    case pmAudioVideo:
    case pmAudioOnlyBlack:
    case pmExtern_THIS_SHOULD_BE_AVOIDED:
      playModeSupported = true;
      break;
    case pmAudioOnly:
#if APIVERSNUM >= 10308
    case pmVideoOnly:
#endif
      break;
    }

    ptsV = ptsA = ptsP = ptsD = -1;
    
    ts = 0;
    np = 0;
    f  = 0;
    
    xfprintf(stderr, "SetPlayMode: %d\n", PlayMode);

    if (pmExtern_THIS_SHOULD_BE_AVOIDED == pm
      && pmNone == PlayMode)
    {
      m_xineLib.enableExternal(false);
    }
 
    m_xineLib.pause(false);
    m_xineLib.execFuncTrickSpeedMode(false);
    m_xineLib.execFuncSetSpeed(100.0);

    if (muted)
    {
      muted = false;
      m_xineLib.execFuncMute(false);
    } 

    if (pmNone == PlayMode)
    {
      pm = PlayMode;
    
      jumboPESsize = 0;
      jumboPEStail = 0;
/*
      for (unsigned int i = 0; i < sizeof (streams) / sizeof (*streams); i++)
      {
        if (streams[ i ])
          fprintf(stderr, "stream: 0x%02x\n", i);
      }
*/
      
      m_xineLib.ignore();
//      m_xineLib.execFuncMute();
//      m_xineLib.execFuncSetPrebuffer(0);
      m_xineLib.execFuncClear(-2);
//      m_xineLib.execFuncStart();
//      m_xineLib.execFuncMetronom(0);
      m_xineLib.execFuncStillFrame();
      m_xineLib.execFuncWait();

//      for (int i = 0; i < 2; i++)
        m_xineLib.showNoSignal();

      PushOut();
      m_xineLib.execFuncFlush();

      {
        cMutexLock lock(&softStartMutex);
        softStartTrigger = sstNone;
        softStartState = sIdle;
      }

      foundVideo = false;
      findVideo = false;
    }
    else
    {
      audioSeen = false;
      {
        cMutexLock lock(&softStartMutex);
        softStartTrigger = sstNone;
        softStartState = sIdle;
      }
//      ::memset(&streams, 0, sizeof (streams));
      
      m_xineLib.freeze();
      m_xineLib.ignore(false);

      m_xineLib.freeze(false);
/*
      PushOut();
      m_xineLib.execFuncFlush();
      m_xineLib.execFuncWait();
*/      
//      m_xineLib.execFuncSetPrebuffer(m_settings.GetModeParams()->m_prebufferFrames);
//      m_xineLib.execFuncSetPrebuffer(0);
      m_xineLib.execFuncClear(-4);
//      m_xineLib.execFuncStart();
      m_xineLib.execFuncWait();

#if APIVERSNUM < 10342
      m_settings.SelectReplayPrebufferMode(0 == cTransferControl::ReceiverDevice());
#else
      m_settings.SelectReplayPrebufferMode(!Transferring());
#endif
      
      if (m_settings.LiveTV())
      {
        cMutexLock lock(&softStartMutex);
//        ::fprintf(stderr, "LiveTV\n");
        softStartTrigger = sstNormal;
      }
      else
        np = true;

      foundVideo = false;
      findVideo = true;

      cMutexLock pmMutexLock(&m_pmMutex);
      pm = PlayMode;
      m_pmCondVar.Broadcast();
    }
    
    if (pmExtern_THIS_SHOULD_BE_AVOIDED == PlayMode)
      m_xineLib.enableExternal();
/*    
    timeval tv1;
    ::gettimeofday(&tv1, 0);

    fprintf(stderr, "spm: %.3lf ms\n", 1000 * ((tv1.tv_sec + tv1.tv_usec / 1.0e+6) - (tv0.tv_sec + tv0.tv_usec / 1.0e+6)));
*/
    if (pmNone == PlayMode) {
      ttt1 = tNow(); ttt4 = 0; ttt5 = 0; }
    else
      ttt3 = tNow();
    return playModeSupported;
  }

  void cXineDevice::DiscontinuityDetected()
  {
    if (m_settings.LiveTV())
    {
      cMutexLock lock(&softStartMutex);
      if (softStartState == sIdle
        && softStartTrigger == sstNone
        && pm != pmNone)
      {
        softStartTrigger = sstNormal;
        xfprintf(stderr, "DiscontinuityDetected: triggering soft start\n");
      }
    }
  }

  static bool lastCmdWasClear = false;

  bool cXineDevice::HasIBPTrickSpeed(void)
  {
/*
#if APIVERSNUM >= 10706
    return true;
#else
*/
    return false;
//#endif
  }

  void cXineDevice::TrickSpeed(int Speed, bool Forward)
  {
    f = false;
    ts = Speed;

    xfprintf(stderr, "TrickSpeed: %d\n", Speed);
    m_xineLib.execFuncTrickSpeedMode(lastCmdWasClear);
    m_xineLib.execFuncSetSpeed(100.0 / Speed);
    m_xineLib.execFuncWait();
    m_xineLib.freeze(false);
    m_xineLib.pause(false);
  }

  void cXineDevice::Clear(void)
  {
    lastCmdWasClear = true;

    doClear = true;
    ptsV = ptsA = ptsP = ptsD = -1;
    
    static int cntClear = 0;

    xfprintf(stderr, "Clear(%d)", cntClear);

    m_xineLib.pause();
    
    jumboPESsize = 0;
    jumboPEStail = 0;

    if (f)
        m_xineLib.execFuncSetSpeed(100.0);

    m_xineLib.execFuncClear(cntClear++);
//    m_xineLib.execFuncStart();
    np = true;
    
    if (f)
        m_xineLib.execFuncSetSpeed(0.0);
    
    m_xineLib.execFuncWait();
    m_xineLib.pause(false);
  xfprintf(stderr, "!\n");
    if (m_settings.LiveTV())
    {
      cMutexLock lock(&softStartMutex);
      softStartTrigger = sstNoMetronom;
    }
   
    cDevice::Clear();
  }

  void cXineDevice::Play(void)
  {
    lastCmdWasClear = false;

    f = false;
    ts = 0;

    xfprintf(stderr, "Play\n");
    m_xineLib.execFuncTrickSpeedMode(false);
    m_xineLib.execFuncSetSpeed(100.0);

    if (muted)
    {
      muted = false;
      m_xineLib.execFuncMute(false);
    }
    
    m_xineLib.execFuncWait();
    m_xineLib.freeze(false);
    m_xineLib.pause(false);
    LOG_ME(::fprintf(stderr, "----\n");)
  }

  void cXineDevice::Freeze(void)
  {
    lastCmdWasClear = false;

    f = true;
    
    xfprintf(stderr, "Freeze\n");  
    m_xineLib.freeze();
    m_xineLib.pause();
    m_xineLib.execFuncSetSpeed(0.0);
    m_xineLib.execFuncWait();
    LOG_ME(::fprintf(stderr, "------\n");)  
  }

  void cXineDevice::Mute(void)
  {
    xfprintf(stderr, "Mute\n");
    m_xineLib.execFuncMute(true);

    muted = true;
  }

  static void store_frame(const unsigned char *buf, int len, int line)
  {
    if (0)
    {
      static int cnt = 0;
      
      char name[ 100 ];
      ::sprintf(name, "/tmp/frame_%05d_%05d", line, cnt++);
      
      FILE *f = fopen(name, "wb");
      size_t r = fwrite(buf, 1, len, f);
      (void)r;
      fclose(f);
    }
  }
  
#define VERBOSE_NOP() do{ xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); } while (0)
#define VERBOSE_NOP1() do{ store_frame(Data, Length, __LINE__); xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); } while (0)
#define VERBOSE_RETURN0(x) do{ xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); return x; } while (0)
#define VERBOSE_RETURN1(x) do{ store_frame(buf0, len0, __LINE__); xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); return x; } while (0)
#define VERBOSE_RETURN2(x) do{ store_frame(buf, len, __LINE__); xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); return x; } while (0)
#define VERBOSE_RETURN3(x) do{ store_frame(Data, Length, __LINE__); xfprintf(stderr, "FIXME: %s:%d\n", __FILE__, __LINE__); return x; } while (0)
  
#if APIVERSNUM < 10331

enum ePesHeader {
  phNeedMoreData = -1,
  phInvalid = 0,
  phMPEG1 = 1,
  phMPEG2 = 2
  };

static ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader = NULL)
{
  if (Count < 7)
     return phNeedMoreData; // too short

  if ((Data[6] & 0xC0) == 0x80) { // MPEG 2
     if (Count < 9)
        return phNeedMoreData; // too short

     PesPayloadOffset = 6 + 3 + Data[8];
     if (Count < PesPayloadOffset)
        return phNeedMoreData; // too short

     if (ContinuationHeader)
        *ContinuationHeader = ((Data[6] == 0x80) && !Data[7] && !Data[8]);

     return phMPEG2; // MPEG 2
     }

  // check for MPEG 1 ...
  PesPayloadOffset = 6;

  // skip up to 16 stuffing bytes
  for (int i = 0; i < 16; i++) {
      if (Data[PesPayloadOffset] != 0xFF)
         break;

      if (Count <= ++PesPayloadOffset)
         return phNeedMoreData; // too short
      }

  // skip STD_buffer_scale/size
  if ((Data[PesPayloadOffset] & 0xC0) == 0x40) {
     PesPayloadOffset += 2;

     if (Count <= PesPayloadOffset)
        return phNeedMoreData; // too short
     }

  if (ContinuationHeader)
     *ContinuationHeader = false;

  if ((Data[PesPayloadOffset] & 0xF0) == 0x20) {
     // skip PTS only
     PesPayloadOffset += 5;
     }
  else if ((Data[PesPayloadOffset] & 0xF0) == 0x30) {
     // skip PTS and DTS
     PesPayloadOffset += 10;
     }
  else if (Data[PesPayloadOffset] == 0x0F) {
     // continuation header
     PesPayloadOffset++;

     if (ContinuationHeader)
        *ContinuationHeader = true;
     }
  else
     return phInvalid; // unknown

  if (Count < PesPayloadOffset)
     return phNeedMoreData; // too short

  return phMPEG1; // MPEG 1
}

#endif 
 
//#if APIVERSNUM < 10345

  namespace cRemux
  {
// Start codes:
#define SC_SEQUENCE 0xB3  // "sequence header code"
#define SC_GROUP    0xB8  // "group start code"
#define SC_PICTURE  0x00  // "picture start code"

    int GetPacketLength(const uchar *Data, int Count, int Offset)
    {
      // Returns the length of the packet starting at Offset, or -1 if Count is
      // too small to contain the entire packet.
      int Length = (Offset + 5 < Count) ? (Data[Offset + 4] << 8) + Data[Offset + 5] + 6 : -1;
      if (Length > 0 && Offset + Length <= Count)
         return Length;
      return -1;
    }

bool IsFrameH264(const uchar *Data, int Length)
{
  int PesPayloadOffset;
  const uchar *limit = Data + Length;
  if (AnalyzePesHeader(Data, Length, PesPayloadOffset) <= phInvalid)
     return false; // neither MPEG1 nor MPEG2

  Data += PesPayloadOffset + 3; // move to video payload and skip 00 00 01
  if (Data < limit) {
     // cVideoRepacker ensures that in case of H264 we will see an access unit delimiter here
     if (0x01 == Data[-1] && 9 == Data[0] && 0x00 == Data[-2] && 0x00 == Data[-3])
        return true;
     }

  return false;
}

int ScanVideoPacket(const uchar *Data, int Count, int Offset, uchar &PictureType)
{
  // Scans the video packet starting at Offset and returns its length.
  // If the return value is -1 the packet was not completely in the buffer.
  int Length = GetPacketLength(Data, Count, Offset);
  if (Length > 0) {
     const bool h264 = IsFrameH264(Data + Offset, Length);
     int PesPayloadOffset = 0;
     if (AnalyzePesHeader(Data + Offset, Length, PesPayloadOffset) >= phMPEG1) {
        const uchar *p = Data + Offset + PesPayloadOffset + 2;
        const uchar *pLimit = Data + Offset + Length - 3;
#if APIVERSNUM >= 10326
        // cVideoRepacker ensures that a new PES packet is started for a new sequence,
        // group or picture which allows us to easily skip scanning through a huge
        // amount of video data.
        if (p < pLimit) {
           if (p[-2] || p[-1] || p[0] != 0x01)
              pLimit = 0; // skip scanning: packet doesn't start with 0x000001
           else {
              if (h264) {
                 int nal_unit_type = p[1] & 0x1F;
                 switch (nal_unit_type) {
                   case 9: // access unit delimiter
                        // when the MSB in p[1] is set (which violates H.264) then this is a hint
                        // from cVideoRepacker::HandleNalUnit() that this bottom field shall not
                        // be reported as picture.
                        if (p[1] & 0x80)
                           ((uchar *)p)[1] &= ~0x80; // revert the hint and fall through
                        else
                           break;
                   default: // skip scanning: packet doesn't start a new picture
                        pLimit = 0;
                   }
                 }
              else {
                 switch (p[1]) {
                   case SC_SEQUENCE:
                   case SC_GROUP:
                   case SC_PICTURE:
                        break;
                   default: // skip scanning: packet doesn't start a new sequence, group or picture
                        pLimit = 0;
                   }
                 }
              }
           }
#endif
        while (p < pLimit && (p = (const uchar *)memchr(p, 0x01, pLimit - p))) {
              if (!p[-2] && !p[-1]) { // found 0x000001
                 if (h264) {
                    int nal_unit_type = p[1] & 0x1F;
                    switch (nal_unit_type) {
                      case 9: { // access unit delimiter
                              int primary_pic_type = p[2] >> 5;
                              switch (primary_pic_type) {
                                case 0: // I
                                case 3: // SI
                                case 5: // I, SI
                                     PictureType = I_FRAME;
                                     break;
                                case 1: // I, P
                                case 4: // SI, SP
                                case 6: // I, SI, P, SP
                                     PictureType = P_FRAME;
                                     break;
                                case 2: // I, P, B
                                case 7: // I, SI, P, SP, B
                                     PictureType = B_FRAME;
                                     break;
                                }
                              return Length;
                              }
                      }
                    }
                 else {
                    switch (p[1]) {
                      case SC_PICTURE: PictureType = (p[3] >> 3) & 0x07;
                                       return Length;
                      }
                    }
                 p += 4; // continue scanning after 0x01ssxxyy
                 }
              else
                 p += 3; // continue scanning after 0x01xxyy
              }
        }
     PictureType = NO_PICTURE;
     return Length;
     }
  return -1;
}
  }

//#endif

/*
  static bool IsNotVideoIorPframe(const uchar *buf, int len)
  {
    if (0xe0 != (0xf0 & buf[ 3 ]))     // not video
      return true;

    uchar pt = NO_PICTURE;
    cRemux::ScanVideoPacket(buf, len, 0, pt);

    return (I_FRAME == pt || P_FRAME == pt);
  }
*/
/*
  static char *getFrameType(const uchar *buf, int len)
  {
    if (0xe0 != (0xf0 & buf[ 3 ]))     // not video
      return "";

    static char *frameTypes[ 8 ] = 
    {
      "",
      "i",
      "p",
      "b",
      "4",
      "5",
      "6",
      "7"
    };

    uchar pt = NO_PICTURE;
    cRemux::ScanVideoPacket(buf, len, 0, pt);

    return frameTypes[ pt ];
  }
*/
  static bool getPTS(const unsigned char *buf0, int len0, int64_t &pts)
  {
    while (len0 > 0)
    {
      while (len0 > 3
             && 0x00 == buf0[ 0 ]
             && 0x00 == buf0[ 1 ]
             && 0x00 == buf0[ 2 ])
      {
        buf0++;
        len0--;
      }
      
      if (3 == len0
          && 0x00 == buf0[ 0 ]
          && 0x00 == buf0[ 1 ]
          && 0x00 == buf0[ 2 ])
      {
        break;
      }
      
      if (len0 < 6)
        VERBOSE_RETURN1(false);
      
      if (0x00 != buf0[ 0 ]
          || 0x00 != buf0[ 1 ]
          || 0x01 != buf0[ 2 ])
      {
        VERBOSE_RETURN1(false);
      }
      
      if (0xe0 != (0xf0 & buf0[ 3 ])      // video
          && 0xc0 != (0xe0 & buf0[ 3 ])   // audio
          && 0xbd != (0xff & buf0[ 3 ])   // dolby
          && 0xbe != (0xff & buf0[ 3 ]))  // padding (DVD)
      {
        VERBOSE_RETURN1(false);
      }
      
      int len = (6 + buf0[ 4 ] * 0x100 + buf0[ 5 ]);
      if (len > len0)
        VERBOSE_RETURN1(false);
      
      const unsigned char *buf = buf0;
      buf0 += len;
      len0 -= len;

//      if (len0 != 0)
//        VERBOSE_NOP();
      
      if (0xbe == (0xff & buf[ 3 ]))  // padding (DVD)
        continue;
      
      if (len < (6 + 3))
        VERBOSE_RETURN2(false);
      
      if (0x80 != (0xc0 & buf[ 6 ]))  // MPEG1
      {
        do // ... while (false);
        {
          int o = 0;

          for (int i = 0; i < 16; i++)
          {
             if (buf[ 6 + o ] != 0xff)
               break;

             if (len < (6 + ++o))
               VERBOSE_RETURN2(false);
          }

          if (0x40 == (0xc0 & buf[ 6 + o ]))
            o += 2;
        
          if (len < (6 + o))
            VERBOSE_RETURN2(false);
          
          if (0x31 == (0xf1 & buf[ 6 + o + 0 ]))
          {
            if (len < (6 + o + 5 + 5))
              VERBOSE_RETURN2(false);
          
            if (0x01 != (0x01 & buf[ 6 + o + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 4 ]))
              VERBOSE_RETURN2(false);
            
            if (0x11 != (0xf1 & buf[ 6 + o + 5 + 0 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 5 + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 5 + 4 ]))
              VERBOSE_RETURN2(false);
            
            int64_t _pts = ((int64_t)(0x0e & buf[ 6 + o + 0 ])) << 29
              |                      (0xff & buf[ 6 + o + 1 ])  << 22
              |                      (0xfe & buf[ 6 + o + 2 ])  << 14
              |                      (0xff & buf[ 6 + o + 3 ])  << 7
              |                      (0xfe & buf[ 6 + o + 4 ])  >> 1;
            
//            ::fprintf(stderr, "pts: %lld\n", _pts);
            
            if (0 == _pts)
              break;
            
//            if (!IsNotVideoIorPframe(buf, len)) // only PTS of I and P frames are progressive in time
//              break;
            
            pts = _pts;
            
            return true;
          }
          else if (0x21 == (0xf1 & buf[ 6 + o + 0 ]))
          {
            if (len < (6 + o + 5))
              VERBOSE_RETURN2(false);
          
            if (0x01 != (0x01 & buf[ 6 + o + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 4 ]))
              VERBOSE_RETURN2(false);
            
            int64_t _pts = ((int64_t)(0x0e & buf[ 6 + o + 0 ])) << 29
              |                      (0xff & buf[ 6 + o + 1 ])  << 22
              |                      (0xfe & buf[ 6 + o + 2 ])  << 14
              |                      (0xff & buf[ 6 + o + 3 ])  << 7
              |                      (0xfe & buf[ 6 + o + 4 ])  >> 1;
            
//            ::fprintf(stderr, "pts: %lld\n", _pts);
            
            if (0 == _pts)
              break;

//            if (!IsNotVideoIorPframe(buf, len)) // only PTS of I and P frames are progressive in time
//              break;
            
            pts = _pts;
            
            return true;
          }
          else if (0x0f == (0xff & buf[ 6 + o + 0 ]))
          {
            break;
          }
          
          for (int i = 0; i < 30; i++)
            xfprintf(stderr, "%02x ", buf[ i ]);
          xfprintf(stderr, "\n");
          
          VERBOSE_RETURN2(false);
        }
        while (false);

        continue;
      }
      
      if (0x40 == (0xc0 & buf[ 7 ]))
        VERBOSE_RETURN2(false);
      
      if (0x00 == (0xc0 & buf[ 7 ]))
        continue;

// ignore      
//      if (0x00 != (0x3f & buf[ 7 ]))
//        VERBOSE_RETURN2(false);
      
      bool hasPTS = (0 != (0x80 & buf[ 7 ]));
      bool hasDTS = (0 != (0x40 & buf[ 7 ]));
      
      unsigned char hdl = buf[ 8 ];
      
      if (hdl < ((hasPTS + hasDTS) * 5))
        VERBOSE_RETURN2(false);
      
      if (len < (6 + 3 + hdl))
        VERBOSE_RETURN2(false);
      
      if ((0x20 * hasPTS + 0x10 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
      {
        if ((0x20 * hasPTS + 0x00 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
        {
          // accept streams, that start with '00X0' instead of '00X1'.
        }
        else if ((0x00 * hasPTS + 0x10 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
        {
          // accept streams, that start with '000X' instead of '001X'.
        }
        else
        {
fprintf(stderr, "buf:"); for (int i = 0; i < 6 + 3 + hdl; i++) fprintf(stderr, " %02x", buf[i]); fprintf(stderr, "\n");
          VERBOSE_RETURN2(false);
        }
      }
      
      if (0x01 != (0x01 & buf[ 11 ]))
        VERBOSE_RETURN2(false);
      
      if (0x01 != (0x01 & buf[ 13 ]))
        VERBOSE_RETURN2(false);
      
      if (hasDTS)
      {
        if (0x11 != (0xf1 & buf[ 14 ]))
        {
          if (0x21 == (0xf1 & buf[ 14 ]))
          {
            // accept streams, that start with '0010' instead of '0001'.
          }
          else if (0xa1 == (0xf1 & buf[ 14 ]))
          {
            // accept streams, that start with '1010' instead of '0001'.
          }
          else
          {
fprintf(stderr, "buf:"); for (int i = 0; i < 6 + 3 + hdl; i++) fprintf(stderr, " %02x", buf[i]); fprintf(stderr, "\n");
            VERBOSE_RETURN2(false);
          }
        }

// accept streams that have no marker bits set                  
//        if (0x01 != (0x01 & buf[ 16 ]))
//          VERBOSE_RETURN2(false);
              
//        if (0x01 != (0x01 & buf[ 18 ]))
//          VERBOSE_RETURN2(false);
      }
/*
      fprintf(stderr, " %02x %02x %02x %02x %02x\n"
              , buf[  9 ]
              , buf[ 10 ]
              , buf[ 11 ]
              , buf[ 12 ]
              , buf[ 13 ]);
*/
      int64_t _pts = ((int64_t)(0x0e & buf[  9 ])) << 29
        |                      (0xff & buf[ 10 ])  << 22
        |                      (0xfe & buf[ 11 ])  << 14
        |                      (0xff & buf[ 12 ])  << 7
        |                      (0xfe & buf[ 13 ])  >> 1;

      if (0 == _pts)
        return false;

//      if (!IsNotVideoIorPframe(buf, len)) // only PTS of I and P frames are progressive in time
//        return false;
            
      pts = _pts;
      
      return true;
    }

//    VERBOSE_RETURN2(false);
    return false;
  }
/*
  static bool stripPTSandDTS(unsigned char *buf0, int len0)
  {
    while (len0 > 0)
    {
      while (len0 > 3
             && 0x00 == buf0[ 0 ]
             && 0x00 == buf0[ 1 ]
             && 0x00 == buf0[ 2 ])
      {
        buf0++;
        len0--;
      }
      
      if (3 == len0
          && 0x00 == buf0[ 0 ]
          && 0x00 == buf0[ 1 ]
          && 0x00 == buf0[ 2 ])
      {
        break;
      }
      
      if (len0 < 6)
        VERBOSE_RETURN1(false);
      
      if (0x00 != buf0[ 0 ]
          || 0x00 != buf0[ 1 ]
          || 0x01 != buf0[ 2 ])
      {
        VERBOSE_RETURN1(false);
      }
      
      if (0xe0 != (0xf0 & buf0[ 3 ])      // video
          && 0xc0 != (0xe0 & buf0[ 3 ])   // audio
          && 0xbd != (0xff & buf0[ 3 ])   // dolby
          && 0xbe != (0xff & buf0[ 3 ])   // padding (DVD)
          && 0xba != (0xff & buf0[ 3 ])   // system layer: pack header
          && 0xbb != (0xff & buf0[ 3 ])   // system layer: system header
          && 0xb9 != (0xff & buf0[ 3 ]))  // system layer: stream end
      {
//fprintf(stderr, "buf0[ 3 ]: %02x\n", buf0[ 3 ]);
        VERBOSE_RETURN1(false);
      }

      int len = (6 + buf0[ 4 ] * 0x100 + buf0[ 5 ]);
      if (0xba == buf0[ 3 ]) // pack header has fixed length
      {
        if (0x00 == (0xc0 & buf0[ 4 ])) // MPEG 1
          len = 12;
        else // MPEG 2
          len = 14 + (buf0[ 13 ] & 0x07);
      }
      else if (0xb9 == buf0[ 3 ]) // stream end has fixed length
      {
        len = 4;
      }

      if (len > len0)
        VERBOSE_RETURN1(false);
      
      unsigned char *buf = buf0;
      buf0 += len;
      len0 -= len;

//      if (len0 != 0)
//        VERBOSE_NOP();
      
      if (0xbe == (0xff & buf[ 3 ])    // padding (DVD)
        || 0xba == (0xff & buf[ 3 ])   // system layer: pack header
        || 0xbb == (0xff & buf[ 3 ])   // system layer: system header
        || 0xb9 == (0xff & buf[ 3 ]))  // system layer: stream end
      {
        continue;
      }
      
      if (len < (6 + 3))
        VERBOSE_RETURN2(false);
      
      if (0x80 != (0xc0 & buf[ 6 ]))  // MPEG1
      {
        bool ok = false;
        
        do // ... while (false);
        {
          int o = 0;

          for (int i = 0; i < 16; i++)
          {
             if (buf[ 6 + o ] != 0xff)
               break;

             if (len < (6 + ++o))
               VERBOSE_RETURN2(false);
          }

          if (0x40 == (0xc0 & buf[ 6 + o ]))
            o += 2;
        
          if (len < (6 + o))
            VERBOSE_RETURN2(false);
          
          if (0x31 == (0xf1 & buf[ 6 + o + 0 ]))
          {
            if (0x01 != (0x01 & buf[ 6 + o + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 4 ]))
              VERBOSE_RETURN2(false);
            
            if (0x11 != (0xf1 & buf[ 6 + o + 5 + 0 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 5 + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 5 + 4 ]))
              VERBOSE_RETURN2(false);
            
            buf[ 6 + o + 0 ] = 0xff;
            buf[ 6 + o + 1 ] = 0xff;
            buf[ 6 + o + 2 ] = 0xff;
            buf[ 6 + o + 3 ] = 0xff;
            buf[ 6 + o + 4 ] = 0xff;
            
            buf[ 6 + o + 5 + 0 ] = 0xff;
            buf[ 6 + o + 5 + 1 ] = 0xff;
            buf[ 6 + o + 5 + 2 ] = 0xff;
            buf[ 6 + o + 5 + 3 ] = 0xff;
            buf[ 6 + o + 5 + 4 ] = 0x0f;
            
            ok = true;
          }
          else if (0x21 == (0xf1 & buf[ 6 + o + 0 ]))
          {
            if (0x01 != (0x01 & buf[ 6 + o + 2 ]))
              VERBOSE_RETURN2(false);
            
            if (0x01 != (0x01 & buf[ 6 + o + 4 ]))
              VERBOSE_RETURN2(false);
            
            buf[ 6 + o + 0 ] = 0xff;
            buf[ 6 + o + 1 ] = 0xff;
            buf[ 6 + o + 2 ] = 0xff;
            buf[ 6 + o + 3 ] = 0xff;
            buf[ 6 + o + 4 ] = 0x0f;
            
            ok = true;
          }
          else if (0x0f == (0xff & buf[ 6 + o + 0 ]))
          {
            ok = true;
          }

          if (ok)
            break;
            
          for (int i = 0; i < 30; i++)
            xfprintf(stderr, "%02x ", buf[ i ]);
          xfprintf(stderr, "\n");
          
          VERBOSE_RETURN2(false);
        }
        while (false);

        if (ok)
          continue;
      }
      
      if (0x40 == (0xc0 & buf[ 7 ]))
        VERBOSE_RETURN2(false);
      
      if (0x00 == (0xc0 & buf[ 7 ]))
        continue;

// ignore      
//      if (0x00 != (0x3f & buf[ 7 ]))
//        VERBOSE_RETURN2(false);
      
      bool hasPTS = (0 != (0x80 & buf[ 7 ]));
      bool hasDTS = (0 != (0x40 & buf[ 7 ]));
      
      unsigned char hdl = buf[ 8 ];
      
      if (hdl < ((hasPTS + hasDTS) * 5))
        VERBOSE_RETURN2(false);
      
      if (len < (6 + 3 + hdl))
        VERBOSE_RETURN2(false);
      
      if ((0x20 * hasPTS + 0x10 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
      {
        if ((0x20 * hasPTS + 0x00 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
        {
          // accept streams, that start with '00X0' instead of '00X1'.
        }
        else if ((0x00 * hasPTS + 0x10 * hasDTS + 0x01) != (0xf1 & buf[ 9 ]))
        {
          // accept streams, that start with '000X' instead of '001X'.
        }
        else
        {
          VERBOSE_RETURN2(false);
        }
      }
      
      if (0x01 != (0x01 & buf[ 11 ]))
        VERBOSE_RETURN2(false);
      
      if (0x01 != (0x01 & buf[ 13 ]))
        VERBOSE_RETURN2(false);
      
      if (hasDTS)
      {
        if (0x11 != (0xf1 & buf[ 14 ]))
          VERBOSE_RETURN2(false);
        
        if (0x01 != (0x01 & buf[ 16 ]))
          VERBOSE_RETURN2(false);
                  
        if (0x01 != (0x01 & buf[ 18 ]))
          VERBOSE_RETURN2(false);
      }
      
      buf[ 7 ] &= 0x3f;
      
      for (int i = 9; i < (9 + ((hasPTS + hasDTS) * 5)); i++)
        buf[ i ] = 0xff;
    }
    
    return true;
  }
*/
  static uchar padding[ 6 + 0xffff ] =
  {
    0x00
    , 0x00
    , 0x01
    , 0xbe
    , 0xff
    , 0xff
  };

  int cXineDevice::PushOut()
  {
    uchar *Data = padding;
    int Length = sizeof (padding);
    
    return PlayCommon3(Data, Length, -1);
  }
  
//static bool blahblah = false;

  void cXineDevice::StillPicture(const uchar *Data, int Length)
  {
#if APIVERSNUM >= 10701
    if (Length > 0 && Data[0] == 0x47)
    {
const uchar *tsData = Data;
const int tsLength = Length;
uchar *pesData = (uchar *)malloc(Length);
int pesLength = 0;
uchar tsHead[4] = { 0xff, 0xff, 0xff, 0xff };
VDR172::cRemux *vRemux = 0;
cPatPmtParser patPmtParser;
while (Length >= TS_SIZE)
{
  if (TsHasPayload(Data))
  {
    int plo = TsPayloadOffset(Data);
    if (plo < Length)
    {
      int pid = TsPid(Data);
      if (pid == 0)
        patPmtParser.ParsePat(Data, TS_SIZE);
#if VDRVERSNUM < 10733
      else if (pid == patPmtParser.PmtPid())
#else
      else if (patPmtParser.IsPmtPid(pid))
#endif
        patPmtParser.ParsePmt(Data, TS_SIZE);
      else if (pid == patPmtParser.Vpid())
      {
        if (!vRemux)
        {
          vRemux = new VDR172::cRemux(patPmtParser.Vpid(), 0, 0, 0);
          vRemux->SetTimeouts(0, 0);
        }

        memcpy(tsHead, Data, sizeof (tsHead));
        vRemux->Put(Data, TS_SIZE);

        int n = 0;
        uchar *p = vRemux->Get(n);
        if (p)
        {
          memcpy(pesData + pesLength, p, n);
          pesLength += n;
          vRemux->Del(n);
        }
      }
    }
  }

Data += TS_SIZE;
Length -= TS_SIZE;
}

if (!vRemux)
{
  free(pesData);
  return;
}

uchar tsTail[TS_SIZE];
memset(tsTail + sizeof (tsHead), 0xff, sizeof (tsTail) - sizeof (tsHead));
memcpy(tsTail, tsHead, sizeof (tsHead)); 
tsTail[1] &= ~TS_ERROR;
tsTail[1] |= TS_PAYLOAD_START;
tsTail[3] &= ~TS_SCRAMBLING_CONTROL;
tsTail[3] |= TS_ADAPT_FIELD_EXISTS;
tsTail[3] |= TS_PAYLOAD_EXISTS;
tsTail[3] = (tsTail[3] & 0xf0) | (((tsTail[3] & TS_CONT_CNT_MASK) + 1) & TS_CONT_CNT_MASK);
tsTail[4] = TS_SIZE - 5 - 13;
tsTail[5] = 0x00;
tsTail[TS_SIZE - 13] = 0x00;
tsTail[TS_SIZE - 12] = 0x00;
tsTail[TS_SIZE - 11] = 0x01;
tsTail[TS_SIZE - 10] = pesData[3];
tsTail[TS_SIZE - 9] = 0x00;
tsTail[TS_SIZE - 8] = 0x07;
tsTail[TS_SIZE - 7] = 0x80;
tsTail[TS_SIZE - 6] = 0x00;
tsTail[TS_SIZE - 5] = 0x00;
tsTail[TS_SIZE - 4] = 0x00;
tsTail[TS_SIZE - 3] = 0x00;
tsTail[TS_SIZE - 2] = 0x01;
tsTail[TS_SIZE - 1] = VDR172::cRemux::IsFrameH264(pesData, pesLength) ? 10 : 0xb7;

if (0)
{
FILE *ff = fopen("/tmp/still.ts", "wb");
fwrite(tsData, 1, tsLength, ff);
fwrite(tsTail, 1, TS_SIZE, ff);
fclose(ff);
}

        vRemux->Put(tsTail, TS_SIZE);

        int n = 0;
        uchar *p = vRemux->Get(n);
        if (p)
        {
          memcpy(pesData + pesLength, p, n);
          pesLength += n;
          vRemux->Del(n);
        }

if (0)
{
FILE *ff = fopen("/tmp/still.pes", "wb");
fwrite(pesData, 1, pesLength, ff);
fclose(ff);
}

delete vRemux;

StillPicture(pesData, pesLength);
free(pesData);
      return;
    }
#endif
    
    xfprintf(stderr, "StillPicture: %p, %d\n", Data, Length);

    if (0)
    {
      for (int i = 0; i < Length - 3; i++)
      {
        if (i != 0
            && Data[ i + 0 ] == 0x00
            && Data[ i + 1 ] == 0x00
            && Data[ i + 2 ] == 0x01)
        {
          xfprintf(stderr, "\n");
        }
        
        xfprintf(stderr, "%02x ", Data[ i ]);
      }
      
      for (int i = Length - 3; i < Length; i++)
      {
        xfprintf(stderr, "%02x ", Data[ i ]);
      }
      
      xfprintf(stderr, "\n");
    }
    
    const int maxPackets = 3;
    uchar pes[ maxPackets * (6 + 0xffff) ];
    static const uchar header[ 6 + 3 ] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
    
    do // ... while (false);
    {
      if (Length < 6)
      {
        VERBOSE_NOP();
        break;
      }
      
      if (0x00 != Data[ 0 ]
          || 0x00 != Data[ 1 ]
          || 0x01 != Data[ 2 ])
      {
        VERBOSE_NOP();
        break;
      }
      
      if (0xe0 != (0xf0 & Data[ 3 ])      // video
          && 0xc0 != (0xe0 & Data[ 3 ])   // audio
          && 0xbd != (0xff & Data[ 3 ])   // dolby
          && 0xbe != (0xff & Data[ 3 ]))  // padding (DVD)
      {
        if (Length > maxPackets * (0xffff - 3))
        {
          VERBOSE_NOP();
          break;
        }
      
	int todo = Length;
        const uchar *src = Data;
        uchar *dst = pes;

        while (todo > 0)
        {
          ::memcpy(dst, header, sizeof (header));
          
          int bite = todo;

          if (bite > 0xffff - 3)
            bite = 0xffff - 3;

          todo -= bite;
          
          dst[ 4 ] = 0xff & ((bite + 3) >> 8);
          dst[ 5 ] = 0xff & (bite + 3);

          ::memcpy(dst + sizeof (header), src, bite);

          Length += sizeof (header);
          dst += sizeof (header) + bite;
          src += bite;
        }
        
        Data = pes;
      }
    }
    while (false);
    
//NNN    stripPTSandDTS((uchar *)Data, Length);
    
    ts = 0;

    m_xineLib.execFuncTrickSpeedMode(false);
    m_xineLib.execFuncSetSpeed(100.0);
    m_xineLib.execFuncStillFrame();
    m_xineLib.execFuncWait();

    f = 0;
    
    m_xineLib.pause(false);

//    PushOut();

//blahblah = true;
#if APIVERSNUM >= 10704
uchar pesTail[] = { 0x00, 0x00, 0x01, Data[3], 0x00, 3 + 4,
0x80, 0x00, 0x00,
0x00, 0x00, 0x01, VDR172::cRemux::IsFrameH264(Data, Length) ? 10 : 0xb7
};
#endif

    for (int i = 0; i < 1 /* 4 */; i++)
    {
//fprintf(stderr, " (%d) ", i);
      int r = PlayVideo1(Data, Length, true);      
      if (r < 0)
        return;
#if APIVERSNUM >= 10704
      r = PlayVideo1(pesTail, sizeof (pesTail), true);      
      if (r < 0)
        return;
#endif
    }
/*
FILE *ff = fopen("/tmp/still.pes", "wb");
fwrite(Data, 1, Length, ff);
fclose(ff);
*/
    PushOut();
//    m_xineLib.execFuncFlush(0);
    m_xineLib.execFuncFlush();

//    ::fprintf(stderr, "------------\n");      
    LOG_ME(::fprintf(stderr, "------------\n");)
  }

  static bool softStartPoll(cXineLib &xineLib, cPoller &poller, const int timeout, const bool result);

  bool cXineDevice::Poll(cPoller &Poller, int TimeoutMs /* = 0 */)
  {
    cMutexLock lock(&softStartMutex);

    if (softStartState != sIdle)
      return true;

    if (m_xineLib.Poll(Poller, TimeoutMs))
      return softStartPoll(m_xineLib, Poller, TimeoutMs, true);

    return softStartPoll(m_xineLib, Poller, TimeoutMs, false);
  }

  static bool jw = false;

  bool cXineDevice::Flush(int TimeoutMs /* = 0 */)
  {
    const bool jw0 = jw;
    
    m_xineLib.pause(false);

    if (!jw0)
    {
      int r = PushOut();
      if (r < 0)
        return true;
    }

    bool retVal = m_xineLib.execFuncFlush(TimeoutMs, jw0);

    if (!retVal)
      xfprintf(stderr, jw0 ? "f" : "F");
    
    jw = true;
    
    return retVal;
  }
  
  static bool dumpAudio(const char *proc, const uchar *Data, int Length)
  {
    return false;
    
  nextPacket:
    if (Length == 0)
      return true;
/*    
    fprintf(stderr
            , "%s: "
            , proc);
*/  
    if (Length < 6)
      VERBOSE_RETURN0(false);
        
    if (0x00 != Data[ 0 ]
        || 0x00 != Data[ 1 ]
        || 0x01 != Data[ 2 ])
    {
      VERBOSE_RETURN3(false);
    }

    int l = Data[ 4 ] * 0x0100 + Data[ 5 ];
    if (Length < (6 + l))
      VERBOSE_RETURN0(false);
    
    if (0xe0 == (Data[ 3 ] & 0xf0)     //video
        || 0xc0 == (Data[ 3 ] & 0xe0)  //audio
        || 0xbe == Data[ 3 ])          //padding
    {
      Data += (6 + l);
      Length -= (6 + l);
      goto nextPacket;
    }
    
  //  if (0xbd != Data[ 3 ])             //private (dolby, pcm)
  //    VERBOSE_RETURN0(false);

//    fprintf(stderr, "private ");
    
    if (l < (3 + 0 + 2))
      VERBOSE_RETURN0(false);
    
    int h = Data[ 8 ];
    if (l < (3 + h + 2))
      VERBOSE_RETURN0(false);

    xfprintf(stderr
            , "%s: "
            , proc);

    xfprintf(stderr
            , "0x%02x 0x%02x\n"
            , Data[ 6 + 3 + h + 0 ]
            , Data[ 6 + 3 + h + 1 ]);

    Data += (6 + l);
    Length -= (6 + l);
    goto nextPacket;
  }
  
  static bool IsVideo(const uchar *Data, int Length)
  {
    return (Length >= 4
            && 0x00 == Data[ 0 ]
            && 0x00 == Data[ 1 ]
            && 0x01 == Data[ 2 ]
            && 0xe0 == (0xf0 & Data[ 3 ]));
  }

#if APIVERSNUM >= 10701
  static int m_tsVideoPid = 0;
#endif

  int cXineDevice::PlayTsVideo(const uchar *Data, int Length)
  {
#if APIVERSNUM >= 10701
    if (Length >= TS_SIZE)
    {
#if APIVERSNUM >= 10712
      m_tsVideoPid = PatPmtParser()->Vpid();
#else
      m_tsVideoPid = TsPid(Data);
#endif
    }
#endif
    return Length;
  }

  int cXineDevice::PlayTsAudio(const uchar *Data, int Length)
  {
    return Length;
  }

#if APIVERSNUM >= 10701

static bool m_trampoline = false;

struct sTrampoline
{
  uchar Header[6];
  const uchar *Data;
  int Length;
  bool VideoOnly;
};
  static VDR172::cRemux *m_tsVideoRemux = 0;
  static VDR172::cRemux *m_tsAudioRemux = 0;
#endif
  int cXineDevice::PlayTs(const uchar *Data, int Length, bool VideoOnly /* = false */)
  {
#if APIVERSNUM >= 10701

    cMutexLock lock(&m_playTsMutex);

    int ret = PlayTsImpl(Data, Length, VideoOnly);

//    if (Length > 0 && ret <= 0) { char cmd[500]; sprintf(cmd, "ddd /soft/vdr-" APIVERSION "/bin/vdr %d", getpid()); system(cmd); sleep(10); } //should never happen!

    return ret;

#endif
    return -1;
  }

  int cXineDevice::PlayTsImpl(const uchar *Data, int Length, bool VideoOnly /* = false */)
  {
    if (!Data)
      return PlaySingleTs(Data, Length, VideoOnly);

    int ret = 0;
    while (Length >= TS_SIZE)
    {
      int r = PlaySingleTs(Data, TS_SIZE, VideoOnly);
      if (r < 0)
      {
        if (!ret)
          return r;

        return ret;
      }

      ret += r;
      Length -= r;
      Data += r;
    } 

    if (ret)
      return ret;

    if (Length > 0)
      return PlaySingleTs(Data, Length, VideoOnly);

    return ret;
  }
 
  int cXineDevice::PlaySingleTs(const uchar *Data, int Length, bool VideoOnly /* = false */)
  {
#if APIVERSNUM >= 10701

    if (!Data)
    {
      m_tsVideoPid = 0;
      delete m_tsVideoRemux;
      delete m_tsAudioRemux;
      m_tsVideoRemux = 0;
      m_tsAudioRemux = 0;
    }

    int ret = cDevice::PlayTs(Data, Length, VideoOnly);
    if (ret <= 0)
      return ret;

    if (!TsHasPayload(Data))
      return ret; // silently ignore TS packets w/o payload
if (TsIsScrambled(Data)) fprintf(stderr, "TS is scrambled **************\n");
    int payloadOffset = TsPayloadOffset(Data);
    if (payloadOffset >= Length)
      return ret;

    int pid = TsPid(Data);
    if (pid == 0)
    {
      m_tsVideoPid = 0;
      return ret;
    }

    m_trampoline = true;
    sTrampoline trampoline = { { 0x00, 0x00, 0x01, 0xe0, 0x00, sizeof (trampoline) - 6 }, Data, Length, VideoOnly };
    PlayPesPacket(trampoline.Header, sizeof (trampoline));

    return ret;

#endif
    return -1;
  }
 
//static int aaz = 0;
  void cXineDevice::PlayTsTrampoline(const uchar *Data, int Length, bool VideoOnly /* = false */)
  {
#if APIVERSNUM >= 10701
m_trampoline = false;
    const tTrackId *track = 0;
    int pid = TsPid(Data);
    if (pid == m_tsVideoPid)
      goto remux;
    else if ((track = GetTrack(GetCurrentAudioTrack())) && pid == track->id)
    {
      if (!VideoOnly || HasIBPTrickSpeed())
        goto remux;
    }
    return;

remux:
    static int vpid = 0;
    static int apid[2] = { 0, 0 };
    static int dpid[2] = { 0, 0 };

    int v = 0;
    int a = 0;
    int d = 0;

    v = m_tsVideoPid;
    if (!m_tsVideoRemux
      || vpid != v)
    {
//fprintf(stderr, "vpid: %d, v: %d\n", vpid, v);

      delete m_tsVideoRemux;

      vpid = v;

      m_tsVideoRemux = new VDR172::cRemux(vpid, 0, 0, 0);
      m_tsVideoRemux->SetTimeouts(0, 0);
    }

    if (ttAudioFirst <= GetCurrentAudioTrack() && GetCurrentAudioTrack() <= ttAudioLast)
      a = (track = GetTrack(GetCurrentAudioTrack())) ? track->id : 0;
    else
      d = (track = GetTrack(GetCurrentAudioTrack())) ? track->id : 0;
/*
if (a || d)
{
if (aaz)
if (aaz != (d ? 1 : 2)) *(char *)0 =0 ;
}
*/
    if (!m_tsAudioRemux
      || *apid != a
      || *dpid != d)
    {
//fprintf(stderr, "a: %d, d: %d\n", a, d);

      delete m_tsAudioRemux;

      *apid = a;
      *dpid = d;

      m_tsAudioRemux = new VDR172::cRemux(0, apid, dpid, 0);
      m_tsAudioRemux->SetTimeouts(0, 0);
    }

    if (pid == vpid)
    {
      m_tsVideoRemux->Put(Data, Length);

      int n = 0;
      uchar pt;
      uchar *p = m_tsVideoRemux->Get(n, &pt);
      if (p)
      {
        if (m_xineLib.isTrickSpeedMode())
        {
          if (pt == I_FRAME && VDR172::cRemux::IsFrameH264(p, n))
          {
fprintf(stderr, "TrickSpeedMode: push H.264 SequenceEndCode\n");
            uchar sequenceEndCode[] = { 0x00, 0x00, 0x01, p[3], 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 10 };
            PlayVideo(sequenceEndCode, sizeof (sequenceEndCode));
          }
        }
        int w = PlayVideo(p, n);
        m_tsVideoRemux->Del(w);
      }
    }
    else
    {
      m_tsAudioRemux->Put(Data, Length);

      int n = 0;
      uchar *p = m_tsAudioRemux->Get(n);
      if (p)
      {
        int w = PlayAudio(p, n, 0);
        m_tsAudioRemux->Del(w);
      }
    }
#endif
  }

  int cXineDevice::PlayVideo(const uchar *Data, int Length)
  {
#if APIVERSNUM >= 10701
if (m_trampoline)
{
  sTrampoline *t = (sTrampoline *)Data;
  PlayTsTrampoline(t->Data, t->Length, t->VideoOnly);
  return Length;
}
#endif
    if (ttt4 == 0) ttt4 = tNow();
//    static FILE *f = fopen("/tmp/egon1", "wb");
//    fwrite(Data, Length, 1, f);

    return PlayVideo1(Data, Length, false);
  }
  
  int cXineDevice::PlayVideo1(const uchar *Data, int Length, const bool stillImageData)
  {
    LOG_ME(::fprintf(stderr, "V");)

    if (f)
    {      
      LOG_ME(::fprintf(stderr, "<");)
      return 0;
    }

    if (pmNone == pm)
    {
      cMutexLock pmMutexLock(&m_pmMutex);

      if (pmNone == pm)
        m_pmCondVar.Wait(m_pmMutex);
    }
    
    int retVal = PlayVideo2(Data, Length, stillImageData);

    LOG_ME(::fprintf(stderr, "v");)

    return retVal;
  }

  int cXineDevice::PlayCommon2(const uchar *Data, int Length, int64_t ptsForce)
  {
/*
if (blahblah)
{
  fprintf(stderr, "blahblah C2");
  for (int i = 0; i < 50 && i < Length; i++)
    fprintf(stderr, " %02x", Data[ i ]);
  fprintf(stderr, "\n");
}
*/
    do // ... while (false);
    {
      if (Length < 6)
      {
        VERBOSE_NOP();
        break;
      }
      
      if (0x00 != Data[ 0 ]
          || 0x00 != Data[ 1 ]
          || 0x01 != Data[ 2 ])
      {
        VERBOSE_NOP();
        break;
      }
      
      int l = 6 + Data[ 4 ] * 0x0100 + Data[ 5 ];
      if (Length < l)
      {
        VERBOSE_NOP();
        break;
      }
      
      if (0xe0 != (Data[ 3 ] & 0xf0)     //video
          && 0xc0 != (Data[ 3 ] & 0xe0)  //audio
          && 0xbd != Data[ 3 ])          //private (dolby, pcm)
      {
        VERBOSE_NOP();
        break;
      }

      int payloadOffset = 0;
      if (AnalyzePesHeader(Data, Length, payloadOffset) <= phInvalid)
      {
        VERBOSE_NOP();
        break;
      }

      if (l <= payloadOffset)
      {
        // drop short frames
//        ::fprintf(stderr, "i");
        return Length;
      }

//      if (0xc0 == (Data[ 3 ] & 0xe0))  //audio
//        return Length;
    }
    while (false);

    return PlayCommon3(Data, Length, ptsForce);
  }

typedef long long int lld_t;

static int xzabc = 0;

  int cXineDevice::PlayCommon3(const uchar *Data, int Length, int64_t ptsForce)
  {
//    if (!m_settings.LiveTV())
//    {
//      VERBOSE_NOP1();
//      return Length;
//    }

//    if (xzabc)
    if (0)
    {
      if (0xe0 == (Data[ 3 ] & 0xf0))      //video
      {
        uchar pt = NO_PICTURE;
        cRemux::ScanVideoPacket(Data, Length, 0, pt);

        if (pt != NO_PICTURE)
        {
          static int64_t last = -1;
          int64_t pts = -1;
          getPTS(Data, Length, pts);
          fprintf(stderr, "** %c ** %lld ** %lld ** %lld **\n", "0IPB4567"[pt], (lld_t)ptsForce, (lld_t)(ptsForce - last), (lld_t)pts);
          last = ptsForce;
        }
      }
      if (0xc0 == (Data[ 3 ] & 0xe0))      //audio
      {
        {
          static int64_t last = -1;
          int64_t pts = -1;
          getPTS(Data, Length, pts);
          fprintf(stderr, "\t\t\t\t\t\t\t** %c ** %lld ** %lld ** %lld **\n", 'A', (lld_t)ptsForce, (lld_t)(ptsForce - last), (lld_t)pts);
          last = ptsForce;
        }
      }
    }
    if (0)
    {
      int64_t pts = ptsForce;
      if (ptsForce > -1
        || getPTS(Data, Length, pts))
      {
        xzabc = 0;
        int64_t *pPTS = 0;
        
        if (0xe0 == (Data[ 3 ] & 0xf0))      //video
        {
          pPTS = &ptsV;
        }
        else if (0xc0 == (Data[ 3 ] & 0xe0)) //audio
        {
          pPTS = &ptsA;
        }
        else if (0xbd == Data[ 3 ])          //private (dolby, pcm)
        {
          int h = Data[ 6 + 2 ];
          
          if (0xa0 == (0xf0 & Data[ 6 + 3 + h + 0 ]))  // pcm?
            pPTS = &ptsP;
          else
            pPTS = &ptsD;
        }
        else
        {
          xfprintf(stderr, "0x%02x\t", Data[ 3 ]);
          VERBOSE_NOP();        
        }
        
        if (pPTS
            && *pPTS != pts)
        {
          *pPTS = pts;
          
          int64_t ptsX = -1;
          m_xineLib.execFuncGetPTS(ptsX);

          int64_t dV = (ptsV != -1 && ptsX != -1) ? ptsV - ptsX : 0;
          int64_t dA = (ptsA != -1 && ptsX != -1) ? ptsA - ptsX : 0;
          int64_t dP = (ptsP != -1 && ptsX != -1) ? ptsP - ptsX : 0;
          int64_t dD = (ptsD != -1 && ptsX != -1) ? ptsD - ptsX : 0;

          int64_t dVA = (ptsV != -1 && ptsA != -1) ? ptsA - ptsV : 0; 
          int64_t dVD = (ptsV != -1 && ptsD != -1) ? ptsD - ptsV : 0; 

          fprintf(stderr, "ptsVideo: %lld, ptsAudio: %lld, ptsPCM: %lld, ptsDolby: %lld, ptsXine: %lld, dVA: %lld, dVD: %lld, dV: %lld, dA: %lld, dP: %lld, dD: %lld\n", (lld_t)ptsV, (lld_t)ptsA, (lld_t)ptsP, (lld_t)ptsD, (lld_t)ptsX, (lld_t)dVA, (lld_t)dVD, (lld_t)dV, (lld_t)dA, (lld_t)dP, (lld_t)dD);
        }
      }
    }
    
/*
if (blahblah)
{
  blahblah = false;
  fprintf(stderr, "blahblah C3");
  for (int i = 0; i < 50 && i < Length; i++)
    fprintf(stderr, " %02x", Data[ i ]);
  fprintf(stderr, "\n");
}
*/
/*
if (aaz)
{
  if (Data[3] == 0xbd || (Data[3] & 0xe0) == 0xc0) {
fprintf(stderr, "===== %c =====\n", Data[3] == 0xbd ? 'D' : 'A');
if (aaz != (Data[3] == 0xbd ? 1 : 2)) *(char *)0 = 0;
aaz = 0;
  }
}
*/ 
    int done = 0;

    while (done < Length)
    {
      int r = m_xineLib.execFuncStream1(Data + done, Length - done);
      if (r < 0)
        return r;
      
      done += r;
    }

    return done;
  }
  
  int cXineDevice::PlayVideo2(const uchar *Data, int Length, const bool stillImageData)
  {
//    fprintf(stderr, "D");
    
    int done = 0;

    while (done < Length)
    {
      char ch = 'X';
      
      int todo = Length - done;
      
      if (todo >= 6)
      {
        if (0x00 == Data[ done + 0 ]
            && 0x00 == Data[ done + 1 ]
            && 0x01 == Data[ done + 2 ])
        {
          int id  = Data[ done + 3 ];
          int len = 6 + Data[ done + 4 ] * 0x0100 + Data[ done + 5 ];

          if (0xba == id) // pack header has fixed length
          {
            if (0x00 == (0xc0 & Data[ done + 4 ])) // MPEG 1
              len = 12;
            else // MPEG 2
              len = 14 + (Data[ done + 13 ] & 0x07);
          }
          else if (0xb9 == id) // stream end has fixed length
          {
            len = 4;
          }

          if (todo >= len)
          {
            todo = len;

            if (0xbe == id   // padding
              || 0xba == id  // system layer: pack header
              || 0xbb == id  // system layer: system header
              || 0xb9 == id) // system layer: stream end
            {
              done += todo;

//              fprintf(stderr, "x");
              continue;
            }

            ch = '.';            
          }
          else        
          {
//            ::fprintf(stderr, "todo: %d, len: %d\t", todo, len);
            VERBOSE_NOP();
            ch = '3';

//            break;
          }
        }
        else        
        {
          VERBOSE_NOP();
          ch = '2';
        }
      }
      else        
      {
        VERBOSE_NOP();
        ch = '1';
      }
      
//      fprintf(stderr, "%c", ch);
      
      int r = PlayVideo3(Data + done, todo, stillImageData);
      if (r < 0)
        return r;

      done += r;
    }
    
    return done;
  }

  static void resetScramblingControl(const uchar *Data, int Length)
  {
    if (Length < 6)
    {
      VERBOSE_NOP();
      return;
    }
      
    if (0x00 != Data[ 0 ]
        || 0x00 != Data[ 1 ]
        || 0x01 != Data[ 2 ])
    {
      VERBOSE_NOP1();
      return;
    }
      
    if (0xe0 != (0xf0 & Data[ 3 ])      // video
        && 0xc0 != (0xe0 & Data[ 3 ])   // audio
        && 0xbd != (0xff & Data[ 3 ])   // dolby
        && 0xbe != (0xff & Data[ 3 ])   // padding (DVD)
        && 0xba != (0xff & Data[ 3 ])   // system layer: pack header
        && 0xbb != (0xff & Data[ 3 ])   // system layer: system header
        && 0xb9 != (0xff & Data[ 3 ]))  // system layer: stream end
    {
      VERBOSE_NOP();
      return;
    }
        
    if (0xbe == (0xff & Data[ 3 ])    // padding (DVD)
      || 0xba == (0xff & Data[ 3 ])   // system layer: pack header
      || 0xbb == (0xff & Data[ 3 ])   // system layer: system header
      || 0xb9 == (0xff & Data[ 3 ]))  // system layer: stream end
    {
      return;
    }

    int len = (6 + Data[ 4 ] * 0x100 + Data[ 5 ]);
    if (len < 6 + 1
        || Length < 6 + 1)
    {
      VERBOSE_NOP();
      return;
    }

    if (0x00 != (0x30 & Data[ 6 ]))
    {
      if (0x80 == (0xc0 & Data[ 6 ]))  // only touch MPEG2
      {
        xfprintf(stderr, "reseting PES_scrambling_control: 0x%02x\n", Data[ 6 ]);

        ((uchar *)Data)[ 6 ] &= ~0x30;
      }
    }
  }
  
  int cXineDevice::PlayVideo3(const uchar *Data, int Length, const bool stillImageData)
  {
/*
if (blahblah)
{
  fprintf(stderr, "blahblah V3");
  for (int i = 0; i < 50 && i < Length; i++)
    fprintf(stderr, " %02x", Data[ i ]);
  fprintf(stderr, "\n");
}
*/
    resetScramblingControl(Data, Length);
    
    dumpAudio("Video", Data, Length);

    return PlayCommon(Data, Length, stillImageData);
  }
  
  static double videoFrameDuration(const uchar *Data, int Length)
  {
    int PesPayloadOffset = 0;
    ePesHeader ph = AnalyzePesHeader(Data, Length, PesPayloadOffset);
    if (ph < phMPEG1)
      return -1;

    if ((Data[ 3 ] & 0xf0) != 0xe0)
      return -1;

    if (Length < PesPayloadOffset + 8)
      return -1;

    const uchar *p = Data + PesPayloadOffset;

    if (*p++ != 0x00)
      return -1;

    if (*p++ != 0x00)
      return -1;

    if (*p++ != 0x01)
      return -1;

    if (*p++ != 0xb3)
      return -1;

    p += 3;

    static const double frameRates[ 16 ] =
    {
      -1,
      24000/1001.0,
      24,
      25,
      30000/1001.0,
      30,
      50,
      60000/1001.0,
      60,
      -1,
      -1,
      -1,
      -1,
      -1,
      -1,
      -1,
    };

    int frameRateIndex = *p & 0x0f;

    if (frameRates[ frameRateIndex ] < 0)
      return -1;

    long n = 0, d = 0;

    if (ph == phMPEG2)
    {
      const uchar *const limit = Data + Length - 10 + 3;
      while (p < limit)
      {
        if (p[ 0 ] != 0x01 || p[ -1 ] != 0x00 || p[ -2 ] != 0x00)
        {
          p++;
          continue;
        }

        if (p[ 1 ] != 0xb5) // extension start code
          break;

        if ((p[ 2 ] & 0xf0) != 0x10) // sequence extension
          break;

        p += 7;

        n = (*p & 0x60) >> 5;
        d = (*p & 0x1f);
        break;
      }
    }

    return 90000 / (frameRates[ frameRateIndex ] * (n + 1) / (d + 1));
  }

  static int frameSizes[ 256 ];
  
  static double audioFrameDuration(const uchar *Data, int Length)
  {
    int PesPayloadOffset = 0;
    ePesHeader ph = AnalyzePesHeader(Data, Length, PesPayloadOffset);
    if (ph < phMPEG1)
      return -1;

    if ((Data[ 3 ] & 0xff) == 0xbd)
    {
      static int samplingFrequencies[ 4 ] = // all values are specified in Hz
      {
        48000, 44100, 32000, -1
      };

      if (PesPayloadOffset + 5 <= Length
        && Data[ PesPayloadOffset + 0 ] == 0x0b
        && Data[ PesPayloadOffset + 1 ] == 0x77
        && frameSizes[ Data[ PesPayloadOffset + 4 ] ] > 0)
      {
        if (samplingFrequencies[ Data[ PesPayloadOffset + 4 ] >> 6 ] < 0)
          return -1;

        return 90000.0 * 1536 / samplingFrequencies[ Data[ PesPayloadOffset + 4 ] >> 6 ];
      }
      else if (PesPayloadOffset + 4 + 5 <= Length
        && Data[ PesPayloadOffset + 4 + 0 ] == 0x0b
        && Data[ PesPayloadOffset + 4 + 1 ] == 0x77
        && frameSizes[ Data[ PesPayloadOffset + 4 + 4 ] ] > 0)
      {
        if (samplingFrequencies[ Data[ PesPayloadOffset + 4 + 4 ] >> 6 ] < 0)
          return -1;

        return 90000.0 * 1536 / samplingFrequencies[ Data[ PesPayloadOffset + 4 + 4 ] >> 6 ];
      }
      else 
        return -1;
    }

    if ((Data[ 3 ] & 0xe0) != 0xc0)
      return -1;

    if (Length < PesPayloadOffset + 4)
      return -1;

    ulong Header = 0;
    Header |= Data[ PesPayloadOffset + 0 ];
    Header <<= 8;
    Header |= Data[ PesPayloadOffset + 1 ];
    Header <<= 8;
    Header |= Data[ PesPayloadOffset + 2 ];
    Header <<= 8;
    Header |= Data[ PesPayloadOffset + 3 ];

    bool Mpeg2 = (ph == phMPEG2);
    
    int syncword           = (Header & 0xFFF00000) >> 20;
    int id                 = (Header & 0x00080000) >> 19;
    int layer              = (Header & 0x00060000) >> 17;
//  int protection_bit     = (Header & 0x00010000) >> 16;
    int bitrate_index      = (Header & 0x0000F000) >> 12;
    int sampling_frequency = (Header & 0x00000C00) >> 10;
//  int padding_bit        = (Header & 0x00000200) >>  9;
//  int private_bit        = (Header & 0x00000100) >>  8;
//  int mode               = (Header & 0x000000C0) >>  6;
//  int mode_extension     = (Header & 0x00000030) >>  4;
//  int copyright          = (Header & 0x00000008) >>  3;
//  int orignal_copy       = (Header & 0x00000004) >>  2;
    int emphasis           = (Header & 0x00000003);

    if (syncword != 0xFFF)
      return -1;

    if (id == 0 && !Mpeg2) // reserved in MPEG 1
      return -1;

    if (layer == 0) // reserved
      return -1;

    if (bitrate_index == 0xF) // forbidden
      return -1;

    if (sampling_frequency == 3) // reserved
      return -1;

    if (emphasis == 2) // reserved
      return -1;

    static int samplingFrequencies[ 2 ][ 4 ] = // all values are specified in Hz
    {
      { 44100, 48000, 32000, -1 }, // MPEG 1
      { 22050, 24000, 16000, -1 }  // MPEG 2
    };

    static int slots_per_frame[ 2 ][ 3 ] =
    {
      { 12, 144, 144 }, // MPEG 1, Layer I, II, III
      { 12, 144,  72 }  // MPEG 2, Layer I, II, III
    };

    int mpegIndex  = 1 - id;
    int layerIndex = 3 - layer;

    // Layer I (i. e., layerIndex == 0) has a larger slot size
    int slotSize = (layerIndex == 0) ? 4 : 1; // bytes

    int sf = samplingFrequencies[ mpegIndex ][ sampling_frequency ];

//fprintf(stderr, "afd: %.3f ms, PES-Length: %d\n", 1000.0 * 8 * slotSize * slots_per_frame[ mpegIndex ][ layerIndex ] / sf, Length);

    return 90000.0 * 8 * slotSize * slots_per_frame[ mpegIndex ][ layerIndex ] / sf;
  }
 
  static double softStartTime = 0;
  static const double softStartSpeedStart = 0.75;
  static const double softStartSpeedMin = 0.15;
  static double softStartDurationVideoSD = 31 / 25.0;
  static double softStartDurationVideoHD = 31 / 25.0;
  static double softStartDurationAudio   = 31 / 25.0;
  static const int softStartMaxSpeedChanges = 20;
  static double softStartLastSpeed = -1;
  static double softStartSpeedChangeTime = -1;
  static int64_t softStartPtsVdr = -1;
  static const int64_t softStartLogPtsDelta = 4 * 90000;
  static int softStartHitPoll = 0;
  static bool softStartNoMetronom = false;
  static const double pi = 4 * ::atan(1);
  static bool softStartHasVideo = true;
  static bool softStartHasAudio = true;
  static bool softStartIsVideoHD = false;
 
  inline double getSoftStartDuration(const bool forVideo)
  {
    if (forVideo)
      return softStartHasVideo ? (softStartIsVideoHD ? softStartDurationVideoHD : softStartDurationVideoSD) : 0;

    return softStartHasAudio ? softStartDurationAudio : 0;
  }

  inline double getSoftStartDuration()
  {
    double dV = getSoftStartDuration(true);
    double dA = getSoftStartDuration(false);
    return (dV > dA) ? dV : dA;
  }

  static double softStartCalcSpeed0(const double /* t */)
  {
    return 0;
  }
  
  static double softStartCalcSpeed1(const double t)
  {
    const double p = (1 + ::cos(2 * pi * t)) / 2;    
          
    return softStartSpeedMin + p * ((softStartSpeedStart * (1 - t) + 1.0 * t) - softStartSpeedMin);
  }
  
  static double softStartCalcSpeed2(const double t)
  {
    double p = 2 * t - 1;
    if (p < 0)
      p = -p;

//    p = p * p * p;
    p = p * p;
          
    return softStartSpeedMin + p * ((softStartSpeedStart * (1 - t) + 1.0 * t) - softStartSpeedMin);
  }
  
  static double softStartCalcSpeed3(const double t)
  {
    if (t < 0.25)
      return softStartSpeedStart;

    if (t < 0.50)
      return softStartSpeedMin + (1 - softStartSpeedMin) * 0 / 3;

    if (t < 0.75)
      return softStartSpeedMin + (1 - softStartSpeedMin) * 1 / 3;

    return   softStartSpeedMin + (1 - softStartSpeedMin) * 2 / 3;
  }
  
  static double softStartCalcSpeed4(const double t)
  {
    const double p = (1 + ::cos(pi * (1 + t))) / 2;    
          
    return softStartSpeedMin + (1 - softStartSpeedMin) * p;
  }

  static double softStartCalcSpeed(const double t)
  {
    if (t >= 1)
      return 1;

    return softStartCalcSpeed0(t);  // choose a method

    (void)softStartCalcSpeed0;
    (void)softStartCalcSpeed1;
    (void)softStartCalcSpeed2;
    (void)softStartCalcSpeed3;
    (void)softStartCalcSpeed4;
  }

  bool softStartPoll(cXineLib &xineLib, cPoller &poller, const int timeout, bool result)
  {
    if (softStartState > sIdle)
    {
      if (result)
      {
        softStartHitPoll = 0;
      }
      else if (++softStartHitPoll >= 2)
      {
        do
        {
          softStartState = sIdle;
          
          xineLib.execFuncFirstFrame();
          xineLib.execFuncSetSpeed(100.0);
          xineLib.execFuncWait();
        }
        while (!xineLib.Poll(poller, timeout, true));

        softStartHitPoll = 0;
        result = true;
      }

//      ::fprintf(stderr, "softStartHitPoll: %d, %d\n", result, softStartHitPoll);      
    }

    return result;
  }

  static int64_t vpts, apts, extra0, extra;
  static bool useApts;
  static bool seenAudio = false;
  static bool seenVideo = false;
  static bool seenApts = false;
  static bool seenVpts = false;
  
  static const int64_t extra_max = 50 * 90000 / 25;

  static bool oldMode = false;

  static inline int64_t calcPtsDelta(int64_t lhs, int64_t rhs)
  {
    int64_t delta = lhs - rhs;

    if (delta < -(1ll << 32))
      delta += 1ll << 33;
    else if (delta > +(1ll << 32))
      delta -= 1ll << 33;

    return delta;
  }

  static inline double softStartCalcQ0(int64_t vdr, int64_t xine, bool queued, bool forVideo, int64_t hyst)
  {
    if (vdr < 0 || xine < 0)
      return 0;

    double delta = calcPtsDelta(vdr, xine) / 90000.0;
    if (delta <= 0)
      return 0;

    if (delta >= softStartLogPtsDelta / 90000.0)
      return queued ? 0 : 1;

    if (delta >= (getSoftStartDuration(forVideo) + hyst / 90000.0))
      return 1;

    return delta / (getSoftStartDuration(forVideo) + hyst / 90000.0);
  }

  static bool gotQ1 = false;
  static int64_t hystQ = 0;
  static int64_t hystQ1 = 0;
  static bool vdrTime100reload = false;

  static inline double softStartCalcQ(int64_t vdr, int64_t xine, bool queued, bool forVideo)
  {
    return softStartCalcQ0(vdr, xine, queued, forVideo, hystQ);
  }

  static inline double softStartCalcQ(int64_t vdrVideo, int64_t vdrAudio, int64_t xine, bool queued)
  {
    double qV = softStartHasVideo ? softStartCalcQ(vdrVideo, xine, queued, true)  : 1;
    double qA = softStartHasAudio ? softStartCalcQ(vdrAudio, xine, queued, false) : 1;
    double q = (qV < qA) ? qV : qA;

    bool gQ1 = (q >= 1);
    if (gQ1 != gotQ1)
    {
      gotQ1 = gQ1;

      if (gotQ1)
      {
        hystQ = 0;
      }
      else
      {
        vdrTime100reload = true;
        hystQ = (hystQ1 += 90000 / 25);
      }
    }

//fprintf(stderr, "q: %.3lf, gq1: %d, hystQ: %lld, hystQ1: %lld, vdr: %lld, xine: %lld, d: %lld\n", q, gotQ1, hystQ, hystQ1, vdr, xine, vdr - xine);
    return q;
  }

  static int64_t vdrPTSLast = -2;
  static int64_t vdrAptsLast = -1;
  static int64_t vdrVptsLast = -1;

  static double vdrVptsCalced = -1;
  static double vdrAptsCalced = -1;
  static double vdrVduration = -1;
  static double vdrAduration = -1;

  static double vdrVptsBuffered[ 2 ] = { -1, -1 };

  static bool getPTS(const uchar *Data, int Length, int64_t &pts, bool isVideo, bool isAudio, double &vptsCalced, double &aptsCalced, double &vDuration, double &aDuration, double vptsBuffered[2])
  {
    uchar pt = NO_PICTURE;

    if (isVideo)
    {
      cRemux::ScanVideoPacket(Data, Length, 0, pt);

      if (pt != NO_PICTURE && vDuration > 0 && vptsBuffered[ 1 ] > 0)
        vptsBuffered[ 1 ] += vDuration;
    }

    bool retVal = false;

    if (getPTS(Data, Length, pts))
    {
      if (isAudio)
      {
        aptsCalced = pts;

        double duration = audioFrameDuration(Data, Length);
        if (duration >= 0)
          aDuration = duration;
      }

      if (isVideo)
      {
        if (pt == B_FRAME)
          vptsCalced = pts;
        else if (pt != NO_PICTURE)
          vptsBuffered[ 1 ] = pts;

        double duration = videoFrameDuration(Data, Length);
        if (duration >= 0)
          vDuration = duration;

        if (pt != B_FRAME)
          goto other;
      }

      return true;
    }
    else
    {
      if (isAudio)
      {
        double duration = audioFrameDuration(Data, Length);

        bool frameStart = (duration > -1);

        if (aptsCalced > -1 && aDuration > -1 && frameStart)
        {
          aptsCalced += aDuration;
          while (aptsCalced >= 0x200000000ull)
            aptsCalced -= 0x200000000ull;

          pts = (int64_t)aptsCalced;

          retVal = true;
        }

        if (duration >= 0)
          aDuration = duration;
      }

      if (isVideo)
      {
other:
        double dur = vDuration;

        if (pt != B_FRAME && pt != NO_PICTURE)
        {
          vptsCalced = vptsBuffered[ 0 ];
          vptsBuffered[ 0 ] = vptsBuffered[ 1 ];
          dur = 0;
        }
          
        bool frameStart = (pt != NO_PICTURE);

        if (vptsCalced > -1 && dur > -1 && frameStart)
        {
          vptsCalced += dur;
          while (vptsCalced >= 0x200000000ull)
            vptsCalced -= 0x200000000ull;

          pts = (int64_t)vptsCalced;

          retVal = true;
        }

        double duration = videoFrameDuration(Data, Length);
        if (duration >= 0)
          vDuration = duration;
      }

      return retVal;
    }
  }

  static double vdrTime100 = -1;

  int cXineDevice::PlayCommon(const uchar *Data, int Length, const bool stillImageData)
  {
    const bool isAudio = !IsVideo(Data, Length);
    const bool isVideo = !isAudio && !stillImageData;
//if (isAudio && ts) fprintf(stderr, "A");
    if ((stillImageData || m_xineLib.isTrickSpeedMode())
      && isAudio)
    {
//      ::fprintf(stderr, "x");
      return Length;
    }
/*    
    if (stillImageData)
      fprintf(stderr, "writing: %d\n", Length - 6);
*/
    if (findVideo && (isVideo || isAudio))
    {
      findVideo = false;
      foundVideo = isVideo;
//xfprintf(stderr, "foundVideo: %d\n", foundVideo);
    }

if (0 && isAudio) {
static int64_t opts = -1;
static int64_t xpts = -1;
getPTS(Data, Length, xpts, isVideo, isAudio, vdrVptsCalced, vdrAptsCalced, vdrVduration, vdrAduration, vdrVptsBuffered);
//fprintf(stderr, "audio: %.3lf, %d, %lld, %lld\n", audioFrameDuration(Data, Length), Length, xpts, xpts - opts);
opts = xpts;
}
if (0 && isVideo) {
static int64_t opts = -1;
static int64_t xpts = -1;
getPTS(Data, Length, xpts, isVideo, isAudio, vdrVptsCalced, vdrAptsCalced, vdrVduration, vdrAduration, vdrVptsBuffered);
//fprintf(stderr, "video: %.3lf, %d, %lld, %lld\n", videoFrameDuration(Data, Length), Length, xpts, xpts - opts);
opts = xpts;
}

    int64_t ptsForce = -1;

    {
      cMutexLock lock(&softStartMutex);

      if (softStartTrigger
        && !stillImageData)
      {
        softStartNoMetronom = (sstNoMetronom == softStartTrigger);
        softStartTrigger = sstNone;

        softStartState = sInitiateSequence;
//fprintf(stderr, "/////////////////////////////\n");
//        ::fprintf(stderr, "#(%d,%d)", ::getpid(), pthread_self());
      }

      if (stillImageData)
      {
        softStartLastSpeed = -1;
      }
      else if (softStartState > sIdle)
      {
        timeval tv;
        ::gettimeofday(&tv, 0);

        const double now = (tv.tv_sec + tv.tv_usec / 1.0e+6);

        if (softStartState == sInitiateSequence)
        {
          xfprintf(stderr, "[");
      
          softStartDurationVideoSD = m_settings.GetModeParams()->m_prebufferFramesVideoSD / 25.0;
          softStartDurationVideoHD = m_settings.GetModeParams()->m_prebufferFramesVideoHD / 25.0;
          softStartDurationAudio   = m_settings.GetModeParams()->m_prebufferFramesAudio   / 25.0;
          softStartHasVideo = false;
          softStartHasAudio = false;
          softStartIsVideoHD = false;
 
          softStartTime = now;        
        
          softStartSpeedChangeTime = -1;
          softStartLastSpeed = -1;
          softStartPtsVdr = -1;
          softStartHitPoll = 0;

          softStartState = sStartSequence;

          vpts = -1;
          apts = -1;
          extra0 = 0;
          extra = 0;

          useApts = 1;
        
          seenAudio = false;
          seenVideo = false;
          seenApts = false;
          seenVpts = false;

          vdrPTSLast = -2;
          vdrAptsLast = -1;
          vdrVptsLast = -1;
        
          vdrAptsCalced = -1;
          vdrVptsCalced = -1;
          vdrAduration = -1;
          vdrVduration = -1;

          vdrVptsBuffered[ 0 ] = -1;
          vdrVptsBuffered[ 1 ] = -1;

          vdrTime100 = -1;
          vdrTime100reload = false;

          gotQ1 = false;
          hystQ = hystQ1 = 90000 * m_settings.GetModeParams()->m_prebufferHysteresis / 25;
        }

        {
          char packetType = 0;
          char packetTypeOnce = 0;

          if (isVideo)
          {
            if (!seenVideo)
            {
              softStartHasVideo = true;
              softStartIsVideoHD = cRemux::IsFrameH264(Data, Length);
//fprintf(stderr, "\nH264: %d\n", softStartIsVideoHD);

              seenVideo = true;
//              ::fprintf(stderr, "seen video\n");
//              xfprintf(stderr, "v");
              packetTypeOnce = 'v';
            }

            packetType = 'v';
          }
          else if (isAudio)
          {
            if (!seenAudio)
            {
              softStartHasAudio = true;

              audioSeen = true;
              seenAudio = true;
//              ::fprintf(stderr, "seen audio\n");
//              xfprintf(stderr, "a");
              packetTypeOnce = 'a';
            }

            packetType = 'a';
          }
        
          int64_t pts = 0;
          if (getPTS(Data, Length, pts, isVideo, isAudio, vdrVptsCalced, vdrAptsCalced, vdrVduration, vdrAduration, vdrVptsBuffered))
          {
            ptsForce = pts;

            if (isVideo)
            {
              if (!seenVpts)
              {
                seenVpts = true;
//                ::fprintf(stderr, "seen video pts\n");
//                xfprintf(stderr, "V");
                packetTypeOnce = 'V';
              }

              packetType = 'V';
            
              vpts = pts;

              if (apts > -1)
              {
                int64_t delta = calcPtsDelta(vpts, apts);
                if (delta < 0)
                  delta = - delta;

                if (extra0 < delta)
                {
                  extra0 = delta;

//                  ::fprintf(stderr, "max. A/V delta: %lld pts => total extra buffering: %d frames", extra0, (int)(extra0 * 25 / 90000));

                  extra = extra0;
                
                  if (extra > extra_max)
                  {
                    extra = extra_max;

//                    ::fprintf(stderr, ", limited to %d frames", (int)(extra * 25 / 90000));
                  }	

//                ::fprintf(stderr, "\n");
                  if (oldMode)
                    xfprintf(stderr, "+%d", (int)(extra * 25 / 90000));
                }
              }

//              ::fprintf(stderr, "video: v: %lld, a: %lld, d: %lld, e: %lld\n", vpts, apts, vpts - apts, extra);
            }
            else if (isAudio)
            {
              if (!seenApts)
              {
                seenApts = true;
//                ::fprintf(stderr, "seen audio pts\n");
//                xfprintf(stderr, "A");
                packetTypeOnce = 'A';
              }
            
              packetType = 'A';

              apts = pts;
            
              if (vpts > -1)
              {
                int64_t delta = calcPtsDelta(vpts, apts);
                if (delta < 0)
                  delta = - delta;

                if (extra0 < delta)
                {
                  extra0 = delta;
                
//                  ::fprintf(stderr, "max. A/V delta: %lld pts => total extra buffering: %d frames", extra0, (int)(extra0 * 25 / 90000));

                  extra = extra0;
                
                  if (extra > extra_max)
                  {
                    extra = extra_max;

//                    ::fprintf(stderr, ", limited to %d frames", (int)(extra * 25 / 90000));
                  }	

//                  ::fprintf(stderr, "\n");
                  if (oldMode)
                    xfprintf(stderr, "+%d", (int)(extra * 25 / 90000));
                }
              }

//              ::fprintf(stderr, "audio: v: %lld, a: %lld, d: %lld, e: %lld\n", vpts, apts, vpts - apts, extra);
            }
          }

//xfprintf(stderr, "%s%c", getFrameType(Data, Length), packetType); packetTypeOnce = 0; //ZZZ
//
          if (packetTypeOnce)
            xfprintf(stderr, "%c", packetTypeOnce);
        }

        if ((seenVideo && !seenVpts)
            || (seenAudio && !seenApts))
        {
          softStartTime = now;
        }
      
        if (softStartState >= sStartSequence)
        {
          if (isVideo)
            useApts = false;
        
          if (useApts
              || isVideo)
          {
            softStartPtsVdr = ptsForce;
//            getPTS(Data, Length, softStartPtsVdr);

            if (softStartPtsVdr != -1)
            {
              bool queued = false; 
              int64_t ptsXine = -1;
              m_xineLib.execFuncGetPTS(ptsXine, 0, &queued);
           
              const int64_t delta = (ptsXine >= 0) ? calcPtsDelta(softStartPtsVdr, ptsXine) : 0;
              if (softStartState == sStartSequence
                  || delta < -softStartLogPtsDelta
                  || (delta > +softStartLogPtsDelta && queued))
              {
//                if (softStartState != sStartSequence)
//                  ::fprintf(stderr, "SoftStart: ptsVdr: %12"PRId64", ptsXine: %12"PRId64", delta: %12"PRId64", queued: %d\n", softStartPtsVdr, ptsXine, delta, queued);

//AAA                m_xineLib.execFuncStart();
//AAA                m_xineLib.execFuncWait();

                if (!softStartNoMetronom)
                {
                  xfprintf(stderr, "M");
//                  xfprintf(stderr, " %12" PRId64 ", %12" PRId64 ", %12" PRId64 "\n", delta, softStartPtsVdr, ptsXine);
                
//ZZZ                  m_xineLib.execFuncMetronom(softStartPtsVdr);
//ZZZ                  m_xineLib.execFuncWait();
                }
              
                softStartTime = now;
              
                softStartState = sContinueSequence;
              }
            }
          }
        }
        
//        if (softStartState <= sStartSequence)
//          stripPTSandDTS((uchar *)Data, Length);

        m_xineLib.execFuncFirstFrame();

        int64_t vdrPTS = -1;
 
        if ((seenVideo && vpts <= -1)
          || (seenAudio && apts <= -1))
        {
        }
        else if (vpts > -1)
        {
          if (apts > -1 && vpts > apts)
            vdrPTS = apts;
          else
            vdrPTS = vpts;
        }
        else if (apts > -1)
          vdrPTS = apts;

        bool queued = false;
        int64_t xinePTS = -1;
        m_xineLib.execFuncGetPTS(xinePTS, 0, &queued);

        const double totalDuration = (getSoftStartDuration() + extra / 90000.0);
        const double q = oldMode ? ((now - softStartTime) / totalDuration) : softStartCalcQ(vpts, apts, xinePTS, queued); 
        double p = softStartCalcSpeed(q);
     
        if (!oldMode)
        {
          if (vdrPTSLast == vdrPTS || vdrVptsLast > vpts || vdrAptsLast > apts)
            p = softStartLastSpeed / 100;
          else
            vdrPTSLast = vdrPTS;

          if (apts != vdrAptsLast || vpts != vdrVptsLast)
          {
//          xfprintf(stderr, "p: %.3lf, DA: %lld, DV: %lld, DC: %lld\n", p, (apts > -1) ? (apts - xinePTS) : -1, (vpts > -1) ? (vpts - xinePTS) : -1, (vdrPTS > - 1) ? (vdrPTS - xinePTS) : -1);
//          xfprintf(stderr, "(%.1lf|%.1lf)", 25 * ((apts > -1) ? ((apts - xinePTS) / 90000.0) : 0.0), 25 * ((vpts > -1) ? ((vpts - xinePTS) / 90000.0) : 0.0));
          }

          vdrVptsLast = vpts;
          vdrAptsLast = apts;
        }

        double speed = (p > 0) ? (100.0 * p) : 12.5;
         
        if (speed >= 100.0 || xinePTS == -1)
        { 
          bool newlineRequired = false;
           
          if (vdrTime100 < 0)
          {
ttt6 = tNow();
/*
fprintf(stderr, "+++++ %.3lf ms, %.3lf ms, %.3lf ms, (%.3lf ms, %.3lf ms, %.3lf ms) +++++\n"
, (ttt1 - ttt0) * 1000.0
, (ttt2 - ttt1) * 1000.0
, (ttt3 - ttt2) * 1000.0
, (ttt4 - ttt3) * 1000.0
, (ttt5 - ttt3) * 1000.0
, (ttt6 - ttt3) * 1000.0
);
*/
            vdrTime100 = now;

            xfprintf(stderr, "]");
            newlineRequired = true;
          }
        
          speed = 100.0;

          if (vdrTime100reload)
          {
            vdrTime100reload = false;

            vdrTime100 = now;
          }
      
          if (xinePTS < 0)
            softStartState = sIdle;
          else if ((now - vdrTime100) >= m_settings.GetModeParams()->m_monitoringDuration)
          {
            if (m_settings.GetModeParams()->MonitoringContinuous())
              hystQ1 = 90000 * (m_settings.GetModeParams()->m_prebufferHysteresis - 1) / 25;
            else
              softStartState = sIdle;
          }

          if ((softStartState == sIdle || speed != softStartLastSpeed) && vdrPTS > -1 && xinePTS > -1)
          {
            xfprintf(stderr, "buffered %.1lf frames (v:%.1lf, a:%.1lf)", calcPtsDelta(vdrPTS, xinePTS) / 90000.0 * 25, (vpts > -1) ? (calcPtsDelta(vpts, xinePTS) / 90000.0 * 25) : 0.0, (apts > -1) ? (calcPtsDelta(apts, xinePTS) / 90000.0 * 25) : 0.0);
            if (softStartState == sIdle)
              xfprintf(stderr, " <<<<<");
            newlineRequired = true;
          }
        
          if (newlineRequired)
            xfprintf(stderr, "\n");
        }

        if (queued)
          speed = 100;        

        if (100.0 == speed
            || (speed != softStartLastSpeed
                && (!oldMode || (now - softStartSpeedChangeTime) > (totalDuration / softStartMaxSpeedChanges))))
        {
          softStartSpeedChangeTime = now;
        
//          fprintf(stderr, "slowstart: %lg, %lg\n", speed, p);

          m_xineLib.execFuncSetSpeed(speed);
          m_xineLib.execFuncWait();

//          if (100.0 == speed)
//            m_xineLib.execFuncSetPrebuffer(m_settings.GetModeParams()->m_prebufferFrames);
        
          softStartLastSpeed = speed;
        }
      }
    }

//if (jw) *(char *)0 = 0;
    jw = false;
    
//    fprintf(stderr, "v");
    
    if (ts)
    {
//NNN      stripPTSandDTS((uchar *)Data, Length);
    }
    else if (0) //ZZZ
    {
      int64_t pts = 0;
      
      if (np && getPTS(Data, Length, pts))
      {
        np = false;
        
//        fprintf(stderr, "M %lld %llx\n", pts);
        m_xineLib.execFuncMetronom(pts);
        m_xineLib.execFuncWait();
      }
    }
        
    int r = PlayCommon1(Data, Length, ptsForce);

//    fprintf(stderr, "V");

    return r;
  }

  int cXineDevice::PlayCommon1(const uchar *Data, int Length, int64_t ptsForce)
  {
/*
if (blahblah)
{
  fprintf(stderr, "blahblah C1");
  for (int i = 0; i < 50 && i < Length; i++)
    fprintf(stderr, " %02x", Data[ i ]);
  fprintf(stderr, "\n");
}
*/
    if (oldMode)
      return PlayCommon2(Data, Length, ptsForce);

    struct sBuffer
    {
      sBuffer *next;
      int length;
      int64_t ptsForce;
      uchar data[1];
    };

    static sBuffer *pHead = 0;
    static sBuffer *pTail = 0;
    static int bufCount = 0;

    while (!doClear && pHead)
    {
      if (sIdle != softStartState)
      {
        cPoller p;
        if (!m_xineLib.Poll(p, -1))
          break;
      }

      int r = PlayCommon2(pHead->data, pHead->length, pHead->ptsForce);
      if (r > 0)
      {
        sBuffer *p = pHead;
        pHead = pHead->next;
        ::free(p);
//fprintf(stderr, "bufCount: %d\n", --bufCount);
      }
      else if (r < 0)
      {
        while (pHead)
        {
          sBuffer *p = pHead;
          pHead = pHead->next;
          ::free(p);
        }

        bufCount = 0;
        pTail = 0;
//fprintf(stderr, "--- bufCount: %d\n", bufCount);
        return r;
      }
      else
        break;
    }
    
    if (doClear)
    {
//fprintf(stderr, "----- clearing -----\n");
      while (pHead)
      {
        sBuffer *p = pHead;
        pHead = pHead->next;
        ::free(p);
      }

      bufCount = 0;
      pTail = 0;
      doClear = false;
//fprintf(stderr, "=== bufCount: %d\n", bufCount);

//      return Length;
    }

    if (!pHead)
    {
      pTail = 0;
      
      do // ... while (false);
      {
        if (sIdle != softStartState)
        {
          cPoller p;
          if (!m_xineLib.Poll(p, -1))
            break;
//fprintf(stderr, "### bufCount: %d\n", bufCount);
        }

        return PlayCommon2(Data, Length, ptsForce);
      }
      while (false);
    }

    sBuffer *p = (sBuffer *)::malloc(sizeof (sBuffer) - sizeof (p->data) + Length);
    if (!p)
      return 0;

    p->next = 0;
    p->length = Length;
    p->ptsForce = ptsForce;
    memcpy(&p->data, Data, Length);

    if (!pTail)
      pHead = pTail = p;
    else
    {
      pTail->next = p;
      pTail = p;
    }

//fprintf(stderr, "*** bufCount: %d\n", ++bufCount);
    return Length;
  }

  static uchar jumboPESdata[ 6 + 0xffff ];
  static uchar *jumboPEStailData = 0;
  
  static bool mkJumboPES(const uchar *Data, int Length)
  {
    int origJumboPESsize = jumboPESsize;
    jumboPESsize = 0;
    
    if (Length < 9)
      VERBOSE_RETURN0(false);

    if (0x00 != Data[ 0 ])
      VERBOSE_RETURN0(false);
    
    if (0x00 != Data[ 1 ])
      VERBOSE_RETURN0(false);
    
    if (0x01 != Data[ 2 ])
      VERBOSE_RETURN0(false);
    
    if (0xbd != Data[ 3 ])
      VERBOSE_RETURN0(false);

    int l = Data[ 4 ] * 256 + Data[ 5 ];    
    if ((6 + l) != Length)
    {
      const uchar *data = Data + (6 + l);
      int length = Length - (6 + l);
      
      if (length < 6)
        VERBOSE_RETURN3(false);
      
      if (0x00 != data[ 0 ])
        VERBOSE_RETURN3(false);
      
      if (0x00 != data[ 1 ])
        VERBOSE_RETURN3(false);
      
      if (0x01 != data[ 2 ])
        VERBOSE_RETURN3(false);
      
      if (0xbe != data[ 3 ])
        VERBOSE_RETURN3(false);
 
      int L = data[ 4 ] * 256 + data[ 5 ];    
      if ((6 + L) != length)
        VERBOSE_RETURN3(false);

      // ignore padding
      Length -= length;
    }
/*
    for (int i = 0; i < 20; i++)
      fprintf(stderr, "%02x ", Data[ i ]);
    fprintf(stderr, "\n");
*/    
    bool cont = (0x80 == Data[ 6 ]
                 && 0x00 == Data[ 7 ]
                 && 0x00 == Data[ 8 ]);

    if (cont
        && Length >= 6 + 3 + 5
        && Data[  9 ] == 0x0b
        && Data[ 10 ] == 0x77
        && frameSizes[ Data[ 13 ] ] > 0)
    {
      cont = false;
    }
    
    if (!cont
      || 0 == origJumboPESsize)
    {
      if (0 != origJumboPESsize)
        VERBOSE_RETURN0(false);

      if ((origJumboPESsize + Length - 0) > (6 + 0xffff))
        VERBOSE_RETURN0(false);

      if (jumboPEStail > 0)
      {
        int headerSize = 6 + 3 + Data[ 8 ];
        ::memcpy(&jumboPESdata[ origJumboPESsize ], &Data[ 0 ], headerSize);

        ::memmove(&jumboPESdata[ origJumboPESsize + headerSize ], jumboPEStailData, jumboPEStail);
        
        ::memcpy(&jumboPESdata[ origJumboPESsize + headerSize + jumboPEStail ], &Data[ headerSize ], Length - headerSize);
        
        origJumboPESsize += headerSize + jumboPEStail + Length - headerSize;

        jumboPEStail = 0;
        jumboPEStailData = 0;

        //FIXME: PTS should be adjusted to take care of jumboPEStail's duration.
        //       Otherwise there is a certain jitter on audio duration <=> PTS.
      }
      else
      {
        ::memcpy(&jumboPESdata[ origJumboPESsize ], &Data[ 0 ], Length - 0);
        origJumboPESsize += Length - 0;
      }
    }
    else
    {
      if (0 == origJumboPESsize)
        VERBOSE_RETURN0(false);
      
      if ((origJumboPESsize + Length - 9) > (6 + 0xffff))
        VERBOSE_RETURN0(false);
        
      ::memcpy(&jumboPESdata[ origJumboPESsize ], &Data[ 9 ], Length - 9);
      origJumboPESsize += Length - 9;
    }

    if (0 == origJumboPESsize)
      VERBOSE_RETURN0(false);

    jumboPESsize = origJumboPESsize;

    if (2048 == Length)
    {
//      fprintf(stderr, " b %d", jumboPESsize);
      return false;
    }
    
    jumboPESdata[ 4 ] = (jumboPESsize - 6) >> 8;
    jumboPESdata[ 5 ] = (jumboPESsize - 6) & 0xff;
    
//    fprintf(stderr, " B %d", jumboPESsize);
    return true;
  }

  static int initFrameSizes()
  {
    ::memset(frameSizes, 0, sizeof (frameSizes));

    // fs = 48 kHz
    frameSizes[ 0x00 ] =   64;
    frameSizes[ 0x01 ] =   64;
    frameSizes[ 0x02 ] =   80;
    frameSizes[ 0x03 ] =   80;
    frameSizes[ 0x04 ] =   96;
    frameSizes[ 0x05 ] =   96;
    frameSizes[ 0x06 ] =  112;
    frameSizes[ 0x07 ] =  112;
    frameSizes[ 0x08 ] =  128;
    frameSizes[ 0x09 ] =  128;
    frameSizes[ 0x0a ] =  160;
    frameSizes[ 0x0b ] =  160;
    frameSizes[ 0x0c ] =  192;
    frameSizes[ 0x0d ] =  192;
    frameSizes[ 0x0e ] =  224;
    frameSizes[ 0x0f ] =  224;
    frameSizes[ 0x10 ] =  256;
    frameSizes[ 0x11 ] =  256;
    frameSizes[ 0x12 ] =  320;
    frameSizes[ 0x13 ] =  320;
    frameSizes[ 0x14 ] =  384;
    frameSizes[ 0x15 ] =  384;
    frameSizes[ 0x16 ] =  448;
    frameSizes[ 0x17 ] =  448;
    frameSizes[ 0x18 ] =  512;
    frameSizes[ 0x19 ] =  512;
    frameSizes[ 0x1a ] =  640;
    frameSizes[ 0x1b ] =  640;
    frameSizes[ 0x1c ] =  768;
    frameSizes[ 0x1d ] =  768;
    frameSizes[ 0x1e ] =  896;
    frameSizes[ 0x1f ] =  896;
    frameSizes[ 0x20 ] = 1024;
    frameSizes[ 0x21 ] = 1024;
    frameSizes[ 0x22 ] = 1152;
    frameSizes[ 0x23 ] = 1152;
    frameSizes[ 0x24 ] = 1280;
    frameSizes[ 0x25 ] = 1280;
    
    // fs = 44.1 kHz
    frameSizes[ 0x40 ] =   69;
    frameSizes[ 0x41 ] =   70;
    frameSizes[ 0x42 ] =   87;
    frameSizes[ 0x43 ] =   88;
    frameSizes[ 0x44 ] =  104;
    frameSizes[ 0x45 ] =  105;
    frameSizes[ 0x46 ] =  121;
    frameSizes[ 0x47 ] =  122;
    frameSizes[ 0x48 ] =  139;
    frameSizes[ 0x49 ] =  140;
    frameSizes[ 0x4a ] =  174;
    frameSizes[ 0x4b ] =  175;
    frameSizes[ 0x4c ] =  208;
    frameSizes[ 0x4d ] =  209;
    frameSizes[ 0x4e ] =  243;
    frameSizes[ 0x4f ] =  244;
    frameSizes[ 0x50 ] =  278;
    frameSizes[ 0x51 ] =  279;
    frameSizes[ 0x52 ] =  348;
    frameSizes[ 0x53 ] =  349;
    frameSizes[ 0x54 ] =  417;
    frameSizes[ 0x55 ] =  418;
    frameSizes[ 0x56 ] =  487;
    frameSizes[ 0x57 ] =  488;
    frameSizes[ 0x58 ] =  557;
    frameSizes[ 0x59 ] =  558;
    frameSizes[ 0x5a ] =  696;
    frameSizes[ 0x5b ] =  697;
    frameSizes[ 0x5c ] =  835;
    frameSizes[ 0x5d ] =  836;
    frameSizes[ 0x5e ] =  975;
    frameSizes[ 0x5f ] =  976;
    frameSizes[ 0x60 ] = 1114;
    frameSizes[ 0x61 ] = 1115;
    frameSizes[ 0x62 ] = 1253;
    frameSizes[ 0x63 ] = 1254;
    frameSizes[ 0x64 ] = 1393;
    frameSizes[ 0x65 ] = 1394;
    
    // fs = 32 kHz
    frameSizes[ 0x80 ] =   96;
    frameSizes[ 0x81 ] =   96;
    frameSizes[ 0x82 ] =  120;
    frameSizes[ 0x83 ] =  120;
    frameSizes[ 0x84 ] =  144;
    frameSizes[ 0x85 ] =  144;
    frameSizes[ 0x86 ] =  168;
    frameSizes[ 0x87 ] =  168;
    frameSizes[ 0x88 ] =  192;
    frameSizes[ 0x89 ] =  192;
    frameSizes[ 0x8a ] =  240;
    frameSizes[ 0x8b ] =  240;
    frameSizes[ 0x8c ] =  288;
    frameSizes[ 0x8d ] =  288;
    frameSizes[ 0x8e ] =  336;
    frameSizes[ 0x8f ] =  336;
    frameSizes[ 0x90 ] =  384;
    frameSizes[ 0x91 ] =  384;
    frameSizes[ 0x92 ] =  480;
    frameSizes[ 0x93 ] =  480;
    frameSizes[ 0x94 ] =  576;
    frameSizes[ 0x95 ] =  576;
    frameSizes[ 0x96 ] =  672;
    frameSizes[ 0x97 ] =  672;
    frameSizes[ 0x98 ] =  768;
    frameSizes[ 0x99 ] =  768;
    frameSizes[ 0x9a ] =  960;
    frameSizes[ 0x9b ] =  960;
    frameSizes[ 0x9c ] = 1152;
    frameSizes[ 0x9d ] = 1152;
    frameSizes[ 0x9e ] = 1344;
    frameSizes[ 0x9f ] = 1344;
    frameSizes[ 0xa0 ] = 1536;
    frameSizes[ 0xa1 ] = 1536;
    frameSizes[ 0xa2 ] = 1728;
    frameSizes[ 0xa3 ] = 1728;
    frameSizes[ 0xa4 ] = 1920;
    frameSizes[ 0xa5 ] = 1920;
    
    return 0;
  };
  
#if APIVERSNUM < 10318

  void cXineDevice::PlayAudio(const uchar *Data, int Length)
  {
    cDevice::PlayAudio(Data, Length);

    PlayAudioCommon(Data, Length);
  }

#else

  int cXineDevice::GetAudioChannelDevice(void)
  {
    return m_audioChannel;
  }
static int m_acd = -1;
  void cXineDevice::SetAudioChannelDevice(int AudioChannel)
  {
#if APIVERSNUM >= 10701
if (m_acd == AudioChannel)
  return;
#endif
m_acd = AudioChannel;

    xfprintf(stderr, "SetAudioChannelDevice: %d\n", AudioChannel);

    m_audioChannel = AudioChannel;

    m_xineLib.execFuncSelectAudio(m_audioChannel);
  }
static int m_dad = -1;
  void cXineDevice::SetDigitalAudioDevice(bool On)
  {
#if APIVERSNUM >= 10701
if (m_dad == On)
  return;
#endif
m_dad = On;

    xfprintf(stderr, "SetDigitalAudioDevice: %d\n", On);
//aaz = On ? 1 : 2;
    m_xineLib.execFuncSelectAudio(On ? -1 : m_audioChannel);

    if (pmNone == pm)
      return;

    doClear = true;

    if (m_settings.LiveTV()
      && !audioSeen)
    {
      cMutexLock lock(&softStartMutex);
      if (softStartState == sIdle)
        softStartTrigger = sstNoMetronom;
      
      return;
    }
    
    m_xineLib.pause();
//xzabc = 1;    
    jumboPESsize = 0;
    jumboPEStail = 0;

    if (f)
        m_xineLib.execFuncSetSpeed(100.0);
//double t0 = tNow();
    if (m_settings.LiveTV()
      || !foundVideo) // radio recording: audio channels are not related
    {
      ptsV = ptsA = ptsP = ptsD = -1;
    
      m_xineLib.execFuncClear(-3);

//      if (!foundVideo)
//        m_xineLib.execFuncStart();
//      np = true;
    }
    
    m_xineLib.execFuncResetAudio();

    if (f)
        m_xineLib.execFuncSetSpeed(0.0);
    
    m_xineLib.execFuncWait();
//xzabc = 2;
    m_xineLib.pause(false);
//double t1 = tNow(); fprintf(stderr, "!!!!!!! %.3lf ms\n", (t1 - t0) * 1000);
    if (m_settings.LiveTV())
    {
      cMutexLock lock(&softStartMutex);
      softStartTrigger = sstNoMetronom;
    } 
//fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");    
  }

#if APIVERSNUM < 10342  

  int cXineDevice::PlayAudio(const uchar *Data, int Length)
  {
//    fprintf(stderr, " 0x%02x ", Data[ 3 ]);
    return PlayAudioCommon(Data, Length);
  }

#else
  
  int cXineDevice::PlayAudio(const uchar *Data, int Length, uchar /* Id */)
  {
//    fprintf(stderr, " 0x%02x ", Data[ 3 ]);
    return PlayAudioCommon(Data, Length);
  }

#endif
#endif
  
  int cXineDevice::PlayAudioCommon(const uchar *Data, int Length)
  {
    if (ttt5 == 0) ttt5 = tNow();
//    fprintf(stderr, " 0x%02x: %d ", Data[ 3 ], Data[4] * 256 + Data[5]);
/*
if (Data[ 3 ] == 0xd0) {
  FILE *f = fopen("/tmp/d0", "ab");
  fwrite(Data, Length, 1, f);
  fclose(f);
}
*/
    {
      static int i = initFrameSizes();
      (void)i;
    }
    
store_frame(Data, Length, __LINE__);
 
    LOG_ME(::fprintf(stderr, "A");)
    
    if (f)
    {      
      LOG_ME(::fprintf(stderr, "<");)
      return Length;
    }
    
    if (pmNone == pm)
    {
      cMutexLock pmMutexLock(&m_pmMutex);

      if (pmNone == pm)
        m_pmCondVar.Wait(m_pmMutex);
    }

    int retVal = PlayAudio2(Data, Length);
    
    LOG_ME(::fprintf(stderr, "a"));

    return retVal;
  }
  
  int cXineDevice::PlayAudio2(const uchar *Data, int Length)
  {
//    fprintf(stderr, "D");
    
    int done = 0;

    while (done < Length)
    {
      char ch = 'X';

      int id = 0x00;
      
      int todo = Length - done;
      if (todo >= 6)
      {
        if (0x00 == Data[ done + 0 ]
            && 0x00 == Data[ done + 1 ]
            && 0x01 == Data[ done + 2 ])
        {
          id  = Data[ done + 3 ];
          int len = Data[ done + 4 ] * 0x0100 + Data[ done + 5 ];

          if (todo >= (6 + len))
          {
            todo = (6 + len);

            if (0xbe == id)
            {
              done += todo;

//              ::fprintf(stderr, "x");
              continue;
            }

            ch = '.';            
          }
          else        
          {
            VERBOSE_NOP();
            ch = '3';
          }
        }
        else        
        {
          VERBOSE_NOP();
          ch = '2';
        }
      }
      else        
      {
        VERBOSE_NOP();
        ch = '1';
      }
      
//      ::fprintf(stderr, "%c", ch);

      int r;
      
      if (0xbd == id)
        r = PlayAudio3(Data + done, todo);
      else
        r = PlayVideo3(Data + done, todo, false);
        
      if (r < 0)
        return r;
      
      if (r != todo)
        VERBOSE_NOP();
                
      done += r;
    }
    
    return done;
  }
  
  int cXineDevice::PlayAudio3(const uchar *Data, int Length)
  {
    resetScramblingControl(Data, Length);
    
store_frame(Data, Length, __LINE__);    
/*    
    ::fprintf(stderr, "l: %d\t", Length);
    for (int i = 0; i < 20; i++)
      ::fprintf(stderr, "%02x ", Data[ i ]);
    ::fprintf(stderr, "\n");
*/ 
//    fprintf(stderr, "A");
    
    if (mkJumboPES(Data, Length))
    {
      int todo = jumboPESsize;
      jumboPESsize = 0;

      dumpAudio("Audio", jumboPESdata, todo);

      bool dolby = false;
      bool pcm = false;
      
      do
      {
        if (todo < (6 + 3 + 0 + 2))
          break;
        
        if (0x00 != jumboPESdata[ 0 ]
            || 0x00 != jumboPESdata[ 1 ]
            || 0x01 != jumboPESdata[ 2 ]
            || 0xbd != jumboPESdata[ 3 ])
        {
          break;
        }

        int l = jumboPESdata[ 4 ] * 0x0100 + jumboPESdata[ 5 ];
        if (l < (3 + 0 + 2))
          break;

        if (todo < (6 + l))
          break;

        int h = jumboPESdata[ 8 ];
        if (l < (3 + h + 2))
          break;

        if (0x0b == jumboPESdata[ 6 + 3 + h + 0 ]
            && 0x77 == jumboPESdata[ 6 + 3 + h + 1 ])
        {
          if (l < (3 + h + 2 + 2 + 1))
          {
            VERBOSE_NOP();            
            break;
          }
          
          int frameStart = 6 + 3 + h;
          bool failed = false;
          
          while (true)
          {
            int frameSize = 2 * frameSizes[ jumboPESdata[ frameStart + 4 ] ];
            if (frameSize <= 0)
            {
              failed = true;
              
              xfprintf(stderr, "frame_size_code: 0x%02x\n", jumboPESdata[ frameStart + 4 ]);
              VERBOSE_NOP();              
              break;
            }

            if (frameStart + frameSize > todo)
              break;

            frameStart += frameSize;
            
            if (frameStart + 2 + 2 + 1 > todo)
              break;

            if (0x0b != jumboPESdata[ frameStart + 0 ]
                || 0x77 != jumboPESdata[ frameStart + 1 ])
            {
              failed = true;
              
              VERBOSE_NOP();
              break;
            }
          }
          
          if (failed)
            break;
          
          jumboPEStail = todo - frameStart;
          jumboPEStailData = jumboPESdata + frameStart;

          todo = frameStart;

          jumboPESdata[ 4 + 0 ] = (todo - 6) >> 8;
          jumboPESdata[ 4 + 1 ] = (todo - 6) & 0xff;
          
          dolby = true;
store_frame(jumboPESdata, todo, __LINE__);    
          break;
        }

        if (0x80 == (0xf0 & jumboPESdata[ 6 + 3 + h + 0 ]))
        {
          dolby = true;
          break;
        }
        
        if (0xa0 == (0xf0 & jumboPESdata[ 6 + 3 + h + 0 ]))
        {
          pcm = true;
          break;
        }

        for (int i = 6 + 3 + h; i < todo - 2 - 2 - 1; i++)
        {
          if (0x0b == jumboPESdata[ i + 0 ]
              && 0x77 == jumboPESdata[ i + 1 ]
              && 0 != frameSizes[ jumboPESdata[ i + 4 ] ])
          {
            jumboPEStail = todo - i;
            jumboPEStailData = jumboPESdata + i;
          }
        }
      }
      while (false);

      if (pcm
          || (dolby && m_settings.AudioDolbyOn()))
      {
        int done = 0;
        
        while (done < todo)
        {
          int r = PlayCommon(jumboPESdata + done, todo - done, false);
          if (r < 0)
            return r;
          
//          fprintf(stderr, ".");
          
          done += r;
        }

// Don't return done here as the remaining bytes were buffered elsewhere!
//        return done;
      }
    }
    else if (jumboPESsize == Length)
    {
      int todo = jumboPESsize;

      bool dolby = false;
      bool pcm = false;
      
      do
      {
        if (todo < (6 + 3 + 0 + 2))
          break;
        
        if (0x00 != jumboPESdata[ 0 ]
            || 0x00 != jumboPESdata[ 1 ]
            || 0x01 != jumboPESdata[ 2 ]
            || 0xbd != jumboPESdata[ 3 ])
        {
          break;
        }

        int l = jumboPESdata[ 4 ] * 0x0100 + jumboPESdata[ 5 ];
        if (l < (3 + 0 + 2))
          break;

        if (todo < (6 + l))
          break;

        int h = jumboPESdata[ 8 ];
        if (l < (3 + h + 2))
          break;

        if (0x80 == (0xf0 & jumboPESdata[ 6 + 3 + h + 0 ]))
        {
          dolby = true;
          break;
        }
        
        if (0xa0 == (0xf0 & jumboPESdata[ 6 + 3 + h + 0 ]))
        {
          pcm = true;
          break;
        }
      }
      while (false);

      if (dolby || pcm)
      {
        dumpAudio("Audio", jumboPESdata, todo);

        jumboPESsize = 0;
      }

      if (pcm
          || (dolby && m_settings.AudioDolbyOn()))
      {
        int done = 0;
        
        while (done < todo)
        {
          int r = PlayCommon(jumboPESdata + done, todo - done, false);
          if (r < 0)
            return r;
          
//          fprintf(stderr, ".");
          
          done += r;
        }

// Don't return done here as the remaining bytes were buffered elsewhere!
//        return done;
      }
    }

//    fprintf(stderr, "\n");

    return Length;
  }
  
#if APIVERSNUM >= 10338
  uchar *cXineDevice::GrabImage(int &Size, bool Jpeg /* = true */, int Quality /* = -1 */, int SizeX /* = -1 */, int SizeY /* = -1 */)
  {
    const char *const FileName = 0;
#else
  bool cXineDevice::GrabImage(const char *FileName, bool Jpeg /* = true */, int Quality /* = -1 */, int SizeX /* = -1 */, int SizeY /* = -1 */)
  {
    int Size = 0;
#endif
    xfprintf(stderr, "GrabImage ...\n\n");

    if (-1 == Quality)
      Quality = 100;

    if (-1 == SizeX)
      SizeX = m_settings.DefaultGrabSizeX();
 
    if (-1 == SizeY)
      SizeY = m_settings.DefaultGrabSizeY();

    uchar *result = m_xineLib.execFuncGrabImage(FileName, Size, Jpeg, Quality, SizeX, SizeY);

    xfprintf(stderr, result ? "\nGrabImage succeeded.\n" : "\nGrabImage failed.\n");    
    return result;
  }

  int64_t cXineDevice::GetSTC(void)
  {
//    ::fprintf(stderr, "GetSTC: ");
    
    int64_t pts = -1;

    if (!m_xineLib.execFuncGetPTS(pts, 100) || pts < 0)
      pts = cDevice::GetSTC();

//    ::fprintf(stderr, "%lld\n", pts);
    
    return pts;
  }
  
  void cXineDevice::SetVideoFormat(bool VideoFormat16_9)
  {
    xfprintf(stderr, "SetVideoFormat: %d\n", VideoFormat16_9);
    cDevice::SetVideoFormat(VideoFormat16_9); 

    m_is16_9 = VideoFormat16_9;
    m_xineLib.selectNoSignalStream(VideoFormat16_9);
  }

#if APIVERSNUM >= 10707
#if APIVERSNUM >= 10708
  void cXineDevice::GetVideoSize(int &Width, int &Height, double &VideoAspect)
  {
    int x, y, zx, zy;
    m_xineLib.execFuncVideoSize(x, y, Width, Height, zx, zy, &VideoAspect); 
  }

  void cXineDevice::GetOsdSize(int &Width, int &Height, double &PixelAspect)
#else
  void cXineDevice::GetVideoSize(int &Width, int &Height, eVideoAspect &Aspect)
  // function name misleading -- max osd extent shall be returned
#endif
  {
    Width  = m_settings.OsdExtentParams().GetOsdExtentWidth();
    Height = m_settings.OsdExtentParams().GetOsdExtentHeight();
#if APIVERSNUM >= 10708
    PixelAspect = (m_is16_9 ? 16.0 : 4.0) * Height / ((m_is16_9 ? 9.0 : 3.0) * Width);
//fprintf(stderr, "GetOsdSize(%d, %d, %lg)\n", Width, Height, PixelAspect);
#else
    Aspect = m_is16_9 ? va16_9 : va4_3;
#endif
  }
#endif

  static bool firstCallToSetVolume = true;
  static void switchSkin(const bool restore);
  
  void cXineDevice::SetVolumeDevice(int Volume)
  {
    if (firstCallToSetVolume)
    {
      firstCallToSetVolume = false;

      if (m_settings.ShallSwitchSkin())
        switchSkin(false);
    }
    
    xfprintf(stderr, "SetVolumeDevice: %d\n", Volume);
    m_xineLib.execFuncSetVolume(Volume);
  }
  
#if APIVERSNUM < 10307
  cOsdBase *cXineDevice::NewOsd(int w, int h, int x, int y)
#elif APIVERSNUM < 10509    
  cOsd *cXineDevice::NewOsd(int w, int h, int x, int y)
#else
  cOsd *cXineDevice::NewOsd(int w, int h, int x, int y, uint Level, bool TrueColor /* = false */)
#endif    
  {
//    ::fprintf(stderr, "NewOsd ---: %s\n", ::ctime(&(const time_t &)::time(0)));
    cXineOsdMutexLock osdLock(&m_osdMutex);
//    ::fprintf(stderr, "NesOsd +++: %s\n", ::ctime(&(const time_t &)::time(0)));
#if APIVERSNUM < 10509    
    if (m_currentOsd)
    {
      esyslog("vdr-xine: new OSD(%d, %d) requested while another OSD is active", x, y);
      xfprintf(stderr, "vdr-xine: new OSD(%d, %d) requested while another OSD is active", x, y);
      return 0;
    }
#endif    
    if (x < 0 || y < 0)
    {
      esyslog("vdr-xine: new OSD(%d, %d) requested with coordinates out of range", x, y);
      xfprintf(stderr, "vdr-xine: new OSD(%d, %d) requested with coordinates out of range", x, y);
    }

    if (w <= 0 || h <= 0)
    {
      w = m_settings.OsdExtentParams().GetOsdExtentWidth();
      h = m_settings.OsdExtentParams().GetOsdExtentHeight();
    }

#if APIVERSNUM < 10509    
    m_currentOsd = new cXineOsd(*this, w, h, x, y);
#else
    return new cXineOsd(*this, w, h, x, y, Level);
#endif    

    return m_currentOsd;
  }

#if APIVERSNUM < 10307
  void cXineDevice::OnFreeOsd(cOsdBase *const osd)
#else    
  void cXineDevice::OnFreeOsd(cOsd *const osd)
#endif    
  {
#if APIVERSNUM < 10509    
    cXineOsdMutexLock osdLock(&m_osdMutex);
    
    assert(osd == m_currentOsd);

    m_currentOsd = 0;
#endif    
  }

  bool cXineDevice::ChangeCurrentOsd(cXineOsd *const osd, bool on)
  {
    cXineOsdMutexLock osdLock(&m_osdMutex);

    if (m_currentOsd)
    {
      if (on)
      {
        esyslog("vdr-xine: OSD activation requested while another OSD is active -- ignoring request");
        xfprintf(stderr, "vdr-xine: OSD activation requested while another OSD is active -- ignoring request");
        return false;
      }
      else if (m_currentOsd != osd)
      {
        esyslog("vdr-xine: OSD deactivation requested for an OSD which is not active -- ignoring request");
        xfprintf(stderr, "vdr-xine: OSD deactivation requested for an OSD which is not active -- ignoring request");
        return false;
      }
      else
      {
        m_currentOsd = 0;
      }
    }
    else
    {
      if (!on)
      {
        esyslog("vdr-xine: OSD deactivation requested while no OSD is active -- ignoring request");
        xfprintf(stderr, "vdr-xine: OSD deactivation requested while no OSD is active -- ignoring request");
        return false;
      }
      else
      {
        m_currentOsd = osd;
      }
    }

    return true;
  }


  
  static cDevice *originalPrimaryDevice = 0;
  
#if APIVERSNUM >= 10307
  
  void cXineDevice::MakePrimaryDevice(bool On)
  {
    cDevice::MakePrimaryDevice(On);

    xfprintf(stderr, "-------------------------\n");
    xfprintf(stderr, "MakePrimaryDevice: %d\n", On);
    xfprintf(stderr, "=========================\n");

    if (On)
      new cXineOsdProvider(*this);
    else
      cOsdProvider::Shutdown();
    
    originalPrimaryDevice = 0;
  }
  
#endif    



#if APIVERSNUM >= 10320
  static cSkin *origSkin = 0;
#endif
  
  static void switchSkin(const bool restore)
  {
#if APIVERSNUM >= 10320
    if (restore)
    {
      Skins.SetCurrent(origSkin->Name());
      cThemes::Load(Skins.Current()->Name(), Setup.OSDTheme, Skins.Current()->Theme());
    }
    else
    {
      origSkin = Skins.Current();
      Skins.SetCurrent("curses");
      cThemes::Load(Skins.Current()->Name(), Setup.OSDTheme, Skins.Current()->Theme());
    }
#else
#warning vdr-xine: switching skins is only available for VDR versions >= 1.3.20
    isyslog("vdr-xine: switching skins is only available for VDR versions >= 1.3.20");
#endif
  }
  
  void cXineDevice::reshowCurrentOsd(const bool dontOptimize /* = true */, const int frameLeft /* = -1 */, const int frameTop /* = -1 */, const int frameWidth /* = -1 */, const int frameHeight /* = -1 */, const int frameZoomX /* = -1 */, const int frameZoomY /* = -1 */, const bool unlockOsdUpdate /* = false */)
  {
    cXineOsdMutexLock osdLock(&m_osdMutex);
    
    if (unlockOsdUpdate)
      m_osdUpdateLocked = false;

    if (m_currentOsd)
      m_currentOsd->ReshowCurrentOsd(dontOptimize, frameLeft, frameTop, frameWidth, frameHeight, frameZoomX, frameZoomY);
  }

  void cXineDevice::LockOsdUpdate()
  {
    cXineOsdMutexLock osdLock(&m_osdMutex);

    m_osdUpdateLocked = true;
  }

  bool cXineDevice::OsdUpdateLocked()
  {
    cXineOsdMutexLock osdLock(&m_osdMutex);

    return m_osdUpdateLocked;
  }

  void cXineDevice::mainMenuTrampoline()
  {
#if APIVERSNUM >= 10332
    cMutexLock switchPrimaryDeviceLock(&m_switchPrimaryDeviceMutex);
    if (m_switchPrimaryDeviceDeviceNo < 0)
      return;

    cControl::Shutdown();

    if (m_switchPrimaryDeviceDeviceNo == (1 + DeviceNumber()))
    {  
      char *msg = 0;
      ::asprintf(&msg, tr("Switching primary DVB to %s..."), m_plugin->Name());

      Skins.Message(mtInfo, msg);
      ::free(msg);
    }

    SetPrimaryDevice(m_switchPrimaryDeviceDeviceNo);

    if (m_switchPrimaryDeviceDeviceNo != (1 + DeviceNumber()))
    {
      char *msg = 0;
      ::asprintf(&msg, tr("Switched primary DVB back from %s"), m_plugin->Name());

      Skins.Message(mtInfo, msg);
      ::free(msg);
    }

    m_switchPrimaryDeviceDeviceNo = -1;

    m_switchPrimaryDeviceCond.Broadcast();
#endif
  }

  void cXineDevice::switchPrimaryDevice(const int deviceNo, const bool waitForExecution)
  {
#if APIVERSNUM >= 10332
#if APIVERSNUM < 10347
    while (cRemote::HasKeys())
      cCondWait::SleepMs(10);
#endif

    cMutexLock switchPrimaryDeviceLock(&m_switchPrimaryDeviceMutex);
    m_switchPrimaryDeviceDeviceNo = deviceNo;

#if APIVERSNUM < 10347
    cRemote::CallPlugin(m_plugin->Name());
#endif

    if (waitForExecution)
      m_switchPrimaryDeviceCond.Wait(m_switchPrimaryDeviceMutex);
#else
#warning vdr-xine: switching primary device is no longer supported for VDR versions < 1.3.32 
    isyslog("vdr-xine: switching primary device is no longer supported for VDR versions < 1.3.32");
#endif
  }

  void cXineDevice::OnClientConnect()
  {
    reshowCurrentOsd();

    if (m_settings.LiveTV())
    {
      cMutexLock lock(&softStartMutex);
      softStartTrigger = sstNormal;
    }

    if (m_settings.AutoPrimaryDevice())
    {
      cDevice *primaryDevice = cDevice::PrimaryDevice();
      if (this != primaryDevice)
        switchPrimaryDevice(1 + DeviceNumber(), true);

      originalPrimaryDevice = primaryDevice;
    }

    if (m_settings.ShallSwitchSkin())
      switchSkin(true);
  }

  void cXineDevice::OnClientDisconnect()
  {
    if (m_settings.ShallSwitchSkin())
      switchSkin(false);

    if (m_settings.AutoPrimaryDevice()
      && originalPrimaryDevice)
    {
      if (this != originalPrimaryDevice)
        switchPrimaryDevice(1 + originalPrimaryDevice->DeviceNumber(), false);
    }
  }
  
  void cXineDevice::ReshowCurrentOSD(const int frameLeft, const int frameTop, const int frameWidth, const int frameHeight, const int frameZoomX, const int frameZoomY, const bool unlockOsdUpdate)
  {
//    ::fprintf(stderr, ">>> cXineDevice::ReshowCurrentOSD()\n");
    reshowCurrentOsd(false, frameLeft, frameTop, frameWidth, frameHeight, frameZoomX, frameZoomY, unlockOsdUpdate);
//    ::fprintf(stderr, "<<< cXineDevice::ReshowCurrentOSD()\n");
  }

  bool cXineDevice::DeviceReplayingOrTransferring()
  {
#if APIVERSNUM >= 10341
    return Replaying() || Transferring();
#else
    return Replaying() /* || Transferring() */;
#endif
  }

  bool cXineDevice::open()
  {
    if (!m_xineLib.Open())
      return false;

    if (m_settings.ShallSwitchSkin())
    {
#if APIVERSNUM >= 10320
      Skins.SetCurrent(Setup.OSDSkin);
      cThemes::Load(Skins.Current()->Name(), Setup.OSDTheme, Skins.Current()->Theme());
#endif
      switchSkin(false);
    }
    
    return true;
  }

  void cXineDevice::close()
  {
    m_xineLib.Close();

    if (m_settings.ShallSwitchSkin())
      switchSkin(true);
  }

  void cXineDevice::Stop()
  {
    if (!theXineDevice)
      return;

    theXineDevice->close();
  }

  cXineDevice::cXineDevice(cPlugin *const plugin, cXineSettings &settings, cXineRemote *remote)
    : cDevice()
    , m_settings(settings)
    , m_osdUpdateLocked(false)
    , m_currentOsd(0)
    , m_spuDecoder(0)
    , m_is16_9(true)
    , m_audioChannel(0)
    , m_plugin(plugin)
    , m_switchPrimaryDeviceDeviceNo(-1)
    , m_xineLib(plugin, settings, m_osdMutex, remote)
  {
    m_xineLib.SetEventSink(this);
  }
  
  cXineDevice::~cXineDevice()
  {
#if APIVERSNUM < 10320
    close();
#endif
    if (m_spuDecoder)
      delete m_spuDecoder;
  }
  
  bool cXineDevice::Create(cPlugin *const plugin, cXineSettings &settings, cXineRemote *remote)
  {
    if (theXineDevice)
      return false;

    theXineDevice = new cXineDevice(plugin, settings, remote);
    
    return 0 != theXineDevice
      && theXineDevice->hasNoSignalStream();
  }

  bool cXineDevice::Open()
  {
    if (!theXineDevice)
      return false;

    return theXineDevice->open();
  }

  cXineDevice *cXineDevice::GetDevice()
  {
    return theXineDevice;
  }
#if APIVERSNUM >= 10733
  ///< Asks the output device whether it can scale the currently shown video in
  ///< such a way that it fits into the given Rect, while retaining its proper
  ///< aspect ratio. If the scaled video doesn't exactly fit into Rect, Alignment
  ///< is used to determine how to align the actual rectangle with the requested
  ///< one. The actual rectangle can be smaller, larger or the same size as the
  ///< given Rect, and its location may differ, depending on the capabilities of
  ///< the output device, which may not be able to display a scaled video at
  ///< arbitrary sizes and locations. The device shall, however, do its best to
  ///< match the requested Rect as closely as possible, preferring a size and
  ///< location that fits completely into the requested Rect if possible.
  ///< Returns the rectangle that can actually be used when scaling the video.
  ///< A skin plugin using this function should rearrange its content according
  ///< to the rectangle returned from calling this function, and should especially
  ///< be prepared for cases where the returned rectangle is way off the requested
  ///< Rect, or even Null. In such cases, the skin may want to fall back to
  ///< working with full screen video.
  ///< If this device can't scale the video, a Null rectangle is returned (this
  ///< is also the default implementation).
  cRect cXineDevice::CanScaleVideo(const cRect &Rect, int Alignment/* = taCenter*/)
  {
    // first implementation: we can always scale, we're a soft device ;-), ignore alignment for now

    // we need to store the value for the case we have to call ScaleVideo ourselves in vdr-xine
    vidWinRect = Rect;
    return vidWinRect;
  }

  ///< Scales the currently shown video in such a way that it fits into the given
  ///< Rect. Rect should be one retrieved through a previous call to
  ///< CanScaleVideo() (otherwise results may be undefined).
  ///< Even if video output is scaled, the functions GetVideoSize() and
  ///< GetOsdSize() must still return the same values as if in full screen mode!
  ///< If this device can't scale the video, nothing happens.
  ///< To restore full screen video, call this function with a Null rectangle.
  void cXineDevice::ScaleVideo(const cRect &Rect/* = cRect::Null*/)
  {
    // refresh stored value
    vidWinRect = Rect;
    // let our specialized code do the actual resizing / repositioning, get accurate parameters first
    int videoLeft, videoTop, videoWidth, videoHeight, videoZoomX, videoZoomY, osdWidth, osdHeight;
    double videoAspect, pixelAspect;
    m_xineLib.execFuncVideoSize(videoLeft, videoTop, videoWidth, videoHeight, videoZoomX, videoZoomY, &videoAspect);
    GetOsdSize(osdWidth, osdHeight, pixelAspect);
    tArea vidWinArea;
    vidWinArea.x1 = vidWinRect.X();
    vidWinArea.y1 = vidWinRect.Y();
    vidWinArea.x2 = vidWinRect.X() + vidWinRect.Width();
    vidWinArea.y2 = vidWinRect.Y() + vidWinRect.Height();
    if (vidWinRect == cRect::Null) {
        // will just resize to full size
        vidWinArea.bpp = 0;
    } else {
        vidWinArea.bpp = 12;
        // make corrections
        double aspectFactor = (double(osdWidth) / double(osdHeight)) / videoAspect;
        int output_width = vidWinRect.Height() * (videoAspect * aspectFactor);
        int output_height = vidWinRect.Width() / (videoAspect * aspectFactor);
        if (double(vidWinRect.Width())/double(vidWinRect.Height()) > videoAspect * aspectFactor) {
            output_height = vidWinRect.Height();
            vidWinArea.x1 += (vidWinRect.Width() - output_width) / 2;
        }
        else if (double(vidWinRect.Width())/double(vidWinRect.Height()) < videoAspect * aspectFactor) {
            output_width = vidWinRect.Width();
            vidWinArea.y1 += (vidWinRect.Height() - output_height) / 2;
        }
        vidWinArea.x2 = vidWinArea.x1 + output_width;
        vidWinArea.y2 = vidWinArea.y1 + output_height;
    }
    m_xineLib.SetVideoWindow(videoWidth, videoHeight, vidWinArea);
  }

  const cRect & cXineDevice::GetScaleRect()
  {
    // just return the stored value
    return vidWinRect;
  }

#endif // APIVERSNUM >= 10733
};