aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-realmedia/src/demuxers/realmedia.rs
blob: 248fa6c6f4c8677c594824fbe706cf0afc8a9518 (plain) (blame)
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
use super::*;
use nihav_core::demuxers::DemuxerError::*;
use std::io::SeekFrom;
use std::mem;
use std::fmt;

macro_rules! mktag {
    ($a:expr, $b:expr, $c:expr, $d:expr) => ({
        (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
    });
    ($arr:expr) => ({
        (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
    });
}

const RM_SIPRO_BLOCK_SIZES: [usize; 4] = [ 29, 19, 37, 20 ];
const RM_SIPRO_SWAPS:   [[u8; 2]; 38] = [
    [  0, 63 ], [  1, 22 ], [  2, 44 ], [  3, 90 ],
    [  5, 81 ], [  7, 31 ], [  8, 86 ], [  9, 58 ],
    [ 10, 36 ], [ 12, 68 ], [ 13, 39 ], [ 14, 73 ],
    [ 15, 53 ], [ 16, 69 ], [ 17, 57 ], [ 19, 88 ],
    [ 20, 34 ], [ 21, 71 ], [ 24, 46 ], [ 25, 94 ],
    [ 26, 54 ], [ 28, 75 ], [ 29, 50 ], [ 32, 70 ],
    [ 33, 92 ], [ 35, 74 ], [ 38, 85 ], [ 40, 56 ],
    [ 42, 87 ], [ 43, 65 ], [ 45, 59 ], [ 48, 79 ],
    [ 49, 93 ], [ 51, 89 ], [ 55, 95 ], [ 61, 76 ],
    [ 67, 83 ], [ 77, 80 ]
];

struct RMVideoStream {
    frame:      Vec<u8>,
    hdr_size:   usize,
    frame_size: usize,
    frame_pos:  usize,
}

impl RMVideoStream {
    fn new() -> Self {
        RMVideoStream {
            frame:      Vec::new(),
            hdr_size:   0,
            frame_size: 0,
            frame_pos:  0,
        }
    }
    fn flush(&mut self) {
        self.frame.truncate(0);
        self.frame_size = 0;
        self.frame_pos  = 0;
    }
    fn start_slice(&mut self, num_slices: usize, frame_size: usize, data: &[u8]) {
        self.hdr_size = num_slices * 8 + 1;
        self.frame.resize(frame_size + self.hdr_size, 0);
        self.frame[0] = (num_slices - 1) as u8;
        self.frame_pos = 0;
        self.add_slice(1, data).unwrap();
    }
    fn add_slice(&mut self, slice_no: usize, data: &[u8]) -> DemuxerResult<()> {
        validate!(self.hdr_size + self.frame_pos + data.len() <= self.frame.len());
        self.write_slice_info(slice_no);
        let dslice = &mut self.frame[self.hdr_size + self.frame_pos..][..data.len()];
        dslice.copy_from_slice(data);
        self.frame_pos += data.len();
        Ok(())
    }
    fn write_slice_info(&mut self, slice_no: usize) {
        let off = 1 + (slice_no - 1) * 8;
        self.frame[off + 0] = 0;
        self.frame[off + 1] = 0;
        self.frame[off + 2] = 0;
        self.frame[off + 3] = 1;
        self.frame[off + 4] = (self.frame_pos >> 24) as u8;
        self.frame[off + 5] = (self.frame_pos >> 16) as u8;
        self.frame[off + 6] = (self.frame_pos >>  8) as u8;
        self.frame[off + 7] = (self.frame_pos >>  0) as u8;
    }
    fn get_frame_data(&mut self) -> Vec<u8> {
        let mut v: Vec<u8> = Vec::new();
        mem::swap(&mut v, &mut self.frame);
        self.flush();
        v
    }
}

#[derive(Clone,Copy,PartialEq)]
enum Deinterleaver {
    None,
    RA28_8,
    Generic,
    Sipro,
    VBR,
}

struct RMAudioStream {
    deint:      Deinterleaver,
    iinfo:      Option<InterleaveInfo>,
    buf:        Vec<u8>,
    sub_packet: usize,
}

const RM_ILEAVE_INT0: u32 = mktag!(b"Int0");
const RM_ILEAVE_INT4: u32 = mktag!(b"Int4");
const RM_ILEAVE_GENR: u32 = mktag!(b"genr");
const RM_ILEAVE_SIPR: u32 = mktag!(b"sipr");
const RM_ILEAVE_VBRF: u32 = mktag!(b"vbrf");
const RM_ILEAVE_VBRS: u32 = mktag!(b"vbrs");

impl RMAudioStream {
    fn new(iinfo: Option<InterleaveInfo>) -> Self {
        let deint;
        let buf: Vec<u8>;
        if let Some(info) = iinfo {
            deint = match info.id {
                    RM_ILEAVE_INT0 => Deinterleaver::None,
                    RM_ILEAVE_INT4 => Deinterleaver::RA28_8,
                    RM_ILEAVE_GENR => Deinterleaver::Generic,
                    RM_ILEAVE_SIPR => Deinterleaver::Sipro,
                    RM_ILEAVE_VBRF => Deinterleaver::VBR,
                    RM_ILEAVE_VBRS => Deinterleaver::VBR,
                    _ => {println!("unknown deint {:X}", info.id); Deinterleaver::None },
                };
            match deint {
                Deinterleaver::None     => { buf = Vec::new(); },
                Deinterleaver::RA28_8  |
                Deinterleaver::Generic |
                Deinterleaver::Sipro    => {
                        let bsize = (info.frame_size as usize) * (info.factor as usize);
                        buf = vec![0; bsize];
                    },
                Deinterleaver::VBR      => {
                        buf = Vec::new();
                    },
            };
        } else {
            deint = Deinterleaver::None;
            buf = Vec::new();
        }
        RMAudioStream { deint, iinfo, buf, sub_packet: 0 }
    }
    fn read_apackets(&mut self, queued_packets: &mut Vec<NAPacket>, src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, payload_size: usize) -> DemuxerResult<NAPacket> {
        let (tb_num, tb_den) = stream.get_timebase();
        let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);

        if keyframe {
            self.sub_packet = 0;
        }
        match self.deint {
            Deinterleaver::None     => { return src.read_packet(stream, ts, keyframe, payload_size); },
            Deinterleaver::RA28_8   => {
                    let iinfo = self.iinfo.unwrap();
                    let factor   = iinfo.factor as usize;
                    let halffact = factor >> 1;
                    let fsize    = iinfo.frame_size as usize;
                    let bsize    = iinfo.block_size as usize;
                    let ppos     = self.sub_packet;
                    for sb in 0..halffact {
                        let mut dst = &mut self.buf[sb * 2 * fsize + ppos * bsize..][..bsize];
                        src.read_buf(&mut dst)?;
                    }
                    self.sub_packet += 1;
                    if self.sub_packet == factor {
                        self.sub_packet = 0;
                        return Ok(NAPacket::new(stream, ts, true, self.buf.clone()));
                    } else {
                        return Err(DemuxerError::TryAgain);
                    }
                },
            Deinterleaver::Generic  => {
                    let iinfo = self.iinfo.unwrap();
                    let factor   = iinfo.factor as usize;
                    let fsize    = iinfo.frame_size as usize;
                    let bsize    = iinfo.block_size as usize;
                    let factor2  = fsize / bsize;
                    let ppos     = self.sub_packet;

                    for sb in 0..factor2 {
                        let sb_pos = factor * sb + ((factor + 1) >> 1) * (ppos & 1) + (ppos >> 1);
                        let mut dst = &mut self.buf[bsize * sb_pos..][..bsize];
                        src.read_buf(&mut dst)?;
                    }
                },
            Deinterleaver::Sipro    => {
                    let iinfo = self.iinfo.unwrap();
                    let fsize    = iinfo.frame_size as usize;
                    let ppos     = self.sub_packet;

                    let mut dst = &mut self.buf[fsize * ppos..][..fsize];
                    src.read_buf(&mut dst)?;
                },
            Deinterleaver::VBR      => {
                    validate!(payload_size >= 5);
                    let hdrsizesize         = src.read_u16be()?;
                    let num_entries = (hdrsizesize / 16) as usize;
                    validate!(payload_size >= num_entries * 3 + 2);
                    let mut sizes: Vec<usize> = Vec::with_capacity(num_entries);
                    let mut tot_size = 0;
                    for _ in 0..num_entries {
                        let sz              = src.read_u16be()? as usize;
                        tot_size += sz;
                        sizes.push(sz);
                    }
                    validate!(tot_size + num_entries * 2 + 2 == payload_size);
                    let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den);
                    let mut first = true;
                    for size in sizes.iter() {
                        let cur_ts = if first { ts } else { pkt_ts };
                        first = false;
                        let pkt = src.read_packet(stream.clone(), cur_ts, true, *size)?;
                        queued_packets.push(pkt);
                    }
                    queued_packets.reverse();
                    let pkt0 = queued_packets.pop().unwrap();
                    return Ok(pkt0);
                },
        };

        let iinfo = self.iinfo.unwrap();
        let factor   = iinfo.factor as usize;
        let fsize    = if iinfo.block_size != 0 { iinfo.block_size } else { iinfo.frame_size } as usize;

        self.sub_packet += 1;
        if self.sub_packet < factor {
            return Err(DemuxerError::TryAgain);
        }

        self.sub_packet = 0;

        if self.deint == Deinterleaver::Sipro {
            sipro_restore(&mut self.buf, factor, iinfo.frame_size as usize);
        }

        let mut frames_iter = self.buf.chunks(fsize);
        let pkt0 = frames_iter.next().unwrap();

        let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den);
        for pkts in frames_iter {
            let pkt = NAPacket::new(stream.clone(), pkt_ts, true, pkts.to_vec());
            queued_packets.push(pkt);
        }
        queued_packets.reverse();
        Ok(NAPacket::new(stream, ts, true, pkt0.to_vec()))
    }
}

fn sipro_restore(buf: &mut [u8], factor: usize, fsize: usize) {
    let stride = factor * fsize * 2 / 96;
    for i in 0..38 {
        let mut sidx = (RM_SIPRO_SWAPS[i][0] as usize) * stride;
        let mut didx = (RM_SIPRO_SWAPS[i][1] as usize) * stride;
        for _ in 0..stride {
            let in0 = buf[sidx >> 1];
            let in1 = buf[didx >> 1];
            let nib0 = (in0 >> ((sidx & 1) * 4)) & 0xF;
            let nib1 = (in1 >> ((didx & 1) * 4)) & 0xF;

            buf[didx >> 1] = (nib0 << (4 * (didx & 1))) | (in1 & (0xF << (4 * (!didx & 1))));
            buf[sidx >> 1] = (nib1 << (4 * (sidx & 1))) | (in0 & (0xF << (4 * (!sidx & 1))));

            sidx += 1;
            didx += 1;
        }
    }
}

struct SubstreamInfo {
    id:         u32,
    map:        Vec<u16>,
    str_ids:    Vec<u32>,
}

struct MLTIMapper {
    sub_info:   Vec<SubstreamInfo>,
    sstr_id:    u32,
}

impl MLTIMapper {
    fn new() -> Self {
        MLTIMapper {
            sub_info:   Vec::new(),
            sstr_id:    0x10000,
        }
    }
    fn add_stream(&mut self, stream_no: u32) {
        let ssinfo = SubstreamInfo { id: stream_no, map: Vec::new(), str_ids: Vec::new() };
        self.sub_info.push(ssinfo);
    }
    fn get_substream_no(&self) -> u32 {
        self.sstr_id - 1
    }
    fn find_idx(&self, stream_no: u32) -> Option<usize> {
        self.sub_info.iter().position(|x| x.id == stream_no)
    }
    fn add_map_rule(&mut self, map_ss: u16) {
        let idx = self.sub_info.len() - 1;
        self.sub_info[idx].map.push(map_ss);
    }
    fn add_substream(&mut self) {
        let idx = self.sub_info.len() - 1;
        self.sub_info[idx].str_ids.push(self.sstr_id);
        self.sstr_id += 1;
    }
    fn is_mlti_stream(&self, stream_no: u32) -> bool {
        self.find_idx(stream_no).is_some()
    }
    fn find_substream(&self, stream_no: u32, grp: u16) -> Option<u32> {
        if let Some(idx) = self.find_idx(stream_no) {
            if (grp as usize) < self.sub_info[idx].map.len() {
                let sub_id = self.sub_info[idx].map[grp as usize] as usize;
                if sub_id < self.sub_info[idx].str_ids.len() {
                    return Some(self.sub_info[idx].str_ids[sub_id]);
                }
            }
        }
        None
    }
}

enum RMStreamType {
    Audio(RMAudioStream),
    Video(RMVideoStream),
    Logical,
    Unknown,
}

struct CommonStreamData {
    streams:        Vec<RMStreamType>,
    str_ids:        Vec<u32>,
    mlti_mapper:    MLTIMapper,
}

impl CommonStreamData {
    fn new() -> Self {
        CommonStreamData {
            streams:        Vec::new(),
            str_ids:        Vec::new(),
            mlti_mapper:    MLTIMapper::new(),
        }
    }
    fn get_stream_id(&self, str_no: u32, pkt_grp: u16) -> u32 {
        if !self.mlti_mapper.is_mlti_stream(str_no) {
            str_no
        } else {
            self.mlti_mapper.find_substream(str_no, pkt_grp).unwrap()
        }
    }
    fn find_stream(&self, stream_id: u32) -> Option<usize> {
        self.str_ids.iter().position(|x| *x == stream_id)
    }
}

struct RealMediaDemuxer<'a> {
    src:            &'a mut ByteReader<'a>,
    data_pos:       u64,
    data_end:       u64,
    next_data:      u64,
    data_ver:       u16,
    num_packets:    u32,
    cur_packet:     u32,

    str_data:       CommonStreamData,

    data_chunks:    Vec<(u64, u32, u16)>,
    cur_data_chunk: usize,

    queued_pkts:    Vec<NAPacket>,
    slice_buf:      Vec<u8>,
}

fn find_codec_name(registry: &[(&[u8;4], &'static str)], fcc: u32) -> &'static str {
    for &(fourcc, name) in registry {
        if mktag!(fourcc) == fcc { return name; }
    }
    "unknown"
}

fn read_14or30(src: &mut ByteReader) -> DemuxerResult<(bool, u32)> {
    let tmp = src.read_u16be()?;
    let flag = (tmp & 0x8000) != 0;
    if (tmp & 0x4000) == 0x4000 {
        Ok((flag, ((tmp & 0x3FFF) as u32)))
    } else {
        let val = ((tmp as u32) << 16) | (src.read_u16be()? as u32);
        Ok((flag, val & 0x3FFFFFFF))
    }
}

fn read_video_buf(src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult<NAPacket> {
    let size = (frame_size as usize) + 9;
    let mut vec: Vec<u8> = vec![0; size];
    //v[0] = 0; // 1 slice
    vec[4] = 1;
    src.read_buf(&mut vec[9..])?;

    let (tb_num, tb_den) = stream.get_timebase();
    let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
    Ok(NAPacket::new(stream, ts, keyframe, vec))
}

fn read_multiple_frame(src: &mut ByteReader, stream: NAStreamRef, keyframe: bool, skip_mtype: bool) -> DemuxerResult<NAPacket> {
    if !skip_mtype {
        let mtype       = src.read_byte()?;
        validate!(mtype == 0xC0);
    }
    let (_, frame_size) = read_14or30(src)?;
    let (_, timestamp)  = read_14or30(src)?;
    let _seq_no         = src.read_byte()?;
//println!("  multiple frame size {} ts {} seq {}", frame_size, timestamp, seq_no);

    read_video_buf(src, stream, timestamp, keyframe, frame_size as usize)
}

struct RMDemuxCommon {}

impl RMDemuxCommon {
    fn parse_stream_info(str_data: &mut CommonStreamData, strmgr: &mut StreamManager, stream_no: u32, edata: &[u8], duration: u32) -> DemuxerResult<bool> {
        let mut is_mlti = false;
        let mut mr = MemoryReader::new_read(edata);
        let mut src = ByteReader::new(&mut mr);
        let tag  = src.read_u32be()?;
        let tag2 = src.peek_u32be()?;
//println!("tag1 {:X} tag2 {:X}", tag, tag2);
        if tag == mktag!('.', 'r', 'a', 0xFD) {
            Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata, duration)?;
        } else if ((tag2 == mktag!('V', 'I', 'D', 'O')) || (tag2 == mktag!('I', 'M', 'A', 'G'))) && ((tag as usize) <= edata.len()) {
            Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata, tag2, duration)?;
        } else if tag == mktag!(b"LSD:") {
            let extradata = Some(edata.to_owned());

            src.read_skip(4)?; //version
            let channels    = src.read_u16be()?;
            let samp_size   = src.read_u16be()?;
            let sample_rate = src.read_u32be()?;

            println!("LSD sr {}, {} ch", sample_rate, channels);
            let soniton = NASoniton::new(samp_size as u8, SONITON_FLAG_SIGNED);
            let ahdr = NAAudioInfo::new(sample_rate, channels as u8, soniton, 1);
            let nainfo = NACodecInfo::new("ralf", NACodecTypeInfo::Audio(ahdr), extradata);
            let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, 1000, u64::from(duration)));
            if res.is_none() { return Err(MemoryError); }
            let astr = RMAudioStream::new(None);
            str_data.streams.push(RMStreamType::Audio(astr));
        } else if tag == mktag!(b"MLTI") {
            is_mlti = true;
            let num_rules       = src.read_u16be()? as usize;
            let mut max_sub = 0;
            str_data.mlti_mapper.add_stream(stream_no);
            for _ in 0..num_rules {
                let substr      = src.read_u16be()?;
                max_sub = max_sub.max(substr);
                str_data.mlti_mapper.add_map_rule(substr);
            }
            let num_substreams  = src.read_u16be()? as usize;
            validate!(num_substreams > (max_sub as usize));
            for _ in 0..num_substreams {
                let hdr_size    = src.read_u32be()? as usize;
                validate!(hdr_size > 8);
                let pos = src.tell() as usize;
                src.read_skip(hdr_size)?;
                str_data.mlti_mapper.add_substream();
                {
                    let hdrsrc = &edata[pos..][..hdr_size];
                    let mut mr = MemoryReader::new_read(hdrsrc);
                    let mut hsrc = ByteReader::new(&mut mr);

                    let tag  = hsrc.read_u32be()?;
                    let tag2 = hsrc.peek_u32be()?;
                    let stream_no = str_data.mlti_mapper.get_substream_no();
//todo check that all substreams are of the same type");
                    if tag == mktag!('.', 'r', 'a', 0xFD) {
                        Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc, duration)?;
                    } else if (tag2 == mktag!('V', 'I', 'D', 'O')) && ((tag as usize) <= hdr_size) {
                        Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc, tag2, duration)?;
                    } else {
println!("unknown MLTI substream {:08X} / {:08X}", tag, tag2);
                        return Err(DemuxerError::InvalidData);
                    }
                    str_data.str_ids.push(stream_no);
                }
            }
        } else {
            str_data.streams.push(RMStreamType::Logical);
        }
        Ok(is_mlti)
    }
    fn parse_audio_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8], duration: u32) -> DemuxerResult<()> {
        let ver         = src.read_u16be()?;
        let ainfo = match ver {
            3 => {
                    parse_aformat3(src)?
                },
            4 => {
                    parse_aformat4(src)?
                },
            5 => {
                    parse_aformat5(src)?
                },
            _ => {
                    println!("unknown version {}", ver);
                    return Err(DemuxerError::InvalidData);
                },
        };
println!(" got ainfo {:?}", ainfo);
        let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc);
        let blk_size = if ainfo.fcc != mktag!(b"sipr") {
                ainfo.granularity as usize
            } else {
                validate!(ainfo.flavor <= 3);
                RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
            };
        let srate = ainfo.sample_rate;
        let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED);
        let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size);
        let extradata = if ainfo.edata_size == 0 {
                None
            } else {
                let eslice = &edata_[(src.tell() as usize)..];
                Some(eslice.to_vec())
            };
        let duration = if duration == 0 { ainfo.get_duration(1000) } else { u64::from(duration) };
        let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata);
        let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, 1000, duration));
        if res.is_none() { return Err(MemoryError); }

        let astr = RMAudioStream::new(ainfo.ileave_info);
        streams.push(RMStreamType::Audio(astr));
        Ok(())
    }
#[allow(unused_variables)]
    fn parse_video_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8], tag2: u32, duration: u32) -> DemuxerResult<()> {
        src.read_skip(4)?;
        let fcc         = src.read_u32be()?;
        let width       = src.read_u16be()? as usize;
        let height      = src.read_u16be()? as usize;
        let bpp         = src.read_u16be()?;
        let pad_w       = src.read_u16be()?;
        let pad_h       = src.read_u16be()?;
        let _fps;
        if tag2 == mktag!('V', 'I', 'D', 'O') {
            _fps        = src.read_u32be()?;
        } else {
            _fps = 0x10000;
        }
        let extradata: Option<Vec<u8>>;
        if src.left() > 0 {
            let eslice = &edata_[(src.tell() as usize)..];
            extradata = Some(eslice.to_vec());
        } else {
            extradata = None;
        }
        let cname = find_codec_name(RM_VIDEO_CODEC_REGISTER, fcc);

        let vhdr = NAVideoInfo::new(width, height, false, RGB24_FORMAT);
        let vinfo = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), extradata);
        let res = strmgr.add_stream(NAStream::new(StreamType::Video, stream_no as u32, vinfo, 1, 1000, u64::from(duration)));
        if res.is_none() { return Err(DemuxerError::MemoryError); }

        let vstr = RMVideoStream::new();
        streams.push(RMStreamType::Video(vstr));
        Ok(())
    }
#[allow(unused_variables)]
    fn parse_packet_payload(src: &mut ByteReader, rmstream: &mut RMStreamType, stream: NAStreamRef, slice_buf: &mut Vec<u8>, queued_pkts: &mut Vec<NAPacket>, keyframe: bool, ts: u32, payload_size: usize) -> DemuxerResult<NAPacket> {
        match rmstream {
            RMStreamType::Video(ref mut vstr) => {

                    let pos = src.tell();
                    let b0          = src.read_byte()?;
                    match b0 >> 6 {
                        0 => { // partial frame
                                let b1  = src.read_byte()?;
                                let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
                                let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
                                let packet_num = hdr1 & 0x7F;
                                let (_, frame_size) = read_14or30(src)?;
                                let (_, off)        = read_14or30(src)?;
                                let seq_no = src.read_byte()?;
//println!(" mode 0 pkt {}/{} off {}/{} seq {}", packet_num, num_pkts, off, frame_size, seq_no);
                                let hdr_skip = (src.tell() - pos) as usize;

                                let slice_size = (payload_size - hdr_skip) as usize;
                                slice_buf.resize(slice_size, 0);
                                src.read_buf(slice_buf.as_mut_slice())?;
                                if packet_num == 1 {
                                    vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice());
                                } else {
                                    vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
                                }
                                if (packet_num as usize) < num_pkts {
                                    return Err(DemuxerError::TryAgain);
                                }
                                //todo: check if full frame is received
                                let (tb_num, tb_den) = stream.get_timebase();
                                let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
                                let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data());
                                Ok(pkt)
                            },
                        1 => { // whole frame
                                let seq_no = src.read_byte()?;
//println!(" mode 1 seq {}", seq_no);
                                read_video_buf(src, stream, ts, keyframe, payload_size - 2)
                            },
                        2 => { // last partial frame
                                let b1  = src.read_byte()?;
                                let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
                                let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
                                let packet_num = hdr1 & 0x7F;
                                let (_, frame_size) = read_14or30(src)?;
                                let (_, tail_size)  = read_14or30(src)?;
                                let seq_no = src.read_byte()?;
//println!(" mode 2 pkt {}/{} tail {}/{} seq {}", packet_num, num_pkts, tail_size, frame_size, seq_no);
                                slice_buf.resize(tail_size as usize, 0);
                                src.read_buf(slice_buf.as_mut_slice())?;
                                if packet_num == 1 && frame_size == tail_size {
                                    vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice());
                                } else {
                                    vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
                                }

                                while src.tell() < pos + (payload_size as u64) {
                                    let res = read_multiple_frame(src, stream.clone(), false, false);
                                    if res.is_err() { break; }
                                    queued_pkts.push(res.unwrap());
                                }
                                queued_pkts.reverse();
                                let (tb_num, tb_den) = stream.get_timebase();
                                let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
                                let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data());
                                Ok(pkt)
                        },
                    _ => { // multiple frames
//println!(" mode 3");
                                let res = read_multiple_frame(src, stream.clone(), keyframe, true);
                                if res.is_err() { return res; }
                                while src.tell() < pos + (payload_size as u64) {
                                    let res = read_multiple_frame(src, stream.clone(), false, false);
                                    if res.is_err() { break; }
                                    queued_pkts.push(res.unwrap());
                                }
                                queued_pkts.reverse();
                                res
                            },
                    }
                },
            RMStreamType::Audio(ref mut astr) => {
                    astr.read_apackets(queued_pkts, src, stream, ts, keyframe, payload_size)
                },
            _ => {
                      src.read_skip(payload_size)?;
                    Err(DemuxerError::InvalidData)
                },
        }
    }
}

impl<'a> DemuxCore<'a> for RealMediaDemuxer<'a> {
    fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
        self.read_header(strmgr, seek_idx)?;
        Ok(())
    }

#[allow(unused_variables)]
    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if !self.queued_pkts.is_empty() {
            let pkt = self.queued_pkts.pop().unwrap();
            return Ok(pkt);
        }
        loop {
            if (self.cur_packet >= self.num_packets) || (self.src.tell() >= self.data_end) {
                self.cur_data_chunk += 1;
                if self.cur_data_chunk < self.data_chunks.len() {
                    let (pos, _, _) = self.data_chunks[self.cur_data_chunk];
                    self.src.seek(SeekFrom::Start(pos))?;
                    let res = read_chunk(self.src);
                    if let Ok((id, size, ver)) = res {
                        self.data_pos = self.src.tell();
                        self.data_ver = ver;
                        self.data_end = self.data_pos + (size as u64);
                        if self.parse_data_start().is_ok() {
                            continue;
                        }
                    }
                }
                return Err(DemuxerError::EOF);
            }

            let pkt_start = self.src.tell();
            let ver             = self.src.read_u16be()?;
            validate!(ver <= 1);
            let len             = self.src.read_u16be()? as usize;
            let str_no          = self.src.read_u16be()? as u32;
            let ts              = self.src.read_u32be()?;
            let pkt_grp;
            let flags;
            if ver == 0 {
                pkt_grp         = self.src.read_byte()? as u16;
                flags           = self.src.read_byte()?;
            } else {
                //asm_rule        = self.src.read_u16be()?;
                //asm_flags       = self.src.read_byte()?;
                self.src.read_skip(2)?;
                pkt_grp = 0;
                self.src.read_skip(1)?;
                flags = 0;
            }
            let hdr_size = self.src.tell() - pkt_start;
//println!("packet @{:X} size {} for {} ts {} grp {} flags {:X}", pkt_start, len, str_no, ts, pkt_grp, flags);
            self.cur_packet += 1;

            let payload_size = len - (hdr_size as usize);

            let stream_id = self.str_data.get_stream_id(str_no as u32, pkt_grp);
            let sr = self.str_data.find_stream(stream_id);
            if sr.is_none() {
//println!("stream {} not found", str_no);
                self.src.read_skip(payload_size)?;
                return Err(DemuxerError::InvalidData);
            }
            let str_id = sr.unwrap();

            let streamres = strmgr.get_stream_by_id(stream_id);
            if streamres.is_none() {
                self.src.read_skip(payload_size)?;
                continue;
            }
            let stream = streamres.unwrap();
//println!("  stream {}", str_id);
            if strmgr.is_ignored_id(stream_id) {
                self.src.read_skip(payload_size)?;
                continue;
            }
            //todo skip unwanted packet
            let keyframe = (flags & KEYFRAME_FLAG) != 0;

            let ret = RMDemuxCommon::parse_packet_payload(&mut self.src, &mut self.str_data.streams[str_id], stream, &mut self.slice_buf, &mut self.queued_pkts, keyframe, ts, payload_size);
            if let Err(DemuxerError::TryAgain) = ret {
                continue;
            } else {
                return ret;
            }
        }
    }

    #[allow(unused_variables)]
    fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> {
        self.queued_pkts.clear();
        let ret = seek_idx.find_pos(time);
        if ret.is_none() {
            return Err(DemuxerError::SeekError);
        }
        let ret = ret.unwrap();
        let seek_pos = ret.pos;
        for (pos, size, ver) in self.data_chunks.iter() {
            if seek_pos < *pos { continue; }
            let end = *pos + (*size as u64);
            if seek_pos < end {
                self.cur_packet = 0;
                self.data_pos = seek_pos;
                self.data_ver = *ver;
                self.data_end = end;
                self.src.seek(SeekFrom::Start(seek_pos))?;
                return Ok(());
            }
        }
        Err(DemuxerError::SeekError)
    }

    fn get_duration(&self) -> u64 { 0 }
}

impl<'a> NAOptionHandler for RealMediaDemuxer<'a> {
    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
    fn set_options(&mut self, _options: &[NAOption]) { }
    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
}

fn read_chunk(src: &mut ByteReader) -> DemuxerResult<(u32, u32, u16)> {
    let id      = src.read_u32be()?;
if id == 0 { return Ok((0, 0, 0)); }
    let size    = src.read_u32be()?;
if size == 0 {
    let ver     = src.read_u16be()?;
    validate!(ver <= 2);
    return Ok((id, 0x0FFFFFFF, ver));
}
    validate!(size >= 10);
    let ver     = src.read_u16be()?;
    validate!(ver <= 2);
    Ok((id, size, ver))
}

#[derive(Clone,Copy,Debug)]
struct InterleaveInfo {
    id:         u32,
    factor:     u16,
    block_size: u16,
    frame_size: u16,
}

#[derive(Clone,Copy,Debug)]
struct RealAudioInfo {
    fcc:                u32,
    sample_rate:        u32,
    sample_size:        u16,
    flavor:             u16,
    channels:           u16,
    channel_mask:       u32,
    granularity:        u32,
    bytes_per_minute:   u32,
    total_bytes:        u32,
    edata_size:         u32,
    ileave_info:        Option<InterleaveInfo>
}

impl RealAudioInfo {
    fn get_duration(&self, base: u32) -> u64 {
        if self.bytes_per_minute != 0 {
            u64::from(self.total_bytes) * 60 * u64::from(base) / u64::from(self.bytes_per_minute)
        } else {
            0
        }
    }
}

fn skip_ra_metadata(src: &mut ByteReader) -> DemuxerResult<()> {
    let title_len           = src.read_byte()? as usize;
    src.read_skip(title_len)?;
    let author_len          = src.read_byte()? as usize;
    src.read_skip(author_len)?;
    let copywrong_len       = src.read_byte()? as usize;
    src.read_skip(copywrong_len)?;
    let comment_len         = src.read_byte()? as usize;
    src.read_skip(comment_len)?;
    Ok(())
}

#[allow(unused_variables)]
fn parse_aformat3(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
    let start = src.tell();
    let header_len          = src.read_u16be()?;
    validate!(header_len >= 24);
    let flavor              = src.read_u16be()?;
    let granularity         = src.read_u32be()?;
    let bytes_per_minute    = src.read_u32be()?;
    let total_bytes         = src.read_u32be()?;

    skip_ra_metadata(src)?;

    let _can_copy           = src.read_byte()?;
    let fcc_len             = src.read_byte()?;
    validate!(fcc_len == 4);
    let fcc                 = src.read_u32be()?;

    let end = src.tell();
    validate!(end - start <= (header_len as u64) + 2);

    Ok(RealAudioInfo {
        fcc, flavor,
        sample_rate: 8000, sample_size: 16, channels: 1, channel_mask: 0,
        granularity, bytes_per_minute,
        total_bytes, edata_size: 0,
        ileave_info: None,
    })
}

#[allow(unused_variables)]
fn parse_aformat4(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
    let start = src.tell();
    src.read_skip(2)?; // zeroes
    let id                  = src.read_u32be()?;
    validate!(id == mktag!(b".ra4"));
    let data_size           = src.read_u32be()?;
    let _ver4               = src.read_u16be()?; // should be 4
    let header_size         = src.read_u32be()?;
    let flavor              = src.read_u16be()?;
    let granularity         = src.read_u32be()?;
    let total_bytes         = src.read_u32be()?;
    let bytes_per_minute    = src.read_u32be()?;
    let _bytes_per_minute2  = src.read_u32be()?;
    let ileave_factor       = src.read_u16be()?;
    let ileave_block_size   = src.read_u16be()?;
    let _user_data          = src.read_u16be()?;
    let sample_rate         = src.read_u32be()?;
    let sample_size         = src.read_u32be()?;
    let channels            = src.read_u16be()?;
    let interleaver_id_len  = src.read_byte()?;
    validate!(interleaver_id_len == 4);
    let interleaver_id      = src.read_u32be()?;
    let fcc_len             = src.read_byte()?;
    validate!(fcc_len == 4);
    let fcc                 = src.read_u32be()?;
    let is_interleaved      = src.read_byte()?;
    let _can_copy           = src.read_byte()?;
    let _stream_type        = src.read_byte()?;

    skip_ra_metadata(src)?;

    let end = src.tell();
    validate!(end - start <= (header_size as u64) + 10);

    let ileave_info = if is_interleaved != 0 {
            Some(InterleaveInfo {
                    id: interleaver_id, factor: ileave_factor, block_size: granularity as u16,
                    frame_size: ileave_block_size,
                })
        } else {
            None
        };

    Ok(RealAudioInfo {
        fcc, flavor,
        sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0,
        granularity, bytes_per_minute,
        total_bytes: total_bytes & 0xFFFFFF, edata_size: 0,
        ileave_info,
    })
}

#[allow(unused_variables)]
fn parse_aformat5(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
    let start = src.tell();
    src.read_skip(2)?; // zeroes
    let id                  = src.read_u32be()?;
    validate!((id == mktag!(b".ra5")) || (id == mktag!(b".ra4")));
    let data_size           = src.read_u32be()?;
    let _ver5               = src.read_u16be()?; // should be 5
    let header_size         = src.read_u32be()?;
    let flavor              = src.read_u16be()?;
    let granularity         = src.read_u32be()?;
    let total_bytes         = src.read_u32be()?;
    let bytes_per_minute    = src.read_u32be()?;
    let _bytes_per_minute2  = src.read_u32be()?;
    let ileave_factor       = src.read_u16be()?;
    let frame_size          = src.read_u16be()?;
    let ileave_block_size   = src.read_u16be()?;
    let user_data           = src.read_u32be()?;
    let _sample_rate1       = src.read_u16be()?;
    let sample_rate         = src.read_u32be()?;
    let sample_size         = src.read_u32be()?;
    let channels            = src.read_u16be()?;
    let interleaver_id      = src.read_u32be()?;
    let fcc                 = src.read_u32be()?;
    let is_interleaved      = src.read_byte()?;
    let _can_copy           = src.read_byte()?;
    let _stream_type        = src.read_byte()?;
    let has_ileave_pattern  = src.read_byte()?;
    if has_ileave_pattern != 0 {
unimplemented!("ra5 interleave pattern");
    }
    let mut edata_size          = src.read_u32be()?;
    let end = src.tell();
    if id == mktag!(b".ra5") {
        validate!(end - start <= (header_size as u64) + 10);
//    src.read_skip(((end - start) as usize) - (header_size as usize) - 10)?;
    } else {
        validate!(end - start <= (header_size as u64) + 15);
    }

    let ileave_info = if is_interleaved != 0 {
            Some(InterleaveInfo {
                    id: interleaver_id, factor: ileave_factor, block_size: ileave_block_size, frame_size,
                })
        } else {
            None
        };
    if (fcc == mktag!(b"raac")) || (fcc == mktag!(b"racp")) {
        validate!(edata_size > 1);
        edata_size -= 1;
        src.read_skip(1)?;
    }

    Ok(RealAudioInfo {
        fcc, flavor,
        sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0,
        granularity, bytes_per_minute,
        total_bytes: total_bytes & 0xFFFFFF, edata_size,
        ileave_info,
    })
}

const RMVB_HDR_SIZE:  u32 = 18;
const RMVB_PROP_SIZE: u32 = 50;
const KEYFRAME_FLAG: u8 = 0x02;

impl<'a> RealMediaDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        RealMediaDemuxer {
            src:            io,
            data_pos:       0,
            data_end:       0,
            next_data:      0,
            data_ver:       0,
            num_packets:    0,
            cur_packet:     0,
            str_data:       CommonStreamData::new(),
            data_chunks:    Vec::new(),
            cur_data_chunk: 0,
            queued_pkts:    Vec::new(),
            slice_buf:      Vec::new(),
        }
    }
#[allow(unused_variables)]
    fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
        let (id, size, ver) = read_chunk(self.src)?;
        validate!((id == mktag!(b".RMF")) || (id == mktag!(b".RMP")));
        validate!(size >= RMVB_HDR_SIZE);
        let fver    = self.src.read_u32be()?;
        validate!(fver <= 1);
        let num_hdr = self.src.read_u32be()? as usize;
        validate!(num_hdr >= 1);
        if size > RMVB_HDR_SIZE {
            self.src.read_skip((size - RMVB_HDR_SIZE) as usize)?;
        }

        let (id, size, ver) = read_chunk(self.src)?;
        let prop_size = if ver == 0 { RMVB_PROP_SIZE } else { RMVB_PROP_SIZE + 4 };
        validate!(size >= prop_size);
        validate!((ver == 0) || (ver == 2));
        let maxbr       = self.src.read_u32be()?;
        let avgbr       = self.src.read_u32be()?;
        let maxps       = self.src.read_u32be()?;
        let avgps       = self.src.read_u32be()?;
        let num_pkt     = self.src.read_u32be()? as usize;
        let duration    = self.src.read_u32be()?;
        let preroll     = self.src.read_u32be()?;
        if ver == 2 {
                          self.src.read_skip(4)?;
        }
        let idx_off     = self.src.read_u32be()?;
        let data_off    = self.src.read_u32be()?;
        let num_streams = self.src.read_u16be()? as usize;
        let flags       = self.src.read_u16be()?;
        if size > prop_size {
            self.src.read_skip((size - prop_size) as usize)?;
        }

        for _ in 0..num_hdr {
            if self.src.is_eof() {
                //warn maybe?
                break;
            }
            let res = self.parse_chunk(strmgr, seek_idx);
            match res {
                Ok(last) => { if last { break; } },
                Err(DemuxerError::IOError) => { break; },
                Err(etype) => {
                        if self.data_chunks.is_empty() { // data is not found, report error
                            return Err(etype);
                        }
                    },
            };
        }
//println!("now @ {:X} / {}", self.src.tell(), self.data_pos);
        validate!(!self.data_chunks.is_empty());
        self.cur_data_chunk = 0;
        let (pos, size, ver) = self.data_chunks[self.cur_data_chunk];
        self.data_pos = pos;
        self.data_ver = ver;
        self.data_end = pos + (size as u64);
        self.src.seek(SeekFrom::Start(self.data_pos + 10))?;
        self.parse_data_start()?;
        Ok(())
    }
    fn parse_data_start(&mut self) -> DemuxerResult<()> {
        let num_packets     = self.src.read_u32be()?;
        if self.data_ver == 2 {
                              self.src.read_skip(12)?; // zeroes?
        }
        let next_data_hdr   = self.src.read_u32be()?;
        self.num_packets = if num_packets > 0 { num_packets } else { 0xFFFFFF };
        self.cur_packet  = 0;
        self.next_data   = next_data_hdr as u64;
        Ok(())
    }
    fn parse_chunk(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<bool> {
        let (id, size, ver) = read_chunk(self.src)?;
        let end_pos = self.src.tell() - 10 + (size as u64);

        validate!((ver == 0) || (ver == 2));
             if id == mktag!(b"CONT") { self.parse_content_desc()?; }
        else if id == mktag!(b"MDPR") { self.parse_mdpr(strmgr)?; }
        else if id == mktag!(b"DATA") {
            self.data_chunks.push((self.src.tell() - 10, size, ver));
        }
        else if id == mktag!(b"INDX") {
            if !seek_idx.skip_index {
                self.parse_index(seek_idx, (size as usize) - 10, ver)?;
            }
        }
        else if id == 0               { return Ok(true); }
        else                          { println!("unknown chunk type {:08X}", id); }

        let cpos = self.src.tell();
        if cpos < end_pos {
            self.src.read_skip((end_pos - cpos) as usize)?;
        }
        Ok(false)
    }
#[allow(unused_variables)]
    fn parse_content_desc(&mut self) -> DemuxerResult<()> {
        let title_len       = self.src.read_u16be()? as usize;
        self.src.read_skip(title_len)?;
        let author_len      = self.src.read_u16be()? as usize;
        self.src.read_skip(author_len)?;
        let copywrong_len   = self.src.read_u16be()? as usize;
        self.src.read_skip(copywrong_len)?;
        let comment_len     = self.src.read_u16be()? as usize;
        self.src.read_skip(comment_len)?;
        Ok(())
    }
#[allow(unused_variables)]
    fn parse_mdpr(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
        let stream_no       = self.src.read_u16be()? as u32;
//todo check stream_no for duplicates
        let maxbr           = self.src.read_u32be()?;
        let avgbr           = self.src.read_u32be()?;
        let maxps           = self.src.read_u32be()?;
        let avgps           = self.src.read_u32be()?;
        let start           = self.src.read_u32be()?;
        let preroll         = self.src.read_u32be()?;
        let duration        = self.src.read_u32be()?;
        let sname_size      = self.src.read_byte()? as usize;
        let sname           = read_string_size(self.src, sname_size)?;
//println!("str #{} sname = {} pkts {}/{} start {} preroll {}", stream_no, sname, maxps, avgps, start, preroll);
        let mime_size       = self.src.read_byte()? as usize;
        let mime            = read_string_size(self.src, mime_size)?;
//println!("mime = {}", mime);
        let edata_size      = self.src.read_u32be()? as usize;
        let edata: Option<Vec<u8>> = if edata_size == 0 { None } else {
            let mut edvec: Vec<u8> = vec![0; edata_size];
            self.src.read_buf(&mut edvec)?;
            Some(edvec)
        };
        let mut is_mlti = false;
        if edata_size > 8 {
            if let Some(edata_) = edata {
                is_mlti = RMDemuxCommon::parse_stream_info(&mut self.str_data, strmgr, stream_no, &edata_, duration)?;
            }
        } else {
            self.str_data.streams.push(RMStreamType::Unknown);
        }
        if !is_mlti {
            self.str_data.str_ids.push(stream_no);
        }

        Ok(())
    }
    fn parse_index(&mut self, seek_idx: &mut SeekIndex, chunk_size: usize, ver: u16) -> DemuxerResult<()> {
        if ver != 0 && ver != 2 { return Ok(()); }
        let num_entries     = self.src.read_u32be()? as usize;
        let str_id          = self.src.read_u16be()? as u32;
        let _next_idx       = self.src.read_u32be()?;
        if ver == 2 {
                              self.src.read_u32be()?;
        }
        if ver == 0 {
            validate!(chunk_size == num_entries * 14 + 10);
        } else {
            validate!(chunk_size == num_entries * 18 + 14);
        }
        if num_entries == 0 { return Ok(()); }

        seek_idx.add_stream(str_id);
        let idx = seek_idx.get_stream_index(str_id).unwrap();
        for _ in 0..num_entries {
            let iver        = self.src.read_u16be()?;
            validate!(iver == ver);
            let ts          = self.src.read_u32be()? as u64;
            if ver == 2 {
                              self.src.read_u32be()?;
            }
            let pos         = self.src.read_u32be()? as u64;
            let _pkt_no     = self.src.read_u32be()?;
            idx.add_entry(SeekEntry { time: ts, pts: 0, pos });
        }
        idx.filled = true;
        seek_idx.mode = SeekIndexMode::Present;
        Ok(())
    }
}

fn read_string_size(src: &mut ByteReader, size: usize) -> DemuxerResult<String> {
    let mut vec: Vec<u8> = Vec::new();
    for _ in 0..size {
        let c = src.read_byte()?;
        vec.push(c);
    }
    let str = String::from_utf8(vec);
    if let Ok(res) = str {
        Ok(res)
    } else {
        Ok(String::new())
    }
}

struct RealAudioDemuxer<'a> {
    src:            &'a mut ByteReader<'a>,
    stream:         Option<RMAudioStream>,
    data_start:     u64,
    data_end:       u64,
    blk_size:       usize,
    queued_pkts:    Vec<NAPacket>,
}

impl<'a> DemuxCore<'a> for RealAudioDemuxer<'a> {
    #[allow(unused_variables)]
    fn open(&mut self, strmgr: &mut StreamManager, _seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
        let magic                                       = self.src.read_u32be()?;
        validate!(magic == mktag!(b".ra\xFD"));
        let ver         = self.src.read_u16be()?;
        let ainfo = match ver {
            3 => {
                    parse_aformat3(&mut self.src)?
                },
            4 => {
                    parse_aformat4(&mut self.src)?
                },
            5 => {
                    parse_aformat5(&mut self.src)?
                },
            _ => {
                    println!("unknown version {}", ver);
                    return Err(DemuxerError::InvalidData);
                },
        };
println!(" got ainfo {:?}", ainfo);
        let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc);
        let blk_size = if ainfo.fcc != mktag!(b"sipr") {
                ainfo.granularity as usize
            } else {
                validate!(ainfo.flavor <= 3);
                RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
            };
        let srate = ainfo.sample_rate;
        let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED);
        let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size);
        let extradata = if ainfo.edata_size == 0 {
                None
            } else {
                let mut dta: Vec<u8> = vec![0; ainfo.edata_size as usize];
                self.src.read_buf(dta.as_mut_slice())?;
                Some(dta)
            };
        let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata);
        let res = strmgr.add_stream(NAStream::new(StreamType::Audio, 0, nainfo, 1, srate, ainfo.get_duration(ainfo.sample_rate)));
        if res.is_none() { return Err(MemoryError); }

        let astr = RMAudioStream::new(ainfo.ileave_info);
        self.data_start = self.src.tell();
        self.data_end   = if ainfo.total_bytes > 0 { self.src.tell() + (ainfo.total_bytes as u64) } else { 0 };
        self.blk_size = blk_size;
        self.stream = Some(astr);

        Ok(())
    }

    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if !self.queued_pkts.is_empty() {
            let pkt = self.queued_pkts.pop().unwrap();
            return Ok(pkt);
        }
        if (self.data_end != 0) && (self.src.tell() >= self.data_end) {
            return Err(DemuxerError::EOF);
        }
        let streamres = strmgr.get_stream_by_id(0);
        let stream = streamres.unwrap();
        if let Some(ref mut astr) = self.stream {
            loop {
                let ret = astr.read_apackets(&mut self.queued_pkts, &mut self.src, stream.clone(), 0, false, self.blk_size);
                if let Err(DemuxerError::TryAgain) = ret {
                    continue;
                }
                return ret;
            }
        }
        Err(DemuxerError::NoSuchInput)
    }

    #[allow(unused_variables)]
    fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> {
        Err(NotImplemented)
    }

    fn get_duration(&self) -> u64 { 0 }
}

impl<'a> NAOptionHandler for RealAudioDemuxer<'a> {
    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
    fn set_options(&mut self, _options: &[NAOption]) { }
    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
}

impl<'a> RealAudioDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        RealAudioDemuxer {
            src:            io,
            data_start:     0,
            data_end:       0,
            blk_size:       0,
            stream:         None,
            queued_pkts:    Vec::new(),
        }
    }
}

enum IVRRecord {
    Invalid(u8),
    StreamProperties(usize),
    Packet { ts: u32, str: u32, flags: u32, len: usize, checksum: u32 },
    IntValue(Vec<u8>, u32),
    BinaryData(Vec<u8>, Vec<u8>),
    StringData(Vec<u8>, Vec<u8>),
    HeaderEnd,
    DataStart,
    DataEnd,
}

impl IVRRecord {
    fn read_string(src: &mut ByteReader) -> DemuxerResult<Vec<u8>> {
        let len                     = src.read_u32be()? as usize;
        let mut val = vec![0; len];
        src.read_buf(val.as_mut_slice())?;
        Ok(val)
    }

    fn read(src: &mut ByteReader) -> DemuxerResult<Self> {
        let code = src.read_byte()?;
        match code {
            1 => {
                    let val     = src.read_u32be()? as usize;
                    Ok(IVRRecord::StreamProperties(val))
                },
            2 => {
                    let ts      = src.read_u32be()?;
                    let str     = src.read_u16be()? as u32;
                    let flags   = src.read_u32be()?;
                    let len     = src.read_u32be()? as usize;
                    let chk     = src.read_u32be()?;
                    validate!((len > 0) && (len < (1 << 24)));
                    Ok(IVRRecord::Packet { ts, str, flags, len, checksum: chk })
                },
            3 => {
                    let name = Self::read_string(src)?;
                    let len  = src.read_u32be()?;
                    validate!(len == 4);
                    let val  = src.read_u32be()?;
                    Ok(IVRRecord::IntValue(name, val))
                },
            4 => {
                    let name = Self::read_string(src)?;
                    let len  = src.read_u32be()? as usize;
                    let mut val = vec![0; len];
                    src.read_buf(val.as_mut_slice())?;
                    Ok(IVRRecord::BinaryData(name, val))
                },
            5 => {
                    let name = Self::read_string(src)?;
                    let val  = Self::read_string(src)?;
                    Ok(IVRRecord::StringData(name, val))
                },
            6 => Ok(IVRRecord::HeaderEnd),
            7 => {
                    src.read_skip(8)?; // always zero?
                    Ok(IVRRecord::DataEnd)
                },
            8 => {
                    src.read_skip(8)?; // always zero?
                    Ok(IVRRecord::DataStart)
                },
            _ => Ok(IVRRecord::Invalid(code)),
        }
    }
    fn is_data_start(&self) -> bool {
        match *self {
            IVRRecord::DataStart => true,
            _ => false,
        }
    }
}

impl fmt::Display for IVRRecord {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            IVRRecord::Invalid(typ) => write!(f, "Invalid({:02X})", typ),
            IVRRecord::StreamProperties(num) =>
                write!(f, "({} stream properties)", num),
            IVRRecord::Packet { ts, str, flags, len, checksum } =>
                write!(f, "paket({}, {}, {:X}, {}, {})", ts, str, flags, len, checksum),
            IVRRecord::IntValue(ref name, val) =>
                write!(f, "({} = {})", String::from_utf8_lossy(name), val),
            IVRRecord::BinaryData(ref name, ref val) =>
                write!(f, "({} = {} bytes)", String::from_utf8_lossy(name), val.len()),
            IVRRecord::StringData(ref name, ref val) =>
                write!(f, "({} = {})", String::from_utf8_lossy(name), String::from_utf8_lossy(val)),
            IVRRecord::HeaderEnd => write!(f, "header end"),
            IVRRecord::DataEnd   => write!(f, "data end"),
            IVRRecord::DataStart => write!(f, "data start"),
        }
    }
}

struct RecordDemuxer {
    start_pos:      u64,
    cur_pos:        u64,
    start_str:      u32,
    remap_ids:      Vec<u32>,
}

impl RecordDemuxer {
    fn new(pos: u64, start_str: u32) -> Self {
        RecordDemuxer {
            start_pos:      pos,
            cur_pos:        pos,
            start_str,
            remap_ids:      Vec::new(),
        }
    }
    fn parse_header(&mut self, src: &mut ByteReader, strmgr: &mut StreamManager, str_data: &mut CommonStreamData) -> DemuxerResult<()> {
        src.seek(SeekFrom::Start(self.cur_pos))?;
        let magic           = src.read_u32be()?;
        validate!(magic == mktag!(b".REC"));
        let _smth           = src.read_byte()?;
        let num_entries     = src.read_u32be()? as usize;
        for _ in 0..num_entries {
            let _rec = IVRRecord::read(src)?;
//println!(" header rec {}", _rec);
        }
        let mut has_seek_table = false;
        let mut cur_str_no = 0;
        loop {
            let rec = IVRRecord::read(src)?;
            match rec {
                IVRRecord::HeaderEnd => { break; },
                IVRRecord::StreamProperties(num) => {
                        let stream_no = cur_str_no + self.start_str;
                        cur_str_no += 1;
                        let mut parsed = false;
                        let mut real_stream_no = 0;
                        let mut duration = 0;
                        for _ in 0..num {
                            let rec = IVRRecord::read(src)?;
//println!("  strm property {}", rec);
                            match rec {
                                IVRRecord::IntValue(ref name, val)   => {
                                        if name == b"StreamNumber\0" {
                                            real_stream_no = val;
                                        }
                                        if name == b"Duration\0" {
                                            duration = val;
                                        }
                                    },
                                IVRRecord::BinaryData(ref name, ref val)   => {
                                        if name == b"OpaqueData\0" {
                                            validate!(!parsed);
                                            let is_mlti = RMDemuxCommon::parse_stream_info(str_data, strmgr, stream_no, val, duration)?;
                                            if !is_mlti {
                                                str_data.str_ids.push(stream_no);
                                            }
                                            parsed = true;
                                        }
                                    },
                                IVRRecord::StringData(ref name, ref val)   => {
                                        if (name == b"SeekType\0") && (val != b"None\0") {
                                            has_seek_table = true;
                                        }
                                    },
                                _ => { return Err(DemuxerError::InvalidData); }
                            };
                        }
                        if !parsed {
                            str_data.streams.push(RMStreamType::Unknown);
                            str_data.str_ids.push(stream_no);
                        }
                        self.remap_ids.push(real_stream_no);
                    },
                _ => {println!(" unexpected {}", rec); return Err(DemuxerError::InvalidData); }
            };
        }
        println!(" now @ {:X}", src.tell());
        let off0        = src.read_u32be()? as u64;
        let _off1       = src.read_u32be()?;
        let _off2       = src.read_u32be()?;
        validate!(off0 + self.start_pos == src.tell());
        println!(" has seek tab: {}", has_seek_table);
        if has_seek_table {
            src.read_skip(4)?;
            let data_off    = src.read_u32be()? as u64;
            println!(" new off {:X}", data_off);
            let pos = src.tell();
            validate!(data_off + self.start_pos > pos);
            src.read_skip((data_off + self.start_pos - pos) as usize)?;
            let rec = IVRRecord::read(src)?;
            validate!(rec.is_data_start());
        } else {
            let ntype = src.peek_byte()?;
            validate!((ntype == 2) || (ntype == 7)); // packet or data end, no start
        }

        self.cur_pos = src.tell();

        Ok(())
    }
    fn get_packet(&mut self, src: &mut ByteReader, str_data: &mut CommonStreamData, strmgr: &StreamManager, queued_pkts: &mut Vec<NAPacket>, slice_buf: &mut Vec<u8>) -> DemuxerResult<NAPacket> {
        src.seek(SeekFrom::Start(self.cur_pos))?;
        loop {
            let rec = IVRRecord::read(src)?;
            match rec {
                IVRRecord::Packet { ts, str, flags, len, .. } => {
                        let payload_size = len;
                        let sr = self.remap_ids.iter().position(|x| *x == str);
                        validate!(sr.is_some());
                        let str_no = self.start_str + (sr.unwrap() as u32);
                        let pkt_grp = ((flags >> 8) & 0xFF) as u16;
                        let stream_id = str_data.get_stream_id(str_no as u32, pkt_grp);
                        let sr = str_data.find_stream(stream_id);
                        if sr.is_none() {
                            src.read_skip(payload_size)?;
                            return Err(DemuxerError::InvalidData);
                        }
                        let str_id = sr.unwrap();

                        let streamres = strmgr.get_stream_by_id(stream_id);
                        if streamres.is_none() {
                            src.read_skip(payload_size)?;
                            continue;
                        }
                        let stream = streamres.unwrap();
                        if strmgr.is_ignored_id(stream_id) {
                            src.read_skip(payload_size)?;
                            continue;
                        }
                        let keyframe = false;
                        let ret = RMDemuxCommon::parse_packet_payload(src, &mut str_data.streams[str_id], stream, slice_buf, queued_pkts, keyframe, ts, payload_size);
                        if let Err(DemuxerError::TryAgain) = ret {
                            continue;
                        } else {
                            self.cur_pos = src.tell();
                            return ret;
                        }
                    },
                IVRRecord::DataEnd      => return Err(DemuxerError::EOF),
                _                       => return Err(DemuxerError::InvalidData),
            }
        }
    }
}

struct RealIVRDemuxer<'a> {
    src:            &'a mut ByteReader<'a>,
    recs:           Vec<RecordDemuxer>,
    cur_rec:        usize,
    queued_pkts:    Vec<NAPacket>,
    slice_buf:      Vec<u8>,
    str_data:       CommonStreamData,
}

impl<'a> DemuxCore<'a> for RealIVRDemuxer<'a> {
    #[allow(unused_variables)]
    fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
        let magic                                       = self.src.peek_u32be()?;
        if magic == mktag!(b".REC") {
            let mut rec = RecordDemuxer::new(0, 0);
            rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?;
            self.recs.push(rec);
        } else if magic == mktag!(b".R1M") {
println!("R1M kind");
            self.src.read_skip(4)?; // magic
            self.src.read_skip(3)?; // always 0, 1, 1 ?
            let _name = IVRRecord::read_string(&mut self.src)?;
            self.src.read_skip(1)?; // always 0?
            let len1 = self.src.read_u32be()? as u64;
            let off1 = self.src.read_u64be()?;
            let cpos = self.src.tell();
            validate!(off1 == len1 + cpos - 8);
            self.src.read_skip((off1 - cpos) as usize)?;
            loop {
                let typ = self.src.read_byte()?;
                println!(" at {:X} type {:02X}", self.src.tell(), typ);
                match typ {
                    1 => {
                            let len = self.src.read_u32be()?;
                            self.src.read_skip(len as usize)?;
                        },
                    2 => {
                            let len = self.src.read_u32be()? as u64;
                            let pos = self.src.tell();
                            if len > 0x20 {
                                let num_streams = self.str_data.streams.len() as u32;
                                let mut rec = RecordDemuxer::new(pos + 12, num_streams);
                                rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?;
                                self.recs.push(rec);
                            }
                            self.src.seek(SeekFrom::Start(pos + len))?;
                        },
                    b'R' => {
                            let mut buf: [u8; 2] = [0; 2];
                            self.src.peek_buf(&mut buf)?;
                            if (buf[0] == b'J') && (buf[1] == b'M') { // RJMx markers at the end of file
                                break;
                            } else {
                                return Err(DemuxerError::InvalidData);
                            }
                        },
                    _ => { return Err(DemuxerError::InvalidData); },
                };
            }
        } else {
            return Err(DemuxerError::InvalidData);
        }

        Ok(())
    }

    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
        if !self.queued_pkts.is_empty() {
            let pkt = self.queued_pkts.pop().unwrap();
            return Ok(pkt);
        }
        loop {
            if self.cur_rec >= self.recs.len() { return Err(DemuxerError::EOF); }
            let res = self.recs[self.cur_rec].get_packet(&mut self.src, &mut self.str_data, strmgr, &mut self.queued_pkts, &mut self.slice_buf);
            if let Err(DemuxerError::EOF) = res {
                self.cur_rec += 1;
            } else {
                return res;
            }
        }
    }

    #[allow(unused_variables)]
    fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> {
        Err(NotImplemented)
    }

    fn get_duration(&self) -> u64 { 0 }
}

impl<'a> NAOptionHandler for RealIVRDemuxer<'a> {
    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
    fn set_options(&mut self, _options: &[NAOption]) { }
    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
}

impl<'a> RealIVRDemuxer<'a> {
    fn new(io: &'a mut ByteReader<'a>) -> Self {
        RealIVRDemuxer {
            src:            io,
            recs:           Vec::new(),
            cur_rec:        0,
            queued_pkts:    Vec::new(),
            slice_buf:      Vec::new(),
            str_data:       CommonStreamData::new(),
        }
    }
}

static RM_VIDEO_CODEC_REGISTER: &[(&[u8;4], &str)] = &[
    (b"RV10", "realvideo1"),
    (b"RV20", "realvideo2"),
    (b"RVTR", "realvideo2"),
    (b"RV30", "realvideo3"),
    (b"RV40", "realvideo4"),
    (b"RV60", "realvideo6"),
    (b"CLV1", "clearvideo_rm"),
];

static RM_AUDIO_CODEC_REGISTER: &[(&[u8;4], &str)] = &[
    (b"lpcJ", "ra14.4"),
    (b"28_8", "ra28.8"),
    (b"cook", "cook"),
    (b"dnet", "ac3"),
    (b"sipr", "sipro"),
    (b"atrc", "atrac3"),
    (b"LSD:", "ralf"),
    (b"raac", "aac"),
    (b"racp", "aac"),
];

pub struct RealMediaDemuxerCreator { }

impl DemuxerCreator for RealMediaDemuxerCreator {
    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
        Box::new(RealMediaDemuxer::new(br))
    }
    fn get_name(&self) -> &'static str { "realmedia" }
}

pub struct RealAudioDemuxerCreator { }

impl DemuxerCreator for RealAudioDemuxerCreator {
    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
        Box::new(RealAudioDemuxer::new(br))
    }
    fn get_name(&self) -> &'static str { "realaudio" }
}

pub struct RealIVRDemuxerCreator { }

impl DemuxerCreator for RealIVRDemuxerCreator {
    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
        Box::new(RealIVRDemuxer::new(br))
    }
    fn get_name(&self) -> &'static str { "real_ivr" }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::fs::File;

    #[test]
    fn test_rm_demux() {
        let mut file =
            File::open("assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm").unwrap();
//            File::open("assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm").unwrap();
//            File::open("assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm").unwrap();
//            File::open("assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm").unwrap();
//            File::open("assets/RV/rv30_chroma_drift.rm").unwrap();
//            File::open("assets/RV/rv30_weighted_mc.rm").unwrap();
//            File::open("assets/RV/rv40_weighted_mc.rmvb").unwrap();
//            File::open("assets/RV/rv40_weighted_mc_2.rmvb").unwrap();
//            File::open("assets/RV/clv1_sipr_384x208_realvideo_encoder_4.0.rm").unwrap();
//            File::open("assets/RV/luckynight.rmvb").unwrap();
//            File::open("assets/RV/rv40_ralf.rmvb").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = RealMediaDemuxer::new(&mut br);
        let mut sm = StreamManager::new();
        let mut si = SeekIndex::new();
        dmx.open(&mut sm, &mut si).unwrap();

        loop {
            let pktres = dmx.get_frame(&mut sm);
            if let Err(e) = pktres {
                if e == DemuxerError::EOF { break; }
                panic!("error");
            }
            let pkt = pktres.unwrap();
            println!("Got {}", pkt);
        }
//panic!("the end");
    }
    #[test]
    fn test_ra_demux() {
        let mut file =
//            File::open("assets/RV/welcome288.ra").unwrap();
            File::open("assets/RV/diemusik.ra").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = RealAudioDemuxer::new(&mut br);
        let mut sm = StreamManager::new();
        let mut si = SeekIndex::new();
        dmx.open(&mut sm, &mut si).unwrap();

        loop {
            let pktres = dmx.get_frame(&mut sm);
            if let Err(e) = pktres {
                if e == DemuxerError::EOF { break; }
                panic!("error");
            }
            let pkt = pktres.unwrap();
            println!("Got {}", pkt);
        }
    }
    #[test]
    fn test_ivr_demux() {
        let mut file =
            File::open("assets/RV/Opener_rm_hi.ivr").unwrap();
//            File::open("assets/RV/SherwinWilliamsCommercial.ivr").unwrap();
        let mut fr = FileReader::new_read(&mut file);
        let mut br = ByteReader::new(&mut fr);
        let mut dmx = RealIVRDemuxer::new(&mut br);
        let mut sm = StreamManager::new();
        let mut si = SeekIndex::new();
        dmx.open(&mut sm, &mut si).unwrap();

        loop {
            let pktres = dmx.get_frame(&mut sm);
            if let Err(e) = pktres {
                if e == DemuxerError::EOF { break; }
                panic!("error");
            }
            let pkt = pktres.unwrap();
            println!("Got {}", pkt);
        }
    }
}