Commit 0709cadc authored by Moto Hira's avatar Moto Hira Committed by Facebook GitHub Bot
Browse files

Remove unnecessary AVFrame allocation (#3021)

Summary:
Pull Request resolved: https://github.com/pytorch/audio/pull/3021

When input format and encode format is different in StreamWriter, filter for format conversion is inserted.

A temporary AVFilter (`dst_frame`) is used for this case,
but FilterGraph handles the memory allocation,
so there is no need to perform allocation by ourselves.

This `dst_frame` is otherwise not used, so we do not have to allocate memory at all.
This commit removes the unnecessary memory allocation at all.

Reviewed By: xiaohui-zhang

Differential Revision: D42865042

fbshipit-source-id: 2673b06de1e905dc73a11e2ec1cc6ce7b525d451
parent da9d1627
...@@ -456,15 +456,12 @@ void StreamWriter::add_audio_stream( ...@@ -456,15 +456,12 @@ void StreamWriter::add_audio_stream(
static const int default_capacity = 10000; static const int default_capacity = 10000;
int frame_capacity = ctx->frame_size ? ctx->frame_size : default_capacity; int frame_capacity = ctx->frame_size ? ctx->frame_size : default_capacity;
AVFramePtr src_frame = get_audio_frame(src_fmt, ctx, frame_capacity); AVFramePtr src_frame = get_audio_frame(src_fmt, ctx, frame_capacity);
AVFramePtr dst_frame = filter
? AVFramePtr{}
: get_audio_frame(ctx->sample_fmt, ctx, frame_capacity);
streams.emplace_back(OutputStream{ streams.emplace_back(OutputStream{
stream, stream,
std::move(ctx), std::move(ctx),
std::move(filter), std::move(filter),
std::move(src_frame), std::move(src_frame),
std::move(dst_frame), {},
0, 0,
frame_capacity, frame_capacity,
AVBufferRefPtr{}, AVBufferRefPtr{},
...@@ -555,22 +552,18 @@ void StreamWriter::add_video_stream( ...@@ -555,22 +552,18 @@ void StreamWriter::add_video_stream(
return std::unique_ptr<FilterGraph>(nullptr); return std::unique_ptr<FilterGraph>(nullptr);
}(); }();
// CUDA: require src_frame
// CPU: require dst_frame when filter is enabled
AVFramePtr src_frame = [&]() { AVFramePtr src_frame = [&]() {
if (device.type() == c10::DeviceType::CUDA) { if (device.type() == c10::DeviceType::CUDA) {
return get_hw_video_frame(ctx); return get_hw_video_frame(ctx);
} }
return get_video_frame(src_fmt, ctx); return get_video_frame(src_fmt, ctx);
}(); }();
AVFramePtr dst_frame =
filter ? AVFramePtr{} : get_video_frame(ctx->pix_fmt, ctx);
streams.emplace_back(OutputStream{ streams.emplace_back(OutputStream{
stream, stream,
std::move(ctx), std::move(ctx),
std::move(filter), std::move(filter),
std::move(src_frame), std::move(src_frame),
std::move(dst_frame), {},
0, 0,
-1, -1,
std::move(hw_device_ctx), std::move(hw_device_ctx),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment