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.
Thanks for the comments and suggestions. I think I've managed to sort it.
end;
The procedure for the Jog button is much the same:
Thanks again for pointing me in the right direction.
John.