本文整理汇总了C#中BitReader.ReadBits方法的典型用法代码示例。如果您正苦于以下问题:C# BitReader.ReadBits方法的具体用法?C# BitReader.ReadBits怎么用?C# BitReader.ReadBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitReader
的用法示例。
在下文中一共展示了BitReader.ReadBits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decode
public static byte[] Decode(byte[] data, int from, int to, int bitsInterval)
{
var copiedData = new byte[to - from + 1];
Array.Copy(data, from, copiedData, 0, copiedData.Length);
byte[] reverseBytes = Convert.ReverseBitsInBytes(copiedData);
using (var s = new MemoryStream(reverseBytes))
using (var reader = new BitReader(s))
{
using (var decoded = new MemoryStream())
using (var writer = new BitWriter(decoded))
{
int counter = 0;
while (reader.CanRead)
{
byte bit = reader.ReadBits(1);
writer.WriteBit(bit);
counter = (bit == 1) ? counter + 1 : 0;
if (counter == bitsInterval)
{
reader.ReadBits(1); // skip next 0 bit
counter = 0;
}
}
// !! do not flush last bits
return Convert.ReverseBitsInBytes(decoded.ToArray());
}
}
}
示例2: ReadSetupData
public override void ReadSetupData( VorbisCodec codec, BitReader reader )
{
int residueBegin = reader.ReadBits( 24 );
int residueEnd = reader.ReadBits( 24 );
int partitionSize = reader.ReadBits( 24 ) + 1;
int classifications = reader.ReadBits( 6 ) + 1;
int classbook = reader.ReadBits( 8 );
byte[] residueCascade = new byte[classifications];
for( int i = 0; i < residueCascade.Length; i++ ) {
int highBits = 0;
int lowBits = reader.ReadBits( 3 );
if( reader.ReadBit() == 1 )
highBits = reader.ReadBits( 5 );
residueCascade[i] = (byte)( lowBits | ( highBits << 3 ) );
}
byte[][] bookNumbers = new byte[classifications][];
for( int i = 0; i < bookNumbers.Length; i++ ) {
byte[] nums = new byte[8];
int cascade = residueCascade[i];
for( int j = 0; j < nums.Length; j++ ) {
if( ( cascade & ( 1 << j ) ) != 0 ) {
nums[j] = (byte)reader.ReadBits( 8 );
}
}
bookNumbers[i] = nums;
}
}
示例3: Encode
public static byte[] Encode(byte[] data, int bitsInterval)
{
byte[] reverseBytes = Convert.ReverseBitsInBytes(data);
using (var s = new MemoryStream(reverseBytes))
using (var reader = new BitReader(s))
{
using (var encoded = new MemoryStream())
using (var writer = new BitWriter(encoded))
{
int counter = 0;
while (reader.CanRead)
{
byte bit = reader.ReadBits(1);
writer.WriteBit(bit);
counter = (bit == 1) ? counter + 1 : 0;
if (counter == bitsInterval)
{
writer.WriteBit(0);
counter = 0;
}
}
writer.FlushBits();
return Convert.ReverseBitsInBytes(encoded.ToArray());
}
}
}
示例4: Read
/* read the contents to the backing store */
internal void Read(BitReader br)
{
bytesAllocated = Helpers.ReadNumber(br);
bytesAllocatedByKids = Helpers.ReadNumber(br);
numberOfObjectsAllocated = Helpers.ReadNumber(br);
numberOfObjectsAllocatedByKids = Helpers.ReadNumber(br);
numberOfFunctionsCalled = Helpers.ReadNumber(br);
numberOfFunctionsCalledByKids = Helpers.ReadNumber(br);
numberOfNewFunctionsBroughtIn = Helpers.ReadNumber(br);
numberOfUnmanagedTransitions = Helpers.ReadNumber(br);
numberOfUnmanagedTransitionsByKids = Helpers.ReadNumber(br);
numberOfAssembliesLoaded = Helpers.ReadNumber(br);
numberOfAssembliesLoadedByKids = Helpers.ReadNumber(br);
firstTimeBroughtIn = (br.ReadBits(1) != 0);
}
示例5: ReadPerPacketData
public override object ReadPerPacketData( VorbisCodec codec, BitReader reader, object args )
{
bool emptyThisFrame = reader.ReadBit() == 0;
if( emptyThisFrame ) return true;
int offset = 0;
yList[offset++] = reader.ReadBits( elems01Bits );
yList[offset++] = reader.ReadBits( elems01Bits );
for( int i = 0; i < partitions; i++ ) {
int classNum = partitionClassList[i];
int cDim = classDimensions[classNum];
int cBits = classSubclasses[classNum];
int cSub = (1 << cBits) - 1;
int cVal = 0;
if( cBits > 0 ) {
int bookNum = classMasterbooks[classNum];
Codebook codebook = codec.codebookConfigs[bookNum];
cVal = codebook.GetScalarContext( reader );
}
for( int j = 0; j < cDim; j++ ) {
int book = subclassBooks[classNum][cVal & cSub];
cVal = (int)((uint)cVal >> cBits);
if( book != 0xFF ) {
Codebook codebook = codec.codebookConfigs[book];
yList[offset++] = codebook.GetScalarContext( reader );
} else {
yList[offset++] = 0;
}
}
}
return false;
}
示例6: ReadSetupData
public override void ReadSetupData( VorbisCodec codec, BitReader reader )
{
partitions = reader.ReadBits( 5 );
int maximumClass = -1;
partitionClassList = new byte[partitions];
for( int i = 0; i < partitionClassList.Length; i++ ) {
int element = reader.ReadBits( 4 );
if( element > maximumClass ) {
maximumClass = element;
}
partitionClassList[i] = (byte)element;
}
classDimensions = new byte[maximumClass + 1];
classSubclasses = new byte[maximumClass + 1];
classMasterbooks = new byte[maximumClass + 1];
subclassBooks = new byte[maximumClass + 1][];
for( int i = 0; i <= maximumClass; i++ ) {
classDimensions[i] = (byte)( reader.ReadBits( 3 ) + 1 );
int subclass = reader.ReadBits( 2 );
classSubclasses[i] = (byte)subclass;
if( subclass > 0 ) {
classMasterbooks[i] = (byte)reader.ReadBits( 8 );
}
int subclassMax = 1 << subclass;
byte[] books = new byte[subclassMax];
for( int j = 0; j < books.Length; j++ ) {
books[j] = (byte)(reader.ReadBits( 8 ) - 1);
}
subclassBooks[i] = books;
}
multiplier = reader.ReadBits( 2 ) + 1;
int rangeBits = reader.ReadBits( 4 );
CalcFloor1Values();
xList = new int[floor1Values];
int xListIndex = 2;
xList[1] = 1 << rangeBits;
for( int i = 0; i < partitions; i++ ) {
int classNumber = partitionClassList[i];
for( int j = 0; j < classDimensions[classNumber]; j++ ) {
xList[xListIndex++] = reader.ReadBits( rangeBits );
}
}
int range = rangeElements[multiplier - 1];
elems01Bits = VorbisUtils.iLog( range - 1 );
yList = new int[floor1Values];
step2Flag = new bool[floor1Values];
finalY = new int[floor1Values];
floor = new int[floor1Values];
}
示例7: ReadNumber
static internal long ReadNumber(BitReader br)
{
ulong tmp;
int quadsToRead = 0;
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 0;
}
else
{
tmp = br.ReadBits(2);
if(tmp == 0)
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 1;
}
else
{
quadsToRead = 9;
}
}
else if(tmp == 1)
{
quadsToRead = 8;
}
else if(tmp == 2)
{
quadsToRead = 5;
}
else
{
tmp = br.ReadBits(2);
if(tmp == 0)
{
quadsToRead = 2;
}
else if(tmp == 1)
{
quadsToRead = 4;
}
else if(tmp == 2)
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 7;
}
else if(tmp == 1)
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 3;
}
else
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 11;
}
else
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 6;
}
else
{
tmp = br.ReadBits(2);
if(tmp == 0)
{
tmp = br.ReadBits(1);
if(tmp == 0)
{
quadsToRead = 16;
}
else
{
quadsToRead = 15;
}
}
else if(tmp == 1)
{
quadsToRead = 14;
}
else if(tmp == 2)
{
quadsToRead = 13;
}
else
{
quadsToRead = 12;
}
}
//.........这里部分代码省略.........
示例8: ProcessConstant
private void ProcessConstant(Frame fr, BitReader str, int bitsPerSample)
{
int value = 0;
value = TruncateTC(str.ReadBits(bitsPerSample),bitsPerSample);
for (ulong i = 0; i < fr.Blocksize; i++)
{
fr.output[channelNum, (int)i] = value;
}
}
示例9: Decompress
// Decode PKWare Compression Library stream.
public static byte[] Decompress(byte[] src)
{
var br = new BitReader(src);
// Are literals coded?
var coded = br.ReadBits(8);
if (coded < 0 || coded > 1)
throw new NotImplementedException("Invalid datastream");
var EncodedLiterals = coded == 1;
// log2(dictionary size) - 6
var dict = br.ReadBits(8);
if (dict < 4 || dict > 6)
throw new InvalidDataException("Invalid dictionary size");
// output state
ushort next = 0; // index of next write location in out[]
var first = true; // true to check distances (for first 4K)
var outBuffer = new byte[MAXWIN]; // output buffer and sliding window
var ms = new MemoryStream();
// decode literals and length/distance pairs
do
{
// length/distance pair
if (br.ReadBits(1) == 1)
{
// Length
var symbol = Decode(lencode, br);
var len = lengthbase[symbol] + br.ReadBits(extra[symbol]);
// Magic number for "done"
if (len == 519)
{
for (var i = 0; i < next; i++)
ms.WriteByte(outBuffer[i]);
break;
}
// Distance
symbol = len == 2 ? 2 : dict;
var dist = Decode(distcode, br) << symbol;
dist += br.ReadBits(symbol);
dist++;
if (first && dist > next)
throw new InvalidDataException("Attempt to jump before data");
// copy length bytes from distance bytes back
do
{
var dest = next;
var source = dest - dist;
var copy = MAXWIN;
if (next < dist)
{
source += copy;
copy = dist;
}
copy -= next;
if (copy > len)
copy = len;
len -= copy;
next += (ushort)copy;
// copy with old-fashioned memcpy semantics
// in case of overlapping ranges. this is NOT
// the same as Array.Copy()
while (copy-- > 0)
outBuffer[dest++] = outBuffer[source++];
// Flush window to outstream
if (next == MAXWIN)
{
for (var i = 0; i < next; i++)
ms.WriteByte(outBuffer[i]);
next = 0;
first = false;
}
} while (len != 0);
}
// literal value
else
{
var symbol = EncodedLiterals ? Decode(litcode, br) : br.ReadBits(8);
outBuffer[next++] = (byte)symbol;
if (next == MAXWIN)
{
for (var i = 0; i < next; i++)
ms.WriteByte(outBuffer[i]);
next = 0;
first = false;
}
}
} while (true);
//.........这里部分代码省略.........
示例10: Decode
/// <summary>Decodes the specified native FLAC file and extracts the raw samples.</summary>
/// <param name="file">The path to the native FLAC file.</param>
/// <param name="sampleRate">Receives the sample rate in Hz.</param>
/// <param name="bitsPerSample">Receives the number of bits per sample.</param>
/// <returns>The raw samples per channel, padded into the most-significant bits (makes all samples 32-bits wide and signed).</returns>
internal static int[][] Decode(string file, out int sampleRate, out int bitsPerSample) {
unchecked {
byte[] bytes = File.ReadAllBytes(file);
BitReader reader = new BitReader(bytes);
// --- identifier ---
if (reader.ReadUInt32BE() != 0x664C6143) {
throw new InvalidDataException();
}
sampleRate = 0;
bitsPerSample = 0;
int numberOfChannels = 0;
int totalNumberOfSamples = 0;
bool streaminfoPresent = false;
byte[] md5 = null;
// --- metadata blocks ---
while (true) {
// --- block header ---
bool lastBlock = reader.ReadBit() == 1;
int blockType = (int)reader.ReadBits(7);
int blockLength = (int)reader.ReadUInt24BE();
if (blockType == 0) {
// --- STREAMINFO ---
if (streaminfoPresent) {
throw new InvalidDataException();
}
if (blockLength != 34) {
throw new InvalidDataException();
}
int minimumBlockSize = (int)reader.ReadUInt16BE();
if (minimumBlockSize < 16) {
throw new InvalidDataException();
}
int maximumBlockSize = (int)reader.ReadUInt16BE();
if (minimumBlockSize > maximumBlockSize | maximumBlockSize > 65535) {
throw new InvalidDataException();
}
int minimumFrameSize = (int)reader.ReadUInt24BE();
int maximumFrameSize = (int)reader.ReadUInt24BE();
if (minimumFrameSize > maximumFrameSize & maximumFrameSize != 0) {
throw new InvalidDataException();
}
sampleRate = (int)reader.ReadBits(20);
if (sampleRate == 0 | sampleRate > 655350) {
throw new InvalidDataException();
}
numberOfChannels = (int)reader.ReadBits(3) + 1;
bitsPerSample = (int)reader.ReadBits(5) + 1;
if (bitsPerSample < 4) {
throw new InvalidDataException();
}
/* total number of samples: 36 bits
* only the lower 31 bits are supported */
if (reader.ReadBits(4) != 0) {
throw new NotSupportedException("Too many samples for this decoder.");
}
totalNumberOfSamples = (int)reader.ReadUInt32BE();
if (totalNumberOfSamples < 0) {
throw new NotSupportedException("Too many samples for this decoder.");
}
md5 = reader.ReadBytes(16);
streaminfoPresent = true;
} else if (blockType >= 1 & blockType <= 6) {
// --- ignored ---
reader.BytePosition += (int)blockLength;
} else {
// --- invalid ---
throw new InvalidDataException();
}
if (lastBlock) {
break;
}
}
// --- prepare samples per channel ---
if (!streaminfoPresent) {
throw new InvalidDataException();
}
int[][] samples = new int[numberOfChannels][];
int sampleCount = totalNumberOfSamples != 0 ? (int)totalNumberOfSamples : 65536;
for (int i = 0; i < numberOfChannels; i++) {
samples[i] = new int[sampleCount];
}
int samplesUsed = 0;
// --- frames ---
while (!reader.EndOfStream()) {
// --- frame header ---
int frameHeaderPosition = reader.BytePosition;
uint syncCode = reader.ReadBits(14);
if (syncCode != 0x3FFE) {
throw new InvalidDataException();
}
uint reserved1 = reader.ReadBit();
if (reserved1 != 0) {
throw new InvalidDataException();
}
uint blockingStrategy = reader.ReadBit();
//.........这里部分代码省略.........
示例11: ReadRice
private void ReadRice(Frame fr, BitReader str)
{
int riceType = str.ReadBits(2);
int riceParam = 0;
int riceParamLen = 0;
int partOrder = 0;
int partSize = 0;
switch (riceType)
{
case 0:
riceParamLen = 4;
break;
case 1:
riceParamLen = 5;
break;
default:
//TODO: exception, handle reserved rice types
return;
}
partOrder = str.ReadBits(4);
int numPartitions = 1 << partOrder;
int fullMask = (1 << (riceParamLen + 1)) - 1;
for (int i = 0; i < numPartitions; i++)
{
riceParam = str.ReadBits(riceParamLen);
if (partOrder == 0)
{
partSize = (int) ((int)fr.Blocksize - order);
}
else if (i == 0)
{
partSize = (int)(Math.Floor(fr.Blocksize / (double)(1 << partOrder))) - (int)(order);
}
else
{
partSize = (int)(Math.Floor(fr.Blocksize / (double)(1 << partOrder)));
}
if (riceParam == fullMask)
{
//unencoded
int residualLen = str.ReadBits(5);
for (int j = 0; j < partSize; j++)
{
residuals[numResiduals++] = str.ReadBits(residualLen);
}
}
else
{
uint uRiceParam = (uint)riceParam;
for (int j = 0; j < partSize; j++)
{
residuals[numResiduals++] = str.ReadRice(riceParam);
}
}
}
}
示例12: ProcessVerbatim
private void ProcessVerbatim(Frame fr, BitReader str, int bitsPerSample)
{
for (ulong i = 0; i < fr.Blocksize; i++)
{
fr.output[channelNum, (int)i] = TruncateTC(str.ReadBits(bitsPerSample),bitsPerSample);
}
}
示例13: ProcessLPC
private void ProcessLPC(Frame fr, BitReader str, int bitsPerSample)
{
for (int i = 0; i < order; i++)
{
samples[numSamples] = TruncateTC(str.ReadBits(bitsPerSample),bitsPerSample);
fr.output[channelNum, (int)numSamples] = samples[numSamples];
numSamples++;
}
int precision = str.ReadBits(4) + 1;
int shift = TruncateTC(str.ReadBits(5),5);
int[] coefficients = new int[32]; //there cannot be more than 32 LPC coefficients
for (int i = 0; i < order; i++)
{
coefficients[i] = TruncateTC(str.ReadBits(precision),precision);
}
ReadRice(fr, str);
//TODO: restore signal, bunch of checks
for (int i = 0; i < numResiduals; i++)
{
long signal = 0;
for (int j = 0; j < order; j++)
{
signal += (long)coefficients[j] * (long)fr.output[channelNum, numSamples - j - 1];
}
fr.output[channelNum, numSamples++] = (int)((long)residuals[i] + (signal >> shift));
if (numSamples == 264)
{
//breakpoint
}
}
}
示例14: ProcessFixed
private void ProcessFixed(Frame fr, BitReader str, int bitsPerSample)
{
for (int i = 0; i < order; i++)
{
samples[numSamples] = TruncateTC(str.ReadBits(bitsPerSample),bitsPerSample);
fr.output[channelNum, (int)numSamples] = samples[numSamples];
numSamples++;
}
ReadRice(fr, str);
//TODO: restore signal, bunch of checks
for (int i = 0; i < numResiduals; i++)
{
switch (order)
{
case 0:
//x[i] = residual[i]
fr.output[channelNum, (int)numSamples++] = residuals[i];
break;
case 1:
//x[i] = residual[i] + x[i-1]
fr.output[channelNum, (int)numSamples] = (residuals[i] + fr.output[channelNum, (int)numSamples - 1]);
numSamples++;
break;
case 2:
//x[i] = residual[i] + 2x[i-1] - x[i-2]
fr.output[channelNum, (int)numSamples] = (residuals[i] + 2 * fr.output[channelNum, (int)numSamples - 1] - fr.output[channelNum, (int)numSamples - 2]);
numSamples++;
break;
case 3:
//x[i] = residual[i] + 3x[i-1] - 3x[i-2] + x[i-3]
fr.output[channelNum, (int)numSamples] = (residuals[i] + 3 * fr.output[channelNum, (int)numSamples - 1] - 3 * fr.output[channelNum, (int)numSamples - 2] + fr.output[channelNum, (int)numSamples - 3]);
numSamples++;
break;
case 4:
//x[i] = residual[i] + 4x[i-1] - 6x[i-2] + 4x[i-3] - x[i-4]
fr.output[channelNum, (int)numSamples] = (residuals[i] + 4 * fr.output[channelNum, (int)numSamples - 1] - 6 * fr.output[channelNum, (int)numSamples - 2] + 4 * fr.output[channelNum, (int)numSamples - 3] - fr.output[channelNum, (int)numSamples - 4]);
numSamples++;
break;
}
}
}
示例15: ReadResiduals
/// <summary>Reads residuals and stores them in the specified array.</summary>
/// <param name="reader">The bit reader.</param>
/// <param name="predictorOrder">The predictor order.</param>
/// <param name="blockSize">The block size.</param>
/// <returns>The signed residuals.</returns>
private static int[] ReadResiduals(BitReader reader, int predictorOrder, int blockSize) {
int[] residuals = new int[blockSize];
uint method = reader.ReadBits(2);
if (method == 0 | method == 1) {
// --- RESIDUAL_CODING_METHOD_PARTITIONED_RICE /
// RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 ---
uint partitionOrder = reader.ReadBits(4);
int numberOfPartitions = 1 << (int)partitionOrder;
int numberOfBits = 4 + (int)method;
uint escape = method == 0 ? (uint)15 : (uint)31;
int offset = predictorOrder;
for (int i = 0; i < numberOfPartitions; i++) {
int riceParameter = (int)reader.ReadBits(numberOfBits);
int numberOfSamples;
if (partitionOrder == 0) {
numberOfSamples = blockSize - predictorOrder;
} else if (i == 0) {
numberOfSamples = (blockSize >> (int)partitionOrder) - predictorOrder;
} else {
numberOfSamples = blockSize >> (int)partitionOrder;
}
if (riceParameter == escape) {
int bitsPerSample = (int)reader.ReadBits(5);
for (int j = 0; j < numberOfSamples; j++) {
residuals[offset + j] = (int)reader.ReadBits(bitsPerSample);
}
offset += numberOfSamples;
} else {
for (int j = 0; j < numberOfSamples; j++) {
int value = reader.ReadRiceEncodedInteger(riceParameter);
residuals[offset + j] = value;
}
offset += numberOfSamples;
}
}
return residuals;
} else {
// --- not supported ---
throw new InvalidDataException();
}
}