static public int vorbis_analysis(ref vorbis_block vb, ref Ogg.ogg_packet op) { int ret, i; vorbis_block_internal vbi = vb._internal as vorbis_block_internal; vb.glue_bits = 0; vb.time_bits = 0; vb.floor_bits = 0; vb.res_bits = 0; /* first things first. Make sure encode is ready */ for (i = 0; i < PACKETBLOBS; i++) { Ogg.oggpack_reset(ref vbi.packetblob[i]); } /* we only have one mapping type (0), and we let the mapping code itself figure out what soft mode to use. * This allows easier bitrate management */ if ((ret = _mapping_P[0].forward(ref vb)) != 0) { return(ret); } if (vorbis_bitrate_managed(ref vb) != 0) { /* The app is using a bitmanaged mode... but not using the * bitrate management interface. */ return(OV_EINVAL); } op.packet = Ogg.oggpack_get_buffer(ref vb.opb); op.bytes = Ogg.oggpack_bytes(ref vb.opb); op.b_o_s = 0; op.granulepos = vb.granulepos; op.packetno = vb.sequence; /* for sake of completeness */ return(0); }
static int _vorbis_unpack_comment(ref vorbis_comment vc, ref Ogg.oggpack_buffer opb) { int i; int vendorlen = Ogg.oggpack_read(ref opb, 32); if (vendorlen < 0) { goto err_out; } if (vendorlen > opb.storage - 8) { goto err_out; } vc.vendor = new char[vendorlen + 1]; _v_readstring(ref opb, ref vc.vendor, vendorlen); i = Ogg.oggpack_read(ref opb, 32); if (i < 0) { goto err_out; } if (i > ((opb.storage - Ogg.oggpack_bytes(ref opb) >> 2))) { goto err_out; } vc.comments = i; vc.user_comments = new char[vc.comments + 1][]; vc.comment_lengths = new int[vc.comments + 1]; for (i = 0; i < vc.comments; i++) { int len = Ogg.oggpack_read(ref opb, 32); if (len < 0) { goto err_out; } if (len > opb.storage - Ogg.oggpack_bytes(ref opb)) { goto err_out; } vc.comment_lengths[i] = len; vc.user_comments[i] = new char[len + 1]; _v_readstring(ref opb, ref vc.user_comments[i], len); } /* EOP check */ if (Ogg.oggpack_read(ref opb, 1) != 1) { goto err_out; } return(0); err_out: vorbis_comment_clear(ref vc); return(OV_EBADHEADER); }
static void ClipTest(uint[] b, int vals, int bits, uint[] comp, int compsize) { int bytes; byte *buffer; Ogg.oggpack_reset(ref o); for (int i = 0; i < vals; i++) { Ogg.oggpack_write(ref o, b[i], bits > 0 ? bits : iLog(b[i])); } buffer = Ogg.oggpack_get_buffer(ref o); bytes = Ogg.oggpack_bytes(ref o); if (bytes != compsize) { throw new Exception("wrong number of bytes!"); } for (int i = 0; i < bytes; i++) { if (buffer[i] != comp[i]) { for (int j = 0; j < bytes; j++) { Console.WriteLine(buffer[j] + " , " + comp[j]); } throw new Exception("wrote incorrect value!"); } } Ogg.oggpack_readinit(ref r, buffer, bytes); for (int i = 0; i < vals; i++) { int tbit = bits > 0 ? bits : iLog(b[i]); if (Ogg.oggpack_look(ref r, tbit) == -1) { throw new Exception("out of data!"); } if (Ogg.oggpack_look(ref r, tbit) != (b[i] & mask[tbit])) { throw new Exception("looked at incorrect value!"); } if (tbit == 1) { if (Ogg.oggpack_look1(ref r) != (b[i] & mask[tbit])) { throw new Exception("looked at single bit incorrect value!"); } } if (tbit == 1) { if (Ogg.oggpack_read1(ref r) != (b[i] & mask[tbit])) { throw new Exception("read incorrect single bit value!"); } } else if (Ogg.oggpack_read(ref r, tbit) != (b[i] & mask[tbit])) { throw new Exception("read incorrect value!"); } } if (Ogg.oggpack_bytes(ref r) != bytes) { throw new Exception("leftover bytes after read!"); } }
static public void Test() { byte *buffer; int bytes = 0; /* Test read/write together */ /* Later we test against pregenerated bitstreams */ Ogg.oggpack_writeinit(ref o); Console.Write("Small preclipped packing (LSb): "); ClipTest(testBuffer1, testSize1, 0, one, oneSize); Console.WriteLine("ok."); Console.Write("Null bit call (LSb): "); ClipTest(testBuffer3, testSize3, 0, two, twoSize); Console.WriteLine("ok."); Console.Write("Large preclipped packing (LSb): "); ClipTest(testBuffer2, testSize2, 0, three, threeSize); Console.WriteLine("ok."); Console.Write("32 bit precliiped packing (LSb): "); { Ogg.oggpack_reset(ref o); for (int i = 0; i < testSize2; i++) { Ogg.oggpack_write(ref o, large[i], 32); } buffer = Ogg.oggpack_get_buffer(ref o); bytes = Ogg.oggpack_bytes(ref o); Ogg.oggpack_readinit(ref r, buffer, bytes); for (int i = 0; i < testSize2; i++) { if (Ogg.oggpack_look(ref r, 32) == -1) { throw new Exception("out of data. failed!"); } if (Ogg.oggpack_look(ref r, 32) != large[i]) { throw new Exception("read incorrect value! " + Ogg.oggpack_look(ref r, 32) + " != " + large[1]); } Ogg.oggpack_adv(ref r, 32); } if (Ogg.oggpack_bytes(ref r) != bytes) { throw new Exception("leftover bytes after read!"); } Console.WriteLine("ok."); } Console.Write("Small unclipped packing (LSb): "); ClipTest(testBuffer1, testSize1, 7, four, fourSize); Console.WriteLine("ok."); Console.Write("Large unclipped packing (LSb): "); ClipTest(testBuffer2, testSize2, 17, five, fiveSize); Console.WriteLine("ok."); Console.Write("Single bit unclipped packing (LSb): "); ClipTest(testBuffer3, testSize3, 1, six, sixSize); Console.WriteLine("ok."); Console.Write("Testing read past end (LSb): "); { byte *temp = (byte *)Ogg._ogg_malloc(8); try { for (int i = 0; i < 8; i++) { temp[i] = 0; } Ogg.oggpack_readinit(ref r, temp, 8); for (int i = 0; i < 64; i++) { if (Ogg.oggpack_read(ref r, 1) != 0) { throw new Exception("failed; got -1 prematurely."); } } if (Ogg.oggpack_look(ref r, 1) != -1 || Ogg.oggpack_read(ref r, 1) != -1) { throw new Exception("failed; read past end without -1"); } for (int i = 0; i < 8; i++) { temp[i] = 0; } Ogg.oggpack_readinit(ref r, temp, 8); if (Ogg.oggpack_read(ref r, 30) != 0 || Ogg.oggpack_read(ref r, 16) != 0) { throw new Exception("failed 2; got -1 prematurely."); } if (Ogg.oggpack_look(ref r, 18) != 0 || Ogg.oggpack_look(ref r, 18) != 0) { throw new Exception("failed 3; got -1 prematurely."); } if (Ogg.oggpack_look(ref r, 19) != -1 || Ogg.oggpack_look(ref r, 19) != -1) { throw new Exception("failed 3; got -1 prematurely."); } if (Ogg.oggpack_look(ref r, 32) != -1 || Ogg.oggpack_look(ref r, 32) != -1) { throw new Exception("failed 3; got -1 prematurely."); } Ogg.oggpack_writeclear(ref o); Console.WriteLine("ok."); } finally { Ogg._ogg_free(temp); } } /********** lazy, cut-n-paste retest with MSb packing ***********/ /* Test read/write together */ /* Later we test against pregenerated bitstreams */ Ogg.oggpackB_writeinit(ref o); Console.Write("Small preclipped packing (MSb): "); ClipTestB(testBuffer1, testSize1, 0, oneB, oneSize); Console.WriteLine("ok."); Console.Write("Null bit call (LSb): "); ClipTestB(testBuffer3, testSize3, 0, twoB, twoSize); Console.WriteLine("ok."); Console.Write("Large preclipped packing (LSb): "); ClipTestB(testBuffer2, testSize2, 0, threeB, threeSize); Console.WriteLine("ok."); Console.Write("32 bit precliiped packing (LSb): "); { Ogg.oggpackB_reset(ref o); for (int i = 0; i < testSize2; i++) { Ogg.oggpackB_write(ref o, large[i], 32); } buffer = Ogg.oggpackB_get_buffer(ref o); bytes = Ogg.oggpackB_bytes(ref o); Ogg.oggpackB_readinit(ref r, buffer, bytes); for (int i = 0; i < testSize2; i++) { if (Ogg.oggpackB_look(ref r, 32) == -1) { throw new Exception("out of data. failed!"); } if (Ogg.oggpackB_look(ref r, 32) != large[i]) { throw new Exception("read incorrect value! " + Ogg.oggpackB_look(ref r, 32) + " != " + large[1]); } Ogg.oggpackB_adv(ref r, 32); } if (Ogg.oggpackB_bytes(ref r) != bytes) { throw new Exception("leftover bytes after read!"); } Console.WriteLine("ok."); } Console.Write("Small unclipped packing (LSb): "); ClipTestB(testBuffer1, testSize1, 7, fourB, fourSize); Console.WriteLine("ok."); Console.Write("Large unclipped packing (LSb): "); ClipTestB(testBuffer2, testSize2, 17, fiveB, fiveSize); Console.WriteLine("ok."); Console.Write("Single bit unclipped packing (LSb): "); ClipTestB(testBuffer3, testSize3, 1, sixB, sixSize); Console.WriteLine("ok."); Console.Write("Testing read past end (LSb): "); { byte *temp = (byte *)Ogg._ogg_malloc(8); try { for (int i = 0; i < 8; i++) { temp[i] = 0; } Ogg.oggpackB_readinit(ref r, temp, 8); for (int i = 0; i < 64; i++) { if (Ogg.oggpackB_read(ref r, 1) != 0) { throw new Exception("failed; got -1 prematurely."); } } if (Ogg.oggpackB_look(ref r, 1) != -1 || Ogg.oggpackB_read(ref r, 1) != -1) { throw new Exception("failed; read past end without -1"); } for (int i = 0; i < 8; i++) { temp[i] = 0; } Ogg.oggpackB_readinit(ref r, temp, 8); if (Ogg.oggpackB_read(ref r, 30) != 0 || Ogg.oggpackB_read(ref r, 16) != 0) { throw new Exception("failed 2; got -1 prematurely."); } if (Ogg.oggpackB_look(ref r, 18) != 0 || Ogg.oggpackB_look(ref r, 18) != 0) { throw new Exception("failed 3; got -1 prematurely."); } if (Ogg.oggpackB_look(ref r, 19) != -1 || Ogg.oggpackB_look(ref r, 19) != -1) { throw new Exception("failed 3; got -1 prematurely."); } if (Ogg.oggpackB_look(ref r, 32) != -1 || Ogg.oggpackB_look(ref r, 32) != -1) { throw new Exception("failed 3; got -1 prematurely."); } Ogg.oggpackB_writeclear(ref o); Console.WriteLine("ok."); } finally { Ogg._ogg_free(temp); } } }