Ignore:
Timestamp:
May 30, 2011, 7:02:15 AM (13 years ago)
Author:
george
Message:
  • Fixed: Writing byte buffer to bitstream.
  • Modified: Better demo testing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolStreaming/UBitStream.pas

    r247 r249  
    228228end;
    229229
    230 function TMemoryBitStream.WriteToByte(var Data: Byte; NewData,Pos,Count:Byte):Byte;
     230function TMemoryBitStream.WriteToByte(var Data: Byte; NewData, Pos, Count: Byte) :Byte;
    231231begin
    232232  Data := Byte(Data and not (((1 shl Count) - 1) shl Pos) // Make zero space for new data
     
    260260        TBytes(Buffer)[I] := TBytes(Buffer)[I] or
    261261          ((Integer(Data) and ((1 shl PosInByte) - 1)) shl (8 - PosInByte));
    262       //if (I = (ByteCount - 1)) and (PosInByte > 0) then
    263       //  TBytes(Buffer)[I] := TBytes(Buffer)[I] and ((1 shl (Count mod 8)) - 1);
     262      if (I = (ByteCount - 1)) and (PosInByte > 0) then
     263        TBytes(Buffer)[I] := TBytes(Buffer)[I] and ((1 shl (Count mod 8)) - 1);
    264264    end;
    265265    Inc(FPosition, Count);
     
    268268end;
    269269
    270 function TMemoryBitStream.Write(const Buffer;Count:Longint):Longint;
     270function TMemoryBitStream.Write(const Buffer; Count: Longint): Longint;
    271271var
    272272  ByteCount: LongInt;
    273273  BitCount: LongInt;
     274  WriteBitCount: Integer;
     275  RestBitCount: Integer;
     276  NextRestBitCount: Integer;
    274277  I: LongInt;
    275278  BytePos: Byte;
     
    286289    raise EWriteError.Create(SWriteError);
    287290
     291  RestBitCount := 0;
     292  NextRestBitCount := 0;
    288293  BitCount := Count;
    289294  ByteCount := Ceil(Count / 8);
     295  Stream.Position := Trunc(FPosition / 8);
    290296  BytePos := FPosition mod 8;
    291   Stream.Position := Trunc(FPosition / 8);
    292   if Stream.Position < Stream.Size then begin
    293     Data := Stream.ReadByte;
    294     Stream.Position := Stream.Position - 1;
    295   end else Data := 0;
    296   for I := 0 to ByteCount - 1 do begin
    297     Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I], BytePos, Min(8 - BytePos, BitCount)));
     297  I := 0;
     298  while (I < ByteCount) or (RestBitCount > 0) do begin
     299    WriteBitCount := Min(8 - BytePos, BitCount);
     300    if (Stream.Position < Stream.Size) and (WriteBitCount < 8) then begin
     301      Data := Stream.ReadByte;
     302      Stream.Position := Stream.Position - 1;
     303    end else Data := 0;
     304
     305    // Write rest of previous source byte to target
     306    if RestBitCount > 0 then begin
     307      Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I - 1] shr (8 - BytePos), 0, RestBitCount));
     308      WriteBitCount := Min(8 - BytePos, BitCount);
     309    end;
     310
     311    // Write part up to one byte from source to target
     312    Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I], BytePos, WriteBitCount));
    298313    Stream.WriteByte(Data);
    299     Data := 0;
    300     if (BitCount > 0) and (BytePos > 0) then begin
    301       if (I = (ByteCount - 1)) and (Stream.Position < Stream.Size) then begin
    302         Data := Stream.ReadByte;
    303         Stream.Position := Stream.Position - 1;
    304       end;
    305       Dec(BitCount, WriteToByte(Data, TBytes(Buffer)[I] shr (8 - BytePos), 0, Min(BytePos, BitCount)));
    306       if I = (ByteCount - 1) then
    307         Stream.WriteByte(Data);
    308     end;
     314
     315    RestBitCount := Min(8 - WriteBitCount, BitCount);
     316    Inc(I);
    309317  end;
    310318  Inc(FPosition, Count);
Note: See TracChangeset for help on using the changeset viewer.