Skip to content
Snippets Groups Projects
Commit ac0b8da8 authored by Philippe Gorley's avatar Philippe Gorley Committed by Guillaume Roguez
Browse files

accel: fix memory leak


AVBufferRef leaked its contents after call to openDevice.
- openDevice renamed to initDevice to better reflect its purpose
- initDevice now responsible for all setup and cleanup of device

Change-Id: I2fc36b6b0fe84158bb62fa2dac0cf441716ac540
Reviewed-by: default avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent 1aa7759f
No related branches found
No related tags found
No related merge requests found
...@@ -82,9 +82,10 @@ transferFrameData(HardwareAccel accel, AVCodecContext* /*codecCtx*/, VideoFrame& ...@@ -82,9 +82,10 @@ transferFrameData(HardwareAccel accel, AVCodecContext* /*codecCtx*/, VideoFrame&
} }
static int static int
openDevice(HardwareAccel accel, AVBufferRef** hardwareDeviceCtx) initDevice(HardwareAccel accel, AVCodecContext* codecCtx)
{ {
int ret; int ret = 0;
AVBufferRef* hardwareDeviceCtx = nullptr;
auto hwType = av_hwdevice_find_type_by_name(accel.name.c_str()); auto hwType = av_hwdevice_find_type_by_name(accel.name.c_str());
#ifdef HAVE_VAAPI_ACCEL_DRM #ifdef HAVE_VAAPI_ACCEL_DRM
// default DRM device may not work on multi GPU computers, so check all possible values // default DRM device may not work on multi GPU computers, so check all possible values
...@@ -95,7 +96,8 @@ openDevice(HardwareAccel accel, AVBufferRef** hardwareDeviceCtx) ...@@ -95,7 +96,8 @@ openDevice(HardwareAccel accel, AVBufferRef** hardwareDeviceCtx)
std::sort(files.rbegin(), files.rend()); std::sort(files.rbegin(), files.rend());
for (auto& entry : files) { for (auto& entry : files) {
std::string deviceName = path + entry; std::string deviceName = path + entry;
if ((ret = av_hwdevice_ctx_create(hardwareDeviceCtx, hwType, deviceName.c_str(), nullptr, 0)) >= 0) { if ((ret = av_hwdevice_ctx_create(&hardwareDeviceCtx, hwType, deviceName.c_str(), nullptr, 0)) >= 0) {
codecCtx->hw_device_ctx = hardwareDeviceCtx;
RING_DBG("Using '%s' hardware acceleration with device '%s'", accel.name.c_str(), deviceName.c_str()); RING_DBG("Using '%s' hardware acceleration with device '%s'", accel.name.c_str(), deviceName.c_str());
return ret; return ret;
} }
...@@ -103,8 +105,10 @@ openDevice(HardwareAccel accel, AVBufferRef** hardwareDeviceCtx) ...@@ -103,8 +105,10 @@ openDevice(HardwareAccel accel, AVBufferRef** hardwareDeviceCtx)
} }
#endif #endif
// default device (nullptr) works for most cases // default device (nullptr) works for most cases
if ((ret = av_hwdevice_ctx_create(hardwareDeviceCtx, hwType, nullptr, nullptr, 0)) >= 0) if ((ret = av_hwdevice_ctx_create(&hardwareDeviceCtx, hwType, nullptr, nullptr, 0)) >= 0) {
codecCtx->hw_device_ctx = hardwareDeviceCtx;
RING_DBG("Using '%s' hardware acceleration", accel.name.c_str()); RING_DBG("Using '%s' hardware acceleration", accel.name.c_str());
}
return ret; return ret;
} }
...@@ -126,12 +130,10 @@ setupHardwareDecoding(AVCodecContext* codecCtx) ...@@ -126,12 +130,10 @@ setupHardwareDecoding(AVCodecContext* codecCtx)
{ "videotoolbox", AV_PIX_FMT_VIDEOTOOLBOX, { AV_CODEC_ID_H264, AV_CODEC_ID_MPEG4, AV_CODEC_ID_H263 } }, { "videotoolbox", AV_PIX_FMT_VIDEOTOOLBOX, { AV_CODEC_ID_H264, AV_CODEC_ID_MPEG4, AV_CODEC_ID_H263 } },
}; };
AVBufferRef* hardwareDeviceCtx = nullptr;
for (auto accel : accels) { for (auto accel : accels) {
if (std::find(accel.supportedCodecs.begin(), accel.supportedCodecs.end(), if (std::find(accel.supportedCodecs.begin(), accel.supportedCodecs.end(),
static_cast<AVCodecID>(codecCtx->codec_id)) != accel.supportedCodecs.end()) { static_cast<AVCodecID>(codecCtx->codec_id)) != accel.supportedCodecs.end()) {
if (openDevice(accel, &hardwareDeviceCtx) >= 0) { if (initDevice(accel, codecCtx) >= 0) {
codecCtx->hw_device_ctx = av_buffer_ref(hardwareDeviceCtx);
codecCtx->get_format = getFormatCb; codecCtx->get_format = getFormatCb;
codecCtx->thread_safe_callbacks = 1; codecCtx->thread_safe_callbacks = 1;
return accel; return accel;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment