[PR] avcodec/j2kenc: fix edge case when packet ends on 0xFF (PR #24081)

Niklas Haas via ffmpeg-devel <[email protected]>
Newsgroups gmane.comp.video.ffmpeg.devel
Message-ID <178646247001.59.5100946141711450927@29965ddac10e>
PR #24081 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24081
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24081.patch

ISO/IEC 15444-1:2002 (E), Annex B.10.1 "Bit-stuffing routine":

> The last byte in the packet header shall not be an 0xFF value (thus the
> single zero bit stuffed after a byte with 0xFF must be included even if
> the 0xFF would otherwise have been the last byte).

This matches the behavior seen in the jpeg2000 decoder (jpeg2000_flush),
and the behavior of openjpeg.

Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <[email protected]>

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From 30bc5a274410ab6ae13806f41f2603eb39f81656 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Tue, 11 Aug 2026 17:30:21 +0200
Subject: [PATCH] avcodec/j2kenc: fix edge case when packet ends on 0xFF

ISO/IEC 15444-1:2002 (E), Annex B.10.1 "Bit-stuffing routine":

> The last byte in the packet header shall not be an 0xFF value (thus the
> single zero bit stuffed after a byte with 0xFF must be included even if
> the 0xFF would otherwise have been the last byte).

This matches the behavior seen in the jpeg2000 decoder (jpeg2000_flush),
and the behavior of openjpeg.

Sponsored-by: nxtedition AB
Signed-off-by: Niklas Haas <[email protected]>
---
 libavcodec/j2kenc.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index cba4d18adf..11de7524b4 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -175,6 +175,8 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 {
     if (s->bit_index){
         s->bit_index = 0;
+        if (*s->buf == 0xff)
+            *(++s->buf) = 0;
         s->buf++;
     }
 }
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.