Skip to content

jpeg: reconstruct baseline bitstream for NVDEC#422

Merged
elFarto merged 1 commit intoelFarto:masterfrom
alper-han:jpeg
Mar 10, 2026
Merged

jpeg: reconstruct baseline bitstream for NVDEC#422
elFarto merged 1 commit intoelFarto:masterfrom
alper-han:jpeg

Conversation

@alper-han
Copy link
Contributor

@alper-han alper-han commented Feb 19, 2026

Description

Previously, the JPEG codec was essentially unusable due to a structural mismatch: VA-API provides JPEG data as separate buffers (picture params, quant tables, Huffman tables, slice params/data), whereas NVDEC expects a fully formed JPEG bitstream.

To fix this, this implementation reconstructs a minimal JFIF stream directly from the VA-API buffers (SOI/APP0/DQT/SOF0/DHT/[optional DRI]/SOS + entropy data + EOI) before handing it off to NVDEC.

Implementation Details

  • Huffman Tables: The decoder uses VA Huffman tables when they are provided. If they are missing or invalid, it safely falls back to the standard baseline tables.
  • Slices: It validates slice bounds and currently expects VA_SLICE_DATA_FLAG_ALL.

Testing

  • Decoded various sample JPEGs via VA-API on NixOS.
  • Compared the raw NV12 output against the native NVDEC (CUDA) output. The results are byte-identical and hash-verified for the test cases.

Known Limitations

  • Baseline only: Progressive JPEG support is out of scope for this PR.
  • Restart Intervals: While it writes the DRI marker when a restart_interval is provided, it doesn't currently insert RST markers into the entropy stream.

@elFarto
Copy link
Owner

elFarto commented Feb 21, 2026

There's a lot of code to go through there.

The first issue I can see is the JPEG context handling. I realise you need to keep track of things and we don't really have a good place to store them (we had this issue with one of the other codecs). I think we can add a void* codecData to the NVContext struct. That should be specific to just this decoder instance, and you can put your JPEGContext in there.

The other issue is the mixed usage of 'uintxx_t' style declarations and 'int/unsigned int' style. We should stick to just uintxx_t style for new code.

@elFarto
Copy link
Owner

elFarto commented Mar 4, 2026

I've reviewed this again (with the help of AI), and the one thing it's thrown up that should probably be fixed is the JPEGContext isn't reset between images. So values set in say validQuantTablesMask one image, would be carried over to the next image. We should probably just memset the whole context to 0 for each image.

Rebuild a minimal JFIF stream from VA-API JPEG buffers (PicParam/IQMatrix/DHT/Slice)
and submit it to NVDEC as a single slice. Validate slice bounds and fall back to
standard Huffman tables when VA tables are missing/invalid.
@elFarto
Copy link
Owner

elFarto commented Mar 10, 2026

Ok, I think we can merge this in now. Thanks for the patch.

@elFarto elFarto merged commit c4e1788 into elFarto:master Mar 10, 2026
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants