Changeset 249


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

Legend:

Unmodified
Added
Removed
  • Common/Common.lpk

    r230 r249  
    44    <PathDelim Value="\"/>
    55    <Name Value="Common"/>
    6     <Author Value="Chronos"/>
     6    <Author Value="Chronos (robie@centrum.cz)"/>
    77    <CompilerOptions>
    88      <Version Value="9"/>
     
    1717    <Description Value="Various libraries"/>
    1818    <License Value="GNU/GPL"/>
    19     <Version Minor="2"/>
     19    <Version Minor="3"/>
    2020    <Files Count="7">
    2121      <Item1>
  • CoolStreaming/Demo/BitStream/BitStreamTest.lpi

    r129 r249  
    4242      </Item2>
    4343    </RequiredPackages>
    44     <Units Count="6">
     44    <Units Count="8">
    4545      <Unit0>
    4646        <Filename Value="BitStreamTest.lpr"/>
    4747        <IsPartOfProject Value="True"/>
    4848        <UnitName Value="BitStreamTest"/>
    49         <EditorIndex Value="2"/>
     49        <EditorIndex Value="3"/>
    5050        <WindowIndex Value="0"/>
    5151        <TopLine Value="1"/>
    5252        <CursorPos X="32" Y="15"/>
    53         <UsageCount Value="20"/>
     53        <UsageCount Value="23"/>
    5454        <Loaded Value="True"/>
    5555        <DefaultSyntaxHighlighter Value="Delphi"/>
     
    6161        <ResourceBaseClass Value="Form"/>
    6262        <UnitName Value="UMainForm"/>
    63         <IsVisibleTab Value="True"/>
    6463        <EditorIndex Value="0"/>
    6564        <WindowIndex Value="0"/>
    66         <TopLine Value="132"/>
    67         <CursorPos X="1" Y="150"/>
    68         <UsageCount Value="20"/>
     65        <TopLine Value="123"/>
     66        <CursorPos X="25" Y="138"/>
     67        <UsageCount Value="23"/>
    6968        <Loaded Value="True"/>
    7069        <LoadedDesigner Value="True"/>
     
    8382        <Filename Value="E:\Programy\Lazarus\lcl\controls.pp"/>
    8483        <UnitName Value="Controls"/>
    85         <EditorIndex Value="3"/>
    8684        <WindowIndex Value="0"/>
    8785        <TopLine Value="1207"/>
    8886        <CursorPos X="14" Y="1220"/>
    8987        <UsageCount Value="10"/>
    90         <Loaded Value="True"/>
    9188      </Unit3>
    9289      <Unit4>
     
    10097      <Unit5>
    10198        <Filename Value="E:\Programy\Lazarus\fpc\2.4.3\source\rtl\objpas\sysutils\sysutilh.inc"/>
    102         <EditorIndex Value="1"/>
    10399        <WindowIndex Value="0"/>
    104100        <TopLine Value="84"/>
    105101        <CursorPos X="79" Y="89"/>
    106102        <UsageCount Value="10"/>
    107         <Loaded Value="True"/>
    108103      </Unit5>
     104      <Unit6>
     105        <Filename Value="..\..\UBitStream.pas"/>
     106        <UnitName Value="UBitStream"/>
     107        <IsVisibleTab Value="True"/>
     108        <EditorIndex Value="1"/>
     109        <WindowIndex Value="0"/>
     110        <TopLine Value="244"/>
     111        <CursorPos X="7" Y="263"/>
     112        <UsageCount Value="11"/>
     113        <Loaded Value="True"/>
     114      </Unit6>
     115      <Unit7>
     116        <Filename Value="H:\0.9.30_2.4.2\fpc\2.4.2\source\rtl\objpas\classes\classesh.inc"/>
     117        <EditorIndex Value="2"/>
     118        <WindowIndex Value="0"/>
     119        <TopLine Value="779"/>
     120        <CursorPos X="14" Y="792"/>
     121        <UsageCount Value="11"/>
     122        <Loaded Value="True"/>
     123      </Unit7>
    109124    </Units>
    110125    <JumpHistory Count="30" HistoryIndex="29">
    111126      <Position1>
    112         <Filename Value="UMainForm.pas"/>
    113         <Caret Line="99" Column="1" TopLine="85"/>
     127        <Filename Value="..\..\UBitStream.pas"/>
     128        <Caret Line="293" Column="1" TopLine="275"/>
    114129      </Position1>
    115130      <Position2>
    116         <Filename Value="UMainForm.pas"/>
    117         <Caret Line="101" Column="1" TopLine="85"/>
     131        <Filename Value="..\..\UBitStream.pas"/>
     132        <Caret Line="294" Column="1" TopLine="275"/>
    118133      </Position2>
    119134      <Position3>
    120         <Filename Value="UMainForm.pas"/>
    121         <Caret Line="94" Column="1" TopLine="85"/>
     135        <Filename Value="..\..\UBitStream.pas"/>
     136        <Caret Line="295" Column="1" TopLine="275"/>
    122137      </Position3>
    123138      <Position4>
    124         <Filename Value="UMainForm.pas"/>
    125         <Caret Line="95" Column="1" TopLine="85"/>
     139        <Filename Value="..\..\UBitStream.pas"/>
     140        <Caret Line="296" Column="1" TopLine="275"/>
    126141      </Position4>
    127142      <Position5>
    128         <Filename Value="UMainForm.pas"/>
    129         <Caret Line="98" Column="1" TopLine="85"/>
     143        <Filename Value="..\..\UBitStream.pas"/>
     144        <Caret Line="297" Column="1" TopLine="276"/>
    130145      </Position5>
    131146      <Position6>
    132         <Filename Value="UMainForm.pas"/>
    133         <Caret Line="99" Column="1" TopLine="85"/>
     147        <Filename Value="..\..\UBitStream.pas"/>
     148        <Caret Line="298" Column="33" TopLine="287"/>
    134149      </Position6>
    135150      <Position7>
    136151        <Filename Value="UMainForm.pas"/>
    137         <Caret Line="101" Column="1" TopLine="85"/>
     152        <Caret Line="183" Column="1" TopLine="169"/>
    138153      </Position7>
    139154      <Position8>
    140         <Filename Value="UMainForm.pas"/>
    141         <Caret Line="94" Column="1" TopLine="85"/>
     155        <Filename Value="..\..\UBitStream.pas"/>
     156        <Caret Line="288" Column="1" TopLine="283"/>
    142157      </Position8>
    143158      <Position9>
    144         <Filename Value="UMainForm.pas"/>
    145         <Caret Line="95" Column="1" TopLine="85"/>
     159        <Filename Value="..\..\UBitStream.pas"/>
     160        <Caret Line="291" Column="1" TopLine="283"/>
    146161      </Position9>
    147162      <Position10>
    148         <Filename Value="UMainForm.pas"/>
    149         <Caret Line="99" Column="1" TopLine="85"/>
     163        <Filename Value="..\..\UBitStream.pas"/>
     164        <Caret Line="292" Column="1" TopLine="283"/>
    150165      </Position10>
    151166      <Position11>
    152         <Filename Value="UMainForm.pas"/>
    153         <Caret Line="119" Column="27" TopLine="109"/>
     167        <Filename Value="..\..\UBitStream.pas"/>
     168        <Caret Line="293" Column="1" TopLine="283"/>
    154169      </Position11>
    155170      <Position12>
    156         <Filename Value="UMainForm.pas"/>
    157         <Caret Line="99" Column="32" TopLine="78"/>
     171        <Filename Value="..\..\UBitStream.pas"/>
     172        <Caret Line="294" Column="1" TopLine="283"/>
    158173      </Position12>
    159174      <Position13>
    160         <Filename Value="UMainForm.pas"/>
    161         <Caret Line="98" Column="29" TopLine="78"/>
     175        <Filename Value="..\..\UBitStream.pas"/>
     176        <Caret Line="295" Column="1" TopLine="283"/>
    162177      </Position13>
    163178      <Position14>
    164         <Filename Value="UMainForm.pas"/>
    165         <Caret Line="102" Column="14" TopLine="78"/>
     179        <Filename Value="..\..\UBitStream.pas"/>
     180        <Caret Line="296" Column="1" TopLine="283"/>
    166181      </Position14>
    167182      <Position15>
    168         <Filename Value="BitStreamTest.lpr"/>
    169         <Caret Line="19" Column="1" TopLine="1"/>
     183        <Filename Value="..\..\UBitStream.pas"/>
     184        <Caret Line="297" Column="1" TopLine="283"/>
    170185      </Position15>
    171186      <Position16>
    172         <Filename Value="UMainForm.pas"/>
    173         <Caret Line="101" Column="12" TopLine="85"/>
     187        <Filename Value="..\..\UBitStream.pas"/>
     188        <Caret Line="298" Column="1" TopLine="283"/>
    174189      </Position16>
    175190      <Position17>
    176         <Filename Value="UMainForm.pas"/>
    177         <Caret Line="111" Column="49" TopLine="111"/>
     191        <Filename Value="..\..\UBitStream.pas"/>
     192        <Caret Line="299" Column="1" TopLine="283"/>
    178193      </Position17>
    179194      <Position18>
    180         <Filename Value="UMainForm.pas"/>
    181         <Caret Line="24" Column="59" TopLine="23"/>
     195        <Filename Value="..\..\UBitStream.pas"/>
     196        <Caret Line="300" Column="1" TopLine="286"/>
    182197      </Position18>
    183198      <Position19>
    184         <Filename Value="UMainForm.pas"/>
    185         <Caret Line="58" Column="21" TopLine="45"/>
     199        <Filename Value="..\..\UBitStream.pas"/>
     200        <Caret Line="303" Column="1" TopLine="286"/>
    186201      </Position19>
    187202      <Position20>
    188         <Filename Value="UMainForm.pas"/>
    189         <Caret Line="72" Column="9" TopLine="59"/>
     203        <Filename Value="..\..\UBitStream.pas"/>
     204        <Caret Line="306" Column="1" TopLine="286"/>
    190205      </Position20>
    191206      <Position21>
    192         <Filename Value="BitStreamTest.lpr"/>
    193         <Caret Line="19" Column="1" TopLine="1"/>
     207        <Filename Value="..\..\UBitStream.pas"/>
     208        <Caret Line="312" Column="1" TopLine="299"/>
    194209      </Position21>
    195210      <Position22>
    196         <Filename Value="BitStreamTest.lpr"/>
    197         <Caret Line="18" Column="29" TopLine="1"/>
     211        <Filename Value="..\..\UBitStream.pas"/>
     212        <Caret Line="313" Column="1" TopLine="299"/>
    198213      </Position22>
    199214      <Position23>
    200         <Filename Value="UMainForm.pas"/>
    201         <Caret Line="89" Column="23" TopLine="80"/>
     215        <Filename Value="..\..\UBitStream.pas"/>
     216        <Caret Line="315" Column="1" TopLine="299"/>
    202217      </Position23>
    203218      <Position24>
    204         <Filename Value="UMainForm.pas"/>
    205         <Caret Line="15" Column="1" TopLine="3"/>
     219        <Filename Value="..\..\UBitStream.pas"/>
     220        <Caret Line="316" Column="1" TopLine="299"/>
    206221      </Position24>
    207222      <Position25>
    208         <Filename Value="UMainForm.pas"/>
    209         <Caret Line="56" Column="11" TopLine="44"/>
     223        <Filename Value="..\..\UBitStream.pas"/>
     224        <Caret Line="298" Column="14" TopLine="287"/>
    210225      </Position25>
    211226      <Position26>
    212227        <Filename Value="UMainForm.pas"/>
    213         <Caret Line="76" Column="3" TopLine="69"/>
     228        <Caret Line="192" Column="14" TopLine="179"/>
    214229      </Position26>
    215230      <Position27>
    216231        <Filename Value="UMainForm.pas"/>
    217         <Caret Line="15" Column="7" TopLine="3"/>
     232        <Caret Line="197" Column="1" TopLine="179"/>
    218233      </Position27>
    219234      <Position28>
    220235        <Filename Value="UMainForm.pas"/>
    221         <Caret Line="11" Column="1" TopLine="1"/>
     236        <Caret Line="190" Column="1" TopLine="167"/>
    222237      </Position28>
    223238      <Position29>
    224239        <Filename Value="UMainForm.pas"/>
    225         <Caret Line="124" Column="53" TopLine="112"/>
     240        <Caret Line="191" Column="44" TopLine="155"/>
    226241      </Position29>
    227242      <Position30>
    228243        <Filename Value="UMainForm.pas"/>
    229         <Caret Line="143" Column="50" TopLine="131"/>
     244        <Caret Line="138" Column="25" TopLine="123"/>
    230245      </Position30>
    231246    </JumpHistory>
  • CoolStreaming/Demo/BitStream/UMainForm.lfm

    r122 r249  
    88  ClientWidth = 613
    99  OnShow = FormShow
    10   LCLVersion = '0.9.31'
     10  LCLVersion = '0.9.30'
    1111  object Memo1: TMemo
    12     Left = 7
    13     Height = 372
    14     Top = 8
     12    Left = 8
     13    Height = 364
     14    Top = 40
    1515    Width = 599
    1616    Anchors = [akTop, akLeft, akRight, akBottom]
     
    2020    TabOrder = 0
    2121  end
     22  object Button1: TButton
     23    Left = 9
     24    Height = 25
     25    Top = 7
     26    Width = 75
     27    Caption = 'Base test'
     28    OnClick = Button1Click
     29    TabOrder = 1
     30  end
     31  object Button2: TButton
     32    Left = 93
     33    Height = 25
     34    Top = 7
     35    Width = 75
     36    Caption = 'Read test'
     37    OnClick = Button2Click
     38    TabOrder = 2
     39  end
     40  object Button3: TButton
     41    Left = 176
     42    Height = 25
     43    Top = 7
     44    Width = 75
     45    Caption = 'Write test'
     46    OnClick = Button3Click
     47    TabOrder = 3
     48  end
    2249end
  • CoolStreaming/Demo/BitStream/UMainForm.pas

    r129 r249  
    1515
    1616  TMainForm = class(TForm)
     17    Button1: TButton;
     18    Button2: TButton;
     19    Button3: TButton;
    1720    Memo1: TMemo;
     21    procedure Button1Click(Sender: TObject);
     22    procedure Button2Click(Sender: TObject);
     23    procedure Button3Click(Sender: TObject);
    1824    procedure FormShow(Sender: TObject);
    1925  private
     
    3238
    3339procedure TMainForm.FormShow(Sender: TObject);
     40begin
     41  WindowState := wsMaximized;
     42end;
     43
     44procedure TMainForm.Button1Click(Sender: TObject);
    3445var
    3546  BitStream: TMemoryBitStream;
     
    4051  C: Integer;
    4152begin
    42   WindowState := wsMaximized;
    43 
    4453  with Memo1, Lines do try
    4554    BeginUpdate;
     
    4756    BitStream2 := TMemoryBitStream.Create;
    4857    SetLength(Buffer, 4);
    49     Buffer[0] := $12;
    50     Buffer[1] := $34;
    51     Buffer[2] := $56;
    52     Buffer[3] := $78;
     58    Buffer[0] := $21;
     59    Buffer[1] := $43;
     60    Buffer[2] := $65;
     61    Buffer[3] := $87;
    5362    Add('Source data:');
    5463    PrintData(Buffer, Length(Buffer) * 8, Lines);
    5564
    56     BitStream.Write(Buffer[0], 28);
     65    BitStream.Write(Buffer[0], Length(Buffer) * 8);
    5766    Add('Write data to stream:');
    5867    PrintBitStream(BitStream, Lines);
     68
    5969    // Write second bit array after first which lead to store data as shifted
    60     BitStream.Write(Buffer[0], 28);
     70    BitStream.Write(Buffer[0], Length(Buffer) * 8);
    6171    Add('Append shifted data to stream:');
    6272    PrintBitStream(BitStream, Lines);
     
    8393      PrintBitStream(BitStream2, Lines);
    8494    end;  *)
     95  finally
     96    BitStream.Free;
     97    BitStream2.Free;
     98    EndUpdate;
     99  end;
     100end;
     101
     102procedure TMainForm.Button2Click(Sender: TObject);
     103var
     104  BitStream: TMemoryBitStream;
     105  BitStream2: TMemoryBitStream;
     106  Buffer: array of Byte;
     107  I: Integer;
     108  Y: Integer;
     109  C: Integer;
     110begin
     111  with Memo1, Lines do try
     112    BeginUpdate;
     113    BitStream := TMemoryBitStream.Create;
     114    BitStream2 := TMemoryBitStream.Create;
     115
     116    SetLength(Buffer, 4);
     117    Buffer[0] := $21;
     118    Buffer[1] := $43;
     119    Buffer[2] := $65;
     120    Buffer[3] := $87;
     121    Add('Source data:');
     122    PrintData(Buffer, Length(Buffer) * 8, Lines);
     123
     124    BitStream.Write(Buffer[0], Length(Buffer) * 8);
     125    Add('Write data to stream:');
     126    PrintBitStream(BitStream, Lines);
    85127
    86128    Add('Bit copy of substreams');
     
    107149end;
    108150
     151procedure TMainForm.Button3Click(Sender: TObject);
     152var
     153  BitStream: TMemoryBitStream;
     154  BitStream2: TMemoryBitStream;
     155  Buffer: array of Byte;
     156  I: Integer;
     157  Y: Integer;
     158  C: Integer;
     159begin
     160  with Memo1, Lines do try
     161    BeginUpdate;
     162    BitStream := TMemoryBitStream.Create;
     163    BitStream2 := TMemoryBitStream.Create;
     164
     165    SetLength(Buffer, 5);
     166    Buffer[0] := $10;
     167    Buffer[1] := $32;
     168    Buffer[2] := $54;
     169    Buffer[3] := $76;
     170    Buffer[4] := $98;
     171    Add('Source data:');
     172    PrintData(Buffer, Length(Buffer) * 8, Lines);
     173
     174    Add('Bit copy of substreams');
     175    BitStream.Size := Length(Buffer) * 8;
     176    SetLength(Buffer, 1000);
     177    for Y := 0 to BitStream.Size do begin
     178      Add('Size: ' + IntToStr(Y));
     179      for I := 0 to BitStream.Size - Y do begin
     180        BitStream.Position := 0;
     181        for C := 0 to BitStream.Size - 1 do
     182          BitStream.WriteBit(True);
     183        BitStream.Position := I;
     184        C := BitStream.Write(Buffer[0], Y);
     185        PrintBitStream(BitStream, Lines);
     186      end;
     187      Add('');
     188    end;
     189
     190    Add('Bit copy of substreams');
     191    SetLength(Buffer, 1000);
     192    for Y := 0 to BitStream.Size do begin
     193      Add('Size: ' + IntToStr(Y));
     194      for I := 0 to BitStream.Size - Y do begin
     195        BitStream.Position := 0;
     196        for C := 0 to BitStream.Size - 1 do
     197          BitStream.WriteBit(False);
     198        BitStream.Position := I;
     199        C := BitStream.Write(Buffer[0], Y);
     200        PrintBitStream(BitStream, Lines);
     201      end;
     202      Add('');
     203    end;
     204  finally
     205    BitStream.Free;
     206    BitStream2.Free;
     207    EndUpdate;
     208  end;
     209end;
     210
    109211procedure TMainForm.PrintData(Data: TArrayOfByte; Count: Integer; StringList: TStrings);
    110212var
  • 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.