egra: agg will respect clip rect now
[iv.d.git] / dopus / orig / zogg_test.d
blobe3cde8e9f54f9ea9adcb9363131526c0d7304e38
1 // simple library for dealing with Ogg container
2 module zogg_test /*is aliced*/;
4 import iv.alice;
5 import zogg;
8 // ////////////////////////////////////////////////////////////////////////// //
9 import iv.cmdcon;
10 import iv.vfs;
13 //enum FileName = "/tmp/03/linda_karandashi_i_spichki.opus";
14 enum FileName = "/tmp/03/melodie_128.opus";
17 // ////////////////////////////////////////////////////////////////////////// //
18 import iv.encoding;
20 void main (string[] args) {
21 if (args.length == 1) args ~= FileName;
23 //testOgg0();
24 OggStream ost;
25 ost.setup(VFile(args[1]));
27 conwriteln("packet size: ", ost.packetLength);
28 conwriteln("packet gran: ", ost.packetGranule);
30 assert(ost.packetBos);
31 assert(ost.packetBop);
32 assert(ost.packetEop);
33 if (ost.packetLength < 19) assert(0);
34 enum OpusSign = "OpusHead";
35 if (cast(char[])ost.packetData[0..OpusSign.length] != OpusSign) assert(0);
36 assert(ost.packetData[8] == 1); // version
38 conwriteln("bos: ", ost.packetBos);
39 conwriteln("bop: ", ost.packetBop);
40 conwriteln("eop: ", ost.packetEop);
41 conwriteln("channels: ", ost.packetData[9]);
42 conwriteln("pre-skip: ", ost.getMemInt!ushort(ost.packetData.ptr+10));
43 conwriteln("rate: ", ost.getMemInt!uint(ost.packetData.ptr+12));
44 conwriteln("gain: ", ost.getMemInt!ushort(ost.packetData.ptr+16));
45 conwriteln("channel map: ", ost.packetData[18]);
47 if (!ost.loadPacket()) assert(0);
48 conwriteln("packet size: ", ost.packetLength);
49 conwriteln("packet gran: ", ost.packetGranule);
50 conwriteln("bos: ", ost.packetBos);
51 conwriteln("bop: ", ost.packetBop);
52 conwriteln("eop: ", ost.packetEop);
55 enum VorbisSign = "vorbis";
56 uint srate = 0;
58 // first three packets should have granule of zero
59 // granules are in samples (not multiplied by channel), and is set to last complete sample on this page
60 // granule might be ulong.max for a page that has no packet end
62 //conwriteln(ost.ogg.seglen[0..ost.ogg.segments]);
63 //assert(ost.ogg.length == 58); // per spec
64 assert(ost.bos); // per spec
65 assert(ost.granulepos == 0); // per spec
66 auto ptype = ost.readNum!ubyte;
67 conwriteln("packet type : ", ptype);
68 assert(ptype == 1);
69 foreach (char ch; VorbisSign) {
70 assert(!ost.atPacketEnd);
71 assert(ost.readNum!char == ch);
73 // version: 0
74 // channels and sample rate must be >0
75 // valid block shifts: [6..13]; bs0 should be <= bs1
76 // framing bit must be nonzero
77 assert(!ost.atPacketEnd);
78 conwriteln("version : ", ost.readNum!uint);
79 assert(!ost.atPacketEnd);
80 conwriteln("channels : ", ost.readNum!ubyte);
81 assert(!ost.atPacketEnd);
82 srate = ost.readNum!uint;
83 conwriteln("sample rate : ", srate);
84 assert(srate > 0 && srate <= 192000); // no, really
85 assert(!ost.atPacketEnd);
86 conwriteln("max rate : ", ost.readNum!uint);
87 assert(!ost.atPacketEnd);
88 conwriteln("nom rate : ", ost.readNum!uint);
89 assert(!ost.atPacketEnd);
90 conwriteln("min rate : ", ost.readNum!uint);
91 assert(!ost.atPacketEnd);
92 ubyte bss = ost.readNum!ubyte;
93 conwriteln("block size 0: ", 1<<(bss&0x0f));
94 conwriteln("block size 1: ", 1<<((bss>>4)&0x0f));
95 //conwriteln(ost.bytesRead);
96 assert(!ost.atPacketEnd);
97 conwriteln("framing flag: ", ost.readNum!ubyte);
98 //conwriteln(ost.bytesRead);
99 conwriteln("end-of-packet: ", ost.atPacketEnd);
100 conwriteln("end-of-page : ", ost.atPageEnd);
101 assert(ost.atPacketEnd == true);
102 assert(ost.atPageEnd == true);
106 auto ptype = ost.readNum!ubyte;
107 conwriteln("packet type: ", ptype);
108 assert(ptype == 3);
109 assert(!ost.bos); // per spec
110 assert(ost.granulepos == 0); // per spec
111 foreach (char ch; VorbisSign) {
112 assert(!ost.atPacketEnd);
113 assert(ost.readNum!(char, true) == ch);
115 char[] vendor;
116 vendor.length = ost.readNum!(uint, true);
117 foreach (ref char ch; vendor) ch = ost.readNum!(char, true);
118 conwriteln("vendor: <", vendor.recodeToKOI8, ">");
119 auto cmtcount = ost.readNum!(uint, true);
120 foreach (immutable _; 0..cmtcount) {
121 char[] str;
122 str.assumeSafeAppend;
123 str.length = ost.readNum!(uint, true);
124 foreach (ref char ch; str) ch = ost.readNum!(char, true);
125 conwriteln(" <", str.recodeToKOI8, ">");
127 conwriteln("framing flag: ", ost.readNum!(ubyte, true));
128 assert(ost.atPacketEnd == true);
131 //conwriteln("*: bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit, "; datapos=", ost.datapos, "; pgdatalength=", ost.pgdatalength, "; eopk=", ost.atPacketEnd, "; sgl=", ost.seglen[0..ost.segments], "; curseg=", ost.curseg, "; csp=", ost.cursegpos);
133 auto ptype = ost.readNum!ubyte;
134 conwriteln("packet type: ", ptype);
135 assert(ptype == 5);
136 assert(!ost.bos); // per spec
137 assert(ost.granulepos == 0); // per spec
138 //conwriteln("0: bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit, "; datapos=", ost.datapos, "; pgdatalength=", ost.pgdatalength, "; eopk=", ost.atPacketEnd, "; sgl=", ost.seglen[0..ost.segments], "; curseg=", ost.curseg, "; csp=", ost.cursegpos);
139 foreach (char ch; VorbisSign) {
140 assert(!ost.atPacketEnd);
141 assert(ost.readNum!char == ch);
143 //conwriteln("1: bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit, "; datapos=", ost.datapos, "; pgdatalength=", ost.pgdatalength, "; eopk=", ost.atPacketEnd, "; sgl=", ost.seglen[0..ost.segments], "; curseg=", ost.curseg, "; csp=", ost.cursegpos);
144 ost.finishPacket();
145 //conwriteln("2: bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit, "; datapos=", ost.datapos, "; pgdatalength=", ost.pgdatalength, "; eopk=", ost.atPacketEnd, "; sgl=", ost.seglen[0..ost.segments], "; curseg=", ost.curseg, "; csp=", ost.cursegpos);
146 assert(ost.atPacketEnd == true);
147 assert(!ost.eos);
149 //conwriteln("3: bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit, "; datapos=", ost.datapos, "; pgdatalength=", ost.pgdatalength, "; eopk=", ost.atPacketEnd, "; sgl=", ost.seglen[0..ost.segments], "; curseg=", ost.curseg, "; csp=", ost.cursegpos);
150 assert(ost.atPageEnd == true); // per spec
153 // scan the file
154 ulong maxgranule = 0;
155 for (;;) {
156 ubyte ptype;
157 if (ost.read(&ptype, 1) != 1) break;
158 auto gran = ost.granulepos;
159 //conwriteln("0: gran=", gran, "; ptype=", ptype, "; bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit);
160 //ost.finishPacket();
161 ost.finishPage();
162 //conwriteln("1: gran=", gran, "; ptype=", ptype, "; bytesRead=", ost.bytesRead, "; newpos=", ost.newpos, "; eof=", ost.eofhit);
163 if (/*ost.atPageEnd && (ptype&0x01) == 0 &&*/ cast(long)gran > 0) {
164 maxgranule = gran;
165 //auto secs = maxgranule/srate;
166 //conwritefln!"%s:%02s"(secs/60, secs%60);
170 auto secs = maxgranule/srate;
171 conwritefln!"%s:%02s"(secs/60, secs%60);