Merge commit '29b00f880faa404aa1d0d6820310c510c5996479'
authorHendrik Leppkes <h.leppkes@gmail.com>
Wed, 16 Sep 2015 10:00:53 +0000 (12:00 +0200)
committerHendrik Leppkes <h.leppkes@gmail.com>
Wed, 16 Sep 2015 10:08:39 +0000 (12:08 +0200)
* commit '29b00f880faa404aa1d0d6820310c510c5996479':
  jpeg2000: Templatize the frame writer

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
1  2 
libavcodec/jpeg2000dec.c

@@@ -1679,125 -1118,74 +1679,95 @@@ static inline void tile_codeblocks(Jpeg
      } /*end comp */
  }
  
 -                                         AVFrame * picture)                                       \
+ #define WRITE_FRAME(D, PIXEL)                                                                     \
+     static inline void write_frame_ ## D(Jpeg2000DecoderContext * s, Jpeg2000Tile * tile,         \
 -        int linesize = picture->linesize[0] / sizeof(PIXEL);                                      \
++                                         AVFrame * picture, int precision)                        \
+     {                                                                                             \
 -            y    = tile->comp[compno].coord[1][0] - s->image_offset_y;                            \
 -            line = (PIXEL *)picture->data[0] + y * linesize;                                      \
 -            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) { \
++        const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);               \
++        int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);                              \
++        int pixelsize = planar ? 1 : pixdesc->nb_components;                                      \
++                                                                                                  \
+         int compno;                                                                               \
+         int x, y;                                                                                 \
+                                                                                                   \
+         for (compno = 0; compno < s->ncomponents; compno++) {                                     \
+             Jpeg2000Component *comp     = tile->comp + compno;                                    \
+             Jpeg2000CodingStyle *codsty = tile->codsty + compno;                                  \
+             PIXEL *line;                                                                          \
+             float *datap     = comp->f_data;                                                      \
+             int32_t *i_datap = comp->i_data;                                                      \
+             int cbps         = s->cbps[compno];                                                   \
+             int w            = tile->comp[compno].coord[0][1] - s->image_offset_x;                \
++            int plane        = 0;                                                                 \
++                                                                                                  \
++            if (planar)                                                                           \
++                plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);                 \
+                                                                                                   \
 -                x   = tile->comp[compno].coord[0][0] - s->image_offset_x;                         \
 -                dst = line + x * s->ncomponents + compno;                                         \
++            y    = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];           \
++            line = (PIXEL *)picture->data[plane] + y * (picture->linesize[plane] / sizeof(PIXEL));\
++            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y++) {                 \
+                 PIXEL *dst;                                                                       \
+                                                                                                   \
 -                    for (; x < w; x += s->cdx[compno]) {                                          \
++                x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];        \
++                dst = line + x * pixelsize + compno*!planar;                                      \
+                                                                                                   \
+                 if (codsty->transform == FF_DWT97) {                                              \
 -                        *dst = val << (8 * sizeof(PIXEL) - cbps);                                 \
++                    for (; x < w; x++) {                                                          \
+                         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 += s->ncomponents;                                                    \
++                        *dst = val << (precision - cbps);                                         \
+                         datap++;                                                                  \
 -                    for (; x < w; x += s->cdx[compno]) {                                          \
++                        dst += pixelsize;                                                         \
+                     }                                                                             \
+                 } else {                                                                          \
 -                        *dst = val << (8 * sizeof(PIXEL) - cbps);                                 \
++                    for (; x < w; x++) {                                                          \
+                         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 += s->ncomponents;                                                    \
++                        *dst = val << (precision - cbps);                                         \
+                         i_datap++;                                                                \
 -                line += linesize;                                                                 \
++                        dst += pixelsize;                                                         \
+                     }                                                                             \
+                 }                                                                                 \
++                line += picture->linesize[plane] / sizeof(PIXEL);                                 \
+             }                                                                                     \
+         }                                                                                         \
+                                                                                                   \
+     }
+ WRITE_FRAME(8, uint8_t)
+ WRITE_FRAME(16, uint16_t)
+ #undef WRITE_FRAME
  static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
                                  AVFrame *picture)
  {
-     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
-     int planar    = !!(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
-     int pixelsize = planar ? 1 : pixdesc->nb_components;
-     int compno;
-     int x, y;
-     uint8_t *line;
++    int x;
 +
      tile_codeblocks(s, tile);
  
      /* inverse MCT transformation */
      if (tile->codsty[0].mct)
          mct_decode(s, tile);
  
 +    if (s->cdef[0] < 0) {
 +        for (x = 0; x < s->ncomponents; x++)
 +            s->cdef[x] = x + 1;
 +        if ((s->ncomponents & 1) == 0)
 +            s->cdef[s->ncomponents-1] = 0;
 +    }
 +
      if (s->precision <= 8) {
-         for (compno = 0; compno < s->ncomponents; compno++) {
-             Jpeg2000Component *comp = tile->comp + compno;
-             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
-             float *datap = comp->f_data;
-             int32_t *i_datap = comp->i_data;
-             int cbps = s->cbps[compno];
-             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
-             int plane = 0;
-             if (planar)
-                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
-             y    = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
-             line = picture->data[plane] + y * picture->linesize[plane];
-             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
-                 uint8_t *dst;
-                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
-                 dst = line + x * pixelsize + compno*!planar;
-                 if (codsty->transform == FF_DWT97) {
-                     for (; x < w; x ++) {
-                         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 << (8 - cbps);
-                         datap++;
-                         dst += pixelsize;
-                     }
-                 } else {
-                     for (; x < w; x ++) {
-                         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 << (8 - cbps);
-                         i_datap++;
-                         dst += pixelsize;
-                     }
-                 }
-                 line += picture->linesize[plane];
-             }
-         }
 -        write_frame_8(s, tile, picture);
++        write_frame_8(s, tile, picture, 8);
      } else {
 -        write_frame_16(s, tile, picture);
 +        int precision = picture->format == AV_PIX_FMT_XYZ12 ||
 +                        picture->format == AV_PIX_FMT_RGB48 ||
 +                        picture->format == AV_PIX_FMT_RGBA64 ||
 +                        picture->format == AV_PIX_FMT_GRAY16 ? 16 : s->precision;
 +
-         for (compno = 0; compno < s->ncomponents; compno++) {
-             Jpeg2000Component *comp = tile->comp + compno;
-             Jpeg2000CodingStyle *codsty = tile->codsty + compno;
-             float *datap = comp->f_data;
-             int32_t *i_datap = comp->i_data;
-             uint16_t *linel;
-             int cbps = s->cbps[compno];
-             int w = tile->comp[compno].coord[0][1] - s->image_offset_x;
-             int plane = 0;
-             if (planar)
-                 plane = s->cdef[compno] ? s->cdef[compno]-1 : (s->ncomponents-1);
-             y     = tile->comp[compno].coord[1][0] - s->image_offset_y / s->cdy[compno];
-             linel = (uint16_t *)picture->data[plane] + y * (picture->linesize[plane] >> 1);
-             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y ++) {
-                 uint16_t *dst;
-                 x   = tile->comp[compno].coord[0][0] - s->image_offset_x / s->cdx[compno];
-                 dst = linel + (x * pixelsize + compno*!planar);
-                 if (codsty->transform == FF_DWT97) {
-                     for (; x < w; x ++) {
-                         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);
-                         /* align 12 bit values in little-endian mode */
-                         *dst = val << (precision - cbps);
-                         datap++;
-                         dst += pixelsize;
-                     }
-                 } else {
-                     for (; x < w; x ++) {
-                         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);
-                         /* align 12 bit values in little-endian mode */
-                         *dst = val << (precision - cbps);
-                         i_datap++;
-                         dst += pixelsize;
-                     }
-                 }
-                 linel += picture->linesize[plane] >> 1;
-             }
-         }
++        write_frame_16(s, tile, picture, precision);
      }
  
      return 0;