[6ec3fb]: src / demuxers / demux_asf.c  Maximize  Restore  History

Download this file

2218 lines (1789 with data), 66.9 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
/*
* 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 asf streams
*
* based on ffmpeg's
* ASF compatible encoder and decoder.
* Copyright (c) 2000, 2001 Gerard Lantau.
*
* GUID list from avifile
* some other ideas from MPlayer
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#define LOG_MODULE "demux_asf"
#define LOG_VERBOSE
/*
#define LOG
*/
#include "xine_internal.h"
#include "demux.h"
#include "xineutils.h"
#include "bswap.h"
#include "asfheader.h"
#include "xmlparser.h"
#define CODEC_TYPE_AUDIO 0
#define CODEC_TYPE_VIDEO 1
#define CODEC_TYPE_CONTROL 2
#define MAX_NUM_STREAMS 23
#define DEFRAG_BUFSIZE 65536
#define WRAP_THRESHOLD 20*90000
#define MAX_FRAME_DUR 90000
#define PTS_AUDIO 0
#define PTS_VIDEO 1
#define ASF_MODE_NORMAL 0
#define ASF_MODE_ASX_REF 1
#define ASF_MODE_HTTP_REF 2
#define ASF_MODE_ASF_REF 3
#define ASF_MODE_ENCRYPTED_CONTENT 4
#define ASF_MODE_NO_CONTENT 5
typedef struct {
int seq;
int frag_offset;
int64_t timestamp;
int ts_per_kbyte;
int defrag;
uint32_t buf_type;
int stream_id;
fifo_buffer_t *fifo;
uint8_t *buffer;
int skip;
int resync;
int first_seq;
int payload_size;
/* palette handling */
int palette_count;
palette_entry_t palette[256];
} asf_demux_stream_t;
typedef struct demux_asf_s {
demux_plugin_t demux_plugin;
xine_stream_t *stream;
fifo_buffer_t *audio_fifo;
fifo_buffer_t *video_fifo;
input_plugin_t *input;
int64_t keyframe_ts;
int keyframe_found;
int seqno;
uint32_t packet_size;
uint8_t packet_len_flags;
uint32_t data_size;
uint64_t packet_count;
asf_demux_stream_t streams[MAX_NUM_STREAMS];
int video_stream;
int audio_stream;
int64_t length;
uint32_t rate;
/* packet filling */
int packet_size_left;
/* discontinuity detection */
int64_t last_pts[2];
int send_newpts;
/* only for reading */
uint32_t packet_padsize;
int nb_frames;
uint8_t frame_flag;
uint8_t packet_prop_flags;
int frame;
int status;
/* byte reordering from audio streams */
int reorder_h;
int reorder_w;
int reorder_b;
int buf_flag_seek;
/* first packet position */
int64_t first_packet_pos;
int mode;
/* for fewer error messages */
GUID last_unknown_guid;
asf_header_t *asf_header;
} demux_asf_t ;
typedef struct {
demux_class_t demux_class;
/* class-wide, global variables here */
xine_t *xine;
config_values_t *config;
} demux_asf_class_t;
static uint8_t get_byte (demux_asf_t *this) {
uint8_t buf;
int i;
i = this->input->read (this->input, &buf, 1);
/* printf ("%02x ", buf); */
if (i != 1) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: end of data\n");
this->status = DEMUX_FINISHED;
}
return buf;
}
static uint16_t get_le16 (demux_asf_t *this) {
uint8_t buf[2];
int i;
i = this->input->read (this->input, buf, 2);
/* printf (" [%02x %02x] ", buf[0], buf[1]); */
if (i != 2) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: end of data\n");
this->status = DEMUX_FINISHED;
}
return _X_LE_16(buf);
}
static uint32_t get_le32 (demux_asf_t *this) {
uint8_t buf[4];
int i;
i = this->input->read (this->input, buf, 4);
/* printf ("%02x %02x %02x %02x ", buf[0], buf[1], buf[2], buf[3]); */
if (i != 4) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: end of data\n");
this->status = DEMUX_FINISHED;
}
return _X_LE_32(buf);
}
static uint64_t get_le64 (demux_asf_t *this) {
uint8_t buf[8];
int i;
i = this->input->read (this->input, buf, 8);
if (i != 8) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: end of data\n");
this->status = DEMUX_FINISHED;
}
return _X_LE_64(buf);
}
static int get_guid_id (demux_asf_t *this, GUID *g) {
int i;
for (i = 1; i < GUID_END; i++) {
if (!memcmp(g, &guids[i].guid, sizeof(GUID))) {
lprintf ("GUID: %s\n", guids[i].name);
return i;
}
}
if (!memcmp(g, &this->last_unknown_guid, sizeof(GUID))) return GUID_ERROR;
memcpy(&this->last_unknown_guid, g, sizeof(GUID));
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: unknown GUID: 0x%" PRIx32 ", 0x%" PRIx16 ", 0x%" PRIx16 ", "
"{ 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 ", 0x%" PRIx8 " }\n",
g->Data1, g->Data2, g->Data3,
g->Data4[0], g->Data4[1], g->Data4[2], g->Data4[3], g->Data4[4], g->Data4[5], g->Data4[6], g->Data4[7]);
return GUID_ERROR;
}
static int get_guid (demux_asf_t *this) {
int i;
GUID g;
g.Data1 = get_le32(this);
g.Data2 = get_le16(this);
g.Data3 = get_le16(this);
for(i = 0; i < 8; i++) {
g.Data4[i] = get_byte(this);
}
return get_guid_id(this, &g);
}
#if 0
static void get_str16_nolen(demux_asf_t *this, int len,
char *buf, int buf_size) {
int c;
char *q;
q = buf;
while (len > 0) {
c = get_le16(this);
if ((q - buf) < buf_size - 1)
*q++ = c;
len-=2;
}
*q = '\0';
}
#endif
static void asf_send_audio_header (demux_asf_t *this, int stream) {
buf_element_t *buf;
asf_stream_t *asf_stream = this->asf_header->streams[stream];
xine_waveformatex *wavex = (xine_waveformatex *)asf_stream->private_data;
if (!this->audio_fifo)
return;
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
if (asf_stream->private_data_length > buf->max_size) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_asf: private decoder data length (%d) is greater than fifo buffer length (%d)\n",
asf_stream->private_data_length, buf->max_size);
buf->free_buffer(buf);
this->status = DEMUX_FINISHED;
return;
}
memcpy (buf->content, wavex, asf_stream->private_data_length);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_FOURCC, wavex->wFormatTag);
lprintf ("wavex header is %d bytes long\n", asf_stream->private_data_length);
buf->size = asf_stream->private_data_length;
buf->type = this->streams[stream].buf_type;
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAME_END;
buf->decoder_info[1] = wavex->nSamplesPerSec;
buf->decoder_info[2] = wavex->wBitsPerSample;
buf->decoder_info[3] = wavex->nChannels;
this->audio_fifo->put (this->audio_fifo, buf);
}
#if 0
static unsigned long str2ulong(unsigned char *str) {
return ( str[0] | (str[1]<<8) | (str[2]<<16) | (str[3]<<24) );
}
#endif
static void asf_send_video_header (demux_asf_t *this, int stream) {
buf_element_t *buf;
asf_demux_stream_t *demux_stream = &this->streams[stream];
asf_stream_t *asf_stream = this->asf_header->streams[stream];
xine_bmiheader *bih = (xine_bmiheader *)(asf_stream->private_data + 11);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_FOURCC, bih->biCompression);
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
if ((asf_stream->private_data_length-11) > buf->max_size) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_asf: private decoder data length (%d) is greater than fifo buffer length (%d)\n",
asf_stream->private_data_length-10, buf->max_size);
buf->free_buffer(buf);
this->status = DEMUX_FINISHED;
return;
}
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAMERATE|
BUF_FLAG_FRAME_END;
buf->decoder_info[0] = 0;
if (this->asf_header->aspect_ratios[stream].x && this->asf_header->aspect_ratios[stream].y)
{
buf->decoder_flags |= BUF_FLAG_ASPECT;
buf->decoder_info[1] = bih->biWidth * this->asf_header->aspect_ratios[stream].x;
buf->decoder_info[2] = bih->biHeight * this->asf_header->aspect_ratios[stream].y;
}
buf->size = asf_stream->private_data_length - 11;
memcpy (buf->content, bih, buf->size);
buf->type = this->streams[stream].buf_type;
this->video_fifo->put (this->video_fifo, buf);
/* send off the palette, if there is one */
if (demux_stream->palette_count) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_asf: stream %d, palette : %d entries\n", stream, demux_stream->palette_count);
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
buf->decoder_flags = BUF_FLAG_SPECIAL|BUF_FLAG_HEADER;
buf->decoder_info[1] = BUF_SPECIAL_PALETTE;
buf->decoder_info[2] = demux_stream->palette_count;
buf->decoder_info_ptr[2] = &demux_stream->palette;
buf->size = 0;
buf->type = this->streams[stream].buf_type;
this->video_fifo->put (this->video_fifo, buf);
}
}
static int asf_read_header (demux_asf_t *this) {
int i;
uint64_t asf_header_len;
char *asf_header_buffer = NULL;
asf_header_len = get_le64(this);
if (asf_header_len > 4 * 1024 * 1024)
{
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: asf_read_header: overly-large header? (%"PRIu64" bytes)\n",
asf_header_len);
return 0;
}
asf_header_buffer = malloc (asf_header_len);
if (this->input->read (this->input, asf_header_buffer, asf_header_len) != asf_header_len)
{
free (asf_header_buffer);
return 0;
}
/* delete previous header */
if (this->asf_header) {
asf_header_delete(this->asf_header);
}
/* the header starts with :
* byte 0-15: header guid
* byte 16-23: header length
*/
this->asf_header = asf_header_new(asf_header_buffer, asf_header_len);
if (!this->asf_header)
{
free (asf_header_buffer);
return 0;
}
free (asf_header_buffer);
lprintf("asf header parsing ok\n");
this->packet_size = this->asf_header->file->packet_size;
this->packet_count = this->asf_header->file->data_packet_count;
/* compute stream duration */
this->length = (this->asf_header->file->send_duration -
this->asf_header->file->preroll) / 10000;
if (this->length < 0)
this->length = 0;
/* compute average byterate (needed for seeking) */
if (this->asf_header->file->max_bitrate)
this->rate = this->asf_header->file->max_bitrate >> 3;
else if (this->length)
this->rate = (int64_t) this->input->get_length(this->input) * 1000 / this->length;
_x_stream_info_set(this->stream, XINE_STREAM_INFO_BITRATE, this->asf_header->file->max_bitrate);
for (i = 0; i < this->asf_header->stream_count; i++) {
asf_stream_t *asf_stream = this->asf_header->streams[i];
asf_demux_stream_t *demux_stream = &this->streams[i];
if (!asf_stream) {
if (this->mode != ASF_MODE_NO_CONTENT) {
xine_log(this->stream->xine, XINE_LOG_MSG,
_("demux_asf: warning: A stream appears to be missing.\n"));
_x_message(this->stream, XINE_MSG_READ_ERROR,
_("Media stream missing?"), NULL);
this->mode = ASF_MODE_NO_CONTENT;
}
return 0;
}
if (asf_stream->encrypted_flag) {
if (this->mode != ASF_MODE_ENCRYPTED_CONTENT) {
xine_log(this->stream->xine, XINE_LOG_MSG,
_("demux_asf: warning: The stream id=%d is encrypted.\n"), asf_stream->stream_number);
_x_message(this->stream, XINE_MSG_ENCRYPTED_SOURCE,
_("Media stream scrambled/encrypted"), NULL);
this->mode = ASF_MODE_ENCRYPTED_CONTENT;
}
}
switch (asf_stream->stream_type) {
case GUID_ASF_AUDIO_MEDIA:
_x_waveformatex_le2me( (xine_waveformatex *) asf_stream->private_data );
if (asf_stream->error_correction_type == GUID_ASF_AUDIO_SPREAD) {
this->reorder_h = asf_stream->error_correction_data[0];
this->reorder_w = (asf_stream->error_correction_data[2]<<8)|asf_stream->error_correction_data[1];
this->reorder_b = (asf_stream->error_correction_data[4]<<8)|asf_stream->error_correction_data[3];
this->reorder_w /= this->reorder_b;
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: audio conceal interleave detected (%d x %d x %d)\n",
this->reorder_w, this->reorder_h, this->reorder_b );
} else {
this->reorder_b = this->reorder_h = this->reorder_w = 1;
}
demux_stream->buf_type = _x_formattag_to_buf_audio
( ((xine_waveformatex *)asf_stream->private_data)->wFormatTag );
if ( !demux_stream->buf_type ) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: unknown audio type 0x%x\n",
((xine_waveformatex *)asf_stream->private_data)->wFormatTag);
demux_stream->buf_type = BUF_AUDIO_UNKNOWN;
}
_x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, _x_buf_audio_name(demux_stream->buf_type));
this->streams[i].fifo = this->audio_fifo;
this->streams[i].frag_offset = 0;
this->streams[i].seq = 0;
if (this->reorder_h > 1 && this->reorder_w > 1 ) {
if( !this->streams[i].buffer )
this->streams[i].buffer = malloc( DEFRAG_BUFSIZE );
this->streams[i].defrag = 1;
} else
this->streams[i].defrag = 0;
lprintf ("found an audio stream id=%d \n", asf_stream->stream_number);
break;
case GUID_ASF_VIDEO_MEDIA:
{
/* video private data
* 11 bytes : header
* 40 bytes : bmiheader
* XX bytes : optional palette
*/
uint32_t width, height;
/*uint16_t bmiheader_size;*/
xine_bmiheader *bmiheader;
width = _X_LE_32(asf_stream->private_data);
height = _X_LE_32(asf_stream->private_data + 4);
/* there is one unknown byte between height and the bmiheader size */
/*bmiheader_size = _X_LE_16(asf_stream->private_data + 9);*/
/* FIXME: bmiheader_size must be >= sizeof(xine_bmiheader) */
bmiheader = (xine_bmiheader *) (asf_stream->private_data + 11);
_x_bmiheader_le2me(bmiheader);
/* FIXME: check if (bmi_header_size == bmiheader->biSize) ? */
demux_stream->buf_type = _x_fourcc_to_buf_video(bmiheader->biCompression);
if( !demux_stream->buf_type ) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: unknown video format %.4s\n", (char*)&(bmiheader->biCompression));
demux_stream->buf_type = BUF_VIDEO_UNKNOWN;
}
_x_meta_info_set(this->stream, XINE_META_INFO_VIDEOCODEC, _x_buf_video_name(demux_stream->buf_type));
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH, width);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT, height);
this->streams[i].fifo = this->video_fifo;
this->streams[i].frag_offset = 0;
this->streams[i].defrag = 0;
/* load the palette, if there is one */
demux_stream->palette_count = bmiheader->biClrUsed;
lprintf ("palette_count: %d\n", demux_stream->palette_count);
if (demux_stream->palette_count > 256) {
lprintf ("number of colours exceeded 256 (%d)", demux_stream->palette_count);
demux_stream->palette_count = 256;
}
if ((asf_stream->private_data_length - sizeof(xine_bmiheader) - 11) >= (demux_stream->palette_count * 4)) {
int j;
uint8_t *palette;
/* according to msdn the palette is located here : */
palette = (uint8_t *)bmiheader + bmiheader->biSize;
/* load the palette */
for (j = 0; j < demux_stream->palette_count; j++) {
demux_stream->palette[j].b = *(palette + j * 4 + 0);
demux_stream->palette[j].g = *(palette + j * 4 + 1);
demux_stream->palette[j].r = *(palette + j * 4 + 2);
}
} else {
int j;
/* generate a greyscale palette */
demux_stream->palette_count = 256;
for (j = 0; j < demux_stream->palette_count; j++) {
demux_stream->palette[j].r = j;
demux_stream->palette[j].g = j;
demux_stream->palette[j].b = j;
}
}
lprintf ("found a video stream id=%d, buf_type=%08x \n",
this->asf_header->streams[i]->stream_number, this->streams[i].buf_type);
}
break;
}
}
this->input->seek (this->input, sizeof(GUID) + 10, SEEK_CUR);
this->packet_size_left = 0;
this->first_packet_pos = this->input->get_current_pos (this->input);
return 1;
}
static int demux_asf_send_headers_common (demux_asf_t *this) {
/* will get overridden later */
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 0);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 0);
/*
* initialize asf engine
*/
this->audio_stream = -1;
this->video_stream = -1;
this->packet_size = 0;
this->seqno = 0;
if (!asf_read_header (this)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_read_header failed.\n");
this->status = DEMUX_FINISHED;
return 1;
} else {
/*
* send start buffer
*/
_x_demux_control_start(this->stream);
if (this->asf_header->content) {
_x_meta_info_set(this->stream, XINE_META_INFO_TITLE, this->asf_header->content->title);
_x_meta_info_set(this->stream, XINE_META_INFO_ARTIST, this->asf_header->content->author);
_x_meta_info_set(this->stream, XINE_META_INFO_COMMENT, this->asf_header->content->description);
}
/* Choose the best audio and the best video stream.
* Use the bitrate to do the choice.
*/
asf_header_choose_streams(this->asf_header, -1, &this->video_stream, &this->audio_stream);
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: video stream_id: %d, audio stream_id: %d\n",
this->video_stream != -1 ? this->asf_header->streams[this->video_stream]->stream_number : -1,
this->audio_stream != -1 ? this->asf_header->streams[this->audio_stream]->stream_number : -1);
if (this->audio_stream != -1) {
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 1);
asf_send_audio_header(this, this->audio_stream);
}
if (this->video_stream != -1) {
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 1);
asf_send_video_header(this, this->video_stream);
}
}
return 0;
}
static void asf_reorder(demux_asf_t *this, uint8_t *src, int len){
uint8_t *dst = malloc(len);
uint8_t *s2 = src;
int i = 0, x, y;
while(len-i >= this->reorder_h * this->reorder_w*this->reorder_b){
for(x = 0; x < this->reorder_w; x++)
for(y = 0; y < this->reorder_h; y++){
memcpy(dst + i, s2 + (y * this->reorder_w+x) * this->reorder_b,
this->reorder_b);
i += this->reorder_b;
}
s2 += this->reorder_h * this->reorder_w * this->reorder_b;
}
xine_fast_memcpy(src,dst,i);
free(dst);
}
/* 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_asf_t *this, int64_t pts, int video, int frame_end) {
int64_t diff;
diff = pts - this->last_pts[video];
#ifdef LOG
if (pts) {
if (video) {
printf ("demux_asf: VIDEO: pts = %8"PRId64", diff = %8"PRId64"\n", pts, pts - this->last_pts[video]);
} else {
printf ("demux_asf: AUDIO: pts = %8"PRId64", diff = %8"PRId64"\n", pts, pts - this->last_pts[video]);
}
}
#endif
if (pts && (this->send_newpts || (this->last_pts[video] && abs(diff) > WRAP_THRESHOLD))) {
lprintf ("sending newpts %"PRId64" (video = %d diff = %"PRId64")\n", pts, video, diff);
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)
this->last_pts[video] = pts;
}
static void asf_send_buffer_nodefrag (demux_asf_t *this, asf_demux_stream_t *stream,
int frag_offset, int64_t timestamp,
int frag_len) {
buf_element_t *buf;
int bufsize;
int package_done;
lprintf ("pts=%"PRId64", off=%d, len=%d, total=%d\n",
timestamp * 90, frag_offset, frag_len, stream->payload_size);
if (frag_offset == 0) {
/* new packet */
stream->frag_offset = 0;
lprintf("new packet\n");
} else {
if (frag_offset == stream->frag_offset) {
/* continuing packet */
lprintf("continuing packet: offset=%d\n", frag_offset);
} else {
/* cannot continue current packet: free it */
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_send_buffer_nodefrag: stream offset: %d, invalid offset: %d\n", stream->frag_offset, frag_offset);
this->input->seek (this->input, frag_len, SEEK_CUR);
return ;
}
}
while( frag_len ) {
if ( frag_len < stream->fifo->buffer_pool_buf_size )
bufsize = frag_len;
else
bufsize = stream->fifo->buffer_pool_buf_size;
buf = stream->fifo->buffer_pool_alloc (stream->fifo);
if (this->input->read (this->input, buf->content, bufsize) != bufsize) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: input buffer starved\n");
return ;
}
lprintf ("data: %d %d %d %d\n", buf->content[0], buf->content[1], buf->content[2], buf->content[3]);
if( this->input->get_length (this->input) )
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 / this->input->get_length (this->input) );
buf->extra_info->input_time = timestamp;
lprintf ("input normpos is %d, input time is %d, rate %d\n",
buf->extra_info->input_normpos,
buf->extra_info->input_time,
this->rate);
buf->pts = timestamp * 90;
buf->type = stream->buf_type;
buf->size = bufsize;
timestamp = 0;
if (stream->frag_offset == 0)
buf->decoder_flags |= BUF_FLAG_FRAME_START;
stream->frag_offset += bufsize;
frag_len -= bufsize;
package_done = (stream->frag_offset >= stream->payload_size);
if ((buf->type & BUF_MAJOR_MASK) == BUF_VIDEO_BASE)
check_newpts (this, buf->pts, PTS_VIDEO, package_done);
else
check_newpts (this, buf->pts, PTS_AUDIO, package_done);
/* test if whole packet read */
if (package_done) {
buf->decoder_flags |= BUF_FLAG_FRAME_END;
lprintf("packet done: offset=%d, payload=%d\n", stream->frag_offset, stream->payload_size);
}
lprintf ("buffer type %08x %8d bytes, %8lld pts\n",
buf->type, buf->size, buf->pts);
stream->fifo->put (stream->fifo, buf);
}
}
static void asf_send_buffer_defrag (demux_asf_t *this, asf_demux_stream_t *stream,
int frag_offset, int64_t timestamp,
int frag_len) {
buf_element_t *buf;
int package_done;
/*
printf("asf_send_buffer seq=%d frag_offset=%d frag_len=%d\n",
seq, frag_offset, frag_len );
*/
lprintf ("asf_send_buffer_defrag: timestamp=%"PRId64", pts=%"PRId64"\n", timestamp, timestamp * 90);
if (frag_offset == 0) {
/* new packet */
lprintf("new packet\n");
stream->frag_offset = 0;
stream->timestamp = timestamp;
} else {
if (frag_offset == stream->frag_offset) {
/* continuing packet */
lprintf("continuing packet: offset=%d\n", frag_offset);
} else {
/* cannot continue current packet: free it */
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_send_buffer_defrag: invalid offset\n");
this->input->seek (this->input, frag_len, SEEK_CUR);
return ;
}
}
if( stream->frag_offset + frag_len > DEFRAG_BUFSIZE ) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: buffer overflow on defrag!\n");
} else {
if (this->input->read (this->input, &stream->buffer[stream->frag_offset], frag_len) != frag_len) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: input buffer starved\n");
return ;
}
stream->frag_offset += frag_len;
}
package_done = (stream->frag_offset >= stream->payload_size);
if (package_done) {
int bufsize;
uint8_t *p;
lprintf("packet done: offset=%d, payload=%d\n", stream->frag_offset, stream->payload_size);
if (stream->fifo == this->audio_fifo &&
this->reorder_h > 1 && this->reorder_w > 1 ) {
asf_reorder(this,stream->buffer,stream->frag_offset);
}
p = stream->buffer;
while( stream->frag_offset ) {
if ( stream->frag_offset < stream->fifo->buffer_pool_buf_size )
bufsize = stream->frag_offset;
else
bufsize = stream->fifo->buffer_pool_buf_size;
buf = stream->fifo->buffer_pool_alloc (stream->fifo);
xine_fast_memcpy (buf->content, p, bufsize);
if( this->input->get_length (this->input) )
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 / this->input->get_length (this->input) );
buf->extra_info->input_time = stream->timestamp;
/* send the same pts for the entire frame */
buf->pts = stream->timestamp * 90;
buf->type = stream->buf_type;
buf->size = bufsize;
lprintf ("buffer type %08x %8d bytes, %8lld pts\n",
buf->type, buf->size, buf->pts);
stream->frag_offset -= bufsize;
p+=bufsize;
if ((buf->type & BUF_MAJOR_MASK) == BUF_VIDEO_BASE)
check_newpts (this, buf->pts, PTS_VIDEO, !stream->frag_offset);
else
check_newpts (this, buf->pts, PTS_AUDIO, !stream->frag_offset);
/* test if whole packet read */
if ( !stream->frag_offset )
buf->decoder_flags |= BUF_FLAG_FRAME_END;
stream->fifo->put (stream->fifo, buf);
}
}
}
/* return 0 if ok */
static int asf_parse_packet_align(demux_asf_t *this) {
uint64_t current_pos, packet_pos;
uint32_t mod;
uint64_t packet_num;
current_pos = this->input->get_current_pos (this->input);
/* seek to the beginning of the next packet */
mod = (current_pos - this->first_packet_pos) % this->packet_size;
this->packet_size_left = mod ? this->packet_size - mod : 0;
packet_pos = current_pos + this->packet_size_left;
if (this->packet_size_left) {
lprintf("last packet is not finished, %d bytes\n", this->packet_size_left);
current_pos = this->input->seek (this->input, packet_pos, SEEK_SET);
if (current_pos != packet_pos) {
return 1;
}
}
this->packet_size_left = 0;
/* check packet_count */
packet_num = (packet_pos - this->first_packet_pos) / this->packet_size;
lprintf("packet_num=%"PRId64", packet_count=%"PRId64"\n", packet_num, this->packet_count);
if (packet_num >= this->packet_count) {
/* end of payload data */
current_pos = this->input->get_current_pos (this->input);
lprintf("end of payload data, current_pos=%"PRId64"\n", current_pos);
{
/* check new asf header */
if (get_guid(this) == GUID_ASF_HEADER) {
lprintf("new asf header detected\n");
_x_demux_control_end(this->stream, 0);
if (demux_asf_send_headers_common(this))
return 1;
} else {
lprintf("not an ASF stream or end of stream\n");
return 1;
}
}
}
return 0;
}
/* return 0 if ok */
static int asf_parse_packet_ecd(demux_asf_t *this, uint32_t *p_hdr_size) {
uint8_t ecd_flags;
uint8_t buf[16];
int invalid_packet;
do {
ecd_flags = get_byte(this); *p_hdr_size = 1;
if (this->status == DEMUX_FINISHED)
return 1;
invalid_packet = 0;
{
int ecd_len;
int ecd_opaque;
int ecd_len_type;
int ecd_present;
ecd_len = ecd_flags & 0xF;
ecd_opaque = (ecd_flags >> 4) & 0x1;
ecd_len_type = (ecd_flags >> 5) & 0x3;
ecd_present = (ecd_flags >> 7) & 0x1;
/* skip ecd */
if (ecd_present && !ecd_opaque && !ecd_len_type) {
int read_size;
read_size = this->input->read (this->input, buf, ecd_len);
if (read_size != ecd_len) {
this->status = DEMUX_FINISHED;
return 1;
}
*p_hdr_size += read_size;
} else {
GUID *guid = (GUID *)buf;
/* check if it's a new stream */
buf[0] = ecd_flags;
if (this->input->read (this->input, buf + 1, 15) != 15) {
this->status = DEMUX_FINISHED;
return 1;
}
*p_hdr_size += 15;
guid->Data1 = _X_LE_32(buf);
guid->Data2 = _X_LE_16(buf + 4);
guid->Data3 = _X_LE_16(buf + 6);
if (get_guid_id(this, guid) == GUID_ASF_HEADER) {
lprintf("new asf header detected\n");
_x_demux_control_end(this->stream, 0);
if (demux_asf_send_headers_common(this))
return 1;
} else {
/* skip invalid packet */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: skip invalid packet: %2X\n", ecd_flags);
this->input->seek (this->input, this->packet_size - *p_hdr_size, SEEK_CUR);
}
invalid_packet = 1;
}
}
} while (invalid_packet);
return 0;
}
/* return 0 if ok */
static int asf_parse_packet_payload_header(demux_asf_t *this, uint32_t p_hdr_size) {
#ifdef LOG
int64_t timestamp;
int64_t duration;
#endif
this->packet_len_flags = get_byte(this); p_hdr_size += 1;
this->packet_prop_flags = get_byte(this); p_hdr_size += 1;
/* packet size */
switch((this->packet_len_flags >> 5) & 3) {
case 1:
this->data_size = get_byte(this); p_hdr_size += 1; break;
case 2:
this->data_size = get_le16(this); p_hdr_size += 2; break;
case 3:
this->data_size = get_le32(this); p_hdr_size += 4; break;
default:
this->data_size = 0;
}
/* sequence */
switch ((this->packet_len_flags >> 1) & 3) {
case 1:
get_byte(this); p_hdr_size += 1; break;
case 2:
get_le16(this); p_hdr_size += 2; break;
case 3:
get_le32(this); p_hdr_size += 4; break;
}
/* padding size */
switch ((this->packet_len_flags >> 3) & 3){
case 1:
this->packet_padsize = get_byte(this); p_hdr_size += 1; break;
case 2:
this->packet_padsize = get_le16(this); p_hdr_size += 2; break;
case 3:
this->packet_padsize = get_le32(this); p_hdr_size += 4; break;
default:
this->packet_padsize = 0;
}
#ifdef LOG
timestamp = get_le32(this); p_hdr_size += 4;
duration = get_le16(this); p_hdr_size += 2;
#endif
lprintf ("timestamp=%"PRId64", duration=%"PRId64"\n", timestamp, duration);
if ((this->packet_len_flags >> 5) & 3) {
/* absolute data size */
lprintf ("absolute data size\n");
this->packet_padsize = this->packet_size - this->data_size; /* not used */
} else {
/* relative data size */
lprintf ("relative data size\n");
this->data_size = this->packet_size - this->packet_padsize;
}
if (this->packet_padsize > this->packet_size) {
/* skip packet */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: invalid padsize: %d\n", this->packet_padsize);
return 1;
}
/* Multiple frames */
if (this->packet_len_flags & 0x01) {
this->frame_flag = get_byte(this); p_hdr_size += 1;
this->nb_frames = (this->frame_flag & 0x3F);
lprintf ("multiple frames %d\n", this->nb_frames);
} else {
this->frame_flag = 0;
this->nb_frames = 1;
}
/* this->packet_size_left = this->packet_size - p_hdr_size; */
this->packet_size_left = this->data_size - p_hdr_size;
lprintf ("new packet, size = %d, size_left = %d, flags = 0x%02x, padsize = %d, this->packet_size = %d\n",
this->data_size, this->packet_size_left, this->packet_len_flags, this->packet_padsize, this->packet_size);
return 0;
}
/* return 0 if ok */
static int asf_parse_packet_payload_common(demux_asf_t *this,
uint8_t raw_id,
asf_demux_stream_t **stream,
uint32_t *frag_offset,
uint32_t *rlen) {
uint8_t stream_id;
int i;
uint32_t s_hdr_size = 0;
uint32_t seq = 0;
uint32_t next_seq = 0;
buf_element_t *buf;
stream_id = raw_id & 0x7f;
*stream = NULL;
lprintf ("got raw_id=%d, stream_id=%d\n", raw_id, stream_id);
for (i = 0; i < this->asf_header->stream_count; i++) {
lprintf ("stream_number = %d\n", this->asf_header->streams[i]->stream_number);
if ((this->asf_header->streams[i]->stream_number == stream_id) &&
(( this->audio_stream != -1 && stream_id == this->asf_header->streams[this->audio_stream]->stream_number ) ||
( this->video_stream != -1 && stream_id == this->asf_header->streams[this->video_stream]->stream_number ))) {
*stream = &this->streams[i];
break;
}
}
switch ((this->packet_prop_flags >> 4) & 3){
case 1:
seq = get_byte(this); s_hdr_size += 1;
if (*stream) {
(*stream)->seq = (*stream)->seq % 256;
next_seq = ((*stream)->seq + 1) % 256;
}
break;
case 2:
seq = get_le16(this); s_hdr_size += 2;
if (*stream) {
(*stream)->seq = (*stream)->seq % 65536;
next_seq = ((*stream)->seq + 1) % 65536;
}
break;
case 3:
seq = get_le32(this); s_hdr_size += 4;
if (*stream)
next_seq = (*stream)->seq + 1;
break;
default:
lprintf ("seq=0\n");
seq = 0;
}
/* check seq number */
if (*stream) {
lprintf ("stream_id = %d, seq = %d\n", stream_id, seq);
if ((*stream)->first_seq || (*stream)->skip) {
next_seq = seq;
(*stream)->first_seq = 0;
}
if ((seq != (*stream)->seq) && (seq != next_seq)) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: bad seq: seq = %d, next_seq = %d, stream seq = %d!\n", seq, next_seq, (*stream)->seq);
/* the stream is corrupted, reset the decoder and restart at a new keyframe */
if ((*stream)->fifo) {
buf = (*stream)->fifo->buffer_pool_alloc ((*stream)->fifo);
buf->type = BUF_CONTROL_RESET_DECODER;
(*stream)->fifo->put((*stream)->fifo, buf);
}
if (this->video_stream != -1 && stream_id == this->asf_header->streams[this->video_stream]->stream_number) {
lprintf ("bad seq: waiting for keyframe\n");
(*stream)->resync = 1;
(*stream)->skip = 1;
this->keyframe_found = 0;
}
}
(*stream)->seq = seq;
}
switch ((this->packet_prop_flags >> 2) & 3) {
case 1:
*frag_offset = get_byte(this); s_hdr_size += 1; break;
case 2:
*frag_offset = get_le16(this); s_hdr_size += 2; break;
case 3:
*frag_offset = get_le32(this); s_hdr_size += 4; break;
default:
lprintf ("frag_offset=0\n");
*frag_offset = 0;
}
switch (this->packet_prop_flags & 3) {
case 1:
*rlen = get_byte(this); s_hdr_size += 1; break;
case 2:
*rlen = get_le16(this); s_hdr_size += 2; break;
case 3:
*rlen = get_le32(this); s_hdr_size += 4; break;
default:
*rlen = 0;
}
if (*rlen > this->packet_size_left) {
/* skip packet */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: invalid rlen %d\n", *rlen);
return 1;
}
lprintf ("segment header, stream id %02x, frag_offset %d, flags : %02x\n",
stream_id, *frag_offset, *rlen);
this->packet_size_left -= s_hdr_size;
return 0;
}
/* return 0 if ok */
static int asf_parse_packet_compressed_payload(demux_asf_t *this,
asf_demux_stream_t *stream,
uint8_t raw_id,
uint32_t frag_offset,
int64_t *timestamp) {
uint32_t s_hdr_size = 0;
uint32_t data_length = 0;
uint32_t data_sent = 0;
*timestamp = frag_offset;
if (*timestamp)
*timestamp -= this->asf_header->file->preroll;
frag_offset = 0;
get_byte (this); s_hdr_size += 1;
if (this->packet_len_flags & 0x01) {
/* multiple frames */
switch ((this->frame_flag >> 6) & 3) {
case 1:
data_length = get_byte(this); s_hdr_size += 1; break;
case 2:
data_length = get_le16(this); s_hdr_size += 2; break;
case 3:
data_length = get_le32(this); s_hdr_size += 4; break;
default:
lprintf ("invalid frame_flag %d\n", this->frame_flag);
data_length = get_le16(this); s_hdr_size += 2;
}
lprintf ("reading multiple payload, size = %d\n", data_length);
} else {
data_length = this->packet_size_left - s_hdr_size;
lprintf ("reading single payload, size = %d\n", data_length);
}
if (data_length > this->packet_size_left) {
/* skip packet */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: invalid data_length\n");
return 1;
}
this->packet_size_left -= s_hdr_size;
while (data_sent < data_length) {
int object_length = get_byte(this);
lprintf ("sending grouped object, len = %d\n", object_length);
if (stream && stream->fifo) {
stream->payload_size = object_length;
/* keyframe detection for non-seekable input plugins */
if (stream->skip && (raw_id & 0x80) && !this->keyframe_found) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: keyframe detected\n");
this->keyframe_ts = *timestamp;
this->keyframe_found = 1;
}
if (stream->resync && (this->keyframe_found) && (*timestamp >= this->keyframe_ts)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: stream resynced\n");
stream->resync = 0;
stream->skip = 0;
}
if (!stream->skip) {
lprintf ("sending buffer of type %08x\n", stream->buf_type);
if (stream->defrag)
asf_send_buffer_defrag (this, stream, 0, *timestamp, object_length);
else
asf_send_buffer_nodefrag (this, stream, 0, *timestamp, object_length);
} else {
lprintf ("skip object\n");
this->input->seek (this->input, object_length, SEEK_CUR);
}
stream->seq++;
} else {
lprintf ("unhandled stream type\n");
this->input->seek (this->input, object_length, SEEK_CUR);
}
data_sent += object_length + 1;
this->packet_size_left -= object_length + 1;
*timestamp = 0;
}
*timestamp = frag_offset;
return 0;
}
/* return 0 if ok */
static int asf_parse_packet_payload(demux_asf_t *this,
asf_demux_stream_t *stream,
uint8_t raw_id,
uint32_t frag_offset,
uint32_t rlen,
int64_t *timestamp) {
uint32_t s_hdr_size = 0;
uint32_t frag_len;
uint32_t payload_size = 0;
if (rlen >= 8) {
payload_size = get_le32(this); s_hdr_size += 4;
*timestamp = get_le32(this); s_hdr_size += 4;
if (*timestamp)
*timestamp -= this->asf_header->file->preroll;
if (stream)
stream->payload_size = payload_size;
if ((rlen - 8) > 0)
this->input->seek (this->input, rlen - 8, SEEK_CUR);
s_hdr_size += rlen - 8;
} else {
*timestamp = 0;
if (rlen) this->input->seek (this->input, rlen, SEEK_CUR);
s_hdr_size += rlen;
}
if (this->packet_len_flags & 0x01) {
switch ((this->frame_flag >> 6) & 3) {
case 1:
frag_len = get_byte(this); s_hdr_size += 1; break;
case 2:
frag_len = get_le16(this); s_hdr_size += 2; break;
case 3:
frag_len = get_le32(this); s_hdr_size += 4; break;
default:
lprintf ("invalid frame_flag %d\n", this->frame_flag);
frag_len = get_le16(this); s_hdr_size += 2;
}
lprintf ("reading multiple payload, payload_size=%d, frag_len=%d\n", payload_size, frag_len);
} else {
frag_len = this->packet_size_left - s_hdr_size;
lprintf ("reading single payload, payload_size=%d, frag_len = %d\n", payload_size, frag_len);
}
if (frag_len > this->packet_size_left) {
/* skip packet */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: invalid frag_len %d\n", frag_len);
return 1;
}
this->packet_size_left -= s_hdr_size;
if (stream && stream->fifo) {
if (!frag_offset) {
/* keyframe detection for non-seekable input plugins */
if (stream->skip && (raw_id & 0x80) && !this->keyframe_found) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: keyframe detected\n");
this->keyframe_found = 1;
this->keyframe_ts = *timestamp;
}
if (stream->resync && this->keyframe_found && (*timestamp >= this->keyframe_ts) &&
!frag_offset) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: stream resynced\n");
stream->resync = 0;
stream->skip = 0;
}
}
if (!stream->skip) {
lprintf ("sending buffer of type %08x\n", stream->buf_type);
if (stream->defrag)
asf_send_buffer_defrag (this, stream, frag_offset, *timestamp, frag_len);
else
asf_send_buffer_nodefrag (this, stream, frag_offset, *timestamp, frag_len);
} else {
lprintf ("skip fragment\n");
this->input->seek (this->input, frag_len, SEEK_CUR);
}
} else {
lprintf ("unhandled stream type\n");
this->input->seek (this->input, frag_len, SEEK_CUR);
}
this->packet_size_left -= frag_len;
return 0;
}
/*
* parse a m$ http reference
* format :
* [Reference]
* Ref1=http://www.blabla.com/blabla
*/
static int demux_asf_parse_http_references( demux_asf_t *this) {
char *buf = NULL;
char *ptr, *end;
int buf_size = 0;
int buf_used = 0;
int len;
char *href = NULL;
int free_href = 0;
/* read file to memory.
* warning: dumb code, but hopefuly ok since reference file is small */
do {
buf_size += 1024;
buf = realloc(buf, buf_size+1);
len = this->input->read(this->input, &buf[buf_used], buf_size-buf_used);
if( len > 0 )
buf_used += len;
/* 50k of reference file? no way. something must be wrong */
if( buf_used > 50*1024 )
break;
} while( len > 0 );
if(buf_used)
buf[buf_used] = '\0';
ptr = buf;
if (!strncmp(ptr, "[Reference]", 11)) {
const char *const mrl = this->input->get_mrl(this->input);
if (!strncmp(mrl, "http", 4)) {
/* never trust a ms server, reopen the same mrl with the mms input plugin
* some servers are badly configured and return a incorrect reference.
*/
href = strdup(mrl);
free_href = 1;
} else {
ptr += 11;
if (*ptr == '\r') ptr ++;
if (*ptr == '\n') ptr ++;
href = strchr(ptr, '=');
if (!href) goto failure;
href++;
end = strchr(href, '\r');
if (!end) goto failure;
*end = '\0';
}
/* replace http by mmsh */
if (!strncmp(href, "http", 4)) {
memcpy(href, "mmsh", 4);
}
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: http ref: %s\n", href);
_x_demux_send_mrl_reference (this->stream, 0, href, NULL, 0, 0);
if (free_href)
free(href);
}
failure:
free (buf);
this->status = DEMUX_FINISHED;
return this->status;
}
/*
* parse a stupid ASF reference in an asx file
* format : "ASF http://www.blabla.com/blabla"
*/
static int demux_asf_parse_asf_references( demux_asf_t *this) {
char *buf = NULL;
char *ptr;
int buf_size = 0;
int buf_used = 0;
int len;
int i;
/* read file to memory.
* warning: dumb code, but hopefuly ok since reference file is small */
do {
buf_size += 1024;
buf = realloc(buf, buf_size+1);
len = this->input->read(this->input, &buf[buf_used], buf_size-buf_used);
if( len > 0 )
buf_used += len;
/* 50k of reference file? no way. something must be wrong */
if( buf_used > 50*1024 )
break;
} while( len > 0 );
if(buf_used)
buf[buf_used] = '\0';
ptr = buf;
if (!strncmp(ptr, "ASF ", 4)) {
ptr += 4;
/* find the end of the string */
for (i = 4; i < buf_used; i++) {
if ((buf[i] == ' ') || (buf[i] == '\r') || (buf[i] == '\n')) {
buf[i] = '\0';
break;
}
}
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf ref: %s\n", ptr);
_x_demux_send_mrl_reference (this->stream, 0, ptr, NULL, 0, 0);
}
free (buf);
this->status = DEMUX_FINISHED;
return this->status;
}
/* .asx playlist parser helper functions */
static uint32_t asx_get_time_value (const xml_node_t *node)
{
const char *value = xml_parser_get_property (node, "VALUE");
if (value)
{
int hours, minutes;
double seconds;
if (sscanf (value, "%d:%d:%lf", &hours, &minutes, &seconds) == 3)
return hours * 3600000 + minutes * 60000 + seconds * 1000;
if (sscanf (value, "%d:%lf", &minutes, &seconds) == 3)
return minutes * 60000 + seconds * 1000;
/* FIXME: single element is minutes or seconds? */
}
return 0; /* value not found */
}
/*
* parse .asx playlist files
*/
static int demux_asf_parse_asx_references( demux_asf_t *this) {
char *buf = NULL;
int buf_size = 0;
int buf_used = 0;
int len;
xml_node_t *xml_tree, *asx_entry, *asx_ref;
xml_parser_t *xml_parser;
int result;
/* read file to memory.
* warning: dumb code, but hopefuly ok since reference file is small */
do {
buf_size += 1024;
buf = realloc(buf, buf_size+1);
len = this->input->read(this->input, &buf[buf_used], buf_size-buf_used);
if( len > 0 )
buf_used += len;
/* 50k of reference file? no way. something must be wrong */
if( buf_used > 50*1024 )
break;
} while( len > 0 );
if(buf_used)
buf[buf_used] = '\0';
xml_parser = xml_parser_init_r(buf, buf_used, XML_PARSER_CASE_INSENSITIVE);
if((result = xml_parser_build_tree_r(xml_parser, &xml_tree)) != XML_PARSER_OK) {
xml_parser_finalize_r(xml_parser);
goto failure;
}
xml_parser_finalize_r(xml_parser);
if(!strcasecmp(xml_tree->name, "ASX")) {
/* Attributes: VERSION, PREVIEWMODE, BANNERBAR
* Child elements: ABSTRACT, AUTHOR, BASE, COPYRIGHT, DURATION, ENTRY,
ENTRYREF, MOREINFO, PARAM, REPEAT, TITLE
*/
/*const char *base_href = NULL;*/
for (asx_entry = xml_tree->child; asx_entry; asx_entry = asx_entry->next)
{
/*const char *ref_base_href = base_href;*/
if (!strcasecmp (asx_entry->name, "ENTRY"))
{
/* Attributes: CLIENTSKIP, SKIPIFREF
* Child elements: ABSTRACT, AUTHOR, BASE, COPYRIGHT, DURATION,
ENDMARKER, MOREINFO, PARAM, REF, STARTMARKER,
STARTTIME, TITLE
*/
const char *href = NULL;
const char *title = NULL;
uint32_t start_time = (uint32_t)-1;
uint32_t duration = (uint32_t)-1;
for (asx_ref = asx_entry->child; asx_ref; asx_ref = asx_ref->next)
{
if (!strcasecmp(asx_ref->name, "REF"))
{
xml_node_t *asx_sub;
/* Attributes: HREF
* Child elements: DURATION, ENDMARKER, STARTMARKER, STARTTIME
*/
/* FIXME: multiple REFs => alternative streams
* (and per-ref start times and durations?).
* Just the one title, though.
*/
href = xml_parser_get_property (asx_ref, "HREF");
for (asx_sub = asx_ref->child; asx_sub; asx_sub = asx_sub->next)
{
if (!strcasecmp (asx_sub->name, "STARTTIME"))
start_time = asx_get_time_value (asx_sub);
else if (!strcasecmp (asx_sub->name, "DURATION"))
duration = asx_get_time_value (asx_sub);
}
}
else if (!strcasecmp (asx_ref->name, "TITLE"))
{
if (!title)
title = asx_ref->data;
}
else if (!strcasecmp (asx_ref->name, "STARTTIME"))
{
if (start_time == (uint32_t)-1)
start_time = asx_get_time_value (asx_ref);
}
else if (!strcasecmp (asx_ref->name, "DURATION"))
{
if (duration == (uint32_t)-1)
duration = asx_get_time_value (asx_ref);
}
#if 0
else if (!strcasecmp (asx_ref->name, "BASE"))
/* Attributes: HREF */
ref_base_href = xml_parser_get_property (asx_entry, "HREF");
#endif
}
/* FIXME: prepend ref_base_href to href */
if (href && *href)
_x_demux_send_mrl_reference (this->stream, 0, href, title,
start_time == (uint32_t)-1 ? 0 : start_time,
duration == (uint32_t)-1 ? -1 : duration);
}
else if (!strcasecmp (asx_entry->name, "ENTRYREF"))
{
/* Attributes: HREF, CLIENTBIND */
const char *href = xml_parser_get_property (asx_entry, "HREF");
if (href && *href)
_x_demux_send_mrl_reference (this->stream, 0, href, NULL, 0, -1);
}
#if 0
else if (!strcasecmp (asx_entry->name, "BASE"))
/* Attributes: HREF */
base_href = xml_parser_get_property (asx_entry, "HREF");
#endif
}
}
else
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: Unsupported XML type: '%s'.\n", xml_tree->name);
xml_parser_free_tree(xml_tree);
failure:
free(buf);
this->status = DEMUX_FINISHED;
return this->status;
}
/*
* xine specific functions start here
*/
static int demux_asf_send_chunk (demux_plugin_t *this_gen) {
demux_asf_t *this = (demux_asf_t *) this_gen;
asf_demux_stream_t *stream = NULL;
uint32_t frag_offset = 0;
uint32_t rlen = 0;
uint8_t raw_id = 0;
int64_t ts = 0;
switch (this->mode) {
case ASF_MODE_ASX_REF:
return demux_asf_parse_asx_references(this);
case ASF_MODE_HTTP_REF:
return demux_asf_parse_http_references(this);
case ASF_MODE_ASF_REF:
return demux_asf_parse_asf_references(this);
case ASF_MODE_ENCRYPTED_CONTENT:
case ASF_MODE_NO_CONTENT:
this->status = DEMUX_FINISHED;
return this->status;
default:
{
int header_size = 0;
if (asf_parse_packet_align(this)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_parse_packet_align failed\n");
this->status = DEMUX_FINISHED;
return this->status;
}
if (asf_parse_packet_ecd(this, &header_size)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_parse_packet_ecd failed\n");
this->status = DEMUX_FINISHED;
return this->status;
}
if (asf_parse_packet_payload_header(this, header_size)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_parse_packet_header failed\n");
this->status = DEMUX_FINISHED;
return this->status;
}
for (this->frame = 0; this->frame < (this->nb_frames & 0x3f); this->frame++) {
raw_id = get_byte(this); this->packet_size_left -= 1;
if (asf_parse_packet_payload_common(this, raw_id, &stream, &frag_offset, &rlen))
break;
if (rlen == 1) {
if (asf_parse_packet_compressed_payload(this, stream, raw_id, frag_offset, &ts))
break;
} else {
if (asf_parse_packet_payload(this, stream, raw_id, frag_offset, rlen, &ts))
break;
}
}
return this->status;
}
}
}
static void demux_asf_dispose (demux_plugin_t *this_gen) {
demux_asf_t *this = (demux_asf_t *) this_gen;
if (this->asf_header) {
int i;
for (i=0; i<this->asf_header->stream_count; i++) {
asf_demux_stream_t *asf_stream;
asf_stream = &this->streams[i];
if (asf_stream->buffer) {
free (asf_stream->buffer);
asf_stream->buffer = NULL;
}
}
asf_header_delete (this->asf_header);
}
free (this);
}
static int demux_asf_get_status (demux_plugin_t *this_gen) {
demux_asf_t *this = (demux_asf_t *) this_gen;
return this->status;
}
static void demux_asf_send_headers (demux_plugin_t *this_gen) {
demux_asf_t *this = (demux_asf_t *) this_gen;
int guid;
this->video_fifo = this->stream->video_fifo;
this->audio_fifo = this->stream->audio_fifo;
this->last_pts[0] = 0;
this->last_pts[1] = 0;
this->status = DEMUX_OK;
if (this->input->get_capabilities (this->input) & INPUT_CAP_SEEKABLE)
this->input->seek (this->input, 0, SEEK_SET);
if ((this->mode == ASF_MODE_ASX_REF) ||
(this->mode == ASF_MODE_HTTP_REF) ||
(this->mode == ASF_MODE_ASF_REF)) {
_x_demux_control_start(this->stream);
return;
}
guid = get_guid(this);
if (guid != GUID_ASF_HEADER) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: file doesn't start with an asf header\n");
this->status = DEMUX_FINISHED;
return;
}
demux_asf_send_headers_common(this);
lprintf ("send header done\n");
}
static int demux_asf_seek (demux_plugin_t *this_gen,
off_t start_pos, int start_time, int playing) {
demux_asf_t *this = (demux_asf_t *) this_gen;
asf_demux_stream_t *stream = NULL;
uint32_t frag_offset = 0;
uint32_t rlen = 0;
uint8_t raw_id, stream_id;
int i, state;
int64_t ts;
lprintf ("demux_asf_seek: start_pos=%"PRId64", start_time=%d\n",
start_pos, start_time);
this->status = DEMUX_OK;
if (this->mode != ASF_MODE_NORMAL) {
return this->status;
}
/*
* seek to start position
*/
for(i = 0; i < this->asf_header->stream_count; i++) {
this->streams[i].frag_offset = 0;
this->streams[i].first_seq = 1;
this->streams[i].seq = 0;
this->streams[i].timestamp = 0;
}
this->last_pts[PTS_VIDEO] = 0;
this->last_pts[PTS_AUDIO] = 0;
this->keyframe_ts = 0;
this->keyframe_found = 0;
/* engine sync stuff */
this->send_newpts = 1;
this->buf_flag_seek = 1;
if (this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) {
_x_demux_flush_engine(this->stream);
start_time /= 1000;
start_pos = (off_t) ( (double) start_pos / 65535 *
this->input->get_length (this->input) );
if ( (!start_pos) && (start_time))
start_pos = start_time * this->rate;
if (start_pos < this->first_packet_pos)
start_pos = this->first_packet_pos;
/*
* Find the previous keyframe
*
* states : 0 start, search a video keyframe
* 1 video keyframe found, search an audio packet
* 2 no video stream, search an audio packet
* 5 end
*/
state = 0;
/* no video stream */
if (this->video_stream == -1) {
if (this->audio_stream == -1) {
lprintf ("demux_asf_seek: no video stream, no audio stream\n");
return this->status;
} else {
lprintf ("demux_asf_seek: no video stream\n");
state = 2;
}
}
/* force the demuxer to not send data to decoders */
if (this->video_stream >= 0) {
this->streams[this->video_stream].skip = 1;
this->streams[this->video_stream].resync = 0;
}
if (this->audio_stream >= 0) {
this->streams[this->audio_stream].skip = 1;
this->streams[this->audio_stream].resync = 0;
}
start_pos -= (start_pos - this->first_packet_pos) % this->packet_size;
while ((start_pos >= this->first_packet_pos) && (state != 5)) {
int header_size;
/* seek to the beginning of the previous packet */
lprintf ("demux_asf_seek: seek back\n");
if (this->input->seek (this->input, start_pos, SEEK_SET) != start_pos) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: demux_asf_seek: seek failed\n");
goto error;
}
header_size = 0;
if (asf_parse_packet_ecd(this, &header_size)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_parse_packet_ecd failed\n");
this->status = DEMUX_FINISHED;
return this->status;
}
if (asf_parse_packet_payload_header(this, header_size)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: asf_parse_packet_header failed\n");
this->status = DEMUX_FINISHED;
return this->status;
}
for (this->frame = 0; this->frame < (this->nb_frames & 0x3f); this->frame++) {
raw_id = get_byte(this); this->packet_size_left -= 1;
lprintf ("demux_asf_seek: raw_id = %d\n", raw_id);
stream_id = raw_id & 0x7f;
if (asf_parse_packet_payload_common(this, raw_id, &stream, &frag_offset, &rlen))
break;
if (rlen == 1) {
if (asf_parse_packet_compressed_payload(this, stream, raw_id, frag_offset, &ts))
break;
} else {
if (asf_parse_packet_payload(this, stream, raw_id, frag_offset, rlen, &ts))
break;
}
if (state == 0) {
if (this->keyframe_found) {
if (this->audio_stream == -1) {
lprintf ("demux_asf_seek: no audio stream\n");
state = 5;
}
state = 1; /* search an audio packet with pts < this->keyframe_pts */
lprintf ("demux_asf_seek: keyframe found at %"PRId64", timestamp = %"PRId64"\n", start_pos, ts);
check_newpts (this, ts * 90, 1, 0);
}
} else if (state == 1) {
if ((this->audio_stream != -1 && stream_id == this->asf_header->streams[this->audio_stream]->stream_number) && ts &&
(ts <= this->keyframe_ts)) {
lprintf ("demux_asf_seek: audio packet found at %"PRId64", ts = %"PRId64"\n", start_pos, ts);
state = 5; /* end */
break;
}
} else if (state == 2) {
if ((this->audio_stream != -1 && stream_id == this->asf_header->streams[this->audio_stream]->stream_number) && !frag_offset) {
this->keyframe_found = 1;
this->keyframe_ts = ts;
state = 5; /* end */
lprintf ("demux_asf_seek: audio packet found at %"PRId64", timestamp = %"PRId64"\n", start_pos, ts);
check_newpts (this, ts * 90, 0, 0);
}
}
}
start_pos -= this->packet_size;
}
if (state != 5) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_asf: demux_asf_seek: beginning of the stream\n");
this->input->seek (this->input, this->first_packet_pos, SEEK_SET);
this->keyframe_found = 1;
} else {
this->input->seek (this->input, start_pos + this->packet_size, SEEK_SET);
}
lprintf ("demux_asf_seek: keyframe_found=%d, keyframe_ts=%"PRId64"\n",
this->keyframe_found, this->keyframe_ts);
if (this->video_stream >= 0) {
this->streams[this->video_stream].resync = 1;
this->streams[this->video_stream].skip = 1;
}
if (this->audio_stream >= 0) {
this->streams[this->audio_stream].resync = 1;
this->streams[this->audio_stream].skip = 1;
}
} else if (!playing && this->input->seek_time != NULL) {
if (start_pos && !start_time)
start_time = this->length * start_pos / 65535;
this->input->seek_time (this->input, start_time, SEEK_SET);
this->keyframe_ts = 0;
this->keyframe_found = 0; /* means next keyframe */
if (this->video_stream >= 0) {
this->streams[this->video_stream].resync = 1;
this->streams[this->video_stream].skip = 1;
}
if (this->audio_stream >= 0) {
this->streams[this->audio_stream].resync = 0;
this->streams[this->audio_stream].skip = 0;
}
} else {
/* "streaming" mode */
this->keyframe_ts = 0;
this->keyframe_found = 0; /* means next keyframe */
if (this->video_stream >= 0) {
this->streams[this->video_stream].resync = 1;
this->streams[this->video_stream].skip = 1;
}
if (this->audio_stream >= 0) {
this->streams[this->audio_stream].resync = 0;
this->streams[this->audio_stream].skip = 0;
}
}
return this->status;
error:
this->status = DEMUX_FINISHED;
return this->status;
}
static int demux_asf_get_stream_length (demux_plugin_t *this_gen) {
demux_asf_t *this = (demux_asf_t *) this_gen;
return this->length;
}
static uint32_t demux_asf_get_capabilities(demux_plugin_t *this_gen) {
return DEMUX_CAP_NOCAP;
}
static int demux_asf_get_optional_data(demux_plugin_t *this_gen,
void *data, int data_type) {
return DEMUX_OPTIONAL_UNSUPPORTED;
}
static demux_plugin_t *open_plugin (demux_class_t *class_gen,
xine_stream_t *stream,
input_plugin_t *input) {
demux_asf_t *this;
uint8_t buf[MAX_PREVIEW_SIZE+1];
int len;
switch (stream->content_detection_method) {
case METHOD_BY_CONTENT:
/*
* try to get a preview of the data
*/
len = input->get_optional_data (input, buf, INPUT_OPTIONAL_DATA_PREVIEW);
if (len == INPUT_OPTIONAL_UNSUPPORTED) {
if (input->get_capabilities (input) & INPUT_CAP_SEEKABLE) {
input->seek (input, 0, SEEK_SET);
if ( (len=input->read (input, buf, 1024)) <= 0)
return NULL;
lprintf ("PREVIEW data unavailable, but seek+read worked.\n");
} else
return NULL;
}
if (memcmp(buf, &guids[GUID_ASF_HEADER].guid, sizeof(GUID))) {
buf[len] = '\0';
if( !strstr(buf,"asx") &&
!strstr(buf,"ASX") &&
strncmp(buf,"[Reference]", 11) &&
strncmp(buf,"ASF ", 4) &&
((buf[0] != 0x30)
|| (buf[1] != 0x26)
|| (buf[2] != 0xb2)
|| (buf[3] != 0x75)))
return NULL;
}
lprintf ("file starts with an asf header\n");
break;
case METHOD_BY_EXTENSION: {
const char *const mrl = input->get_mrl (input);
const char *const ending = strrchr (mrl, '.');
if (!ending)
return NULL;
if (strncasecmp(ending, ".asf", 4) &&
strncasecmp(ending, ".wmv", 4) &&
strncasecmp(ending, ".wma", 4) ) {
return NULL;
}
lprintf ("extension accepted.\n");
}
break;
case METHOD_EXPLICIT:
break;
default:
xprintf (stream->xine, XINE_VERBOSITY_DEBUG,
"demux_asf: warning, unknown method %d\n", stream->content_detection_method);
return NULL;
}
this = calloc(1, sizeof(demux_asf_t));
this->stream = stream;
this->input = input;
/*
* check for reference stream
*/
this->mode = ASF_MODE_NORMAL;
len = input->get_optional_data (input, buf, INPUT_OPTIONAL_DATA_PREVIEW);
if ( (len == INPUT_OPTIONAL_UNSUPPORTED) &&
(input->get_capabilities (input) & INPUT_CAP_SEEKABLE) ) {
input->seek (input, 0, SEEK_SET);
len=input->read (input, buf, 1024);
}
if(len > 0) {
buf[len] = '\0';
if( strstr((char*)buf,"asx") || strstr((char*)buf,"ASX") )
this->mode = ASF_MODE_ASX_REF;
if( strstr((char*)buf,"[Reference]") )
this->mode = ASF_MODE_HTTP_REF;
if( strstr((char*)buf,"ASF ") )
this->mode = ASF_MODE_ASF_REF;
}
this->demux_plugin.send_headers = demux_asf_send_headers;
this->demux_plugin.send_chunk = demux_asf_send_chunk;
this->demux_plugin.seek = demux_asf_seek;
this->demux_plugin.dispose = demux_asf_dispose;
this->demux_plugin.get_status = demux_asf_get_status;
this->demux_plugin.get_stream_length = demux_asf_get_stream_length;
this->demux_plugin.get_capabilities = demux_asf_get_capabilities;
this->demux_plugin.get_optional_data = demux_asf_get_optional_data;
this->demux_plugin.demux_class = class_gen;
this->status = DEMUX_FINISHED;
return &this->demux_plugin;
}
static const char *get_description (demux_class_t *this_gen) {
return "ASF demux plugin";
}
static const char *get_identifier (demux_class_t *this_gen) {
return "ASF";
}
static const char *get_extensions (demux_class_t *this_gen) {
/* asx, wvx, wax are metafile or playlist */
return "asf wmv wma asx wvx wax";
}
static const char *get_mimetypes (demux_class_t *this_gen) {
return "video/x-ms-asf: asf: ASF stream;"
"video/x-ms-wmv: wmv: Windows Media Video;"
"audio/x-ms-wma: wma: Windows Media Audio;"
"application/vnd.ms-asf: asf: ASF stream;"
"application/x-mplayer2: asf,asx,asp: mplayer2;"
"video/x-ms-asf-plugin: asf,asx,asp: mms animation;"
"video/x-ms-wvx: wvx: wmv metafile;"
"video/x-ms-wax: wva: wma metafile;";
}
static void class_dispose (demux_class_t *this_gen) {
demux_asf_class_t *this = (demux_asf_class_t *) this_gen;
free (this);
}
static void *init_class (xine_t *xine, void *data) {
demux_asf_class_t *this;
this = calloc(1, sizeof(demux_asf_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_asf = {
10 /* priority */
};
const plugin_info_t xine_plugin_info[] EXPORTED = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_DEMUX, 26, "asf", XINE_VERSION_CODE, &demux_info_asf, init_class },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks