From: "Tomas Härdin" <tjoppen@acc.umu.se>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 04/13] lavc/jpeg2000dec: Implement IDWT slicing
Date: Tue, 14 Jun 2022 16:40:46 +0200
Message-ID: <188b5011c7be29b073a107e5913620cb315977dd.camel@acc.umu.se> (raw)
In-Reply-To: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se>
[-- Attachment #1: Type: text/plain, Size: 1 bytes --]
[-- Attachment #2: 0004-lavc-jpeg2000dec-Implement-IDWT-slicing.patch --]
[-- Type: text/x-patch, Size: 9640 bytes --]
From d0ec602b0f61dd7f8d53efccc2c4859058a5d55d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Mon, 13 Jun 2022 14:45:07 +0200
Subject: [PATCH 04/13] lavc/jpeg2000dec: Implement IDWT slicing
---
libavcodec/jpeg2000dec.c | 99 +++++++++++++++++++++++++++++++++++-----
libavcodec/jpeg2000dwt.c | 1 -
libavcodec/jpeg2000dwt.h | 1 +
3 files changed, 88 insertions(+), 13 deletions(-)
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 8999974a56..9344630c6f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -150,6 +150,10 @@ typedef struct Jpeg2000DecoderContext {
unsigned int idwt_size;
Jpeg2000CodeblockThread *cb;
unsigned int cb_size;
+
+ // used for idwt slicing
+ int reslevel, dir, slices;
+ int have_dwt97_int; // 1 if any coding style is FF_DWT97_INT
} Jpeg2000DecoderContext;
/* get_bits functions for JPEG2000 packet bitstream
@@ -541,9 +545,10 @@ static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
}
c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
/* set integer 9/7 DWT in case of BITEXACT flag */
- if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
+ if ((s->avctx->flags & AV_CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97)) {
c->transform = FF_DWT97_INT;
- else if (c->transform == FF_DWT53) {
+ s->have_dwt97_int = 1;
+ } else if (c->transform == FF_DWT53) {
s->avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
}
@@ -1052,7 +1057,7 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
return AVERROR_INVALIDDATA;
if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
s->cbps[compno], s->cdx[compno],
- s->cdy[compno], s->avctx, 1))
+ s->cdy[compno], s->avctx, s->slices))
return ret;
}
return 0;
@@ -1993,19 +1998,74 @@ static int jpeg2000_decode_cb(AVCodecContext *avctx, void *td,
return 0;
}
+static int jpeg2000_dwt97_int_preshift(AVCodecContext *avctx, void *td,
+ int jobnr, int threadnr)
+{
+ Jpeg2000DecoderContext *s = avctx->priv_data;
+ Jpeg2000IdwtThread *idwt = s->idwt + jobnr / s->slices;
+ Jpeg2000Tile *tile = s->tile + jobnr / s->slices / s->ncomponents;
+ int compno = (jobnr / s->slices) % s->ncomponents;
+ int slice = jobnr % s->slices;
+ Jpeg2000Component *comp = tile->comp + compno;
+ Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+ int a = comp->dwt.linelen[comp->dwt.ndeclevels - 1][0] *
+ comp->dwt.linelen[comp->dwt.ndeclevels - 1][1];
+ int as = (a + s->slices - 1)/s->slices;
+
+ for (int i = idwt->cb_start; i < idwt->cb_end; i++) {
+ if (s->cb[i].coded) {
+ if (codsty->transform == FF_DWT97_INT) {
+ for (int i = as*slice; i - as < as*slice; i++)
+ comp->i_data[i] *= 1LL << I_PRESHIFT;
+ }
+ break;
+ }
+ }
+
+ 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;
+ Jpeg2000IdwtThread *idwt = s->idwt + jobnr / s->slices;
+ Jpeg2000Tile *tile = s->tile + jobnr / s->slices / s->ncomponents;
+ int compno = (jobnr / s->slices) % s->ncomponents;
+ int slice = jobnr % s->slices;
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);
+ ff_dwt_decode_thread(&comp->dwt, codsty->transform == FF_DWT97 ? (void*)comp->f_data : (void*)comp->i_data, s->reslevel, s->dir, slice, s->slices);
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int jpeg2000_dwt97_int_postshift(AVCodecContext *avctx, void *td,
+ int jobnr, int threadnr)
+{
+ Jpeg2000DecoderContext *s = avctx->priv_data;
+ Jpeg2000IdwtThread *idwt = s->idwt + jobnr / s->slices;
+ Jpeg2000Tile *tile = s->tile + jobnr / s->slices / s->ncomponents;
+ int compno = (jobnr / s->slices) % s->ncomponents;
+ int slice = jobnr % s->slices;
+ Jpeg2000Component *comp = tile->comp + compno;
+ Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+ int a = comp->dwt.linelen[comp->dwt.ndeclevels - 1][0] *
+ comp->dwt.linelen[comp->dwt.ndeclevels - 1][1];
+ int as = (a + s->slices - 1)/s->slices;
+
+ for (int i = idwt->cb_start; i < idwt->cb_end; i++) {
+ if (s->cb[i].coded) {
+ if (codsty->transform == FF_DWT97_INT) {
+ for (int i = as*slice; i - as < as*slice; i++)
+ comp->i_data[i] = (comp->i_data[i] + ((1LL<<I_PRESHIFT)>>1)) >> I_PRESHIFT;
+ }
break;
}
}
@@ -2476,7 +2536,7 @@ static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
return 0;
}
-static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out)
+static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out, int *maxreslevels_out)
{
if (s->numXtiles * s->numYtiles > INT_MAX/sizeof(*s->idwt)/s->ncomponents)
return AVERROR(ENOMEM);
@@ -2486,7 +2546,7 @@ static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out)
return AVERROR(ENOMEM);
for (int pass = 0; pass < 2; pass++) {
- int cbs = 0;
+ int cbs = 0, maxreslevels = 0;
for (int tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
for (int compno = 0; compno < s->ncomponents; compno++) {
Jpeg2000Tile *tile = s->tile + tileno;
@@ -2495,6 +2555,7 @@ static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out)
Jpeg2000IdwtThread *idwt = s->idwt + compno + tileno * s->ncomponents;
idwt->cb_start = cbs;
+ maxreslevels = FFMAX(maxreslevels, codsty->nreslevels2decode);
for (int reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
@@ -2541,6 +2602,7 @@ static int jpeg2000_setup_cbs(Jpeg2000DecoderContext *s, int *cbs_out)
}
*cbs_out = cbs;
+ *maxreslevels_out = maxreslevels;
}
return 0;
}
@@ -2549,7 +2611,7 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
int *got_frame, AVPacket *avpkt)
{
Jpeg2000DecoderContext *s = avctx->priv_data;
- int ret, cbs;
+ int ret, cbs, maxreslevels;
s->avctx = avctx;
bytestream2_init(&s->g, avpkt->data, avpkt->size);
@@ -2592,6 +2654,7 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
goto end;
picture->pict_type = AV_PICTURE_TYPE_I;
picture->key_frame = 1;
+ s->slices = avctx->active_thread_type == FF_THREAD_SLICE ? avctx->thread_count : 1;
if (ret = jpeg2000_read_bitstream_packets(s))
goto end;
@@ -2607,11 +2670,23 @@ static int jpeg2000_decode_frame(AVCodecContext *avctx, AVFrame *picture,
}
}
- if ((ret = jpeg2000_setup_cbs(s, &cbs)))
+ if ((ret = jpeg2000_setup_cbs(s, &cbs, &maxreslevels)))
goto end;
avctx->execute2(avctx, jpeg2000_decode_cb, NULL, NULL, cbs);
- avctx->execute2(avctx, jpeg2000_idwt, NULL, NULL, s->numXtiles * s->numYtiles * s->ncomponents);
+
+ if (s->have_dwt97_int)
+ avctx->execute2(avctx, jpeg2000_dwt97_int_preshift, NULL, NULL, s->numXtiles * s->numYtiles * s->ncomponents * s->slices);
+
+ for (s->reslevel = 0; s->reslevel < maxreslevels; s->reslevel++) {
+ for (s->dir = 0; s->dir < 2; s->dir++) {
+ avctx->execute2(avctx, jpeg2000_idwt, NULL, NULL, s->numXtiles * s->numYtiles * s->ncomponents * s->slices);
+ }
+ }
+
+ if (s->have_dwt97_int)
+ avctx->execute2(avctx, jpeg2000_dwt97_int_postshift, NULL, NULL, s->numXtiles * s->numYtiles * s->ncomponents * s->slices);
+
avctx->execute2(avctx, jpeg2000_mct_write_frame, picture, NULL, s->numXtiles * s->numYtiles);
jpeg2000_dec_cleanup(s);
diff --git a/libavcodec/jpeg2000dwt.c b/libavcodec/jpeg2000dwt.c
index 42a92b6c64..921461b6d7 100644
--- a/libavcodec/jpeg2000dwt.c
+++ b/libavcodec/jpeg2000dwt.c
@@ -45,7 +45,6 @@
#define I_LFTG_DELTA 29066ll
#define I_LFTG_K 80621ll
#define I_LFTG_X 53274ll
-#define I_PRESHIFT 8
static inline void extend53(int *p, int i0, int i1)
{
diff --git a/libavcodec/jpeg2000dwt.h b/libavcodec/jpeg2000dwt.h
index 0589c8355c..d5e94c9916 100644
--- a/libavcodec/jpeg2000dwt.h
+++ b/libavcodec/jpeg2000dwt.h
@@ -32,6 +32,7 @@
#define FF_DWT_MAX_DECLVLS 32 ///< max number of decomposition levels
#define F_LFTG_K 1.230174104914001f
#define F_LFTG_X 0.812893066115961f
+#define I_PRESHIFT 8
enum DWTType {
FF_DWT97,
--
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 prev parent reply other threads:[~2022-06-14 14:40 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-14 14:39 [FFmpeg-devel] [PATCH 01/13] lavc/jpeg2000dec: Finer granularity threading Tomas Härdin
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 ` Tomas Härdin [this message]
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=188b5011c7be29b073a107e5913620cb315977dd.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