diff mbox series

[FFmpeg-devel] lavc/jpeg2000dec: Scale 4-7bit output to 8 bits

Message ID CAB0OVGq4P8Df2Ybnwv2E9F7zfqv0qgEEvhdg3Nxcqvc02Pvmsg@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] lavc/jpeg2000dec: Scale 4-7bit output to 8 bits
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Carl Eugen Hoyos April 25, 2020, 5:26 p.m. UTC
Hi!

Attached patch makes the output of the reference sample p0_03.j2k
bit-exact with opj_decompress and kdu_render and more similar to
jasper's output.

Please comment, Carl Eugen

Comments

Michael Niedermayer April 25, 2020, 10:59 p.m. UTC | #1
On Sat, Apr 25, 2020 at 07:26:08PM +0200, Carl Eugen Hoyos wrote:
> Hi!
> 
> Attached patch makes the output of the reference sample p0_03.j2k
> bit-exact with opj_decompress and kdu_render and more similar to
> jasper's output.
> 
> Please comment, Carl Eugen

can these per pixel computations be avoided by adjusting
f_stepsize / i_stepsize ?

thx

[...]
Carl Eugen Hoyos April 25, 2020, 11:07 p.m. UTC | #2
Am So., 26. Apr. 2020 um 01:00 Uhr schrieb Michael Niedermayer
<michael@niedermayer.cc>:
>
> On Sat, Apr 25, 2020 at 07:26:08PM +0200, Carl Eugen Hoyos wrote:
> > Hi!
> >
> > Attached patch makes the output of the reference sample p0_03.j2k
> > bit-exact with opj_decompress and kdu_render and more similar to
> > jasper's output.
> >
> > Please comment, Carl Eugen
>
> can these per pixel computations be avoided by adjusting
> f_stepsize / i_stepsize ?

Sorry, I cannot answer this but I realize that the patch probably
only makes sense for the lossless transformation.

Carl Eugen
Michael Niedermayer April 26, 2020, 9:36 p.m. UTC | #3
On Sat, Apr 25, 2020 at 07:26:08PM +0200, Carl Eugen Hoyos wrote:
> Hi!
> 
> Attached patch makes the output of the reference sample p0_03.j2k
> bit-exact with opj_decompress and kdu_render and more similar to
> jasper's output.
> 
> Please comment, Carl Eugen

>  jpeg2000dec.c |   10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 895351be2965009049944f0ceeb42bc5f8cee733  0001-lavc-jpeg2000dec-Scale-4-7-bit-output-to-8-bits.patch
> From de80453a8decd95b4a71cea71b20ba0bd74485cb Mon Sep 17 00:00:00 2001
> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
> Date: Sat, 25 Apr 2020 18:31:22 +0200
> Subject: [PATCH] lavc/jpeg2000dec: Scale 4-7 bit output to 8 bits.
> 
> Makes p0_03.j2k output bitexact with opj_decompress and kdu_render
> and more similar with the output of jasper.
> ---
>  libavcodec/jpeg2000dec.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
> index 460a4ad95c..8d7c729530 100644
> --- a/libavcodec/jpeg2000dec.c
> +++ b/libavcodec/jpeg2000dec.c
> @@ -1938,7 +1938,9 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
>                          int val = lrintf(*datap) + (1 << (cbps - 1));                             \
>                          /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
>                          val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
> -                        *dst = val << (precision - cbps);                                         \
> +                        *dst = val << ((precision < 8 ? 8 : precision) - cbps);                   \
> +                        if (precision < 8)                                                        \
> +                            *dst |= *dst >> (8 - precision);                                      \
>                          datap++;                                                                  \
>                          dst += pixelsize;                                                         \
>                      }                                                                             \
> @@ -1947,7 +1949,9 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
>                          int val = *i_datap + (1 << (cbps - 1));                                   \
>                          /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
>                          val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
> -                        *dst = val << (precision - cbps);                                         \
> +                        *dst = val << ((precision < 8 ? 8 : precision) - cbps);                   \
> +                        if (precision < 8)                                                        \
> +                            *dst |= *dst >> (8 - precision);                                      \
>                          i_datap++;                                                                \
>                          dst += pixelsize;                                                         \
>                      }                                                                             \

the check should be outside the pixel loop, as precission does not change
between pixels, this is already a macro to avoid a 8/16 check. And it would
probably be hard for the compiler to figure out when it can remove which side

thx

[...]
diff mbox series

Patch

From de80453a8decd95b4a71cea71b20ba0bd74485cb Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Date: Sat, 25 Apr 2020 18:31:22 +0200
Subject: [PATCH] lavc/jpeg2000dec: Scale 4-7 bit output to 8 bits.

Makes p0_03.j2k output bitexact with opj_decompress and kdu_render
and more similar with the output of jasper.
---
 libavcodec/jpeg2000dec.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 460a4ad95c..8d7c729530 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1938,7 +1938,9 @@  static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                         int val = lrintf(*datap) + (1 << (cbps - 1));                             \
                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
-                        *dst = val << (precision - cbps);                                         \
+                        *dst = val << ((precision < 8 ? 8 : precision) - cbps);                   \
+                        if (precision < 8)                                                        \
+                            *dst |= *dst >> (8 - precision);                                      \
                         datap++;                                                                  \
                         dst += pixelsize;                                                         \
                     }                                                                             \
@@ -1947,7 +1949,9 @@  static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                         int val = *i_datap + (1 << (cbps - 1));                                   \
                         /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */                  \
                         val  = av_clip(val, 0, (1 << cbps) - 1);                                  \
-                        *dst = val << (precision - cbps);                                         \
+                        *dst = val << ((precision < 8 ? 8 : precision) - cbps);                   \
+                        if (precision < 8)                                                        \
+                            *dst |= *dst >> (8 - precision);                                      \
                         i_datap++;                                                                \
                         dst += pixelsize;                                                         \
                     }                                                                             \
@@ -1989,7 +1993,7 @@  static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
     }
 
     if (s->precision <= 8) {
-        write_frame_8(s, tile, picture, 8);
+        write_frame_8(s, tile, picture, s->precision);
     } else {
         int precision = picture->format == AV_PIX_FMT_XYZ12 ||
                         picture->format == AV_PIX_FMT_RGB48 ||
-- 
2.24.1