If you get a Unknown or invalid JPEG Marker this means one of two:
- The photo viewer or editor encountered a JPEG marker it isn’t familiar with.
- The JPEG file is corrupt.
To rule out cause 1, your photo viewer editor doesn’t recognize a certain JPEG marker, update your software and if that does not help, try different software. If none of those help resolve the error then we can assume the cause is the JPEG file being corrupt.
Reference: Full list of JPEG Markers.
JPEG Markers
JPEGs consist of different sections. Each section contains information and data which is needed to decode an image. A section starts with a JPEG Marker which tells what data is inside the section, and the length of the section. In order to be able to display a JPEG a photo viewer or editor needs to process this chain of sections. As each JPEG marker tells the length of a section the software knows where to find the next JPEG marker and section. A JPEG marker is always a combination of 2 bytes, the first byte being 0xFF.
What each ‘section’ or marker contains is outside the scope of this post and quite irrelevant, really. Basically there are 3 scenarios that can cause a JPEG marker to be invalid:
- JPEG Marker is corrupt
- JPEG Marker length value is corrupt
- JPEG Marker in encoded/compressed image data
JPEG Marker is corrupt
Here you can find a more or less complete list of JPEG markers. It’s a combination of two bytes, the first byte always being 0xFF. If 0xFF for whatever reason changes to any other value the photo viewer or editor you’re using will error out.
JPEG Marker length value is incorrect
The 2 bytes for storing the length of the section allows the software to find the next marker. If this is value is incorrect it will not be able to. It will jump to an incorrect byte address, interpret whatever data it finds there as a JPEG marker, which in fact isn’t a JPEG marker and may report an unknown or invalid JPEG Marker.
JPEG Marker in encoded & compressed image data
You may have noticed that the JPEG Marker for the actual encoded and compressed image data section does not specify a length. Image data is a stream that simply is decompressed and decoded until an End Of Image (EOI) marker, 0xFF D9 is encountered.
I already explained Normally you will not find any JPEG Markers inside the encoded data with the following exceptions:
- 0xFF, 00. As the result of encoding and compression data may actually be value 0xFF (255 in decimals) we need a way to store this and separate from an actual JPEG marker. To do so it must be followed by a value of 0x00.
- 0xFF, Dn where n is a value from 0 to 7, for example 0xFF, D5. These are so called Restart Markers.
Repairing an Unknown or Invalid JPEG Marker
Let’s for now call all JPEG markers proceeding the actual payload, the image data the JPEG header. Then we have two situations, the Unknown or invalid JPEG Marker is either encountered in the JPEG header or in the image data.
In header
Unknown or Invalid JPEG Markers in the header portion can be recognized by the JPEG image not being displayed at all.
By far the easiest method to repair an Unknown or Invalid JPEG Marker in the header portion is by replacing the header entirely with an intact header, shot with the same camera and settings the the corrupt file.
You can do this using a HEX editor or by using software designed for this such as my JPEG-Repair tool.
If you are familiar with using a hex editor, copy the portion of a healthy JPEG right from the start (FF D8) to the latest instance you can find of byte pair FF DA. This is your healthy header.
Open the corrupt file and find the latest instance of FF DA you can find in the corrupted file. Overwrite anything before it with your intact header.
In image data
If an unknown or invalid JPEG Marker is encountered in the image or scan data, the photo viewer or editor will probably stop decoding that point. Result is very often the image is grey from that point on.
Using a hex editor you’d basically need to find any FF xx byte combination that is not FF 00 or FF Dn (n being 0 to 7) in the compressed image stream and modify it. Alternatively use JPEGSnoop to help you find the invalid JPEG markers.
If you are looking for a more automatic method you can use my tool JPEG-Repair.
In JPEG-Repair select the Patch tool, then slick the folder icon and select the corrupt JPEG. Click Repair. Repairs are done automatically and result is saved to a new file. It is likely additional repairs are required because in my experience 9 out of 10 times it is not just the bytes that result in invalid JPEG markers that are corrupt.
Possible causes
In practice I see 2 main causes for this type of errors:
- File is entirely corrupt and contains no (JPEG) data at all. If you open these files in a hex editor you see only zeros or a repeating byte pattern (example: FF FF FF etc.).
- File is the result of undelete or file recovery and incorrectly recovered. File data may have already been overwritten after deletion or the software used to recover the file made a mistake. In such a case I find all sorts of data inside a file which may be part of other files or directory structures.
- Uncorrectable read errors. Commonly the files I am repairing are one some kind of flash memory (USB key, SD Card, CF Card etc.). One ‘flipped’ bit that can not be ECC corrected is all it takes to change 0xFE to 0xFF. (in binary: 1111 1110 vs. 1111 1111) which is the difference between a valid value in the image stream and a JPEG Marker.