diff mbox series

[FFmpeg-devel,01/13] lavc/jpeg2000dec: Finer granularity threading

Message ID 10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se
State New
Headers show
Series [FFmpeg-devel,01/13] lavc/jpeg2000dec: Finer granularity threading | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Tomas Härdin June 14, 2022, 2:39 p.m. UTC
Patch 12 in this series is optional since it's just me getting the
speed up on a specific machine

/Tomas

Comments

Anton Khirnov June 18, 2022, 2:50 p.m. UTC | #1
Quoting Tomas Härdin (2022-06-14 16:39:00)
> Patch 12 in this series is optional since it's just me getting the
> speed up on a specific machine
> 
> /Tomas
> 
> From 115aa26c343419e81c1b5ba0bfdb1615cbec27e9 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
> Date: Fri, 10 Jun 2022 14:10:02 +0200
> Subject: [PATCH 01/13] lavc/jpeg2000dec: Finer granularity threading
> 
> Decoding and dequant is now threaded on codeblock level.
> IDWT is threaded on component level.
> MCT and write_frame() remain threaded on tile level.
> 
> This brings lossless 4K J2K with -lowres 2 -thread_type slice -threads 96 on an AMD EPYC 7R32 from 4.8 fps (177% CPU) to 31 fps (1284% CPU).

Any measurable impact on single-threaded or frame-threaded decoding?
Tomas Härdin June 24, 2022, 8:19 a.m. UTC | #2
lör 2022-06-18 klockan 16:50 +0200 skrev Anton Khirnov:
> Quoting Tomas Härdin (2022-06-14 16:39:00)
> > Patch 12 in this series is optional since it's just me getting the
> > speed up on a specific machine
> > 
> > /Tomas
> > 
> > From 115aa26c343419e81c1b5ba0bfdb1615cbec27e9 Mon Sep 17 00:00:00
> > 2001
> > From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
> > Date: Fri, 10 Jun 2022 14:10:02 +0200
> > Subject: [PATCH 01/13] lavc/jpeg2000dec: Finer granularity
> > threading
> > 
> > Decoding and dequant is now threaded on codeblock level.
> > IDWT is threaded on component level.
> > MCT and write_frame() remain threaded on tile level.
> > 
> > This brings lossless 4K J2K with -lowres 2 -thread_type slice -
> > threads 96 on an AMD EPYC 7R32 from 4.8 fps (177% CPU) to 31 fps
> > (1284% CPU).
> 
> Any measurable impact on single-threaded or frame-threaded decoding?
> 

median of 11 runs with -threads 1 -vframes 100 on a 4K file
before: real    0m38,664s
 after: real    0m39,139s

I have in mind to try and roll together the last step in the IDWT code
with the av_clip() in write_frame() which should improve run time in
all cases.

/Tomas
diff mbox series

Patch

From 115aa26c343419e81c1b5ba0bfdb1615cbec27e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Fri, 10 Jun 2022 14:10:02 +0200
Subject: [PATCH 01/13] lavc/jpeg2000dec: Finer granularity threading

Decoding and dequant is now threaded on codeblock level.
IDWT is threaded on component level.
MCT and write_frame() remain threaded on tile level.

This brings lossless 4K J2K with -lowres 2 -thread_type slice -threads 96 on an AMD EPYC 7R32 from 4.8 fps (177% CPU) to 31 fps (1284% CPU).
---
 libavcodec/jpeg2000dec.c | 196 ++++++++++++++++++++++++++++-----------
 1 file changed, 142 insertions(+), 54 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 92966b11f5..d9754fc50e 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -92,6 +92,15 @@  typedef struct Jpeg2000Tile {
     int coord[2][2];                    // border coordinates {{x0, x1}, {y0, y1}}
 } Jpeg2000Tile;
 
+typedef struct Jpeg2000IdwtThread {
+    int cb_start, cb_end;
+} Jpeg2000IdwtThread;
+
+typedef struct Jpeg2000CodeblockThread {
+    int tileno, compno, reslevelno, bandno, precno, cblkno;
+    int coded;
+} Jpeg2000CodeblockThread;
+
 typedef struct Jpeg2000DecoderContext {
     AVClass         *class;
     AVCodecContext  *avctx;
@@ -136,6 +145,11 @@  typedef struct Jpeg2000DecoderContext {
 
     /*options parameters*/
     int             reduction_factor;
+
+    Jpeg2000IdwtThread *idwt;
+    unsigned int idwt_size;
+    Jpeg2000CodeblockThread *cb;
+    unsigned int cb_size;
 } Jpeg2000DecoderContext;
 
 /* get_bits functions for JPEG2000 packet bitstream
@@ -1937,54 +1951,33 @@  static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
     }
 }
 
-static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
+static int jpeg2000_decode_cb(AVCodecContext *avctx, void *td,
+                              int jobnr, int threadnr)
 {
     Jpeg2000T1Context t1;
-
-    int compno, reslevelno, bandno;
-
-    /* Loop on tile components */
-    for (compno = 0; compno < s->ncomponents; compno++) {
-        Jpeg2000Component *comp     = tile->comp + compno;
-        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
-        int coded = 0;
-
-        t1.stride = (1<<codsty->log2_cblk_width) + 2;
-
-        /* Loop on resolution levels */
-        for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
-            Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
-            /* Loop on bands */
-            for (bandno = 0; bandno < rlevel->nbands; bandno++) {
-                int nb_precincts, precno;
-                Jpeg2000Band *band = rlevel->band + bandno;
-                int cblkno = 0, bandpos;
-
-                bandpos = bandno + (reslevelno > 0);
-
-                if (band->coord[0][0] == band->coord[0][1] ||
-                    band->coord[1][0] == band->coord[1][1])
-                    continue;
-
-                nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
-                /* Loop on precincts */
-                for (precno = 0; precno < nb_precincts; precno++) {
-                    Jpeg2000Prec *prec = band->prec + precno;
-
-                    /* Loop on codeblocks */
-                    for (cblkno = 0;
-                         cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
-                         cblkno++) {
-                        int x, y;
-                        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
-                        int ret = decode_cblk(s, codsty, &t1, cblk,
+    Jpeg2000DecoderContext *s   = avctx->priv_data;
+    Jpeg2000CodeblockThread *cb = s->cb + jobnr;
+    Jpeg2000Tile *tile          = s->tile + cb->tileno;
+    Jpeg2000Component *comp     = tile->comp + cb->compno;
+    Jpeg2000CodingStyle *codsty = tile->codsty + cb->compno;
+    Jpeg2000ResLevel *rlevel    = comp->reslevel + cb->reslevelno;
+    Jpeg2000Band *band          = rlevel->band + cb->bandno;
+    Jpeg2000Prec *prec          = band->prec + cb->precno;
+    Jpeg2000Cblk *cblk          = prec->cblk + cb->cblkno;
+    int ret, x, y, bandpos      = cb->bandno + (cb->reslevelno > 0);
+
+    t1.stride = (1<<codsty->log2_cblk_width) + 2;
+    cb->coded = 0;
+
+                        ret = decode_cblk(s, codsty, &t1, cblk,
                                     cblk->coord[0][1] - cblk->coord[0][0],
                                     cblk->coord[1][1] - cblk->coord[1][0],
                                     bandpos, comp->roi_shift);
                         if (ret)
-                            coded = 1;
+                            cb->coded = 1;
                         else
-                            continue;
+                            return 0;
+
                         x = cblk->coord[0][0] - band->coord[0][0];
                         y = cblk->coord[1][0] - band->coord[1][0];
 
@@ -1996,16 +1989,28 @@  static inline void tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
                             dequantization_int_97(x, y, cblk, comp, &t1, band);
                         else
                             dequantization_int(x, y, cblk, comp, &t1, band);
-                   } /* end cblk */
-                } /*end prec */
-            } /* end band */
-        } /* end reslevel */
 
-        /* inverse DWT */
-        if (coded)
+    return 0;
+}
+
+static int jpeg2000_idwt(AVCodecContext *avctx, void *td,
+                         int jobnr, int threadnr)
+{
+    Jpeg2000DecoderContext *s   = avctx->priv_data;
+    Jpeg2000IdwtThread *idwt    = s->idwt + jobnr;
+    Jpeg2000Tile *tile          = s->tile + jobnr / s->ncomponents;
+    int compno                  = jobnr % s->ncomponents;
+    Jpeg2000Component *comp     = tile->comp + compno;
+    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+
+    for (int i = idwt->cb_start; i < idwt->cb_end; i++) {
+        if (s->cb[i].coded) {
             ff_dwt_decode(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data);
+            break;
+        }
+    }
 
-    } /*end comp */
+    return 0;
 }
 
 #define WRITE_FRAME(D, PIXEL)                                                                     \
@@ -2075,15 +2080,13 @@  WRITE_FRAME(16, uint16_t)
 
 #undef WRITE_FRAME
 
-static int jpeg2000_decode_tile(AVCodecContext *avctx, void *td,
-                                int jobnr, int threadnr)
+static int jpeg2000_mct_write_frame(AVCodecContext *avctx, void *td,
+                                    int jobnr, int threadnr)
 {
     Jpeg2000DecoderContext *s = avctx->priv_data;
     AVFrame *picture = td;
     Jpeg2000Tile *tile = s->tile + jobnr;
 
-    tile_codeblocks(s, tile);
-
     /* inverse MCT transformation */
     if (tile->codsty[0].mct)
         mct_decode(s, tile);
@@ -2473,11 +2476,80 @@  static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
+static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out)
+{
+    if (s->numXtiles * s->numYtiles > INT_MAX/sizeof(*s->idwt)/s->ncomponents)
+        return AVERROR(ENOMEM);
+
+    av_fast_malloc(&s->idwt, &s->idwt_size, s->numXtiles * s->numYtiles * s->ncomponents * sizeof(*s->idwt));
+    if (!s->idwt)
+        return AVERROR(ENOMEM);
+
+    for (int pass = 0; pass < 2; pass++) {
+        int cbs = 0;
+        for (int tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
+            for (int compno = 0; compno < s->ncomponents; compno++) {
+                Jpeg2000Tile *tile          = s->tile + tileno;
+                Jpeg2000Component *comp     = tile->comp + compno;
+                Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+                Jpeg2000IdwtThread *idwt    = s->idwt + compno + tileno * s->ncomponents;
+
+                idwt->cb_start = cbs;
+
+                for (int reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
+                    Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+                    for (int bandno = 0; bandno < rlevel->nbands; bandno++) {
+                        int nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
+                        Jpeg2000Band *band = rlevel->band + bandno;
+
+                        if (band->coord[0][0] == band->coord[0][1] ||
+                            band->coord[1][0] == band->coord[1][1])
+                            continue;
+
+                        for (int precno = 0; precno < nb_precincts; precno++) {
+                            Jpeg2000Prec *prec = band->prec + precno;
+                            int prec_cbs = prec->nb_codeblocks_width * prec->nb_codeblocks_height;
+
+                            if (cbs > INT_MAX - prec_cbs)
+                                return AVERROR(ENOMEM);
+
+                            for (int cblkno = 0; cblkno < prec_cbs; cblkno++, cbs++) {
+                                if (pass == 1) {
+                                    Jpeg2000CodeblockThread *cb = s->cb + cbs;
+                                    cb->tileno = tileno;
+                                    cb->compno = compno;
+                                    cb->reslevelno = reslevelno;
+                                    cb->bandno = bandno;
+                                    cb->precno = precno;
+                                    cb->cblkno = cblkno;
+                                }
+                            }
+                        }
+                    }
+                }
+
+                idwt->cb_end = cbs;
+            }
+        }
+
+        if (pass == 0) {
+            if (cbs > INT_MAX/sizeof(*s->cb))
+                return AVERROR(ENOMEM);
+            av_fast_malloc(&s->cb, &s->cb_size, cbs*sizeof(*s->cb));
+            if (!s->cb)
+                return AVERROR(ENOMEM);
+        }
+
+        *cbs_out = cbs;
+    }
+    return 0;
+}
+
 static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
                                  int *got_frame, AVPacket *avpkt)
 {
     Jpeg2000DecoderContext *s = avctx->priv_data;
-    int ret;
+    int ret, cbs;
 
     s->avctx     = avctx;
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
@@ -2535,7 +2607,12 @@  static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
         }
     }
 
-    avctx->execute2(avctx, jpeg2000_decode_tile, picture, NULL, s->numXtiles * s->numYtiles);
+    if ((ret = jpeg2000_setup_cbs(s, &cbs)))
+        goto end;
+
+    avctx->execute2(avctx, jpeg2000_decode_cb, NULL, NULL, cbs);
+    avctx->execute2(avctx, jpeg2000_idwt, NULL, NULL, s->numXtiles * s->numYtiles * s->ncomponents);
+    avctx->execute2(avctx, jpeg2000_mct_write_frame, picture, NULL, s->numXtiles * s->numYtiles);
 
     jpeg2000_dec_cleanup(s);
 
@@ -2554,6 +2631,16 @@  end:
     return ret;
 }
 
+static av_cold int jpeg2000_decode_close(AVCodecContext *avctx)
+{
+    Jpeg2000DecoderContext *s = avctx->priv_data;
+
+    av_freep(&s->idwt);
+    av_freep(&s->cb);
+
+    return 0;
+}
+
 #define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 
@@ -2579,6 +2666,7 @@  const FFCodec ff_jpeg2000_decoder = {
     .priv_data_size   = sizeof(Jpeg2000DecoderContext),
     .init             = jpeg2000_decode_init,
     FF_CODEC_DECODE_CB(jpeg2000_decode_frame),
+    .close            = jpeg2000_decode_close,
     .p.priv_class     = &jpeg2000_class,
     .p.max_lowres     = 5,
     .p.profiles       = NULL_IF_CONFIG_SMALL(ff_jpeg2000_profiles),
-- 
2.30.2