What's the best way to find the first JPEG frame tag (0xFFD8) in a proprietary video format?

5 views (last 30 days)
I have a proprietary video file which is essentially a bunch of JPEG frames, plus a 1B-byte frame header on each frame, and an arbitrary-length file header.
After I find the first frame, I can read the entire file, and have already made my own MATLAB viewer for this format.
THE PROBLEM: the very first frame can occur anywhere in, say, the first 5kB. I need to find it quickly; JPEG files begin with a 'FFD8' tag, but this tag can be at an even or odd byte, so simply reading the first 5k once as uint16s won't necessarily find it.
MY SOLUTIONS: The first was to read the first 5k as uint8s with a zero offset, and then to do the same with a one offset; then compare the zero offset with 255, which is hex FF; compare the one offset with 216, which is D8, and AND them:
first_5k_zero_offset = fread( hd_fid , 5000 , 'uint8' ) ;
fseek( hd_fid , 1 , 'bof' ) ;
first_5k_one_offset = fread( hd_fid , 5000 , 'uint8' ) ;
find( and( ( first_1k_zero_offset == 255 ) , ...
( first_1k_one_offset == 216 ) ) )
This works, but I've had to do it a few times and I feel like I'm missing an obvious better way. I also did the equivalent approach by reading zero- and one-offset uint16s, and comparing them to 65496, which is hex 'FFD8.'
I snipped a short portion of a video, just a few frames, and attached it in case you'd like to try playing with the file. So how about it? Is there a smarter way?
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 17 Oct 2013
Edited: Walter Roberson on 17 Oct 2013
Read it once as uint8. strfind() [255 216] in the resulting vector.
Or
find(Vector(1:end-1) == 255 & Vector(2:end) == 216, 1, 'first')
  1 Comment
Brad Clymer
Brad Clymer on 23 Oct 2013
Thanks! I've gotten too buried for a speed comparison, but I'll try one when I have time and let you know. Most appreciated!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!