Changeset 249 for CoolStreaming/UBitStream.pas
- Timestamp:
- May 30, 2011, 7:02:15 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
CoolStreaming/UBitStream.pas
r247 r249 228 228 end; 229 229 230 function TMemoryBitStream.WriteToByte(var Data: Byte; NewData, Pos,Count:Byte):Byte;230 function TMemoryBitStream.WriteToByte(var Data: Byte; NewData, Pos, Count: Byte) :Byte; 231 231 begin 232 232 Data := Byte(Data and not (((1 shl Count) - 1) shl Pos) // Make zero space for new data … … 260 260 TBytes(Buffer)[I] := TBytes(Buffer)[I] or 261 261 ((Integer(Data) and ((1 shl PosInByte) - 1)) shl (8 - PosInByte)); 262 //if (I = (ByteCount - 1)) and (PosInByte > 0) then263 //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); 264 264 end; 265 265 Inc(FPosition, Count); … … 268 268 end; 269 269 270 function TMemoryBitStream.Write(const Buffer; Count:Longint):Longint;270 function TMemoryBitStream.Write(const Buffer; Count: Longint): Longint; 271 271 var 272 272 ByteCount: LongInt; 273 273 BitCount: LongInt; 274 WriteBitCount: Integer; 275 RestBitCount: Integer; 276 NextRestBitCount: Integer; 274 277 I: LongInt; 275 278 BytePos: Byte; … … 286 289 raise EWriteError.Create(SWriteError); 287 290 291 RestBitCount := 0; 292 NextRestBitCount := 0; 288 293 BitCount := Count; 289 294 ByteCount := Ceil(Count / 8); 295 Stream.Position := Trunc(FPosition / 8); 290 296 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)); 298 313 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); 309 317 end; 310 318 Inc(FPosition, Count);
Note:
See TracChangeset
for help on using the changeset viewer.