Skip to content

Commit a4d8987

Browse files
authored
Remove libavutils from the asan suppression list (#5783)
* Remove libavutils from the asan suppression list - keeps suppression for FFmpegDemuxer from the PyNvVideoCodec DALI doesn't have control on - fixes leaks from libavutils Signed-off-by: Janusz Lisiecki <[email protected]>
1 parent cfbf402 commit a4d8987

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

Diff for: dali/operators/reader/loader/video/frames_decoder.cc

+12-7
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ FramesDecoder::FramesDecoder(const char *memory_file, int memory_file_size, bool
236236

237237
int ret = avformat_open_input(&av_state_->ctx_, "", nullptr, nullptr);
238238
if (ret != 0) {
239+
// av_state_->ctx_ is nullified so we need to free the memory here instead of the
240+
// AvState destructor which cannot access it through av_state_->ctx_ anymore
241+
av_freep(&av_io_context->buffer);
242+
avio_context_free(&av_io_context);
239243
DALI_WARN(make_string("Failed to open video file \"", Filename(), "\", due to ",
240244
detail::av_error_string(ret)));
241245
return;
@@ -284,13 +288,14 @@ void FramesDecoder::CreateAvState(std::unique_ptr<AvState> &av_state, bool init_
284288
av_state->ctx_->pb = av_io_context;
285289

286290
int ret = avformat_open_input(&av_state->ctx_, "", nullptr, nullptr);
287-
DALI_ENFORCE(
288-
ret == 0,
289-
make_string(
290-
"Failed to open video file \"",
291-
Filename(),
292-
"\", due to ",
293-
detail::av_error_string(ret)));
291+
if (ret != 0) {
292+
// av_state_->ctx_ is nullified so we need to free the memory here instead of the
293+
// AvState destructor which cannot access it through av_state_->ctx_ anymore
294+
av_freep(&av_io_context->buffer);
295+
avio_context_free(&av_io_context);
296+
DALI_FAIL(make_string("Failed to open video file \"", Filename(), "\", due to ",
297+
detail::av_error_string(ret)));
298+
}
294299
av_state->stream_id_ = av_find_best_stream(
295300
av_state->ctx_, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
296301
av_state->codec_params_ = av_state->ctx_->streams[av_state->stream_id_]->codecpar;

Diff for: dali/operators/reader/loader/video/frames_decoder.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ struct AvState {
6060
avcodec_free_context(&codec_ctx_);
6161
if (ctx_ != nullptr) {
6262
// if we use avio_alloc_context we need a custom deallocator for ctx_->pb
63-
if (ctx_->flags & AVFMT_FLAG_CUSTOM_IO) {
64-
av_freep(&ctx_->pb->buffer);
65-
av_freep(&ctx_->pb);
66-
}
63+
auto custom_dealloc = ctx_->flags & AVFMT_FLAG_CUSTOM_IO;
64+
auto custom_ctx = ctx_->pb;
65+
auto custom_buffer = ctx_->pb ? ctx_->pb->buffer : nullptr;
6766
avformat_close_input(&ctx_);
6867
avformat_free_context(ctx_);
68+
if (custom_dealloc) {
69+
av_freep(&custom_buffer);
70+
avio_context_free(&custom_ctx);
71+
}
6972
}
7073

7174
ctx_ = nullptr;

Diff for: dali/operators/reader/loader/video_loader.cc

+16-3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ static int64_t seek_file(void *opaque, int64_t offset, int whence) {
266266
}
267267
}
268268

269+
void clean_avformat_context(AVFormatContext **p) {
270+
if (*p && (*p)->flags & AVFMT_FLAG_CUSTOM_IO) {
271+
if (*p && (*p)->pb) av_freep(&(*p)->pb->buffer);
272+
avio_context_free(&(*p)->pb);
273+
}
274+
avformat_close_input(p);
275+
}
276+
269277
VideoFile& VideoLoader::get_or_open_file(const std::string &filename) {
270278
static VideoFile empty_file = {};
271279
auto& file = open_files_[filename];
@@ -299,12 +307,16 @@ VideoFile& VideoLoader::get_or_open_file(const std::string &filename) {
299307
// if avformat_open_input fails it frees raw_fmt_ctx so we can release it from unique_ptr
300308
int ret = avformat_open_input(&tmp_raw_fmt_ctx, NULL, NULL, NULL);
301309
if (ret < 0) {
310+
// avio_ctx->ctx_ is nullified so we need to free the memory here instead of the
311+
// AVFormatContext destructor which cannot access it through avio_ctx->ctx_ anymore
312+
av_freep(&avio_ctx->buffer);
313+
avio_context_free(&avio_ctx);
302314
DALI_WARN(make_string("Failed to open video file ", filename, " because of ",
303315
av_err2str(ret)));
304316
open_files_.erase(filename);
305317
return empty_file;
306318
}
307-
file.fmt_ctx_ = make_unique_av<AVFormatContext>(tmp_raw_fmt_ctx, avformat_close_input);
319+
file.fmt_ctx_ = make_unique_av<AVFormatContext>(tmp_raw_fmt_ctx, clean_avformat_context);
308320
LOG_LINE << "File open " << filename << std::endl;
309321

310322
LOG_LINE << "File info fetched for " << filename << std::endl;
@@ -398,6 +410,8 @@ VideoFile& VideoLoader::get_or_open_file(const std::string &filename) {
398410
if (pkt.stream_index == file.vid_stream_idx_) break;
399411
av_packet_unref(&pkt);
400412
}
413+
auto pkt_duration = pkt.duration;
414+
av_packet_unref(&pkt);
401415

402416
if (ret < 0) {
403417
DALI_WARN(make_string("Unable to read frame from file :", filename));
@@ -406,10 +420,9 @@ VideoFile& VideoLoader::get_or_open_file(const std::string &filename) {
406420
}
407421

408422
DALI_ENFORCE(skip_vfr_check_ ||
409-
almost_equal(av_q2d(file.frame_base_), pkt.duration * av_q2d(file.stream_base_), 2),
423+
almost_equal(av_q2d(file.frame_base_), pkt_duration * av_q2d(file.stream_base_), 2),
410424
"Variable frame rate videos are unsupported. This heuristic can yield false positives. "
411425
"The check can be disabled via the skip_vfr_check flag. Check failed for file: " + filename);
412-
av_packet_unref(&pkt);
413426
// empty the read buffer from av_read_frame to save the memory usage
414427
avformat_flush(file.fmt_ctx_.get());
415428

Diff for: qa/leak.sup

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ leak:numba
1111
leak:libmxnet
1212
leak:libtorch
1313
leak:libc10
14-
leak:av_realloc_f
15-
leak:av_malloc
14+
# we don't control FFmpegDemuxer destructor so we cannot fix that in DALI
15+
leak:FFmpegDemuxer
1616
# tensorflow
1717
leak:PyInit__pywrap_debug_events_writer
1818
leak:_pywrap

0 commit comments

Comments
 (0)