Search code examples
imageparsingimage-processingjpeg

Detect MCU boundaries in JPEG


Could please someone help me with parsing jpeg.

Is there a way do detect MCU (8x8px blocks) boundaries in encoded data in SOS section without actually decode jpeg?

It is easy to detect fragments by looking for fragment markers [0xff 0xd0..0xd7].

`

[SOS header]
[3 bytes to skip]
[Segment 0: [MCU][MCU][MCU][MCU][MCU]...0xff 0xd0-7]
[Segment 1: [MCU][MCU][MCU][MCU][MCU]...0xff 0xd0-7]
...
[Segment x: [MCU][MCU][MCU][MCU][MCU]...0xff 0xd0-7]
[End of SOS: 0xffd9]

`

Now I need to split segment to MCUs, and I don' see any options except to just decode it, which I prefer not to do.

Thank you!


Solution

  • Sadly, the answer is not really. A JPEG stream might contain restart markers, when used, they are usually placed between multiples of MCUs. The only real way to detect an MCU boundary is to entropy decode the scan.