diff --git a/ffmpeg_vaapi.c b/ffmpeg_vaapi.c
index b9ad7adee9a2a91d7437004454b2f9c8c0df1471..6530908ef4352106a2aff5976065bb0a3b76e388 100644
--- a/ffmpeg_vaapi.c
+++ b/ffmpeg_vaapi.c
@@ -220,6 +220,8 @@ av_cold int vaapi_device_init(const char *device)
 {
     int err;
 
+    av_buffer_unref(&hw_device_ctx);
+
     err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
                                  device, NULL, 0);
     if (err < 0) {