diff mbox series

[FFmpeg-devel,2/2] libavcodec/jpeg2000dec.c: ROI marker support

Message ID 20200420193739.15378-2-gautamramk@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/2] libavcodec/jpeg2000dec.c: Support for CRG marker | expand

Checks

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

Commit Message

Gautam Ramakrishnan April 20, 2020, 7:37 p.m. UTC
From: Gautam Ramakrishnan <gautamramk@gmail.com>

This patch adds support for decoding images
with a Region of Interest. Allows decoding
samples such as p0_03.j2k. This patch should
fix ticket #4681.
---
 libavcodec/jpeg2000.h    |  1 +
 libavcodec/jpeg2000dec.c | 57 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 51 insertions(+), 7 deletions(-)

Comments

Carl Eugen Hoyos April 20, 2020, 8:43 p.m. UTC | #1
Am Mo., 20. Apr. 2020 um 21:38 Uhr schrieb <gautamramk@gmail.com>:
>
> From: Gautam Ramakrishnan <gautamramk@gmail.com>
>
> This patch adds support for decoding images
> with a Region of Interest. Allows decoding
> samples such as p0_03.j2k. This patch should
> fix ticket #4681.

The following inlined poc makes FFmpeg's output for this
sample bit-exact with kdu_render and opj_decompress.

jasper's output file looks ugly.

Carl Eugen

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index af6dcee228..5380596c04 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1921,7 +1921,9 @@ static inline void tile_codeblocks
                         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;
                            \
                     }
                            \
@@ -1930,7 +1932,9 @@ static inline void tile_codeblocks
                         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;
                            \
                     }
                            \
@@ -1972,7 +1976,7 @@ static int jpeg2000_decode_tile
     }

     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 ||
Michael Niedermayer April 21, 2020, 9:34 p.m. UTC | #2
On Tue, Apr 21, 2020 at 01:07:39AM +0530, gautamramk@gmail.com wrote:
> From: Gautam Ramakrishnan <gautamramk@gmail.com>
> 
> This patch adds support for decoding images
> with a Region of Interest. Allows decoding
> samples such as p0_03.j2k. This patch should
> fix ticket #4681.
> ---
>  libavcodec/jpeg2000.h    |  1 +
>  libavcodec/jpeg2000dec.c | 57 +++++++++++++++++++++++++++++++++++-----
>  2 files changed, 51 insertions(+), 7 deletions(-)
> 
> diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
> index 7b78c0193e..0f82716981 100644
> --- a/libavcodec/jpeg2000.h
> +++ b/libavcodec/jpeg2000.h
> @@ -210,6 +210,7 @@ typedef struct Jpeg2000Component {
>      int *i_data;
>      int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
>      int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
> +    uint8_t roi_shift; // ROI scaling value for the component
>  } Jpeg2000Component;
>  
>  /* misc tools */
> diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
> index 5a7d9e7882..da19345ee7 100644
> --- a/libavcodec/jpeg2000dec.c
> +++ b/libavcodec/jpeg2000dec.c
> @@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext {
>      Jpeg2000CodingStyle codsty[4];
>      Jpeg2000QuantStyle  qntsty[4];
>      Jpeg2000POC         poc;

> +    uint8_t             roi_shift[4];
>  
>      int             bit_index;
>  
> @@ -598,6 +599,29 @@ static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
>      return 0;
>  }
>  
> +static int get_rgn(Jpeg2000DecoderContext *s, int n)
> +{
> +    uint16_t compno;
> +    compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
> +                                     bytestream2_get_be16u(&s->g);
> +    if (bytestream2_get_byte(&s->g)) {
> +        av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
> +        return AVERROR_INVALIDDATA; // SRgn field value is 0
> +    }
> +    // SPrgn field
> +    if (compno < s->ncomponents) {
> +        if (s->curtileno == -1)
> +            s->roi_shift[compno] = bytestream2_get_byte(&s->g);

theres a check for s->ncomponents < 257 implying that if this check is not a
dead check ncomponents can be bigger and then compno is just checked by that
before being used to index into a 4 entry array (roi_shift)

something in here is not entirely correct



> +        else {
> +            if (s->tile[s->curtileno].tp_idx != 0)
> +                return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
> +            s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g);
> +        }
> +        return 0;
> +    }
> +    return AVERROR_INVALIDDATA;
> +}
> +
>  /* Get common part for QCD and QCC segments. */
>  static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
>  {
> @@ -947,6 +971,9 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
>          comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
>          comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
>  
> +        if (!comp->roi_shift)
> +            comp->roi_shift = s->roi_shift[compno];
> +
>          if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
>                                               s->cbps[compno], s->cdx[compno],
>                                               s->cdy[compno], s->avctx))
> @@ -1615,9 +1642,9 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
>  
>  static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
>                         Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
> -                       int width, int height, int bandpos)
> +                       int width, int height, int bandpos, uint8_t roi_shift)
>  {
> -    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
> +    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
>      int pass_cnt = 0;
>      int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
>      int term_cnt = 0;

> @@ -1691,6 +1718,19 @@ static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
>      return 1;
>  }
>  
> +static inline int roi_shift_param(Jpeg2000Component *comp,
> +                                   int quan_parameter)
> +{
> +    uint8_t roi_shift;
> +    int val;
> +    roi_shift = comp->roi_shift;
> +    val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
> +
> +    if (val > (1 << roi_shift))
> +        return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
> +    return quan_parameter;
> +}
> +
>  /* TODO: Verify dequantization for lossless case
>   * comp->data can be float or int
>   * band->stepsize can be float or int
> @@ -1708,7 +1748,7 @@ static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
>          float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
>          int *src = t1->data + j*t1->stride;
>          for (i = 0; i < w; ++i)
> -            datap[i] = src[i] * band->f_stepsize;
> +            datap[i] = roi_shift_param(comp, src[i]) * band->f_stepsize;
>      }
>  }
>  
> @@ -1724,11 +1764,11 @@ static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
>          int *src = t1->data + j*t1->stride;
>          if (band->i_stepsize == 32768) {
>              for (i = 0; i < w; ++i)
> -                datap[i] = src[i] / 2;
> +                datap[i] = roi_shift_param(comp, src[i]) / 2;
>          } else {
>              // This should be VERY uncommon
>              for (i = 0; i < w; ++i)
> -                datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
> +                datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize) / 65536;
>          }
>      }
>  }
> @@ -1743,7 +1783,7 @@ static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
>          int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
>          int *src = t1->data + j*t1->stride;
>          for (i = 0; i < w; ++i)
> -            datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
> +            datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize + (1<<15)) >> 16;
>      }
>  }

the dequantization functions matter speedwise adding the whole 
roi_shift_param() stuff repeatly to each sample would slow the code down
more than neccessary

thx

[...]
Gautam Ramakrishnan April 22, 2020, 3:14 a.m. UTC | #3
On Wed, Apr 22, 2020 at 3:04 AM Michael Niedermayer
<michael@niedermayer.cc> wrote:
>
> On Tue, Apr 21, 2020 at 01:07:39AM +0530, gautamramk@gmail.com wrote:
> > From: Gautam Ramakrishnan <gautamramk@gmail.com>
> >
> > This patch adds support for decoding images
> > with a Region of Interest. Allows decoding
> > samples such as p0_03.j2k. This patch should
> > fix ticket #4681.
> > ---
> >  libavcodec/jpeg2000.h    |  1 +
> >  libavcodec/jpeg2000dec.c | 57 +++++++++++++++++++++++++++++++++++-----
> >  2 files changed, 51 insertions(+), 7 deletions(-)
> >
> > diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
> > index 7b78c0193e..0f82716981 100644
> > --- a/libavcodec/jpeg2000.h
> > +++ b/libavcodec/jpeg2000.h
> > @@ -210,6 +210,7 @@ typedef struct Jpeg2000Component {
> >      int *i_data;
> >      int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
> >      int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
> > +    uint8_t roi_shift; // ROI scaling value for the component
> >  } Jpeg2000Component;
> >
> >  /* misc tools */
> > diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
> > index 5a7d9e7882..da19345ee7 100644
> > --- a/libavcodec/jpeg2000dec.c
> > +++ b/libavcodec/jpeg2000dec.c
> > @@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext {
> >      Jpeg2000CodingStyle codsty[4];
> >      Jpeg2000QuantStyle  qntsty[4];
> >      Jpeg2000POC         poc;
>
> > +    uint8_t             roi_shift[4];
> >
> >      int             bit_index;
> >
> > @@ -598,6 +599,29 @@ static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
> >      return 0;
> >  }
> >
> > +static int get_rgn(Jpeg2000DecoderContext *s, int n)
> > +{
> > +    uint16_t compno;
> > +    compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
> > +                                     bytestream2_get_be16u(&s->g);
> > +    if (bytestream2_get_byte(&s->g)) {
> > +        av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
> > +        return AVERROR_INVALIDDATA; // SRgn field value is 0
> > +    }
> > +    // SPrgn field
> > +    if (compno < s->ncomponents) {
> > +        if (s->curtileno == -1)
> > +            s->roi_shift[compno] = bytestream2_get_byte(&s->g);
>
> theres a check for s->ncomponents < 257 implying that if this check is not a
> dead check ncomponents can be bigger and then compno is just checked by that
> before being used to index into a 4 entry array (roi_shift)
>
> something in here is not entirely correct
For our decoder, it is a dead check as we are supposed to return an error
before this marker reaches if the number of components is greater than 4.
However, I have still kept this check keeping in mind support for more than
4 components in the future.
>
>
>
> > +        else {
> > +            if (s->tile[s->curtileno].tp_idx != 0)
> > +                return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
> > +            s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g);
> > +        }
> > +        return 0;
> > +    }
> > +    return AVERROR_INVALIDDATA;
> > +}
> > +
> >  /* Get common part for QCD and QCC segments. */
> >  static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
> >  {
> > @@ -947,6 +971,9 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
> >          comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
> >          comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
> >
> > +        if (!comp->roi_shift)
> > +            comp->roi_shift = s->roi_shift[compno];
> > +
> >          if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
> >                                               s->cbps[compno], s->cdx[compno],
> >                                               s->cdy[compno], s->avctx))
> > @@ -1615,9 +1642,9 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
> >
> >  static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
> >                         Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
> > -                       int width, int height, int bandpos)
> > +                       int width, int height, int bandpos, uint8_t roi_shift)
> >  {
> > -    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
> > +    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
> >      int pass_cnt = 0;
> >      int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
> >      int term_cnt = 0;
>
> > @@ -1691,6 +1718,19 @@ static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
> >      return 1;
> >  }
> >
> > +static inline int roi_shift_param(Jpeg2000Component *comp,
> > +                                   int quan_parameter)
> > +{
> > +    uint8_t roi_shift;
> > +    int val;
> > +    roi_shift = comp->roi_shift;
> > +    val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
> > +
> > +    if (val > (1 << roi_shift))
> > +        return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
> > +    return quan_parameter;
> > +}
> > +
> >  /* TODO: Verify dequantization for lossless case
> >   * comp->data can be float or int
> >   * band->stepsize can be float or int
> > @@ -1708,7 +1748,7 @@ static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
> >          float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
> >          int *src = t1->data + j*t1->stride;
> >          for (i = 0; i < w; ++i)
> > -            datap[i] = src[i] * band->f_stepsize;
> > +            datap[i] = roi_shift_param(comp, src[i]) * band->f_stepsize;
> >      }
> >  }
> >
> > @@ -1724,11 +1764,11 @@ static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
> >          int *src = t1->data + j*t1->stride;
> >          if (band->i_stepsize == 32768) {
> >              for (i = 0; i < w; ++i)
> > -                datap[i] = src[i] / 2;
> > +                datap[i] = roi_shift_param(comp, src[i]) / 2;
> >          } else {
> >              // This should be VERY uncommon
> >              for (i = 0; i < w; ++i)
> > -                datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
> > +                datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize) / 65536;
> >          }
> >      }
> >  }
> > @@ -1743,7 +1783,7 @@ static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
> >          int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
> >          int *src = t1->data + j*t1->stride;
> >          for (i = 0; i < w; ++i)
> > -            datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
> > +            datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize + (1<<15)) >> 16;
> >      }
> >  }
>
> the dequantization functions matter speedwise adding the whole
> roi_shift_param() stuff repeatly to each sample would slow the code down
> more than neccessary
>
I shall make the shifting conditional now, so the shift is performed beforehand.
> thx
>
> [...]
>
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> I am the wisest man alive, for I know one thing, and that is that I know
> nothing. -- Socrates
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Michael Niedermayer April 22, 2020, 3:23 p.m. UTC | #4
On Wed, Apr 22, 2020 at 08:44:10AM +0530, Gautam Ramakrishnan wrote:
> On Wed, Apr 22, 2020 at 3:04 AM Michael Niedermayer
> <michael@niedermayer.cc> wrote:
> >
> > On Tue, Apr 21, 2020 at 01:07:39AM +0530, gautamramk@gmail.com wrote:
> > > From: Gautam Ramakrishnan <gautamramk@gmail.com>
> > >
> > > This patch adds support for decoding images
> > > with a Region of Interest. Allows decoding
> > > samples such as p0_03.j2k. This patch should
> > > fix ticket #4681.
> > > ---
> > >  libavcodec/jpeg2000.h    |  1 +
> > >  libavcodec/jpeg2000dec.c | 57 +++++++++++++++++++++++++++++++++++-----
> > >  2 files changed, 51 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
> > > index 7b78c0193e..0f82716981 100644
> > > --- a/libavcodec/jpeg2000.h
> > > +++ b/libavcodec/jpeg2000.h
> > > @@ -210,6 +210,7 @@ typedef struct Jpeg2000Component {
> > >      int *i_data;
> > >      int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
> > >      int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
> > > +    uint8_t roi_shift; // ROI scaling value for the component
> > >  } Jpeg2000Component;
> > >
> > >  /* misc tools */
> > > diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
> > > index 5a7d9e7882..da19345ee7 100644
> > > --- a/libavcodec/jpeg2000dec.c
> > > +++ b/libavcodec/jpeg2000dec.c
> > > @@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext {
> > >      Jpeg2000CodingStyle codsty[4];
> > >      Jpeg2000QuantStyle  qntsty[4];
> > >      Jpeg2000POC         poc;
> >
> > > +    uint8_t             roi_shift[4];
> > >
> > >      int             bit_index;
> > >
> > > @@ -598,6 +599,29 @@ static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
> > >      return 0;
> > >  }
> > >
> > > +static int get_rgn(Jpeg2000DecoderContext *s, int n)
> > > +{
> > > +    uint16_t compno;
> > > +    compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
> > > +                                     bytestream2_get_be16u(&s->g);
> > > +    if (bytestream2_get_byte(&s->g)) {
> > > +        av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
> > > +        return AVERROR_INVALIDDATA; // SRgn field value is 0
> > > +    }
> > > +    // SPrgn field
> > > +    if (compno < s->ncomponents) {
> > > +        if (s->curtileno == -1)
> > > +            s->roi_shift[compno] = bytestream2_get_byte(&s->g);
> >
> > theres a check for s->ncomponents < 257 implying that if this check is not a
> > dead check ncomponents can be bigger and then compno is just checked by that
> > before being used to index into a 4 entry array (roi_shift)
> >
> > something in here is not entirely correct
> For our decoder, it is a dead check as we are supposed to return an error
> before this marker reaches if the number of components is greater than 4.
> However, I have still kept this check keeping in mind support for more than
> 4 components in the future.

ok, please add a comment in the code
(this dead check can be detected probably by automated tools and
 without a comment people would otherwise "fix" it)

 [...]
 
thx
diff mbox series

Patch

diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 7b78c0193e..0f82716981 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -210,6 +210,7 @@  typedef struct Jpeg2000Component {
     int *i_data;
     int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
     int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
+    uint8_t roi_shift; // ROI scaling value for the component
 } Jpeg2000Component;
 
 /* misc tools */
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5a7d9e7882..da19345ee7 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -117,6 +117,7 @@  typedef struct Jpeg2000DecoderContext {
     Jpeg2000CodingStyle codsty[4];
     Jpeg2000QuantStyle  qntsty[4];
     Jpeg2000POC         poc;
+    uint8_t             roi_shift[4];
 
     int             bit_index;
 
@@ -598,6 +599,29 @@  static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
     return 0;
 }
 
+static int get_rgn(Jpeg2000DecoderContext *s, int n)
+{
+    uint16_t compno;
+    compno = (s->ncomponents < 257)? bytestream2_get_byte(&s->g):
+                                     bytestream2_get_be16u(&s->g);
+    if (bytestream2_get_byte(&s->g)) {
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
+        return AVERROR_INVALIDDATA; // SRgn field value is 0
+    }
+    // SPrgn field
+    if (compno < s->ncomponents) {
+        if (s->curtileno == -1)
+            s->roi_shift[compno] = bytestream2_get_byte(&s->g);
+        else {
+            if (s->tile[s->curtileno].tp_idx != 0)
+                return AVERROR_INVALIDDATA; // marker occurs only in first tile part of tile
+            s->tile[s->curtileno].comp[compno].roi_shift = bytestream2_get_byte(&s->g);
+        }
+        return 0;
+    }
+    return AVERROR_INVALIDDATA;
+}
+
 /* Get common part for QCD and QCC segments. */
 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
 {
@@ -947,6 +971,9 @@  static int init_tile(Jpeg2000DecoderContext *s, int tileno)
         comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], s->reduction_factor);
         comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], s->reduction_factor);
 
+        if (!comp->roi_shift)
+            comp->roi_shift = s->roi_shift[compno];
+
         if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
                                              s->cbps[compno], s->cdx[compno],
                                              s->cdy[compno], s->avctx))
@@ -1615,9 +1642,9 @@  static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
 
 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
                        Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
-                       int width, int height, int bandpos)
+                       int width, int height, int bandpos, uint8_t roi_shift)
 {
-    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
+    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + roi_shift;
     int pass_cnt = 0;
     int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
     int term_cnt = 0;
@@ -1691,6 +1718,19 @@  static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
     return 1;
 }
 
+static inline int roi_shift_param(Jpeg2000Component *comp,
+                                   int quan_parameter)
+{
+    uint8_t roi_shift;
+    int val;
+    roi_shift = comp->roi_shift;
+    val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
+
+    if (val > (1 << roi_shift))
+        return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
+    return quan_parameter;
+}
+
 /* TODO: Verify dequantization for lossless case
  * comp->data can be float or int
  * band->stepsize can be float or int
@@ -1708,7 +1748,7 @@  static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
         float *datap = &comp->f_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
         int *src = t1->data + j*t1->stride;
         for (i = 0; i < w; ++i)
-            datap[i] = src[i] * band->f_stepsize;
+            datap[i] = roi_shift_param(comp, src[i]) * band->f_stepsize;
     }
 }
 
@@ -1724,11 +1764,11 @@  static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
         int *src = t1->data + j*t1->stride;
         if (band->i_stepsize == 32768) {
             for (i = 0; i < w; ++i)
-                datap[i] = src[i] / 2;
+                datap[i] = roi_shift_param(comp, src[i]) / 2;
         } else {
             // This should be VERY uncommon
             for (i = 0; i < w; ++i)
-                datap[i] = (src[i] * (int64_t)band->i_stepsize) / 65536;
+                datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize) / 65536;
         }
     }
 }
@@ -1743,7 +1783,7 @@  static void dequantization_int_97(int x, int y, Jpeg2000Cblk *cblk,
         int32_t *datap = &comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * (y + j) + x];
         int *src = t1->data + j*t1->stride;
         for (i = 0; i < w; ++i)
-            datap[i] = (src[i] * (int64_t)band->i_stepsize + (1<<15)) >> 16;
+            datap[i] = (roi_shift_param(comp, src[i]) * (int64_t)band->i_stepsize + (1<<15)) >> 16;
     }
 }
 
@@ -1818,7 +1858,7 @@  static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                         int ret = decode_cblk(s, codsty, &t1, cblk,
                                     cblk->coord[0][1] - cblk->coord[0][0],
                                     cblk->coord[1][1] - cblk->coord[1][0],
-                                    bandpos);
+                                    bandpos, comp->roi_shift);
                         if (ret)
                             coded = 1;
                         else
@@ -2046,6 +2086,9 @@  static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
         case JPEG2000_COD:
             ret = get_cod(s, codsty, properties);
             break;
+        case JPEG2000_RGN:
+            ret = get_rgn(s, len);
+            break;
         case JPEG2000_QCC:
             ret = get_qcc(s, len, qntsty, properties);
             break;