[28a33b]: src / demuxers / demux_ts.c Maximize Restore History

Download this file

demux_ts.c    2779 lines (2332 with data), 81.0 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 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
/*
* Copyright (C) 2000-2003 the xine project
*
* This file is part of xine, a free video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* Demultiplexer for MPEG2 Transport Streams.
*
* For the purposes of playing video, we make some assumptions about the
* kinds of TS we have to process. The most important simplification is to
* assume that the TS contains a single program (SPTS) because this then
* allows significant simplifications to be made in processing PATs.
*
* The next simplification is to assume that the program has a reasonable
* number of video, audio and other streams. This allows PMT processing to
* be simplified.
*
* MODIFICATION HISTORY
*
* Date Author
* ---- ------
*
* 8-Apr-2009 Petri Hintukainen <phi@sdf-eu.org>
* - support for 192-byte packets (HDMV/BluRay)
* - support for audio inside PES PID 0xfd (HDMV/BluRay)
* - demux HDMV/BluRay bitmap subtitles
*
* 28-Nov-2004 Mike Lampard <mlampard>
* - Added support for PMT sections larger than 1 ts packet
*
* 28-Aug-2004 James Courtier-Dutton <jcdutton>
* - Improve PAT and PMT handling. Added some FIXME comments.
*
* 9-Aug-2003 James Courtier-Dutton <jcdutton>
* - Improve readability of code. Added some FIXME comments.
*
* 25-Nov-2002 Peter Liljenberg
* - Added DVBSUB support
*
* 07-Nov-2992 Howdy Pierce
* - various bugfixes
*
* 30-May-2002 Mauro Borghi
* - dynamic allocation leaks fixes
*
* 27-May-2002 Giovanni Baronetti and Mauro Borghi <mauro.borghi@tilab.com>
* - fill buffers before putting them in fifos
* - force PMT reparsing when PMT PID changes
* - accept non seekable input plugins -- FIX?
* - accept dvb as input plugin
* - optimised read operations
* - modified resync code
*
* 16-May-2002 Thibaut Mattern <tmattern@noos.fr>
* - fix demux loop
*
* 07-Jan-2002 Andr Draszik <andid@gmx.net>
* - added support for single-section PMTs
* spanning multiple TS packets
*
* 10-Sep-2001 James Courtier-Dutton <jcdutton>
* - re-wrote sync code so that it now does not loose any data
*
* 27-Aug-2001 Hubert Matthews Reviewed by: n/a
* - added in synchronisation code
*
* 1-Aug-2001 James Courtier-Dutton <jcdutton> Reviewed by: n/a
* - TS Streams with zero PES length should now work
*
* 30-Jul-2001 shaheedhaque Reviewed by: n/a
* - PATs and PMTs seem to work
*
* 29-Jul-2001 shaheedhaque Reviewed by: n/a
* - Compiles!
*
*
* TODO: do without memcpys, preview buffers
*/
/** HOW TO IMPLEMENT A DVBSUB DECODER.
*
* The DVBSUB protocol is specified in ETSI EN 300 743. It can be
* downloaded for free (registration required, though) from
* www.etsi.org.
*
* The spu_decoder should handle the packet type BUF_SPU_DVB.
*
* BUF_SPU_DVBSUB packets without the flag BUF_FLAG_SPECIAL contain
* the payload of the PES packets carrying DVBSUB data. Since the
* payload can be broken up over several buf_element_t and the DVBSUB
* is PES oriented, the decoder_info[2] field (low 16 bits) is used to convey the
* packet boundaries to the decoder:
*
* + For the first buffer of a packet, buf->content points to the
* first byte of the PES payload. decoder_info[2] is set to the length of the
* payload. The decoder can use this value to determine when a
* complete PES packet has been collected.
*
* + For the following buffers of the PES packet, decoder_info[2] is 0.
*
* The decoder can either use this information to reconstruct the PES
* payload, or ignore it and implement a parser that handles the
* irregularites at the start and end of PES packets.
*
* In any case buf->pts is always set to the PTS of the PES packet.
*
*
* BUF_SPU_DVB with BUF_FLAG_SPECIAL set contains no payload, and is
* used to pass control information to the decoder.
*
* If decoder_info[1] == BUF_SPECIAL_SPU_DVB_DESCRIPTOR then
* decoder_info_ptr[2] either points to a spu_dvb_descriptor_t or is NULL.
*
* If it is 0, the user has disabled the subtitling, or has selected a
* channel that is not present in the stream. The decoder should
* remove any visible subtitling.
*
* If it is a pointer, the decoder should reset itself and start
* extracting the subtitle service identified by comp_page_id and
* aux_page_id in the spu_dvb_descriptor_t, (the composition and
* auxilliary page ids, respectively).
**/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define LOG_MODULE "demux_ts"
#define LOG_VERBOSE
/*
#define LOG
*/
#include "xine_internal.h"
#include "xineutils.h"
#include "demux.h"
/*
#define TS_LOG
#define TS_PMT_LOG
#define TS_PAT_LOG
#define TS_READ_STATS // activates read statistics generation
#define TS_HEADER_LOG // prints out the Transport packet header.
*/
/*
* The maximum number of PIDs we are prepared to handle in a single program
* is the number that fits in a single-packet PMT.
*/
#define PKT_SIZE 188
#define BODY_SIZE (188 - 4)
/* more PIDS are needed due "auto-detection". 40 spare media entries */
#define MAX_PIDS ((BODY_SIZE - 1 - 13) / 4) + 40
#define MAX_PMTS ((BODY_SIZE - 1 - 13) / 4) + 10
#define SYNC_BYTE 0x47
#define MIN_SYNCS 3
#define NPKT_PER_READ 96 // 96*188 = 94*192
#define BUF_SIZE (NPKT_PER_READ * (PKT_SIZE + 4))
#define CORRUPT_PES_THRESHOLD 10
#define NULL_PID 0x1fff
#define INVALID_PID ((unsigned int)(-1))
#define INVALID_PROGRAM ((unsigned int)(-1))
#define INVALID_CC ((unsigned int)(-1))
#define PROG_STREAM_MAP 0xBC
#define PRIVATE_STREAM1 0xBD
#define PADDING_STREAM 0xBE
#define PRIVATE_STREAM2 0xBF
#define AUDIO_STREAM_S 0xC0
#define AUDIO_STREAM_E 0xDF
#define VIDEO_STREAM_S 0xE0
#define VIDEO_STREAM_E 0xEF
#define ECM_STREAM 0xF0
#define EMM_STREAM 0xF1
#define DSM_CC_STREAM 0xF2
#define ISO13522_STREAM 0xF3
#define PROG_STREAM_DIR 0xFF
/* descriptors in PMT stream info */
#define DESCRIPTOR_REG_FORMAT 0x05
#define DESCRIPTOR_LANG 0x0a
#define DESCRIPTOR_TELETEXT 0x56
#define DESCRIPTOR_DVBSUB 0x59
#define DESCRIPTOR_AC3 0x6a
#define DESCRIPTOR_EAC3 0x7a
#define DESCRIPTOR_DTS 0x7b
#define DESCRIPTOR_AAC 0x7c
typedef enum
{
ISO_11172_VIDEO = 0x01, /* ISO/IEC 11172 Video */
ISO_13818_VIDEO = 0x02, /* ISO/IEC 13818-2 Video */
ISO_11172_AUDIO = 0x03, /* ISO/IEC 11172 Audio */
ISO_13818_AUDIO = 0x04, /* ISO/IEC 13818-3 Audi */
ISO_13818_PRIVATE = 0x05, /* ISO/IEC 13818-1 private sections */
ISO_13818_PES_PRIVATE = 0x06, /* ISO/IEC 13818-1 PES packets containing private data */
ISO_13522_MHEG = 0x07, /* ISO/IEC 13512 MHEG */
ISO_13818_DSMCC = 0x08, /* ISO/IEC 13818-1 Annex A DSM CC */
ISO_13818_TYPE_A = 0x0a, /* ISO/IEC 13818-6 Multiprotocol encapsulation */
ISO_13818_TYPE_B = 0x0b, /* ISO/IEC 13818-6 DSM-CC U-N Messages */
ISO_13818_TYPE_C = 0x0c, /* ISO/IEC 13818-6 Stream Descriptors */
ISO_13818_TYPE_D = 0x0d, /* ISO/IEC 13818-6 Sections (any type, including private data) */
ISO_13818_AUX = 0x0e, /* ISO/IEC 13818-1 auxiliary */
ISO_13818_PART7_AUDIO = 0x0f, /* ISO/IEC 13818-7 Audio with ADTS transport sytax */
ISO_14496_PART2_VIDEO = 0x10, /* ISO/IEC 14496-2 Visual (MPEG-4) */
ISO_14496_PART3_AUDIO = 0x11, /* ISO/IEC 14496-3 Audio with LATM transport syntax */
ISO_14496_PART10_VIDEO = 0x1b, /* ISO/IEC 14496-10 Video (MPEG-4 part 10/AVC, aka H.264) */
STREAM_VIDEO_MPEG = 0x80,
STREAM_AUDIO_AC3 = 0x81,
STREAM_VIDEO_VC1 = 0xea, /* VC-1 Video */
HDMV_AUDIO_80_PCM = 0x80, /* BluRay PCM */
HDMV_AUDIO_82_DTS = 0x82, /* DTS */
HDMV_AUDIO_83_TRUEHD = 0x83, /* Dolby TrueHD, primary audio */
HDMV_AUDIO_84_EAC3 = 0x84, /* Dolby Digital plus, primary audio */
HDMV_AUDIO_85_DTS_HRA = 0x85, /* DTS-HRA */
HDMV_AUDIO_86_DTS_HD_MA = 0x86, /* DTS-HD Master audio */
HDMV_SPU_BITMAP = 0x90,
HDMV_SPU_INTERACTIVE = 0x91,
HDMV_SPU_TEXT = 0x92,
/* pseudo tags */
STREAM_AUDIO_EAC3 = (DESCRIPTOR_EAC3 << 8),
STREAM_AUDIO_DTS = (DESCRIPTOR_DTS << 8),
} streamType;
#define WRAP_THRESHOLD 270000
#define PTS_AUDIO 0
#define PTS_VIDEO 1
/* bitrate estimation */
#define TBRE_MIN_TIME ( 2 * 90000)
#define TBRE_TIME (480 * 90000)
#define TBRE_MODE_PROBE 0
#define TBRE_MODE_AUDIO_PTS 1
#define TBRE_MODE_AUDIO_PCR 2
#define TBRE_MODE_PCR 3
#define TBRE_MODE_DONE 4
#undef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#undef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
/*
**
** DATA STRUCTURES
**
*/
/*
* Describe a single elementary stream.
*/
typedef struct {
unsigned int pid;
fifo_buffer_t *fifo;
uint32_t type;
int64_t pts;
buf_element_t *buf;
unsigned int counter;
uint16_t descriptor_tag; /* +0x100 for PES stream IDs (no available TS descriptor tag?) */
uint8_t keep; /* used by demux_ts_dynamic_pmt_*() */
int corrupted_pes;
int pes_bytes_left; /* butes left if PES packet size is known */
int input_normpos;
int input_time;
} demux_ts_media;
/* DVBSUB */
#define MAX_SPU_LANGS 32
typedef struct {
spu_dvb_descriptor_t desc;
unsigned int pid;
unsigned int media_index;
} demux_ts_spu_lang;
/* Audio Channels */
#define MAX_AUDIO_TRACKS 32
typedef struct {
unsigned int pid;
unsigned int media_index;
char lang[4];
} demux_ts_audio_track;
typedef struct {
/*
* The first field must be the "base class" for the plugin!
*/
demux_plugin_t demux_plugin;
xine_stream_t *stream;
config_values_t *config;
fifo_buffer_t *audio_fifo;
fifo_buffer_t *video_fifo;
input_plugin_t *input;
unsigned int read_retries;
int status;
int hdmv; /* -1 = unknown, 0 = mpeg-ts, 1 = hdmv/m2ts */
int pkt_size; /* TS packet size */
int pkt_offset; /* TS packet offset */
int rate;
unsigned int media_num;
demux_ts_media media[MAX_PIDS];
/* PAT */
uint32_t last_pat_crc;
uint32_t transport_stream_id;
/* programs */
uint32_t program_number[MAX_PMTS];
uint32_t pmt_pid[MAX_PMTS];
uint8_t *pmt[MAX_PMTS];
uint8_t *pmt_write_ptr[MAX_PMTS];
uint32_t crc32_table[256];
uint32_t last_pmt_crc;
/*
* Stuff to do with the transport header. As well as the video
* and audio PIDs, we keep the index of the corresponding entry
* inthe media[] array.
*/
unsigned int pcr_pid;
unsigned int videoPid;
unsigned int videoMedia;
demux_ts_audio_track audio_tracks[MAX_AUDIO_TRACKS];
int audio_tracks_count;
int64_t last_pts[2];
int send_newpts;
int buf_flag_seek;
unsigned int scrambled_pids[MAX_PIDS];
unsigned int scrambled_npids;
#ifdef TS_READ_STATS
uint32_t rstat[NPKT_PER_READ + 1];
#endif
/* DVBSUB */
unsigned int spu_pid;
unsigned int spu_media;
demux_ts_spu_lang spu_langs[MAX_SPU_LANGS];
int spu_langs_count;
int current_spu_channel;
/* dvb */
xine_event_queue_t *event_queue;
/* For syncronisation */
int32_t packet_number;
/* NEW: var to keep track of number of last read packets */
int32_t npkt_read;
uint8_t buf[BUF_SIZE]; /* == PKT_SIZE * NPKT_PER_READ */
off_t frame_pos; /* current ts packet position in input stream (bytes from beginning) */
/* bitrate estimation */
off_t tbre_bytes, tbre_lastpos;
int64_t tbre_time, tbre_lasttime;
unsigned int tbre_mode, tbre_pid;
} demux_ts_t;
typedef struct {
demux_class_t demux_class;
/* class-wide, global variables here */
xine_t *xine;
config_values_t *config;
} demux_ts_class_t;
static void reset_track_map(fifo_buffer_t *fifo)
{
buf_element_t *buf = fifo->buffer_pool_alloc (fifo);
buf->type = BUF_CONTROL_RESET_TRACK_MAP;
buf->decoder_info[1] = -1;
fifo->put (fifo, buf);
}
/* TJ. dynamic PMT support. The idea is:
First, reuse unchanged pids and add new ones.
Then, comb out those who are no longer referenced.
For example, the Kaffeine dvb frontend preserves original pids but only
sends the currently user selected ones, plus matching generated pat/pmt */
static int demux_ts_dynamic_pmt_find (demux_ts_t *this,
int pid, int type, unsigned int descriptor_tag) {
unsigned int i;
demux_ts_media *m;
for (i = 0; i < this->media_num; i++) {
m = &this->media[i];
if ((m->pid == pid) && ((m->type & BUF_MAJOR_MASK) == type)) {
/* mark this media decriptor for reuse */
m->keep = 1;
return i;
}
}
if (i < MAX_PIDS) {
/* prepare new media descriptor */
#ifdef LOG_DYNAMIC_PMT
char *name = "";
if (type == BUF_VIDEO_BASE) name = "video";
else if (type == BUF_AUDIO_BASE) name = "audio";
else if (type == BUF_SPU_BASE) name = "subtitle";
printf ("demux_ts: new %s pid %d\n", name, pid);
#endif
m = &this->media[i];
if (type == BUF_AUDIO_BASE) {
/* allocate new audio track as well */
if (this->audio_tracks_count >= MAX_AUDIO_TRACKS) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: too many audio PIDs, ignoring pid %d\n", pid);
return -1;
}
m->type = type | this->audio_tracks_count;
this->audio_tracks[this->audio_tracks_count].pid = pid;
this->audio_tracks[this->audio_tracks_count].media_index = i;
this->audio_tracks_count++;
m->fifo = this->stream->audio_fifo;
} else {
m->type = type;
m->fifo = this->stream->video_fifo;
}
m->pid = pid;
if (m->buf) {
m->buf->free_buffer(m->buf);
m->buf = NULL;
}
m->counter = INVALID_CC;
m->corrupted_pes = 1;
m->pts = 0;
m->descriptor_tag = descriptor_tag;
m->keep = 1;
this->media_num++;
return i;
}
/* table full */
return -1;
}
static void demux_ts_dynamic_pmt_clean (demux_ts_t *this) {
int i, count = 0, tracks = 0, spus = 0;
/* densify media table */
for (i = 0; i < this->media_num; i++) {
demux_ts_media *m = &this->media[i];
int type = m->type & BUF_MAJOR_MASK;
int chan = m->type & 0xff;
if (m->keep) {
m->keep = 0;
if (type == BUF_VIDEO_BASE) {
/* adjust single video link */
this->videoMedia = count;
} else if (type == BUF_AUDIO_BASE) {
/* densify audio track table */
this->audio_tracks[chan].media_index = count;
if (chan > tracks) {
m->type = (m->type & ~0xff) | tracks;
this->audio_tracks[tracks] = this->audio_tracks[chan];
}
tracks++;
} else if (type == BUF_SPU_BASE) {
/* spu language table has already been rebuilt from scratch.
Adjust backlinks only */
while ((spus < this->spu_langs_count) && (this->spu_langs[spus].pid == m->pid)) {
this->spu_langs[spus].media_index = count;
spus++;
}
}
if (i > count) {
this->media[count] = *m;
m->buf = NULL;
m->pid = INVALID_PID;
}
count++;
} else {
/* drop this no longer needed media descriptor */
#ifdef LOG_DYNAMIC_PMT
char *name = "";
if (type == BUF_VIDEO_BASE) name = "video";
else if (type == BUF_AUDIO_BASE) name = "audio";
else if (type == BUF_SPU_BASE) name = "subtitle";
printf ("demux_ts: dropped %s pid %d\n", name, m->pid);
#endif
if (m->buf) {
m->buf->free_buffer (m->buf);
m->buf = NULL;
}
m->pid = INVALID_PID;
}
}
if ((tracks < this->audio_tracks_count) && this->audio_fifo) {
/* at least 1 audio track removed, tell audio decoder loop */
reset_track_map(this->audio_fifo);
#ifdef LOG_DYNAMIC_PMT
printf ("demux_ts: new audio track map\n");
#endif
}
#ifdef LOG_DYNAMIC_PMT
printf ("demux_ts: using %d pids, %d audio %d subtitle channels\n", count, tracks, spus);
#endif
/* adjust table sizes */
this->media_num = count;
this->audio_tracks_count = tracks;
/* should really have no effect */
this->spu_langs_count = spus;
}
static void demux_ts_dynamic_pmt_clear (demux_ts_t *this) {
unsigned int i;
for (i = 0; i < this->media_num; i++) {
if (this->media[i].buf) {
this->media[i].buf->free_buffer (this->media[i].buf);
this->media[i].buf = NULL;
}
}
this->media_num = 0;
this->videoPid = INVALID_PID;
this->audio_tracks_count = 0;
this->spu_pid = INVALID_PID;
this->spu_langs_count = 0;
this->spu_media = 0;
this->pcr_pid = INVALID_PID;
this->last_pmt_crc = 0;
}
static void demux_ts_tbre_reset (demux_ts_t *this) {
if (this->tbre_time <= TBRE_TIME) {
this->tbre_pid = INVALID_PID;
this->tbre_mode = TBRE_MODE_PROBE;
}
}
static void demux_ts_tbre_update (demux_ts_t *this, unsigned int mode, int64_t now) {
/* select best available timesource on the fly */
if ((mode < this->tbre_mode) || (now <= 0))
return;
if (mode == this->tbre_mode) {
/* skip discontinuities */
int64_t diff = now - this->tbre_lasttime;
if ((diff < 0 ? -diff : diff) < 220000) {
/* add this step */
this->tbre_bytes += this->frame_pos - this->tbre_lastpos;
this->tbre_time += diff;
/* update bitrate */
if (this->tbre_time > TBRE_MIN_TIME)
this->rate = this->tbre_bytes * 90000 / this->tbre_time;
/* stop analyzing */
if (this->tbre_time > TBRE_TIME)
this->tbre_mode = TBRE_MODE_DONE;
}
} else {
/* upgrade timesource */
this->tbre_mode = mode;
}
/* remember where and when */
this->tbre_lastpos = this->frame_pos;
this->tbre_lasttime = now;
}
static void demux_ts_build_crc32_table(demux_ts_t*this) {
uint32_t i, j, k;
for( i = 0 ; i < 256 ; i++ ) {
k = 0;
for (j = (i << 24) | 0x800000 ; j != 0x80000000 ; j <<= 1) {
k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
}
this->crc32_table[i] = k;
}
}
static uint32_t demux_ts_compute_crc32(demux_ts_t*this, uint8_t *data,
int32_t length, uint32_t crc32) {
int32_t i;
for(i = 0; i < length; i++) {
crc32 = (crc32 << 8) ^ this->crc32_table[(crc32 >> 24) ^ data[i]];
}
return crc32;
}
/* redefine abs as macro to handle 64-bit diffs.
i guess llabs may not be available everywhere */
#define abs(x) ( ((x)<0) ? -(x) : (x) )
static void check_newpts( demux_ts_t *this, int64_t pts, int video )
{
int64_t diff;
#ifdef TS_LOG
printf ("demux_ts: check_newpts %lld, send_newpts %d, buf_flag_seek %d\n",
pts, this->send_newpts, this->buf_flag_seek);
#endif
diff = pts - this->last_pts[video];
if( pts &&
(this->send_newpts || (this->last_pts[video] && abs(diff)>WRAP_THRESHOLD) ) ) {
if (this->buf_flag_seek) {
_x_demux_control_newpts(this->stream, pts, BUF_FLAG_SEEK);
this->buf_flag_seek = 0;
} else {
_x_demux_control_newpts(this->stream, pts, 0);
}
this->send_newpts = 0;
this->last_pts[1-video] = 0;
}
if( pts )
{
/* don't detect a discontinuity only for video respectively audio. It's also a discontinuity
indication when audio and video pts differ to much e. g. when a pts wrap happens.
The original code worked well when the wrap happend like this:
V7 A7 V8 V9 A9 Dv V0 V1 da A1 V2 V3 A3 V4
Legend:
Vn = video packet with timestamp n
An = audio packet with timestamp n
Dv = discontinuity detected on following video packet
Da = discontinuity detected on following audio packet
dv = discontinuity detected on following video packet but ignored
da = discontinuity detected on following audio packet but ignored
But with a certain delay between audio and video packets (e. g. the way DVB-S broadcasts
the packets) the code didn't work:
V7 V8 A7 V9 Dv V0 _A9_ V1 V2 Da _A1_ V3 V4 A3
Packet A9 caused audio to jump forward and A1 caused it to jump backward with inserting
a delay of almoust 26.5 hours!
The new code gives the following sequences for the above examples:
V7 A7 V8 V9 A9 Dv V0 V1 A1 V2 V3 A3 V4
V7 V8 A7 V9 Dv V0 Da A9 Dv V1 V2 A1 V3 V4 A3
After proving this code it should be cleaned up to use just a single variable "last_pts". */
/*
this->last_pts[video] = pts;
*/
this->last_pts[video] = this->last_pts[1-video] = pts;
}
}
/* Send a BUF_SPU_DVB to let xine know of that channel. */
static void demux_send_special_spu_buf( demux_ts_t *this, uint32_t spu_type, int spu_channel )
{
buf_element_t *buf;
buf = this->video_fifo->buffer_pool_alloc( this->video_fifo );
buf->type = spu_type|spu_channel;
buf->content = buf->mem;
buf->size = 0;
this->video_fifo->put( this->video_fifo, buf );
}
/*
* demux_ts_update_spu_channel
*
* Send a BUF_SPU_DVB with BUF_SPECIAL_SPU_DVB_DESCRIPTOR to tell
* the decoder to reset itself on the new channel.
*/
static void demux_ts_update_spu_channel(demux_ts_t *this)
{
buf_element_t *buf;
this->current_spu_channel = this->stream->spu_channel;
buf = this->video_fifo->buffer_pool_alloc(this->video_fifo);
buf->type = BUF_SPU_DVB;
buf->content = buf->mem;
buf->decoder_flags = BUF_FLAG_SPECIAL;
buf->decoder_info[1] = BUF_SPECIAL_SPU_DVB_DESCRIPTOR;
buf->size = 0;
if (this->current_spu_channel >= 0
&& this->current_spu_channel < this->spu_langs_count)
{
demux_ts_spu_lang *lang = &this->spu_langs[this->current_spu_channel];
buf->decoder_info[2] = sizeof(lang->desc);
buf->decoder_info_ptr[2] = &(lang->desc);
buf->type |= this->current_spu_channel;
this->spu_pid = lang->pid;
this->spu_media = lang->media_index;
/* multiple spu langs can share same media descriptor */
this->media[lang->media_index].type =
(this->media[lang->media_index].type & ~0xff) | this->current_spu_channel;
#ifdef TS_LOG
printf("demux_ts: DVBSUB: selecting lang: %s page %ld %ld\n",
lang->desc.lang, lang->desc.comp_page_id, lang->desc.aux_page_id);
#endif
}
else
{
buf->decoder_info_ptr[2] = NULL;
this->spu_pid = INVALID_PID;
#ifdef TS_LOG
printf("demux_ts: DVBSUB: deselecting lang\n");
#endif
}
if ((this->media[this->spu_media].type & BUF_MAJOR_MASK) == BUF_SPU_HDMV) {
buf->type = BUF_SPU_HDMV;
buf->type |= this->current_spu_channel;
}
this->video_fifo->put(this->video_fifo, buf);
}
static void demux_ts_send_buffer(demux_ts_media *m, int flags)
{
if (m->buf) {
m->buf->content = m->buf->mem;
m->buf->type = m->type;
m->buf->decoder_flags |= flags;
m->buf->pts = m->pts;
m->buf->decoder_info[0] = 1;
m->buf->extra_info->input_normpos = m->input_normpos;
m->buf->extra_info->input_time = m->input_time;
m->fifo->put(m->fifo, m->buf);
m->buf = NULL;
#ifdef TS_LOG
printf ("demux_ts: produced buffer, pts=%lld\n", m->pts);
#endif
}
}
static void demux_ts_flush_media(demux_ts_media *m)
{
demux_ts_send_buffer(m, BUF_FLAG_FRAME_END);
}
static void demux_ts_flush(demux_ts_t *this)
{
unsigned int i;
for (i = 0; i < this->media_num; ++i) {
demux_ts_flush_media(&this->media[i]);
this->media[i].corrupted_pes = 1;
}
}
/*
* demux_ts_parse_pat
*
* Parse a program association table (PAT).
* The PAT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* The PAT is assumed to contain a single program definition, though
* we can cope with the stupidity of SPTSs which contain NITs.
*/
static void demux_ts_parse_pat (demux_ts_t*this, unsigned char *original_pkt,
unsigned char *pkt, unsigned int pusi) {
#ifdef TS_PAT_LOG
uint32_t table_id;
uint32_t version_number;
#endif
uint32_t section_syntax_indicator;
int32_t section_length;
uint32_t transport_stream_id;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t crc32;
uint32_t calc_crc32;
unsigned char *program;
unsigned int program_number;
unsigned int pmt_pid;
unsigned int program_count;
/*
* A PAT in a single section should start with a payload unit start
* indicator set.
*/
if (!pusi) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: demux error! PAT without payload unit start indicator\n");
return;
}
/*
* sections start with a pointer. Skip it!
*/
pkt += pkt[4];
if (pkt - original_pkt > PKT_SIZE) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: demux error! PAT with invalid pointer\n");
return;
}
#ifdef TS_PAT_LOG
table_id = (unsigned int)pkt[5] ;
#endif
section_syntax_indicator = (((unsigned int)pkt[6] >> 7) & 1) ;
section_length = (((unsigned int)pkt[6] & 0x03) << 8) | pkt[7];
transport_stream_id = ((uint32_t)pkt[8] << 8) | pkt[9];
#ifdef TS_PAT_LOG
version_number = ((uint32_t)pkt[10] >> 1) & 0x1f;
#endif
current_next_indicator = ((uint32_t)pkt[10] & 0x01);
section_number = (uint32_t)pkt[11];
last_section_number = (uint32_t)pkt[12];
crc32 = (uint32_t)pkt[4+section_length] << 24;
crc32 |= (uint32_t)pkt[5+section_length] << 16;
crc32 |= (uint32_t)pkt[6+section_length] << 8;
crc32 |= (uint32_t)pkt[7+section_length] ;
#ifdef TS_PAT_LOG
printf ("demux_ts: PAT table_id: %.2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
printf (" transport_stream_id: %#.4x\n", transport_stream_id);
printf (" version_number: %d\n", version_number);
printf (" c/n indicator: %d\n", current_next_indicator);
printf (" section_number: %d\n", section_number);
printf (" last_section_number: %d\n", last_section_number);
#endif
if ((section_syntax_indicator != 1) || !(current_next_indicator)) {
return;
}
if (pkt - original_pkt > BODY_SIZE - 1 - 3 - section_length) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: FIXME: (unsupported )PAT spans multiple TS packets\n");
return;
}
if ((section_number != 0) || (last_section_number != 0)) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: FIXME: (unsupported) PAT consists of multiple (%d) sections\n", last_section_number);
return;
}
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32 (this, pkt+5, section_length+3-4,
0xffffffff);
if (crc32 != calc_crc32) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: demux error! PAT with invalid CRC32: packet_crc32: %.8x calc_crc32: %.8x\n",
crc32,calc_crc32);
return;
}
#ifdef TS_PAT_LOG
else {
printf ("demux_ts: PAT CRC32 ok.\n");
}
#endif
if (crc32 == this->last_pat_crc &&
this->transport_stream_id == transport_stream_id) {
lprintf("demux_ts: PAT CRC unchanged\n");
return;
}
if (this->transport_stream_id != transport_stream_id) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: PAT transport_stream_id changed\n");
}
this->last_pat_crc = crc32;
this->transport_stream_id = transport_stream_id;
/*
* Process all programs in the program loop.
*/
program_count = 0;
for (program = pkt + 13;
(program < pkt + 13 + section_length - 9) && (program_count < MAX_PMTS);
program += 4) {
program_number = ((unsigned int)program[0] << 8) | program[1];
pmt_pid = (((unsigned int)program[2] & 0x1f) << 8) | program[3];
/*
* completely skip NIT pids.
*/
if (program_number == 0x0000)
continue;
/*
* Add the program number to the table if we haven't already
* seen it. The order of the program numbers is assumed not
* to change between otherwise identical PATs.
*/
if (this->program_number[program_count] != program_number) {
this->program_number[program_count] = program_number;
this->pmt_pid[program_count] = INVALID_PID;
}
/* force PMT reparsing when pmt_pid changes */
if (this->pmt_pid[program_count] != pmt_pid) {
this->pmt_pid[program_count] = pmt_pid;
demux_ts_dynamic_pmt_clear (this);
if (this->pmt[program_count] != NULL) {
free(this->pmt[program_count]);
this->pmt[program_count] = NULL;
this->pmt_write_ptr[program_count] = NULL;
}
}
#ifdef TS_PAT_LOG
if (this->program_number[program_count] != INVALID_PROGRAM)
printf ("demux_ts: PAT acquired count=%d programNumber=0x%04x "
"pmtPid=0x%04x\n",
program_count,
this->program_number[program_count],
this->pmt_pid[program_count]);
#endif
++program_count;
}
/* Add "end of table" markers. */
if (program_count < MAX_PMTS) {
this->program_number[program_count] = INVALID_PROGRAM;
this->pmt_pid[program_count] = INVALID_PID;
}
}
static int demux_ts_parse_pes_header (xine_t *xine, demux_ts_media *m,
uint8_t *buf, int packet_len) {
unsigned char *p;
uint32_t header_len;
int64_t pts;
uint32_t stream_id;
if (packet_len < 9) {
xprintf (xine, XINE_VERBOSITY_DEBUG,
"demux_ts: too short PES packet header (%d bytes)\n", packet_len);
return 0;
}
p = buf;
/* we should have a PES packet here */
if (p[0] || p[1] || (p[2] != 1)) {
xprintf (xine, XINE_VERBOSITY_DEBUG,
"demux_ts: error %02x %02x %02x (should be 0x000001) \n", p[0], p[1], p[2]);
return 0 ;
}
stream_id = p[3];
header_len = p[8] + 9;
/* sometimes corruption on header_len causes segfault in memcpy below */
if (header_len > packet_len) {
xprintf (xine, XINE_VERBOSITY_DEBUG,
"demux_ts: illegal value for PES_header_data_length (0x%x)\n", header_len - 9);
return 0;
}
#ifdef TS_LOG
printf ("demux_ts: packet stream id: %.2x len: %d (%x)\n",
stream_id, packet_len, packet_len);
#endif
if (p[7] & 0x80) { /* pts avail */
if (header_len < 14) {
return 0;
}
pts = (int64_t)(p[ 9] & 0x0E) << 29 ;
pts |= p[10] << 22 ;
pts |= (p[11] & 0xFE) << 14 ;
pts |= p[12] << 7 ;
pts |= (p[13] & 0xFE) >> 1 ;
} else
pts = 0;
/* code works but not used in xine
if (p[7] & 0x40) {
DTS = (p[14] & 0x0E) << 29 ;
DTS |= p[15] << 22 ;
DTS |= (p[16] & 0xFE) << 14 ;
DTS |= p[17] << 7 ;
DTS |= (p[18] & 0xFE) >> 1 ;
} else
DTS = 0;
*/
m->pts = pts;
m->pes_bytes_left = (int)(p[4] << 8 | p[5]) - header_len + 6;
lprintf("PES packet payload left: %d bytes\n", m->pes_bytes_left);
p += header_len;
packet_len -= header_len;
if (m->descriptor_tag == STREAM_VIDEO_VC1) {
m->type = BUF_VIDEO_VC1;
return header_len;
}
if (m->descriptor_tag == HDMV_SPU_BITMAP) {
m->type |= BUF_SPU_HDMV;
m->buf->decoder_info[2] = m->pes_bytes_left;
return header_len;
} else
if (stream_id == 0xbd || stream_id == 0xfd /* HDMV */) {
int spu_id;
lprintf ("audio buf = %02X %02X %02X %02X %02X %02X %02X %02X\n",
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
/*
* we check the descriptor tag first because some stations
* do not include any of the ac3 header info in their audio tracks
* these "raw" streams may begin with a byte that looks like a stream type.
* For audio streams, m->type already contains the stream no.
*/
if(m->descriptor_tag == HDMV_AUDIO_84_EAC3 ||
m->descriptor_tag == STREAM_AUDIO_EAC3) {
m->type |= BUF_AUDIO_EAC3;
return header_len;
} else if(m->descriptor_tag == STREAM_AUDIO_AC3) { /* ac3 - raw */
m->type |= BUF_AUDIO_A52;
return header_len;
} else if (m->descriptor_tag == HDMV_AUDIO_83_TRUEHD) {
/* TODO: separate AC3 and TrueHD streams ... */
m->type |= BUF_AUDIO_A52;
return header_len;
} else if (m->descriptor_tag == STREAM_AUDIO_DTS ||
m->descriptor_tag == HDMV_AUDIO_82_DTS ||
m->descriptor_tag == HDMV_AUDIO_86_DTS_HD_MA ) {
m->type |= BUF_AUDIO_DTS;
return header_len;
} else if (packet_len < 2) {
return 0;
} else if (m->descriptor_tag == HDMV_AUDIO_80_PCM) {
if (packet_len < 4) {
return 0;
}
m->type |= BUF_AUDIO_LPCM_BE;
m->buf->decoder_flags |= BUF_FLAG_SPECIAL;
m->buf->decoder_info[1] = BUF_SPECIAL_LPCM_CONFIG;
m->buf->decoder_info[2] = (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
m->pes_bytes_left -= 4;
return header_len + 4;
} else if (m->descriptor_tag == ISO_13818_PES_PRIVATE
&& p[0] == 0x20 && p[1] == 0x00) {
/* DVBSUB */
m->type |= BUF_SPU_DVB;
m->buf->decoder_info[2] = m->pes_bytes_left;
return header_len;
} else if (p[0] == 0x0B && p[1] == 0x77) { /* ac3 - syncword */
m->type |= BUF_AUDIO_A52;
return header_len;
} else if ((p[0] & 0xE0) == 0x20) {
spu_id = (p[0] & 0x1f);
m->type = BUF_SPU_DVD + spu_id;
m->pes_bytes_left -= 1;
return header_len + 1;
} else if ((p[0] & 0xF0) == 0x80) {
if (packet_len < 4) {
return 0;
}
m->type |= BUF_AUDIO_A52;
m->pes_bytes_left -= 4;
return header_len + 4;
#if 0
/* commented out: does not set PCM type. Decoder can't handle raw PCM stream without configuration. */
} else if ((p[0]&0xf0) == 0xa0) {
unsigned int pcm_offset;
for (pcm_offset=0; ++pcm_offset < packet_len-1 ; ){
if (p[pcm_offset] == 0x01 && p[pcm_offset+1] == 0x80 ) { /* START */
pcm_offset += 2;
break;
}
}
if (packet_len < pcm_offset) {
return 0;
}
m->type |= BUF_AUDIO_LPCM_BE;
m->pes_bytes_left -= pcm_offset;
return header_len + pcm_offset;
#endif
}
} else if ((stream_id & 0xf0) == 0xe0) {
switch (m->descriptor_tag) {
case ISO_11172_VIDEO:
case ISO_13818_VIDEO:
case STREAM_VIDEO_MPEG:
lprintf ("demux_ts: found MPEG video type.\n");
m->type = BUF_VIDEO_MPEG;
break;
case ISO_14496_PART2_VIDEO:
lprintf ("demux_ts: found MPEG4 video type.\n");
m->type = BUF_VIDEO_MPEG4;
break;
case ISO_14496_PART10_VIDEO:
lprintf ("demux_ts: found H264 video type.\n");
m->type = BUF_VIDEO_H264;
break;
default:
lprintf ("demux_ts: unknown video type: %d, defaulting to MPEG.\n", m->descriptor_tag);
m->type = BUF_VIDEO_MPEG;
break;
}
return header_len;
} else if ((stream_id & 0xe0) == 0xc0) {
switch (m->descriptor_tag) {
case ISO_11172_AUDIO:
case ISO_13818_AUDIO:
lprintf ("demux_ts: found MPEG audio track.\n");
m->type |= BUF_AUDIO_MPEG;
break;
case ISO_13818_PART7_AUDIO:
lprintf ("demux_ts: found AAC audio track.\n");
m->type |= BUF_AUDIO_AAC;
break;
case ISO_14496_PART3_AUDIO:
lprintf ("demux_ts: found AAC LATM audio track.\n");
m->type |= BUF_AUDIO_AAC_LATM;
break;
default:
lprintf ("demux_ts: unknown audio type: %d, defaulting to MPEG.\n", m->descriptor_tag);
m->type |= BUF_AUDIO_MPEG;
break;
}
return header_len;
} else {
#ifdef TS_LOG
printf ("demux_ts: unknown packet, id: %x\n", stream_id);
#endif
}
return 0 ;
}
static void update_extra_info(demux_ts_t *this, demux_ts_media *m)
{
off_t length = this->input->get_length (this->input);
/* cache frame position */
if (length > 0) {
m->input_normpos = (double)this->frame_pos * 65535.0 / length;
}
if (this->rate) {
m->input_time = this->frame_pos * 1000 / this->rate;
}
}
/*
* buffer arriving pes data
*/
static void demux_ts_buffer_pes(demux_ts_t*this, unsigned char *ts,
unsigned int mediaIndex,
unsigned int pus,
unsigned int cc,
unsigned int len) {
demux_ts_media *m = &this->media[mediaIndex];
if (!m->fifo) {
#ifdef TS_LOG
printf ("fifo unavailable (%d)\n", mediaIndex);
#endif
return; /* To avoid segfault if video out or audio out plugin not loaded */
}
/* By checking the CC here, we avoid the need to check for the no-payload
case (i.e. adaptation field only) when it does not get bumped. */
if (m->counter != INVALID_CC) {
if ((m->counter & 0x0f) != cc) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: PID 0x%.4x: unexpected cc %d (expected %d)\n", m->pid, cc, m->counter);
}
}
m->counter = cc;
m->counter++;
if (pus) { /* new PES packet */
demux_ts_flush_media(m);
/* allocate the buffer here, as pes_header needs a valid buf for dvbsubs */
m->buf = m->fifo->buffer_pool_alloc(m->fifo);
int pes_header_len = demux_ts_parse_pes_header(this->stream->xine, m, ts, len);
if (pes_header_len <= 0) {
m->buf->free_buffer(m->buf);
m->buf = NULL;
m->corrupted_pes++;
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: PID 0x%.4x: corrupted pes encountered\n", m->pid);
} else {
m->corrupted_pes = 0;
/* skip PES header */
ts += pes_header_len;
len -= pes_header_len;
update_extra_info(this, m);
/* rate estimation */
if ((this->tbre_pid == INVALID_PID) && (this->audio_fifo == m->fifo))
this->tbre_pid = m->pid;
if (m->pid == this->tbre_pid)
demux_ts_tbre_update (this, TBRE_MODE_AUDIO_PTS, m->pts);
}
}
if (!m->corrupted_pes) {
if ((m->buf->size + len) > m->buf->max_size) {
m->pes_bytes_left -= m->buf->size;
demux_ts_send_buffer(m, 0);
m->buf = m->fifo->buffer_pool_alloc(m->fifo);
}
memcpy(m->buf->mem + m->buf->size, ts, len);
m->buf->size += len;
if (m->pes_bytes_left > 0 && m->buf->size >= m->pes_bytes_left) {
/* PES payload complete */
m->pes_bytes_left -= m->buf->size;
demux_ts_flush_media(m);
/* skip rest data - there shouldn't be any */
m->corrupted_pes = 1;
}
}
}
/* Find the first ISO 639 language descriptor (tag 10) and
* store the 3-char code in dest, nullterminated. If no
* code is found, zero out dest.
**/
static void demux_ts_get_lang_desc(demux_ts_t *this, char *dest,
const unsigned char *data, int length)
{
const unsigned char *d = data;
while (d < (data + length))
{
if (d[0] == DESCRIPTOR_LANG && d[1] >= 4)
{
memcpy(dest, d + 2, 3);
dest[3] = 0;
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: found ISO 639 lang: %s\n", dest);
return;
}
d += 2 + d[1];
}
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: found no ISO 639 lang\n");
memset(dest, 0, 4);
}
/* Find the registration code (tag=5) and return it as a uint32_t
* This should return "AC-3" or 0x41432d33 for AC3/A52 audio tracks.
*/
static void demux_ts_get_reg_desc(demux_ts_t *this, uint32_t *dest,
const unsigned char *data, int length)
{
const unsigned char *d = data;
while (d < (data + length))
{
if (d[0] == DESCRIPTOR_REG_FORMAT && d[1] >= 4)
{
*dest = (d[2] << 24) | (d[3] << 16) | (d[4] << 8) | d[5];
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: found registration format identifier: 0x%.4x\n", *dest);
return;
}
d += 2 + d[1];
}
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: found no format id\n");
*dest = 0;
}
static inline int ts_payloadsize(unsigned char * tsp)
{
if (!(tsp[3] & 0x10))
return 0;
if (tsp[3] & 0x20) {
if (tsp[4] > 183)
return 0;
else
return 183 - tsp[4];
}
return 184;
}
/* check if an apid is in the list of known apids */
static int apid_check(demux_ts_t*this, unsigned int pid) {
int i;
for (i = 0; i < this->audio_tracks_count; i++) {
if (this->audio_tracks[i].pid == pid)
return i;
}
return -1;
}
/*
* NAME demux_ts_parse_pmt
*
* Parse a PMT. The PMT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* In other words, the PMT is assumed to describe a reasonable number of
* video, audio and other streams (with descriptors).
* FIXME: Implement support for multi section PMT.
*/
static void demux_ts_parse_pmt (demux_ts_t *this,
unsigned char *originalPkt,
unsigned char *pkt,
unsigned int pusi,
uint32_t program_count) {
#ifdef TS_PMT_LOG
uint32_t table_id;
uint32_t version_number;
#endif
uint32_t section_syntax_indicator;
uint32_t section_length = 0; /* to calm down gcc */
uint32_t program_number;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t program_info_length;
uint32_t crc32;
uint32_t calc_crc32;
uint32_t coded_length;
unsigned int pid;
unsigned char *stream;
unsigned int i;
int count;
char *ptr = NULL;
unsigned char len;
unsigned int offset=0;
int mi;
/*
* A new section should start with the payload unit start
* indicator set. We allocate some mem (max. allowed for a PM section)
* to copy the complete section into one chunk.
*/
if (pusi) {
pkt+=pkt[4]; /* pointer to start of section */
offset=1;
free(this->pmt[program_count]);
this->pmt[program_count] = (uint8_t *) calloc(4096, sizeof(unsigned char));
this->pmt_write_ptr[program_count] = this->pmt[program_count];
#ifdef TS_PMT_LOG
table_id = pkt[5] ;
#endif
section_syntax_indicator = (pkt[6] >> 7) & 0x01;
section_length = (((uint32_t) pkt[6] << 8) | pkt[7]) & 0x03ff;
program_number = ((uint32_t) pkt[8] << 8) | pkt[9];
#ifdef TS_PMT_LOG
version_number = (pkt[10] >> 1) & 0x1f;
#endif
current_next_indicator = pkt[10] & 0x01;
section_number = pkt[11];
last_section_number = pkt[12];
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT table_id: %2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
printf (" program_number: %#.4x\n", program_number);
printf (" version_number: %d\n", version_number);
printf (" c/n indicator: %d\n", current_next_indicator);
printf (" section_number: %d\n", section_number);
printf (" last_section_number: %d\n", last_section_number);
#endif
if ((section_syntax_indicator != 1) || !current_next_indicator) {
#ifdef TS_PMT_LOG
printf ("ts_demux: section_syntax_indicator != 1 "
"|| !current_next_indicator\n");
#endif
return;
}
if (program_number != this->program_number[program_count]) {
/* several programs can share the same PMT pid */
#ifdef TS_PMT_LOG
printf("Program Number is %i, looking for %i\n",program_number,this->program_number[program_count]);
printf ("ts_demux: waiting for next PMT on this PID...\n");
#endif
return;
}
if ((section_number != 0) || (last_section_number != 0)) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: FIXME (unsupported) PMT consists of multiple (%d) sections\n", last_section_number);
return;
}
}
if (!this->pmt[program_count]) {
/* not the first TS packet of a PMT, or the calloc didn't work */
#ifdef TS_PMT_LOG
printf ("ts_demux: not the first TS packet of a PMT...\n");
#endif
return;
}
if (!pusi){
section_length = (this->pmt[program_count][1] << 8
| this->pmt[program_count][2]) & 0x03ff;
}
count=ts_payloadsize(originalPkt);
ptr = originalPkt+offset+(PKT_SIZE-count);
len = count-offset;
memcpy (this->pmt_write_ptr[program_count], ptr, len);
this->pmt_write_ptr[program_count] +=len;
#ifdef TS_PMT_LOG
printf ("ts_demux: wr_ptr: %p, will be %p when finished\n",
this->pmt_write_ptr[program_count],
this->pmt[program_count] + section_length);
#endif
if (this->pmt_write_ptr[program_count] < this->pmt[program_count]
+ section_length) {
/* didn't get all TS packets for this section yet */
#ifdef TS_PMT_LOG
printf ("ts_demux: didn't get all PMT TS packets yet...\n");
#endif
return;
}
if (!section_length) {
free (this->pmt[program_count]);
this->pmt[program_count] = NULL;
#ifdef TS_PMT_LOG
printf ("ts_demux: eek, zero-length section?\n");
#endif
return;
}
#ifdef TS_PMT_LOG
printf ("ts_demux: have all TS packets for the PMT section\n");
#endif
crc32 = (uint32_t) this->pmt[program_count][section_length+3-4] << 24;
crc32 |= (uint32_t) this->pmt[program_count][section_length+3-3] << 16;
crc32 |= (uint32_t) this->pmt[program_count][section_length+3-2] << 8;
crc32 |= (uint32_t) this->pmt[program_count][section_length+3-1] ;
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32 (this,
this->pmt[program_count],
section_length+3-4, 0xffffffff);
if (crc32 != calc_crc32) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: demux error! PMT with invalid CRC32: packet_crc32: %#.8x calc_crc32: %#.8x\n",
crc32,calc_crc32);
return;
}
else {
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT CRC32 ok.\n");
#endif
if ( crc32==this->last_pmt_crc ) {
#ifdef TS_PMT_LOG
printf("demux_ts: PMT with CRC32=%d already parsed. Skipping.\n", crc32);
#endif
return;
}
else {
#ifdef TS_PMT_LOG
printf("demux_ts: new PMT, parsing...\n");
#endif
this->last_pmt_crc = crc32;
}
}
/*
* Forget the current video, audio and subtitle PIDs; if the PMT has not
* changed, we'll pick them up again when we parse this PMT, while if the
* PMT has changed (e.g. an IPTV streamer that's just changed its source),
* we'll get new PIDs that we should follow.
*/
this->videoPid = INVALID_PID;
this->spu_pid = INVALID_PID;
this->spu_langs_count = 0;
reset_track_map(this->video_fifo);
/*
* ES definitions start here...we are going to learn upto one video
* PID and one audio PID.
*/
program_info_length = ((this->pmt[program_count][10] << 8)
| this->pmt[program_count][11]) & 0x0fff;
/* Program info descriptor is currently just ignored.
* printf ("demux_ts: program_info_desc: ");
* for (i = 0; i < program_info_length; i++)
* printf ("%.2x ", this->pmt[program_count][12+i]);
* printf ("\n");
*/
stream = &this->pmt[program_count][12] + program_info_length;
coded_length = 13 + program_info_length;
if (coded_length > section_length) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux error! PMT with inconsistent progInfo length\n");
return;
}
section_length -= coded_length;
/*
* Extract the elementary streams.
*/
while (section_length > 0) {
unsigned int stream_info_length;
pid = ((stream[1] << 8) | stream[2]) & 0x1fff;
stream_info_length = ((stream[3] << 8) | stream[4]) & 0x0fff;
coded_length = 5 + stream_info_length;
if (coded_length > section_length) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux error! PMT with inconsistent streamInfo length\n");
return;
}
/*
* Squirrel away the first audio and the first video stream. TBD: there
* should really be a way to select the stream of interest.
*/
switch (stream[0]) {
case ISO_11172_VIDEO:
case ISO_13818_VIDEO:
case ISO_14496_PART2_VIDEO:
case ISO_14496_PART10_VIDEO:
case STREAM_VIDEO_VC1:
if (this->videoPid == INVALID_PID) {
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT video pid 0x%.4x type %2.2x\n", pid, stream[0]);
#endif
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_VIDEO_BASE, stream[0]);
if (mi >= 0) {
this->videoMedia = mi;
this->videoPid = pid;
}
}
break;
case ISO_11172_AUDIO:
case ISO_13818_AUDIO:
case ISO_13818_PART7_AUDIO:
case ISO_14496_PART3_AUDIO:
if (this->audio_tracks_count < MAX_AUDIO_TRACKS) {
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_AUDIO_BASE, stream[0]);
if (mi >= 0) {
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT audio pid 0x%.4x type %2.2x\n", pid, stream[0]);
#endif
demux_ts_get_lang_desc (this,
this->audio_tracks[this->media[mi].type & 0xff].lang,
stream + 5, stream_info_length);
}
}
break;
case ISO_13818_PRIVATE:
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT streamtype 13818_PRIVATE, pid: 0x%.4x type %2.2x\n", pid, stream[0]);
for (i = 5; i < coded_length; i++)
printf ("%.2x ", stream[i]);
printf ("\n");
#endif
break;
case ISO_13818_TYPE_C: /* data carousel */
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT streamtype 13818_TYPE_C, pid: 0x%.4x type %2.2x\n", pid, stream[0]);
#endif
break;
case ISO_13818_PES_PRIVATE:
for (i = 5; i < coded_length; i += stream[i+1] + 2) {
if ((stream[i] == DESCRIPTOR_AC3) || (stream[i] == DESCRIPTOR_EAC3) || (stream[i] == DESCRIPTOR_DTS)) {
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_AUDIO_BASE,
stream[i] == DESCRIPTOR_AC3 ? STREAM_AUDIO_AC3 :
stream[i] == DESCRIPTOR_DTS ? STREAM_AUDIO_DTS :
STREAM_AUDIO_EAC3);
if (mi >= 0) {
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT AC3 audio pid 0x%.4x type %2.2x\n", pid, stream[0]);
#endif
demux_ts_get_lang_desc (this,
this->audio_tracks[this->media[mi].type & 0xff].lang,
stream + 5, stream_info_length);
break;
}
}
/* Teletext */
else if (stream[i] == DESCRIPTOR_TELETEXT)
{
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT Teletext, pid: 0x%.4x type %2.2x\n", pid, stream[0]);
for (i = 5; i < coded_length; i++)
printf ("%.2x ", stream[i]);
printf ("\n");
#endif
break;
}
/* DVBSUB */
else if (stream[i] == DESCRIPTOR_DVBSUB)
{
int pos;
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_SPU_BASE, stream[0]);
if (mi < 0) break;
for (pos = i + 2;
pos + 8 <= i + 2 + stream[i + 1]
&& this->spu_langs_count < MAX_SPU_LANGS;
pos += 8)
{
int no = this->spu_langs_count;
demux_ts_spu_lang *lang = &this->spu_langs[no];
this->spu_langs_count++;
memcpy(lang->desc.lang, &stream[pos], 3);
lang->desc.lang[3] = 0;
lang->desc.comp_page_id =
(stream[pos + 4] << 8) | stream[pos + 5];
lang->desc.aux_page_id =
(stream[pos + 6] << 8) | stream[pos + 7];
lang->pid = pid;
lang->media_index = mi;
demux_send_special_spu_buf( this, BUF_SPU_DVB, no );
#ifdef TS_LOG
printf("demux_ts: DVBSUB: pid 0x%.4x: %s page %ld %ld type %2.2x\n",
pid, lang->desc.lang,
lang->desc.comp_page_id,
lang->desc.aux_page_id,
stream[0]);
#endif
}
}
}
break;
case HDMV_SPU_INTERACTIVE:
case HDMV_SPU_TEXT:
if (this->hdmv > 0) {
printf("demux_ts: Skipping unsupported HDMV subtitle stream_type: 0x%.2x pid: 0x%.4x\n",
stream[0], pid);
break;
}
/* fall thru */
case HDMV_SPU_BITMAP:
if (this->hdmv > 0) {
if (pid >= 0x1200 && pid < 0x1300) {
/* HDMV Presentation Graphics / SPU */
if (this->spu_langs_count >= MAX_SPU_LANGS) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: too many SPU tracks! ignoring pid 0x%.4x\n",
pid);
break;
}
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_SPU_BASE, stream[0]);
if (mi < 0) break;
demux_ts_spu_lang *lang = &this->spu_langs[this->spu_langs_count];
memset(lang->desc.lang, 0, sizeof(lang->desc.lang));
/*memcpy(lang->desc.lang, &stream[pos], 3);*/
/*lang->desc.lang[3] = 0;*/
lang->pid = pid;
lang->media_index = mi;
demux_send_special_spu_buf( this, BUF_SPU_HDMV, this->spu_langs_count );
this->spu_langs_count++;
#ifdef TS_PMT_LOG
printf("demux_ts: HDMV subtitle stream_type: 0x%.2x pid: 0x%.4x\n",
stream[0], pid);
#endif
break;
}
}
/* fall thru */
default:
/* This following section handles all the cases where the audio track info is stored in PMT user info with stream id >= 0x80
* We first check that the stream id >= 0x80, because all values below that are invalid if not handled above,
* then we check the registration format identifier to see if it holds "AC-3" (0x41432d33) and
* if is does, we tag this as an audio stream.
* FIXME: This will need expanding if we ever see a DTS or other media format here.
*/
if ((this->audio_tracks_count < MAX_AUDIO_TRACKS) && (stream[0] >= 0x80) ) {
uint32_t format_identifier=0;
demux_ts_get_reg_desc(this, &format_identifier, stream + 5, stream_info_length);
/* If no format identifier, assume A52 */
if (( format_identifier == 0x41432d33) ||
( format_identifier == 0) ||
((format_identifier == 0x48444d56 || this->hdmv>0) && stream[0] == HDMV_AUDIO_80_PCM) /* BluRay PCM */) {
mi = demux_ts_dynamic_pmt_find (this, pid, BUF_AUDIO_BASE, stream[0]);
if (mi >= 0) {
demux_ts_get_lang_desc (this,
this->audio_tracks[this->media[mi].type & 0xff].lang,
stream + 5, stream_info_length);
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT audio pid 0x%.4x type %2.2x\n", pid, stream[0]);
#endif
break;
}
}
}
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT unknown stream_type: 0x%.2x pid: 0x%.4x\n",
stream[0], pid);
for (i = 5; i < coded_length; i++)
printf ("%.2x ", stream[i]);
printf ("\n");
#endif
break;
}
stream += coded_length;
section_length -= coded_length;
}
demux_ts_dynamic_pmt_clean (this);
/*
* Get the current PCR PID.
*/
pid = ((this->pmt[program_count][8] << 8)
| this->pmt[program_count][9]) & 0x1fff;
if (this->pcr_pid != pid) {
#ifdef TS_PMT_LOG
if (this->pcr_pid == INVALID_PID) {
printf ("demux_ts: PMT pcr pid 0x%.4x\n", pid);
} else {
printf ("demux_ts: PMT pcr pid changed 0x%.4x\n", pid);
}
#endif
this->pcr_pid = pid;
}
if ( this->stream->spu_channel>=0 && this->spu_langs_count>0 )
demux_ts_update_spu_channel( this );
demux_ts_tbre_reset (this);
/* Inform UI of channels changes */
xine_event_t ui_event;
ui_event.type = XINE_EVENT_UI_CHANNELS_CHANGED;
ui_event.data_length = 0;
xine_event_send( this->stream, &ui_event );
}
static int sync_correct(demux_ts_t*this, uint8_t *buf, int32_t npkt_read) {
int p = 0;
int n = 0;
int i = 0;
int sync_ok = 0;
int read_length;
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: about to resync!\n");
for (p=0; p < npkt_read; p++) {
for(n=0; n < this->pkt_size; n++) {
sync_ok = 1;
for (i=0; i < MIN(MIN_SYNCS, npkt_read - p); i++) {
if (buf[this->pkt_offset + n + ((i+p) * this->pkt_size)] != SYNC_BYTE) {
sync_ok = 0;
break;
}
}
if (sync_ok) break;
}
if (sync_ok) break;
}
if (sync_ok) {
/* Found sync, fill in */
memmove(&buf[0], &buf[n + p * this->pkt_size],
((this->pkt_size * (npkt_read - p)) - n));
read_length = this->input->read(this->input,
&buf[(this->pkt_size * (npkt_read - p)) - n],
n + p * this->pkt_size);
/* FIXME: when read_length is not as required... we now stop demuxing */
if (read_length != (n + p * this->pkt_size)) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts_tsync_correct: sync found, but read failed\n");
return 0;
}
} else {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts_tsync_correct: sync not found! Stop demuxing\n");
return 0;
}
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: resync successful!\n");
return 1;
}
static int sync_detect(demux_ts_t*this, uint8_t *buf, int32_t npkt_read) {
int i, sync_ok;
sync_ok = 1;
if (this->hdmv) {
this->pkt_size = PKT_SIZE + 4;
this->pkt_offset = 4;
for (i=0; i < MIN(MIN_SYNCS, npkt_read - 3); i++) {
if (buf[this->pkt_offset + i * this->pkt_size] != SYNC_BYTE) {
sync_ok = 0;
break;
}
}
if (sync_ok) {
if (this->hdmv < 0) {
/* fix npkt_read (packet size is 192, not 188) */
this->npkt_read = npkt_read * PKT_SIZE / this->pkt_size;
}
this->hdmv = 1;
return sync_ok;
}
if (this->hdmv > 0)
return sync_correct(this, buf, npkt_read);
/* plain ts */
this->hdmv = 0;
this->pkt_size = PKT_SIZE;
this->pkt_offset = 0;
}
for (i=0; i < MIN(MIN_SYNCS, npkt_read); i++) {
if (buf[i * PKT_SIZE] != SYNC_BYTE) {
sync_ok = 0;
break;
}
}
if (!sync_ok) return sync_correct(this, buf, npkt_read);
return sync_ok;
}
/*
* Main synchronisation routine.
*/
static unsigned char * demux_synchronise(demux_ts_t* this) {
uint8_t *return_pointer = NULL;
int32_t read_length;
this->frame_pos += this->pkt_size;
if ( (this->packet_number) >= this->npkt_read) {
/* NEW: handle read returning less packets than NPKT_PER_READ... */
do {
this->frame_pos = this->input->get_current_pos (this->input);
read_length = this->input->read(this->input, this->buf,
this->pkt_size * NPKT_PER_READ);
if (read_length < 0) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: read returned %d\n", read_length);
if (this->read_retries > 2)
this->status = DEMUX_FINISHED;
this->read_retries++;
return NULL;
}
this->read_retries = 0;
if (read_length % this->pkt_size) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: read returned %d bytes (not a multiple of %d!)\n",
read_length, this->pkt_size);
this->status = DEMUX_FINISHED;
return NULL;
}
this->npkt_read = read_length / this->pkt_size;
#ifdef TS_READ_STATS
this->rstat[this->npkt_read]++;
#endif
/*
* what if this->npkt_read < 5 ? --> ok in sync_detect
*
* NEW: stop demuxing if read returns 0 a few times... (200)
*/
if (this->npkt_read == 0) {
demux_ts_flush(this);
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: read 0 packets\n");
this->status = DEMUX_FINISHED;
return NULL;
}
} while (! read_length);
this->packet_number = 0;
if (!sync_detect(this, &(this->buf)[0], this->npkt_read)) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: sync error.\n");
this->status = DEMUX_FINISHED;
return NULL;
}
}
return_pointer = &(this->buf)[this->pkt_offset + this->pkt_size * this->packet_number];
this->packet_number++;
return return_pointer;
}
static int64_t demux_ts_adaptation_field_parse(uint8_t *data,
uint32_t adaptation_field_length) {
#ifdef TS_LOG
uint32_t discontinuity_indicator=0;
uint32_t random_access_indicator=0;
uint32_t elementary_stream_priority_indicator=0;
#endif
uint32_t PCR_flag=0;
int64_t PCR=-1;
#ifdef TS_LOG
uint32_t EPCR=0;
uint32_t OPCR_flag=0;
uint32_t OPCR=0;
uint32_t EOPCR=0;
uint32_t slicing_point_flag=0;
uint32_t transport_private_data_flag=0;
uint32_t adaptation_field_extension_flag=0;
#endif
uint32_t offset = 1;
#ifdef TS_LOG
discontinuity_indicator = ((data[0] >> 7) & 0x01);
random_access_indicator = ((data[0] >> 6) & 0x01);
elementary_stream_priority_indicator = ((data[0] >> 5) & 0x01);
#endif
PCR_flag = ((data[0] >> 4) & 0x01);
#ifdef TS_LOG
OPCR_flag = ((data[0] >> 3) & 0x01);
slicing_point_flag = ((data[0] >> 2) & 0x01);
transport_private_data_flag = ((data[0] >> 1) & 0x01);
adaptation_field_extension_flag = (data[0] & 0x01);
#endif
#ifdef TS_LOG
printf ("demux_ts: ADAPTATION FIELD length: %d (%x)\n",
adaptation_field_length, adaptation_field_length);
if(discontinuity_indicator) {
printf (" Discontinuity indicator: %d\n",
discontinuity_indicator);
}
if(random_access_indicator) {
printf (" Random_access indicator: %d\n",
random_access_indicator);
}
if(elementary_stream_priority_indicator) {
printf (" Elementary_stream_priority_indicator: %d\n",
elementary_stream_priority_indicator);
}
#endif
if(PCR_flag) {
if (adaptation_field_length < offset + 6)
return -1;
PCR = (((int64_t) data[offset]) & 0xFF) << 25;
PCR += (int64_t) ((data[offset+1] & 0xFF) << 17);
PCR += (int64_t) ((data[offset+2] & 0xFF) << 9);
PCR += (int64_t) ((data[offset+3] & 0xFF) << 1);
PCR += (int64_t) ((data[offset+4] & 0x80) >> 7);
#ifdef TS_LOG
EPCR = ((data[offset+4] & 0x1) << 8) | data[offset+5];
printf ("demux_ts: PCR: %lld, EPCR: %u\n",
PCR, EPCR);
#endif
offset+=6;
}
#ifdef TS_LOG
if(OPCR_flag) {
if (adaptation_field_length < offset + 6)
return PCR;
OPCR = data[offset] << 25;
OPCR |= data[offset+1] << 17;
OPCR |= data[offset+2] << 9;
OPCR |= data[offset+3] << 1;
OPCR |= (data[offset+4] >> 7) & 0x01;
EOPCR = ((data[offset+4] & 0x1) << 8) | data[offset+5];
printf ("demux_ts: OPCR: %u, EOPCR: %u\n",
OPCR,EOPCR);
offset+=6;
}
if(slicing_point_flag) {
printf ("demux_ts: slicing_point_flag: %d\n",
slicing_point_flag);
}
if(transport_private_data_flag) {
printf ("demux_ts: transport_private_data_flag: %d\n",
transport_private_data_flag);
}
if(adaptation_field_extension_flag) {
printf ("demux_ts: adaptation_field_extension_flag: %d\n",
adaptation_field_extension_flag);
}
#endif /* TS_LOG */
return PCR;
}
/* transport stream packet layer */
static void demux_ts_parse_packet (demux_ts_t*this) {
unsigned char *originalPkt;
unsigned int sync_byte;
unsigned int transport_error_indicator;
unsigned int payload_unit_start_indicator;
#ifdef TS_HEADER_LOG
unsigned int transport_priority;
#endif
unsigned int pid;
unsigned int transport_scrambling_control;
unsigned int adaptation_field_control;
unsigned int continuity_counter;
unsigned int data_offset;
unsigned int data_len;
uint32_t program_count;
int i;
/* get next synchronised packet, or NULL */
originalPkt = demux_synchronise(this);
if (originalPkt == NULL)
return;
sync_byte = originalPkt[0];
transport_error_indicator = (originalPkt[1] >> 7) & 0x01;
payload_unit_start_indicator = (originalPkt[1] >> 6) & 0x01;
#ifdef TS_HEADER_LOG
transport_priority = (originalPkt[1] >> 5) & 0x01;
#endif
pid = ((originalPkt[1] << 8) |
originalPkt[2]) & 0x1fff;
transport_scrambling_control = (originalPkt[3] >> 6) & 0x03;
adaptation_field_control = (originalPkt[3] >> 4) & 0x03;
continuity_counter = originalPkt[3] & 0x0f;
#ifdef TS_HEADER_LOG
printf("demux_ts:ts_header:sync_byte=0x%.2x\n",sync_byte);
printf("demux_ts:ts_header:transport_error_indicator=%d\n", transport_error_indicator);
printf("demux_ts:ts_header:payload_unit_start_indicator=%d\n", payload_unit_start_indicator);
printf("demux_ts:ts_header:transport_priority=%d\n", transport_priority);
printf("demux_ts:ts_header:pid=0x%.4x\n", pid);
printf("demux_ts:ts_header:transport_scrambling_control=0x%.1x\n", transport_scrambling_control);
printf("demux_ts:ts_header:adaptation_field_control=0x%.1x\n", adaptation_field_control);
printf("demux_ts:ts_header:continuity_counter=0x%.1x\n", continuity_counter);
#endif
/*
* Discard packets that are obviously bad.
*/
if (sync_byte != SYNC_BYTE) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux error! invalid ts sync byte %.2x\n", sync_byte);
return;
}
if (transport_error_indicator) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux error! transport error\n");
return;
}
if (pid == 0x1ffb) {
/* printf ("demux_ts: PSIP table. Program Guide etc....not supported yet. PID = 0x1ffb\n"); */
return;
}
if (transport_scrambling_control) {
if (this->videoPid == pid) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: selected videoPid is scrambled; skipping...\n");
}
for (i=0; i < this->scrambled_npids; i++) {
if (this->scrambled_pids[i] == pid) return;
}
if (this->scrambled_npids < MAX_PIDS) {
this->scrambled_pids[this->scrambled_npids] = pid;
this->scrambled_npids++;
}
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_ts: PID 0x%.4x is scrambled!\n", pid);
return;
}
data_offset = 4;
if( adaptation_field_control & 0x2 ){
uint32_t adaptation_field_length = originalPkt[4];
if (adaptation_field_length > 0) {
int64_t pcr = demux_ts_adaptation_field_parse (originalPkt+5, adaptation_field_length);
if (pid == this->pcr_pid)
demux_ts_tbre_update (this, TBRE_MODE_PCR, pcr);
else if (pid == this->tbre_pid)
demux_ts_tbre_update (this, TBRE_MODE_AUDIO_PCR, pcr);
}
/*
* Skip adaptation header.
*/
data_offset += adaptation_field_length + 1;
}
if (! (adaptation_field_control & 0x1)) {
return;
}
/* PAT */
if (pid == 0) {
demux_ts_parse_pat(this, originalPkt, originalPkt+data_offset-4,
payload_unit_start_indicator);
return;
}
/* PMT */
program_count=0;
while ((this->program_number[program_count] != INVALID_PROGRAM) &&
(program_count < MAX_PMTS)) {
if (pid == this->pmt_pid[program_count]) {
#ifdef TS_LOG
printf ("demux_ts: PMT prog: 0x%.4x pid: 0x%.4x\n",
this->program_number[program_count],
this->pmt_pid[program_count]);
#endif
demux_ts_parse_pmt (this, originalPkt, originalPkt+data_offset-4,
payload_unit_start_indicator,
program_count);
return;
}
program_count++;
}
data_len = PKT_SIZE - data_offset;
if (data_len > PKT_SIZE) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_ts: demux error! invalid payload size %d\n", data_len);
} else {
/*
* Do the demuxing in descending order of packet frequency!
*/
int index;
if (pid == this->videoPid) {
#ifdef TS_LOG
printf ("demux_ts: Video pid: 0x%.4x\n", pid);
#endif
check_newpts(this, this->media[this->videoMedia].pts, PTS_VIDEO);
demux_ts_buffer_pes (this, originalPkt+data_offset, this->videoMedia,
payload_unit_start_indicator, continuity_counter,
data_len);
return;
}
else if ((index = apid_check(this, pid)) > -1) {
#ifdef TS_LOG
printf ("demux_ts: Audio pid: 0x%.4x\n", pid);
#endif
check_newpts(this, this->media[this->audio_tracks[index].media_index].pts, PTS_AUDIO);
demux_ts_buffer_pes (this, originalPkt+data_offset,
this->audio_tracks[index].media_index,
payload_unit_start_indicator, continuity_counter,
data_len);
return;
}
else if (pid == NULL_PID) {
#ifdef TS_LOG
printf ("demux_ts: Null Packet\n");
#endif
return;
}
/* DVBSUB */
else if (pid == this->spu_pid) {
#ifdef TS_LOG
printf ("demux_ts: SPU pid: 0x%.4x\n", pid);
#endif
demux_ts_buffer_pes (this, originalPkt+data_offset, this->spu_media,
payload_unit_start_indicator, continuity_counter,
data_len);
return;
}
}
}
/*
* check for pids change events
*/
static void demux_ts_event_handler (demux_ts_t *this) {
xine_event_t *event;
while ((event = xine_event_get (this->event_queue))) {
switch (event->type) {
case XINE_EVENT_END_OF_CLIP:
/* flush all streams */
demux_ts_flush(this);
/* fall thru */
case XINE_EVENT_PIDS_CHANGE:
demux_ts_dynamic_pmt_clear(this);
this->send_newpts = 1;
_x_demux_control_start (this->stream);
break;
}
xine_event_free (event);
}
}
/*
* send a piece of data down the fifos
*/
static int demux_ts_send_chunk (demux_plugin_t *this_gen) {
demux_ts_t*this = (demux_ts_t*)this_gen;
demux_ts_event_handler (this);
demux_ts_parse_packet(this);
/* DVBSUB: check if channel has changed. Dunno if I should, or
* even could, lock the xine object. */
if (this->stream->spu_channel != this->current_spu_channel) {
demux_ts_update_spu_channel(this);
}
return this->status;
}
static void demux_ts_dispose (demux_plugin_t *this_gen) {
int i;
demux_ts_t*this = (demux_ts_t*)this_gen;
for (i=0; i < MAX_PMTS; i++) {
if (this->pmt[i] != NULL) {
free(this->pmt[i]);
this->pmt[i] = NULL;
}
}
for (i=0; i < MAX_PIDS; i++) {
if (this->media[i].buf != NULL) {
this->media[i].buf->free_buffer(this->media[i].buf);
this->media[i].buf = NULL;
}
}
xine_event_dispose_queue (this->event_queue);
free(this_gen);
}
static int demux_ts_get_status(demux_plugin_t *this_gen) {
demux_ts_t*this = (demux_ts_t*)this_gen;
return this->status;
}
static void demux_ts_send_headers (demux_plugin_t *this_gen) {
demux_ts_t *this = (demux_ts_t *) this_gen;
this->video_fifo = this->stream->video_fifo;
this->audio_fifo = this->stream->audio_fifo;
this->status = DEMUX_OK;
/*
* send start buffers
*/
this->videoPid = INVALID_PID;
this->pcr_pid = INVALID_PID;
this->audio_tracks_count = 0;
this->media_num= 0;
this->last_pmt_crc = 0;
_x_demux_control_start (this->stream);
this->input->seek (this->input, 0, SEEK_SET);
this->send_newpts = 1;
demux_ts_build_crc32_table (this);
this->status = DEMUX_OK ;
this->scrambled_npids = 0;
/* DVBSUB */
this->spu_pid = INVALID_PID;
this->spu_langs_count = 0;
this->current_spu_channel = -1;
/* FIXME ? */
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 1);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 1);
}
static int demux_ts_seek (demux_plugin_t *this_gen,
off_t start_pos, int start_time, int playing) {
demux_ts_t *this = (demux_ts_t *) this_gen;
int i;
start_pos = (off_t) ( (double) start_pos / 65535 *
this->input->get_length (this->input) );
if (this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) {
if ((!start_pos) && (start_time)) {
if (this->input->seek_time) {
this->input->seek_time(this->input, start_time, SEEK_SET);
} else {
start_pos = (int64_t)start_time * this->rate / 1000;
this->input->seek (this->input, start_pos, SEEK_SET);
}
} else {
this->input->seek (this->input, start_pos, SEEK_SET);
}
}
this->send_newpts = 1;
for (i=0; i<MAX_PIDS; i++) {
demux_ts_media *m = &this->media[i];
if (m->buf != NULL)
m->buf->free_buffer(m->buf);
m->buf = NULL;
m->counter = INVALID_CC;
m->corrupted_pes = 1;
m->pts = 0;
}
if( !playing ) {
this->status = DEMUX_OK;
this->buf_flag_seek = 0;
} else {
this->buf_flag_seek = 1;
_x_demux_flush_engine(this->stream);
}
demux_ts_tbre_reset (this);
return this->status;
}
static int demux_ts_get_stream_length (demux_plugin_t *this_gen) {
demux_ts_t*this = (demux_ts_t*)this_gen;
if (this->rate)
return (int)((int64_t) this->input->get_length (this->input)
* 1000 / this->rate);
else
return 0;
}
static uint32_t demux_ts_get_capabilities(demux_plugin_t *this_gen)
{
return DEMUX_CAP_AUDIOLANG | DEMUX_CAP_SPULANG;
}
static int demux_ts_get_optional_data(demux_plugin_t *this_gen,
void *data, int data_type)
{
demux_ts_t *this = (demux_ts_t *) this_gen;
char *str = data;
int channel = *((int *)data);
/* be a bit paranoid */
if (this == NULL || this->stream == NULL)
return DEMUX_OPTIONAL_UNSUPPORTED;
switch (data_type)
{
case DEMUX_OPTIONAL_DATA_AUDIOLANG:
if ((channel >= 0) && (channel < this->audio_tracks_count)) {
if (this->audio_tracks[channel].lang[0]) {
strcpy(str, this->audio_tracks[channel].lang);
} else {
/* input plugin may know the language */
if (this->input->get_capabilities(this->input) & INPUT_CAP_AUDIOLANG)
return DEMUX_OPTIONAL_UNSUPPORTED;
sprintf(str, "%3i", channel);
}
return DEMUX_OPTIONAL_SUCCESS;
}
else {
strcpy(str, "none");
}
return DEMUX_OPTIONAL_UNSUPPORTED;
case DEMUX_OPTIONAL_DATA_SPULANG:
if (channel>=0 && channel<this->spu_langs_count) {
if (this->spu_langs[channel].desc.lang[0]) {
strcpy(str, this->spu_langs[channel].desc.lang);
} else {
/* input plugin may know the language */
if (this->input->get_capabilities(this->input) & INPUT_CAP_SPULANG)
return DEMUX_OPTIONAL_UNSUPPORTED;
sprintf(str, "%3i", channel);
}
return DEMUX_OPTIONAL_SUCCESS;
} else {
strcpy(str, "none");
}
return DEMUX_OPTIONAL_UNSUPPORTED;
default:
return DEMUX_OPTIONAL_UNSUPPORTED;
}
}
static int detect_ts(uint8_t *buf, size_t len, int ts_size)
{
int i, j;
int try_again, ts_detected = 0;
size_t packs = len / ts_size - 2;
for (i = 0; i < ts_size; i++) {
try_again = 0;
if (buf[i] == SYNC_BYTE) {
for (j = 1; j < packs; j++) {
if (buf[i + j*ts_size] != SYNC_BYTE) {
try_again = 1;
break;
}
}
if (try_again == 0) {
#ifdef TS_LOG
printf ("demux_ts: found 0x47 pattern at offset %d\n", i);
#endif
ts_detected = 1;
}
}
}
return ts_detected;
}
static demux_plugin_t *open_plugin (demux_class_t *class_gen,
xine_stream_t *stream,
input_plugin_t *input) {
demux_ts_t *this;
int i;
int hdmv = -1;
int size;
switch (stream->content_detection_method) {
case METHOD_BY_CONTENT: {
uint8_t buf[2069];
size = _x_demux_read_header(input, buf, sizeof(buf));
if (size < PKT_SIZE)
return NULL;
if (detect_ts(buf, sizeof(buf), PKT_SIZE))
hdmv = 0;
else if (size >= PKT_SIZE + 4 && detect_ts(buf, sizeof(buf), PKT_SIZE+4))
hdmv = 1;
else
return NULL;
}
break;
case METHOD_BY_EXTENSION: {
const char *const mrl = input->get_mrl (input);
if (_x_demux_check_extension (mrl, "m2ts mts"))
hdmv = 1;
else
hdmv = 0;
/* check extension */
const char *const extensions = class_gen->get_extensions (class_gen);
if (_x_demux_check_extension (mrl, extensions))
break;
/* accept dvb streams */
/*
* Also handle the special dvbs,dvbt and dvbc mrl formats:
* the content is exactly the same but the input plugin
* uses a different tuning algorithm [Pragma]
*/
if (!strncasecmp (mrl, "dvb://", 6))
break;
if (!strncasecmp (mrl, "dvbs://", 7))
break;
if (!strncasecmp (mrl, "dvbc://", 7))
break;
if (!strncasecmp (mrl, "dvbt://", 7))
break;
return NULL;
}
case METHOD_EXPLICIT:
break;
default:
return NULL;
}
/*
* if we reach this point, the input has been accepted.
*/
this = calloc(1, sizeof(*this));
this->stream = stream;
this->input = input;
this->demux_plugin.send_headers = demux_ts_send_headers;
this->demux_plugin.send_chunk = demux_ts_send_chunk;
this->demux_plugin.seek = demux_ts_seek;
this->demux_plugin.dispose = demux_ts_dispose;
this->demux_plugin.get_status = demux_ts_get_status;
this->demux_plugin.get_stream_length = demux_ts_get_stream_length;
this->demux_plugin.get_capabilities = demux_ts_get_capabilities;
this->demux_plugin.get_optional_data = demux_ts_get_optional_data;
this->demux_plugin.demux_class = class_gen;
/*
* Initialise our specialised data.
*/
this->last_pat_crc = 0;
this->transport_stream_id = -1;
for (i = 0; i < MAX_PIDS; i++) {
this->media[i].pid = INVALID_PID;
this->media[i].buf = NULL;
}
for (i = 0; i < MAX_PMTS; i++) {
this->program_number[i] = INVALID_PROGRAM;
this->pmt_pid[i] = INVALID_PID;
this->pmt[i] = NULL;
this->pmt_write_ptr[i] = NULL;
}
this->scrambled_npids = 0;
this->videoPid = INVALID_PID;
this->pcr_pid = INVALID_PID;
this->audio_tracks_count = 0;
this->last_pmt_crc = 0;
this->rate = 1000000; /* byte/sec */
this->tbre_pid = INVALID_PID;
this->status = DEMUX_FINISHED;
/* DVBSUB */
this->spu_pid = INVALID_PID;
this->spu_langs_count = 0;
this->current_spu_channel = -1;
/* dvb */
this->event_queue = xine_event_new_queue (this->stream);
/* HDMV */
this->hdmv = hdmv;
this->pkt_offset = (hdmv > 0) ? 4 : 0;
this->pkt_size = PKT_SIZE + this->pkt_offset;
return &this->demux_plugin;
}
/*
* ts demuxer class
*/
static const char *get_description (demux_class_t *this_gen) {
return "MPEG Transport Stream demuxer";
}
static const char *get_identifier (demux_class_t *this_gen) {
return "MPEG_TS";
}
static const char *get_extensions (demux_class_t *this_gen) {
return "ts m2t trp m2ts mts";
}
static const char *get_mimetypes (demux_class_t *this_gen) {
return "video/mp2t: m2t: MPEG2 transport stream;";
}
static void class_dispose (demux_class_t *this_gen) {
demux_ts_class_t *this = (demux_ts_class_t *) this_gen;
free (this);
}
static void *init_class (xine_t *xine, void *data) {
demux_ts_class_t *this;
this = calloc(1, sizeof(demux_ts_class_t));
this->config = xine->config;
this->xine = xine;
this->demux_class.open_plugin = open_plugin;
this->demux_class.get_description = get_description;
this->demux_class.get_identifier = get_identifier;
this->demux_class.get_mimetypes = get_mimetypes;
this->demux_class.get_extensions = get_extensions;
this->demux_class.dispose = class_dispose;
return this;
}
/*
* exported plugin catalog entry
*/
static const demuxer_info_t demux_info_ts = {
10 /* priority */
};
const plugin_info_t xine_plugin_info[] EXPORTED = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_DEMUX, 26, "mpeg-ts", XINE_VERSION_CODE, &demux_info_ts, init_class },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};