Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

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

Download this file

demux_avi.c    2374 lines (1923 with data), 77.2 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
/*
* Copyright (C) 2000-2004 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 avi streams
*
* part of the code is taken from
* avilib (C) 1999 Rainer Johanni <Rainer@Johanni.de>
*/
/*
* Ian Goldberg <ian@cypherpunks.ca> modified this code so that it can
* handle "streaming" AVI files. By that I mean real seekable files, but
* ones that are growing as we're displaying them. Examples include
* AVI's you're downloading, or ones you're writing in real time using
* xawtv streamer, or whatever. This latter is really useful, for
* example, for doing the PVR trick of starting the streamer to record
* TV, starting to watch it ~10 minutes later, and skipping the
* commercials to catch up to real time. If you accidentally hit the
* end of the stream, just hit your "back 15 seconds" key, and all is
* good.
*
* Theory of operation: the video and audio indices have been separated
* out of the main avi_t and avi_audio_t structures into separate
* structures that can grow during playback. We use the idx_grow_t
* structure to keep track of the offset into the AVI file where we
* expect to find the next A/V frame. We periodically check if we can
* read data from the file at that offset. If we can, we append index
* data for as many frames as we can read at the time.
*/
/*
* OpenDML (AVI2.0) stuff was done by Tilmann Bitterberg
* <transcode@tibit.org> in December 2003.
* Transcode's and xine's avi code comes from the same source and
* still has a very similar architecture, so it wasn't much effort to
* port it from transcode to xine.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LOG_MODULE "demux_avi"
#define LOG_VERBOSE
/*
#define DEBUG_ODML
#define LOG
*/
#include "xine_internal.h"
#include "xineutils.h"
#include "demux.h"
#include "bswap.h"
/*
* stolen from wine headers
*/
#define AVIIF_KEYFRAME 0x00000010L
#define MAX_AUDIO_STREAMS 8
#define NUM_PREVIEW_BUFFERS 10
typedef struct{
off_t pos;
uint32_t len;
uint32_t flags;
} video_index_entry_t;
typedef struct{
off_t pos;
uint32_t len;
off_t tot;
uint32_t block_no; /* block number, used compute pts in audio VBR streams */
} audio_index_entry_t;
/* For parsing OpenDML AVI2.0 files */
#define AVI_INDEX_OF_INDEXES 0x00 /* when each entry in aIndex */
/* array points to an index chunk */
#define AVI_INDEX_OF_CHUNKS 0x01 /* when each entry in aIndex */
/* array points to a chunk in the file */
#define AVI_INDEX_IS_DATA 0x80 /* when each entry is aIndex is */
/* really the data */
/* bIndexSubtype codes for INDEX_OF_CHUNKS */
#define AVI_INDEX_2FIELD 0x01 /* when fields within frames */
/* are also indexed */
typedef struct _avisuperindex_entry {
uint64_t qwOffset; /* absolute file offset */
uint32_t dwSize; /* size of index chunk at this offset */
uint32_t dwDuration; /* time span in stream ticks */
} avisuperindex_entry;
typedef struct _avistdindex_entry {
uint32_t dwOffset; /* qwBaseOffset + this is absolute file offset */
uint32_t dwSize; /* bit 31 is set if this is NOT a keyframe */
} avistdindex_entry;
/* Standard index */
typedef struct _avistdindex_chunk {
char fcc[4]; /* ix## */
uint32_t dwSize; /* size of this chunk */
uint16_t wLongsPerEntry; /* must be sizeof(aIndex[0])/sizeof(DWORD) */
uint8_t bIndexSubType; /* must be 0 */
uint8_t bIndexType; /* must be AVI_INDEX_OF_CHUNKS */
uint32_t nEntriesInUse; /* */
char dwChunkId[4]; /* '##dc' or '##db' or '##wb' etc.. */
uint64_t qwBaseOffset; /* all dwOffsets in aIndex array are relative to this */
uint32_t dwReserved3; /* must be 0 */
avistdindex_entry *aIndex;
} avistdindex_chunk;
/* Base Index Form 'indx' */
typedef struct _avisuperindex_chunk {
char fcc[4];
uint32_t dwSize; /* size of this chunk */
uint16_t wLongsPerEntry; /* size of each entry in aIndex array (must be 8 for us) */
uint8_t bIndexSubType; /* future use. must be 0 */
uint8_t bIndexType; /* one of AVI_INDEX_* codes */
uint32_t nEntriesInUse; /* index of first unused member in aIndex array */
char dwChunkId[4]; /* fcc of what is indexed */
uint32_t dwReserved[3]; /* meaning differs for each index type/subtype. */
/* 0 if unused */
avisuperindex_entry *aIndex; /* where are the ix## chunks */
avistdindex_chunk **stdindex; /* the ix## chunks itself (array) */
} avisuperindex_chunk;
/* These next three are the video and audio structures that can grow
* during the playback of a streaming file. */
typedef struct{
uint32_t video_frames; /* Number of video frames */
uint32_t alloc_frames; /* Allocated number of frames */
video_index_entry_t *vindex;
} video_index_t;
typedef struct{
uint32_t audio_chunks; /* Chunks of audio data in the file */
uint32_t alloc_chunks; /* Allocated number of chunks */
audio_index_entry_t *aindex;
} audio_index_t;
typedef struct{
off_t nexttagoffset; /* The offset into the AVI file where we expect */
/* to find the next A/V frame */
} idx_grow_t;
typedef struct{
uint32_t dwInitialFrames;
uint32_t dwScale;
uint32_t dwRate;
uint32_t dwStart;
uint32_t dwSampleSize;
uint32_t block_no;
uint32_t audio_type; /* BUF_AUDIO_xxx type */
uint32_t audio_strn; /* Audio stream number */
char audio_tag[4]; /* Tag of audio data */
uint32_t audio_posc; /* Audio position: chunk */
uint32_t audio_posb; /* Audio position: byte within chunk */
int wavex_len;
xine_waveformatex *wavex;
audio_index_t audio_idx;
avisuperindex_chunk *audio_superindex;
off_t audio_tot; /* Total number of audio bytes */
} avi_audio_t;
typedef struct{
int32_t width; /* Width of a video frame */
int32_t height; /* Height of a video frame */
uint32_t dwInitialFrames;
uint32_t dwScale;
uint32_t dwRate;
uint32_t dwStart;
double fps; /* Frames per second */
uint32_t compressor; /* Type of compressor */
uint32_t video_strn; /* Video stream number */
char video_tag[4]; /* Tag of video data */
uint32_t video_posf; /* Number of next frame to be read
(if index present) */
uint32_t video_posb; /* Video position: byte within frame */
avi_audio_t *audio[MAX_AUDIO_STREAMS];
int n_audio;
uint32_t video_type; /* BUF_VIDEO_xxx type */
uint32_t n_idx; /* number of index entries actually filled */
uint32_t max_idx; /* number of index entries actually allocated */
unsigned char (*idx)[16]; /* index entries (AVI idx1 tag) */
video_index_t video_idx;
xine_bmiheader *bih;
off_t movi_start;
off_t movi_end;
int palette_count;
palette_entry_t palette[256];
int is_opendml; /* set to 1 if this is an odml file with multiple index chunks */
avisuperindex_chunk *video_superindex; /* index of indices */
int total_frames; /* total number of frames if dmlh is present */
} avi_t;
typedef struct demux_avi_s {
demux_plugin_t demux_plugin;
xine_stream_t *stream;
fifo_buffer_t *audio_fifo;
fifo_buffer_t *video_fifo;
input_plugin_t *input;
int status;
uint32_t video_step;
uint32_t AVI_errno;
/* seeking args backup */
int seek_start_time;
off_t seek_start_pos;
avi_t *avi;
idx_grow_t idx_grow;
uint8_t no_audio:1;
uint8_t streaming:1;
uint8_t has_index:1;
uint8_t seek_request:1;
/* discontinuity detection (only at seek) */
uint8_t buf_flag_seek:1;
uint8_t send_newpts:1;
} demux_avi_t ;
typedef struct {
demux_class_t demux_class;
} demux_avi_class_t;
/* The following variable indicates the kind of error */
#define AVI_ERR_SIZELIM 1 /* The write of the data would exceed
the maximum size of the AVI file.
This is more a warning than an error
since the file may be closed safely */
#define AVI_ERR_OPEN 2 /* Error opening the AVI file - wrong path
name or file nor readable/writable */
#define AVI_ERR_READ 3 /* Error reading from AVI File */
#define AVI_ERR_WRITE 4 /* Error writing to AVI File,
disk full ??? */
#define AVI_ERR_WRITE_INDEX 5 /* Could not write index to AVI file
during close, file may still be
usable */
#define AVI_ERR_CLOSE 6 /* Could not write header to AVI file
or not truncate the file during close,
file is most probably corrupted */
#define AVI_ERR_NOT_PERM 7 /* Operation not permitted:
trying to read from a file open
for writing or vice versa */
#define AVI_ERR_NO_MEM 8 /* malloc failed */
#define AVI_ERR_NO_AVI 9 /* Not an AVI file */
#define AVI_ERR_NO_HDRL 10 /* AVI file has no header list,
corrupted ??? */
#define AVI_ERR_NO_MOVI 11 /* AVI file has no MOVI list,
corrupted ??? */
#define AVI_ERR_NO_VIDS 12 /* AVI file contains no video data */
#define AVI_ERR_NO_IDX 13 /* The file has been opened with
getIndex==0, but an operation has been
performed that needs an index */
#define AVI_ERR_BAD_SIZE 14 /* A chunk has an invalid size */
#define AVI_HEADER_UNKNOWN -1
#define AVI_HEADER_AUDIO 0
#define AVI_HEADER_VIDEO 1
#define AVI_HEADER_SIZE 8
#define WRAP_THRESHOLD 90000
#define PTS_AUDIO 0
#define PTS_VIDEO 1
/* bit 31 denotes a keyframe */
static uint32_t odml_len (unsigned char *str)
{
return _X_LE_32(str) & 0x7fffffff;
}
/* if bit 31 is 0, its a keyframe */
static uint32_t odml_key (unsigned char *str)
{
return (_X_LE_32(str) & 0x80000000)?0:0x10;
}
static void check_newpts (demux_avi_t *this, int64_t pts, int video) {
if (this->send_newpts) {
lprintf ("sending newpts %" PRId64 " (video = %d)\n", pts, video);
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;
}
}
/* Append an index entry for a newly-found video frame */
static int video_index_append(avi_t *AVI, off_t pos, uint32_t len, uint32_t flags) {
video_index_t *vit = &(AVI->video_idx);
/* Make sure there's room */
if (vit->video_frames == vit->alloc_frames) {
long newalloc = vit->alloc_frames + 4096;
video_index_entry_t *newindex =
realloc(vit->vindex, newalloc * sizeof(video_index_entry_t));
if (!newindex) return -1;
vit->vindex = newindex;
vit->alloc_frames = newalloc;
}
/* Set the new index entry */
vit->vindex[vit->video_frames].pos = pos;
vit->vindex[vit->video_frames].len = len;
vit->vindex[vit->video_frames].flags = flags;
vit->video_frames += 1;
return 0;
}
/* Append an index entry for a newly-found audio frame */
static int audio_index_append(avi_t *AVI, int stream, off_t pos, uint32_t len,
off_t tot, uint32_t block_no) {
audio_index_t *ait = &(AVI->audio[stream]->audio_idx);
/* Make sure there's room */
if (ait->audio_chunks == ait->alloc_chunks) {
uint32_t newalloc = ait->alloc_chunks + 4096;
audio_index_entry_t *newindex =
realloc(ait->aindex, newalloc * sizeof(audio_index_entry_t));
if (!newindex) return -1;
ait->aindex = newindex;
ait->alloc_chunks = newalloc;
}
/* Set the new index entry */
ait->aindex[ait->audio_chunks].pos = pos;
ait->aindex[ait->audio_chunks].len = len;
ait->aindex[ait->audio_chunks].tot = tot;
ait->aindex[ait->audio_chunks].block_no = block_no;
ait->audio_chunks += 1;
return 0;
}
#define PAD_EVEN(x) ( ((x)+1) & ~1 )
static int64_t get_audio_pts (demux_avi_t *this, int track, uint32_t posc,
off_t postot, uint32_t posb) {
avi_audio_t *at = this->avi->audio[track];
lprintf("get_audio_pts: track=%d, posc=%d, postot=%" PRIdMAX ", posb=%d\n", track, posc, (intmax_t)postot, posb);
if ((at->dwSampleSize == 0) && (at->dwScale > 1)) {
/* variable bitrate */
lprintf("get_audio_pts: VBR: nBlockAlign=%d, dwSampleSize=%d, dwScale=%d, dwRate=%d\n",
at->wavex->nBlockAlign, at->dwSampleSize, at->dwScale, at->dwRate);
return (int64_t)(90000.0 * (double)(posc + at->dwStart) *
(double)at->dwScale / (double)at->dwRate);
} else {
/* constant bitrate */
lprintf("get_audio_pts: CBR: nBlockAlign=%d, dwSampleSize=%d, dwScale=%d, dwRate=%d\n",
at->wavex->nBlockAlign, at->dwSampleSize, at->dwScale, at->dwRate);
if( at->wavex && at->wavex->nBlockAlign ) {
return (int64_t)((double)((postot + posb) / (double)at->wavex->nBlockAlign + at->dwStart) *
(double)at->dwScale / (double)at->dwRate * 90000.0);
} else {
return (int64_t)((double)((postot + posb) / (double)at->dwSampleSize + at->dwStart) *
(double)at->dwScale / (double)at->dwRate * 90000.0);
}
}
}
static int64_t get_video_pts (demux_avi_t *this, off_t pos) {
lprintf("get_video_pts: dwScale=%d, dwRate=%d, pos=%" PRIdMAX "\n",
this->avi->dwScale, this->avi->dwRate, (intmax_t)pos);
return (int64_t)(90000.0 * (double)(pos + this->avi->dwStart) *
(double)this->avi->dwScale / (double)this->avi->dwRate);
}
/* Some handy stopper tests for idx_grow, below. */
/* Use this one to ensure the current video frame is in the index. */
static int video_pos_stopper(demux_avi_t *this, void *data){
if (this->avi->video_posf >= this->avi->video_idx.video_frames) {
return -1;
}
return 1;
}
/* Use this one to ensure the current audio chunk is in the index. */
static int audio_pos_stopper(demux_avi_t *this, void *data) {
avi_audio_t *AVI_A = (avi_audio_t *)data;
if (AVI_A->audio_posc >= AVI_A->audio_idx.audio_chunks) {
return -1;
}
return 1;
}
/* Use this one to ensure that a video frame with the given position
* is in the index. */
static int start_pos_stopper(demux_avi_t *this, void *data) {
off_t start_pos = *(off_t *)data;
int32_t maxframe = this->avi->video_idx.video_frames - 1;
while( maxframe >= 0 && this->avi->video_idx.vindex[maxframe].pos >= start_pos ) {
if ( this->avi->video_idx.vindex[maxframe].flags & AVIIF_KEYFRAME )
return 1;
maxframe--;
}
return -1;
}
/* Use this one to ensure that a video frame with the given timestamp
* is in the index. */
static int start_time_stopper(demux_avi_t *this, void *data) {
int64_t video_pts = *(int64_t *)data;
int32_t maxframe = this->avi->video_idx.video_frames - 1;
while( maxframe >= 0 && get_video_pts(this,maxframe) >= video_pts ) {
if ( this->avi->video_idx.vindex[maxframe].flags & AVIIF_KEYFRAME )
return 1;
maxframe--;
}
return -1;
}
/* This is called periodically to check if there's more file now than
* there was before. If there is, we constuct the index for (just) the
* new part, and append it to the index we've got so far. We stop
* slurping in the new part when stopper(this, stopdata) returns a
* non-negative value, or there's no more file to read. If we're taking
* a long time slurping in the new part, use the on-screen display to
* notify the user. Returns -1 if EOF was reached, the non-negative
* return value of stopper otherwise. */
static int idx_grow(demux_avi_t *this, int (*stopper)(demux_avi_t *, void *),
void *stopdata) {
int retval = -1;
int num_read = 0;
uint8_t data[AVI_HEADER_SIZE];
uint8_t data2[4];
off_t savepos = this->input->seek(this->input, 0, SEEK_CUR);
off_t chunk_pos;
uint32_t chunk_len;
int sent_event = 0;
this->input->seek(this->input, this->idx_grow.nexttagoffset, SEEK_SET);
chunk_pos = this->idx_grow.nexttagoffset;
while (((retval = stopper(this, stopdata)) < 0) &&
(!_x_action_pending(this->stream))) {
int valid_chunk = 0;
num_read += 1;
if (num_read % 1000 == 0) {
/* send event to frontend about index generation progress */
xine_event_t event;
xine_progress_data_t prg;
off_t file_len;
file_len = this->input->get_length (this->input);
prg.description = _("Restoring index...");
prg.percent = 100 * this->idx_grow.nexttagoffset / file_len;
event.type = XINE_EVENT_PROGRESS;
event.data = &prg;
event.data_length = sizeof (xine_progress_data_t);
xine_event_send (this->stream, &event);
sent_event = 1;
}
if (this->input->read(this->input, data, AVI_HEADER_SIZE) != AVI_HEADER_SIZE) {
lprintf("read failed, chunk_pos=%" PRIdMAX "\n", (intmax_t)chunk_pos);
break;
}
/* Dive into RIFF and LIST entries */
if(strncasecmp(data, "LIST", 4) == 0 ||
strncasecmp(data, "RIFF", 4) == 0) {
this->idx_grow.nexttagoffset =
this->input->seek(this->input, 4,SEEK_CUR);
continue;
}
chunk_len = _X_LE_32(data + 4);
this->idx_grow.nexttagoffset += PAD_EVEN(chunk_len + AVI_HEADER_SIZE);
/* Video chunk */
if ((data[0] == this->avi->video_tag[0]) &&
(data[1] == this->avi->video_tag[1])) {
int flags = AVIIF_KEYFRAME;
off_t pos = chunk_pos + AVI_HEADER_SIZE;
uint32_t tmp;
valid_chunk = 1;
/* FIXME:
* UGLY hack to detect a keyframe parsing decoder data
* AVI chuncks doesn't provide this info and we need it during
* index building
* this hack comes from mplayer (aviheader.c)
* i've added XVID which looks like iso mpeg 4
*/
if (this->input->read(this->input, data2, 4) != 4) {
lprintf("read failed\n");
break;
}
tmp = data2[3] | (data2[2]<<8) | (data2[1]<<16) | (data2[0]<<24);
switch(this->avi->video_type) {
case BUF_VIDEO_MSMPEG4_V1:
this->input->read(this->input, data2, 4);
tmp = data2[3] | (data2[2]<<8) | (data2[1]<<16) | (data2[0]<<24);
tmp = tmp << 5;
case BUF_VIDEO_MSMPEG4_V2:
case BUF_VIDEO_MSMPEG4_V3:
if (tmp & 0x40000000) flags = 0;
break;
case BUF_VIDEO_DIVX5:
case BUF_VIDEO_MPEG4:
case BUF_VIDEO_XVID:
if (tmp == 0x000001B6) flags = 0;
break;
}
if (video_index_append(this->avi, pos, chunk_len, flags) == -1) {
/* If we're out of memory, we just don't grow the index, but
* nothing really bad happens. */
}
} else {
int i;
/* Audio chunk */
for(i = 0; i < this->avi->n_audio; ++i) {
avi_audio_t *audio = this->avi->audio[i];
if ((data[0] == audio->audio_tag[0]) &&
(data[1] == audio->audio_tag[1])) {
off_t pos = chunk_pos + AVI_HEADER_SIZE;
valid_chunk = 1;
/* VBR streams (hack from mplayer) */
if (audio->wavex && audio->wavex->nBlockAlign) {
audio->block_no += (chunk_len + audio->wavex->nBlockAlign - 1) /
audio->wavex->nBlockAlign;
} else {
audio->block_no += 1;
}
if (audio_index_append(this->avi, i, pos, chunk_len, audio->audio_tot,
audio->block_no) == -1) {
/* As above. */
}
this->avi->audio[i]->audio_tot += chunk_len;
}
}
}
if (!valid_chunk) {
xine_log(this->stream->xine, XINE_LOG_MSG, _("demux_avi: invalid avi chunk \"%c%c%c%c\" at pos %" PRIdMAX "\n"), data[0], data[1], data[2], data[3], (intmax_t)chunk_pos);
}
chunk_pos = this->input->seek(this->input, this->idx_grow.nexttagoffset, SEEK_SET);
if (chunk_pos != this->idx_grow.nexttagoffset) {
lprintf("seek failed: %" PRIdMAX " != %" PRIdMAX "\n", (intmax_t)chunk_pos, (intmax_t)this->idx_grow.nexttagoffset);
break;
}
}
if (sent_event == 1) {
/* send event to frontend about index generation progress */
xine_event_t event;
xine_progress_data_t prg;
prg.description = _("Restoring index...");
prg.percent = 100;
event.type = XINE_EVENT_PROGRESS;
event.data = &prg;
event.data_length = sizeof (xine_progress_data_t);
xine_event_send (this->stream, &event);
}
this->input->seek (this->input, savepos, SEEK_SET);
if (retval < 0) retval = -1;
return retval;
}
/* Fetch the current video index entry, growing the index if necessary. */
static video_index_entry_t *video_cur_index_entry(demux_avi_t *this) {
avi_t *AVI = this->avi;
if (AVI->video_posf >= AVI->video_idx.video_frames) {
/* We don't have enough frames; see if the file's bigger yet. */
if (idx_grow(this, video_pos_stopper, NULL) < 0) {
/* We still don't have enough frames. Oh, well. */
return NULL;
}
}
return &(AVI->video_idx.vindex[AVI->video_posf]);
}
/* Fetch the current audio index entry, growing the index if necessary. */
static audio_index_entry_t *audio_cur_index_entry(demux_avi_t *this,
avi_audio_t *AVI_A) {
lprintf("posc: %d, chunks: %d\n", AVI_A->audio_posc, AVI_A->audio_idx.audio_chunks);
if (AVI_A->audio_posc >= AVI_A->audio_idx.audio_chunks) {
/* We don't have enough chunks; see if the file's bigger yet. */
if (idx_grow(this, audio_pos_stopper, AVI_A) < 0) {
/* We still don't have enough chunks. Oh, well. */
return NULL;
}
}
return &(AVI_A->audio_idx.aindex[AVI_A->audio_posc]);
}
static void AVI_close(avi_t *AVI){
int i;
if(AVI->idx) free(AVI->idx);
if(AVI->video_idx.vindex) free(AVI->video_idx.vindex);
if(AVI->bih) free(AVI->bih);
for(i=0; i<AVI->n_audio; i++) {
if(AVI->audio[i]->audio_idx.aindex) free(AVI->audio[i]->audio_idx.aindex);
if(AVI->audio[i]->wavex) free(AVI->audio[i]->wavex);
free(AVI->audio[i]);
}
free(AVI);
}
#define ERR_EXIT(x) \
do { \
this->AVI_errno = x; \
free (AVI); \
return 0; \
} while(0)
static void reset_idx(demux_avi_t *this, avi_t *AVI) {
int n;
this->idx_grow.nexttagoffset = AVI->movi_start;
this->has_index = 0;
AVI->video_idx.video_frames = 0;
for(n = 0; n < AVI->n_audio; n++) {
AVI->audio[n]->audio_idx.audio_chunks = 0;
}
}
static avi_t *XINE_MALLOC AVI_init(demux_avi_t *this) {
avi_t *AVI;
int i, j, idx_type;
uint32_t n;
uint8_t *hdrl_data;
int hdrl_len = 0;
off_t ioff;
int lasttag = 0;
int vids_strh_seen = 0;
int vids_strf_seen = 0;
int num_stream = 0;
uint8_t data[256];
int strf_size;
/* Create avi_t structure */
lprintf("start\n");
AVI = (avi_t *) calloc(1, sizeof(avi_t));
if(AVI==NULL) {
this->AVI_errno = AVI_ERR_NO_MEM;
return 0;
}
/* Read first 12 bytes and check that this is an AVI file */
if (!this->streaming)
this->input->seek(this->input, 0, SEEK_SET);
if( this->input->read(this->input, data,12) != 12 ) ERR_EXIT(AVI_ERR_READ) ;
if( !( (strncasecmp(data ,"ON2 ",4) == 0 &&
strncasecmp(data+8,"ON2f",4) == 0) ||
(strncasecmp(data ,"RIFF",4) == 0 &&
strncasecmp(data+8,"AVI ",4) == 0) ) )
ERR_EXIT(AVI_ERR_NO_AVI) ;
/* Go through the AVI file and extract the header list,
the start position of the 'movi' list and an optionally
present idx1 tag */
hdrl_data = NULL;
while(1) {
off_t next_chunk;
/* Keep track of the last place we tried to read something. */
this->idx_grow.nexttagoffset = this->input->get_current_pos(this->input);
if (this->input->read(this->input, data,8) != 8 ) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG, "failed to read 8 bytes at pos %" PRIdMAX "\n", (intmax_t)this->idx_grow.nexttagoffset);
break; /* We assume it's EOF */
}
n = _X_LE_32(data + 4);
n = PAD_EVEN(n);
next_chunk = this->idx_grow.nexttagoffset + 8 + n;
lprintf("chunk: %c%c%c%c, size: %" PRId64 "\n",
data[0], data[1], data[2], data[3], (int64_t)n);
if (n >= 4 && strncasecmp(data,"LIST",4) == 0) {
if( this->input->read(this->input, data,4) != 4 ) ERR_EXIT(AVI_ERR_READ);
n -= 4;
lprintf(" chunk: %c%c%c%c\n",
data[0], data[1], data[2], data[3]);
if(strncasecmp(data,"hdrl",4) == 0) {
hdrl_len = n;
hdrl_data = (unsigned char *) malloc(n);
if(hdrl_data==0)
ERR_EXIT(AVI_ERR_NO_MEM);
if (this->input->read(this->input, hdrl_data,n) != n )
ERR_EXIT(AVI_ERR_READ);
} else if(strncasecmp(data,"movi",4) == 0) {
AVI->movi_start = this->input->get_current_pos(this->input);
AVI->movi_end = AVI->movi_start + n - 1;
if (this->streaming)
/* stop reading here, we can't seek back */
break;
}
} else if(strncasecmp(data,"idx1",4) == 0 ||
strncasecmp(data,"iddx",4) == 0) {
/* n must be a multiple of 16, but the reading does not
break if this is not the case */
AVI->n_idx = AVI->max_idx = n / 16;
free(AVI->idx); /* On the off chance there are multiple index chunks */
AVI->idx = (unsigned char((*)[16])) malloc(n);
if (AVI->idx == 0)
ERR_EXIT(AVI_ERR_NO_MEM);
if (this->input->read(this->input, (char *)AVI->idx, n) != n ) {
xine_log (this->stream->xine, XINE_LOG_MSG,
_("demux_avi: avi index is broken\n"));
free (AVI->idx); /* Index is broken, reconstruct */
AVI->idx = NULL;
AVI->n_idx = AVI->max_idx = 0;
break; /* EOF */
}
}
if (next_chunk != this->input->seek(this->input, next_chunk, SEEK_SET)) {
xine_log (this->stream->xine, XINE_LOG_MSG, _("demux_avi: failed to seek to the next chunk (pos %" PRIdMAX ")\n"), (intmax_t)next_chunk);
break; /* probably slow seek */
}
}
/* Interpret the header list */
for (i = 0; i < hdrl_len;) {
const int old_i = i;
/* List tags are completly ignored */
lprintf("tag: %c%c%c%c\n",
hdrl_data[i], hdrl_data[i+1], hdrl_data[i+2], hdrl_data[i+3]);
if (strncasecmp(hdrl_data + i, "LIST", 4) == 0) {
i += 12;
continue;
}
n = _X_LE_32(hdrl_data + i + 4);
n = PAD_EVEN(n);
/* Interpret the tag and its args */
if(strncasecmp(hdrl_data + i, "strh", 4) == 0) {
i += 8;
lprintf("tag: %c%c%c%c\n",
hdrl_data[i], hdrl_data[i+1], hdrl_data[i+2], hdrl_data[i+3]);
if(strncasecmp(hdrl_data + i, "vids", 4) == 0 && !vids_strh_seen) {
AVI->compressor = *(uint32_t *) (hdrl_data + i + 4);
AVI->dwInitialFrames = _X_LE_32(hdrl_data + i + 16);
AVI->dwScale = _X_LE_32(hdrl_data + i + 20);
AVI->dwRate = _X_LE_32(hdrl_data + i + 24);
AVI->dwStart = _X_LE_32(hdrl_data + i + 28);
if(AVI->dwScale!=0)
AVI->fps = (double)AVI->dwRate/(double)AVI->dwScale;
this->video_step = (long) (90000.0 / AVI->fps);
AVI->video_strn = num_stream;
vids_strh_seen = 1;
lprintf("video stream header, num_stream=%d\n", num_stream);
lasttag = 1; /* vids */
lprintf("dwScale=%d, dwRate=%d, dwInitialFrames=%d, dwStart=%d, num_stream=%d\n",
AVI->dwScale, AVI->dwRate, AVI->dwInitialFrames, AVI->dwStart, num_stream);
} else if (strncasecmp (hdrl_data+i,"auds",4) ==0 /* && ! auds_strh_seen*/) {
if(AVI->n_audio < MAX_AUDIO_STREAMS) {
avi_audio_t *a = (avi_audio_t *) calloc(1, sizeof(avi_audio_t));
if(a==NULL) {
this->AVI_errno = AVI_ERR_NO_MEM;
return 0;
}
AVI->audio[AVI->n_audio] = a;
a->audio_strn = num_stream;
a->dwInitialFrames = _X_LE_32(hdrl_data + i + 16);
a->dwScale = _X_LE_32(hdrl_data + i + 20);
a->dwRate = _X_LE_32(hdrl_data + i + 24);
a->dwStart = _X_LE_32(hdrl_data + i + 28);
lprintf("dwScale=%d, dwRate=%d, dwInitialFrames=%d, dwStart=%d, num_stream=%d\n",
a->dwScale, a->dwRate, a->dwInitialFrames, a->dwStart, num_stream);
a->dwSampleSize = _X_LE_32(hdrl_data + i + 44);
a->audio_tot = 0;
lprintf("audio stream header, num_stream=%d\n", num_stream);
lasttag = 2; /* auds */
AVI->n_audio++;
}
} else {
/* unknown stream type */
lasttag = 0;
}
num_stream++;
} else if(strncasecmp(hdrl_data+i,"dmlh",4) == 0) {
AVI->total_frames = _X_LE_32(hdrl_data+i+8);
#ifdef DEBUG_ODML
lprintf( "AVI: real number of frames %d\n", AVI->total_frames);
#endif
i += 8;
} else if(strncasecmp(hdrl_data + i, "strf", 4) == 0) {
i += 4;
strf_size = _X_LE_32(hdrl_data + i);
i += 4;
if(lasttag == 1) {
/* lprintf ("size : %d\n",sizeof(AVI->bih)); */
AVI->bih = (xine_bmiheader *)
malloc((n < sizeof(xine_bmiheader)) ? sizeof(xine_bmiheader) : n);
if(AVI->bih == NULL) {
this->AVI_errno = AVI_ERR_NO_MEM;
return 0;
}
memcpy (AVI->bih, hdrl_data+i, n);
_x_bmiheader_le2me( AVI->bih );
/* stream_read(demuxer->stream,(char*) &avi_header.bih,MIN(size2,sizeof(avi_header.bih))); */
AVI->width = AVI->bih->biWidth;
AVI->height = AVI->bih->biHeight;
/*
lprintf ("size : %d x %d (%d x %d)\n", AVI->width, AVI->height, AVI->bih.biWidth, AVI->bih.biHeight);
lprintf (" biCompression %d='%.4s'\n", AVI->bih.biCompression,
&AVI->bih.biCompression);
*/
lprintf("video stream format\n");
vids_strf_seen = 1;
/* load the palette, if there is one */
AVI->palette_count = AVI->bih->biClrUsed;
lprintf ("palette_count: %d\n", AVI->palette_count);
if (AVI->palette_count > 256) {
lprintf ("number of colours exceeded 256 (%d)", AVI->palette_count);
AVI->palette_count = 256;
}
if ((strf_size - sizeof(xine_bmiheader)) >= (AVI->palette_count * 4)) {
/* load the palette from the end of the strf chunk */
for (j = 0; j < AVI->palette_count; j++) {
AVI->palette[j].b = *(hdrl_data + i + sizeof(xine_bmiheader) + j * 4 + 0);
AVI->palette[j].g = *(hdrl_data + i + sizeof(xine_bmiheader) + j * 4 + 1);
AVI->palette[j].r = *(hdrl_data + i + sizeof(xine_bmiheader) + j * 4 + 2);
}
} else {
/* generate a greyscale palette */
AVI->palette_count = 256;
for (j = 0; j < AVI->palette_count; j++) {
AVI->palette[j].r = j;
AVI->palette[j].g = j;
AVI->palette[j].b = j;
}
}
} else if(lasttag == 2) {
xine_waveformatex *wavex;
wavex = (xine_waveformatex *)malloc(n);
if (!wavex) {
this->AVI_errno = AVI_ERR_NO_MEM;
return 0;
}
memcpy((void *)wavex, hdrl_data+i, n);
_x_waveformatex_le2me( wavex );
AVI->audio[AVI->n_audio-1]->wavex = wavex;
AVI->audio[AVI->n_audio-1]->wavex_len = n;
lprintf("audio stream format\n");
}
} else if(strncasecmp(hdrl_data + i, "indx",4) == 0) {
uint8_t *a;
int j;
avisuperindex_chunk *superindex;
if (n < sizeof (avisuperindex_chunk)) {
lprintf("broken index !, dwSize=%d\n", n);
i += 8 + n;
continue;
}
superindex = (avisuperindex_chunk *) malloc (sizeof (avisuperindex_chunk));
a = hdrl_data + i;
memcpy (superindex->fcc, a, 4); a += 4;
superindex->dwSize = _X_LE_32(a); a += 4;
superindex->wLongsPerEntry = _X_LE_16(a); a += 2;
superindex->bIndexSubType = *a; a += 1;
superindex->bIndexType = *a; a += 1;
superindex->nEntriesInUse = _X_LE_32(a); a += 4;
memcpy (superindex->dwChunkId, a, 4); a += 4;
#ifdef DEBUG_ODML
printf("FOURCC \"%c%c%c%c\"\n",
superindex->fcc[0], superindex->fcc[1],
superindex->fcc[2], superindex->fcc[3]);
printf("LEN \"%ld\"\n", (long)superindex->dwSize);
printf("wLongsPerEntry \"%d\"\n", superindex->wLongsPerEntry);
printf("bIndexSubType \"%d\"\n", superindex->bIndexSubType);
printf("bIndexType \"%d\"\n", superindex->bIndexType);
printf("nEntriesInUse \"%ld\"\n", (long)superindex->nEntriesInUse);
printf("dwChunkId \"%c%c%c%c\"\n",
superindex->dwChunkId[0], superindex->dwChunkId[1],
superindex->dwChunkId[2], superindex->dwChunkId[3]);
printf("--\n");
#endif
/* 3 * reserved */
a += 4; a += 4; a += 4;
if (superindex->bIndexSubType != 0) {
lprintf("Invalid Header, bIndexSubType != 0\n");
}
if (superindex->nEntriesInUse > n / sizeof (avisuperindex_entry))
{
lprintf("broken index !, dwSize=%d, entries=%d\n", n, superindex->nEntriesInUse);
i += 8 + n;
continue;
}
superindex->aIndex = malloc (superindex->nEntriesInUse * sizeof (avisuperindex_entry));
/* position of ix## chunks */
for (j = 0; j < superindex->nEntriesInUse; ++j) {
superindex->aIndex[j].qwOffset = _X_LE_64 (a); a += 8;
superindex->aIndex[j].dwSize = _X_LE_32 (a); a += 4;
superindex->aIndex[j].dwDuration = _X_LE_32 (a); a += 4;
#ifdef DEBUG_ODML
printf("[%d] 0x%llx 0x%x %u\n", j,
(uint64_t)superindex->aIndex[j].qwOffset,
(uint32_t)superindex->aIndex[j].dwSize,
(uint32_t)superindex->aIndex[j].dwDuration);
#endif
}
this->has_index = 1;
if (lasttag == 1) {
/* V I D E O */
AVI->video_superindex = superindex;
AVI->is_opendml = 1;
} else if (lasttag == 2) {
/* A U D I O */
AVI->audio[AVI->n_audio-1]->audio_superindex = superindex;
AVI->is_opendml = 1;
} else {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_avi: there should not be an index there, lasttag = %d\n", lasttag);
}
i += 8;
} else if(strncasecmp(hdrl_data + i, "JUNK", 4) == 0) {
i += 8;
/* do not reset lasttag */
} else if(strncasecmp(hdrl_data + i, "strd", 4) == 0) {
/* additional header data */
i += 8;
/* do not reset lasttag */
} else if(strncasecmp(hdrl_data + i, "strn", 4) == 0) {
/* stream name */
i += 8;
/* do not reset lasttag */
} else if(strncasecmp(hdrl_data + i, "vprp", 4) == 0) {
/* video properties header*/
i += 8;
/* do not reset lasttag */
} else {
i += 8;
lasttag = 0;
}
i += n;
if (i <= old_i)
ERR_EXIT(AVI_ERR_BAD_SIZE);
}
if( hdrl_data )
free( hdrl_data );
hdrl_data = NULL;
/* somehow ffmpeg doesn't specify the number of frames here */
/* if (!vids_strh_seen || !vids_strf_seen || AVI->video_frames==0) */
if (!vids_strh_seen || !vids_strf_seen)
ERR_EXIT(AVI_ERR_NO_VIDS);
AVI->video_tag[0] = AVI->video_strn / 10 + '0';
AVI->video_tag[1] = AVI->video_strn % 10 + '0';
/* do not use the two following bytes */
AVI->video_tag[2] = 'd';
AVI->video_tag[3] = 'b';
for(i = 0; i < AVI->n_audio; i++) {
/* Audio tag is set to "99wb" if no audio present */
if (!AVI->audio[i]->wavex->nChannels)
AVI->audio[i]->audio_strn = 99;
AVI->audio[i]->audio_tag[0] = AVI->audio[i]->audio_strn / 10 + '0';
AVI->audio[i]->audio_tag[1] = AVI->audio[i]->audio_strn % 10 + '0';
/* do not use the two following bytes */
AVI->audio[i]->audio_tag[2] = 'w';
AVI->audio[i]->audio_tag[3] = 'b';
}
idx_type = 0;
if (!this->streaming) {
this->input->seek(this->input, AVI->movi_start, SEEK_SET);
/* if the file has an idx1, check if this is relative
to the start of the file or to the start of the movi list */
if(AVI->idx) {
off_t pos;
uint32_t len;
/* Search the first videoframe in the idx1 and look where
it is in the file */
for(i = 0; i < AVI->n_idx; i++)
if( (AVI->idx[i][0] == AVI->video_tag[0]) &&
(AVI->idx[i][1] == AVI->video_tag[1]))
break;
#if 0
/* try again for ##ix */
if (i >= AVI->n_idx) {
AVI->video_tag[2] = 'i';
AVI->video_tag[3] = 'x';
}
for(i = 0; i < AVI->n_idx; i++)
if( strncasecmp(AVI->idx[i], AVI->video_tag, 3) == 0 ) break;
#endif
if (i >= AVI->n_idx) {
ERR_EXIT(AVI_ERR_NO_VIDS);
}
pos = _X_LE_32(AVI->idx[i] + 8);
len = _X_LE_32(AVI->idx[i] + 12);
this->input->seek(this->input, pos, SEEK_SET);
if(this->input->read(this->input, data, 8) != 8)
ERR_EXIT(AVI_ERR_READ) ;
if( (strncasecmp(data, AVI->idx[i], 4) == 0) && (_X_LE_32(data + 4) == len) ) {
idx_type = 1; /* Index from start of file */
} else {
this->input->seek(this->input, pos + AVI->movi_start - 4, SEEK_SET);
if(this->input->read(this->input, data, 8) != 8)
ERR_EXIT(AVI_ERR_READ) ;
if( strncasecmp(data,AVI->idx[i], 4) == 0 && _X_LE_32(data + 4) == len ) {
idx_type = 2; /* Index from start of movi list */
}
}
/* idx_type remains 0 if neither of the two tests above succeeds */
}
}
lprintf("idx_type=%d, AVI->n_idx=%d\n", idx_type, AVI->n_idx);
if (idx_type != 0 && !AVI->is_opendml) {
/* Now generate the video index and audio index arrays from the
* idx1 record. */
this->has_index = 1;
ioff = (idx_type == 1) ? AVI_HEADER_SIZE : AVI->movi_start + 4;
for(i = 0; i < AVI->n_idx; i++) {
if((AVI->idx[i][0] == AVI->video_tag[0]) &&
(AVI->idx[i][1] == AVI->video_tag[1])) {
off_t pos = _X_LE_32(AVI->idx[i] + 8) + ioff;
uint32_t len = _X_LE_32(AVI->idx[i] + 12);
uint32_t flags = _X_LE_32(AVI->idx[i] + 4);
if (video_index_append(AVI, pos, len, flags) == -1) {
ERR_EXIT(AVI_ERR_NO_MEM) ;
}
} else {
for(n = 0; n < AVI->n_audio; n++) {
avi_audio_t *audio = AVI->audio[n];
if((AVI->idx[i][0] == audio->audio_tag[0]) &&
(AVI->idx[i][1] == audio->audio_tag[1])) {
off_t pos = _X_LE_32(AVI->idx[i] + 8) + ioff;
uint32_t len = _X_LE_32(AVI->idx[i] + 12);
/* VBR streams (hack from mplayer) */
if (audio->wavex && audio->wavex->nBlockAlign) {
audio->block_no += (len + audio->wavex->nBlockAlign - 1) /
audio->wavex->nBlockAlign;
} else {
audio->block_no += 1;
}
if (audio_index_append(AVI, n, pos, len, audio->audio_tot,
audio->block_no) == -1) {
ERR_EXIT(AVI_ERR_NO_MEM) ;
}
AVI->audio[n]->audio_tot += len;
break;
}
}
}
}
} else if (AVI->is_opendml && !this->streaming) {
uint64_t offset = 0;
int hdrl_len = 4+4+2+1+1+4+4+8+4;
char *en, *chunk_start;
int k = 0, audtr = 0;
uint32_t nrEntries = 0;
int nvi, nai[MAX_AUDIO_STREAMS];
xprintf (this->stream->xine, XINE_VERBOSITY_LOG,
"demux_avi: This is an OpenDML stream\n");
nvi = 0;
for(audtr=0; audtr<AVI->n_audio; ++audtr) nai[audtr] = 0;
/* ************************ */
/* VIDEO */
/* ************************ */
lprintf("video track\n");
if (AVI->video_superindex != NULL) {
for (j=0; j<AVI->video_superindex->nEntriesInUse; j++) {
/* read from file */
chunk_start = en = malloc (AVI->video_superindex->aIndex[j].dwSize+hdrl_len);
if (!chunk_start)
ERR_EXIT(AVI_ERR_NO_MEM);
if (this->input->seek(this->input, AVI->video_superindex->aIndex[j].qwOffset, SEEK_SET) == (off_t)-1) {
lprintf("cannot seek to 0x%" PRIx64 "\n", AVI->video_superindex->aIndex[j].qwOffset);
free(chunk_start);
continue;
}
if (this->input->read(this->input, en, AVI->video_superindex->aIndex[j].dwSize+hdrl_len) <= 0) {
lprintf("cannot read from offset 0x%" PRIx64 " %ld bytes; broken (incomplete) file?\n",
AVI->video_superindex->aIndex[j].qwOffset,
(unsigned long)AVI->video_superindex->aIndex[j].dwSize+hdrl_len);
free(chunk_start);
continue;
}
nrEntries = _X_LE_32(en + 12);
#ifdef DEBUG_ODML
printf("[%d:0] Video nrEntries %ld\n", j, (long)nrEntries);
#endif
offset = _X_LE_64(en + 20);
/* skip header */
en += hdrl_len;
nvi += nrEntries;
while (k < nvi) {
off_t pos;
uint32_t len;
uint32_t flags;
pos = offset + _X_LE_32(en); en += 4;
len = odml_len(en);
flags = odml_key(en); en += 4;
video_index_append(AVI, pos, len, flags);
#ifdef DEBUG_ODML
/*
printf("[%d] POS 0x%llX len=%d key=%s offset (%llx) (%ld)\n", k,
pos, len, flags?"yes":"no ", offset,
(long)AVI->video_superindex->aIndex[j].dwSize);
*/
#endif
k++;
}
free(chunk_start);
}
} else {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_avi: Warning: the video super index is NULL\n");
}
/* ************************ */
/* AUDIO */
/* ************************ */
lprintf("audio tracks\n");
for(audtr=0; audtr<AVI->n_audio; ++audtr) {
avi_audio_t *audio = AVI->audio[audtr];
k = 0;
if (!audio->audio_superindex) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_avi: Warning: cannot read audio index for track %d\n", audtr);
continue;
}
for (j=0; j<audio->audio_superindex->nEntriesInUse; j++) {
/* read from file */
chunk_start = en = malloc (audio->audio_superindex->aIndex[j].dwSize+hdrl_len);
if (!chunk_start)
ERR_EXIT(AVI_ERR_NO_MEM);
if (this->input->seek(this->input, audio->audio_superindex->aIndex[j].qwOffset, SEEK_SET) == (off_t)-1) {
lprintf("cannot seek to 0x%" PRIx64 "\n", audio->audio_superindex->aIndex[j].qwOffset);
free(chunk_start);
continue;
}
if (this->input->read(this->input, en, audio->audio_superindex->aIndex[j].dwSize+hdrl_len) <= 0) {
lprintf("cannot read from offset 0x%" PRIx64 "; broken (incomplete) file?\n",
audio->audio_superindex->aIndex[j].qwOffset);
free(chunk_start);
continue;
}
nrEntries = _X_LE_32(en + 12);
#ifdef DEBUG_ODML
/*printf("[%d:%d] Audio nrEntries %ld\n", j, audtr, nrEntries); */
#endif
offset = _X_LE_64(en + 20);
/* skip header */
en += hdrl_len;
nai[audtr] += nrEntries;
while (k < nai[audtr]) {
off_t pos;
uint32_t len;
pos = offset + _X_LE_32(en); en += 4;
len = odml_len(en); en += 4;
/* VBR streams (hack from mplayer) */
if (audio->wavex && audio->wavex->nBlockAlign) {
audio->block_no += (len + audio->wavex->nBlockAlign - 1) /
audio->wavex->nBlockAlign;
} else {
audio->block_no += 1;
}
audio_index_append(AVI, audtr, pos, len, audio->audio_tot, audio->block_no);
#ifdef DEBUG_ODML
/*
printf("[%d:%d] POS 0x%llX len=%d offset (%llx) (%ld)\n", k, audtr,
pos, (int)len,
offset, (long)audio->audio_superindex->aIndex[j].dwSize);
*/
#endif
audio->audio_tot += len;
++k;
}
free(chunk_start);
}
}
}
if (this->has_index) {
/* check index validity, there must be an index for each video/audio stream */
if (AVI->video_idx.video_frames == 0) {
reset_idx(this, AVI);
}
for(n = 0; n < AVI->n_audio; n++) {
if (AVI->audio[n]->audio_idx.audio_chunks == 0) {
reset_idx(this, AVI);
}
}
} else {
/* We'll just dynamically grow the index as needed. */
lprintf("no index\n");
this->idx_grow.nexttagoffset = AVI->movi_start;
this->has_index = 0;
}
/* Reposition the file */
if (!this->streaming)
this->input->seek(this->input, AVI->movi_start, SEEK_SET);
AVI->video_posf = 0;
AVI->video_posb = 0;
lprintf("done, pos=%"PRId64", AVI->movi_start=%" PRIdMAX "\n", this->input->get_current_pos(this->input), (intmax_t)AVI->movi_start);
return AVI;
}
static void AVI_seek_start(avi_t *AVI) {
int i;
AVI->video_posf = 0;
AVI->video_posb = 0;
for(i = 0; i < AVI->n_audio; i++) {
AVI->audio[i]->audio_posc = 0;
AVI->audio[i]->audio_posb = 0;
}
}
static int AVI_read_audio(demux_avi_t *this, avi_audio_t *AVI_A, char *audbuf,
uint32_t bytes, int *buf_flags) {
off_t pos;
int nr, left, todo;
audio_index_entry_t *aie = audio_cur_index_entry(this, AVI_A);
if(!aie) {
this->AVI_errno = AVI_ERR_NO_IDX;
return -1;
}
nr = 0; /* total number of bytes read */
/* lprintf ("avi audio package len: %d\n", AVI_A->audio_index[AVI_A->audio_posc].len); */
left = aie->len - AVI_A->audio_posb;
while ((bytes > 0) && (left > 0)) {
if (bytes < left)
todo = bytes;
else
todo = left;
pos = aie->pos + AVI_A->audio_posb;
/* lprintf ("read audio from %lld\n", pos); */
if (this->input->seek (this->input, pos, SEEK_SET)<0)
return -1;
if (this->input->read(this->input, audbuf + nr, todo) != todo) {
this->AVI_errno = AVI_ERR_READ;
*buf_flags = 0;
return -1;
}
bytes -= todo;
nr += todo;
AVI_A->audio_posb += todo;
left = aie->len - AVI_A->audio_posb;
}
if (left == 0) {
AVI_A->audio_posc++;
AVI_A->audio_posb = 0;
*buf_flags = BUF_FLAG_FRAME_END;
} else {
*buf_flags = 0;
}
return nr;
}
static int AVI_read_video(demux_avi_t *this, avi_t *AVI, char *vidbuf,
uint32_t bytes, int *buf_flags) {
off_t pos;
int nr, left, todo;
video_index_entry_t *vie = video_cur_index_entry(this);
if (!vie) {
this->AVI_errno = AVI_ERR_NO_IDX;
return -1;
}
nr = 0; /* total number of bytes read */
left = vie->len - AVI->video_posb;
while ((bytes > 0) && (left > 0)) {
if (bytes < left)
todo = bytes;
else
todo = left;
pos = vie->pos + AVI->video_posb;
/* lprintf ("read video from %lld\n", pos); */
if (this->input->seek (this->input, pos, SEEK_SET)<0)
return -1;
if (this->input->read(this->input, vidbuf + nr, todo) != todo) {
this->AVI_errno = AVI_ERR_READ;
*buf_flags = 0;
return -1;
}
bytes -= todo;
nr += todo;
AVI->video_posb += todo;
left = vie->len - AVI->video_posb;
}
if (left == 0) {
AVI->video_posf++;
AVI->video_posb = 0;
*buf_flags = BUF_FLAG_FRAME_END;
} else {
*buf_flags = 0;
}
return nr;
}
static int demux_avi_next (demux_avi_t *this, int decoder_flags) {
int i;
buf_element_t *buf = NULL;
int64_t audio_pts, video_pts;
int do_read_video = (this->avi->n_audio == 0);
int video_sent = 0;
int audio_sent = 0;
lprintf("begin\n");
/* Try to grow the index, in case more of the avi file has shown up
* since we last checked. If it's still too small, well then we're at
* the end of the stream. */
if (this->avi->video_idx.video_frames <= this->avi->video_posf) {
if (idx_grow(this, video_pos_stopper, NULL) < 0) {
lprintf("end of stream\n");
}
}
for (i = 0; i < this->avi->n_audio; i++) {
avi_audio_t *audio = this->avi->audio[i];
if (!this->no_audio &&
(audio->audio_idx.audio_chunks <= audio->audio_posc)) {
if (idx_grow(this, audio_pos_stopper, this->avi->audio[i]) < 0) {
lprintf("end of stream\n");
}
}
}
video_pts = get_video_pts (this, this->avi->video_posf);
for (i=0; i < this->avi->n_audio; i++) {
avi_audio_t *audio = this->avi->audio[i];
audio_index_entry_t *aie = audio_cur_index_entry(this, audio);
/* The tests above mean aie should never be NULL, but just to be
* safe. */
if (!aie) {
lprintf("aie == NULL\n");
continue;
}
audio_pts =
get_audio_pts (this, i, aie->block_no, aie->tot, audio->audio_posb);
lprintf ("video_pts %" PRId64 " audio_pts %" PRId64 "\n", video_pts, audio_pts);
if (!this->no_audio && (audio_pts < video_pts)) {
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
/* read audio */
buf->pts = audio_pts;
buf->size = AVI_read_audio (this, audio, buf->mem, buf->max_size, &buf->decoder_flags);
buf->decoder_flags |= decoder_flags;
if (buf->size < 0) {
buf->free_buffer (buf);
lprintf("audio buf->size < 0\n");
} else {
buf->type = audio->audio_type | i;
buf->extra_info->input_time = audio_pts / 90;
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) );
check_newpts (this, buf->pts, PTS_AUDIO);
this->audio_fifo->put (this->audio_fifo, buf);
audio_sent++;
}
} else
do_read_video = 1;
}
if (audio_sent == 0) {
do_read_video = 1;
}
if (do_read_video) {
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
/* read video */
buf->pts = video_pts;
buf->size = AVI_read_video (this, this->avi, buf->mem, buf->max_size, &buf->decoder_flags);
buf->type = this->avi->video_type;
buf->extra_info->input_time = video_pts / 90;
if (this->has_index && this->avi->video_idx.video_frames > 2) {
/* use video_frames-2 instead of video_frames-1 to fix problems with weird
non-interleaved streams */
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 /
this->avi->video_idx.vindex[this->avi->video_idx.video_frames - 2].pos);
} else {
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->frame_number = this->avi->video_posf;
buf->decoder_flags |= decoder_flags;
if (buf->size < 0) {
buf->free_buffer (buf);
lprintf("video buf->size < 0\n");
} else {
/*
lprintf ("adding buf %d to video fifo, decoder_info[0]: %d\n",
buf, buf->decoder_info[0]);
*/
check_newpts (this, buf->pts, PTS_VIDEO);
this->video_fifo->put (this->video_fifo, buf);
video_sent++;
}
}
if (!audio_sent && !video_sent) {
xprintf (this->stream->xine, XINE_VERBOSITY_LOG,
"demux_avi: video and audio streams are ended\n");
return 0;
}
return 1;
}
/*
* Returns next chunk type
* It's used in streaming mode
*/
static int get_chunk_header(demux_avi_t *this, uint32_t *len, int *audio_stream) {
int i;
char data[AVI_HEADER_SIZE];
while (1) {
if (this->input->read(this->input, data, AVI_HEADER_SIZE) != AVI_HEADER_SIZE)
break;
*len = _X_LE_32(data + 4);
lprintf("header: %c%c%c%c, pos=%" PRIdMAX ", len=%u\n",
data[0], data[1], data[2], data[3],
(intmax_t)this->input->get_current_pos(this->input), *len);
/* Dive into RIFF and LIST entries */
if(strncasecmp(data, "LIST", 4) == 0 ||
strncasecmp(data, "RIFF", 4) == 0) {
this->idx_grow.nexttagoffset =
this->input->seek(this->input, 4,SEEK_CUR);
continue;
}
/*
* Check if we got a tag ##db, ##dc or ##wb
* only the 2 first bytes are reliable
*/
if ((data[0] == this->avi->video_tag[0]) &&
(data[1] == this->avi->video_tag[1])) {
lprintf("video header: %c %c %c %c\n", data[0], data[1], data[2], data[3]);
return AVI_HEADER_VIDEO;
}
for(i = 0; i < this->avi->n_audio; ++i) {
avi_audio_t *audio = this->avi->audio[i];
/*
* only the 2 first bytes are reliable
*/
if ((data[0] == audio->audio_tag[0]) &&
(data[1] == audio->audio_tag[1])) {
*audio_stream = i;
audio->audio_tot += *len;
lprintf("audio header: %c %c %c %c\n", data[0], data[1], data[2], data[3]);
return AVI_HEADER_AUDIO;
}
}
xine_log (this->stream->xine, XINE_LOG_MSG, _("demux_avi: invalid avi chunk \"%c%c%c%c\" at pos %" PRIdMAX "\n"), data[0], data[1], data[2], data[3], (intmax_t)this->input->get_current_pos(this->input));
return AVI_HEADER_UNKNOWN;
}
/* unreachable code */
return AVI_HEADER_UNKNOWN;
}
/*
* Read next chunk
* It's streaming version of demux_avi_next()
* There is no seeking here.
*/
static int demux_avi_next_streaming (demux_avi_t *this, int decoder_flags) {
buf_element_t *buf = NULL;
int64_t audio_pts, video_pts;
off_t current_pos;
int left;
int header, chunk_len = 0, audio_stream;
avi_audio_t *audio;
current_pos = this->input->get_current_pos(this->input);
lprintf("input_pos=%" PRIdMAX "\n", (intmax_t)current_pos);
header = get_chunk_header(this, &chunk_len, &audio_stream);
switch (header) {
case AVI_HEADER_AUDIO:
audio = this->avi->audio[audio_stream];
left = chunk_len;
lprintf("AVI_HEADER_AUDIO: chunk %d, len=%d\n", audio->audio_posc, chunk_len);
while (left > 0) {
audio_pts =
get_audio_pts (this, audio_stream, audio->block_no,
audio->audio_tot - chunk_len, chunk_len - left);
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
/* read audio */
buf->pts = audio_pts;
lprintf("audio pts: %" PRId64 "\n", audio_pts);
if (left > this->audio_fifo->buffer_pool_buf_size) {
buf->size = this->audio_fifo->buffer_pool_buf_size;
buf->decoder_flags = 0;
} else {
buf->size = left;
buf->decoder_flags = BUF_FLAG_FRAME_END;
}
left -= buf->size;
if (this->input->read(this->input, buf->mem, buf->size) != buf->size) {
buf->free_buffer (buf);
return 0;
}
buf->extra_info->input_time = audio_pts / 90;
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->type = audio->audio_type | audio_stream;
this->audio_fifo->put (this->audio_fifo, buf);
}
audio->audio_posc++;
/* VBR streams (hack from mplayer) */
if (audio->wavex && audio->wavex->nBlockAlign) {
audio->block_no += (chunk_len + audio->wavex->nBlockAlign - 1) / audio->wavex->nBlockAlign;
} else {
audio->block_no += 1;
}
break;
case AVI_HEADER_VIDEO:
left = chunk_len;
lprintf("AVI_HEADER_VIDEO: chunk %d, len=%d\n", this->avi->video_posf, chunk_len);
while (left > 0) {
video_pts = get_video_pts (this, this->avi->video_posf);
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
/* read video */
buf->pts = video_pts;
lprintf("video pts: %" PRId64 "\n", video_pts);
if (left > this->video_fifo->buffer_pool_buf_size) {
buf->size = this->video_fifo->buffer_pool_buf_size;
buf->decoder_flags = 0;
} else {
buf->size = left;
buf->decoder_flags = BUF_FLAG_FRAME_END;
}
left -= buf->size;
if (this->input->read(this->input, buf->mem, buf->size) != buf->size) {
buf->free_buffer (buf);
return 0;
}
buf->type = this->avi->video_type;
buf->extra_info->input_time = video_pts / 90;
buf->extra_info->input_normpos = 65535;
buf->extra_info->frame_number = this->avi->video_posf;
buf->decoder_flags |= decoder_flags;
this->video_fifo->put (this->video_fifo, buf);
}
this->avi->video_posf++;
break;
case AVI_HEADER_UNKNOWN:
current_pos = this->input->get_current_pos(this->input);
if (this->input->seek(this->input, chunk_len, SEEK_CUR) != (current_pos + chunk_len)) {
return 0;
}
break;
}
/* skip padding */
this->input->seek (this->input,
this->input->get_current_pos(this->input) & 1, SEEK_CUR);
lprintf("done\n");
return 1;
}
static int demux_avi_seek_internal (demux_avi_t *this);
static int demux_avi_send_chunk (demux_plugin_t *this_gen) {
demux_avi_t *this = (demux_avi_t *) this_gen;
if (this->streaming) {
if (!demux_avi_next_streaming (this, 0)) {
this->status = DEMUX_FINISHED;
}
} else {
if (this->seek_request) {
this->seek_request = 0;
demux_avi_seek_internal(this);
}
if (!demux_avi_next (this, 0)) {
this->status = DEMUX_FINISHED;
}
}
return this->status;
}
static void demux_avi_dispose (demux_plugin_t *this_gen) {
demux_avi_t *this = (demux_avi_t *) this_gen;
if (this->avi)
AVI_close (this->avi);
free(this);
}
static int demux_avi_get_status (demux_plugin_t *this_gen) {
demux_avi_t *this = (demux_avi_t *) this_gen;
return this->status;
}
static void demux_avi_send_headers (demux_plugin_t *this_gen) {
demux_avi_t *this = (demux_avi_t *) this_gen;
int i;
lprintf("start\n");
this->video_fifo = this->stream->video_fifo;
this->audio_fifo = this->stream->audio_fifo;
this->status = DEMUX_OK;
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH, this->avi->width);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT, this->avi->height);
if (this->stream->xine->verbosity >= XINE_VERBOSITY_DEBUG) {
for (i=0; i < this->avi->n_audio; i++)
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_avi: audio format[%d] = 0x%x\n",
i, this->avi->audio[i]->wavex->wFormatTag);
}
this->no_audio = 0;
if (!this->avi->bih->biCompression)
this->avi->video_type = BUF_VIDEO_RGB;
else
this->avi->video_type = _x_fourcc_to_buf_video(this->avi->bih->biCompression);
for(i=0; i < this->avi->n_audio; i++) {
this->avi->audio[i]->audio_type = _x_formattag_to_buf_audio (this->avi->audio[i]->wavex->wFormatTag);
/* special case time: An AVI file encoded with Xan video will have Xan
* DPCM audio marked as PCM; hack around this */
if (this->avi->video_type == BUF_VIDEO_XXAN) {
this->avi->audio[i]->audio_type = BUF_AUDIO_XAN_DPCM;
this->avi->audio[i]->dwRate = 11025; /* why this ??? */
}
if( !this->avi->audio[i]->audio_type ) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "unknown audio type 0x%x\n",
this->avi->audio[i]->wavex->wFormatTag);
this->no_audio = 1;
this->avi->audio[i]->audio_type = BUF_AUDIO_UNKNOWN;
} else
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_avi: audio type %s (wFormatTag 0x%x)\n",
_x_buf_audio_name(this->avi->audio[i]->audio_type),
(int)this->avi->audio[i]->wavex->wFormatTag);
}
/*
* I don't know who should we trust more, if avi->compressor or bih->biCompression.
* however, at least for this case (compressor: xvid biCompression: DIVX), the
* xvid fourcc must prevail as it is used by ffmpeg to detect encoder bugs. [MF]
*/
if( _x_fourcc_to_buf_video(this->avi->compressor) == BUF_VIDEO_XVID &&
_x_fourcc_to_buf_video(this->avi->bih->biCompression) == BUF_VIDEO_MPEG4 ) {
this->avi->bih->biCompression = this->avi->compressor;
this->avi->video_type = BUF_VIDEO_XVID;
}
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 1);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, !this->no_audio);
_x_meta_info_set(this->stream, XINE_META_INFO_VIDEOCODEC, _x_buf_video_name(this->avi->video_type));
if (this->avi->audio[0] && !this->no_audio)
_x_meta_info_set(this->stream, XINE_META_INFO_AUDIOCODEC, _x_buf_audio_name(this->avi->audio[0]->audio_type));
/*
* send start/header buffers
*/
{
buf_element_t *buf;
int i;
_x_demux_control_start (this->stream);
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
if (this->avi->bih->biSize > buf->max_size) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_avi: private video decoder data length (%d) is greater than fifo buffer length (%d)\n",
this->avi->bih->biSize, buf->max_size);
buf->free_buffer(buf);
this->status = DEMUX_FINISHED;
return;
}
/* wait, before sending out the video header, one more special case hack:
* if the video type is RGB, indicate that it is upside down with a
* negative height */
if (this->avi->video_type == BUF_VIDEO_RGB) {
this->avi->bih->biHeight = -this->avi->bih->biHeight;
}
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAMERATE|
BUF_FLAG_FRAME_END;
buf->decoder_info[0] = this->video_step;
memcpy (buf->content, this->avi->bih, this->avi->bih->biSize);
buf->size = this->avi->bih->biSize;
if (this->avi->video_type) {
this->avi->compressor = this->avi->bih->biCompression;
} else {
this->avi->video_type = _x_fourcc_to_buf_video(this->avi->compressor);
}
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_FOURCC,
this->avi->compressor);
if (!this->avi->video_type) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_avi: unknown video codec '%.4s'\n",
(char*)&this->avi->bih->biCompression);
this->avi->video_type = BUF_VIDEO_UNKNOWN;
}
buf->type = this->avi->video_type;
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_avi: video codec is '%s'\n",
_x_buf_video_name(buf->type));
this->video_fifo->put (this->video_fifo, buf);
/* send off the palette, if there is one */
if (this->avi->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] = this->avi->palette_count;
buf->decoder_info_ptr[2] = &this->avi->palette;
buf->size = 0;
buf->type = this->avi->video_type;
this->video_fifo->put (this->video_fifo, buf);
}
if (this->audio_fifo) {
for (i=0; i<this->avi->n_audio; i++) {
avi_audio_t *a = this->avi->audio[i];
uint32_t todo = a->wavex_len;
uint32_t done = 0;
while (todo) {
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
if (todo > buf->max_size) {
buf->size = buf->max_size;
} else {
buf->size = todo;
}
todo -= buf->size;
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER;
if (todo == 0)
buf->decoder_flags |= BUF_FLAG_FRAME_END;
memcpy (buf->content, (uint8_t *)a->wavex + done, buf->size);
buf->type = a->audio_type | i;
buf->decoder_info[0] = 0; /* first package, containing wavex */
buf->decoder_info[1] = a->wavex->nSamplesPerSec; /* Audio Rate */
buf->decoder_info[2] = a->wavex->wBitsPerSample; /* Audio bits */
buf->decoder_info[3] = a->wavex->nChannels; /* Audio channels */
this->audio_fifo->put (this->audio_fifo, buf);
done += buf->size;
}
}
if(this->avi->n_audio == 1)
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_FOURCC,
this->avi->audio[0]->wavex->wFormatTag);
}
/*
* send preview buffers
*/
AVI_seek_start (this->avi);
if (!this->streaming) {
for (i=0; i<NUM_PREVIEW_BUFFERS; i++) {
if (!demux_avi_next(this, BUF_FLAG_PREVIEW))
break;
}
}
lprintf("done\n");
}
}
/*
* Seeking can take a lot of time due to the index reconstruction.
* To avoid to block the frontend thread during a seek, the real seek is done
* by the demux_loop thread (see demux_send_chunk).
* This way index reconstruction can be interrupted by a stop() or an other
* seek().
*/
static int demux_avi_seek (demux_plugin_t *this_gen,
off_t start_pos, int start_time, int playing) {
demux_avi_t *this = (demux_avi_t *) this_gen;
if (!this->streaming) {
_x_demux_flush_engine (this->stream);
this->seek_request = 1;
this->seek_start_pos = start_pos;
this->seek_start_time = start_time;
this->status = DEMUX_OK;
}
return this->status;
}
static int demux_avi_seek_internal (demux_avi_t *this) {
int64_t video_pts = 0, max_pos, min_pos = 0, cur_pos;
video_index_entry_t *vie = NULL;
int64_t audio_pts;
off_t start_pos = this->seek_start_pos;
int start_time = this->seek_start_time;
start_pos = (off_t) ( (double) start_pos / 65535 *
this->input->get_length (this->input) );
this->status = DEMUX_OK;
if (this->streaming)
return this->status;
AVI_seek_start (this->avi);
/*
* seek to start pos / time
*/
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
"start pos is %" PRIdMAX ", start time is %d\n", (intmax_t)start_pos,
start_time);
/* Seek video. We do a single idx_grow at the beginning rather than
* incrementally growing the index in a loop, so that if the index
* grow is going to take a while, the user is notified via the OSD
* (which only shows up if >= 1000 index entries are added at a time). */
/* We know for sure the last index entry is past our starting
* point; find the lowest index entry that's past our starting
* point. */
min_pos = 0;
if (start_pos) {
idx_grow(this, start_pos_stopper, &start_pos);
} else if (start_time) {
video_pts = start_time * 90;
idx_grow(this, start_time_stopper, &video_pts);
}
if (start_pos || start_time)
max_pos = this->avi->video_idx.video_frames - 1;
else
max_pos=0;
cur_pos = this->avi->video_posf;
if (max_pos < 0) {
this->status = DEMUX_FINISHED;
return this->status;
} else if (start_pos) {
while (min_pos < max_pos) {
this->avi->video_posf = cur_pos = (min_pos + max_pos) / 2;
if (cur_pos == min_pos) break;
vie = video_cur_index_entry(this);
if (vie->pos >= start_pos) {
max_pos = cur_pos;
} else {
min_pos = cur_pos;
}
}
} else if (start_time) {
while (min_pos < max_pos) {
this->avi->video_posf = cur_pos = (min_pos + max_pos) / 2;
if (cur_pos == min_pos) break;
vie = video_cur_index_entry(this);
if (get_video_pts (this, cur_pos) >= video_pts) {
max_pos = cur_pos;
} else {
min_pos = cur_pos;
}
}
}
while (vie && !(vie->flags & AVIIF_KEYFRAME) && cur_pos) {
this->avi->video_posf = --cur_pos;
vie = video_cur_index_entry(this);
}
if (!vie || !(vie->flags & AVIIF_KEYFRAME)) {
lprintf ("No previous keyframe found\n");
}
video_pts = get_video_pts (this, cur_pos);
/* Seek audio. We can do this incrementally, on the theory that the
* audio position we're looking for will be pretty close to the video
* position we've already found, so we won't be seeking though the
* file much at this point. */
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "video_pts = %" PRId64 "\n", video_pts);
/* FIXME ? */
audio_pts = 77777777;
if (!this->no_audio && this->status == DEMUX_OK) {
audio_index_entry_t *aie;
int i;
for(i = 0; i < this->avi->n_audio; i++) {
max_pos = this->avi->audio[i]->audio_idx.audio_chunks - 1;
min_pos = 0;
lprintf("audio_chunks=%d, min=%" PRId64 ", max=%" PRId64 "\n", this->avi->audio[i]->audio_idx.audio_chunks, min_pos, max_pos);
while (min_pos < max_pos) {
cur_pos = this->avi->audio[i]->audio_posc = (max_pos + min_pos) / 2;
if (cur_pos == min_pos) break;
aie = audio_cur_index_entry(this, this->avi->audio[i]);
if (aie) {
if ( (audio_pts = get_audio_pts(this, i, aie->block_no, aie->tot, 0)) >= video_pts) {
max_pos = cur_pos;
} else {
min_pos = cur_pos;
}
lprintf ("audio_pts = %" PRId64 " %" PRId64 " < %" PRId64 " < %" PRId64 "\n",
audio_pts, min_pos, cur_pos, max_pos);
} else {
if (cur_pos > min_pos) {
max_pos = cur_pos;
} else {
this->status = DEMUX_FINISHED;
lprintf ("audio seek to start failed\n");
break;
}
}
}
lprintf ("audio_pts = %" PRId64 "\n", audio_pts);
/*
* try to make audio pos more accurate for long index entries
*
* yeah, i know this implementation is pathetic (gb)
*/
if ((audio_pts > video_pts) && (this->avi->audio[i]->audio_posc>0))
this->avi->audio[i]->audio_posc--;
aie = audio_cur_index_entry(this, this->avi->audio[i]);
if (aie) {
while ((this->avi->audio[i]->audio_posb < aie->len)
&& ((audio_pts = get_audio_pts(this, i, aie->block_no, aie->tot,
this->avi->audio[i]->audio_posb)) < video_pts))
this->avi->audio[i]->audio_posb++;
}
}
}
lprintf ("video posc: %d\n", this->avi->video_posf);
this->send_newpts = 1;
this->buf_flag_seek = 1;
_x_demux_control_newpts (this->stream, video_pts, BUF_FLAG_SEEK);
return this->status;
}
static int demux_avi_get_stream_length (demux_plugin_t *this_gen) {
demux_avi_t *this = (demux_avi_t *) this_gen;
if (this->avi) {
if (this->streaming) {
return (int)(get_video_pts(this, this->avi->video_posf) / 90);
} else {
return (int)(get_video_pts(this, this->avi->video_idx.video_frames) / 90);
}
}
return 0;
}
static uint32_t demux_avi_get_capabilities(demux_plugin_t *this_gen) {
return DEMUX_CAP_NOCAP;
}
static int demux_avi_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_avi_t *this;
switch (stream->content_detection_method) {
case METHOD_BY_CONTENT: {
uint8_t buf[12];
if (input->get_capabilities(input) & INPUT_CAP_BLOCK)
return NULL;
if (_x_demux_read_header(input, buf, 12) != 12)
return NULL;
if( !( (strncasecmp(buf ,"ON2 ",4) == 0 &&
strncasecmp(buf+8,"ON2f",4) == 0) ||
(strncasecmp(buf ,"RIFF",4) == 0 &&
strncasecmp(buf+8,"AVI ",4) == 0) ) )
return NULL;
}
break;
case METHOD_BY_EXTENSION: {
const char *extensions, *mrl;
mrl = input->get_mrl (input);
extensions = class_gen->get_extensions (class_gen);
if (!_x_demux_check_extension (mrl, extensions))
return NULL;
}
/* we want to fall through here */
case METHOD_EXPLICIT:
break;
default:
return NULL;
}
this = calloc(1, sizeof(demux_avi_t));
this->stream = stream;
this->input = input;
this->demux_plugin.send_headers = demux_avi_send_headers;
this->demux_plugin.send_chunk = demux_avi_send_chunk;
this->demux_plugin.seek = demux_avi_seek;
this->demux_plugin.dispose = demux_avi_dispose;
this->demux_plugin.get_status = demux_avi_get_status;
this->demux_plugin.get_stream_length = demux_avi_get_stream_length;
this->demux_plugin.get_capabilities = demux_avi_get_capabilities;
this->demux_plugin.get_optional_data = demux_avi_get_optional_data;
this->demux_plugin.demux_class = class_gen;
this->status = DEMUX_FINISHED;
if (!INPUT_IS_SEEKABLE(input)) {
xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "streaming mode\n");
this->streaming = 1;
}
this->avi = AVI_init (this);
if (!this->avi) {
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"AVI_init failed (AVI_errno: %d)\n", this->AVI_errno);
free (this);
return NULL;
}
xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
"demux_avi: %d frames\n", this->avi->video_idx.video_frames);
return &this->demux_plugin;
}
/*
* demux avi class
*/
static const char *get_description (demux_class_t *this_gen) {
return "AVI/RIFF demux plugin";
}
static const char *get_identifier (demux_class_t *this_gen) {
return "AVI";
}
static const char *get_extensions (demux_class_t *this_gen) {
return "avi";
}
static const char *get_mimetypes (demux_class_t *this_gen) {
return "video/msvideo: avi: AVI video;"
"video/x-msvideo: avi: AVI video;";
}
static void class_dispose (demux_class_t *this_gen) {
demux_avi_class_t *this = (demux_avi_class_t *) this_gen;
free (this);
}
static void *init_class (xine_t *xine, void *data) {
demux_avi_class_t *this;
this = calloc(1, sizeof(demux_avi_class_t));
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_avi = {
10 /* priority */
};
const plugin_info_t xine_plugin_info[] EXPORTED = {
/* type, API, "name", version, special_info, init_function */
{ PLUGIN_DEMUX, 26, "avi", XINE_VERSION_CODE, &demux_info_avi, init_class },
{ PLUGIN_NONE, 0, "", 0, NULL, NULL }
};