rustsynth-sys 0.5.6

Low level bindings to VapourSynth
Documentation
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
// avfsavi.cpp : Avisynth Virtual File System
//
// Avisynth v2.5.  Copyright 2008-2010 Ben Rudiak-Gould et al.
// http://www.avisynth.org

// This program 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.
//
// This program 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., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
// http://www.gnu.org/copyleft/gpl.html .
//
// Linking Avisynth statically or dynamically with other modules is making a
// combined work based on Avisynth.  Thus, the terms and conditions of the GNU
// General Public License cover the whole combination.
//
// As a special exception, the copyright holders of Avisynth give you
// permission to link Avisynth with independent modules that communicate with
// Avisynth solely through the interfaces defined in avisynth.h, regardless of
// the license terms of these independent modules, and to copy and distribute
// the resulting combined work under terms of your choice, provided that
// every copy of the combined work is accompanied by a complete copy of
// the source code of Avisynth (the version of Avisynth used to produce the
// combined work), being distributed under the terms of the GNU General
// Public License plus this exception.  An independent module is a module
// which is not derived from or based on Avisynth, such as 3rd-party filters,
// import and export plugins, or graphical user interfaces.



//----------------------------------------------------------------------------
// Implementation Notes.
//
// The AVI v2 file format is a sequence of RIFF segments appended
// end to end. Each segment is formatted the same as a single RIFF
// file, which limits the segment size to 4GB. For compatibility
// it is best to limit the actual max segment size to 1GB. The first
// segment contains the full AVI headers including a "super" index
// that can be used to find the location of any chunk of audio/video
// data.
//
// The AVI2 index must be broken into chunks to handle >4GB file
// sizes. The header of each index chunk has a 64 bit base offset.
// Each index entry has a 32 bit offset which is combined with
// the base offset to get the actual 64 bit file offset.
//
// This implementation creates an index chunk at the end of each
// segment that indexes the contents of the one segment. The
// number of audo/video data chunks in all the segments is the
// same, except for the final segment which is the same or
// smaller. This has the effect of making the index chunks all
// the same size, except for the last.
//
// The AVI and AVI2 file formats allow full flexibility over the
// placement of audio data in the file. However, applications that
// read AVI files can be pretty picky. This implementation
// interleaves audio data per video frame, creating the same number
// of audio chunks as video chunks.
//
//----------------------------------------------------------------------------

#include "avfsincludes.h"

#define AVI_INDEX_OF_INDEXES       0x00
#define AVI_INDEX_OF_CHUNKS        0x01
#define AVI_INDEX_SUB_DEFAULT      0x00
#define AVI_INDEX_SUB_2FIELD       0x01

#define AVIF_HASINDEX        0x00000010
#define AVIF_MUSTUSEINDEX    0x00000020
#define AVIF_ISINTERLEAVED   0x00000100

#ifndef AVIIF_LIST
# define AVIIF_LIST       0x00000001
# define AVIIF_KEYFRAME   0x00000010
#endif

unsigned RiffAlignUp(unsigned size)  // Align size to 16bits
{
   return (size+(2-1))&~(2-1);
}

// RIFFCHUNK - 'JUNK', 'strf' etc

struct RiffTag
{
   uint32_t fcc;
   uint32_t cb;
};

/*  typedef struct _rifflist
    {
      FOURCC fcc;
      DWORD cb;
      FOURCC fccListType;
    } RIFFLIST, * LPRIFFLIST; */

// RIFFLIST  - 'RIFF', 'LIST'

static const uint32_t riffFcc     = MAKETAGUINT32('R','I','F','F');
static const uint32_t riffLstFcc  = MAKETAGUINT32('L','I','S','T');
static const uint32_t riffJunkFcc = MAKETAGUINT32('J','U','N','K');

struct RiffLst {
  RiffTag tag;
  uint32_t fcc;
};

static const uint32_t avi2MaxSegSize    = 0x3FFFFFFE;
static const uint32_t avi2Max4GbSegSize = 0xFFFFFFFE;

static const uint32_t avi2FileFcc    = MAKETAGUINT32('A','V','I',' ');
static const uint32_t avi2SegLstFcc  = MAKETAGUINT32('A','V','I','X');
static const uint32_t avi2HdrLstFcc  = MAKETAGUINT32('h','d','r','l');
static const uint32_t avi2DataLstFcc = MAKETAGUINT32('m','o','v','i');


// AVIMAINHEADER - 'avih'

static const uint32_t avi2MainHdrFcc = MAKETAGUINT32('a','v','i','h');

struct Avi2MainHdr {
  RiffTag tag;
  uint32_t dwMicroSecPerFrame;
  uint32_t dwMaxBytesPerSec;
  uint32_t dwPaddingGranularity;
  uint32_t dwFlags;
  uint32_t dwTotalFrames;
  uint32_t dwInitialFrames;
  uint32_t dwStreams;
  uint32_t dwSuggestedBufferSize;
  uint32_t dwWidth;
  uint32_t dwHeight;
  uint8_t  reserved1[16];
};

// AVISTREAMHEADER - 'strh'

static const uint32_t avi2StrHdrFcc = MAKETAGUINT32('s','t','r','h');

struct Avi2StrHdr {
  RiffTag tag;
  uint32_t fccType;
  uint32_t fccHandler;
  uint32_t dwFlags;
  uint16_t wPriority;
  uint16_t wLanguage;
  uint32_t dwInitialFrames;
  uint32_t dwScale;
  uint32_t dwRate;       // dwRate/dwScale is stream tick rate in ticks/sec
  uint32_t dwStart;
  uint32_t dwLength;
  uint32_t dwSuggestedBufferSize;
  uint32_t dwQuality;
  uint32_t dwSampleSize;
  int16_t  frameLeft;
  int16_t  frameTop;
  int16_t  frameRight;
  int16_t  frameBottom;
};

// RIFFCHUNK + BITMAPINFOHEADER - 'strf'

static const uint32_t avi2VidFrmtFcc = MAKETAGUINT32('s','t','r','f');

struct Avi2VidFrmt {
  RiffTag tag;
  uint32_t biSize;
  int32_t  biWidth;
  int32_t  biHeight;
  uint16_t biPlanes;
  uint16_t biBitCount;
  uint32_t biCompression;
  uint32_t biSizeImage;
  int32_t  biXPelsPerMeter;
  int32_t  biYPelsPerMeter;
  uint32_t biClrUsed;
  uint32_t biClrImportant;
};

static const uint32_t avi2VidHdrLstFcc = MAKETAGUINT32('s','t','r','l');
static const uint32_t avi2VidStrTypeFcc = MAKETAGUINT32('v','i','d','s');

// RIFFCHUNK + PCMWAVEFORMAT or (WAVEFORMAT + WORD) - 'strf'

static const uint32_t avi2AudFrmtFcc = MAKETAGUINT32('s','t','r','f');

struct Avi2AudFrmt {
  RiffTag tag;
  WaveFormatExtensible wfx;
};

static const uint32_t avi2AudHdrLstFcc     = MAKETAGUINT32('s','t','r','l');
static const uint32_t avi2AudStrTypeFcc    = MAKETAGUINT32('a','u','d','s');

static const uint32_t avi2IndxFcc = MAKETAGUINT32('i','n','d','x');

struct Avi2IndxHdr {
  RiffTag tag;
  uint16_t wLongsPerEntry;
  uint8_t  bIndxSubType;
  uint8_t  bIndxType;
  uint32_t nEntriesInUse;
  uint32_t dwChunkId;
  uint32_t qwBaseOffsetLow;
  uint32_t qwBaseOffsetHigh;
  uint8_t reserved1[4];
};
static_assert(sizeof(Avi2IndxHdr) == 32, "");


// hdr.wLongsPerEntry = sizeof(Avi2SuperIndxEntry)/4
// hdr.bIndxSubType = AVI_INDEX_SUB_DEFAULT or AVI_INDEX_SUB_2FIELD
// hdr.bIndxType = AVI_INDEX_OF_INDEXES
// hdr.qwBaseOffset = 0
struct Avi2SuperIndxEntry {
  uint32_t qwOffsetLow;
  uint32_t qwOffsetHigh;
  uint32_t dwSize;
  uint32_t dwDuration;
};

static const unsigned avfsAvi2MaxSuperIndxEntryCount = 5000;

// AVISUPERINDEX - 'indx'

struct AvfsAvi2SuperIndx {
  Avi2IndxHdr hdr;
  Avi2SuperIndxEntry ents[avfsAvi2MaxSuperIndxEntryCount];
};


// hdr.wLongsPerEntry = sizeof(Avi2IndxEntry)/4
// hdr.bIndxType = AVI_INDEX_OF_CHUNKS
// hdr.dwChunkId = avi2AudFcc or avi2VidFcc
struct Avi2IndxEntry {
  uint32_t dwOffset;
  uint32_t dwSize;
};

// AVISTDINDEX - 'ix00', 'ix01'

struct AvfsAvi2Indx {
  Avi2IndxHdr hdr;
  Avi2IndxEntry ents[1/*hdr.nEntriesInUse*/];
};

// AVIEXTHEADER - 'dmlh'

static const uint32_t avi2ExtHdrFcc = MAKETAGUINT32('d','m','l','h');

struct Avi2ExtHdr {
  RiffTag tag;
  uint32_t dwGrandFrames;
  uint8_t reserved[244];
};

// RIFFLIST - 'odml'

static const uint32_t avi2ExtHdrLstFcc = MAKETAGUINT32('o','d','m','l');

struct Avi2ExtHdrLst {
  RiffLst lst;
  Avi2ExtHdr hdr;
};

// AVIOLDINDEX - 'idx1'

static const uint32_t avi2OldIndxFcc = MAKETAGUINT32('i','d','x','1');

struct Avi2OldIndxEntry {
  uint32_t dwChunkId;
  uint32_t dwFlags;
  uint32_t dwOffset;
  uint32_t dwSize;
};

// {video} = RIFFLIST + AVISTREAMHEADER + RIFFCHUNK + BITMAPINFOHEAD + AVISUPERINDEX
//         - 'LIST', 'strl', 'strh', 'vids', 'indx'

struct AvfsAvi2VidHdrLst {
  RiffLst lst;
  Avi2StrHdr hdr;
  Avi2VidFrmt vidFrmt;
  AvfsAvi2SuperIndx indx;
};

// {audio} = RIFFLIST + AVISTREAMHEADER + RIFFCHUNK + PCMWAVEFORMAT + AVISUPERINDEX
//         - 'LIST', 'strl', 'strh', 'auds', 'indx'

struct AvfsAvi2AudHdrLst {
  RiffLst lst;
  Avi2StrHdr hdr;
  Avi2AudFrmt audFrmt;
  AvfsAvi2SuperIndx indx;
};

// RIFFCHUNK - 'JUNK'

struct AvfsAvi2HdrJunk {
  Avi2IndxHdr hdr;
  uint8_t junk[10*1024];
};

// RIFFLIST + AVIMAINHEADER + {video} + {audio} + RIFFLIST + AVIEXTHEADER
//         - 'LIST' 'hdrl' 'avih' {} {} 'LIST' 'odml' 'dmlh'

struct AvfsAvi2HdrLst {
  RiffLst lst;
  Avi2MainHdr mainHdr;
  AvfsAvi2VidHdrLst vidLst;
  AvfsAvi2AudHdrLst audLst;
  Avi2ExtHdrLst extLst;
  AvfsAvi2HdrJunk junk;
};

// RIFFCHUNK - '01wb'

struct AvfsAvi2Aud {
  RiffTag tag;
  uint8_t data[1];
};

// RIFFCHUNK - '00db' DIB or '00dc' Compressed

struct AvfsAvi2Vid {
  RiffTag tag;
  uint8_t data[1];
};

// RIFFLIST + {data} - 'LIST', 'movi', ...

struct AvfsAvi2DataLst {
  RiffLst lst;
  uint8_t data[1];
  // AvfsAvi2Aud frame0Aud;
  // AvfsAvi2Vid frame0Vid;
  // ...
  // AvfsAvi2Aud frameNAud;
  // AvfsAvi2Vid frameNVid;
  // AvfsAvi2Indx vidIndx;
  // AvfsAvi2Indx audIndx;
};

// AVIOLDINDEX - 'idx1'

struct AvfsAvi2OldIndx {
  RiffTag tag;
  Avi2OldIndxEntry ents[1/*hdr.cb/sizeof(ents[0])*/];
};

// RIFFLIST + ... - 'RIFF', 'AVI ', ...

struct AvfsAvi2Seg0 {
  RiffLst lst;
  AvfsAvi2HdrLst hdrLst;
  AvfsAvi2DataLst dataLst;
  // AvfsAvi2OldIndx oldIndx;
};

// RIFFLIST + RIFFLIST - 'RIFF', 'AVIX', 'LIST', 'movi'

struct AvfsAvi2SegN {
  RiffLst lst;
  AvfsAvi2DataLst dataLst;
};

// struct AvfsAvi2File {
//   AvfsAvi2Seg0 seg0;
//   AvfsAvi2SegN seg1;
//   ...
//   AvfsAvi2SegN segN;
// };

static const uint32_t avfsAvi2VidRgbFcc  = MAKETAGUINT32('0','0','d','b');
static const uint32_t avfsAvi2VidCompFcc = MAKETAGUINT32('0','0','d','c');
static const uint32_t avfsAvi2AudFcc     = MAKETAGUINT32('0','1','w','b');

static const uint32_t avfsAvi2VidIndxFcc = MAKETAGUINT32('i','x','0','0');
static const uint32_t avfsAvi2AudIndxFcc = MAKETAGUINT32('i','x','0','1');

static const unsigned avfsAvi2MaxDataLstSize = avi2MaxSegSize-
  offsetof(AvfsAvi2Seg0, dataLst.data)-offsetof(AvfsAvi2OldIndx, ents);

static const unsigned avfsAvi2Max4GbDataLstSize = avi2Max4GbSegSize-
  offsetof(AvfsAvi2Seg0, dataLst.data)-offsetof(AvfsAvi2OldIndx, ents);

struct AvfsAvi2File final: AvfsMediaFile_ {
  int references = 1;
  Synther_* avs = nullptr;
  Avisynther_* avssynther = nullptr;
  VapourSynther_* vssynther = nullptr;

  VideoInfoAdapter vi;

  uint32_t frameVidFcc = 0;
  unsigned frameAudHdrSize = 0;
  unsigned frameVidDataSize = 0;
  unsigned frameVidAlignSize = 0;
  unsigned vidFrameCount = 0;
  unsigned audFrameCount = 0;
  unsigned fileFrameCount = 0;
  unsigned durFrameCount = 0;
  unsigned sampleSize = 0;
  unsigned firstAudFramePackCount = 0;
  uint64_t fileSampleCount = 0;
  unsigned fileSegCount = 0;
  uint64_t fileSize = 0;

  enum { indxPrePadSize  = 0x20000 };
  enum { indxPostPadSize = 0x20000 };

  struct Seg {
    uint64_t startOffset;
    unsigned startFrame;
    unsigned vidFrameCount;
    unsigned audFrameCount;
    unsigned frameCount;
    unsigned lastAudFramePackCount;
    unsigned hdrSize;
    unsigned dataSize;
    unsigned vidIndxSize;
    unsigned audIndxSize;
    unsigned oldIndxSize;
    unsigned segSize;
    unsigned* frameIndx;
    AvfsAvi2Indx* vidIndx;
    AvfsAvi2Indx* audIndx;
    AvfsAvi2OldIndx* oldIndx;
    union {
      AvfsAvi2Seg0 seg0;
      AvfsAvi2SegN segN;
      uint8_t buf[1];
    } hdr;
  };
  Seg** segs = nullptr;
  uint8_t* sampleScratch = nullptr;

  AvfsAvi2File(Avisynther_* avs);
  AvfsAvi2File(VapourSynther_* vs);

  ~AvfsAvi2File(void);

  uint64_t/*start*/ LocateFrameSamples(
    unsigned frame,
    unsigned frameCount,
    unsigned* sampleCount);

  unsigned FrameSampleCount(unsigned frame) {
    unsigned sampleCount;
    LocateFrameSamples(frame, 1, &sampleCount);
    return sampleCount;
  }

  bool/*success*/ Init(AvfsLog_* log);

  void AddRef(void);
  void Release(void);

  bool/*success*/ GetFrameData(
    AvfsLog_* log,
    uint8_t* buffer,
    unsigned position,
    size_t offset,
    size_t size);

  bool/*success*/ ReadMedia(
    AvfsLog_* log,
    uint64_t fileOffset,
    void* buffer,
    size_t requestedSize);
};

AvfsAvi2File::AvfsAvi2File(Avisynther_* inAvs)
 :
  vi(inAvs->GetVideoInfo())
{
  avs = inAvs;
  avssynther = inAvs;
  avs->AddRef();
}

AvfsAvi2File::AvfsAvi2File(VapourSynther_* inVs)
  :
   vi(inVs->GetVideoInfo())
{
    avs = inVs;
    vssynther = inVs;
    avs->AddRef();
}

AvfsAvi2File::~AvfsAvi2File(void)
{
  ASSERT(!references);
  unsigned segi;
  avs->Release();
  if (segs) {
    for (segi = 0; segi < fileSegCount; segi ++) {
      if (segs[segi]) {
        free(segs[segi]);
      }
    }
    free(segs);
  }
  if (sampleScratch) {
    free(sampleScratch);
  }
}

// Return the starting Audio sample number for frame N
// and the number of samples in frameCount.
// Handles audio preload

uint64_t/*start*/ AvfsAvi2File::LocateFrameSamples(
  unsigned frame,
  unsigned frameCount,
  unsigned* outSampleCount)
{
  uint64_t startSample = 0;
  uint64_t endSample = 0;
  unsigned startFrame = frame;
  unsigned endFrame = frame+frameCount;
  if (startFrame != 0) {
    startFrame += firstAudFramePackCount;
  }
  if (endFrame != 0) {
    endFrame += firstAudFramePackCount;
  }
  if (fileSampleCount) {
    startSample = uint64_t(vi.AudioSamplesFromFrames(startFrame));
    if (startSample > fileSampleCount) {
      startSample = fileSampleCount;
    }
    endSample = uint64_t(vi.AudioSamplesFromFrames(endFrame));
    if (endSample > fileSampleCount) {
      endSample = fileSampleCount;
    }
  }
  if(outSampleCount) {
    *outSampleCount = unsigned(endSample-startSample);
  }
  return startSample;
}

bool/*success*/ AvfsAvi2File::Init(
  AvfsLog_* log)
{
  bool success = true;
  unsigned long vidType;
  unsigned long vidCompress;
  unsigned maxFrameAudDataSize = 0;
  uint16_t sampleType = 0;
  unsigned clippedSampleCount = 0xFFFFFFFF;
  unsigned maxSegFrameCount = 0;
  unsigned allocSize;
  unsigned segi;
  unsigned segSize;
  unsigned segStartFrame;
  unsigned segVidFrameCount;
  unsigned segAudFrameCount;
  unsigned segLastAudFramePackCount;
  unsigned segFrameCount;
  unsigned segDurFrameCount;
  unsigned segFrame;
  unsigned segHdrSize;
  unsigned segDataSize;
  unsigned segVidIndxSize;
  unsigned segAudIndxSize;
  unsigned segOldIndxSize;
  uint16_t bitsPerPixel;
  uint64_t duration;
  Seg* seg;
  uint8_t* buf;
  Avi2SuperIndxEntry* vidIndxEnts = 0;
  Avi2SuperIndxEntry* audIndxEnts = 0;
  Avi2OldIndxEntry* oldIndxEnts = 0;
  unsigned frameSampleCount;
  unsigned frameAudDataSize;
  unsigned oldi;
  const char* fourcc;
  bool noInterleave = false;

  // Setup video attributes.
  vidFrameCount = unsigned(vi.num_frames);
  frameVidDataSize = vi.BMPSize();
  bitsPerPixel = uint16_t(vi.BitsPerPixel());

  vidType = 0;
  vidCompress = 0;

  bool vidSuccess = true;

  if (vi.HasVideo()) {
      vidSuccess = vidSuccess && GetFourCC(vi.vf, vi.alt_output, vidType);
      vidSuccess = vidSuccess && GetBiCompression(vi.vf, vi.alt_output, vidCompress);
  }

  if (!vidType || !vidFrameCount || !frameVidDataSize || !vidSuccess) {
    vidType = 0;
    vidFrameCount = 0;
    frameVidDataSize = 0;
  } else {
    frameVidAlignSize = RiffAlignUp(frameVidDataSize)-frameVidDataSize;

    // Allow script to override fourcc code for the video. Non RGB
    // uncompressed AVI files are not very compatible, but specific
    // fourcc codes might help in some cases.

    fourcc = avs->GetVarAsString("AVFS_AVI_VidFcc", 0);
    if (sslen(fourcc) >= 4) {
      vidType = vidCompress = MAKETAGUINT32(
        (uint8_t)(fourcc[0]), (uint8_t)(fourcc[1]),
        (uint8_t)(fourcc[2]), (uint8_t)(fourcc[3]));
    }
  }

  if (vidType == MAKETAGUINT32('D','I','B',' '))
    frameVidFcc = avfsAvi2VidRgbFcc;       // 00db
  else
    frameVidFcc = avfsAvi2VidCompFcc;      // 00dc

  durFrameCount  = vidFrameCount;
  fileFrameCount = vidFrameCount;

  if (vi.HasAudio())
      sampleType = vi.AudioIsFloat() ? 2 : 1;

  int bytesPerOutputSample = (vi.BitsPerChannelSample() + 7) / 8;
  sampleSize = bytesPerOutputSample * vi.AudioChannels();
  fileSampleCount = unsigned(vi.num_audio_samples); // FIXME, how does avi handle more than 2^32-1 samples? there should probably be some erroring out in this case

  if (!sampleType || !sampleSize || !fileSampleCount) {
    sampleType = 0;
    sampleSize = 0;
    fileSampleCount = 0;
  }
  else {
    frameAudHdrSize = sizeof(RiffTag);
    maxFrameAudDataSize = RiffAlignUp(unsigned(vi.AudioSamplesFromFrames(1)+1)*sampleSize);
    // How many frames are needed to represent audio stream.
    audFrameCount = unsigned(vi.FramesFromAudioSamples(fileSampleCount+vi.AudioSamplesFromFrames(1)-1)); // ceil!

    // Fixme - prove this test is redundant!
    if (!audFrameCount) {
      audFrameCount = 1;
    }
    // Duration of AVI is in frames, larger of audio and video
    // streams.
    if (durFrameCount < audFrameCount) {
      durFrameCount = audFrameCount;
    }

    noInterleave = avs->GetVarAsBool("AVFS_AVI_NoInterleave", false);

    if (!noInterleave) {
      // Preload extra .5 sec worth of audio samples with first frame.
      firstAudFramePackCount = unsigned(vi.fps_numerator+vi.fps_denominator-1)/  // ceil!
         (unsigned(vi.fps_denominator)*2);
      while (audFrameCount && FrameSampleCount(audFrameCount-1) == 0) {
        audFrameCount --;
      }
    }

    if (audFrameCount > fileFrameCount) {
      fileFrameCount = audFrameCount;
    }
  }

  if (clippedSampleCount > fileSampleCount) {
    clippedSampleCount = unsigned(fileSampleCount);
  }

  // Fixme - No video AVIs although unusual are valid
  if (!vidFrameCount) {
    log->Printf(L"AvfsAviMediaInit: Clip has no supported video.\n");
    success = false;
  }
  else {
    // Calculate max number of frames that can go in a 4GB segment.
    maxSegFrameCount = (   avfsAvi2Max4GbDataLstSize
                         - firstAudFramePackCount*maxFrameAudDataSize
                         - indxPrePadSize
                         - indxPostPadSize
                       )
                     /
                       (   sizeof(RiffTag)
                         + maxFrameAudDataSize
                         + sizeof(RiffTag)
                         + frameVidDataSize
                         + frameVidAlignSize
                         + sizeof(Avi2IndxEntry)*2
                         + sizeof(Avi2OldIndxEntry)*2
                       );

    fileSegCount = (fileFrameCount+maxSegFrameCount-1)/maxSegFrameCount; // ceil!
    ASSERT(fileSegCount);

    if (/*fileSegCount > 1 &&*/ avs->GetVarAsBool("AVFS_AVI_SmallSegments", false))
    {
      // Break file into 1GB segments instead of 4GB segments. Slows
      // initialization of some applications (mplayer/mencoder, vlc), but
      // may improve compatibility.
      // Calculate max number of frames that can go in each segment and
      // still have the first segment size <1GB.
      maxSegFrameCount = (   avfsAvi2MaxDataLstSize
                           - firstAudFramePackCount*maxFrameAudDataSize
                           - indxPrePadSize
                           - indxPostPadSize
                         )
                       /
                         (   sizeof(RiffTag)
                           + maxFrameAudDataSize
                           + sizeof(RiffTag)
                           + frameVidDataSize
                           + frameVidAlignSize
                           + sizeof(Avi2IndxEntry)*2
                           + sizeof(Avi2OldIndxEntry)*2
                         );

      fileSegCount = (fileFrameCount+maxSegFrameCount-1)/maxSegFrameCount;
    }

    if (fileSegCount > avfsAvi2MaxSuperIndxEntryCount) {
      fileSegCount = avfsAvi2MaxSuperIndxEntryCount;
    }
  }

  if (fileSegCount) {
    segs = static_cast<Seg**>(malloc(sizeof(segs[0])*fileSegCount));
    if (!segs) {
      success = false;
      fileSegCount = 0;
    }
    else {
      memset(segs, 0, sizeof(segs[0])*fileSegCount);
    }
  }

  if (sampleSize) {
    sampleScratch = static_cast<uint8_t*>(malloc(sampleSize));
    if (!sampleScratch) {
      success = false;
    }
  }

  // Create header and index structures for each segment in the file.

  segStartFrame = 0;
  for (segi = 0; segi < fileSegCount; segi ++) {
    segFrameCount = fileFrameCount-segStartFrame;

    if (segFrameCount > maxSegFrameCount) {
      segFrameCount = maxSegFrameCount;
    }

    segVidFrameCount = 0;

    if (segStartFrame < vidFrameCount) {
      segVidFrameCount = vidFrameCount-segStartFrame;
    }

    if (segVidFrameCount > segFrameCount) {
      segVidFrameCount = segFrameCount;
    }

    segAudFrameCount = 0;
    segLastAudFramePackCount = 0;

    if (segStartFrame < audFrameCount) {
      segAudFrameCount = audFrameCount-segStartFrame;
    }

    if (segAudFrameCount > segFrameCount) {
      segAudFrameCount = segFrameCount;
    }

    segDurFrameCount = segFrameCount;

    if (segi+1 == fileSegCount) {
      // No more segments so remaining duration in this segment.
      segDurFrameCount = durFrameCount-segStartFrame;
    }

    // If in no-interleave mode then pack each segments audio data in
    // a single chunk.
    if (noInterleave && segAudFrameCount) {
      segLastAudFramePackCount = segAudFrameCount-1;
      segAudFrameCount = 1;
    }

    segHdrSize = offsetof(AvfsAvi2SegN, dataLst.data);

    if (segi == 0) {
      segHdrSize = offsetof(AvfsAvi2Seg0, dataLst.data);
    }

    segVidIndxSize = offsetof(AvfsAvi2Indx, ents)+
      segVidFrameCount*sizeof(Avi2IndxEntry);

    segAudIndxSize = 0;

    if (fileSampleCount) {
      segAudIndxSize = offsetof(AvfsAvi2Indx, ents)+
        segAudFrameCount*sizeof(Avi2IndxEntry);
    }

    segOldIndxSize = (segi == 0)*(offsetof(AvfsAvi2OldIndx, ents)+
        (segVidFrameCount+segAudFrameCount)*sizeof(Avi2OldIndxEntry));

    allocSize = offsetof(Seg, hdr)+segHdrSize+
      segFrameCount*sizeof(seg->frameIndx[0])+segVidIndxSize+segAudIndxSize+
      segOldIndxSize;

    seg = segs[segi] = static_cast<Seg*>(malloc(allocSize));

    if (!seg) {
      success = false;
    }
    else {
      memset(seg, 0, allocSize);

      seg->startOffset           = fileSize;
      seg->startFrame            = segStartFrame;
      seg->vidFrameCount         = segVidFrameCount;
      seg->audFrameCount         = segAudFrameCount;
      seg->frameCount            = segFrameCount;
      seg->lastAudFramePackCount = segLastAudFramePackCount;
      seg->hdrSize               = segHdrSize;
      seg->vidIndxSize           = segVidIndxSize;
      seg->audIndxSize           = segAudIndxSize;
      seg->oldIndxSize           = segOldIndxSize;

      buf            = seg->hdr.buf + segHdrSize;
      seg->frameIndx = reinterpret_cast<unsigned*>(buf);
      buf           += segFrameCount*sizeof(seg->frameIndx[0]);
      seg->vidIndx   = reinterpret_cast<AvfsAvi2Indx*>(buf);
      buf           += segVidIndxSize;
      seg->audIndx   = reinterpret_cast<AvfsAvi2Indx*>(buf);
      buf           += segAudIndxSize;
      seg->oldIndx   = reinterpret_cast<AvfsAvi2OldIndx*>(buf);

      oldIndxEnts = 0;

      if (segi == 0) {

        // First segment has full header, including super index to
        // locate the per-segment index chunks.
//                                               -- RIFFLIST
        seg->hdr.seg0.lst.tag.fcc                             = riffFcc;                 // 'RIFF'
        seg->hdr.seg0.lst.fcc                                 = avi2FileFcc;             // 'AVI '

//                                               -- RIFFLIST
        seg->hdr.seg0.hdrLst.lst.tag.fcc                      = riffLstFcc;              // 'LIST'
        seg->hdr.seg0.hdrLst.lst.tag.cb                       = sizeof(seg->hdr.seg0.hdrLst)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.lst.fcc                          = avi2HdrLstFcc;           // 'hdrl'

//                                               -- AVIMAINHEADER
        seg->hdr.seg0.hdrLst.mainHdr.tag.fcc                  = avi2MainHdrFcc;          // 'avih'
        seg->hdr.seg0.hdrLst.mainHdr.tag.cb                   = sizeof(seg->hdr.seg0.hdrLst.mainHdr)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.mainHdr.dwMicroSecPerFrame       = unsigned((uint64_t(1000000)*unsigned(vi.fps_denominator)+unsigned(vi.fps_numerator)/2)/unsigned(vi.fps_numerator));
        seg->hdr.seg0.hdrLst.mainHdr.dwFlags                  = AVIF_HASINDEX | AVIF_ISINTERLEAVED;
        seg->hdr.seg0.hdrLst.mainHdr.dwTotalFrames            = segDurFrameCount;
        seg->hdr.seg0.hdrLst.mainHdr.dwStreams                = 1+!!fileSampleCount;
     // seg->hdr.seg0.hdrLst.mainHdr.dwSuggestedBufferSize    = 0;
        seg->hdr.seg0.hdrLst.mainHdr.dwWidth                  = vi.width;
        seg->hdr.seg0.hdrLst.mainHdr.dwHeight                 = vi.height;

//                                               -- RIFFLIST
        seg->hdr.seg0.hdrLst.vidLst.lst.tag.fcc               = riffLstFcc;              // 'LIST'
        seg->hdr.seg0.hdrLst.vidLst.lst.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.vidLst)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.vidLst.lst.fcc                   = avi2VidHdrLstFcc;        // 'strl'

//                                               -- AVISTREAMHEADER
        seg->hdr.seg0.hdrLst.vidLst.hdr.tag.fcc               = avi2StrHdrFcc;           // 'strh'
        seg->hdr.seg0.hdrLst.vidLst.hdr.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.vidLst.hdr)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.vidLst.hdr.fccType               = avi2VidStrTypeFcc;       // 'vids'
        seg->hdr.seg0.hdrLst.vidLst.hdr.fccHandler            = vidType;
        seg->hdr.seg0.hdrLst.vidLst.hdr.dwScale               = vi.fps_denominator;
        seg->hdr.seg0.hdrLst.vidLst.hdr.dwRate                = vi.fps_numerator;
        seg->hdr.seg0.hdrLst.vidLst.hdr.dwLength              = vidFrameCount;
        seg->hdr.seg0.hdrLst.vidLst.hdr.dwSuggestedBufferSize = frameVidDataSize;
        seg->hdr.seg0.hdrLst.vidLst.hdr.dwQuality             = 0xFFFFFFFF;
        seg->hdr.seg0.hdrLst.vidLst.hdr.frameRight            = int16_t(vi.width);
        seg->hdr.seg0.hdrLst.vidLst.hdr.frameBottom           = int16_t(vi.height);

//                                               -- RIFFCHUNK
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.tag.fcc           = avi2VidFrmtFcc;          // 'strf'
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.tag.cb            = sizeof(seg->hdr.seg0.hdrLst.vidLst.vidFrmt)-sizeof(RiffTag);

//                                               -- BITMAPINFOHEAD
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biSize            = sizeof(seg->hdr.seg0.hdrLst.vidLst.vidFrmt)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biWidth           = vi.width;
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biHeight          = vi.height;
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biPlanes          = 1;
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biBitCount        = bitsPerPixel;
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biCompression     = vidCompress;
        seg->hdr.seg0.hdrLst.vidLst.vidFrmt.biSizeImage       = frameVidDataSize;

//                                               -- AVISUPERINDEX
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.tag.fcc          = avi2IndxFcc;             // 'indx'
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.tag.cb           = sizeof(seg->hdr.seg0.hdrLst.vidLst.indx)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.wLongsPerEntry   = sizeof(seg->hdr.seg0.hdrLst.vidLst.indx.ents[0])/4;
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.dwChunkId        = frameVidFcc;             // '00db' DIB or '00dc' Compressed
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.bIndxSubType     = AVI_INDEX_SUB_DEFAULT;
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.bIndxType        = AVI_INDEX_OF_INDEXES;
        seg->hdr.seg0.hdrLst.vidLst.indx.hdr.nEntriesInUse    = fileSegCount;

//                                               -- RIFFLIST
        seg->hdr.seg0.hdrLst.audLst.lst.tag.fcc               = fileSampleCount?riffLstFcc:riffJunkFcc; // 'LIST' or 'JUNK'
        seg->hdr.seg0.hdrLst.audLst.lst.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.audLst)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.audLst.lst.fcc                   = avi2AudHdrLstFcc;        // 'strl'

//                                               -- AVISTREAMHEADER
        seg->hdr.seg0.hdrLst.audLst.hdr.tag.fcc               = avi2StrHdrFcc;           // 'strh'
        seg->hdr.seg0.hdrLst.audLst.hdr.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.audLst.hdr)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.audLst.hdr.fccType               = avi2AudStrTypeFcc;       // 'auds'
     // seg->hdr.seg0.hdrLst.audLst.hdr.fccHandler            = ?;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwInitialFrames       = 1; // audio preload ?
        seg->hdr.seg0.hdrLst.audLst.hdr.dwScale               = sampleSize;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwRate                = vi.SamplesPerSecond()*sampleSize;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwLength              = clippedSampleCount;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwSuggestedBufferSize = unsigned(LocateFrameSamples(1, 1, 0)+1)*sampleSize;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwQuality             = 0xFFFFFFFF;
        seg->hdr.seg0.hdrLst.audLst.hdr.dwSampleSize          = sampleSize;

//                                               -- RIFFCHUNK
        seg->hdr.seg0.hdrLst.audLst.audFrmt.tag.fcc           = avi2AudFrmtFcc;          // 'strf'
        seg->hdr.seg0.hdrLst.audLst.audFrmt.tag.cb            = sizeof(seg->hdr.seg0.hdrLst.audLst.audFrmt)-sizeof(RiffTag);

//                                               -- WAVEFORMATEXTENSIBLE
        CreateWaveFormatExtensible(seg->hdr.seg0.hdrLst.audLst.audFrmt.wfx, sampleType == 2, vi.BitsPerChannelSample(), vi.SamplesPerSecond(), vi.ChannelLayout()); // fixme, no error check

//                                               -- AVISUPERINDEX
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.tag.fcc          = avi2IndxFcc;             // 'indx'
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.tag.cb           = sizeof(seg->hdr.seg0.hdrLst.audLst.indx)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.wLongsPerEntry   = sizeof(seg->hdr.seg0.hdrLst.audLst.indx.ents[0])/4;
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.dwChunkId        = avfsAvi2AudFcc;          // '01wb'
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.bIndxSubType     = AVI_INDEX_SUB_DEFAULT;
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.bIndxType        = AVI_INDEX_OF_INDEXES;
        seg->hdr.seg0.hdrLst.audLst.indx.hdr.nEntriesInUse    = fileSegCount;

//                                               -- RIFFLIST
        seg->hdr.seg0.hdrLst.extLst.lst.tag.fcc               = riffLstFcc;              // 'LIST'
        seg->hdr.seg0.hdrLst.extLst.lst.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.extLst)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.extLst.lst.fcc                   = avi2ExtHdrLstFcc;        // 'odml'

//                                               -- AVIEXTHEADER
        seg->hdr.seg0.hdrLst.extLst.hdr.tag.fcc               = avi2ExtHdrFcc;           // 'dmlh'
        seg->hdr.seg0.hdrLst.extLst.hdr.tag.cb                = sizeof(seg->hdr.seg0.hdrLst.extLst.hdr)-sizeof(RiffTag);
        seg->hdr.seg0.hdrLst.extLst.hdr.dwGrandFrames         = durFrameCount;

//                                               -- RIFFCHUNK
        seg->hdr.seg0.hdrLst.junk.hdr.tag.fcc                 = riffJunkFcc;             // 'JUNK'
        seg->hdr.seg0.hdrLst.junk.hdr.tag.cb                  = sizeof(seg->hdr.seg0.hdrLst.junk)-sizeof(RiffTag);

//                                               -- RIFFLIST
        seg->hdr.seg0.dataLst.lst.tag.fcc                     = riffLstFcc;              // 'LIST'
        seg->hdr.seg0.dataLst.lst.fcc                         = avi2DataLstFcc;          // 'movi'

        // Save ptr to super indexes in first segment header, to
        // allow filling in super index entries as per segment
        // index chunks are built.
        vidIndxEnts = seg->hdr.seg0.hdrLst.vidLst.indx.ents;
        audIndxEnts = seg->hdr.seg0.hdrLst.audLst.indx.ents;

        // First segment also has a legacy AVI v1 index for
        // compatibility with picky applications.
        oldIndxEnts = seg->oldIndx->ents;
      }
      else {

        // Non-first segments have only a minimal RIFF header, and
        // no legacy index.

//                                               -- RIFFLIST
        seg->hdr.segN.lst.tag.fcc                             = riffFcc;                 // 'RIFF'
        seg->hdr.segN.lst.fcc                                 = avi2SegLstFcc;           // 'AVIX'

//                                               -- RIFFLIST
        seg->hdr.segN.dataLst.lst.tag.fcc                     = riffLstFcc;              // 'LIST'
        seg->hdr.segN.dataLst.lst.fcc                         = avi2DataLstFcc;          // 'movi'
      }

      // Build the per segment indexes, audio, video, and an
      // internal frame index needed by the read logic. The
      // base offset for the audio and video indexes is the
      // offset of the RIFF header at the start of the segment.

      segSize = segHdrSize;
      oldi = 0;
      segDataSize = 0;
      for (segFrame = 0; segFrame < segFrameCount; segFrame ++) {

        // Since the per-frame audio chunk can vary in size, we need
        // a frame index so the read logic can quickly associate
        // file offsets back to frame.
        seg->frameIndx[segFrame] = segDataSize;

        if (segFrame < segAudFrameCount) {
          LocateFrameSamples(segStartFrame+segFrame,
              (segFrame+1 == segAudFrameCount) ? segLastAudFramePackCount+1 : 1,
                                                &frameSampleCount);
          ASSERT(frameSampleCount);
          frameAudDataSize = frameSampleCount*sampleSize;

          seg->audIndx->ents[segFrame].dwOffset = segSize+segDataSize + sizeof(RiffTag);
          seg->audIndx->ents[segFrame].dwSize   = frameAudDataSize;

          if (oldIndxEnts) {
            oldIndxEnts[oldi].dwChunkId = avfsAvi2AudFcc;         // '01wb'
            oldIndxEnts[oldi].dwFlags   = AVIIF_KEYFRAME;
            oldIndxEnts[oldi].dwOffset  = segSize+segDataSize;
            oldIndxEnts[oldi].dwSize    = frameAudDataSize;
            oldi ++;
          }
          segDataSize += sizeof(RiffTag)+RiffAlignUp(frameAudDataSize);
        }

        if (segFrame < segVidFrameCount) {
          seg->vidIndx->ents[segFrame].dwOffset = segSize+segDataSize+sizeof(RiffTag);
          seg->vidIndx->ents[segFrame].dwSize   = frameVidDataSize;

          if (oldIndxEnts) {
            oldIndxEnts[oldi].dwChunkId = frameVidFcc;
            oldIndxEnts[oldi].dwFlags   = AVIIF_KEYFRAME;
            oldIndxEnts[oldi].dwOffset  = segSize+segDataSize;
            oldIndxEnts[oldi].dwSize    = frameVidDataSize;
            oldi ++;
          }
          segDataSize += sizeof(RiffTag)+frameVidDataSize+frameVidAlignSize;
        }
      }
      seg->dataSize = segDataSize;
      segSize += segDataSize;

      // Junk pad before index, to keep cache block aligned reads of index
      // from overlapping audio/video data.
      segSize += indxPrePadSize;

      // Update the super index in the first segment header with the
      // location of this segments audio and video index chunks.

      seg->vidIndx->hdr.tag.fcc          = avfsAvi2VidIndxFcc;             // 'ix00'
      seg->vidIndx->hdr.tag.cb           = segVidIndxSize-sizeof(RiffTag);
      seg->vidIndx->hdr.wLongsPerEntry   = sizeof(seg->vidIndx->ents[0])/4;
      seg->vidIndx->hdr.bIndxType        = AVI_INDEX_OF_CHUNKS;
      seg->vidIndx->hdr.nEntriesInUse    = segVidFrameCount;
      seg->vidIndx->hdr.dwChunkId        = frameVidFcc;
      seg->vidIndx->hdr.qwBaseOffsetLow  = uint32_t(fileSize);
      seg->vidIndx->hdr.qwBaseOffsetHigh = uint32_t(fileSize>>32);

      vidIndxEnts[segi].qwOffsetLow  = uint32_t(fileSize+segSize);
      vidIndxEnts[segi].qwOffsetHigh = uint32_t((fileSize+segSize)>>32);
      vidIndxEnts[segi].dwSize       = offsetof(AvfsAvi2Indx, ents)+sizeof(seg->vidIndx->ents[0])*segFrameCount;
      vidIndxEnts[segi].dwDuration   = segVidFrameCount;

      segSize += segVidIndxSize;

      if (fileSampleCount) {
        LocateFrameSamples(segStartFrame, segAudFrameCount+segLastAudFramePackCount, &frameSampleCount);

        seg->audIndx->hdr.tag.fcc          = avfsAvi2AudIndxFcc;             // 'ix01'
        seg->audIndx->hdr.tag.cb           = segAudIndxSize-sizeof(RiffTag);
        seg->audIndx->hdr.wLongsPerEntry   = sizeof(seg->audIndx->ents[0])/4;
        seg->audIndx->hdr.bIndxType        = AVI_INDEX_OF_CHUNKS;
        seg->audIndx->hdr.nEntriesInUse    = segAudFrameCount;
        seg->audIndx->hdr.dwChunkId        = avfsAvi2AudFcc;         // '01wb'
        seg->audIndx->hdr.qwBaseOffsetLow  = uint32_t(fileSize);
        seg->audIndx->hdr.qwBaseOffsetHigh = uint32_t(fileSize>>32);

        audIndxEnts[segi].qwOffsetLow  = uint32_t(fileSize+segSize);
        audIndxEnts[segi].qwOffsetHigh = uint32_t((fileSize+segSize)>>32);
        audIndxEnts[segi].dwSize       = offsetof(AvfsAvi2Indx, ents)+sizeof(seg->audIndx->ents[0])*segFrameCount;
        audIndxEnts[segi].dwDuration   = frameSampleCount;

        segSize += segAudIndxSize;
      }

      // Update the data list (movi) header now that size is known.
      if (segi == 0) {
        seg->hdr.seg0.dataLst.lst.tag.cb  = segSize-offsetof(AvfsAvi2Seg0, dataLst)-sizeof(RiffTag);
      }
      else {
        seg->hdr.segN.dataLst.lst.tag.cb  = segSize-offsetof(AvfsAvi2SegN, dataLst)-sizeof(RiffTag);
      }

      // For the first segment update the segment header with
      // the final location of the legacy index.
      if (segOldIndxSize) {
        seg->oldIndx->tag.fcc = avi2OldIndxFcc;
        seg->oldIndx->tag.cb  = segOldIndxSize-sizeof(RiffTag);
        segSize += segOldIndxSize;
      }

      // Junk padding after index, to keep cache block aligned
      // reads of index data from overlapping audio/video data.
      segSize += indxPostPadSize;

      // Update the segment RIFF header with the final segment size.
      if (segi == 0) {
        seg->hdr.seg0.lst.tag.cb = segSize-sizeof(RiffTag);
      }
      else {
        seg->hdr.segN.lst.tag.cb = segSize-sizeof(RiffTag);
      }
      seg->segSize = segSize;
      ASSERT(RiffAlignUp(segSize) == segSize);
      ASSERT(segSize <= avi2Max4GbSegSize);

      fileSize += segSize;
    }
    segStartFrame += segFrameCount;
  }

  if (segs && segs[0]) {
    duration = (uint64_t(durFrameCount)*unsigned(vi.fps_denominator)+
      unsigned(vi.fps_numerator)/2)/unsigned(vi.fps_numerator);
    duration += !duration;
    segs[0]->hdr.seg0.hdrLst.mainHdr.dwMaxBytesPerSec = unsigned(fileSize/duration);
  }

  ASSERT(!success || segStartFrame == fileFrameCount);
  return success;
}

void AvfsAvi2File::AddRef(void)
{
  ASSERT(references);
  references ++;
}

void AvfsAvi2File::Release(void)
{
  ASSERT(references);
  if (!--references) {
    delete this;
  }
}

static void copyPlaneAvs(
  const VSVideoFormat &fi,
  uint8_t*     &buffer,
  size_t       &offset,
  size_t       &rqsize,
  avs::PVideoFrame  &frame,
  int          plane,
  unsigned     alignMask, /*align-1*/
  int         alt_output)
{
  if (rqsize) {
    if (NeedsUVSwap(fi, alt_output)) {
      if (plane == avs::PLANAR_V)
        plane = avs::PLANAR_U;
      else if (plane == avs::PLANAR_U)
        plane = avs::PLANAR_V;
    }

    const unsigned rowsize = (frame->GetRowSize(plane) + alignMask) & ~alignMask;
    const unsigned plsize  = frame->GetHeight(plane) * rowsize;
    const unsigned pitch   = frame->GetPitch(plane);

    if (offset < plsize) {
      size_t size = std::min(plsize - offset, rqsize);

      const uint8_t* data = frame->GetReadPtr(plane);

      data += (offset / rowsize) * pitch;
      size_t initoff = offset % rowsize;
      offset += size;
      rqsize -= size;

      while (size > 0) {
        size_t xfer = std::min(rowsize-initoff, size);
        memcpy(buffer, data+initoff, xfer);
        buffer += xfer;
        size   -= xfer;
        data   += pitch;
        initoff = 0;
      }
    }
    offset -= plsize;
  }
}

static void copyPlaneVS(
    const VSVideoFormat &fi,
    uint8_t*     &buffer,
    size_t     &offset,
    size_t     &rqsize,
    const VSFrame *frame,
    int          plane,
    unsigned     alignMask,/*align-1*/
    int         alt_output,
    const VSAPI *vsapi) {
    if (rqsize) {
        if (NeedsUVSwap(fi, alt_output)) {
            if (plane == 2)
                plane = 1;
            else if (plane == 1)
                plane = 2;
        }

        const unsigned rowsize = (vsapi->getFrameWidth(frame, plane) * fi.bytesPerSample + alignMask) & ~alignMask;
        const unsigned plsize = vsapi->getFrameHeight(frame, plane) * rowsize;
        const ptrdiff_t pitch = vsapi->getStride(frame, plane);

        if (offset < plsize) {
            size_t size = std::min(plsize - offset, rqsize);

            const uint8_t* data = vsapi->getReadPtr(frame, plane);

            data += (offset / rowsize) * pitch;
            size_t initoff = offset % rowsize;
            offset += size;
            rqsize -= size;

            while (size > 0) {
                size_t xfer = std::min(rowsize - initoff, size);
                memcpy(buffer, data + initoff, xfer);
                buffer += xfer;
                size -= xfer;
                data += pitch;
                initoff = 0;
            }
        }
        offset -= plsize;
    }
}

bool/*success*/ AvfsAvi2File::GetFrameData(
  AvfsLog_* log,
  uint8_t* buffer,
  unsigned n,
  size_t offset,
  size_t size)
{
  ASSERT(offset < frameVidDataSize && offset+size <= frameVidDataSize);
  bool success = true;
  if (vssynther) {
      const VSAPI *vsapi = vssynther->GetVSApi();
      const VSFrame *frame = vssynther->GetFrame(log, n, &success);
      if (success) {
          VideoInfoAdapter via = vssynther->GetVideoInfo();
          if (NeedsPacking(via.vf, via.alt_output)) {
              memcpy(buffer, vssynther->GetPackedFrame() + offset, size);
          } else {
              copyPlaneVS(via.vf, buffer, offset, size, frame, 0, (via.vf.numPlanes == 1) ? 3 : 0, via.alt_output, vsapi);
              copyPlaneVS(via.vf, buffer, offset, size, frame, 2, 0, via.alt_output, vsapi);
              copyPlaneVS(via.vf, buffer, offset, size, frame, 1, 0, via.alt_output, vsapi);
              ASSERT(size == 0);
          }
      }
      vsapi->freeFrame(frame);
  } else {
      avs::PVideoFrame frame = avssynther->GetFrame(log, n, &success);
      if (success) {
          VideoInfoAdapter via = avssynther->GetVideoInfo();
          if (NeedsPacking(via.vf, via.alt_output)) {
              memcpy(buffer, avssynther->GetPackedFrame() + offset, size);
          } else {
              // They won't be explicitly aligned but it doesn't matter since they're always mod4
              // Poking extra planes will read nothing so it's harmless
              copyPlaneAvs(via.vf, buffer, offset, size, frame, avs::PLANAR_Y, (via.vf.numPlanes == 1) ? 3 : 0, via.alt_output);
              copyPlaneAvs(via.vf, buffer, offset, size, frame, avs::PLANAR_V, 0, via.alt_output);
              copyPlaneAvs(via.vf, buffer, offset, size, frame, avs::PLANAR_U, 0, via.alt_output);
              ASSERT(size == 0);
          }
      }
  }
  return success;
}

bool/*success*/ AvfsAvi2File::ReadMedia(
  AvfsLog_* log,
  uint64_t inFileOffset,
  void* inBuffer,
  size_t inRequestedSize)
{
  // Avfspfm.cpp logic makes some guarantees.
  ASSERT(inRequestedSize);
  ASSERT(inFileOffset < fileSize);
  ASSERT(inFileOffset+inRequestedSize <= fileSize);

  bool success = true;
  uint64_t fileOffset = inFileOffset;
  size_t remainingSize = inRequestedSize;
  uint8_t* buffer = static_cast<uint8_t*>(inBuffer);
  unsigned segi = 0;
  size_t offset = 0;
  unsigned b;
  RiffTag riffTag;
  Seg* seg;

  // Init logic has pre-setup the header and index data needed
  // to satisfy reads.

  // Binary search for segment containing start of needed data.
  ASSERT(fileSegCount);
  b = 1;
  while (b < fileSegCount) {
    b <<= 1;
  }
  segi = 0;
  while ((b >>= 1) != 0) {
    segi |= b;
    if (segi >= fileSegCount || segs[segi]->startOffset > fileOffset) {
      segi &= ~b;
    }
  }
  offset = unsigned(fileOffset-segs[segi]->startOffset);

  // For each segment containing needed data.
  while (success && remainingSize) {
    seg = segs[segi];

    // Copy any needed segment header.
    if (offset >= seg->hdrSize) {
      offset -= seg->hdrSize;
    }
    else {
      if (remainingSize) {
          size_t partSize = seg->hdrSize-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        if (partSize) {
          memcpy(buffer, reinterpret_cast<uint8_t*>(&(seg->hdr))+offset,
            partSize);
        }
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Copy any needed frame data, one at a time.

    // Binary search for frame containing start of needed data.
    unsigned segFrame = 0;
    if (offset >= seg->dataSize) {
      offset -= seg->dataSize;
      segFrame = UINT_MAX;
    }
    else
    {
      b = 1;
      while (b < seg->frameCount) {
        b <<= 1;
      }
      while ((b >>= 1) != 0) {
        segFrame |= b;
        if (segFrame >= seg->frameCount || (seg->frameIndx[segFrame] > offset)) {
          segFrame &= ~b;
        }
      }
      offset -= seg->frameIndx[segFrame];
    }

    while (remainingSize && segFrame < seg->frameCount) {
      if(segFrame < seg->audFrameCount) {
        // Last audio frame may get extra samples if in interleave
        // disabled.
        unsigned sampleCount;
        uint64_t startSample = LocateFrameSamples(seg->startFrame+segFrame,
          (segFrame+1 == seg->audFrameCount) ? seg->lastAudFramePackCount+1 : 1,
          &sampleCount);
        ASSERT(sampleCount);
        unsigned audDataSize = sampleCount*sampleSize;
        unsigned audAlignSize = RiffAlignUp(audDataSize)-audDataSize;

        // Copy any needed portion of frame audio header.
        if (offset >= sizeof(riffTag)) {
          offset -= sizeof(riffTag);
        }
        else {
          if (remainingSize) {
            riffTag.fcc = avfsAvi2AudFcc;         // '01wb'
            riffTag.cb  = audDataSize;
            size_t partSize = sizeof(riffTag)-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            memcpy(buffer, reinterpret_cast<uint8_t*>(&riffTag)+offset,
               partSize);
            buffer += partSize;
            remainingSize -= partSize;
          }
          offset = 0;
        }

        // Copy any needed frame audio data.
        if (offset >= audDataSize) {
          offset -= audDataSize;
        }
        else {
          // Deal with ragged front.
          unsigned extraOffset = offset%sampleSize;
          if (remainingSize && extraOffset) {
            size_t partSize = sampleSize-extraOffset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            success = success && avs->GetAudio(log, sampleScratch,
              offset/sampleSize, 1);
            memcpy(buffer, sampleScratch+extraOffset, partSize);
            buffer += partSize;
            remainingSize -= partSize;
            offset += partSize;
          }

          // Do sample aligned bulk of transfer.
          if (offset < audDataSize && remainingSize >= sampleSize) {
            ASSERT(offset%sampleSize == 0);
            size_t partSize = audDataSize-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize-remainingSize%sampleSize;
            }
            success = success && avs->GetAudio(log, buffer, startSample+
              offset/sampleSize, int(partSize/sampleSize));
            buffer += partSize;
            remainingSize -= partSize;
            offset += partSize;
          }

          // Deal with ragged tail.
          if (offset < audDataSize && remainingSize) {
            ASSERT(remainingSize < sampleSize);
            success = success && avs->GetAudio(log, sampleScratch,
              startSample+offset/sampleSize, 1);
            memcpy(buffer, sampleScratch, remainingSize);
            buffer += remainingSize;
            remainingSize = 0;
          }
          offset = 0;
        }

        // Pad audio data up to riff alignment.
        if (offset >= audAlignSize) {
          offset -= audAlignSize;
        }
        else {
          if (remainingSize) {
            size_t partSize = audAlignSize-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            memset(buffer, 0, remainingSize);
            buffer += remainingSize;
            remainingSize = 0;
          }
          offset = 0;
        }
      }

      if (segFrame < seg->vidFrameCount) {
        // Copy any needed portion of frame video header.
        if (offset >= sizeof(riffTag)) {
          offset -= sizeof(riffTag);
        }
        else {
          if (remainingSize) {
            riffTag.fcc = frameVidFcc;
            riffTag.cb  = frameVidDataSize;
            size_t partSize = sizeof(riffTag)-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            memcpy(buffer, reinterpret_cast<uint8_t*>(&riffTag)+offset,
              partSize);
            buffer += partSize;
            remainingSize -= partSize;
          }
          offset = 0;
        }

        // Copy needed portion of frame video data.
        if (offset >= frameVidDataSize) {
          offset -= frameVidDataSize;
        }
        else {
          if (remainingSize) {
            size_t partSize = frameVidDataSize-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            success = success && GetFrameData(log, buffer,
              seg->startFrame+segFrame, offset, partSize);
            buffer += partSize;
            remainingSize -= partSize;
          }
          offset = 0;
        }

        // Pad video data up to riff alignment.
        if (offset >= frameVidAlignSize) {
          offset -= frameVidAlignSize;
        }
        else {
          if (remainingSize) {
            size_t partSize = frameVidAlignSize-offset;
            if (partSize > remainingSize) {
              partSize = remainingSize;
            }
            memset(buffer, 0, remainingSize);
            buffer += remainingSize;
            remainingSize = 0;
          }
          offset = 0;
        }
      }

      // Next frame.
      segFrame ++;
#ifndef NDEBUG
      unsigned temp = seg->dataSize;
      if(segFrame < seg->frameCount) {
        temp = seg->frameIndx[segFrame];
      }
      temp -= seg->frameIndx[segFrame-1];
#endif
    }

    // Copy any needed portion of pre-index pad.
    if (offset >= indxPrePadSize) {
      offset -= indxPrePadSize;
    }
    else {
      ASSERT(indxPrePadSize >= sizeof(riffTag));
      if (offset >= sizeof(riffTag)) {
        offset -= sizeof(riffTag);
      }
      else {
        if (remainingSize) {
          riffTag.fcc = riffJunkFcc;
          riffTag.cb  = indxPrePadSize-sizeof(riffTag);
          size_t partSize = sizeof(riffTag)-offset;
          if (partSize > remainingSize) {
            partSize = remainingSize;
          }
          memcpy(buffer, reinterpret_cast<uint8_t*>(&riffTag)+offset,
             partSize);
          buffer += partSize;
          remainingSize -= partSize;
        }
        offset = 0;
      }
      if (remainingSize) {
        size_t partSize = indxPrePadSize-sizeof(riffTag)-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        memset(buffer, 0, partSize);
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Copy any needed portion of video index.
    if (offset >= seg->vidIndxSize) {
      offset -= seg->vidIndxSize;
    }
    else {
      if (remainingSize) {
        size_t partSize = seg->vidIndxSize-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        memcpy(buffer, reinterpret_cast<uint8_t*>(seg->vidIndx)+offset,
          partSize);
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Copy any needed portion of audio index.
    if (offset >= seg->audIndxSize) {
      offset -= seg->audIndxSize;
    }
    else {
      if (remainingSize) {
        size_t partSize = seg->audIndxSize-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        memcpy(buffer, reinterpret_cast<uint8_t*>(seg->audIndx)+offset,
          partSize);
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Copy any needed portion of legacy index.
    if (offset >= seg->oldIndxSize) {
      offset -= seg->oldIndxSize;
    }
    else {
      if (remainingSize) {
        size_t partSize = seg->oldIndxSize-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        memcpy(buffer, reinterpret_cast<uint8_t*>(seg->oldIndx)+offset,
          partSize);
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Copy any needed portion of post-index pad.
    if (offset >= indxPostPadSize) {
      offset -= indxPostPadSize;
    }
    else {
      ASSERT(sizeof(riffTag) <= indxPostPadSize);
      if (offset >= sizeof(riffTag)) {
        offset -= sizeof(riffTag);
      }
      else {
        if (remainingSize) {
          riffTag.fcc = riffJunkFcc;
          riffTag.cb  = indxPostPadSize-sizeof(riffTag);
          size_t partSize = sizeof(riffTag)-offset;
          if (partSize > remainingSize) {
            partSize = remainingSize;
          }
          memcpy(buffer, reinterpret_cast<uint8_t*>(&riffTag)+offset,
             partSize);
          buffer += partSize;
          remainingSize -= partSize;
        }
        offset = 0;
      }
      if (remainingSize) {
        size_t partSize = indxPostPadSize-sizeof(riffTag)-offset;
        if (partSize > remainingSize) {
          partSize = remainingSize;
        }
        memset(buffer, 0, partSize);
        buffer += partSize;
        remainingSize -= partSize;
      }
      offset = 0;
    }

    // Next segment.
    segi++;
    offset = 0;
  }

  return success;
}

void AvfsAviMediaInit(
  AvfsLog_* log,
  Avisynther_* avs,
  AvfsVolume_* volume)
{
  ASSERT(log && avs && volume);
  static const size_t maxNameChars = 300;
  wchar_t name[maxNameChars];
  AvfsAvi2File* avi = new(std::nothrow) AvfsAvi2File(avs);
  if (avi && !avi->Init(log)) {
    avi->Release();
    avi = 0;
  }
  if (avi) {
    ssformat(name, maxNameChars, L"%s.avi", volume->GetMediaName());
    volume->CreateMediaFile(avi, name, avi->fileSize);
    avi->Release();
  }
}

void VsfsAviMediaInit(
    AvfsLog_* log,
    VapourSynther_* avs,
    AvfsVolume_* volume) {
    ASSERT(log && avs && volume);
    static const size_t maxNameChars = 300;
    wchar_t name[maxNameChars];
    AvfsAvi2File* avi = new(std::nothrow) AvfsAvi2File(avs);
    if (avi && !avi->Init(log)) {
        avi->Release();
        avi = 0;
    }
    if (avi) {
        ssformat(name, maxNameChars, L"%s.avi", volume->GetMediaName());
        volume->CreateMediaFile(avi, name, avi->fileSize);
        avi->Release();
    }
}