Delphi 7: Reading a block of Bytes from a TFileStream & copying to TMemorySTream

2.6k views Asked by At

I've written a Delphi program which creates MJPEG files, which can be several GB in length. The JPGs are grabbed from a DirectX camera using DSPack. That part works fine and creates a file of JPG images in the format:

FF D8 ....(image data)... FF D9 FF D8 .... (image data)... FF D9 FF D8 etc

FF D8 marks the start of a JPG and FF D9 marks the end. Each JPG is around 21KB in size.

Now, I'm trying to write a matching MJPEG player.

In the Form's FormCreate procedure, I create a FileStream and display the first JPG which works fine:

procedure TForm1.FormCreate(Sender: TObject);
var
  b: Array[0..1] of Byte;
  jpg: TJPEGImage;
begin
 :
 :
   MemoryStream:= TMemoryStream.Create;
   jpg:= TJPEGImage.Create;

   MJPEGStream:= TFileStream.Create(MJPEGFileName, fmOpenRead);

   MJPEGStream.Position:= 0;

   repeat                                   
      MJPEGStream.Read(b[0], 2);            // Find end of first jpg
      MemoryStream.Write(b[0], 2);          // and write to memory
   until (b[0] = $FF) and (b[1] = $D9);

   MemoryStream.Position:= 0;
   jpg.LoadFromStream(memoryStream);
   Image1.Picture.Assign(jpg);
   MemoryStream.Free;
   jpg.Free;
end;

I leave the FileStream open so, hopefully, its Position pointer is retained. I have a button on the form, the intention being to jog forwards one JPG at a time but, although the first 'jog' advances one JPG, subsequent jogs advance a random number of times. Here's the procedure:

procedure TForm1.btnJogForwardClick(Sender: TObject);
var
   b: Array[0..1] of Byte;
   jpg: TJPEGImage;

begin
    MemoryStream:= TMemoryStream.Create;
    try
       repeat
          MJPEGStream.Read(b[0], 2);
          MemoryStream.Write(b[0], 2);
       until ((b[0] = $FF) and (b[1] = $D9));

       MemoryStream.Position:= 0;
       jpg:= TJPEGImage.Create;
       try
         try
           jpg.LoadFromStream(MemoryStream);
           Image1.Picture.Assign(jpg);
         except
         end;
       finally
         jpg.Free;
       end;
    finally
       MemoryStream.Free;
  end;

I've checked with a 3rd Party MJPEG player and that is able to jog frame by frame so I know the MJPEG file is ok. Any suggestions as to why my procedure isn't stepping uniformly frame by frame would be appreciated.

Thanks, John.

1

There are 1 answers

0
vwlowen On

Thanks for the comments and suggestions. I think I've managed to sort it.

const
  JPGSizeMax = 100000;

procedure TForm1.FormCreate(Sender: TObject);
var
   b: Array[0..JPGSizeMax] of Byte;
 :
 :
begin
 :
 :
MJPEGStream:= TFileStream.Create(MJPEGFileName, fmOpenRead);
MJPEGStream.Position:= 0;

MJPEGStream.Read(b[0], JPGSizeMax);
for i:= 0 to JPGSizeMax do
begin
   if (b[i] = $D9) and (b[i-1] = $FF) then
   begin
     Count:= i;
     break;
   end;
end;

MemoryStream.Write(b[0], Count);
FilePosition:= Count + 1;

MemoryStream.Position:= 0;
jpg.LoadFromStream(memoryStream);
Image1.Picture.Assign(jpg);

MemoryStream.Free;
jpg.Free;

end;

The procedure for the Jog button is much the same:

MJPEGStream.Position:= FilePosition;

MJPEGStream.Read(b[0], JPGSizeMax);
for i:= 0 to JPGSizeMax do
begin
   if (b[i] = $D9) and (b[i-1] = $FF) then
   begin
     Count:= i;
     break;
   end;
end;

  memoryStream.Write(b[0], Count);
  FilePosition:= FilePosition + count + 1;

// etc

Thanks again for pointing me in the right direction.

John.