Skip to content

Commit ca66a1d

Browse files
mthrokfacebook-github-bot
authored andcommitted
Revert "[audio][PR] Add option to dlopen FFmpeg libraries (#3402)" (#3456)
Summary: This reverts commit b7d3e89. We will use pre-built binaries instead of dlopen. Pull Request resolved: #3456 Differential Revision: D47239681 Pulled By: mthrok fbshipit-source-id: 0446a62410d914081184fc20c386afa00b1e41b6
1 parent 662f067 commit ca66a1d

19 files changed

+218
-774
lines changed

tools/setup_helpers/extension.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def _get_build(var, default=False):
3737
_BUILD_RIR = _get_build("BUILD_RIR", True)
3838
_BUILD_RNNT = _get_build("BUILD_RNNT", True)
3939
_USE_FFMPEG = _get_build("USE_FFMPEG", False)
40-
_DLOPEN_FFMPEG = _get_build("DLOPEN_FFMPEG", False)
4140
_USE_ROCM = _get_build("USE_ROCM", torch.backends.cuda.is_built() and torch.version.hip is not None)
4241
_USE_CUDA = _get_build("USE_CUDA", torch.backends.cuda.is_built() and torch.version.hip is None)
4342
_BUILD_ALIGN = _get_build("BUILD_ALIGN", True)
@@ -125,7 +124,6 @@ def build_extension(self, ext):
125124
f"-DUSE_CUDA:BOOL={'ON' if _USE_CUDA else 'OFF'}",
126125
f"-DUSE_OPENMP:BOOL={'ON' if _USE_OPENMP else 'OFF'}",
127126
f"-DUSE_FFMPEG:BOOL={'ON' if _USE_FFMPEG else 'OFF'}",
128-
f"-DDLOPEN_FFMPEG:BOOL={'ON' if _DLOPEN_FFMPEG else 'OFF'}",
129127
]
130128
build_args = ["--target", "install"]
131129
# Pass CUDA architecture to cmake

torchaudio/csrc/ffmpeg/CMakeLists.txt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ message(STATUS "FFMPEG_ROOT=$ENV{FFMPEG_ROOT}")
22
find_package(FFMPEG 4.1 REQUIRED COMPONENTS avdevice avfilter avformat avcodec avutil)
33
add_library(ffmpeg INTERFACE)
44
target_include_directories(ffmpeg INTERFACE "${FFMPEG_INCLUDE_DIRS}")
5-
if (NOT DLOPEN_FFMPEG)
65
target_link_libraries(ffmpeg INTERFACE "${FFMPEG_LIBRARIES}")
7-
endif()
6+
87

98
set(
109
sources
11-
stub.cpp
1210
ffmpeg.cpp
1311
filter_graph.cpp
1412
hw_context.cpp
@@ -33,24 +31,24 @@ if (USE_CUDA)
3331
cuda_deps)
3432
endif()
3533

36-
if (DLOPEN_FFMPEG)
37-
set(compile_definitions DLOPEN_FFMPEG)
38-
endif()
39-
4034
torchaudio_library(
4135
libtorchaudio_ffmpeg
4236
"${sources}"
4337
""
4438
"torch;ffmpeg;${additional_lib}"
45-
"${compile_definitions}"
39+
""
4640
)
4741

4842
if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
43+
set(
44+
ext_sources
45+
pybind/pybind.cpp
46+
)
4947
torchaudio_extension(
5048
_torchaudio_ffmpeg
51-
pybind/pybind.cpp
49+
"${ext_sources}"
5250
""
5351
"libtorchaudio_ffmpeg"
54-
"${compile_definitions}"
52+
""
5553
)
5654
endif ()

torchaudio/csrc/ffmpeg/ffmpeg.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
#include <c10/util/Exception.h>
22
#include <torchaudio/csrc/ffmpeg/ffmpeg.h>
3-
#include <torchaudio/csrc/ffmpeg/stub.h>
43
#include <sstream>
54
#include <stdexcept>
65
#include <string>
76
#include <vector>
87

98
namespace torchaudio::io {
109

11-
std::string av_err2string(int errnum) {
12-
char str[AV_ERROR_MAX_STRING_SIZE];
13-
FFMPEG av_strerror(errnum, str, AV_ERROR_MAX_STRING_SIZE);
14-
return str;
15-
}
16-
1710
////////////////////////////////////////////////////////////////////////////////
1811
// AVDictionary
1912
////////////////////////////////////////////////////////////////////////////////
2013
AVDictionary* get_option_dict(const c10::optional<OptionDict>& option) {
2114
AVDictionary* opt = nullptr;
2215
if (option) {
2316
for (auto const& [key, value] : option.value()) {
24-
FFMPEG av_dict_set(&opt, key.c_str(), value.c_str(), 0);
17+
av_dict_set(&opt, key.c_str(), value.c_str(), 0);
2518
}
2619
}
2720
return opt;
@@ -32,10 +25,10 @@ void clean_up_dict(AVDictionary* p) {
3225
std::vector<std::string> unused_keys;
3326
// Check and copy unused keys, clean up the original dictionary
3427
AVDictionaryEntry* t = nullptr;
35-
while ((t = FFMPEG av_dict_get(p, "", t, AV_DICT_IGNORE_SUFFIX))) {
28+
while ((t = av_dict_get(p, "", t, AV_DICT_IGNORE_SUFFIX))) {
3629
unused_keys.emplace_back(t->key);
3730
}
38-
FFMPEG av_dict_free(&p);
31+
av_dict_free(&p);
3932
TORCH_CHECK(
4033
unused_keys.empty(),
4134
"Unexpected options: ",
@@ -47,14 +40,14 @@ void clean_up_dict(AVDictionary* p) {
4740
// AVFormatContext
4841
////////////////////////////////////////////////////////////////////////////////
4942
void AVFormatInputContextDeleter::operator()(AVFormatContext* p) {
50-
FFMPEG avformat_close_input(&p);
43+
avformat_close_input(&p);
5144
};
5245

5346
AVFormatInputContextPtr::AVFormatInputContextPtr(AVFormatContext* p)
5447
: Wrapper<AVFormatContext, AVFormatInputContextDeleter>(p) {}
5548

5649
void AVFormatOutputContextDeleter::operator()(AVFormatContext* p) {
57-
FFMPEG avformat_free_context(p);
50+
avformat_free_context(p);
5851
};
5952

6053
AVFormatOutputContextPtr::AVFormatOutputContextPtr(AVFormatContext* p)
@@ -64,9 +57,9 @@ AVFormatOutputContextPtr::AVFormatOutputContextPtr(AVFormatContext* p)
6457
// AVIO
6558
////////////////////////////////////////////////////////////////////////////////
6659
void AVIOContextDeleter::operator()(AVIOContext* p) {
67-
FFMPEG avio_flush(p);
68-
FFMPEG av_freep(&p->buffer);
69-
FFMPEG av_freep(&p);
60+
avio_flush(p);
61+
av_freep(&p->buffer);
62+
av_freep(&p);
7063
};
7164

7265
AVIOContextPtr::AVIOContextPtr(AVIOContext* p)
@@ -76,13 +69,13 @@ AVIOContextPtr::AVIOContextPtr(AVIOContext* p)
7669
// AVPacket
7770
////////////////////////////////////////////////////////////////////////////////
7871
void AVPacketDeleter::operator()(AVPacket* p) {
79-
FFMPEG av_packet_free(&p);
72+
av_packet_free(&p);
8073
};
8174

8275
AVPacketPtr::AVPacketPtr(AVPacket* p) : Wrapper<AVPacket, AVPacketDeleter>(p) {}
8376

8477
AVPacketPtr alloc_avpacket() {
85-
AVPacket* p = FFMPEG av_packet_alloc();
78+
AVPacket* p = av_packet_alloc();
8679
TORCH_CHECK(p, "Failed to allocate AVPacket object.");
8780
return AVPacketPtr{p};
8881
}
@@ -92,7 +85,7 @@ AVPacketPtr alloc_avpacket() {
9285
////////////////////////////////////////////////////////////////////////////////
9386
AutoPacketUnref::AutoPacketUnref(AVPacketPtr& p) : p_(p){};
9487
AutoPacketUnref::~AutoPacketUnref() {
95-
FFMPEG av_packet_unref(p_);
88+
av_packet_unref(p_);
9689
}
9790
AutoPacketUnref::operator AVPacket*() const {
9891
return p_;
@@ -102,13 +95,13 @@ AutoPacketUnref::operator AVPacket*() const {
10295
// AVFrame
10396
////////////////////////////////////////////////////////////////////////////////
10497
void AVFrameDeleter::operator()(AVFrame* p) {
105-
FFMPEG av_frame_free(&p);
98+
av_frame_free(&p);
10699
};
107100

108101
AVFramePtr::AVFramePtr(AVFrame* p) : Wrapper<AVFrame, AVFrameDeleter>(p) {}
109102

110103
AVFramePtr alloc_avframe() {
111-
AVFrame* p = FFMPEG av_frame_alloc();
104+
AVFrame* p = av_frame_alloc();
112105
TORCH_CHECK(p, "Failed to allocate AVFrame object.");
113106
return AVFramePtr{p};
114107
};
@@ -117,7 +110,7 @@ AVFramePtr alloc_avframe() {
117110
// AVCodecContext
118111
////////////////////////////////////////////////////////////////////////////////
119112
void AVCodecContextDeleter::operator()(AVCodecContext* p) {
120-
FFMPEG avcodec_free_context(&p);
113+
avcodec_free_context(&p);
121114
};
122115

123116
AVCodecContextPtr::AVCodecContextPtr(AVCodecContext* p)
@@ -127,7 +120,7 @@ AVCodecContextPtr::AVCodecContextPtr(AVCodecContext* p)
127120
// AVBufferRefPtr
128121
////////////////////////////////////////////////////////////////////////////////
129122
void AutoBufferUnref::operator()(AVBufferRef* p) {
130-
FFMPEG av_buffer_unref(&p);
123+
av_buffer_unref(&p);
131124
}
132125

133126
AVBufferRefPtr::AVBufferRefPtr(AVBufferRef* p)
@@ -137,7 +130,7 @@ AVBufferRefPtr::AVBufferRefPtr(AVBufferRef* p)
137130
// AVFilterGraph
138131
////////////////////////////////////////////////////////////////////////////////
139132
void AVFilterGraphDeleter::operator()(AVFilterGraph* p) {
140-
FFMPEG avfilter_graph_free(&p);
133+
avfilter_graph_free(&p);
141134
};
142135

143136
AVFilterGraphPtr::AVFilterGraphPtr(AVFilterGraph* p)
@@ -147,7 +140,7 @@ AVFilterGraphPtr::AVFilterGraphPtr(AVFilterGraph* p)
147140
// AVCodecParameters
148141
////////////////////////////////////////////////////////////////////////////////
149142
void AVCodecParametersDeleter::operator()(AVCodecParameters* codecpar) {
150-
FFMPEG avcodec_parameters_free(&codecpar);
143+
avcodec_parameters_free(&codecpar);
151144
}
152145

153146
AVCodecParametersPtr::AVCodecParametersPtr(AVCodecParameters* p)

torchaudio/csrc/ffmpeg/ffmpeg.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ using OptionDict = std::map<std::string, std::string>;
4141
// Replacement of av_err2str, which causes
4242
// `error: taking address of temporary array`
4343
// https://github.com/joncampbell123/composite-video-simulator/issues/5
44-
std::string av_err2string(int errnum);
44+
av_always_inline std::string av_err2string(int errnum) {
45+
char str[AV_ERROR_MAX_STRING_SIZE];
46+
return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);
47+
}
4548

4649
// Base structure that handles memory management.
4750
// Resource is freed by the destructor of unique_ptr,

torchaudio/csrc/ffmpeg/filter_graph.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include <torchaudio/csrc/ffmpeg/filter_graph.h>
2-
#include <torchaudio/csrc/ffmpeg/stub.h>
32
#include <stdexcept>
43

54
namespace torchaudio::io {
65

76
namespace {
87
AVFilterGraph* get_filter_graph() {
9-
AVFilterGraph* ptr = FFMPEG avfilter_graph_alloc();
8+
AVFilterGraph* ptr = avfilter_graph_alloc();
109
TORCH_CHECK(ptr, "Failed to allocate resouce.");
1110
ptr->nb_threads = 1;
1211
return ptr;
@@ -32,7 +31,7 @@ std::string get_audio_src_args(
3231
time_base.num,
3332
time_base.den,
3433
sample_rate,
35-
FFMPEG av_get_sample_fmt_name(format),
34+
av_get_sample_fmt_name(format),
3635
channel_layout);
3736
return std::string(args);
3837
}
@@ -51,7 +50,7 @@ std::string get_video_src_args(
5150
"video_size=%dx%d:pix_fmt=%s:time_base=%d/%d:frame_rate=%d/%d:pixel_aspect=%d/%d",
5251
width,
5352
height,
54-
FFMPEG av_get_pix_fmt_name(format),
53+
av_get_pix_fmt_name(format),
5554
time_base.num,
5655
time_base.den,
5756
frame_rate.num,
@@ -69,7 +68,7 @@ void FilterGraph::add_audio_src(
6968
int sample_rate,
7069
uint64_t channel_layout) {
7170
add_src(
72-
FFMPEG avfilter_get_by_name("abuffer"),
71+
avfilter_get_by_name("abuffer"),
7372
get_audio_src_args(format, time_base, sample_rate, channel_layout));
7473
}
7574

@@ -81,13 +80,13 @@ void FilterGraph::add_video_src(
8180
int height,
8281
AVRational sample_aspect_ratio) {
8382
add_src(
84-
FFMPEG avfilter_get_by_name("buffer"),
83+
avfilter_get_by_name("buffer"),
8584
get_video_src_args(
8685
format, time_base, frame_rate, width, height, sample_aspect_ratio));
8786
}
8887

8988
void FilterGraph::add_src(const AVFilter* buffersrc, const std::string& args) {
90-
int ret = FFMPEG avfilter_graph_create_filter(
89+
int ret = avfilter_graph_create_filter(
9190
&buffersrc_ctx, buffersrc, "in", args.c_str(), nullptr, graph);
9291
TORCH_CHECK(
9392
ret >= 0,
@@ -96,11 +95,11 @@ void FilterGraph::add_src(const AVFilter* buffersrc, const std::string& args) {
9695
}
9796

9897
void FilterGraph::add_audio_sink() {
99-
add_sink(FFMPEG avfilter_get_by_name("abuffersink"));
98+
add_sink(avfilter_get_by_name("abuffersink"));
10099
}
101100

102101
void FilterGraph::add_video_sink() {
103-
add_sink(FFMPEG avfilter_get_by_name("buffersink"));
102+
add_sink(avfilter_get_by_name("buffersink"));
104103
}
105104

106105
void FilterGraph::add_sink(const AVFilter* buffersink) {
@@ -114,7 +113,7 @@ void FilterGraph::add_sink(const AVFilter* buffersink) {
114113
// According to the other example
115114
// https://ffmpeg.org/doxygen/4.1/filter_audio_8c-example.html
116115
// `abuffersink` should not take options, and this resolved issue.
117-
int ret = FFMPEG avfilter_graph_create_filter(
116+
int ret = avfilter_graph_create_filter(
118117
&buffersink_ctx, buffersink, "out", nullptr, nullptr, graph);
119118
TORCH_CHECK(ret >= 0, "Failed to create output filter.");
120119
}
@@ -131,15 +130,15 @@ class InOuts {
131130

132131
public:
133132
InOuts(const char* name, AVFilterContext* pCtx) {
134-
p = FFMPEG avfilter_inout_alloc();
133+
p = avfilter_inout_alloc();
135134
TORCH_CHECK(p, "Failed to allocate AVFilterInOut.");
136-
p->name = FFMPEG av_strdup(name);
135+
p->name = av_strdup(name);
137136
p->filter_ctx = pCtx;
138137
p->pad_idx = 0;
139138
p->next = nullptr;
140139
}
141140
~InOuts() {
142-
FFMPEG avfilter_inout_free(&p);
141+
avfilter_inout_free(&p);
143142
}
144143
operator AVFilterInOut**() {
145144
return &p;
@@ -156,7 +155,7 @@ void FilterGraph::add_process(const std::string& filter_description) {
156155
// If you are debugging this part of the code, you might get confused.
157156
InOuts in{"in", buffersrc_ctx}, out{"out", buffersink_ctx};
158157

159-
int ret = FFMPEG avfilter_graph_parse_ptr(
158+
int ret = avfilter_graph_parse_ptr(
160159
graph, filter_description.c_str(), out, in, nullptr);
161160

162161
TORCH_CHECK(
@@ -167,11 +166,11 @@ void FilterGraph::add_process(const std::string& filter_description) {
167166

168167
void FilterGraph::create_filter(AVBufferRef* hw_frames_ctx) {
169168
buffersrc_ctx->outputs[0]->hw_frames_ctx = hw_frames_ctx;
170-
int ret = FFMPEG avfilter_graph_config(graph, nullptr);
169+
int ret = avfilter_graph_config(graph, nullptr);
171170
TORCH_CHECK(ret >= 0, "Failed to configure the graph: " + av_err2string(ret));
172-
// char* desc = FFMPEG avfilter_graph_dump(graph, NULL);
171+
// char* desc = avfilter_graph_dump(graph, NULL);
173172
// std::cerr << "Filter created:\n" << desc << std::endl;
174-
// FFMPEG av_free(static_cast<void*>(desc));
173+
// av_free(static_cast<void*>(desc));
175174
}
176175

177176
//////////////////////////////////////////////////////////////////////////////
@@ -191,8 +190,7 @@ FilterGraphOutputInfo FilterGraph::get_output_info() const {
191190
ret.num_channels = l->ch_layout.nb_channels;
192191
#else
193192
// Before FFmpeg 5.1
194-
ret.num_channels =
195-
FFMPEG av_get_channel_layout_nb_channels(l->channel_layout);
193+
ret.num_channels = av_get_channel_layout_nb_channels(l->channel_layout);
196194
#endif
197195
break;
198196
}
@@ -215,12 +213,12 @@ FilterGraphOutputInfo FilterGraph::get_output_info() const {
215213
// Streaming process
216214
//////////////////////////////////////////////////////////////////////////////
217215
int FilterGraph::add_frame(AVFrame* pInputFrame) {
218-
return FFMPEG av_buffersrc_add_frame_flags(
216+
return av_buffersrc_add_frame_flags(
219217
buffersrc_ctx, pInputFrame, AV_BUFFERSRC_FLAG_KEEP_REF);
220218
}
221219

222220
int FilterGraph::get_frame(AVFrame* pOutputFrame) {
223-
return FFMPEG av_buffersink_get_frame(buffersink_ctx, pOutputFrame);
221+
return av_buffersink_get_frame(buffersink_ctx, pOutputFrame);
224222
}
225223

226224
} // namespace torchaudio::io

torchaudio/csrc/ffmpeg/hw_context.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <torchaudio/csrc/ffmpeg/hw_context.h>
2-
#include <torchaudio/csrc/ffmpeg/stub.h>
32

43
namespace torchaudio::io {
54
namespace {
@@ -16,7 +15,7 @@ AVBufferRef* get_cuda_context(int index) {
1615
}
1716
if (CUDA_CONTEXT_CACHE.count(index) == 0) {
1817
AVBufferRef* p = nullptr;
19-
int ret = FFMPEG av_hwdevice_ctx_create(
18+
int ret = av_hwdevice_ctx_create(
2019
&p, AV_HWDEVICE_TYPE_CUDA, std::to_string(index).c_str(), nullptr, 0);
2120
TORCH_CHECK(
2221
ret >= 0,

0 commit comments

Comments
 (0)