Jpeg restart markers
Asked Answered
D

1

11

I made jpeg decoder, but I didn't implement restart markers logic. That is reason why my program don't work on some images (for example images saved with Photoshop: File->Save As->jpeg). I want to implement restart marker logic, but there is no detailed online explanation how restart marker logic works. Please can anyone tell me more about restart markers, or suggest me online resource where I can read more about it. Thx!

Duhamel answered 5/1, 2012 at 19:40 Comment(0)
B
26

Restart markers are quite simple. They were designed to allow resynchronization after an error. Since most JPEG images are transmitted over error-free channels, they're rarely needed. A restart interval is defined with the FFDD marker as a 2-byte number. This tells how many MCUs between restart markers. When you encounter a restart marker (FFD0-FFD7), reset the DC values (Y,Cr,Cb) to 0 and the bitstream is started on a byte boundary (after the FFDx). It's simply a matter of counting through the restart interval over and over as you decode the image. The restart marker values will increment from FFD0 to FFD7 and then start again at FFD0. The marker value itself is not terribly important, but it can indicate if large chunks of data is missing. Here's an example of how I do it in my decoder. I throw away the restart markers in my bitstream reader.

iRestartCount = iRestartInterval;
for (y=0; y<Height_in_MCUs; y++)
   {
   for (x=0; x<Width_in_MCUs; x++)
       {
       <decode an MCU>
       if (iRestartInterval) // if there is a restart interval defined
          {
          if (--iRestartCount == 0)
             {
             iRestartCount = iRestartInterval; // reset restart inverval counter
             iDCPred0 = iDCPred1 = iDCPred2 = 0; // reset DC predictors
             if (*iBit & 7) // adjust bitstream to start on the next byte boundary
                {
                *iBit += (8 - (*iBit & 7));
                }
             } // if restart interval expired
          } // if restart interval defined
       } // for x
    } // for y

Update: Restart markers now serve a new purpose - to allow multi-threaded JPEG encoders and decoders. Since each "strip" of MCUs has its DC values reset at the beginning of each restart interval and starts on a byte boundary, each restart interval can be independently encoded or decoded by a different thread. An encoder can now arbitrarily divide the task into N threads and then 'glue' the data together with restart markers. For decoders, it's not as easy. If restart markers are present, then each interval can be assigned to a different thread. If not present, you can still do some pre-decoding tricks to split the job into multiple threads.

Brockington answered 6/1, 2012 at 0:13 Comment(6)
Please can you tell me more implementation details and logic?Duhamel
what if it is a multi-component image with interleaving ?Butcherbird
@Butcherbird - As far as I've seen, the restart interval refers to MCUs (minimum coded units) no matter what the configuration. For a 3-component color image (Y/Cb/Cr), the restart interval refers to the groups of 3 DCT blocks. For example, an interval of 100 would mean 300 DCT blocks (100 MCUs).Brockington
@afuna The restart interval is a word value and there is no mystery about it because the MCU size is fixed for each image. The restart marker is a single byte with a modulo-8 value indicating the sequence number so that data loss can be detected.Brockington
Oops. You're right about the value being a number of MCU's. However, although the MCU dimensions are fixed (i.e. 8x8 block etc.) the number of encoded bits (after run-length and huffman encoding) still differ from block to block so how can a decoder know where to start from (unless you mean to decode the whole stream first and then do the IDCT in parallel).Despotism
@afuna of course with variable length coded data you don't know the exact byte offset of each MCU, but because all marker bytes start with FF, it's quick and easy to scan forward to find them.Brockington

© 2022 - 2024 — McMap. All rights reserved.