Skip to content

Commit 9e31132

Browse files
author
Daniel Flores
committed
handle deprecated filter ctx function
1 parent a132fe5 commit 9e31132

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <c10/util/Exception.h>
1010

11+
extern "C" {
12+
#include <libavfilter/avfilter.h>
13+
#include <libavfilter/buffersink.h>
14+
}
15+
1116
namespace facebook::torchcodec {
1217

1318
AutoAVPacket::AutoAVPacket() : avPacket_(av_packet_alloc()) {
@@ -374,6 +379,63 @@ SwrContext* createSwrContext(
374379
return swrContext;
375380
}
376381

382+
AVFilterContext* createBuffersinkFilter(
383+
AVFilterGraph* filterGraph,
384+
const char* name,
385+
enum AVPixelFormat outputFormat) {
386+
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
387+
if (!buffersink) {
388+
return nullptr;
389+
}
390+
391+
AVFilterContext* sinkContext = nullptr;
392+
int status;
393+
394+
// av_opt_set_int_list was replaced by av_opt_set() in FFmpeg 8.
395+
#if LIBAVUTIL_VERSION_MAJOR >= 60 // FFmpeg >= 8
396+
// Output options like pixel_formats must be set before filter init
397+
sinkContext = avfilter_graph_alloc_filter(filterGraph, buffersink, name);
398+
if (!sinkContext) {
399+
return nullptr;
400+
}
401+
// av_opt_set uses the string representation of a pixel format
402+
const char* fmt_name = av_get_pix_fmt_name(outputFormat);
403+
if (!fmt_name) {
404+
return nullptr;
405+
}
406+
status = av_opt_set(
407+
sinkContext, "pixel_formats", fmt_name, AV_OPT_SEARCH_CHILDREN);
408+
if (status < 0) {
409+
return nullptr;
410+
}
411+
status = avfilter_init_str(sinkContext, nullptr);
412+
if (status < 0) {
413+
return nullptr;
414+
}
415+
#else // FFmpeg <= 7
416+
// For older FFmpeg versions, create filter and then set options
417+
status = avfilter_graph_create_filter(
418+
&sinkContext, buffersink, name, nullptr, nullptr, filterGraph);
419+
if (status < 0) {
420+
return nullptr;
421+
}
422+
423+
enum AVPixelFormat pix_fmts[] = {outputFormat, AV_PIX_FMT_NONE};
424+
425+
status = av_opt_set_int_list(
426+
sinkContext,
427+
"pix_fmts",
428+
pix_fmts,
429+
AV_PIX_FMT_NONE,
430+
AV_OPT_SEARCH_CHILDREN);
431+
if (status < 0) {
432+
return nullptr;
433+
}
434+
#endif
435+
436+
return sinkContext;
437+
}
438+
377439
UniqueAVFrame convertAudioAVFrameSamples(
378440
const UniqueSwrContext& swrContext,
379441
const UniqueAVFrame& srcAVFrame,

src/torchcodec/_core/FFMPEGCommon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
#include <libavutil/display.h>
2424
#include <libavutil/file.h>
2525
#include <libavutil/opt.h>
26+
#include <libavutil/pixdesc.h>
2627
#include <libavutil/pixfmt.h>
2728
#include <libavutil/version.h>
2829
#include <libswresample/swresample.h>
@@ -237,4 +238,9 @@ int64_t computeSafeDuration(
237238
const AVRational& frameRate,
238239
const AVRational& timeBase);
239240

241+
AVFilterContext* createBuffersinkFilter(
242+
AVFilterGraph* filterGraph,
243+
const char* name,
244+
enum AVPixelFormat outputFormat);
245+
240246
} // namespace facebook::torchcodec

src/torchcodec/_core/FilterGraph.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// LICENSE file in the root directory of this source tree.
66

77
#include "src/torchcodec/_core/FilterGraph.h"
8+
#include "src/torchcodec/_core/FFMPEGCommon.h"
89

910
extern "C" {
1011
#include <libavfilter/buffersink.h>
@@ -63,7 +64,6 @@ FilterGraph::FilterGraph(
6364
}
6465

6566
const AVFilter* buffersrc = avfilter_get_by_name("buffer");
66-
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
6767

6868
UniqueAVBufferSrcParameters srcParams(av_buffersrc_parameters_alloc());
6969
TORCH_CHECK(srcParams, "Failed to allocate buffersrc params");
@@ -93,26 +93,10 @@ FilterGraph::FilterGraph(
9393
"Failed to create filter graph : ",
9494
getFFMPEGErrorStringFromErrorCode(status));
9595

96-
status = avfilter_graph_create_filter(
97-
&sinkContext_, buffersink, "out", nullptr, nullptr, filterGraph_.get());
96+
sinkContext_ = createBuffersinkFilter(
97+
filterGraph_.get(), "out", filtersContext.outputFormat);
9898
TORCH_CHECK(
99-
status >= 0,
100-
"Failed to create filter graph: ",
101-
getFFMPEGErrorStringFromErrorCode(status));
102-
103-
enum AVPixelFormat pix_fmts[] = {
104-
filtersContext.outputFormat, AV_PIX_FMT_NONE};
105-
106-
status = av_opt_set_int_list(
107-
sinkContext_,
108-
"pix_fmts",
109-
pix_fmts,
110-
AV_PIX_FMT_NONE,
111-
AV_OPT_SEARCH_CHILDREN);
112-
TORCH_CHECK(
113-
status >= 0,
114-
"Failed to set output pixel formats: ",
115-
getFFMPEGErrorStringFromErrorCode(status));
99+
sinkContext_ != nullptr, "Failed to create and configure buffersink");
116100

117101
UniqueAVFilterInOut outputs(avfilter_inout_alloc());
118102
UniqueAVFilterInOut inputs(avfilter_inout_alloc());

0 commit comments

Comments
 (0)