Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for images not having huffman table #76

Closed
codec-abc opened this issue Aug 25, 2017 · 5 comments
Closed

Support for images not having huffman table #76

codec-abc opened this issue Aug 25, 2017 · 5 comments

Comments

@codec-abc
Copy link

codec-abc commented Aug 25, 2017

Hi,

I try to make use of this library to convert MJPG encoded frames to a format (RGB, RGBA or YUV) that can suite my needs for image processing . This is because on my Windows computer (and maybe others) the only webcam encoding that can provide (somewhat) high resolution with decent framerate is the MJPG one.

Anyway, I am using a modified version of Escapi which is a Rust library around some Windows API for Webcam capture (among other things). I managed to get the MJPG encoded frames buffer but while trying to decode them I got the following error:

Error is invalid JPEG format: scan makes use of unset dc huffman table

After dumping a frame into a file on looking at it in an hex editor I can confirm that the file has no huffman table section. The Windows image viewer cannot open the file but GIMP manage to do it. Plus, inserting some huffman tables before the "start of scan" section from another random jpeg (found on the web) allow me to see it in the Windows image viewer and to decode it using this library. So I am no expert in JPG nor data compression/decompression but it seems that while my buffers are not strictly valid jpeg files because they lack huffman tables, there is a way to decode nonetheless them. Do you think you will be able to add support for this case?

By the way, I can provide some files if you want to take a look at the MJPG buffers.

@codec-abc codec-abc changed the title Support for image not having huffman table Support for images not having huffman table Aug 25, 2017
@kaksmet
Copy link
Member

kaksmet commented Aug 25, 2017

@codec-abc Please upload a sample file and I'll take a look at this.

@codec-abc
Copy link
Author

codec-abc commented Aug 25, 2017

Here you go,

This is the image buffer once I added a huffman table from another jpeg with a hex editor.
bufferfixed

This is the raw buffer that I got from the webcam capture.
mjpgbufferraw

EDIT: They both display well in Chrome and Firefox I did not expected that.

@codec-abc
Copy link
Author

codec-abc commented Aug 27, 2017

I did some reading and it seems that the JPEG standard define some huffman tables (Near the end of the page). I think that how GIMP, Firefox and Chrome are able to display those images. Moreover, it seems that usually most jpg uses these default tables, that's would explain why after pasting the tables from a random jpg it display without problem. I will test my hypothesis and post the result.

@kaksmet
Copy link
Member

kaksmet commented Aug 27, 2017

@codec-abc Yes, it seems that it is valid for MJPEG frames to omit the huffman table and expect some default tables to be used. More info can be found here.

I plan to add support for this behavior by detecting MJPEGs and falling back to these huffman tables if no others has been defined. Hopefully I'll have a patch for it during the week.

@codec-abc
Copy link
Author

codec-abc commented Aug 28, 2017

I ran some test and indeed adding the default huffman tables when none are provided fix this edge case. You may want to look at my commit if you plan to fix this edge case. My fix seems a bit hackish so I am not doing a pull request but feel free to get the huffman tables from it if it's convenient for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants