[FFmpeg-devel] [PATCH] Boost FPS and performance: Optimize vertical loop for cache-friendly access [libavcodec/jpeg2000dwt.c:dwt_decode97_float]
Michael Niedermayer
michael at niedermayer.cc
Wed May 14 19:40:03 EEST 2025
Hi Chitra
On Wed, May 14, 2025 at 03:55:59AM +0000, Chitra Dey Sarkar via ffmpeg-devel wrote:
> Original Implementation:
> ---------------------------------
> In the original implementation, the "VER_SD" section processes image data stored in *data using strided memory access in a vertical fashion This leads to inefficient memory access patterns and cache thrashing due to non-sequential data access across multiple inner loops.
>
> Proposed Refactor:
> ---------------------------------
> The proposed refactor replaces this by allocating a cache-friendly 2D array buffer. This change eliminates strided memory access across the three inner loops, significantly improving cache locality and reducing cache thrashing.
>
> Additionally, the data is transposed outside the lp loop, which allows for efficient per-line access and write-back to the l buffer, further optimizing performance.
>
> Performance improvements
> -------------------------------------------------------
> This change results in a substantial performance improvement Sharing the FPS data benchmarked on our end for the file 'Tears of Steel' using HandBrake
>
> Device / CPU Model Official FPS Optimized FPS % Improvement
> Surface Laptop 11 (10-core X1P64100, L2: 36MB) 3.18 6.15 +93%
> Surface Laptop 11(10-core X1P64100, L2: 36MB) 5.16 7.31 +41%
> Surface Laptop 11 (10-core X1P64100, L2: 36MB) 5.57 9.21 +65%
> AMD Ryzen + NVIDIA RTX 4060 Laptop (12C/24T) 9.97 11.22 +12%
> Mac Mini Apple M4 Chip 9.00 12.00 +30%
>
> -------------------------------------------------------------------------------------------------------
> ---
> libavcodec/jpeg2000dwt.c | 72 +++++++++++++++++++++++++++++++---------
> 1 file changed, 57 insertions(+), 15 deletions(-)
>
> diff --git a/libavcodec/jpeg2000dwt.c b/libavcodec/jpeg2000dwt.c index 9ee8122658..45d7897893 100644
> --- a/libavcodec/jpeg2000dwt.c
> +++ b/libavcodec/jpeg2000dwt.c
> @@ -409,6 +409,15 @@ static void dwt_decode97_float(DWTContext *s, float *t)
> /* position at index O of line range [0-5,w+5] cf. extend function */
> line += 5;
>
> + /* Find the largest lv and lv to allocate a 2D Array*/
lv and lv ?
you mean lv anf lh ?
> + int max_dim = 0;
> + for (lev = 0; lev < s->ndeclevels; lev++) {
> + if (s->linelen[lev][0] > max_dim) max_dim = s->linelen[lev][0];
> + if (s->linelen[lev][1] > max_dim) max_dim = s->linelen[lev][1];
FFMAX()
> + }
> + float *array2DBlock = av_malloc(max_dim * max_dim * sizeof(float));
> + int useFallback = !array2DBlock;
also is this supposed to be max_dim_h * max_dim_v ?
> +
> for (lev = 0; lev < s->ndeclevels; lev++) {
> int lh = s->linelen[lev][0],
> lv = s->linelen[lev][1],
> @@ -431,23 +440,56 @@ static void dwt_decode97_float(DWTContext *s, float *t)
> for (i = 0; i < lh; i++)
> data[w * lp + i] = l[i];
> }
> -
> - // VER_SD
> - l = line + mv;
> - for (lp = 0; lp < lh; lp++) {
> - int i, j = 0;
> - // copy with interleaving
> - for (i = mv; i < lv; i += 2, j++)
> - l[i] = data[w * j + lp];
> - for (i = 1 - mv; i < lv; i += 2, j++)
> - l[i] = data[w * j + lp];
> -
> - sr_1d97_float(line, mv, mv + lv);
this should be run linewise not columnwise
if you dont understand what i mean here, please say so and ill elaborate
But basically both vertical and horizontal transforms should be done with
row based implementations
The code before loads and safes each column (which is bad)
your code adds an efficient transpose and then copies each row
Theres a ton of unneeded copying here, i think the data in your
implementation now is copied 4 times for each vertical transform
pass
But iam very happy to see a patch submission from Microsoft! :)
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
"I am not trying to be anyone's saviour, I'm trying to think about the
future and not be sad" - Elon Musk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20250514/76fd6e6f/attachment.sig>
More information about the ffmpeg-devel
mailing list