PR #25906 opened by Anton Kesy (antonkesy)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25906
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25906.patch

## Problem
When a QSV decoder outputs frames to system memory (no -hwaccel qsv), it keeps 
a buffer pool for the decoded frames. 
When the stream changes resolution or format, the decoder reinitializes and 
creates a new pool, but never frees the old one. 
Every resolution change leaks the old pool plus all frame buffers in it 
(several MiB per change at 1080p). 

## Fix
Free the old pool before creating the new one.
`av_buffer_pool_uninit()` should be safe here since the frames are still held 
by the caller, and the old pool is released once they are returned.

### How to reproduce
Use any H.264 clip that changes resolution (e.g. 1080p -> 720p -> 1080p) and 
loop it so the decoder reinitializes many times:

```
ffmpeg -benchmark -stream_loop 0  -c:v h264_qsv -i size_change.mp4 -an -f null -
ffmpeg -benchmark -stream_loop 50 -c:v h264_qsv -i size_change.mp4 -an -f null -
```

Compare the `bench: maxrss=` line printed by each command.

### Results:

Before:
```
passes=1 peak memory=219 MiB
passes=51 peak memory=2634 MiB
```
  
After:
```
passes=1 peak memory=194 MiB
passes=51 peak memory=193 MiB
```


>From 9a45d2422447742842f0d063a4c98d7cc4e1600d Mon Sep 17 00:00:00 2001
From: Anton Kesy <[email protected]>
Date: Mon, 9 Nov 2026 09:11:40 +0100
Subject: [PATCH] avcodec/qsvdec: free the system memory pool on reinit

qsv_decode_init_context() runs again after a resolution or format
change and overwrote q->pool without releasing the old one. When
decoding to system memory (no hw_frames_ctx), every reinit leaked the
old pool and all buffers allocated from it.

Signed-off-by: Anton Kesy <[email protected]>
---
 libavcodec/qsvdec.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index 990e46ea1d7..8e493146519 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -417,6 +417,7 @@ static int qsv_decode_init_context(AVCodecContext *avctx, 
QSVContext *q, mfxVide
         ret = av_image_get_buffer_size(avctx->pix_fmt, 
FFALIGN(avctx->coded_width, 128), FFALIGN(avctx->coded_height, 64), 1);
         if (ret < 0)
             return ret;
+        av_buffer_pool_uninit(&q->pool);
         q->pool = av_buffer_pool_init(ret, av_buffer_allocz);
     }
     return 0;
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to