From: "Tomas Härdin" <tjoppen@acc.umu.se>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 01/13] lavc/jpeg2000dec: Finer granularity threading
Date: Tue, 14 Jun 2022 16:39:00 +0200
Message-ID: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se> (raw)
[-- Attachment #1: Type: text/plain, Size: 106 bytes --]
Patch 12 in this series is optional since it's just me getting the
speed up on a specific machine
/Tomas
[-- Attachment #2: 0001-lavc-jpeg2000dec-Finer-granularity-threading.patch --]
[-- Type: text/x-patch, Size: 11603 bytes --]
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
[-- Attachment #3: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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".
next reply other threads:[~2022-06-14 14:39 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-14 14:39 Tomas Härdin [this message]
2022-06-14 14:39 ` [FFmpeg-devel] [PATCH 02/13] lavc/jpeg2000dec: Reindent Tomas Härdin
2022-06-14 14:40 ` [FFmpeg-devel] [PATCH 03/13] lavc/jpeg2000dwt: Implement sliced transforms Tomas Härdin
2022-06-14 14:40 ` [FFmpeg-devel] [PATCH 04/13] lavc/jpeg2000dec: Implement IDWT slicing Tomas Härdin
2022-06-14 14:41 ` [FFmpeg-devel] [PATCH 05/13] lavc/jpeg2000dec: Thread init_tile() Tomas Härdin
2022-06-14 21:11 ` Michael Niedermayer
2022-06-15 13:11 ` Tomas Härdin
2022-06-15 21:05 ` Michael Niedermayer
2022-06-16 9:28 ` Tomas Härdin
2022-06-14 14:42 ` [FFmpeg-devel] [PATCH 06/13] lavu/mem: Add ff_fast_recalloc() Tomas Härdin
2022-06-14 20:26 ` Michael Niedermayer
2022-06-15 9:59 ` Tomas Härdin
2022-06-15 12:15 ` James Almer
2022-06-16 12:44 ` Tomas Härdin
2022-06-18 14:57 ` Anton Khirnov
2022-06-21 8:04 ` Tomas Härdin
2022-06-14 14:42 ` [FFmpeg-devel] [PATCH 07/13] lavc/jpeg2000*: Use ff_fast_recalloc() to eliminate lots of allocations Tomas Härdin
2022-06-14 15:23 ` Andreas Rheinhardt
2022-06-15 10:03 ` Tomas Härdin
2022-06-14 14:43 ` [FFmpeg-devel] [PATCH 08/13] lavc/jpeg2000: Switch Jpeg2000TgtNode to int32_t parent Tomas Härdin
2022-06-14 14:43 ` [FFmpeg-devel] [PATCH 09/13] lavc/jpeg2000: Speed up ff_jpeg2000_tag_tree_init() using stereotypes for sizes <= 4x4 Tomas Härdin
2022-06-18 15:00 ` Anton Khirnov
2022-06-21 7:57 ` Tomas Härdin
2022-06-14 14:43 ` [FFmpeg-devel] [PATCH 10/13] lavc/jpeg2000: Reindent Tomas Härdin
2022-06-14 14:44 ` [FFmpeg-devel] [PATCH 11/13] lavc/jpeg2000: Minimize calls to av_codec_is_encoder() Tomas Härdin
2022-06-14 15:04 ` Andreas Rheinhardt
2022-06-15 10:20 ` Tomas Härdin
2022-06-14 14:44 ` [FFmpeg-devel] [PATCH 12/13] lavc/jpeg2000dec: Use coarser slicing for initial reslevels Tomas Härdin
2022-06-14 14:47 ` [FFmpeg-devel] [PATCH 13/13] lavc/jpeg2000dec: Component-level threading of write_frame() Tomas Härdin
2022-06-18 14:50 ` [FFmpeg-devel] [PATCH 01/13] lavc/jpeg2000dec: Finer granularity threading Anton Khirnov
2022-06-24 8:19 ` Tomas Härdin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se \
--to=tjoppen@acc.umu.se \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git